Issue 244594
Summary [fuzzer-found][DAGCombiner][Mips] `xor (setcc), 1` -> inverted setcc treats `1` as "true" for FP compares whose booleans are 0/-1: MIPS R6 `(a > b) ^ 1` gives 0/-1 instead of -2/1
Labels backend:MIPS, miscompilation, llvm:SelectionDAG, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```c
// clang --target=mips64el-linux-gnuabi64 -mcpu=mips64r6 -O1 repro.c   (also -O0/-O2/-O3; mips32r6 too)
// run: qemu-mips64el -cpu I6400 ./a.out ; expected output "0" (exit 0)
#include <stdio.h>
typedef float v2f __attribute__((vector_size(8)));
typedef int v2i __attribute__((vector_size(8)));
__attribute__((noinline)) v2i f(v2f a, v2f b) {
  return (a > b) ^ 1;  // true lane: -1 ^ 1 = -2, false lane: 0 ^ 1 = 1
}
int main(void) {
  volatile float x = 2.0f, y = 1.0f;
  v2i r = f((v2f){x, y}, (v2f){y, x});
  int bad = (r[0] != -2) | (r[1] != 1) << 1;
 printf("%d\n", bad);
  return bad;  // expect 0; MIPS R6 prints 3: r = {0, -1}
}
```

Resulted in this output:
```
$ clang --target=mips64el-linux-gnuabi64 -mcpu=mips64r6 -O1 repro.c -o r && qemu-mips64el -cpu I6400 ./r
3
```
The expected output is `0`. Each lane is the inverted compare itself (0/-1) instead of `compare ^ 1` (-2/1):
```
f:  ...
    cmp.ule.s $f0, $f1, $f0     # !(a > b) as 0 / all-ones
    mfc1      $2, $f0           # used directly, no xor with 1
 ...
    cmp.ule.s $f0, $f1, $f0
    mfc1      $1, $f0
```

Trunk: https://godbolt.org/z/63d7c83Pc (clang trunk `--target=mips64el-linux-gnuabi64 -mcpu=mips64r6 -O1`; clang 12 and 18 emit the same `cmp.ule.s` without the xor). Wrong at `-O0` too; `--target=mipsel-linux-gnu -mcpu=mips32r6` is also wrong.

Root cause: `DAGCombiner::visitXOR` folds `not (setcc x, y, cc)` into `setcc x, y, !cc` when `TLI.isConstTrueVal(N1)`. `isConstTrueVal` decides what "true" is from the boolean contents of the xor's own type (`i32`, ZeroOrOne on MIPS), so it accepts `xor ..., 1`. But a SETCC's boolean contents come from the compared type, and MIPS R6 uses ZeroOrNegativeOneBooleanContent for floating-point compares (`cmp.cond.fmt` writes all ones). When the `<2 x i1>` fcmp + `sext` to `<2 x i32>` is scalarized, the `sext` folds into an `i32` FP setcc whose true value is -1, and `xor` with 1 is then not a logical not: `-1 ^ 1 = -2`, but the fold produces the inverted setcc (0 / -1).

Suggested fix: only treat the constant as "true" when the setcc and the result type agree on the boolean contents (validated locally: the C repro prints `0` at -O0/-O1/-O2/-O3 on mips64r6 and mips32r6; llc output of the 100 Mips, M68k and CSKY CodeGen tests with `fcmp` and of the Hexagon/AMDGPU tests with `xor` + `fcmp`/`setcc` (all RUN lines) is unchanged):
```diff
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ SDValue DAGCombiner::visitXOR(SDNode *N) {
   if (TLI.isConstTrueVal(N1) &&
 isSetCCEquivalent(N0, LHS, RHS, CC, /*MatchStrict*/ true) &&
+      // N1 must be the true value of the setcc itself: a SETCC's boolean
+      // contents come from the compared type (FP compares on MIPS R6 are
+      // ZeroOrNegativeOne while the i32 result type is ZeroOrOne).
+      (N0Opcode != ISD::SETCC || VT.getScalarType() == MVT::i1 ||
+ TLI.getBooleanContents(LHS.getValueType()) ==
+ TLI.getBooleanContents(VT)) &&
       !(VT.isVector() && TLI.hasAndNot(SDValue(N, 0)) && N->hasOneUse() &&
 N->use_begin()->getUser()->getOpcode() == ISD::AND)) {
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to