2002-09-24 00:08:39 +00:00
|
|
|
//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-24 00:08:39 +00:00
|
|
|
//
|
|
|
|
// BreakCriticalEdges pass - Break all of the critical edges in the CFG by
|
|
|
|
// inserting a dummy basic block. This pass may be "required" by passes that
|
|
|
|
// cannot deal with critical edges. For this usage, the structure type is
|
|
|
|
// forward declared. This pass obviously invalidates the CFG, but can update
|
2003-11-10 04:42:42 +00:00
|
|
|
// forward dominator (set, immediate dominators, tree, and frontier)
|
|
|
|
// information.
|
2002-09-24 00:08:39 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
#define DEBUG_TYPE "break-crit-edges"
|
2002-09-24 00:08:39 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2003-11-10 04:10:50 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2002-09-24 00:08:39 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2005-08-13 01:38:43 +00:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2009-09-09 17:53:39 +00:00
|
|
|
#include "llvm/Analysis/ProfileInfo.h"
|
2002-09-24 00:08:39 +00:00
|
|
|
#include "llvm/Function.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2005-03-17 15:38:16 +00:00
|
|
|
#include "llvm/Type.h"
|
2002-09-24 15:51:56 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2006-10-03 07:02:02 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-01-09 06:12:26 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
STATISTIC(NumBroken, "Number of blocks inserted");
|
2002-09-24 15:43:12 +00:00
|
|
|
|
2006-12-19 22:17:40 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct BreakCriticalEdges : public FunctionPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
BreakCriticalEdges() : FunctionPass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2002-09-24 15:43:12 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-09-24 15:43:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2007-04-07 05:57:09 +00:00
|
|
|
AU.addPreserved<DominatorTree>();
|
2002-10-31 02:44:36 +00:00
|
|
|
AU.addPreserved<DominanceFrontier>();
|
2005-08-13 01:38:43 +00:00
|
|
|
AU.addPreserved<LoopInfo>();
|
2009-09-09 17:53:39 +00:00
|
|
|
AU.addPreserved<ProfileInfo>();
|
2003-10-12 21:52:28 +00:00
|
|
|
|
|
|
|
// No loop canonicalization guarantees are broken by this pass.
|
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
2002-09-24 15:43:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2002-09-24 00:08:39 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char BreakCriticalEdges::ID = 0;
|
|
|
|
static RegisterPass<BreakCriticalEdges>
|
|
|
|
X("break-crit-edges", "Break critical edges in CFG");
|
|
|
|
|
2002-09-24 15:51:56 +00:00
|
|
|
// Publically exposed interface to pass...
|
2008-05-13 02:05:11 +00:00
|
|
|
const PassInfo *const llvm::BreakCriticalEdgesID = &X;
|
2004-07-31 10:01:58 +00:00
|
|
|
FunctionPass *llvm::createBreakCriticalEdgesPass() {
|
|
|
|
return new BreakCriticalEdges();
|
|
|
|
}
|
2002-09-24 00:08:39 +00:00
|
|
|
|
2003-11-10 04:42:42 +00:00
|
|
|
// runOnFunction - Loop over all of the edges in the CFG, breaking critical
|
|
|
|
// edges as they are found.
|
|
|
|
//
|
|
|
|
bool BreakCriticalEdges::runOnFunction(Function &F) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
|
|
|
TerminatorInst *TI = I->getTerminator();
|
2009-10-31 21:51:10 +00:00
|
|
|
if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
|
2003-11-10 04:42:42 +00:00
|
|
|
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
|
|
|
|
if (SplitCriticalEdge(TI, i, this)) {
|
|
|
|
++NumBroken;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implementation of the external critical edge manipulation functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-24 15:51:56 +00:00
|
|
|
|
|
|
|
// isCriticalEdge - Return true if the specified edge is a critical edge.
|
|
|
|
// Critical edges are edges from a block with multiple successors to a block
|
|
|
|
// with multiple predecessors.
|
|
|
|
//
|
2006-10-28 06:58:17 +00:00
|
|
|
bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
|
|
|
|
bool AllowIdenticalEdges) {
|
2002-09-24 15:51:56 +00:00
|
|
|
assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
|
2002-10-08 21:06:27 +00:00
|
|
|
if (TI->getNumSuccessors() == 1) return false;
|
2002-09-24 15:51:56 +00:00
|
|
|
|
|
|
|
const BasicBlock *Dest = TI->getSuccessor(SuccNum);
|
|
|
|
pred_const_iterator I = pred_begin(Dest), E = pred_end(Dest);
|
|
|
|
|
|
|
|
// If there is more than one predecessor, this is a critical edge...
|
|
|
|
assert(I != E && "No preds, but we have an edge to the block?");
|
2006-10-28 06:58:17 +00:00
|
|
|
const BasicBlock *FirstPred = *I;
|
2002-09-24 15:51:56 +00:00
|
|
|
++I; // Skip one edge due to the incoming arc from TI.
|
2006-10-28 06:58:17 +00:00
|
|
|
if (!AllowIdenticalEdges)
|
|
|
|
return I != E;
|
|
|
|
|
|
|
|
// If AllowIdenticalEdges is true, then we allow this edge to be considered
|
|
|
|
// non-critical iff all preds come from TI's block.
|
2008-04-16 23:46:39 +00:00
|
|
|
while (I != E) {
|
|
|
|
if (*I != FirstPred)
|
|
|
|
return true;
|
|
|
|
// Note: leave this as is until no one ever compiles with either gcc 4.0.1
|
|
|
|
// or Xcode 2. This seems to work around the pred_iterator assert in PR 2207
|
|
|
|
E = pred_end(*I);
|
|
|
|
++I;
|
|
|
|
}
|
2006-10-28 06:58:17 +00:00
|
|
|
return false;
|
2002-09-24 15:51:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 18:18:18 +00:00
|
|
|
/// CreatePHIsForSplitLoopExit - When a loop exit edge is split, LCSSA form
|
|
|
|
/// may require new PHIs in the new exit block. This function inserts the
|
|
|
|
/// new PHIs, as needed. Preds is a list of preds inside the loop, SplitBB
|
|
|
|
/// is the new loop exit block, and DestBB is the old loop exit, now the
|
|
|
|
/// successor of SplitBB.
|
|
|
|
static void CreatePHIsForSplitLoopExit(SmallVectorImpl<BasicBlock *> &Preds,
|
|
|
|
BasicBlock *SplitBB,
|
|
|
|
BasicBlock *DestBB) {
|
|
|
|
// SplitBB shouldn't have anything non-trivial in it yet.
|
|
|
|
assert(SplitBB->getFirstNonPHI() == SplitBB->getTerminator() &&
|
|
|
|
"SplitBB has non-PHI nodes!");
|
|
|
|
|
|
|
|
// For each PHI in the destination block...
|
|
|
|
for (BasicBlock::iterator I = DestBB->begin();
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(I); ++I) {
|
|
|
|
unsigned Idx = PN->getBasicBlockIndex(SplitBB);
|
|
|
|
Value *V = PN->getIncomingValue(Idx);
|
|
|
|
// If the input is a PHI which already satisfies LCSSA, don't create
|
|
|
|
// a new one.
|
|
|
|
if (const PHINode *VP = dyn_cast<PHINode>(V))
|
|
|
|
if (VP->getParent() == SplitBB)
|
|
|
|
continue;
|
|
|
|
// Otherwise a new PHI is needed. Create one and populate it.
|
|
|
|
PHINode *NewPN = PHINode::Create(PN->getType(), "split",
|
|
|
|
SplitBB->getTerminator());
|
|
|
|
for (unsigned i = 0, e = Preds.size(); i != e; ++i)
|
|
|
|
NewPN->addIncoming(V, Preds[i]);
|
|
|
|
// Update the original PHI.
|
|
|
|
PN->setIncomingValue(Idx, NewPN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-21 00:19:16 +00:00
|
|
|
/// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
|
|
|
|
/// split the critical edge. This will update DominatorTree and
|
2009-10-31 21:51:10 +00:00
|
|
|
/// DominatorFrontier information if it is available, thus calling this pass
|
|
|
|
/// will not invalidate either of them. This returns the new block if the edge
|
|
|
|
/// was split, null otherwise.
|
|
|
|
///
|
|
|
|
/// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
|
|
|
|
/// specified successor will be merged into the same critical edge block.
|
|
|
|
/// This is most commonly interesting with switch instructions, which may
|
|
|
|
/// have many edges to any one destination. This ensures that all edges to that
|
|
|
|
/// dest go to one block instead of each going to a different block, but isn't
|
|
|
|
/// the standard definition of a "critical edge".
|
|
|
|
///
|
|
|
|
/// It is invalid to call this function on a critical edge that starts at an
|
|
|
|
/// IndirectBrInst. Splitting these edges will almost always create an invalid
|
2009-11-01 18:17:37 +00:00
|
|
|
/// program because the address of the new block won't be the one that is jumped
|
2009-10-31 21:51:10 +00:00
|
|
|
/// to.
|
|
|
|
///
|
2009-09-08 15:45:00 +00:00
|
|
|
BasicBlock *llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
|
|
|
|
Pass *P, bool MergeIdenticalEdges) {
|
|
|
|
if (!isCriticalEdge(TI, SuccNum, MergeIdenticalEdges)) return 0;
|
2009-10-31 21:51:10 +00:00
|
|
|
|
|
|
|
assert(!isa<IndirectBrInst>(TI) &&
|
|
|
|
"Cannot split critical edge from IndirectBrInst");
|
|
|
|
|
2002-09-24 15:51:56 +00:00
|
|
|
BasicBlock *TIBB = TI->getParent();
|
2002-10-31 02:44:36 +00:00
|
|
|
BasicBlock *DestBB = TI->getSuccessor(SuccNum);
|
2002-09-24 15:51:56 +00:00
|
|
|
|
|
|
|
// Create a new basic block, linking it into the CFG.
|
2009-08-13 21:58:54 +00:00
|
|
|
BasicBlock *NewBB = BasicBlock::Create(TI->getContext(),
|
|
|
|
TIBB->getName() + "." + DestBB->getName() + "_crit_edge");
|
2002-09-24 15:51:56 +00:00
|
|
|
// Create our unconditional branch...
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(DestBB, NewBB);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2006-10-28 06:44:56 +00:00
|
|
|
// Branch to the new block, breaking the edge.
|
2002-09-24 15:51:56 +00:00
|
|
|
TI->setSuccessor(SuccNum, NewBB);
|
|
|
|
|
|
|
|
// Insert the block into the function... right after the block TI lives in.
|
|
|
|
Function &F = *TIBB->getParent();
|
2007-04-17 18:09:47 +00:00
|
|
|
Function::iterator FBBI = TIBB;
|
|
|
|
F.getBasicBlockList().insert(++FBBI, NewBB);
|
2006-10-28 06:44:56 +00:00
|
|
|
|
2002-09-24 15:51:56 +00:00
|
|
|
// If there are any PHI nodes in DestBB, we need to update them so that they
|
|
|
|
// merge incoming values from NewBB instead of from TIBB.
|
|
|
|
//
|
2004-09-15 17:06:42 +00:00
|
|
|
for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
2004-02-29 22:24:41 +00:00
|
|
|
// We no longer enter through TIBB, now we come in through NewBB. Revector
|
|
|
|
// exactly one entry in the PHI node that used to come from TIBB to come
|
|
|
|
// from NewBB.
|
2005-08-12 21:58:07 +00:00
|
|
|
int BBIdx = PN->getBasicBlockIndex(TIBB);
|
|
|
|
PN->setIncomingBlock(BBIdx, NewBB);
|
2002-09-24 15:51:56 +00:00
|
|
|
}
|
2006-10-28 06:44:56 +00:00
|
|
|
|
|
|
|
// If there are any other edges from TIBB to DestBB, update those to go
|
|
|
|
// through the split block, making those edges non-critical as well (and
|
|
|
|
// reducing the number of phi entries in the DestBB if relevant).
|
|
|
|
if (MergeIdenticalEdges) {
|
|
|
|
for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {
|
|
|
|
if (TI->getSuccessor(i) != DestBB) continue;
|
|
|
|
|
|
|
|
// Remove an entry for TIBB from DestBB phi nodes.
|
|
|
|
DestBB->removePredecessor(TIBB);
|
|
|
|
|
|
|
|
// We found another edge to DestBB, go to NewBB instead.
|
|
|
|
TI->setSuccessor(i, NewBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-24 15:51:56 +00:00
|
|
|
|
2002-10-08 21:06:27 +00:00
|
|
|
// If we don't have a pass object, we can't update anything...
|
2009-09-08 15:45:00 +00:00
|
|
|
if (P == 0) return NewBB;
|
2002-10-08 21:06:27 +00:00
|
|
|
|
2006-10-03 07:02:02 +00:00
|
|
|
// Now update analysis information. Since the only predecessor of NewBB is
|
|
|
|
// the TIBB, TIBB clearly dominates NewBB. TIBB usually doesn't dominate
|
|
|
|
// anything, as there are other successors of DestBB. However, if all other
|
|
|
|
// predecessors of DestBB are already dominated by DestBB (e.g. DestBB is a
|
|
|
|
// loop header) then NewBB dominates DestBB.
|
|
|
|
SmallVector<BasicBlock*, 8> OtherPreds;
|
2002-09-24 15:51:56 +00:00
|
|
|
|
2006-10-03 07:02:02 +00:00
|
|
|
for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E; ++I)
|
|
|
|
if (*I != NewBB)
|
|
|
|
OtherPreds.push_back(*I);
|
|
|
|
|
|
|
|
bool NewBBDominatesDestBB = true;
|
|
|
|
|
2002-09-26 16:18:51 +00:00
|
|
|
// Should we update DominatorTree information?
|
2009-01-28 13:14:17 +00:00
|
|
|
if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) {
|
2007-06-04 00:32:22 +00:00
|
|
|
DomTreeNode *TINode = DT->getNode(TIBB);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-09-26 16:18:51 +00:00
|
|
|
// The new block is not the immediate dominator for any other nodes, but
|
|
|
|
// TINode is the immediate dominator for the new node.
|
|
|
|
//
|
2006-10-03 07:02:02 +00:00
|
|
|
if (TINode) { // Don't break unreachable code!
|
2007-06-04 16:43:25 +00:00
|
|
|
DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
|
2007-06-04 00:32:22 +00:00
|
|
|
DomTreeNode *DestBBNode = 0;
|
2006-10-03 07:02:02 +00:00
|
|
|
|
|
|
|
// If NewBBDominatesDestBB hasn't been computed yet, do so with DT.
|
|
|
|
if (!OtherPreds.empty()) {
|
|
|
|
DestBBNode = DT->getNode(DestBB);
|
|
|
|
while (!OtherPreds.empty() && NewBBDominatesDestBB) {
|
2007-06-04 00:32:22 +00:00
|
|
|
if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back()))
|
2007-06-07 17:47:21 +00:00
|
|
|
NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
|
2006-10-03 07:02:02 +00:00
|
|
|
OtherPreds.pop_back();
|
|
|
|
}
|
|
|
|
OtherPreds.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it
|
|
|
|
// doesn't dominate anything.
|
|
|
|
if (NewBBDominatesDestBB) {
|
|
|
|
if (!DestBBNode) DestBBNode = DT->getNode(DestBB);
|
|
|
|
DT->changeImmediateDominator(DestBBNode, NewBBNode);
|
|
|
|
}
|
|
|
|
}
|
2002-09-24 15:51:56 +00:00
|
|
|
}
|
2002-10-31 02:44:36 +00:00
|
|
|
|
|
|
|
// Should we update DominanceFrontier information?
|
2009-01-28 13:14:17 +00:00
|
|
|
if (DominanceFrontier *DF = P->getAnalysisIfAvailable<DominanceFrontier>()) {
|
2006-10-03 07:02:02 +00:00
|
|
|
// If NewBBDominatesDestBB hasn't been computed yet, do so with DF.
|
|
|
|
if (!OtherPreds.empty()) {
|
|
|
|
// FIXME: IMPLEMENT THIS!
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Requiring domfrontiers but not idom/domtree/domset."
|
2009-07-11 20:10:48 +00:00
|
|
|
" not implemented yet!");
|
2006-10-03 07:02:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-31 02:44:36 +00:00
|
|
|
// Since the new block is dominated by its only predecessor TIBB,
|
2006-10-03 07:02:02 +00:00
|
|
|
// it cannot be in any block's dominance frontier. If NewBB dominates
|
|
|
|
// DestBB, its dominance frontier is the same as DestBB's, otherwise it is
|
|
|
|
// just {DestBB}.
|
2002-10-31 02:44:36 +00:00
|
|
|
DominanceFrontier::DomSetType NewDFSet;
|
2006-10-03 07:02:02 +00:00
|
|
|
if (NewBBDominatesDestBB) {
|
|
|
|
DominanceFrontier::iterator I = DF->find(DestBB);
|
2007-08-13 21:59:17 +00:00
|
|
|
if (I != DF->end()) {
|
2006-10-03 07:02:02 +00:00
|
|
|
DF->addBasicBlock(NewBB, I->second);
|
2008-06-03 18:29:48 +00:00
|
|
|
|
|
|
|
if (I->second.count(DestBB)) {
|
|
|
|
// However NewBB's frontier does not include DestBB.
|
|
|
|
DominanceFrontier::iterator NF = DF->find(NewBB);
|
|
|
|
DF->removeFromFrontier(NF, DestBB);
|
|
|
|
}
|
2007-08-13 21:59:17 +00:00
|
|
|
}
|
2006-10-03 07:02:02 +00:00
|
|
|
else
|
|
|
|
DF->addBasicBlock(NewBB, DominanceFrontier::DomSetType());
|
|
|
|
} else {
|
|
|
|
DominanceFrontier::DomSetType NewDFSet;
|
|
|
|
NewDFSet.insert(DestBB);
|
|
|
|
DF->addBasicBlock(NewBB, NewDFSet);
|
|
|
|
}
|
2002-10-31 02:44:36 +00:00
|
|
|
}
|
2005-08-13 01:38:43 +00:00
|
|
|
|
|
|
|
// Update LoopInfo if it is around.
|
2009-01-28 13:14:17 +00:00
|
|
|
if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>()) {
|
2009-09-08 15:45:00 +00:00
|
|
|
if (Loop *TIL = LI->getLoopFor(TIBB)) {
|
|
|
|
// If one or the other blocks were not in a loop, the new block is not
|
|
|
|
// either, and thus LI doesn't need to be updated.
|
2005-08-13 01:38:43 +00:00
|
|
|
if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
|
|
|
|
if (TIL == DestLoop) {
|
|
|
|
// Both in the same loop, the NewBB joins loop.
|
2007-11-27 03:43:35 +00:00
|
|
|
DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
|
2009-12-18 01:24:09 +00:00
|
|
|
} else if (TIL->contains(DestLoop)) {
|
2006-10-03 07:02:02 +00:00
|
|
|
// Edge from an outer loop to an inner loop. Add to the outer loop.
|
2007-11-27 03:43:35 +00:00
|
|
|
TIL->addBasicBlockToLoop(NewBB, LI->getBase());
|
2009-12-18 01:24:09 +00:00
|
|
|
} else if (DestLoop->contains(TIL)) {
|
2006-10-03 07:02:02 +00:00
|
|
|
// Edge from an inner loop to an outer loop. Add to the outer loop.
|
2007-11-27 03:43:35 +00:00
|
|
|
DestLoop->addBasicBlockToLoop(NewBB, LI->getBase());
|
2005-08-13 01:38:43 +00:00
|
|
|
} else {
|
|
|
|
// Edge from two loops with no containment relation. Because these
|
|
|
|
// are natural loops, we know that the destination block must be the
|
|
|
|
// header of its loop (adding a branch into a loop elsewhere would
|
|
|
|
// create an irreducible loop).
|
|
|
|
assert(DestLoop->getHeader() == DestBB &&
|
|
|
|
"Should not create irreducible loops!");
|
|
|
|
if (Loop *P = DestLoop->getParentLoop())
|
2007-11-27 03:43:35 +00:00
|
|
|
P->addBasicBlockToLoop(NewBB, LI->getBase());
|
2005-08-13 01:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-08 15:45:00 +00:00
|
|
|
// If TIBB is in a loop and DestBB is outside of that loop, split the
|
|
|
|
// other exit blocks of the loop that also have predecessors outside
|
|
|
|
// the loop, to maintain a LoopSimplify guarantee.
|
|
|
|
if (!TIL->contains(DestBB) &&
|
|
|
|
P->mustPreserveAnalysisID(LoopSimplifyID)) {
|
2009-09-09 18:18:18 +00:00
|
|
|
assert(!TIL->contains(NewBB) &&
|
|
|
|
"Split point for loop exit is contained in loop!");
|
|
|
|
|
|
|
|
// Update LCSSA form in the newly created exit block.
|
|
|
|
if (P->mustPreserveAnalysisID(LCSSAID)) {
|
|
|
|
SmallVector<BasicBlock *, 1> OrigPred;
|
|
|
|
OrigPred.push_back(TIBB);
|
|
|
|
CreatePHIsForSplitLoopExit(OrigPred, NewBB, DestBB);
|
|
|
|
}
|
|
|
|
|
2009-09-08 15:45:00 +00:00
|
|
|
// For each unique exit block...
|
|
|
|
SmallVector<BasicBlock *, 4> ExitBlocks;
|
|
|
|
TIL->getExitBlocks(ExitBlocks);
|
|
|
|
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
|
|
|
|
// Collect all the preds that are inside the loop, and note
|
|
|
|
// whether there are any preds outside the loop.
|
|
|
|
SmallVector<BasicBlock *, 4> Preds;
|
2009-09-09 18:18:18 +00:00
|
|
|
bool HasPredOutsideOfLoop = false;
|
2009-09-08 15:45:00 +00:00
|
|
|
BasicBlock *Exit = ExitBlocks[i];
|
|
|
|
for (pred_iterator I = pred_begin(Exit), E = pred_end(Exit);
|
|
|
|
I != E; ++I)
|
|
|
|
if (TIL->contains(*I))
|
|
|
|
Preds.push_back(*I);
|
|
|
|
else
|
2009-09-09 18:18:18 +00:00
|
|
|
HasPredOutsideOfLoop = true;
|
2009-09-08 15:45:00 +00:00
|
|
|
// If there are any preds not in the loop, we'll need to split
|
|
|
|
// the edges. The Preds.empty() check is needed because a block
|
|
|
|
// may appear multiple times in the list. We can't use
|
|
|
|
// getUniqueExitBlocks above because that depends on LoopSimplify
|
|
|
|
// form, which we're in the process of restoring!
|
2009-09-09 18:18:18 +00:00
|
|
|
if (!Preds.empty() && HasPredOutsideOfLoop) {
|
|
|
|
BasicBlock *NewExitBB =
|
|
|
|
SplitBlockPredecessors(Exit, Preds.data(), Preds.size(),
|
|
|
|
"split", P);
|
|
|
|
if (P->mustPreserveAnalysisID(LCSSAID))
|
|
|
|
CreatePHIsForSplitLoopExit(Preds, NewExitBB, Exit);
|
|
|
|
}
|
2009-09-08 15:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// LCSSA form was updated above for the case where LoopSimplify is
|
|
|
|
// available, which means that all predecessors of loop exit blocks
|
|
|
|
// are within the loop. Without LoopSimplify form, it would be
|
|
|
|
// necessary to insert a new phi.
|
|
|
|
assert((!P->mustPreserveAnalysisID(LCSSAID) ||
|
|
|
|
P->mustPreserveAnalysisID(LoopSimplifyID)) &&
|
|
|
|
"SplitCriticalEdge doesn't know how to update LCCSA form "
|
|
|
|
"without LoopSimplify!");
|
|
|
|
}
|
2005-08-13 01:38:43 +00:00
|
|
|
}
|
2009-09-08 15:45:00 +00:00
|
|
|
|
2009-09-09 17:53:39 +00:00
|
|
|
// Update ProfileInfo if it is around.
|
|
|
|
if (ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>()) {
|
|
|
|
PI->splitEdge(TIBB,DestBB,NewBB,MergeIdenticalEdges);
|
|
|
|
}
|
|
|
|
|
2009-09-08 15:45:00 +00:00
|
|
|
return NewBB;
|
2002-09-24 15:51:56 +00:00
|
|
|
}
|