Issue 235261
Summary [clang] [Mips] lowerShiftRightParts/lowerShiftLeftParts select on `and shamt, 32` (0 or 32, not a 0/1 boolean): `0x1FFFFFFFF >> (b & 32)` gives 0x2100000001 on MIPS32
Labels backend:MIPS, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```cpp
/* [Mips] lowerShiftLeftParts/lowerShiftRightParts select on `and shamt, 32`, a value that is 0 or 32, although MIPS
 * booleans are ZeroOrOne. Generic DAG folds that assume a 0/1 condition then miscompile 64-bit shifts on MIPS32
 * (and 128-bit shifts on MIPS64): select(c, 0, 1) -> xor c, 1 gives 33, select(c, 1, 0) -> c gives 32.
 *
 * Godbolt: compiler "mips clang (trunk)" (or x86-64 clang trunk with --target=mips-linux-gnu), flags: -O2
 *   (also -O0, -O1, --target=mipsel-linux-gnu, -mcpu=mips32r2..r5; mips32r6 (seleqz/selnez) and mips2 are correct)
 *   Buggy asm for f:  andi $1,$4,32 ; ... ; ori $2,$1,1 <- high word = (b & 32) | 1
 * Output: for each check, got.hi got.lo exp.hi exp.lo
 * EXPECTED (correct compiler; gcc x86-64 prints this):
 *   0 1 0 1 1 0 1 0  1 -1 1 -1  0 1 0 1            exit 0
 * BUGGY (vanilla trunk c4a7814f8b, clang -O2 --target=mips-linux-gnu / mipsel, executed in a MIPS32 emulator):
 *   33 1 0 1  1 0 1 0  1 -1 1 -1  0 1 0 1  -999999   exit 1
 * i.e. f(32) = 0x0000002100000001 instead of 1 (g is a correct control: 1 << 32).
 */
int printf(const char *, ...);

__attribute__((noinline)) unsigned long long f(unsigned b) { return 0x1FFFFFFFFULL >> (b & 32); }
__attribute__((noinline)) unsigned long long g(unsigned b) { return 1ULL << (b & 32); }

volatile unsigned B = 32;

static int check(const char *name, unsigned long long got, unsigned long long exp) {
  /* one %d per printf: high word, low word of got, then of expected */
  printf("%d\n", (int)(got >> 32)); printf("%d\n", (int)got);
  printf("%d\n", (int)(exp >> 32)); printf("%d\n", (int)exp);
  (void)name;
  return got != exp;
}

int main(void) {
  unsigned b = B;
  int bad = 0;
  bad |= check("f", f(b), 1ULL);          /* 0x1FFFFFFFF >> 32 = 1 */
  bad |= check("g", g(b), 1ULL << 32);    /* 1 << 32 */
  bad |= check("f0", f(0), 0x1FFFFFFFFULL);
  bad |= check("g0", g(0), 1ULL);
  if (bad) printf("%d\n", -999999);       /* MISMATCH marker */
  return bad;
}
```

Resulted in this output:
```
$ clang --target=mips-linux-gnu -O2 repro.c   # run in a MIPS32 emulator (same for mipsel)
33 1 0 1  1 0 1 0  1 -1 1 -1  0 1 0 1  -999999      (exit 1)
$ gcc -O2 repro.c && ./a.out                     # x86-64 reference
0 1 0 1  1 0 1 0  1 -1 1 -1  0 1 0 1                (exit 0)

f: # mips -O2
	andi	$1, $4, 32
	addiu	$3, $zero, -1
	addiu	$2, $zero, 1
	movn	$3, $2, $1
	jr	$ra
	ori	$2, $1, 1 # high word = (b & 32) | 1
```

| call | expected | actual |
|------|----------|--------|
| f(32) = 0x1FFFFFFFF >> 32 | 0x00000000_00000001 | 0x00000021_00000001 |
| g(32), f(0), g(0) | correct | correct |

Trunk: https://godbolt.org/z/eeYYzhjTq (x86-64 clang trunk, `--target=mips-linux-gnu -O2`, assembly view; reproduces as of 2026-10-24)

Root cause: `MipsTargetLowering::lowerShiftRightParts` and `lowerShiftLeftParts` (llvm/lib/Target/Mips/MipsISelLowering.cpp) build `Cond = and Shamt, VTBits`, which is 0 or 32 (64 on MIPS64), and use it directly as an `ISD::SELECT` condition. MIPS uses `ZeroOrOneBooleanContent`, so generic DAG folds may assume the condition is 0 or 1. MOVN/SELNEZ treat any nonzero value as true, but `DAGCombiner::foldSelectOfConstants` rewrites `select Cond, 0, 1` to `xor Cond, 1`, which gives 33 for Cond = 32. Affected: mips/mipsel mips32..mips32r5 at -O0/-O1/-O2, and 128-bit shifts on mips64. mips32r6 and mips1/2 take different lowerings and are correct.

Suggested fix: Make the condition a real boolean in both lowerings, e.g. `Cond = setcc ne (and Shamt, VTBits), 0` (or `srl (and Shamt, VTBits), log2(VTBits)`). Validated locally (a local tree carries the setcc fix, and the reproducer then prints the expected values with exit 0).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to