llvm / llvm/llvm-project

Missed optimization: Fold (or (or %0, (add (shl %1, 1), 2)), 1) into (or %0, (add (shl %1, 1), 3))

Open
#193,898 1 comment 0 reactions 1 assignee Claimed by @squirrel1027 View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The following reduced IR is derived from https://github.com/redis/redis/blob/47c5136/deps/lua/src/lcode.c#L69

Godbolt: https://godbolt.org/z/M4ea3q95K
alive2 proof: https://alive2.llvm.org/ce/z/gHkm2x

```llvm
define i32 @luaK_ret(i32 %0, i32 %1) {
%3 = shl i32 %1, 1
%4 = add i32 %3, 2
%5 = or i32 %0, %4
%6 = or i32 %5, 1
ret i32 %6
}
```

expected:
```llvm
define i32 @tgt(i32 %0, i32 %1) {
%3 = shl i32 %1, 1
%4 = add i32 %3, 3
%5 = or i32 %4, %0
ret i32 %5
}
```

This may be a reassociation issue, since (or (add (shl x, 1), 2), 1) can already be simplified to (add (shl x, 1), 3) when it appears directly (https://godbolt.org/z/Yrcdnd18b).

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.