blob: 8f8f0eea2dae7e5e6f3c79d08a41aabb516c597f [file] [log] [blame]
Chris Lattnerb03832d2002-09-24 00:08:39 +00001//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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
14// forward dominator (set, immediate dominators, and tree) information.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/Transforms/Scalar.h"
Chris Lattnerb03832d2002-09-24 00:08:39 +000019#include "llvm/Analysis/Dominators.h"
20#include "llvm/Function.h"
Chris Lattner75f80bd2002-09-24 15:51:56 +000021#include "llvm/iTerminators.h"
22#include "llvm/iPHINode.h"
23#include "llvm/Support/CFG.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000024#include "Support/Statistic.h"
Chris Lattnerb03832d2002-09-24 00:08:39 +000025
Chris Lattner4bec6652002-09-24 15:43:12 +000026namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000027 Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
Chris Lattnerb03832d2002-09-24 00:08:39 +000028
Chris Lattner4bec6652002-09-24 15:43:12 +000029 struct BreakCriticalEdges : public FunctionPass {
30 virtual bool runOnFunction(Function &F);
31
32 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
33 AU.addPreserved<DominatorSet>();
34 AU.addPreserved<ImmediateDominators>();
35 AU.addPreserved<DominatorTree>();
Chris Lattner12764c82002-10-31 02:44:36 +000036 AU.addPreserved<DominanceFrontier>();
Chris Lattner72272a72003-10-12 21:52:28 +000037
38 // No loop canonicalization guarantees are broken by this pass.
39 AU.addPreservedID(LoopSimplifyID);
Chris Lattner4bec6652002-09-24 15:43:12 +000040 }
41 };
Chris Lattnerb03832d2002-09-24 00:08:39 +000042
Chris Lattner4bec6652002-09-24 15:43:12 +000043 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
44 "Break critical edges in CFG");
45}
Chris Lattnerb03832d2002-09-24 00:08:39 +000046
Chris Lattner75f80bd2002-09-24 15:51:56 +000047// Publically exposed interface to pass...
Chris Lattner4bec6652002-09-24 15:43:12 +000048const PassInfo *BreakCriticalEdgesID = X.getPassInfo();
Chris Lattnerb03832d2002-09-24 00:08:39 +000049Pass *createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); }
50
Chris Lattner75f80bd2002-09-24 15:51:56 +000051
52// isCriticalEdge - Return true if the specified edge is a critical edge.
53// Critical edges are edges from a block with multiple successors to a block
54// with multiple predecessors.
55//
Chris Lattner5ac72de2002-10-08 21:06:27 +000056bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
Chris Lattner75f80bd2002-09-24 15:51:56 +000057 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
Chris Lattner5ac72de2002-10-08 21:06:27 +000058 if (TI->getNumSuccessors() == 1) return false;
Chris Lattner75f80bd2002-09-24 15:51:56 +000059
60 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
61 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
62
63 // If there is more than one predecessor, this is a critical edge...
64 assert(I != E && "No preds, but we have an edge to the block?");
65 ++I; // Skip one edge due to the incoming arc from TI.
66 return I != E;
67}
68
69// SplitCriticalEdge - Insert a new node node to split the critical edge. This
70// will update DominatorSet, ImmediateDominator and DominatorTree information if
71// it is available, thus calling this pass will not invalidate either of them.
72//
Chris Lattner5ac72de2002-10-08 21:06:27 +000073void SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
Chris Lattner75f80bd2002-09-24 15:51:56 +000074 assert(isCriticalEdge(TI, SuccNum) &&
75 "Cannot break a critical edge, if it isn't a critical edge");
76 BasicBlock *TIBB = TI->getParent();
Chris Lattner12764c82002-10-31 02:44:36 +000077 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
Chris Lattner75f80bd2002-09-24 15:51:56 +000078
79 // Create a new basic block, linking it into the CFG.
Chris Lattner12764c82002-10-31 02:44:36 +000080 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
81 DestBB->getName() + "_crit_edge");
Chris Lattner75f80bd2002-09-24 15:51:56 +000082 // Create our unconditional branch...
83 BranchInst *BI = new BranchInst(DestBB);
84 NewBB->getInstList().push_back(BI);
85
86 // Branch to the new block, breaking the edge...
87 TI->setSuccessor(SuccNum, NewBB);
88
89 // Insert the block into the function... right after the block TI lives in.
90 Function &F = *TIBB->getParent();
91 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
92
93 // If there are any PHI nodes in DestBB, we need to update them so that they
94 // merge incoming values from NewBB instead of from TIBB.
95 //
96 for (BasicBlock::iterator I = DestBB->begin();
Chris Lattner889f6202003-04-23 16:37:45 +000097 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattner75f80bd2002-09-24 15:51:56 +000098 // We no longer enter through TIBB, now we come in through NewBB.
99 PN->replaceUsesOfWith(TIBB, NewBB);
100 }
101
Chris Lattner5ac72de2002-10-08 21:06:27 +0000102 // If we don't have a pass object, we can't update anything...
103 if (P == 0) return;
104
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000105 // Now update analysis information. These are the analyses that we are
106 // currently capable of updating...
Chris Lattner75f80bd2002-09-24 15:51:56 +0000107 //
Chris Lattner75f80bd2002-09-24 15:51:56 +0000108
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000109 // Should we update DominatorSet information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000110 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000111 // The blocks that dominate the new one are the blocks that dominate TIBB
112 // plus the new block itself.
113 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
114 DomSet.insert(NewBB); // A block always dominates itself.
115 DS->addBasicBlock(NewBB, DomSet);
116 }
Chris Lattner75f80bd2002-09-24 15:51:56 +0000117
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000118 // Should we update ImmediateDominator information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000119 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000120 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
121 // anything.
122 ID->addNewBlock(NewBB, TIBB);
123 }
124
125 // Should we update DominatorTree information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000126 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000127 DominatorTree::Node *TINode = DT->getNode(TIBB);
128
129 // The new block is not the immediate dominator for any other nodes, but
130 // TINode is the immediate dominator for the new node.
131 //
Chris Lattnerb7368502002-10-08 21:53:51 +0000132 if (TINode) // Don't break unreachable code!
133 DT->createNewNode(NewBB, TINode);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000134 }
Chris Lattner12764c82002-10-31 02:44:36 +0000135
136 // Should we update DominanceFrontier information?
137 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) {
138 // Since the new block is dominated by its only predecessor TIBB,
139 // it cannot be in any block's dominance frontier. Its dominance
140 // frontier is {DestBB}.
141 DominanceFrontier::DomSetType NewDFSet;
142 NewDFSet.insert(DestBB);
143 DF->addBasicBlock(NewBB, NewDFSet);
144 }
Chris Lattner75f80bd2002-09-24 15:51:56 +0000145}
146
Chris Lattnerb03832d2002-09-24 00:08:39 +0000147// runOnFunction - Loop over all of the edges in the CFG, breaking critical
148// edges as they are found.
149//
150bool BreakCriticalEdges::runOnFunction(Function &F) {
151 bool Changed = false;
152 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
153 TerminatorInst *TI = I->getTerminator();
Chris Lattner75f80bd2002-09-24 15:51:56 +0000154 if (TI->getNumSuccessors() > 1)
155 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
156 if (isCriticalEdge(TI, i)) {
Chris Lattner5ac72de2002-10-08 21:06:27 +0000157 SplitCriticalEdge(TI, i, this);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000158 ++NumBroken;
159 Changed = true;
160 }
Chris Lattnerb03832d2002-09-24 00:08:39 +0000161 }
162
163 return Changed;
164}