Skip to content

Commit 9100bd7

Browse files
committed
[SCEV][NFC] Introduce isBasicBlockEntryGuardedByCond
Currently, we have `isLoopEntryGuardedByCond` method in SCEV, which checks that some fact is true if we enter the loop. In fact, this is just a particular case of more general concept `isBasicBlockEntryGuardedByCond` applied to given loop's header. In fact, the logic if this code is largely independent on the given loop and only cares code above it. This patch makes this generalization. Now we can query it for any block, and `isBasicBlockEntryGuardedByCond` is just a particular case. Differential Revision: https://reviews.llvm.org/D87828 Reviewed By: fhahn
1 parent eb9f7c2 commit 9100bd7

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,12 @@ class ScalarEvolution {
677677
bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
678678
const SCEV *LHS, const SCEV *RHS);
679679

680+
/// Test whether entry to the basic block is protected by a conditional
681+
/// between LHS and RHS.
682+
bool isBasicBlockEntryGuardedByCond(const BasicBlock *BB,
683+
ICmpInst::Predicate Pred, const SCEV *LHS,
684+
const SCEV *RHS);
685+
680686
/// Test whether the backedge of the loop is protected by a conditional
681687
/// between LHS and RHS. This is used to eliminate casts.
682688
bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9492,24 +9492,14 @@ ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
94929492
return false;
94939493
}
94949494

9495-
bool
9496-
ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
9497-
ICmpInst::Predicate Pred,
9498-
const SCEV *LHS, const SCEV *RHS) {
9499-
// Interpret a null as meaning no loop, where there is obviously no guard
9500-
// (interprocedural conditions notwithstanding).
9501-
if (!L) return false;
9502-
9495+
bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB,
9496+
ICmpInst::Predicate Pred,
9497+
const SCEV *LHS,
9498+
const SCEV *RHS) {
95039499
if (VerifyIR)
9504-
assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
9500+
assert(!verifyFunction(*BB->getParent(), &dbgs()) &&
95059501
"This cannot be done on broken IR!");
95069502

9507-
// Both LHS and RHS must be available at loop entry.
9508-
assert(isAvailableAtLoopEntry(LHS, L) &&
9509-
"LHS is not available at Loop Entry");
9510-
assert(isAvailableAtLoopEntry(RHS, L) &&
9511-
"RHS is not available at Loop Entry");
9512-
95139503
if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
95149504
return true;
95159505

@@ -9566,13 +9556,17 @@ ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
95669556
return false;
95679557
};
95689558

9569-
// Starting at the loop predecessor, climb up the predecessor chain, as long
9559+
// Starting at the block's predecessor, climb up the predecessor chain, as long
95709560
// as there are predecessors that can be found that have unique successors
9571-
// leading to the original header.
9572-
for (std::pair<const BasicBlock *, const BasicBlock *> Pair(
9573-
L->getLoopPredecessor(), L->getHeader());
9561+
// leading to the original block.
9562+
const Loop *ContainingLoop = LI.getLoopFor(BB);
9563+
const BasicBlock *PredBB;
9564+
if (ContainingLoop && ContainingLoop->getHeader() == BB)
9565+
PredBB = ContainingLoop->getLoopPredecessor();
9566+
else
9567+
PredBB = BB->getSinglePredecessor();
9568+
for (std::pair<const BasicBlock *, const BasicBlock *> Pair(PredBB, BB);
95749569
Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
9575-
95769570
if (ProveViaGuard(Pair.first))
95779571
return true;
95789572

@@ -9592,7 +9586,7 @@ ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
95929586
if (!AssumeVH)
95939587
continue;
95949588
auto *CI = cast<CallInst>(AssumeVH);
9595-
if (!DT.dominates(CI, L->getHeader()))
9589+
if (!DT.dominates(CI, BB))
95969590
continue;
95979591

95989592
if (ProveViaCond(CI->getArgOperand(0), false))
@@ -9602,6 +9596,23 @@ ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
96029596
return false;
96039597
}
96049598

9599+
bool ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
9600+
ICmpInst::Predicate Pred,
9601+
const SCEV *LHS,
9602+
const SCEV *RHS) {
9603+
// Interpret a null as meaning no loop, where there is obviously no guard
9604+
// (interprocedural conditions notwithstanding).
9605+
if (!L)
9606+
return false;
9607+
9608+
// Both LHS and RHS must be available at loop entry.
9609+
assert(isAvailableAtLoopEntry(LHS, L) &&
9610+
"LHS is not available at Loop Entry");
9611+
assert(isAvailableAtLoopEntry(RHS, L) &&
9612+
"RHS is not available at Loop Entry");
9613+
return isBasicBlockEntryGuardedByCond(L->getHeader(), Pred, LHS, RHS);
9614+
}
9615+
96059616
bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS,
96069617
const SCEV *RHS,
96079618
const Value *FoundCondValue, bool Inverse) {

0 commit comments

Comments
 (0)