2013-07-27 03:24:00 +02:00
|
|
|
//===-- CFG.cpp - BasicBlock analysis --------------------------------------==//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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
|
2013-07-27 03:24:00 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This family of functions performs analyses on basic blocks, and instructions
|
|
|
|
// contained within basic blocks.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/CFG.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2014-01-13 10:26:24 +01:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-07-27 03:24:00 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// FindFunctionBackedges - Analyze the specified function to find all of the
|
|
|
|
/// loop backedges in the function and return them. This is a relatively cheap
|
|
|
|
/// (compared to computing dominators and loop info) analysis.
|
|
|
|
///
|
|
|
|
/// The output is added to Result, as pairs of <from,to> edge info.
|
|
|
|
void llvm::FindFunctionBackedges(const Function &F,
|
|
|
|
SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
|
|
|
|
const BasicBlock *BB = &F.getEntryBlock();
|
2015-01-13 04:46:47 +01:00
|
|
|
if (succ_empty(BB))
|
2013-07-27 03:24:00 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
SmallPtrSet<const BasicBlock*, 8> Visited;
|
2020-03-10 19:33:02 +01:00
|
|
|
SmallVector<std::pair<const BasicBlock *, const_succ_iterator>, 8> VisitStack;
|
2013-07-27 03:24:00 +02:00
|
|
|
SmallPtrSet<const BasicBlock*, 8> InStack;
|
|
|
|
|
|
|
|
Visited.insert(BB);
|
|
|
|
VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
|
|
|
|
InStack.insert(BB);
|
|
|
|
do {
|
2020-03-10 19:33:02 +01:00
|
|
|
std::pair<const BasicBlock *, const_succ_iterator> &Top = VisitStack.back();
|
2013-07-27 03:24:00 +02:00
|
|
|
const BasicBlock *ParentBB = Top.first;
|
2020-03-10 19:33:02 +01:00
|
|
|
const_succ_iterator &I = Top.second;
|
2013-07-27 03:24:00 +02:00
|
|
|
|
|
|
|
bool FoundNew = false;
|
|
|
|
while (I != succ_end(ParentBB)) {
|
|
|
|
BB = *I++;
|
2014-11-19 08:49:26 +01:00
|
|
|
if (Visited.insert(BB).second) {
|
2013-07-27 03:24:00 +02:00
|
|
|
FoundNew = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Successor is in VisitStack, it's a back edge.
|
|
|
|
if (InStack.count(BB))
|
|
|
|
Result.push_back(std::make_pair(ParentBB, BB));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FoundNew) {
|
|
|
|
// Go down one level if there is a unvisited successor.
|
|
|
|
InStack.insert(BB);
|
|
|
|
VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
|
|
|
|
} else {
|
|
|
|
// Go up one level.
|
|
|
|
InStack.erase(VisitStack.pop_back_val().first);
|
|
|
|
}
|
|
|
|
} while (!VisitStack.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GetSuccessorNumber - Search for the specified successor of basic block BB
|
|
|
|
/// and return its position in the terminator instruction's list of
|
|
|
|
/// successors. It is an error to call this with a block that is not a
|
|
|
|
/// successor.
|
2015-11-21 00:02:06 +01:00
|
|
|
unsigned llvm::GetSuccessorNumber(const BasicBlock *BB,
|
|
|
|
const BasicBlock *Succ) {
|
2018-10-15 12:04:59 +02:00
|
|
|
const Instruction *Term = BB->getTerminator();
|
2013-07-27 03:24:00 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
unsigned e = Term->getNumSuccessors();
|
|
|
|
#endif
|
|
|
|
for (unsigned i = 0; ; ++i) {
|
|
|
|
assert(i != e && "Didn't find edge?");
|
|
|
|
if (Term->getSuccessor(i) == Succ)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2018-10-15 11:17:09 +02:00
|
|
|
bool llvm::isCriticalEdge(const Instruction *TI, unsigned SuccNum,
|
2013-07-27 03:24:00 +02:00
|
|
|
bool AllowIdenticalEdges) {
|
2019-07-30 19:04:58 +02:00
|
|
|
assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
|
2019-07-31 11:27:54 +02:00
|
|
|
return isCriticalEdge(TI, TI->getSuccessor(SuccNum), AllowIdenticalEdges);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool llvm::isCriticalEdge(const Instruction *TI, const BasicBlock *Dest,
|
|
|
|
bool AllowIdenticalEdges) {
|
|
|
|
assert(TI->isTerminator() && "Must be a terminator to have successors!");
|
2013-07-27 03:24:00 +02:00
|
|
|
if (TI->getNumSuccessors() == 1) return false;
|
|
|
|
|
2019-07-31 11:27:54 +02:00
|
|
|
assert(find(predecessors(Dest), TI->getParent()) != pred_end(Dest) &&
|
|
|
|
"No edge between TI's block and Dest.");
|
|
|
|
|
2013-07-27 03:24:00 +02:00
|
|
|
const_pred_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?");
|
|
|
|
const BasicBlock *FirstPred = *I;
|
|
|
|
++I; // Skip one edge due to the incoming arc from TI.
|
|
|
|
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.
|
2014-03-25 10:06:18 +01:00
|
|
|
for (; I != E; ++I)
|
|
|
|
if (*I != FirstPred)
|
2013-07-27 03:24:00 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoopInfo contains a mapping from basic block to the innermost loop. Find
|
|
|
|
// the outermost loop in the loop nest that contains BB.
|
2013-08-21 01:04:15 +02:00
|
|
|
static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
|
2013-07-27 03:24:00 +02:00
|
|
|
const Loop *L = LI->getLoopFor(BB);
|
|
|
|
if (L) {
|
|
|
|
while (const Loop *Parent = L->getParentLoop())
|
|
|
|
L = Parent;
|
|
|
|
}
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
2015-06-24 19:53:17 +02:00
|
|
|
bool llvm::isPotentiallyReachableFromMany(
|
|
|
|
SmallVectorImpl<BasicBlock *> &Worklist, BasicBlock *StopBB,
|
2019-04-02 03:05:48 +02:00
|
|
|
const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
|
|
|
|
const LoopInfo *LI) {
|
2013-07-27 03:24:00 +02:00
|
|
|
// When the stop block is unreachable, it's dominated from everywhere,
|
|
|
|
// regardless of whether there's a path between the two blocks.
|
|
|
|
if (DT && !DT->isReachableFromEntry(StopBB))
|
2014-04-15 06:59:12 +02:00
|
|
|
DT = nullptr;
|
2013-07-27 03:24:00 +02:00
|
|
|
|
2019-04-02 03:05:48 +02:00
|
|
|
// We can't skip directly from a block that dominates the stop block if the
|
|
|
|
// exclusion block is potentially in between.
|
|
|
|
if (ExclusionSet && !ExclusionSet->empty())
|
|
|
|
DT = nullptr;
|
|
|
|
|
|
|
|
// Normally any block in a loop is reachable from any other block in a loop,
|
|
|
|
// however excluded blocks might partition the body of a loop to make that
|
|
|
|
// untrue.
|
|
|
|
SmallPtrSet<const Loop *, 8> LoopsWithHoles;
|
|
|
|
if (LI && ExclusionSet) {
|
|
|
|
for (auto BB : *ExclusionSet) {
|
|
|
|
if (const Loop *L = getOutermostLoop(LI, BB))
|
|
|
|
LoopsWithHoles.insert(L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Loop *StopLoop = LI ? getOutermostLoop(LI, StopBB) : nullptr;
|
|
|
|
|
2013-07-27 03:24:00 +02:00
|
|
|
// Limit the number of blocks we visit. The goal is to avoid run-away compile
|
|
|
|
// times on large CFGs without hampering sensible code. Arbitrarily chosen.
|
|
|
|
unsigned Limit = 32;
|
2016-01-30 02:24:31 +01:00
|
|
|
SmallPtrSet<const BasicBlock*, 32> Visited;
|
2013-07-27 03:24:00 +02:00
|
|
|
do {
|
|
|
|
BasicBlock *BB = Worklist.pop_back_val();
|
2014-11-19 08:49:26 +01:00
|
|
|
if (!Visited.insert(BB).second)
|
2013-07-27 03:24:00 +02:00
|
|
|
continue;
|
|
|
|
if (BB == StopBB)
|
|
|
|
return true;
|
2019-04-02 03:05:48 +02:00
|
|
|
if (ExclusionSet && ExclusionSet->count(BB))
|
|
|
|
continue;
|
2013-07-27 03:24:00 +02:00
|
|
|
if (DT && DT->dominates(BB, StopBB))
|
|
|
|
return true;
|
2019-04-02 03:05:48 +02:00
|
|
|
|
|
|
|
const Loop *Outer = nullptr;
|
|
|
|
if (LI) {
|
|
|
|
Outer = getOutermostLoop(LI, BB);
|
|
|
|
// If we're in a loop with a hole, not all blocks in the loop are
|
|
|
|
// reachable from all other blocks. That implies we can't simply jump to
|
|
|
|
// the loop's exit blocks, as that exit might need to pass through an
|
|
|
|
// excluded block. Clear Outer so we process BB's successors.
|
|
|
|
if (LoopsWithHoles.count(Outer))
|
|
|
|
Outer = nullptr;
|
|
|
|
if (StopLoop && Outer == StopLoop)
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-27 03:24:00 +02:00
|
|
|
|
|
|
|
if (!--Limit) {
|
|
|
|
// We haven't been able to prove it one way or the other. Conservatively
|
|
|
|
// answer true -- that there is potentially a path.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-02 03:05:48 +02:00
|
|
|
if (Outer) {
|
2013-07-27 03:24:00 +02:00
|
|
|
// All blocks in a single loop are reachable from all other blocks. From
|
|
|
|
// any of these blocks, we can skip directly to the exits of the loop,
|
|
|
|
// ignoring any other blocks inside the loop body.
|
|
|
|
Outer->getExitBlocks(Worklist);
|
|
|
|
} else {
|
2014-02-10 15:17:42 +01:00
|
|
|
Worklist.append(succ_begin(BB), succ_end(BB));
|
2013-07-27 03:24:00 +02:00
|
|
|
}
|
|
|
|
} while (!Worklist.empty());
|
|
|
|
|
2013-08-13 02:03:47 +02:00
|
|
|
// We have exhausted all possible paths and are certain that 'To' can not be
|
|
|
|
// reached from 'From'.
|
2013-07-27 03:24:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-13 02:03:47 +02:00
|
|
|
|
|
|
|
bool llvm::isPotentiallyReachable(const BasicBlock *A, const BasicBlock *B,
|
2013-08-21 01:04:15 +02:00
|
|
|
const DominatorTree *DT, const LoopInfo *LI) {
|
2013-08-13 02:03:47 +02:00
|
|
|
assert(A->getParent() == B->getParent() &&
|
|
|
|
"This analysis is function-local!");
|
|
|
|
|
|
|
|
SmallVector<BasicBlock*, 32> Worklist;
|
|
|
|
Worklist.push_back(const_cast<BasicBlock*>(A));
|
|
|
|
|
2015-06-24 19:53:17 +02:00
|
|
|
return isPotentiallyReachableFromMany(Worklist, const_cast<BasicBlock *>(B),
|
2019-04-02 03:05:48 +02:00
|
|
|
nullptr, DT, LI);
|
2013-08-13 02:03:47 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 03:05:48 +02:00
|
|
|
bool llvm::isPotentiallyReachable(
|
|
|
|
const Instruction *A, const Instruction *B,
|
|
|
|
const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
|
|
|
|
const LoopInfo *LI) {
|
2013-08-13 02:03:47 +02:00
|
|
|
assert(A->getParent()->getParent() == B->getParent()->getParent() &&
|
|
|
|
"This analysis is function-local!");
|
|
|
|
|
|
|
|
SmallVector<BasicBlock*, 32> Worklist;
|
|
|
|
|
|
|
|
if (A->getParent() == B->getParent()) {
|
|
|
|
// The same block case is special because it's the only time we're looking
|
|
|
|
// within a single block to see which instruction comes first. Once we
|
|
|
|
// start looking at multiple blocks, the first instruction of the block is
|
|
|
|
// reachable, so we only need to determine reachability between whole
|
|
|
|
// blocks.
|
|
|
|
BasicBlock *BB = const_cast<BasicBlock *>(A->getParent());
|
|
|
|
|
|
|
|
// If the block is in a loop then we can reach any instruction in the block
|
|
|
|
// from any other instruction in the block by going around a backedge.
|
2014-04-15 06:59:12 +02:00
|
|
|
if (LI && LI->getLoopFor(BB) != nullptr)
|
2013-08-13 02:03:47 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Linear scan, start at 'A', see whether we hit 'B' or the end first.
|
Analysis: Remove implicit ilist iterator conversions
Remove implicit ilist iterator conversions from LLVMAnalysis.
I came across something really scary in `llvm::isKnownNotFullPoison()`
which relied on `Instruction::getNextNode()` being completely broken
(not surprising, but scary nevertheless). This function is documented
(and coded to) return `nullptr` when it gets to the sentinel, but with
an `ilist_half_node` as a sentinel, the sentinel check looks into some
other memory and we don't recognize we've hit the end.
Rooting out these scary cases is the reason I'm removing the implicit
conversions before doing anything else with `ilist`; I'm not at all
surprised that clients rely on badness.
I found another scary case -- this time, not relying on badness, just
bad (but I guess getting lucky so far) -- in
`ObjectSizeOffsetEvaluator::compute_()`. Here, we save out the
insertion point, do some things, and then restore it. Previously, we
let the iterator auto-convert to `Instruction*`, and then set it back
using the `Instruction*` version:
Instruction *PrevInsertPoint = Builder.GetInsertPoint();
/* Logic that may change insert point */
if (PrevInsertPoint)
Builder.SetInsertPoint(PrevInsertPoint);
The check for `PrevInsertPoint` doesn't protect correctly against bad
accesses. If the insertion point has been set to the end of a basic
block (i.e., `SetInsertPoint(SomeBB)`), then `GetInsertPoint()` returns
an iterator pointing at the list sentinel. The version of
`SetInsertPoint()` that's getting called will then call
`PrevInsertPoint->getParent()`, which explodes horribly. The only
reason this hasn't blown up is that it's fairly unlikely the builder is
adding to the end of the block; usually, we're adding instructions
somewhere before the terminator.
llvm-svn: 249925
2015-10-10 02:53:03 +02:00
|
|
|
for (BasicBlock::const_iterator I = A->getIterator(), E = BB->end(); I != E;
|
|
|
|
++I) {
|
2013-08-13 02:03:47 +02:00
|
|
|
if (&*I == B)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't be in a loop if it's the entry block -- the entry block may not
|
|
|
|
// have predecessors.
|
|
|
|
if (BB == &BB->getParent()->getEntryBlock())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, continue doing the normal per-BB CFG walk.
|
2014-02-10 15:17:42 +01:00
|
|
|
Worklist.append(succ_begin(BB), succ_end(BB));
|
2013-08-13 02:03:47 +02:00
|
|
|
|
|
|
|
if (Worklist.empty()) {
|
|
|
|
// We've proven that there's no path!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Worklist.push_back(const_cast<BasicBlock*>(A->getParent()));
|
|
|
|
}
|
|
|
|
|
2019-04-01 22:03:16 +02:00
|
|
|
if (DT) {
|
2019-04-05 01:09:40 +02:00
|
|
|
if (DT->isReachableFromEntry(A->getParent()) &&
|
|
|
|
!DT->isReachableFromEntry(B->getParent()))
|
2019-04-01 22:03:16 +02:00
|
|
|
return false;
|
2019-04-02 03:05:48 +02:00
|
|
|
if (!ExclusionSet || ExclusionSet->empty()) {
|
|
|
|
if (A->getParent() == &A->getParent()->getParent()->getEntryBlock() &&
|
|
|
|
DT->isReachableFromEntry(B->getParent()))
|
|
|
|
return true;
|
|
|
|
if (B->getParent() == &A->getParent()->getParent()->getEntryBlock() &&
|
|
|
|
DT->isReachableFromEntry(A->getParent()))
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-01 22:03:16 +02:00
|
|
|
}
|
2013-08-13 02:03:47 +02:00
|
|
|
|
2015-06-24 19:53:17 +02:00
|
|
|
return isPotentiallyReachableFromMany(
|
2019-04-02 03:05:48 +02:00
|
|
|
Worklist, const_cast<BasicBlock *>(B->getParent()), ExclusionSet, DT, LI);
|
2013-08-13 02:03:47 +02:00
|
|
|
}
|