[PATCH] D52102: [ARM] Disallow icmp with negative imm and overflow

Sam Parker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 14 09:22:07 PDT 2018


samparker created this revision.
samparker added reviewers: olista01, john.brawn, SjoerdMeijer.
Herald added a reviewer: javed.absar.
Herald added subscribers: chrib, kristof.beyls.

We allow overflowing instructions if they're decreasing and only used by an unsigned compare. Add the extra condition that the icmp cannot be using a negative immediate.


https://reviews.llvm.org/D52102

Files:
  lib/Target/ARM/ARMCodeGenPrepare.cpp
  test/CodeGen/ARM/arm-cgp-icmps.ll


Index: test/CodeGen/ARM/arm-cgp-icmps.ll
===================================================================
--- test/CodeGen/ARM/arm-cgp-icmps.ll
+++ test/CodeGen/ARM/arm-cgp-icmps.ll
@@ -288,3 +288,16 @@
   ret i32 %res
 }
 
+; CHECK-COMMON-LABEL: icmp_minus_imm
+; CHECK-COMMON: subs [[SUB:r[0-9]+]],
+; CHECK-COMMON: uxtb [[UXT:r[0-9]+]],
+; CHECK-COMMON: cmp [[UXT]], #251
+define hidden i32 @icmp_minus_imm(i8* %a) {
+entry:
+  %0 = load i8, i8* %a, align 1
+  %add.i = add i8 %0, -7
+  %cmp = icmp ugt i8 %add.i, -5
+  %conv1 = zext i1 %cmp to i32
+  ret i32 %conv1
+}
+
Index: lib/Target/ARM/ARMCodeGenPrepare.cpp
===================================================================
--- lib/Target/ARM/ARMCodeGenPrepare.cpp
+++ lib/Target/ARM/ARMCodeGenPrepare.cpp
@@ -257,9 +257,20 @@
       return false;
 
     auto *CI = cast<ICmpInst>(*I->user_begin());
+    
+    // Don't support an icmp that deals with sign bits, including negative
+    // immediates
     if (CI->isSigned())
       return false;
 
+    if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(0)))
+      if (Const->isNegative())
+        return false;
+
+    if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(1)))
+      if (Const->isNegative())
+        return false;
+
     bool NegImm = cast<ConstantInt>(I->getOperand(1))->isNegative();
     bool IsDecreasing = ((Opc == Instruction::Sub) && !NegImm) ||
                         ((Opc == Instruction::Add) && NegImm);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52102.165524.patch
Type: text/x-patch
Size: 1460 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180914/95e78a98/attachment.bin>


More information about the llvm-commits mailing list