DFGBackwardsPropagationPhase.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (C) 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. #include "config.h"
  26. #include "DFGBackwardsPropagationPhase.h"
  27. #if ENABLE(DFG_JIT)
  28. #include "DFGBasicBlockInlines.h"
  29. #include "DFGGraph.h"
  30. #include "DFGPhase.h"
  31. #include "Operations.h"
  32. namespace JSC { namespace DFG {
  33. class BackwardsPropagationPhase : public Phase {
  34. public:
  35. BackwardsPropagationPhase(Graph& graph)
  36. : Phase(graph, "backwards propagation")
  37. {
  38. }
  39. bool run()
  40. {
  41. for (BlockIndex blockIndex = 0; blockIndex < m_graph.m_blocks.size(); ++blockIndex) {
  42. BasicBlock* block = m_graph.m_blocks[blockIndex].get();
  43. if (!block)
  44. continue;
  45. // Prevent a tower of overflowing additions from creating a value that is out of the
  46. // safe 2^48 range.
  47. m_allowNestedOverflowingAdditions = block->size() < (1 << 16);
  48. for (unsigned indexInBlock = block->size(); indexInBlock--;)
  49. propagate(block->at(indexInBlock));
  50. }
  51. return true;
  52. }
  53. private:
  54. bool isNotNegZero(Node* node)
  55. {
  56. if (!m_graph.isNumberConstant(node))
  57. return false;
  58. double value = m_graph.valueOfNumberConstant(node);
  59. return (value || 1.0 / value > 0.0);
  60. }
  61. bool isNotPosZero(Node* node)
  62. {
  63. if (!m_graph.isNumberConstant(node))
  64. return false;
  65. double value = m_graph.valueOfNumberConstant(node);
  66. return (value || 1.0 / value < 0.0);
  67. }
  68. // Tests if the absolute value is strictly less than the power of two.
  69. template<int power>
  70. bool isWithinPowerOfTwoForConstant(Node* node)
  71. {
  72. JSValue immediateValue = node->valueOfJSConstant(codeBlock());
  73. if (!immediateValue.isNumber())
  74. return false;
  75. double immediate = immediateValue.asNumber();
  76. return immediate > -(static_cast<int64_t>(1) << power) && immediate < (static_cast<int64_t>(1) << power);
  77. }
  78. template<int power>
  79. bool isWithinPowerOfTwoNonRecursive(Node* node)
  80. {
  81. if (node->op() != JSConstant)
  82. return false;
  83. return isWithinPowerOfTwoForConstant<power>(node);
  84. }
  85. template<int power>
  86. bool isWithinPowerOfTwo(Node* node)
  87. {
  88. switch (node->op()) {
  89. case JSConstant: {
  90. return isWithinPowerOfTwoForConstant<power>(node);
  91. }
  92. case BitAnd: {
  93. if (power > 31)
  94. return true;
  95. return isWithinPowerOfTwoNonRecursive<power>(node->child1().node())
  96. || isWithinPowerOfTwoNonRecursive<power>(node->child2().node());
  97. }
  98. case BitOr:
  99. case BitXor:
  100. case BitLShift:
  101. case ValueToInt32: {
  102. return power > 31;
  103. }
  104. case BitRShift:
  105. case BitURShift: {
  106. if (power > 31)
  107. return true;
  108. Node* shiftAmount = node->child2().node();
  109. if (shiftAmount->op() != JSConstant)
  110. return false;
  111. JSValue immediateValue = shiftAmount->valueOfJSConstant(codeBlock());
  112. if (!immediateValue.isInt32())
  113. return false;
  114. return immediateValue.asInt32() > 32 - power;
  115. }
  116. default:
  117. return false;
  118. }
  119. }
  120. template<int power>
  121. bool isWithinPowerOfTwo(Edge edge)
  122. {
  123. return isWithinPowerOfTwo<power>(edge.node());
  124. }
  125. bool mergeDefaultFlags(Node* node)
  126. {
  127. bool changed = false;
  128. if (node->flags() & NodeHasVarArgs) {
  129. for (unsigned childIdx = node->firstChild();
  130. childIdx < node->firstChild() + node->numChildren();
  131. childIdx++) {
  132. if (!!m_graph.m_varArgChildren[childIdx])
  133. changed |= m_graph.m_varArgChildren[childIdx]->mergeFlags(NodeUsedAsValue);
  134. }
  135. } else {
  136. if (!node->child1())
  137. return changed;
  138. changed |= node->child1()->mergeFlags(NodeUsedAsValue);
  139. if (!node->child2())
  140. return changed;
  141. changed |= node->child2()->mergeFlags(NodeUsedAsValue);
  142. if (!node->child3())
  143. return changed;
  144. changed |= node->child3()->mergeFlags(NodeUsedAsValue);
  145. }
  146. return changed;
  147. }
  148. void propagate(Node* node)
  149. {
  150. NodeFlags flags = node->flags() & NodeBackPropMask;
  151. switch (node->op()) {
  152. case GetLocal: {
  153. VariableAccessData* variableAccessData = node->variableAccessData();
  154. variableAccessData->mergeFlags(flags);
  155. break;
  156. }
  157. case SetLocal: {
  158. VariableAccessData* variableAccessData = node->variableAccessData();
  159. if (!variableAccessData->isLoadedFrom())
  160. break;
  161. node->child1()->mergeFlags(NodeUsedAsValue);
  162. break;
  163. }
  164. case BitAnd:
  165. case BitOr:
  166. case BitXor:
  167. case BitRShift:
  168. case BitLShift:
  169. case BitURShift:
  170. case ArithIMul: {
  171. flags |= NodeUsedAsInt;
  172. flags &= ~(NodeUsedAsNumber | NodeNeedsNegZero | NodeUsedAsOther);
  173. node->child1()->mergeFlags(flags);
  174. node->child2()->mergeFlags(flags);
  175. break;
  176. }
  177. case ValueToInt32: {
  178. flags |= NodeUsedAsInt;
  179. flags &= ~(NodeUsedAsNumber | NodeNeedsNegZero | NodeUsedAsOther);
  180. node->child1()->mergeFlags(flags);
  181. break;
  182. }
  183. case StringCharCodeAt: {
  184. node->child1()->mergeFlags(NodeUsedAsValue);
  185. node->child2()->mergeFlags(NodeUsedAsValue | NodeUsedAsInt);
  186. break;
  187. }
  188. case Identity:
  189. case UInt32ToNumber: {
  190. node->child1()->mergeFlags(flags);
  191. break;
  192. }
  193. case ValueAdd: {
  194. if (isNotNegZero(node->child1().node()) || isNotNegZero(node->child2().node()))
  195. flags &= ~NodeNeedsNegZero;
  196. if (node->child1()->hasNumberResult() || node->child2()->hasNumberResult())
  197. flags &= ~NodeUsedAsOther;
  198. if (!isWithinPowerOfTwo<32>(node->child1()) && !isWithinPowerOfTwo<32>(node->child2()))
  199. flags |= NodeUsedAsNumber;
  200. if (!m_allowNestedOverflowingAdditions)
  201. flags |= NodeUsedAsNumber;
  202. node->child1()->mergeFlags(flags);
  203. node->child2()->mergeFlags(flags);
  204. break;
  205. }
  206. case ArithAdd: {
  207. if (isNotNegZero(node->child1().node()) || isNotNegZero(node->child2().node()))
  208. flags &= ~NodeNeedsNegZero;
  209. if (!isWithinPowerOfTwo<32>(node->child1()) && !isWithinPowerOfTwo<32>(node->child2()))
  210. flags |= NodeUsedAsNumber;
  211. if (!m_allowNestedOverflowingAdditions)
  212. flags |= NodeUsedAsNumber;
  213. node->child1()->mergeFlags(flags);
  214. node->child2()->mergeFlags(flags);
  215. break;
  216. }
  217. case ArithSub: {
  218. if (isNotNegZero(node->child1().node()) || isNotPosZero(node->child2().node()))
  219. flags &= ~NodeNeedsNegZero;
  220. if (!isWithinPowerOfTwo<32>(node->child1()) && !isWithinPowerOfTwo<32>(node->child2()))
  221. flags |= NodeUsedAsNumber;
  222. if (!m_allowNestedOverflowingAdditions)
  223. flags |= NodeUsedAsNumber;
  224. node->child1()->mergeFlags(flags);
  225. node->child2()->mergeFlags(flags);
  226. break;
  227. }
  228. case ArithNegate: {
  229. flags &= ~NodeUsedAsOther;
  230. node->child1()->mergeFlags(flags);
  231. break;
  232. }
  233. case ArithMul: {
  234. // As soon as a multiply happens, we can easily end up in the part
  235. // of the double domain where the point at which you do truncation
  236. // can change the outcome. So, ArithMul always forces its inputs to
  237. // check for overflow. Additionally, it will have to check for overflow
  238. // itself unless we can prove that there is no way for the values
  239. // produced to cause double rounding.
  240. if (!isWithinPowerOfTwo<22>(node->child1().node())
  241. && !isWithinPowerOfTwo<22>(node->child2().node()))
  242. flags |= NodeUsedAsNumber;
  243. node->mergeFlags(flags);
  244. flags |= NodeUsedAsNumber | NodeNeedsNegZero;
  245. flags &= ~NodeUsedAsOther;
  246. node->child1()->mergeFlags(flags);
  247. node->child2()->mergeFlags(flags);
  248. break;
  249. }
  250. case ArithDiv: {
  251. flags |= NodeUsedAsNumber | NodeNeedsNegZero;
  252. flags &= ~NodeUsedAsOther;
  253. node->child1()->mergeFlags(flags);
  254. node->child2()->mergeFlags(flags);
  255. break;
  256. }
  257. case ArithMod: {
  258. flags |= NodeUsedAsNumber | NodeNeedsNegZero;
  259. flags &= ~NodeUsedAsOther;
  260. node->child1()->mergeFlags(flags);
  261. node->child2()->mergeFlags(flags);
  262. break;
  263. }
  264. case GetByVal: {
  265. node->child1()->mergeFlags(NodeUsedAsValue);
  266. node->child2()->mergeFlags(NodeUsedAsNumber | NodeUsedAsOther | NodeUsedAsInt);
  267. break;
  268. }
  269. case GetMyArgumentByValSafe: {
  270. node->child1()->mergeFlags(NodeUsedAsNumber | NodeUsedAsOther | NodeUsedAsInt);
  271. break;
  272. }
  273. case NewArrayWithSize: {
  274. node->child1()->mergeFlags(NodeUsedAsValue | NodeUsedAsInt);
  275. break;
  276. }
  277. case StringCharAt: {
  278. node->child1()->mergeFlags(NodeUsedAsValue);
  279. node->child2()->mergeFlags(NodeUsedAsValue | NodeUsedAsInt);
  280. break;
  281. }
  282. case ToString: {
  283. node->child1()->mergeFlags(NodeUsedAsNumber | NodeUsedAsOther);
  284. break;
  285. }
  286. case ToPrimitive: {
  287. node->child1()->mergeFlags(flags);
  288. break;
  289. }
  290. case PutByVal: {
  291. m_graph.varArgChild(node, 0)->mergeFlags(NodeUsedAsValue);
  292. m_graph.varArgChild(node, 1)->mergeFlags(NodeUsedAsNumber | NodeUsedAsOther | NodeUsedAsInt);
  293. m_graph.varArgChild(node, 2)->mergeFlags(NodeUsedAsValue);
  294. break;
  295. }
  296. default:
  297. mergeDefaultFlags(node);
  298. break;
  299. }
  300. }
  301. bool m_allowNestedOverflowingAdditions;
  302. };
  303. bool performBackwardsPropagation(Graph& graph)
  304. {
  305. SamplingRegion samplingRegion("DFG Backwards Propagation Phase");
  306. return runPhase<BackwardsPropagationPhase>(graph);
  307. }
  308. } } // namespace JSC::DFG
  309. #endif // ENABLE(DFG_JIT)