[llvm] [SCCP] Add support for trunc nuw range. (PR #152990)

Mikael Holmén via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 18 04:21:35 PDT 2025


mikaelholmen wrote:

Hi @andjo403

I think I've found a miscompile with this patch:
```opt -passes="sccp" bbi-109669.ll -S -o -```

The input program executes the loop two times, and then end up at %if.end and returns 0.
In the output program %if.end is removed and the only ret instruction left in the program is
```
ret i16 1
```
The most interesting part of the input is
```
@switch.table.main = private unnamed_addr constant [4 x i16] [i16 -19, i16 -18, i16 -17, i16 -10], align 1
[...]
%1 = trunc nuw nsw i16 %j.03 to i8                       ; -20, -19
%2 = getelementptr [4 x i16], ptr @switch.table.main, i16 0, i8 %1    ; switch.table.main - 20x2, switch.table.main - 19x2
%switch.gep6 = getelementptr i8, ptr %2, i16 40                       ; switch.table.main, switch.table.main + 2
%switch.load = load i16, ptr %switch.gep6, align 1                    ; -19, -18
```
where %j.03 is first -20 and then -19, leading to leads of the values -19 and -18, but with the patch this is somehow changed so we never "load" -18, but we're stuck at -19 and then we end up at the wrong ret.

So I guess either a miscompile, or the input program is already broken and doesn't do what I think it does, but then I fail to realize how it's broken.


https://github.com/llvm/llvm-project/pull/152990


More information about the llvm-commits mailing list