DFGVariableAccessData.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (C) 2011, 2012, 2013 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. #ifndef DFGVariableAccessData_h
  26. #define DFGVariableAccessData_h
  27. #include "DFGDoubleFormatState.h"
  28. #include "DFGNodeFlags.h"
  29. #include "Operands.h"
  30. #include "SpeculatedType.h"
  31. #include "VirtualRegister.h"
  32. #include <wtf/Platform.h>
  33. #include <wtf/UnionFind.h>
  34. #include <wtf/Vector.h>
  35. namespace JSC { namespace DFG {
  36. enum DoubleBallot { VoteValue, VoteDouble };
  37. class VariableAccessData : public UnionFind<VariableAccessData> {
  38. public:
  39. VariableAccessData()
  40. : m_local(static_cast<VirtualRegister>(std::numeric_limits<int>::min()))
  41. , m_prediction(SpecNone)
  42. , m_argumentAwarePrediction(SpecNone)
  43. , m_flags(0)
  44. , m_isCaptured(false)
  45. , m_shouldNeverUnbox(false)
  46. , m_isArgumentsAlias(false)
  47. , m_structureCheckHoistingFailed(false)
  48. , m_isProfitableToUnbox(false)
  49. , m_isLoadedFrom(false)
  50. , m_doubleFormatState(EmptyDoubleFormatState)
  51. {
  52. clearVotes();
  53. }
  54. VariableAccessData(VirtualRegister local, bool isCaptured)
  55. : m_local(local)
  56. , m_prediction(SpecNone)
  57. , m_argumentAwarePrediction(SpecNone)
  58. , m_flags(0)
  59. , m_isCaptured(isCaptured)
  60. , m_shouldNeverUnbox(isCaptured)
  61. , m_isArgumentsAlias(false)
  62. , m_structureCheckHoistingFailed(false)
  63. , m_isProfitableToUnbox(false)
  64. , m_doubleFormatState(EmptyDoubleFormatState)
  65. {
  66. clearVotes();
  67. }
  68. VirtualRegister local()
  69. {
  70. ASSERT(m_local == find()->m_local);
  71. return m_local;
  72. }
  73. int operand()
  74. {
  75. return static_cast<int>(local());
  76. }
  77. bool mergeIsCaptured(bool isCaptured)
  78. {
  79. return checkAndSet(m_shouldNeverUnbox, m_shouldNeverUnbox | isCaptured)
  80. | checkAndSet(m_isCaptured, m_isCaptured | isCaptured);
  81. }
  82. bool isCaptured()
  83. {
  84. return m_isCaptured;
  85. }
  86. bool mergeIsProfitableToUnbox(bool isProfitableToUnbox)
  87. {
  88. return checkAndSet(m_isProfitableToUnbox, m_isProfitableToUnbox | isProfitableToUnbox);
  89. }
  90. bool isProfitableToUnbox()
  91. {
  92. return m_isProfitableToUnbox;
  93. }
  94. bool mergeShouldNeverUnbox(bool shouldNeverUnbox)
  95. {
  96. bool newShouldNeverUnbox = m_shouldNeverUnbox | shouldNeverUnbox;
  97. if (newShouldNeverUnbox == m_shouldNeverUnbox)
  98. return false;
  99. m_shouldNeverUnbox = newShouldNeverUnbox;
  100. return true;
  101. }
  102. // Returns true if it would be unsound to store the value in an unboxed fashion.
  103. // If this returns false, it simply means that it is sound to unbox; it doesn't
  104. // mean that we have actually done so.
  105. bool shouldNeverUnbox()
  106. {
  107. ASSERT(!(m_isCaptured && !m_shouldNeverUnbox));
  108. return m_shouldNeverUnbox;
  109. }
  110. // Returns true if we should be unboxing the value provided that the predictions
  111. // and double format vote say so. This may return false even if shouldNeverUnbox()
  112. // returns false, since this incorporates heuristics of profitability.
  113. bool shouldUnboxIfPossible()
  114. {
  115. return !shouldNeverUnbox() && isProfitableToUnbox();
  116. }
  117. bool mergeStructureCheckHoistingFailed(bool failed)
  118. {
  119. return checkAndSet(m_structureCheckHoistingFailed, m_structureCheckHoistingFailed | failed);
  120. }
  121. bool structureCheckHoistingFailed()
  122. {
  123. return m_structureCheckHoistingFailed;
  124. }
  125. bool mergeIsArgumentsAlias(bool isArgumentsAlias)
  126. {
  127. return checkAndSet(m_isArgumentsAlias, m_isArgumentsAlias | isArgumentsAlias);
  128. }
  129. bool isArgumentsAlias()
  130. {
  131. return m_isArgumentsAlias;
  132. }
  133. bool mergeIsLoadedFrom(bool isLoadedFrom)
  134. {
  135. return checkAndSet(m_isLoadedFrom, m_isLoadedFrom | isLoadedFrom);
  136. }
  137. void setIsLoadedFrom(bool isLoadedFrom)
  138. {
  139. m_isLoadedFrom = isLoadedFrom;
  140. }
  141. bool isLoadedFrom()
  142. {
  143. return m_isLoadedFrom;
  144. }
  145. bool predict(SpeculatedType prediction)
  146. {
  147. VariableAccessData* self = find();
  148. bool result = mergeSpeculation(self->m_prediction, prediction);
  149. if (result)
  150. mergeSpeculation(m_argumentAwarePrediction, m_prediction);
  151. return result;
  152. }
  153. SpeculatedType nonUnifiedPrediction()
  154. {
  155. return m_prediction;
  156. }
  157. SpeculatedType prediction()
  158. {
  159. return find()->m_prediction;
  160. }
  161. SpeculatedType argumentAwarePrediction()
  162. {
  163. return find()->m_argumentAwarePrediction;
  164. }
  165. bool mergeArgumentAwarePrediction(SpeculatedType prediction)
  166. {
  167. return mergeSpeculation(find()->m_argumentAwarePrediction, prediction);
  168. }
  169. void clearVotes()
  170. {
  171. ASSERT(find() == this);
  172. m_votes[0] = 0;
  173. m_votes[1] = 0;
  174. }
  175. void vote(unsigned ballot)
  176. {
  177. ASSERT(ballot < 2);
  178. m_votes[ballot]++;
  179. }
  180. double voteRatio()
  181. {
  182. ASSERT(find() == this);
  183. return static_cast<double>(m_votes[1]) / m_votes[0];
  184. }
  185. bool shouldUseDoubleFormatAccordingToVote()
  186. {
  187. // We don't support this facility for arguments, yet.
  188. // FIXME: make this work for arguments.
  189. if (operandIsArgument(operand()))
  190. return false;
  191. // If the variable is not a number prediction, then this doesn't
  192. // make any sense.
  193. if (!isNumberSpeculation(prediction())) {
  194. // FIXME: we may end up forcing a local in inlined argument position to be a double even
  195. // if it is sometimes not even numeric, since this never signals the fact that it doesn't
  196. // want doubles. https://bugs.webkit.org/show_bug.cgi?id=109511
  197. return false;
  198. }
  199. // If the variable is predicted to hold only doubles, then it's a
  200. // no-brainer: it should be formatted as a double.
  201. if (isDoubleSpeculation(prediction()))
  202. return true;
  203. // If the variable is known to be used as an integer, then be safe -
  204. // don't force it to be a double.
  205. if (flags() & NodeUsedAsInt)
  206. return false;
  207. // If the variable has been voted to become a double, then make it a
  208. // double.
  209. if (voteRatio() >= Options::doubleVoteRatioForDoubleFormat())
  210. return true;
  211. return false;
  212. }
  213. DoubleFormatState doubleFormatState()
  214. {
  215. return find()->m_doubleFormatState;
  216. }
  217. bool shouldUseDoubleFormat()
  218. {
  219. ASSERT(isRoot());
  220. bool doubleState = m_doubleFormatState == UsingDoubleFormat;
  221. ASSERT(!(doubleState && shouldNeverUnbox()));
  222. ASSERT(!(doubleState && isCaptured()));
  223. return doubleState && isProfitableToUnbox();
  224. }
  225. bool tallyVotesForShouldUseDoubleFormat()
  226. {
  227. ASSERT(isRoot());
  228. if (operandIsArgument(local()) || shouldNeverUnbox())
  229. return DFG::mergeDoubleFormatState(m_doubleFormatState, NotUsingDoubleFormat);
  230. if (m_doubleFormatState == CantUseDoubleFormat)
  231. return false;
  232. bool newValueOfShouldUseDoubleFormat = shouldUseDoubleFormatAccordingToVote();
  233. if (!newValueOfShouldUseDoubleFormat) {
  234. // We monotonically convert to double. Hence, if the fixpoint leads us to conclude that we should
  235. // switch back to int, we instead ignore this and stick with double.
  236. return false;
  237. }
  238. if (m_doubleFormatState == UsingDoubleFormat)
  239. return false;
  240. return DFG::mergeDoubleFormatState(m_doubleFormatState, UsingDoubleFormat);
  241. }
  242. bool mergeDoubleFormatState(DoubleFormatState doubleFormatState)
  243. {
  244. return DFG::mergeDoubleFormatState(find()->m_doubleFormatState, doubleFormatState);
  245. }
  246. bool makePredictionForDoubleFormat()
  247. {
  248. ASSERT(isRoot());
  249. if (m_doubleFormatState != UsingDoubleFormat)
  250. return false;
  251. return mergeSpeculation(m_prediction, SpecDouble);
  252. }
  253. NodeFlags flags() const { return m_flags; }
  254. bool mergeFlags(NodeFlags newFlags)
  255. {
  256. return checkAndSet(m_flags, m_flags | newFlags);
  257. }
  258. private:
  259. // This is slightly space-inefficient, since anything we're unified with
  260. // will have the same operand and should have the same prediction. But
  261. // putting them here simplifies the code, and we don't expect DFG space
  262. // usage for variable access nodes do be significant.
  263. VirtualRegister m_local;
  264. SpeculatedType m_prediction;
  265. SpeculatedType m_argumentAwarePrediction;
  266. NodeFlags m_flags;
  267. bool m_isCaptured;
  268. bool m_shouldNeverUnbox;
  269. bool m_isArgumentsAlias;
  270. bool m_structureCheckHoistingFailed;
  271. bool m_isProfitableToUnbox;
  272. bool m_isLoadedFrom;
  273. float m_votes[2]; // Used primarily for double voting but may be reused for other purposes.
  274. DoubleFormatState m_doubleFormatState;
  275. };
  276. } } // namespace JSC::DFG
  277. #endif // DFGVariableAccessData_h