OSDN Git Service

Reapply [AArch64] Fix isLegalAddImmediate() to return true for valid negative values.
authorGeoff Berry <gberry@codeaurora.org>
Tue, 7 Jun 2016 16:48:43 +0000 (16:48 +0000)
committerGeoff Berry <gberry@codeaurora.org>
Tue, 7 Jun 2016 16:48:43 +0000 (16:48 +0000)
Originally reviewed here: http://reviews.llvm.org/D17463

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272023 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/AArch64/AArch64ISelLowering.cpp
test/CodeGen/AArch64/neg-imm.ll [new file with mode: 0644]

index 8d91246..8d10384 100644 (file)
@@ -7313,14 +7313,17 @@ EVT AArch64TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
 
 // 12-bit optionally shifted immediates are legal for adds.
 bool AArch64TargetLowering::isLegalAddImmediate(int64_t Immed) const {
+  // Avoid UB for INT64_MIN.
+  if (Immed == std::numeric_limits<int64_t>::min())
+    return false;
+  // Same encoding for add/sub, just flip the sign.
+  Immed = std::abs(Immed);
   return ((Immed >> 12) == 0 || ((Immed & 0xfff) == 0 && Immed >> 24 == 0));
 }
 
 // Integer comparisons are implemented with ADDS/SUBS, so the range of valid
 // immediates is the same as for an add or a sub.
 bool AArch64TargetLowering::isLegalICmpImmediate(int64_t Immed) const {
-  if (Immed < 0)
-    Immed *= -1;
   return isLegalAddImmediate(Immed);
 }
 
diff --git a/test/CodeGen/AArch64/neg-imm.ll b/test/CodeGen/AArch64/neg-imm.ll
new file mode 100644 (file)
index 0000000..375d3db
--- /dev/null
@@ -0,0 +1,46 @@
+; RUN: llc -mtriple=aarch64-linux-gnu -verify-machineinstrs -o - %s | FileCheck %s
+; LSR used to pick a sub-optimal solution due to the target responding
+; conservatively to isLegalAddImmediate for negative values.
+
+declare void @foo(i32)
+
+define void @test(i32 %px) {
+; CHECK_LABEL: test:
+; CHECK_LABEL: %entry
+; CHECK: subs
+; CHECK-NEXT: csel
+entry:
+  %sub = add nsw i32 %px, -1
+  %cmp = icmp slt i32 %px, 1
+  %.sub = select i1 %cmp, i32 0, i32 %sub
+  br label %for.body
+
+for.body:
+; CHECK_LABEL: %for.body
+; CHECK:  cmp
+; CHECK-NEXT:  b.eq
+; CHECK-LABEL:  %if.then3
+  %x.015 = phi i32 [ %inc, %for.inc ], [ %.sub, %entry ]
+  %cmp2 = icmp eq i32 %x.015, %px
+  br i1 %cmp2, label %for.inc, label %if.then3
+
+if.then3:
+  tail call void @foo(i32 %x.015)
+  br label %for.inc
+
+for.inc:
+; CHECK_LABEL: %for.inc
+; CHECK:  add
+; CHECK-NEXT:  cmp
+; CHECK:  b.le
+; CHECK_LABEL: %for.cond.cleanup
+  %inc = add nsw i32 %x.015, 1
+  %cmp1 = icmp sgt i32 %x.015, %px
+  br i1 %cmp1, label %for.cond.cleanup.loopexit, label %for.body
+
+for.cond.cleanup.loopexit:
+  br label %for.cond.cleanup
+
+for.cond.cleanup:
+  ret void
+}