2002-09-24 08:08:39 +08:00
|
|
|
//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-24 08:08:39 +08: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 12:11:31 +08:00
|
|
|
// dominator trees.
|
2002-09-24 08:08:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-23 02:04:25 +08:00
|
|
|
#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
|
2017-12-05 04:36:01 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-12-13 03:07:43 +08:00
|
|
|
#include "llvm/Analysis/BlockFrequencyInfo.h"
|
|
|
|
#include "llvm/Analysis/BranchProbabilityInfo.h"
|
2013-07-27 09:24:00 +08:00
|
|
|
#include "llvm/Analysis/CFG.h"
|
2005-08-13 09:38:43 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2018-08-22 07:32:03 +08:00
|
|
|
#include "llvm/Analysis/MemorySSAUpdater.h"
|
2019-02-22 23:01:41 +08:00
|
|
|
#include "llvm/Analysis/PostDominators.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2018-03-29 01:44:36 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2017-12-05 04:36:01 +08:00
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
#include "llvm/Transforms/Utils/ValueMapper.h"
|
2004-01-09 14:12:26 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "break-crit-edges"
|
|
|
|
|
2006-12-20 06:17:40 +08:00
|
|
|
STATISTIC(NumBroken, "Number of blocks inserted");
|
2002-09-24 23:43:12 +08:00
|
|
|
|
2006-12-20 06:17:40 +08:00
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
struct BreakCriticalEdges : public FunctionPass {
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-20 01:21:58 +08:00
|
|
|
BreakCriticalEdges() : FunctionPass(ID) {
|
|
|
|
initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2014-11-19 08:17:31 +08:00
|
|
|
bool runOnFunction(Function &F) override {
|
2015-01-19 20:09:11 +08:00
|
|
|
auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
|
|
|
|
auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
|
2019-02-22 23:01:41 +08:00
|
|
|
|
|
|
|
auto *PDTWP = getAnalysisIfAvailable<PostDominatorTreeWrapperPass>();
|
|
|
|
auto *PDT = PDTWP ? &PDTWP->getPostDomTree() : nullptr;
|
|
|
|
|
2015-01-19 20:09:11 +08:00
|
|
|
auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
|
|
|
|
auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
|
|
|
|
unsigned N =
|
2019-02-22 23:01:41 +08:00
|
|
|
SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI, nullptr, PDT));
|
2014-11-19 08:17:31 +08:00
|
|
|
NumBroken += N;
|
|
|
|
return N > 0;
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2014-01-13 21:07:17 +08:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2015-01-17 22:16:18 +08:00
|
|
|
AU.addPreserved<LoopInfoWrapperPass>();
|
2003-10-13 05:52:28 +08:00
|
|
|
|
|
|
|
// No loop canonicalization guarantees are broken by this pass.
|
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
2002-09-24 23:43:12 +08:00
|
|
|
}
|
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2002-09-24 08:08:39 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char BreakCriticalEdges::ID = 0;
|
2010-08-24 01:52:01 +08:00
|
|
|
INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Break critical edges in CFG", false, false)
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2011-04-15 13:18:47 +08:00
|
|
|
// Publicly exposed interface to pass...
|
2010-08-07 02:33:48 +08:00
|
|
|
char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID;
|
2004-07-31 18:01:58 +08:00
|
|
|
FunctionPass *llvm::createBreakCriticalEdgesPass() {
|
|
|
|
return new BreakCriticalEdges();
|
|
|
|
}
|
2002-09-24 08:08:39 +08:00
|
|
|
|
2016-07-23 02:04:25 +08:00
|
|
|
PreservedAnalyses BreakCriticalEdgesPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
|
|
|
auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
|
|
|
|
auto *LI = AM.getCachedResult<LoopAnalysis>(F);
|
|
|
|
unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI));
|
|
|
|
NumBroken += N;
|
|
|
|
if (N == 0)
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
PreservedAnalyses PA;
|
|
|
|
PA.preserve<DominatorTreeAnalysis>();
|
|
|
|
PA.preserve<LoopAnalysis>();
|
|
|
|
return PA;
|
|
|
|
}
|
|
|
|
|
2003-11-10 12:42:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implementation of the external critical edge manipulation functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-09-24 23:51:56 +08:00
|
|
|
|
2016-04-24 00:31:48 +08:00
|
|
|
/// 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.
|
2012-04-30 18:25:51 +08:00
|
|
|
static void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
|
2009-09-10 02:18:18 +08:00
|
|
|
BasicBlock *SplitBB,
|
|
|
|
BasicBlock *DestBB) {
|
|
|
|
// SplitBB shouldn't have anything non-trivial in it yet.
|
2012-04-30 18:44:54 +08:00
|
|
|
assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||
|
|
|
|
SplitBB->isLandingPad()) && "SplitBB has non-PHI nodes!");
|
2009-09-10 02:18:18 +08:00
|
|
|
|
2012-04-30 18:44:54 +08:00
|
|
|
// For each PHI in the destination block.
|
2017-12-30 23:27:33 +08:00
|
|
|
for (PHINode &PN : DestBB->phis()) {
|
|
|
|
unsigned Idx = PN.getBasicBlockIndex(SplitBB);
|
|
|
|
Value *V = PN.getIncomingValue(Idx);
|
2012-04-30 18:44:54 +08:00
|
|
|
|
2009-09-10 02:18:18 +08: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 18:44:54 +08:00
|
|
|
|
2009-09-10 02:18:18 +08:00
|
|
|
// Otherwise a new PHI is needed. Create one and populate it.
|
2015-10-13 10:39:05 +08:00
|
|
|
PHINode *NewPN = PHINode::Create(
|
2017-12-30 23:27:33 +08:00
|
|
|
PN.getType(), Preds.size(), "split",
|
2015-10-13 10:39:05 +08:00
|
|
|
SplitBB->isLandingPad() ? &SplitBB->front() : SplitBB->getTerminator());
|
2009-09-10 02:18:18 +08:00
|
|
|
for (unsigned i = 0, e = Preds.size(); i != e; ++i)
|
|
|
|
NewPN->addIncoming(V, Preds[i]);
|
2012-04-30 18:44:54 +08:00
|
|
|
|
2009-09-10 02:18:18 +08:00
|
|
|
// Update the original PHI.
|
2017-12-30 23:27:33 +08:00
|
|
|
PN.setIncomingValue(Idx, NewPN);
|
2009-09-10 02:18:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-24 00:31:48 +08:00
|
|
|
BasicBlock *
|
2018-10-15 17:17:09 +08:00
|
|
|
llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
|
2016-04-24 00:31:48 +08:00
|
|
|
const CriticalEdgeSplittingOptions &Options) {
|
2015-01-19 20:09:11 +08:00
|
|
|
if (!isCriticalEdge(TI, SuccNum, Options.MergeIdenticalEdges))
|
|
|
|
return nullptr;
|
2011-10-04 11:34:49 +08:00
|
|
|
|
2009-11-01 05:51:10 +08:00
|
|
|
assert(!isa<IndirectBrInst>(TI) &&
|
|
|
|
"Cannot split critical edge from IndirectBrInst");
|
2011-10-04 11:34:49 +08:00
|
|
|
|
2002-09-24 23:51:56 +08:00
|
|
|
BasicBlock *TIBB = TI->getParent();
|
2002-10-31 10:44:36 +08:00
|
|
|
BasicBlock *DestBB = TI->getSuccessor(SuccNum);
|
2002-09-24 23:51:56 +08:00
|
|
|
|
2015-08-04 16:21:40 +08:00
|
|
|
// Splitting the critical edge to a pad block is non-trivial. Don't do
|
2011-08-18 05:04:05 +08:00
|
|
|
// it in this generic function.
|
2015-08-04 16:21:40 +08:00
|
|
|
if (DestBB->isEHPad()) return nullptr;
|
2011-08-18 05:04:05 +08:00
|
|
|
|
2019-02-09 04:48:56 +08:00
|
|
|
// Don't split the non-fallthrough edge from a callbr.
|
|
|
|
if (isa<CallBrInst>(TI) && SuccNum > 0)
|
|
|
|
return nullptr;
|
|
|
|
|
2019-03-13 02:20:25 +08:00
|
|
|
if (Options.IgnoreUnreachableDests &&
|
|
|
|
isa<UnreachableInst>(DestBB->getFirstNonPHIOrDbgOrLifetime()))
|
|
|
|
return nullptr;
|
|
|
|
|
2002-09-24 23:51:56 +08:00
|
|
|
// Create a new basic block, linking it into the CFG.
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *NewBB = BasicBlock::Create(TI->getContext(),
|
|
|
|
TIBB->getName() + "." + DestBB->getName() + "_crit_edge");
|
2010-02-13 12:24:19 +08:00
|
|
|
// Create our unconditional branch.
|
2011-05-18 03:43:06 +08:00
|
|
|
BranchInst *NewBI = BranchInst::Create(DestBB, NewBB);
|
|
|
|
NewBI->setDebugLoc(TI->getDebugLoc());
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2006-10-28 14:44:56 +08:00
|
|
|
// Branch to the new block, breaking the edge.
|
2002-09-24 23:51:56 +08:00
|
|
|
TI->setSuccessor(SuccNum, NewBB);
|
|
|
|
|
|
|
|
// Insert the block into the function... right after the block TI lives in.
|
|
|
|
Function &F = *TIBB->getParent();
|
2015-10-13 10:39:05 +08:00
|
|
|
Function::iterator FBBI = TIBB->getIterator();
|
2007-04-18 02:09:47 +08:00
|
|
|
F.getBasicBlockList().insert(++FBBI, NewBB);
|
2011-10-04 11:34:49 +08:00
|
|
|
|
2002-09-24 23:51:56 +08: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 17:09:15 +08: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 11:34:49 +08:00
|
|
|
BBIdx = PN->getBasicBlockIndex(TIBB);
|
2011-06-23 17:09:15 +08:00
|
|
|
PN->setIncomingBlock(BBIdx, NewBB);
|
2010-02-13 13:01:14 +08:00
|
|
|
}
|
2002-09-24 23:51:56 +08:00
|
|
|
}
|
2011-10-04 11:34:49 +08:00
|
|
|
|
2006-10-28 14:44:56 +08: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).
|
2015-01-19 20:09:11 +08:00
|
|
|
if (Options.MergeIdenticalEdges) {
|
2006-10-28 14:44:56 +08:00
|
|
|
for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {
|
|
|
|
if (TI->getSuccessor(i) != DestBB) continue;
|
2011-10-04 11:34:49 +08:00
|
|
|
|
2006-10-28 14:44:56 +08:00
|
|
|
// Remove an entry for TIBB from DestBB phi nodes.
|
2019-02-12 15:09:29 +08:00
|
|
|
DestBB->removePredecessor(TIBB, Options.KeepOneInputPHIs);
|
2011-10-04 11:34:49 +08:00
|
|
|
|
2006-10-28 14:44:56 +08:00
|
|
|
// We found another edge to DestBB, go to NewBB instead.
|
|
|
|
TI->setSuccessor(i, NewBB);
|
|
|
|
}
|
|
|
|
}
|
2011-10-04 11:34:49 +08:00
|
|
|
|
2010-02-13 13:01:14 +08:00
|
|
|
// If we have nothing to update, just return.
|
2015-01-19 20:09:11 +08:00
|
|
|
auto *DT = Options.DT;
|
2019-02-22 23:01:41 +08:00
|
|
|
auto *PDT = Options.PDT;
|
2015-01-19 20:09:11 +08:00
|
|
|
auto *LI = Options.LI;
|
2018-08-22 07:32:03 +08:00
|
|
|
auto *MSSAU = Options.MSSAU;
|
|
|
|
if (MSSAU)
|
2018-09-08 05:14:48 +08:00
|
|
|
MSSAU->wireOldPredecessorsToNewImmediatePredecessor(
|
|
|
|
DestBB, NewBB, {TIBB}, Options.MergeIdenticalEdges);
|
2018-08-22 07:32:03 +08:00
|
|
|
|
2019-02-22 23:01:41 +08:00
|
|
|
if (!DT && !PDT && !LI)
|
2010-02-13 13:01:14 +08:00
|
|
|
return NewBB;
|
2002-10-09 05:06:27 +08:00
|
|
|
|
2019-02-22 23:01:41 +08:00
|
|
|
if (DT || PDT) {
|
2017-08-18 00:45:35 +08:00
|
|
|
// Update the DominatorTree.
|
|
|
|
// ---> NewBB -----\
|
|
|
|
// / V
|
|
|
|
// TIBB -------\\------> DestBB
|
2002-09-27 00:18:51 +08:00
|
|
|
//
|
2017-08-18 00:45:35 +08:00
|
|
|
// First, inform the DT about the new path from TIBB to DestBB via NewBB,
|
|
|
|
// then delete the old edge from TIBB to DestBB. By doing this in that order
|
|
|
|
// DestBB stays reachable in the DT the whole time and its subtree doesn't
|
|
|
|
// get disconnected.
|
|
|
|
SmallVector<DominatorTree::UpdateType, 3> Updates;
|
|
|
|
Updates.push_back({DominatorTree::Insert, TIBB, NewBB});
|
|
|
|
Updates.push_back({DominatorTree::Insert, NewBB, DestBB});
|
|
|
|
if (llvm::find(successors(TIBB), DestBB) == succ_end(TIBB))
|
|
|
|
Updates.push_back({DominatorTree::Delete, TIBB, DestBB});
|
|
|
|
|
2019-02-22 23:01:41 +08:00
|
|
|
if (DT)
|
|
|
|
DT->applyUpdates(Updates);
|
|
|
|
if (PDT)
|
|
|
|
PDT->applyUpdates(Updates);
|
2002-09-24 23:51:56 +08:00
|
|
|
}
|
2002-10-31 10:44:36 +08:00
|
|
|
|
2005-08-13 09:38:43 +08:00
|
|
|
// Update LoopInfo if it is around.
|
2010-02-13 13:01:14 +08:00
|
|
|
if (LI) {
|
2009-09-08 23:45:00 +08: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 09:38:43 +08:00
|
|
|
if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
|
|
|
|
if (TIL == DestLoop) {
|
|
|
|
// Both in the same loop, the NewBB joins loop.
|
2015-01-18 09:25:51 +08:00
|
|
|
DestLoop->addBasicBlockToLoop(NewBB, *LI);
|
2009-12-18 09:24:09 +08:00
|
|
|
} else if (TIL->contains(DestLoop)) {
|
2006-10-03 15:02:02 +08:00
|
|
|
// Edge from an outer loop to an inner loop. Add to the outer loop.
|
2015-01-18 09:25:51 +08:00
|
|
|
TIL->addBasicBlockToLoop(NewBB, *LI);
|
2009-12-18 09:24:09 +08:00
|
|
|
} else if (DestLoop->contains(TIL)) {
|
2006-10-03 15:02:02 +08:00
|
|
|
// Edge from an inner loop to an outer loop. Add to the outer loop.
|
2015-01-18 09:25:51 +08:00
|
|
|
DestLoop->addBasicBlockToLoop(NewBB, *LI);
|
2005-08-13 09:38:43 +08: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())
|
2015-01-18 09:25:51 +08:00
|
|
|
P->addBasicBlockToLoop(NewBB, *LI);
|
2005-08-13 09:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-19 18:23:00 +08: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.
llvm-svn: 200393
2014-01-29 21:16:53 +08:00
|
|
|
// If TIBB is in a loop and DestBB is outside of that loop, we may need
|
|
|
|
// to update LoopSimplify form and LCSSA form.
|
2015-01-19 18:23:00 +08:00
|
|
|
if (!TIL->contains(DestBB)) {
|
2009-09-10 02:18:18 +08:00
|
|
|
assert(!TIL->contains(NewBB) &&
|
|
|
|
"Split point for loop exit is contained in loop!");
|
|
|
|
|
|
|
|
// Update LCSSA form in the newly created exit block.
|
2015-01-19 20:09:11 +08:00
|
|
|
if (Options.PreserveLCSSA) {
|
2012-04-30 18:25:51 +08:00
|
|
|
createPHIsForSplitLoopExit(TIBB, NewBB, DestBB);
|
2015-01-19 18:23:00 +08:00
|
|
|
}
|
2009-09-10 02:18:18 +08: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.
llvm-svn: 200393
2014-01-29 21:16:53 +08: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-22 01:06:51 +08: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.
llvm-svn: 200393
2014-01-29 21:16:53 +08: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-10 02:18:18 +08: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.
llvm-svn: 200393
2014-01-29 21:16:53 +08:00
|
|
|
LoopPreds.push_back(P);
|
|
|
|
}
|
|
|
|
if (!LoopPreds.empty()) {
|
2015-08-04 16:21:40 +08:00
|
|
|
assert(!DestBB->isEHPad() && "We don't split edges to EH pads!");
|
2015-01-18 17:21:15 +08:00
|
|
|
BasicBlock *NewExitBB = SplitBlockPredecessors(
|
2018-08-22 07:32:03 +08:00
|
|
|
DestBB, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA);
|
2015-01-19 20:09:11 +08:00
|
|
|
if (Options.PreserveLCSSA)
|
[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.
llvm-svn: 200393
2014-01-29 21:16:53 +08:00
|
|
|
createPHIsForSplitLoopExit(LoopPreds, NewExitBB, DestBB);
|
2009-09-08 23:45:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-08-13 09:38:43 +08:00
|
|
|
}
|
2009-09-08 23:45:00 +08:00
|
|
|
|
|
|
|
return NewBB;
|
2002-09-24 23:51:56 +08:00
|
|
|
}
|
2017-12-05 04:36:01 +08:00
|
|
|
|
|
|
|
// Return the unique indirectbr predecessor of a block. This may return null
|
|
|
|
// even if such a predecessor exists, if it's not useful for splitting.
|
|
|
|
// If a predecessor is found, OtherPreds will contain all other (non-indirectbr)
|
|
|
|
// predecessors of BB.
|
|
|
|
static BasicBlock *
|
|
|
|
findIBRPredecessor(BasicBlock *BB, SmallVectorImpl<BasicBlock *> &OtherPreds) {
|
|
|
|
// If the block doesn't have any PHIs, we don't care about it, since there's
|
|
|
|
// no point in splitting it.
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(BB->begin());
|
|
|
|
if (!PN)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Verify we have exactly one IBR predecessor.
|
|
|
|
// Conservatively bail out if one of the other predecessors is not a "regular"
|
|
|
|
// terminator (that is, not a switch or a br).
|
|
|
|
BasicBlock *IBB = nullptr;
|
|
|
|
for (unsigned Pred = 0, E = PN->getNumIncomingValues(); Pred != E; ++Pred) {
|
|
|
|
BasicBlock *PredBB = PN->getIncomingBlock(Pred);
|
2018-10-15 18:04:59 +08:00
|
|
|
Instruction *PredTerm = PredBB->getTerminator();
|
2017-12-05 04:36:01 +08:00
|
|
|
switch (PredTerm->getOpcode()) {
|
|
|
|
case Instruction::IndirectBr:
|
|
|
|
if (IBB)
|
|
|
|
return nullptr;
|
|
|
|
IBB = PredBB;
|
|
|
|
break;
|
|
|
|
case Instruction::Br:
|
|
|
|
case Instruction::Switch:
|
|
|
|
OtherPreds.push_back(PredBB);
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return IBB;
|
|
|
|
}
|
|
|
|
|
2017-12-13 03:07:43 +08:00
|
|
|
bool llvm::SplitIndirectBrCriticalEdges(Function &F,
|
|
|
|
BranchProbabilityInfo *BPI,
|
|
|
|
BlockFrequencyInfo *BFI) {
|
2017-12-05 04:36:01 +08:00
|
|
|
// Check whether the function has any indirectbrs, and collect which blocks
|
|
|
|
// they may jump to. Since most functions don't have indirect branches,
|
|
|
|
// this lowers the common case's overhead to O(Blocks) instead of O(Edges).
|
|
|
|
SmallSetVector<BasicBlock *, 16> Targets;
|
|
|
|
for (auto &BB : F) {
|
|
|
|
auto *IBI = dyn_cast<IndirectBrInst>(BB.getTerminator());
|
|
|
|
if (!IBI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (unsigned Succ = 0, E = IBI->getNumSuccessors(); Succ != E; ++Succ)
|
|
|
|
Targets.insert(IBI->getSuccessor(Succ));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Targets.empty())
|
|
|
|
return false;
|
|
|
|
|
2017-12-13 03:07:43 +08:00
|
|
|
bool ShouldUpdateAnalysis = BPI && BFI;
|
2017-12-05 04:36:01 +08:00
|
|
|
bool Changed = false;
|
|
|
|
for (BasicBlock *Target : Targets) {
|
|
|
|
SmallVector<BasicBlock *, 16> OtherPreds;
|
|
|
|
BasicBlock *IBRPred = findIBRPredecessor(Target, OtherPreds);
|
|
|
|
// If we did not found an indirectbr, or the indirectbr is the only
|
|
|
|
// incoming edge, this isn't the kind of edge we're looking for.
|
|
|
|
if (!IBRPred || OtherPreds.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Don't even think about ehpads/landingpads.
|
|
|
|
Instruction *FirstNonPHI = Target->getFirstNonPHI();
|
|
|
|
if (FirstNonPHI->isEHPad() || Target->isLandingPad())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BasicBlock *BodyBlock = Target->splitBasicBlock(FirstNonPHI, ".split");
|
2017-12-13 03:07:43 +08:00
|
|
|
if (ShouldUpdateAnalysis) {
|
|
|
|
// Copy the BFI/BPI from Target to BodyBlock.
|
|
|
|
for (unsigned I = 0, E = BodyBlock->getTerminator()->getNumSuccessors();
|
|
|
|
I < E; ++I)
|
|
|
|
BPI->setEdgeProbability(BodyBlock, I,
|
|
|
|
BPI->getEdgeProbability(Target, I));
|
|
|
|
BFI->setBlockFreq(BodyBlock, BFI->getBlockFreq(Target).getFrequency());
|
|
|
|
}
|
2017-12-05 04:36:01 +08:00
|
|
|
// It's possible Target was its own successor through an indirectbr.
|
|
|
|
// In this case, the indirectbr now comes from BodyBlock.
|
|
|
|
if (IBRPred == Target)
|
|
|
|
IBRPred = BodyBlock;
|
|
|
|
|
|
|
|
// At this point Target only has PHIs, and BodyBlock has the rest of the
|
|
|
|
// block's body. Create a copy of Target that will be used by the "direct"
|
|
|
|
// preds.
|
|
|
|
ValueToValueMapTy VMap;
|
|
|
|
BasicBlock *DirectSucc = CloneBasicBlock(Target, VMap, ".clone", &F);
|
|
|
|
|
2017-12-13 03:07:43 +08:00
|
|
|
BlockFrequency BlockFreqForDirectSucc;
|
2017-12-05 04:36:01 +08:00
|
|
|
for (BasicBlock *Pred : OtherPreds) {
|
|
|
|
// If the target is a loop to itself, then the terminator of the split
|
2017-12-13 03:07:43 +08:00
|
|
|
// block (BodyBlock) needs to be updated.
|
|
|
|
BasicBlock *Src = Pred != Target ? Pred : BodyBlock;
|
|
|
|
Src->getTerminator()->replaceUsesOfWith(Target, DirectSucc);
|
|
|
|
if (ShouldUpdateAnalysis)
|
|
|
|
BlockFreqForDirectSucc += BFI->getBlockFreq(Src) *
|
|
|
|
BPI->getEdgeProbability(Src, DirectSucc);
|
|
|
|
}
|
|
|
|
if (ShouldUpdateAnalysis) {
|
|
|
|
BFI->setBlockFreq(DirectSucc, BlockFreqForDirectSucc.getFrequency());
|
|
|
|
BlockFrequency NewBlockFreqForTarget =
|
|
|
|
BFI->getBlockFreq(Target) - BlockFreqForDirectSucc;
|
|
|
|
BFI->setBlockFreq(Target, NewBlockFreqForTarget.getFrequency());
|
|
|
|
BPI->eraseBlock(Target);
|
2017-12-05 04:36:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, now fix up the PHIs. We know the two blocks only have PHIs, and that
|
|
|
|
// they are clones, so the number of PHIs are the same.
|
|
|
|
// (a) Remove the edge coming from IBRPred from the "Direct" PHI
|
|
|
|
// (b) Leave that as the only edge in the "Indirect" PHI.
|
|
|
|
// (c) Merge the two in the body block.
|
|
|
|
BasicBlock::iterator Indirect = Target->begin(),
|
|
|
|
End = Target->getFirstNonPHI()->getIterator();
|
|
|
|
BasicBlock::iterator Direct = DirectSucc->begin();
|
|
|
|
BasicBlock::iterator MergeInsert = BodyBlock->getFirstInsertionPt();
|
|
|
|
|
|
|
|
assert(&*End == Target->getTerminator() &&
|
|
|
|
"Block was expected to only contain PHIs");
|
|
|
|
|
|
|
|
while (Indirect != End) {
|
|
|
|
PHINode *DirPHI = cast<PHINode>(Direct);
|
|
|
|
PHINode *IndPHI = cast<PHINode>(Indirect);
|
|
|
|
|
|
|
|
// Now, clean up - the direct block shouldn't get the indirect value,
|
|
|
|
// and vice versa.
|
|
|
|
DirPHI->removeIncomingValue(IBRPred);
|
|
|
|
Direct++;
|
|
|
|
|
|
|
|
// Advance the pointer here, to avoid invalidation issues when the old
|
|
|
|
// PHI is erased.
|
|
|
|
Indirect++;
|
|
|
|
|
|
|
|
PHINode *NewIndPHI = PHINode::Create(IndPHI->getType(), 1, "ind", IndPHI);
|
|
|
|
NewIndPHI->addIncoming(IndPHI->getIncomingValueForBlock(IBRPred),
|
|
|
|
IBRPred);
|
|
|
|
|
|
|
|
// Create a PHI in the body block, to merge the direct and indirect
|
|
|
|
// predecessors.
|
|
|
|
PHINode *MergePHI =
|
|
|
|
PHINode::Create(IndPHI->getType(), 2, "merge", &*MergeInsert);
|
|
|
|
MergePHI->addIncoming(NewIndPHI, Target);
|
|
|
|
MergePHI->addIncoming(DirPHI, DirectSucc);
|
|
|
|
|
|
|
|
IndPHI->replaceAllUsesWith(MergePHI);
|
|
|
|
IndPHI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|