<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/55168>55168</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Miscompile due to SelectionDAG::FoldConstantArithmetic not supporting setBooleanVectorContents(ZeroOrOneBooleanContent)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          bjope
      </td>
    </tr>
</table>

<pre>
    Given a test case like this

`
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -start-after codegenprepare -mtriple=riscv64 -mattr=-v -o - %s | FileCheck --check-prefix CHECK-OK %s
; RUN: llc -start-after codegenprepare -mtriple=riscv64 -mattr=+v -o - %s | FileCheck --check-prefix CHECK-NOK %s

define i32 @zextVectorBool(i32 %a) {
; CHECK-OK-LABEL: zextVectorBool:
; CHECK-OK:       # %bb.0:
; CHECK-OK-NEXT:    li a0, 1
; CHECK-OK-NEXT:    ret
;
; CHECK-NOK-LABEL: zextVectorBool:
; CHECK-NOK:       # %bb.0:
; CHECK-NOK-NEXT:    li a0, -1
; CHECK-NOK-NEXT:    ret
  %t1 = insertelement <2 x i32> <i32 poison, i32 -2147483648>, i32 %a, i64 0
  %t2 = icmp slt <2 x i32> %t1, <i32 -2147483646, i32 -2147483646>
  %t3 = zext <2 x i1> %t2 to <2 x i32>
  %t4 = extractelement <2 x i32> %t3, i64 1
  ret i32 %t4
}
`

we see that the result become -1 instead of 1 when having `mattr=+v`.

With the vector instruction support enabled by the mattr the RISCV target for example sets
  setBooleanVectorContents(ZeroOrOneBooleanContent);
while also making some vector operations etc legal. And then one ends up in a situation when SelectionDAG::FoldConstantArithmetic is performed on a vector setcc after legalization has splitted the v2i32 into two v1i32

`
Legalizing node: t20: v2i32 = setcc t23, t10, setlt:ch
Analyzing result type: v2i32
Split node result: t20: v2i32 = setcc t23, t10, setlt:ch

Creating new node: t29: i1 = setcc t3, Constant:i32<-2147483646>, setlt:ch
Creating new node: t30: i32 = sign_extend t29
Creating new node: t31: v1i32 = setcc t27, t25, setlt:ch
Creating constant: t32: i1 = Constant<-1>
Creating constant: t33: i32 = Constant<-1>
Creating new node: t34: v1i32 = BUILD_VECTOR Constant:i32<-1>
New node fold constant vector: t34: v1i32 = BUILD_VECTOR Constant:i32<-1>
New node vector constant folding: t34: v1i32 = BUILD_VECTOR Constant:i32<-1>
`

So both the sign_extend  for the first element is wrong, and the constant folding of the secone element is wrong as it also has sign-extended the <i1 1> into <i32 -1>.


Problem seem to be these code fragments in SelectionDAG::FoldConstantArithmetic:

`
  // If we are comparing vectors, then the result needs to be a i1 boolean
  // that is then sign-extended back to the legal result type.
  EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());

  ...

    // Legalize the (integer) scalar constant if necessary.
    if (LegalSVT != SVT)
      ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
`

The folding logic is trying to use i1 as a type when having found a SETCC as we know that we are dealing with booleans.
But as indicated in those code fragments it always assume that it is correct to sign-extend to get back to the legal integer result type.
However, if the SETCC returned something larger than i1, then such a bool should adhere to what the target has defined by using setBooleanVectorContents. For a target such as RISCV with mattr=+v the correct thing would be to zero extend instead of sign extend.

So a potential fix is probably to also check if `Opcode == ISD::SETCC && SVT != VT.getScalarType()` and if so, either sign or zero extend depending on `TLI.getBooleanContents(VT)`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytWF1z2jgU_TXOiwYG20DIAw8ESJtpSnYSmt3Zl4xsC1BjLEaSQ-iv33MlGwz52Ha3ndbYsnTuuVf3Hl03Udlu-Ek-i4JxZoWxLOVGsFw-CWZX0gSdSdAZVdd-p7qJL9nsdj4N4hEbGSO0laowbMWfBUsEQZVWLUUhNLciY8mOlVbmJoiuyk2Gocc8Tx_J2GO6EumTaW92B-C7bzP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