Skip to content

[InstCombine] Missed optimization for icmp of select with constants and invertible binop #146642

@nikic

Description

@nikic

We currently fail to fold variations of this pattern:

https://alive2.llvm.org/ce/z/tT3yNU

define i1 @src(i64 %a, i1 %c) {
  %idx = shl nsw i64 %a, 3
  %sel = select i1 %c, i64 8, i64 0
  %cmp = icmp eq i64 %idx, %sel
  ret i1 %cmp
}

define i1 @tgt(i64 %a, i1 %c) {
  %sel = select i1 %c, i64 1, i64 0
  %cmp = icmp eq i64 %a, %sel
  ret i1 %cmp
}

The underlying pattern here is something like f(x) == y converted to x == f^-1(y) where f invertible and f^-1(y) folds because the shift arms are constant.

From https://github.com/dtcxzyw/llvm-opt-benchmark/pull/2524/files/9ca17b717ceea34b52263bf8b20cf12ad02cd4fd#r2179220735.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions