[llvm] fd23677 - [IndVars] Forget SCEV for value after simplifying condition.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 21 03:18:18 PDT 2022
Author: Florian Hahn
Date: 2022-10-21T11:18:01+01:00
New Revision: fd236772f5665ce9e55f85ac992be6c22a918d3e
URL: https://github.com/llvm/llvm-project/commit/fd236772f5665ce9e55f85ac992be6c22a918d3e
DIFF: https://github.com/llvm/llvm-project/commit/fd236772f5665ce9e55f85ac992be6c22a918d3e.diff
LOG: [IndVars] Forget SCEV for value after simplifying condition.
Additional SCEV verification highlighted a case where the cached loop
dispositions where incorrect after simplifying a condition in IndVars
and moving the user in LoopDeletion. Fix it by invalidating ICmp and all
its users.
Fixes #58515.
Added:
Modified:
llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index 11efcae029579..9e23fa9af786b 100644
--- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -289,6 +289,7 @@ void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp,
Users.push_back(cast<Instruction>(U));
const Instruction *CtxI = findCommonDominator(Users, *DT);
if (auto Ev = SE->evaluatePredicateAt(Pred, S, X, CtxI)) {
+ SE->forgetValue(ICmp);
ICmp->replaceAllUsesWith(ConstantInt::getBool(ICmp->getContext(), *Ev));
DeadInsts.emplace_back(ICmp);
LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
diff --git a/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll b/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
index 265a985d0f7fc..fb748ae4dc494 100644
--- a/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
+++ b/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
@@ -120,3 +120,39 @@ exit:
%lcssa = phi i32 [ %0, %loop.2.latch ]
ret i32 %lcssa
}
+
+
+define i16 @test_pr58515_invalidate_loop_disposition(ptr %a) {
+; CHECK-LABEL: @test_pr58515_invalidate_loop_disposition(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[SEL:%.*]] = select i1 true, i16 2, i16 0
+; CHECK-NEXT: br label [[LOOP:%.*]]
+; CHECK: loop:
+; CHECK-NEXT: [[IV:%.*]] = phi i16 [ 1, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
+; CHECK-NEXT: [[SUM:%.*]] = phi i16 [ 0, [[ENTRY]] ], [ [[SUM_NEXT:%.*]], [[LOOP]] ]
+; CHECK-NEXT: [[SUM_NEXT]] = add i16 [[SEL]], [[SUM]]
+; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i16 [[IV]], 1
+; CHECK-NEXT: [[C_2:%.*]] = icmp ult i16 [[IV]], 9
+; CHECK-NEXT: br i1 [[C_2]], label [[LOOP]], label [[EXIT:%.*]]
+; CHECK: exit:
+; CHECK-NEXT: [[LCSSA:%.*]] = phi i16 [ [[SUM_NEXT]], [[LOOP]] ]
+; CHECK-NEXT: ret i16 0
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i16 [ 1, %entry ], [ %iv.next, %loop ]
+ %sum = phi i16 [ 0, %entry ], [ %sum.next, %loop ]
+ %gep = getelementptr inbounds i16, ptr %a, i16 %iv
+ %c.1 = icmp ne ptr %a, %gep
+ %sel = select i1 %c.1, i16 2, i16 0
+ %sum.next = add i16 %sel, %sum
+ %iv.next = add nuw nsw i16 %iv, 1
+ %c.2 = icmp ult i16 %iv, 9
+ br i1 %c.2, label %loop, label %exit
+
+exit:
+ %lcssa = phi i16 [ %sum.next, %loop ]
+ ret i16 0
+}
More information about the llvm-commits
mailing list