[llvm] 3641d37 - [InstCombine] Handle GEP inbounds in select op replacement (PR47730)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 12:14:41 PDT 2020


Author: Nikita Popov
Date: 2020-10-05T21:13:02+02:00
New Revision: 3641d375f6747237b1b55a25a55e9028d8a67a02

URL: https://github.com/llvm/llvm-project/commit/3641d375f6747237b1b55a25a55e9028d8a67a02
DIFF: https://github.com/llvm/llvm-project/commit/3641d375f6747237b1b55a25a55e9028d8a67a02.diff

LOG: [InstCombine] Handle GEP inbounds in select op replacement (PR47730)

When retrying the "simplify with operand replaced" select
optimization without poison flags, also handle inbounds on GEPs.

Of course, this particular example would also be safe to transform
while keeping inbounds, but the underlying machinery does not
know this (yet).

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 087586ede808..2501c564c3ff 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -1200,7 +1200,7 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
   // InstSimplify already performed this fold if it was possible subject to
   // current poison-generating flags. Try the transform again with
   // poison-generating flags temporarily dropped.
-  bool WasNUW = false, WasNSW = false, WasExact = false;
+  bool WasNUW = false, WasNSW = false, WasExact = false, WasInBounds = false;
   if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(FalseVal)) {
     WasNUW = OBO->hasNoUnsignedWrap();
     WasNSW = OBO->hasNoSignedWrap();
@@ -1211,6 +1211,10 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
     WasExact = PEO->isExact();
     FalseInst->setIsExact(false);
   }
+  if (auto *GEP = dyn_cast<GetElementPtrInst>(FalseVal)) {
+    WasInBounds = GEP->isInBounds();
+    GEP->setIsInBounds(false);
+  }
 
   // Try each equivalence substitution possibility.
   // We have an 'EQ' comparison, so the select's false value will propagate.
@@ -1230,6 +1234,8 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
     FalseInst->setHasNoSignedWrap();
   if (WasExact)
     FalseInst->setIsExact();
+  if (WasInBounds)
+    cast<GetElementPtrInst>(FalseInst)->setIsInBounds();
 
   return nullptr;
 }

diff  --git a/llvm/test/Transforms/InstCombine/select.ll b/llvm/test/Transforms/InstCombine/select.ll
index 326050c5a200..987f34e52ad2 100644
--- a/llvm/test/Transforms/InstCombine/select.ll
+++ b/llvm/test/Transforms/InstCombine/select.ll
@@ -2725,12 +2725,11 @@ define i32 @select_replacement_loop2(i32 %arg, i32 %arg2) {
   ret i32 %sel
 }
 
+; TODO: Dropping the inbounds flag should not be necessary for this fold.
 define i8* @select_replacement_gep_inbounds(i8* %base, i64 %offset) {
 ; CHECK-LABEL: @select_replacement_gep_inbounds(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[OFFSET:%.*]], 0
-; CHECK-NEXT:    [[GEP:%.*]] = getelementptr inbounds i8, i8* [[BASE:%.*]], i64 [[OFFSET]]
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i8* [[BASE]], i8* [[GEP]]
-; CHECK-NEXT:    ret i8* [[SEL]]
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i8, i8* [[BASE:%.*]], i64 [[OFFSET:%.*]]
+; CHECK-NEXT:    ret i8* [[GEP]]
 ;
   %cmp = icmp eq i64 %offset, 0
   %gep = getelementptr inbounds i8, i8* %base, i64 %offset


        


More information about the llvm-commits mailing list