[PATCH] D131113: [RISCV] Prevent infinite loop after D129980.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 3 13:32:20 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: asb, reames, luismarques, frasercrmck.
Herald added subscribers: sunshaoce, VincentWu, luke957, StephenFan, vkmr, evandro, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, arichardson.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added subscribers: pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.

D129980 <https://reviews.llvm.org/D129980> converts (seteq (i64 (and X, 0xffffffff)), C1 <https://reviews.llvm.org/C1>) into
(seteq (i64 (sext_inreg X, i32)), C1 <https://reviews.llvm.org/C1>). If bit 31 of X is 0, it
will be turned back into an 'and' by SimplifyDemandedBits which
can cause an infinite loop.

To prevent this, check if bit 31 is 0 with computeKnownBits before
doing the transformation.

Fixes PR56905.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131113

Files:
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/test/CodeGen/RISCV/i64-icmp.ll


Index: llvm/test/CodeGen/RISCV/i64-icmp.ll
===================================================================
--- llvm/test/CodeGen/RISCV/i64-icmp.ll
+++ llvm/test/CodeGen/RISCV/i64-icmp.ll
@@ -736,3 +736,25 @@
   %3 = zext i1 %2 to i64
   ret i64 %3
 }
+
+; This used to trigger an infinite loop where we toggled between 'and' and
+; 'sext_inreg'.
+define i64 @icmp_ne_zext_inreg_umin(i64 %a) nounwind {
+; RV64I-LABEL: icmp_ne_zext_inreg_umin:
+; RV64I:       # %bb.0:
+; RV64I-NEXT:    lui a1, 30141
+; RV64I-NEXT:    addiw a1, a1, -747
+; RV64I-NEXT:    bltu a0, a1, .LBB66_2
+; RV64I-NEXT:  # %bb.1:
+; RV64I-NEXT:    mv a0, a1
+; RV64I-NEXT:  .LBB66_2:
+; RV64I-NEXT:    addi a0, a0, -123
+; RV64I-NEXT:    snez a0, a0
+; RV64I-NEXT:    ret
+  %1 = call i64 @llvm.umin.i64(i64 %a, i64 123456789)
+  %2 = and i64 %1, 4294967295
+  %3 = icmp ne i64 %2, 123
+  %4 = zext i1 %3 to i64
+  ret i64 %4
+}
+declare i64 @llvm.umin.i64(i64, i64)
Index: llvm/lib/Target/RISCV/RISCVISelLowering.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -8370,6 +8370,12 @@
   if (!isIntEqualitySetCC(Cond))
     return SDValue();
 
+  // Don't do this if the sign bit is provably zero, it will be turned back into
+  // an AND.
+  APInt SignMask = APInt::getOneBitSet(64, 31);
+  if (DAG.MaskedValueIsZero(N0.getOperand(0), SignMask))
+    return SDValue();
+
   const APInt &C1 = N1C->getAPIntValue();
 
   SDLoc dl(N);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131113.449773.patch
Type: text/x-patch
Size: 1529 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220803/164ee2af/attachment.bin>


More information about the llvm-commits mailing list