blob: 59ec01c7d585f5184e7ebf0c6bce8009c2e5328b [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"
22#include "llvm/Function.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000023#include "llvm/Instructions.h"
Chris Lattner7b9020a2005-03-17 15:38:16 +000024#include "llvm/Type.h"
Chris Lattner75f80bd2002-09-24 15:51:56 +000025#include "llvm/Support/CFG.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner4bec6652002-09-24 15:43:12 +000029namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000030 Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
Chris Lattnerb03832d2002-09-24 00:08:39 +000031
Chris Lattner4bec6652002-09-24 15:43:12 +000032 struct BreakCriticalEdges : public FunctionPass {
33 virtual bool runOnFunction(Function &F);
Misha Brukmanb1c93172005-04-21 23:48:37 +000034
Chris Lattner4bec6652002-09-24 15:43:12 +000035 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.addPreserved<DominatorSet>();
37 AU.addPreserved<ImmediateDominators>();
38 AU.addPreserved<DominatorTree>();
Chris Lattner12764c82002-10-31 02:44:36 +000039 AU.addPreserved<DominanceFrontier>();
Chris Lattner72272a72003-10-12 21:52:28 +000040
41 // No loop canonicalization guarantees are broken by this pass.
42 AU.addPreservedID(LoopSimplifyID);
Chris Lattner4bec6652002-09-24 15:43:12 +000043 }
44 };
Chris Lattnerb03832d2002-09-24 00:08:39 +000045
Chris Lattner4bec6652002-09-24 15:43:12 +000046 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
47 "Break critical edges in CFG");
48}
Chris Lattnerb03832d2002-09-24 00:08:39 +000049
Chris Lattner75f80bd2002-09-24 15:51:56 +000050// Publically exposed interface to pass...
Chris Lattnerdf3c3422004-01-09 06:12:26 +000051const PassInfo *llvm::BreakCriticalEdgesID = X.getPassInfo();
Chris Lattner7471b962004-07-31 10:01:58 +000052FunctionPass *llvm::createBreakCriticalEdgesPass() {
53 return new BreakCriticalEdges();
54}
Chris Lattnerb03832d2002-09-24 00:08:39 +000055
Chris Lattner1e6d3052003-11-10 04:42:42 +000056// runOnFunction - Loop over all of the edges in the CFG, breaking critical
57// edges as they are found.
58//
59bool BreakCriticalEdges::runOnFunction(Function &F) {
60 bool Changed = false;
61 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
62 TerminatorInst *TI = I->getTerminator();
63 if (TI->getNumSuccessors() > 1)
64 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
65 if (SplitCriticalEdge(TI, i, this)) {
66 ++NumBroken;
67 Changed = true;
68 }
69 }
70
71 return Changed;
72}
73
74//===----------------------------------------------------------------------===//
75// Implementation of the external critical edge manipulation functions
76//===----------------------------------------------------------------------===//
Chris Lattner75f80bd2002-09-24 15:51:56 +000077
78// isCriticalEdge - Return true if the specified edge is a critical edge.
79// Critical edges are edges from a block with multiple successors to a block
80// with multiple predecessors.
81//
Chris Lattnerdf3c3422004-01-09 06:12:26 +000082bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
Chris Lattner75f80bd2002-09-24 15:51:56 +000083 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
Chris Lattner5ac72de2002-10-08 21:06:27 +000084 if (TI->getNumSuccessors() == 1) return false;
Chris Lattner75f80bd2002-09-24 15:51:56 +000085
86 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
87 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
88
89 // If there is more than one predecessor, this is a critical edge...
90 assert(I != E && "No preds, but we have an edge to the block?");
91 ++I; // Skip one edge due to the incoming arc from TI.
92 return I != E;
93}
94
Chris Lattner44743362003-11-10 04:10:50 +000095// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
96// split the critical edge. This will update DominatorSet, ImmediateDominator,
97// DominatorTree, and DominatorFrontier information if it is available, thus
98// calling this pass will not invalidate either of them. This returns true if
99// the edge was split, false otherwise.
Chris Lattner75f80bd2002-09-24 15:51:56 +0000100//
Chris Lattnerdf3c3422004-01-09 06:12:26 +0000101bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
Chris Lattner44743362003-11-10 04:10:50 +0000102 if (!isCriticalEdge(TI, SuccNum)) return false;
Chris Lattner75f80bd2002-09-24 15:51:56 +0000103 BasicBlock *TIBB = TI->getParent();
Chris Lattner12764c82002-10-31 02:44:36 +0000104 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000105
106 // Create a new basic block, linking it into the CFG.
Chris Lattner12764c82002-10-31 02:44:36 +0000107 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
108 DestBB->getName() + "_crit_edge");
Chris Lattner75f80bd2002-09-24 15:51:56 +0000109 // Create our unconditional branch...
Chris Lattnera2960002003-11-21 16:52:05 +0000110 new BranchInst(DestBB, NewBB);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000111
Chris Lattner75f80bd2002-09-24 15:51:56 +0000112 // Branch to the new block, breaking the edge...
113 TI->setSuccessor(SuccNum, NewBB);
114
115 // Insert the block into the function... right after the block TI lives in.
116 Function &F = *TIBB->getParent();
117 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
118
119 // If there are any PHI nodes in DestBB, we need to update them so that they
120 // merge incoming values from NewBB instead of from TIBB.
121 //
Reid Spencer66149462004-09-15 17:06:42 +0000122 for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
123 PHINode *PN = cast<PHINode>(I);
Chris Lattner2de229f2004-02-29 22:24:41 +0000124 // We no longer enter through TIBB, now we come in through NewBB. Revector
125 // exactly one entry in the PHI node that used to come from TIBB to come
126 // from NewBB.
Chris Lattnerb7ebe652005-08-12 21:58:07 +0000127 int BBIdx = PN->getBasicBlockIndex(TIBB);
128 PN->setIncomingBlock(BBIdx, NewBB);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000129 }
130
Chris Lattner5ac72de2002-10-08 21:06:27 +0000131 // If we don't have a pass object, we can't update anything...
Chris Lattner44743362003-11-10 04:10:50 +0000132 if (P == 0) return true;
Chris Lattner5ac72de2002-10-08 21:06:27 +0000133
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000134 // Now update analysis information. These are the analyses that we are
135 // currently capable of updating...
Chris Lattner75f80bd2002-09-24 15:51:56 +0000136 //
Chris Lattner75f80bd2002-09-24 15:51:56 +0000137
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000138 // Should we update DominatorSet information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000139 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000140 // The blocks that dominate the new one are the blocks that dominate TIBB
141 // plus the new block itself.
142 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
143 DomSet.insert(NewBB); // A block always dominates itself.
144 DS->addBasicBlock(NewBB, DomSet);
145 }
Chris Lattner75f80bd2002-09-24 15:51:56 +0000146
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000147 // Should we update ImmediateDominator information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000148 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000149 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
150 // anything.
151 ID->addNewBlock(NewBB, TIBB);
152 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000153
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000154 // Should we update DominatorTree information?
Chris Lattner5ac72de2002-10-08 21:06:27 +0000155 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000156 DominatorTree::Node *TINode = DT->getNode(TIBB);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000157
Chris Lattnerbedbd6b2002-09-26 16:18:51 +0000158 // The new block is not the immediate dominator for any other nodes, but
159 // TINode is the immediate dominator for the new node.
160 //
Chris Lattnerb7368502002-10-08 21:53:51 +0000161 if (TINode) // Don't break unreachable code!
162 DT->createNewNode(NewBB, TINode);
Chris Lattner75f80bd2002-09-24 15:51:56 +0000163 }
Chris Lattner12764c82002-10-31 02:44:36 +0000164
165 // Should we update DominanceFrontier information?
166 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) {
167 // Since the new block is dominated by its only predecessor TIBB,
168 // it cannot be in any block's dominance frontier. Its dominance
169 // frontier is {DestBB}.
170 DominanceFrontier::DomSetType NewDFSet;
171 NewDFSet.insert(DestBB);
172 DF->addBasicBlock(NewBB, NewDFSet);
173 }
Chris Lattner44743362003-11-10 04:10:50 +0000174 return true;
Chris Lattner75f80bd2002-09-24 15:51:56 +0000175}