Issue 244472
Summary [Mips] MIPS r6: negated FP compares (`!=`, une/one/ord) used as integers produce wrong values since #173541
Labels new issue
Assignees
Reporter 20000419
    On mips32r6 and mips64r6, a negated floating-point compare (`fcmp une`, `one` or `ord`) whose result is used as an integer produces the wrong value. This is a regression in clang 22.

```c
volatile double a = 9.0;
volatile double b = 25.0;
int f(void) {
  int u0 = (a != 9.0);    /* 0 */
  int u1 = (b != 8.5);    /* 1 */
 return u0 | (u1 << 1);  /* 2 */
}
int main(void) { return f(); }
```

`clang --target=mips64-linux-gnuabi64 -mcpu=mips64r6 -O2` returns `4` instead of `2` (run under `qemu-mips64 -cpu I6400` with a minimal `_start` that passes the return value of `main` to `exit`). `-O1` and `-Os` also return `4`, `-O0` returns `2`. mips64el r6 and mips32r6 (big and little endian, `qemu-mips[el] -cpu mips32r6-generic`) also return `4`. clang 21 returns `2`, and so does `-mcpu=mips64r5`.

Trunk, `-O2`:

```asm
 cmp.eq.d  $f0, $f1, $f0       # a == 9.0 -> all-ones mask
        mfc1 $2, $f0
        xori      $2, $2, 1           # "not" applied to the 0/-1 mask
        jr        $ra
        subu      $2, $1, $2
```

clang 21 uses `not $1, $1` on the same mask instead of `xori $1, $1, 1`.

Godbolt (mips64 clang 21.1.0 and clang trunk, `-mcpu=mips64r6 -O2`): https://godbolt.org/z/7veEeqsE7

The same happens at the IR level:

```llvm
@v0 = global double 9.0
define i32 @g() {
  %a = load volatile double, ptr @v0
  %c0 = fcmp une double %a, 9.0
  %z0 = zext i1 %c0 to i32
  %c1 = fcmp une double %a, 8.5
  %z1 = zext i1 %c1 to i32
  %s = shl i32 %z1, 1
  %o = or i32 %z0, %s
  ret i32 %o
}
```

`llc -mtriple=mips64-linux-gnuabi64 -mcpu=mips64r6` gives `4` at `-O0`, `-O1` and `-O2` with llc 22, 23 and trunk, and `2` with llc 21.

#173541 (016c0b50ce5a, "MIPSr6: Set SETCC CondCode not supported by hardware to Expand") removed the `Cmp_Pats` patterns that selected `setone`/`seto`/`setune`/`setne` as `NOR (CMP_UEQ/CMP_UN/CMP_EQ), ZERO`, and marked these condition codes `Expand` in `MipsSETargetLowering`. On r6, `CMP.cond.fmt` produces a 0/-1 mask, and `MipsTargetLowering` declares that with `setBooleanContents(ZeroOrOneBooleanContent, ZeroOrNegativeOneBooleanContent)` (integer booleans 0/1, FP compare booleans 0/-1). The expansion of `ISD::SETCC` in `SelectionDAGLegalize::ExpandNode` (`llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp`) emits the inverted compare and then:

```cpp
      if (NeedInvert) {
        Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
 }
```

`getLogicalNOT` picks the "true" constant from the boolean contents of the type it is given, here the `i32` result type, so it XORs with `1`. The value being inverted is the 0/-1 mask of an FP compare, so true becomes `0xfffffffe` and false becomes `1`. When the boolean is used only once it is often masked back to bit 0 later, which hides the problem; it shows up when the value is combined with other integers as above. The problem seems to be that this inversion uses the integer boolean contents instead of those of the FP operand type.

Versions: clang 19, 20 and 21 return `2` at all levels. clang 22.1.8, 23.1.5 and 24.0.0 (`d5a22c0d1c26`) return `4` at `-O1`, `-O2` and `-Os`. Compiler Explorer clang trunk (`b86056e175f6`) emits the same `xori` sequence. The code quoted above is unchanged on main (`aa8482fc9fe4`).

cc @wzssyqa @FlyGoat

Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.

Assisted-by: Grok

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to