blob: cc822dae86adbfa8764621a1db4bd1e43b38619d [file] [log] [blame]
Chris Lattnerb03832d2002-09-24 00:08:39 +00001//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb03832d2002-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 Lattner1e6d3052003-11-10 04:42:42 +000014// forward dominator (set, immediate dominators, tree, and frontier)
15// information.
Chris Lattnerb03832d2002-09-24 00:08:39 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Transforms/Scalar.h"
Chris Lattner44743362003-11-10 04:10:50 +000020#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerb03832d2002-09-24 00:08:39 +000021#include "llvm/Analysis/Dominators.h"
Chris Lattner89c1dfc2005-08-13 01:38:43 +000022#include "llvm/Analysis/LoopInfo.h"
Chris Lattnerb03832d2002-09-24 00:08:39 +000023#include "llvm/Function.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000024#include "llvm/Instructions.h"
Chris Lattner7b9020a2005-03-17 15:38:16 +000025#include "llvm/Type.h"
Chris Lattner75f80bd2002-09-24 15:51:56 +000026#include "llvm/Support/CFG.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/ADT/Statistic.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner4bec6652002-09-24 15:43:12 +000031namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000032 Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
Chris Lattnerb03832d2002-09-24 00:08:39 +000033
Chris Lattner996795b2006-06-28 23:17:24 +000034 struct VISIBILITY_HIDDEN BreakCriticalEdges : public FunctionPass {
Chris Lattner4bec6652002-09-24 15:43:12 +000035 virtual bool runOnFunction(Function &F);
Misha Brukmanb1c93172005-04-21 23:48:37 +000036
Chris Lattner4bec6652002-09-24 15:43:12 +000037 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner0770d8e2006-01-11 05:11:13 +000038 AU.addPreserved<ETForest>();
Chris Lattner4bec6652002-09-24 15:43:12 +000039 AU.addPreserved<DominatorSet>();
40 AU.addPreserved<ImmediateDominators>();
41 AU.addPreserved<DominatorTree>();
Chris Lattner12764c82002-10-31 02:44:36 +000042 AU.addPreserved<DominanceFrontier>();
Chris Lattner89c1dfc2005-08-13 01:38:43 +000043 AU.addPreserved<LoopInfo>();
Chris Lattner72272a72003-10-12 21:52:28 +000044
45 // No loop canonicalization guarantees are broken by this pass.
46 AU.addPreservedID(LoopSimplifyID);
Chris Lattner4bec6652002-09-24 15:43:12 +000047 }
48 };
Chris Lattnerb03832d2002-09-24 00:08:39 +000049
Chris Lattner4bec6652002-09-24 15:43:12 +000050 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
51 "Break critical edges in CFG");
52}
Chris Lattnerb03832d2002-09-24 00:08:39 +000053
Chris Lattner75f80bd2002-09-24 15:51:56 +000054// Publically exposed interface to pass...
Chris Lattnerdf3c3422004-01-09 06:12:26 +000055const PassInfo *llvm::BreakCriticalEdgesID = X.getPassInfo();
Chris Lattner7471b962004-07-31 10:01:58 +000056FunctionPass *llvm::createBreakCriticalEdgesPass() {
57 return new BreakCriticalEdges();
58}
Chris Lattnerb03832d2002-09-24 00:08:39 +000059
Chris Lattner1e6d3052003-11-10 04:42:42 +000060// runOnFunction - Loop over all of the edges in the CFG, breaking critical
61// edges as they are found.
62//
63bool BreakCriticalEdges::runOnFunction(Function &F) {
64 bool Changed = false;
65 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
66 TerminatorInst *TI = I->getTerminator();
67 if (TI->getNumSuccessors() > 1)
68 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
69 if (SplitCriticalEdge(TI, i, this)) {
70 ++NumBroken;
71 Changed = true;
72 }
73 }
74
75 return Changed;
76}
77
78//===----------------------------------------------------------------------===//
79// Implementation of the external critical edge manipulation functions
80//===----------------------------------------------------------------------===//
Chris Lattner75f80bd2002-09-24 15:51:56 +000081
82// isCriticalEdge - Return true if the specified edge is a critical edge.
83// Critical edges are edges from a block with multiple successors to a block
84// with multiple predecessors.
85//
Chris Lattnerdf3c3422004-01-09 06:12:26 +000086bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
Chris Lattner75f80bd2002-09-24 15:51:56 +000087 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
Chris Lattner5ac72de2002-10-08 21:06:27 +000088 if (TI->getNumSuccessors() == 1) return false;
Chris Lattner75f80bd2002-09-24 15:51:56 +000089
90 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
91 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
92
93 // If there is more than one predecessor, this is a critical edge...
94 assert(I != E && "No preds, but we have an edge to the block?");
95 ++I; // Skip one edge due to the incoming arc from TI.
96 return I != E;
97}
98
Chris Lattner44743362003-11-10 04:10:50 +000099// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
100// split the critical edge. This will update DominatorSet, ImmediateDominator,
101// DominatorTree, and DominatorFrontier information if it is available, thus
102// calling this pass will not invalidate either of them. This returns true if
103// the edge was split, false otherwise.
Chris Lattner75f80bd2002-09-24 15:51:56 +0000104//
Chris Lattnerdf3c3422004-01-09 06:12:26 +0000105bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
Chris Lattner44743362003-11-10 04:10:50 +0000106 if (!isCriticalEdge(TI, SuccNum)) return false;
Chris Lattner75f80bd2002-09-24 15:51:56 +0000107 BasicBlock *TIBB = TI->getParent();
Chris Lattner12764c82002-10-31 02:44:36 +0000108 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000109
110 // Create a new basic block, linking it into the CFG.
Chris Lattner12764c82002-10-31 02:44:36 +0000111 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
112 DestBB->getName() + "_crit_edge");
Chris Lattner75f80bd2002-09-24 15:51:56 +0000113 // Create our unconditional branch...
Chris Lattnera2960002003-11-21 16:52:05 +0000114 new BranchInst(DestBB, NewBB);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000115
Chris Lattner75f80bd2002-09-24 15:51:56 +0000116 // Branch to the new block, breaking the edge...
117 TI->setSuccessor(SuccNum, NewBB);
118
119 // Insert the block into the function... right after the block TI lives in.
120 Function &F = *TIBB->getParent();
121 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
122
123 // If there are any PHI nodes in DestBB, we need to update them so that they
124 // merge incoming values from NewBB instead of from TIBB.
125 //
Reid Spencer66149462004-09-15 17:06:42 +0000126 for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
127 PHINode *PN = cast<PHINode>(I);
Chris Lattner2de229f2004-02-29 22:24:41 +0000128 // We no longer enter through TIBB, now we come in through NewBB. Revector
129 // exactly one entry in the PHI node that used to come from TIBB to come
130 // from NewBB.
Chris Lattnerb7ebe652005-08-12 21:58:07 +0000131 int BBIdx = PN->getBasicBlockIndex(TIBB);
132 PN->setIncomingBlock(BBIdx, NewBB);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000133 }
134
Chris Lattner5ac72de2002-10-08 21:06:27 +0000135 // If we don't have a pass object, we can't update anything...
Chris Lattner44743362003-11-10 04:10:50 +0000136 if (P == 0) return true;
Chris Lattner5ac72de2002-10-08 21:06:27 +0000137
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000138 // Now update analysis information. These are the analyses that we are
139 // currently capable of updating...
Chris Lattner75f80bd2002-09-24 15:51:56 +0000140 //
Chris Lattner75f80bd2002-09-24 15:51:56 +0000141
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000142 // Should we update DominatorSet information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000143 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000144 // The blocks that dominate the new one are the blocks that dominate TIBB
145 // plus the new block itself.
146 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
147 DomSet.insert(NewBB); // A block always dominates itself.
148 DS->addBasicBlock(NewBB, DomSet);
149 }
Chris Lattner75f80bd2002-09-24 15:51:56 +0000150
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000151 // Should we update ImmediateDominator information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000152 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000153 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
154 // anything.
155 ID->addNewBlock(NewBB, TIBB);
156 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000157
Chris Lattner0770d8e2006-01-11 05:11:13 +0000158 // Update the forest?
159 if (ETForest *EF = P->getAnalysisToUpdate<ETForest>())
160 EF->addNewBlock(NewBB, TIBB);
161
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000162 // Should we update DominatorTree information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000163 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000164 DominatorTree::Node *TINode = DT->getNode(TIBB);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000165
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000166 // The new block is not the immediate dominator for any other nodes, but
167 // TINode is the immediate dominator for the new node.
168 //
Chris Lattnerb7368502002-10-08 21:53:51 +0000169 if (TINode) // Don't break unreachable code!
170 DT->createNewNode(NewBB, TINode);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000171 }
Chris Lattner12764c82002-10-31 02:44:36 +0000172
173 // Should we update DominanceFrontier information?
174 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) {
175 // Since the new block is dominated by its only predecessor TIBB,
176 // it cannot be in any block's dominance frontier. Its dominance
177 // frontier is {DestBB}.
178 DominanceFrontier::DomSetType NewDFSet;
179 NewDFSet.insert(DestBB);
180 DF->addBasicBlock(NewBB, NewDFSet);
181 }
Chris Lattner89c1dfc2005-08-13 01:38:43 +0000182
183 // Update LoopInfo if it is around.
184 if (LoopInfo *LI = P->getAnalysisToUpdate<LoopInfo>()) {
185 // If one or the other blocks were not in a loop, the new block is not
186 // either, and thus LI doesn't need to be updated.
187 if (Loop *TIL = LI->getLoopFor(TIBB))
188 if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
189 if (TIL == DestLoop) {
190 // Both in the same loop, the NewBB joins loop.
191 DestLoop->addBasicBlockToLoop(NewBB, *LI);
192 } else if (TIL->contains(DestLoop->getHeader())) {
193 // Edge from an outer loop to an inner loop. Add to the outer lopo.
194 TIL->addBasicBlockToLoop(NewBB, *LI);
195 } else if (DestLoop->contains(TIL->getHeader())) {
196 // Edge from an inner loop to an outer loop. Add to the outer lopo.
197 DestLoop->addBasicBlockToLoop(NewBB, *LI);
198 } else {
199 // Edge from two loops with no containment relation. Because these
200 // are natural loops, we know that the destination block must be the
201 // header of its loop (adding a branch into a loop elsewhere would
202 // create an irreducible loop).
203 assert(DestLoop->getHeader() == DestBB &&
204 "Should not create irreducible loops!");
205 if (Loop *P = DestLoop->getParentLoop())
206 P->addBasicBlockToLoop(NewBB, *LI);
207 }
208 }
209
210 }
Chris Lattner44743362003-11-10 04:10:50 +0000211 return true;
Chris Lattner75f80bd2002-09-24 15:51:56 +0000212}