[x86-64] Wrong code: shift+mask equality on a sub-word integer is compiled as a rotate comparison
Environment
Clang 19.1.0 (x86-64-unknown-linux-gnu); llc 19.1.0 for the IR-level reproducer
Command
clang -O1 test11.c -o a.out && ./a.out # also -O2; IR level: llc -mtriple=x86_64-linux-gnu -filetype=obj test11.ll && link test11_driver.c
Expected behavior
Every input a in 0..7 must yield f(a<<8) per the reference table {1,3,1,3,3,1,3,1} (the -O0 build prints "RESULT: correct").
Actual behavior
-O1/-O2 builds print "RESULT: wrong": inputs a=2 and a=5 return 3 instead of 1. The generated code compares X against rotl(X, 2) instead of evaluating (X << 2) == (X & -4) on the 3-bit value; the two forms differ whenever the shift amount does not divide the bit width. The same wrong fold is visible when test11.ll is compiled with llc for x86-64.
Additional notes
The source is UBSan-clean at -O0. The C test uses C23 _BitInt(3) to obtain the i3 type that exposes the fold; the .ll file reproduces it without any front end. The test case was produced by an automated compiler-testing tool (GapSmith).
Test case (test11.c)
#include <stdio.h>
__attribute__((noinline))
long f(long x) {
unsigned _BitInt(3) v = (unsigned _BitInt(3))(x >> 8);
unsigned _BitInt(3) s = (unsigned _BitInt(3))(v << 2);
unsigned _BitInt(3) m = (unsigned _BitInt(3))(v & (unsigned _BitInt(3))(-4));
return s == m ? 1 : 3;
}
int main() {
int exp[8] = {1,3,1,3,3,1,3,1};
int bad = 0;
for (long a = 0; a < 8; a++) {
long r = f(a << 8);
if (r != exp[a]) { printf("a=%ld got=%ld expected=%d MISMATCH\n", a, r, exp[a]); bad++; }
}
printf(bad ? "RESULT: wrong\n" : "RESULT: correct\n");
return bad;
}
Additional file (test11.ll)
; LLVM 19.1.0 miscompilation case (IR level): i3 shift+mask equality folded to a rotate
; on x86-64 (llc -mtriple=x86_64-linux-gnu); wrong for inputs a=2 and a=5.
define i64 @f(i64 %0) {
%2 = lshr i64 %0, 8
%3 = trunc i64 %2 to i3
%4 = shl i3 %3, 2
%5 = and i3 %3, -4
%6 = icmp eq i3 %4, %5
%7 = select i1 %6, i64 1, i64 3
ret i64 %7
}
Additional file (test11_driver.c)
#include <stdio.h>
long f(long);
int main() {
int exp[8] = {1,3,1,3,3,1,3,1};
int bad = 0;
for (long a = 0; a < 8; a++) {
long r = f(a << 8);
printf("a=%ld got=%ld expected=%d %s\n", a, r, exp[a], r==exp[a]?"ok":"MISMATCH");
if (r != exp[a]) bad++;
}
return bad;
}
@ICSE-27: Please specify language for code snippets.
https://godbolt.org/z/T9veT5f3d
fixed in trunk but not 23.x
Looks like a reduced C23 reproducer in #220542.
@llvm/issue-subscribers-backend-x86
Author: ICSE-27
Command
clang -O1 test11.c -o a.out && ./a.out # also -O2; IR level: llc -mtriple=x86_64-linux-gnu -filetype=obj test11.ll && link test11_driver.c
Expected behavior
Every input a in 0..7 must yield f(a<<8) per the reference table {1,3,1,3,3,1,3,1} (the -O0 build prints "RESULT: correct").
Actual behavior
-O1/-O2 builds print "RESULT: wrong": inputs a=2 and a=5 return 3 instead of 1. The generated code compares X against rotl(X, 2) instead of evaluating (X << 2) == (X & -4) on the 3-bit value; the two forms differ whenever the shift amount does not divide the bit width. The same wrong fold is visible when test11.ll is compiled with llc for x86-64.
Additional notes
The source is UBSan-clean at -O0. The C test uses C23 _BitInt(3) to obtain the i3 type that exposes the fold; the .ll file reproduces it without any front end. The test case was produced by an automated compiler-testing tool (GapSmith).
Test case (test11.c)
#include <stdio.h>
__attribute__((noinline))
long f(long x) {
unsigned _BitInt(3) v = (unsigned _BitInt(3))(x >> 8);
unsigned _BitInt(3) s = (unsigned _BitInt(3))(v << 2);
unsigned _BitInt(3) m = (unsigned _BitInt(3))(v & (unsigned _BitInt(3))(-4));
return s == m ? 1 : 3;
}
int main() {
int exp[8] = {1,3,1,3,3,1,3,1};
int bad = 0;
for (long a = 0; a < 8; a++) {
long r = f(a << 8);
if (r != exp[a]) { printf("a=%ld got=%ld expected=%d MISMATCH\n", a, r, exp[a]); bad++; }
}
printf(bad ? "RESULT: wrong\n" : "RESULT: correct\n");
return bad;
}
Additional file (test11.ll)
; LLVM 19.1.0 miscompilation case (IR level): i3 shift+mask equality folded to a rotate
; on x86-64 (llc -mtriple=x86_64-linux-gnu); wrong for inputs a=2 and a=5.
define i64 @<!-- -->f(i64 %0) {
%2 = lshr i64 %0, 8
%3 = trunc i64 %2 to i3
%4 = shl i3 %3, 2
%5 = and i3 %3, -4
%6 = icmp eq i3 %4, %5
%7 = select i1 %6, i64 1, i64 3
ret i64 %7
}
Additional file (test11_driver.c)
#include <stdio.h>
long f(long);
int main() {
int exp[8] = {1,3,1,3,3,1,3,1};
int bad = 0;
for (long a = 0; a < 8; a++) {
long r = f(a << 8);
printf("a=%ld got=%ld expected=%d %s\n", a, r, exp[a], r==exp[a]?"ok":"MISMATCH");
if (r != exp[a]) bad++;
}
return bad;
}