2018-03-21 01:09:21 +08:00
|
|
|
//===- MustExecute.cpp - Printer for isGuaranteedToExecute ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-03-30 03:22:12 +08:00
|
|
|
#include "llvm/Analysis/MustExecute.h"
|
2018-03-21 06:45:23 +08:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2018-03-21 01:09:21 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2018-03-21 02:43:44 +08:00
|
|
|
#include "llvm/IR/AssemblyAnnotationWriter.h"
|
2018-03-21 01:09:21 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/InstIterator.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2018-03-21 02:43:44 +08:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2018-03-21 01:09:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-10-16 16:07:14 +08: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 16:31:05 +08:00
|
|
|
bool SimpleLoopSafetyInfo::blockMayThrow(const BasicBlock *BB) const {
|
2018-10-16 15:50:14 +08:00
|
|
|
(void)BB;
|
|
|
|
return anyBlockMayThrow();
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:31:05 +08:00
|
|
|
bool SimpleLoopSafetyInfo::anyBlockMayThrow() const {
|
2018-08-15 13:55:43 +08:00
|
|
|
return MayThrow;
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:31:05 +08:00
|
|
|
void SimpleLoopSafetyInfo::computeLoopSafetyInfo(const Loop *CurLoop) {
|
2018-07-20 03:11:29 +08:00
|
|
|
assert(CurLoop != nullptr && "CurLoop can't be null");
|
2018-03-21 06:45:23 +08:00
|
|
|
BasicBlock *Header = CurLoop->getHeader();
|
|
|
|
// Iterate over header and compute safety info.
|
2018-08-15 13:55:43 +08:00
|
|
|
HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header);
|
|
|
|
MayThrow = HeaderMayThrow;
|
2018-03-21 06:45:23 +08: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 13:55:43 +08:00
|
|
|
(BB != BBE) && !MayThrow; ++BB)
|
|
|
|
MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB);
|
2018-03-21 06:45:23 +08:00
|
|
|
|
2018-10-16 16:07:14 +08:00
|
|
|
computeBlockColors(CurLoop);
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:58:09 +08: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 17:29:58 +08:00
|
|
|
MW.clear();
|
2018-10-16 17:58:09 +08: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);
|
|
|
|
}
|
|
|
|
|
2018-11-01 18:16:06 +08:00
|
|
|
void ICFLoopSafetyInfo::insertInstructionTo(const BasicBlock *BB) {
|
2018-10-16 17:58:09 +08:00
|
|
|
ICF.invalidateBlock(BB);
|
2018-11-12 17:29:58 +08:00
|
|
|
MW.invalidateBlock(BB);
|
2018-10-16 17:58:09 +08:00
|
|
|
}
|
|
|
|
|
2018-11-01 18:16:06 +08:00
|
|
|
void ICFLoopSafetyInfo::removeInstruction(const Instruction *Inst) {
|
|
|
|
// TODO: So far we just conservatively drop cache, but maybe we can not do it
|
|
|
|
// when Inst is not an ICF instruction. Follow-up on that.
|
|
|
|
ICF.invalidateBlock(Inst->getParent());
|
2018-11-12 17:29:58 +08:00
|
|
|
MW.invalidateBlock(Inst->getParent());
|
2018-11-01 18:16:06 +08:00
|
|
|
}
|
|
|
|
|
2018-10-16 16:07:14 +08:00
|
|
|
void LoopSafetyInfo::computeBlockColors(const Loop *CurLoop) {
|
2018-03-21 06:45:23 +08: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-18 04:52:03 +08:00
|
|
|
if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
|
2018-08-15 13:55:43 +08:00
|
|
|
BlockColors = colorEHFunclets(*Fn);
|
2018-03-21 06:45:23 +08: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 14:28:04 +08:00
|
|
|
static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
|
2018-03-21 06:45:23 +08: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 12:56:28 +08: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-21 06:45:23 +08: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 17:07:03 +08: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 15:15:06 +08:00
|
|
|
const Loop *CurLoop, const BasicBlock *BB,
|
2018-11-06 17:07:03 +08:00
|
|
|
SmallPtrSetImpl<const BasicBlock *> &Predecessors) {
|
2018-08-21 15:15:06 +08:00
|
|
|
assert(Predecessors.empty() && "Garbage in predecessors set?");
|
2018-08-17 14:19:17 +08:00
|
|
|
assert(CurLoop->contains(BB) && "Should only be called for loop blocks!");
|
|
|
|
if (BB == CurLoop->getHeader())
|
2018-08-21 15:15:06 +08:00
|
|
|
return;
|
2018-08-17 14:19:17 +08: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 15:15:06 +08: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 14:19:17 +08:00
|
|
|
|
|
|
|
// Make sure that all successors of all predecessors of BB are either:
|
|
|
|
// 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 15:50:14 +08:00
|
|
|
for (auto *Pred : Predecessors) {
|
|
|
|
// Predecessor block may throw, so it has a side exit.
|
|
|
|
if (blockMayThrow(Pred))
|
|
|
|
return false;
|
2018-08-17 14:19:17 +08: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 15:50:14 +08:00
|
|
|
}
|
2018-08-17 14:19:17 +08:00
|
|
|
|
|
|
|
// All predecessors can only lead us to BB.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-21 06:45:23 +08:00
|
|
|
/// Returns true if the instruction in a loop is guaranteed to execute at least
|
|
|
|
/// once.
|
2018-10-16 16:31:05 +08:00
|
|
|
bool SimpleLoopSafetyInfo::isGuaranteedToExecute(const Instruction &Inst,
|
|
|
|
const DominatorTree *DT,
|
|
|
|
const Loop *CurLoop) const {
|
2018-03-21 06:45:23 +08: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-28 04:44:01 +08: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 17:11:25 +08:00
|
|
|
return !HeaderMayThrow ||
|
2018-10-16 14:34:53 +08:00
|
|
|
Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
|
2018-03-21 06:45:23 +08:00
|
|
|
|
2018-08-17 14:19:17 +08: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 17:11:25 +08:00
|
|
|
return allLoopPathsLeadToBlock(CurLoop, Inst.getParent(), DT);
|
2018-03-21 06:45:23 +08:00
|
|
|
}
|
|
|
|
|
2018-10-16 17:58:09 +08: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-21 06:45:23 +08:00
|
|
|
|
2018-11-12 17:29:58 +08: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-21 01:09:21 +08: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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-04-04 19:45:11 +08:00
|
|
|
static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) {
|
2018-03-21 07:00:54 +08: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 16:31:05 +08:00
|
|
|
SimpleLoopSafetyInfo LSI;
|
2018-08-15 13:55:43 +08:00
|
|
|
LSI.computeLoopSafetyInfo(L);
|
2018-10-16 14:34:53 +08:00
|
|
|
return LSI.isGuaranteedToExecute(I, DT, L) ||
|
2018-03-21 07:00:54 +08:00
|
|
|
isGuaranteedToExecuteForEveryIteration(&I, L);
|
2018-03-21 01:09:21 +08:00
|
|
|
}
|
|
|
|
|
2018-04-04 19:45:11 +08:00
|
|
|
namespace {
|
2018-05-01 23:54:18 +08:00
|
|
|
/// An assembly annotator class to print must execute information in
|
2018-03-21 02:43:44 +08: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-21 01:09:21 +08:00
|
|
|
}
|
2018-03-21 02:43:44 +08: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-31 03:41:25 +08:00
|
|
|
void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
|
2018-03-21 02:43:44 +08:00
|
|
|
if (!MustExec.count(&V))
|
|
|
|
return;
|
2018-03-21 01:09:21 +08:00
|
|
|
|
2018-03-21 02:43:44 +08:00
|
|
|
const auto &Loops = MustExec.lookup(&V);
|
|
|
|
const auto NumLoops = Loops.size();
|
2018-03-21 01:09:21 +08:00
|
|
|
if (NumLoops > 1)
|
2018-03-21 02:43:44 +08:00
|
|
|
OS << " ; (mustexec in " << NumLoops << " loops: ";
|
2018-03-21 01:09:21 +08:00
|
|
|
else
|
2018-03-21 02:43:44 +08:00
|
|
|
OS << " ; (mustexec in: ";
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2018-03-21 01:09:21 +08:00
|
|
|
bool first = true;
|
2018-03-21 02:43:44 +08:00
|
|
|
for (const Loop *L : Loops) {
|
2018-03-21 01:09:21 +08:00
|
|
|
if (!first)
|
|
|
|
OS << ", ";
|
|
|
|
first = false;
|
|
|
|
OS << L->getHeader()->getName();
|
|
|
|
}
|
2018-03-21 02:43:44 +08:00
|
|
|
OS << ")";
|
2018-03-21 01:09:21 +08:00
|
|
|
}
|
2018-03-21 02:43:44 +08:00
|
|
|
};
|
2018-04-04 19:45:11 +08:00
|
|
|
} // namespace
|
2018-03-21 02:43:44 +08: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-31 03:41:25 +08:00
|
|
|
|
2018-03-21 02:43:44 +08:00
|
|
|
return false;
|
2018-03-21 01:09:21 +08:00
|
|
|
}
|