[llvm] [InstCombine] fold icmp of select with invertible shl (PR #147182)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 9 02:43:55 PST 2025
dtcxzyw wrote:
> > Miscompilation reproducer: https://alive2.llvm.org/ce/z/-TagCh
>
> Is this patch causing this error? My compilation result is https://alive2.llvm.org/ce/z/enaJTE @dtcxzyw
>
> ```
> define i1 @src(i64 %indvars.iv.i, i32 %conv161.i.i.i) {
> entry:
> %cmp104.i.i.i = icmp slt i64 %indvars.iv.i, 1
> br i1 %cmp104.i.i.i, label %lor.end157.i.i.i, label %lor.rhs106.i.i.i
>
> lor.rhs106.i.i.i:
> br label %lor.end157.i.i.i
>
> lor.end157.i.i.i:
> %conv159.i.i.i = zext i1 %cmp104.i.i.i to i16
> %sub.i122.i.i.i = sub i16 0, %conv159.i.i.i
> %conv161.i.i.i2 = zext i16 %sub.i122.i.i.i to i32
> %shl.i.i.i.i = shl nuw i32 %conv161.i.i.i, 31
> %cond.i124.i.i.i = select i1 %cmp104.i.i.i, i32 %conv161.i.i.i2, i32 %shl.i.i.i.i
> %conv163.i.i.i = zext i32 %cond.i124.i.i.i to i64
> %tobool166.not.i.i.i = icmp eq i64 %conv163.i.i.i, 0
> ret i1 %tobool166.not.i.i.i
> }
>
> define i1 @tgt(i64 %indvars.iv.i, i32 %conv161.i.i.i) {
> entry:
> %cmp104.i.i.i = icmp sgt i64 %indvars.iv.i, 0
> br i1 %cmp104.i.i.i, label %lor.rhs106.i.i.i, label %lor.end157.i.i.i
>
> lor.rhs106.i.i.i: ; preds = %entry
> br label %lor.end157.i.i.i
>
> lor.end157.i.i.i: ; preds = %lor.rhs106.i.i.i, %entry
> %tobool166.not.i.i.i1 = icmp eq i32 %conv161.i.i.i, 0
> %tobool166.not.i.i.i = select i1 %cmp104.i.i.i, i1 %tobool166.not.i.i.i1, i1 false
> ret i1 %tobool166.not.i.i.i
> }
> ```
Confirmed. I forgot to include the datalayout string in the reproducer. Sorry about this.
```
; bin/opt -passes=instcombine test.ll -S
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define i1 @src(i64 %indvars.iv.i, i32 %conv161.i.i.i) {
entry:
%cmp104.i.i.i = icmp slt i64 %indvars.iv.i, 1
br i1 %cmp104.i.i.i, label %lor.end157.i.i.i, label %lor.rhs106.i.i.i
lor.rhs106.i.i.i: ; preds = %entry
br label %lor.end157.i.i.i
lor.end157.i.i.i: ; preds = %lor.rhs106.i.i.i, %entry
%conv159.i.i.i = zext i1 %cmp104.i.i.i to i16
%sub.i122.i.i.i = sub i16 0, %conv159.i.i.i
%conv161.i.i.i2 = zext i16 %sub.i122.i.i.i to i32
%shl.i.i.i.i = shl nuw i32 %conv161.i.i.i, 31
%cond.i124.i.i.i = select i1 %cmp104.i.i.i, i32 %conv161.i.i.i2, i32 %shl.i.i.i.i
%conv163.i.i.i = zext i32 %cond.i124.i.i.i to i64
%tobool166.not.i.i.i = icmp eq i64 %conv163.i.i.i, 0
ret i1 %tobool166.not.i.i.i
}
```
With this patch:
```
define i1 @src(i64 %indvars.iv.i, i32 %conv161.i.i.i) {
entry:
%cmp104.i.i.i = icmp slt i64 %indvars.iv.i, 1
br i1 %cmp104.i.i.i, label %lor.end157.i.i.i, label %lor.rhs106.i.i.i
lor.rhs106.i.i.i: ; preds = %entry
%0 = icmp eq i32 %conv161.i.i.i, 0
br label %lor.end157.i.i.i
lor.end157.i.i.i: ; preds = %lor.rhs106.i.i.i, %entry
%tobool166.not.i.i.i = phi i1 [ %0, %lor.rhs106.i.i.i ], [ true, %entry ]
ret i1 %tobool166.not.i.i.i
}
```
Without this patch: https://alive2.llvm.org/ce/z/eL3WkX
```
define i1 @src(i64 %indvars.iv.i, i32 %conv161.i.i.i) {
entry:
%cmp104.i.i.i = icmp slt i64 %indvars.iv.i, 1
br i1 %cmp104.i.i.i, label %lor.end157.i.i.i, label %lor.rhs106.i.i.i
lor.rhs106.i.i.i: ; preds = %entry
br label %lor.end157.i.i.i
lor.end157.i.i.i: ; preds = %lor.rhs106.i.i.i, %entry
%conv161.i.i.i2 = phi i32 [ 0, %lor.rhs106.i.i.i ], [ 65535, %entry ]
%shl.i.i.i.i = shl nuw i32 %conv161.i.i.i, 31
%cond.i124.i.i.i = select i1 %cmp104.i.i.i, i32 %conv161.i.i.i2, i32 %shl.i.i.i.i
%tobool166.not.i.i.i = icmp eq i32 %cond.i124.i.i.i, 0
ret i1 %tobool166.not.i.i.i
}
```
If it is still not reproducible, use the base commit 77342761a9060159f09c0c6d5aa3da2e543a4877.
https://github.com/llvm/llvm-project/pull/147182
More information about the llvm-commits
mailing list