forked from OSchip/llvm-project
[NFC] Refactoring of LoopSafetyInfo, step 1
Turn structure into class, encapsulate methods, add clarifying comments. Differential Revision: https://reviews.llvm.org/D50693 Reviewed By: reames llvm-svn: 339752
This commit is contained in:
parent
df58dd8418
commit
530b8d1c3d
|
@ -36,23 +36,42 @@ class Loop;
|
||||||
/// at runtime. The primary way to consume this infromation is via
|
/// at runtime. The primary way to consume this infromation is via
|
||||||
/// isGuaranteedToExecute below, but some callers bailout or fallback to
|
/// isGuaranteedToExecute below, but some callers bailout or fallback to
|
||||||
/// alternate reasoning if a loop contains any implicit control flow.
|
/// alternate reasoning if a loop contains any implicit control flow.
|
||||||
struct LoopSafetyInfo {
|
/// NOTE: LoopSafetyInfo contains cached information regarding loops and their
|
||||||
|
/// particular blocks. This information is only dropped on invocation of
|
||||||
|
/// computeLoopSafetyInfo. If the loop or any of its block is deleted, or if
|
||||||
|
/// any thrower instructions have been added or removed from them, or if the
|
||||||
|
/// control flow has changed, or in case of other meaningful modifications, the
|
||||||
|
/// LoopSafetyInfo needs to be recomputed. If a meaningful modifications to the
|
||||||
|
/// loop were made and the info wasn't recomputed properly, the behavior of all
|
||||||
|
/// methods except for computeLoopSafetyInfo is undefined.
|
||||||
|
class LoopSafetyInfo {
|
||||||
bool MayThrow = false; // The current loop contains an instruction which
|
bool MayThrow = false; // The current loop contains an instruction which
|
||||||
// may throw.
|
// may throw.
|
||||||
bool HeaderMayThrow = false; // Same as previous, but specific to loop header
|
bool HeaderMayThrow = false; // Same as previous, but specific to loop header
|
||||||
|
|
||||||
|
public:
|
||||||
// Used to update funclet bundle operands.
|
// Used to update funclet bundle operands.
|
||||||
DenseMap<BasicBlock *, ColorVector> BlockColors;
|
DenseMap<BasicBlock *, ColorVector> BlockColors;
|
||||||
|
|
||||||
|
/// Returns true iff the header block of the loop for which this info is
|
||||||
|
/// calculated contains an instruction that may throw or otherwise exit
|
||||||
|
/// abnormally.
|
||||||
|
bool headerMayThrow() const;
|
||||||
|
|
||||||
|
/// Returns true iff any block of the loop for which this info is contains an
|
||||||
|
/// instruction that may throw or otherwise exit abnormally.
|
||||||
|
bool anyBlockMayThrow() const;
|
||||||
|
|
||||||
|
/// Computes safety information for a loop checks loop body & header for
|
||||||
|
/// the possibility of may throw exception, it takes LoopSafetyInfo and loop
|
||||||
|
/// as argument. Updates safety information in LoopSafetyInfo argument.
|
||||||
|
/// Note: This is defined to clear and reinitialize an already initialized
|
||||||
|
/// LoopSafetyInfo. Some callers rely on this fact.
|
||||||
|
void computeLoopSafetyInfo(Loop *);
|
||||||
|
|
||||||
LoopSafetyInfo() = default;
|
LoopSafetyInfo() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Computes safety information for a loop checks loop body & header for
|
|
||||||
/// the possibility of may throw exception, it takes LoopSafetyInfo and loop as
|
|
||||||
/// argument. Updates safety information in LoopSafetyInfo argument.
|
|
||||||
/// Note: This is defined to clear and reinitialize an already initialized
|
|
||||||
/// LoopSafetyInfo. Some callers rely on this fact.
|
|
||||||
void computeLoopSafetyInfo(LoopSafetyInfo *, Loop *);
|
|
||||||
|
|
||||||
/// Returns true if the instruction in a loop is guaranteed to execute at least
|
/// Returns true if the instruction in a loop is guaranteed to execute at least
|
||||||
/// once (under the assumption that the loop is entered).
|
/// once (under the assumption that the loop is entered).
|
||||||
bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT,
|
bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT,
|
||||||
|
|
|
@ -22,20 +22,20 @@
|
||||||
#include "llvm/Support/raw_ostream.h"
|
#include "llvm/Support/raw_ostream.h"
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
/// Computes loop safety information, checks loop body & header
|
bool LoopSafetyInfo::headerMayThrow() const {
|
||||||
/// for the possibility of may throw exception.
|
return HeaderMayThrow;
|
||||||
///
|
}
|
||||||
void llvm::computeLoopSafetyInfo(LoopSafetyInfo *SafetyInfo, Loop *CurLoop) {
|
|
||||||
|
bool LoopSafetyInfo::anyBlockMayThrow() const {
|
||||||
|
return MayThrow;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoopSafetyInfo::computeLoopSafetyInfo(Loop *CurLoop) {
|
||||||
assert(CurLoop != nullptr && "CurLoop can't be null");
|
assert(CurLoop != nullptr && "CurLoop can't be null");
|
||||||
BasicBlock *Header = CurLoop->getHeader();
|
BasicBlock *Header = CurLoop->getHeader();
|
||||||
// Setting default safety values.
|
|
||||||
SafetyInfo->MayThrow = false;
|
|
||||||
SafetyInfo->HeaderMayThrow = false;
|
|
||||||
// Iterate over header and compute safety info.
|
// Iterate over header and compute safety info.
|
||||||
SafetyInfo->HeaderMayThrow =
|
HeaderMayThrow = !isGuaranteedToTransferExecutionToSuccessor(Header);
|
||||||
!isGuaranteedToTransferExecutionToSuccessor(Header);
|
MayThrow = HeaderMayThrow;
|
||||||
|
|
||||||
SafetyInfo->MayThrow = SafetyInfo->HeaderMayThrow;
|
|
||||||
// Iterate over loop instructions and compute safety info.
|
// Iterate over loop instructions and compute safety info.
|
||||||
// Skip header as it has been computed and stored in HeaderMayThrow.
|
// Skip header as it has been computed and stored in HeaderMayThrow.
|
||||||
// The first block in loopinfo.Blocks is guaranteed to be the header.
|
// The first block in loopinfo.Blocks is guaranteed to be the header.
|
||||||
|
@ -43,9 +43,8 @@ void llvm::computeLoopSafetyInfo(LoopSafetyInfo *SafetyInfo, Loop *CurLoop) {
|
||||||
"First block must be header");
|
"First block must be header");
|
||||||
for (Loop::block_iterator BB = std::next(CurLoop->block_begin()),
|
for (Loop::block_iterator BB = std::next(CurLoop->block_begin()),
|
||||||
BBE = CurLoop->block_end();
|
BBE = CurLoop->block_end();
|
||||||
(BB != BBE) && !SafetyInfo->MayThrow; ++BB)
|
(BB != BBE) && !MayThrow; ++BB)
|
||||||
SafetyInfo->MayThrow |=
|
MayThrow |= !isGuaranteedToTransferExecutionToSuccessor(*BB);
|
||||||
!isGuaranteedToTransferExecutionToSuccessor(*BB);
|
|
||||||
|
|
||||||
// Compute funclet colors if we might sink/hoist in a function with a funclet
|
// Compute funclet colors if we might sink/hoist in a function with a funclet
|
||||||
// personality routine.
|
// personality routine.
|
||||||
|
@ -53,7 +52,7 @@ void llvm::computeLoopSafetyInfo(LoopSafetyInfo *SafetyInfo, Loop *CurLoop) {
|
||||||
if (Fn->hasPersonalityFn())
|
if (Fn->hasPersonalityFn())
|
||||||
if (Constant *PersonalityFn = Fn->getPersonalityFn())
|
if (Constant *PersonalityFn = Fn->getPersonalityFn())
|
||||||
if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
|
if (isScopedEHPersonality(classifyEHPersonality(PersonalityFn)))
|
||||||
SafetyInfo->BlockColors = colorEHFunclets(*Fn);
|
BlockColors = colorEHFunclets(*Fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return true if we can prove that the given ExitBlock is not reached on the
|
/// Return true if we can prove that the given ExitBlock is not reached on the
|
||||||
|
@ -116,12 +115,12 @@ bool llvm::isGuaranteedToExecute(const Instruction &Inst,
|
||||||
// Inst unless we can prove that Inst comes before the potential implicit
|
// 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
|
// 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.
|
// the instruction of interest is the first one in the block.
|
||||||
return !SafetyInfo->HeaderMayThrow ||
|
return !SafetyInfo->headerMayThrow() ||
|
||||||
Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
|
Inst.getParent()->getFirstNonPHIOrDbg() == &Inst;
|
||||||
|
|
||||||
// Somewhere in this loop there is an instruction which may throw and make us
|
// Somewhere in this loop there is an instruction which may throw and make us
|
||||||
// exit the loop.
|
// exit the loop.
|
||||||
if (SafetyInfo->MayThrow)
|
if (SafetyInfo->anyBlockMayThrow())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Note: There are two styles of reasoning intermixed below for
|
// Note: There are two styles of reasoning intermixed below for
|
||||||
|
@ -196,7 +195,7 @@ static bool isMustExecuteIn(const Instruction &I, Loop *L, DominatorTree *DT) {
|
||||||
// result obtained by *either* implementation. This is a bit unfair since no
|
// result obtained by *either* implementation. This is a bit unfair since no
|
||||||
// caller actually gets the full power at the moment.
|
// caller actually gets the full power at the moment.
|
||||||
LoopSafetyInfo LSI;
|
LoopSafetyInfo LSI;
|
||||||
computeLoopSafetyInfo(&LSI, L);
|
LSI.computeLoopSafetyInfo(L);
|
||||||
return isGuaranteedToExecute(I, DT, L, &LSI) ||
|
return isGuaranteedToExecute(I, DT, L, &LSI) ||
|
||||||
isGuaranteedToExecuteForEveryIteration(&I, L);
|
isGuaranteedToExecuteForEveryIteration(&I, L);
|
||||||
}
|
}
|
||||||
|
|
|
@ -261,7 +261,7 @@ bool LoopInvariantCodeMotion::runOnLoop(
|
||||||
|
|
||||||
// Compute loop safety information.
|
// Compute loop safety information.
|
||||||
LoopSafetyInfo SafetyInfo;
|
LoopSafetyInfo SafetyInfo;
|
||||||
computeLoopSafetyInfo(&SafetyInfo, L);
|
SafetyInfo.computeLoopSafetyInfo(L);
|
||||||
|
|
||||||
// We want to visit all of the instructions in this loop... that are not parts
|
// We want to visit all of the instructions in this loop... that are not parts
|
||||||
// of our subloops (they have already had their invariants hoisted out of
|
// of our subloops (they have already had their invariants hoisted out of
|
||||||
|
@ -1310,7 +1310,7 @@ bool llvm::promoteLoopAccessesToScalars(
|
||||||
const DataLayout &MDL = Preheader->getModule()->getDataLayout();
|
const DataLayout &MDL = Preheader->getModule()->getDataLayout();
|
||||||
|
|
||||||
bool IsKnownThreadLocalObject = false;
|
bool IsKnownThreadLocalObject = false;
|
||||||
if (SafetyInfo->MayThrow) {
|
if (SafetyInfo->anyBlockMayThrow()) {
|
||||||
// If a loop can throw, we have to insert a store along each unwind edge.
|
// If a loop can throw, we have to insert a store along each unwind edge.
|
||||||
// That said, we can't actually make the unwind edge explicit. Therefore,
|
// That said, we can't actually make the unwind edge explicit. Therefore,
|
||||||
// we have to prove that the store is dead along the unwind edge. We do
|
// we have to prove that the store is dead along the unwind edge. We do
|
||||||
|
|
|
@ -320,8 +320,8 @@ bool LoopIdiomRecognize::runOnCountableLoop() {
|
||||||
// The following transforms hoist stores/memsets into the loop pre-header.
|
// The following transforms hoist stores/memsets into the loop pre-header.
|
||||||
// Give up if the loop has instructions may throw.
|
// Give up if the loop has instructions may throw.
|
||||||
LoopSafetyInfo SafetyInfo;
|
LoopSafetyInfo SafetyInfo;
|
||||||
computeLoopSafetyInfo(&SafetyInfo, CurLoop);
|
SafetyInfo.computeLoopSafetyInfo(CurLoop);
|
||||||
if (SafetyInfo.MayThrow)
|
if (SafetyInfo.anyBlockMayThrow())
|
||||||
return MadeChange;
|
return MadeChange;
|
||||||
|
|
||||||
// Scan all the blocks in the loop that are not in subloops.
|
// Scan all the blocks in the loop that are not in subloops.
|
||||||
|
|
|
@ -520,7 +520,7 @@ bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) {
|
||||||
|
|
||||||
SanitizeMemory = F->hasFnAttribute(Attribute::SanitizeMemory);
|
SanitizeMemory = F->hasFnAttribute(Attribute::SanitizeMemory);
|
||||||
if (SanitizeMemory)
|
if (SanitizeMemory)
|
||||||
computeLoopSafetyInfo(&SafetyInfo, L);
|
SafetyInfo.computeLoopSafetyInfo(L);
|
||||||
|
|
||||||
bool Changed = false;
|
bool Changed = false;
|
||||||
do {
|
do {
|
||||||
|
|
|
@ -762,8 +762,8 @@ bool llvm::isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
|
||||||
|
|
||||||
// Check the loop safety info for exceptions.
|
// Check the loop safety info for exceptions.
|
||||||
LoopSafetyInfo LSI;
|
LoopSafetyInfo LSI;
|
||||||
computeLoopSafetyInfo(&LSI, L);
|
LSI.computeLoopSafetyInfo(L);
|
||||||
if (LSI.MayThrow) {
|
if (LSI.anyBlockMayThrow()) {
|
||||||
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Something may throw\n");
|
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Something may throw\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue