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
|
2011-01-18 04:11:31 +00:00
|
|
|
// dominator trees.
|
2002-09-24 00:08:39 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-07-27 01:24:00 +00:00
|
|
|
#include "llvm/Analysis/CFG.h"
|
2005-08-13 01:38:43 +00:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2014-03-04 11:45:46 +00:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-01-13 09:26:24 +00:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2004-01-09 06:12:26 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "break-crit-edges"
|
|
|
|
|
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
|
2010-10-19 17:21:58 +00:00
|
|
|
BreakCriticalEdges() : FunctionPass(ID) {
|
|
|
|
initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-13 13:07:17 +00:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2005-08-13 01:38:43 +00:00
|
|
|
AU.addPreserved<LoopInfo>();
|
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;
|
2010-08-23 17:52:01 +00:00
|
|
|
INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Break critical edges in CFG", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2011-04-15 05:18:47 +00:00
|
|
|
// Publicly exposed interface to pass...
|
2010-08-06 18:33:48 +00:00
|
|
|
char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID;
|
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
|
|
|
|
2012-04-30 10:25:51 +00:00
|
|
|
/// createPHIsForSplitLoopExit - When a loop exit edge is split, LCSSA form
|
2009-09-09 18:18:18 +00:00
|
|
|
/// may require new PHIs in the new exit block. This function inserts the
|
2012-04-30 10:44:54 +00:00
|
|
|
/// new PHIs, as needed. Preds is a list of preds inside the loop, SplitBB
|
2009-09-09 18:18:18 +00:00
|
|
|
/// is the new loop exit block, and DestBB is the old loop exit, now the
|
|
|
|
/// successor of SplitBB.
|
2012-04-30 10:25:51 +00:00
|
|
|
static void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
|
2009-09-09 18:18:18 +00:00
|
|
|
BasicBlock *SplitBB,
|
|
|
|
BasicBlock *DestBB) {
|
|
|
|
// SplitBB shouldn't have anything non-trivial in it yet.
|
2012-04-30 10:44:54 +00:00
|
|
|
assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||
|
|
|
|
SplitBB->isLandingPad()) && "SplitBB has non-PHI nodes!");
|
2009-09-09 18:18:18 +00:00
|
|
|
|
2012-04-30 10:44:54 +00:00
|
|
|
// For each PHI in the destination block.
|
2009-09-09 18:18:18 +00:00
|
|
|
for (BasicBlock::iterator I = DestBB->begin();
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(I); ++I) {
|
|
|
|
unsigned Idx = PN->getBasicBlockIndex(SplitBB);
|
|
|
|
Value *V = PN->getIncomingValue(Idx);
|
2012-04-30 10:44:54 +00:00
|
|
|
|
2009-09-09 18:18:18 +00:00
|
|
|
// 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;
|
2012-04-30 10:44:54 +00:00
|
|
|
|
2009-09-09 18:18:18 +00:00
|
|
|
// Otherwise a new PHI is needed. Create one and populate it.
|
2012-04-30 10:44:54 +00:00
|
|
|
PHINode *NewPN =
|
|
|
|
PHINode::Create(PN->getType(), Preds.size(), "split",
|
|
|
|
SplitBB->isLandingPad() ?
|
|
|
|
SplitBB->begin() : SplitBB->getTerminator());
|
2009-09-09 18:18:18 +00:00
|
|
|
for (unsigned i = 0, e = Preds.size(); i != e; ++i)
|
|
|
|
NewPN->addIncoming(V, Preds[i]);
|
2012-04-30 10:44:54 +00:00
|
|
|
|
2009-09-09 18:18:18 +00:00
|
|
|
// 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
|
2011-01-18 04:11:31 +00:00
|
|
|
/// split the critical edge. This will update DominatorTree 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.
|
2009-10-31 21:51:10 +00:00
|
|
|
///
|
|
|
|
/// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
|
2011-10-04 03:34:49 +00:00
|
|
|
/// specified successor will be merged into the same critical edge block.
|
|
|
|
/// This is most commonly interesting with switch instructions, which may
|
2009-10-31 21:51:10 +00:00
|
|
|
/// have many edges to any one destination. This ensures that all edges to that
|
2011-10-04 03:34:49 +00:00
|
|
|
/// dest go to one block instead of each going to a different block, but isn't
|
2009-10-31 21:51:10 +00:00
|
|
|
/// 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,
|
2011-10-04 03:50:44 +00:00
|
|
|
Pass *P, bool MergeIdenticalEdges,
|
2012-04-30 10:44:54 +00:00
|
|
|
bool DontDeleteUselessPhis,
|
|
|
|
bool SplitLandingPads) {
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!isCriticalEdge(TI, SuccNum, MergeIdenticalEdges)) return nullptr;
|
2011-10-04 03:34:49 +00:00
|
|
|
|
2009-10-31 21:51:10 +00:00
|
|
|
assert(!isa<IndirectBrInst>(TI) &&
|
|
|
|
"Cannot split critical edge from IndirectBrInst");
|
2011-10-04 03:34:49 +00:00
|
|
|
|
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
|
|
|
|
2011-08-17 21:04:05 +00:00
|
|
|
// Splitting the critical edge to a landing pad block is non-trivial. Don't do
|
|
|
|
// it in this generic function.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (DestBB->isLandingPad()) return nullptr;
|
2011-08-17 21:04:05 +00:00
|
|
|
|
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");
|
2010-02-13 04:24:19 +00:00
|
|
|
// Create our unconditional branch.
|
2011-05-17 19:43:06 +00:00
|
|
|
BranchInst *NewBI = BranchInst::Create(DestBB, NewBB);
|
|
|
|
NewBI->setDebugLoc(TI->getDebugLoc());
|
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);
|
2011-10-04 03:34:49 +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.
|
2011-06-23 09:09:15 +00:00
|
|
|
{
|
|
|
|
unsigned BBIdx = 0;
|
|
|
|
for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
// 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.
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
|
|
|
|
|
|
|
// Reuse the previous value of BBIdx if it lines up. In cases where we
|
|
|
|
// have multiple phi nodes with *lots* of predecessors, this is a speed
|
|
|
|
// win because we don't have to scan the PHI looking for TIBB. This
|
|
|
|
// happens because the BB list of PHI nodes are usually in the same
|
|
|
|
// order.
|
|
|
|
if (PN->getIncomingBlock(BBIdx) != TIBB)
|
2011-10-04 03:34:49 +00:00
|
|
|
BBIdx = PN->getBasicBlockIndex(TIBB);
|
2011-06-23 09:09:15 +00:00
|
|
|
PN->setIncomingBlock(BBIdx, NewBB);
|
2010-02-13 05:01:14 +00:00
|
|
|
}
|
2002-09-24 15:51:56 +00:00
|
|
|
}
|
2011-10-04 03:34:49 +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;
|
2011-10-04 03:34:49 +00:00
|
|
|
|
2006-10-28 06:44:56 +00:00
|
|
|
// Remove an entry for TIBB from DestBB phi nodes.
|
2011-10-04 03:50:44 +00:00
|
|
|
DestBB->removePredecessor(TIBB, DontDeleteUselessPhis);
|
2011-10-04 03:34:49 +00:00
|
|
|
|
2006-10-28 06:44:56 +00:00
|
|
|
// We found another edge to DestBB, go to NewBB instead.
|
|
|
|
TI->setSuccessor(i, NewBB);
|
|
|
|
}
|
|
|
|
}
|
2011-10-04 03:34:49 +00:00
|
|
|
|
|
|
|
|
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...
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!P) return NewBB;
|
2011-10-04 03:34:49 +00:00
|
|
|
|
2014-01-13 13:07:17 +00:00
|
|
|
DominatorTreeWrapperPass *DTWP =
|
|
|
|
P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
|
2014-04-25 05:29:35 +00:00
|
|
|
DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
|
2010-02-13 05:01:14 +00:00
|
|
|
LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>();
|
2011-10-04 03:34:49 +00:00
|
|
|
|
2010-02-13 05:01:14 +00:00
|
|
|
// If we have nothing to update, just return.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!DT && !LI)
|
2010-02-13 05:01:14 +00:00
|
|
|
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
|
|
|
|
2010-02-13 04:15:26 +00:00
|
|
|
// If there is a PHI in the block, loop over predecessors with it, which is
|
|
|
|
// faster than iterating pred_begin/end.
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
|
|
|
if (PN->getIncomingBlock(i) != NewBB)
|
|
|
|
OtherPreds.push_back(PN->getIncomingBlock(i));
|
|
|
|
} else {
|
2014-07-21 17:06:51 +00:00
|
|
|
for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB);
|
|
|
|
I != E; ++I) {
|
|
|
|
BasicBlock *P = *I;
|
2010-07-09 15:25:42 +00:00
|
|
|
if (P != NewBB)
|
2011-01-14 04:23:53 +00:00
|
|
|
OtherPreds.push_back(P);
|
2010-07-09 15:25:42 +00:00
|
|
|
}
|
2010-02-13 04:15:26 +00:00
|
|
|
}
|
2010-07-09 15:25:42 +00:00
|
|
|
|
2006-10-03 07:02:02 +00:00
|
|
|
bool NewBBDominatesDestBB = true;
|
2011-10-04 03:34:49 +00:00
|
|
|
|
2002-09-26 16:18:51 +00:00
|
|
|
// Should we update DominatorTree information?
|
2010-02-13 05:01:14 +00:00
|
|
|
if (DT) {
|
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);
|
2014-04-25 05:29:35 +00:00
|
|
|
DomTreeNode *DestBBNode = nullptr;
|
2011-10-04 03:34:49 +00:00
|
|
|
|
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();
|
|
|
|
}
|
2011-10-04 03:34:49 +00:00
|
|
|
|
2006-10-03 07:02:02 +00:00
|
|
|
// 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
|
|
|
|
2005-08-13 01:38:43 +00:00
|
|
|
// Update LoopInfo if it is around.
|
2010-02-13 05:01:14 +00:00
|
|
|
if (LI) {
|
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
|
|
|
}
|
|
|
|
}
|
[LPM] Fix PR18643, another scary place where loop transforms failed to
preserve loop simplify of enclosing loops.
The problem here starts with LoopRotation which ends up cloning code out
of the latch into the new preheader it is buidling. This can create
a new edge from the preheader into the exit block of the loop which
breaks LoopSimplify form. The code tries to fix this by splitting the
critical edge between the latch and the exit block to get a new exit
block that only the latch dominates. This sadly isn't sufficient.
The exit block may be an exit block for multiple nested loops. When we
clone an edge from the latch of the inner loop to the new preheader
being built in the outer loop, we create an exiting edge from the outer
loop to this exit block. Despite breaking the LoopSimplify form for the
inner loop, this is fine for the outer loop. However, when we split the
edge from the inner loop to the exit block, we create a new block which
is in neither the inner nor outer loop as the new exit block. This is
a predecessor to the old exit block, and so the split itself takes the
outer loop out of LoopSimplify form. We need to split every edge
entering the exit block from inside a loop nested more deeply than the
exit block in order to preserve all of the loop simplify constraints.
Once we try to do that, a problem with splitting critical edges
surfaces. Previously, we tried a very brute force to update LoopSimplify
form by re-computing it for all exit blocks. We don't need to do this,
and doing this much will sometimes but not always overlap with the
LoopRotate bug fix. Instead, the code needs to specifically handle the
cases which can start to violate LoopSimplify -- they aren't that
common. We need to see if the destination of the split edge was a loop
exit block in simplified form for the loop of the source of the edge.
For this to be true, all the predecessors need to be in the exact same
loop as the source of the edge being split. If the dest block was
originally in this form, we have to split all of the deges back into
this loop to recover it. The old mechanism of doing this was
conservatively correct because at least *one* of the exiting blocks it
rewrote was the DestBB and so the DestBB's predecessors were fixed. But
this is a much more targeted way of doing it. Making it targeted is
important, because ballooning the set of edges touched prevents
LoopRotate from being able to split edges *it* needs to split to
preserve loop simplify in a coherent way -- the critical edge splitting
would sometimes find the other edges in need of splitting but not
others.
Many, *many* thanks for help from Nick reducing these test cases
mightily. And helping lots with the analysis here as this one was quite
tricky to track down.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200393 91177308-0d34-0410-b5e6-96231b3b80d8
2014-01-29 13:16:53 +00:00
|
|
|
// If TIBB is in a loop and DestBB is outside of that loop, we may need
|
|
|
|
// to update LoopSimplify form and LCSSA form.
|
2009-09-08 15:45:00 +00:00
|
|
|
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.
|
2012-04-30 10:25:51 +00:00
|
|
|
if (P->mustPreserveAnalysisID(LCSSAID))
|
|
|
|
createPHIsForSplitLoopExit(TIBB, NewBB, DestBB);
|
2009-09-09 18:18:18 +00:00
|
|
|
|
[LPM] Fix PR18643, another scary place where loop transforms failed to
preserve loop simplify of enclosing loops.
The problem here starts with LoopRotation which ends up cloning code out
of the latch into the new preheader it is buidling. This can create
a new edge from the preheader into the exit block of the loop which
breaks LoopSimplify form. The code tries to fix this by splitting the
critical edge between the latch and the exit block to get a new exit
block that only the latch dominates. This sadly isn't sufficient.
The exit block may be an exit block for multiple nested loops. When we
clone an edge from the latch of the inner loop to the new preheader
being built in the outer loop, we create an exiting edge from the outer
loop to this exit block. Despite breaking the LoopSimplify form for the
inner loop, this is fine for the outer loop. However, when we split the
edge from the inner loop to the exit block, we create a new block which
is in neither the inner nor outer loop as the new exit block. This is
a predecessor to the old exit block, and so the split itself takes the
outer loop out of LoopSimplify form. We need to split every edge
entering the exit block from inside a loop nested more deeply than the
exit block in order to preserve all of the loop simplify constraints.
Once we try to do that, a problem with splitting critical edges
surfaces. Previously, we tried a very brute force to update LoopSimplify
form by re-computing it for all exit blocks. We don't need to do this,
and doing this much will sometimes but not always overlap with the
LoopRotate bug fix. Instead, the code needs to specifically handle the
cases which can start to violate LoopSimplify -- they aren't that
common. We need to see if the destination of the split edge was a loop
exit block in simplified form for the loop of the source of the edge.
For this to be true, all the predecessors need to be in the exact same
loop as the source of the edge being split. If the dest block was
originally in this form, we have to split all of the deges back into
this loop to recover it. The old mechanism of doing this was
conservatively correct because at least *one* of the exiting blocks it
rewrote was the DestBB and so the DestBB's predecessors were fixed. But
this is a much more targeted way of doing it. Making it targeted is
important, because ballooning the set of edges touched prevents
LoopRotate from being able to split edges *it* needs to split to
preserve loop simplify in a coherent way -- the critical edge splitting
would sometimes find the other edges in need of splitting but not
others.
Many, *many* thanks for help from Nick reducing these test cases
mightily. And helping lots with the analysis here as this one was quite
tricky to track down.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200393 91177308-0d34-0410-b5e6-96231b3b80d8
2014-01-29 13:16:53 +00:00
|
|
|
// The only that we can break LoopSimplify form by splitting a critical
|
|
|
|
// edge is if after the split there exists some edge from TIL to DestBB
|
|
|
|
// *and* the only edge into DestBB from outside of TIL is that of
|
|
|
|
// NewBB. If the first isn't true, then LoopSimplify still holds, NewBB
|
|
|
|
// is the new exit block and it has no non-loop predecessors. If the
|
|
|
|
// second isn't true, then DestBB was not in LoopSimplify form prior to
|
|
|
|
// the split as it had a non-loop predecessor. In both of these cases,
|
|
|
|
// the predecessor must be directly in TIL, not in a subloop, or again
|
|
|
|
// LoopSimplify doesn't hold.
|
|
|
|
SmallVector<BasicBlock *, 4> LoopPreds;
|
2014-07-21 17:06:51 +00:00
|
|
|
for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E;
|
|
|
|
++I) {
|
|
|
|
BasicBlock *P = *I;
|
[LPM] Fix PR18643, another scary place where loop transforms failed to
preserve loop simplify of enclosing loops.
The problem here starts with LoopRotation which ends up cloning code out
of the latch into the new preheader it is buidling. This can create
a new edge from the preheader into the exit block of the loop which
breaks LoopSimplify form. The code tries to fix this by splitting the
critical edge between the latch and the exit block to get a new exit
block that only the latch dominates. This sadly isn't sufficient.
The exit block may be an exit block for multiple nested loops. When we
clone an edge from the latch of the inner loop to the new preheader
being built in the outer loop, we create an exiting edge from the outer
loop to this exit block. Despite breaking the LoopSimplify form for the
inner loop, this is fine for the outer loop. However, when we split the
edge from the inner loop to the exit block, we create a new block which
is in neither the inner nor outer loop as the new exit block. This is
a predecessor to the old exit block, and so the split itself takes the
outer loop out of LoopSimplify form. We need to split every edge
entering the exit block from inside a loop nested more deeply than the
exit block in order to preserve all of the loop simplify constraints.
Once we try to do that, a problem with splitting critical edges
surfaces. Previously, we tried a very brute force to update LoopSimplify
form by re-computing it for all exit blocks. We don't need to do this,
and doing this much will sometimes but not always overlap with the
LoopRotate bug fix. Instead, the code needs to specifically handle the
cases which can start to violate LoopSimplify -- they aren't that
common. We need to see if the destination of the split edge was a loop
exit block in simplified form for the loop of the source of the edge.
For this to be true, all the predecessors need to be in the exact same
loop as the source of the edge being split. If the dest block was
originally in this form, we have to split all of the deges back into
this loop to recover it. The old mechanism of doing this was
conservatively correct because at least *one* of the exiting blocks it
rewrote was the DestBB and so the DestBB's predecessors were fixed. But
this is a much more targeted way of doing it. Making it targeted is
important, because ballooning the set of edges touched prevents
LoopRotate from being able to split edges *it* needs to split to
preserve loop simplify in a coherent way -- the critical edge splitting
would sometimes find the other edges in need of splitting but not
others.
Many, *many* thanks for help from Nick reducing these test cases
mightily. And helping lots with the analysis here as this one was quite
tricky to track down.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200393 91177308-0d34-0410-b5e6-96231b3b80d8
2014-01-29 13:16:53 +00:00
|
|
|
if (P == NewBB)
|
|
|
|
continue; // The new block is known.
|
|
|
|
if (LI->getLoopFor(P) != TIL) {
|
|
|
|
// No need to re-simplify, it wasn't to start with.
|
|
|
|
LoopPreds.clear();
|
|
|
|
break;
|
2009-09-09 18:18:18 +00:00
|
|
|
}
|
[LPM] Fix PR18643, another scary place where loop transforms failed to
preserve loop simplify of enclosing loops.
The problem here starts with LoopRotation which ends up cloning code out
of the latch into the new preheader it is buidling. This can create
a new edge from the preheader into the exit block of the loop which
breaks LoopSimplify form. The code tries to fix this by splitting the
critical edge between the latch and the exit block to get a new exit
block that only the latch dominates. This sadly isn't sufficient.
The exit block may be an exit block for multiple nested loops. When we
clone an edge from the latch of the inner loop to the new preheader
being built in the outer loop, we create an exiting edge from the outer
loop to this exit block. Despite breaking the LoopSimplify form for the
inner loop, this is fine for the outer loop. However, when we split the
edge from the inner loop to the exit block, we create a new block which
is in neither the inner nor outer loop as the new exit block. This is
a predecessor to the old exit block, and so the split itself takes the
outer loop out of LoopSimplify form. We need to split every edge
entering the exit block from inside a loop nested more deeply than the
exit block in order to preserve all of the loop simplify constraints.
Once we try to do that, a problem with splitting critical edges
surfaces. Previously, we tried a very brute force to update LoopSimplify
form by re-computing it for all exit blocks. We don't need to do this,
and doing this much will sometimes but not always overlap with the
LoopRotate bug fix. Instead, the code needs to specifically handle the
cases which can start to violate LoopSimplify -- they aren't that
common. We need to see if the destination of the split edge was a loop
exit block in simplified form for the loop of the source of the edge.
For this to be true, all the predecessors need to be in the exact same
loop as the source of the edge being split. If the dest block was
originally in this form, we have to split all of the deges back into
this loop to recover it. The old mechanism of doing this was
conservatively correct because at least *one* of the exiting blocks it
rewrote was the DestBB and so the DestBB's predecessors were fixed. But
this is a much more targeted way of doing it. Making it targeted is
important, because ballooning the set of edges touched prevents
LoopRotate from being able to split edges *it* needs to split to
preserve loop simplify in a coherent way -- the critical edge splitting
would sometimes find the other edges in need of splitting but not
others.
Many, *many* thanks for help from Nick reducing these test cases
mightily. And helping lots with the analysis here as this one was quite
tricky to track down.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200393 91177308-0d34-0410-b5e6-96231b3b80d8
2014-01-29 13:16:53 +00:00
|
|
|
LoopPreds.push_back(P);
|
|
|
|
}
|
|
|
|
if (!LoopPreds.empty()) {
|
|
|
|
assert(!DestBB->isLandingPad() &&
|
|
|
|
"We don't split edges to landing pads!");
|
|
|
|
BasicBlock *NewExitBB =
|
|
|
|
SplitBlockPredecessors(DestBB, LoopPreds, "split", P);
|
|
|
|
if (P->mustPreserveAnalysisID(LCSSAID))
|
|
|
|
createPHIsForSplitLoopExit(LoopPreds, NewExitBB, DestBB);
|
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
|
|
|
|
|
|
|
return NewBB;
|
2002-09-24 15:51:56 +00:00
|
|
|
}
|