[PATCH] D148414: [InstCombine] Expand `foldSelectICmpAndOr` -> `foldSelectICmpAndBinOp` to work for more binops

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 25 10:44:29 PDT 2023


goldstein.w.n added a comment.

Seems overkill for adding tests for each binop, but ran them offline and all transform verify with alive2:

  ; $> /home/noah/programs/opensource/llvm-dev/src/alive2/build/alive-tv (-smt-to=200000000)
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_add_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %badd = add i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %badd
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_add_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = add i8 %1, %y
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_add_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %badd = add i8 %y, 2
    %select = select i1 %cmp, i8 %badd, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_add_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = add i8 %1, %y
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_or_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %bor = or i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %bor
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_or_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = or i8 %1, %y
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_or_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %bor = or i8 %y, 2
    %select = select i1 %cmp, i8 %bor, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_or_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = or i8 %1, %y
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_xor_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %bxor = xor i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %bxor
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_xor_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = xor i8 %1, %y
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_xor_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %bxor = xor i8 %y, 2
    %select = select i1 %cmp, i8 %bxor, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_xor_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = xor i8 %1, %y
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_mul_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %bmul = mul i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %bmul
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_mul_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %select = shl i8 %y, %and
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_mul_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %bmul = mul i8 %y, 2
    %select = select i1 %cmp, i8 %bmul, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_mul_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %select = shl i8 %y, %and
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_and_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %band = and i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %band
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_and_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %band = and i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %band
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_and_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %band = and i8 %y, 2
    %select = select i1 %cmp, i8 %band, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_and_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %cmp.not = icmp eq i8 %and, 0
    %band = and i8 %y, 2
    %select = select i1 %cmp.not, i8 %y, i8 %band
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_sub_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %bsub = sub i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %bsub
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_sub_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %bsub = add i8 %y, 254
    %select = select i1 %cmp, i8 %y, i8 %bsub
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_sub_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %bsub = sub i8 %y, 2
    %select = select i1 %cmp, i8 %bsub, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_sub_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %cmp.not = icmp eq i8 %and, 0
    %bsub = add i8 %y, 254
    %select = select i1 %cmp.not, i8 %y, i8 %bsub
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_shl_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %bshl = shl i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %bshl
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_shl_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = shl i8 %y, %1
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_shl_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %bshl = shl i8 %y, 2
    %select = select i1 %cmp, i8 %bshl, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_shl_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = shl i8 %y, %1
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_lshr_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %blshr = lshr i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %blshr
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_lshr_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = lshr i8 %y, %1
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_lshr_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %blshr = lshr i8 %y, 2
    %select = select i1 %cmp, i8 %blshr, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_lshr_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = lshr i8 %y, %1
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_ashr_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %bashr = ashr i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %bashr
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_ashr_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = ashr i8 %y, %1
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_ashr_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %bashr = ashr i8 %y, 2
    %select = select i1 %cmp, i8 %bashr, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_ashr_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = shl i8 %x, 1
    %1 = and i8 %and, 2
    %select = ashr i8 %y, %1
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_sdiv_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %bsdiv = sdiv i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %bsdiv
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_sdiv_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %bsdiv = sdiv i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %bsdiv
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_sdiv_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %bsdiv = sdiv i8 %y, 2
    %select = select i1 %cmp, i8 %bsdiv, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_sdiv_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %cmp.not = icmp eq i8 %and, 0
    %bsdiv = sdiv i8 %y, 2
    %select = select i1 %cmp.not, i8 %y, i8 %bsdiv
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_udiv_fv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp eq i8 %and, 0
    %budiv = udiv i8 %y, 2
    %select = select i1 %cmp, i8 %y, i8 %budiv
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_udiv_fv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %select = lshr i8 %y, %and
    ret i8 %select
  }
  Transformation seems to be correct!
  
  
  ----------------------------------------
  define i8 @select_icmp_eq_and_1_0_udiv_tv(i8 %x, i8 %y) {
  %0:
    %and = and i8 %x, 1
    %cmp = icmp ne i8 %and, 0
    %budiv = udiv i8 %y, 2
    %select = select i1 %cmp, i8 %budiv, i8 %y
    ret i8 %select
  }
  =>
  define i8 @select_icmp_eq_and_1_0_udiv_tv(i8 %x, i8 %y) nofree willreturn memory(none) {
  %0:
    %and = and i8 %x, 1
    %select = lshr i8 %y, %and
    ret i8 %select
  }
  Transformation seems to be correct!
  
  Summary:
    22 correct transformations
    0 incorrect transformations
    0 failed-to-prove transformations
    0 Alive2 errors


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148414



More information about the llvm-commits mailing list