[llvm] 9e6625d - [InstCombine] Preserve all gep flags in another select of gep fold

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


Author: Nikita Popov
Date: 2024-06-19T12:18:01+02:00
New Revision: 9e6625d6a25bf5849d381fa8774adfd29ed823fb

URL: https://github.com/llvm/llvm-project/commit/9e6625d6a25bf5849d381fa8774adfd29ed823fb
DIFF: https://github.com/llvm/llvm-project/commit/9e6625d6a25bf5849d381fa8774adfd29ed823fb.diff

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

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/test/Transforms/InstCombine/select-gep.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 66b7aa4b5bda4..fbac209c3da6f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -3712,9 +3712,8 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
       std::swap(NewT, NewF);
     Value *NewSI =
         Builder.CreateSelect(CondVal, NewT, NewF, SI.getName() + ".idx", &SI);
-    if (Gep->isInBounds())
-      return GetElementPtrInst::CreateInBounds(ElementType, Ptr, {NewSI});
-    return GetElementPtrInst::Create(ElementType, Ptr, {NewSI});
+    return GetElementPtrInst::Create(ElementType, Ptr, NewSI,
+                                     Gep->getNoWrapFlags());
   };
   if (auto *TrueGep = dyn_cast<GetElementPtrInst>(TrueVal))
     if (auto *NewGep = SelectGepWithBase(TrueGep, FalseVal, false))

diff  --git a/llvm/test/Transforms/InstCombine/select-gep.ll b/llvm/test/Transforms/InstCombine/select-gep.ll
index 53bbcda85bb22..17304801b1b34 100644
--- a/llvm/test/Transforms/InstCombine/select-gep.ll
+++ b/llvm/test/Transforms/InstCombine/select-gep.ll
@@ -84,6 +84,32 @@ define ptr @test2a(ptr %p, i64 %x, i64 %y) {
   ret ptr %select
 }
 
+define ptr @test2a_nusw(ptr %p, i64 %x, i64 %y) {
+; CHECK-LABEL: @test2a_nusw(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 [[X]], i64 0
+; CHECK-NEXT:    [[SELECT:%.*]] = getelementptr nusw i32, ptr [[P:%.*]], i64 [[SELECT_IDX]]
+; CHECK-NEXT:    ret ptr [[SELECT]]
+;
+  %gep = getelementptr nusw i32, ptr %p, i64 %x
+  %cmp = icmp ugt i64 %x, %y
+  %select = select i1 %cmp, ptr %gep, ptr %p
+  ret ptr %select
+}
+
+define ptr @test2a_nuw(ptr %p, i64 %x, i64 %y) {
+; CHECK-LABEL: @test2a_nuw(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 [[X]], i64 0
+; CHECK-NEXT:    [[SELECT:%.*]] = getelementptr nuw i32, ptr [[P:%.*]], i64 [[SELECT_IDX]]
+; CHECK-NEXT:    ret ptr [[SELECT]]
+;
+  %gep = getelementptr nuw i32, ptr %p, i64 %x
+  %cmp = icmp ugt i64 %x, %y
+  %select = select i1 %cmp, ptr %gep, ptr %p
+  ret ptr %select
+}
+
 ; PR50183
 define ptr @test2b(ptr %p, i64 %x, i64 %y) {
 ; CHECK-LABEL: @test2b(


        


More information about the llvm-commits mailing list