[llvm] [InstCombine] Fix GEPNoWrapFlags propagation in `foldGEPOfPhi` (PR #121572)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 3 05:43:34 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Yingwei Zheng (dtcxzyw)
<details>
<summary>Changes</summary>
Closes https://github.com/llvm/llvm-project/issues/121459.
---
Full diff: https://github.com/llvm/llvm-project/pull/121572.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+5)
- (modified) llvm/test/Transforms/InstCombine/opaque-ptr.ll (+58)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 934156f04f7fdd..f63de1f0d410e2 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2782,6 +2782,7 @@ static Instruction *foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN,
// loop iteration).
if (Op1 == &GEP)
return nullptr;
+ GEPNoWrapFlags NW = Op1->getNoWrapFlags();
int DI = -1;
@@ -2838,6 +2839,8 @@ static Instruction *foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN,
}
}
}
+
+ NW &= Op2->getNoWrapFlags();
}
// If not all GEPs are identical we'll have to create a new PHI node.
@@ -2847,6 +2850,8 @@ static Instruction *foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN,
return nullptr;
auto *NewGEP = cast<GetElementPtrInst>(Op1->clone());
+ NewGEP->setNoWrapFlags(NW);
+
if (DI == -1) {
// All the GEPs feeding the PHI are identical. Clone one down into our
// BB so that it can be merged with the current GEP.
diff --git a/llvm/test/Transforms/InstCombine/opaque-ptr.ll b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
index bac51c82f36dda..b05274658e812e 100644
--- a/llvm/test/Transforms/InstCombine/opaque-ptr.ll
+++ b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
@@ -654,6 +654,64 @@ join:
ret ptr %gep
}
+define ptr @gep_of_phi_of_gep_flags1(i1 %c, ptr %p) {
+; CHECK-LABEL: @gep_of_phi_of_gep_flags1(
+; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
+; CHECK: if:
+; CHECK-NEXT: br label [[JOIN:%.*]]
+; CHECK: else:
+; CHECK-NEXT: br label [[JOIN]]
+; CHECK: join:
+; CHECK-NEXT: [[TMP1:%.*]] = phi i64 [ 4, [[IF]] ], [ 8, [[ELSE]] ]
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[TMP1]]
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[TMP2]], i64 4
+; CHECK-NEXT: ret ptr [[GEP]]
+;
+ br i1 %c, label %if, label %else
+
+if:
+ %gep1 = getelementptr inbounds i32, ptr %p, i64 1
+ br label %join
+
+else:
+ %gep2 = getelementptr i32, ptr %p, i64 2
+ br label %join
+
+join:
+ %phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]
+ %gep = getelementptr i32, ptr %phi, i64 1
+ ret ptr %gep
+}
+
+define ptr @gep_of_phi_of_gep_flags2(i1 %c, ptr %p) {
+; CHECK-LABEL: @gep_of_phi_of_gep_flags2(
+; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
+; CHECK: if:
+; CHECK-NEXT: br label [[JOIN:%.*]]
+; CHECK: else:
+; CHECK-NEXT: br label [[JOIN]]
+; CHECK: join:
+; CHECK-NEXT: [[TMP1:%.*]] = phi i64 [ 4, [[IF]] ], [ 8, [[ELSE]] ]
+; CHECK-NEXT: [[TMP2:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 [[TMP1]]
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[TMP2]], i64 4
+; CHECK-NEXT: ret ptr [[GEP]]
+;
+ br i1 %c, label %if, label %else
+
+if:
+ %gep1 = getelementptr nuw i32, ptr %p, i64 1
+ br label %join
+
+else:
+ %gep2 = getelementptr nuw i32, ptr %p, i64 2
+ br label %join
+
+join:
+ %phi = phi ptr [ %gep1, %if ], [ %gep2, %else ]
+ %gep = getelementptr i32, ptr %phi, i64 1
+ ret ptr %gep
+}
+
define ptr @gep_of_phi_of_gep_different_type(i1 %c, ptr %p) {
; CHECK-LABEL: @gep_of_phi_of_gep_different_type(
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
``````````
</details>
https://github.com/llvm/llvm-project/pull/121572
More information about the llvm-commits
mailing list