[PATCH] D137934: [InstCombine] Fold extractelt with select of constants

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 15 08:39:31 PST 2022


spatel added a comment.

This patch is more general than where it started. I'm not opposed to that, but it needs more tests.

This crashes:

  define i32 @extelt_select_const_operand_vector_var_index(i1 %c, i32 %e) {
    %s = select i1 %c, <3 x i32> <i32 2, i32 3, i32 4>, <3 x i32> <i32 5, i32 6, i32 7>
    %r = extractelement <3 x i32> %s, i32 %e
    ret i32 %r
  }

We can fold with a single constant select operand (the srem diff show this, but we should have minimal tests too):

  define i32 @extelt_select_var_const_operand_vector(i1 %c, <3 x i32> %v) {
    %s = select i1 %c, <3 x i32> %v, <3 x i32> <i32 5, i32 6, i32 7>
    %r = extractelement <3 x i32> %s, i32 1
    ret i32 %r
  }
  
  define i32 @extelt_select_const_var_operand_vector(i1 %c, <3 x i32> %v) {
    %s = select i1 %c, <3 x i32> <i32 5, i32 6, i32 7>, <3 x i32> %v
    %r = extractelement <3 x i32> %s, i32 0
    ret i32 %r
  }

Does this behave as expected when the select has another use besides the extract?

  declare void @use(<3 x i32>)
  
  define i32 @extelt_select_const_var_operands_vector_extra_use(i1 %c, <3 x i32> %x) {
    %s = select i1 %c, <3 x i32> <i32 42, i32 5, i32 4>, <3 x i32> %x
    call void @use(<3 x i32> %s)
    %r = extractelement <3 x i32> %s, i64 0
    ret i32 %r
  }
  
  define i32 @extelt_select_const_operands_vector_extra_use(i1 %c, <3 x i32> %x) {
    %s = select i1 %c, <3 x i32> <i32 42, i32 5, i32 4>, <3 x i32> <i32 5, i32 6, i32 7>
    call void @use(<3 x i32> %s)
    %r = extractelement <3 x i32> %s, i64 0
    ret i32 %r
  }


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D137934



More information about the llvm-commits mailing list