Skip to content

Commit 65cb9d7

Browse files
author
Max Kazantsev
committed
[SCEV][NFC] Verify IR in isLoop[Entry,Backedge]GuardedByCond
We have a lot of various bugs that are caused by misuse of SCEV (in particular in LV), all of them can simply be described as "we ask SCEV to prove some fact on invalid IR". Some of examples of those are PR36311, PR37221, PR39160. The problem is that these failues manifest differently (what we saw was failure of various asserts across SCEV, but there can also be miscompiles). This patch adds an assert into two SCEV methods that strongly rely on correctness of the IR and are involved in known failues. This will at least allow us to have a clear indication of what was wrong in this case. This patch also fixes a unit test with incorrect IR that fails this verification. Differential Revision: https://reviews.llvm.org/D52930 Reviewed By: fhahn llvm-svn: 346389
1 parent ac067b9 commit 65cb9d7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
#include "llvm/IR/Use.h"
113113
#include "llvm/IR/User.h"
114114
#include "llvm/IR/Value.h"
115+
#include "llvm/IR/Verifier.h"
115116
#include "llvm/Pass.h"
116117
#include "llvm/Support/Casting.h"
117118
#include "llvm/Support/CommandLine.h"
@@ -162,6 +163,11 @@ static cl::opt<bool>
162163
cl::desc("Verify no dangling value in ScalarEvolution's "
163164
"ExprValueMap (slow)"));
164165

166+
static cl::opt<bool> VerifyIR(
167+
"scev-verify-ir", cl::Hidden,
168+
cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"),
169+
cl::init(false));
170+
165171
static cl::opt<unsigned> MulOpsInlineThreshold(
166172
"scev-mulops-inline-threshold", cl::Hidden,
167173
cl::desc("Threshold for inlining multiplication operands into a SCEV"),
@@ -9370,6 +9376,11 @@ ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
93709376
// (interprocedural conditions notwithstanding).
93719377
if (!L) return true;
93729378

9379+
if (VerifyIR)
9380+
assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
9381+
"This cannot be done on broken IR!");
9382+
9383+
93739384
if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
93749385
return true;
93759386

@@ -9475,6 +9486,10 @@ ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
94759486
// (interprocedural conditions notwithstanding).
94769487
if (!L) return false;
94779488

9489+
if (VerifyIR)
9490+
assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
9491+
"This cannot be done on broken IR!");
9492+
94789493
// Both LHS and RHS must be available at loop entry.
94799494
assert(isAvailableAtLoopEntry(LHS, L) &&
94809495
"LHS is not available at Loop Entry");

llvm/unittests/Analysis/ScalarEvolutionTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ TEST_F(ScalarEvolutionsTest, SCEVZeroExtendExpr) {
701701
PN->addIncoming(Dec, IncBB);
702702
BranchInst::Create(CondBB, IncBB);
703703

704-
Accum = GetElementPtrInst::Create(I8Ty, Accum, Dec, "gep", EndBB);
704+
Accum = GetElementPtrInst::Create(I8Ty, Accum, PN, "gep", EndBB);
705705

706706
PrevBB = CondBB;
707707
CondBB = NextBB;

0 commit comments

Comments
 (0)