blob: 8ec726267e8d5a03f8ad300897d4228e352eab00 [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
Matthias Braund9a59a82017-02-17 23:15:03 +0000870 // Clear kill flags between store and load.
871 for (MachineInstr &MI : make_range(StoreI->getIterator(),
872 BitExtMI->getIterator()))
873 MI.clearRegisterKills(StRt, TRI);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000874
875 DEBUG(dbgs() << "Promoting load by replacing :\n ");
876 DEBUG(StoreI->print(dbgs()));
877 DEBUG(dbgs() << " ");
878 DEBUG(LoadI->print(dbgs()));
879 DEBUG(dbgs() << " with instructions:\n ");
880 DEBUG(StoreI->print(dbgs()));
881 DEBUG(dbgs() << " ");
882 DEBUG((BitExtMI)->print(dbgs()));
883 DEBUG(dbgs() << "\n");
884
885 // Erase the old instructions.
886 LoadI->eraseFromParent();
887 return NextI;
888}
889
Tim Northover3b0846e2014-05-24 12:50:23 +0000890/// trackRegDefsUses - Remember what registers the specified instruction uses
891/// and modifies.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000892static void trackRegDefsUses(const MachineInstr &MI, BitVector &ModifiedRegs,
Tim Northover3b0846e2014-05-24 12:50:23 +0000893 BitVector &UsedRegs,
894 const TargetRegisterInfo *TRI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000895 for (const MachineOperand &MO : MI.operands()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000896 if (MO.isRegMask())
897 ModifiedRegs.setBitsNotInMask(MO.getRegMask());
898
899 if (!MO.isReg())
900 continue;
901 unsigned Reg = MO.getReg();
Geoff Berry173b14d2016-02-09 20:47:21 +0000902 if (!Reg)
903 continue;
Tim Northover3b0846e2014-05-24 12:50:23 +0000904 if (MO.isDef()) {
Geoff Berrye0bf52f2016-11-21 22:51:10 +0000905 // WZR/XZR are not modified even when used as a destination register.
906 if (Reg != AArch64::WZR && Reg != AArch64::XZR)
907 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
908 ModifiedRegs.set(*AI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000909 } else {
910 assert(MO.isUse() && "Reg operand not a def and not a use?!?");
911 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
912 UsedRegs.set(*AI);
913 }
914 }
915}
916
917static bool inBoundsForPair(bool IsUnscaled, int Offset, int OffsetStride) {
Chad Rosier3dd0e942015-08-18 16:20:03 +0000918 // Convert the byte-offset used by unscaled into an "element" offset used
919 // by the scaled pair load/store instructions.
Chad Rosier00f9d232016-02-11 14:25:08 +0000920 if (IsUnscaled) {
921 // If the byte-offset isn't a multiple of the stride, there's no point
922 // trying to match it.
923 if (Offset % OffsetStride)
924 return false;
Chad Rosier3dd0e942015-08-18 16:20:03 +0000925 Offset /= OffsetStride;
Chad Rosier00f9d232016-02-11 14:25:08 +0000926 }
Chad Rosier3dd0e942015-08-18 16:20:03 +0000927 return Offset <= 63 && Offset >= -64;
Tim Northover3b0846e2014-05-24 12:50:23 +0000928}
929
930// Do alignment, specialized to power of 2 and for signed ints,
931// avoiding having to do a C-style cast from uint_64t to int when
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000932// using alignTo from include/llvm/Support/MathExtras.h.
Tim Northover3b0846e2014-05-24 12:50:23 +0000933// FIXME: Move this function to include/MathExtras.h?
934static int alignTo(int Num, int PowOf2) {
935 return (Num + PowOf2 - 1) & ~(PowOf2 - 1);
936}
937
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000938static bool mayAlias(MachineInstr &MIa, MachineInstr &MIb,
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000939 const AArch64InstrInfo *TII) {
940 // One of the instructions must modify memory.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000941 if (!MIa.mayStore() && !MIb.mayStore())
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000942 return false;
943
944 // Both instructions must be memory operations.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000945 if (!MIa.mayLoadOrStore() && !MIb.mayLoadOrStore())
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000946 return false;
947
948 return !TII->areMemAccessesTriviallyDisjoint(MIa, MIb);
949}
950
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000951static bool mayAlias(MachineInstr &MIa,
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000952 SmallVectorImpl<MachineInstr *> &MemInsns,
953 const AArch64InstrInfo *TII) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000954 for (MachineInstr *MIb : MemInsns)
955 if (mayAlias(MIa, *MIb, TII))
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000956 return true;
957
958 return false;
959}
960
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000961bool AArch64LoadStoreOpt::findMatchingStore(
962 MachineBasicBlock::iterator I, unsigned Limit,
963 MachineBasicBlock::iterator &StoreI) {
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000964 MachineBasicBlock::iterator B = I->getParent()->begin();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000965 MachineBasicBlock::iterator MBBI = I;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000966 MachineInstr &LoadMI = *I;
Chad Rosier5c6a66c2016-02-09 15:59:57 +0000967 unsigned BaseReg = getLdStBaseOp(LoadMI).getReg();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000968
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000969 // If the load is the first instruction in the block, there's obviously
970 // not any matching store.
971 if (MBBI == B)
972 return false;
973
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000974 // Track which registers have been modified and used between the first insn
975 // and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +0000976 ModifiedRegs.reset();
977 UsedRegs.reset();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000978
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000979 unsigned Count = 0;
980 do {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000981 --MBBI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000982 MachineInstr &MI = *MBBI;
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000983
Geoff Berry4ff2e362016-07-21 15:20:25 +0000984 // Don't count transient instructions towards the search limit since there
985 // may be different numbers of them if e.g. debug information is present.
986 if (!MI.isTransient())
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000987 ++Count;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000988
989 // If the load instruction reads directly from the address to which the
990 // store instruction writes and the stored value is not modified, we can
991 // promote the load. Since we do not handle stores with pre-/post-index,
992 // it's unnecessary to check if BaseReg is modified by the store itself.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000993 if (MI.mayStore() && isMatchingStore(LoadMI, MI) &&
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000994 BaseReg == getLdStBaseOp(MI).getReg() &&
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000995 isLdOffsetInRangeOfSt(LoadMI, MI, TII) &&
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000996 !ModifiedRegs[getLdStRegOp(MI).getReg()]) {
997 StoreI = MBBI;
998 return true;
999 }
1000
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001001 if (MI.isCall())
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001002 return false;
1003
1004 // Update modified / uses register lists.
1005 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1006
1007 // Otherwise, if the base register is modified, we have no match, so
1008 // return early.
1009 if (ModifiedRegs[BaseReg])
1010 return false;
1011
1012 // If we encounter a store aliased with the load, return early.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001013 if (MI.mayStore() && mayAlias(LoadMI, MI, TII))
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001014 return false;
Jun Bum Lim633b2d82016-02-11 16:18:24 +00001015 } while (MBBI != B && Count < Limit);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001016 return false;
1017}
1018
Chad Rosierc5083c22016-06-10 20:47:14 +00001019// Returns true if FirstMI and MI are candidates for merging or pairing.
1020// Otherwise, returns false.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001021static bool areCandidatesToMergeOrPair(MachineInstr &FirstMI, MachineInstr &MI,
Chad Rosierc5083c22016-06-10 20:47:14 +00001022 LdStPairFlags &Flags,
1023 const AArch64InstrInfo *TII) {
1024 // If this is volatile or if pairing is suppressed, not a candidate.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001025 if (MI.hasOrderedMemoryRef() || TII->isLdStPairSuppressed(MI))
Chad Rosierc5083c22016-06-10 20:47:14 +00001026 return false;
1027
1028 // We should have already checked FirstMI for pair suppression and volatility.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001029 assert(!FirstMI.hasOrderedMemoryRef() &&
1030 !TII->isLdStPairSuppressed(FirstMI) &&
Chad Rosierc5083c22016-06-10 20:47:14 +00001031 "FirstMI shouldn't get here if either of these checks are true.");
1032
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001033 unsigned OpcA = FirstMI.getOpcode();
1034 unsigned OpcB = MI.getOpcode();
Chad Rosierc5083c22016-06-10 20:47:14 +00001035
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001036 // Opcodes match: nothing more to check.
1037 if (OpcA == OpcB)
1038 return true;
1039
1040 // Try to match a sign-extended load/store with a zero-extended load/store.
1041 bool IsValidLdStrOpc, PairIsValidLdStrOpc;
1042 unsigned NonSExtOpc = getMatchingNonSExtOpcode(OpcA, &IsValidLdStrOpc);
1043 assert(IsValidLdStrOpc &&
1044 "Given Opc should be a Load or Store with an immediate");
1045 // OpcA will be the first instruction in the pair.
1046 if (NonSExtOpc == getMatchingNonSExtOpcode(OpcB, &PairIsValidLdStrOpc)) {
1047 Flags.setSExtIdx(NonSExtOpc == (unsigned)OpcA ? 1 : 0);
1048 return true;
1049 }
Chad Rosier00f9d232016-02-11 14:25:08 +00001050
Chad Rosierd6daac42016-11-07 15:27:22 +00001051 // If the second instruction isn't even a mergable/pairable load/store, bail
1052 // out.
Chad Rosier00f9d232016-02-11 14:25:08 +00001053 if (!PairIsValidLdStrOpc)
1054 return false;
1055
Chad Rosierd6daac42016-11-07 15:27:22 +00001056 // FIXME: We don't support merging narrow stores with mixed scaled/unscaled
1057 // offsets.
1058 if (isNarrowStore(OpcA) || isNarrowStore(OpcB))
Chad Rosier00f9d232016-02-11 14:25:08 +00001059 return false;
1060
1061 // Try to match an unscaled load/store with a scaled load/store.
Chad Rosiere4e15ba2016-03-09 17:29:48 +00001062 return TII->isUnscaledLdSt(OpcA) != TII->isUnscaledLdSt(OpcB) &&
Chad Rosier00f9d232016-02-11 14:25:08 +00001063 getMatchingPairOpcode(OpcA) == getMatchingPairOpcode(OpcB);
1064
1065 // FIXME: Can we also match a mixed sext/zext unscaled/scaled pair?
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001066}
1067
Chad Rosier9f4ec2e2016-02-10 18:49:28 +00001068/// Scan the instructions looking for a load/store that can be combined with the
1069/// current instruction into a wider equivalent or a load/store pair.
Tim Northover3b0846e2014-05-24 12:50:23 +00001070MachineBasicBlock::iterator
1071AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
Jun Bum Limcf974432016-03-31 14:47:24 +00001072 LdStPairFlags &Flags, unsigned Limit,
1073 bool FindNarrowMerge) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001074 MachineBasicBlock::iterator E = I->getParent()->end();
1075 MachineBasicBlock::iterator MBBI = I;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001076 MachineInstr &FirstMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001077 ++MBBI;
1078
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001079 bool MayLoad = FirstMI.mayLoad();
1080 bool IsUnscaled = TII->isUnscaledLdSt(FirstMI);
Chad Rosierf77e9092015-08-06 15:50:12 +00001081 unsigned Reg = getLdStRegOp(FirstMI).getReg();
1082 unsigned BaseReg = getLdStBaseOp(FirstMI).getReg();
1083 int Offset = getLdStOffsetOp(FirstMI).getImm();
Chad Rosierf11d0402015-10-01 18:17:12 +00001084 int OffsetStride = IsUnscaled ? getMemScale(FirstMI) : 1;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +00001085 bool IsPromotableZeroStore = isPromotableZeroStoreInst(FirstMI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001086
1087 // Track which registers have been modified and used between the first insn
1088 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001089 ModifiedRegs.reset();
1090 UsedRegs.reset();
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001091
1092 // Remember any instructions that read/write memory between FirstMI and MI.
1093 SmallVector<MachineInstr *, 4> MemInsns;
1094
Tim Northover3b0846e2014-05-24 12:50:23 +00001095 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001096 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001097
Geoff Berry4ff2e362016-07-21 15:20:25 +00001098 // Don't count transient instructions towards the search limit since there
1099 // may be different numbers of them if e.g. debug information is present.
1100 if (!MI.isTransient())
1101 ++Count;
Tim Northover3b0846e2014-05-24 12:50:23 +00001102
Chad Rosier18896c02016-02-04 16:01:40 +00001103 Flags.setSExtIdx(-1);
Chad Rosierc5083c22016-06-10 20:47:14 +00001104 if (areCandidatesToMergeOrPair(FirstMI, MI, Flags, TII) &&
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001105 getLdStOffsetOp(MI).isImm()) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001106 assert(MI.mayLoadOrStore() && "Expected memory operation.");
Tim Northover3b0846e2014-05-24 12:50:23 +00001107 // If we've found another instruction with the same opcode, check to see
1108 // if the base and offset are compatible with our starting instruction.
1109 // These instructions all have scaled immediate operands, so we just
1110 // check for +1/-1. Make sure to check the new instruction offset is
1111 // actually an immediate and not a symbolic reference destined for
1112 // a relocation.
Chad Rosierf77e9092015-08-06 15:50:12 +00001113 unsigned MIBaseReg = getLdStBaseOp(MI).getReg();
1114 int MIOffset = getLdStOffsetOp(MI).getImm();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001115 bool MIIsUnscaled = TII->isUnscaledLdSt(MI);
Chad Rosier00f9d232016-02-11 14:25:08 +00001116 if (IsUnscaled != MIIsUnscaled) {
1117 // We're trying to pair instructions that differ in how they are scaled.
1118 // If FirstMI is scaled then scale the offset of MI accordingly.
1119 // Otherwise, do the opposite (i.e., make MI's offset unscaled).
1120 int MemSize = getMemScale(MI);
1121 if (MIIsUnscaled) {
1122 // If the unscaled offset isn't a multiple of the MemSize, we can't
1123 // pair the operations together: bail and keep looking.
Eli Friedmanf184e4b2016-08-12 20:39:51 +00001124 if (MIOffset % MemSize) {
1125 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1126 MemInsns.push_back(&MI);
Chad Rosier00f9d232016-02-11 14:25:08 +00001127 continue;
Eli Friedmanf184e4b2016-08-12 20:39:51 +00001128 }
Chad Rosier00f9d232016-02-11 14:25:08 +00001129 MIOffset /= MemSize;
1130 } else {
1131 MIOffset *= MemSize;
1132 }
1133 }
1134
Tim Northover3b0846e2014-05-24 12:50:23 +00001135 if (BaseReg == MIBaseReg && ((Offset == MIOffset + OffsetStride) ||
1136 (Offset + OffsetStride == MIOffset))) {
1137 int MinOffset = Offset < MIOffset ? Offset : MIOffset;
Jun Bum Limcf974432016-03-31 14:47:24 +00001138 if (FindNarrowMerge) {
Jun Bum Lim80ec0d32015-11-20 21:14:07 +00001139 // If the alignment requirements of the scaled wide load/store
Jun Bum Limcf974432016-03-31 14:47:24 +00001140 // instruction can't express the offset of the scaled narrow input,
1141 // bail and keep looking. For promotable zero stores, allow only when
1142 // the stored value is the same (i.e., WZR).
1143 if ((!IsUnscaled && alignTo(MinOffset, 2) != MinOffset) ||
1144 (IsPromotableZeroStore && Reg != getLdStRegOp(MI).getReg())) {
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001145 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001146 MemInsns.push_back(&MI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001147 continue;
1148 }
1149 } else {
Chad Rosierd1f6c842016-06-10 20:49:18 +00001150 // Pairwise instructions have a 7-bit signed offset field. Single
1151 // insns have a 12-bit unsigned offset field. If the resultant
1152 // immediate offset of merging these instructions is out of range for
1153 // a pairwise instruction, bail and keep looking.
Jun Bum Limcf974432016-03-31 14:47:24 +00001154 if (!inBoundsForPair(IsUnscaled, MinOffset, OffsetStride)) {
1155 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001156 MemInsns.push_back(&MI);
Jun Bum Limcf974432016-03-31 14:47:24 +00001157 continue;
1158 }
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001159 // If the alignment requirements of the paired (scaled) instruction
1160 // can't express the offset of the unscaled input, bail and keep
1161 // looking.
1162 if (IsUnscaled && (alignTo(MinOffset, OffsetStride) != MinOffset)) {
1163 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001164 MemInsns.push_back(&MI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001165 continue;
1166 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001167 }
1168 // If the destination register of the loads is the same register, bail
1169 // and keep looking. A load-pair instruction with both destination
1170 // registers the same is UNPREDICTABLE and will result in an exception.
Jun Bum Limcf974432016-03-31 14:47:24 +00001171 if (MayLoad && Reg == getLdStRegOp(MI).getReg()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001172 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001173 MemInsns.push_back(&MI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001174 continue;
1175 }
1176
1177 // If the Rt of the second instruction was not modified or used between
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001178 // the two instructions and none of the instructions between the second
1179 // and first alias with the second, we can combine the second into the
1180 // first.
Chad Rosierf77e9092015-08-06 15:50:12 +00001181 if (!ModifiedRegs[getLdStRegOp(MI).getReg()] &&
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001182 !(MI.mayLoad() && UsedRegs[getLdStRegOp(MI).getReg()]) &&
1183 !mayAlias(MI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +00001184 Flags.setMergeForward(false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001185 return MBBI;
1186 }
1187
1188 // Likewise, if the Rt of the first instruction is not modified or used
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001189 // between the two instructions and none of the instructions between the
1190 // first and the second alias with the first, we can combine the first
1191 // into the second.
Chad Rosierf77e9092015-08-06 15:50:12 +00001192 if (!ModifiedRegs[getLdStRegOp(FirstMI).getReg()] &&
Chad Rosier5f668e12015-09-03 14:19:43 +00001193 !(MayLoad && UsedRegs[getLdStRegOp(FirstMI).getReg()]) &&
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001194 !mayAlias(FirstMI, MemInsns, TII)) {
Chad Rosier96a18a92015-07-21 17:42:04 +00001195 Flags.setMergeForward(true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001196 return MBBI;
1197 }
1198 // Unable to combine these instructions due to interference in between.
1199 // Keep looking.
1200 }
1201 }
1202
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001203 // If the instruction wasn't a matching load or store. Stop searching if we
1204 // encounter a call instruction that might modify memory.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001205 if (MI.isCall())
Tim Northover3b0846e2014-05-24 12:50:23 +00001206 return E;
1207
1208 // Update modified / uses register lists.
1209 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1210
1211 // Otherwise, if the base register is modified, we have no match, so
1212 // return early.
1213 if (ModifiedRegs[BaseReg])
1214 return E;
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001215
1216 // Update list of instructions that read/write memory.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001217 if (MI.mayLoadOrStore())
1218 MemInsns.push_back(&MI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001219 }
1220 return E;
1221}
1222
1223MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +00001224AArch64LoadStoreOpt::mergeUpdateInsn(MachineBasicBlock::iterator I,
1225 MachineBasicBlock::iterator Update,
1226 bool IsPreIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001227 assert((Update->getOpcode() == AArch64::ADDXri ||
1228 Update->getOpcode() == AArch64::SUBXri) &&
1229 "Unexpected base register update instruction to merge!");
1230 MachineBasicBlock::iterator NextI = I;
1231 // Return the instruction following the merged instruction, which is
1232 // the instruction following our unmerged load. Unless that's the add/sub
1233 // instruction we're merging, in which case it's the one after that.
1234 if (++NextI == Update)
1235 ++NextI;
1236
1237 int Value = Update->getOperand(2).getImm();
1238 assert(AArch64_AM::getShiftValue(Update->getOperand(3).getImm()) == 0 &&
Chad Rosier2dfd3542015-09-23 13:51:44 +00001239 "Can't merge 1 << 12 offset into pre-/post-indexed load / store");
Tim Northover3b0846e2014-05-24 12:50:23 +00001240 if (Update->getOpcode() == AArch64::SUBXri)
1241 Value = -Value;
1242
Chad Rosier2dfd3542015-09-23 13:51:44 +00001243 unsigned NewOpc = IsPreIdx ? getPreIndexedOpcode(I->getOpcode())
1244 : getPostIndexedOpcode(I->getOpcode());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001245 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001246 if (!isPairedLdSt(*I)) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001247 // Non-paired instruction.
1248 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001249 .add(getLdStRegOp(*Update))
1250 .add(getLdStRegOp(*I))
1251 .add(getLdStBaseOp(*I))
Chad Rosier3ada75f2016-01-28 15:38:24 +00001252 .addImm(Value)
1253 .setMemRefs(I->memoperands_begin(), I->memoperands_end());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001254 } else {
1255 // Paired instruction.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001256 int Scale = getMemScale(*I);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001257 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001258 .add(getLdStRegOp(*Update))
1259 .add(getLdStRegOp(*I, 0))
1260 .add(getLdStRegOp(*I, 1))
1261 .add(getLdStBaseOp(*I))
Chad Rosier3ada75f2016-01-28 15:38:24 +00001262 .addImm(Value / Scale)
1263 .setMemRefs(I->memoperands_begin(), I->memoperands_end());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001264 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001265 (void)MIB;
1266
Chad Rosier2dfd3542015-09-23 13:51:44 +00001267 if (IsPreIdx)
1268 DEBUG(dbgs() << "Creating pre-indexed load/store.");
1269 else
1270 DEBUG(dbgs() << "Creating post-indexed load/store.");
Tim Northover3b0846e2014-05-24 12:50:23 +00001271 DEBUG(dbgs() << " Replacing instructions:\n ");
1272 DEBUG(I->print(dbgs()));
1273 DEBUG(dbgs() << " ");
1274 DEBUG(Update->print(dbgs()));
1275 DEBUG(dbgs() << " with instruction:\n ");
1276 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
1277 DEBUG(dbgs() << "\n");
1278
1279 // Erase the old instructions for the block.
1280 I->eraseFromParent();
1281 Update->eraseFromParent();
1282
1283 return NextI;
1284}
1285
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001286bool AArch64LoadStoreOpt::isMatchingUpdateInsn(MachineInstr &MemMI,
1287 MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001288 unsigned BaseReg, int Offset) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001289 switch (MI.getOpcode()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001290 default:
1291 break;
1292 case AArch64::SUBXri:
Tim Northover3b0846e2014-05-24 12:50:23 +00001293 case AArch64::ADDXri:
1294 // Make sure it's a vanilla immediate operand, not a relocation or
1295 // anything else we can't handle.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001296 if (!MI.getOperand(2).isImm())
Tim Northover3b0846e2014-05-24 12:50:23 +00001297 break;
1298 // Watch out for 1 << 12 shifted value.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001299 if (AArch64_AM::getShiftValue(MI.getOperand(3).getImm()))
Tim Northover3b0846e2014-05-24 12:50:23 +00001300 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001301
1302 // The update instruction source and destination register must be the
1303 // same as the load/store base register.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001304 if (MI.getOperand(0).getReg() != BaseReg ||
1305 MI.getOperand(1).getReg() != BaseReg)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001306 break;
1307
1308 bool IsPairedInsn = isPairedLdSt(MemMI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001309 int UpdateOffset = MI.getOperand(2).getImm();
Eli Friedman8585e9d2016-08-12 20:28:02 +00001310 if (MI.getOpcode() == AArch64::SUBXri)
1311 UpdateOffset = -UpdateOffset;
1312
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001313 // For non-paired load/store instructions, the immediate must fit in a
1314 // signed 9-bit integer.
1315 if (!IsPairedInsn && (UpdateOffset > 255 || UpdateOffset < -256))
1316 break;
1317
1318 // For paired load/store instructions, the immediate must be a multiple of
1319 // the scaling factor. The scaled offset must also fit into a signed 7-bit
1320 // integer.
1321 if (IsPairedInsn) {
Chad Rosier32d4d372015-09-29 16:07:32 +00001322 int Scale = getMemScale(MemMI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001323 if (UpdateOffset % Scale != 0)
1324 break;
1325
1326 int ScaledOffset = UpdateOffset / Scale;
Eli Friedman8585e9d2016-08-12 20:28:02 +00001327 if (ScaledOffset > 63 || ScaledOffset < -64)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001328 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00001329 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001330
1331 // If we have a non-zero Offset, we check that it matches the amount
1332 // we're adding to the register.
Eli Friedman8585e9d2016-08-12 20:28:02 +00001333 if (!Offset || Offset == UpdateOffset)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001334 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001335 break;
1336 }
1337 return false;
1338}
1339
1340MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward(
Chad Rosier35706ad2016-02-04 21:26:02 +00001341 MachineBasicBlock::iterator I, int UnscaledOffset, unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001342 MachineBasicBlock::iterator E = I->getParent()->end();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001343 MachineInstr &MemMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001344 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001345
Chad Rosierf77e9092015-08-06 15:50:12 +00001346 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001347 int MIUnscaledOffset = getLdStOffsetOp(MemMI).getImm() * getMemScale(MemMI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001348
Chad Rosierb7c5b912015-10-01 13:43:05 +00001349 // Scan forward looking for post-index opportunities. Updating instructions
1350 // can't be formed if the memory instruction doesn't have the offset we're
1351 // looking for.
1352 if (MIUnscaledOffset != UnscaledOffset)
1353 return E;
1354
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001355 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +00001356 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001357 bool IsPairedInsn = isPairedLdSt(MemMI);
1358 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
1359 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
1360 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
1361 return E;
1362 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001363
Tim Northover3b0846e2014-05-24 12:50:23 +00001364 // Track which registers have been modified and used between the first insn
1365 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001366 ModifiedRegs.reset();
1367 UsedRegs.reset();
Tim Northover3b0846e2014-05-24 12:50:23 +00001368 ++MBBI;
Chad Rosier35706ad2016-02-04 21:26:02 +00001369 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001370 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001371
Geoff Berry4ff2e362016-07-21 15:20:25 +00001372 // Don't count transient instructions towards the search limit since there
1373 // may be different numbers of them if e.g. debug information is present.
1374 if (!MI.isTransient())
1375 ++Count;
Chad Rosier35706ad2016-02-04 21:26:02 +00001376
Tim Northover3b0846e2014-05-24 12:50:23 +00001377 // If we found a match, return it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001378 if (isMatchingUpdateInsn(*I, MI, BaseReg, UnscaledOffset))
Tim Northover3b0846e2014-05-24 12:50:23 +00001379 return MBBI;
1380
1381 // Update the status of what the instruction clobbered and used.
1382 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1383
1384 // Otherwise, if the base register is used or modified, we have no match, so
1385 // return early.
1386 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
1387 return E;
1388 }
1389 return E;
1390}
1391
1392MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnBackward(
Chad Rosier35706ad2016-02-04 21:26:02 +00001393 MachineBasicBlock::iterator I, unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001394 MachineBasicBlock::iterator B = I->getParent()->begin();
1395 MachineBasicBlock::iterator E = I->getParent()->end();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001396 MachineInstr &MemMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001397 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001398
Chad Rosierf77e9092015-08-06 15:50:12 +00001399 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
1400 int Offset = getLdStOffsetOp(MemMI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +00001401
1402 // If the load/store is the first instruction in the block, there's obviously
1403 // not any matching update. Ditto if the memory offset isn't zero.
1404 if (MBBI == B || Offset != 0)
1405 return E;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001406 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +00001407 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001408 bool IsPairedInsn = isPairedLdSt(MemMI);
1409 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
1410 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
1411 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
1412 return E;
1413 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001414
1415 // Track which registers have been modified and used between the first insn
1416 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001417 ModifiedRegs.reset();
1418 UsedRegs.reset();
Geoff Berry173b14d2016-02-09 20:47:21 +00001419 unsigned Count = 0;
1420 do {
1421 --MBBI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001422 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001423
Geoff Berry4ff2e362016-07-21 15:20:25 +00001424 // Don't count transient instructions towards the search limit since there
1425 // may be different numbers of them if e.g. debug information is present.
1426 if (!MI.isTransient())
Geoff Berry173b14d2016-02-09 20:47:21 +00001427 ++Count;
Chad Rosier35706ad2016-02-04 21:26:02 +00001428
Tim Northover3b0846e2014-05-24 12:50:23 +00001429 // If we found a match, return it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001430 if (isMatchingUpdateInsn(*I, MI, BaseReg, Offset))
Tim Northover3b0846e2014-05-24 12:50:23 +00001431 return MBBI;
1432
1433 // Update the status of what the instruction clobbered and used.
1434 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1435
1436 // Otherwise, if the base register is used or modified, we have no match, so
1437 // return early.
1438 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
1439 return E;
Geoff Berry173b14d2016-02-09 20:47:21 +00001440 } while (MBBI != B && Count < Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001441 return E;
1442}
1443
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001444bool AArch64LoadStoreOpt::tryToPromoteLoadFromStore(
1445 MachineBasicBlock::iterator &MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001446 MachineInstr &MI = *MBBI;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001447 // If this is a volatile load, don't mess with it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001448 if (MI.hasOrderedMemoryRef())
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001449 return false;
1450
1451 // Make sure this is a reg+imm.
1452 // FIXME: It is possible to extend it to handle reg+reg cases.
1453 if (!getLdStOffsetOp(MI).isImm())
1454 return false;
1455
Chad Rosier35706ad2016-02-04 21:26:02 +00001456 // Look backward up to LdStLimit instructions.
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001457 MachineBasicBlock::iterator StoreI;
Chad Rosier35706ad2016-02-04 21:26:02 +00001458 if (findMatchingStore(MBBI, LdStLimit, StoreI)) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001459 ++NumLoadsFromStoresPromoted;
1460 // Promote the load. Keeping the iterator straight is a
1461 // pain, so we let the merge routine tell us what the next instruction
1462 // is after it's done mucking about.
1463 MBBI = promoteLoadFromStore(MBBI, StoreI);
1464 return true;
1465 }
1466 return false;
1467}
1468
Chad Rosierd6daac42016-11-07 15:27:22 +00001469// Merge adjacent zero stores into a wider store.
1470bool AArch64LoadStoreOpt::tryToMergeZeroStInst(
Chad Rosier24c46ad2016-02-09 18:10:20 +00001471 MachineBasicBlock::iterator &MBBI) {
Chad Rosierd6daac42016-11-07 15:27:22 +00001472 assert(isPromotableZeroStoreInst(*MBBI) && "Expected narrow store.");
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001473 MachineInstr &MI = *MBBI;
1474 MachineBasicBlock::iterator E = MI.getParent()->end();
Chad Rosier24c46ad2016-02-09 18:10:20 +00001475
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001476 if (!TII->isCandidateToMergeOrPair(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001477 return false;
1478
1479 // Look ahead up to LdStLimit instructions for a mergable instruction.
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001480 LdStPairFlags Flags;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +00001481 MachineBasicBlock::iterator MergeMI =
Jun Bum Limcf974432016-03-31 14:47:24 +00001482 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ true);
Chad Rosierd7363db2016-02-09 19:09:22 +00001483 if (MergeMI != E) {
Chad Rosierd6daac42016-11-07 15:27:22 +00001484 ++NumZeroStoresPromoted;
1485
Chad Rosier24c46ad2016-02-09 18:10:20 +00001486 // Keeping the iterator straight is a pain, so we let the merge routine tell
1487 // us what the next instruction is after it's done mucking about.
Chad Rosierd6daac42016-11-07 15:27:22 +00001488 MBBI = mergeNarrowZeroStores(MBBI, MergeMI, Flags);
Chad Rosier24c46ad2016-02-09 18:10:20 +00001489 return true;
1490 }
1491 return false;
1492}
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001493
Chad Rosier24c46ad2016-02-09 18:10:20 +00001494// Find loads and stores that can be merged into a single load or store pair
1495// instruction.
1496bool AArch64LoadStoreOpt::tryToPairLdStInst(MachineBasicBlock::iterator &MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001497 MachineInstr &MI = *MBBI;
1498 MachineBasicBlock::iterator E = MI.getParent()->end();
Chad Rosier24c46ad2016-02-09 18:10:20 +00001499
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001500 if (!TII->isCandidateToMergeOrPair(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001501 return false;
1502
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001503 // Early exit if the offset is not possible to match. (6 bits of positive
1504 // range, plus allow an extra one in case we find a later insn that matches
1505 // with Offset-1)
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001506 bool IsUnscaled = TII->isUnscaledLdSt(MI);
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001507 int Offset = getLdStOffsetOp(MI).getImm();
1508 int OffsetStride = IsUnscaled ? getMemScale(MI) : 1;
Nirav Dave0f9d1112017-01-04 21:21:46 +00001509 // Allow one more for offset.
1510 if (Offset > 0)
1511 Offset -= OffsetStride;
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001512 if (!inBoundsForPair(IsUnscaled, Offset, OffsetStride))
1513 return false;
1514
Chad Rosier24c46ad2016-02-09 18:10:20 +00001515 // Look ahead up to LdStLimit instructions for a pairable instruction.
1516 LdStPairFlags Flags;
Jun Bum Limcf974432016-03-31 14:47:24 +00001517 MachineBasicBlock::iterator Paired =
1518 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ false);
Chad Rosier24c46ad2016-02-09 18:10:20 +00001519 if (Paired != E) {
1520 ++NumPairCreated;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001521 if (TII->isUnscaledLdSt(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001522 ++NumUnscaledPairCreated;
1523 // Keeping the iterator straight is a pain, so we let the merge routine tell
1524 // us what the next instruction is after it's done mucking about.
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001525 MBBI = mergePairedInsns(MBBI, Paired, Flags);
1526 return true;
1527 }
1528 return false;
1529}
1530
Jun Bum Lim22fe15e2015-11-06 16:27:47 +00001531bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB,
Chad Rosierd6daac42016-11-07 15:27:22 +00001532 bool EnableNarrowZeroStOpt) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001533 bool Modified = false;
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001534 // Four tranformations to do here:
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001535 // 1) Find loads that directly read from stores and promote them by
1536 // replacing with mov instructions. If the store is wider than the load,
1537 // the load will be replaced with a bitfield extract.
1538 // e.g.,
1539 // str w1, [x0, #4]
1540 // ldrh w2, [x0, #6]
1541 // ; becomes
1542 // str w1, [x0, #4]
NAKAMURA Takumife1202c2016-06-20 00:37:41 +00001543 // lsr w2, w1, #16
Tim Northover3b0846e2014-05-24 12:50:23 +00001544 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001545 MBBI != E;) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001546 MachineInstr &MI = *MBBI;
1547 switch (MI.getOpcode()) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001548 default:
1549 // Just move on to the next instruction.
1550 ++MBBI;
1551 break;
1552 // Scaled instructions.
1553 case AArch64::LDRBBui:
1554 case AArch64::LDRHHui:
1555 case AArch64::LDRWui:
1556 case AArch64::LDRXui:
1557 // Unscaled instructions.
1558 case AArch64::LDURBBi:
1559 case AArch64::LDURHHi:
1560 case AArch64::LDURWi:
Eugene Zelenko11f69072017-01-25 00:29:26 +00001561 case AArch64::LDURXi:
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001562 if (tryToPromoteLoadFromStore(MBBI)) {
1563 Modified = true;
1564 break;
1565 }
1566 ++MBBI;
1567 break;
1568 }
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001569 }
Chad Rosierd6daac42016-11-07 15:27:22 +00001570 // 2) Merge adjacent zero stores into a wider store.
Jun Bum Lim1de2d442016-02-05 20:02:03 +00001571 // e.g.,
1572 // strh wzr, [x0]
1573 // strh wzr, [x0, #2]
1574 // ; becomes
1575 // str wzr, [x0]
Chad Rosierd6daac42016-11-07 15:27:22 +00001576 // e.g.,
1577 // str wzr, [x0]
1578 // str wzr, [x0, #4]
1579 // ; becomes
1580 // str xzr, [x0]
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001581 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Chad Rosierd6daac42016-11-07 15:27:22 +00001582 EnableNarrowZeroStOpt && MBBI != E;) {
1583 if (isPromotableZeroStoreInst(*MBBI)) {
1584 if (tryToMergeZeroStInst(MBBI)) {
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001585 Modified = true;
Jun Bum Lim33be4992016-05-06 15:08:57 +00001586 } else
1587 ++MBBI;
1588 } else
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001589 ++MBBI;
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001590 }
Jun Bum Lim33be4992016-05-06 15:08:57 +00001591
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001592 // 3) Find loads and stores that can be merged into a single load or store
1593 // pair instruction.
1594 // e.g.,
1595 // ldr x0, [x2]
1596 // ldr x1, [x2, #8]
1597 // ; becomes
1598 // ldp x0, x1, [x2]
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001599 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Tim Northover3b0846e2014-05-24 12:50:23 +00001600 MBBI != E;) {
Geoff Berry22dfbc52016-08-12 15:26:00 +00001601 if (TII->isPairableLdStInst(*MBBI) && tryToPairLdStInst(MBBI))
1602 Modified = true;
1603 else
Tim Northover3b0846e2014-05-24 12:50:23 +00001604 ++MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001605 }
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001606 // 4) Find base register updates that can be merged into the load or store
1607 // as a base-reg writeback.
1608 // e.g.,
1609 // ldr x0, [x2]
1610 // add x2, x2, #4
1611 // ; becomes
1612 // ldr x0, [x2], #4
Tim Northover3b0846e2014-05-24 12:50:23 +00001613 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
1614 MBBI != E;) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001615 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001616 // Do update merging. It's simpler to keep this separate from the above
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001617 // switchs, though not strictly necessary.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001618 unsigned Opc = MI.getOpcode();
Tim Northover3b0846e2014-05-24 12:50:23 +00001619 switch (Opc) {
1620 default:
1621 // Just move on to the next instruction.
1622 ++MBBI;
1623 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001624 // Scaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001625 case AArch64::STRSui:
1626 case AArch64::STRDui:
1627 case AArch64::STRQui:
1628 case AArch64::STRXui:
1629 case AArch64::STRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001630 case AArch64::STRHHui:
1631 case AArch64::STRBBui:
Tim Northover3b0846e2014-05-24 12:50:23 +00001632 case AArch64::LDRSui:
1633 case AArch64::LDRDui:
1634 case AArch64::LDRQui:
1635 case AArch64::LDRXui:
1636 case AArch64::LDRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001637 case AArch64::LDRHHui:
1638 case AArch64::LDRBBui:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001639 // Unscaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001640 case AArch64::STURSi:
1641 case AArch64::STURDi:
1642 case AArch64::STURQi:
1643 case AArch64::STURWi:
1644 case AArch64::STURXi:
1645 case AArch64::LDURSi:
1646 case AArch64::LDURDi:
1647 case AArch64::LDURQi:
1648 case AArch64::LDURWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001649 case AArch64::LDURXi:
1650 // Paired instructions.
1651 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +00001652 case AArch64::LDPSWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001653 case AArch64::LDPDi:
1654 case AArch64::LDPQi:
1655 case AArch64::LDPWi:
1656 case AArch64::LDPXi:
1657 case AArch64::STPSi:
1658 case AArch64::STPDi:
1659 case AArch64::STPQi:
1660 case AArch64::STPWi:
1661 case AArch64::STPXi: {
Tim Northover3b0846e2014-05-24 12:50:23 +00001662 // Make sure this is a reg+imm (as opposed to an address reloc).
Chad Rosierf77e9092015-08-06 15:50:12 +00001663 if (!getLdStOffsetOp(MI).isImm()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001664 ++MBBI;
1665 break;
1666 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001667 // Look forward to try to form a post-index instruction. For example,
1668 // ldr x0, [x20]
1669 // add x20, x20, #32
1670 // merged into:
1671 // ldr x0, [x20], #32
Tim Northover3b0846e2014-05-24 12:50:23 +00001672 MachineBasicBlock::iterator Update =
Chad Rosier35706ad2016-02-04 21:26:02 +00001673 findMatchingUpdateInsnForward(MBBI, 0, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001674 if (Update != E) {
1675 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001676 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001677 Modified = true;
1678 ++NumPostFolded;
1679 break;
1680 }
1681 // Don't know how to handle pre/post-index versions, so move to the next
1682 // instruction.
Chad Rosiere4e15ba2016-03-09 17:29:48 +00001683 if (TII->isUnscaledLdSt(Opc)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001684 ++MBBI;
1685 break;
1686 }
1687
1688 // Look back to try to find a pre-index instruction. For example,
1689 // add x0, x0, #8
1690 // ldr x1, [x0]
1691 // merged into:
1692 // ldr x1, [x0, #8]!
Chad Rosier35706ad2016-02-04 21:26:02 +00001693 Update = findMatchingUpdateInsnBackward(MBBI, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001694 if (Update != E) {
1695 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001696 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001697 Modified = true;
1698 ++NumPreFolded;
1699 break;
1700 }
Chad Rosier7a83d772015-10-01 13:09:44 +00001701 // The immediate in the load/store is scaled by the size of the memory
1702 // operation. The immediate in the add we're looking for,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001703 // however, is not, so adjust here.
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001704 int UnscaledOffset = getLdStOffsetOp(MI).getImm() * getMemScale(MI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001705
Tim Northover3b0846e2014-05-24 12:50:23 +00001706 // Look forward to try to find a post-index instruction. For example,
1707 // ldr x1, [x0, #64]
1708 // add x0, x0, #64
1709 // merged into:
1710 // ldr x1, [x0, #64]!
Chad Rosier35706ad2016-02-04 21:26:02 +00001711 Update = findMatchingUpdateInsnForward(MBBI, UnscaledOffset, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001712 if (Update != E) {
1713 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001714 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001715 Modified = true;
1716 ++NumPreFolded;
1717 break;
1718 }
1719
1720 // Nothing found. Just move to the next instruction.
1721 ++MBBI;
1722 break;
1723 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001724 }
1725 }
1726
1727 return Modified;
1728}
1729
1730bool AArch64LoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
Andrew Kaylor1ac98bb2016-04-25 21:58:52 +00001731 if (skipFunction(*Fn.getFunction()))
1732 return false;
1733
Oliver Stannardd414c992015-11-10 11:04:18 +00001734 Subtarget = &static_cast<const AArch64Subtarget &>(Fn.getSubtarget());
1735 TII = static_cast<const AArch64InstrInfo *>(Subtarget->getInstrInfo());
1736 TRI = Subtarget->getRegisterInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +00001737
Chad Rosierbba881e2016-02-02 15:02:30 +00001738 // Resize the modified and used register bitfield trackers. We do this once
1739 // per function and then clear the bitfield each time we optimize a load or
1740 // store.
1741 ModifiedRegs.resize(TRI->getNumRegs());
1742 UsedRegs.resize(TRI->getNumRegs());
1743
Tim Northover3b0846e2014-05-24 12:50:23 +00001744 bool Modified = false;
Chad Rosier10c7aaa2016-11-11 14:10:12 +00001745 bool enableNarrowZeroStOpt = !Subtarget->requiresStrictAlign();
Tim Northover3b0846e2014-05-24 12:50:23 +00001746 for (auto &MBB : Fn)
Chad Rosierd6daac42016-11-07 15:27:22 +00001747 Modified |= optimizeBlock(MBB, enableNarrowZeroStOpt);
Tim Northover3b0846e2014-05-24 12:50:23 +00001748
1749 return Modified;
1750}
1751
Chad Rosier8ade0342016-11-11 19:52:45 +00001752// FIXME: Do we need/want a pre-alloc pass like ARM has to try to keep loads and
1753// stores near one another? Note: The pre-RA instruction scheduler already has
1754// hooks to try and schedule pairable loads/stores together to improve pairing
1755// opportunities. Thus, pre-RA pairing pass may not be worth the effort.
Tim Northover3b0846e2014-05-24 12:50:23 +00001756
Chad Rosier3f8b09d2016-02-09 19:42:19 +00001757// FIXME: When pairing store instructions it's very possible for this pass to
1758// hoist a store with a KILL marker above another use (without a KILL marker).
1759// The resulting IR is invalid, but nothing uses the KILL markers after this
1760// pass, so it's never caused a problem in practice.
1761
Chad Rosier43f5c842015-08-05 12:40:13 +00001762/// createAArch64LoadStoreOptimizationPass - returns an instance of the
1763/// load / store optimization pass.
Tim Northover3b0846e2014-05-24 12:50:23 +00001764FunctionPass *llvm::createAArch64LoadStoreOptimizationPass() {
1765 return new AArch64LoadStoreOpt();
1766}