blob: c83f08ec40a9a8836295a04a1bb7c87ad3a7c095 [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
Tim Northover3b0846e2014-05-24 12:50:23 +000036STATISTIC(NumPairCreated, "Number of load/store pair instructions generated");
37STATISTIC(NumPostFolded, "Number of post-index updates folded");
38STATISTIC(NumPreFolded, "Number of pre-index updates folded");
39STATISTIC(NumUnscaledPairCreated,
40 "Number of load/store from unscaled generated");
Jun Bum Lim80ec0d32015-11-20 21:14:07 +000041STATISTIC(NumZeroStoresPromoted, "Number of narrow zero stores promoted");
Jun Bum Lim6755c3b2015-12-22 16:36:16 +000042STATISTIC(NumLoadsFromStoresPromoted, "Number of loads from stores promoted");
Tim Northover3b0846e2014-05-24 12:50:23 +000043
Chad Rosier35706ad2016-02-04 21:26:02 +000044// The LdStLimit limits how far we search for load/store pairs.
45static cl::opt<unsigned> LdStLimit("aarch64-load-store-scan-limit",
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +000046 cl::init(20), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000047
Chad Rosier35706ad2016-02-04 21:26:02 +000048// The UpdateLimit limits how far we search for update instructions when we form
49// pre-/post-index instructions.
50static cl::opt<unsigned> UpdateLimit("aarch64-update-scan-limit", cl::init(100),
51 cl::Hidden);
52
Chad Rosier96530b32015-08-05 13:44:51 +000053#define AARCH64_LOAD_STORE_OPT_NAME "AArch64 load / store optimization pass"
54
Tim Northover3b0846e2014-05-24 12:50:23 +000055namespace {
Chad Rosier96a18a92015-07-21 17:42:04 +000056
57typedef struct LdStPairFlags {
58 // If a matching instruction is found, MergeForward is set to true if the
59 // merge is to remove the first instruction and replace the second with
60 // a pair-wise insn, and false if the reverse is true.
61 bool MergeForward;
62
63 // SExtIdx gives the index of the result of the load pair that must be
64 // extended. The value of SExtIdx assumes that the paired load produces the
65 // value in this order: (I, returned iterator), i.e., -1 means no value has
66 // to be extended, 0 means I, and 1 means the returned iterator.
67 int SExtIdx;
68
69 LdStPairFlags() : MergeForward(false), SExtIdx(-1) {}
70
71 void setMergeForward(bool V = true) { MergeForward = V; }
72 bool getMergeForward() const { return MergeForward; }
73
74 void setSExtIdx(int V) { SExtIdx = V; }
75 int getSExtIdx() const { return SExtIdx; }
76
77} LdStPairFlags;
78
Tim Northover3b0846e2014-05-24 12:50:23 +000079struct AArch64LoadStoreOpt : public MachineFunctionPass {
80 static char ID;
Jun Bum Lim22fe15e2015-11-06 16:27:47 +000081 AArch64LoadStoreOpt() : MachineFunctionPass(ID) {
Chad Rosier96530b32015-08-05 13:44:51 +000082 initializeAArch64LoadStoreOptPass(*PassRegistry::getPassRegistry());
83 }
Tim Northover3b0846e2014-05-24 12:50:23 +000084
85 const AArch64InstrInfo *TII;
86 const TargetRegisterInfo *TRI;
Oliver Stannardd414c992015-11-10 11:04:18 +000087 const AArch64Subtarget *Subtarget;
Tim Northover3b0846e2014-05-24 12:50:23 +000088
Chad Rosierbba881e2016-02-02 15:02:30 +000089 // Track which registers have been modified and used.
90 BitVector ModifiedRegs, UsedRegs;
91
Tim Northover3b0846e2014-05-24 12:50:23 +000092 // 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,
Jun Bum Limcf974432016-03-31 14:47:24 +000097 unsigned Limit,
98 bool FindNarrowMerge);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +000099
100 // Scan the instructions looking for a store that writes to the address from
101 // which the current load instruction reads. Return true if one is found.
102 bool findMatchingStore(MachineBasicBlock::iterator I, unsigned Limit,
103 MachineBasicBlock::iterator &StoreI);
104
Chad Rosierd6daac42016-11-07 15:27:22 +0000105 // Merge the two instructions indicated into a wider narrow store instruction.
Chad Rosierb5933d72016-02-09 19:02:12 +0000106 MachineBasicBlock::iterator
Chad Rosierd6daac42016-11-07 15:27:22 +0000107 mergeNarrowZeroStores(MachineBasicBlock::iterator I,
108 MachineBasicBlock::iterator MergeMI,
109 const LdStPairFlags &Flags);
Chad Rosierb5933d72016-02-09 19:02:12 +0000110
Tim Northover3b0846e2014-05-24 12:50:23 +0000111 // Merge the two instructions indicated into a single pair-wise instruction.
Tim Northover3b0846e2014-05-24 12:50:23 +0000112 MachineBasicBlock::iterator
113 mergePairedInsns(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +0000114 MachineBasicBlock::iterator Paired,
Chad Rosierfe5399f2015-07-21 17:47:56 +0000115 const LdStPairFlags &Flags);
Tim Northover3b0846e2014-05-24 12:50:23 +0000116
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000117 // Promote the load that reads directly from the address stored to.
118 MachineBasicBlock::iterator
119 promoteLoadFromStore(MachineBasicBlock::iterator LoadI,
120 MachineBasicBlock::iterator StoreI);
121
Tim Northover3b0846e2014-05-24 12:50:23 +0000122 // Scan the instruction list to find a base register update that can
123 // be combined with the current instruction (a load or store) using
124 // pre or post indexed addressing with writeback. Scan forwards.
125 MachineBasicBlock::iterator
Chad Rosier234bf6f2016-01-18 21:56:40 +0000126 findMatchingUpdateInsnForward(MachineBasicBlock::iterator I,
Chad Rosier35706ad2016-02-04 21:26:02 +0000127 int UnscaledOffset, unsigned Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +0000128
129 // Scan the instruction list to find a base register update that can
130 // be combined with the current instruction (a load or store) using
131 // pre or post indexed addressing with writeback. Scan backwards.
132 MachineBasicBlock::iterator
Chad Rosier35706ad2016-02-04 21:26:02 +0000133 findMatchingUpdateInsnBackward(MachineBasicBlock::iterator I, unsigned Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +0000134
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000135 // Find an instruction that updates the base register of the ld/st
136 // instruction.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000137 bool isMatchingUpdateInsn(MachineInstr &MemMI, MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000138 unsigned BaseReg, int Offset);
139
Chad Rosier2dfd3542015-09-23 13:51:44 +0000140 // Merge a pre- or post-index base register update into a ld/st instruction.
Tim Northover3b0846e2014-05-24 12:50:23 +0000141 MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +0000142 mergeUpdateInsn(MachineBasicBlock::iterator I,
143 MachineBasicBlock::iterator Update, bool IsPreIdx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000144
Chad Rosierd6daac42016-11-07 15:27:22 +0000145 // Find and merge zero store instructions.
146 bool tryToMergeZeroStInst(MachineBasicBlock::iterator &MBBI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +0000147
Chad Rosier24c46ad2016-02-09 18:10:20 +0000148 // Find and pair ldr/str instructions.
149 bool tryToPairLdStInst(MachineBasicBlock::iterator &MBBI);
150
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000151 // Find and promote load instructions which read directly from store.
152 bool tryToPromoteLoadFromStore(MachineBasicBlock::iterator &MBBI);
153
Chad Rosierd6daac42016-11-07 15:27:22 +0000154 bool optimizeBlock(MachineBasicBlock &MBB, bool EnableNarrowZeroStOpt);
Tim Northover3b0846e2014-05-24 12:50:23 +0000155
156 bool runOnMachineFunction(MachineFunction &Fn) override;
157
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000158 MachineFunctionProperties getRequiredProperties() const override {
159 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000160 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000161 }
162
Mehdi Amini117296c2016-10-01 02:56:57 +0000163 StringRef getPassName() const override { return AARCH64_LOAD_STORE_OPT_NAME; }
Tim Northover3b0846e2014-05-24 12:50:23 +0000164};
165char AArch64LoadStoreOpt::ID = 0;
Jim Grosbach1eee3df2014-08-11 22:42:31 +0000166} // namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000167
Chad Rosier96530b32015-08-05 13:44:51 +0000168INITIALIZE_PASS(AArch64LoadStoreOpt, "aarch64-ldst-opt",
169 AARCH64_LOAD_STORE_OPT_NAME, false, false)
170
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000171static bool isNarrowStore(unsigned Opc) {
172 switch (Opc) {
173 default:
174 return false;
175 case AArch64::STRBBui:
176 case AArch64::STURBBi:
177 case AArch64::STRHHui:
178 case AArch64::STURHHi:
179 return true;
180 }
181}
182
Chad Rosier32d4d372015-09-29 16:07:32 +0000183// Scaling factor for unscaled load or store.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000184static int getMemScale(MachineInstr &MI) {
185 switch (MI.getOpcode()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000186 default:
Chad Rosierdabe2532015-09-29 18:26:15 +0000187 llvm_unreachable("Opcode has unknown scale!");
188 case AArch64::LDRBBui:
Jun Bum Lim4c35cca2015-11-19 17:21:41 +0000189 case AArch64::LDURBBi:
190 case AArch64::LDRSBWui:
191 case AArch64::LDURSBWi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000192 case AArch64::STRBBui:
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000193 case AArch64::STURBBi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000194 return 1;
195 case AArch64::LDRHHui:
Jun Bum Limc9879ec2015-10-27 19:16:03 +0000196 case AArch64::LDURHHi:
Jun Bum Lim4c35cca2015-11-19 17:21:41 +0000197 case AArch64::LDRSHWui:
198 case AArch64::LDURSHWi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000199 case AArch64::STRHHui:
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000200 case AArch64::STURHHi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000201 return 2;
Chad Rosiera4d32172015-09-29 14:57:10 +0000202 case AArch64::LDRSui:
203 case AArch64::LDURSi:
204 case AArch64::LDRSWui:
205 case AArch64::LDURSWi:
206 case AArch64::LDRWui:
207 case AArch64::LDURWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000208 case AArch64::STRSui:
209 case AArch64::STURSi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000210 case AArch64::STRWui:
211 case AArch64::STURWi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000212 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +0000213 case AArch64::LDPSWi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000214 case AArch64::LDPWi:
215 case AArch64::STPSi:
216 case AArch64::STPWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000217 return 4;
Chad Rosiera4d32172015-09-29 14:57:10 +0000218 case AArch64::LDRDui:
219 case AArch64::LDURDi:
220 case AArch64::LDRXui:
221 case AArch64::LDURXi:
222 case AArch64::STRDui:
223 case AArch64::STURDi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000224 case AArch64::STRXui:
225 case AArch64::STURXi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000226 case AArch64::LDPDi:
227 case AArch64::LDPXi:
228 case AArch64::STPDi:
229 case AArch64::STPXi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000230 return 8;
Tim Northover3b0846e2014-05-24 12:50:23 +0000231 case AArch64::LDRQui:
232 case AArch64::LDURQi:
Chad Rosiera4d32172015-09-29 14:57:10 +0000233 case AArch64::STRQui:
234 case AArch64::STURQi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000235 case AArch64::LDPQi:
236 case AArch64::STPQi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000237 return 16;
Tim Northover3b0846e2014-05-24 12:50:23 +0000238 }
239}
240
Quentin Colombet66b61632015-03-06 22:42:10 +0000241static unsigned getMatchingNonSExtOpcode(unsigned Opc,
242 bool *IsValidLdStrOpc = nullptr) {
243 if (IsValidLdStrOpc)
244 *IsValidLdStrOpc = true;
245 switch (Opc) {
246 default:
247 if (IsValidLdStrOpc)
248 *IsValidLdStrOpc = false;
249 return UINT_MAX;
250 case AArch64::STRDui:
251 case AArch64::STURDi:
252 case AArch64::STRQui:
253 case AArch64::STURQi:
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000254 case AArch64::STRBBui:
255 case AArch64::STURBBi:
256 case AArch64::STRHHui:
257 case AArch64::STURHHi:
Quentin Colombet66b61632015-03-06 22:42:10 +0000258 case AArch64::STRWui:
259 case AArch64::STURWi:
260 case AArch64::STRXui:
261 case AArch64::STURXi:
262 case AArch64::LDRDui:
263 case AArch64::LDURDi:
264 case AArch64::LDRQui:
265 case AArch64::LDURQi:
266 case AArch64::LDRWui:
267 case AArch64::LDURWi:
268 case AArch64::LDRXui:
269 case AArch64::LDURXi:
270 case AArch64::STRSui:
271 case AArch64::STURSi:
272 case AArch64::LDRSui:
273 case AArch64::LDURSi:
274 return Opc;
275 case AArch64::LDRSWui:
276 return AArch64::LDRWui;
277 case AArch64::LDURSWi:
278 return AArch64::LDURWi;
279 }
280}
281
Jun Bum Lim1de2d442016-02-05 20:02:03 +0000282static unsigned getMatchingWideOpcode(unsigned Opc) {
283 switch (Opc) {
284 default:
285 llvm_unreachable("Opcode has no wide equivalent!");
286 case AArch64::STRBBui:
287 return AArch64::STRHHui;
288 case AArch64::STRHHui:
289 return AArch64::STRWui;
290 case AArch64::STURBBi:
291 return AArch64::STURHHi;
292 case AArch64::STURHHi:
293 return AArch64::STURWi;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +0000294 case AArch64::STURWi:
295 return AArch64::STURXi;
296 case AArch64::STRWui:
297 return AArch64::STRXui;
Jun Bum Lim1de2d442016-02-05 20:02:03 +0000298 }
299}
300
Tim Northover3b0846e2014-05-24 12:50:23 +0000301static unsigned getMatchingPairOpcode(unsigned Opc) {
302 switch (Opc) {
303 default:
304 llvm_unreachable("Opcode has no pairwise equivalent!");
305 case AArch64::STRSui:
306 case AArch64::STURSi:
307 return AArch64::STPSi;
308 case AArch64::STRDui:
309 case AArch64::STURDi:
310 return AArch64::STPDi;
311 case AArch64::STRQui:
312 case AArch64::STURQi:
313 return AArch64::STPQi;
314 case AArch64::STRWui:
315 case AArch64::STURWi:
316 return AArch64::STPWi;
317 case AArch64::STRXui:
318 case AArch64::STURXi:
319 return AArch64::STPXi;
320 case AArch64::LDRSui:
321 case AArch64::LDURSi:
322 return AArch64::LDPSi;
323 case AArch64::LDRDui:
324 case AArch64::LDURDi:
325 return AArch64::LDPDi;
326 case AArch64::LDRQui:
327 case AArch64::LDURQi:
328 return AArch64::LDPQi;
329 case AArch64::LDRWui:
330 case AArch64::LDURWi:
331 return AArch64::LDPWi;
332 case AArch64::LDRXui:
333 case AArch64::LDURXi:
334 return AArch64::LDPXi;
Quentin Colombet29f55332015-01-24 01:25:54 +0000335 case AArch64::LDRSWui:
336 case AArch64::LDURSWi:
337 return AArch64::LDPSWi;
Tim Northover3b0846e2014-05-24 12:50:23 +0000338 }
339}
340
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000341static unsigned isMatchingStore(MachineInstr &LoadInst,
342 MachineInstr &StoreInst) {
343 unsigned LdOpc = LoadInst.getOpcode();
344 unsigned StOpc = StoreInst.getOpcode();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000345 switch (LdOpc) {
346 default:
347 llvm_unreachable("Unsupported load instruction!");
348 case AArch64::LDRBBui:
349 return StOpc == AArch64::STRBBui || StOpc == AArch64::STRHHui ||
350 StOpc == AArch64::STRWui || StOpc == AArch64::STRXui;
351 case AArch64::LDURBBi:
352 return StOpc == AArch64::STURBBi || StOpc == AArch64::STURHHi ||
353 StOpc == AArch64::STURWi || StOpc == AArch64::STURXi;
354 case AArch64::LDRHHui:
355 return StOpc == AArch64::STRHHui || StOpc == AArch64::STRWui ||
356 StOpc == AArch64::STRXui;
357 case AArch64::LDURHHi:
358 return StOpc == AArch64::STURHHi || StOpc == AArch64::STURWi ||
359 StOpc == AArch64::STURXi;
360 case AArch64::LDRWui:
361 return StOpc == AArch64::STRWui || StOpc == AArch64::STRXui;
362 case AArch64::LDURWi:
363 return StOpc == AArch64::STURWi || StOpc == AArch64::STURXi;
364 case AArch64::LDRXui:
365 return StOpc == AArch64::STRXui;
366 case AArch64::LDURXi:
367 return StOpc == AArch64::STURXi;
368 }
369}
370
Tim Northover3b0846e2014-05-24 12:50:23 +0000371static unsigned getPreIndexedOpcode(unsigned Opc) {
372 switch (Opc) {
373 default:
374 llvm_unreachable("Opcode has no pre-indexed equivalent!");
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000375 case AArch64::STRSui:
376 return AArch64::STRSpre;
377 case AArch64::STRDui:
378 return AArch64::STRDpre;
379 case AArch64::STRQui:
380 return AArch64::STRQpre;
Chad Rosierdabe2532015-09-29 18:26:15 +0000381 case AArch64::STRBBui:
382 return AArch64::STRBBpre;
383 case AArch64::STRHHui:
384 return AArch64::STRHHpre;
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000385 case AArch64::STRWui:
386 return AArch64::STRWpre;
387 case AArch64::STRXui:
388 return AArch64::STRXpre;
389 case AArch64::LDRSui:
390 return AArch64::LDRSpre;
391 case AArch64::LDRDui:
392 return AArch64::LDRDpre;
393 case AArch64::LDRQui:
394 return AArch64::LDRQpre;
Chad Rosierdabe2532015-09-29 18:26:15 +0000395 case AArch64::LDRBBui:
396 return AArch64::LDRBBpre;
397 case AArch64::LDRHHui:
398 return AArch64::LDRHHpre;
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000399 case AArch64::LDRWui:
400 return AArch64::LDRWpre;
401 case AArch64::LDRXui:
402 return AArch64::LDRXpre;
Quentin Colombet29f55332015-01-24 01:25:54 +0000403 case AArch64::LDRSWui:
404 return AArch64::LDRSWpre;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000405 case AArch64::LDPSi:
406 return AArch64::LDPSpre;
Chad Rosier43150122015-09-29 20:39:55 +0000407 case AArch64::LDPSWi:
408 return AArch64::LDPSWpre;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000409 case AArch64::LDPDi:
410 return AArch64::LDPDpre;
411 case AArch64::LDPQi:
412 return AArch64::LDPQpre;
413 case AArch64::LDPWi:
414 return AArch64::LDPWpre;
415 case AArch64::LDPXi:
416 return AArch64::LDPXpre;
417 case AArch64::STPSi:
418 return AArch64::STPSpre;
419 case AArch64::STPDi:
420 return AArch64::STPDpre;
421 case AArch64::STPQi:
422 return AArch64::STPQpre;
423 case AArch64::STPWi:
424 return AArch64::STPWpre;
425 case AArch64::STPXi:
426 return AArch64::STPXpre;
Tim Northover3b0846e2014-05-24 12:50:23 +0000427 }
428}
429
430static unsigned getPostIndexedOpcode(unsigned Opc) {
431 switch (Opc) {
432 default:
433 llvm_unreachable("Opcode has no post-indexed wise equivalent!");
434 case AArch64::STRSui:
435 return AArch64::STRSpost;
436 case AArch64::STRDui:
437 return AArch64::STRDpost;
438 case AArch64::STRQui:
439 return AArch64::STRQpost;
Chad Rosierdabe2532015-09-29 18:26:15 +0000440 case AArch64::STRBBui:
441 return AArch64::STRBBpost;
442 case AArch64::STRHHui:
443 return AArch64::STRHHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000444 case AArch64::STRWui:
445 return AArch64::STRWpost;
446 case AArch64::STRXui:
447 return AArch64::STRXpost;
448 case AArch64::LDRSui:
449 return AArch64::LDRSpost;
450 case AArch64::LDRDui:
451 return AArch64::LDRDpost;
452 case AArch64::LDRQui:
453 return AArch64::LDRQpost;
Chad Rosierdabe2532015-09-29 18:26:15 +0000454 case AArch64::LDRBBui:
455 return AArch64::LDRBBpost;
456 case AArch64::LDRHHui:
457 return AArch64::LDRHHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000458 case AArch64::LDRWui:
459 return AArch64::LDRWpost;
460 case AArch64::LDRXui:
461 return AArch64::LDRXpost;
Quentin Colombet29f55332015-01-24 01:25:54 +0000462 case AArch64::LDRSWui:
463 return AArch64::LDRSWpost;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000464 case AArch64::LDPSi:
465 return AArch64::LDPSpost;
Chad Rosier43150122015-09-29 20:39:55 +0000466 case AArch64::LDPSWi:
467 return AArch64::LDPSWpost;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000468 case AArch64::LDPDi:
469 return AArch64::LDPDpost;
470 case AArch64::LDPQi:
471 return AArch64::LDPQpost;
472 case AArch64::LDPWi:
473 return AArch64::LDPWpost;
474 case AArch64::LDPXi:
475 return AArch64::LDPXpost;
476 case AArch64::STPSi:
477 return AArch64::STPSpost;
478 case AArch64::STPDi:
479 return AArch64::STPDpost;
480 case AArch64::STPQi:
481 return AArch64::STPQpost;
482 case AArch64::STPWi:
483 return AArch64::STPWpost;
484 case AArch64::STPXi:
485 return AArch64::STPXpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000486 }
487}
488
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000489static bool isPairedLdSt(const MachineInstr &MI) {
490 switch (MI.getOpcode()) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000491 default:
492 return false;
493 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +0000494 case AArch64::LDPSWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000495 case AArch64::LDPDi:
496 case AArch64::LDPQi:
497 case AArch64::LDPWi:
498 case AArch64::LDPXi:
499 case AArch64::STPSi:
500 case AArch64::STPDi:
501 case AArch64::STPQi:
502 case AArch64::STPWi:
503 case AArch64::STPXi:
504 return true;
505 }
506}
507
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000508static const MachineOperand &getLdStRegOp(const MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000509 unsigned PairedRegOp = 0) {
510 assert(PairedRegOp < 2 && "Unexpected register operand idx.");
511 unsigned Idx = isPairedLdSt(MI) ? PairedRegOp : 0;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000512 return MI.getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000513}
514
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000515static const MachineOperand &getLdStBaseOp(const MachineInstr &MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000516 unsigned Idx = isPairedLdSt(MI) ? 2 : 1;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000517 return MI.getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000518}
519
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000520static const MachineOperand &getLdStOffsetOp(const MachineInstr &MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000521 unsigned Idx = isPairedLdSt(MI) ? 3 : 2;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000522 return MI.getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000523}
524
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000525static bool isLdOffsetInRangeOfSt(MachineInstr &LoadInst,
526 MachineInstr &StoreInst,
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000527 const AArch64InstrInfo *TII) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000528 assert(isMatchingStore(LoadInst, StoreInst) && "Expect only matched ld/st.");
529 int LoadSize = getMemScale(LoadInst);
530 int StoreSize = getMemScale(StoreInst);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000531 int UnscaledStOffset = TII->isUnscaledLdSt(StoreInst)
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000532 ? getLdStOffsetOp(StoreInst).getImm()
533 : getLdStOffsetOp(StoreInst).getImm() * StoreSize;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000534 int UnscaledLdOffset = TII->isUnscaledLdSt(LoadInst)
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000535 ? getLdStOffsetOp(LoadInst).getImm()
536 : getLdStOffsetOp(LoadInst).getImm() * LoadSize;
537 return (UnscaledStOffset <= UnscaledLdOffset) &&
538 (UnscaledLdOffset + LoadSize <= (UnscaledStOffset + StoreSize));
539}
540
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000541static bool isPromotableZeroStoreInst(MachineInstr &MI) {
Chad Rosierd6daac42016-11-07 15:27:22 +0000542 unsigned Opc = MI.getOpcode();
543 return (Opc == AArch64::STRWui || Opc == AArch64::STURWi ||
544 isNarrowStore(Opc)) &&
Jun Bum Lim397eb7b2016-02-12 15:25:39 +0000545 getLdStRegOp(MI).getReg() == AArch64::WZR;
546}
547
Tim Northover3b0846e2014-05-24 12:50:23 +0000548MachineBasicBlock::iterator
Chad Rosierd6daac42016-11-07 15:27:22 +0000549AArch64LoadStoreOpt::mergeNarrowZeroStores(MachineBasicBlock::iterator I,
550 MachineBasicBlock::iterator MergeMI,
551 const LdStPairFlags &Flags) {
552 assert(isPromotableZeroStoreInst(*I) && isPromotableZeroStoreInst(*MergeMI) &&
553 "Expected promotable zero stores.");
554
Tim Northover3b0846e2014-05-24 12:50:23 +0000555 MachineBasicBlock::iterator NextI = I;
556 ++NextI;
557 // If NextI is the second of the two instructions to be merged, we need
558 // to skip one further. Either way we merge will invalidate the iterator,
559 // and we don't need to scan the new instruction, as it's a pairwise
560 // instruction, which we're not considering for further action anyway.
Chad Rosierd7363db2016-02-09 19:09:22 +0000561 if (NextI == MergeMI)
Tim Northover3b0846e2014-05-24 12:50:23 +0000562 ++NextI;
563
Chad Rosierb5933d72016-02-09 19:02:12 +0000564 unsigned Opc = I->getOpcode();
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000565 bool IsScaled = !TII->isUnscaledLdSt(Opc);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000566 int OffsetStride = IsScaled ? 1 : getMemScale(*I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000567
Chad Rosier96a18a92015-07-21 17:42:04 +0000568 bool MergeForward = Flags.getMergeForward();
Tim Northover3b0846e2014-05-24 12:50:23 +0000569 // Insert our new paired instruction after whichever of the paired
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000570 // instructions MergeForward indicates.
Chad Rosierd7363db2016-02-09 19:09:22 +0000571 MachineBasicBlock::iterator InsertionPoint = MergeForward ? MergeMI : I;
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000572 // Also based on MergeForward is from where we copy the base register operand
Tim Northover3b0846e2014-05-24 12:50:23 +0000573 // so we get the flags compatible with the input code.
Chad Rosierf77e9092015-08-06 15:50:12 +0000574 const MachineOperand &BaseRegOp =
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000575 MergeForward ? getLdStBaseOp(*MergeMI) : getLdStBaseOp(*I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000576
577 // Which register is Rt and which is Rt2 depends on the offset order.
578 MachineInstr *RtMI, *Rt2MI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000579 if (getLdStOffsetOp(*I).getImm() ==
580 getLdStOffsetOp(*MergeMI).getImm() + OffsetStride) {
581 RtMI = &*MergeMI;
582 Rt2MI = &*I;
Tim Northover3b0846e2014-05-24 12:50:23 +0000583 } else {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000584 RtMI = &*I;
585 Rt2MI = &*MergeMI;
Tim Northover3b0846e2014-05-24 12:50:23 +0000586 }
Jun Bum Limc9879ec2015-10-27 19:16:03 +0000587
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000588 int OffsetImm = getLdStOffsetOp(*RtMI).getImm();
Chad Rosier11eedc92016-02-09 19:17:18 +0000589 // Change the scaled offset from small to large type.
590 if (IsScaled) {
591 assert(((OffsetImm & 1) == 0) && "Unexpected offset to merge");
592 OffsetImm /= 2;
593 }
594
Chad Rosierd6daac42016-11-07 15:27:22 +0000595 // Construct the new instruction.
Chad Rosierc46ef882016-02-09 19:33:42 +0000596 DebugLoc DL = I->getDebugLoc();
597 MachineBasicBlock *MBB = I->getParent();
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000598 MachineInstrBuilder MIB;
Chad Rosierc46ef882016-02-09 19:33:42 +0000599 MIB = BuildMI(*MBB, InsertionPoint, DL, TII->get(getMatchingWideOpcode(Opc)))
Jun Bum Lim397eb7b2016-02-12 15:25:39 +0000600 .addReg(isNarrowStore(Opc) ? AArch64::WZR : AArch64::XZR)
Chad Rosierb5933d72016-02-09 19:02:12 +0000601 .addOperand(BaseRegOp)
602 .addImm(OffsetImm)
Chad Rosierd7363db2016-02-09 19:09:22 +0000603 .setMemRefs(I->mergeMemRefsWith(*MergeMI));
Tim Northover3b0846e2014-05-24 12:50:23 +0000604 (void)MIB;
605
Chad Rosierd6daac42016-11-07 15:27:22 +0000606 DEBUG(dbgs() << "Creating wider store. Replacing instructions:\n ");
Chad Rosierb5933d72016-02-09 19:02:12 +0000607 DEBUG(I->print(dbgs()));
608 DEBUG(dbgs() << " ");
Chad Rosierd7363db2016-02-09 19:09:22 +0000609 DEBUG(MergeMI->print(dbgs()));
Chad Rosierb5933d72016-02-09 19:02:12 +0000610 DEBUG(dbgs() << " with instruction:\n ");
611 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
612 DEBUG(dbgs() << "\n");
613
614 // Erase the old instructions.
615 I->eraseFromParent();
Chad Rosierd7363db2016-02-09 19:09:22 +0000616 MergeMI->eraseFromParent();
Chad Rosierb5933d72016-02-09 19:02:12 +0000617 return NextI;
618}
619
620MachineBasicBlock::iterator
621AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
622 MachineBasicBlock::iterator Paired,
623 const LdStPairFlags &Flags) {
624 MachineBasicBlock::iterator NextI = I;
625 ++NextI;
626 // If NextI is the second of the two instructions to be merged, we need
627 // to skip one further. Either way we merge will invalidate the iterator,
628 // and we don't need to scan the new instruction, as it's a pairwise
629 // instruction, which we're not considering for further action anyway.
630 if (NextI == Paired)
631 ++NextI;
632
633 int SExtIdx = Flags.getSExtIdx();
634 unsigned Opc =
635 SExtIdx == -1 ? I->getOpcode() : getMatchingNonSExtOpcode(I->getOpcode());
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000636 bool IsUnscaled = TII->isUnscaledLdSt(Opc);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000637 int OffsetStride = IsUnscaled ? getMemScale(*I) : 1;
Chad Rosierb5933d72016-02-09 19:02:12 +0000638
639 bool MergeForward = Flags.getMergeForward();
640 // Insert our new paired instruction after whichever of the paired
641 // instructions MergeForward indicates.
642 MachineBasicBlock::iterator InsertionPoint = MergeForward ? Paired : I;
643 // Also based on MergeForward is from where we copy the base register operand
644 // so we get the flags compatible with the input code.
645 const MachineOperand &BaseRegOp =
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000646 MergeForward ? getLdStBaseOp(*Paired) : getLdStBaseOp(*I);
Chad Rosierb5933d72016-02-09 19:02:12 +0000647
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000648 int Offset = getLdStOffsetOp(*I).getImm();
649 int PairedOffset = getLdStOffsetOp(*Paired).getImm();
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000650 bool PairedIsUnscaled = TII->isUnscaledLdSt(Paired->getOpcode());
Chad Rosier00f9d232016-02-11 14:25:08 +0000651 if (IsUnscaled != PairedIsUnscaled) {
652 // We're trying to pair instructions that differ in how they are scaled. If
653 // I is scaled then scale the offset of Paired accordingly. Otherwise, do
654 // the opposite (i.e., make Paired's offset unscaled).
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000655 int MemSize = getMemScale(*Paired);
Chad Rosier00f9d232016-02-11 14:25:08 +0000656 if (PairedIsUnscaled) {
657 // If the unscaled offset isn't a multiple of the MemSize, we can't
658 // pair the operations together.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000659 assert(!(PairedOffset % getMemScale(*Paired)) &&
Chad Rosier00f9d232016-02-11 14:25:08 +0000660 "Offset should be a multiple of the stride!");
661 PairedOffset /= MemSize;
662 } else {
663 PairedOffset *= MemSize;
664 }
665 }
666
Chad Rosierb5933d72016-02-09 19:02:12 +0000667 // Which register is Rt and which is Rt2 depends on the offset order.
668 MachineInstr *RtMI, *Rt2MI;
Chad Rosier00f9d232016-02-11 14:25:08 +0000669 if (Offset == PairedOffset + OffsetStride) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000670 RtMI = &*Paired;
671 Rt2MI = &*I;
Chad Rosierb5933d72016-02-09 19:02:12 +0000672 // Here we swapped the assumption made for SExtIdx.
673 // I.e., we turn ldp I, Paired into ldp Paired, I.
674 // Update the index accordingly.
675 if (SExtIdx != -1)
676 SExtIdx = (SExtIdx + 1) % 2;
677 } else {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000678 RtMI = &*I;
679 Rt2MI = &*Paired;
Chad Rosierb5933d72016-02-09 19:02:12 +0000680 }
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000681 int OffsetImm = getLdStOffsetOp(*RtMI).getImm();
Chad Rosier00f9d232016-02-11 14:25:08 +0000682 // Scale the immediate offset, if necessary.
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000683 if (TII->isUnscaledLdSt(RtMI->getOpcode())) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000684 assert(!(OffsetImm % getMemScale(*RtMI)) &&
Chad Rosier00f9d232016-02-11 14:25:08 +0000685 "Unscaled offset cannot be scaled.");
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000686 OffsetImm /= getMemScale(*RtMI);
Chad Rosier87e33412016-02-09 20:18:07 +0000687 }
Chad Rosierb5933d72016-02-09 19:02:12 +0000688
689 // Construct the new instruction.
690 MachineInstrBuilder MIB;
Chad Rosierc46ef882016-02-09 19:33:42 +0000691 DebugLoc DL = I->getDebugLoc();
692 MachineBasicBlock *MBB = I->getParent();
693 MIB = BuildMI(*MBB, InsertionPoint, DL, TII->get(getMatchingPairOpcode(Opc)))
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000694 .addOperand(getLdStRegOp(*RtMI))
695 .addOperand(getLdStRegOp(*Rt2MI))
Chad Rosierb5933d72016-02-09 19:02:12 +0000696 .addOperand(BaseRegOp)
Chad Rosiere40b9512016-03-08 17:16:38 +0000697 .addImm(OffsetImm)
698 .setMemRefs(I->mergeMemRefsWith(*Paired));
Chad Rosierb5933d72016-02-09 19:02:12 +0000699
700 (void)MIB;
Tim Northover3b0846e2014-05-24 12:50:23 +0000701
702 DEBUG(dbgs() << "Creating pair load/store. Replacing instructions:\n ");
703 DEBUG(I->print(dbgs()));
704 DEBUG(dbgs() << " ");
705 DEBUG(Paired->print(dbgs()));
706 DEBUG(dbgs() << " with instruction:\n ");
Quentin Colombet66b61632015-03-06 22:42:10 +0000707 if (SExtIdx != -1) {
708 // Generate the sign extension for the proper result of the ldp.
709 // I.e., with X1, that would be:
710 // %W1<def> = KILL %W1, %X1<imp-def>
711 // %X1<def> = SBFMXri %X1<kill>, 0, 31
712 MachineOperand &DstMO = MIB->getOperand(SExtIdx);
713 // Right now, DstMO has the extended register, since it comes from an
714 // extended opcode.
715 unsigned DstRegX = DstMO.getReg();
716 // Get the W variant of that register.
717 unsigned DstRegW = TRI->getSubReg(DstRegX, AArch64::sub_32);
718 // Update the result of LDP to use the W instead of the X variant.
719 DstMO.setReg(DstRegW);
720 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
721 DEBUG(dbgs() << "\n");
722 // Make the machine verifier happy by providing a definition for
723 // the X register.
724 // Insert this definition right after the generated LDP, i.e., before
725 // InsertionPoint.
726 MachineInstrBuilder MIBKill =
Chad Rosierc46ef882016-02-09 19:33:42 +0000727 BuildMI(*MBB, InsertionPoint, DL, TII->get(TargetOpcode::KILL), DstRegW)
Quentin Colombet66b61632015-03-06 22:42:10 +0000728 .addReg(DstRegW)
729 .addReg(DstRegX, RegState::Define);
730 MIBKill->getOperand(2).setImplicit();
731 // Create the sign extension.
732 MachineInstrBuilder MIBSXTW =
Chad Rosierc46ef882016-02-09 19:33:42 +0000733 BuildMI(*MBB, InsertionPoint, DL, TII->get(AArch64::SBFMXri), DstRegX)
Quentin Colombet66b61632015-03-06 22:42:10 +0000734 .addReg(DstRegX)
735 .addImm(0)
736 .addImm(31);
737 (void)MIBSXTW;
738 DEBUG(dbgs() << " Extend operand:\n ");
739 DEBUG(((MachineInstr *)MIBSXTW)->print(dbgs()));
Quentin Colombet66b61632015-03-06 22:42:10 +0000740 } else {
741 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
Quentin Colombet66b61632015-03-06 22:42:10 +0000742 }
Chad Rosier1c44c5982016-02-09 20:27:45 +0000743 DEBUG(dbgs() << "\n");
Tim Northover3b0846e2014-05-24 12:50:23 +0000744
745 // Erase the old instructions.
746 I->eraseFromParent();
747 Paired->eraseFromParent();
748
749 return NextI;
750}
751
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000752MachineBasicBlock::iterator
753AArch64LoadStoreOpt::promoteLoadFromStore(MachineBasicBlock::iterator LoadI,
754 MachineBasicBlock::iterator StoreI) {
755 MachineBasicBlock::iterator NextI = LoadI;
756 ++NextI;
757
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000758 int LoadSize = getMemScale(*LoadI);
759 int StoreSize = getMemScale(*StoreI);
760 unsigned LdRt = getLdStRegOp(*LoadI).getReg();
761 unsigned StRt = getLdStRegOp(*StoreI).getReg();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000762 bool IsStoreXReg = TRI->getRegClass(AArch64::GPR64RegClassID)->contains(StRt);
763
764 assert((IsStoreXReg ||
765 TRI->getRegClass(AArch64::GPR32RegClassID)->contains(StRt)) &&
766 "Unexpected RegClass");
767
768 MachineInstr *BitExtMI;
769 if (LoadSize == StoreSize && (LoadSize == 4 || LoadSize == 8)) {
770 // Remove the load, if the destination register of the loads is the same
771 // register for stored value.
772 if (StRt == LdRt && LoadSize == 8) {
773 DEBUG(dbgs() << "Remove load instruction:\n ");
774 DEBUG(LoadI->print(dbgs()));
775 DEBUG(dbgs() << "\n");
776 LoadI->eraseFromParent();
777 return NextI;
778 }
779 // Replace the load with a mov if the load and store are in the same size.
780 BitExtMI =
781 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(),
782 TII->get(IsStoreXReg ? AArch64::ORRXrs : AArch64::ORRWrs), LdRt)
783 .addReg(IsStoreXReg ? AArch64::XZR : AArch64::WZR)
784 .addReg(StRt)
785 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
786 } else {
787 // FIXME: Currently we disable this transformation in big-endian targets as
788 // performance and correctness are verified only in little-endian.
789 if (!Subtarget->isLittleEndian())
790 return NextI;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000791 bool IsUnscaled = TII->isUnscaledLdSt(*LoadI);
792 assert(IsUnscaled == TII->isUnscaledLdSt(*StoreI) &&
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000793 "Unsupported ld/st match");
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000794 assert(LoadSize <= StoreSize && "Invalid load size");
795 int UnscaledLdOffset = IsUnscaled
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000796 ? getLdStOffsetOp(*LoadI).getImm()
797 : getLdStOffsetOp(*LoadI).getImm() * LoadSize;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000798 int UnscaledStOffset = IsUnscaled
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000799 ? getLdStOffsetOp(*StoreI).getImm()
800 : getLdStOffsetOp(*StoreI).getImm() * StoreSize;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000801 int Width = LoadSize * 8;
802 int Immr = 8 * (UnscaledLdOffset - UnscaledStOffset);
803 int Imms = Immr + Width - 1;
804 unsigned DestReg = IsStoreXReg
805 ? TRI->getMatchingSuperReg(LdRt, AArch64::sub_32,
806 &AArch64::GPR64RegClass)
807 : LdRt;
808
809 assert((UnscaledLdOffset >= UnscaledStOffset &&
810 (UnscaledLdOffset + LoadSize) <= UnscaledStOffset + StoreSize) &&
811 "Invalid offset");
812
813 Immr = 8 * (UnscaledLdOffset - UnscaledStOffset);
814 Imms = Immr + Width - 1;
815 if (UnscaledLdOffset == UnscaledStOffset) {
816 uint32_t AndMaskEncoded = ((IsStoreXReg ? 1 : 0) << 12) // N
817 | ((Immr) << 6) // immr
818 | ((Imms) << 0) // imms
819 ;
820
821 BitExtMI =
822 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(),
823 TII->get(IsStoreXReg ? AArch64::ANDXri : AArch64::ANDWri),
824 DestReg)
825 .addReg(StRt)
826 .addImm(AndMaskEncoded);
827 } else {
828 BitExtMI =
829 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(),
830 TII->get(IsStoreXReg ? AArch64::UBFMXri : AArch64::UBFMWri),
831 DestReg)
832 .addReg(StRt)
833 .addImm(Immr)
834 .addImm(Imms);
835 }
836 }
Chad Rosierf7ac5f22016-03-30 18:08:51 +0000837 (void)BitExtMI;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000838
839 DEBUG(dbgs() << "Promoting load by replacing :\n ");
840 DEBUG(StoreI->print(dbgs()));
841 DEBUG(dbgs() << " ");
842 DEBUG(LoadI->print(dbgs()));
843 DEBUG(dbgs() << " with instructions:\n ");
844 DEBUG(StoreI->print(dbgs()));
845 DEBUG(dbgs() << " ");
846 DEBUG((BitExtMI)->print(dbgs()));
847 DEBUG(dbgs() << "\n");
848
849 // Erase the old instructions.
850 LoadI->eraseFromParent();
851 return NextI;
852}
853
Tim Northover3b0846e2014-05-24 12:50:23 +0000854/// trackRegDefsUses - Remember what registers the specified instruction uses
855/// and modifies.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000856static void trackRegDefsUses(const MachineInstr &MI, BitVector &ModifiedRegs,
Tim Northover3b0846e2014-05-24 12:50:23 +0000857 BitVector &UsedRegs,
858 const TargetRegisterInfo *TRI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000859 for (const MachineOperand &MO : MI.operands()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000860 if (MO.isRegMask())
861 ModifiedRegs.setBitsNotInMask(MO.getRegMask());
862
863 if (!MO.isReg())
864 continue;
865 unsigned Reg = MO.getReg();
Geoff Berry173b14d2016-02-09 20:47:21 +0000866 if (!Reg)
867 continue;
Tim Northover3b0846e2014-05-24 12:50:23 +0000868 if (MO.isDef()) {
869 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
870 ModifiedRegs.set(*AI);
871 } else {
872 assert(MO.isUse() && "Reg operand not a def and not a use?!?");
873 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
874 UsedRegs.set(*AI);
875 }
876 }
877}
878
879static bool inBoundsForPair(bool IsUnscaled, int Offset, int OffsetStride) {
Chad Rosier3dd0e942015-08-18 16:20:03 +0000880 // Convert the byte-offset used by unscaled into an "element" offset used
881 // by the scaled pair load/store instructions.
Chad Rosier00f9d232016-02-11 14:25:08 +0000882 if (IsUnscaled) {
883 // If the byte-offset isn't a multiple of the stride, there's no point
884 // trying to match it.
885 if (Offset % OffsetStride)
886 return false;
Chad Rosier3dd0e942015-08-18 16:20:03 +0000887 Offset /= OffsetStride;
Chad Rosier00f9d232016-02-11 14:25:08 +0000888 }
Chad Rosier3dd0e942015-08-18 16:20:03 +0000889 return Offset <= 63 && Offset >= -64;
Tim Northover3b0846e2014-05-24 12:50:23 +0000890}
891
892// Do alignment, specialized to power of 2 and for signed ints,
893// avoiding having to do a C-style cast from uint_64t to int when
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000894// using alignTo from include/llvm/Support/MathExtras.h.
Tim Northover3b0846e2014-05-24 12:50:23 +0000895// FIXME: Move this function to include/MathExtras.h?
896static int alignTo(int Num, int PowOf2) {
897 return (Num + PowOf2 - 1) & ~(PowOf2 - 1);
898}
899
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000900static bool mayAlias(MachineInstr &MIa, MachineInstr &MIb,
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000901 const AArch64InstrInfo *TII) {
902 // One of the instructions must modify memory.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000903 if (!MIa.mayStore() && !MIb.mayStore())
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000904 return false;
905
906 // Both instructions must be memory operations.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000907 if (!MIa.mayLoadOrStore() && !MIb.mayLoadOrStore())
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000908 return false;
909
910 return !TII->areMemAccessesTriviallyDisjoint(MIa, MIb);
911}
912
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000913static bool mayAlias(MachineInstr &MIa,
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000914 SmallVectorImpl<MachineInstr *> &MemInsns,
915 const AArch64InstrInfo *TII) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000916 for (MachineInstr *MIb : MemInsns)
917 if (mayAlias(MIa, *MIb, TII))
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000918 return true;
919
920 return false;
921}
922
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000923bool AArch64LoadStoreOpt::findMatchingStore(
924 MachineBasicBlock::iterator I, unsigned Limit,
925 MachineBasicBlock::iterator &StoreI) {
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000926 MachineBasicBlock::iterator B = I->getParent()->begin();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000927 MachineBasicBlock::iterator MBBI = I;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000928 MachineInstr &LoadMI = *I;
Chad Rosier5c6a66c2016-02-09 15:59:57 +0000929 unsigned BaseReg = getLdStBaseOp(LoadMI).getReg();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000930
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000931 // If the load is the first instruction in the block, there's obviously
932 // not any matching store.
933 if (MBBI == B)
934 return false;
935
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000936 // Track which registers have been modified and used between the first insn
937 // and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +0000938 ModifiedRegs.reset();
939 UsedRegs.reset();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000940
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000941 unsigned Count = 0;
942 do {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000943 --MBBI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000944 MachineInstr &MI = *MBBI;
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000945
Geoff Berry4ff2e362016-07-21 15:20:25 +0000946 // Don't count transient instructions towards the search limit since there
947 // may be different numbers of them if e.g. debug information is present.
948 if (!MI.isTransient())
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000949 ++Count;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000950
951 // If the load instruction reads directly from the address to which the
952 // store instruction writes and the stored value is not modified, we can
953 // promote the load. Since we do not handle stores with pre-/post-index,
954 // it's unnecessary to check if BaseReg is modified by the store itself.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000955 if (MI.mayStore() && isMatchingStore(LoadMI, MI) &&
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000956 BaseReg == getLdStBaseOp(MI).getReg() &&
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000957 isLdOffsetInRangeOfSt(LoadMI, MI, TII) &&
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000958 !ModifiedRegs[getLdStRegOp(MI).getReg()]) {
959 StoreI = MBBI;
960 return true;
961 }
962
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000963 if (MI.isCall())
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000964 return false;
965
966 // Update modified / uses register lists.
967 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
968
969 // Otherwise, if the base register is modified, we have no match, so
970 // return early.
971 if (ModifiedRegs[BaseReg])
972 return false;
973
974 // If we encounter a store aliased with the load, return early.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000975 if (MI.mayStore() && mayAlias(LoadMI, MI, TII))
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000976 return false;
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000977 } while (MBBI != B && Count < Limit);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000978 return false;
979}
980
Chad Rosierc5083c22016-06-10 20:47:14 +0000981// Returns true if FirstMI and MI are candidates for merging or pairing.
982// Otherwise, returns false.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000983static bool areCandidatesToMergeOrPair(MachineInstr &FirstMI, MachineInstr &MI,
Chad Rosierc5083c22016-06-10 20:47:14 +0000984 LdStPairFlags &Flags,
985 const AArch64InstrInfo *TII) {
986 // If this is volatile or if pairing is suppressed, not a candidate.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000987 if (MI.hasOrderedMemoryRef() || TII->isLdStPairSuppressed(MI))
Chad Rosierc5083c22016-06-10 20:47:14 +0000988 return false;
989
990 // We should have already checked FirstMI for pair suppression and volatility.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000991 assert(!FirstMI.hasOrderedMemoryRef() &&
992 !TII->isLdStPairSuppressed(FirstMI) &&
Chad Rosierc5083c22016-06-10 20:47:14 +0000993 "FirstMI shouldn't get here if either of these checks are true.");
994
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000995 unsigned OpcA = FirstMI.getOpcode();
996 unsigned OpcB = MI.getOpcode();
Chad Rosierc5083c22016-06-10 20:47:14 +0000997
Chad Rosierc3f6cb92016-02-10 19:45:48 +0000998 // Opcodes match: nothing more to check.
999 if (OpcA == OpcB)
1000 return true;
1001
1002 // Try to match a sign-extended load/store with a zero-extended load/store.
1003 bool IsValidLdStrOpc, PairIsValidLdStrOpc;
1004 unsigned NonSExtOpc = getMatchingNonSExtOpcode(OpcA, &IsValidLdStrOpc);
1005 assert(IsValidLdStrOpc &&
1006 "Given Opc should be a Load or Store with an immediate");
1007 // OpcA will be the first instruction in the pair.
1008 if (NonSExtOpc == getMatchingNonSExtOpcode(OpcB, &PairIsValidLdStrOpc)) {
1009 Flags.setSExtIdx(NonSExtOpc == (unsigned)OpcA ? 1 : 0);
1010 return true;
1011 }
Chad Rosier00f9d232016-02-11 14:25:08 +00001012
Chad Rosierd6daac42016-11-07 15:27:22 +00001013 // If the second instruction isn't even a mergable/pairable load/store, bail
1014 // out.
Chad Rosier00f9d232016-02-11 14:25:08 +00001015 if (!PairIsValidLdStrOpc)
1016 return false;
1017
Chad Rosierd6daac42016-11-07 15:27:22 +00001018 // FIXME: We don't support merging narrow stores with mixed scaled/unscaled
1019 // offsets.
1020 if (isNarrowStore(OpcA) || isNarrowStore(OpcB))
Chad Rosier00f9d232016-02-11 14:25:08 +00001021 return false;
1022
1023 // Try to match an unscaled load/store with a scaled load/store.
Chad Rosiere4e15ba2016-03-09 17:29:48 +00001024 return TII->isUnscaledLdSt(OpcA) != TII->isUnscaledLdSt(OpcB) &&
Chad Rosier00f9d232016-02-11 14:25:08 +00001025 getMatchingPairOpcode(OpcA) == getMatchingPairOpcode(OpcB);
1026
1027 // FIXME: Can we also match a mixed sext/zext unscaled/scaled pair?
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001028}
1029
Chad Rosier9f4ec2e2016-02-10 18:49:28 +00001030/// Scan the instructions looking for a load/store that can be combined with the
1031/// current instruction into a wider equivalent or a load/store pair.
Tim Northover3b0846e2014-05-24 12:50:23 +00001032MachineBasicBlock::iterator
1033AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
Jun Bum Limcf974432016-03-31 14:47:24 +00001034 LdStPairFlags &Flags, unsigned Limit,
1035 bool FindNarrowMerge) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001036 MachineBasicBlock::iterator E = I->getParent()->end();
1037 MachineBasicBlock::iterator MBBI = I;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001038 MachineInstr &FirstMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001039 ++MBBI;
1040
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001041 bool MayLoad = FirstMI.mayLoad();
1042 bool IsUnscaled = TII->isUnscaledLdSt(FirstMI);
Chad Rosierf77e9092015-08-06 15:50:12 +00001043 unsigned Reg = getLdStRegOp(FirstMI).getReg();
1044 unsigned BaseReg = getLdStBaseOp(FirstMI).getReg();
1045 int Offset = getLdStOffsetOp(FirstMI).getImm();
Chad Rosierf11d0402015-10-01 18:17:12 +00001046 int OffsetStride = IsUnscaled ? getMemScale(FirstMI) : 1;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +00001047 bool IsPromotableZeroStore = isPromotableZeroStoreInst(FirstMI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001048
1049 // Track which registers have been modified and used between the first insn
1050 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001051 ModifiedRegs.reset();
1052 UsedRegs.reset();
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001053
1054 // Remember any instructions that read/write memory between FirstMI and MI.
1055 SmallVector<MachineInstr *, 4> MemInsns;
1056
Tim Northover3b0846e2014-05-24 12:50:23 +00001057 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001058 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001059
Geoff Berry4ff2e362016-07-21 15:20:25 +00001060 // Don't count transient instructions towards the search limit since there
1061 // may be different numbers of them if e.g. debug information is present.
1062 if (!MI.isTransient())
1063 ++Count;
Tim Northover3b0846e2014-05-24 12:50:23 +00001064
Chad Rosier18896c02016-02-04 16:01:40 +00001065 Flags.setSExtIdx(-1);
Chad Rosierc5083c22016-06-10 20:47:14 +00001066 if (areCandidatesToMergeOrPair(FirstMI, MI, Flags, TII) &&
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001067 getLdStOffsetOp(MI).isImm()) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001068 assert(MI.mayLoadOrStore() && "Expected memory operation.");
Tim Northover3b0846e2014-05-24 12:50:23 +00001069 // If we've found another instruction with the same opcode, check to see
1070 // if the base and offset are compatible with our starting instruction.
1071 // These instructions all have scaled immediate operands, so we just
1072 // check for +1/-1. Make sure to check the new instruction offset is
1073 // actually an immediate and not a symbolic reference destined for
1074 // a relocation.
Chad Rosierf77e9092015-08-06 15:50:12 +00001075 unsigned MIBaseReg = getLdStBaseOp(MI).getReg();
1076 int MIOffset = getLdStOffsetOp(MI).getImm();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001077 bool MIIsUnscaled = TII->isUnscaledLdSt(MI);
Chad Rosier00f9d232016-02-11 14:25:08 +00001078 if (IsUnscaled != MIIsUnscaled) {
1079 // We're trying to pair instructions that differ in how they are scaled.
1080 // If FirstMI is scaled then scale the offset of MI accordingly.
1081 // Otherwise, do the opposite (i.e., make MI's offset unscaled).
1082 int MemSize = getMemScale(MI);
1083 if (MIIsUnscaled) {
1084 // If the unscaled offset isn't a multiple of the MemSize, we can't
1085 // pair the operations together: bail and keep looking.
Eli Friedmanf184e4b2016-08-12 20:39:51 +00001086 if (MIOffset % MemSize) {
1087 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1088 MemInsns.push_back(&MI);
Chad Rosier00f9d232016-02-11 14:25:08 +00001089 continue;
Eli Friedmanf184e4b2016-08-12 20:39:51 +00001090 }
Chad Rosier00f9d232016-02-11 14:25:08 +00001091 MIOffset /= MemSize;
1092 } else {
1093 MIOffset *= MemSize;
1094 }
1095 }
1096
Tim Northover3b0846e2014-05-24 12:50:23 +00001097 if (BaseReg == MIBaseReg && ((Offset == MIOffset + OffsetStride) ||
1098 (Offset + OffsetStride == MIOffset))) {
1099 int MinOffset = Offset < MIOffset ? Offset : MIOffset;
Jun Bum Limcf974432016-03-31 14:47:24 +00001100 if (FindNarrowMerge) {
Jun Bum Lim80ec0d32015-11-20 21:14:07 +00001101 // If the alignment requirements of the scaled wide load/store
Jun Bum Limcf974432016-03-31 14:47:24 +00001102 // instruction can't express the offset of the scaled narrow input,
1103 // bail and keep looking. For promotable zero stores, allow only when
1104 // the stored value is the same (i.e., WZR).
1105 if ((!IsUnscaled && alignTo(MinOffset, 2) != MinOffset) ||
1106 (IsPromotableZeroStore && Reg != getLdStRegOp(MI).getReg())) {
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001107 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001108 MemInsns.push_back(&MI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001109 continue;
1110 }
1111 } else {
Chad Rosierd1f6c842016-06-10 20:49:18 +00001112 // Pairwise instructions have a 7-bit signed offset field. Single
1113 // insns have a 12-bit unsigned offset field. If the resultant
1114 // immediate offset of merging these instructions is out of range for
1115 // a pairwise instruction, bail and keep looking.
Jun Bum Limcf974432016-03-31 14:47:24 +00001116 if (!inBoundsForPair(IsUnscaled, MinOffset, OffsetStride)) {
1117 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001118 MemInsns.push_back(&MI);
Jun Bum Limcf974432016-03-31 14:47:24 +00001119 continue;
1120 }
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001121 // If the alignment requirements of the paired (scaled) instruction
1122 // can't express the offset of the unscaled input, bail and keep
1123 // looking.
1124 if (IsUnscaled && (alignTo(MinOffset, OffsetStride) != MinOffset)) {
1125 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001126 MemInsns.push_back(&MI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001127 continue;
1128 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001129 }
1130 // If the destination register of the loads is the same register, bail
1131 // and keep looking. A load-pair instruction with both destination
1132 // registers the same is UNPREDICTABLE and will result in an exception.
Jun Bum Limcf974432016-03-31 14:47:24 +00001133 if (MayLoad && Reg == getLdStRegOp(MI).getReg()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001134 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001135 MemInsns.push_back(&MI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001136 continue;
1137 }
1138
1139 // If the Rt of the second instruction was not modified or used between
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001140 // the two instructions and none of the instructions between the second
1141 // and first alias with the second, we can combine the second into the
1142 // first.
Chad Rosierf77e9092015-08-06 15:50:12 +00001143 if (!ModifiedRegs[getLdStRegOp(MI).getReg()] &&
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001144 !(MI.mayLoad() && UsedRegs[getLdStRegOp(MI).getReg()]) &&
1145 !mayAlias(MI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +00001146 Flags.setMergeForward(false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001147 return MBBI;
1148 }
1149
1150 // Likewise, if the Rt of the first instruction is not modified or used
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001151 // between the two instructions and none of the instructions between the
1152 // first and the second alias with the first, we can combine the first
1153 // into the second.
Chad Rosierf77e9092015-08-06 15:50:12 +00001154 if (!ModifiedRegs[getLdStRegOp(FirstMI).getReg()] &&
Chad Rosier5f668e12015-09-03 14:19:43 +00001155 !(MayLoad && UsedRegs[getLdStRegOp(FirstMI).getReg()]) &&
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001156 !mayAlias(FirstMI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +00001157 Flags.setMergeForward(true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001158 return MBBI;
1159 }
1160 // Unable to combine these instructions due to interference in between.
1161 // Keep looking.
1162 }
1163 }
1164
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001165 // If the instruction wasn't a matching load or store. Stop searching if we
1166 // encounter a call instruction that might modify memory.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001167 if (MI.isCall())
Tim Northover3b0846e2014-05-24 12:50:23 +00001168 return E;
1169
1170 // Update modified / uses register lists.
1171 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1172
1173 // Otherwise, if the base register is modified, we have no match, so
1174 // return early.
1175 if (ModifiedRegs[BaseReg])
1176 return E;
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001177
1178 // Update list of instructions that read/write memory.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001179 if (MI.mayLoadOrStore())
1180 MemInsns.push_back(&MI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001181 }
1182 return E;
1183}
1184
1185MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +00001186AArch64LoadStoreOpt::mergeUpdateInsn(MachineBasicBlock::iterator I,
1187 MachineBasicBlock::iterator Update,
1188 bool IsPreIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001189 assert((Update->getOpcode() == AArch64::ADDXri ||
1190 Update->getOpcode() == AArch64::SUBXri) &&
1191 "Unexpected base register update instruction to merge!");
1192 MachineBasicBlock::iterator NextI = I;
1193 // Return the instruction following the merged instruction, which is
1194 // the instruction following our unmerged load. Unless that's the add/sub
1195 // instruction we're merging, in which case it's the one after that.
1196 if (++NextI == Update)
1197 ++NextI;
1198
1199 int Value = Update->getOperand(2).getImm();
1200 assert(AArch64_AM::getShiftValue(Update->getOperand(3).getImm()) == 0 &&
Chad Rosier2dfd3542015-09-23 13:51:44 +00001201 "Can't merge 1 << 12 offset into pre-/post-indexed load / store");
Tim Northover3b0846e2014-05-24 12:50:23 +00001202 if (Update->getOpcode() == AArch64::SUBXri)
1203 Value = -Value;
1204
Chad Rosier2dfd3542015-09-23 13:51:44 +00001205 unsigned NewOpc = IsPreIdx ? getPreIndexedOpcode(I->getOpcode())
1206 : getPostIndexedOpcode(I->getOpcode());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001207 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001208 if (!isPairedLdSt(*I)) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001209 // Non-paired instruction.
1210 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001211 .addOperand(getLdStRegOp(*Update))
1212 .addOperand(getLdStRegOp(*I))
1213 .addOperand(getLdStBaseOp(*I))
Chad Rosier3ada75f2016-01-28 15:38:24 +00001214 .addImm(Value)
1215 .setMemRefs(I->memoperands_begin(), I->memoperands_end());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001216 } else {
1217 // Paired instruction.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001218 int Scale = getMemScale(*I);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001219 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001220 .addOperand(getLdStRegOp(*Update))
1221 .addOperand(getLdStRegOp(*I, 0))
1222 .addOperand(getLdStRegOp(*I, 1))
1223 .addOperand(getLdStBaseOp(*I))
Chad Rosier3ada75f2016-01-28 15:38:24 +00001224 .addImm(Value / Scale)
1225 .setMemRefs(I->memoperands_begin(), I->memoperands_end());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001226 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001227 (void)MIB;
1228
Chad Rosier2dfd3542015-09-23 13:51:44 +00001229 if (IsPreIdx)
1230 DEBUG(dbgs() << "Creating pre-indexed load/store.");
1231 else
1232 DEBUG(dbgs() << "Creating post-indexed load/store.");
Tim Northover3b0846e2014-05-24 12:50:23 +00001233 DEBUG(dbgs() << " Replacing instructions:\n ");
1234 DEBUG(I->print(dbgs()));
1235 DEBUG(dbgs() << " ");
1236 DEBUG(Update->print(dbgs()));
1237 DEBUG(dbgs() << " with instruction:\n ");
1238 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
1239 DEBUG(dbgs() << "\n");
1240
1241 // Erase the old instructions for the block.
1242 I->eraseFromParent();
1243 Update->eraseFromParent();
1244
1245 return NextI;
1246}
1247
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001248bool AArch64LoadStoreOpt::isMatchingUpdateInsn(MachineInstr &MemMI,
1249 MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001250 unsigned BaseReg, int Offset) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001251 switch (MI.getOpcode()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001252 default:
1253 break;
1254 case AArch64::SUBXri:
Tim Northover3b0846e2014-05-24 12:50:23 +00001255 case AArch64::ADDXri:
1256 // Make sure it's a vanilla immediate operand, not a relocation or
1257 // anything else we can't handle.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001258 if (!MI.getOperand(2).isImm())
Tim Northover3b0846e2014-05-24 12:50:23 +00001259 break;
1260 // Watch out for 1 << 12 shifted value.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001261 if (AArch64_AM::getShiftValue(MI.getOperand(3).getImm()))
Tim Northover3b0846e2014-05-24 12:50:23 +00001262 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001263
1264 // The update instruction source and destination register must be the
1265 // same as the load/store base register.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001266 if (MI.getOperand(0).getReg() != BaseReg ||
1267 MI.getOperand(1).getReg() != BaseReg)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001268 break;
1269
1270 bool IsPairedInsn = isPairedLdSt(MemMI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001271 int UpdateOffset = MI.getOperand(2).getImm();
Eli Friedman8585e9d2016-08-12 20:28:02 +00001272 if (MI.getOpcode() == AArch64::SUBXri)
1273 UpdateOffset = -UpdateOffset;
1274
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001275 // For non-paired load/store instructions, the immediate must fit in a
1276 // signed 9-bit integer.
1277 if (!IsPairedInsn && (UpdateOffset > 255 || UpdateOffset < -256))
1278 break;
1279
1280 // For paired load/store instructions, the immediate must be a multiple of
1281 // the scaling factor. The scaled offset must also fit into a signed 7-bit
1282 // integer.
1283 if (IsPairedInsn) {
Chad Rosier32d4d372015-09-29 16:07:32 +00001284 int Scale = getMemScale(MemMI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001285 if (UpdateOffset % Scale != 0)
1286 break;
1287
1288 int ScaledOffset = UpdateOffset / Scale;
Eli Friedman8585e9d2016-08-12 20:28:02 +00001289 if (ScaledOffset > 63 || ScaledOffset < -64)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001290 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00001291 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001292
1293 // If we have a non-zero Offset, we check that it matches the amount
1294 // we're adding to the register.
Eli Friedman8585e9d2016-08-12 20:28:02 +00001295 if (!Offset || Offset == UpdateOffset)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001296 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001297 break;
1298 }
1299 return false;
1300}
1301
1302MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward(
Chad Rosier35706ad2016-02-04 21:26:02 +00001303 MachineBasicBlock::iterator I, int UnscaledOffset, unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001304 MachineBasicBlock::iterator E = I->getParent()->end();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001305 MachineInstr &MemMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001306 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001307
Chad Rosierf77e9092015-08-06 15:50:12 +00001308 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001309 int MIUnscaledOffset = getLdStOffsetOp(MemMI).getImm() * getMemScale(MemMI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001310
Chad Rosierb7c5b912015-10-01 13:43:05 +00001311 // Scan forward looking for post-index opportunities. Updating instructions
1312 // can't be formed if the memory instruction doesn't have the offset we're
1313 // looking for.
1314 if (MIUnscaledOffset != UnscaledOffset)
1315 return E;
1316
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001317 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +00001318 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001319 bool IsPairedInsn = isPairedLdSt(MemMI);
1320 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
1321 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
1322 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
1323 return E;
1324 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001325
Tim Northover3b0846e2014-05-24 12:50:23 +00001326 // Track which registers have been modified and used between the first insn
1327 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001328 ModifiedRegs.reset();
1329 UsedRegs.reset();
Tim Northover3b0846e2014-05-24 12:50:23 +00001330 ++MBBI;
Chad Rosier35706ad2016-02-04 21:26:02 +00001331 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001332 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001333
Geoff Berry4ff2e362016-07-21 15:20:25 +00001334 // Don't count transient instructions towards the search limit since there
1335 // may be different numbers of them if e.g. debug information is present.
1336 if (!MI.isTransient())
1337 ++Count;
Chad Rosier35706ad2016-02-04 21:26:02 +00001338
Tim Northover3b0846e2014-05-24 12:50:23 +00001339 // If we found a match, return it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001340 if (isMatchingUpdateInsn(*I, MI, BaseReg, UnscaledOffset))
Tim Northover3b0846e2014-05-24 12:50:23 +00001341 return MBBI;
1342
1343 // Update the status of what the instruction clobbered and used.
1344 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1345
1346 // Otherwise, if the base register is used or modified, we have no match, so
1347 // return early.
1348 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
1349 return E;
1350 }
1351 return E;
1352}
1353
1354MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnBackward(
Chad Rosier35706ad2016-02-04 21:26:02 +00001355 MachineBasicBlock::iterator I, unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001356 MachineBasicBlock::iterator B = I->getParent()->begin();
1357 MachineBasicBlock::iterator E = I->getParent()->end();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001358 MachineInstr &MemMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001359 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001360
Chad Rosierf77e9092015-08-06 15:50:12 +00001361 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
1362 int Offset = getLdStOffsetOp(MemMI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +00001363
1364 // If the load/store is the first instruction in the block, there's obviously
1365 // not any matching update. Ditto if the memory offset isn't zero.
1366 if (MBBI == B || Offset != 0)
1367 return E;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001368 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +00001369 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001370 bool IsPairedInsn = isPairedLdSt(MemMI);
1371 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
1372 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
1373 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
1374 return E;
1375 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001376
1377 // Track which registers have been modified and used between the first insn
1378 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001379 ModifiedRegs.reset();
1380 UsedRegs.reset();
Geoff Berry173b14d2016-02-09 20:47:21 +00001381 unsigned Count = 0;
1382 do {
1383 --MBBI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001384 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001385
Geoff Berry4ff2e362016-07-21 15:20:25 +00001386 // Don't count transient instructions towards the search limit since there
1387 // may be different numbers of them if e.g. debug information is present.
1388 if (!MI.isTransient())
Geoff Berry173b14d2016-02-09 20:47:21 +00001389 ++Count;
Chad Rosier35706ad2016-02-04 21:26:02 +00001390
Tim Northover3b0846e2014-05-24 12:50:23 +00001391 // If we found a match, return it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001392 if (isMatchingUpdateInsn(*I, MI, BaseReg, Offset))
Tim Northover3b0846e2014-05-24 12:50:23 +00001393 return MBBI;
1394
1395 // Update the status of what the instruction clobbered and used.
1396 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1397
1398 // Otherwise, if the base register is used or modified, we have no match, so
1399 // return early.
1400 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
1401 return E;
Geoff Berry173b14d2016-02-09 20:47:21 +00001402 } while (MBBI != B && Count < Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001403 return E;
1404}
1405
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001406bool AArch64LoadStoreOpt::tryToPromoteLoadFromStore(
1407 MachineBasicBlock::iterator &MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001408 MachineInstr &MI = *MBBI;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001409 // If this is a volatile load, don't mess with it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001410 if (MI.hasOrderedMemoryRef())
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001411 return false;
1412
1413 // Make sure this is a reg+imm.
1414 // FIXME: It is possible to extend it to handle reg+reg cases.
1415 if (!getLdStOffsetOp(MI).isImm())
1416 return false;
1417
Chad Rosier35706ad2016-02-04 21:26:02 +00001418 // Look backward up to LdStLimit instructions.
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001419 MachineBasicBlock::iterator StoreI;
Chad Rosier35706ad2016-02-04 21:26:02 +00001420 if (findMatchingStore(MBBI, LdStLimit, StoreI)) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001421 ++NumLoadsFromStoresPromoted;
1422 // Promote the load. Keeping the iterator straight is a
1423 // pain, so we let the merge routine tell us what the next instruction
1424 // is after it's done mucking about.
1425 MBBI = promoteLoadFromStore(MBBI, StoreI);
1426 return true;
1427 }
1428 return false;
1429}
1430
Chad Rosierd6daac42016-11-07 15:27:22 +00001431// Merge adjacent zero stores into a wider store.
1432bool AArch64LoadStoreOpt::tryToMergeZeroStInst(
Chad Rosier24c46ad2016-02-09 18:10:20 +00001433 MachineBasicBlock::iterator &MBBI) {
Chad Rosierd6daac42016-11-07 15:27:22 +00001434 assert(isPromotableZeroStoreInst(*MBBI) && "Expected narrow store.");
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001435 MachineInstr &MI = *MBBI;
1436 MachineBasicBlock::iterator E = MI.getParent()->end();
Chad Rosier24c46ad2016-02-09 18:10:20 +00001437
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001438 if (!TII->isCandidateToMergeOrPair(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001439 return false;
1440
1441 // Look ahead up to LdStLimit instructions for a mergable instruction.
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001442 LdStPairFlags Flags;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +00001443 MachineBasicBlock::iterator MergeMI =
Jun Bum Limcf974432016-03-31 14:47:24 +00001444 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ true);
Chad Rosierd7363db2016-02-09 19:09:22 +00001445 if (MergeMI != E) {
Chad Rosierd6daac42016-11-07 15:27:22 +00001446 ++NumZeroStoresPromoted;
1447
Chad Rosier24c46ad2016-02-09 18:10:20 +00001448 // Keeping the iterator straight is a pain, so we let the merge routine tell
1449 // us what the next instruction is after it's done mucking about.
Chad Rosierd6daac42016-11-07 15:27:22 +00001450 MBBI = mergeNarrowZeroStores(MBBI, MergeMI, Flags);
Chad Rosier24c46ad2016-02-09 18:10:20 +00001451 return true;
1452 }
1453 return false;
1454}
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001455
Chad Rosier24c46ad2016-02-09 18:10:20 +00001456// Find loads and stores that can be merged into a single load or store pair
1457// instruction.
1458bool AArch64LoadStoreOpt::tryToPairLdStInst(MachineBasicBlock::iterator &MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001459 MachineInstr &MI = *MBBI;
1460 MachineBasicBlock::iterator E = MI.getParent()->end();
Chad Rosier24c46ad2016-02-09 18:10:20 +00001461
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001462 if (!TII->isCandidateToMergeOrPair(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001463 return false;
1464
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001465 // Early exit if the offset is not possible to match. (6 bits of positive
1466 // range, plus allow an extra one in case we find a later insn that matches
1467 // with Offset-1)
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001468 bool IsUnscaled = TII->isUnscaledLdSt(MI);
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001469 int Offset = getLdStOffsetOp(MI).getImm();
1470 int OffsetStride = IsUnscaled ? getMemScale(MI) : 1;
1471 if (!inBoundsForPair(IsUnscaled, Offset, OffsetStride))
1472 return false;
1473
Chad Rosier24c46ad2016-02-09 18:10:20 +00001474 // Look ahead up to LdStLimit instructions for a pairable instruction.
1475 LdStPairFlags Flags;
Jun Bum Limcf974432016-03-31 14:47:24 +00001476 MachineBasicBlock::iterator Paired =
1477 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ false);
Chad Rosier24c46ad2016-02-09 18:10:20 +00001478 if (Paired != E) {
1479 ++NumPairCreated;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001480 if (TII->isUnscaledLdSt(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001481 ++NumUnscaledPairCreated;
1482 // Keeping the iterator straight is a pain, so we let the merge routine tell
1483 // us what the next instruction is after it's done mucking about.
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001484 MBBI = mergePairedInsns(MBBI, Paired, Flags);
1485 return true;
1486 }
1487 return false;
1488}
1489
Jun Bum Lim22fe15e2015-11-06 16:27:47 +00001490bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB,
Chad Rosierd6daac42016-11-07 15:27:22 +00001491 bool EnableNarrowZeroStOpt) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001492 bool Modified = false;
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001493 // Four tranformations to do here:
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001494 // 1) Find loads that directly read from stores and promote them by
1495 // replacing with mov instructions. If the store is wider than the load,
1496 // the load will be replaced with a bitfield extract.
1497 // e.g.,
1498 // str w1, [x0, #4]
1499 // ldrh w2, [x0, #6]
1500 // ; becomes
1501 // str w1, [x0, #4]
NAKAMURA Takumife1202c2016-06-20 00:37:41 +00001502 // lsr w2, w1, #16
Tim Northover3b0846e2014-05-24 12:50:23 +00001503 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001504 MBBI != E;) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001505 MachineInstr &MI = *MBBI;
1506 switch (MI.getOpcode()) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001507 default:
1508 // Just move on to the next instruction.
1509 ++MBBI;
1510 break;
1511 // Scaled instructions.
1512 case AArch64::LDRBBui:
1513 case AArch64::LDRHHui:
1514 case AArch64::LDRWui:
1515 case AArch64::LDRXui:
1516 // Unscaled instructions.
1517 case AArch64::LDURBBi:
1518 case AArch64::LDURHHi:
1519 case AArch64::LDURWi:
1520 case AArch64::LDURXi: {
1521 if (tryToPromoteLoadFromStore(MBBI)) {
1522 Modified = true;
1523 break;
1524 }
1525 ++MBBI;
1526 break;
1527 }
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001528 }
1529 }
Chad Rosierd6daac42016-11-07 15:27:22 +00001530 // 2) Merge adjacent zero stores into a wider store.
Jun Bum Lim1de2d442016-02-05 20:02:03 +00001531 // e.g.,
1532 // strh wzr, [x0]
1533 // strh wzr, [x0, #2]
1534 // ; becomes
1535 // str wzr, [x0]
Chad Rosierd6daac42016-11-07 15:27:22 +00001536 // e.g.,
1537 // str wzr, [x0]
1538 // str wzr, [x0, #4]
1539 // ; becomes
1540 // str xzr, [x0]
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001541 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Chad Rosierd6daac42016-11-07 15:27:22 +00001542 EnableNarrowZeroStOpt && MBBI != E;) {
1543 if (isPromotableZeroStoreInst(*MBBI)) {
1544 if (tryToMergeZeroStInst(MBBI)) {
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001545 Modified = true;
Jun Bum Lim33be4992016-05-06 15:08:57 +00001546 } else
1547 ++MBBI;
1548 } else
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001549 ++MBBI;
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001550 }
Jun Bum Lim33be4992016-05-06 15:08:57 +00001551
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001552 // 3) Find loads and stores that can be merged into a single load or store
1553 // pair instruction.
1554 // e.g.,
1555 // ldr x0, [x2]
1556 // ldr x1, [x2, #8]
1557 // ; becomes
1558 // ldp x0, x1, [x2]
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001559 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Tim Northover3b0846e2014-05-24 12:50:23 +00001560 MBBI != E;) {
Geoff Berry22dfbc52016-08-12 15:26:00 +00001561 if (TII->isPairableLdStInst(*MBBI) && tryToPairLdStInst(MBBI))
1562 Modified = true;
1563 else
Tim Northover3b0846e2014-05-24 12:50:23 +00001564 ++MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001565 }
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001566 // 4) Find base register updates that can be merged into the load or store
1567 // as a base-reg writeback.
1568 // e.g.,
1569 // ldr x0, [x2]
1570 // add x2, x2, #4
1571 // ; becomes
1572 // ldr x0, [x2], #4
Tim Northover3b0846e2014-05-24 12:50:23 +00001573 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
1574 MBBI != E;) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001575 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001576 // Do update merging. It's simpler to keep this separate from the above
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001577 // switchs, though not strictly necessary.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001578 unsigned Opc = MI.getOpcode();
Tim Northover3b0846e2014-05-24 12:50:23 +00001579 switch (Opc) {
1580 default:
1581 // Just move on to the next instruction.
1582 ++MBBI;
1583 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001584 // Scaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001585 case AArch64::STRSui:
1586 case AArch64::STRDui:
1587 case AArch64::STRQui:
1588 case AArch64::STRXui:
1589 case AArch64::STRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001590 case AArch64::STRHHui:
1591 case AArch64::STRBBui:
Tim Northover3b0846e2014-05-24 12:50:23 +00001592 case AArch64::LDRSui:
1593 case AArch64::LDRDui:
1594 case AArch64::LDRQui:
1595 case AArch64::LDRXui:
1596 case AArch64::LDRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001597 case AArch64::LDRHHui:
1598 case AArch64::LDRBBui:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001599 // Unscaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001600 case AArch64::STURSi:
1601 case AArch64::STURDi:
1602 case AArch64::STURQi:
1603 case AArch64::STURWi:
1604 case AArch64::STURXi:
1605 case AArch64::LDURSi:
1606 case AArch64::LDURDi:
1607 case AArch64::LDURQi:
1608 case AArch64::LDURWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001609 case AArch64::LDURXi:
1610 // Paired instructions.
1611 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +00001612 case AArch64::LDPSWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001613 case AArch64::LDPDi:
1614 case AArch64::LDPQi:
1615 case AArch64::LDPWi:
1616 case AArch64::LDPXi:
1617 case AArch64::STPSi:
1618 case AArch64::STPDi:
1619 case AArch64::STPQi:
1620 case AArch64::STPWi:
1621 case AArch64::STPXi: {
Tim Northover3b0846e2014-05-24 12:50:23 +00001622 // Make sure this is a reg+imm (as opposed to an address reloc).
Chad Rosierf77e9092015-08-06 15:50:12 +00001623 if (!getLdStOffsetOp(MI).isImm()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001624 ++MBBI;
1625 break;
1626 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001627 // Look forward to try to form a post-index instruction. For example,
1628 // ldr x0, [x20]
1629 // add x20, x20, #32
1630 // merged into:
1631 // ldr x0, [x20], #32
Tim Northover3b0846e2014-05-24 12:50:23 +00001632 MachineBasicBlock::iterator Update =
Chad Rosier35706ad2016-02-04 21:26:02 +00001633 findMatchingUpdateInsnForward(MBBI, 0, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001634 if (Update != E) {
1635 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001636 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001637 Modified = true;
1638 ++NumPostFolded;
1639 break;
1640 }
1641 // Don't know how to handle pre/post-index versions, so move to the next
1642 // instruction.
Chad Rosiere4e15ba2016-03-09 17:29:48 +00001643 if (TII->isUnscaledLdSt(Opc)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001644 ++MBBI;
1645 break;
1646 }
1647
1648 // Look back to try to find a pre-index instruction. For example,
1649 // add x0, x0, #8
1650 // ldr x1, [x0]
1651 // merged into:
1652 // ldr x1, [x0, #8]!
Chad Rosier35706ad2016-02-04 21:26:02 +00001653 Update = findMatchingUpdateInsnBackward(MBBI, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001654 if (Update != E) {
1655 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001656 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001657 Modified = true;
1658 ++NumPreFolded;
1659 break;
1660 }
Chad Rosier7a83d772015-10-01 13:09:44 +00001661 // The immediate in the load/store is scaled by the size of the memory
1662 // operation. The immediate in the add we're looking for,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001663 // however, is not, so adjust here.
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001664 int UnscaledOffset = getLdStOffsetOp(MI).getImm() * getMemScale(MI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001665
Tim Northover3b0846e2014-05-24 12:50:23 +00001666 // Look forward to try to find a post-index instruction. For example,
1667 // ldr x1, [x0, #64]
1668 // add x0, x0, #64
1669 // merged into:
1670 // ldr x1, [x0, #64]!
Chad Rosier35706ad2016-02-04 21:26:02 +00001671 Update = findMatchingUpdateInsnForward(MBBI, UnscaledOffset, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001672 if (Update != E) {
1673 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001674 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001675 Modified = true;
1676 ++NumPreFolded;
1677 break;
1678 }
1679
1680 // Nothing found. Just move to the next instruction.
1681 ++MBBI;
1682 break;
1683 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001684 }
1685 }
1686
1687 return Modified;
1688}
1689
1690bool AArch64LoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
Andrew Kaylor1ac98bb2016-04-25 21:58:52 +00001691 if (skipFunction(*Fn.getFunction()))
1692 return false;
1693
Oliver Stannardd414c992015-11-10 11:04:18 +00001694 Subtarget = &static_cast<const AArch64Subtarget &>(Fn.getSubtarget());
1695 TII = static_cast<const AArch64InstrInfo *>(Subtarget->getInstrInfo());
1696 TRI = Subtarget->getRegisterInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +00001697
Chad Rosierbba881e2016-02-02 15:02:30 +00001698 // Resize the modified and used register bitfield trackers. We do this once
1699 // per function and then clear the bitfield each time we optimize a load or
1700 // store.
1701 ModifiedRegs.resize(TRI->getNumRegs());
1702 UsedRegs.resize(TRI->getNumRegs());
1703
Tim Northover3b0846e2014-05-24 12:50:23 +00001704 bool Modified = false;
Chad Rosierd6daac42016-11-07 15:27:22 +00001705 bool enableNarrowZeroStOpt =
1706 Subtarget->mergeNarrowStores() && !Subtarget->requiresStrictAlign();
Tim Northover3b0846e2014-05-24 12:50:23 +00001707 for (auto &MBB : Fn)
Chad Rosierd6daac42016-11-07 15:27:22 +00001708 Modified |= optimizeBlock(MBB, enableNarrowZeroStOpt);
Tim Northover3b0846e2014-05-24 12:50:23 +00001709
1710 return Modified;
1711}
1712
1713// FIXME: Do we need/want a pre-alloc pass like ARM has to try to keep
1714// loads and stores near one another?
1715
Chad Rosier3f8b09d2016-02-09 19:42:19 +00001716// FIXME: When pairing store instructions it's very possible for this pass to
1717// hoist a store with a KILL marker above another use (without a KILL marker).
1718// The resulting IR is invalid, but nothing uses the KILL markers after this
1719// pass, so it's never caused a problem in practice.
1720
Chad Rosier43f5c842015-08-05 12:40:13 +00001721/// createAArch64LoadStoreOptimizationPass - returns an instance of the
1722/// load / store optimization pass.
Tim Northover3b0846e2014-05-24 12:50:23 +00001723FunctionPass *llvm::createAArch64LoadStoreOptimizationPass() {
1724 return new AArch64LoadStoreOpt();
1725}