1/*
2 * Copyright (C) 2012, 2013, 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "DFGDoubleFormatState.h"
29#include "DFGVariableAccessData.h"
30#include "DFGVariableAccessDataDump.h"
31#include "SpeculatedType.h"
32
33namespace JSC { namespace DFG {
34
35class ArgumentPosition {
36public:
37 ArgumentPosition()
38 : m_prediction(SpecNone)
39 , m_doubleFormatState(EmptyDoubleFormatState)
40 , m_isProfitableToUnbox(false)
41 , m_shouldNeverUnbox(false)
42 {
43 }
44
45 void addVariable(VariableAccessData* variable)
46 {
47 m_variables.append(variable);
48
49 // We may set this early. Merging it here saves us time in prediction propagation.
50 variable->mergeShouldNeverUnbox(m_shouldNeverUnbox);
51 }
52
53 VariableAccessData* someVariable() const
54 {
55 if (m_variables.isEmpty())
56 return 0;
57 return m_variables[0]->find();
58 }
59
60 FlushFormat flushFormat() const
61 {
62 if (VariableAccessData* variable = someVariable())
63 return variable->flushFormat();
64 return DeadFlush;
65 }
66
67 bool mergeShouldNeverUnbox(bool shouldNeverUnbox)
68 {
69 return checkAndSet(m_shouldNeverUnbox, m_shouldNeverUnbox || shouldNeverUnbox);
70 }
71
72 bool mergeArgumentPredictionAwareness()
73 {
74 bool changed = false;
75 for (unsigned i = 0; i < m_variables.size(); ++i) {
76 VariableAccessData* variable = m_variables[i]->find();
77 changed |= mergeSpeculation(m_prediction, variable->argumentAwarePrediction());
78 changed |= mergeDoubleFormatState(m_doubleFormatState, variable->doubleFormatState());
79 changed |= mergeShouldNeverUnbox(variable->shouldNeverUnbox());
80 }
81 if (!changed)
82 return false;
83 changed = false;
84 for (unsigned i = 0; i < m_variables.size(); ++i) {
85 VariableAccessData* variable = m_variables[i]->find();
86 changed |= variable->mergeArgumentAwarePrediction(m_prediction);
87 changed |= variable->mergeDoubleFormatState(m_doubleFormatState);
88 changed |= variable->mergeShouldNeverUnbox(m_shouldNeverUnbox);
89 }
90 return changed;
91 }
92
93 bool mergeArgumentUnboxingAwareness()
94 {
95 bool changed = false;
96 for (unsigned i = 0; i < m_variables.size(); ++i) {
97 VariableAccessData* variable = m_variables[i]->find();
98 changed |= checkAndSet(m_isProfitableToUnbox, m_isProfitableToUnbox || variable->isProfitableToUnbox());
99 }
100 if (!changed)
101 return false;
102 changed = false;
103 for (unsigned i = 0; i < m_variables.size(); ++i) {
104 VariableAccessData* variable = m_variables[i]->find();
105 changed |= variable->mergeIsProfitableToUnbox(m_isProfitableToUnbox);
106 }
107 return changed;
108 }
109
110 bool shouldUnboxIfPossible() const { return m_isProfitableToUnbox && !m_shouldNeverUnbox; }
111
112 SpeculatedType prediction() const { return m_prediction; }
113 DoubleFormatState doubleFormatState() const { return m_doubleFormatState; }
114 bool shouldUseDoubleFormat() const
115 {
116 return doubleFormatState() == UsingDoubleFormat && shouldUnboxIfPossible();
117 }
118
119 void dump(PrintStream& out, Graph* graph)
120 {
121 for (unsigned i = 0; i < m_variables.size(); ++i) {
122 VariableAccessData* variable = m_variables[i]->find();
123 VirtualRegister operand = variable->local();
124
125 if (i)
126 out.print(" ");
127
128 out.print(operand, "(", VariableAccessDataDump(*graph, variable), ")");
129 }
130 out.print("\n");
131 }
132
133private:
134 SpeculatedType m_prediction;
135 DoubleFormatState m_doubleFormatState;
136 bool m_isProfitableToUnbox;
137 bool m_shouldNeverUnbox;
138
139 Vector<VariableAccessData*, 2> m_variables;
140};
141
142} } // namespace JSC::DFG
143