blob: 7a7688bd5ace7470ba6d37113558cc82537f87bf [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//=- AArch64LoadStoreOptimizer.cpp - AArch64 load/store opt. pass -*- C++ -*-=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a pass that performs load / store related peephole
11// optimizations. This pass should be run after register allocation.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AArch64InstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000016#include "AArch64Subtarget.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000017#include "MCTargetDesc/AArch64AddressingModes.h"
18#include "llvm/ADT/BitVector.h"
Chad Rosierce8e5ab2015-05-21 21:36:46 +000019#include "llvm/ADT/SmallVector.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000020#include "llvm/ADT/Statistic.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000021#include "llvm/CodeGen/MachineBasicBlock.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000029#include "llvm/Target/TargetInstrInfo.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetRegisterInfo.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000032using namespace llvm;
33
34#define DEBUG_TYPE "aarch64-ldst-opt"
35
36/// AArch64AllocLoadStoreOpt - Post-register allocation pass to combine
37/// load / store instructions to form ldp / stp instructions.
38
39STATISTIC(NumPairCreated, "Number of load/store pair instructions generated");
40STATISTIC(NumPostFolded, "Number of post-index updates folded");
41STATISTIC(NumPreFolded, "Number of pre-index updates folded");
42STATISTIC(NumUnscaledPairCreated,
43 "Number of load/store from unscaled generated");
44
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +000045static cl::opt<unsigned> ScanLimit("aarch64-load-store-scan-limit",
46 cl::init(20), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000047
48// Place holder while testing unscaled load/store combining
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +000049static cl::opt<bool> EnableAArch64UnscaledMemOp(
50 "aarch64-unscaled-mem-op", cl::Hidden,
51 cl::desc("Allow AArch64 unscaled load/store combining"), cl::init(true));
Tim Northover3b0846e2014-05-24 12:50:23 +000052
Chad Rosier96530b32015-08-05 13:44:51 +000053namespace llvm {
54void initializeAArch64LoadStoreOptPass(PassRegistry &);
55}
56
57#define AARCH64_LOAD_STORE_OPT_NAME "AArch64 load / store optimization pass"
58
Tim Northover3b0846e2014-05-24 12:50:23 +000059namespace {
Chad Rosier96a18a92015-07-21 17:42:04 +000060
61typedef struct LdStPairFlags {
62 // If a matching instruction is found, MergeForward is set to true if the
63 // merge is to remove the first instruction and replace the second with
64 // a pair-wise insn, and false if the reverse is true.
65 bool MergeForward;
66
67 // SExtIdx gives the index of the result of the load pair that must be
68 // extended. The value of SExtIdx assumes that the paired load produces the
69 // value in this order: (I, returned iterator), i.e., -1 means no value has
70 // to be extended, 0 means I, and 1 means the returned iterator.
71 int SExtIdx;
72
73 LdStPairFlags() : MergeForward(false), SExtIdx(-1) {}
74
75 void setMergeForward(bool V = true) { MergeForward = V; }
76 bool getMergeForward() const { return MergeForward; }
77
78 void setSExtIdx(int V) { SExtIdx = V; }
79 int getSExtIdx() const { return SExtIdx; }
80
81} LdStPairFlags;
82
Tim Northover3b0846e2014-05-24 12:50:23 +000083struct AArch64LoadStoreOpt : public MachineFunctionPass {
84 static char ID;
Chad Rosier96530b32015-08-05 13:44:51 +000085 AArch64LoadStoreOpt() : MachineFunctionPass(ID) {
86 initializeAArch64LoadStoreOptPass(*PassRegistry::getPassRegistry());
87 }
Tim Northover3b0846e2014-05-24 12:50:23 +000088
89 const AArch64InstrInfo *TII;
90 const TargetRegisterInfo *TRI;
91
92 // Scan the instructions looking for a load/store that can be combined
93 // with the current instruction into a load/store pair.
94 // Return the matching instruction if one is found, else MBB->end().
Tim Northover3b0846e2014-05-24 12:50:23 +000095 MachineBasicBlock::iterator findMatchingInsn(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +000096 LdStPairFlags &Flags,
Tim Northover3b0846e2014-05-24 12:50:23 +000097 unsigned Limit);
98 // Merge the two instructions indicated into a single pair-wise instruction.
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +000099 // If MergeForward is true, erase the first instruction and fold its
Tim Northover3b0846e2014-05-24 12:50:23 +0000100 // operation into the second. If false, the reverse. Return the instruction
101 // following the first instruction (which may change during processing).
102 MachineBasicBlock::iterator
103 mergePairedInsns(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +0000104 MachineBasicBlock::iterator Paired,
Chad Rosierfe5399f2015-07-21 17:47:56 +0000105 const LdStPairFlags &Flags);
Tim Northover3b0846e2014-05-24 12:50:23 +0000106
107 // Scan the instruction list to find a base register update that can
108 // be combined with the current instruction (a load or store) using
109 // pre or post indexed addressing with writeback. Scan forwards.
110 MachineBasicBlock::iterator
111 findMatchingUpdateInsnForward(MachineBasicBlock::iterator I, unsigned Limit,
112 int Value);
113
114 // Scan the instruction list to find a base register update that can
115 // be combined with the current instruction (a load or store) using
116 // pre or post indexed addressing with writeback. Scan backwards.
117 MachineBasicBlock::iterator
118 findMatchingUpdateInsnBackward(MachineBasicBlock::iterator I, unsigned Limit);
119
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000120 // Find an instruction that updates the base register of the ld/st
121 // instruction.
122 bool isMatchingUpdateInsn(MachineInstr *MemMI, MachineInstr *MI,
123 unsigned BaseReg, int Offset);
124
Chad Rosier2dfd3542015-09-23 13:51:44 +0000125 // Merge a pre- or post-index base register update into a ld/st instruction.
Tim Northover3b0846e2014-05-24 12:50:23 +0000126 MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +0000127 mergeUpdateInsn(MachineBasicBlock::iterator I,
128 MachineBasicBlock::iterator Update, bool IsPreIdx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000129
130 bool optimizeBlock(MachineBasicBlock &MBB);
131
132 bool runOnMachineFunction(MachineFunction &Fn) override;
133
134 const char *getPassName() const override {
Chad Rosier96530b32015-08-05 13:44:51 +0000135 return AARCH64_LOAD_STORE_OPT_NAME;
Tim Northover3b0846e2014-05-24 12:50:23 +0000136 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000137};
138char AArch64LoadStoreOpt::ID = 0;
Jim Grosbach1eee3df2014-08-11 22:42:31 +0000139} // namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000140
Chad Rosier96530b32015-08-05 13:44:51 +0000141INITIALIZE_PASS(AArch64LoadStoreOpt, "aarch64-ldst-opt",
142 AARCH64_LOAD_STORE_OPT_NAME, false, false)
143
Chad Rosier22eb7102015-08-06 17:37:18 +0000144static bool isUnscaledLdSt(unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000145 switch (Opc) {
146 default:
147 return false;
148 case AArch64::STURSi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000149 case AArch64::STURDi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000150 case AArch64::STURQi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000151 case AArch64::STURWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000152 case AArch64::STURXi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000153 case AArch64::LDURSi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000154 case AArch64::LDURDi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000155 case AArch64::LDURQi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000156 case AArch64::LDURWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000157 case AArch64::LDURXi:
Quentin Colombet29f55332015-01-24 01:25:54 +0000158 case AArch64::LDURSWi:
159 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000160 }
161}
162
Chad Rosier22eb7102015-08-06 17:37:18 +0000163static bool isUnscaledLdSt(MachineInstr *MI) {
164 return isUnscaledLdSt(MI->getOpcode());
165}
166
Chad Rosiera4d32172015-09-29 14:57:10 +0000167// Size in bytes of the data moved by an unscaled load or store.
Chad Rosier22eb7102015-08-06 17:37:18 +0000168static int getMemSize(MachineInstr *MI) {
169 switch (MI->getOpcode()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000170 default:
Tilmann Schellera17a4322014-06-03 16:33:13 +0000171 llvm_unreachable("Opcode has unknown size!");
Chad Rosiera4d32172015-09-29 14:57:10 +0000172 case AArch64::LDRSui:
173 case AArch64::LDURSi:
174 case AArch64::LDRSWui:
175 case AArch64::LDURSWi:
176 case AArch64::LDRWui:
177 case AArch64::LDURWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000178 case AArch64::STRSui:
179 case AArch64::STURSi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000180 case AArch64::STRWui:
181 case AArch64::STURWi:
182 return 4;
Chad Rosiera4d32172015-09-29 14:57:10 +0000183 case AArch64::LDRDui:
184 case AArch64::LDURDi:
185 case AArch64::LDRXui:
186 case AArch64::LDURXi:
187 case AArch64::STRDui:
188 case AArch64::STURDi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000189 case AArch64::STRXui:
190 case AArch64::STURXi:
191 return 8;
Tim Northover3b0846e2014-05-24 12:50:23 +0000192 case AArch64::LDRQui:
193 case AArch64::LDURQi:
Chad Rosiera4d32172015-09-29 14:57:10 +0000194 case AArch64::STRQui:
195 case AArch64::STURQi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000196 return 16;
Tim Northover3b0846e2014-05-24 12:50:23 +0000197 }
198}
199
Quentin Colombet66b61632015-03-06 22:42:10 +0000200static unsigned getMatchingNonSExtOpcode(unsigned Opc,
201 bool *IsValidLdStrOpc = nullptr) {
202 if (IsValidLdStrOpc)
203 *IsValidLdStrOpc = true;
204 switch (Opc) {
205 default:
206 if (IsValidLdStrOpc)
207 *IsValidLdStrOpc = false;
208 return UINT_MAX;
209 case AArch64::STRDui:
210 case AArch64::STURDi:
211 case AArch64::STRQui:
212 case AArch64::STURQi:
213 case AArch64::STRWui:
214 case AArch64::STURWi:
215 case AArch64::STRXui:
216 case AArch64::STURXi:
217 case AArch64::LDRDui:
218 case AArch64::LDURDi:
219 case AArch64::LDRQui:
220 case AArch64::LDURQi:
221 case AArch64::LDRWui:
222 case AArch64::LDURWi:
223 case AArch64::LDRXui:
224 case AArch64::LDURXi:
225 case AArch64::STRSui:
226 case AArch64::STURSi:
227 case AArch64::LDRSui:
228 case AArch64::LDURSi:
229 return Opc;
230 case AArch64::LDRSWui:
231 return AArch64::LDRWui;
232 case AArch64::LDURSWi:
233 return AArch64::LDURWi;
234 }
235}
236
Tim Northover3b0846e2014-05-24 12:50:23 +0000237static unsigned getMatchingPairOpcode(unsigned Opc) {
238 switch (Opc) {
239 default:
240 llvm_unreachable("Opcode has no pairwise equivalent!");
241 case AArch64::STRSui:
242 case AArch64::STURSi:
243 return AArch64::STPSi;
244 case AArch64::STRDui:
245 case AArch64::STURDi:
246 return AArch64::STPDi;
247 case AArch64::STRQui:
248 case AArch64::STURQi:
249 return AArch64::STPQi;
250 case AArch64::STRWui:
251 case AArch64::STURWi:
252 return AArch64::STPWi;
253 case AArch64::STRXui:
254 case AArch64::STURXi:
255 return AArch64::STPXi;
256 case AArch64::LDRSui:
257 case AArch64::LDURSi:
258 return AArch64::LDPSi;
259 case AArch64::LDRDui:
260 case AArch64::LDURDi:
261 return AArch64::LDPDi;
262 case AArch64::LDRQui:
263 case AArch64::LDURQi:
264 return AArch64::LDPQi;
265 case AArch64::LDRWui:
266 case AArch64::LDURWi:
267 return AArch64::LDPWi;
268 case AArch64::LDRXui:
269 case AArch64::LDURXi:
270 return AArch64::LDPXi;
Quentin Colombet29f55332015-01-24 01:25:54 +0000271 case AArch64::LDRSWui:
272 case AArch64::LDURSWi:
273 return AArch64::LDPSWi;
Tim Northover3b0846e2014-05-24 12:50:23 +0000274 }
275}
276
277static unsigned getPreIndexedOpcode(unsigned Opc) {
278 switch (Opc) {
279 default:
280 llvm_unreachable("Opcode has no pre-indexed equivalent!");
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000281 case AArch64::STRSui:
282 return AArch64::STRSpre;
283 case AArch64::STRDui:
284 return AArch64::STRDpre;
285 case AArch64::STRQui:
286 return AArch64::STRQpre;
287 case AArch64::STRWui:
288 return AArch64::STRWpre;
289 case AArch64::STRXui:
290 return AArch64::STRXpre;
291 case AArch64::LDRSui:
292 return AArch64::LDRSpre;
293 case AArch64::LDRDui:
294 return AArch64::LDRDpre;
295 case AArch64::LDRQui:
296 return AArch64::LDRQpre;
297 case AArch64::LDRWui:
298 return AArch64::LDRWpre;
299 case AArch64::LDRXui:
300 return AArch64::LDRXpre;
Quentin Colombet29f55332015-01-24 01:25:54 +0000301 case AArch64::LDRSWui:
302 return AArch64::LDRSWpre;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000303 case AArch64::LDPSi:
304 return AArch64::LDPSpre;
305 case AArch64::LDPDi:
306 return AArch64::LDPDpre;
307 case AArch64::LDPQi:
308 return AArch64::LDPQpre;
309 case AArch64::LDPWi:
310 return AArch64::LDPWpre;
311 case AArch64::LDPXi:
312 return AArch64::LDPXpre;
313 case AArch64::STPSi:
314 return AArch64::STPSpre;
315 case AArch64::STPDi:
316 return AArch64::STPDpre;
317 case AArch64::STPQi:
318 return AArch64::STPQpre;
319 case AArch64::STPWi:
320 return AArch64::STPWpre;
321 case AArch64::STPXi:
322 return AArch64::STPXpre;
Tim Northover3b0846e2014-05-24 12:50:23 +0000323 }
324}
325
326static unsigned getPostIndexedOpcode(unsigned Opc) {
327 switch (Opc) {
328 default:
329 llvm_unreachable("Opcode has no post-indexed wise equivalent!");
330 case AArch64::STRSui:
331 return AArch64::STRSpost;
332 case AArch64::STRDui:
333 return AArch64::STRDpost;
334 case AArch64::STRQui:
335 return AArch64::STRQpost;
336 case AArch64::STRWui:
337 return AArch64::STRWpost;
338 case AArch64::STRXui:
339 return AArch64::STRXpost;
340 case AArch64::LDRSui:
341 return AArch64::LDRSpost;
342 case AArch64::LDRDui:
343 return AArch64::LDRDpost;
344 case AArch64::LDRQui:
345 return AArch64::LDRQpost;
346 case AArch64::LDRWui:
347 return AArch64::LDRWpost;
348 case AArch64::LDRXui:
349 return AArch64::LDRXpost;
Quentin Colombet29f55332015-01-24 01:25:54 +0000350 case AArch64::LDRSWui:
351 return AArch64::LDRSWpost;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000352 case AArch64::LDPSi:
353 return AArch64::LDPSpost;
354 case AArch64::LDPDi:
355 return AArch64::LDPDpost;
356 case AArch64::LDPQi:
357 return AArch64::LDPQpost;
358 case AArch64::LDPWi:
359 return AArch64::LDPWpost;
360 case AArch64::LDPXi:
361 return AArch64::LDPXpost;
362 case AArch64::STPSi:
363 return AArch64::STPSpost;
364 case AArch64::STPDi:
365 return AArch64::STPDpost;
366 case AArch64::STPQi:
367 return AArch64::STPQpost;
368 case AArch64::STPWi:
369 return AArch64::STPWpost;
370 case AArch64::STPXi:
371 return AArch64::STPXpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000372 }
373}
374
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000375static bool isPairedLdSt(const MachineInstr *MI) {
376 switch (MI->getOpcode()) {
377 default:
378 return false;
379 case AArch64::LDPSi:
380 case AArch64::LDPDi:
381 case AArch64::LDPQi:
382 case AArch64::LDPWi:
383 case AArch64::LDPXi:
384 case AArch64::STPSi:
385 case AArch64::STPDi:
386 case AArch64::STPQi:
387 case AArch64::STPWi:
388 case AArch64::STPXi:
389 return true;
390 }
391}
392
393static const MachineOperand &getLdStRegOp(const MachineInstr *MI,
394 unsigned PairedRegOp = 0) {
395 assert(PairedRegOp < 2 && "Unexpected register operand idx.");
396 unsigned Idx = isPairedLdSt(MI) ? PairedRegOp : 0;
397 return MI->getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000398}
399
400static const MachineOperand &getLdStBaseOp(const MachineInstr *MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000401 unsigned Idx = isPairedLdSt(MI) ? 2 : 1;
402 return MI->getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000403}
404
405static const MachineOperand &getLdStOffsetOp(const MachineInstr *MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000406 unsigned Idx = isPairedLdSt(MI) ? 3 : 2;
407 return MI->getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000408}
409
Tim Northover3b0846e2014-05-24 12:50:23 +0000410MachineBasicBlock::iterator
411AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
412 MachineBasicBlock::iterator Paired,
Chad Rosier96a18a92015-07-21 17:42:04 +0000413 const LdStPairFlags &Flags) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000414 MachineBasicBlock::iterator NextI = I;
415 ++NextI;
416 // If NextI is the second of the two instructions to be merged, we need
417 // to skip one further. Either way we merge will invalidate the iterator,
418 // and we don't need to scan the new instruction, as it's a pairwise
419 // instruction, which we're not considering for further action anyway.
420 if (NextI == Paired)
421 ++NextI;
422
Chad Rosier96a18a92015-07-21 17:42:04 +0000423 int SExtIdx = Flags.getSExtIdx();
Quentin Colombet66b61632015-03-06 22:42:10 +0000424 unsigned Opc =
425 SExtIdx == -1 ? I->getOpcode() : getMatchingNonSExtOpcode(I->getOpcode());
Chad Rosier22eb7102015-08-06 17:37:18 +0000426 bool IsUnscaled = isUnscaledLdSt(Opc);
Tim Northover3b0846e2014-05-24 12:50:23 +0000427 int OffsetStride =
428 IsUnscaled && EnableAArch64UnscaledMemOp ? getMemSize(I) : 1;
429
Chad Rosier96a18a92015-07-21 17:42:04 +0000430 bool MergeForward = Flags.getMergeForward();
Quentin Colombet66b61632015-03-06 22:42:10 +0000431 unsigned NewOpc = getMatchingPairOpcode(Opc);
Tim Northover3b0846e2014-05-24 12:50:23 +0000432 // Insert our new paired instruction after whichever of the paired
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000433 // instructions MergeForward indicates.
434 MachineBasicBlock::iterator InsertionPoint = MergeForward ? Paired : I;
435 // Also based on MergeForward is from where we copy the base register operand
Tim Northover3b0846e2014-05-24 12:50:23 +0000436 // so we get the flags compatible with the input code.
Chad Rosierf77e9092015-08-06 15:50:12 +0000437 const MachineOperand &BaseRegOp =
438 MergeForward ? getLdStBaseOp(Paired) : getLdStBaseOp(I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000439
440 // Which register is Rt and which is Rt2 depends on the offset order.
441 MachineInstr *RtMI, *Rt2MI;
Chad Rosier08ef4622015-09-03 16:41:28 +0000442 if (getLdStOffsetOp(I).getImm() ==
443 getLdStOffsetOp(Paired).getImm() + OffsetStride) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000444 RtMI = Paired;
445 Rt2MI = I;
Quentin Colombet66b61632015-03-06 22:42:10 +0000446 // Here we swapped the assumption made for SExtIdx.
447 // I.e., we turn ldp I, Paired into ldp Paired, I.
448 // Update the index accordingly.
449 if (SExtIdx != -1)
450 SExtIdx = (SExtIdx + 1) % 2;
Tim Northover3b0846e2014-05-24 12:50:23 +0000451 } else {
452 RtMI = I;
453 Rt2MI = Paired;
454 }
Chad Rosier08ef4622015-09-03 16:41:28 +0000455 // Handle Unscaled
Chad Rosierf77e9092015-08-06 15:50:12 +0000456 int OffsetImm = getLdStOffsetOp(RtMI).getImm();
Chad Rosier08ef4622015-09-03 16:41:28 +0000457 if (IsUnscaled && EnableAArch64UnscaledMemOp)
458 OffsetImm /= OffsetStride;
Tim Northover3b0846e2014-05-24 12:50:23 +0000459
460 // Construct the new instruction.
461 MachineInstrBuilder MIB = BuildMI(*I->getParent(), InsertionPoint,
462 I->getDebugLoc(), TII->get(NewOpc))
Chad Rosierf77e9092015-08-06 15:50:12 +0000463 .addOperand(getLdStRegOp(RtMI))
464 .addOperand(getLdStRegOp(Rt2MI))
Tim Northover3b0846e2014-05-24 12:50:23 +0000465 .addOperand(BaseRegOp)
466 .addImm(OffsetImm);
467 (void)MIB;
468
469 // FIXME: Do we need/want to copy the mem operands from the source
470 // instructions? Probably. What uses them after this?
471
472 DEBUG(dbgs() << "Creating pair load/store. Replacing instructions:\n ");
473 DEBUG(I->print(dbgs()));
474 DEBUG(dbgs() << " ");
475 DEBUG(Paired->print(dbgs()));
476 DEBUG(dbgs() << " with instruction:\n ");
Quentin Colombet66b61632015-03-06 22:42:10 +0000477
478 if (SExtIdx != -1) {
479 // Generate the sign extension for the proper result of the ldp.
480 // I.e., with X1, that would be:
481 // %W1<def> = KILL %W1, %X1<imp-def>
482 // %X1<def> = SBFMXri %X1<kill>, 0, 31
483 MachineOperand &DstMO = MIB->getOperand(SExtIdx);
484 // Right now, DstMO has the extended register, since it comes from an
485 // extended opcode.
486 unsigned DstRegX = DstMO.getReg();
487 // Get the W variant of that register.
488 unsigned DstRegW = TRI->getSubReg(DstRegX, AArch64::sub_32);
489 // Update the result of LDP to use the W instead of the X variant.
490 DstMO.setReg(DstRegW);
491 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
492 DEBUG(dbgs() << "\n");
493 // Make the machine verifier happy by providing a definition for
494 // the X register.
495 // Insert this definition right after the generated LDP, i.e., before
496 // InsertionPoint.
497 MachineInstrBuilder MIBKill =
498 BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
499 TII->get(TargetOpcode::KILL), DstRegW)
500 .addReg(DstRegW)
501 .addReg(DstRegX, RegState::Define);
502 MIBKill->getOperand(2).setImplicit();
503 // Create the sign extension.
504 MachineInstrBuilder MIBSXTW =
505 BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
506 TII->get(AArch64::SBFMXri), DstRegX)
507 .addReg(DstRegX)
508 .addImm(0)
509 .addImm(31);
510 (void)MIBSXTW;
511 DEBUG(dbgs() << " Extend operand:\n ");
512 DEBUG(((MachineInstr *)MIBSXTW)->print(dbgs()));
513 DEBUG(dbgs() << "\n");
514 } else {
515 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
516 DEBUG(dbgs() << "\n");
517 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000518
519 // Erase the old instructions.
520 I->eraseFromParent();
521 Paired->eraseFromParent();
522
523 return NextI;
524}
525
526/// trackRegDefsUses - Remember what registers the specified instruction uses
527/// and modifies.
Pete Cooper7be8f8f2015-08-03 19:04:32 +0000528static void trackRegDefsUses(const MachineInstr *MI, BitVector &ModifiedRegs,
Tim Northover3b0846e2014-05-24 12:50:23 +0000529 BitVector &UsedRegs,
530 const TargetRegisterInfo *TRI) {
Pete Cooper7be8f8f2015-08-03 19:04:32 +0000531 for (const MachineOperand &MO : MI->operands()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000532 if (MO.isRegMask())
533 ModifiedRegs.setBitsNotInMask(MO.getRegMask());
534
535 if (!MO.isReg())
536 continue;
537 unsigned Reg = MO.getReg();
538 if (MO.isDef()) {
539 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
540 ModifiedRegs.set(*AI);
541 } else {
542 assert(MO.isUse() && "Reg operand not a def and not a use?!?");
543 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
544 UsedRegs.set(*AI);
545 }
546 }
547}
548
549static bool inBoundsForPair(bool IsUnscaled, int Offset, int OffsetStride) {
Chad Rosier3dd0e942015-08-18 16:20:03 +0000550 // Convert the byte-offset used by unscaled into an "element" offset used
551 // by the scaled pair load/store instructions.
Chad Rosier08ef4622015-09-03 16:41:28 +0000552 if (IsUnscaled)
Chad Rosier3dd0e942015-08-18 16:20:03 +0000553 Offset /= OffsetStride;
554
555 return Offset <= 63 && Offset >= -64;
Tim Northover3b0846e2014-05-24 12:50:23 +0000556}
557
558// Do alignment, specialized to power of 2 and for signed ints,
559// avoiding having to do a C-style cast from uint_64t to int when
560// using RoundUpToAlignment from include/llvm/Support/MathExtras.h.
561// FIXME: Move this function to include/MathExtras.h?
562static int alignTo(int Num, int PowOf2) {
563 return (Num + PowOf2 - 1) & ~(PowOf2 - 1);
564}
565
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000566static bool mayAlias(MachineInstr *MIa, MachineInstr *MIb,
567 const AArch64InstrInfo *TII) {
568 // One of the instructions must modify memory.
569 if (!MIa->mayStore() && !MIb->mayStore())
570 return false;
571
572 // Both instructions must be memory operations.
573 if (!MIa->mayLoadOrStore() && !MIb->mayLoadOrStore())
574 return false;
575
576 return !TII->areMemAccessesTriviallyDisjoint(MIa, MIb);
577}
578
579static bool mayAlias(MachineInstr *MIa,
580 SmallVectorImpl<MachineInstr *> &MemInsns,
581 const AArch64InstrInfo *TII) {
582 for (auto &MIb : MemInsns)
583 if (mayAlias(MIa, MIb, TII))
584 return true;
585
586 return false;
587}
588
Tim Northover3b0846e2014-05-24 12:50:23 +0000589/// findMatchingInsn - Scan the instructions looking for a load/store that can
590/// be combined with the current instruction into a load/store pair.
591MachineBasicBlock::iterator
592AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +0000593 LdStPairFlags &Flags,
Quentin Colombet66b61632015-03-06 22:42:10 +0000594 unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000595 MachineBasicBlock::iterator E = I->getParent()->end();
596 MachineBasicBlock::iterator MBBI = I;
597 MachineInstr *FirstMI = I;
598 ++MBBI;
599
Matthias Braunfa3872e2015-05-18 20:27:55 +0000600 unsigned Opc = FirstMI->getOpcode();
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000601 bool MayLoad = FirstMI->mayLoad();
Chad Rosier22eb7102015-08-06 17:37:18 +0000602 bool IsUnscaled = isUnscaledLdSt(FirstMI);
Chad Rosierf77e9092015-08-06 15:50:12 +0000603 unsigned Reg = getLdStRegOp(FirstMI).getReg();
604 unsigned BaseReg = getLdStBaseOp(FirstMI).getReg();
605 int Offset = getLdStOffsetOp(FirstMI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +0000606
607 // Early exit if the first instruction modifies the base register.
608 // e.g., ldr x0, [x0]
Tim Northover3b0846e2014-05-24 12:50:23 +0000609 if (FirstMI->modifiesRegister(BaseReg, TRI))
610 return E;
Chad Rosiercaed6db2015-08-10 17:17:19 +0000611
612 // Early exit if the offset if not possible to match. (6 bits of positive
613 // range, plus allow an extra one in case we find a later insn that matches
614 // with Offset-1)
Tim Northover3b0846e2014-05-24 12:50:23 +0000615 int OffsetStride =
616 IsUnscaled && EnableAArch64UnscaledMemOp ? getMemSize(FirstMI) : 1;
617 if (!inBoundsForPair(IsUnscaled, Offset, OffsetStride))
618 return E;
619
620 // Track which registers have been modified and used between the first insn
621 // (inclusive) and the second insn.
622 BitVector ModifiedRegs, UsedRegs;
623 ModifiedRegs.resize(TRI->getNumRegs());
624 UsedRegs.resize(TRI->getNumRegs());
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000625
626 // Remember any instructions that read/write memory between FirstMI and MI.
627 SmallVector<MachineInstr *, 4> MemInsns;
628
Tim Northover3b0846e2014-05-24 12:50:23 +0000629 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
630 MachineInstr *MI = MBBI;
631 // Skip DBG_VALUE instructions. Otherwise debug info can affect the
632 // optimization by changing how far we scan.
633 if (MI->isDebugValue())
634 continue;
635
636 // Now that we know this is a real instruction, count it.
637 ++Count;
638
Chad Rosier08ef4622015-09-03 16:41:28 +0000639 bool CanMergeOpc = Opc == MI->getOpcode();
640 Flags.setSExtIdx(-1);
641 if (!CanMergeOpc) {
642 bool IsValidLdStrOpc;
643 unsigned NonSExtOpc = getMatchingNonSExtOpcode(Opc, &IsValidLdStrOpc);
644 assert(IsValidLdStrOpc &&
645 "Given Opc should be a Load or Store with an immediate");
646 // Opc will be the first instruction in the pair.
647 Flags.setSExtIdx(NonSExtOpc == (unsigned)Opc ? 1 : 0);
648 CanMergeOpc = NonSExtOpc == getMatchingNonSExtOpcode(MI->getOpcode());
649 }
650
651 if (CanMergeOpc && getLdStOffsetOp(MI).isImm()) {
Chad Rosierc56a9132015-08-10 18:42:45 +0000652 assert(MI->mayLoadOrStore() && "Expected memory operation.");
Tim Northover3b0846e2014-05-24 12:50:23 +0000653 // If we've found another instruction with the same opcode, check to see
654 // if the base and offset are compatible with our starting instruction.
655 // These instructions all have scaled immediate operands, so we just
656 // check for +1/-1. Make sure to check the new instruction offset is
657 // actually an immediate and not a symbolic reference destined for
658 // a relocation.
659 //
660 // Pairwise instructions have a 7-bit signed offset field. Single insns
661 // have a 12-bit unsigned offset field. To be a valid combine, the
662 // final offset must be in range.
Chad Rosierf77e9092015-08-06 15:50:12 +0000663 unsigned MIBaseReg = getLdStBaseOp(MI).getReg();
664 int MIOffset = getLdStOffsetOp(MI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +0000665 if (BaseReg == MIBaseReg && ((Offset == MIOffset + OffsetStride) ||
666 (Offset + OffsetStride == MIOffset))) {
667 int MinOffset = Offset < MIOffset ? Offset : MIOffset;
668 // If this is a volatile load/store that otherwise matched, stop looking
669 // as something is going on that we don't have enough information to
670 // safely transform. Similarly, stop if we see a hint to avoid pairs.
671 if (MI->hasOrderedMemoryRef() || TII->isLdStPairSuppressed(MI))
672 return E;
673 // If the resultant immediate offset of merging these instructions
674 // is out of range for a pairwise instruction, bail and keep looking.
Chad Rosier08ef4622015-09-03 16:41:28 +0000675 bool MIIsUnscaled = isUnscaledLdSt(MI);
676 if (!inBoundsForPair(MIIsUnscaled, MinOffset, OffsetStride)) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000677 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Chad Rosierc56a9132015-08-10 18:42:45 +0000678 MemInsns.push_back(MI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000679 continue;
680 }
681 // If the alignment requirements of the paired (scaled) instruction
682 // can't express the offset of the unscaled input, bail and keep
683 // looking.
684 if (IsUnscaled && EnableAArch64UnscaledMemOp &&
685 (alignTo(MinOffset, OffsetStride) != MinOffset)) {
686 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Chad Rosierc56a9132015-08-10 18:42:45 +0000687 MemInsns.push_back(MI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000688 continue;
689 }
690 // If the destination register of the loads is the same register, bail
691 // and keep looking. A load-pair instruction with both destination
692 // registers the same is UNPREDICTABLE and will result in an exception.
Chad Rosierf77e9092015-08-06 15:50:12 +0000693 if (MayLoad && Reg == getLdStRegOp(MI).getReg()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000694 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Chad Rosierc56a9132015-08-10 18:42:45 +0000695 MemInsns.push_back(MI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000696 continue;
697 }
698
699 // If the Rt of the second instruction was not modified or used between
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000700 // the two instructions and none of the instructions between the second
701 // and first alias with the second, we can combine the second into the
702 // first.
Chad Rosierf77e9092015-08-06 15:50:12 +0000703 if (!ModifiedRegs[getLdStRegOp(MI).getReg()] &&
704 !(MI->mayLoad() && UsedRegs[getLdStRegOp(MI).getReg()]) &&
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000705 !mayAlias(MI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +0000706 Flags.setMergeForward(false);
Tim Northover3b0846e2014-05-24 12:50:23 +0000707 return MBBI;
708 }
709
710 // Likewise, if the Rt of the first instruction is not modified or used
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000711 // between the two instructions and none of the instructions between the
712 // first and the second alias with the first, we can combine the first
713 // into the second.
Chad Rosierf77e9092015-08-06 15:50:12 +0000714 if (!ModifiedRegs[getLdStRegOp(FirstMI).getReg()] &&
Chad Rosier5f668e12015-09-03 14:19:43 +0000715 !(MayLoad && UsedRegs[getLdStRegOp(FirstMI).getReg()]) &&
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000716 !mayAlias(FirstMI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +0000717 Flags.setMergeForward(true);
Tim Northover3b0846e2014-05-24 12:50:23 +0000718 return MBBI;
719 }
720 // Unable to combine these instructions due to interference in between.
721 // Keep looking.
722 }
723 }
724
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000725 // If the instruction wasn't a matching load or store. Stop searching if we
726 // encounter a call instruction that might modify memory.
727 if (MI->isCall())
Tim Northover3b0846e2014-05-24 12:50:23 +0000728 return E;
729
730 // Update modified / uses register lists.
731 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
732
733 // Otherwise, if the base register is modified, we have no match, so
734 // return early.
735 if (ModifiedRegs[BaseReg])
736 return E;
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000737
738 // Update list of instructions that read/write memory.
739 if (MI->mayLoadOrStore())
740 MemInsns.push_back(MI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000741 }
742 return E;
743}
744
745MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +0000746AArch64LoadStoreOpt::mergeUpdateInsn(MachineBasicBlock::iterator I,
747 MachineBasicBlock::iterator Update,
748 bool IsPreIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000749 assert((Update->getOpcode() == AArch64::ADDXri ||
750 Update->getOpcode() == AArch64::SUBXri) &&
751 "Unexpected base register update instruction to merge!");
752 MachineBasicBlock::iterator NextI = I;
753 // Return the instruction following the merged instruction, which is
754 // the instruction following our unmerged load. Unless that's the add/sub
755 // instruction we're merging, in which case it's the one after that.
756 if (++NextI == Update)
757 ++NextI;
758
759 int Value = Update->getOperand(2).getImm();
760 assert(AArch64_AM::getShiftValue(Update->getOperand(3).getImm()) == 0 &&
Chad Rosier2dfd3542015-09-23 13:51:44 +0000761 "Can't merge 1 << 12 offset into pre-/post-indexed load / store");
Tim Northover3b0846e2014-05-24 12:50:23 +0000762 if (Update->getOpcode() == AArch64::SUBXri)
763 Value = -Value;
764
Chad Rosier2dfd3542015-09-23 13:51:44 +0000765 unsigned NewOpc = IsPreIdx ? getPreIndexedOpcode(I->getOpcode())
766 : getPostIndexedOpcode(I->getOpcode());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000767 MachineInstrBuilder MIB;
768 if (!isPairedLdSt(I)) {
769 // Non-paired instruction.
770 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
771 .addOperand(getLdStRegOp(Update))
772 .addOperand(getLdStRegOp(I))
773 .addOperand(getLdStBaseOp(I))
774 .addImm(Value);
775 } else {
776 // Paired instruction.
777 const MachineFunction &MF = *I->getParent()->getParent();
778 int Scale = TII->getRegClass(I->getDesc(), 0, TRI, MF)->getSize();
779 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
780 .addOperand(getLdStRegOp(Update))
781 .addOperand(getLdStRegOp(I, 0))
782 .addOperand(getLdStRegOp(I, 1))
783 .addOperand(getLdStBaseOp(I))
784 .addImm(Value / Scale);
785 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000786 (void)MIB;
787
Chad Rosier2dfd3542015-09-23 13:51:44 +0000788 if (IsPreIdx)
789 DEBUG(dbgs() << "Creating pre-indexed load/store.");
790 else
791 DEBUG(dbgs() << "Creating post-indexed load/store.");
Tim Northover3b0846e2014-05-24 12:50:23 +0000792 DEBUG(dbgs() << " Replacing instructions:\n ");
793 DEBUG(I->print(dbgs()));
794 DEBUG(dbgs() << " ");
795 DEBUG(Update->print(dbgs()));
796 DEBUG(dbgs() << " with instruction:\n ");
797 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
798 DEBUG(dbgs() << "\n");
799
800 // Erase the old instructions for the block.
801 I->eraseFromParent();
802 Update->eraseFromParent();
803
804 return NextI;
805}
806
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000807bool AArch64LoadStoreOpt::isMatchingUpdateInsn(MachineInstr *MemMI,
808 MachineInstr *MI,
809 unsigned BaseReg, int Offset) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000810 switch (MI->getOpcode()) {
811 default:
812 break;
813 case AArch64::SUBXri:
814 // Negate the offset for a SUB instruction.
815 Offset *= -1;
816 // FALLTHROUGH
817 case AArch64::ADDXri:
818 // Make sure it's a vanilla immediate operand, not a relocation or
819 // anything else we can't handle.
820 if (!MI->getOperand(2).isImm())
821 break;
822 // Watch out for 1 << 12 shifted value.
823 if (AArch64_AM::getShiftValue(MI->getOperand(3).getImm()))
824 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000825
826 // The update instruction source and destination register must be the
827 // same as the load/store base register.
828 if (MI->getOperand(0).getReg() != BaseReg ||
829 MI->getOperand(1).getReg() != BaseReg)
830 break;
831
832 bool IsPairedInsn = isPairedLdSt(MemMI);
833 int UpdateOffset = MI->getOperand(2).getImm();
834 // For non-paired load/store instructions, the immediate must fit in a
835 // signed 9-bit integer.
836 if (!IsPairedInsn && (UpdateOffset > 255 || UpdateOffset < -256))
837 break;
838
839 // For paired load/store instructions, the immediate must be a multiple of
840 // the scaling factor. The scaled offset must also fit into a signed 7-bit
841 // integer.
842 if (IsPairedInsn) {
843 const MachineFunction &MF = *MemMI->getParent()->getParent();
844 int Scale = TII->getRegClass(MemMI->getDesc(), 0, TRI, MF)->getSize();
845 if (UpdateOffset % Scale != 0)
846 break;
847
848 int ScaledOffset = UpdateOffset / Scale;
849 if (ScaledOffset > 64 || ScaledOffset < -64)
850 break;
Tim Northover3b0846e2014-05-24 12:50:23 +0000851 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000852
853 // If we have a non-zero Offset, we check that it matches the amount
854 // we're adding to the register.
855 if (!Offset || Offset == MI->getOperand(2).getImm())
856 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000857 break;
858 }
859 return false;
860}
861
862MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward(
863 MachineBasicBlock::iterator I, unsigned Limit, int Value) {
864 MachineBasicBlock::iterator E = I->getParent()->end();
865 MachineInstr *MemMI = I;
866 MachineBasicBlock::iterator MBBI = I;
867 const MachineFunction &MF = *MemMI->getParent()->getParent();
868
Chad Rosierf77e9092015-08-06 15:50:12 +0000869 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
870 int Offset = getLdStOffsetOp(MemMI).getImm() *
Tim Northover3b0846e2014-05-24 12:50:23 +0000871 TII->getRegClass(MemMI->getDesc(), 0, TRI, MF)->getSize();
872
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000873 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +0000874 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000875 bool IsPairedInsn = isPairedLdSt(MemMI);
876 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
877 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
878 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
879 return E;
880 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000881
882 // Scan forward looking for post-index opportunities.
883 // Updating instructions can't be formed if the memory insn already
884 // has an offset other than the value we're looking for.
885 if (Offset != Value)
886 return E;
887
888 // Track which registers have been modified and used between the first insn
889 // (inclusive) and the second insn.
890 BitVector ModifiedRegs, UsedRegs;
891 ModifiedRegs.resize(TRI->getNumRegs());
892 UsedRegs.resize(TRI->getNumRegs());
893 ++MBBI;
894 for (unsigned Count = 0; MBBI != E; ++MBBI) {
895 MachineInstr *MI = MBBI;
896 // Skip DBG_VALUE instructions. Otherwise debug info can affect the
897 // optimization by changing how far we scan.
898 if (MI->isDebugValue())
899 continue;
900
901 // Now that we know this is a real instruction, count it.
902 ++Count;
903
904 // If we found a match, return it.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000905 if (isMatchingUpdateInsn(I, MI, BaseReg, Value))
Tim Northover3b0846e2014-05-24 12:50:23 +0000906 return MBBI;
907
908 // Update the status of what the instruction clobbered and used.
909 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
910
911 // Otherwise, if the base register is used or modified, we have no match, so
912 // return early.
913 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
914 return E;
915 }
916 return E;
917}
918
919MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnBackward(
920 MachineBasicBlock::iterator I, unsigned Limit) {
921 MachineBasicBlock::iterator B = I->getParent()->begin();
922 MachineBasicBlock::iterator E = I->getParent()->end();
923 MachineInstr *MemMI = I;
924 MachineBasicBlock::iterator MBBI = I;
925 const MachineFunction &MF = *MemMI->getParent()->getParent();
926
Chad Rosierf77e9092015-08-06 15:50:12 +0000927 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
928 int Offset = getLdStOffsetOp(MemMI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +0000929 unsigned RegSize = TII->getRegClass(MemMI->getDesc(), 0, TRI, MF)->getSize();
930
931 // If the load/store is the first instruction in the block, there's obviously
932 // not any matching update. Ditto if the memory offset isn't zero.
933 if (MBBI == B || Offset != 0)
934 return E;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000935 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +0000936 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000937 bool IsPairedInsn = isPairedLdSt(MemMI);
938 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
939 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
940 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
941 return E;
942 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000943
944 // Track which registers have been modified and used between the first insn
945 // (inclusive) and the second insn.
946 BitVector ModifiedRegs, UsedRegs;
947 ModifiedRegs.resize(TRI->getNumRegs());
948 UsedRegs.resize(TRI->getNumRegs());
949 --MBBI;
950 for (unsigned Count = 0; MBBI != B; --MBBI) {
951 MachineInstr *MI = MBBI;
952 // Skip DBG_VALUE instructions. Otherwise debug info can affect the
953 // optimization by changing how far we scan.
954 if (MI->isDebugValue())
955 continue;
956
957 // Now that we know this is a real instruction, count it.
958 ++Count;
959
960 // If we found a match, return it.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000961 if (isMatchingUpdateInsn(I, MI, BaseReg, RegSize))
Tim Northover3b0846e2014-05-24 12:50:23 +0000962 return MBBI;
963
964 // Update the status of what the instruction clobbered and used.
965 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
966
967 // Otherwise, if the base register is used or modified, we have no match, so
968 // return early.
969 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
970 return E;
971 }
972 return E;
973}
974
975bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB) {
976 bool Modified = false;
977 // Two tranformations to do here:
978 // 1) Find loads and stores that can be merged into a single load or store
979 // pair instruction.
980 // e.g.,
981 // ldr x0, [x2]
982 // ldr x1, [x2, #8]
983 // ; becomes
984 // ldp x0, x1, [x2]
985 // 2) Find base register updates that can be merged into the load or store
986 // as a base-reg writeback.
987 // e.g.,
988 // ldr x0, [x2]
989 // add x2, x2, #4
990 // ; becomes
991 // ldr x0, [x2], #4
992
993 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
994 MBBI != E;) {
995 MachineInstr *MI = MBBI;
996 switch (MI->getOpcode()) {
997 default:
998 // Just move on to the next instruction.
999 ++MBBI;
1000 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001001 // Scaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001002 case AArch64::STRSui:
1003 case AArch64::STRDui:
1004 case AArch64::STRQui:
1005 case AArch64::STRXui:
1006 case AArch64::STRWui:
1007 case AArch64::LDRSui:
1008 case AArch64::LDRDui:
1009 case AArch64::LDRQui:
1010 case AArch64::LDRXui:
1011 case AArch64::LDRWui:
Quentin Colombet29f55332015-01-24 01:25:54 +00001012 case AArch64::LDRSWui:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001013 // Unscaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001014 case AArch64::STURSi:
1015 case AArch64::STURDi:
1016 case AArch64::STURQi:
1017 case AArch64::STURWi:
1018 case AArch64::STURXi:
1019 case AArch64::LDURSi:
1020 case AArch64::LDURDi:
1021 case AArch64::LDURQi:
1022 case AArch64::LDURWi:
Quentin Colombet29f55332015-01-24 01:25:54 +00001023 case AArch64::LDURXi:
1024 case AArch64::LDURSWi: {
Tim Northover3b0846e2014-05-24 12:50:23 +00001025 // If this is a volatile load/store, don't mess with it.
1026 if (MI->hasOrderedMemoryRef()) {
1027 ++MBBI;
1028 break;
1029 }
1030 // Make sure this is a reg+imm (as opposed to an address reloc).
Chad Rosierf77e9092015-08-06 15:50:12 +00001031 if (!getLdStOffsetOp(MI).isImm()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001032 ++MBBI;
1033 break;
1034 }
1035 // Check if this load/store has a hint to avoid pair formation.
1036 // MachineMemOperands hints are set by the AArch64StorePairSuppress pass.
1037 if (TII->isLdStPairSuppressed(MI)) {
1038 ++MBBI;
1039 break;
1040 }
1041 // Look ahead up to ScanLimit instructions for a pairable instruction.
Chad Rosier96a18a92015-07-21 17:42:04 +00001042 LdStPairFlags Flags;
Tim Northover3b0846e2014-05-24 12:50:23 +00001043 MachineBasicBlock::iterator Paired =
Chad Rosier96a18a92015-07-21 17:42:04 +00001044 findMatchingInsn(MBBI, Flags, ScanLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001045 if (Paired != E) {
Chad Rosier9f4709b2015-08-26 13:39:48 +00001046 ++NumPairCreated;
1047 if (isUnscaledLdSt(MI))
1048 ++NumUnscaledPairCreated;
1049
Tim Northover3b0846e2014-05-24 12:50:23 +00001050 // Merge the loads into a pair. Keeping the iterator straight is a
1051 // pain, so we let the merge routine tell us what the next instruction
1052 // is after it's done mucking about.
Chad Rosier96a18a92015-07-21 17:42:04 +00001053 MBBI = mergePairedInsns(MBBI, Paired, Flags);
Tim Northover3b0846e2014-05-24 12:50:23 +00001054 Modified = true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001055 break;
1056 }
1057 ++MBBI;
1058 break;
1059 }
1060 // FIXME: Do the other instructions.
1061 }
1062 }
1063
1064 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
1065 MBBI != E;) {
1066 MachineInstr *MI = MBBI;
1067 // Do update merging. It's simpler to keep this separate from the above
1068 // switch, though not strictly necessary.
Matthias Braunfa3872e2015-05-18 20:27:55 +00001069 unsigned Opc = MI->getOpcode();
Tim Northover3b0846e2014-05-24 12:50:23 +00001070 switch (Opc) {
1071 default:
1072 // Just move on to the next instruction.
1073 ++MBBI;
1074 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001075 // Scaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001076 case AArch64::STRSui:
1077 case AArch64::STRDui:
1078 case AArch64::STRQui:
1079 case AArch64::STRXui:
1080 case AArch64::STRWui:
1081 case AArch64::LDRSui:
1082 case AArch64::LDRDui:
1083 case AArch64::LDRQui:
1084 case AArch64::LDRXui:
1085 case AArch64::LDRWui:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001086 // Unscaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001087 case AArch64::STURSi:
1088 case AArch64::STURDi:
1089 case AArch64::STURQi:
1090 case AArch64::STURWi:
1091 case AArch64::STURXi:
1092 case AArch64::LDURSi:
1093 case AArch64::LDURDi:
1094 case AArch64::LDURQi:
1095 case AArch64::LDURWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001096 case AArch64::LDURXi:
1097 // Paired instructions.
1098 case AArch64::LDPSi:
1099 case AArch64::LDPDi:
1100 case AArch64::LDPQi:
1101 case AArch64::LDPWi:
1102 case AArch64::LDPXi:
1103 case AArch64::STPSi:
1104 case AArch64::STPDi:
1105 case AArch64::STPQi:
1106 case AArch64::STPWi:
1107 case AArch64::STPXi: {
Tim Northover3b0846e2014-05-24 12:50:23 +00001108 // Make sure this is a reg+imm (as opposed to an address reloc).
Chad Rosierf77e9092015-08-06 15:50:12 +00001109 if (!getLdStOffsetOp(MI).isImm()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001110 ++MBBI;
1111 break;
1112 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001113 // Look forward to try to form a post-index instruction. For example,
1114 // ldr x0, [x20]
1115 // add x20, x20, #32
1116 // merged into:
1117 // ldr x0, [x20], #32
Tim Northover3b0846e2014-05-24 12:50:23 +00001118 MachineBasicBlock::iterator Update =
1119 findMatchingUpdateInsnForward(MBBI, ScanLimit, 0);
1120 if (Update != E) {
1121 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001122 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001123 Modified = true;
1124 ++NumPostFolded;
1125 break;
1126 }
1127 // Don't know how to handle pre/post-index versions, so move to the next
1128 // instruction.
Chad Rosier22eb7102015-08-06 17:37:18 +00001129 if (isUnscaledLdSt(Opc)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001130 ++MBBI;
1131 break;
1132 }
1133
1134 // Look back to try to find a pre-index instruction. For example,
1135 // add x0, x0, #8
1136 // ldr x1, [x0]
1137 // merged into:
1138 // ldr x1, [x0, #8]!
1139 Update = findMatchingUpdateInsnBackward(MBBI, ScanLimit);
1140 if (Update != E) {
1141 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001142 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001143 Modified = true;
1144 ++NumPreFolded;
1145 break;
1146 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001147 // The immediate in the load/store is scaled by the size of the register
1148 // being loaded. The immediate in the add we're looking for,
1149 // however, is not, so adjust here.
1150 int Value = MI->getOperand(isPairedLdSt(MI) ? 3 : 2).getImm() *
1151 TII->getRegClass(MI->getDesc(), 0, TRI, *(MBB.getParent()))
1152 ->getSize();
1153
1154 // FIXME: The immediate in the load/store should be scaled by the size of
1155 // the memory operation, not the size of the register being loaded/stored.
1156 // This works in general, but does not work for the LDPSW instruction,
1157 // which defines two 64-bit registers, but loads 32-bit values.
Tim Northover3b0846e2014-05-24 12:50:23 +00001158
1159 // Look forward to try to find a post-index instruction. For example,
1160 // ldr x1, [x0, #64]
1161 // add x0, x0, #64
1162 // merged into:
1163 // ldr x1, [x0, #64]!
Tim Northover3b0846e2014-05-24 12:50:23 +00001164 Update = findMatchingUpdateInsnForward(MBBI, ScanLimit, Value);
1165 if (Update != E) {
1166 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001167 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001168 Modified = true;
1169 ++NumPreFolded;
1170 break;
1171 }
1172
1173 // Nothing found. Just move to the next instruction.
1174 ++MBBI;
1175 break;
1176 }
1177 // FIXME: Do the other instructions.
1178 }
1179 }
1180
1181 return Modified;
1182}
1183
1184bool AArch64LoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
Eric Christopher6c901622015-01-28 03:51:33 +00001185 TII = static_cast<const AArch64InstrInfo *>(Fn.getSubtarget().getInstrInfo());
1186 TRI = Fn.getSubtarget().getRegisterInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +00001187
1188 bool Modified = false;
1189 for (auto &MBB : Fn)
1190 Modified |= optimizeBlock(MBB);
1191
1192 return Modified;
1193}
1194
1195// FIXME: Do we need/want a pre-alloc pass like ARM has to try to keep
1196// loads and stores near one another?
1197
Chad Rosier43f5c842015-08-05 12:40:13 +00001198/// createAArch64LoadStoreOptimizationPass - returns an instance of the
1199/// load / store optimization pass.
Tim Northover3b0846e2014-05-24 12:50:23 +00001200FunctionPass *llvm::createAArch64LoadStoreOptimizationPass() {
1201 return new AArch64LoadStoreOpt();
1202}