[PATCH] D21190: [InstCombine] allow more than one use for vector cast folding with selects

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 9 13:45:41 PDT 2016


spatel added a comment.

In http://reviews.llvm.org/D21190#453811, @eli.friedman wrote:

> Something like this?
>
>   loop:
>     %sel = select <4 x i1> %cmp, <4 x i32> %selx, <4 x i32> %selx
>     %selx = bitcast <4 x i32> %sel to <4 x f32>
>     br label %loop
>   
>
> Your optimization flips the types of the select and the bitcast, I think.  Obviously not an actual testcase because it's missing PHI nodes, but that's the basic idea.


Interesting...I'm trying to be more diabolical, but I can't get it yet. :)
In order for the bitcasted output to feed back into the original intstruction, it would have to be bitcasted back to the original type somewhere along the way? In that case, I think we'd eliminate the bitcasts as they get paired up:

  define void @infloop(<4 x i1> %cmp, <4 x i32> %a, <4 x i32> %b) {
  entry:
    br label %loop
  
  loop:
    %phi1 = phi <4 x i32> [ %a, %entry ], [ %self1, %loop ]
    %phi2 = phi <4 x i32> [ %b, %entry ], [ %self2, %loop ]
    %selx1 = bitcast <4 x i32> %phi1 to <4 x float>
    %selx2 = bitcast <4 x i32> %phi2 to <4 x float>
    %sel1 = select <4 x i1> %cmp, <4 x float> %selx1, <4 x float> %selx2
    %sel2 = select <4 x i1> %cmp, <4 x float> %selx2, <4 x float> %selx1
    %self1 = bitcast <4 x float> %sel1 to <4 x i32>
    %self2 = bitcast <4 x float> %sel2 to <4 x i32>
    br label %loop
  
    ret void
  }

$ ./opt -instcombine infloop.ll -S

  define void @infloop(<4 x i1> %cmp, <4 x i32> %a, <4 x i32> %b) {
  entry:
    br label %loop
  
  loop:                                             ; preds = %loop, %entry
    %phi1 = phi <4 x i32> [ %a, %entry ], [ %sel1.v, %loop ]
    %phi2 = phi <4 x i32> [ %b, %entry ], [ %sel2.v, %loop ]
    %sel1.v = select <4 x i1> %cmp, <4 x i32> %phi1, <4 x i32> %phi2
    %sel2.v = select <4 x i1> %cmp, <4 x i32> %phi2, <4 x i32> %phi1
    br label %loop
                                                    ; No predecessors!
    ret void
  }


http://reviews.llvm.org/D21190





More information about the llvm-commits mailing list