[InstCombine] Strip inbounds from (select C, (gep Ptr, Idx), Ptr) -> (gep Ptr, (selec...
authorSimon Pilgrim <llvm-dev@redking.me.uk>
Thu, 15 Jul 2021 11:19:10 +0000 (12:19 +0100)
committerSimon Pilgrim <llvm-dev@redking.me.uk>
Thu, 15 Jul 2021 11:19:10 +0000 (12:19 +0100)
As discussed on rGd561b6fbdbe6, we can't guarantee that the new gep is inbounds

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

index fb9750d..7bd94af 100644 (file)
@@ -2949,9 +2949,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
       std::swap(NewT, NewF);
     Value *NewSI =
         Builder.CreateSelect(CondVal, NewT, NewF, SI.getName() + ".idx", &SI);
-    return Gep->isInBounds()
-               ? GetElementPtrInst::CreateInBounds(ElementType, Ptr, {NewSI})
-               : GetElementPtrInst::Create(ElementType, Ptr, {NewSI});
+    return GetElementPtrInst::Create(ElementType, Ptr, {NewSI});
   };
   if (auto *TrueGep = dyn_cast<GetElementPtrInst>(TrueVal))
     if (auto *NewGep = SelectGepWithBase(TrueGep, FalseVal, false))
index bba6709..45bc1da 100644 (file)
@@ -76,7 +76,7 @@ define i32* @test2a(i32* %p, i64 %x, i64 %y) {
 ; CHECK-LABEL: @test2a(
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 [[X]], i64 0
-; CHECK-NEXT:    [[SELECT:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[SELECT_IDX]]
+; CHECK-NEXT:    [[SELECT:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 [[SELECT_IDX]]
 ; CHECK-NEXT:    ret i32* [[SELECT]]
 ;
   %gep = getelementptr inbounds i32, i32* %p, i64 %x
@@ -90,7 +90,7 @@ define i32* @test2b(i32* %p, i64 %x, i64 %y) {
 ; CHECK-LABEL: @test2b(
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i64 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    [[SELECT_IDX:%.*]] = select i1 [[CMP]], i64 0, i64 [[X]]
-; CHECK-NEXT:    [[SELECT:%.*]] = getelementptr inbounds i32, i32* [[P:%.*]], i64 [[SELECT_IDX]]
+; CHECK-NEXT:    [[SELECT:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 [[SELECT_IDX]]
 ; CHECK-NEXT:    ret i32* [[SELECT]]
 ;
   %gep = getelementptr inbounds i32, i32* %p, i64 %x