[PATCH] D124618: [SelectionDAG] Use correct boolean representation in FoldConstantArithmetic

Bjorn Pettersson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 28 08:24:51 PDT 2022


bjope updated this revision to Diff 425791.
bjope added a comment.

Shorten a variable name according to review feedback.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D124618/new/

https://reviews.llvm.org/D124618

Files:
  llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
  llvm/test/CodeGen/RISCV/fold-vector-cmp.ll


Index: llvm/test/CodeGen/RISCV/fold-vector-cmp.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/RISCV/fold-vector-cmp.ll
@@ -0,0 +1,17 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -start-after codegenprepare -mtriple=riscv64 -mattr=-v -o - %s | FileCheck %s
+; RUN: llc -start-after codegenprepare -mtriple=riscv64 -mattr=+v -o - %s | FileCheck %s
+
+; Reproducer for https://github.com/llvm/llvm-project/issues/55168.
+; We should always return 1 (and not -1).
+define i32 @test(i32 %call.i) {
+; CHECK-LABEL: test:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    li a0, 1
+; CHECK-NEXT:    ret
+  %t2 = insertelement <2 x i32> <i32 poison, i32 -2147483648>, i32 %call.i, i64 0
+  %t3 = icmp slt <2 x i32> %t2, <i32 -2147483646, i32 -2147483646>
+  %t4 = zext <2 x i1> %t3 to <2 x i32>
+  %t6 = extractelement <2 x i32> %t4, i64 1
+  ret i32 %t6
+}
Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -5580,9 +5580,12 @@
       !llvm::all_of(Ops, IsScalarOrSameVectorSize))
     return SDValue();
 
-  // 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.
+  // If we are comparing vectors, then the result needs to be a i1 boolean that
+  // is then extended back to the legal result type depending on how booleans
+  // are represented.
   EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
+  bool ExtendToBooleanContent =
+      Opcode == ISD::SETCC && SVT != VT.getScalarType();
 
   // Find legal integer scalar type for constant promotion and
   // ensure that its scalar size is at least as large as source.
@@ -5639,8 +5642,14 @@
     SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps);
 
     // Legalize the (integer) scalar constant if necessary.
-    if (LegalSVT != SVT)
-      ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
+    if (LegalSVT != SVT) {
+      if (ExtendToBooleanContent) {
+        ISD::NodeType ExtendCode =
+            TargetLowering::getExtendForContent(TLI->getBooleanContents(VT));
+        ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
+      } else
+        ScalarResult = getNode(ISD::SIGN_EXTEND, DL, LegalSVT, ScalarResult);
+    }
 
     // Scalar folding only succeeded if the result is a constant or UNDEF.
     if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124618.425791.patch
Type: text/x-patch
Size: 2657 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220428/694da220/attachment.bin>


More information about the llvm-commits mailing list