2018-03-20 18:09:21 +01:00
|
|
|
//===- MustExecute.cpp - Printer for isGuaranteedToExecute ----------------===//
|
|
|
|
//
|
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
|
2018-03-20 18:09:21 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-03-29 21:22:12 +02:00
|
|
|
#include "llvm/Analysis/MustExecute.h"
|
2019-08-23 17:17:27 +02:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/Analysis/CFG.h"
|
2018-03-20 23:45:23 +01:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2018-03-20 18:09:21 +01:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
2019-10-13 03:46:49 +02:00
|
|
|
#include "llvm/Analysis/PostDominators.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-13 22:15:01 +01:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2018-03-20 19:43:44 +01:00
|
|
|
#include "llvm/IR/AssemblyAnnotationWriter.h"
|
2018-03-20 18:09:21 +01:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/InstIterator.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.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-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2018-03-20 18:09:21 +01:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2018-03-20 19:43:44 +01:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2018-03-20 18:09:21 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2019-08-23 17:17:27 +02:00
|
|
|
|
2018-03-20 18:09:21 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2019-08-23 17:17:27 +02:00
|
|
|
#define DEBUG_TYPE "must-execute"
|
|
|
|
|
2018-10-16 10:07:14 +02:00
|
|
|
const DenseMap<BasicBlock *, ColorVector> &
|
|
|
|
LoopSafetyInfo::getBlockColors() const {
|
|
|
|
return BlockColors;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoopSafetyInfo::copyColors(BasicBlock *New, BasicBlock *Old) {
|
|
|
|
ColorVector &ColorsForNewBlock = BlockColors[New];
|
|
|
|
ColorVector &ColorsForOldBlock = BlockColors[Old];
|
|
|
|
ColorsForNewBlock = ColorsForOldBlock;
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +02:00
|
|
|
bool SimpleLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
|
2018-10-16 09:50:14 +02:00
|
|
|
(void)BB;
|
|
|
|
return anyBlockMayThrow();
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +02:00
|
|
|
bool SimpleLoopSafetyInfo::anyBlockMayThrow() const {
|
2018-08-15 07:55:43 +02:00
|
|
|
return MayThrow;
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +02:00
|
|
|
void SimpleLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
|
2018-07-19 21:11:29 +02:00
|
|
|
assert(CurLoop != nullptr && "CurLoop can't be null");
|
2018-03-20 23:45:23 +01:00
|
|
|
BasicBlock *Header = CurLoop->getHeader();
|
|
|
|
// Iterate over header and compute safety info.
|
2018-08-15 07:55:43 +02:00
|
|
|
HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header);
|
|
|
|
MayThrow = HeaderMayThrow;
|
2018-03-20 23:45:23 +01:00
|
|
|
// Iterate over loop instructions and compute safety info.
|
|
|
|
// Skip header as it has been computed and stored in HeaderMayThrow.
|
|
|
|
// The first block in loopinfo.Blocks is guaranteed to be the header.
|
|
|
|
assert(Header == *CurLoop->getBlocks().begin() &&
|
|
|
|
"First block must be header");
|
|
|
|
for (Loop::block_iterator BB = std::next(CurLoop->block_begin()),
|
|
|
|
BBE = CurLoop->block_end();
|
2018-08-15 07:55:43 +02:00
|
|
|
(BB != BBE) && !MayThrow; ++BB)
|
|
|
|
MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB);
|
2018-03-20 23:45:23 +01:00
|
|
|
|
2018-10-16 10:07:14 +02:00
|
|
|
computeBlockColors(CurLoop);
|
|
|
|
}
|
|
|
|
|
2018-10-16 11:58:09 +02:00
|
|
|
bool ICFLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
|
|
|
|
return ICF.hasICF(BB);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ICFLoopSafetyInfo::anyBlockMayThrow() const {
|
|
|
|
return MayThrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICFLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
|
|
|
|
assert(CurLoop != nullptr && "CurLoop can't be null");
|
|
|
|
ICF.clear();
|
2018-11-12 10:29:58 +01:00
|
|
|
MW.clear();
|
2018-10-16 11:58:09 +02:00
|
|
|
MayThrow = false;
|
|
|
|
// Figure out the fact that at least one block may throw.
|
|
|
|
for (auto &BB : CurLoop->blocks())
|
|
|
|
if (ICF.hasICF(&*BB)) {
|
|
|
|
MayThrow = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
computeBlockColors(CurLoop);
|
|
|
|
}
|
|
|
|
|
2019-01-09 08:28:13 +01:00
|
|
|
void ICFLoopSafetyInfo::insertInstructionTo(const Instruction *Inst,
|
|
|
|
const BasicBlock *BB) {
|
|
|
|
ICF.insertInstructionTo(Inst, BB);
|
|
|
|
MW.insertInstructionTo(Inst, BB);
|
2018-10-16 11:58:09 +02:00
|
|
|
}
|
|
|
|
|
2018-11-01 11:16:06 +01:00
|
|
|
void ICFLoopSafetyInfo::removeInstruction(const Instruction *Inst) {
|
2019-01-09 08:28:13 +01:00
|
|
|
ICF.removeInstruction(Inst);
|
|
|
|
MW.removeInstruction(Inst);
|
2018-11-01 11:16:06 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 10:07:14 +02:00
|
|
|
void LoopSafetyInfo::computeBlockColors(const Loop *CurLoop) {
|
2018-03-20 23:45:23 +01:00
|
|
|
// Compute funclet colors if we might sink/hoist in a function with a funclet
|
|
|
|
// personality routine.
|
|
|
|
Function *Fn = CurLoop->getHeader()->getParent();
|
|
|
|
if (Fn->hasPersonalityFn())
|
|
|
|
if (Constant *PersonalityFn = Fn->getPersonalityFn())
|
[WebAssembly] Add Wasm personality and isScopedEHPersonality()
Summary:
- Add wasm personality function
- Re-categorize the existing `isFuncletEHPersonality()` function into
two different functions: `isFuncletEHPersonality()` and
`isScopedEHPersonality(). This becomes necessary as wasm EH uses scoped
EH instructions (catchswitch, catchpad/ret, and cleanuppad/ret) but not
outlined funclets.
- Changed some callsites of `isFuncletEHPersonality()` to
`isScopedEHPersonality()` if they are related to scoped EH IR-level
stuff.
Reviewers: majnemer, dschuff, rnk
Subscribers: jfb, sbc100, jgravelle-google, eraman, JDevlieghere, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D45559
llvm-svn: 332667
2018-05-17 22:52:03 +02:00
|
|
|
if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
|
2018-08-15 07:55:43 +02:00
|
|
|
BlockColors = colorEHFunclets(*Fn);
|
2018-03-20 23:45:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if we can prove that the given ExitBlock is not reached on the
|
|
|
|
/// first iteration of the given loop. That is, the backedge of the loop must
|
|
|
|
/// be executed before the ExitBlock is executed in any dynamic execution trace.
|
2018-08-16 08:28:04 +02:00
|
|
|
static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
|
2018-03-20 23:45:23 +01:00
|
|
|
const DominatorTree *DT,
|
|
|
|
const Loop *CurLoop) {
|
|
|
|
auto *CondExitBlock = ExitBlock->getSinglePredecessor();
|
|
|
|
if (!CondExitBlock)
|
|
|
|
// expect unique exits
|
|
|
|
return false;
|
|
|
|
assert(CurLoop->contains(CondExitBlock) && "meaning of exit block");
|
|
|
|
auto *BI = dyn_cast<BranchInst>(CondExitBlock->getTerminator());
|
|
|
|
if (!BI || !BI->isConditional())
|
|
|
|
return false;
|
2018-05-18 06:56:28 +02:00
|
|
|
// If condition is constant and false leads to ExitBlock then we always
|
|
|
|
// execute the true branch.
|
|
|
|
if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition()))
|
|
|
|
return BI->getSuccessor(Cond->getZExtValue() ? 1 : 0) == ExitBlock;
|
2018-03-20 23:45:23 +01:00
|
|
|
auto *Cond = dyn_cast<CmpInst>(BI->getCondition());
|
|
|
|
if (!Cond)
|
|
|
|
return false;
|
|
|
|
// todo: this would be a lot more powerful if we used scev, but all the
|
|
|
|
// plumbing is currently missing to pass a pointer in from the pass
|
|
|
|
// Check for cmp (phi [x, preheader] ...), y where (pred x, y is known
|
|
|
|
auto *LHS = dyn_cast<PHINode>(Cond->getOperand(0));
|
|
|
|
auto *RHS = Cond->getOperand(1);
|
|
|
|
if (!LHS || LHS->getParent() != CurLoop->getHeader())
|
|
|
|
return false;
|
|
|
|
auto DL = ExitBlock->getModule()->getDataLayout();
|
|
|
|
auto *IVStart = LHS->getIncomingValueForBlock(CurLoop->getLoopPreheader());
|
|
|
|
auto *SimpleValOrNull = SimplifyCmpInst(Cond->getPredicate(),
|
|
|
|
IVStart, RHS,
|
|
|
|
{DL, /*TLI*/ nullptr,
|
|
|
|
DT, /*AC*/ nullptr, BI});
|
|
|
|
auto *SimpleCst = dyn_cast_or_null<Constant>(SimpleValOrNull);
|
|
|
|
if (!SimpleCst)
|
|
|
|
return false;
|
|
|
|
if (ExitBlock == BI->getSuccessor(0))
|
|
|
|
return SimpleCst->isZeroValue();
|
|
|
|
assert(ExitBlock == BI->getSuccessor(1) && "implied by above");
|
|
|
|
return SimpleCst->isAllOnesValue();
|
|
|
|
}
|
|
|
|
|
2018-11-06 10:07:03 +01:00
|
|
|
/// Collect all blocks from \p CurLoop which lie on all possible paths from
|
|
|
|
/// the header of \p CurLoop (inclusive) to BB (exclusive) into the set
|
|
|
|
/// \p Predecessors. If \p BB is the header, \p Predecessors will be empty.
|
|
|
|
static void collectTransitivePredecessors(
|
2018-08-21 09:15:06 +02:00
|
|
|
const Loop *CurLoop, const BasicBlock *BB,
|
2018-11-06 10:07:03 +01:00
|
|
|
SmallPtrSetImpl<const BasicBlock *> &Predecessors) {
|
2018-08-21 09:15:06 +02:00
|
|
|
assert(Predecessors.empty() && "Garbage in predecessors set?");
|
2018-08-17 08:19:17 +02:00
|
|
|
assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
|
|
|
|
if (BB == CurLoop->getHeader())
|
2018-08-21 09:15:06 +02:00
|
|
|
return;
|
2018-08-17 08:19:17 +02:00
|
|
|
SmallVector<const BasicBlock *, 4> WorkList;
|
|
|
|
for (auto *Pred : predecessors(BB)) {
|
|
|
|
Predecessors.insert(Pred);
|
|
|
|
WorkList.push_back(Pred);
|
|
|
|
}
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
auto *Pred = WorkList.pop_back_val();
|
|
|
|
assert(CurLoop->contains(Pred) && "Should only reach loop blocks!");
|
|
|
|
// We are not interested in backedges and we don't want to leave loop.
|
|
|
|
if (Pred == CurLoop->getHeader())
|
|
|
|
continue;
|
|
|
|
// TODO: If BB lies in an inner loop of CurLoop, this will traverse over all
|
|
|
|
// blocks of this inner loop, even those that are always executed AFTER the
|
|
|
|
// BB. It may make our analysis more conservative than it could be, see test
|
|
|
|
// @nested and @nested_no_throw in test/Analysis/MustExecute/loop-header.ll.
|
|
|
|
// We can ignore backedge of all loops containing BB to get a sligtly more
|
|
|
|
// optimistic result.
|
|
|
|
for (auto *PredPred : predecessors(Pred))
|
|
|
|
if (Predecessors.insert(PredPred).second)
|
|
|
|
WorkList.push_back(PredPred);
|
|
|
|
}
|
2018-08-21 09:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LoopSafetyInfo::allLoopPathsLeadToBlock(const Loop *CurLoop,
|
|
|
|
const BasicBlock *BB,
|
|
|
|
const DominatorTree *DT) const {
|
|
|
|
assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
|
|
|
|
|
|
|
|
// Fast path: header is always reached once the loop is entered.
|
|
|
|
if (BB == CurLoop->getHeader())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Collect all transitive predecessors of BB in the same loop. This set will
|
|
|
|
// be a subset of the blocks within the loop.
|
|
|
|
SmallPtrSet<const BasicBlock *, 4> Predecessors;
|
|
|
|
collectTransitivePredecessors(CurLoop, BB, Predecessors);
|
2018-08-17 08:19:17 +02:00
|
|
|
|
[MustExecute] Improve MustExecute to correctly handle loop nest
Summary:
for.outer:
br for.inner
for.inner:
LI <loop invariant load instruction>
for.inner.latch:
br for.inner, for.outer.latch
for.outer.latch:
br for.outer, for.outer.exit
LI is a loop invariant load instruction that post dominate for.outer, so LI should be able to move out of the loop nest. However, there is a bug in allLoopPathsLeadToBlock().
Current algorithm of allLoopPathsLeadToBlock()
1. get all the transitive predecessors of the basic block LI belongs to (for.inner) ==> for.outer, for.inner.latch
2. if any successors of any of the predecessors are not for.inner or for.inner's predecessors, then return false
3. return true
Although for.inner.latch is for.inner's predecessor, but for.inner dominates for.inner.latch, which means if for.inner.latch is ever executed, for.inner should be as well. It should not return false for cases like this.
Author: Whitney (committed by xingxue)
Reviewers: kbarton, jdoerfert, Meinersbur, hfinkel, fhahn
Reviewed By: jdoerfert
Subscribers: hiraditya, jsji, llvm-commits, etiotto, bmahjour
Tags: #LLVM
Differential Revision: https://reviews.llvm.org/D62418
llvm-svn: 361762
2019-05-27 15:57:28 +02:00
|
|
|
// Make sure that all successors of, all predecessors of BB which are not
|
|
|
|
// dominated by BB, are either:
|
2018-08-17 08:19:17 +02:00
|
|
|
// 1) BB,
|
|
|
|
// 2) Also predecessors of BB,
|
|
|
|
// 3) Exit blocks which are not taken on 1st iteration.
|
|
|
|
// Memoize blocks we've already checked.
|
|
|
|
SmallPtrSet<const BasicBlock *, 4> CheckedSuccessors;
|
2018-10-16 09:50:14 +02:00
|
|
|
for (auto *Pred : Predecessors) {
|
|
|
|
// Predecessor block may throw, so it has a side exit.
|
|
|
|
if (blockMayThrow(Pred))
|
|
|
|
return false;
|
[MustExecute] Improve MustExecute to correctly handle loop nest
Summary:
for.outer:
br for.inner
for.inner:
LI <loop invariant load instruction>
for.inner.latch:
br for.inner, for.outer.latch
for.outer.latch:
br for.outer, for.outer.exit
LI is a loop invariant load instruction that post dominate for.outer, so LI should be able to move out of the loop nest. However, there is a bug in allLoopPathsLeadToBlock().
Current algorithm of allLoopPathsLeadToBlock()
1. get all the transitive predecessors of the basic block LI belongs to (for.inner) ==> for.outer, for.inner.latch
2. if any successors of any of the predecessors are not for.inner or for.inner's predecessors, then return false
3. return true
Although for.inner.latch is for.inner's predecessor, but for.inner dominates for.inner.latch, which means if for.inner.latch is ever executed, for.inner should be as well. It should not return false for cases like this.
Author: Whitney (committed by xingxue)
Reviewers: kbarton, jdoerfert, Meinersbur, hfinkel, fhahn
Reviewed By: jdoerfert
Subscribers: hiraditya, jsji, llvm-commits, etiotto, bmahjour
Tags: #LLVM
Differential Revision: https://reviews.llvm.org/D62418
llvm-svn: 361762
2019-05-27 15:57:28 +02:00
|
|
|
|
|
|
|
// BB dominates Pred, so if Pred runs, BB must run.
|
|
|
|
// This is true when Pred is a loop latch.
|
|
|
|
if (DT->dominates(BB, Pred))
|
|
|
|
continue;
|
|
|
|
|
2018-08-17 08:19:17 +02:00
|
|
|
for (auto *Succ : successors(Pred))
|
|
|
|
if (CheckedSuccessors.insert(Succ).second &&
|
|
|
|
Succ != BB && !Predecessors.count(Succ))
|
|
|
|
// By discharging conditions that are not executed on the 1st iteration,
|
|
|
|
// we guarantee that *at least* on the first iteration all paths from
|
|
|
|
// header that *may* execute will lead us to the block of interest. So
|
|
|
|
// that if we had virtually peeled one iteration away, in this peeled
|
|
|
|
// iteration the set of predecessors would contain only paths from
|
|
|
|
// header to BB without any exiting edges that may execute.
|
|
|
|
//
|
|
|
|
// TODO: We only do it for exiting edges currently. We could use the
|
|
|
|
// same function to skip some of the edges within the loop if we know
|
|
|
|
// that they will not be taken on the 1st iteration.
|
|
|
|
//
|
|
|
|
// TODO: If we somehow know the number of iterations in loop, the same
|
|
|
|
// check may be done for any arbitrary N-th iteration as long as N is
|
|
|
|
// not greater than minimum number of iterations in this loop.
|
|
|
|
if (CurLoop->contains(Succ) ||
|
|
|
|
!CanProveNotTakenFirstIteration(Succ, DT, CurLoop))
|
|
|
|
return false;
|
2018-10-16 09:50:14 +02:00
|
|
|
}
|
2018-08-17 08:19:17 +02:00
|
|
|
|
|
|
|
// All predecessors can only lead us to BB.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-20 23:45:23 +01:00
|
|
|
/// Returns true if the instruction in a loop is guaranteed to execute at least
|
|
|
|
/// once.
|
2018-10-16 10:31:05 +02:00
|
|
|
bool SimpleLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
|
|
|
|
const DominatorTree *DT,
|
|
|
|
const Loop *CurLoop) const {
|
2018-03-20 23:45:23 +01:00
|
|
|
// If the instruction is in the header block for the loop (which is very
|
|
|
|
// common), it is always guaranteed to dominate the exit blocks. Since this
|
|
|
|
// is a common case, and can save some work, check it now.
|
|
|
|
if (Inst.getParent() == CurLoop->getHeader())
|
|
|
|
// If there's a throw in the header block, we can't guarantee we'll reach
|
2018-04-27 22:44:01 +02:00
|
|
|
// Inst unless we can prove that Inst comes before the potential implicit
|
|
|
|
// exit. At the moment, we use a (cheap) hack for the common case where
|
|
|
|
// the instruction of interest is the first one in the block.
|
2018-10-16 11:11:25 +02:00
|
|
|
return !HeaderMayThrow ||
|
2018-10-16 08:34:53 +02:00
|
|
|
Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
|
2018-03-20 23:45:23 +01:00
|
|
|
|
2018-08-17 08:19:17 +02:00
|
|
|
// If there is a path from header to exit or latch that doesn't lead to our
|
|
|
|
// instruction's block, return false.
|
2018-10-16 11:11:25 +02:00
|
|
|
return allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
|
2018-03-20 23:45:23 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 11:58:09 +02:00
|
|
|
bool ICFLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
|
|
|
|
const DominatorTree *DT,
|
|
|
|
const Loop *CurLoop) const {
|
|
|
|
return !ICF.isDominatedByICFIFromSameBlock(&Inst) &&
|
|
|
|
allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
|
|
|
|
}
|
2018-03-20 23:45:23 +01:00
|
|
|
|
2018-11-12 10:29:58 +01:00
|
|
|
bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const BasicBlock *BB,
|
|
|
|
const Loop *CurLoop) const {
|
|
|
|
assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
|
|
|
|
|
|
|
|
// Fast path: there are no instructions before header.
|
|
|
|
if (BB == CurLoop->getHeader())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Collect all transitive predecessors of BB in the same loop. This set will
|
|
|
|
// be a subset of the blocks within the loop.
|
|
|
|
SmallPtrSet<const BasicBlock *, 4> Predecessors;
|
|
|
|
collectTransitivePredecessors(CurLoop, BB, Predecessors);
|
|
|
|
// Find if there any instruction in either predecessor that could write
|
|
|
|
// to memory.
|
|
|
|
for (auto *Pred : Predecessors)
|
|
|
|
if (MW.mayWriteToMemory(Pred))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ICFLoopSafetyInfo::doesNotWriteMemoryBefore(const Instruction &I,
|
|
|
|
const Loop *CurLoop) const {
|
|
|
|
auto *BB = I.getParent();
|
|
|
|
assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
|
|
|
|
return !MW.isDominatedByMemoryWriteFromSameBlock(&I) &&
|
|
|
|
doesNotWriteMemoryBefore(BB, CurLoop);
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:09:21 +01:00
|
|
|
namespace {
|
|
|
|
struct MustExecutePrinter : public FunctionPass {
|
|
|
|
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
MustExecutePrinter() : FunctionPass(ID) {
|
|
|
|
initializeMustExecutePrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
|
|
|
}
|
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
};
|
2019-08-23 17:17:27 +02:00
|
|
|
struct MustBeExecutedContextPrinter : public ModulePass {
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
MustBeExecutedContextPrinter() : ModulePass(ID) {
|
|
|
|
initializeMustBeExecutedContextPrinterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
bool runOnModule(Module &M) override;
|
|
|
|
};
|
2018-03-20 18:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char MustExecutePrinter::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(MustExecutePrinter, "print-mustexecute",
|
|
|
|
"Instructions which execute on loop entry", false, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(MustExecutePrinter, "print-mustexecute",
|
|
|
|
"Instructions which execute on loop entry", false, true)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createMustExecutePrinter() {
|
|
|
|
return new MustExecutePrinter();
|
|
|
|
}
|
|
|
|
|
2019-08-23 17:17:27 +02:00
|
|
|
char MustBeExecutedContextPrinter::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(
|
|
|
|
MustBeExecutedContextPrinter, "print-must-be-executed-contexts",
|
|
|
|
"print the must-be-executed-contexed for all instructions", false, true)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(MustBeExecutedContextPrinter,
|
|
|
|
"print-must-be-executed-contexts",
|
|
|
|
"print the must-be-executed-contexed for all instructions",
|
|
|
|
false, true)
|
|
|
|
|
|
|
|
ModulePass *llvm::createMustBeExecutedContextPrinter() {
|
|
|
|
return new MustBeExecutedContextPrinter();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MustBeExecutedContextPrinter::runOnModule(Module &M) {
|
2019-10-13 03:46:49 +02:00
|
|
|
// We provide non-PM analysis here because the old PM doesn't like to query
|
2019-10-31 18:06:21 +01:00
|
|
|
// function passes from a module pass.
|
2020-04-28 20:30:22 +02:00
|
|
|
SmallVector<std::unique_ptr<PostDominatorTree>, 8> PDTs;
|
|
|
|
SmallVector<std::unique_ptr<DominatorTree>, 8> DTs;
|
|
|
|
SmallVector<std::unique_ptr<LoopInfo>, 8> LIs;
|
2019-10-31 18:06:21 +01:00
|
|
|
|
|
|
|
GetterTy<LoopInfo> LIGetter = [&](const Function &F) {
|
2020-04-28 20:30:22 +02:00
|
|
|
DTs.push_back(std::make_unique<DominatorTree>(const_cast<Function &>(F)));
|
|
|
|
LIs.push_back(std::make_unique<LoopInfo>(*DTs.back()));
|
|
|
|
return LIs.back().get();
|
2019-10-13 03:46:49 +02:00
|
|
|
};
|
2020-02-20 05:00:43 +01:00
|
|
|
GetterTy<DominatorTree> DTGetter = [&](const Function &F) {
|
2020-04-28 20:30:22 +02:00
|
|
|
DTs.push_back(std::make_unique<DominatorTree>(const_cast<Function&>(F)));
|
|
|
|
return DTs.back().get();
|
2020-02-20 05:00:43 +01:00
|
|
|
};
|
2019-10-31 18:06:21 +01:00
|
|
|
GetterTy<PostDominatorTree> PDTGetter = [&](const Function &F) {
|
2020-04-28 20:30:22 +02:00
|
|
|
PDTs.push_back(
|
|
|
|
std::make_unique<PostDominatorTree>(const_cast<Function &>(F)));
|
|
|
|
return PDTs.back().get();
|
2019-10-13 03:46:49 +02:00
|
|
|
};
|
2020-02-20 05:00:43 +01:00
|
|
|
MustBeExecutedContextExplorer Explorer(
|
|
|
|
/* ExploreInterBlock */ true,
|
|
|
|
/* ExploreCFGForward */ true,
|
|
|
|
/* ExploreCFGBackward */ true, LIGetter, DTGetter, PDTGetter);
|
|
|
|
|
2019-08-23 17:17:27 +02:00
|
|
|
for (Function &F : M) {
|
|
|
|
for (Instruction &I : instructions(F)) {
|
|
|
|
dbgs() << "-- Explore context of: " << I << "\n";
|
|
|
|
for (const Instruction *CI : Explorer.range(&I))
|
|
|
|
dbgs() << " [F: " << CI->getFunction()->getName() << "] " << *CI
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-04 13:45:11 +02:00
|
|
|
static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) {
|
2018-03-21 00:00:54 +01:00
|
|
|
// TODO: merge these two routines. For the moment, we display the best
|
|
|
|
// result obtained by *either* implementation. This is a bit unfair since no
|
|
|
|
// caller actually gets the full power at the moment.
|
2018-10-16 10:31:05 +02:00
|
|
|
SimpleLoopSafetyInfo LSI;
|
2018-08-15 07:55:43 +02:00
|
|
|
LSI.computeLoopSafetyInfo(L);
|
2018-10-16 08:34:53 +02:00
|
|
|
return LSI.isGuaranteedToExecute(I, DT, L) ||
|
2018-03-21 00:00:54 +01:00
|
|
|
isGuaranteedToExecuteForEveryIteration(&I, L);
|
2018-03-20 18:09:21 +01:00
|
|
|
}
|
|
|
|
|
2018-04-04 13:45:11 +02:00
|
|
|
namespace {
|
2018-05-01 17:54:18 +02:00
|
|
|
/// An assembly annotator class to print must execute information in
|
2018-03-20 19:43:44 +01:00
|
|
|
/// comments.
|
|
|
|
class MustExecuteAnnotatedWriter : public AssemblyAnnotationWriter {
|
|
|
|
DenseMap<const Value*, SmallVector<Loop*, 4> > MustExec;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MustExecuteAnnotatedWriter(const Function &F,
|
|
|
|
DominatorTree &DT, LoopInfo &LI) {
|
|
|
|
for (auto &I: instructions(F)) {
|
|
|
|
Loop *L = LI.getLoopFor(I.getParent());
|
|
|
|
while (L) {
|
|
|
|
if (isMustExecuteIn(I, L, &DT)) {
|
|
|
|
MustExec[&I].push_back(L);
|
|
|
|
}
|
|
|
|
L = L->getParentLoop();
|
|
|
|
};
|
|
|
|
}
|
2018-03-20 18:09:21 +01:00
|
|
|
}
|
2018-03-20 19:43:44 +01:00
|
|
|
MustExecuteAnnotatedWriter(const Module &M,
|
|
|
|
DominatorTree &DT, LoopInfo &LI) {
|
|
|
|
for (auto &F : M)
|
|
|
|
for (auto &I: instructions(F)) {
|
|
|
|
Loop *L = LI.getLoopFor(I.getParent());
|
|
|
|
while (L) {
|
|
|
|
if (isMustExecuteIn(I, L, &DT)) {
|
|
|
|
MustExec[&I].push_back(L);
|
|
|
|
}
|
|
|
|
L = L->getParentLoop();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 21:41:25 +02:00
|
|
|
void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
|
2018-03-20 19:43:44 +01:00
|
|
|
if (!MustExec.count(&V))
|
|
|
|
return;
|
2018-03-20 18:09:21 +01:00
|
|
|
|
2018-03-20 19:43:44 +01:00
|
|
|
const auto &Loops = MustExec.lookup(&V);
|
|
|
|
const auto NumLoops = Loops.size();
|
2018-03-20 18:09:21 +01:00
|
|
|
if (NumLoops > 1)
|
2018-03-20 19:43:44 +01:00
|
|
|
OS << " ; (mustexec in " << NumLoops << " loops: ";
|
2018-03-20 18:09:21 +01:00
|
|
|
else
|
2018-03-20 19:43:44 +01:00
|
|
|
OS << " ; (mustexec in: ";
|
2018-07-30 21:41:25 +02:00
|
|
|
|
2018-03-20 18:09:21 +01:00
|
|
|
bool first = true;
|
2018-03-20 19:43:44 +01:00
|
|
|
for (const Loop *L : Loops) {
|
2018-03-20 18:09:21 +01:00
|
|
|
if (!first)
|
|
|
|
OS << ", ";
|
|
|
|
first = false;
|
|
|
|
OS << L->getHeader()->getName();
|
|
|
|
}
|
2018-03-20 19:43:44 +01:00
|
|
|
OS << ")";
|
2018-03-20 18:09:21 +01:00
|
|
|
}
|
2018-03-20 19:43:44 +01:00
|
|
|
};
|
2018-04-04 13:45:11 +02:00
|
|
|
} // namespace
|
2018-03-20 19:43:44 +01:00
|
|
|
|
|
|
|
bool MustExecutePrinter::runOnFunction(Function &F) {
|
|
|
|
auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
|
|
|
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
|
|
|
|
MustExecuteAnnotatedWriter Writer(F, DT, LI);
|
|
|
|
F.print(dbgs(), &Writer);
|
2018-07-30 21:41:25 +02:00
|
|
|
|
2018-03-20 19:43:44 +01:00
|
|
|
return false;
|
2018-03-20 18:09:21 +01:00
|
|
|
}
|
2019-08-23 17:17:27 +02:00
|
|
|
|
2019-10-13 03:46:49 +02:00
|
|
|
/// Return true if \p L might be an endless loop.
|
|
|
|
static bool maybeEndlessLoop(const Loop &L) {
|
|
|
|
if (L.getHeader()->getParent()->hasFnAttribute(Attribute::WillReturn))
|
|
|
|
return false;
|
|
|
|
// TODO: Actually try to prove it is not.
|
|
|
|
// TODO: If maybeEndlessLoop is going to be expensive, cache it.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-13 16:30:36 +01:00
|
|
|
bool llvm::mayContainIrreducibleControl(const Function &F, const LoopInfo *LI) {
|
2019-10-13 03:46:49 +02:00
|
|
|
if (!LI)
|
|
|
|
return false;
|
|
|
|
using RPOTraversal = ReversePostOrderTraversal<const Function *>;
|
|
|
|
RPOTraversal FuncRPOT(&F);
|
2020-03-13 16:30:36 +01:00
|
|
|
return containsIrreducibleCFG<const BasicBlock *, const RPOTraversal,
|
|
|
|
const LoopInfo>(FuncRPOT, *LI);
|
2019-10-13 03:46:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Lookup \p Key in \p Map and return the result, potentially after
|
|
|
|
/// initializing the optional through \p Fn(\p args).
|
|
|
|
template <typename K, typename V, typename FnTy, typename... ArgsTy>
|
|
|
|
static V getOrCreateCachedOptional(K Key, DenseMap<K, Optional<V>> &Map,
|
|
|
|
FnTy &&Fn, ArgsTy&&... args) {
|
|
|
|
Optional<V> &OptVal = Map[Key];
|
|
|
|
if (!OptVal.hasValue())
|
|
|
|
OptVal = Fn(std::forward<ArgsTy>(args)...);
|
|
|
|
return OptVal.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
const BasicBlock *
|
|
|
|
MustBeExecutedContextExplorer::findForwardJoinPoint(const BasicBlock *InitBB) {
|
|
|
|
const LoopInfo *LI = LIGetter(*InitBB->getParent());
|
|
|
|
const PostDominatorTree *PDT = PDTGetter(*InitBB->getParent());
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "\tFind forward join point for " << InitBB->getName()
|
|
|
|
<< (LI ? " [LI]" : "") << (PDT ? " [PDT]" : ""));
|
|
|
|
|
|
|
|
const Function &F = *InitBB->getParent();
|
|
|
|
const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr;
|
|
|
|
const BasicBlock *HeaderBB = L ? L->getHeader() : InitBB;
|
|
|
|
bool WillReturnAndNoThrow = (F.hasFnAttribute(Attribute::WillReturn) ||
|
|
|
|
(L && !maybeEndlessLoop(*L))) &&
|
|
|
|
F.doesNotThrow();
|
|
|
|
LLVM_DEBUG(dbgs() << (L ? " [in loop]" : "")
|
|
|
|
<< (WillReturnAndNoThrow ? " [WillReturn] [NoUnwind]" : "")
|
|
|
|
<< "\n");
|
|
|
|
|
|
|
|
// Determine the adjacent blocks in the given direction but exclude (self)
|
|
|
|
// loops under certain circumstances.
|
|
|
|
SmallVector<const BasicBlock *, 8> Worklist;
|
|
|
|
for (const BasicBlock *SuccBB : successors(InitBB)) {
|
|
|
|
bool IsLatch = SuccBB == HeaderBB;
|
|
|
|
// Loop latches are ignored in forward propagation if the loop cannot be
|
|
|
|
// endless and may not throw: control has to go somewhere.
|
|
|
|
if (!WillReturnAndNoThrow || !IsLatch)
|
|
|
|
Worklist.push_back(SuccBB);
|
|
|
|
}
|
|
|
|
LLVM_DEBUG(dbgs() << "\t\t#Worklist: " << Worklist.size() << "\n");
|
|
|
|
|
|
|
|
// If there are no other adjacent blocks, there is no join point.
|
|
|
|
if (Worklist.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If there is one adjacent block, it is the join point.
|
|
|
|
if (Worklist.size() == 1)
|
|
|
|
return Worklist[0];
|
|
|
|
|
|
|
|
// Try to determine a join block through the help of the post-dominance
|
|
|
|
// tree. If no tree was provided, we perform simple pattern matching for one
|
|
|
|
// block conditionals and one block loops only.
|
|
|
|
const BasicBlock *JoinBB = nullptr;
|
|
|
|
if (PDT)
|
|
|
|
if (const auto *InitNode = PDT->getNode(InitBB))
|
|
|
|
if (const auto *IDomNode = InitNode->getIDom())
|
|
|
|
JoinBB = IDomNode->getBlock();
|
|
|
|
|
|
|
|
if (!JoinBB && Worklist.size() == 2) {
|
|
|
|
const BasicBlock *Succ0 = Worklist[0];
|
|
|
|
const BasicBlock *Succ1 = Worklist[1];
|
|
|
|
const BasicBlock *Succ0UniqueSucc = Succ0->getUniqueSuccessor();
|
|
|
|
const BasicBlock *Succ1UniqueSucc = Succ1->getUniqueSuccessor();
|
|
|
|
if (Succ0UniqueSucc == InitBB) {
|
|
|
|
// InitBB -> Succ0 -> InitBB
|
|
|
|
// InitBB -> Succ1 = JoinBB
|
|
|
|
JoinBB = Succ1;
|
|
|
|
} else if (Succ1UniqueSucc == InitBB) {
|
|
|
|
// InitBB -> Succ1 -> InitBB
|
|
|
|
// InitBB -> Succ0 = JoinBB
|
|
|
|
JoinBB = Succ0;
|
|
|
|
} else if (Succ0 == Succ1UniqueSucc) {
|
|
|
|
// InitBB -> Succ0 = JoinBB
|
|
|
|
// InitBB -> Succ1 -> Succ0 = JoinBB
|
|
|
|
JoinBB = Succ0;
|
|
|
|
} else if (Succ1 == Succ0UniqueSucc) {
|
|
|
|
// InitBB -> Succ0 -> Succ1 = JoinBB
|
|
|
|
// InitBB -> Succ1 = JoinBB
|
|
|
|
JoinBB = Succ1;
|
|
|
|
} else if (Succ0UniqueSucc == Succ1UniqueSucc) {
|
|
|
|
// InitBB -> Succ0 -> JoinBB
|
|
|
|
// InitBB -> Succ1 -> JoinBB
|
|
|
|
JoinBB = Succ0UniqueSucc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JoinBB && L)
|
|
|
|
JoinBB = L->getUniqueExitBlock();
|
|
|
|
|
|
|
|
if (!JoinBB)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "\t\tJoin block candidate: " << JoinBB->getName() << "\n");
|
|
|
|
|
|
|
|
// In forward direction we check if control will for sure reach JoinBB from
|
|
|
|
// InitBB, thus it can not be "stopped" along the way. Ways to "stop" control
|
|
|
|
// are: infinite loops and instructions that do not necessarily transfer
|
|
|
|
// execution to their successor. To check for them we traverse the CFG from
|
|
|
|
// the adjacent blocks to the JoinBB, looking at all intermediate blocks.
|
|
|
|
|
|
|
|
// If we know the function is "will-return" and "no-throw" there is no need
|
|
|
|
// for futher checks.
|
|
|
|
if (!F.hasFnAttribute(Attribute::WillReturn) || !F.doesNotThrow()) {
|
|
|
|
|
|
|
|
auto BlockTransfersExecutionToSuccessor = [](const BasicBlock *BB) {
|
|
|
|
return isGuaranteedToTransferExecutionToSuccessor(BB);
|
|
|
|
};
|
|
|
|
|
|
|
|
SmallPtrSet<const BasicBlock *, 16> Visited;
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
const BasicBlock *ToBB = Worklist.pop_back_val();
|
|
|
|
if (ToBB == JoinBB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Make sure all loops in-between are finite.
|
|
|
|
if (!Visited.insert(ToBB).second) {
|
|
|
|
if (!F.hasFnAttribute(Attribute::WillReturn)) {
|
|
|
|
if (!LI)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
bool MayContainIrreducibleControl = getOrCreateCachedOptional(
|
|
|
|
&F, IrreducibleControlMap, mayContainIrreducibleControl, F, LI);
|
|
|
|
if (MayContainIrreducibleControl)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Loop *L = LI->getLoopFor(ToBB);
|
|
|
|
if (L && maybeEndlessLoop(*L))
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the block has no instructions that could stop control
|
|
|
|
// transfer.
|
|
|
|
bool TransfersExecution = getOrCreateCachedOptional(
|
|
|
|
ToBB, BlockTransferMap, BlockTransfersExecutionToSuccessor, ToBB);
|
|
|
|
if (!TransfersExecution)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
for (const BasicBlock *AdjacentBB : successors(ToBB))
|
|
|
|
Worklist.push_back(AdjacentBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "\tJoin block: " << JoinBB->getName() << "\n");
|
|
|
|
return JoinBB;
|
|
|
|
}
|
2020-02-20 05:00:43 +01:00
|
|
|
const BasicBlock *
|
|
|
|
MustBeExecutedContextExplorer::findBackwardJoinPoint(const BasicBlock *InitBB) {
|
|
|
|
const LoopInfo *LI = LIGetter(*InitBB->getParent());
|
|
|
|
const DominatorTree *DT = DTGetter(*InitBB->getParent());
|
|
|
|
LLVM_DEBUG(dbgs() << "\tFind backward join point for " << InitBB->getName()
|
|
|
|
<< (LI ? " [LI]" : "") << (DT ? " [DT]" : ""));
|
|
|
|
|
|
|
|
// Try to determine a join block through the help of the dominance tree. If no
|
|
|
|
// tree was provided, we perform simple pattern matching for one block
|
|
|
|
// conditionals only.
|
|
|
|
if (DT)
|
|
|
|
if (const auto *InitNode = DT->getNode(InitBB))
|
|
|
|
if (const auto *IDomNode = InitNode->getIDom())
|
|
|
|
return IDomNode->getBlock();
|
|
|
|
|
|
|
|
const Loop *L = LI ? LI->getLoopFor(InitBB) : nullptr;
|
|
|
|
const BasicBlock *HeaderBB = L ? L->getHeader() : nullptr;
|
|
|
|
|
|
|
|
// Determine the predecessor blocks but ignore backedges.
|
|
|
|
SmallVector<const BasicBlock *, 8> Worklist;
|
|
|
|
for (const BasicBlock *PredBB : predecessors(InitBB)) {
|
|
|
|
bool IsBackedge =
|
|
|
|
(PredBB == InitBB) || (HeaderBB == InitBB && L->contains(PredBB));
|
|
|
|
// Loop backedges are ignored in backwards propagation: control has to come
|
|
|
|
// from somewhere.
|
|
|
|
if (!IsBackedge)
|
|
|
|
Worklist.push_back(PredBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no other predecessor blocks, there is no join point.
|
|
|
|
if (Worklist.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If there is one predecessor block, it is the join point.
|
|
|
|
if (Worklist.size() == 1)
|
|
|
|
return Worklist[0];
|
|
|
|
|
|
|
|
const BasicBlock *JoinBB = nullptr;
|
|
|
|
if (Worklist.size() == 2) {
|
|
|
|
const BasicBlock *Pred0 = Worklist[0];
|
|
|
|
const BasicBlock *Pred1 = Worklist[1];
|
|
|
|
const BasicBlock *Pred0UniquePred = Pred0->getUniquePredecessor();
|
|
|
|
const BasicBlock *Pred1UniquePred = Pred1->getUniquePredecessor();
|
|
|
|
if (Pred0 == Pred1UniquePred) {
|
|
|
|
// InitBB <- Pred0 = JoinBB
|
|
|
|
// InitBB <- Pred1 <- Pred0 = JoinBB
|
|
|
|
JoinBB = Pred0;
|
|
|
|
} else if (Pred1 == Pred0UniquePred) {
|
|
|
|
// InitBB <- Pred0 <- Pred1 = JoinBB
|
|
|
|
// InitBB <- Pred1 = JoinBB
|
|
|
|
JoinBB = Pred1;
|
|
|
|
} else if (Pred0UniquePred == Pred1UniquePred) {
|
|
|
|
// InitBB <- Pred0 <- JoinBB
|
|
|
|
// InitBB <- Pred1 <- JoinBB
|
|
|
|
JoinBB = Pred0UniquePred;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JoinBB && L)
|
|
|
|
JoinBB = L->getHeader();
|
|
|
|
|
|
|
|
// In backwards direction there is no need to show termination of previous
|
|
|
|
// instructions. If they do not terminate, the code afterward is dead, making
|
|
|
|
// any information/transformation correct anyway.
|
|
|
|
return JoinBB;
|
|
|
|
}
|
2019-10-13 03:46:49 +02:00
|
|
|
|
2019-08-23 17:17:27 +02:00
|
|
|
const Instruction *
|
|
|
|
MustBeExecutedContextExplorer::getMustBeExecutedNextInstruction(
|
|
|
|
MustBeExecutedIterator &It, const Instruction *PP) {
|
|
|
|
if (!PP)
|
|
|
|
return PP;
|
|
|
|
LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP << "\n");
|
|
|
|
|
|
|
|
// If we explore only inside a given basic block we stop at terminators.
|
|
|
|
if (!ExploreInterBlock && PP->isTerminator()) {
|
|
|
|
LLVM_DEBUG(dbgs() << "\tReached terminator in intra-block mode, done\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we do not traverse the call graph we check if we can make progress in
|
|
|
|
// the current function. First, check if the instruction is guaranteed to
|
|
|
|
// transfer execution to the successor.
|
|
|
|
bool TransfersExecution = isGuaranteedToTransferExecutionToSuccessor(PP);
|
|
|
|
if (!TransfersExecution)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If this is not a terminator we know that there is a single instruction
|
|
|
|
// after this one that is executed next if control is transfered. If not,
|
|
|
|
// we can try to go back to a call site we entered earlier. If none exists, we
|
|
|
|
// do not know any instruction that has to be executd next.
|
|
|
|
if (!PP->isTerminator()) {
|
|
|
|
const Instruction *NextPP = PP->getNextNode();
|
|
|
|
LLVM_DEBUG(dbgs() << "\tIntermediate instruction does transfer control\n");
|
|
|
|
return NextPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we have to handle terminators, trivial ones first.
|
|
|
|
assert(PP->isTerminator() && "Expected a terminator!");
|
|
|
|
|
|
|
|
// A terminator without a successor is not handled yet.
|
|
|
|
if (PP->getNumSuccessors() == 0) {
|
|
|
|
LLVM_DEBUG(dbgs() << "\tUnhandled terminator\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A terminator with a single successor, we will continue at the beginning of
|
|
|
|
// that one.
|
|
|
|
if (PP->getNumSuccessors() == 1) {
|
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "\tUnconditional terminator, continue with successor\n");
|
|
|
|
return &PP->getSuccessor(0)->front();
|
|
|
|
}
|
|
|
|
|
2019-10-13 03:46:49 +02:00
|
|
|
// Multiple successors mean we need to find the join point where control flow
|
|
|
|
// converges again. We use the findForwardJoinPoint helper function with
|
|
|
|
// information about the function and helper analyses, if available.
|
|
|
|
if (const BasicBlock *JoinBB = findForwardJoinPoint(PP->getParent()))
|
|
|
|
return &JoinBB->front();
|
|
|
|
|
2019-08-23 17:17:27 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "\tNo join point found\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-20 05:00:43 +01:00
|
|
|
const Instruction *
|
|
|
|
MustBeExecutedContextExplorer::getMustBeExecutedPrevInstruction(
|
|
|
|
MustBeExecutedIterator &It, const Instruction *PP) {
|
|
|
|
if (!PP)
|
|
|
|
return PP;
|
|
|
|
|
|
|
|
bool IsFirst = !(PP->getPrevNode());
|
|
|
|
LLVM_DEBUG(dbgs() << "Find next instruction for " << *PP
|
|
|
|
<< (IsFirst ? " [IsFirst]" : "") << "\n");
|
|
|
|
|
|
|
|
// If we explore only inside a given basic block we stop at the first
|
|
|
|
// instruction.
|
|
|
|
if (!ExploreInterBlock && IsFirst) {
|
|
|
|
LLVM_DEBUG(dbgs() << "\tReached block front in intra-block mode, done\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The block and function that contains the current position.
|
|
|
|
const BasicBlock *PPBlock = PP->getParent();
|
|
|
|
|
|
|
|
// If we are inside a block we know what instruction was executed before, the
|
|
|
|
// previous one.
|
|
|
|
if (!IsFirst) {
|
|
|
|
const Instruction *PrevPP = PP->getPrevNode();
|
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "\tIntermediate instruction, continue with previous\n");
|
|
|
|
// We did not enter a callee so we simply return the previous instruction.
|
|
|
|
return PrevPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we have to handle the case where the program point is the first in
|
|
|
|
// a block but not in the function. We use the findBackwardJoinPoint helper
|
|
|
|
// function with information about the function and helper analyses, if
|
|
|
|
// available.
|
|
|
|
if (const BasicBlock *JoinBB = findBackwardJoinPoint(PPBlock))
|
|
|
|
return &JoinBB->back();
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "\tNo join point found\n");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-08-23 17:17:27 +02:00
|
|
|
MustBeExecutedIterator::MustBeExecutedIterator(
|
|
|
|
MustBeExecutedContextExplorer &Explorer, const Instruction *I)
|
|
|
|
: Explorer(Explorer), CurInst(I) {
|
|
|
|
reset(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MustBeExecutedIterator::reset(const Instruction *I) {
|
|
|
|
Visited.clear();
|
2020-02-20 05:00:43 +01:00
|
|
|
resetInstruction(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MustBeExecutedIterator::resetInstruction(const Instruction *I) {
|
|
|
|
CurInst = I;
|
|
|
|
Head = Tail = nullptr;
|
|
|
|
Visited.insert({I, ExplorationDirection::FORWARD});
|
|
|
|
Visited.insert({I, ExplorationDirection::BACKWARD});
|
|
|
|
if (Explorer.ExploreCFGForward)
|
|
|
|
Head = I;
|
|
|
|
if (Explorer.ExploreCFGBackward)
|
|
|
|
Tail = I;
|
2019-08-23 17:17:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Instruction *MustBeExecutedIterator::advance() {
|
|
|
|
assert(CurInst && "Cannot advance an end iterator!");
|
2020-02-20 05:00:43 +01:00
|
|
|
Head = Explorer.getMustBeExecutedNextInstruction(*this, Head);
|
|
|
|
if (Head && Visited.insert({Head, ExplorationDirection ::FORWARD}).second)
|
|
|
|
return Head;
|
|
|
|
Head = nullptr;
|
|
|
|
|
|
|
|
Tail = Explorer.getMustBeExecutedPrevInstruction(*this, Tail);
|
|
|
|
if (Tail && Visited.insert({Tail, ExplorationDirection ::BACKWARD}).second)
|
|
|
|
return Tail;
|
|
|
|
Tail = nullptr;
|
|
|
|
return nullptr;
|
2019-08-23 17:17:27 +02:00
|
|
|
}
|