[PATCH] D108049: [InstCombine] Extend canonicalizeClampLike to handle truncated inputs

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 15 12:04:56 PDT 2021


dmgreen added a comment.

In D108049#3067063 <https://reviews.llvm.org/D108049#3067063>, @lebedev.ri wrote:

> I notice that all the changed tests perform signed clamping,
> and likewise, you `sext`. Is `sext` always the right choice?
> Please add at least one test with unsigned clamp,
> and an alive proof.

This method wont performs unsigned clamping (depending on what unsigned clamping means). It transforms code of the form:
`select (icmp ult X, C0), add(X, C1), select(icmp slt(X, C2), L, H))`
And always produces signed clamp outputs. So there is an unsigned comparison in there (and a signed one), but they are not interchangable.

I can change either the first icmp to signed, or the second to unsigned, but then the method wont match them and this patch doesn't alter the codegen. I'll add them to the list of tests, but the codegen here won't change

  define i16 @testi32i16i8_s(i32 %add) {
  ; CHECK-LABEL: @testi32i16i8_s(
  ; CHECK-NEXT:    [[A:%.*]] = add i32 [[ADD:%.*]], 128
  ; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[A]], 256
  ; CHECK-NEXT:    [[T:%.*]] = trunc i32 [[ADD]] to i16
  ; CHECK-NEXT:    [[C:%.*]] = icmp sgt i32 [[ADD]], -1
  ; CHECK-NEXT:    [[F:%.*]] = select i1 [[C]], i16 127, i16 -128
  ; CHECK-NEXT:    [[R:%.*]] = select i1 [[CMP]], i16 [[T]], i16 [[F]]
  ; CHECK-NEXT:    ret i16 [[R]]
  ;
    %a = add i32 %add, 128
    %cmp = icmp slt i32 %a, 256
    %t = trunc i32 %add to i16
    %c = icmp sgt i32 %add, -1
    %f = select i1 %c, i16 127, i16 -128
    %r = select i1 %cmp, i16 %t, i16 %f
    ret i16 %r
  }
  
  define i16 @testi32i16i8_u(i32 %add) {
  ; CHECK-LABEL: @testi32i16i8_u(
  ; CHECK-NEXT:    [[A:%.*]] = add i32 [[ADD:%.*]], 128
  ; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i32 [[A]], 256
  ; CHECK-NEXT:    [[T:%.*]] = trunc i32 [[ADD]] to i16
  ; CHECK-NEXT:    [[R:%.*]] = select i1 [[CMP]], i16 [[T]], i16 -128
  ; CHECK-NEXT:    ret i16 [[R]]
  ;
    %a = add i32 %add, 128
    %cmp = icmp ult i32 %a, 256
    %t = trunc i32 %add to i16
    %c = icmp ugt i32 %add, -1
    %f = select i1 %c, i16 127, i16 -128
    %r = select i1 %cmp, i16 %t, i16 %f
    ret i16 %r
  }

This is a proof that includes the sexts: https://alive2.llvm.org/ce/z/Y_Q3yQ


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D108049/new/

https://reviews.llvm.org/D108049



More information about the llvm-commits mailing list