[FastISel][AArch64] Fold offset into the memory operation.
authorJuergen Ributzka <juergen@apple.com>
Fri, 1 Aug 2014 19:40:16 +0000 (19:40 +0000)
committerJuergen Ributzka <juergen@apple.com>
Fri, 1 Aug 2014 19:40:16 +0000 (19:40 +0000)
Fold simple offsets into the memory operation:
  add x0, x0, #8
  ldr x0, [x0]
-->
  ldr x0, [x0, #8]

Fixes <rdar://problem/17887945>.

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

lib/Target/AArch64/AArch64FastISel.cpp
test/CodeGen/AArch64/fast-isel-compute-address.ll [new file with mode: 0644]

index f748185bc9579b90af46e0c2e548fe932c68b250..8cfdab24651fe868c07e19cf9c6e39ab88cdfa7b 100644 (file)
@@ -419,6 +419,13 @@ bool AArch64FastISel::ComputeAddress(const Value *Obj, Address &Addr) {
     }
     break;
   }
+  case Instruction::Add:
+    // Adds of constants are common and easy enough.
+    if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
+      Addr.setOffset(Addr.getOffset() + (uint64_t)CI->getSExtValue());
+      return ComputeAddress(U->getOperand(0), Addr);
+    }
+    break;
   }
 
   // Try to get this in a register if nothing else has worked.
diff --git a/test/CodeGen/AArch64/fast-isel-compute-address.ll b/test/CodeGen/AArch64/fast-isel-compute-address.ll
new file mode 100644 (file)
index 0000000..f0b198c
--- /dev/null
@@ -0,0 +1,23 @@
+; RUN: llc -fast-isel -fast-isel-abort -mtriple=arm64-apple-darwin < %s | FileCheck %s
+
+; Test simple constant offset.
+define i64 @test_load1(i64 %a) {
+; CHECK-LABEL: test_load1
+; CHECK: ldr  x0, [x0, #16]
+  %1 = add i64 %a, 16
+  %2 = inttoptr i64 %1 to i64*
+  %3 = load i64* %2
+  ret i64 %3
+}
+
+; Test large constant offset.
+define i64 @test_load2(i64 %a) {
+; CHECK-LABEL: test_load2
+; CHECK: add [[REG:x[0-9]+]], x0, {{x[0-9]+}}
+; CHECK: ldr  x0, {{\[}}[[REG]]{{\]}}
+  %1 = add i64 %a, 16777216
+  %2 = inttoptr i64 %1 to i64*
+  %3 = load i64* %2
+  ret i64 %3
+}
+