blob: f6f4ad2c927593cff20bb7f62cb4ac9c20d90773 [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
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"
22#include "llvm/Function.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000023#include "llvm/Instructions.h"
Chris Lattner75f80bd2002-09-24 15:51:56 +000024#include "llvm/Support/CFG.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000025#include "Support/Statistic.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000026using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattner4bec6652002-09-24 15:43:12 +000028namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000029 Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
Chris Lattnerb03832d2002-09-24 00:08:39 +000030
Chris Lattner4bec6652002-09-24 15:43:12 +000031 struct BreakCriticalEdges : public FunctionPass {
32 virtual bool runOnFunction(Function &F);
33
34 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
35 AU.addPreserved<DominatorSet>();
36 AU.addPreserved<ImmediateDominators>();
37 AU.addPreserved<DominatorTree>();
Chris Lattner12764c82002-10-31 02:44:36 +000038 AU.addPreserved<DominanceFrontier>();
Chris Lattner72272a72003-10-12 21:52:28 +000039
40 // No loop canonicalization guarantees are broken by this pass.
41 AU.addPreservedID(LoopSimplifyID);
Chris Lattner4bec6652002-09-24 15:43:12 +000042 }
43 };
Chris Lattnerb03832d2002-09-24 00:08:39 +000044
Chris Lattner4bec6652002-09-24 15:43:12 +000045 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
46 "Break critical edges in CFG");
47}
Chris Lattnerb03832d2002-09-24 00:08:39 +000048
Chris Lattner75f80bd2002-09-24 15:51:56 +000049// Publically exposed interface to pass...
Chris Lattnerdf3c3422004-01-09 06:12:26 +000050const PassInfo *llvm::BreakCriticalEdgesID = X.getPassInfo();
51Pass *llvm::createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); }
Chris Lattnerb03832d2002-09-24 00:08:39 +000052
Chris Lattner1e6d3052003-11-10 04:42:42 +000053// runOnFunction - Loop over all of the edges in the CFG, breaking critical
54// edges as they are found.
55//
56bool BreakCriticalEdges::runOnFunction(Function &F) {
57 bool Changed = false;
58 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
59 TerminatorInst *TI = I->getTerminator();
60 if (TI->getNumSuccessors() > 1)
61 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
62 if (SplitCriticalEdge(TI, i, this)) {
63 ++NumBroken;
64 Changed = true;
65 }
66 }
67
68 return Changed;
69}
70
71//===----------------------------------------------------------------------===//
72// Implementation of the external critical edge manipulation functions
73//===----------------------------------------------------------------------===//
Chris Lattner75f80bd2002-09-24 15:51:56 +000074
75// isCriticalEdge - Return true if the specified edge is a critical edge.
76// Critical edges are edges from a block with multiple successors to a block
77// with multiple predecessors.
78//
Chris Lattnerdf3c3422004-01-09 06:12:26 +000079bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
Chris Lattner75f80bd2002-09-24 15:51:56 +000080 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
Chris Lattner5ac72de2002-10-08 21:06:27 +000081 if (TI->getNumSuccessors() == 1) return false;
Chris Lattner75f80bd2002-09-24 15:51:56 +000082
83 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
84 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
85
86 // If there is more than one predecessor, this is a critical edge...
87 assert(I != E && "No preds, but we have an edge to the block?");
88 ++I; // Skip one edge due to the incoming arc from TI.
89 return I != E;
90}
91
Chris Lattner44743362003-11-10 04:10:50 +000092// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
93// split the critical edge. This will update DominatorSet, ImmediateDominator,
94// DominatorTree, and DominatorFrontier information if it is available, thus
95// calling this pass will not invalidate either of them. This returns true if
96// the edge was split, false otherwise.
Chris Lattner75f80bd2002-09-24 15:51:56 +000097//
Chris Lattnerdf3c3422004-01-09 06:12:26 +000098bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
Chris Lattner44743362003-11-10 04:10:50 +000099 if (!isCriticalEdge(TI, SuccNum)) return false;
Chris Lattner75f80bd2002-09-24 15:51:56 +0000100 BasicBlock *TIBB = TI->getParent();
Chris Lattner12764c82002-10-31 02:44:36 +0000101 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000102
103 // Create a new basic block, linking it into the CFG.
Chris Lattner12764c82002-10-31 02:44:36 +0000104 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
105 DestBB->getName() + "_crit_edge");
Chris Lattner75f80bd2002-09-24 15:51:56 +0000106 // Create our unconditional branch...
Chris Lattnera2960002003-11-21 16:52:05 +0000107 new BranchInst(DestBB, NewBB);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000108
109 // Branch to the new block, breaking the edge...
110 TI->setSuccessor(SuccNum, NewBB);
111
112 // Insert the block into the function... right after the block TI lives in.
113 Function &F = *TIBB->getParent();
114 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
115
116 // If there are any PHI nodes in DestBB, we need to update them so that they
117 // merge incoming values from NewBB instead of from TIBB.
118 //
119 for (BasicBlock::iterator I = DestBB->begin();
Chris Lattner889f6202003-04-23 16:37:45 +0000120 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattner2de229f2004-02-29 22:24:41 +0000121 // We no longer enter through TIBB, now we come in through NewBB. Revector
122 // exactly one entry in the PHI node that used to come from TIBB to come
123 // from NewBB.
124 Value *InVal = PN->removeIncomingValue(TIBB, false);
125 PN->addIncoming(InVal, NewBB);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000126 }
127
Chris Lattner5ac72de2002-10-08 21:06:27 +0000128 // If we don't have a pass object, we can't update anything...
Chris Lattner44743362003-11-10 04:10:50 +0000129 if (P == 0) return true;
Chris Lattner5ac72de2002-10-08 21:06:27 +0000130
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000131 // Now update analysis information. These are the analyses that we are
132 // currently capable of updating...
Chris Lattner75f80bd2002-09-24 15:51:56 +0000133 //
Chris Lattner75f80bd2002-09-24 15:51:56 +0000134
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000135 // Should we update DominatorSet information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000136 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000137 // The blocks that dominate the new one are the blocks that dominate TIBB
138 // plus the new block itself.
139 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
140 DomSet.insert(NewBB); // A block always dominates itself.
141 DS->addBasicBlock(NewBB, DomSet);
142 }
Chris Lattner75f80bd2002-09-24 15:51:56 +0000143
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000144 // Should we update ImmediateDominator information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000145 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000146 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
147 // anything.
148 ID->addNewBlock(NewBB, TIBB);
149 }
150
151 // Should we update DominatorTree information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000152 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000153 DominatorTree::Node *TINode = DT->getNode(TIBB);
154
155 // The new block is not the immediate dominator for any other nodes, but
156 // TINode is the immediate dominator for the new node.
157 //
Chris Lattnerb7368502002-10-08 21:53:51 +0000158 if (TINode) // Don't break unreachable code!
159 DT->createNewNode(NewBB, TINode);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000160 }
Chris Lattner12764c82002-10-31 02:44:36 +0000161
162 // Should we update DominanceFrontier information?
163 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) {
164 // Since the new block is dominated by its only predecessor TIBB,
165 // it cannot be in any block's dominance frontier. Its dominance
166 // frontier is {DestBB}.
167 DominanceFrontier::DomSetType NewDFSet;
168 NewDFSet.insert(DestBB);
169 DF->addBasicBlock(NewBB, NewDFSet);
170 }
Chris Lattner44743362003-11-10 04:10:50 +0000171 return true;
Chris Lattner75f80bd2002-09-24 15:51:56 +0000172}