1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
6 #pragma warning(disable : 4503) // 'identifier' : decorated name length exceeded, name was truncated
8 #undef SSA_FEATURE_DOMARR
12 struct SsaRenameState;
14 typedef int LclVarNum;
16 // Pair of a local var name eg: V01 and Ssa number; eg: V01_01
17 typedef jitstd::pair<LclVarNum, int> SsaVarName;
22 inline void EndPhase(Phases phase)
24 m_pCompiler->EndPhase(phase);
29 SsaBuilder(Compiler* pCompiler);
31 // Requires stmt nodes to be already sequenced in evaluation order. Analyzes the graph
32 // for introduction of phi-nodes as GT_PHI tree nodes at the beginning of each block.
33 // Each GT_LCL_VAR is given its ssa number through its gtSsaNum field in the node.
34 // Each GT_PHI node will have gtOp1 set to lhs of the phi node and the gtOp2 to be a
35 // GT_LIST of GT_PHI_ARG. Each use or def is denoted by the corresponding GT_LCL_VAR
36 // tree. For example, to get all uses of a particular variable fully defined by its
37 // lclNum and ssaNum, one would use m_uses and look up all the uses. Similarly, a single
38 // def of an SSA variable can be looked up similarly using m_defs member.
41 // Requires "bbIDom" of each block to be computed. Requires "domTree" to be allocated
42 // and can be updated, i.e., by adding mapping from a block to it's dominated children.
43 // Using IDom of each basic block, compute the whole domTree. If a block "b" has IDom "i",
44 // then, block "b" is dominated by "i". The mapping then is i -> { ..., b, ... }, in
45 // other words, "domTree" is a tree represented by nodes mapped to their children.
46 static void ComputeDominators(Compiler* pCompiler, BlkToBlkSetMap* domTree);
49 // Ensures that the basic block graph has a root for the dominator graph, by ensuring
50 // that there is a first block that is not in a try region (adding an empty block for that purpose
51 // if necessary). Eventually should move to Compiler.
54 // Requires "postOrder" to be an array of size "count". Requires "count" to at least
55 // be the size of the flow graph. Sorts the current compiler's flow-graph and places
56 // the blocks in post order (i.e., a node's children first) in the array. Returns the
57 // number of nodes visited while sorting the graph. In other words, valid entries in
59 int TopologicalSort(BasicBlock** postOrder, int count);
61 // Requires "postOrder" to hold the blocks of the flowgraph in topologically sorted
62 // order. Requires count to be the valid entries in the "postOrder" array. Computes
63 // each block's immediate dominator and records it in the BasicBlock in bbIDom.
64 void ComputeImmediateDom(BasicBlock** postOrder, int count);
66 #ifdef SSA_FEATURE_DOMARR
67 // Requires "curBlock" to be the first basic block at the first step of the recursion.
68 // Requires "domTree" to be a adjacency list (actually, a set of blocks with a set of blocks
69 // as children.) Requires "preIndex" and "postIndex" to be initialized to 0 at entry into recursion.
70 // Computes arrays "m_pDomPreOrder" and "m_pDomPostOrder" of block indices such that the blocks of a
71 // "domTree" are in pre and postorder respectively.
72 void DomTreeWalk(BasicBlock* curBlock, BlkToBlkSetMap* domTree, int* preIndex, int* postIndex);
75 // Requires all blocks to have computed "bbIDom." Requires "domTree" to be a preallocated BlkToBlkSetMap.
76 // Helper to compute "domTree" from the pre-computed bbIDom of the basic blocks.
77 static void ConstructDomTreeForBlock(Compiler* pCompiler, BasicBlock* block, BlkToBlkSetMap* domTree);
79 // Requires "postOrder" to hold the blocks of the flowgraph in topologically sorted order. Requires
80 // count to be the valid entries in the "postOrder" array. Computes "domTree" as a adjacency list
81 // like object, i.e., a set of blocks with a set of blocks as children defining the DOM relation.
82 void ComputeDominators(BasicBlock** postOrder, int count, BlkToBlkSetMap* domTree);
85 // Display the dominator tree.
86 static void DisplayDominators(BlkToBlkSetMap* domTree);
89 // Compute flow graph dominance frontiers.
90 void ComputeDominanceFrontiers(BasicBlock** postOrder, int count, BlkToBlkVectorMap* mapDF);
92 // Compute the iterated dominance frontier for the specified block.
93 void ComputeIteratedDominanceFrontier(BasicBlock* b, const BlkToBlkVectorMap* mapDF, BlkVector* bIDF);
95 // Requires "postOrder" to hold the blocks of the flowgraph in topologically sorted order. Requires
96 // count to be the valid entries in the "postOrder" array. Inserts GT_PHI nodes at the beginning
97 // of basic blocks that require them like so:
98 // GT_ASG(GT_LCL_VAR, GT_PHI(GT_PHI_ARG(GT_LCL_VAR, Block*), GT_LIST(GT_PHI_ARG(GT_LCL_VAR, Block*), NULL));
99 void InsertPhiFunctions(BasicBlock** postOrder, int count);
101 // Requires "domTree" to be the dominator tree relation defined by a DOM b.
102 // Requires "pRenameState" to have counts and stacks at their initial state.
103 // Assigns gtSsaNames to all variables.
104 void RenameVariables(BlkToBlkSetMap* domTree, SsaRenameState* pRenameState);
106 // Requires "block" to be any basic block participating in variable renaming, and has at least a
107 // definition that pushed a ssa number into the rename stack for a variable. Requires "pRenameState"
108 // to have variable stacks that have counts pushed into them for the block while assigning def
109 // numbers. Pops the stack for any local variable that has an entry for block on top.
110 void BlockPopStacks(BasicBlock* block, SsaRenameState* pRenameState);
112 // Requires "block" to be non-NULL; and is searched for defs and uses to assign ssa numbers.
113 // Requires "pRenameState" to be non-NULL and be currently used for variables renaming.
114 void BlockRenameVariables(BasicBlock* block, SsaRenameState* pRenameState);
116 // Requires "tree" (assumed to be a statement in "block") to be searched for defs and uses to assign ssa numbers.
117 // Requires "pRenameState" to be non-NULL and be currently used for variables renaming. Assumes that "isPhiDefn"
118 // implies that any definition occurring within "tree" is a phi definition.
119 void TreeRenameVariables(GenTree* tree, BasicBlock* block, SsaRenameState* pRenameState, bool isPhiDefn);
121 // Assumes that "block" contains a definition for local var "lclNum", with SSA number "count".
122 // IF "block" is within one or more try blocks,
123 // and the local variable is live at the start of the corresponding handlers,
124 // add this SSA number "count" to the argument list of the phi for the variable in the start
125 // block of those handlers.
126 void AddDefToHandlerPhis(BasicBlock* block, unsigned lclNum, unsigned count);
128 // Same as above, for memory.
129 void AddMemoryDefToHandlerPhis(MemoryKind memoryKind, BasicBlock* block, unsigned count);
131 // Requires "block" to be non-NULL. Requires "pRenameState" to be non-NULL and be currently used
132 // for variables renaming. Assigns the rhs arguments to the phi, i.e., block's phi node arguments.
133 void AssignPhiNodeRhsVariables(BasicBlock* block, SsaRenameState* pRenameState);
135 // Requires "tree" to be a local variable node. Maintains a map of <lclNum, ssaNum> -> tree
136 // information in m_defs.
137 void AddDefPoint(GenTree* tree, BasicBlock* blk);
139 // Returns true, and sets "*ppIndirAssign", if "tree" has been recorded as an indirect assignment.
140 // (If the tree is an assignment, it's a definition only if it's labeled as an indirect definition, where
141 // we took the address of the local elsewhere in the extended tree.)
142 bool IsIndirectAssign(GenTree* tree, Compiler::IndirectAssignmentAnnotation** ppIndirAssign);
145 void Print(BasicBlock** postOrder, int count);
149 Compiler* m_pCompiler;
150 CompAllocator m_allocator;
152 // Bit vector used by TopologicalSort and ComputeImmediateDom to track already visited blocks.
153 BitVecTraits m_visitedTraits;
156 #ifdef SSA_FEATURE_DOMARR
157 // To answer queries of type a DOM b.
158 // Do not move these outside of this class, use accessors/interface methods.
160 int* m_pDomPostOrder;