blob: 5d31016fe8b4585efc42b562139f1c72b4033888 [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 Lattner44743362003-11-10 04:10:50 +000019#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerb03832d2002-09-24 00:08:39 +000020#include "llvm/Analysis/Dominators.h"
21#include "llvm/Function.h"
Chris Lattner75f80bd2002-09-24 15:51:56 +000022#include "llvm/iTerminators.h"
23#include "llvm/iPHINode.h"
24#include "llvm/Support/CFG.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000025#include "Support/Statistic.h"
Chris Lattnerb03832d2002-09-24 00:08:39 +000026
Chris Lattner4bec6652002-09-24 15:43:12 +000027namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000028 Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
Chris Lattnerb03832d2002-09-24 00:08:39 +000029
Chris Lattner4bec6652002-09-24 15:43:12 +000030 struct BreakCriticalEdges : public FunctionPass {
31 virtual bool runOnFunction(Function &F);
32
33 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
34 AU.addPreserved<DominatorSet>();
35 AU.addPreserved<ImmediateDominators>();
36 AU.addPreserved<DominatorTree>();
Chris Lattner12764c82002-10-31 02:44:36 +000037 AU.addPreserved<DominanceFrontier>();
Chris Lattner72272a72003-10-12 21:52:28 +000038
39 // No loop canonicalization guarantees are broken by this pass.
40 AU.addPreservedID(LoopSimplifyID);
Chris Lattner4bec6652002-09-24 15:43:12 +000041 }
42 };
Chris Lattnerb03832d2002-09-24 00:08:39 +000043
Chris Lattner4bec6652002-09-24 15:43:12 +000044 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
45 "Break critical edges in CFG");
46}
Chris Lattnerb03832d2002-09-24 00:08:39 +000047
Chris Lattner75f80bd2002-09-24 15:51:56 +000048// Publically exposed interface to pass...
Chris Lattner4bec6652002-09-24 15:43:12 +000049const PassInfo *BreakCriticalEdgesID = X.getPassInfo();
Chris Lattnerb03832d2002-09-24 00:08:39 +000050Pass *createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); }
51
Chris Lattner75f80bd2002-09-24 15:51:56 +000052
53// isCriticalEdge - Return true if the specified edge is a critical edge.
54// Critical edges are edges from a block with multiple successors to a block
55// with multiple predecessors.
56//
Chris Lattner5ac72de2002-10-08 21:06:27 +000057bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
Chris Lattner75f80bd2002-09-24 15:51:56 +000058 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
Chris Lattner5ac72de2002-10-08 21:06:27 +000059 if (TI->getNumSuccessors() == 1) return false;
Chris Lattner75f80bd2002-09-24 15:51:56 +000060
61 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
62 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
63
64 // If there is more than one predecessor, this is a critical edge...
65 assert(I != E && "No preds, but we have an edge to the block?");
66 ++I; // Skip one edge due to the incoming arc from TI.
67 return I != E;
68}
69
Chris Lattner44743362003-11-10 04:10:50 +000070// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
71// split the critical edge. This will update DominatorSet, ImmediateDominator,
72// DominatorTree, and DominatorFrontier information if it is available, thus
73// calling this pass will not invalidate either of them. This returns true if
74// the edge was split, false otherwise.
Chris Lattner75f80bd2002-09-24 15:51:56 +000075//
Chris Lattner44743362003-11-10 04:10:50 +000076bool SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
77 if (!isCriticalEdge(TI, SuccNum)) return false;
Chris Lattner75f80bd2002-09-24 15:51:56 +000078 BasicBlock *TIBB = TI->getParent();
Chris Lattner12764c82002-10-31 02:44:36 +000079 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
Chris Lattner75f80bd2002-09-24 15:51:56 +000080
81 // Create a new basic block, linking it into the CFG.
Chris Lattner12764c82002-10-31 02:44:36 +000082 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
83 DestBB->getName() + "_crit_edge");
Chris Lattner75f80bd2002-09-24 15:51:56 +000084 // Create our unconditional branch...
85 BranchInst *BI = new BranchInst(DestBB);
86 NewBB->getInstList().push_back(BI);
87
88 // Branch to the new block, breaking the edge...
89 TI->setSuccessor(SuccNum, NewBB);
90
91 // Insert the block into the function... right after the block TI lives in.
92 Function &F = *TIBB->getParent();
93 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
94
95 // If there are any PHI nodes in DestBB, we need to update them so that they
96 // merge incoming values from NewBB instead of from TIBB.
97 //
98 for (BasicBlock::iterator I = DestBB->begin();
Chris Lattner889f6202003-04-23 16:37:45 +000099 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattner75f80bd2002-09-24 15:51:56 +0000100 // We no longer enter through TIBB, now we come in through NewBB.
101 PN->replaceUsesOfWith(TIBB, NewBB);
102 }
103
Chris Lattner5ac72de2002-10-08 21:06:27 +0000104 // If we don't have a pass object, we can't update anything...
Chris Lattner44743362003-11-10 04:10:50 +0000105 if (P == 0) return true;
Chris Lattner5ac72de2002-10-08 21:06:27 +0000106
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000107 // Now update analysis information. These are the analyses that we are
108 // currently capable of updating...
Chris Lattner75f80bd2002-09-24 15:51:56 +0000109 //
Chris Lattner75f80bd2002-09-24 15:51:56 +0000110
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000111 // Should we update DominatorSet information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000112 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000113 // The blocks that dominate the new one are the blocks that dominate TIBB
114 // plus the new block itself.
115 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
116 DomSet.insert(NewBB); // A block always dominates itself.
117 DS->addBasicBlock(NewBB, DomSet);
118 }
Chris Lattner75f80bd2002-09-24 15:51:56 +0000119
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000120 // Should we update ImmediateDominator information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000121 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000122 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
123 // anything.
124 ID->addNewBlock(NewBB, TIBB);
125 }
126
127 // Should we update DominatorTree information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000128 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000129 DominatorTree::Node *TINode = DT->getNode(TIBB);
130
131 // The new block is not the immediate dominator for any other nodes, but
132 // TINode is the immediate dominator for the new node.
133 //
Chris Lattnerb7368502002-10-08 21:53:51 +0000134 if (TINode) // Don't break unreachable code!
135 DT->createNewNode(NewBB, TINode);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000136 }
Chris Lattner12764c82002-10-31 02:44:36 +0000137
138 // Should we update DominanceFrontier information?
139 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) {
140 // Since the new block is dominated by its only predecessor TIBB,
141 // it cannot be in any block's dominance frontier. Its dominance
142 // frontier is {DestBB}.
143 DominanceFrontier::DomSetType NewDFSet;
144 NewDFSet.insert(DestBB);
145 DF->addBasicBlock(NewBB, NewDFSet);
146 }
Chris Lattner44743362003-11-10 04:10:50 +0000147 return true;
Chris Lattner75f80bd2002-09-24 15:51:56 +0000148}
149
Chris Lattnerb03832d2002-09-24 00:08:39 +0000150// runOnFunction - Loop over all of the edges in the CFG, breaking critical
151// edges as they are found.
152//
153bool BreakCriticalEdges::runOnFunction(Function &F) {
154 bool Changed = false;
155 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
156 TerminatorInst *TI = I->getTerminator();
Chris Lattner75f80bd2002-09-24 15:51:56 +0000157 if (TI->getNumSuccessors() > 1)
158 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
Chris Lattner44743362003-11-10 04:10:50 +0000159 if (SplitCriticalEdge(TI, i, this)) {
Chris Lattner75f80bd2002-09-24 15:51:56 +0000160 ++NumBroken;
161 Changed = true;
162 }
Chris Lattnerb03832d2002-09-24 00:08:39 +0000163 }
164
165 return Changed;
166}