DFGAbstractState.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 DFGAbstractState_h
  26. #define DFGAbstractState_h
  27. #include <wtf/Platform.h>
  28. #if ENABLE(DFG_JIT)
  29. #include "DFGAbstractValue.h"
  30. #include "DFGBranchDirection.h"
  31. #include "DFGGraph.h"
  32. #include "DFGNode.h"
  33. #include <wtf/Vector.h>
  34. namespace JSC {
  35. class CodeBlock;
  36. namespace DFG {
  37. struct BasicBlock;
  38. // This implements the notion of an abstract state for flow-sensitive intraprocedural
  39. // control flow analysis (CFA), with a focus on the elimination of redundant type checks.
  40. // It also implements most of the mechanisms of abstract interpretation that such an
  41. // analysis would use. This class should be used in two idioms:
  42. //
  43. // 1) Performing the CFA. In this case, AbstractState should be run over all basic
  44. // blocks repeatedly until convergence is reached. Convergence is defined by
  45. // endBasicBlock(AbstractState::MergeToSuccessors) returning false for all blocks.
  46. //
  47. // 2) Rematerializing the results of a previously executed CFA. In this case,
  48. // AbstractState should be run over whatever basic block you're interested in up
  49. // to the point of the node at which you'd like to interrogate the known type
  50. // of all other nodes. At this point it's safe to discard the AbstractState entirely,
  51. // call reset(), or to run it to the end of the basic block and call
  52. // endBasicBlock(AbstractState::DontMerge). The latter option is safest because
  53. // it performs some useful integrity checks.
  54. //
  55. // After the CFA is run, the inter-block state is saved at the heads and tails of all
  56. // basic blocks. This allows the intra-block state to be rematerialized by just
  57. // executing the CFA for that block. If you need to know inter-block state only, then
  58. // you only need to examine the BasicBlock::m_valuesAtHead or m_valuesAtTail fields.
  59. //
  60. // Running this analysis involves the following, modulo the inter-block state
  61. // merging and convergence fixpoint:
  62. //
  63. // AbstractState state(codeBlock, graph);
  64. // state.beginBasicBlock(basicBlock);
  65. // bool endReached = true;
  66. // for (unsigned i = 0; i < basicBlock->size(); ++i) {
  67. // if (!state.execute(i))
  68. // break;
  69. // }
  70. // bool result = state.endBasicBlock(<either Merge or DontMerge>);
  71. class AbstractState {
  72. public:
  73. enum MergeMode {
  74. // Don't merge the state in AbstractState with basic blocks.
  75. DontMerge,
  76. // Merge the state in AbstractState with the tail of the basic
  77. // block being analyzed.
  78. MergeToTail,
  79. // Merge the state in AbstractState with the tail of the basic
  80. // block, and with the heads of successor blocks.
  81. MergeToSuccessors
  82. };
  83. AbstractState(Graph&);
  84. ~AbstractState();
  85. AbstractValue& forNode(Node* node)
  86. {
  87. return node->value;
  88. }
  89. AbstractValue& forNode(Edge edge)
  90. {
  91. return forNode(edge.node());
  92. }
  93. Operands_shared<AbstractValue>& variables()
  94. {
  95. return m_variables;
  96. }
  97. // Call this before beginning CFA to initialize the abstract values of
  98. // arguments, and to indicate which blocks should be listed for CFA
  99. // execution.
  100. static void initialize(Graph&);
  101. // Start abstractly executing the given basic block. Initializes the
  102. // notion of abstract state to what we believe it to be at the head
  103. // of the basic block, according to the basic block's data structures.
  104. // This method also sets cfaShouldRevisit to false.
  105. void beginBasicBlock(BasicBlock*);
  106. // Finish abstractly executing a basic block. If MergeToTail or
  107. // MergeToSuccessors is passed, then this merges everything we have
  108. // learned about how the state changes during this block's execution into
  109. // the block's data structures. There are three return modes, depending
  110. // on the value of mergeMode:
  111. //
  112. // DontMerge:
  113. // Always returns false.
  114. //
  115. // MergeToTail:
  116. // Returns true if the state of the block at the tail was changed.
  117. // This means that you must call mergeToSuccessors(), and if that
  118. // returns true, then you must revisit (at least) the successor
  119. // blocks. False will always be returned if the block is terminal
  120. // (i.e. ends in Throw or Return, or has a ForceOSRExit inside it).
  121. //
  122. // MergeToSuccessors:
  123. // Returns true if the state of the block at the tail was changed,
  124. // and, if the state at the heads of successors was changed.
  125. // A true return means that you must revisit (at least) the successor
  126. // blocks. This also sets cfaShouldRevisit to true for basic blocks
  127. // that must be visited next.
  128. bool endBasicBlock(MergeMode);
  129. // Reset the AbstractState. This throws away any results, and at this point
  130. // you can safely call beginBasicBlock() on any basic block.
  131. void reset();
  132. // Abstractly executes the given node. The new abstract state is stored into an
  133. // abstract stack stored in *this. Loads of local variables (that span
  134. // basic blocks) interrogate the basic block's notion of the state at the head.
  135. // Stores to local variables are handled in endBasicBlock(). This returns true
  136. // if execution should continue past this node. Notably, it will return true
  137. // for block terminals, so long as those terminals are not Return or variants
  138. // of Throw.
  139. //
  140. // This is guaranteed to be equivalent to doing:
  141. //
  142. // if (state.startExecuting(index)) {
  143. // state.executeEdges(index);
  144. // result = state.executeEffects(index);
  145. // } else
  146. // result = true;
  147. bool execute(unsigned indexInBlock);
  148. // Indicate the start of execution of the node. It resets any state in the node,
  149. // that is progressively built up by executeEdges() and executeEffects(). In
  150. // particular, this resets canExit(), so if you want to "know" between calls of
  151. // startExecuting() and executeEdges()/Effects() whether the last run of the
  152. // analysis concluded that the node can exit, you should probably set that
  153. // information aside prior to calling startExecuting().
  154. bool startExecuting(Node*);
  155. bool startExecuting(unsigned indexInBlock);
  156. // Abstractly execute the edges of the given node. This runs filterEdgeByUse()
  157. // on all edges of the node. You can skip this step, if you have already used
  158. // filterEdgeByUse() (or some equivalent) on each edge.
  159. void executeEdges(Node*);
  160. void executeEdges(unsigned indexInBlock);
  161. ALWAYS_INLINE void filterEdgeByUse(Node* node, Edge& edge)
  162. {
  163. #if !ASSERT_DISABLED
  164. switch (edge.useKind()) {
  165. case KnownInt32Use:
  166. case KnownNumberUse:
  167. case KnownCellUse:
  168. case KnownStringUse:
  169. ASSERT(!(forNode(edge).m_type & ~typeFilterFor(edge.useKind())));
  170. break;
  171. default:
  172. break;
  173. }
  174. #endif // !ASSERT_DISABLED
  175. filterByType(node, edge, typeFilterFor(edge.useKind()));
  176. }
  177. // Abstractly execute the effects of the given node. This changes the abstract
  178. // state assuming that edges have already been filtered.
  179. bool executeEffects(unsigned indexInBlock);
  180. bool executeEffects(unsigned indexInBlock, Node*);
  181. // Did the last executed node clobber the world?
  182. bool didClobber() const { return m_didClobber; }
  183. // Is the execution state still valid? This will be false if execute() has
  184. // returned false previously.
  185. bool isValid() const { return m_isValid; }
  186. // Merge the abstract state stored at the first block's tail into the second
  187. // block's head. Returns true if the second block's state changed. If so,
  188. // that block must be abstractly interpreted again. This also sets
  189. // to->cfaShouldRevisit to true, if it returns true, or if to has not been
  190. // visited yet.
  191. bool merge(BasicBlock* from, BasicBlock* to);
  192. // Merge the abstract state stored at the block's tail into all of its
  193. // successors. Returns true if any of the successors' states changed. Note
  194. // that this is automatically called in endBasicBlock() if MergeMode is
  195. // MergeToSuccessors.
  196. bool mergeToSuccessors(Graph&, BasicBlock*);
  197. void dump(PrintStream& out);
  198. private:
  199. void clobberWorld(const CodeOrigin&, unsigned indexInBlock);
  200. void clobberCapturedVars(const CodeOrigin&);
  201. void clobberStructures(unsigned indexInBlock);
  202. bool mergeStateAtTail(AbstractValue& destination, AbstractValue& inVariable, Node*);
  203. static bool mergeVariableBetweenBlocks(AbstractValue& destination, AbstractValue& source, Node* destinationNode, Node* sourceNode);
  204. enum BooleanResult {
  205. UnknownBooleanResult,
  206. DefinitelyFalse,
  207. DefinitelyTrue
  208. };
  209. BooleanResult booleanResult(Node*, AbstractValue&);
  210. bool trySetConstant(Node* node, JSValue value)
  211. {
  212. // Make sure we don't constant fold something that will produce values that contravene
  213. // predictions. If that happens then we know that the code will OSR exit, forcing
  214. // recompilation. But if we tried to constant fold then we'll have a very degenerate
  215. // IR: namely we'll have a JSConstant that contravenes its own prediction. There's a
  216. // lot of subtle code that assumes that
  217. // speculationFromValue(jsConstant) == jsConstant.prediction(). "Hardening" that code
  218. // is probably less sane than just pulling back on constant folding.
  219. SpeculatedType oldType = node->prediction();
  220. if (mergeSpeculations(speculationFromValue(value), oldType) != oldType)
  221. return false;
  222. forNode(node).set(value);
  223. return true;
  224. }
  225. ALWAYS_INLINE void filterByType(Node* node, Edge& edge, SpeculatedType type)
  226. {
  227. AbstractValue& value = forNode(edge);
  228. if (value.m_type & ~type) {
  229. node->setCanExit(true);
  230. edge.setProofStatus(NeedsCheck);
  231. } else
  232. edge.setProofStatus(IsProved);
  233. value.filter(type);
  234. }
  235. void verifyEdge(Node*, Edge);
  236. void verifyEdges(Node*);
  237. CodeBlock* m_codeBlock;
  238. Graph& m_graph;
  239. Operands_shared<AbstractValue> m_variables;
  240. BasicBlock* m_block;
  241. bool m_haveStructures;
  242. bool m_foundConstants;
  243. bool m_isValid;
  244. bool m_didClobber;
  245. BranchDirection m_branchDirection; // This is only set for blocks that end in Branch and that execute to completion (i.e. m_isValid == true).
  246. };
  247. } } // namespace JSC::DFG
  248. #endif // ENABLE(DFG_JIT)
  249. #endif // DFGAbstractState_h