xref: /freebsd-src/contrib/llvm-project/clang/lib/Analysis/ThreadSafetyTIL.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- ThreadSafetyTIL.cpp ------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
10*0b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
11*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
12*0b57cec5SDimitry Andric #include <cassert>
13*0b57cec5SDimitry Andric #include <cstddef>
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric using namespace clang;
16*0b57cec5SDimitry Andric using namespace threadSafety;
17*0b57cec5SDimitry Andric using namespace til;
18*0b57cec5SDimitry Andric 
getUnaryOpcodeString(TIL_UnaryOpcode Op)19*0b57cec5SDimitry Andric StringRef til::getUnaryOpcodeString(TIL_UnaryOpcode Op) {
20*0b57cec5SDimitry Andric   switch (Op) {
21*0b57cec5SDimitry Andric     case UOP_Minus:    return "-";
22*0b57cec5SDimitry Andric     case UOP_BitNot:   return "~";
23*0b57cec5SDimitry Andric     case UOP_LogicNot: return "!";
24*0b57cec5SDimitry Andric   }
25*0b57cec5SDimitry Andric   return {};
26*0b57cec5SDimitry Andric }
27*0b57cec5SDimitry Andric 
getBinaryOpcodeString(TIL_BinaryOpcode Op)28*0b57cec5SDimitry Andric StringRef til::getBinaryOpcodeString(TIL_BinaryOpcode Op) {
29*0b57cec5SDimitry Andric   switch (Op) {
30*0b57cec5SDimitry Andric     case BOP_Mul:      return "*";
31*0b57cec5SDimitry Andric     case BOP_Div:      return "/";
32*0b57cec5SDimitry Andric     case BOP_Rem:      return "%";
33*0b57cec5SDimitry Andric     case BOP_Add:      return "+";
34*0b57cec5SDimitry Andric     case BOP_Sub:      return "-";
35*0b57cec5SDimitry Andric     case BOP_Shl:      return "<<";
36*0b57cec5SDimitry Andric     case BOP_Shr:      return ">>";
37*0b57cec5SDimitry Andric     case BOP_BitAnd:   return "&";
38*0b57cec5SDimitry Andric     case BOP_BitXor:   return "^";
39*0b57cec5SDimitry Andric     case BOP_BitOr:    return "|";
40*0b57cec5SDimitry Andric     case BOP_Eq:       return "==";
41*0b57cec5SDimitry Andric     case BOP_Neq:      return "!=";
42*0b57cec5SDimitry Andric     case BOP_Lt:       return "<";
43*0b57cec5SDimitry Andric     case BOP_Leq:      return "<=";
44*0b57cec5SDimitry Andric     case BOP_Cmp:      return "<=>";
45*0b57cec5SDimitry Andric     case BOP_LogicAnd: return "&&";
46*0b57cec5SDimitry Andric     case BOP_LogicOr:  return "||";
47*0b57cec5SDimitry Andric   }
48*0b57cec5SDimitry Andric   return {};
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
force()51*0b57cec5SDimitry Andric SExpr* Future::force() {
52*0b57cec5SDimitry Andric   Status = FS_evaluating;
53*0b57cec5SDimitry Andric   Result = compute();
54*0b57cec5SDimitry Andric   Status = FS_done;
55*0b57cec5SDimitry Andric   return Result;
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric 
addPredecessor(BasicBlock * Pred)58*0b57cec5SDimitry Andric unsigned BasicBlock::addPredecessor(BasicBlock *Pred) {
59*0b57cec5SDimitry Andric   unsigned Idx = Predecessors.size();
60*0b57cec5SDimitry Andric   Predecessors.reserveCheck(1, Arena);
61*0b57cec5SDimitry Andric   Predecessors.push_back(Pred);
62*0b57cec5SDimitry Andric   for (auto *E : Args) {
63*0b57cec5SDimitry Andric     if (auto *Ph = dyn_cast<Phi>(E)) {
64*0b57cec5SDimitry Andric       Ph->values().reserveCheck(1, Arena);
65*0b57cec5SDimitry Andric       Ph->values().push_back(nullptr);
66*0b57cec5SDimitry Andric     }
67*0b57cec5SDimitry Andric   }
68*0b57cec5SDimitry Andric   return Idx;
69*0b57cec5SDimitry Andric }
70*0b57cec5SDimitry Andric 
reservePredecessors(unsigned NumPreds)71*0b57cec5SDimitry Andric void BasicBlock::reservePredecessors(unsigned NumPreds) {
72*0b57cec5SDimitry Andric   Predecessors.reserve(NumPreds, Arena);
73*0b57cec5SDimitry Andric   for (auto *E : Args) {
74*0b57cec5SDimitry Andric     if (auto *Ph = dyn_cast<Phi>(E)) {
75*0b57cec5SDimitry Andric       Ph->values().reserve(NumPreds, Arena);
76*0b57cec5SDimitry Andric     }
77*0b57cec5SDimitry Andric   }
78*0b57cec5SDimitry Andric }
79*0b57cec5SDimitry Andric 
80*0b57cec5SDimitry Andric // If E is a variable, then trace back through any aliases or redundant
81*0b57cec5SDimitry Andric // Phi nodes to find the canonical definition.
getCanonicalVal(const SExpr * E)82*0b57cec5SDimitry Andric const SExpr *til::getCanonicalVal(const SExpr *E) {
83*0b57cec5SDimitry Andric   while (true) {
84*0b57cec5SDimitry Andric     if (const auto *V = dyn_cast<Variable>(E)) {
85*0b57cec5SDimitry Andric       if (V->kind() == Variable::VK_Let) {
86*0b57cec5SDimitry Andric         E = V->definition();
87*0b57cec5SDimitry Andric         continue;
88*0b57cec5SDimitry Andric       }
89*0b57cec5SDimitry Andric     }
90*0b57cec5SDimitry Andric     if (const auto *Ph = dyn_cast<Phi>(E)) {
91*0b57cec5SDimitry Andric       if (Ph->status() == Phi::PH_SingleVal) {
92*0b57cec5SDimitry Andric         E = Ph->values()[0];
93*0b57cec5SDimitry Andric         continue;
94*0b57cec5SDimitry Andric       }
95*0b57cec5SDimitry Andric     }
96*0b57cec5SDimitry Andric     break;
97*0b57cec5SDimitry Andric   }
98*0b57cec5SDimitry Andric   return E;
99*0b57cec5SDimitry Andric }
100*0b57cec5SDimitry Andric 
101*0b57cec5SDimitry Andric // If E is a variable, then trace back through any aliases or redundant
102*0b57cec5SDimitry Andric // Phi nodes to find the canonical definition.
103*0b57cec5SDimitry Andric // The non-const version will simplify incomplete Phi nodes.
simplifyToCanonicalVal(SExpr * E)104*0b57cec5SDimitry Andric SExpr *til::simplifyToCanonicalVal(SExpr *E) {
105*0b57cec5SDimitry Andric   while (true) {
106*0b57cec5SDimitry Andric     if (auto *V = dyn_cast<Variable>(E)) {
107*0b57cec5SDimitry Andric       if (V->kind() != Variable::VK_Let)
108*0b57cec5SDimitry Andric         return V;
109*0b57cec5SDimitry Andric       // Eliminate redundant variables, e.g. x = y, or x = 5,
110*0b57cec5SDimitry Andric       // but keep anything more complicated.
111*0b57cec5SDimitry Andric       if (til::ThreadSafetyTIL::isTrivial(V->definition())) {
112*0b57cec5SDimitry Andric         E = V->definition();
113*0b57cec5SDimitry Andric         continue;
114*0b57cec5SDimitry Andric       }
115*0b57cec5SDimitry Andric       return V;
116*0b57cec5SDimitry Andric     }
117*0b57cec5SDimitry Andric     if (auto *Ph = dyn_cast<Phi>(E)) {
118*0b57cec5SDimitry Andric       if (Ph->status() == Phi::PH_Incomplete)
119*0b57cec5SDimitry Andric         simplifyIncompleteArg(Ph);
120*0b57cec5SDimitry Andric       // Eliminate redundant Phi nodes.
121*0b57cec5SDimitry Andric       if (Ph->status() == Phi::PH_SingleVal) {
122*0b57cec5SDimitry Andric         E = Ph->values()[0];
123*0b57cec5SDimitry Andric         continue;
124*0b57cec5SDimitry Andric       }
125*0b57cec5SDimitry Andric     }
126*0b57cec5SDimitry Andric     return E;
127*0b57cec5SDimitry Andric   }
128*0b57cec5SDimitry Andric }
129*0b57cec5SDimitry Andric 
130*0b57cec5SDimitry Andric // Trace the arguments of an incomplete Phi node to see if they have the same
131*0b57cec5SDimitry Andric // canonical definition.  If so, mark the Phi node as redundant.
132*0b57cec5SDimitry Andric // getCanonicalVal() will recursively call simplifyIncompletePhi().
simplifyIncompleteArg(til::Phi * Ph)133*0b57cec5SDimitry Andric void til::simplifyIncompleteArg(til::Phi *Ph) {
134*0b57cec5SDimitry Andric   assert(Ph && Ph->status() == Phi::PH_Incomplete);
135*0b57cec5SDimitry Andric 
136*0b57cec5SDimitry Andric   // eliminate infinite recursion -- assume that this node is not redundant.
137*0b57cec5SDimitry Andric   Ph->setStatus(Phi::PH_MultiVal);
138*0b57cec5SDimitry Andric 
139*0b57cec5SDimitry Andric   SExpr *E0 = simplifyToCanonicalVal(Ph->values()[0]);
140*0b57cec5SDimitry Andric   for (unsigned i = 1, n = Ph->values().size(); i < n; ++i) {
141*0b57cec5SDimitry Andric     SExpr *Ei = simplifyToCanonicalVal(Ph->values()[i]);
142*0b57cec5SDimitry Andric     if (Ei == Ph)
143*0b57cec5SDimitry Andric       continue;  // Recursive reference to itself.  Don't count.
144*0b57cec5SDimitry Andric     if (Ei != E0) {
145*0b57cec5SDimitry Andric       return;    // Status is already set to MultiVal.
146*0b57cec5SDimitry Andric     }
147*0b57cec5SDimitry Andric   }
148*0b57cec5SDimitry Andric   Ph->setStatus(Phi::PH_SingleVal);
149*0b57cec5SDimitry Andric }
150*0b57cec5SDimitry Andric 
151*0b57cec5SDimitry Andric // Renumbers the arguments and instructions to have unique, sequential IDs.
renumberInstrs(unsigned ID)152*0b57cec5SDimitry Andric unsigned BasicBlock::renumberInstrs(unsigned ID) {
153*0b57cec5SDimitry Andric   for (auto *Arg : Args)
154*0b57cec5SDimitry Andric     Arg->setID(this, ID++);
155*0b57cec5SDimitry Andric   for (auto *Instr : Instrs)
156*0b57cec5SDimitry Andric     Instr->setID(this, ID++);
157*0b57cec5SDimitry Andric   TermInstr->setID(this, ID++);
158*0b57cec5SDimitry Andric   return ID;
159*0b57cec5SDimitry Andric }
160*0b57cec5SDimitry Andric 
161*0b57cec5SDimitry Andric // Sorts the CFGs blocks using a reverse post-order depth-first traversal.
162*0b57cec5SDimitry Andric // Each block will be written into the Blocks array in order, and its BlockID
163*0b57cec5SDimitry Andric // will be set to the index in the array.  Sorting should start from the entry
164*0b57cec5SDimitry Andric // block, and ID should be the total number of blocks.
topologicalSort(SimpleArray<BasicBlock * > & Blocks,unsigned ID)165*0b57cec5SDimitry Andric unsigned BasicBlock::topologicalSort(SimpleArray<BasicBlock *> &Blocks,
166*0b57cec5SDimitry Andric                                      unsigned ID) {
167*0b57cec5SDimitry Andric   if (Visited) return ID;
168*0b57cec5SDimitry Andric   Visited = true;
169*0b57cec5SDimitry Andric   for (auto *Block : successors())
170*0b57cec5SDimitry Andric     ID = Block->topologicalSort(Blocks, ID);
171*0b57cec5SDimitry Andric   // set ID and update block array in place.
172*0b57cec5SDimitry Andric   // We may lose pointers to unreachable blocks.
173*0b57cec5SDimitry Andric   assert(ID > 0);
174*0b57cec5SDimitry Andric   BlockID = --ID;
175*0b57cec5SDimitry Andric   Blocks[BlockID] = this;
176*0b57cec5SDimitry Andric   return ID;
177*0b57cec5SDimitry Andric }
178*0b57cec5SDimitry Andric 
179*0b57cec5SDimitry Andric // Performs a reverse topological traversal, starting from the exit block and
180*0b57cec5SDimitry Andric // following back-edges.  The dominator is serialized before any predecessors,
181*0b57cec5SDimitry Andric // which guarantees that all blocks are serialized after their dominator and
182*0b57cec5SDimitry Andric // before their post-dominator (because it's a reverse topological traversal).
183*0b57cec5SDimitry Andric // ID should be initially set to 0.
184*0b57cec5SDimitry Andric //
185*0b57cec5SDimitry Andric // This sort assumes that (1) dominators have been computed, (2) there are no
186*0b57cec5SDimitry Andric // critical edges, and (3) the entry block is reachable from the exit block
187*0b57cec5SDimitry Andric // and no blocks are accessible via traversal of back-edges from the exit that
188*0b57cec5SDimitry Andric // weren't accessible via forward edges from the entry.
topologicalFinalSort(SimpleArray<BasicBlock * > & Blocks,unsigned ID)189*0b57cec5SDimitry Andric unsigned BasicBlock::topologicalFinalSort(SimpleArray<BasicBlock *> &Blocks,
190*0b57cec5SDimitry Andric                                           unsigned ID) {
191*0b57cec5SDimitry Andric   // Visited is assumed to have been set by the topologicalSort.  This pass
192*0b57cec5SDimitry Andric   // assumes !Visited means that we've visited this node before.
193*0b57cec5SDimitry Andric   if (!Visited) return ID;
194*0b57cec5SDimitry Andric   Visited = false;
195*0b57cec5SDimitry Andric   if (DominatorNode.Parent)
196*0b57cec5SDimitry Andric     ID = DominatorNode.Parent->topologicalFinalSort(Blocks, ID);
197*0b57cec5SDimitry Andric   for (auto *Pred : Predecessors)
198*0b57cec5SDimitry Andric     ID = Pred->topologicalFinalSort(Blocks, ID);
199*0b57cec5SDimitry Andric   assert(static_cast<size_t>(ID) < Blocks.size());
200*0b57cec5SDimitry Andric   BlockID = ID++;
201*0b57cec5SDimitry Andric   Blocks[BlockID] = this;
202*0b57cec5SDimitry Andric   return ID;
203*0b57cec5SDimitry Andric }
204*0b57cec5SDimitry Andric 
205*0b57cec5SDimitry Andric // Computes the immediate dominator of the current block.  Assumes that all of
206*0b57cec5SDimitry Andric // its predecessors have already computed their dominators.  This is achieved
207*0b57cec5SDimitry Andric // by visiting the nodes in topological order.
computeDominator()208*0b57cec5SDimitry Andric void BasicBlock::computeDominator() {
209*0b57cec5SDimitry Andric   BasicBlock *Candidate = nullptr;
210*0b57cec5SDimitry Andric   // Walk backwards from each predecessor to find the common dominator node.
211*0b57cec5SDimitry Andric   for (auto *Pred : Predecessors) {
212*0b57cec5SDimitry Andric     // Skip back-edges
213*0b57cec5SDimitry Andric     if (Pred->BlockID >= BlockID) continue;
214*0b57cec5SDimitry Andric     // If we don't yet have a candidate for dominator yet, take this one.
215*0b57cec5SDimitry Andric     if (Candidate == nullptr) {
216*0b57cec5SDimitry Andric       Candidate = Pred;
217*0b57cec5SDimitry Andric       continue;
218*0b57cec5SDimitry Andric     }
219*0b57cec5SDimitry Andric     // Walk the alternate and current candidate back to find a common ancestor.
220*0b57cec5SDimitry Andric     auto *Alternate = Pred;
221*0b57cec5SDimitry Andric     while (Alternate != Candidate) {
222*0b57cec5SDimitry Andric       if (Candidate->BlockID > Alternate->BlockID)
223*0b57cec5SDimitry Andric         Candidate = Candidate->DominatorNode.Parent;
224*0b57cec5SDimitry Andric       else
225*0b57cec5SDimitry Andric         Alternate = Alternate->DominatorNode.Parent;
226*0b57cec5SDimitry Andric     }
227*0b57cec5SDimitry Andric   }
228*0b57cec5SDimitry Andric   DominatorNode.Parent = Candidate;
229*0b57cec5SDimitry Andric   DominatorNode.SizeOfSubTree = 1;
230*0b57cec5SDimitry Andric }
231*0b57cec5SDimitry Andric 
232*0b57cec5SDimitry Andric // Computes the immediate post-dominator of the current block.  Assumes that all
233*0b57cec5SDimitry Andric // of its successors have already computed their post-dominators.  This is
234*0b57cec5SDimitry Andric // achieved visiting the nodes in reverse topological order.
computePostDominator()235*0b57cec5SDimitry Andric void BasicBlock::computePostDominator() {
236*0b57cec5SDimitry Andric   BasicBlock *Candidate = nullptr;
237*0b57cec5SDimitry Andric   // Walk back from each predecessor to find the common post-dominator node.
238*0b57cec5SDimitry Andric   for (auto *Succ : successors()) {
239*0b57cec5SDimitry Andric     // Skip back-edges
240*0b57cec5SDimitry Andric     if (Succ->BlockID <= BlockID) continue;
241*0b57cec5SDimitry Andric     // If we don't yet have a candidate for post-dominator yet, take this one.
242*0b57cec5SDimitry Andric     if (Candidate == nullptr) {
243*0b57cec5SDimitry Andric       Candidate = Succ;
244*0b57cec5SDimitry Andric       continue;
245*0b57cec5SDimitry Andric     }
246*0b57cec5SDimitry Andric     // Walk the alternate and current candidate back to find a common ancestor.
247*0b57cec5SDimitry Andric     auto *Alternate = Succ;
248*0b57cec5SDimitry Andric     while (Alternate != Candidate) {
249*0b57cec5SDimitry Andric       if (Candidate->BlockID < Alternate->BlockID)
250*0b57cec5SDimitry Andric         Candidate = Candidate->PostDominatorNode.Parent;
251*0b57cec5SDimitry Andric       else
252*0b57cec5SDimitry Andric         Alternate = Alternate->PostDominatorNode.Parent;
253*0b57cec5SDimitry Andric     }
254*0b57cec5SDimitry Andric   }
255*0b57cec5SDimitry Andric   PostDominatorNode.Parent = Candidate;
256*0b57cec5SDimitry Andric   PostDominatorNode.SizeOfSubTree = 1;
257*0b57cec5SDimitry Andric }
258*0b57cec5SDimitry Andric 
259*0b57cec5SDimitry Andric // Renumber instructions in all blocks
renumberInstrs()260*0b57cec5SDimitry Andric void SCFG::renumberInstrs() {
261*0b57cec5SDimitry Andric   unsigned InstrID = 0;
262*0b57cec5SDimitry Andric   for (auto *Block : Blocks)
263*0b57cec5SDimitry Andric     InstrID = Block->renumberInstrs(InstrID);
264*0b57cec5SDimitry Andric }
265*0b57cec5SDimitry Andric 
computeNodeSize(BasicBlock * B,BasicBlock::TopologyNode BasicBlock::* TN)266*0b57cec5SDimitry Andric static inline void computeNodeSize(BasicBlock *B,
267*0b57cec5SDimitry Andric                                    BasicBlock::TopologyNode BasicBlock::*TN) {
268*0b57cec5SDimitry Andric   BasicBlock::TopologyNode *N = &(B->*TN);
269*0b57cec5SDimitry Andric   if (N->Parent) {
270*0b57cec5SDimitry Andric     BasicBlock::TopologyNode *P = &(N->Parent->*TN);
271*0b57cec5SDimitry Andric     // Initially set ID relative to the (as yet uncomputed) parent ID
272*0b57cec5SDimitry Andric     N->NodeID = P->SizeOfSubTree;
273*0b57cec5SDimitry Andric     P->SizeOfSubTree += N->SizeOfSubTree;
274*0b57cec5SDimitry Andric   }
275*0b57cec5SDimitry Andric }
276*0b57cec5SDimitry Andric 
computeNodeID(BasicBlock * B,BasicBlock::TopologyNode BasicBlock::* TN)277*0b57cec5SDimitry Andric static inline void computeNodeID(BasicBlock *B,
278*0b57cec5SDimitry Andric                                  BasicBlock::TopologyNode BasicBlock::*TN) {
279*0b57cec5SDimitry Andric   BasicBlock::TopologyNode *N = &(B->*TN);
280*0b57cec5SDimitry Andric   if (N->Parent) {
281*0b57cec5SDimitry Andric     BasicBlock::TopologyNode *P = &(N->Parent->*TN);
282*0b57cec5SDimitry Andric     N->NodeID += P->NodeID;    // Fix NodeIDs relative to starting node.
283*0b57cec5SDimitry Andric   }
284*0b57cec5SDimitry Andric }
285*0b57cec5SDimitry Andric 
286*0b57cec5SDimitry Andric // Normalizes a CFG.  Normalization has a few major components:
287*0b57cec5SDimitry Andric // 1) Removing unreachable blocks.
288*0b57cec5SDimitry Andric // 2) Computing dominators and post-dominators
289*0b57cec5SDimitry Andric // 3) Topologically sorting the blocks into the "Blocks" array.
computeNormalForm()290*0b57cec5SDimitry Andric void SCFG::computeNormalForm() {
291*0b57cec5SDimitry Andric   // Topologically sort the blocks starting from the entry block.
292*0b57cec5SDimitry Andric   unsigned NumUnreachableBlocks = Entry->topologicalSort(Blocks, Blocks.size());
293*0b57cec5SDimitry Andric   if (NumUnreachableBlocks > 0) {
294*0b57cec5SDimitry Andric     // If there were unreachable blocks shift everything down, and delete them.
295*0b57cec5SDimitry Andric     for (unsigned I = NumUnreachableBlocks, E = Blocks.size(); I < E; ++I) {
296*0b57cec5SDimitry Andric       unsigned NI = I - NumUnreachableBlocks;
297*0b57cec5SDimitry Andric       Blocks[NI] = Blocks[I];
298*0b57cec5SDimitry Andric       Blocks[NI]->BlockID = NI;
299*0b57cec5SDimitry Andric       // FIXME: clean up predecessor pointers to unreachable blocks?
300*0b57cec5SDimitry Andric     }
301*0b57cec5SDimitry Andric     Blocks.drop(NumUnreachableBlocks);
302*0b57cec5SDimitry Andric   }
303*0b57cec5SDimitry Andric 
304*0b57cec5SDimitry Andric   // Compute dominators.
305*0b57cec5SDimitry Andric   for (auto *Block : Blocks)
306*0b57cec5SDimitry Andric     Block->computeDominator();
307*0b57cec5SDimitry Andric 
308*0b57cec5SDimitry Andric   // Once dominators have been computed, the final sort may be performed.
309*0b57cec5SDimitry Andric   unsigned NumBlocks = Exit->topologicalFinalSort(Blocks, 0);
310*0b57cec5SDimitry Andric   assert(static_cast<size_t>(NumBlocks) == Blocks.size());
311*0b57cec5SDimitry Andric   (void) NumBlocks;
312*0b57cec5SDimitry Andric 
313*0b57cec5SDimitry Andric   // Renumber the instructions now that we have a final sort.
314*0b57cec5SDimitry Andric   renumberInstrs();
315*0b57cec5SDimitry Andric 
316*0b57cec5SDimitry Andric   // Compute post-dominators and compute the sizes of each node in the
317*0b57cec5SDimitry Andric   // dominator tree.
318*0b57cec5SDimitry Andric   for (auto *Block : Blocks.reverse()) {
319*0b57cec5SDimitry Andric     Block->computePostDominator();
320*0b57cec5SDimitry Andric     computeNodeSize(Block, &BasicBlock::DominatorNode);
321*0b57cec5SDimitry Andric   }
322*0b57cec5SDimitry Andric   // Compute the sizes of each node in the post-dominator tree and assign IDs in
323*0b57cec5SDimitry Andric   // the dominator tree.
324*0b57cec5SDimitry Andric   for (auto *Block : Blocks) {
325*0b57cec5SDimitry Andric     computeNodeID(Block, &BasicBlock::DominatorNode);
326*0b57cec5SDimitry Andric     computeNodeSize(Block, &BasicBlock::PostDominatorNode);
327*0b57cec5SDimitry Andric   }
328*0b57cec5SDimitry Andric   // Assign IDs in the post-dominator tree.
329*0b57cec5SDimitry Andric   for (auto *Block : Blocks.reverse()) {
330*0b57cec5SDimitry Andric     computeNodeID(Block, &BasicBlock::PostDominatorNode);
331*0b57cec5SDimitry Andric   }
332*0b57cec5SDimitry Andric }
333