Skip to content

[InstCombine] fold select of select with common argument #82350

@sftlbcn

Description

@sftlbcn

fold other select to logical and/or if either icmp predicate is free to invert
https://alive2.llvm.org/ce/z/aSYXfg

----------------------------------------
define i32 @src_or(i32 %x, i32 %y, i32 %z, i1 %cmp1) {
#0:
  %cmp2 = icmp eq i32 %z, 0
  %sel1 = select i1 %cmp1, i32 %x, i32 %y
  %sel2 = select i1 %cmp2, i32 %sel1, i32 %x
  ret i32 %sel2
}
=>
define i32 @tgt_or(i32 %x, i32 %y, i32 %z, i1 %cmp1) {
#0:
  %cmp2_inv = icmp ne i32 %z, 0
  %sel1 = select i1 %cmp2_inv, i1 1, i1 %cmp1
  %sel2 = select i1 %sel1, i32 %x, i32 %y
  ret i32 %sel2
}
Transformation seems to be correct!


----------------------------------------
define i32 @src_and(i32 %x, i32 %y, i32 %z, i1 %cmp1) {
#0:
  %cmp2 = icmp eq i32 %z, 0
  %sel1 = select i1 %cmp2, i32 %x, i32 %y
  %sel2 = select i1 %cmp1, i32 %sel1, i32 %x
  ret i32 %sel2
}
=>
define i32 @tgt_and(i32 %x, i32 %y, i32 %z, i1 %cmp1) {
#0:
  %cmp2_inv = icmp ne i32 %z, 0
  %sel1 = select i1 %cmp1, i1 %cmp2_inv, i1 0
  %sel2 = select i1 %sel1, i32 %y, i32 %x
  ret i32 %sel2
}
Transformation seems to be correct!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions