Fix reading Time zone rules using Julian days (#17672)
[platform/upstream/coreclr.git] / src / jit / ssabuilder.h
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.
4
5 #pragma once
6 #pragma warning(disable : 4503) // 'identifier' : decorated name length exceeded, name was truncated
7
8 #undef SSA_FEATURE_DOMARR
9
10 #include "compiler.h"
11
12 struct SsaRenameState;
13
14 typedef int LclVarNum;
15
16 // Pair of a local var name eg: V01 and Ssa number; eg: V01_01
17 typedef jitstd::pair<LclVarNum, int> SsaVarName;
18
19 class SsaBuilder
20 {
21 private:
22     inline void EndPhase(Phases phase)
23     {
24         m_pCompiler->EndPhase(phase);
25     }
26
27 public:
28     // Constructor
29     SsaBuilder(Compiler* pCompiler);
30
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.
39     void Build();
40
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);
47
48 private:
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.
52     void SetupBBRoot();
53
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
58     // the output array.
59     int TopologicalSort(BasicBlock** postOrder, int count);
60
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);
65
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);
73 #endif
74
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);
78
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);
83
84 #ifdef DEBUG
85     // Display the dominator tree.
86     static void DisplayDominators(BlkToBlkSetMap* domTree);
87 #endif // DEBUG
88
89     // Compute flow graph dominance frontiers.
90     void ComputeDominanceFrontiers(BasicBlock** postOrder, int count, BlkToBlkVectorMap* mapDF);
91
92     // Compute the iterated dominance frontier for the specified block.
93     void ComputeIteratedDominanceFrontier(BasicBlock* b, const BlkToBlkVectorMap* mapDF, BlkVector* bIDF);
94
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);
100
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);
105
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);
111
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);
115
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);
120
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);
127
128     // Same as above, for memory.
129     void AddMemoryDefToHandlerPhis(MemoryKind memoryKind, BasicBlock* block, unsigned count);
130
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);
134
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);
138
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);
143
144 #ifdef DEBUG
145     void Print(BasicBlock** postOrder, int count);
146 #endif
147
148 private:
149     Compiler*     m_pCompiler;
150     CompAllocator m_allocator;
151
152     // Bit vector used by TopologicalSort and ComputeImmediateDom to track already visited blocks.
153     BitVecTraits m_visitedTraits;
154     BitVec       m_visited;
155
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.
159     int* m_pDomPreOrder;
160     int* m_pDomPostOrder;
161 #endif
162 };