[PATCH] D87188: [InstCombine] Canonicalize SPF to abs intrinc

Roman Lebedev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 10 04:37:34 PST 2020


lebedev.ri commandeered this revision.
lebedev.ri edited reviewers, added: nikic; removed: lebedev.ri.
lebedev.ri added a comment.

@mstorsjo please can you be more specific what kind of tests are starting to fail?
Do those tests just check that the code compiled into an identical assembly?

I reduced `vc1_block-aarch64.c`, and got:

  ; ModuleID = 'input.ll'
  source_filename = "vc1_block-aarch64.c"
  target datalayout = "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128"
  target triple = "aarch64-w64-windows-gnu"
  
  define i1 @barney(i8 %arg, i8 %arg9) {
  bb:
    %tmp = sext i8 %arg to i32
    %tmp10 = icmp slt i32 %tmp, 0
    %tmp11 = sub nsw i32 0, %tmp
    %tmp12 = select i1 %tmp10, i32 %tmp11, i32 %tmp
    %tmp13 = shl nuw nsw i32 %tmp12, 1
    %tmp14 = zext i8 %arg9 to i32
    %tmp15 = add nuw nsw i32 %tmp13, %tmp14
    %tmp16 = icmp slt i32 %tmp15, 2
    ret i1 %tmp16
  }
  
  declare i32 @eggs()

which used to get optimized into

  ; ModuleID = '<stdin>'
  source_filename = "vc1_block-aarch64.c"
  target datalayout = "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128"
  target triple = "aarch64-w64-windows-gnu"
  
  define i1 @barney(i8 %arg, i8 %arg9) {
  bb:
    %tmp = sext i8 %arg to i32
    %tmp10 = icmp slt i32 %tmp, 0
    %tmp11 = sub nsw i32 0, %tmp
    %tmp12 = select i1 %tmp10, i32 %tmp11, i32 %tmp
    %tmp13 = shl nuw nsw i32 %tmp12, 1
    %tmp14 = zext i8 %arg9 to i32
    %tmp15 = add nuw nsw i32 %tmp13, %tmp14
    %tmp16 = icmp slt i32 %tmp15, 2
    ret i1 %tmp16
  }
  
  declare i32 @eggs()

and is now optimized into

  ; ModuleID = '<stdin>'
  source_filename = "vc1_block-aarch64.c"
  target datalayout = "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128"
  target triple = "aarch64-w64-windows-gnu"
  
  define i1 @barney(i8 %arg, i8 %arg9) {
  bb:
    %tmp = sext i8 %arg to i32
    %0 = call i32 @llvm.abs.i32(i32 %tmp, i1 true)
    %tmp13 = shl nuw nsw i32 %0, 1
    %tmp14 = zext i8 %arg9 to i32
    %tmp15 = add nuw nsw i32 %tmp13, %tmp14
    %tmp16 = icmp ult i32 %tmp15, 2
    ret i1 %tmp16
  }
  
  declare i32 @eggs()
  
  ; Function Attrs: nofree nosync nounwind readnone speculatable willreturn
  declare i32 @llvm.abs.i32(i32, i1 immarg) #0
  
  attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

which is not a miscompile:

  $ /repositories/alive2/build-Clang-release/alive-tv old.ll new.ll
  
  ----------------------------------------
  define i1 @barney(i8 %arg, i8 %arg9) {
  %bb:
    %tmp = sext i8 %arg to i32
    %tmp10 = icmp slt i32 %tmp, 0
    %tmp11 = sub nsw i32 0, %tmp
    %tmp12 = select i1 %tmp10, i32 %tmp11, i32 %tmp
    %tmp13 = shl nsw nuw i32 %tmp12, 1
    %tmp14 = zext i8 %arg9 to i32
    %tmp15 = add nsw nuw i32 %tmp13, %tmp14
    %tmp16 = icmp slt i32 %tmp15, 2
    ret i1 %tmp16
  }
  =>
  define i1 @barney(i8 %arg, i8 %arg9) {
  %bb:
    %tmp = sext i8 %arg to i32
    %0 = abs i32 %tmp, 1
    %tmp13 = shl nsw nuw i32 %0, 1
    %tmp14 = zext i8 %arg9 to i32
    %tmp15 = add nsw nuw i32 %tmp13, %tmp14
    %tmp16 = icmp ult i32 %tmp15, 2
    ret i1 %tmp16
  }
  Transformation seems to be correct!
  
  Summary:
    1 correct transformations
    0 incorrect transformations
    0 Alive2 errors

The original assembly was:

          .text
          .file   "vc1_block-aarch64.c"
          .def     barney;
          .scl    2;
          .type   32;
          .endef
          .globl  barney                          // -- Begin function barney
          .p2align        2
  barney:                                 // @barney
  // %bb.0:                               // %bb
          sxtb    w8, w0
          cmp     w8, #0                          // =0
          cneg    w8, w8, mi
          lsl     w8, w8, #1
          add     w8, w8, w1, uxtb
          cmp     w8, #2                          // =2
          cset    w0, lt
          ret
                                          // -- End function

and new one is

          .text
          .file   "vc1_block-aarch64.c"
          .def     barney;
          .scl    2;
          .type   32;
          .endef
          .globl  barney                          // -- Begin function barney
          .p2align        2
  barney:                                 // @barney
  // %bb.0:                               // %bb
          sxtb    w8, w0
          cmp     w8, #0                          // =0
          cneg    w8, w8, mi
          lsl     w8, w8, #1
          add     w8, w8, w1, uxtb
          cmp     w8, #2                          // =2
          cset    w0, lo
          ret
                                          // -- End function

with diff being

  $ diff old.s new.s 
  17c17
  <       cset    w0, lt
  ---
  >       cset    w0, lo


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

https://reviews.llvm.org/D87188



More information about the cfe-commits mailing list