2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "DFGVariableAccessData.h"
31 namespace JSC
{ namespace DFG
{
33 VariableAccessData::VariableAccessData()
34 : m_local(static_cast<VirtualRegister
>(std::numeric_limits
<int>::min()))
35 , m_prediction(SpecNone
)
36 , m_argumentAwarePrediction(SpecNone
)
39 , m_shouldNeverUnbox(false)
40 , m_isArgumentsAlias(false)
41 , m_structureCheckHoistingFailed(false)
42 , m_checkArrayHoistingFailed(false)
43 , m_isProfitableToUnbox(false)
44 , m_isLoadedFrom(false)
45 , m_doubleFormatState(EmptyDoubleFormatState
)
50 VariableAccessData::VariableAccessData(VirtualRegister local
, bool isCaptured
)
52 , m_prediction(SpecNone
)
53 , m_argumentAwarePrediction(SpecNone
)
55 , m_isCaptured(isCaptured
)
56 , m_shouldNeverUnbox(isCaptured
)
57 , m_isArgumentsAlias(false)
58 , m_structureCheckHoistingFailed(false)
59 , m_checkArrayHoistingFailed(false)
60 , m_isProfitableToUnbox(false)
61 , m_isLoadedFrom(false)
62 , m_doubleFormatState(EmptyDoubleFormatState
)
67 bool VariableAccessData::mergeIsCaptured(bool isCaptured
)
69 return checkAndSet(m_shouldNeverUnbox
, m_shouldNeverUnbox
| isCaptured
)
70 | checkAndSet(m_isCaptured
, m_isCaptured
| isCaptured
);
73 bool VariableAccessData::mergeShouldNeverUnbox(bool shouldNeverUnbox
)
75 bool newShouldNeverUnbox
= m_shouldNeverUnbox
| shouldNeverUnbox
;
76 if (newShouldNeverUnbox
== m_shouldNeverUnbox
)
78 m_shouldNeverUnbox
= newShouldNeverUnbox
;
82 bool VariableAccessData::predict(SpeculatedType prediction
)
84 VariableAccessData
* self
= find();
85 bool result
= mergeSpeculation(self
->m_prediction
, prediction
);
87 mergeSpeculation(m_argumentAwarePrediction
, m_prediction
);
91 bool VariableAccessData::mergeArgumentAwarePrediction(SpeculatedType prediction
)
93 return mergeSpeculation(find()->m_argumentAwarePrediction
, prediction
);
96 bool VariableAccessData::shouldUseDoubleFormatAccordingToVote()
98 // We don't support this facility for arguments, yet.
99 // FIXME: make this work for arguments.
100 if (local().isArgument())
103 // If the variable is not a number prediction, then this doesn't
105 if (!isFullNumberSpeculation(prediction())) {
106 // FIXME: we may end up forcing a local in inlined argument position to be a double even
107 // if it is sometimes not even numeric, since this never signals the fact that it doesn't
108 // want doubles. https://bugs.webkit.org/show_bug.cgi?id=109511
112 // If the variable is predicted to hold only doubles, then it's a
113 // no-brainer: it should be formatted as a double.
114 if (isDoubleSpeculation(prediction()))
117 // If the variable is known to be used as an integer, then be safe -
118 // don't force it to be a double.
119 if (flags() & NodeBytecodeUsesAsInt
)
122 // If the variable has been voted to become a double, then make it a
124 if (voteRatio() >= Options::doubleVoteRatioForDoubleFormat())
130 bool VariableAccessData::tallyVotesForShouldUseDoubleFormat()
134 if (local().isArgument() || shouldNeverUnbox()
135 || (flags() & NodeBytecodeUsesAsArrayIndex
))
136 return DFG::mergeDoubleFormatState(m_doubleFormatState
, NotUsingDoubleFormat
);
138 if (m_doubleFormatState
== CantUseDoubleFormat
)
141 bool newValueOfShouldUseDoubleFormat
= shouldUseDoubleFormatAccordingToVote();
142 if (!newValueOfShouldUseDoubleFormat
) {
143 // We monotonically convert to double. Hence, if the fixpoint leads us to conclude that we should
144 // switch back to int, we instead ignore this and stick with double.
148 if (m_doubleFormatState
== UsingDoubleFormat
)
151 return DFG::mergeDoubleFormatState(m_doubleFormatState
, UsingDoubleFormat
);
154 bool VariableAccessData::mergeDoubleFormatState(DoubleFormatState doubleFormatState
)
156 return DFG::mergeDoubleFormatState(find()->m_doubleFormatState
, doubleFormatState
);
159 bool VariableAccessData::makePredictionForDoubleFormat()
163 if (m_doubleFormatState
!= UsingDoubleFormat
)
166 SpeculatedType type
= m_prediction
;
167 if (type
& ~SpecBytecodeNumber
)
168 type
|= SpecDoublePureNaN
;
169 if (type
& SpecMachineInt
)
170 type
|= SpecInt52AsDouble
;
171 return checkAndSet(m_prediction
, type
);
174 bool VariableAccessData::couldRepresentInt52()
176 if (shouldNeverUnbox())
179 return couldRepresentInt52Impl();
182 bool VariableAccessData::couldRepresentInt52Impl()
184 // The hardware has to support it.
188 // We punt for machine arguments.
189 if (m_local
.isArgument())
192 // The argument-aware prediction -- which merges all of an (inlined or machine)
193 // argument's variable access datas' predictions -- must possibly be MachineInt.
194 return !(argumentAwarePrediction() & ~SpecMachineInt
);
197 FlushFormat
VariableAccessData::flushFormat()
199 ASSERT(find() == this);
201 if (isArgumentsAlias())
202 return FlushedArguments
;
204 if (!shouldUnboxIfPossible())
205 return FlushedJSValue
;
207 if (shouldUseDoubleFormat())
208 return FlushedDouble
;
210 SpeculatedType prediction
= argumentAwarePrediction();
212 // This guard is here to protect the call to couldRepresentInt52(), which will return
213 // true for !prediction.
215 return FlushedJSValue
;
217 if (isInt32Speculation(prediction
))
220 if (couldRepresentInt52Impl())
223 if (isCellSpeculation(prediction
))
226 if (isBooleanSpeculation(prediction
))
227 return FlushedBoolean
;
229 return FlushedJSValue
;
232 } } // namespace JSC::DFG
234 #endif // ENABLE(DFG_JIT)