[llvm-branch-commits] [llvm] release/19.x: backport PR115901 (PR #116104)
Antonio Frighetto via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 13 12:50:28 PST 2024
https://github.com/antoniofrighetto created https://github.com/llvm/llvm-project/pull/116104
Backport: 929cbe7f596733f85cd274485acc19442dd34a80.
Requested-by: @AreaZR.
>From 134b1917d27e268d8771f76f22d2ee32fbc2a2b3 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Tue, 12 Nov 2024 10:45:46 +0100
Subject: [PATCH] [InstCombine] Intersect nowrap flags between geps while
folding into phi
A miscompilation issue has been addressed with refined checking.
---
.../Transforms/InstCombine/InstCombinePHI.cpp | 3 +-
.../test/Transforms/InstCombine/opaque-ptr.ll | 2 +-
llvm/test/Transforms/InstCombine/phi.ll | 28 +++++++++++++++++++
3 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index 86411320ab2487..b05a33c688890d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -513,7 +513,8 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
// especially bad when the PHIs are in the header of a loop.
bool NeededPhi = false;
- GEPNoWrapFlags NW = GEPNoWrapFlags::all();
+ // Remember flags of the first phi-operand getelementptr.
+ GEPNoWrapFlags NW = FirstInst->getNoWrapFlags();
// Scan to see if all operands are the same opcode, and all have one user.
for (Value *V : drop_begin(PN.incoming_values())) {
diff --git a/llvm/test/Transforms/InstCombine/opaque-ptr.ll b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
index df85547f56d74f..1fd8281b53816f 100644
--- a/llvm/test/Transforms/InstCombine/opaque-ptr.ll
+++ b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
@@ -549,7 +549,7 @@ define ptr @phi_of_gep_flags_1(i1 %c, ptr %p) {
; CHECK: else:
; CHECK-NEXT: br label [[JOIN]]
; CHECK: join:
-; CHECK-NEXT: [[PHI:%.*]] = getelementptr nusw nuw i8, ptr [[P:%.*]], i64 4
+; CHECK-NEXT: [[PHI:%.*]] = getelementptr nusw i8, ptr [[P:%.*]], i64 4
; CHECK-NEXT: ret ptr [[PHI]]
;
br i1 %c, label %if, label %else
diff --git a/llvm/test/Transforms/InstCombine/phi.ll b/llvm/test/Transforms/InstCombine/phi.ll
index b12982dd27e404..82ea9bb439b0bb 100644
--- a/llvm/test/Transforms/InstCombine/phi.ll
+++ b/llvm/test/Transforms/InstCombine/phi.ll
@@ -2714,3 +2714,31 @@ join:
%cmp = icmp slt i32 %13, 0
ret i1 %cmp
}
+
+define i64 @wrong_gep_arg_into_phi(ptr noundef %ptr) {
+; CHECK-LABEL: @wrong_gep_arg_into_phi(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[FOR_COND:%.*]]
+; CHECK: for.cond:
+; CHECK-NEXT: [[PTR_PN:%.*]] = phi ptr [ [[PTR:%.*]], [[ENTRY:%.*]] ], [ [[DOTPN:%.*]], [[FOR_COND]] ]
+; CHECK-NEXT: [[DOTPN]] = getelementptr i8, ptr [[PTR_PN]], i64 1
+; CHECK-NEXT: [[VAL:%.*]] = load i8, ptr [[DOTPN]], align 1
+; CHECK-NEXT: [[COND_NOT:%.*]] = icmp eq i8 [[VAL]], 0
+; CHECK-NEXT: br i1 [[COND_NOT]], label [[EXIT:%.*]], label [[FOR_COND]]
+; CHECK: exit:
+; CHECK-NEXT: ret i64 0
+;
+entry:
+ %add.ptr = getelementptr i8, ptr %ptr, i64 1
+ br label %for.cond
+
+for.cond: ; preds = %for.cond, %entry
+ %.pn = phi ptr [ %add.ptr, %entry ], [ %incdec.ptr, %for.cond ]
+ %val = load i8, ptr %.pn, align 1
+ %cond = icmp ne i8 %val, 0
+ %incdec.ptr = getelementptr inbounds nuw i8, ptr %.pn, i64 1
+ br i1 %cond, label %for.cond, label %exit
+
+exit: ; preds = %for.cond
+ ret i64 0
+}
More information about the llvm-branch-commits
mailing list