blob: c8dfa326451fca00c0d40835a8f2ad7f255d910b [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
Chad Rosier96530b32015-08-05 13:44:51 +000048namespace llvm {
49void initializeAArch64LoadStoreOptPass(PassRegistry &);
50}
51
52#define AARCH64_LOAD_STORE_OPT_NAME "AArch64 load / store optimization pass"
53
Tim Northover3b0846e2014-05-24 12:50:23 +000054namespace {
Chad Rosier96a18a92015-07-21 17:42:04 +000055
56typedef struct LdStPairFlags {
57 // If a matching instruction is found, MergeForward is set to true if the
58 // merge is to remove the first instruction and replace the second with
59 // a pair-wise insn, and false if the reverse is true.
60 bool MergeForward;
61
62 // SExtIdx gives the index of the result of the load pair that must be
63 // extended. The value of SExtIdx assumes that the paired load produces the
64 // value in this order: (I, returned iterator), i.e., -1 means no value has
65 // to be extended, 0 means I, and 1 means the returned iterator.
66 int SExtIdx;
67
68 LdStPairFlags() : MergeForward(false), SExtIdx(-1) {}
69
70 void setMergeForward(bool V = true) { MergeForward = V; }
71 bool getMergeForward() const { return MergeForward; }
72
73 void setSExtIdx(int V) { SExtIdx = V; }
74 int getSExtIdx() const { return SExtIdx; }
75
76} LdStPairFlags;
77
Tim Northover3b0846e2014-05-24 12:50:23 +000078struct AArch64LoadStoreOpt : public MachineFunctionPass {
79 static char ID;
James Molloy5b18b4c2015-10-23 10:41:38 +000080 AArch64LoadStoreOpt() : MachineFunctionPass(ID) {
Chad Rosier96530b32015-08-05 13:44:51 +000081 initializeAArch64LoadStoreOptPass(*PassRegistry::getPassRegistry());
82 }
Tim Northover3b0846e2014-05-24 12:50:23 +000083
84 const AArch64InstrInfo *TII;
85 const TargetRegisterInfo *TRI;
86
87 // Scan the instructions looking for a load/store that can be combined
88 // with the current instruction into a load/store pair.
89 // Return the matching instruction if one is found, else MBB->end().
Tim Northover3b0846e2014-05-24 12:50:23 +000090 MachineBasicBlock::iterator findMatchingInsn(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +000091 LdStPairFlags &Flags,
Tim Northover3b0846e2014-05-24 12:50:23 +000092 unsigned Limit);
93 // Merge the two instructions indicated into a single pair-wise instruction.
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +000094 // If MergeForward is true, erase the first instruction and fold its
Tim Northover3b0846e2014-05-24 12:50:23 +000095 // operation into the second. If false, the reverse. Return the instruction
96 // following the first instruction (which may change during processing).
97 MachineBasicBlock::iterator
98 mergePairedInsns(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +000099 MachineBasicBlock::iterator Paired,
Chad Rosierfe5399f2015-07-21 17:47:56 +0000100 const LdStPairFlags &Flags);
Tim Northover3b0846e2014-05-24 12:50:23 +0000101
102 // Scan the instruction list to find a base register update that can
103 // be combined with the current instruction (a load or store) using
104 // pre or post indexed addressing with writeback. Scan forwards.
105 MachineBasicBlock::iterator
106 findMatchingUpdateInsnForward(MachineBasicBlock::iterator I, unsigned Limit,
Chad Rosier0b15e7c2015-10-01 13:33:31 +0000107 int UnscaledOffset);
Tim Northover3b0846e2014-05-24 12:50:23 +0000108
109 // Scan the instruction list to find a base register update that can
110 // be combined with the current instruction (a load or store) using
111 // pre or post indexed addressing with writeback. Scan backwards.
112 MachineBasicBlock::iterator
113 findMatchingUpdateInsnBackward(MachineBasicBlock::iterator I, unsigned Limit);
114
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000115 // Find an instruction that updates the base register of the ld/st
116 // instruction.
117 bool isMatchingUpdateInsn(MachineInstr *MemMI, MachineInstr *MI,
118 unsigned BaseReg, int Offset);
119
Chad Rosier2dfd3542015-09-23 13:51:44 +0000120 // Merge a pre- or post-index base register update into a ld/st instruction.
Tim Northover3b0846e2014-05-24 12:50:23 +0000121 MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +0000122 mergeUpdateInsn(MachineBasicBlock::iterator I,
123 MachineBasicBlock::iterator Update, bool IsPreIdx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000124
125 bool optimizeBlock(MachineBasicBlock &MBB);
126
127 bool runOnMachineFunction(MachineFunction &Fn) override;
128
129 const char *getPassName() const override {
Chad Rosier96530b32015-08-05 13:44:51 +0000130 return AARCH64_LOAD_STORE_OPT_NAME;
Tim Northover3b0846e2014-05-24 12:50:23 +0000131 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000132};
133char AArch64LoadStoreOpt::ID = 0;
Jim Grosbach1eee3df2014-08-11 22:42:31 +0000134} // namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000135
Chad Rosier96530b32015-08-05 13:44:51 +0000136INITIALIZE_PASS(AArch64LoadStoreOpt, "aarch64-ldst-opt",
137 AARCH64_LOAD_STORE_OPT_NAME, false, false)
138
Chad Rosier22eb7102015-08-06 17:37:18 +0000139static bool isUnscaledLdSt(unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000140 switch (Opc) {
141 default:
142 return false;
143 case AArch64::STURSi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000144 case AArch64::STURDi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000145 case AArch64::STURQi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000146 case AArch64::STURWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000147 case AArch64::STURXi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000148 case AArch64::LDURSi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000149 case AArch64::LDURDi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000150 case AArch64::LDURQi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000151 case AArch64::LDURWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000152 case AArch64::LDURXi:
Quentin Colombet29f55332015-01-24 01:25:54 +0000153 case AArch64::LDURSWi:
154 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000155 }
156}
157
Chad Rosier22eb7102015-08-06 17:37:18 +0000158static bool isUnscaledLdSt(MachineInstr *MI) {
159 return isUnscaledLdSt(MI->getOpcode());
160}
161
Chad Rosier32d4d372015-09-29 16:07:32 +0000162// Scaling factor for unscaled load or store.
163static int getMemScale(MachineInstr *MI) {
Chad Rosier22eb7102015-08-06 17:37:18 +0000164 switch (MI->getOpcode()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000165 default:
Chad Rosierdabe2532015-09-29 18:26:15 +0000166 llvm_unreachable("Opcode has unknown scale!");
167 case AArch64::LDRBBui:
168 case AArch64::STRBBui:
169 return 1;
170 case AArch64::LDRHHui:
171 case AArch64::STRHHui:
172 return 2;
Chad Rosiera4d32172015-09-29 14:57:10 +0000173 case AArch64::LDRSui:
174 case AArch64::LDURSi:
175 case AArch64::LDRSWui:
176 case AArch64::LDURSWi:
177 case AArch64::LDRWui:
178 case AArch64::LDURWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000179 case AArch64::STRSui:
180 case AArch64::STURSi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000181 case AArch64::STRWui:
182 case AArch64::STURWi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000183 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +0000184 case AArch64::LDPSWi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000185 case AArch64::LDPWi:
186 case AArch64::STPSi:
187 case AArch64::STPWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000188 return 4;
Chad Rosiera4d32172015-09-29 14:57:10 +0000189 case AArch64::LDRDui:
190 case AArch64::LDURDi:
191 case AArch64::LDRXui:
192 case AArch64::LDURXi:
193 case AArch64::STRDui:
194 case AArch64::STURDi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000195 case AArch64::STRXui:
196 case AArch64::STURXi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000197 case AArch64::LDPDi:
198 case AArch64::LDPXi:
199 case AArch64::STPDi:
200 case AArch64::STPXi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000201 return 8;
Tim Northover3b0846e2014-05-24 12:50:23 +0000202 case AArch64::LDRQui:
203 case AArch64::LDURQi:
Chad Rosiera4d32172015-09-29 14:57:10 +0000204 case AArch64::STRQui:
205 case AArch64::STURQi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000206 case AArch64::LDPQi:
207 case AArch64::STPQi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000208 return 16;
Tim Northover3b0846e2014-05-24 12:50:23 +0000209 }
210}
211
Quentin Colombet66b61632015-03-06 22:42:10 +0000212static unsigned getMatchingNonSExtOpcode(unsigned Opc,
213 bool *IsValidLdStrOpc = nullptr) {
214 if (IsValidLdStrOpc)
215 *IsValidLdStrOpc = true;
216 switch (Opc) {
217 default:
218 if (IsValidLdStrOpc)
219 *IsValidLdStrOpc = false;
220 return UINT_MAX;
221 case AArch64::STRDui:
222 case AArch64::STURDi:
223 case AArch64::STRQui:
224 case AArch64::STURQi:
225 case AArch64::STRWui:
226 case AArch64::STURWi:
227 case AArch64::STRXui:
228 case AArch64::STURXi:
229 case AArch64::LDRDui:
230 case AArch64::LDURDi:
231 case AArch64::LDRQui:
232 case AArch64::LDURQi:
233 case AArch64::LDRWui:
234 case AArch64::LDURWi:
235 case AArch64::LDRXui:
236 case AArch64::LDURXi:
237 case AArch64::STRSui:
238 case AArch64::STURSi:
239 case AArch64::LDRSui:
240 case AArch64::LDURSi:
241 return Opc;
242 case AArch64::LDRSWui:
243 return AArch64::LDRWui;
244 case AArch64::LDURSWi:
245 return AArch64::LDURWi;
246 }
247}
248
Tim Northover3b0846e2014-05-24 12:50:23 +0000249static unsigned getMatchingPairOpcode(unsigned Opc) {
250 switch (Opc) {
251 default:
252 llvm_unreachable("Opcode has no pairwise equivalent!");
253 case AArch64::STRSui:
254 case AArch64::STURSi:
255 return AArch64::STPSi;
256 case AArch64::STRDui:
257 case AArch64::STURDi:
258 return AArch64::STPDi;
259 case AArch64::STRQui:
260 case AArch64::STURQi:
261 return AArch64::STPQi;
262 case AArch64::STRWui:
263 case AArch64::STURWi:
264 return AArch64::STPWi;
265 case AArch64::STRXui:
266 case AArch64::STURXi:
267 return AArch64::STPXi;
268 case AArch64::LDRSui:
269 case AArch64::LDURSi:
270 return AArch64::LDPSi;
271 case AArch64::LDRDui:
272 case AArch64::LDURDi:
273 return AArch64::LDPDi;
274 case AArch64::LDRQui:
275 case AArch64::LDURQi:
276 return AArch64::LDPQi;
277 case AArch64::LDRWui:
278 case AArch64::LDURWi:
279 return AArch64::LDPWi;
280 case AArch64::LDRXui:
281 case AArch64::LDURXi:
282 return AArch64::LDPXi;
Quentin Colombet29f55332015-01-24 01:25:54 +0000283 case AArch64::LDRSWui:
284 case AArch64::LDURSWi:
285 return AArch64::LDPSWi;
Tim Northover3b0846e2014-05-24 12:50:23 +0000286 }
287}
288
289static unsigned getPreIndexedOpcode(unsigned Opc) {
290 switch (Opc) {
291 default:
292 llvm_unreachable("Opcode has no pre-indexed equivalent!");
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000293 case AArch64::STRSui:
294 return AArch64::STRSpre;
295 case AArch64::STRDui:
296 return AArch64::STRDpre;
297 case AArch64::STRQui:
298 return AArch64::STRQpre;
Chad Rosierdabe2532015-09-29 18:26:15 +0000299 case AArch64::STRBBui:
300 return AArch64::STRBBpre;
301 case AArch64::STRHHui:
302 return AArch64::STRHHpre;
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000303 case AArch64::STRWui:
304 return AArch64::STRWpre;
305 case AArch64::STRXui:
306 return AArch64::STRXpre;
307 case AArch64::LDRSui:
308 return AArch64::LDRSpre;
309 case AArch64::LDRDui:
310 return AArch64::LDRDpre;
311 case AArch64::LDRQui:
312 return AArch64::LDRQpre;
Chad Rosierdabe2532015-09-29 18:26:15 +0000313 case AArch64::LDRBBui:
314 return AArch64::LDRBBpre;
315 case AArch64::LDRHHui:
316 return AArch64::LDRHHpre;
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000317 case AArch64::LDRWui:
318 return AArch64::LDRWpre;
319 case AArch64::LDRXui:
320 return AArch64::LDRXpre;
Quentin Colombet29f55332015-01-24 01:25:54 +0000321 case AArch64::LDRSWui:
322 return AArch64::LDRSWpre;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000323 case AArch64::LDPSi:
324 return AArch64::LDPSpre;
Chad Rosier43150122015-09-29 20:39:55 +0000325 case AArch64::LDPSWi:
326 return AArch64::LDPSWpre;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000327 case AArch64::LDPDi:
328 return AArch64::LDPDpre;
329 case AArch64::LDPQi:
330 return AArch64::LDPQpre;
331 case AArch64::LDPWi:
332 return AArch64::LDPWpre;
333 case AArch64::LDPXi:
334 return AArch64::LDPXpre;
335 case AArch64::STPSi:
336 return AArch64::STPSpre;
337 case AArch64::STPDi:
338 return AArch64::STPDpre;
339 case AArch64::STPQi:
340 return AArch64::STPQpre;
341 case AArch64::STPWi:
342 return AArch64::STPWpre;
343 case AArch64::STPXi:
344 return AArch64::STPXpre;
Tim Northover3b0846e2014-05-24 12:50:23 +0000345 }
346}
347
348static unsigned getPostIndexedOpcode(unsigned Opc) {
349 switch (Opc) {
350 default:
351 llvm_unreachable("Opcode has no post-indexed wise equivalent!");
352 case AArch64::STRSui:
353 return AArch64::STRSpost;
354 case AArch64::STRDui:
355 return AArch64::STRDpost;
356 case AArch64::STRQui:
357 return AArch64::STRQpost;
Chad Rosierdabe2532015-09-29 18:26:15 +0000358 case AArch64::STRBBui:
359 return AArch64::STRBBpost;
360 case AArch64::STRHHui:
361 return AArch64::STRHHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000362 case AArch64::STRWui:
363 return AArch64::STRWpost;
364 case AArch64::STRXui:
365 return AArch64::STRXpost;
366 case AArch64::LDRSui:
367 return AArch64::LDRSpost;
368 case AArch64::LDRDui:
369 return AArch64::LDRDpost;
370 case AArch64::LDRQui:
371 return AArch64::LDRQpost;
Chad Rosierdabe2532015-09-29 18:26:15 +0000372 case AArch64::LDRBBui:
373 return AArch64::LDRBBpost;
374 case AArch64::LDRHHui:
375 return AArch64::LDRHHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000376 case AArch64::LDRWui:
377 return AArch64::LDRWpost;
378 case AArch64::LDRXui:
379 return AArch64::LDRXpost;
Quentin Colombet29f55332015-01-24 01:25:54 +0000380 case AArch64::LDRSWui:
381 return AArch64::LDRSWpost;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000382 case AArch64::LDPSi:
383 return AArch64::LDPSpost;
Chad Rosier43150122015-09-29 20:39:55 +0000384 case AArch64::LDPSWi:
385 return AArch64::LDPSWpost;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000386 case AArch64::LDPDi:
387 return AArch64::LDPDpost;
388 case AArch64::LDPQi:
389 return AArch64::LDPQpost;
390 case AArch64::LDPWi:
391 return AArch64::LDPWpost;
392 case AArch64::LDPXi:
393 return AArch64::LDPXpost;
394 case AArch64::STPSi:
395 return AArch64::STPSpost;
396 case AArch64::STPDi:
397 return AArch64::STPDpost;
398 case AArch64::STPQi:
399 return AArch64::STPQpost;
400 case AArch64::STPWi:
401 return AArch64::STPWpost;
402 case AArch64::STPXi:
403 return AArch64::STPXpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000404 }
405}
406
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000407static bool isPairedLdSt(const MachineInstr *MI) {
408 switch (MI->getOpcode()) {
409 default:
410 return false;
411 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +0000412 case AArch64::LDPSWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000413 case AArch64::LDPDi:
414 case AArch64::LDPQi:
415 case AArch64::LDPWi:
416 case AArch64::LDPXi:
417 case AArch64::STPSi:
418 case AArch64::STPDi:
419 case AArch64::STPQi:
420 case AArch64::STPWi:
421 case AArch64::STPXi:
422 return true;
423 }
424}
425
426static const MachineOperand &getLdStRegOp(const MachineInstr *MI,
427 unsigned PairedRegOp = 0) {
428 assert(PairedRegOp < 2 && "Unexpected register operand idx.");
429 unsigned Idx = isPairedLdSt(MI) ? PairedRegOp : 0;
430 return MI->getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000431}
432
433static const MachineOperand &getLdStBaseOp(const MachineInstr *MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000434 unsigned Idx = isPairedLdSt(MI) ? 2 : 1;
435 return MI->getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000436}
437
438static const MachineOperand &getLdStOffsetOp(const MachineInstr *MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000439 unsigned Idx = isPairedLdSt(MI) ? 3 : 2;
440 return MI->getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000441}
442
Tim Northover3b0846e2014-05-24 12:50:23 +0000443MachineBasicBlock::iterator
444AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
445 MachineBasicBlock::iterator Paired,
Chad Rosier96a18a92015-07-21 17:42:04 +0000446 const LdStPairFlags &Flags) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000447 MachineBasicBlock::iterator NextI = I;
448 ++NextI;
449 // If NextI is the second of the two instructions to be merged, we need
450 // to skip one further. Either way we merge will invalidate the iterator,
451 // and we don't need to scan the new instruction, as it's a pairwise
452 // instruction, which we're not considering for further action anyway.
453 if (NextI == Paired)
454 ++NextI;
455
Chad Rosier96a18a92015-07-21 17:42:04 +0000456 int SExtIdx = Flags.getSExtIdx();
Quentin Colombet66b61632015-03-06 22:42:10 +0000457 unsigned Opc =
458 SExtIdx == -1 ? I->getOpcode() : getMatchingNonSExtOpcode(I->getOpcode());
Chad Rosier22eb7102015-08-06 17:37:18 +0000459 bool IsUnscaled = isUnscaledLdSt(Opc);
Chad Rosierf11d0402015-10-01 18:17:12 +0000460 int OffsetStride = IsUnscaled ? getMemScale(I) : 1;
Tim Northover3b0846e2014-05-24 12:50:23 +0000461
Chad Rosier96a18a92015-07-21 17:42:04 +0000462 bool MergeForward = Flags.getMergeForward();
Quentin Colombet66b61632015-03-06 22:42:10 +0000463 unsigned NewOpc = getMatchingPairOpcode(Opc);
Tim Northover3b0846e2014-05-24 12:50:23 +0000464 // Insert our new paired instruction after whichever of the paired
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000465 // instructions MergeForward indicates.
466 MachineBasicBlock::iterator InsertionPoint = MergeForward ? Paired : I;
467 // Also based on MergeForward is from where we copy the base register operand
Tim Northover3b0846e2014-05-24 12:50:23 +0000468 // so we get the flags compatible with the input code.
Chad Rosierf77e9092015-08-06 15:50:12 +0000469 const MachineOperand &BaseRegOp =
470 MergeForward ? getLdStBaseOp(Paired) : getLdStBaseOp(I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000471
472 // Which register is Rt and which is Rt2 depends on the offset order.
473 MachineInstr *RtMI, *Rt2MI;
Chad Rosier08ef4622015-09-03 16:41:28 +0000474 if (getLdStOffsetOp(I).getImm() ==
475 getLdStOffsetOp(Paired).getImm() + OffsetStride) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000476 RtMI = Paired;
477 Rt2MI = I;
Quentin Colombet66b61632015-03-06 22:42:10 +0000478 // Here we swapped the assumption made for SExtIdx.
479 // I.e., we turn ldp I, Paired into ldp Paired, I.
480 // Update the index accordingly.
481 if (SExtIdx != -1)
482 SExtIdx = (SExtIdx + 1) % 2;
Tim Northover3b0846e2014-05-24 12:50:23 +0000483 } else {
484 RtMI = I;
485 Rt2MI = Paired;
486 }
Jun Bum Limd3548302015-10-19 18:34:53 +0000487 // Handle Unscaled
James Molloy5b18b4c2015-10-23 10:41:38 +0000488 int OffsetImm = getLdStOffsetOp(RtMI).getImm();
Chad Rosierf11d0402015-10-01 18:17:12 +0000489 if (IsUnscaled)
Chad Rosier08ef4622015-09-03 16:41:28 +0000490 OffsetImm /= OffsetStride;
Tim Northover3b0846e2014-05-24 12:50:23 +0000491
492 // Construct the new instruction.
493 MachineInstrBuilder MIB = BuildMI(*I->getParent(), InsertionPoint,
494 I->getDebugLoc(), TII->get(NewOpc))
Chad Rosierf77e9092015-08-06 15:50:12 +0000495 .addOperand(getLdStRegOp(RtMI))
496 .addOperand(getLdStRegOp(Rt2MI))
Tim Northover3b0846e2014-05-24 12:50:23 +0000497 .addOperand(BaseRegOp)
498 .addImm(OffsetImm);
499 (void)MIB;
500
501 // FIXME: Do we need/want to copy the mem operands from the source
502 // instructions? Probably. What uses them after this?
503
504 DEBUG(dbgs() << "Creating pair load/store. Replacing instructions:\n ");
505 DEBUG(I->print(dbgs()));
506 DEBUG(dbgs() << " ");
507 DEBUG(Paired->print(dbgs()));
508 DEBUG(dbgs() << " with instruction:\n ");
Quentin Colombet66b61632015-03-06 22:42:10 +0000509
510 if (SExtIdx != -1) {
511 // Generate the sign extension for the proper result of the ldp.
512 // I.e., with X1, that would be:
513 // %W1<def> = KILL %W1, %X1<imp-def>
514 // %X1<def> = SBFMXri %X1<kill>, 0, 31
515 MachineOperand &DstMO = MIB->getOperand(SExtIdx);
516 // Right now, DstMO has the extended register, since it comes from an
517 // extended opcode.
518 unsigned DstRegX = DstMO.getReg();
519 // Get the W variant of that register.
520 unsigned DstRegW = TRI->getSubReg(DstRegX, AArch64::sub_32);
521 // Update the result of LDP to use the W instead of the X variant.
522 DstMO.setReg(DstRegW);
523 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
524 DEBUG(dbgs() << "\n");
525 // Make the machine verifier happy by providing a definition for
526 // the X register.
527 // Insert this definition right after the generated LDP, i.e., before
528 // InsertionPoint.
529 MachineInstrBuilder MIBKill =
530 BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
531 TII->get(TargetOpcode::KILL), DstRegW)
532 .addReg(DstRegW)
533 .addReg(DstRegX, RegState::Define);
534 MIBKill->getOperand(2).setImplicit();
535 // Create the sign extension.
536 MachineInstrBuilder MIBSXTW =
537 BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
538 TII->get(AArch64::SBFMXri), DstRegX)
539 .addReg(DstRegX)
540 .addImm(0)
541 .addImm(31);
542 (void)MIBSXTW;
543 DEBUG(dbgs() << " Extend operand:\n ");
544 DEBUG(((MachineInstr *)MIBSXTW)->print(dbgs()));
545 DEBUG(dbgs() << "\n");
546 } else {
547 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
548 DEBUG(dbgs() << "\n");
549 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000550
551 // Erase the old instructions.
552 I->eraseFromParent();
553 Paired->eraseFromParent();
554
555 return NextI;
556}
557
558/// trackRegDefsUses - Remember what registers the specified instruction uses
559/// and modifies.
Pete Cooper7be8f8f2015-08-03 19:04:32 +0000560static void trackRegDefsUses(const MachineInstr *MI, BitVector &ModifiedRegs,
Tim Northover3b0846e2014-05-24 12:50:23 +0000561 BitVector &UsedRegs,
562 const TargetRegisterInfo *TRI) {
Pete Cooper7be8f8f2015-08-03 19:04:32 +0000563 for (const MachineOperand &MO : MI->operands()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000564 if (MO.isRegMask())
565 ModifiedRegs.setBitsNotInMask(MO.getRegMask());
566
567 if (!MO.isReg())
568 continue;
569 unsigned Reg = MO.getReg();
570 if (MO.isDef()) {
571 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
572 ModifiedRegs.set(*AI);
573 } else {
574 assert(MO.isUse() && "Reg operand not a def and not a use?!?");
575 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
576 UsedRegs.set(*AI);
577 }
578 }
579}
580
581static bool inBoundsForPair(bool IsUnscaled, int Offset, int OffsetStride) {
Chad Rosier3dd0e942015-08-18 16:20:03 +0000582 // Convert the byte-offset used by unscaled into an "element" offset used
583 // by the scaled pair load/store instructions.
Chad Rosier08ef4622015-09-03 16:41:28 +0000584 if (IsUnscaled)
Chad Rosier3dd0e942015-08-18 16:20:03 +0000585 Offset /= OffsetStride;
586
587 return Offset <= 63 && Offset >= -64;
Tim Northover3b0846e2014-05-24 12:50:23 +0000588}
589
590// Do alignment, specialized to power of 2 and for signed ints,
591// avoiding having to do a C-style cast from uint_64t to int when
592// using RoundUpToAlignment from include/llvm/Support/MathExtras.h.
593// FIXME: Move this function to include/MathExtras.h?
594static int alignTo(int Num, int PowOf2) {
595 return (Num + PowOf2 - 1) & ~(PowOf2 - 1);
596}
597
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000598static bool mayAlias(MachineInstr *MIa, MachineInstr *MIb,
599 const AArch64InstrInfo *TII) {
600 // One of the instructions must modify memory.
601 if (!MIa->mayStore() && !MIb->mayStore())
602 return false;
603
604 // Both instructions must be memory operations.
605 if (!MIa->mayLoadOrStore() && !MIb->mayLoadOrStore())
606 return false;
607
608 return !TII->areMemAccessesTriviallyDisjoint(MIa, MIb);
609}
610
611static bool mayAlias(MachineInstr *MIa,
612 SmallVectorImpl<MachineInstr *> &MemInsns,
613 const AArch64InstrInfo *TII) {
614 for (auto &MIb : MemInsns)
615 if (mayAlias(MIa, MIb, TII))
616 return true;
617
618 return false;
619}
620
Tim Northover3b0846e2014-05-24 12:50:23 +0000621/// findMatchingInsn - Scan the instructions looking for a load/store that can
622/// be combined with the current instruction into a load/store pair.
623MachineBasicBlock::iterator
624AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
James Molloy5b18b4c2015-10-23 10:41:38 +0000625 LdStPairFlags &Flags,
626 unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000627 MachineBasicBlock::iterator E = I->getParent()->end();
628 MachineBasicBlock::iterator MBBI = I;
629 MachineInstr *FirstMI = I;
630 ++MBBI;
631
Matthias Braunfa3872e2015-05-18 20:27:55 +0000632 unsigned Opc = FirstMI->getOpcode();
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000633 bool MayLoad = FirstMI->mayLoad();
Chad Rosier22eb7102015-08-06 17:37:18 +0000634 bool IsUnscaled = isUnscaledLdSt(FirstMI);
Chad Rosierf77e9092015-08-06 15:50:12 +0000635 unsigned Reg = getLdStRegOp(FirstMI).getReg();
636 unsigned BaseReg = getLdStBaseOp(FirstMI).getReg();
637 int Offset = getLdStOffsetOp(FirstMI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +0000638
639 // Early exit if the first instruction modifies the base register.
640 // e.g., ldr x0, [x0]
Tim Northover3b0846e2014-05-24 12:50:23 +0000641 if (FirstMI->modifiesRegister(BaseReg, TRI))
642 return E;
Chad Rosiercaed6db2015-08-10 17:17:19 +0000643
644 // Early exit if the offset if not possible to match. (6 bits of positive
645 // range, plus allow an extra one in case we find a later insn that matches
646 // with Offset-1)
Chad Rosierf11d0402015-10-01 18:17:12 +0000647 int OffsetStride = IsUnscaled ? getMemScale(FirstMI) : 1;
James Molloy5b18b4c2015-10-23 10:41:38 +0000648 if (!inBoundsForPair(IsUnscaled, Offset, OffsetStride))
Tim Northover3b0846e2014-05-24 12:50:23 +0000649 return E;
650
651 // Track which registers have been modified and used between the first insn
652 // (inclusive) and the second insn.
653 BitVector ModifiedRegs, UsedRegs;
654 ModifiedRegs.resize(TRI->getNumRegs());
655 UsedRegs.resize(TRI->getNumRegs());
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000656
657 // Remember any instructions that read/write memory between FirstMI and MI.
658 SmallVector<MachineInstr *, 4> MemInsns;
659
Tim Northover3b0846e2014-05-24 12:50:23 +0000660 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
661 MachineInstr *MI = MBBI;
662 // Skip DBG_VALUE instructions. Otherwise debug info can affect the
663 // optimization by changing how far we scan.
664 if (MI->isDebugValue())
665 continue;
666
667 // Now that we know this is a real instruction, count it.
668 ++Count;
669
Chad Rosier08ef4622015-09-03 16:41:28 +0000670 bool CanMergeOpc = Opc == MI->getOpcode();
671 Flags.setSExtIdx(-1);
672 if (!CanMergeOpc) {
673 bool IsValidLdStrOpc;
674 unsigned NonSExtOpc = getMatchingNonSExtOpcode(Opc, &IsValidLdStrOpc);
675 assert(IsValidLdStrOpc &&
676 "Given Opc should be a Load or Store with an immediate");
677 // Opc will be the first instruction in the pair.
678 Flags.setSExtIdx(NonSExtOpc == (unsigned)Opc ? 1 : 0);
679 CanMergeOpc = NonSExtOpc == getMatchingNonSExtOpcode(MI->getOpcode());
680 }
681
682 if (CanMergeOpc && getLdStOffsetOp(MI).isImm()) {
Chad Rosierc56a9132015-08-10 18:42:45 +0000683 assert(MI->mayLoadOrStore() && "Expected memory operation.");
Tim Northover3b0846e2014-05-24 12:50:23 +0000684 // If we've found another instruction with the same opcode, check to see
685 // if the base and offset are compatible with our starting instruction.
686 // These instructions all have scaled immediate operands, so we just
687 // check for +1/-1. Make sure to check the new instruction offset is
688 // actually an immediate and not a symbolic reference destined for
689 // a relocation.
690 //
691 // Pairwise instructions have a 7-bit signed offset field. Single insns
692 // have a 12-bit unsigned offset field. To be a valid combine, the
693 // final offset must be in range.
Chad Rosierf77e9092015-08-06 15:50:12 +0000694 unsigned MIBaseReg = getLdStBaseOp(MI).getReg();
695 int MIOffset = getLdStOffsetOp(MI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +0000696 if (BaseReg == MIBaseReg && ((Offset == MIOffset + OffsetStride) ||
697 (Offset + OffsetStride == MIOffset))) {
698 int MinOffset = Offset < MIOffset ? Offset : MIOffset;
699 // If this is a volatile load/store that otherwise matched, stop looking
700 // as something is going on that we don't have enough information to
701 // safely transform. Similarly, stop if we see a hint to avoid pairs.
702 if (MI->hasOrderedMemoryRef() || TII->isLdStPairSuppressed(MI))
703 return E;
704 // If the resultant immediate offset of merging these instructions
705 // is out of range for a pairwise instruction, bail and keep looking.
Chad Rosier08ef4622015-09-03 16:41:28 +0000706 bool MIIsUnscaled = isUnscaledLdSt(MI);
James Molloy5b18b4c2015-10-23 10:41:38 +0000707 if (!inBoundsForPair(MIIsUnscaled, MinOffset, OffsetStride)) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000708 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Chad Rosierc56a9132015-08-10 18:42:45 +0000709 MemInsns.push_back(MI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000710 continue;
711 }
James Molloy5b18b4c2015-10-23 10:41:38 +0000712 // If the alignment requirements of the paired (scaled) instruction
713 // can't express the offset of the unscaled input, bail and keep
714 // looking.
715 if (IsUnscaled && (alignTo(MinOffset, OffsetStride) != MinOffset)) {
716 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
717 MemInsns.push_back(MI);
718 continue;
Tim Northover3b0846e2014-05-24 12:50:23 +0000719 }
720 // If the destination register of the loads is the same register, bail
721 // and keep looking. A load-pair instruction with both destination
722 // registers the same is UNPREDICTABLE and will result in an exception.
Chad Rosierf77e9092015-08-06 15:50:12 +0000723 if (MayLoad && Reg == getLdStRegOp(MI).getReg()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000724 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Chad Rosierc56a9132015-08-10 18:42:45 +0000725 MemInsns.push_back(MI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000726 continue;
727 }
728
729 // If the Rt of the second instruction was not modified or used between
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000730 // the two instructions and none of the instructions between the second
731 // and first alias with the second, we can combine the second into the
732 // first.
Chad Rosierf77e9092015-08-06 15:50:12 +0000733 if (!ModifiedRegs[getLdStRegOp(MI).getReg()] &&
734 !(MI->mayLoad() && UsedRegs[getLdStRegOp(MI).getReg()]) &&
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000735 !mayAlias(MI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +0000736 Flags.setMergeForward(false);
Tim Northover3b0846e2014-05-24 12:50:23 +0000737 return MBBI;
738 }
739
740 // Likewise, if the Rt of the first instruction is not modified or used
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000741 // between the two instructions and none of the instructions between the
742 // first and the second alias with the first, we can combine the first
743 // into the second.
Chad Rosierf77e9092015-08-06 15:50:12 +0000744 if (!ModifiedRegs[getLdStRegOp(FirstMI).getReg()] &&
Chad Rosier5f668e12015-09-03 14:19:43 +0000745 !(MayLoad && UsedRegs[getLdStRegOp(FirstMI).getReg()]) &&
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000746 !mayAlias(FirstMI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +0000747 Flags.setMergeForward(true);
Tim Northover3b0846e2014-05-24 12:50:23 +0000748 return MBBI;
749 }
750 // Unable to combine these instructions due to interference in between.
751 // Keep looking.
752 }
753 }
754
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000755 // If the instruction wasn't a matching load or store. Stop searching if we
756 // encounter a call instruction that might modify memory.
757 if (MI->isCall())
Tim Northover3b0846e2014-05-24 12:50:23 +0000758 return E;
759
760 // Update modified / uses register lists.
761 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
762
763 // Otherwise, if the base register is modified, we have no match, so
764 // return early.
765 if (ModifiedRegs[BaseReg])
766 return E;
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000767
768 // Update list of instructions that read/write memory.
769 if (MI->mayLoadOrStore())
770 MemInsns.push_back(MI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000771 }
772 return E;
773}
774
775MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +0000776AArch64LoadStoreOpt::mergeUpdateInsn(MachineBasicBlock::iterator I,
777 MachineBasicBlock::iterator Update,
778 bool IsPreIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000779 assert((Update->getOpcode() == AArch64::ADDXri ||
780 Update->getOpcode() == AArch64::SUBXri) &&
781 "Unexpected base register update instruction to merge!");
782 MachineBasicBlock::iterator NextI = I;
783 // Return the instruction following the merged instruction, which is
784 // the instruction following our unmerged load. Unless that's the add/sub
785 // instruction we're merging, in which case it's the one after that.
786 if (++NextI == Update)
787 ++NextI;
788
789 int Value = Update->getOperand(2).getImm();
790 assert(AArch64_AM::getShiftValue(Update->getOperand(3).getImm()) == 0 &&
Chad Rosier2dfd3542015-09-23 13:51:44 +0000791 "Can't merge 1 << 12 offset into pre-/post-indexed load / store");
Tim Northover3b0846e2014-05-24 12:50:23 +0000792 if (Update->getOpcode() == AArch64::SUBXri)
793 Value = -Value;
794
Chad Rosier2dfd3542015-09-23 13:51:44 +0000795 unsigned NewOpc = IsPreIdx ? getPreIndexedOpcode(I->getOpcode())
796 : getPostIndexedOpcode(I->getOpcode());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000797 MachineInstrBuilder MIB;
798 if (!isPairedLdSt(I)) {
799 // Non-paired instruction.
800 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
801 .addOperand(getLdStRegOp(Update))
802 .addOperand(getLdStRegOp(I))
803 .addOperand(getLdStBaseOp(I))
804 .addImm(Value);
805 } else {
806 // Paired instruction.
Chad Rosier32d4d372015-09-29 16:07:32 +0000807 int Scale = getMemScale(I);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000808 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
809 .addOperand(getLdStRegOp(Update))
810 .addOperand(getLdStRegOp(I, 0))
811 .addOperand(getLdStRegOp(I, 1))
812 .addOperand(getLdStBaseOp(I))
813 .addImm(Value / Scale);
814 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000815 (void)MIB;
816
Chad Rosier2dfd3542015-09-23 13:51:44 +0000817 if (IsPreIdx)
818 DEBUG(dbgs() << "Creating pre-indexed load/store.");
819 else
820 DEBUG(dbgs() << "Creating post-indexed load/store.");
Tim Northover3b0846e2014-05-24 12:50:23 +0000821 DEBUG(dbgs() << " Replacing instructions:\n ");
822 DEBUG(I->print(dbgs()));
823 DEBUG(dbgs() << " ");
824 DEBUG(Update->print(dbgs()));
825 DEBUG(dbgs() << " with instruction:\n ");
826 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
827 DEBUG(dbgs() << "\n");
828
829 // Erase the old instructions for the block.
830 I->eraseFromParent();
831 Update->eraseFromParent();
832
833 return NextI;
834}
835
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000836bool AArch64LoadStoreOpt::isMatchingUpdateInsn(MachineInstr *MemMI,
837 MachineInstr *MI,
838 unsigned BaseReg, int Offset) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000839 switch (MI->getOpcode()) {
840 default:
841 break;
842 case AArch64::SUBXri:
843 // Negate the offset for a SUB instruction.
844 Offset *= -1;
845 // FALLTHROUGH
846 case AArch64::ADDXri:
847 // Make sure it's a vanilla immediate operand, not a relocation or
848 // anything else we can't handle.
849 if (!MI->getOperand(2).isImm())
850 break;
851 // Watch out for 1 << 12 shifted value.
852 if (AArch64_AM::getShiftValue(MI->getOperand(3).getImm()))
853 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000854
855 // The update instruction source and destination register must be the
856 // same as the load/store base register.
857 if (MI->getOperand(0).getReg() != BaseReg ||
858 MI->getOperand(1).getReg() != BaseReg)
859 break;
860
861 bool IsPairedInsn = isPairedLdSt(MemMI);
862 int UpdateOffset = MI->getOperand(2).getImm();
863 // For non-paired load/store instructions, the immediate must fit in a
864 // signed 9-bit integer.
865 if (!IsPairedInsn && (UpdateOffset > 255 || UpdateOffset < -256))
866 break;
867
868 // For paired load/store instructions, the immediate must be a multiple of
869 // the scaling factor. The scaled offset must also fit into a signed 7-bit
870 // integer.
871 if (IsPairedInsn) {
Chad Rosier32d4d372015-09-29 16:07:32 +0000872 int Scale = getMemScale(MemMI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000873 if (UpdateOffset % Scale != 0)
874 break;
875
876 int ScaledOffset = UpdateOffset / Scale;
877 if (ScaledOffset > 64 || ScaledOffset < -64)
878 break;
Tim Northover3b0846e2014-05-24 12:50:23 +0000879 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000880
881 // If we have a non-zero Offset, we check that it matches the amount
882 // we're adding to the register.
883 if (!Offset || Offset == MI->getOperand(2).getImm())
884 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000885 break;
886 }
887 return false;
888}
889
890MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward(
Chad Rosier0b15e7c2015-10-01 13:33:31 +0000891 MachineBasicBlock::iterator I, unsigned Limit, int UnscaledOffset) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000892 MachineBasicBlock::iterator E = I->getParent()->end();
893 MachineInstr *MemMI = I;
894 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +0000895
Chad Rosierf77e9092015-08-06 15:50:12 +0000896 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
Chad Rosier0b15e7c2015-10-01 13:33:31 +0000897 int MIUnscaledOffset = getLdStOffsetOp(MemMI).getImm() * getMemScale(MemMI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000898
Chad Rosierb7c5b912015-10-01 13:43:05 +0000899 // Scan forward looking for post-index opportunities. Updating instructions
900 // can't be formed if the memory instruction doesn't have the offset we're
901 // looking for.
902 if (MIUnscaledOffset != UnscaledOffset)
903 return E;
904
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000905 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +0000906 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000907 bool IsPairedInsn = isPairedLdSt(MemMI);
908 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
909 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
910 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
911 return E;
912 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000913
Tim Northover3b0846e2014-05-24 12:50:23 +0000914 // Track which registers have been modified and used between the first insn
915 // (inclusive) and the second insn.
916 BitVector ModifiedRegs, UsedRegs;
917 ModifiedRegs.resize(TRI->getNumRegs());
918 UsedRegs.resize(TRI->getNumRegs());
919 ++MBBI;
920 for (unsigned Count = 0; MBBI != E; ++MBBI) {
921 MachineInstr *MI = MBBI;
922 // Skip DBG_VALUE instructions. Otherwise debug info can affect the
923 // optimization by changing how far we scan.
924 if (MI->isDebugValue())
925 continue;
926
927 // Now that we know this is a real instruction, count it.
928 ++Count;
929
930 // If we found a match, return it.
Chad Rosier0b15e7c2015-10-01 13:33:31 +0000931 if (isMatchingUpdateInsn(I, MI, BaseReg, UnscaledOffset))
Tim Northover3b0846e2014-05-24 12:50:23 +0000932 return MBBI;
933
934 // Update the status of what the instruction clobbered and used.
935 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
936
937 // Otherwise, if the base register is used or modified, we have no match, so
938 // return early.
939 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
940 return E;
941 }
942 return E;
943}
944
945MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnBackward(
946 MachineBasicBlock::iterator I, unsigned Limit) {
947 MachineBasicBlock::iterator B = I->getParent()->begin();
948 MachineBasicBlock::iterator E = I->getParent()->end();
949 MachineInstr *MemMI = I;
950 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +0000951
Chad Rosierf77e9092015-08-06 15:50:12 +0000952 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
953 int Offset = getLdStOffsetOp(MemMI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +0000954
955 // If the load/store is the first instruction in the block, there's obviously
956 // not any matching update. Ditto if the memory offset isn't zero.
957 if (MBBI == B || Offset != 0)
958 return E;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000959 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +0000960 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000961 bool IsPairedInsn = isPairedLdSt(MemMI);
962 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
963 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
964 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
965 return E;
966 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000967
968 // Track which registers have been modified and used between the first insn
969 // (inclusive) and the second insn.
970 BitVector ModifiedRegs, UsedRegs;
971 ModifiedRegs.resize(TRI->getNumRegs());
972 UsedRegs.resize(TRI->getNumRegs());
973 --MBBI;
974 for (unsigned Count = 0; MBBI != B; --MBBI) {
975 MachineInstr *MI = MBBI;
976 // Skip DBG_VALUE instructions. Otherwise debug info can affect the
977 // optimization by changing how far we scan.
978 if (MI->isDebugValue())
979 continue;
980
981 // Now that we know this is a real instruction, count it.
982 ++Count;
983
984 // If we found a match, return it.
Chad Rosier11c825f2015-09-30 19:44:40 +0000985 if (isMatchingUpdateInsn(I, MI, BaseReg, Offset))
Tim Northover3b0846e2014-05-24 12:50:23 +0000986 return MBBI;
987
988 // Update the status of what the instruction clobbered and used.
989 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
990
991 // Otherwise, if the base register is used or modified, we have no match, so
992 // return early.
993 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
994 return E;
995 }
996 return E;
997}
998
999bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB) {
1000 bool Modified = false;
James Molloy5b18b4c2015-10-23 10:41:38 +00001001 // Two tranformations to do here:
1002 // 1) Find loads and stores that can be merged into a single load or store
Tim Northover3b0846e2014-05-24 12:50:23 +00001003 // pair instruction.
1004 // e.g.,
1005 // ldr x0, [x2]
1006 // ldr x1, [x2, #8]
1007 // ; becomes
1008 // ldp x0, x1, [x2]
James Molloy5b18b4c2015-10-23 10:41:38 +00001009 // 2) Find base register updates that can be merged into the load or store
Tim Northover3b0846e2014-05-24 12:50:23 +00001010 // as a base-reg writeback.
1011 // e.g.,
1012 // ldr x0, [x2]
1013 // add x2, x2, #4
1014 // ; becomes
1015 // ldr x0, [x2], #4
1016
1017 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
1018 MBBI != E;) {
1019 MachineInstr *MI = MBBI;
1020 switch (MI->getOpcode()) {
1021 default:
1022 // Just move on to the next instruction.
1023 ++MBBI;
1024 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001025 // Scaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001026 case AArch64::STRSui:
1027 case AArch64::STRDui:
1028 case AArch64::STRQui:
1029 case AArch64::STRXui:
1030 case AArch64::STRWui:
1031 case AArch64::LDRSui:
1032 case AArch64::LDRDui:
1033 case AArch64::LDRQui:
1034 case AArch64::LDRXui:
1035 case AArch64::LDRWui:
Quentin Colombet29f55332015-01-24 01:25:54 +00001036 case AArch64::LDRSWui:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001037 // Unscaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001038 case AArch64::STURSi:
1039 case AArch64::STURDi:
1040 case AArch64::STURQi:
1041 case AArch64::STURWi:
1042 case AArch64::STURXi:
1043 case AArch64::LDURSi:
1044 case AArch64::LDURDi:
1045 case AArch64::LDURQi:
1046 case AArch64::LDURWi:
Quentin Colombet29f55332015-01-24 01:25:54 +00001047 case AArch64::LDURXi:
1048 case AArch64::LDURSWi: {
James Molloy5b18b4c2015-10-23 10:41:38 +00001049 // If this is a volatile load/store, don't mess with it.
1050 if (MI->hasOrderedMemoryRef()) {
1051 ++MBBI;
1052 break;
1053 }
1054 // Make sure this is a reg+imm (as opposed to an address reloc).
1055 if (!getLdStOffsetOp(MI).isImm()) {
1056 ++MBBI;
1057 break;
1058 }
1059 // Check if this load/store has a hint to avoid pair formation.
1060 // MachineMemOperands hints are set by the AArch64StorePairSuppress pass.
1061 if (TII->isLdStPairSuppressed(MI)) {
1062 ++MBBI;
1063 break;
1064 }
1065 // Look ahead up to ScanLimit instructions for a pairable instruction.
1066 LdStPairFlags Flags;
1067 MachineBasicBlock::iterator Paired =
1068 findMatchingInsn(MBBI, Flags, ScanLimit);
1069 if (Paired != E) {
1070 ++NumPairCreated;
1071 if (isUnscaledLdSt(MI))
1072 ++NumUnscaledPairCreated;
1073
1074 // Merge the loads into a pair. Keeping the iterator straight is a
1075 // pain, so we let the merge routine tell us what the next instruction
1076 // is after it's done mucking about.
1077 MBBI = mergePairedInsns(MBBI, Paired, Flags);
Tim Northover3b0846e2014-05-24 12:50:23 +00001078 Modified = true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001079 break;
1080 }
1081 ++MBBI;
1082 break;
1083 }
1084 // FIXME: Do the other instructions.
1085 }
1086 }
1087
1088 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
1089 MBBI != E;) {
1090 MachineInstr *MI = MBBI;
1091 // Do update merging. It's simpler to keep this separate from the above
1092 // switch, though not strictly necessary.
Matthias Braunfa3872e2015-05-18 20:27:55 +00001093 unsigned Opc = MI->getOpcode();
Tim Northover3b0846e2014-05-24 12:50:23 +00001094 switch (Opc) {
1095 default:
1096 // Just move on to the next instruction.
1097 ++MBBI;
1098 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001099 // Scaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001100 case AArch64::STRSui:
1101 case AArch64::STRDui:
1102 case AArch64::STRQui:
1103 case AArch64::STRXui:
1104 case AArch64::STRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001105 case AArch64::STRHHui:
1106 case AArch64::STRBBui:
Tim Northover3b0846e2014-05-24 12:50:23 +00001107 case AArch64::LDRSui:
1108 case AArch64::LDRDui:
1109 case AArch64::LDRQui:
1110 case AArch64::LDRXui:
1111 case AArch64::LDRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001112 case AArch64::LDRHHui:
1113 case AArch64::LDRBBui:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001114 // Unscaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001115 case AArch64::STURSi:
1116 case AArch64::STURDi:
1117 case AArch64::STURQi:
1118 case AArch64::STURWi:
1119 case AArch64::STURXi:
1120 case AArch64::LDURSi:
1121 case AArch64::LDURDi:
1122 case AArch64::LDURQi:
1123 case AArch64::LDURWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001124 case AArch64::LDURXi:
1125 // Paired instructions.
1126 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +00001127 case AArch64::LDPSWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001128 case AArch64::LDPDi:
1129 case AArch64::LDPQi:
1130 case AArch64::LDPWi:
1131 case AArch64::LDPXi:
1132 case AArch64::STPSi:
1133 case AArch64::STPDi:
1134 case AArch64::STPQi:
1135 case AArch64::STPWi:
1136 case AArch64::STPXi: {
Tim Northover3b0846e2014-05-24 12:50:23 +00001137 // Make sure this is a reg+imm (as opposed to an address reloc).
Chad Rosierf77e9092015-08-06 15:50:12 +00001138 if (!getLdStOffsetOp(MI).isImm()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001139 ++MBBI;
1140 break;
1141 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001142 // Look forward to try to form a post-index instruction. For example,
1143 // ldr x0, [x20]
1144 // add x20, x20, #32
1145 // merged into:
1146 // ldr x0, [x20], #32
Tim Northover3b0846e2014-05-24 12:50:23 +00001147 MachineBasicBlock::iterator Update =
1148 findMatchingUpdateInsnForward(MBBI, ScanLimit, 0);
1149 if (Update != E) {
1150 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001151 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001152 Modified = true;
1153 ++NumPostFolded;
1154 break;
1155 }
1156 // Don't know how to handle pre/post-index versions, so move to the next
1157 // instruction.
Chad Rosier22eb7102015-08-06 17:37:18 +00001158 if (isUnscaledLdSt(Opc)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001159 ++MBBI;
1160 break;
1161 }
1162
1163 // Look back to try to find a pre-index instruction. For example,
1164 // add x0, x0, #8
1165 // ldr x1, [x0]
1166 // merged into:
1167 // ldr x1, [x0, #8]!
1168 Update = findMatchingUpdateInsnBackward(MBBI, ScanLimit);
1169 if (Update != E) {
1170 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001171 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001172 Modified = true;
1173 ++NumPreFolded;
1174 break;
1175 }
Chad Rosier7a83d772015-10-01 13:09:44 +00001176 // The immediate in the load/store is scaled by the size of the memory
1177 // operation. The immediate in the add we're looking for,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001178 // however, is not, so adjust here.
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001179 int UnscaledOffset = getLdStOffsetOp(MI).getImm() * getMemScale(MI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001180
Tim Northover3b0846e2014-05-24 12:50:23 +00001181 // Look forward to try to find a post-index instruction. For example,
1182 // ldr x1, [x0, #64]
1183 // add x0, x0, #64
1184 // merged into:
1185 // ldr x1, [x0, #64]!
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001186 Update = findMatchingUpdateInsnForward(MBBI, ScanLimit, UnscaledOffset);
Tim Northover3b0846e2014-05-24 12:50:23 +00001187 if (Update != E) {
1188 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001189 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001190 Modified = true;
1191 ++NumPreFolded;
1192 break;
1193 }
1194
1195 // Nothing found. Just move to the next instruction.
1196 ++MBBI;
1197 break;
1198 }
1199 // FIXME: Do the other instructions.
1200 }
1201 }
1202
1203 return Modified;
1204}
1205
1206bool AArch64LoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
Eric Christopher6c901622015-01-28 03:51:33 +00001207 TII = static_cast<const AArch64InstrInfo *>(Fn.getSubtarget().getInstrInfo());
1208 TRI = Fn.getSubtarget().getRegisterInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +00001209
1210 bool Modified = false;
1211 for (auto &MBB : Fn)
1212 Modified |= optimizeBlock(MBB);
1213
1214 return Modified;
1215}
1216
1217// FIXME: Do we need/want a pre-alloc pass like ARM has to try to keep
1218// loads and stores near one another?
1219
Chad Rosier43f5c842015-08-05 12:40:13 +00001220/// createAArch64LoadStoreOptimizationPass - returns an instance of the
1221/// load / store optimization pass.
Tim Northover3b0846e2014-05-24 12:50:23 +00001222FunctionPass *llvm::createAArch64LoadStoreOptimizationPass() {
1223 return new AArch64LoadStoreOpt();
1224}