187fe1a79SEugene Zelenko //===- ThreadSafetyTIL.cpp ------------------------------------------------===//
2f4b5e7c6SDeLesley Hutchins //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f4b5e7c6SDeLesley Hutchins //
7f4b5e7c6SDeLesley Hutchins //===----------------------------------------------------------------------===//
8f4b5e7c6SDeLesley Hutchins
9f4b5e7c6SDeLesley Hutchins #include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
1087fe1a79SEugene Zelenko #include "clang/Basic/LLVM.h"
1187fe1a79SEugene Zelenko #include "llvm/Support/Casting.h"
1287fe1a79SEugene Zelenko #include <cassert>
1387fe1a79SEugene Zelenko #include <cstddef>
1487fe1a79SEugene Zelenko
1566a97ee9SBenjamin Kramer using namespace clang;
1666a97ee9SBenjamin Kramer using namespace threadSafety;
1766a97ee9SBenjamin Kramer using namespace til;
18f4b5e7c6SDeLesley Hutchins
getUnaryOpcodeString(TIL_UnaryOpcode Op)1966a97ee9SBenjamin Kramer StringRef til::getUnaryOpcodeString(TIL_UnaryOpcode Op) {
20f4b5e7c6SDeLesley Hutchins switch (Op) {
21f4b5e7c6SDeLesley Hutchins case UOP_Minus: return "-";
22f4b5e7c6SDeLesley Hutchins case UOP_BitNot: return "~";
23f4b5e7c6SDeLesley Hutchins case UOP_LogicNot: return "!";
24f4b5e7c6SDeLesley Hutchins }
2587fe1a79SEugene Zelenko return {};
26f4b5e7c6SDeLesley Hutchins }
27f4b5e7c6SDeLesley Hutchins
getBinaryOpcodeString(TIL_BinaryOpcode Op)2866a97ee9SBenjamin Kramer StringRef til::getBinaryOpcodeString(TIL_BinaryOpcode Op) {
29f4b5e7c6SDeLesley Hutchins switch (Op) {
30f4b5e7c6SDeLesley Hutchins case BOP_Mul: return "*";
31f4b5e7c6SDeLesley Hutchins case BOP_Div: return "/";
32f4b5e7c6SDeLesley Hutchins case BOP_Rem: return "%";
33f4b5e7c6SDeLesley Hutchins case BOP_Add: return "+";
34f4b5e7c6SDeLesley Hutchins case BOP_Sub: return "-";
35f4b5e7c6SDeLesley Hutchins case BOP_Shl: return "<<";
36f4b5e7c6SDeLesley Hutchins case BOP_Shr: return ">>";
37f4b5e7c6SDeLesley Hutchins case BOP_BitAnd: return "&";
38f4b5e7c6SDeLesley Hutchins case BOP_BitXor: return "^";
39f4b5e7c6SDeLesley Hutchins case BOP_BitOr: return "|";
40f4b5e7c6SDeLesley Hutchins case BOP_Eq: return "==";
41f4b5e7c6SDeLesley Hutchins case BOP_Neq: return "!=";
42f4b5e7c6SDeLesley Hutchins case BOP_Lt: return "<";
43f4b5e7c6SDeLesley Hutchins case BOP_Leq: return "<=";
44c70f1d63SRichard Smith case BOP_Cmp: return "<=>";
45f4b5e7c6SDeLesley Hutchins case BOP_LogicAnd: return "&&";
46f4b5e7c6SDeLesley Hutchins case BOP_LogicOr: return "||";
47f4b5e7c6SDeLesley Hutchins }
4887fe1a79SEugene Zelenko return {};
49f4b5e7c6SDeLesley Hutchins }
50f4b5e7c6SDeLesley Hutchins
force()514e38f100SDeLesley Hutchins SExpr* Future::force() {
524e38f100SDeLesley Hutchins Status = FS_evaluating;
534e38f100SDeLesley Hutchins Result = compute();
544e38f100SDeLesley Hutchins Status = FS_done;
554e38f100SDeLesley Hutchins return Result;
564e38f100SDeLesley Hutchins }
574e38f100SDeLesley Hutchins
addPredecessor(BasicBlock * Pred)5844be81b5SDeLesley Hutchins unsigned BasicBlock::addPredecessor(BasicBlock *Pred) {
5944be81b5SDeLesley Hutchins unsigned Idx = Predecessors.size();
6044be81b5SDeLesley Hutchins Predecessors.reserveCheck(1, Arena);
6144be81b5SDeLesley Hutchins Predecessors.push_back(Pred);
6287fe1a79SEugene Zelenko for (auto *E : Args) {
6387fe1a79SEugene Zelenko if (auto *Ph = dyn_cast<Phi>(E)) {
6444be81b5SDeLesley Hutchins Ph->values().reserveCheck(1, Arena);
6544be81b5SDeLesley Hutchins Ph->values().push_back(nullptr);
6644be81b5SDeLesley Hutchins }
6744be81b5SDeLesley Hutchins }
6844be81b5SDeLesley Hutchins return Idx;
6944be81b5SDeLesley Hutchins }
7044be81b5SDeLesley Hutchins
reservePredecessors(unsigned NumPreds)7144be81b5SDeLesley Hutchins void BasicBlock::reservePredecessors(unsigned NumPreds) {
7244be81b5SDeLesley Hutchins Predecessors.reserve(NumPreds, Arena);
7387fe1a79SEugene Zelenko for (auto *E : Args) {
7487fe1a79SEugene Zelenko if (auto *Ph = dyn_cast<Phi>(E)) {
7544be81b5SDeLesley Hutchins Ph->values().reserve(NumPreds, Arena);
7644be81b5SDeLesley Hutchins }
7744be81b5SDeLesley Hutchins }
7844be81b5SDeLesley Hutchins }
7944be81b5SDeLesley Hutchins
80ea1f8338SDeLesley Hutchins // If E is a variable, then trace back through any aliases or redundant
81ea1f8338SDeLesley Hutchins // Phi nodes to find the canonical definition.
getCanonicalVal(const SExpr * E)8266a97ee9SBenjamin Kramer const SExpr *til::getCanonicalVal(const SExpr *E) {
834e38f100SDeLesley Hutchins while (true) {
8487fe1a79SEugene Zelenko if (const auto *V = dyn_cast<Variable>(E)) {
854e38f100SDeLesley Hutchins if (V->kind() == Variable::VK_Let) {
864e38f100SDeLesley Hutchins E = V->definition();
874e38f100SDeLesley Hutchins continue;
884e38f100SDeLesley Hutchins }
894e38f100SDeLesley Hutchins }
9087fe1a79SEugene Zelenko if (const auto *Ph = dyn_cast<Phi>(E)) {
91ea1f8338SDeLesley Hutchins if (Ph->status() == Phi::PH_SingleVal) {
92ea1f8338SDeLesley Hutchins E = Ph->values()[0];
93ea1f8338SDeLesley Hutchins continue;
94ea1f8338SDeLesley Hutchins }
95ea1f8338SDeLesley Hutchins }
964e38f100SDeLesley Hutchins break;
97ea1f8338SDeLesley Hutchins }
98ea1f8338SDeLesley Hutchins return E;
99ea1f8338SDeLesley Hutchins }
100ea1f8338SDeLesley Hutchins
101f4b5e7c6SDeLesley Hutchins // If E is a variable, then trace back through any aliases or redundant
102f4b5e7c6SDeLesley Hutchins // Phi nodes to find the canonical definition.
103ea1f8338SDeLesley Hutchins // The non-const version will simplify incomplete Phi nodes.
simplifyToCanonicalVal(SExpr * E)10466a97ee9SBenjamin Kramer SExpr *til::simplifyToCanonicalVal(SExpr *E) {
1054e38f100SDeLesley Hutchins while (true) {
1064e38f100SDeLesley Hutchins if (auto *V = dyn_cast<Variable>(E)) {
107f4b5e7c6SDeLesley Hutchins if (V->kind() != Variable::VK_Let)
108f4b5e7c6SDeLesley Hutchins return V;
1094e38f100SDeLesley Hutchins // Eliminate redundant variables, e.g. x = y, or x = 5,
1104e38f100SDeLesley Hutchins // but keep anything more complicated.
1114e38f100SDeLesley Hutchins if (til::ThreadSafetyTIL::isTrivial(V->definition())) {
1124e38f100SDeLesley Hutchins E = V->definition();
1134e38f100SDeLesley Hutchins continue;
1144e38f100SDeLesley Hutchins }
1154e38f100SDeLesley Hutchins return V;
1164e38f100SDeLesley Hutchins }
1174e38f100SDeLesley Hutchins if (auto *Ph = dyn_cast<Phi>(E)) {
118f4b5e7c6SDeLesley Hutchins if (Ph->status() == Phi::PH_Incomplete)
1194e38f100SDeLesley Hutchins simplifyIncompleteArg(Ph);
1204e38f100SDeLesley Hutchins // Eliminate redundant Phi nodes.
121f4b5e7c6SDeLesley Hutchins if (Ph->status() == Phi::PH_SingleVal) {
122f4b5e7c6SDeLesley Hutchins E = Ph->values()[0];
123f4b5e7c6SDeLesley Hutchins continue;
124f4b5e7c6SDeLesley Hutchins }
125f4b5e7c6SDeLesley Hutchins }
126f4b5e7c6SDeLesley Hutchins return E;
127f4b5e7c6SDeLesley Hutchins }
1284e38f100SDeLesley Hutchins }
129f4b5e7c6SDeLesley Hutchins
130f4b5e7c6SDeLesley Hutchins // Trace the arguments of an incomplete Phi node to see if they have the same
131f4b5e7c6SDeLesley Hutchins // canonical definition. If so, mark the Phi node as redundant.
132f4b5e7c6SDeLesley Hutchins // getCanonicalVal() will recursively call simplifyIncompletePhi().
simplifyIncompleteArg(til::Phi * Ph)13366a97ee9SBenjamin Kramer void til::simplifyIncompleteArg(til::Phi *Ph) {
134f4b5e7c6SDeLesley Hutchins assert(Ph && Ph->status() == Phi::PH_Incomplete);
135f4b5e7c6SDeLesley Hutchins
136f4b5e7c6SDeLesley Hutchins // eliminate infinite recursion -- assume that this node is not redundant.
137f4b5e7c6SDeLesley Hutchins Ph->setStatus(Phi::PH_MultiVal);
138f4b5e7c6SDeLesley Hutchins
139ea1f8338SDeLesley Hutchins SExpr *E0 = simplifyToCanonicalVal(Ph->values()[0]);
140f4b5e7c6SDeLesley Hutchins for (unsigned i = 1, n = Ph->values().size(); i < n; ++i) {
141ea1f8338SDeLesley Hutchins SExpr *Ei = simplifyToCanonicalVal(Ph->values()[i]);
1424e38f100SDeLesley Hutchins if (Ei == Ph)
143f4b5e7c6SDeLesley Hutchins continue; // Recursive reference to itself. Don't count.
144f4b5e7c6SDeLesley Hutchins if (Ei != E0) {
145f4b5e7c6SDeLesley Hutchins return; // Status is already set to MultiVal.
146f4b5e7c6SDeLesley Hutchins }
147f4b5e7c6SDeLesley Hutchins }
148f4b5e7c6SDeLesley Hutchins Ph->setStatus(Phi::PH_SingleVal);
149f4b5e7c6SDeLesley Hutchins }
150f4b5e7c6SDeLesley Hutchins
1514e38f100SDeLesley Hutchins // Renumbers the arguments and instructions to have unique, sequential IDs.
renumberInstrs(unsigned ID)15288d85365SAaron Puchert unsigned BasicBlock::renumberInstrs(unsigned ID) {
1534e38f100SDeLesley Hutchins for (auto *Arg : Args)
1544e38f100SDeLesley Hutchins Arg->setID(this, ID++);
1554e38f100SDeLesley Hutchins for (auto *Instr : Instrs)
1564e38f100SDeLesley Hutchins Instr->setID(this, ID++);
1574e38f100SDeLesley Hutchins TermInstr->setID(this, ID++);
1584e38f100SDeLesley Hutchins return ID;
1594e38f100SDeLesley Hutchins }
1604e38f100SDeLesley Hutchins
1614e38f100SDeLesley Hutchins // Sorts the CFGs blocks using a reverse post-order depth-first traversal.
1624e38f100SDeLesley Hutchins // Each block will be written into the Blocks array in order, and its BlockID
1634e38f100SDeLesley Hutchins // will be set to the index in the array. Sorting should start from the entry
1644e38f100SDeLesley Hutchins // block, and ID should be the total number of blocks.
topologicalSort(SimpleArray<BasicBlock * > & Blocks,unsigned ID)16588d85365SAaron Puchert unsigned BasicBlock::topologicalSort(SimpleArray<BasicBlock *> &Blocks,
16688d85365SAaron Puchert unsigned ID) {
1674e38f100SDeLesley Hutchins if (Visited) return ID;
1686afd1eb3SAaron Ballman Visited = true;
1694e38f100SDeLesley Hutchins for (auto *Block : successors())
1704e38f100SDeLesley Hutchins ID = Block->topologicalSort(Blocks, ID);
1714e38f100SDeLesley Hutchins // set ID and update block array in place.
1724e38f100SDeLesley Hutchins // We may lose pointers to unreachable blocks.
1734e38f100SDeLesley Hutchins assert(ID > 0);
1744e38f100SDeLesley Hutchins BlockID = --ID;
1754e38f100SDeLesley Hutchins Blocks[BlockID] = this;
1764e38f100SDeLesley Hutchins return ID;
1774e38f100SDeLesley Hutchins }
1784e38f100SDeLesley Hutchins
1794e38f100SDeLesley Hutchins // Performs a reverse topological traversal, starting from the exit block and
1804e38f100SDeLesley Hutchins // following back-edges. The dominator is serialized before any predecessors,
1814e38f100SDeLesley Hutchins // which guarantees that all blocks are serialized after their dominator and
1824e38f100SDeLesley Hutchins // before their post-dominator (because it's a reverse topological traversal).
1834e38f100SDeLesley Hutchins // ID should be initially set to 0.
1844e38f100SDeLesley Hutchins //
1854e38f100SDeLesley Hutchins // This sort assumes that (1) dominators have been computed, (2) there are no
1864e38f100SDeLesley Hutchins // critical edges, and (3) the entry block is reachable from the exit block
1872c51880aSSimon Pilgrim // and no blocks are accessible via traversal of back-edges from the exit that
1882c51880aSSimon Pilgrim // weren't accessible via forward edges from the entry.
topologicalFinalSort(SimpleArray<BasicBlock * > & Blocks,unsigned ID)18988d85365SAaron Puchert unsigned BasicBlock::topologicalFinalSort(SimpleArray<BasicBlock *> &Blocks,
19088d85365SAaron Puchert unsigned ID) {
1914e38f100SDeLesley Hutchins // Visited is assumed to have been set by the topologicalSort. This pass
1924e38f100SDeLesley Hutchins // assumes !Visited means that we've visited this node before.
1934e38f100SDeLesley Hutchins if (!Visited) return ID;
1946afd1eb3SAaron Ballman Visited = false;
1954e38f100SDeLesley Hutchins if (DominatorNode.Parent)
1964e38f100SDeLesley Hutchins ID = DominatorNode.Parent->topologicalFinalSort(Blocks, ID);
1974e38f100SDeLesley Hutchins for (auto *Pred : Predecessors)
1984e38f100SDeLesley Hutchins ID = Pred->topologicalFinalSort(Blocks, ID);
199560cbf50SJustin Bogner assert(static_cast<size_t>(ID) < Blocks.size());
2004e38f100SDeLesley Hutchins BlockID = ID++;
2014e38f100SDeLesley Hutchins Blocks[BlockID] = this;
2024e38f100SDeLesley Hutchins return ID;
2034e38f100SDeLesley Hutchins }
2044e38f100SDeLesley Hutchins
2054e38f100SDeLesley Hutchins // Computes the immediate dominator of the current block. Assumes that all of
2064e38f100SDeLesley Hutchins // its predecessors have already computed their dominators. This is achieved
2074e38f100SDeLesley Hutchins // by visiting the nodes in topological order.
computeDominator()2084e38f100SDeLesley Hutchins void BasicBlock::computeDominator() {
2094e38f100SDeLesley Hutchins BasicBlock *Candidate = nullptr;
2104e38f100SDeLesley Hutchins // Walk backwards from each predecessor to find the common dominator node.
2114e38f100SDeLesley Hutchins for (auto *Pred : Predecessors) {
2124e38f100SDeLesley Hutchins // Skip back-edges
2134e38f100SDeLesley Hutchins if (Pred->BlockID >= BlockID) continue;
2144e38f100SDeLesley Hutchins // If we don't yet have a candidate for dominator yet, take this one.
2154e38f100SDeLesley Hutchins if (Candidate == nullptr) {
2164e38f100SDeLesley Hutchins Candidate = Pred;
2174e38f100SDeLesley Hutchins continue;
2184e38f100SDeLesley Hutchins }
2194e38f100SDeLesley Hutchins // Walk the alternate and current candidate back to find a common ancestor.
2204e38f100SDeLesley Hutchins auto *Alternate = Pred;
2214e38f100SDeLesley Hutchins while (Alternate != Candidate) {
2224e38f100SDeLesley Hutchins if (Candidate->BlockID > Alternate->BlockID)
2234e38f100SDeLesley Hutchins Candidate = Candidate->DominatorNode.Parent;
2244e38f100SDeLesley Hutchins else
2254e38f100SDeLesley Hutchins Alternate = Alternate->DominatorNode.Parent;
2264e38f100SDeLesley Hutchins }
2274e38f100SDeLesley Hutchins }
2284e38f100SDeLesley Hutchins DominatorNode.Parent = Candidate;
2294e38f100SDeLesley Hutchins DominatorNode.SizeOfSubTree = 1;
2304e38f100SDeLesley Hutchins }
2314e38f100SDeLesley Hutchins
2324e38f100SDeLesley Hutchins // Computes the immediate post-dominator of the current block. Assumes that all
2334e38f100SDeLesley Hutchins // of its successors have already computed their post-dominators. This is
2344e38f100SDeLesley Hutchins // achieved visiting the nodes in reverse topological order.
computePostDominator()2354e38f100SDeLesley Hutchins void BasicBlock::computePostDominator() {
2364e38f100SDeLesley Hutchins BasicBlock *Candidate = nullptr;
2374e38f100SDeLesley Hutchins // Walk back from each predecessor to find the common post-dominator node.
2384e38f100SDeLesley Hutchins for (auto *Succ : successors()) {
2394e38f100SDeLesley Hutchins // Skip back-edges
2404e38f100SDeLesley Hutchins if (Succ->BlockID <= BlockID) continue;
2414e38f100SDeLesley Hutchins // If we don't yet have a candidate for post-dominator yet, take this one.
2424e38f100SDeLesley Hutchins if (Candidate == nullptr) {
2434e38f100SDeLesley Hutchins Candidate = Succ;
2444e38f100SDeLesley Hutchins continue;
2454e38f100SDeLesley Hutchins }
2464e38f100SDeLesley Hutchins // Walk the alternate and current candidate back to find a common ancestor.
2474e38f100SDeLesley Hutchins auto *Alternate = Succ;
2484e38f100SDeLesley Hutchins while (Alternate != Candidate) {
2494e38f100SDeLesley Hutchins if (Candidate->BlockID < Alternate->BlockID)
2504e38f100SDeLesley Hutchins Candidate = Candidate->PostDominatorNode.Parent;
2514e38f100SDeLesley Hutchins else
2524e38f100SDeLesley Hutchins Alternate = Alternate->PostDominatorNode.Parent;
2534e38f100SDeLesley Hutchins }
2544e38f100SDeLesley Hutchins }
2554e38f100SDeLesley Hutchins PostDominatorNode.Parent = Candidate;
2564e38f100SDeLesley Hutchins PostDominatorNode.SizeOfSubTree = 1;
2574e38f100SDeLesley Hutchins }
2584e38f100SDeLesley Hutchins
2594e38f100SDeLesley Hutchins // Renumber instructions in all blocks
renumberInstrs()2604e38f100SDeLesley Hutchins void SCFG::renumberInstrs() {
26188d85365SAaron Puchert unsigned InstrID = 0;
2624e38f100SDeLesley Hutchins for (auto *Block : Blocks)
2634e38f100SDeLesley Hutchins InstrID = Block->renumberInstrs(InstrID);
2644e38f100SDeLesley Hutchins }
2654e38f100SDeLesley Hutchins
computeNodeSize(BasicBlock * B,BasicBlock::TopologyNode BasicBlock::* TN)2664e38f100SDeLesley Hutchins static inline void computeNodeSize(BasicBlock *B,
2674e38f100SDeLesley Hutchins BasicBlock::TopologyNode BasicBlock::*TN) {
2684e38f100SDeLesley Hutchins BasicBlock::TopologyNode *N = &(B->*TN);
2694e38f100SDeLesley Hutchins if (N->Parent) {
2704e38f100SDeLesley Hutchins BasicBlock::TopologyNode *P = &(N->Parent->*TN);
2714e38f100SDeLesley Hutchins // Initially set ID relative to the (as yet uncomputed) parent ID
2724e38f100SDeLesley Hutchins N->NodeID = P->SizeOfSubTree;
2734e38f100SDeLesley Hutchins P->SizeOfSubTree += N->SizeOfSubTree;
2744e38f100SDeLesley Hutchins }
2754e38f100SDeLesley Hutchins }
2764e38f100SDeLesley Hutchins
computeNodeID(BasicBlock * B,BasicBlock::TopologyNode BasicBlock::* TN)2774e38f100SDeLesley Hutchins static inline void computeNodeID(BasicBlock *B,
2784e38f100SDeLesley Hutchins BasicBlock::TopologyNode BasicBlock::*TN) {
2794e38f100SDeLesley Hutchins BasicBlock::TopologyNode *N = &(B->*TN);
2804e38f100SDeLesley Hutchins if (N->Parent) {
2814e38f100SDeLesley Hutchins BasicBlock::TopologyNode *P = &(N->Parent->*TN);
2824e38f100SDeLesley Hutchins N->NodeID += P->NodeID; // Fix NodeIDs relative to starting node.
2834e38f100SDeLesley Hutchins }
2844e38f100SDeLesley Hutchins }
2854e38f100SDeLesley Hutchins
2864e38f100SDeLesley Hutchins // Normalizes a CFG. Normalization has a few major components:
2874e38f100SDeLesley Hutchins // 1) Removing unreachable blocks.
2884e38f100SDeLesley Hutchins // 2) Computing dominators and post-dominators
2894e38f100SDeLesley Hutchins // 3) Topologically sorting the blocks into the "Blocks" array.
computeNormalForm()2904e38f100SDeLesley Hutchins void SCFG::computeNormalForm() {
2914e38f100SDeLesley Hutchins // Topologically sort the blocks starting from the entry block.
29288d85365SAaron Puchert unsigned NumUnreachableBlocks = Entry->topologicalSort(Blocks, Blocks.size());
2934e38f100SDeLesley Hutchins if (NumUnreachableBlocks > 0) {
2944e38f100SDeLesley Hutchins // If there were unreachable blocks shift everything down, and delete them.
29588d85365SAaron Puchert for (unsigned I = NumUnreachableBlocks, E = Blocks.size(); I < E; ++I) {
29688d85365SAaron Puchert unsigned NI = I - NumUnreachableBlocks;
2974e38f100SDeLesley Hutchins Blocks[NI] = Blocks[I];
2984e38f100SDeLesley Hutchins Blocks[NI]->BlockID = NI;
2994e38f100SDeLesley Hutchins // FIXME: clean up predecessor pointers to unreachable blocks?
3004e38f100SDeLesley Hutchins }
3014e38f100SDeLesley Hutchins Blocks.drop(NumUnreachableBlocks);
3024e38f100SDeLesley Hutchins }
3034e38f100SDeLesley Hutchins
3044e38f100SDeLesley Hutchins // Compute dominators.
3054e38f100SDeLesley Hutchins for (auto *Block : Blocks)
3064e38f100SDeLesley Hutchins Block->computeDominator();
3074e38f100SDeLesley Hutchins
3084e38f100SDeLesley Hutchins // Once dominators have been computed, the final sort may be performed.
30988d85365SAaron Puchert unsigned NumBlocks = Exit->topologicalFinalSort(Blocks, 0);
310560cbf50SJustin Bogner assert(static_cast<size_t>(NumBlocks) == Blocks.size());
3114e38f100SDeLesley Hutchins (void) NumBlocks;
3124e38f100SDeLesley Hutchins
3134e38f100SDeLesley Hutchins // Renumber the instructions now that we have a final sort.
3144e38f100SDeLesley Hutchins renumberInstrs();
3154e38f100SDeLesley Hutchins
3164e38f100SDeLesley Hutchins // Compute post-dominators and compute the sizes of each node in the
3174e38f100SDeLesley Hutchins // dominator tree.
3184e38f100SDeLesley Hutchins for (auto *Block : Blocks.reverse()) {
3194e38f100SDeLesley Hutchins Block->computePostDominator();
3204e38f100SDeLesley Hutchins computeNodeSize(Block, &BasicBlock::DominatorNode);
3214e38f100SDeLesley Hutchins }
3224e38f100SDeLesley Hutchins // Compute the sizes of each node in the post-dominator tree and assign IDs in
3234e38f100SDeLesley Hutchins // the dominator tree.
3244e38f100SDeLesley Hutchins for (auto *Block : Blocks) {
3254e38f100SDeLesley Hutchins computeNodeID(Block, &BasicBlock::DominatorNode);
3264e38f100SDeLesley Hutchins computeNodeSize(Block, &BasicBlock::PostDominatorNode);
3274e38f100SDeLesley Hutchins }
3284e38f100SDeLesley Hutchins // Assign IDs in the post-dominator tree.
3294e38f100SDeLesley Hutchins for (auto *Block : Blocks.reverse()) {
3304e38f100SDeLesley Hutchins computeNodeID(Block, &BasicBlock::PostDominatorNode);
3314e38f100SDeLesley Hutchins }
3324e38f100SDeLesley Hutchins }
333