<div dir="ltr">I decided to pump some IR through the optimizers and was surprised to find that we don't choose to canonicalize any of the following functions to the other even though they are all roughly the same.<div>
<br></div><div><div>define i32 @f1(i32 %x, i32 %m) #0 {</div><div>entry:  </div><div>  %and = and i32 %m, 32896</div><div>  %tobool = icmp ne i32 %and, 0</div><div>  %cond = select i1 %tobool, i32 8, i32 0</div><div>  %shr = lshr i32 %x, %cond</div>
<div>  ret i32 %shr</div><div>}       </div><div>        </div><div>; Function Attrs: nounwind readnone uwtable</div><div>define i32 @f2(i32 %x, i32 %m) #0 {</div><div>entry:</div><div>  %and = and i32 %m, 32896</div><div>
  %tobool = icmp eq i32 %and, 0</div><div>  %shr = lshr i32 %x, 8</div><div>  %x.shr = select i1 %tobool, i32 %x, i32 %shr</div><div>  ret i32 %x.shr</div><div>}</div><div><br></div><div>; Function Attrs: nounwind readnone uwtable</div>
<div>define i32 @f3(i32 %x, i32 %m) #0 {</div><div>entry:</div><div>  %and = and i32 %m, 32896</div><div>  %tobool = icmp ne i32 %and, 0</div><div>  %conv = zext i1 %tobool to i32</div><div>  %shl = shl nuw nsw i32 %conv, 3</div>
<div>  %shr = lshr i32 %x, %shl</div><div>  ret i32 %shr</div><div>}</div><div><br></div><div>attributes #0 = { nounwind readnone uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }</div>
</div><div><br></div><div style>My gut says f2 should be our canonicalization target because it doesn't have a variable width shift while f1 and f3 both do while having the same number immediates as f3.</div><div style>
<br></div><div style>Thoughts? Is it not worth looking into?</div><div style><br></div><div style>Thanks</div><div style>-- </div><div style>David Majnemer</div></div>