1 //===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // BreakCriticalEdges pass - Break all of the critical edges in the CFG by
11 // inserting a dummy basic block. This pass may be "required" by passes that
12 // cannot deal with critical edges. For this usage, the structure type is
13 // forward declared. This pass obviously invalidates the CFG, but can update
14 // forward dominator (set, immediate dominators, tree, and frontier)
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
21 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/Function.h"
23 #include "llvm/Instructions.h"
24 #include "llvm/Type.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/ADT/Statistic.h"
30 Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
32 struct BreakCriticalEdges : public FunctionPass {
33 virtual bool runOnFunction(Function &F);
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.addPreserved<DominatorSet>();
37 AU.addPreserved<ImmediateDominators>();
38 AU.addPreserved<DominatorTree>();
39 AU.addPreserved<DominanceFrontier>();
41 // No loop canonicalization guarantees are broken by this pass.
42 AU.addPreservedID(LoopSimplifyID);
46 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
47 "Break critical edges in CFG");
50 // Publically exposed interface to pass...
51 const PassInfo *llvm::BreakCriticalEdgesID = X.getPassInfo();
52 FunctionPass *llvm::createBreakCriticalEdgesPass() {
53 return new BreakCriticalEdges();
56 // runOnFunction - Loop over all of the edges in the CFG, breaking critical
57 // edges as they are found.
59 bool BreakCriticalEdges::runOnFunction(Function &F) {
61 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
62 TerminatorInst *TI = I->getTerminator();
63 if (TI->getNumSuccessors() > 1)
64 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
65 if (SplitCriticalEdge(TI, i, this)) {
74 //===----------------------------------------------------------------------===//
75 // Implementation of the external critical edge manipulation functions
76 //===----------------------------------------------------------------------===//
78 // isCriticalEdge - Return true if the specified edge is a critical edge.
79 // Critical edges are edges from a block with multiple successors to a block
80 // with multiple predecessors.
82 bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
83 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
84 if (TI->getNumSuccessors() == 1) return false;
86 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
87 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
89 // If there is more than one predecessor, this is a critical edge...
90 assert(I != E && "No preds, but we have an edge to the block?");
91 ++I; // Skip one edge due to the incoming arc from TI.
95 // SplitCriticalEdge - If this edge is a critical edge, insert a new node to
96 // split the critical edge. This will update DominatorSet, ImmediateDominator,
97 // DominatorTree, and DominatorFrontier information if it is available, thus
98 // calling this pass will not invalidate either of them. This returns true if
99 // the edge was split, false otherwise.
101 bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
102 if (!isCriticalEdge(TI, SuccNum)) return false;
103 BasicBlock *TIBB = TI->getParent();
104 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
106 // Create a new basic block, linking it into the CFG.
107 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
108 DestBB->getName() + "_crit_edge");
109 // Create our unconditional branch...
110 new BranchInst(DestBB, NewBB);
112 // Branch to the new block, breaking the edge...
113 TI->setSuccessor(SuccNum, NewBB);
115 // Insert the block into the function... right after the block TI lives in.
116 Function &F = *TIBB->getParent();
117 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
119 // If there are any PHI nodes in DestBB, we need to update them so that they
120 // merge incoming values from NewBB instead of from TIBB.
122 for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
123 PHINode *PN = cast<PHINode>(I);
124 // We no longer enter through TIBB, now we come in through NewBB. Revector
125 // exactly one entry in the PHI node that used to come from TIBB to come
127 Value *InVal = PN->removeIncomingValue(TIBB, false);
128 PN->addIncoming(InVal, NewBB);
131 // If we don't have a pass object, we can't update anything...
132 if (P == 0) return true;
134 // Now update analysis information. These are the analyses that we are
135 // currently capable of updating...
138 // Should we update DominatorSet information?
139 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
140 // The blocks that dominate the new one are the blocks that dominate TIBB
141 // plus the new block itself.
142 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
143 DomSet.insert(NewBB); // A block always dominates itself.
144 DS->addBasicBlock(NewBB, DomSet);
147 // Should we update ImmediateDominator information?
148 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
149 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
151 ID->addNewBlock(NewBB, TIBB);
154 // Should we update DominatorTree information?
155 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
156 DominatorTree::Node *TINode = DT->getNode(TIBB);
158 // The new block is not the immediate dominator for any other nodes, but
159 // TINode is the immediate dominator for the new node.
161 if (TINode) // Don't break unreachable code!
162 DT->createNewNode(NewBB, TINode);
165 // Should we update DominanceFrontier information?
166 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) {
167 // Since the new block is dominated by its only predecessor TIBB,
168 // it cannot be in any block's dominance frontier. Its dominance
169 // frontier is {DestBB}.
170 DominanceFrontier::DomSetType NewDFSet;
171 NewDFSet.insert(DestBB);
172 DF->addBasicBlock(NewBB, NewDFSet);