blob: 7e44574a6e864b96031964094dd581753a6223e2 [file] [log] [blame]
Chris Lattnerd76efa02002-09-24 00:08:39 +00001//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd76efa02002-09-24 00:08:39 +00009//
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
Chris Lattner363ca612003-11-10 04:42:42 +000014// forward dominator (set, immediate dominators, tree, and frontier)
15// information.
Chris Lattnerd76efa02002-09-24 00:08:39 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Transforms/Scalar.h"
Chris Lattnerd23520c2003-11-10 04:10:50 +000020#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerd76efa02002-09-24 00:08:39 +000021#include "llvm/Analysis/Dominators.h"
Chris Lattner0ae380a2005-08-13 01:38:43 +000022#include "llvm/Analysis/LoopInfo.h"
Chris Lattnerd76efa02002-09-24 00:08:39 +000023#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000024#include "llvm/Instructions.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000025#include "llvm/Type.h"
Chris Lattnereb0456c2002-09-24 15:51:56 +000026#include "llvm/Support/CFG.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/ADT/Statistic.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner6de302b2002-09-24 15:43:12 +000030namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000031 Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
Chris Lattnerd76efa02002-09-24 00:08:39 +000032
Chris Lattner6de302b2002-09-24 15:43:12 +000033 struct BreakCriticalEdges : public FunctionPass {
34 virtual bool runOnFunction(Function &F);
Misha Brukmanfd939082005-04-21 23:48:37 +000035
Chris Lattner6de302b2002-09-24 15:43:12 +000036 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
37 AU.addPreserved<DominatorSet>();
38 AU.addPreserved<ImmediateDominators>();
39 AU.addPreserved<DominatorTree>();
Chris Lattner6918c072002-10-31 02:44:36 +000040 AU.addPreserved<DominanceFrontier>();
Chris Lattner0ae380a2005-08-13 01:38:43 +000041 AU.addPreserved<LoopInfo>();
Chris Lattner98bf4362003-10-12 21:52:28 +000042
43 // No loop canonicalization guarantees are broken by this pass.
44 AU.addPreservedID(LoopSimplifyID);
Chris Lattner6de302b2002-09-24 15:43:12 +000045 }
46 };
Chris Lattnerd76efa02002-09-24 00:08:39 +000047
Chris Lattner6de302b2002-09-24 15:43:12 +000048 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
49 "Break critical edges in CFG");
50}
Chris Lattnerd76efa02002-09-24 00:08:39 +000051
Chris Lattnereb0456c2002-09-24 15:51:56 +000052// Publically exposed interface to pass...
Chris Lattnerf7703df2004-01-09 06:12:26 +000053const PassInfo *llvm::BreakCriticalEdgesID = X.getPassInfo();
Chris Lattner1e5fdf82004-07-31 10:01:58 +000054FunctionPass *llvm::createBreakCriticalEdgesPass() {
55 return new BreakCriticalEdges();
56}
Chris Lattnerd76efa02002-09-24 00:08:39 +000057
Chris Lattner363ca612003-11-10 04:42:42 +000058// runOnFunction - Loop over all of the edges in the CFG, breaking critical
59// edges as they are found.
60//
61bool BreakCriticalEdges::runOnFunction(Function &F) {
62 bool Changed = false;
63 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
64 TerminatorInst *TI = I->getTerminator();
65 if (TI->getNumSuccessors() > 1)
66 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
67 if (SplitCriticalEdge(TI, i, this)) {
68 ++NumBroken;
69 Changed = true;
70 }
71 }
72
73 return Changed;
74}
75
76//===----------------------------------------------------------------------===//
77// Implementation of the external critical edge manipulation functions
78//===----------------------------------------------------------------------===//
Chris Lattnereb0456c2002-09-24 15:51:56 +000079
80// isCriticalEdge - Return true if the specified edge is a critical edge.
81// Critical edges are edges from a block with multiple successors to a block
82// with multiple predecessors.
83//
Chris Lattnerf7703df2004-01-09 06:12:26 +000084bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
Chris Lattnereb0456c2002-09-24 15:51:56 +000085 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
Chris Lattnere802a022002-10-08 21:06:27 +000086 if (TI->getNumSuccessors() == 1) return false;
Chris Lattnereb0456c2002-09-24 15:51:56 +000087
88 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
89 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
90
91 // If there is more than one predecessor, this is a critical edge...
92 assert(I != E && "No preds, but we have an edge to the block?");
93 ++I; // Skip one edge due to the incoming arc from TI.
94 return I != E;
95}
96
Chris Lattnerd23520c2003-11-10 04:10:50 +000097// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
98// split the critical edge. This will update DominatorSet, ImmediateDominator,
99// DominatorTree, and DominatorFrontier information if it is available, thus
100// calling this pass will not invalidate either of them. This returns true if
101// the edge was split, false otherwise.
Chris Lattnereb0456c2002-09-24 15:51:56 +0000102//
Chris Lattnerf7703df2004-01-09 06:12:26 +0000103bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
Chris Lattnerd23520c2003-11-10 04:10:50 +0000104 if (!isCriticalEdge(TI, SuccNum)) return false;
Chris Lattnereb0456c2002-09-24 15:51:56 +0000105 BasicBlock *TIBB = TI->getParent();
Chris Lattner6918c072002-10-31 02:44:36 +0000106 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000107
108 // Create a new basic block, linking it into the CFG.
Chris Lattner6918c072002-10-31 02:44:36 +0000109 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
110 DestBB->getName() + "_crit_edge");
Chris Lattnereb0456c2002-09-24 15:51:56 +0000111 // Create our unconditional branch...
Chris Lattner108e4ab2003-11-21 16:52:05 +0000112 new BranchInst(DestBB, NewBB);
Misha Brukmanfd939082005-04-21 23:48:37 +0000113
Chris Lattnereb0456c2002-09-24 15:51:56 +0000114 // Branch to the new block, breaking the edge...
115 TI->setSuccessor(SuccNum, NewBB);
116
117 // Insert the block into the function... right after the block TI lives in.
118 Function &F = *TIBB->getParent();
119 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
120
121 // If there are any PHI nodes in DestBB, we need to update them so that they
122 // merge incoming values from NewBB instead of from TIBB.
123 //
Reid Spencer2da5c3d2004-09-15 17:06:42 +0000124 for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
125 PHINode *PN = cast<PHINode>(I);
Chris Lattner06887c92004-02-29 22:24:41 +0000126 // We no longer enter through TIBB, now we come in through NewBB. Revector
127 // exactly one entry in the PHI node that used to come from TIBB to come
128 // from NewBB.
Chris Lattnerb01bfd42005-08-12 21:58:07 +0000129 int BBIdx = PN->getBasicBlockIndex(TIBB);
130 PN->setIncomingBlock(BBIdx, NewBB);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000131 }
132
Chris Lattnere802a022002-10-08 21:06:27 +0000133 // If we don't have a pass object, we can't update anything...
Chris Lattnerd23520c2003-11-10 04:10:50 +0000134 if (P == 0) return true;
Chris Lattnere802a022002-10-08 21:06:27 +0000135
Chris Lattnerc178d942002-09-26 16:18:51 +0000136 // Now update analysis information. These are the analyses that we are
137 // currently capable of updating...
Chris Lattnereb0456c2002-09-24 15:51:56 +0000138 //
Chris Lattnereb0456c2002-09-24 15:51:56 +0000139
Chris Lattnerc178d942002-09-26 16:18:51 +0000140 // Should we update DominatorSet information?
Chris Lattnere802a022002-10-08 21:06:27 +0000141 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000142 // The blocks that dominate the new one are the blocks that dominate TIBB
143 // plus the new block itself.
144 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
145 DomSet.insert(NewBB); // A block always dominates itself.
146 DS->addBasicBlock(NewBB, DomSet);
147 }
Chris Lattnereb0456c2002-09-24 15:51:56 +0000148
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000149 // Should we update ImmediateDominator information?
Chris Lattnere802a022002-10-08 21:06:27 +0000150 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000151 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
152 // anything.
153 ID->addNewBlock(NewBB, TIBB);
154 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000155
Chris Lattnerc178d942002-09-26 16:18:51 +0000156 // Should we update DominatorTree information?
Chris Lattnere802a022002-10-08 21:06:27 +0000157 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000158 DominatorTree::Node *TINode = DT->getNode(TIBB);
Misha Brukmanfd939082005-04-21 23:48:37 +0000159
Chris Lattnerc178d942002-09-26 16:18:51 +0000160 // The new block is not the immediate dominator for any other nodes, but
161 // TINode is the immediate dominator for the new node.
162 //
Chris Lattnerd3d06a52002-10-08 21:53:51 +0000163 if (TINode) // Don't break unreachable code!
164 DT->createNewNode(NewBB, TINode);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000165 }
Chris Lattner6918c072002-10-31 02:44:36 +0000166
167 // Should we update DominanceFrontier information?
168 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) {
169 // Since the new block is dominated by its only predecessor TIBB,
170 // it cannot be in any block's dominance frontier. Its dominance
171 // frontier is {DestBB}.
172 DominanceFrontier::DomSetType NewDFSet;
173 NewDFSet.insert(DestBB);
174 DF->addBasicBlock(NewBB, NewDFSet);
175 }
Chris Lattner0ae380a2005-08-13 01:38:43 +0000176
177 // Update LoopInfo if it is around.
178 if (LoopInfo *LI = P->getAnalysisToUpdate<LoopInfo>()) {
179 // If one or the other blocks were not in a loop, the new block is not
180 // either, and thus LI doesn't need to be updated.
181 if (Loop *TIL = LI->getLoopFor(TIBB))
182 if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
183 if (TIL == DestLoop) {
184 // Both in the same loop, the NewBB joins loop.
185 DestLoop->addBasicBlockToLoop(NewBB, *LI);
186 } else if (TIL->contains(DestLoop->getHeader())) {
187 // Edge from an outer loop to an inner loop. Add to the outer lopo.
188 TIL->addBasicBlockToLoop(NewBB, *LI);
189 } else if (DestLoop->contains(TIL->getHeader())) {
190 // Edge from an inner loop to an outer loop. Add to the outer lopo.
191 DestLoop->addBasicBlockToLoop(NewBB, *LI);
192 } else {
193 // Edge from two loops with no containment relation. Because these
194 // are natural loops, we know that the destination block must be the
195 // header of its loop (adding a branch into a loop elsewhere would
196 // create an irreducible loop).
197 assert(DestLoop->getHeader() == DestBB &&
198 "Should not create irreducible loops!");
199 if (Loop *P = DestLoop->getParentLoop())
200 P->addBasicBlockToLoop(NewBB, *LI);
201 }
202 }
203
204 }
Chris Lattnerd23520c2003-11-10 04:10:50 +0000205 return true;
Chris Lattnereb0456c2002-09-24 15:51:56 +0000206}