From 22b557d94277947d88029ed20a89381842ec22b9 Mon Sep 17 00:00:00 2001 From: Juergen Ributzka Date: Thu, 18 Sep 2014 05:40:47 +0000 Subject: [PATCH] [FastISel][AArch64] Try to fold the offset into the add instruction when simplifying a memory address. Small optimization in 'simplifyAddress'. When the offset cannot be encoded in the load/store instruction, then we need to materialize the address manually. The add instruction can encode a wider range of immediates than the load/store instructions. This change tries to fold the offset into the add instruction first before materializing the offset in a register. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218031 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/AArch64/AArch64FastISel.cpp | 14 +++++-- .../AArch64/fast-isel-addressing-modes.ll | 38 +++++++------------ 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/lib/Target/AArch64/AArch64FastISel.cpp b/lib/Target/AArch64/AArch64FastISel.cpp index 0e9ef990962..ff56209d2ff 100644 --- a/lib/Target/AArch64/AArch64FastISel.cpp +++ b/lib/Target/AArch64/AArch64FastISel.cpp @@ -918,10 +918,16 @@ bool AArch64FastISel::simplifyAddress(Address &Addr, MVT VT) { // reg+offset into a register. if (ImmediateOffsetNeedsLowering) { unsigned ResultReg = 0; - if (Addr.getReg()) - ResultReg = fastEmit_ri_(MVT::i64, ISD::ADD, Addr.getReg(), - /*IsKill=*/false, Offset, MVT::i64); - else + if (Addr.getReg()) { + // Try to fold the immediate into the add instruction. + ResultReg = emitAddSub_ri(/*UseAdd=*/true, MVT::i64, Addr.getReg(), + /*IsKill=*/false, Offset); + if (!ResultReg) { + unsigned ImmReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset); + ResultReg = emitAddSub_rr(/*UseAdd=*/true, MVT::i64, Addr.getReg(), + /*IsKill=*/false, ImmReg, /*IsKill=*/true); + } + } else ResultReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset); if (!ResultReg) diff --git a/test/CodeGen/AArch64/fast-isel-addressing-modes.ll b/test/CodeGen/AArch64/fast-isel-addressing-modes.ll index b33c06ee278..88a845821f2 100644 --- a/test/CodeGen/AArch64/fast-isel-addressing-modes.ll +++ b/test/CodeGen/AArch64/fast-isel-addressing-modes.ll @@ -154,12 +154,9 @@ define i32 @load_breg_immoff_3(i64 %a) { ; Min un-supported unscaled offset define i32 @load_breg_immoff_4(i64 %a) { -; SDAG-LABEL: load_breg_immoff_4 -; SDAG: add [[REG:x[0-9]+]], x0, #257 -; SDAG-NEXT: ldr {{w[0-9]+}}, {{\[}}[[REG]]{{\]}} -; FAST-LABEL: load_breg_immoff_4 -; FAST: add [[REG:x[0-9]+]], x0, {{x[0-9]+}} -; FAST-NEXT: ldr {{w[0-9]+}}, {{\[}}[[REG]]{{\]}} +; CHECK-LABEL: load_breg_immoff_4 +; CHECK: add [[REG:x[0-9]+]], x0, #257 +; CHECK-NEXT: ldr {{w[0-9]+}}, {{\[}}[[REG]]{{\]}} %1 = add i64 %a, 257 %2 = inttoptr i64 %1 to i32* %3 = load i32* %2 @@ -178,12 +175,9 @@ define i32 @load_breg_immoff_5(i64 %a) { ; Min un-supported scaled offset define i32 @load_breg_immoff_6(i64 %a) { -; SDAG-LABEL: load_breg_immoff_6 -; SDAG: add [[REG:x[0-9]+]], x0, #4, lsl #12 -; SDAG-NEXT: ldr {{w[0-9]+}}, {{\[}}[[REG]]{{\]}} -; FAST-LABEL: load_breg_immoff_6 -; FAST: add [[REG:x[0-9]+]], x0, {{x[0-9]+}} -; FAST-NEXT: ldr {{w[0-9]+}}, {{\[}}[[REG]]{{\]}} +; CHECK-LABEL: load_breg_immoff_6 +; CHECK: add [[REG:x[0-9]+]], x0, #4, lsl #12 +; CHECK-NEXT: ldr {{w[0-9]+}}, {{\[}}[[REG]]{{\]}} %1 = add i64 %a, 16384 %2 = inttoptr i64 %1 to i32* %3 = load i32* %2 @@ -226,12 +220,9 @@ define void @store_breg_immoff_3(i64 %a) { ; Min un-supported unscaled offset define void @store_breg_immoff_4(i64 %a) { -; SDAG-LABEL: store_breg_immoff_4 -; SDAG: add [[REG:x[0-9]+]], x0, #257 -; SDAG-NEXT: str wzr, {{\[}}[[REG]]{{\]}} -; FAST-LABEL: store_breg_immoff_4 -; FAST: add [[REG:x[0-9]+]], x0, {{x[0-9]+}} -; FAST-NEXT: str wzr, {{\[}}[[REG]]{{\]}} +; CHECK-LABEL: store_breg_immoff_4 +; CHECK: add [[REG:x[0-9]+]], x0, #257 +; CHECK-NEXT: str wzr, {{\[}}[[REG]]{{\]}} %1 = add i64 %a, 257 %2 = inttoptr i64 %1 to i32* store i32 0, i32* %2 @@ -250,12 +241,9 @@ define void @store_breg_immoff_5(i64 %a) { ; Min un-supported scaled offset define void @store_breg_immoff_6(i64 %a) { -; SDAG-LABEL: store_breg_immoff_6 -; SDAG: add [[REG:x[0-9]+]], x0, #4, lsl #12 -; SDAG-NEXT: str wzr, {{\[}}[[REG]]{{\]}} -; FAST-LABEL: store_breg_immoff_6 -; FAST: add [[REG:x[0-9]+]], x0, {{x[0-9]+}} -; FAST-NEXT: str wzr, {{\[}}[[REG]]{{\]}} +; CHECK-LABEL: store_breg_immoff_6 +; CHECK: add [[REG:x[0-9]+]], x0, #4, lsl #12 +; CHECK-NEXT: str wzr, {{\[}}[[REG]]{{\]}} %1 = add i64 %a, 16384 %2 = inttoptr i64 %1 to i32* store i32 0, i32* %2 @@ -319,7 +307,7 @@ define i64 @load_breg_offreg_immoff_2(i64 %a, i64 %b) { ; SDAG-NEXT: add [[REG2:x[0-9]+]], [[REG1]], #15, lsl #12 ; SDAG-NEXT: ldr x0, {{\[}}[[REG2]]{{\]}} ; FAST-LABEL: load_breg_offreg_immoff_2 -; FAST: add [[REG:x[0-9]+]], x0, {{x[0-9]+}} +; FAST: add [[REG:x[0-9]+]], x0, #15, lsl #12 ; FAST-NEXT: ldr x0, {{\[}}[[REG]], x1{{\]}} %1 = add i64 %a, %b %2 = add i64 %1, 61440