Skip to content

Commit 8d8cb1a

Browse files
Congzhe Caodancgr
authored andcommitted
[AArch64] Avoid pairing loads when the base reg is modified
When pairing loads, we should check if in between the two loads the base register has been modified. If that is the case then avoid pairing them because the second load actually loads from a different address. Reviewed By: fhahn Differential Revision: https://reviews.llvm.org/D86956
1 parent 73fb969 commit 8d8cb1a

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

llvm/lib/Target/AArch64/AArch64LoadStoreOptimizer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,15 @@ AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
15641564
continue;
15651565
}
15661566

1567+
// If the BaseReg has been modified, then we cannot do the optimization.
1568+
// For example, in the following pattern
1569+
// ldr x1 [x2]
1570+
// ldr x2 [x3]
1571+
// ldr x4 [x2, #8],
1572+
// the first and third ldr cannot be converted to ldp x1, x4, [x2]
1573+
if (!ModifiedRegUnits.available(BaseReg))
1574+
return E;
1575+
15671576
// If the Rt of the second instruction was not modified or used between
15681577
// the two instructions and none of the instructions between the second
15691578
// and first alias with the second, we can combine the second into the
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# RUN: llc -mtriple=aarch64-linux-gnu -verify-machineinstrs -run-pass=aarch64-ldst-opt %s -o - | FileCheck %s
2+
#
3+
# When the AArch64 Load Store Optimization pass tries to convert load instructions
4+
# into a ldp instruction, and when the base register of the second ldr instruction
5+
# has been modified in between these two ldr instructions, the conversion should not
6+
# occur.
7+
#
8+
# For example, for the following pattern:
9+
# ldr x9 [x10]
10+
# ldr x10 [x8]
11+
# ldr x10 [x10, 8],
12+
# the first and third ldr instructions cannot be converted to ldp x9, x10, [x10].
13+
#
14+
# CHECK-LABEL: name: ldr-modified-baseReg-no-ldp1
15+
# CHECK-NOT: LDP
16+
# CHECK: $x9 = LDRXui $x10, 1 :: (load 8)
17+
# CHECK: $x10 = LDURXi $x8, 1 :: (load 8)
18+
# CHECK: $x10 = LDRXui $x10, 0 :: (load 8)
19+
# CHECK: RET
20+
---
21+
name: ldr-modified-baseReg-no-ldp1
22+
tracksRegLiveness: true
23+
body: |
24+
bb.0:
25+
liveins: $x8, $x10
26+
27+
$x9 = LDRXui $x10, 1 :: (load 8)
28+
$x10 = LDURXi $x8, 1 :: (load 8)
29+
$x10 = LDRXui $x10, 0 :: (load 8)
30+
RET undef $lr, implicit undef $w0
31+
...
32+
33+
# CHECK-LABEL: name: str-modified-baseReg-no-stp1
34+
# CHECK-NOT: STP
35+
# CHECK: STRXui $x9, $x10, 1 :: (store 8)
36+
# CHECK: $x10 = LDRXui $x8, 0 :: (load 8)
37+
# CHECK: STRXui $x10, $x10, 0 :: (store 8)
38+
# CHECK: RET
39+
---
40+
name: str-modified-baseReg-no-stp1
41+
tracksRegLiveness: true
42+
body: |
43+
bb.0:
44+
liveins: $x9, $x8, $x10
45+
46+
STRXui $x9, $x10, 1 :: (store 8)
47+
$x10 = LDRXui $x8, 0 :: (load 8)
48+
STRXui $x10, $x10, 0 :: (store 8)
49+
RET undef $lr, implicit undef $w0
50+
...
51+
52+
# CHECK-LABEL: name: ldr-modified-baseReg-no-ldp2
53+
# CHECK-NOT: LDP
54+
# CHECK: $x9 = LDRXui $x10, 1 :: (load 8)
55+
# CHECK: $x10 = MOVi64imm 13
56+
# CHECK: $x11 = LDRXui $x10, 0 :: (load 8)
57+
# CHECK: RET
58+
---
59+
name: ldr-modified-baseReg-no-ldp2
60+
tracksRegLiveness: true
61+
body: |
62+
bb.0:
63+
liveins: $x8, $x10
64+
65+
$x9 = LDRXui $x10, 1 :: (load 8)
66+
$x10 = MOVi64imm 13
67+
$x11 = LDRXui $x10, 0 :: (load 8)
68+
RET undef $lr, implicit undef $w0
69+
...
70+
71+
# CHECK-LABEL: name: ldr-modified-baseReg-no-ldp3
72+
# CHECK-NOT: LDP
73+
# CHECK: $x9 = LDRXui $x10, 1 :: (load 8)
74+
# CHECK: $x10 = ADDXri $x8, $x11, 0
75+
# CHECK: $x12 = LDRXui $x10, 0 :: (load 8)
76+
# CHECK: RET
77+
---
78+
name: ldr-modified-baseReg-no-ldp3
79+
tracksRegLiveness: true
80+
body: |
81+
bb.0:
82+
liveins: $x8, $x10, $x11
83+
84+
$x9 = LDRXui $x10, 1 :: (load 8)
85+
$x10 = ADDXri $x8, $x11, 0
86+
$x12 = LDRXui $x10, 0 :: (load 8)
87+
RET undef $lr, implicit undef $w0
88+
...
89+
90+
# CHECK-LABEL: name: ldr-modified-baseAddr-convert-to-ldp
91+
# CHECK: $x12, $x9 = LDPXi $x10, 0 :: (load 8)
92+
# CHECK: STRXui $x11, $x10, 1 :: (store 8)
93+
# CHECK: RET
94+
---
95+
name: ldr-modified-baseAddr-convert-to-ldp
96+
tracksRegLiveness: true
97+
body: |
98+
bb.0:
99+
liveins: $x8, $x10, $x11
100+
101+
$x9 = LDRXui $x10, 1 :: (load 8)
102+
STRXui $x11, $x10, 1 :: (store 8)
103+
$x12 = LDRXui $x10, 0 :: (load 8)
104+
RET undef $lr, implicit undef $w0
105+
...

0 commit comments

Comments
 (0)