[llvm] r342392 - [ARM] Disallow icmp with negative imm and overflow

Sam Parker via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 17 06:48:25 PDT 2018


Author: sam_parker
Date: Mon Sep 17 06:48:25 2018
New Revision: 342392

URL: http://llvm.org/viewvc/llvm-project?rev=342392&view=rev
Log:
[ARM] Disallow icmp with negative imm and overflow

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.

Differential Revision: https://reviews.llvm.org/D52102

Modified:
    llvm/trunk/lib/Target/ARM/ARMCodeGenPrepare.cpp
    llvm/trunk/test/CodeGen/ARM/arm-cgp-icmps.ll

Modified: llvm/trunk/lib/Target/ARM/ARMCodeGenPrepare.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMCodeGenPrepare.cpp?rev=342392&r1=342391&r2=342392&view=diff
==============================================================================
--- llvm/trunk/lib/Target/ARM/ARMCodeGenPrepare.cpp (original)
+++ llvm/trunk/lib/Target/ARM/ARMCodeGenPrepare.cpp Mon Sep 17 06:48:25 2018
@@ -257,9 +257,20 @@ static bool isSafeOverflow(Instruction *
       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);

Modified: llvm/trunk/test/CodeGen/ARM/arm-cgp-icmps.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/ARM/arm-cgp-icmps.ll?rev=342392&r1=342391&r2=342392&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/ARM/arm-cgp-icmps.ll (original)
+++ llvm/trunk/test/CodeGen/ARM/arm-cgp-icmps.ll Mon Sep 17 06:48:25 2018
@@ -288,3 +288,25 @@ define i32 @icmp_i15(i15 zeroext %arg0,
   ret i32 %res
 }
 
+; CHECK-COMMON-LABEL: icmp_minus_imm
+; CHECK-NODSP: subs [[SUB:r[0-9]+]],
+; CHECK-NODSP: uxtb [[UXT:r[0-9]+]],
+; CHECK-NODSP: cmp [[UXT]], #251
+
+; CHECK-DSP: subs [[SUB:r[0-9]+]],
+; CHECK-DSP: uxtb [[UXT:r[0-9]+]],
+; CHECK-DSP: cmp [[UXT]], #251
+
+; CHECK-DSP-IMM: ldrb [[A:r[0-9]+]],
+; CHECK-DSP-IMM: movs  [[MINUS_7:r[0-9]+]], #249
+; CHECK-DSP-IMM: uadd8 [[RES:r[0-9]+]], [[A]], [[MINUS_7]]
+; CHECK-DSP-IMM: cmp [[RES]], #251
+define 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
+}
+




More information about the llvm-commits mailing list