[PATCH] D48907: [ARM] Treat cmn immediates as legal in isLegalICmpImmediate.

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 3 17:08:44 PDT 2018


efriedma created this revision.
efriedma added reviewers: t.p.northover, samparker, SjoerdMeijer.
Herald added a reviewer: javed.absar.
Herald added subscribers: chrib, kristof.beyls.

The original code attempted to do this, but the std::abs() call didn't actually do anything due to implicit type conversions.  Fix the type conversions, and perform the correct check for negated immediates.

This probably has very little practical impact, but it's worth fixing just to avoid confusion in the future, I think.


Repository:
  rL LLVM

https://reviews.llvm.org/D48907

Files:
  lib/Target/ARM/ARMISelLowering.cpp
  test/CodeGen/Thumb2/thumb2-cmp.ll


Index: test/CodeGen/Thumb2/thumb2-cmp.ll
===================================================================
--- test/CodeGen/Thumb2/thumb2-cmp.ll
+++ test/CodeGen/Thumb2/thumb2-cmp.ll
@@ -169,7 +169,7 @@
 
 define i32 @slt_neg_soimm(i32 %a) {
 ; CHECK-LABEL: slt_neg_soimm:
-; CHECK: mvn     r1, #7929856
+; CHECK: cmn.w r0, #7929856
   %b = icmp slt i32 %a, -7929856
   br i1 %b, label %true, label %false
 
@@ -208,8 +208,7 @@
 
 define i32 @sgt_neg_soimm(i32 %a) {
 ; CHECK-LABEL: sgt_neg_soimm:
-; CHECK: movs    r1, #1
-; CHECK: movt    r1, #65415
+; CHECK: cmn.w r0, #7929856
   %b = icmp sgt i32 %a, -7929856
   br i1 %b, label %true, label %false
 
Index: lib/Target/ARM/ARMISelLowering.cpp
===================================================================
--- lib/Target/ARM/ARMISelLowering.cpp
+++ lib/Target/ARM/ARMISelLowering.cpp
@@ -3857,8 +3857,9 @@
                                      const SDLoc &dl) const {
   if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
     unsigned C = RHSC->getZExtValue();
-    if (!isLegalICmpImmediate(C)) {
-      // Constant does not fit, try adjusting it by one?
+    if (!isLegalICmpImmediate((int32_t)C) ||
+        (C == -1U && (CC == ISD::SETLE || CC == ISD::SETGT))) {
+      // Constant does not fit, try adjusting it by one.
       switch (CC) {
       default: break;
       case ISD::SETLT:
@@ -13195,9 +13196,11 @@
 bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
   // Thumb2 and ARM modes can use cmn for negative immediates.
   if (!Subtarget->isThumb())
-    return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
+    return ARM_AM::getSOImmVal((uint32_t)Imm) != -1 ||
+           ARM_AM::getSOImmVal(-(uint32_t)Imm) != -1;
   if (Subtarget->isThumb2())
-    return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
+    return ARM_AM::getT2SOImmVal((uint32_t)Imm) != -1 ||
+           ARM_AM::getT2SOImmVal(-(uint32_t)Imm) != -1;
   // Thumb1 doesn't have cmn, and only 8-bit immediates.
   return Imm >= 0 && Imm <= 255;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48907.154023.patch
Type: text/x-patch
Size: 2023 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180704/1693b058/attachment.bin>


More information about the llvm-commits mailing list