blob: 1175b1053b2d386dace2552c1d4c33d5a6ed02a4 [file] [log] [blame]
Chris Lattnerd76efa02002-09-24 00:08:39 +00001//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
John Criswellb576c942003-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 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"
22#include "llvm/Function.h"
Chris Lattnereb0456c2002-09-24 15:51:56 +000023#include "llvm/iTerminators.h"
24#include "llvm/iPHINode.h"
25#include "llvm/Support/CFG.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000026#include "Support/Statistic.h"
Chris Lattnerd76efa02002-09-24 00:08:39 +000027
Brian Gaeked0fde302003-11-11 22:41:34 +000028namespace llvm {
29
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);
35
36 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 Lattner98bf4362003-10-12 21:52:28 +000041
42 // No loop canonicalization guarantees are broken by this pass.
43 AU.addPreservedID(LoopSimplifyID);
Chris Lattner6de302b2002-09-24 15:43:12 +000044 }
45 };
Chris Lattnerd76efa02002-09-24 00:08:39 +000046
Chris Lattner6de302b2002-09-24 15:43:12 +000047 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
48 "Break critical edges in CFG");
49}
Chris Lattnerd76efa02002-09-24 00:08:39 +000050
Chris Lattnereb0456c2002-09-24 15:51:56 +000051// Publically exposed interface to pass...
Chris Lattner6de302b2002-09-24 15:43:12 +000052const PassInfo *BreakCriticalEdgesID = X.getPassInfo();
Chris Lattnerd76efa02002-09-24 00:08:39 +000053Pass *createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); }
54
Chris Lattner363ca612003-11-10 04:42:42 +000055// runOnFunction - Loop over all of the edges in the CFG, breaking critical
56// edges as they are found.
57//
58bool BreakCriticalEdges::runOnFunction(Function &F) {
59 bool Changed = false;
60 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
61 TerminatorInst *TI = I->getTerminator();
62 if (TI->getNumSuccessors() > 1)
63 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
64 if (SplitCriticalEdge(TI, i, this)) {
65 ++NumBroken;
66 Changed = true;
67 }
68 }
69
70 return Changed;
71}
72
73//===----------------------------------------------------------------------===//
74// Implementation of the external critical edge manipulation functions
75//===----------------------------------------------------------------------===//
Chris Lattnereb0456c2002-09-24 15:51:56 +000076
77// isCriticalEdge - Return true if the specified edge is a critical edge.
78// Critical edges are edges from a block with multiple successors to a block
79// with multiple predecessors.
80//
Chris Lattnere802a022002-10-08 21:06:27 +000081bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
Chris Lattnereb0456c2002-09-24 15:51:56 +000082 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
Chris Lattnere802a022002-10-08 21:06:27 +000083 if (TI->getNumSuccessors() == 1) return false;
Chris Lattnereb0456c2002-09-24 15:51:56 +000084
85 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
86 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
87
88 // If there is more than one predecessor, this is a critical edge...
89 assert(I != E && "No preds, but we have an edge to the block?");
90 ++I; // Skip one edge due to the incoming arc from TI.
91 return I != E;
92}
93
Chris Lattnerd23520c2003-11-10 04:10:50 +000094// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
95// split the critical edge. This will update DominatorSet, ImmediateDominator,
96// DominatorTree, and DominatorFrontier information if it is available, thus
97// calling this pass will not invalidate either of them. This returns true if
98// the edge was split, false otherwise.
Chris Lattnereb0456c2002-09-24 15:51:56 +000099//
Chris Lattnerd23520c2003-11-10 04:10:50 +0000100bool SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
101 if (!isCriticalEdge(TI, SuccNum)) return false;
Chris Lattnereb0456c2002-09-24 15:51:56 +0000102 BasicBlock *TIBB = TI->getParent();
Chris Lattner6918c072002-10-31 02:44:36 +0000103 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000104
105 // Create a new basic block, linking it into the CFG.
Chris Lattner6918c072002-10-31 02:44:36 +0000106 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
107 DestBB->getName() + "_crit_edge");
Chris Lattnereb0456c2002-09-24 15:51:56 +0000108 // Create our unconditional branch...
Chris Lattner108e4ab2003-11-21 16:52:05 +0000109 new BranchInst(DestBB, NewBB);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000110
111 // Branch to the new block, breaking the edge...
112 TI->setSuccessor(SuccNum, NewBB);
113
114 // Insert the block into the function... right after the block TI lives in.
115 Function &F = *TIBB->getParent();
116 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
117
118 // If there are any PHI nodes in DestBB, we need to update them so that they
119 // merge incoming values from NewBB instead of from TIBB.
120 //
121 for (BasicBlock::iterator I = DestBB->begin();
Chris Lattnere408e252003-04-23 16:37:45 +0000122 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattnereb0456c2002-09-24 15:51:56 +0000123 // We no longer enter through TIBB, now we come in through NewBB.
124 PN->replaceUsesOfWith(TIBB, NewBB);
125 }
126
Chris Lattnere802a022002-10-08 21:06:27 +0000127 // If we don't have a pass object, we can't update anything...
Chris Lattnerd23520c2003-11-10 04:10:50 +0000128 if (P == 0) return true;
Chris Lattnere802a022002-10-08 21:06:27 +0000129
Chris Lattnerc178d942002-09-26 16:18:51 +0000130 // Now update analysis information. These are the analyses that we are
131 // currently capable of updating...
Chris Lattnereb0456c2002-09-24 15:51:56 +0000132 //
Chris Lattnereb0456c2002-09-24 15:51:56 +0000133
Chris Lattnerc178d942002-09-26 16:18:51 +0000134 // Should we update DominatorSet information?
Chris Lattnere802a022002-10-08 21:06:27 +0000135 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000136 // The blocks that dominate the new one are the blocks that dominate TIBB
137 // plus the new block itself.
138 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
139 DomSet.insert(NewBB); // A block always dominates itself.
140 DS->addBasicBlock(NewBB, DomSet);
141 }
Chris Lattnereb0456c2002-09-24 15:51:56 +0000142
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000143 // Should we update ImmediateDominator information?
Chris Lattnere802a022002-10-08 21:06:27 +0000144 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000145 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
146 // anything.
147 ID->addNewBlock(NewBB, TIBB);
148 }
149
150 // Should we update DominatorTree information?
Chris Lattnere802a022002-10-08 21:06:27 +0000151 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000152 DominatorTree::Node *TINode = DT->getNode(TIBB);
153
154 // The new block is not the immediate dominator for any other nodes, but
155 // TINode is the immediate dominator for the new node.
156 //
Chris Lattnerd3d06a52002-10-08 21:53:51 +0000157 if (TINode) // Don't break unreachable code!
158 DT->createNewNode(NewBB, TINode);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000159 }
Chris Lattner6918c072002-10-31 02:44:36 +0000160
161 // Should we update DominanceFrontier information?
162 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) {
163 // Since the new block is dominated by its only predecessor TIBB,
164 // it cannot be in any block's dominance frontier. Its dominance
165 // frontier is {DestBB}.
166 DominanceFrontier::DomSetType NewDFSet;
167 NewDFSet.insert(DestBB);
168 DF->addBasicBlock(NewBB, NewDFSet);
169 }
Chris Lattnerd23520c2003-11-10 04:10:50 +0000170 return true;
Chris Lattnereb0456c2002-09-24 15:51:56 +0000171}
Brian Gaeked0fde302003-11-11 22:41:34 +0000172
173} // End llvm namespace