blob: 443a14f7068bd1f2dca64c09016349ea42e33b2c [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"
Eugene Zelenko11f69072017-01-25 00:29:26 +000019#include "llvm/ADT/iterator_range.h"
Chad Rosierce8e5ab2015-05-21 21:36:46 +000020#include "llvm/ADT/SmallVector.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000021#include "llvm/ADT/Statistic.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +000022#include "llvm/ADT/StringRef.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000023#include "llvm/CodeGen/MachineBasicBlock.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +000024#include "llvm/CodeGen/MachineFunction.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstr.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +000028#include "llvm/CodeGen/MachineOperand.h"
29#include "llvm/IR/DebugLoc.h"
30#include "llvm/MC/MCRegisterInfo.h"
31#include "llvm/Pass.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/raw_ostream.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000036#include "llvm/Target/TargetRegisterInfo.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +000037#include <cassert>
38#include <cstdint>
39#include <iterator>
40#include <limits>
41
Tim Northover3b0846e2014-05-24 12:50:23 +000042using namespace llvm;
43
44#define DEBUG_TYPE "aarch64-ldst-opt"
45
Tim Northover3b0846e2014-05-24 12:50:23 +000046STATISTIC(NumPairCreated, "Number of load/store pair instructions generated");
47STATISTIC(NumPostFolded, "Number of post-index updates folded");
48STATISTIC(NumPreFolded, "Number of pre-index updates folded");
49STATISTIC(NumUnscaledPairCreated,
50 "Number of load/store from unscaled generated");
Jun Bum Lim80ec0d32015-11-20 21:14:07 +000051STATISTIC(NumZeroStoresPromoted, "Number of narrow zero stores promoted");
Jun Bum Lim6755c3b2015-12-22 16:36:16 +000052STATISTIC(NumLoadsFromStoresPromoted, "Number of loads from stores promoted");
Tim Northover3b0846e2014-05-24 12:50:23 +000053
Chad Rosier35706ad2016-02-04 21:26:02 +000054// The LdStLimit limits how far we search for load/store pairs.
55static cl::opt<unsigned> LdStLimit("aarch64-load-store-scan-limit",
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +000056 cl::init(20), cl::Hidden);
Tim Northover3b0846e2014-05-24 12:50:23 +000057
Chad Rosier35706ad2016-02-04 21:26:02 +000058// The UpdateLimit limits how far we search for update instructions when we form
59// pre-/post-index instructions.
60static cl::opt<unsigned> UpdateLimit("aarch64-update-scan-limit", cl::init(100),
61 cl::Hidden);
62
Chad Rosier96530b32015-08-05 13:44:51 +000063#define AARCH64_LOAD_STORE_OPT_NAME "AArch64 load / store optimization pass"
64
Tim Northover3b0846e2014-05-24 12:50:23 +000065namespace {
Chad Rosier96a18a92015-07-21 17:42:04 +000066
67typedef struct LdStPairFlags {
68 // If a matching instruction is found, MergeForward is set to true if the
69 // merge is to remove the first instruction and replace the second with
70 // a pair-wise insn, and false if the reverse is true.
Eugene Zelenko11f69072017-01-25 00:29:26 +000071 bool MergeForward = false;
Chad Rosier96a18a92015-07-21 17:42:04 +000072
73 // SExtIdx gives the index of the result of the load pair that must be
74 // extended. The value of SExtIdx assumes that the paired load produces the
75 // value in this order: (I, returned iterator), i.e., -1 means no value has
76 // to be extended, 0 means I, and 1 means the returned iterator.
Eugene Zelenko11f69072017-01-25 00:29:26 +000077 int SExtIdx = -1;
Chad Rosier96a18a92015-07-21 17:42:04 +000078
Eugene Zelenko11f69072017-01-25 00:29:26 +000079 LdStPairFlags() = default;
Chad Rosier96a18a92015-07-21 17:42:04 +000080
81 void setMergeForward(bool V = true) { MergeForward = V; }
82 bool getMergeForward() const { return MergeForward; }
83
84 void setSExtIdx(int V) { SExtIdx = V; }
85 int getSExtIdx() const { return SExtIdx; }
86
87} LdStPairFlags;
88
Tim Northover3b0846e2014-05-24 12:50:23 +000089struct AArch64LoadStoreOpt : public MachineFunctionPass {
90 static char ID;
Eugene Zelenko11f69072017-01-25 00:29:26 +000091
Jun Bum Lim22fe15e2015-11-06 16:27:47 +000092 AArch64LoadStoreOpt() : MachineFunctionPass(ID) {
Chad Rosier96530b32015-08-05 13:44:51 +000093 initializeAArch64LoadStoreOptPass(*PassRegistry::getPassRegistry());
94 }
Tim Northover3b0846e2014-05-24 12:50:23 +000095
96 const AArch64InstrInfo *TII;
97 const TargetRegisterInfo *TRI;
Oliver Stannardd414c992015-11-10 11:04:18 +000098 const AArch64Subtarget *Subtarget;
Tim Northover3b0846e2014-05-24 12:50:23 +000099
Chad Rosierbba881e2016-02-02 15:02:30 +0000100 // Track which registers have been modified and used.
101 BitVector ModifiedRegs, UsedRegs;
102
Tim Northover3b0846e2014-05-24 12:50:23 +0000103 // Scan the instructions looking for a load/store that can be combined
104 // with the current instruction into a load/store pair.
105 // Return the matching instruction if one is found, else MBB->end().
Tim Northover3b0846e2014-05-24 12:50:23 +0000106 MachineBasicBlock::iterator findMatchingInsn(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +0000107 LdStPairFlags &Flags,
Jun Bum Limcf974432016-03-31 14:47:24 +0000108 unsigned Limit,
109 bool FindNarrowMerge);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000110
111 // Scan the instructions looking for a store that writes to the address from
112 // which the current load instruction reads. Return true if one is found.
113 bool findMatchingStore(MachineBasicBlock::iterator I, unsigned Limit,
114 MachineBasicBlock::iterator &StoreI);
115
Chad Rosierd6daac42016-11-07 15:27:22 +0000116 // Merge the two instructions indicated into a wider narrow store instruction.
Chad Rosierb5933d72016-02-09 19:02:12 +0000117 MachineBasicBlock::iterator
Chad Rosierd6daac42016-11-07 15:27:22 +0000118 mergeNarrowZeroStores(MachineBasicBlock::iterator I,
119 MachineBasicBlock::iterator MergeMI,
120 const LdStPairFlags &Flags);
Chad Rosierb5933d72016-02-09 19:02:12 +0000121
Tim Northover3b0846e2014-05-24 12:50:23 +0000122 // Merge the two instructions indicated into a single pair-wise instruction.
Tim Northover3b0846e2014-05-24 12:50:23 +0000123 MachineBasicBlock::iterator
124 mergePairedInsns(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +0000125 MachineBasicBlock::iterator Paired,
Chad Rosierfe5399f2015-07-21 17:47:56 +0000126 const LdStPairFlags &Flags);
Tim Northover3b0846e2014-05-24 12:50:23 +0000127
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000128 // Promote the load that reads directly from the address stored to.
129 MachineBasicBlock::iterator
130 promoteLoadFromStore(MachineBasicBlock::iterator LoadI,
131 MachineBasicBlock::iterator StoreI);
132
Tim Northover3b0846e2014-05-24 12:50:23 +0000133 // Scan the instruction list to find a base register update that can
134 // be combined with the current instruction (a load or store) using
135 // pre or post indexed addressing with writeback. Scan forwards.
136 MachineBasicBlock::iterator
Chad Rosier234bf6f2016-01-18 21:56:40 +0000137 findMatchingUpdateInsnForward(MachineBasicBlock::iterator I,
Chad Rosier35706ad2016-02-04 21:26:02 +0000138 int UnscaledOffset, unsigned Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +0000139
140 // Scan the instruction list to find a base register update that can
141 // be combined with the current instruction (a load or store) using
142 // pre or post indexed addressing with writeback. Scan backwards.
143 MachineBasicBlock::iterator
Chad Rosier35706ad2016-02-04 21:26:02 +0000144 findMatchingUpdateInsnBackward(MachineBasicBlock::iterator I, unsigned Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +0000145
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000146 // Find an instruction that updates the base register of the ld/st
147 // instruction.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000148 bool isMatchingUpdateInsn(MachineInstr &MemMI, MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000149 unsigned BaseReg, int Offset);
150
Chad Rosier2dfd3542015-09-23 13:51:44 +0000151 // Merge a pre- or post-index base register update into a ld/st instruction.
Tim Northover3b0846e2014-05-24 12:50:23 +0000152 MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +0000153 mergeUpdateInsn(MachineBasicBlock::iterator I,
154 MachineBasicBlock::iterator Update, bool IsPreIdx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000155
Chad Rosierd6daac42016-11-07 15:27:22 +0000156 // Find and merge zero store instructions.
157 bool tryToMergeZeroStInst(MachineBasicBlock::iterator &MBBI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +0000158
Chad Rosier24c46ad2016-02-09 18:10:20 +0000159 // Find and pair ldr/str instructions.
160 bool tryToPairLdStInst(MachineBasicBlock::iterator &MBBI);
161
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000162 // Find and promote load instructions which read directly from store.
163 bool tryToPromoteLoadFromStore(MachineBasicBlock::iterator &MBBI);
164
Chad Rosierd6daac42016-11-07 15:27:22 +0000165 bool optimizeBlock(MachineBasicBlock &MBB, bool EnableNarrowZeroStOpt);
Tim Northover3b0846e2014-05-24 12:50:23 +0000166
167 bool runOnMachineFunction(MachineFunction &Fn) override;
168
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000169 MachineFunctionProperties getRequiredProperties() const override {
170 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000171 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000172 }
173
Mehdi Amini117296c2016-10-01 02:56:57 +0000174 StringRef getPassName() const override { return AARCH64_LOAD_STORE_OPT_NAME; }
Tim Northover3b0846e2014-05-24 12:50:23 +0000175};
Eugene Zelenko11f69072017-01-25 00:29:26 +0000176
Tim Northover3b0846e2014-05-24 12:50:23 +0000177char AArch64LoadStoreOpt::ID = 0;
Eugene Zelenko11f69072017-01-25 00:29:26 +0000178
179} // end anonymous namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000180
Chad Rosier96530b32015-08-05 13:44:51 +0000181INITIALIZE_PASS(AArch64LoadStoreOpt, "aarch64-ldst-opt",
182 AARCH64_LOAD_STORE_OPT_NAME, false, false)
183
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000184static bool isNarrowStore(unsigned Opc) {
185 switch (Opc) {
186 default:
187 return false;
188 case AArch64::STRBBui:
189 case AArch64::STURBBi:
190 case AArch64::STRHHui:
191 case AArch64::STURHHi:
192 return true;
193 }
194}
195
Chad Rosier32d4d372015-09-29 16:07:32 +0000196// Scaling factor for unscaled load or store.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000197static int getMemScale(MachineInstr &MI) {
198 switch (MI.getOpcode()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000199 default:
Chad Rosierdabe2532015-09-29 18:26:15 +0000200 llvm_unreachable("Opcode has unknown scale!");
201 case AArch64::LDRBBui:
Jun Bum Lim4c35cca2015-11-19 17:21:41 +0000202 case AArch64::LDURBBi:
203 case AArch64::LDRSBWui:
204 case AArch64::LDURSBWi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000205 case AArch64::STRBBui:
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000206 case AArch64::STURBBi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000207 return 1;
208 case AArch64::LDRHHui:
Jun Bum Limc9879ec2015-10-27 19:16:03 +0000209 case AArch64::LDURHHi:
Jun Bum Lim4c35cca2015-11-19 17:21:41 +0000210 case AArch64::LDRSHWui:
211 case AArch64::LDURSHWi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000212 case AArch64::STRHHui:
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000213 case AArch64::STURHHi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000214 return 2;
Chad Rosiera4d32172015-09-29 14:57:10 +0000215 case AArch64::LDRSui:
216 case AArch64::LDURSi:
217 case AArch64::LDRSWui:
218 case AArch64::LDURSWi:
219 case AArch64::LDRWui:
220 case AArch64::LDURWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000221 case AArch64::STRSui:
222 case AArch64::STURSi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000223 case AArch64::STRWui:
224 case AArch64::STURWi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000225 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +0000226 case AArch64::LDPSWi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000227 case AArch64::LDPWi:
228 case AArch64::STPSi:
229 case AArch64::STPWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000230 return 4;
Chad Rosiera4d32172015-09-29 14:57:10 +0000231 case AArch64::LDRDui:
232 case AArch64::LDURDi:
233 case AArch64::LDRXui:
234 case AArch64::LDURXi:
235 case AArch64::STRDui:
236 case AArch64::STURDi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000237 case AArch64::STRXui:
238 case AArch64::STURXi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000239 case AArch64::LDPDi:
240 case AArch64::LDPXi:
241 case AArch64::STPDi:
242 case AArch64::STPXi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000243 return 8;
Tim Northover3b0846e2014-05-24 12:50:23 +0000244 case AArch64::LDRQui:
245 case AArch64::LDURQi:
Chad Rosiera4d32172015-09-29 14:57:10 +0000246 case AArch64::STRQui:
247 case AArch64::STURQi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000248 case AArch64::LDPQi:
249 case AArch64::STPQi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000250 return 16;
Tim Northover3b0846e2014-05-24 12:50:23 +0000251 }
252}
253
Quentin Colombet66b61632015-03-06 22:42:10 +0000254static unsigned getMatchingNonSExtOpcode(unsigned Opc,
255 bool *IsValidLdStrOpc = nullptr) {
256 if (IsValidLdStrOpc)
257 *IsValidLdStrOpc = true;
258 switch (Opc) {
259 default:
260 if (IsValidLdStrOpc)
261 *IsValidLdStrOpc = false;
Eugene Zelenko11f69072017-01-25 00:29:26 +0000262 return std::numeric_limits<unsigned>::max();
Quentin Colombet66b61632015-03-06 22:42:10 +0000263 case AArch64::STRDui:
264 case AArch64::STURDi:
265 case AArch64::STRQui:
266 case AArch64::STURQi:
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000267 case AArch64::STRBBui:
268 case AArch64::STURBBi:
269 case AArch64::STRHHui:
270 case AArch64::STURHHi:
Quentin Colombet66b61632015-03-06 22:42:10 +0000271 case AArch64::STRWui:
272 case AArch64::STURWi:
273 case AArch64::STRXui:
274 case AArch64::STURXi:
275 case AArch64::LDRDui:
276 case AArch64::LDURDi:
277 case AArch64::LDRQui:
278 case AArch64::LDURQi:
279 case AArch64::LDRWui:
280 case AArch64::LDURWi:
281 case AArch64::LDRXui:
282 case AArch64::LDURXi:
283 case AArch64::STRSui:
284 case AArch64::STURSi:
285 case AArch64::LDRSui:
286 case AArch64::LDURSi:
287 return Opc;
288 case AArch64::LDRSWui:
289 return AArch64::LDRWui;
290 case AArch64::LDURSWi:
291 return AArch64::LDURWi;
292 }
293}
294
Jun Bum Lim1de2d442016-02-05 20:02:03 +0000295static unsigned getMatchingWideOpcode(unsigned Opc) {
296 switch (Opc) {
297 default:
298 llvm_unreachable("Opcode has no wide equivalent!");
299 case AArch64::STRBBui:
300 return AArch64::STRHHui;
301 case AArch64::STRHHui:
302 return AArch64::STRWui;
303 case AArch64::STURBBi:
304 return AArch64::STURHHi;
305 case AArch64::STURHHi:
306 return AArch64::STURWi;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +0000307 case AArch64::STURWi:
308 return AArch64::STURXi;
309 case AArch64::STRWui:
310 return AArch64::STRXui;
Jun Bum Lim1de2d442016-02-05 20:02:03 +0000311 }
312}
313
Tim Northover3b0846e2014-05-24 12:50:23 +0000314static unsigned getMatchingPairOpcode(unsigned Opc) {
315 switch (Opc) {
316 default:
317 llvm_unreachable("Opcode has no pairwise equivalent!");
318 case AArch64::STRSui:
319 case AArch64::STURSi:
320 return AArch64::STPSi;
321 case AArch64::STRDui:
322 case AArch64::STURDi:
323 return AArch64::STPDi;
324 case AArch64::STRQui:
325 case AArch64::STURQi:
326 return AArch64::STPQi;
327 case AArch64::STRWui:
328 case AArch64::STURWi:
329 return AArch64::STPWi;
330 case AArch64::STRXui:
331 case AArch64::STURXi:
332 return AArch64::STPXi;
333 case AArch64::LDRSui:
334 case AArch64::LDURSi:
335 return AArch64::LDPSi;
336 case AArch64::LDRDui:
337 case AArch64::LDURDi:
338 return AArch64::LDPDi;
339 case AArch64::LDRQui:
340 case AArch64::LDURQi:
341 return AArch64::LDPQi;
342 case AArch64::LDRWui:
343 case AArch64::LDURWi:
344 return AArch64::LDPWi;
345 case AArch64::LDRXui:
346 case AArch64::LDURXi:
347 return AArch64::LDPXi;
Quentin Colombet29f55332015-01-24 01:25:54 +0000348 case AArch64::LDRSWui:
349 case AArch64::LDURSWi:
350 return AArch64::LDPSWi;
Tim Northover3b0846e2014-05-24 12:50:23 +0000351 }
352}
353
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000354static unsigned isMatchingStore(MachineInstr &LoadInst,
355 MachineInstr &StoreInst) {
356 unsigned LdOpc = LoadInst.getOpcode();
357 unsigned StOpc = StoreInst.getOpcode();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000358 switch (LdOpc) {
359 default:
360 llvm_unreachable("Unsupported load instruction!");
361 case AArch64::LDRBBui:
362 return StOpc == AArch64::STRBBui || StOpc == AArch64::STRHHui ||
363 StOpc == AArch64::STRWui || StOpc == AArch64::STRXui;
364 case AArch64::LDURBBi:
365 return StOpc == AArch64::STURBBi || StOpc == AArch64::STURHHi ||
366 StOpc == AArch64::STURWi || StOpc == AArch64::STURXi;
367 case AArch64::LDRHHui:
368 return StOpc == AArch64::STRHHui || StOpc == AArch64::STRWui ||
369 StOpc == AArch64::STRXui;
370 case AArch64::LDURHHi:
371 return StOpc == AArch64::STURHHi || StOpc == AArch64::STURWi ||
372 StOpc == AArch64::STURXi;
373 case AArch64::LDRWui:
374 return StOpc == AArch64::STRWui || StOpc == AArch64::STRXui;
375 case AArch64::LDURWi:
376 return StOpc == AArch64::STURWi || StOpc == AArch64::STURXi;
377 case AArch64::LDRXui:
378 return StOpc == AArch64::STRXui;
379 case AArch64::LDURXi:
380 return StOpc == AArch64::STURXi;
381 }
382}
383
Tim Northover3b0846e2014-05-24 12:50:23 +0000384static unsigned getPreIndexedOpcode(unsigned Opc) {
385 switch (Opc) {
386 default:
387 llvm_unreachable("Opcode has no pre-indexed equivalent!");
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000388 case AArch64::STRSui:
389 return AArch64::STRSpre;
390 case AArch64::STRDui:
391 return AArch64::STRDpre;
392 case AArch64::STRQui:
393 return AArch64::STRQpre;
Chad Rosierdabe2532015-09-29 18:26:15 +0000394 case AArch64::STRBBui:
395 return AArch64::STRBBpre;
396 case AArch64::STRHHui:
397 return AArch64::STRHHpre;
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000398 case AArch64::STRWui:
399 return AArch64::STRWpre;
400 case AArch64::STRXui:
401 return AArch64::STRXpre;
402 case AArch64::LDRSui:
403 return AArch64::LDRSpre;
404 case AArch64::LDRDui:
405 return AArch64::LDRDpre;
406 case AArch64::LDRQui:
407 return AArch64::LDRQpre;
Chad Rosierdabe2532015-09-29 18:26:15 +0000408 case AArch64::LDRBBui:
409 return AArch64::LDRBBpre;
410 case AArch64::LDRHHui:
411 return AArch64::LDRHHpre;
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000412 case AArch64::LDRWui:
413 return AArch64::LDRWpre;
414 case AArch64::LDRXui:
415 return AArch64::LDRXpre;
Quentin Colombet29f55332015-01-24 01:25:54 +0000416 case AArch64::LDRSWui:
417 return AArch64::LDRSWpre;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000418 case AArch64::LDPSi:
419 return AArch64::LDPSpre;
Chad Rosier43150122015-09-29 20:39:55 +0000420 case AArch64::LDPSWi:
421 return AArch64::LDPSWpre;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000422 case AArch64::LDPDi:
423 return AArch64::LDPDpre;
424 case AArch64::LDPQi:
425 return AArch64::LDPQpre;
426 case AArch64::LDPWi:
427 return AArch64::LDPWpre;
428 case AArch64::LDPXi:
429 return AArch64::LDPXpre;
430 case AArch64::STPSi:
431 return AArch64::STPSpre;
432 case AArch64::STPDi:
433 return AArch64::STPDpre;
434 case AArch64::STPQi:
435 return AArch64::STPQpre;
436 case AArch64::STPWi:
437 return AArch64::STPWpre;
438 case AArch64::STPXi:
439 return AArch64::STPXpre;
Tim Northover3b0846e2014-05-24 12:50:23 +0000440 }
441}
442
443static unsigned getPostIndexedOpcode(unsigned Opc) {
444 switch (Opc) {
445 default:
446 llvm_unreachable("Opcode has no post-indexed wise equivalent!");
447 case AArch64::STRSui:
448 return AArch64::STRSpost;
449 case AArch64::STRDui:
450 return AArch64::STRDpost;
451 case AArch64::STRQui:
452 return AArch64::STRQpost;
Chad Rosierdabe2532015-09-29 18:26:15 +0000453 case AArch64::STRBBui:
454 return AArch64::STRBBpost;
455 case AArch64::STRHHui:
456 return AArch64::STRHHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000457 case AArch64::STRWui:
458 return AArch64::STRWpost;
459 case AArch64::STRXui:
460 return AArch64::STRXpost;
461 case AArch64::LDRSui:
462 return AArch64::LDRSpost;
463 case AArch64::LDRDui:
464 return AArch64::LDRDpost;
465 case AArch64::LDRQui:
466 return AArch64::LDRQpost;
Chad Rosierdabe2532015-09-29 18:26:15 +0000467 case AArch64::LDRBBui:
468 return AArch64::LDRBBpost;
469 case AArch64::LDRHHui:
470 return AArch64::LDRHHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000471 case AArch64::LDRWui:
472 return AArch64::LDRWpost;
473 case AArch64::LDRXui:
474 return AArch64::LDRXpost;
Quentin Colombet29f55332015-01-24 01:25:54 +0000475 case AArch64::LDRSWui:
476 return AArch64::LDRSWpost;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000477 case AArch64::LDPSi:
478 return AArch64::LDPSpost;
Chad Rosier43150122015-09-29 20:39:55 +0000479 case AArch64::LDPSWi:
480 return AArch64::LDPSWpost;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000481 case AArch64::LDPDi:
482 return AArch64::LDPDpost;
483 case AArch64::LDPQi:
484 return AArch64::LDPQpost;
485 case AArch64::LDPWi:
486 return AArch64::LDPWpost;
487 case AArch64::LDPXi:
488 return AArch64::LDPXpost;
489 case AArch64::STPSi:
490 return AArch64::STPSpost;
491 case AArch64::STPDi:
492 return AArch64::STPDpost;
493 case AArch64::STPQi:
494 return AArch64::STPQpost;
495 case AArch64::STPWi:
496 return AArch64::STPWpost;
497 case AArch64::STPXi:
498 return AArch64::STPXpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000499 }
500}
501
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000502static bool isPairedLdSt(const MachineInstr &MI) {
503 switch (MI.getOpcode()) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000504 default:
505 return false;
506 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +0000507 case AArch64::LDPSWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000508 case AArch64::LDPDi:
509 case AArch64::LDPQi:
510 case AArch64::LDPWi:
511 case AArch64::LDPXi:
512 case AArch64::STPSi:
513 case AArch64::STPDi:
514 case AArch64::STPQi:
515 case AArch64::STPWi:
516 case AArch64::STPXi:
517 return true;
518 }
519}
520
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000521static const MachineOperand &getLdStRegOp(const MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000522 unsigned PairedRegOp = 0) {
523 assert(PairedRegOp < 2 && "Unexpected register operand idx.");
524 unsigned Idx = isPairedLdSt(MI) ? PairedRegOp : 0;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000525 return MI.getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000526}
527
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000528static const MachineOperand &getLdStBaseOp(const MachineInstr &MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000529 unsigned Idx = isPairedLdSt(MI) ? 2 : 1;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000530 return MI.getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000531}
532
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000533static const MachineOperand &getLdStOffsetOp(const MachineInstr &MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000534 unsigned Idx = isPairedLdSt(MI) ? 3 : 2;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000535 return MI.getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000536}
537
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000538static bool isLdOffsetInRangeOfSt(MachineInstr &LoadInst,
539 MachineInstr &StoreInst,
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000540 const AArch64InstrInfo *TII) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000541 assert(isMatchingStore(LoadInst, StoreInst) && "Expect only matched ld/st.");
542 int LoadSize = getMemScale(LoadInst);
543 int StoreSize = getMemScale(StoreInst);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000544 int UnscaledStOffset = TII->isUnscaledLdSt(StoreInst)
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000545 ? getLdStOffsetOp(StoreInst).getImm()
546 : getLdStOffsetOp(StoreInst).getImm() * StoreSize;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000547 int UnscaledLdOffset = TII->isUnscaledLdSt(LoadInst)
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000548 ? getLdStOffsetOp(LoadInst).getImm()
549 : getLdStOffsetOp(LoadInst).getImm() * LoadSize;
550 return (UnscaledStOffset <= UnscaledLdOffset) &&
551 (UnscaledLdOffset + LoadSize <= (UnscaledStOffset + StoreSize));
552}
553
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000554static bool isPromotableZeroStoreInst(MachineInstr &MI) {
Chad Rosierd6daac42016-11-07 15:27:22 +0000555 unsigned Opc = MI.getOpcode();
556 return (Opc == AArch64::STRWui || Opc == AArch64::STURWi ||
557 isNarrowStore(Opc)) &&
Jun Bum Lim397eb7b2016-02-12 15:25:39 +0000558 getLdStRegOp(MI).getReg() == AArch64::WZR;
559}
560
Tim Northover3b0846e2014-05-24 12:50:23 +0000561MachineBasicBlock::iterator
Chad Rosierd6daac42016-11-07 15:27:22 +0000562AArch64LoadStoreOpt::mergeNarrowZeroStores(MachineBasicBlock::iterator I,
563 MachineBasicBlock::iterator MergeMI,
564 const LdStPairFlags &Flags) {
565 assert(isPromotableZeroStoreInst(*I) && isPromotableZeroStoreInst(*MergeMI) &&
566 "Expected promotable zero stores.");
567
Tim Northover3b0846e2014-05-24 12:50:23 +0000568 MachineBasicBlock::iterator NextI = I;
569 ++NextI;
570 // If NextI is the second of the two instructions to be merged, we need
571 // to skip one further. Either way we merge will invalidate the iterator,
572 // and we don't need to scan the new instruction, as it's a pairwise
573 // instruction, which we're not considering for further action anyway.
Chad Rosierd7363db2016-02-09 19:09:22 +0000574 if (NextI == MergeMI)
Tim Northover3b0846e2014-05-24 12:50:23 +0000575 ++NextI;
576
Chad Rosierb5933d72016-02-09 19:02:12 +0000577 unsigned Opc = I->getOpcode();
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000578 bool IsScaled = !TII->isUnscaledLdSt(Opc);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000579 int OffsetStride = IsScaled ? 1 : getMemScale(*I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000580
Chad Rosier96a18a92015-07-21 17:42:04 +0000581 bool MergeForward = Flags.getMergeForward();
Tim Northover3b0846e2014-05-24 12:50:23 +0000582 // Insert our new paired instruction after whichever of the paired
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000583 // instructions MergeForward indicates.
Chad Rosierd7363db2016-02-09 19:09:22 +0000584 MachineBasicBlock::iterator InsertionPoint = MergeForward ? MergeMI : I;
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000585 // Also based on MergeForward is from where we copy the base register operand
Tim Northover3b0846e2014-05-24 12:50:23 +0000586 // so we get the flags compatible with the input code.
Chad Rosierf77e9092015-08-06 15:50:12 +0000587 const MachineOperand &BaseRegOp =
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000588 MergeForward ? getLdStBaseOp(*MergeMI) : getLdStBaseOp(*I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000589
590 // Which register is Rt and which is Rt2 depends on the offset order.
Davide Italiano5df60662016-11-07 19:11:25 +0000591 MachineInstr *RtMI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000592 if (getLdStOffsetOp(*I).getImm() ==
Davide Italiano5df60662016-11-07 19:11:25 +0000593 getLdStOffsetOp(*MergeMI).getImm() + OffsetStride)
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000594 RtMI = &*MergeMI;
Davide Italiano5df60662016-11-07 19:11:25 +0000595 else
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000596 RtMI = &*I;
Jun Bum Limc9879ec2015-10-27 19:16:03 +0000597
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000598 int OffsetImm = getLdStOffsetOp(*RtMI).getImm();
Chad Rosier11eedc92016-02-09 19:17:18 +0000599 // Change the scaled offset from small to large type.
600 if (IsScaled) {
601 assert(((OffsetImm & 1) == 0) && "Unexpected offset to merge");
602 OffsetImm /= 2;
603 }
604
Chad Rosierd6daac42016-11-07 15:27:22 +0000605 // Construct the new instruction.
Chad Rosierc46ef882016-02-09 19:33:42 +0000606 DebugLoc DL = I->getDebugLoc();
607 MachineBasicBlock *MBB = I->getParent();
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000608 MachineInstrBuilder MIB;
Chad Rosierc46ef882016-02-09 19:33:42 +0000609 MIB = BuildMI(*MBB, InsertionPoint, DL, TII->get(getMatchingWideOpcode(Opc)))
Jun Bum Lim397eb7b2016-02-12 15:25:39 +0000610 .addReg(isNarrowStore(Opc) ? AArch64::WZR : AArch64::XZR)
Diana Picus116bbab2017-01-13 09:58:52 +0000611 .add(BaseRegOp)
Chad Rosierb5933d72016-02-09 19:02:12 +0000612 .addImm(OffsetImm)
Chad Rosierd7363db2016-02-09 19:09:22 +0000613 .setMemRefs(I->mergeMemRefsWith(*MergeMI));
Tim Northover3b0846e2014-05-24 12:50:23 +0000614 (void)MIB;
615
Chad Rosierd6daac42016-11-07 15:27:22 +0000616 DEBUG(dbgs() << "Creating wider store. Replacing instructions:\n ");
Chad Rosierb5933d72016-02-09 19:02:12 +0000617 DEBUG(I->print(dbgs()));
618 DEBUG(dbgs() << " ");
Chad Rosierd7363db2016-02-09 19:09:22 +0000619 DEBUG(MergeMI->print(dbgs()));
Chad Rosierb5933d72016-02-09 19:02:12 +0000620 DEBUG(dbgs() << " with instruction:\n ");
621 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
622 DEBUG(dbgs() << "\n");
623
624 // Erase the old instructions.
625 I->eraseFromParent();
Chad Rosierd7363db2016-02-09 19:09:22 +0000626 MergeMI->eraseFromParent();
Chad Rosierb5933d72016-02-09 19:02:12 +0000627 return NextI;
628}
629
630MachineBasicBlock::iterator
631AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
632 MachineBasicBlock::iterator Paired,
633 const LdStPairFlags &Flags) {
634 MachineBasicBlock::iterator NextI = I;
635 ++NextI;
636 // If NextI is the second of the two instructions to be merged, we need
637 // to skip one further. Either way we merge will invalidate the iterator,
638 // and we don't need to scan the new instruction, as it's a pairwise
639 // instruction, which we're not considering for further action anyway.
640 if (NextI == Paired)
641 ++NextI;
642
643 int SExtIdx = Flags.getSExtIdx();
644 unsigned Opc =
645 SExtIdx == -1 ? I->getOpcode() : getMatchingNonSExtOpcode(I->getOpcode());
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000646 bool IsUnscaled = TII->isUnscaledLdSt(Opc);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000647 int OffsetStride = IsUnscaled ? getMemScale(*I) : 1;
Chad Rosierb5933d72016-02-09 19:02:12 +0000648
649 bool MergeForward = Flags.getMergeForward();
650 // Insert our new paired instruction after whichever of the paired
651 // instructions MergeForward indicates.
652 MachineBasicBlock::iterator InsertionPoint = MergeForward ? Paired : I;
653 // Also based on MergeForward is from where we copy the base register operand
654 // so we get the flags compatible with the input code.
655 const MachineOperand &BaseRegOp =
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000656 MergeForward ? getLdStBaseOp(*Paired) : getLdStBaseOp(*I);
Chad Rosierb5933d72016-02-09 19:02:12 +0000657
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000658 int Offset = getLdStOffsetOp(*I).getImm();
659 int PairedOffset = getLdStOffsetOp(*Paired).getImm();
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000660 bool PairedIsUnscaled = TII->isUnscaledLdSt(Paired->getOpcode());
Chad Rosier00f9d232016-02-11 14:25:08 +0000661 if (IsUnscaled != PairedIsUnscaled) {
662 // We're trying to pair instructions that differ in how they are scaled. If
663 // I is scaled then scale the offset of Paired accordingly. Otherwise, do
664 // the opposite (i.e., make Paired's offset unscaled).
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000665 int MemSize = getMemScale(*Paired);
Chad Rosier00f9d232016-02-11 14:25:08 +0000666 if (PairedIsUnscaled) {
667 // If the unscaled offset isn't a multiple of the MemSize, we can't
668 // pair the operations together.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000669 assert(!(PairedOffset % getMemScale(*Paired)) &&
Chad Rosier00f9d232016-02-11 14:25:08 +0000670 "Offset should be a multiple of the stride!");
671 PairedOffset /= MemSize;
672 } else {
673 PairedOffset *= MemSize;
674 }
675 }
676
Chad Rosierb5933d72016-02-09 19:02:12 +0000677 // Which register is Rt and which is Rt2 depends on the offset order.
678 MachineInstr *RtMI, *Rt2MI;
Chad Rosier00f9d232016-02-11 14:25:08 +0000679 if (Offset == PairedOffset + OffsetStride) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000680 RtMI = &*Paired;
681 Rt2MI = &*I;
Chad Rosierb5933d72016-02-09 19:02:12 +0000682 // Here we swapped the assumption made for SExtIdx.
683 // I.e., we turn ldp I, Paired into ldp Paired, I.
684 // Update the index accordingly.
685 if (SExtIdx != -1)
686 SExtIdx = (SExtIdx + 1) % 2;
687 } else {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000688 RtMI = &*I;
689 Rt2MI = &*Paired;
Chad Rosierb5933d72016-02-09 19:02:12 +0000690 }
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000691 int OffsetImm = getLdStOffsetOp(*RtMI).getImm();
Chad Rosier00f9d232016-02-11 14:25:08 +0000692 // Scale the immediate offset, if necessary.
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000693 if (TII->isUnscaledLdSt(RtMI->getOpcode())) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000694 assert(!(OffsetImm % getMemScale(*RtMI)) &&
Chad Rosier00f9d232016-02-11 14:25:08 +0000695 "Unscaled offset cannot be scaled.");
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000696 OffsetImm /= getMemScale(*RtMI);
Chad Rosier87e33412016-02-09 20:18:07 +0000697 }
Chad Rosierb5933d72016-02-09 19:02:12 +0000698
699 // Construct the new instruction.
700 MachineInstrBuilder MIB;
Chad Rosierc46ef882016-02-09 19:33:42 +0000701 DebugLoc DL = I->getDebugLoc();
702 MachineBasicBlock *MBB = I->getParent();
Matthias Braun2e8c11e2017-01-20 18:04:27 +0000703 MachineOperand RegOp0 = getLdStRegOp(*RtMI);
704 MachineOperand RegOp1 = getLdStRegOp(*Rt2MI);
705 // Kill flags may become invalid when moving stores for pairing.
706 if (RegOp0.isUse()) {
707 if (!MergeForward) {
708 // Clear kill flags on store if moving upwards. Example:
709 // STRWui %w0, ...
710 // USE %w1
711 // STRWui kill %w1 ; need to clear kill flag when moving STRWui upwards
712 RegOp0.setIsKill(false);
713 RegOp1.setIsKill(false);
714 } else {
715 // Clear kill flags of the first stores register. Example:
716 // STRWui %w1, ...
717 // USE kill %w1 ; need to clear kill flag when moving STRWui downwards
718 // STRW %w0
719 unsigned Reg = getLdStRegOp(*I).getReg();
720 for (MachineInstr &MI : make_range(std::next(I), Paired))
721 MI.clearRegisterKills(Reg, TRI);
722 }
723 }
Chad Rosierc46ef882016-02-09 19:33:42 +0000724 MIB = BuildMI(*MBB, InsertionPoint, DL, TII->get(getMatchingPairOpcode(Opc)))
Matthias Braun2e8c11e2017-01-20 18:04:27 +0000725 .add(RegOp0)
726 .add(RegOp1)
Diana Picus116bbab2017-01-13 09:58:52 +0000727 .add(BaseRegOp)
Chad Rosiere40b9512016-03-08 17:16:38 +0000728 .addImm(OffsetImm)
729 .setMemRefs(I->mergeMemRefsWith(*Paired));
Chad Rosierb5933d72016-02-09 19:02:12 +0000730
731 (void)MIB;
Tim Northover3b0846e2014-05-24 12:50:23 +0000732
733 DEBUG(dbgs() << "Creating pair load/store. Replacing instructions:\n ");
734 DEBUG(I->print(dbgs()));
735 DEBUG(dbgs() << " ");
736 DEBUG(Paired->print(dbgs()));
737 DEBUG(dbgs() << " with instruction:\n ");
Quentin Colombet66b61632015-03-06 22:42:10 +0000738 if (SExtIdx != -1) {
739 // Generate the sign extension for the proper result of the ldp.
740 // I.e., with X1, that would be:
741 // %W1<def> = KILL %W1, %X1<imp-def>
742 // %X1<def> = SBFMXri %X1<kill>, 0, 31
743 MachineOperand &DstMO = MIB->getOperand(SExtIdx);
744 // Right now, DstMO has the extended register, since it comes from an
745 // extended opcode.
746 unsigned DstRegX = DstMO.getReg();
747 // Get the W variant of that register.
748 unsigned DstRegW = TRI->getSubReg(DstRegX, AArch64::sub_32);
749 // Update the result of LDP to use the W instead of the X variant.
750 DstMO.setReg(DstRegW);
751 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
752 DEBUG(dbgs() << "\n");
753 // Make the machine verifier happy by providing a definition for
754 // the X register.
755 // Insert this definition right after the generated LDP, i.e., before
756 // InsertionPoint.
757 MachineInstrBuilder MIBKill =
Chad Rosierc46ef882016-02-09 19:33:42 +0000758 BuildMI(*MBB, InsertionPoint, DL, TII->get(TargetOpcode::KILL), DstRegW)
Quentin Colombet66b61632015-03-06 22:42:10 +0000759 .addReg(DstRegW)
760 .addReg(DstRegX, RegState::Define);
761 MIBKill->getOperand(2).setImplicit();
762 // Create the sign extension.
763 MachineInstrBuilder MIBSXTW =
Chad Rosierc46ef882016-02-09 19:33:42 +0000764 BuildMI(*MBB, InsertionPoint, DL, TII->get(AArch64::SBFMXri), DstRegX)
Quentin Colombet66b61632015-03-06 22:42:10 +0000765 .addReg(DstRegX)
766 .addImm(0)
767 .addImm(31);
768 (void)MIBSXTW;
769 DEBUG(dbgs() << " Extend operand:\n ");
770 DEBUG(((MachineInstr *)MIBSXTW)->print(dbgs()));
Quentin Colombet66b61632015-03-06 22:42:10 +0000771 } else {
772 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
Quentin Colombet66b61632015-03-06 22:42:10 +0000773 }
Chad Rosier1c44c5982016-02-09 20:27:45 +0000774 DEBUG(dbgs() << "\n");
Tim Northover3b0846e2014-05-24 12:50:23 +0000775
776 // Erase the old instructions.
777 I->eraseFromParent();
778 Paired->eraseFromParent();
779
780 return NextI;
781}
782
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000783MachineBasicBlock::iterator
784AArch64LoadStoreOpt::promoteLoadFromStore(MachineBasicBlock::iterator LoadI,
785 MachineBasicBlock::iterator StoreI) {
786 MachineBasicBlock::iterator NextI = LoadI;
787 ++NextI;
788
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000789 int LoadSize = getMemScale(*LoadI);
790 int StoreSize = getMemScale(*StoreI);
791 unsigned LdRt = getLdStRegOp(*LoadI).getReg();
792 unsigned StRt = getLdStRegOp(*StoreI).getReg();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000793 bool IsStoreXReg = TRI->getRegClass(AArch64::GPR64RegClassID)->contains(StRt);
794
795 assert((IsStoreXReg ||
796 TRI->getRegClass(AArch64::GPR32RegClassID)->contains(StRt)) &&
797 "Unexpected RegClass");
798
799 MachineInstr *BitExtMI;
800 if (LoadSize == StoreSize && (LoadSize == 4 || LoadSize == 8)) {
801 // Remove the load, if the destination register of the loads is the same
802 // register for stored value.
803 if (StRt == LdRt && LoadSize == 8) {
Matthias Braun76bb4132016-12-16 23:55:43 +0000804 StoreI->clearRegisterKills(StRt, TRI);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000805 DEBUG(dbgs() << "Remove load instruction:\n ");
806 DEBUG(LoadI->print(dbgs()));
807 DEBUG(dbgs() << "\n");
808 LoadI->eraseFromParent();
809 return NextI;
810 }
811 // Replace the load with a mov if the load and store are in the same size.
812 BitExtMI =
813 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(),
814 TII->get(IsStoreXReg ? AArch64::ORRXrs : AArch64::ORRWrs), LdRt)
815 .addReg(IsStoreXReg ? AArch64::XZR : AArch64::WZR)
816 .addReg(StRt)
817 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
818 } else {
819 // FIXME: Currently we disable this transformation in big-endian targets as
820 // performance and correctness are verified only in little-endian.
821 if (!Subtarget->isLittleEndian())
822 return NextI;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000823 bool IsUnscaled = TII->isUnscaledLdSt(*LoadI);
824 assert(IsUnscaled == TII->isUnscaledLdSt(*StoreI) &&
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000825 "Unsupported ld/st match");
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000826 assert(LoadSize <= StoreSize && "Invalid load size");
827 int UnscaledLdOffset = IsUnscaled
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000828 ? getLdStOffsetOp(*LoadI).getImm()
829 : getLdStOffsetOp(*LoadI).getImm() * LoadSize;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000830 int UnscaledStOffset = IsUnscaled
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000831 ? getLdStOffsetOp(*StoreI).getImm()
832 : getLdStOffsetOp(*StoreI).getImm() * StoreSize;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000833 int Width = LoadSize * 8;
834 int Immr = 8 * (UnscaledLdOffset - UnscaledStOffset);
835 int Imms = Immr + Width - 1;
836 unsigned DestReg = IsStoreXReg
837 ? TRI->getMatchingSuperReg(LdRt, AArch64::sub_32,
838 &AArch64::GPR64RegClass)
839 : LdRt;
840
841 assert((UnscaledLdOffset >= UnscaledStOffset &&
842 (UnscaledLdOffset + LoadSize) <= UnscaledStOffset + StoreSize) &&
843 "Invalid offset");
844
845 Immr = 8 * (UnscaledLdOffset - UnscaledStOffset);
846 Imms = Immr + Width - 1;
847 if (UnscaledLdOffset == UnscaledStOffset) {
848 uint32_t AndMaskEncoded = ((IsStoreXReg ? 1 : 0) << 12) // N
849 | ((Immr) << 6) // immr
850 | ((Imms) << 0) // imms
851 ;
852
853 BitExtMI =
854 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(),
855 TII->get(IsStoreXReg ? AArch64::ANDXri : AArch64::ANDWri),
856 DestReg)
857 .addReg(StRt)
858 .addImm(AndMaskEncoded);
859 } else {
860 BitExtMI =
861 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(),
862 TII->get(IsStoreXReg ? AArch64::UBFMXri : AArch64::UBFMWri),
863 DestReg)
864 .addReg(StRt)
865 .addImm(Immr)
866 .addImm(Imms);
867 }
868 }
Matthias Braun76bb4132016-12-16 23:55:43 +0000869 StoreI->clearRegisterKills(StRt, TRI);
870
Chad Rosierf7ac5f22016-03-30 18:08:51 +0000871 (void)BitExtMI;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000872
873 DEBUG(dbgs() << "Promoting load by replacing :\n ");
874 DEBUG(StoreI->print(dbgs()));
875 DEBUG(dbgs() << " ");
876 DEBUG(LoadI->print(dbgs()));
877 DEBUG(dbgs() << " with instructions:\n ");
878 DEBUG(StoreI->print(dbgs()));
879 DEBUG(dbgs() << " ");
880 DEBUG((BitExtMI)->print(dbgs()));
881 DEBUG(dbgs() << "\n");
882
883 // Erase the old instructions.
884 LoadI->eraseFromParent();
885 return NextI;
886}
887
Tim Northover3b0846e2014-05-24 12:50:23 +0000888/// trackRegDefsUses - Remember what registers the specified instruction uses
889/// and modifies.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000890static void trackRegDefsUses(const MachineInstr &MI, BitVector &ModifiedRegs,
Tim Northover3b0846e2014-05-24 12:50:23 +0000891 BitVector &UsedRegs,
892 const TargetRegisterInfo *TRI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000893 for (const MachineOperand &MO : MI.operands()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000894 if (MO.isRegMask())
895 ModifiedRegs.setBitsNotInMask(MO.getRegMask());
896
897 if (!MO.isReg())
898 continue;
899 unsigned Reg = MO.getReg();
Geoff Berry173b14d2016-02-09 20:47:21 +0000900 if (!Reg)
901 continue;
Tim Northover3b0846e2014-05-24 12:50:23 +0000902 if (MO.isDef()) {
Geoff Berrye0bf52f2016-11-21 22:51:10 +0000903 // WZR/XZR are not modified even when used as a destination register.
904 if (Reg != AArch64::WZR && Reg != AArch64::XZR)
905 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
906 ModifiedRegs.set(*AI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000907 } else {
908 assert(MO.isUse() && "Reg operand not a def and not a use?!?");
909 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
910 UsedRegs.set(*AI);
911 }
912 }
913}
914
915static bool inBoundsForPair(bool IsUnscaled, int Offset, int OffsetStride) {
Chad Rosier3dd0e942015-08-18 16:20:03 +0000916 // Convert the byte-offset used by unscaled into an "element" offset used
917 // by the scaled pair load/store instructions.
Chad Rosier00f9d232016-02-11 14:25:08 +0000918 if (IsUnscaled) {
919 // If the byte-offset isn't a multiple of the stride, there's no point
920 // trying to match it.
921 if (Offset % OffsetStride)
922 return false;
Chad Rosier3dd0e942015-08-18 16:20:03 +0000923 Offset /= OffsetStride;
Chad Rosier00f9d232016-02-11 14:25:08 +0000924 }
Chad Rosier3dd0e942015-08-18 16:20:03 +0000925 return Offset <= 63 && Offset >= -64;
Tim Northover3b0846e2014-05-24 12:50:23 +0000926}
927
928// Do alignment, specialized to power of 2 and for signed ints,
929// avoiding having to do a C-style cast from uint_64t to int when
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000930// using alignTo from include/llvm/Support/MathExtras.h.
Tim Northover3b0846e2014-05-24 12:50:23 +0000931// FIXME: Move this function to include/MathExtras.h?
932static int alignTo(int Num, int PowOf2) {
933 return (Num + PowOf2 - 1) & ~(PowOf2 - 1);
934}
935
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000936static bool mayAlias(MachineInstr &MIa, MachineInstr &MIb,
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000937 const AArch64InstrInfo *TII) {
938 // One of the instructions must modify memory.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000939 if (!MIa.mayStore() && !MIb.mayStore())
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000940 return false;
941
942 // Both instructions must be memory operations.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000943 if (!MIa.mayLoadOrStore() && !MIb.mayLoadOrStore())
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000944 return false;
945
946 return !TII->areMemAccessesTriviallyDisjoint(MIa, MIb);
947}
948
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000949static bool mayAlias(MachineInstr &MIa,
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000950 SmallVectorImpl<MachineInstr *> &MemInsns,
951 const AArch64InstrInfo *TII) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000952 for (MachineInstr *MIb : MemInsns)
953 if (mayAlias(MIa, *MIb, TII))
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000954 return true;
955
956 return false;
957}
958
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000959bool AArch64LoadStoreOpt::findMatchingStore(
960 MachineBasicBlock::iterator I, unsigned Limit,
961 MachineBasicBlock::iterator &StoreI) {
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000962 MachineBasicBlock::iterator B = I->getParent()->begin();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000963 MachineBasicBlock::iterator MBBI = I;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000964 MachineInstr &LoadMI = *I;
Chad Rosier5c6a66c2016-02-09 15:59:57 +0000965 unsigned BaseReg = getLdStBaseOp(LoadMI).getReg();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000966
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000967 // If the load is the first instruction in the block, there's obviously
968 // not any matching store.
969 if (MBBI == B)
970 return false;
971
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000972 // Track which registers have been modified and used between the first insn
973 // and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +0000974 ModifiedRegs.reset();
975 UsedRegs.reset();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000976
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000977 unsigned Count = 0;
978 do {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000979 --MBBI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000980 MachineInstr &MI = *MBBI;
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000981
Geoff Berry4ff2e362016-07-21 15:20:25 +0000982 // Don't count transient instructions towards the search limit since there
983 // may be different numbers of them if e.g. debug information is present.
984 if (!MI.isTransient())
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000985 ++Count;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000986
987 // If the load instruction reads directly from the address to which the
988 // store instruction writes and the stored value is not modified, we can
989 // promote the load. Since we do not handle stores with pre-/post-index,
990 // it's unnecessary to check if BaseReg is modified by the store itself.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000991 if (MI.mayStore() && isMatchingStore(LoadMI, MI) &&
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000992 BaseReg == getLdStBaseOp(MI).getReg() &&
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000993 isLdOffsetInRangeOfSt(LoadMI, MI, TII) &&
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000994 !ModifiedRegs[getLdStRegOp(MI).getReg()]) {
995 StoreI = MBBI;
996 return true;
997 }
998
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000999 if (MI.isCall())
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001000 return false;
1001
1002 // Update modified / uses register lists.
1003 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1004
1005 // Otherwise, if the base register is modified, we have no match, so
1006 // return early.
1007 if (ModifiedRegs[BaseReg])
1008 return false;
1009
1010 // If we encounter a store aliased with the load, return early.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001011 if (MI.mayStore() && mayAlias(LoadMI, MI, TII))
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001012 return false;
Jun Bum Lim633b2d82016-02-11 16:18:24 +00001013 } while (MBBI != B && Count < Limit);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001014 return false;
1015}
1016
Chad Rosierc5083c22016-06-10 20:47:14 +00001017// Returns true if FirstMI and MI are candidates for merging or pairing.
1018// Otherwise, returns false.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001019static bool areCandidatesToMergeOrPair(MachineInstr &FirstMI, MachineInstr &MI,
Chad Rosierc5083c22016-06-10 20:47:14 +00001020 LdStPairFlags &Flags,
1021 const AArch64InstrInfo *TII) {
1022 // If this is volatile or if pairing is suppressed, not a candidate.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001023 if (MI.hasOrderedMemoryRef() || TII->isLdStPairSuppressed(MI))
Chad Rosierc5083c22016-06-10 20:47:14 +00001024 return false;
1025
1026 // We should have already checked FirstMI for pair suppression and volatility.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001027 assert(!FirstMI.hasOrderedMemoryRef() &&
1028 !TII->isLdStPairSuppressed(FirstMI) &&
Chad Rosierc5083c22016-06-10 20:47:14 +00001029 "FirstMI shouldn't get here if either of these checks are true.");
1030
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001031 unsigned OpcA = FirstMI.getOpcode();
1032 unsigned OpcB = MI.getOpcode();
Chad Rosierc5083c22016-06-10 20:47:14 +00001033
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001034 // Opcodes match: nothing more to check.
1035 if (OpcA == OpcB)
1036 return true;
1037
1038 // Try to match a sign-extended load/store with a zero-extended load/store.
1039 bool IsValidLdStrOpc, PairIsValidLdStrOpc;
1040 unsigned NonSExtOpc = getMatchingNonSExtOpcode(OpcA, &IsValidLdStrOpc);
1041 assert(IsValidLdStrOpc &&
1042 "Given Opc should be a Load or Store with an immediate");
1043 // OpcA will be the first instruction in the pair.
1044 if (NonSExtOpc == getMatchingNonSExtOpcode(OpcB, &PairIsValidLdStrOpc)) {
1045 Flags.setSExtIdx(NonSExtOpc == (unsigned)OpcA ? 1 : 0);
1046 return true;
1047 }
Chad Rosier00f9d232016-02-11 14:25:08 +00001048
Chad Rosierd6daac42016-11-07 15:27:22 +00001049 // If the second instruction isn't even a mergable/pairable load/store, bail
1050 // out.
Chad Rosier00f9d232016-02-11 14:25:08 +00001051 if (!PairIsValidLdStrOpc)
1052 return false;
1053
Chad Rosierd6daac42016-11-07 15:27:22 +00001054 // FIXME: We don't support merging narrow stores with mixed scaled/unscaled
1055 // offsets.
1056 if (isNarrowStore(OpcA) || isNarrowStore(OpcB))
Chad Rosier00f9d232016-02-11 14:25:08 +00001057 return false;
1058
1059 // Try to match an unscaled load/store with a scaled load/store.
Chad Rosiere4e15ba2016-03-09 17:29:48 +00001060 return TII->isUnscaledLdSt(OpcA) != TII->isUnscaledLdSt(OpcB) &&
Chad Rosier00f9d232016-02-11 14:25:08 +00001061 getMatchingPairOpcode(OpcA) == getMatchingPairOpcode(OpcB);
1062
1063 // FIXME: Can we also match a mixed sext/zext unscaled/scaled pair?
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001064}
1065
Chad Rosier9f4ec2e2016-02-10 18:49:28 +00001066/// Scan the instructions looking for a load/store that can be combined with the
1067/// current instruction into a wider equivalent or a load/store pair.
Tim Northover3b0846e2014-05-24 12:50:23 +00001068MachineBasicBlock::iterator
1069AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
Jun Bum Limcf974432016-03-31 14:47:24 +00001070 LdStPairFlags &Flags, unsigned Limit,
1071 bool FindNarrowMerge) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001072 MachineBasicBlock::iterator E = I->getParent()->end();
1073 MachineBasicBlock::iterator MBBI = I;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001074 MachineInstr &FirstMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001075 ++MBBI;
1076
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001077 bool MayLoad = FirstMI.mayLoad();
1078 bool IsUnscaled = TII->isUnscaledLdSt(FirstMI);
Chad Rosierf77e9092015-08-06 15:50:12 +00001079 unsigned Reg = getLdStRegOp(FirstMI).getReg();
1080 unsigned BaseReg = getLdStBaseOp(FirstMI).getReg();
1081 int Offset = getLdStOffsetOp(FirstMI).getImm();
Chad Rosierf11d0402015-10-01 18:17:12 +00001082 int OffsetStride = IsUnscaled ? getMemScale(FirstMI) : 1;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +00001083 bool IsPromotableZeroStore = isPromotableZeroStoreInst(FirstMI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001084
1085 // Track which registers have been modified and used between the first insn
1086 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001087 ModifiedRegs.reset();
1088 UsedRegs.reset();
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001089
1090 // Remember any instructions that read/write memory between FirstMI and MI.
1091 SmallVector<MachineInstr *, 4> MemInsns;
1092
Tim Northover3b0846e2014-05-24 12:50:23 +00001093 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001094 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001095
Geoff Berry4ff2e362016-07-21 15:20:25 +00001096 // Don't count transient instructions towards the search limit since there
1097 // may be different numbers of them if e.g. debug information is present.
1098 if (!MI.isTransient())
1099 ++Count;
Tim Northover3b0846e2014-05-24 12:50:23 +00001100
Chad Rosier18896c02016-02-04 16:01:40 +00001101 Flags.setSExtIdx(-1);
Chad Rosierc5083c22016-06-10 20:47:14 +00001102 if (areCandidatesToMergeOrPair(FirstMI, MI, Flags, TII) &&
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001103 getLdStOffsetOp(MI).isImm()) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001104 assert(MI.mayLoadOrStore() && "Expected memory operation.");
Tim Northover3b0846e2014-05-24 12:50:23 +00001105 // If we've found another instruction with the same opcode, check to see
1106 // if the base and offset are compatible with our starting instruction.
1107 // These instructions all have scaled immediate operands, so we just
1108 // check for +1/-1. Make sure to check the new instruction offset is
1109 // actually an immediate and not a symbolic reference destined for
1110 // a relocation.
Chad Rosierf77e9092015-08-06 15:50:12 +00001111 unsigned MIBaseReg = getLdStBaseOp(MI).getReg();
1112 int MIOffset = getLdStOffsetOp(MI).getImm();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001113 bool MIIsUnscaled = TII->isUnscaledLdSt(MI);
Chad Rosier00f9d232016-02-11 14:25:08 +00001114 if (IsUnscaled != MIIsUnscaled) {
1115 // We're trying to pair instructions that differ in how they are scaled.
1116 // If FirstMI is scaled then scale the offset of MI accordingly.
1117 // Otherwise, do the opposite (i.e., make MI's offset unscaled).
1118 int MemSize = getMemScale(MI);
1119 if (MIIsUnscaled) {
1120 // If the unscaled offset isn't a multiple of the MemSize, we can't
1121 // pair the operations together: bail and keep looking.
Eli Friedmanf184e4b2016-08-12 20:39:51 +00001122 if (MIOffset % MemSize) {
1123 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1124 MemInsns.push_back(&MI);
Chad Rosier00f9d232016-02-11 14:25:08 +00001125 continue;
Eli Friedmanf184e4b2016-08-12 20:39:51 +00001126 }
Chad Rosier00f9d232016-02-11 14:25:08 +00001127 MIOffset /= MemSize;
1128 } else {
1129 MIOffset *= MemSize;
1130 }
1131 }
1132
Tim Northover3b0846e2014-05-24 12:50:23 +00001133 if (BaseReg == MIBaseReg && ((Offset == MIOffset + OffsetStride) ||
1134 (Offset + OffsetStride == MIOffset))) {
1135 int MinOffset = Offset < MIOffset ? Offset : MIOffset;
Jun Bum Limcf974432016-03-31 14:47:24 +00001136 if (FindNarrowMerge) {
Jun Bum Lim80ec0d32015-11-20 21:14:07 +00001137 // If the alignment requirements of the scaled wide load/store
Jun Bum Limcf974432016-03-31 14:47:24 +00001138 // instruction can't express the offset of the scaled narrow input,
1139 // bail and keep looking. For promotable zero stores, allow only when
1140 // the stored value is the same (i.e., WZR).
1141 if ((!IsUnscaled && alignTo(MinOffset, 2) != MinOffset) ||
1142 (IsPromotableZeroStore && Reg != getLdStRegOp(MI).getReg())) {
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001143 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001144 MemInsns.push_back(&MI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001145 continue;
1146 }
1147 } else {
Chad Rosierd1f6c842016-06-10 20:49:18 +00001148 // Pairwise instructions have a 7-bit signed offset field. Single
1149 // insns have a 12-bit unsigned offset field. If the resultant
1150 // immediate offset of merging these instructions is out of range for
1151 // a pairwise instruction, bail and keep looking.
Jun Bum Limcf974432016-03-31 14:47:24 +00001152 if (!inBoundsForPair(IsUnscaled, MinOffset, OffsetStride)) {
1153 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001154 MemInsns.push_back(&MI);
Jun Bum Limcf974432016-03-31 14:47:24 +00001155 continue;
1156 }
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001157 // If the alignment requirements of the paired (scaled) instruction
1158 // can't express the offset of the unscaled input, bail and keep
1159 // looking.
1160 if (IsUnscaled && (alignTo(MinOffset, OffsetStride) != MinOffset)) {
1161 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001162 MemInsns.push_back(&MI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001163 continue;
1164 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001165 }
1166 // If the destination register of the loads is the same register, bail
1167 // and keep looking. A load-pair instruction with both destination
1168 // registers the same is UNPREDICTABLE and will result in an exception.
Jun Bum Limcf974432016-03-31 14:47:24 +00001169 if (MayLoad && Reg == getLdStRegOp(MI).getReg()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001170 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001171 MemInsns.push_back(&MI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001172 continue;
1173 }
1174
1175 // If the Rt of the second instruction was not modified or used between
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001176 // the two instructions and none of the instructions between the second
1177 // and first alias with the second, we can combine the second into the
1178 // first.
Chad Rosierf77e9092015-08-06 15:50:12 +00001179 if (!ModifiedRegs[getLdStRegOp(MI).getReg()] &&
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001180 !(MI.mayLoad() && UsedRegs[getLdStRegOp(MI).getReg()]) &&
1181 !mayAlias(MI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +00001182 Flags.setMergeForward(false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001183 return MBBI;
1184 }
1185
1186 // Likewise, if the Rt of the first instruction is not modified or used
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001187 // between the two instructions and none of the instructions between the
1188 // first and the second alias with the first, we can combine the first
1189 // into the second.
Chad Rosierf77e9092015-08-06 15:50:12 +00001190 if (!ModifiedRegs[getLdStRegOp(FirstMI).getReg()] &&
Chad Rosier5f668e12015-09-03 14:19:43 +00001191 !(MayLoad && UsedRegs[getLdStRegOp(FirstMI).getReg()]) &&
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001192 !mayAlias(FirstMI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +00001193 Flags.setMergeForward(true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001194 return MBBI;
1195 }
1196 // Unable to combine these instructions due to interference in between.
1197 // Keep looking.
1198 }
1199 }
1200
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001201 // If the instruction wasn't a matching load or store. Stop searching if we
1202 // encounter a call instruction that might modify memory.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001203 if (MI.isCall())
Tim Northover3b0846e2014-05-24 12:50:23 +00001204 return E;
1205
1206 // Update modified / uses register lists.
1207 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1208
1209 // Otherwise, if the base register is modified, we have no match, so
1210 // return early.
1211 if (ModifiedRegs[BaseReg])
1212 return E;
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001213
1214 // Update list of instructions that read/write memory.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001215 if (MI.mayLoadOrStore())
1216 MemInsns.push_back(&MI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001217 }
1218 return E;
1219}
1220
1221MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +00001222AArch64LoadStoreOpt::mergeUpdateInsn(MachineBasicBlock::iterator I,
1223 MachineBasicBlock::iterator Update,
1224 bool IsPreIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001225 assert((Update->getOpcode() == AArch64::ADDXri ||
1226 Update->getOpcode() == AArch64::SUBXri) &&
1227 "Unexpected base register update instruction to merge!");
1228 MachineBasicBlock::iterator NextI = I;
1229 // Return the instruction following the merged instruction, which is
1230 // the instruction following our unmerged load. Unless that's the add/sub
1231 // instruction we're merging, in which case it's the one after that.
1232 if (++NextI == Update)
1233 ++NextI;
1234
1235 int Value = Update->getOperand(2).getImm();
1236 assert(AArch64_AM::getShiftValue(Update->getOperand(3).getImm()) == 0 &&
Chad Rosier2dfd3542015-09-23 13:51:44 +00001237 "Can't merge 1 << 12 offset into pre-/post-indexed load / store");
Tim Northover3b0846e2014-05-24 12:50:23 +00001238 if (Update->getOpcode() == AArch64::SUBXri)
1239 Value = -Value;
1240
Chad Rosier2dfd3542015-09-23 13:51:44 +00001241 unsigned NewOpc = IsPreIdx ? getPreIndexedOpcode(I->getOpcode())
1242 : getPostIndexedOpcode(I->getOpcode());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001243 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001244 if (!isPairedLdSt(*I)) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001245 // Non-paired instruction.
1246 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001247 .add(getLdStRegOp(*Update))
1248 .add(getLdStRegOp(*I))
1249 .add(getLdStBaseOp(*I))
Chad Rosier3ada75f2016-01-28 15:38:24 +00001250 .addImm(Value)
1251 .setMemRefs(I->memoperands_begin(), I->memoperands_end());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001252 } else {
1253 // Paired instruction.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001254 int Scale = getMemScale(*I);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001255 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001256 .add(getLdStRegOp(*Update))
1257 .add(getLdStRegOp(*I, 0))
1258 .add(getLdStRegOp(*I, 1))
1259 .add(getLdStBaseOp(*I))
Chad Rosier3ada75f2016-01-28 15:38:24 +00001260 .addImm(Value / Scale)
1261 .setMemRefs(I->memoperands_begin(), I->memoperands_end());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001262 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001263 (void)MIB;
1264
Chad Rosier2dfd3542015-09-23 13:51:44 +00001265 if (IsPreIdx)
1266 DEBUG(dbgs() << "Creating pre-indexed load/store.");
1267 else
1268 DEBUG(dbgs() << "Creating post-indexed load/store.");
Tim Northover3b0846e2014-05-24 12:50:23 +00001269 DEBUG(dbgs() << " Replacing instructions:\n ");
1270 DEBUG(I->print(dbgs()));
1271 DEBUG(dbgs() << " ");
1272 DEBUG(Update->print(dbgs()));
1273 DEBUG(dbgs() << " with instruction:\n ");
1274 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
1275 DEBUG(dbgs() << "\n");
1276
1277 // Erase the old instructions for the block.
1278 I->eraseFromParent();
1279 Update->eraseFromParent();
1280
1281 return NextI;
1282}
1283
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001284bool AArch64LoadStoreOpt::isMatchingUpdateInsn(MachineInstr &MemMI,
1285 MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001286 unsigned BaseReg, int Offset) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001287 switch (MI.getOpcode()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001288 default:
1289 break;
1290 case AArch64::SUBXri:
Tim Northover3b0846e2014-05-24 12:50:23 +00001291 case AArch64::ADDXri:
1292 // Make sure it's a vanilla immediate operand, not a relocation or
1293 // anything else we can't handle.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001294 if (!MI.getOperand(2).isImm())
Tim Northover3b0846e2014-05-24 12:50:23 +00001295 break;
1296 // Watch out for 1 << 12 shifted value.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001297 if (AArch64_AM::getShiftValue(MI.getOperand(3).getImm()))
Tim Northover3b0846e2014-05-24 12:50:23 +00001298 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001299
1300 // The update instruction source and destination register must be the
1301 // same as the load/store base register.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001302 if (MI.getOperand(0).getReg() != BaseReg ||
1303 MI.getOperand(1).getReg() != BaseReg)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001304 break;
1305
1306 bool IsPairedInsn = isPairedLdSt(MemMI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001307 int UpdateOffset = MI.getOperand(2).getImm();
Eli Friedman8585e9d2016-08-12 20:28:02 +00001308 if (MI.getOpcode() == AArch64::SUBXri)
1309 UpdateOffset = -UpdateOffset;
1310
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001311 // For non-paired load/store instructions, the immediate must fit in a
1312 // signed 9-bit integer.
1313 if (!IsPairedInsn && (UpdateOffset > 255 || UpdateOffset < -256))
1314 break;
1315
1316 // For paired load/store instructions, the immediate must be a multiple of
1317 // the scaling factor. The scaled offset must also fit into a signed 7-bit
1318 // integer.
1319 if (IsPairedInsn) {
Chad Rosier32d4d372015-09-29 16:07:32 +00001320 int Scale = getMemScale(MemMI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001321 if (UpdateOffset % Scale != 0)
1322 break;
1323
1324 int ScaledOffset = UpdateOffset / Scale;
Eli Friedman8585e9d2016-08-12 20:28:02 +00001325 if (ScaledOffset > 63 || ScaledOffset < -64)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001326 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00001327 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001328
1329 // If we have a non-zero Offset, we check that it matches the amount
1330 // we're adding to the register.
Eli Friedman8585e9d2016-08-12 20:28:02 +00001331 if (!Offset || Offset == UpdateOffset)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001332 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001333 break;
1334 }
1335 return false;
1336}
1337
1338MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward(
Chad Rosier35706ad2016-02-04 21:26:02 +00001339 MachineBasicBlock::iterator I, int UnscaledOffset, unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001340 MachineBasicBlock::iterator E = I->getParent()->end();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001341 MachineInstr &MemMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001342 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001343
Chad Rosierf77e9092015-08-06 15:50:12 +00001344 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001345 int MIUnscaledOffset = getLdStOffsetOp(MemMI).getImm() * getMemScale(MemMI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001346
Chad Rosierb7c5b912015-10-01 13:43:05 +00001347 // Scan forward looking for post-index opportunities. Updating instructions
1348 // can't be formed if the memory instruction doesn't have the offset we're
1349 // looking for.
1350 if (MIUnscaledOffset != UnscaledOffset)
1351 return E;
1352
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001353 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +00001354 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001355 bool IsPairedInsn = isPairedLdSt(MemMI);
1356 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
1357 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
1358 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
1359 return E;
1360 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001361
Tim Northover3b0846e2014-05-24 12:50:23 +00001362 // Track which registers have been modified and used between the first insn
1363 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001364 ModifiedRegs.reset();
1365 UsedRegs.reset();
Tim Northover3b0846e2014-05-24 12:50:23 +00001366 ++MBBI;
Chad Rosier35706ad2016-02-04 21:26:02 +00001367 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001368 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001369
Geoff Berry4ff2e362016-07-21 15:20:25 +00001370 // Don't count transient instructions towards the search limit since there
1371 // may be different numbers of them if e.g. debug information is present.
1372 if (!MI.isTransient())
1373 ++Count;
Chad Rosier35706ad2016-02-04 21:26:02 +00001374
Tim Northover3b0846e2014-05-24 12:50:23 +00001375 // If we found a match, return it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001376 if (isMatchingUpdateInsn(*I, MI, BaseReg, UnscaledOffset))
Tim Northover3b0846e2014-05-24 12:50:23 +00001377 return MBBI;
1378
1379 // Update the status of what the instruction clobbered and used.
1380 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1381
1382 // Otherwise, if the base register is used or modified, we have no match, so
1383 // return early.
1384 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
1385 return E;
1386 }
1387 return E;
1388}
1389
1390MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnBackward(
Chad Rosier35706ad2016-02-04 21:26:02 +00001391 MachineBasicBlock::iterator I, unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001392 MachineBasicBlock::iterator B = I->getParent()->begin();
1393 MachineBasicBlock::iterator E = I->getParent()->end();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001394 MachineInstr &MemMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001395 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001396
Chad Rosierf77e9092015-08-06 15:50:12 +00001397 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
1398 int Offset = getLdStOffsetOp(MemMI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +00001399
1400 // If the load/store is the first instruction in the block, there's obviously
1401 // not any matching update. Ditto if the memory offset isn't zero.
1402 if (MBBI == B || Offset != 0)
1403 return E;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001404 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +00001405 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001406 bool IsPairedInsn = isPairedLdSt(MemMI);
1407 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
1408 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
1409 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
1410 return E;
1411 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001412
1413 // Track which registers have been modified and used between the first insn
1414 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001415 ModifiedRegs.reset();
1416 UsedRegs.reset();
Geoff Berry173b14d2016-02-09 20:47:21 +00001417 unsigned Count = 0;
1418 do {
1419 --MBBI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001420 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001421
Geoff Berry4ff2e362016-07-21 15:20:25 +00001422 // Don't count transient instructions towards the search limit since there
1423 // may be different numbers of them if e.g. debug information is present.
1424 if (!MI.isTransient())
Geoff Berry173b14d2016-02-09 20:47:21 +00001425 ++Count;
Chad Rosier35706ad2016-02-04 21:26:02 +00001426
Tim Northover3b0846e2014-05-24 12:50:23 +00001427 // If we found a match, return it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001428 if (isMatchingUpdateInsn(*I, MI, BaseReg, Offset))
Tim Northover3b0846e2014-05-24 12:50:23 +00001429 return MBBI;
1430
1431 // Update the status of what the instruction clobbered and used.
1432 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1433
1434 // Otherwise, if the base register is used or modified, we have no match, so
1435 // return early.
1436 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
1437 return E;
Geoff Berry173b14d2016-02-09 20:47:21 +00001438 } while (MBBI != B && Count < Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001439 return E;
1440}
1441
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001442bool AArch64LoadStoreOpt::tryToPromoteLoadFromStore(
1443 MachineBasicBlock::iterator &MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001444 MachineInstr &MI = *MBBI;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001445 // If this is a volatile load, don't mess with it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001446 if (MI.hasOrderedMemoryRef())
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001447 return false;
1448
1449 // Make sure this is a reg+imm.
1450 // FIXME: It is possible to extend it to handle reg+reg cases.
1451 if (!getLdStOffsetOp(MI).isImm())
1452 return false;
1453
Chad Rosier35706ad2016-02-04 21:26:02 +00001454 // Look backward up to LdStLimit instructions.
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001455 MachineBasicBlock::iterator StoreI;
Chad Rosier35706ad2016-02-04 21:26:02 +00001456 if (findMatchingStore(MBBI, LdStLimit, StoreI)) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001457 ++NumLoadsFromStoresPromoted;
1458 // Promote the load. Keeping the iterator straight is a
1459 // pain, so we let the merge routine tell us what the next instruction
1460 // is after it's done mucking about.
1461 MBBI = promoteLoadFromStore(MBBI, StoreI);
1462 return true;
1463 }
1464 return false;
1465}
1466
Chad Rosierd6daac42016-11-07 15:27:22 +00001467// Merge adjacent zero stores into a wider store.
1468bool AArch64LoadStoreOpt::tryToMergeZeroStInst(
Chad Rosier24c46ad2016-02-09 18:10:20 +00001469 MachineBasicBlock::iterator &MBBI) {
Chad Rosierd6daac42016-11-07 15:27:22 +00001470 assert(isPromotableZeroStoreInst(*MBBI) && "Expected narrow store.");
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001471 MachineInstr &MI = *MBBI;
1472 MachineBasicBlock::iterator E = MI.getParent()->end();
Chad Rosier24c46ad2016-02-09 18:10:20 +00001473
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001474 if (!TII->isCandidateToMergeOrPair(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001475 return false;
1476
1477 // Look ahead up to LdStLimit instructions for a mergable instruction.
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001478 LdStPairFlags Flags;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +00001479 MachineBasicBlock::iterator MergeMI =
Jun Bum Limcf974432016-03-31 14:47:24 +00001480 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ true);
Chad Rosierd7363db2016-02-09 19:09:22 +00001481 if (MergeMI != E) {
Chad Rosierd6daac42016-11-07 15:27:22 +00001482 ++NumZeroStoresPromoted;
1483
Chad Rosier24c46ad2016-02-09 18:10:20 +00001484 // Keeping the iterator straight is a pain, so we let the merge routine tell
1485 // us what the next instruction is after it's done mucking about.
Chad Rosierd6daac42016-11-07 15:27:22 +00001486 MBBI = mergeNarrowZeroStores(MBBI, MergeMI, Flags);
Chad Rosier24c46ad2016-02-09 18:10:20 +00001487 return true;
1488 }
1489 return false;
1490}
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001491
Chad Rosier24c46ad2016-02-09 18:10:20 +00001492// Find loads and stores that can be merged into a single load or store pair
1493// instruction.
1494bool AArch64LoadStoreOpt::tryToPairLdStInst(MachineBasicBlock::iterator &MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001495 MachineInstr &MI = *MBBI;
1496 MachineBasicBlock::iterator E = MI.getParent()->end();
Chad Rosier24c46ad2016-02-09 18:10:20 +00001497
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001498 if (!TII->isCandidateToMergeOrPair(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001499 return false;
1500
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001501 // Early exit if the offset is not possible to match. (6 bits of positive
1502 // range, plus allow an extra one in case we find a later insn that matches
1503 // with Offset-1)
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001504 bool IsUnscaled = TII->isUnscaledLdSt(MI);
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001505 int Offset = getLdStOffsetOp(MI).getImm();
1506 int OffsetStride = IsUnscaled ? getMemScale(MI) : 1;
Nirav Dave0f9d1112017-01-04 21:21:46 +00001507 // Allow one more for offset.
1508 if (Offset > 0)
1509 Offset -= OffsetStride;
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001510 if (!inBoundsForPair(IsUnscaled, Offset, OffsetStride))
1511 return false;
1512
Chad Rosier24c46ad2016-02-09 18:10:20 +00001513 // Look ahead up to LdStLimit instructions for a pairable instruction.
1514 LdStPairFlags Flags;
Jun Bum Limcf974432016-03-31 14:47:24 +00001515 MachineBasicBlock::iterator Paired =
1516 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ false);
Chad Rosier24c46ad2016-02-09 18:10:20 +00001517 if (Paired != E) {
1518 ++NumPairCreated;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001519 if (TII->isUnscaledLdSt(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001520 ++NumUnscaledPairCreated;
1521 // Keeping the iterator straight is a pain, so we let the merge routine tell
1522 // us what the next instruction is after it's done mucking about.
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001523 MBBI = mergePairedInsns(MBBI, Paired, Flags);
1524 return true;
1525 }
1526 return false;
1527}
1528
Jun Bum Lim22fe15e2015-11-06 16:27:47 +00001529bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB,
Chad Rosierd6daac42016-11-07 15:27:22 +00001530 bool EnableNarrowZeroStOpt) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001531 bool Modified = false;
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001532 // Four tranformations to do here:
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001533 // 1) Find loads that directly read from stores and promote them by
1534 // replacing with mov instructions. If the store is wider than the load,
1535 // the load will be replaced with a bitfield extract.
1536 // e.g.,
1537 // str w1, [x0, #4]
1538 // ldrh w2, [x0, #6]
1539 // ; becomes
1540 // str w1, [x0, #4]
NAKAMURA Takumife1202c2016-06-20 00:37:41 +00001541 // lsr w2, w1, #16
Tim Northover3b0846e2014-05-24 12:50:23 +00001542 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001543 MBBI != E;) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001544 MachineInstr &MI = *MBBI;
1545 switch (MI.getOpcode()) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001546 default:
1547 // Just move on to the next instruction.
1548 ++MBBI;
1549 break;
1550 // Scaled instructions.
1551 case AArch64::LDRBBui:
1552 case AArch64::LDRHHui:
1553 case AArch64::LDRWui:
1554 case AArch64::LDRXui:
1555 // Unscaled instructions.
1556 case AArch64::LDURBBi:
1557 case AArch64::LDURHHi:
1558 case AArch64::LDURWi:
Eugene Zelenko11f69072017-01-25 00:29:26 +00001559 case AArch64::LDURXi:
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001560 if (tryToPromoteLoadFromStore(MBBI)) {
1561 Modified = true;
1562 break;
1563 }
1564 ++MBBI;
1565 break;
1566 }
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001567 }
Chad Rosierd6daac42016-11-07 15:27:22 +00001568 // 2) Merge adjacent zero stores into a wider store.
Jun Bum Lim1de2d442016-02-05 20:02:03 +00001569 // e.g.,
1570 // strh wzr, [x0]
1571 // strh wzr, [x0, #2]
1572 // ; becomes
1573 // str wzr, [x0]
Chad Rosierd6daac42016-11-07 15:27:22 +00001574 // e.g.,
1575 // str wzr, [x0]
1576 // str wzr, [x0, #4]
1577 // ; becomes
1578 // str xzr, [x0]
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001579 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Chad Rosierd6daac42016-11-07 15:27:22 +00001580 EnableNarrowZeroStOpt && MBBI != E;) {
1581 if (isPromotableZeroStoreInst(*MBBI)) {
1582 if (tryToMergeZeroStInst(MBBI)) {
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001583 Modified = true;
Jun Bum Lim33be4992016-05-06 15:08:57 +00001584 } else
1585 ++MBBI;
1586 } else
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001587 ++MBBI;
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001588 }
Jun Bum Lim33be4992016-05-06 15:08:57 +00001589
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001590 // 3) Find loads and stores that can be merged into a single load or store
1591 // pair instruction.
1592 // e.g.,
1593 // ldr x0, [x2]
1594 // ldr x1, [x2, #8]
1595 // ; becomes
1596 // ldp x0, x1, [x2]
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001597 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Tim Northover3b0846e2014-05-24 12:50:23 +00001598 MBBI != E;) {
Geoff Berry22dfbc52016-08-12 15:26:00 +00001599 if (TII->isPairableLdStInst(*MBBI) && tryToPairLdStInst(MBBI))
1600 Modified = true;
1601 else
Tim Northover3b0846e2014-05-24 12:50:23 +00001602 ++MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001603 }
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001604 // 4) Find base register updates that can be merged into the load or store
1605 // as a base-reg writeback.
1606 // e.g.,
1607 // ldr x0, [x2]
1608 // add x2, x2, #4
1609 // ; becomes
1610 // ldr x0, [x2], #4
Tim Northover3b0846e2014-05-24 12:50:23 +00001611 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
1612 MBBI != E;) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001613 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001614 // Do update merging. It's simpler to keep this separate from the above
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001615 // switchs, though not strictly necessary.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001616 unsigned Opc = MI.getOpcode();
Tim Northover3b0846e2014-05-24 12:50:23 +00001617 switch (Opc) {
1618 default:
1619 // Just move on to the next instruction.
1620 ++MBBI;
1621 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001622 // Scaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001623 case AArch64::STRSui:
1624 case AArch64::STRDui:
1625 case AArch64::STRQui:
1626 case AArch64::STRXui:
1627 case AArch64::STRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001628 case AArch64::STRHHui:
1629 case AArch64::STRBBui:
Tim Northover3b0846e2014-05-24 12:50:23 +00001630 case AArch64::LDRSui:
1631 case AArch64::LDRDui:
1632 case AArch64::LDRQui:
1633 case AArch64::LDRXui:
1634 case AArch64::LDRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001635 case AArch64::LDRHHui:
1636 case AArch64::LDRBBui:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001637 // Unscaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001638 case AArch64::STURSi:
1639 case AArch64::STURDi:
1640 case AArch64::STURQi:
1641 case AArch64::STURWi:
1642 case AArch64::STURXi:
1643 case AArch64::LDURSi:
1644 case AArch64::LDURDi:
1645 case AArch64::LDURQi:
1646 case AArch64::LDURWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001647 case AArch64::LDURXi:
1648 // Paired instructions.
1649 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +00001650 case AArch64::LDPSWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001651 case AArch64::LDPDi:
1652 case AArch64::LDPQi:
1653 case AArch64::LDPWi:
1654 case AArch64::LDPXi:
1655 case AArch64::STPSi:
1656 case AArch64::STPDi:
1657 case AArch64::STPQi:
1658 case AArch64::STPWi:
1659 case AArch64::STPXi: {
Tim Northover3b0846e2014-05-24 12:50:23 +00001660 // Make sure this is a reg+imm (as opposed to an address reloc).
Chad Rosierf77e9092015-08-06 15:50:12 +00001661 if (!getLdStOffsetOp(MI).isImm()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001662 ++MBBI;
1663 break;
1664 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001665 // Look forward to try to form a post-index instruction. For example,
1666 // ldr x0, [x20]
1667 // add x20, x20, #32
1668 // merged into:
1669 // ldr x0, [x20], #32
Tim Northover3b0846e2014-05-24 12:50:23 +00001670 MachineBasicBlock::iterator Update =
Chad Rosier35706ad2016-02-04 21:26:02 +00001671 findMatchingUpdateInsnForward(MBBI, 0, 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=*/false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001675 Modified = true;
1676 ++NumPostFolded;
1677 break;
1678 }
1679 // Don't know how to handle pre/post-index versions, so move to the next
1680 // instruction.
Chad Rosiere4e15ba2016-03-09 17:29:48 +00001681 if (TII->isUnscaledLdSt(Opc)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001682 ++MBBI;
1683 break;
1684 }
1685
1686 // Look back to try to find a pre-index instruction. For example,
1687 // add x0, x0, #8
1688 // ldr x1, [x0]
1689 // merged into:
1690 // ldr x1, [x0, #8]!
Chad Rosier35706ad2016-02-04 21:26:02 +00001691 Update = findMatchingUpdateInsnBackward(MBBI, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001692 if (Update != E) {
1693 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001694 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001695 Modified = true;
1696 ++NumPreFolded;
1697 break;
1698 }
Chad Rosier7a83d772015-10-01 13:09:44 +00001699 // The immediate in the load/store is scaled by the size of the memory
1700 // operation. The immediate in the add we're looking for,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001701 // however, is not, so adjust here.
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001702 int UnscaledOffset = getLdStOffsetOp(MI).getImm() * getMemScale(MI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001703
Tim Northover3b0846e2014-05-24 12:50:23 +00001704 // Look forward to try to find a post-index instruction. For example,
1705 // ldr x1, [x0, #64]
1706 // add x0, x0, #64
1707 // merged into:
1708 // ldr x1, [x0, #64]!
Chad Rosier35706ad2016-02-04 21:26:02 +00001709 Update = findMatchingUpdateInsnForward(MBBI, UnscaledOffset, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001710 if (Update != E) {
1711 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001712 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001713 Modified = true;
1714 ++NumPreFolded;
1715 break;
1716 }
1717
1718 // Nothing found. Just move to the next instruction.
1719 ++MBBI;
1720 break;
1721 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001722 }
1723 }
1724
1725 return Modified;
1726}
1727
1728bool AArch64LoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
Andrew Kaylor1ac98bb2016-04-25 21:58:52 +00001729 if (skipFunction(*Fn.getFunction()))
1730 return false;
1731
Oliver Stannardd414c992015-11-10 11:04:18 +00001732 Subtarget = &static_cast<const AArch64Subtarget &>(Fn.getSubtarget());
1733 TII = static_cast<const AArch64InstrInfo *>(Subtarget->getInstrInfo());
1734 TRI = Subtarget->getRegisterInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +00001735
Chad Rosierbba881e2016-02-02 15:02:30 +00001736 // Resize the modified and used register bitfield trackers. We do this once
1737 // per function and then clear the bitfield each time we optimize a load or
1738 // store.
1739 ModifiedRegs.resize(TRI->getNumRegs());
1740 UsedRegs.resize(TRI->getNumRegs());
1741
Tim Northover3b0846e2014-05-24 12:50:23 +00001742 bool Modified = false;
Chad Rosier10c7aaa2016-11-11 14:10:12 +00001743 bool enableNarrowZeroStOpt = !Subtarget->requiresStrictAlign();
Tim Northover3b0846e2014-05-24 12:50:23 +00001744 for (auto &MBB : Fn)
Chad Rosierd6daac42016-11-07 15:27:22 +00001745 Modified |= optimizeBlock(MBB, enableNarrowZeroStOpt);
Tim Northover3b0846e2014-05-24 12:50:23 +00001746
1747 return Modified;
1748}
1749
Chad Rosier8ade0342016-11-11 19:52:45 +00001750// FIXME: Do we need/want a pre-alloc pass like ARM has to try to keep loads and
1751// stores near one another? Note: The pre-RA instruction scheduler already has
1752// hooks to try and schedule pairable loads/stores together to improve pairing
1753// opportunities. Thus, pre-RA pairing pass may not be worth the effort.
Tim Northover3b0846e2014-05-24 12:50:23 +00001754
Chad Rosier3f8b09d2016-02-09 19:42:19 +00001755// FIXME: When pairing store instructions it's very possible for this pass to
1756// hoist a store with a KILL marker above another use (without a KILL marker).
1757// The resulting IR is invalid, but nothing uses the KILL markers after this
1758// pass, so it's never caused a problem in practice.
1759
Chad Rosier43f5c842015-08-05 12:40:13 +00001760/// createAArch64LoadStoreOptimizationPass - returns an instance of the
1761/// load / store optimization pass.
Tim Northover3b0846e2014-05-24 12:50:23 +00001762FunctionPass *llvm::createAArch64LoadStoreOptimizationPass() {
1763 return new AArch64LoadStoreOpt();
1764}