[llvm] 4c8ce5d - [InstCombine] Preserve all flags in select of gep fold

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 19 03:01:56 PDT 2024


Author: Nikita Popov
Date: 2024-06-19T12:01:48+02:00
New Revision: 4c8ce5d3012cc9f16ab8fee5ec444f6036cb099e

URL: https://github.com/llvm/llvm-project/commit/4c8ce5d3012cc9f16ab8fee5ec444f6036cb099e
DIFF: https://github.com/llvm/llvm-project/commit/4c8ce5d3012cc9f16ab8fee5ec444f6036cb099e.diff

LOG: [InstCombine] Preserve all flags in select of gep fold

Preserve the flag intersection.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/test/Transforms/InstCombine/opaque-ptr.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 960c5a29569e3..66b7aa4b5bda4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -486,9 +486,8 @@ Instruction *InstCombinerImpl::foldSelectOpOp(SelectInst &SI, Instruction *TI,
   if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) {
     auto *FGEP = cast<GetElementPtrInst>(FI);
     Type *ElementType = TGEP->getSourceElementType();
-    return TGEP->isInBounds() && FGEP->isInBounds()
-               ? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1})
-               : GetElementPtrInst::Create(ElementType, Op0, {Op1});
+    return GetElementPtrInst::Create(
+        ElementType, Op0, Op1, TGEP->getNoWrapFlags() & FGEP->getNoWrapFlags());
   }
   llvm_unreachable("Expected BinaryOperator or GEP");
   return nullptr;

diff  --git a/llvm/test/Transforms/InstCombine/opaque-ptr.ll b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
index 5cc444042e179..75020343b1d8a 100644
--- a/llvm/test/Transforms/InstCombine/opaque-ptr.ll
+++ b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
@@ -639,6 +639,30 @@ define ptr @select_of_gep(i1 %c, ptr %p) {
   ret ptr %s
 }
 
+define ptr @select_of_gep_flags_1(i1 %c, ptr %p) {
+; CHECK-LABEL: @select_of_gep_flags_1(
+; CHECK-NEXT:    [[S_V:%.*]] = select i1 [[C:%.*]], i64 4, i64 8
+; CHECK-NEXT:    [[S:%.*]] = getelementptr nusw i8, ptr [[P:%.*]], i64 [[S_V]]
+; CHECK-NEXT:    ret ptr [[S]]
+;
+  %gep1 = getelementptr inbounds i32, ptr %p, i64 1
+  %gep2 = getelementptr nusw nuw i32, ptr %p, i64 2
+  %s = select i1 %c, ptr %gep1, ptr %gep2
+  ret ptr %s
+}
+
+define ptr @select_of_gep_flags_2(i1 %c, ptr %p) {
+; CHECK-LABEL: @select_of_gep_flags_2(
+; CHECK-NEXT:    [[S_V:%.*]] = select i1 [[C:%.*]], i64 4, i64 8
+; CHECK-NEXT:    [[S:%.*]] = getelementptr nuw i8, ptr [[P:%.*]], i64 [[S_V]]
+; CHECK-NEXT:    ret ptr [[S]]
+;
+  %gep1 = getelementptr nuw i32, ptr %p, i64 1
+  %gep2 = getelementptr nusw nuw i32, ptr %p, i64 2
+  %s = select i1 %c, ptr %gep1, ptr %gep2
+  ret ptr %s
+}
+
 define ptr @select_of_gep_
diff erent_type(i1 %c, ptr %p) {
 ; CHECK-LABEL: @select_of_gep_
diff erent_type(
 ; CHECK-NEXT:    [[S_V:%.*]] = select i1 [[C:%.*]], i64 4, i64 16


        


More information about the llvm-commits mailing list