[llvm] r281044 - [ARM] ADD with a negative offset can become SUB for free

James Molloy via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 9 06:35:37 PDT 2016


Author: jamesm
Date: Fri Sep  9 08:35:36 2016
New Revision: 281044

URL: http://llvm.org/viewvc/llvm-project?rev=281044&view=rev
Log:
[ARM] ADD with a negative offset can become SUB for free

So model that directly in TTI::getIntImmCost().

Modified:
    llvm/trunk/lib/Target/ARM/ARMTargetTransformInfo.cpp
    llvm/trunk/test/CodeGen/ARM/immcost.ll

Modified: llvm/trunk/lib/Target/ARM/ARMTargetTransformInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMTargetTransformInfo.cpp?rev=281044&r1=281043&r2=281044&view=diff
==============================================================================
--- llvm/trunk/lib/Target/ARM/ARMTargetTransformInfo.cpp (original)
+++ llvm/trunk/lib/Target/ARM/ARMTargetTransformInfo.cpp Fri Sep  9 08:35:36 2016
@@ -73,6 +73,10 @@ int ARMTTIImpl::getIntImmCost(unsigned O
       // Conversion to BIC is free, and means we can use ~Imm instead.
       return std::min(getIntImmCost(Imm, Ty), getIntImmCost(~Imm, Ty));
 
+  if (Opcode == Instruction::Add)
+    // Conversion to SUB is free, and means we can use -Imm instead.
+    return std::min(getIntImmCost(Imm, Ty), getIntImmCost(-Imm, Ty));
+
   if (Opcode == Instruction::ICmp && Imm.isNegative() &&
       Ty->getIntegerBitWidth() == 32) {
     int64_t NegImm = -Imm.getSExtValue();

Modified: llvm/trunk/test/CodeGen/ARM/immcost.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/ARM/immcost.ll?rev=281044&r1=281043&r2=281044&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/ARM/immcost.ll (original)
+++ llvm/trunk/test/CodeGen/ARM/immcost.ll Fri Sep  9 08:35:36 2016
@@ -71,3 +71,20 @@ true:
 ret:
   ret void
 }
+
+; CHECK: Function: test_add_neg
+; CHECK-NOT: Collect constant i32 -5
+define void @test_add_neg(i1 %cond, i32 %arg, i32 %arg2) {
+entry:
+  %a = add i32 %arg, -5
+  call void @g(i32 %a)
+  br i1 %cond, label %true, label %ret
+
+true:
+  %b = add i32 %arg2, -5
+  call void @g(i32 %b)
+  br label %ret
+
+ret:
+  ret void
+}




More information about the llvm-commits mailing list