Skip to content

Commit

Permalink
Revert "[SCEV] Don't invalidate past dependency-breaking instructions"
Browse files Browse the repository at this point in the history
Unforuntately, the assumption underlying this optimization is
incorrect for getSCEVAtScope(): A SCEVUnknown instruction with
operands that have constant loop exit values can evaluate to
a constant, thus creating a dependency from an "always unknown"
instruction.

Losing this optimization is quite unfortunate, but it doesn't
seem like there is any simple workaround for this.

Fixes #68260.

This reverts commit 3ddd1ff.
  • Loading branch information
nikic committed Oct 9, 2023
1 parent 2a2b426 commit 1c3fdb3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 21 deletions.
20 changes: 1 addition & 19 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4494,30 +4494,14 @@ void ScalarEvolution::insertValueToMap(Value *V, const SCEV *S) {
}
}

/// Determine whether this instruction is either not SCEVable or will always
/// produce a SCEVUnknown. We do not have to walk past such instructions when
/// invalidating.
static bool isAlwaysUnknown(const Instruction *I) {
switch (I->getOpcode()) {
case Instruction::Load:
return true;
default:
return false;
}
}

/// Return an existing SCEV if it exists, otherwise analyze the expression and
/// create a new one.
const SCEV *ScalarEvolution::getSCEV(Value *V) {
assert(isSCEVable(V->getType()) && "Value is not SCEVable!");

if (const SCEV *S = getExistingSCEV(V))
return S;
const SCEV *S = createSCEVIter(V);
assert((!isa<Instruction>(V) || !isAlwaysUnknown(cast<Instruction>(V)) ||
isa<SCEVUnknown>(S)) &&
"isAlwaysUnknown() instruction is not SCEVUnknown");
return S;
return createSCEVIter(V);
}

const SCEV *ScalarEvolution::getExistingSCEV(Value *V) {
Expand Down Expand Up @@ -4818,8 +4802,6 @@ static void PushDefUseChildren(Instruction *I,
// Push the def-use children onto the Worklist stack.
for (User *U : I->users()) {
auto *UserInsn = cast<Instruction>(U);
if (isAlwaysUnknown(UserInsn))
continue;
if (Visited.insert(UserInsn).second)
Worklist.push_back(UserInsn);
}
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/IndVarSimplify/pr68260.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

declare void @use(i32)

; FIXME: This is a miscompile.
define void @test() {
; CHECK-LABEL: define void @test() {
; CHECK-NEXT: entry:
Expand All @@ -14,7 +13,7 @@ define void @test() {
; CHECK: loop2:
; CHECK-NEXT: br i1 false, label [[LOOP2]], label [[LOOP_LATCH:%.*]], !llvm.loop [[LOOP0:![0-9]+]]
; CHECK: loop.latch:
; CHECK-NEXT: call void @use(i32 4)
; CHECK-NEXT: call void @use(i32 3)
; CHECK-NEXT: br label [[LOOP2_1:%.*]]
; CHECK: loop2.1:
; CHECK-NEXT: br i1 false, label [[LOOP2_1]], label [[LOOP_LATCH_1:%.*]], !llvm.loop [[LOOP0]]
Expand Down

0 comments on commit 1c3fdb3

Please sign in to comment.