llvm / llvm/llvm-project

Select of lshr not converted to lshr of select with one multi-use operand

Open
#184,131 2 comments 0 reactions 1 assignee Claimed by @SeokhunEom View on GitHub
llvm:instcombine missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

From https://github.com/rust-lang/rust/pull/151780.

https://alive2.llvm.org/ce/z/AHRhE2
```llvm
define i16 @or(i16 %0, i16 %1) {
start:
%.sroa.0.0.extract.shift = lshr i16 %0, 8
%2 = trunc i16 %.sroa.0.0.extract.shift to i1
%_0.sroa.0.0.extract.shift = lshr i16 %1, 8
%_0.sroa.0.0.v = select i1 %2, i16 %.sroa.0.0.extract.shift, i16 %_0.sroa.0.0.extract.shift
%_0.sroa.3.0.v = select i1 %2, i16 %0, i16 %1
%_0.sroa.3.0.insert.ext = and i16 %_0.sroa.3.0.v, 255
%_0.sroa.0.0.insert.shift = shl nuw i16 %_0.sroa.0.0.v, 8
%_0.sroa.0.0.insert.insert = or disjoint i16 %_0.sroa.0.0.insert.shift, %_0.sroa.3.0.insert.ext
ret i16 %_0.sroa.0.0.insert.insert
}

define i16 @tgt(i16 %0, i16 %1) {
start:
%2 = and i16 %0, 256
%.not = icmp eq i16 %2, 0
%_0.sroa.3.0.v = select i1 %.not, i16 %1, i16 %0
ret i16 %_0.sroa.3.0.v
}
```

This would simplify if the select of lshr would be converted into lshr of select, but this doesn't happen because one of the operands is multi-use.

Possibly we can relax this fold to allow one multi-use operand, not sure if that will cause other issues.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.