[llvm] 9a456b7 - [IndVars] Forget SCEV for replaced PHI.
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 4 11:42:42 PDT 2022
Author: Florian Hahn
Date: 2022-11-04T18:42:07Z
New Revision: 9a456b7ad3125834377b8aab1598785e8559c224
URL: https://github.com/llvm/llvm-project/commit/9a456b7ad3125834377b8aab1598785e8559c224
DIFF: https://github.com/llvm/llvm-project/commit/9a456b7ad3125834377b8aab1598785e8559c224.diff
LOG: [IndVars] Forget SCEV for replaced PHI.
Additional SCEV verification highlighted a case where the cached loop
dispositions where incorrect after simplifying a phi node in IndVars.
Fix it by invalidating the phi before replacing it.
Fixes #58750
Added:
Modified:
llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index 9efb40f231e40..f6431b77d8027 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -1308,7 +1308,8 @@ static void foldExit(const Loop *L, BasicBlock *ExitingBB, bool IsTaken,
}
static void replaceLoopPHINodesWithPreheaderValues(
- LoopInfo *LI, Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
+ LoopInfo *LI, Loop *L, SmallVectorImpl<WeakTrackingVH> &DeadInsts,
+ ScalarEvolution &SE) {
assert(L->isLoopSimplifyForm() && "Should only do it in simplify form!");
auto *LoopPreheader = L->getLoopPreheader();
auto *LoopHeader = L->getHeader();
@@ -1317,6 +1318,7 @@ static void replaceLoopPHINodesWithPreheaderValues(
auto *PreheaderIncoming = PN.getIncomingValueForBlock(LoopPreheader);
for (User *U : PN.users())
Worklist.push_back(cast<Instruction>(U));
+ SE.forgetValue(&PN);
PN.replaceAllUsesWith(PreheaderIncoming);
DeadInsts.emplace_back(&PN);
}
@@ -1588,7 +1590,7 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) {
// unconditional exit, we can still replace header phis with their
// preheader value.
if (!L->contains(BI->getSuccessor(CI->isNullValue())))
- replaceLoopPHINodesWithPreheaderValues(LI, L, DeadInsts);
+ replaceLoopPHINodesWithPreheaderValues(LI, L, DeadInsts, *SE);
return true;
}
@@ -1675,7 +1677,7 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) {
// the header PHIs with values coming from the preheader.
if (ExitCount->isZero()) {
foldExit(L, ExitingBB, true, DeadInsts);
- replaceLoopPHINodesWithPreheaderValues(LI, L, DeadInsts);
+ replaceLoopPHINodesWithPreheaderValues(LI, L, DeadInsts, *SE);
Changed = true;
continue;
}
diff --git a/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll b/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
index fb748ae4dc494..856fc37620499 100644
--- a/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
+++ b/llvm/test/Transforms/IndVarSimplify/invalidate-modified-lcssa-phi.ll
@@ -156,3 +156,55 @@ exit:
%lcssa = phi i16 [ %sum.next, %loop ]
ret i16 0
}
+
+define i32 @pr58750(i16 %a, ptr %dst, i1 %c.0) {
+; CHECK-LABEL: @pr58750(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP186_NOT:%.*]] = icmp eq i16 [[A:%.*]], 0
+; CHECK-NEXT: call void @llvm.assume(i1 [[CMP186_NOT]])
+; CHECK-NEXT: br label [[OUTER_HEADER:%.*]]
+; CHECK: outer.header:
+; CHECK-NEXT: [[P_0:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[LCSSA:%.*]], [[OUTER_LATCH:%.*]] ]
+; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[P_0]], 0
+; CHECK-NEXT: br label [[INNER:%.*]]
+; CHECK: inner:
+; CHECK-NEXT: store i16 0, ptr [[DST:%.*]], align 1
+; CHECK-NEXT: br i1 false, label [[INNER]], label [[OUTER_LATCH]]
+; CHECK: outer.latch:
+; CHECK-NEXT: [[LCSSA]] = phi i32 [ [[XOR]], [[INNER]] ]
+; CHECK-NEXT: br i1 [[C_0:%.*]], label [[OUTER_HEADER]], label [[EXIT:%.*]]
+; CHECK: exit:
+; CHECK-NEXT: [[LCSSA_LCSSA:%.*]] = phi i32 [ [[LCSSA]], [[OUTER_LATCH]] ]
+; CHECK-NEXT: ret i32 [[LCSSA_LCSSA]]
+;
+entry:
+ %cmp186.not = icmp eq i16 %a, 0
+ call void @llvm.assume(i1 %cmp186.not)
+ br label %outer.header
+
+outer.header:
+ %p.0 = phi i32 [ 0, %entry ], [ %lcssa, %outer.latch ]
+ br label %inner
+
+inner:
+ %inner.iv = phi i16 [ 0, %outer.header ], [ %inner.iv.next, %inner ]
+ %p.1 = phi i32 [ %p.0, %outer.header ], [ %xor, %inner ]
+ store i16 %inner.iv, ptr %dst, align 1
+ %conv = sext i16 %inner.iv to i32
+ %xor = xor i32 %p.1, %conv
+ %inner.iv.next = add nuw i16 %inner.iv, 1
+ %c.1 = icmp ult i16 %inner.iv.next, %a
+ br i1 %c.1, label %inner, label %outer.latch
+
+outer.latch:
+ %lcssa = phi i32 [ %xor, %inner ]
+ br i1 %c.0, label %outer.header, label %exit
+
+exit:
+ ret i32 %lcssa
+}
+
+; Function Attrs: inaccessiblememonly nocallback nofree nosync nounwind willreturn
+declare void @llvm.assume(i1 noundef) #1
+
+
More information about the llvm-commits
mailing list