[llvm] r301880 - [InstSimplify] Handle selects of GEPs with 0 offset

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 23 17:50:25 PDT 2018


Thank you George!

On Mon, Apr 23, 2018 at 5:30 PM, George Burgess IV
<george.burgess.iv at gmail.com> wrote:
> (+davide -- fyi)
>
> Relanded in r330667
>
> On Mon, May 1, 2017 at 5:08 PM, George Burgess IV
> <george.burgess.iv at gmail.com> wrote:
>>
>> reverted by r301885
>>
>> On Mon, May 1, 2017 at 4:12 PM, George Burgess IV via llvm-commits
>> <llvm-commits at lists.llvm.org> wrote:
>>>
>>> Author: gbiv
>>> Date: Mon May  1 18:12:08 2017
>>> New Revision: 301880
>>>
>>> URL: http://llvm.org/viewvc/llvm-project?rev=301880&view=rev
>>> Log:
>>> [InstSimplify] Handle selects of GEPs with 0 offset
>>>
>>> In particular (since it wouldn't fit nicely in the summary):
>>> (select (icmp eq V 0) P (getelementptr P V)) -> (getelementptr P V)
>>>
>>> Differential Revision: https://reviews.llvm.org/D31435
>>>
>>> Modified:
>>>     llvm/trunk/lib/Analysis/InstructionSimplify.cpp
>>>     llvm/trunk/test/Transforms/InstSimplify/select.ll
>>>
>>> Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=301880&r1=301879&r2=301880&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
>>> +++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Mon May  1 18:12:08
>>> 2017
>>> @@ -62,6 +62,8 @@ static Value *SimplifyOrInst(Value *, Va
>>>  static Value *SimplifyXorInst(Value *, Value *, const SimplifyQuery &,
>>> unsigned);
>>>  static Value *SimplifyCastInst(unsigned, Value *, Type *,
>>>                                 const SimplifyQuery &, unsigned);
>>> +static Value *SimplifyGEPInst(Type *, ArrayRef<Value *>, const
>>> SimplifyQuery &,
>>> +                              unsigned);
>>>
>>>  /// For a boolean type or a vector of boolean type, return false or a
>>> vector
>>>  /// with every element false.
>>> @@ -3491,6 +3493,17 @@ static const Value *SimplifyWithOpReplac
>>>      }
>>>    }
>>>
>>> +  // Same for GEPs.
>>> +  if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
>>> +    if (MaxRecurse) {
>>> +      SmallVector<Value *, 8> NewOps(GEP->getNumOperands());
>>> +      transform(GEP->operands(), NewOps.begin(),
>>> +                [&](Value *V) { return V == Op ? RepOp : V; });
>>> +      return SimplifyGEPInst(GEP->getSourceElementType(), NewOps, Q,
>>> +                             MaxRecurse - 1);
>>> +    }
>>> +  }
>>> +
>>>    // TODO: We could hand off more cases to instsimplify here.
>>>
>>>    // If all operands are constant after substituting Op for RepOp then
>>> we can
>>>
>>> Modified: llvm/trunk/test/Transforms/InstSimplify/select.ll
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/select.ll?rev=301880&r1=301879&r2=301880&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/test/Transforms/InstSimplify/select.ll (original)
>>> +++ llvm/trunk/test/Transforms/InstSimplify/select.ll Mon May  1 18:12:08
>>> 2017
>>> @@ -431,3 +431,22 @@ define i8 @do_not_assume_sel_cond(i1 %co
>>>    ret i8 %sel
>>>  }
>>>
>>> +define i32* @select_icmp_eq_0_gep_operand(i32* %base, i64 %n) {
>>> +; CHECK-LABEL: @select_icmp_eq_0_gep_operand(
>>> +; CHECK-NEXT: [[GEP:%.*]] = getelementptr
>>> +; CHECK-NEXT: ret i32* [[GEP]]
>>> +  %cond = icmp eq i64 %n, 0
>>> +  %gep = getelementptr i32, i32* %base, i64 %n
>>> +  %r = select i1 %cond, i32* %base, i32* %gep
>>> +  ret i32* %r
>>> +}
>>> +
>>> +define i32* @select_icmp_ne_0_gep_operand(i32* %base, i64 %n) {
>>> +; CHECK-LABEL: @select_icmp_ne_0_gep_operand(
>>> +; CHECK-NEXT: [[GEP:%.*]] = getelementptr
>>> +; CHECK-NEXT: ret i32* [[GEP]]
>>> +  %cond = icmp ne i64 %n, 0
>>> +  %gep = getelementptr i32, i32* %base, i64 %n
>>> +  %r = select i1 %cond, i32* %gep, i32* %base
>>> +  ret i32* %r
>>> +}
>>>
>>>
>>> _______________________________________________
>>> llvm-commits mailing list
>>> llvm-commits at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>>
>



-- 
Davide

"There are no solved problems; there are only problems that are more
or less solved" -- Henri Poincare

On Mon, Apr 23, 2018 at 5:30 PM, George Burgess IV
<george.burgess.iv at gmail.com> wrote:
> (+davide -- fyi)
>
> Relanded in r330667
>
> On Mon, May 1, 2017 at 5:08 PM, George Burgess IV
> <george.burgess.iv at gmail.com> wrote:
>>
>> reverted by r301885
>>
>> On Mon, May 1, 2017 at 4:12 PM, George Burgess IV via llvm-commits
>> <llvm-commits at lists.llvm.org> wrote:
>>>
>>> Author: gbiv
>>> Date: Mon May  1 18:12:08 2017
>>> New Revision: 301880
>>>
>>> URL: http://llvm.org/viewvc/llvm-project?rev=301880&view=rev
>>> Log:
>>> [InstSimplify] Handle selects of GEPs with 0 offset
>>>
>>> In particular (since it wouldn't fit nicely in the summary):
>>> (select (icmp eq V 0) P (getelementptr P V)) -> (getelementptr P V)
>>>
>>> Differential Revision: https://reviews.llvm.org/D31435
>>>
>>> Modified:
>>>     llvm/trunk/lib/Analysis/InstructionSimplify.cpp
>>>     llvm/trunk/test/Transforms/InstSimplify/select.ll
>>>
>>> Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=301880&r1=301879&r2=301880&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
>>> +++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Mon May  1 18:12:08
>>> 2017
>>> @@ -62,6 +62,8 @@ static Value *SimplifyOrInst(Value *, Va
>>>  static Value *SimplifyXorInst(Value *, Value *, const SimplifyQuery &,
>>> unsigned);
>>>  static Value *SimplifyCastInst(unsigned, Value *, Type *,
>>>                                 const SimplifyQuery &, unsigned);
>>> +static Value *SimplifyGEPInst(Type *, ArrayRef<Value *>, const
>>> SimplifyQuery &,
>>> +                              unsigned);
>>>
>>>  /// For a boolean type or a vector of boolean type, return false or a
>>> vector
>>>  /// with every element false.
>>> @@ -3491,6 +3493,17 @@ static const Value *SimplifyWithOpReplac
>>>      }
>>>    }
>>>
>>> +  // Same for GEPs.
>>> +  if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
>>> +    if (MaxRecurse) {
>>> +      SmallVector<Value *, 8> NewOps(GEP->getNumOperands());
>>> +      transform(GEP->operands(), NewOps.begin(),
>>> +                [&](Value *V) { return V == Op ? RepOp : V; });
>>> +      return SimplifyGEPInst(GEP->getSourceElementType(), NewOps, Q,
>>> +                             MaxRecurse - 1);
>>> +    }
>>> +  }
>>> +
>>>    // TODO: We could hand off more cases to instsimplify here.
>>>
>>>    // If all operands are constant after substituting Op for RepOp then
>>> we can
>>>
>>> Modified: llvm/trunk/test/Transforms/InstSimplify/select.ll
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/select.ll?rev=301880&r1=301879&r2=301880&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/test/Transforms/InstSimplify/select.ll (original)
>>> +++ llvm/trunk/test/Transforms/InstSimplify/select.ll Mon May  1 18:12:08
>>> 2017
>>> @@ -431,3 +431,22 @@ define i8 @do_not_assume_sel_cond(i1 %co
>>>    ret i8 %sel
>>>  }
>>>
>>> +define i32* @select_icmp_eq_0_gep_operand(i32* %base, i64 %n) {
>>> +; CHECK-LABEL: @select_icmp_eq_0_gep_operand(
>>> +; CHECK-NEXT: [[GEP:%.*]] = getelementptr
>>> +; CHECK-NEXT: ret i32* [[GEP]]
>>> +  %cond = icmp eq i64 %n, 0
>>> +  %gep = getelementptr i32, i32* %base, i64 %n
>>> +  %r = select i1 %cond, i32* %base, i32* %gep
>>> +  ret i32* %r
>>> +}
>>> +
>>> +define i32* @select_icmp_ne_0_gep_operand(i32* %base, i64 %n) {
>>> +; CHECK-LABEL: @select_icmp_ne_0_gep_operand(
>>> +; CHECK-NEXT: [[GEP:%.*]] = getelementptr
>>> +; CHECK-NEXT: ret i32* [[GEP]]
>>> +  %cond = icmp ne i64 %n, 0
>>> +  %gep = getelementptr i32, i32* %base, i64 %n
>>> +  %r = select i1 %cond, i32* %gep, i32* %base
>>> +  ret i32* %r
>>> +}
>>>
>>>
>>> _______________________________________________
>>> llvm-commits mailing list
>>> llvm-commits at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>>
>



-- 
Davide

"A mathematical theory is not to be considered complete until you have
made it so clear that you can explain it to the first man whom you
meet on the street." (D. Hilbert)


More information about the llvm-commits mailing list