blob: 0c4974db10ac4c325d94ceb981ace69ec4e5d53a [file] [log] [blame]
Chris Lattnerd76efa02002-09-24 00:08:39 +00001//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
2//
3// BreakCriticalEdges pass - Break all of the critical edges in the CFG by
4// inserting a dummy basic block. This pass may be "required" by passes that
5// cannot deal with critical edges. For this usage, the structure type is
6// forward declared. This pass obviously invalidates the CFG, but can update
7// forward dominator (set, immediate dominators, and tree) information.
8//
9//===----------------------------------------------------------------------===//
10
11#include "llvm/Transforms/Scalar.h"
Chris Lattnerd76efa02002-09-24 00:08:39 +000012#include "llvm/Analysis/Dominators.h"
13#include "llvm/Function.h"
Chris Lattnereb0456c2002-09-24 15:51:56 +000014#include "llvm/iTerminators.h"
15#include "llvm/iPHINode.h"
16#include "llvm/Support/CFG.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000017#include "Support/Statistic.h"
Chris Lattnerd76efa02002-09-24 00:08:39 +000018
Chris Lattner6de302b2002-09-24 15:43:12 +000019namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000020 Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
Chris Lattnerd76efa02002-09-24 00:08:39 +000021
Chris Lattner6de302b2002-09-24 15:43:12 +000022 struct BreakCriticalEdges : public FunctionPass {
23 virtual bool runOnFunction(Function &F);
24
25 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
26 AU.addPreserved<DominatorSet>();
27 AU.addPreserved<ImmediateDominators>();
28 AU.addPreserved<DominatorTree>();
Chris Lattnerc178d942002-09-26 16:18:51 +000029 AU.addPreservedID(LoopPreheadersID); // No preheaders deleted.
Chris Lattner6de302b2002-09-24 15:43:12 +000030 }
31 };
Chris Lattnerd76efa02002-09-24 00:08:39 +000032
Chris Lattner6de302b2002-09-24 15:43:12 +000033 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
34 "Break critical edges in CFG");
35}
Chris Lattnerd76efa02002-09-24 00:08:39 +000036
Chris Lattnereb0456c2002-09-24 15:51:56 +000037// Publically exposed interface to pass...
Chris Lattner6de302b2002-09-24 15:43:12 +000038const PassInfo *BreakCriticalEdgesID = X.getPassInfo();
Chris Lattnerd76efa02002-09-24 00:08:39 +000039Pass *createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); }
40
Chris Lattnereb0456c2002-09-24 15:51:56 +000041
42// isCriticalEdge - Return true if the specified edge is a critical edge.
43// Critical edges are edges from a block with multiple successors to a block
44// with multiple predecessors.
45//
Chris Lattnere802a022002-10-08 21:06:27 +000046bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
Chris Lattnereb0456c2002-09-24 15:51:56 +000047 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
Chris Lattnere802a022002-10-08 21:06:27 +000048 if (TI->getNumSuccessors() == 1) return false;
Chris Lattnereb0456c2002-09-24 15:51:56 +000049
50 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
51 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
52
53 // If there is more than one predecessor, this is a critical edge...
54 assert(I != E && "No preds, but we have an edge to the block?");
55 ++I; // Skip one edge due to the incoming arc from TI.
56 return I != E;
57}
58
59// SplitCriticalEdge - Insert a new node node to split the critical edge. This
60// will update DominatorSet, ImmediateDominator and DominatorTree information if
61// it is available, thus calling this pass will not invalidate either of them.
62//
Chris Lattnere802a022002-10-08 21:06:27 +000063void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
Chris Lattnereb0456c2002-09-24 15:51:56 +000064 assert(isCriticalEdge(TI, SuccNum) &&
65 "Cannot break a critical edge, if it isn't a critical edge");
66 BasicBlock *TIBB = TI->getParent();
67
68 // Create a new basic block, linking it into the CFG.
69 BasicBlock *NewBB = new BasicBlock(TIBB->getName()+"_crit_edge");
70 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
71 // Create our unconditional branch...
72 BranchInst *BI = new BranchInst(DestBB);
73 NewBB->getInstList().push_back(BI);
74
75 // Branch to the new block, breaking the edge...
76 TI->setSuccessor(SuccNum, NewBB);
77
78 // Insert the block into the function... right after the block TI lives in.
79 Function &F = *TIBB->getParent();
80 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
81
82 // If there are any PHI nodes in DestBB, we need to update them so that they
83 // merge incoming values from NewBB instead of from TIBB.
84 //
85 for (BasicBlock::iterator I = DestBB->begin();
86 PHINode *PN = dyn_cast<PHINode>(&*I); ++I) {
87 // We no longer enter through TIBB, now we come in through NewBB.
88 PN->replaceUsesOfWith(TIBB, NewBB);
89 }
90
Chris Lattnere802a022002-10-08 21:06:27 +000091 // If we don't have a pass object, we can't update anything...
92 if (P == 0) return;
93
Chris Lattnerc178d942002-09-26 16:18:51 +000094 // Now update analysis information. These are the analyses that we are
95 // currently capable of updating...
Chris Lattnereb0456c2002-09-24 15:51:56 +000096 //
Chris Lattnereb0456c2002-09-24 15:51:56 +000097
Chris Lattnerc178d942002-09-26 16:18:51 +000098 // Should we update DominatorSet information?
Chris Lattnere802a022002-10-08 21:06:27 +000099 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000100 // The blocks that dominate the new one are the blocks that dominate TIBB
101 // plus the new block itself.
102 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
103 DomSet.insert(NewBB); // A block always dominates itself.
104 DS->addBasicBlock(NewBB, DomSet);
105 }
Chris Lattnereb0456c2002-09-24 15:51:56 +0000106
Chris Lattnerc178d942002-09-26 16:18:51 +0000107 // Should we update ImmdediateDominator information?
Chris Lattnere802a022002-10-08 21:06:27 +0000108 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000109 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
110 // anything.
111 ID->addNewBlock(NewBB, TIBB);
112 }
113
114 // Should we update DominatorTree information?
Chris Lattnere802a022002-10-08 21:06:27 +0000115 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000116 DominatorTree::Node *TINode = DT->getNode(TIBB);
117
118 // The new block is not the immediate dominator for any other nodes, but
119 // TINode is the immediate dominator for the new node.
120 //
Chris Lattnerd3d06a52002-10-08 21:53:51 +0000121 if (TINode) // Don't break unreachable code!
122 DT->createNewNode(NewBB, TINode);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000123 }
124}
125
Chris Lattnerd76efa02002-09-24 00:08:39 +0000126// runOnFunction - Loop over all of the edges in the CFG, breaking critical
127// edges as they are found.
128//
129bool BreakCriticalEdges::runOnFunction(Function &F) {
130 bool Changed = false;
131 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
132 TerminatorInst *TI = I->getTerminator();
Chris Lattnereb0456c2002-09-24 15:51:56 +0000133 if (TI->getNumSuccessors() > 1)
134 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
135 if (isCriticalEdge(TI, i)) {
Chris Lattnere802a022002-10-08 21:06:27 +0000136 SplitCriticalEdge(TI, i, this);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000137 ++NumBroken;
138 Changed = true;
139 }
Chris Lattnerd76efa02002-09-24 00:08:39 +0000140 }
141
142 return Changed;
143}