<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56810>56810</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
WRONG code with Instcombine at -O1
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:instcombine
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
nikic,
alexander-shaposhnikov,
rotateright
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
JonPsson
</td>
</tr>
</table>
<pre>
This program:
```
int printf(const char *, ...);
long a;
int b, c;
long d[2];
static long *e = &d[1];
char f;
short g, h;
int *i = &b;
short mod(short k, short l) { return k % l; }
static short add(short k, short l) { return k + l; }
int main() {
long *n = &a;
f = 3;
g = mod(n != e, 51); // 1, 51 => 1
h = add(g & 3, 6); // 1, 6 => 7
c = f % h;
*i = c;
printf("%d\n", b);
}
```
should print '3', which happens except on -O1. I suspect Instcombine is to blame as I can't help to think that it has mixed up the operands in the urem instruction:
```
*** IR Dump After SimplifyCFGPass on main ***
; Function Attrs: nofree nounwind
define dso_local signext i32 @main() local_unnamed_addr #1 {
entry:
store i8 3, ptr @f, align 2, !tbaa !4
%rem.i = srem i32 zext (i1 icmp ne (ptr getelementptr inbounds ([2 x i64], ptr @d, i64 0, i64 1), ptr @a) to i32), 51
%conv2.i = trunc i32 %rem.i to i16
store i16 %conv2.i, ptr @g, align 2, !tbaa !7
%0 = and i16 %conv2.i, 3
%add.i = add i16 %0, 6
store i16 %add.i, ptr @h, align 2, !tbaa !7
%conv6 = zext i16 %add.i to i32
**%rem = urem i32 3, %conv6**
store i32 %rem, ptr @c, align 4, !tbaa !9
%1 = load ptr, ptr @i, align 8, !tbaa !11
store i32 %rem, ptr %1, align 4, !tbaa !9
%2 = load i32, ptr @b, align 4, !tbaa !9
%call7 = call signext i32 (ptr, ...) @printf(ptr noundef nonnull @.str, i32 noundef signext %2)
ret i32 0
}
*** IR Dump After InstCombinePass on main ***
; Function Attrs: nofree nounwind
define dso_local signext i32 @main() local_unnamed_addr #1 {
entry:
store i8 3, ptr @f, align 2, !tbaa !4
store i16 zext (i1 icmp ne (ptr getelementptr inbounds ([2 x i64], ptr @d, i64 0, i64 1), ptr @a) to i16), ptr @g, align 2, !tbaa !7
store i16 add (i16 zext (i1 icmp ne (ptr getelementptr inbounds ([2 x i64], ptr @d, i64 0, i64 1), ptr @a) to i16), i16 6), ptr @h, align 2, !tbaa !7
**%0 = urem i16 add (i16 zext (i1 icmp ne (ptr getelementptr inbounds ([2 x i64], ptr @d, i64 0, i64 1), ptr @a) to i16), i16 6), 3**
%rem = zext i16 %0 to i32
store i32 %rem, ptr @c, align 4, !tbaa !9
%1 = load ptr, ptr @i, align 8, !tbaa !11
store i32 %rem, ptr %1, align 4, !tbaa !9
%2 = load i32, ptr @b, align 4, !tbaa !9
%call7 = call signext i32 (ptr, ...) @printf(ptr noundef nonnull @.str, i32 noundef signext %2)
ret i32 0
}
```
After Instcombine the '3' is now the second operand, which can't be right. It now prints '1' instead (7%3)...
`clang -target s390x-linux-gnu -march=arch13 -O1 wrong0.i`
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztV92PozYQ_2vIyygRhgDJQx6ym-5p-9A7XSv1cWXAgLvERLbZzfWv74wNgUR7at7uVFVC4I_5-M14ZhjnXflt90cjDZx0V2t-DOJ9EB6CcB-k4fD4qXtLZZEQ31UQbYpOGQtFwzUE0T6IHmG1WgXRNogfPHXbqRr4ZUrMOZEV1xRlkDxEQXK4rBrLrSzAbaJkAUF8wEFKhGxO6HRXE1_TaQs1qWiutKIQOQrJb8iPXYm2-PErsfphi4ZAkD2AFrbXCl6RN8HV-AFXD1c4PQMv75bzcCNnAnrkUqGUgcevw8URarRh8ilA5Rbj2UrtVrxdyBIxmgqClDB_PICrT_gA86vEEMS_4HQQ0TgR3iRSnaICpExH9msBKYwCslFA4QRUzmnNDNvlJIrZ4iWigihCBjzmR-WGjxQvUzxNDruOTfR035ZeDGrIEGtGzO-NLBp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