blob: 178e8ca1957c468b4fd7a126b566eaf56f99233e [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 Lattnerf7703df2004-01-09 06:12:26 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner6de302b2002-09-24 15:43:12 +000029namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000030 Statistic<> NumBroken("break-crit-edges", "Number of blocks inserted");
Chris Lattnerd76efa02002-09-24 00:08:39 +000031
Chris Lattner6de302b2002-09-24 15:43:12 +000032 struct BreakCriticalEdges : public FunctionPass {
33 virtual bool runOnFunction(Function &F);
34
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.addPreserved<DominatorSet>();
37 AU.addPreserved<ImmediateDominators>();
38 AU.addPreserved<DominatorTree>();
Chris Lattner6918c072002-10-31 02:44:36 +000039 AU.addPreserved<DominanceFrontier>();
Chris Lattner98bf4362003-10-12 21:52:28 +000040
41 // No loop canonicalization guarantees are broken by this pass.
42 AU.addPreservedID(LoopSimplifyID);
Chris Lattner6de302b2002-09-24 15:43:12 +000043 }
44 };
Chris Lattnerd76efa02002-09-24 00:08:39 +000045
Chris Lattner6de302b2002-09-24 15:43:12 +000046 RegisterOpt<BreakCriticalEdges> X("break-crit-edges",
47 "Break critical edges in CFG");
48}
Chris Lattnerd76efa02002-09-24 00:08:39 +000049
Chris Lattnereb0456c2002-09-24 15:51:56 +000050// Publically exposed interface to pass...
Chris Lattnerf7703df2004-01-09 06:12:26 +000051const PassInfo *llvm::BreakCriticalEdgesID = X.getPassInfo();
52Pass *llvm::createBreakCriticalEdgesPass() { return new BreakCriticalEdges(); }
Chris Lattnerd76efa02002-09-24 00:08:39 +000053
Chris Lattner363ca612003-11-10 04:42:42 +000054// runOnFunction - Loop over all of the edges in the CFG, breaking critical
55// edges as they are found.
56//
57bool BreakCriticalEdges::runOnFunction(Function &F) {
58 bool Changed = false;
59 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
60 TerminatorInst *TI = I->getTerminator();
61 if (TI->getNumSuccessors() > 1)
62 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
63 if (SplitCriticalEdge(TI, i, this)) {
64 ++NumBroken;
65 Changed = true;
66 }
67 }
68
69 return Changed;
70}
71
72//===----------------------------------------------------------------------===//
73// Implementation of the external critical edge manipulation functions
74//===----------------------------------------------------------------------===//
Chris Lattnereb0456c2002-09-24 15:51:56 +000075
76// isCriticalEdge - Return true if the specified edge is a critical edge.
77// Critical edges are edges from a block with multiple successors to a block
78// with multiple predecessors.
79//
Chris Lattnerf7703df2004-01-09 06:12:26 +000080bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum) {
Chris Lattnereb0456c2002-09-24 15:51:56 +000081 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
Chris Lattnere802a022002-10-08 21:06:27 +000082 if (TI->getNumSuccessors() == 1) return false;
Chris Lattnereb0456c2002-09-24 15:51:56 +000083
84 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
85 pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
86
87 // If there is more than one predecessor, this is a critical edge...
88 assert(I != E && "No preds, but we have an edge to the block?");
89 ++I; // Skip one edge due to the incoming arc from TI.
90 return I != E;
91}
92
Chris Lattnerd23520c2003-11-10 04:10:50 +000093// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
94// split the critical edge. This will update DominatorSet, ImmediateDominator,
95// DominatorTree, and DominatorFrontier information if it is available, thus
96// calling this pass will not invalidate either of them. This returns true if
97// the edge was split, false otherwise.
Chris Lattnereb0456c2002-09-24 15:51:56 +000098//
Chris Lattnerf7703df2004-01-09 06:12:26 +000099bool llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
Chris Lattnerd23520c2003-11-10 04:10:50 +0000100 if (!isCriticalEdge(TI, SuccNum)) return false;
Chris Lattnereb0456c2002-09-24 15:51:56 +0000101 BasicBlock *TIBB = TI->getParent();
Chris Lattner6918c072002-10-31 02:44:36 +0000102 BasicBlock *DestBB = TI->getSuccessor(SuccNum);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000103
104 // Create a new basic block, linking it into the CFG.
Chris Lattner6918c072002-10-31 02:44:36 +0000105 BasicBlock *NewBB = new BasicBlock(TIBB->getName() + "." +
106 DestBB->getName() + "_crit_edge");
Chris Lattnereb0456c2002-09-24 15:51:56 +0000107 // Create our unconditional branch...
Chris Lattner108e4ab2003-11-21 16:52:05 +0000108 new BranchInst(DestBB, NewBB);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000109
110 // Branch to the new block, breaking the edge...
111 TI->setSuccessor(SuccNum, NewBB);
112
113 // Insert the block into the function... right after the block TI lives in.
114 Function &F = *TIBB->getParent();
115 F.getBasicBlockList().insert(TIBB->getNext(), NewBB);
116
117 // If there are any PHI nodes in DestBB, we need to update them so that they
118 // merge incoming values from NewBB instead of from TIBB.
119 //
120 for (BasicBlock::iterator I = DestBB->begin();
Chris Lattnere408e252003-04-23 16:37:45 +0000121 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Chris Lattnereb0456c2002-09-24 15:51:56 +0000122 // We no longer enter through TIBB, now we come in through NewBB.
123 PN->replaceUsesOfWith(TIBB, NewBB);
124 }
125
Chris Lattnere802a022002-10-08 21:06:27 +0000126 // If we don't have a pass object, we can't update anything...
Chris Lattnerd23520c2003-11-10 04:10:50 +0000127 if (P == 0) return true;
Chris Lattnere802a022002-10-08 21:06:27 +0000128
Chris Lattnerc178d942002-09-26 16:18:51 +0000129 // Now update analysis information. These are the analyses that we are
130 // currently capable of updating...
Chris Lattnereb0456c2002-09-24 15:51:56 +0000131 //
Chris Lattnereb0456c2002-09-24 15:51:56 +0000132
Chris Lattnerc178d942002-09-26 16:18:51 +0000133 // Should we update DominatorSet information?
Chris Lattnere802a022002-10-08 21:06:27 +0000134 if (DominatorSet *DS = P->getAnalysisToUpdate<DominatorSet>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000135 // The blocks that dominate the new one are the blocks that dominate TIBB
136 // plus the new block itself.
137 DominatorSet::DomSetType DomSet = DS->getDominators(TIBB);
138 DomSet.insert(NewBB); // A block always dominates itself.
139 DS->addBasicBlock(NewBB, DomSet);
140 }
Chris Lattnereb0456c2002-09-24 15:51:56 +0000141
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000142 // Should we update ImmediateDominator information?
Chris Lattnere802a022002-10-08 21:06:27 +0000143 if (ImmediateDominators *ID = P->getAnalysisToUpdate<ImmediateDominators>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000144 // TIBB is the new immediate dominator for NewBB. NewBB doesn't dominate
145 // anything.
146 ID->addNewBlock(NewBB, TIBB);
147 }
148
149 // Should we update DominatorTree information?
Chris Lattnere802a022002-10-08 21:06:27 +0000150 if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) {
Chris Lattnerc178d942002-09-26 16:18:51 +0000151 DominatorTree::Node *TINode = DT->getNode(TIBB);
152
153 // The new block is not the immediate dominator for any other nodes, but
154 // TINode is the immediate dominator for the new node.
155 //
Chris Lattnerd3d06a52002-10-08 21:53:51 +0000156 if (TINode) // Don't break unreachable code!
157 DT->createNewNode(NewBB, TINode);
Chris Lattnereb0456c2002-09-24 15:51:56 +0000158 }
Chris Lattner6918c072002-10-31 02:44:36 +0000159
160 // Should we update DominanceFrontier information?
161 if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) {
162 // Since the new block is dominated by its only predecessor TIBB,
163 // it cannot be in any block's dominance frontier. Its dominance
164 // frontier is {DestBB}.
165 DominanceFrontier::DomSetType NewDFSet;
166 NewDFSet.insert(DestBB);
167 DF->addBasicBlock(NewBB, NewDFSet);
168 }
Chris Lattnerd23520c2003-11-10 04:10:50 +0000169 return true;
Chris Lattnereb0456c2002-09-24 15:51:56 +0000170}