blob: c45c0b4c8ed7ba3142ad073e82cf20d189861f5d [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//=- AArch64LoadStoreOptimizer.cpp - AArch64 load/store opt. pass -*- C++ -*-=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a pass that performs load / store related peephole
11// optimizations. This pass should be run after register allocation.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AArch64InstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000016#include "AArch64Subtarget.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000017#include "MCTargetDesc/AArch64AddressingModes.h"
18#include "llvm/ADT/BitVector.h"
Chad Rosierce8e5ab2015-05-21 21:36:46 +000019#include "llvm/ADT/SmallVector.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000020#include "llvm/ADT/Statistic.h"
Eugene Zelenko11f69072017-01-25 00:29:26 +000021#include "llvm/ADT/StringRef.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000022#include "llvm/ADT/iterator_range.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
Chad Rosiera69dcb62017-03-17 14:19:55 +000096 AliasAnalysis *AA;
Tim Northover3b0846e2014-05-24 12:50:23 +000097 const AArch64InstrInfo *TII;
98 const TargetRegisterInfo *TRI;
Oliver Stannardd414c992015-11-10 11:04:18 +000099 const AArch64Subtarget *Subtarget;
Tim Northover3b0846e2014-05-24 12:50:23 +0000100
Chad Rosierbba881e2016-02-02 15:02:30 +0000101 // Track which registers have been modified and used.
102 BitVector ModifiedRegs, UsedRegs;
103
Chad Rosiera69dcb62017-03-17 14:19:55 +0000104 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
105 AU.addRequired<AAResultsWrapperPass>();
106 MachineFunctionPass::getAnalysisUsage(AU);
107 }
108
Tim Northover3b0846e2014-05-24 12:50:23 +0000109 // Scan the instructions looking for a load/store that can be combined
110 // with the current instruction into a load/store pair.
111 // Return the matching instruction if one is found, else MBB->end().
Tim Northover3b0846e2014-05-24 12:50:23 +0000112 MachineBasicBlock::iterator findMatchingInsn(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +0000113 LdStPairFlags &Flags,
Jun Bum Limcf974432016-03-31 14:47:24 +0000114 unsigned Limit,
115 bool FindNarrowMerge);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000116
117 // Scan the instructions looking for a store that writes to the address from
118 // which the current load instruction reads. Return true if one is found.
119 bool findMatchingStore(MachineBasicBlock::iterator I, unsigned Limit,
120 MachineBasicBlock::iterator &StoreI);
121
Chad Rosierd6daac42016-11-07 15:27:22 +0000122 // Merge the two instructions indicated into a wider narrow store instruction.
Chad Rosierb5933d72016-02-09 19:02:12 +0000123 MachineBasicBlock::iterator
Chad Rosierd6daac42016-11-07 15:27:22 +0000124 mergeNarrowZeroStores(MachineBasicBlock::iterator I,
125 MachineBasicBlock::iterator MergeMI,
126 const LdStPairFlags &Flags);
Chad Rosierb5933d72016-02-09 19:02:12 +0000127
Tim Northover3b0846e2014-05-24 12:50:23 +0000128 // Merge the two instructions indicated into a single pair-wise instruction.
Tim Northover3b0846e2014-05-24 12:50:23 +0000129 MachineBasicBlock::iterator
130 mergePairedInsns(MachineBasicBlock::iterator I,
Chad Rosier96a18a92015-07-21 17:42:04 +0000131 MachineBasicBlock::iterator Paired,
Chad Rosierfe5399f2015-07-21 17:47:56 +0000132 const LdStPairFlags &Flags);
Tim Northover3b0846e2014-05-24 12:50:23 +0000133
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000134 // Promote the load that reads directly from the address stored to.
135 MachineBasicBlock::iterator
136 promoteLoadFromStore(MachineBasicBlock::iterator LoadI,
137 MachineBasicBlock::iterator StoreI);
138
Tim Northover3b0846e2014-05-24 12:50:23 +0000139 // Scan the instruction list to find a base register update that can
140 // be combined with the current instruction (a load or store) using
141 // pre or post indexed addressing with writeback. Scan forwards.
142 MachineBasicBlock::iterator
Chad Rosier234bf6f2016-01-18 21:56:40 +0000143 findMatchingUpdateInsnForward(MachineBasicBlock::iterator I,
Chad Rosier35706ad2016-02-04 21:26:02 +0000144 int UnscaledOffset, unsigned Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +0000145
146 // Scan the instruction list to find a base register update that can
147 // be combined with the current instruction (a load or store) using
148 // pre or post indexed addressing with writeback. Scan backwards.
149 MachineBasicBlock::iterator
Chad Rosier35706ad2016-02-04 21:26:02 +0000150 findMatchingUpdateInsnBackward(MachineBasicBlock::iterator I, unsigned Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +0000151
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000152 // Find an instruction that updates the base register of the ld/st
153 // instruction.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000154 bool isMatchingUpdateInsn(MachineInstr &MemMI, MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000155 unsigned BaseReg, int Offset);
156
Chad Rosier2dfd3542015-09-23 13:51:44 +0000157 // Merge a pre- or post-index base register update into a ld/st instruction.
Tim Northover3b0846e2014-05-24 12:50:23 +0000158 MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +0000159 mergeUpdateInsn(MachineBasicBlock::iterator I,
160 MachineBasicBlock::iterator Update, bool IsPreIdx);
Tim Northover3b0846e2014-05-24 12:50:23 +0000161
Chad Rosierd6daac42016-11-07 15:27:22 +0000162 // Find and merge zero store instructions.
163 bool tryToMergeZeroStInst(MachineBasicBlock::iterator &MBBI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +0000164
Chad Rosier24c46ad2016-02-09 18:10:20 +0000165 // Find and pair ldr/str instructions.
166 bool tryToPairLdStInst(MachineBasicBlock::iterator &MBBI);
167
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000168 // Find and promote load instructions which read directly from store.
169 bool tryToPromoteLoadFromStore(MachineBasicBlock::iterator &MBBI);
170
Chad Rosierd6daac42016-11-07 15:27:22 +0000171 bool optimizeBlock(MachineBasicBlock &MBB, bool EnableNarrowZeroStOpt);
Tim Northover3b0846e2014-05-24 12:50:23 +0000172
173 bool runOnMachineFunction(MachineFunction &Fn) override;
174
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000175 MachineFunctionProperties getRequiredProperties() const override {
176 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000177 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000178 }
179
Mehdi Amini117296c2016-10-01 02:56:57 +0000180 StringRef getPassName() const override { return AARCH64_LOAD_STORE_OPT_NAME; }
Tim Northover3b0846e2014-05-24 12:50:23 +0000181};
Eugene Zelenko11f69072017-01-25 00:29:26 +0000182
Tim Northover3b0846e2014-05-24 12:50:23 +0000183char AArch64LoadStoreOpt::ID = 0;
Eugene Zelenko11f69072017-01-25 00:29:26 +0000184
185} // end anonymous namespace
Tim Northover3b0846e2014-05-24 12:50:23 +0000186
Chad Rosier96530b32015-08-05 13:44:51 +0000187INITIALIZE_PASS(AArch64LoadStoreOpt, "aarch64-ldst-opt",
188 AARCH64_LOAD_STORE_OPT_NAME, false, false)
189
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000190static bool isNarrowStore(unsigned Opc) {
191 switch (Opc) {
192 default:
193 return false;
194 case AArch64::STRBBui:
195 case AArch64::STURBBi:
196 case AArch64::STRHHui:
197 case AArch64::STURHHi:
198 return true;
199 }
200}
201
Chad Rosier32d4d372015-09-29 16:07:32 +0000202// Scaling factor for unscaled load or store.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000203static int getMemScale(MachineInstr &MI) {
204 switch (MI.getOpcode()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000205 default:
Chad Rosierdabe2532015-09-29 18:26:15 +0000206 llvm_unreachable("Opcode has unknown scale!");
207 case AArch64::LDRBBui:
Jun Bum Lim4c35cca2015-11-19 17:21:41 +0000208 case AArch64::LDURBBi:
209 case AArch64::LDRSBWui:
210 case AArch64::LDURSBWi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000211 case AArch64::STRBBui:
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000212 case AArch64::STURBBi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000213 return 1;
214 case AArch64::LDRHHui:
Jun Bum Limc9879ec2015-10-27 19:16:03 +0000215 case AArch64::LDURHHi:
Jun Bum Lim4c35cca2015-11-19 17:21:41 +0000216 case AArch64::LDRSHWui:
217 case AArch64::LDURSHWi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000218 case AArch64::STRHHui:
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000219 case AArch64::STURHHi:
Chad Rosierdabe2532015-09-29 18:26:15 +0000220 return 2;
Chad Rosiera4d32172015-09-29 14:57:10 +0000221 case AArch64::LDRSui:
222 case AArch64::LDURSi:
223 case AArch64::LDRSWui:
224 case AArch64::LDURSWi:
225 case AArch64::LDRWui:
226 case AArch64::LDURWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000227 case AArch64::STRSui:
228 case AArch64::STURSi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000229 case AArch64::STRWui:
230 case AArch64::STURWi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000231 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +0000232 case AArch64::LDPSWi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000233 case AArch64::LDPWi:
234 case AArch64::STPSi:
235 case AArch64::STPWi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000236 return 4;
Chad Rosiera4d32172015-09-29 14:57:10 +0000237 case AArch64::LDRDui:
238 case AArch64::LDURDi:
239 case AArch64::LDRXui:
240 case AArch64::LDURXi:
241 case AArch64::STRDui:
242 case AArch64::STURDi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000243 case AArch64::STRXui:
244 case AArch64::STURXi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000245 case AArch64::LDPDi:
246 case AArch64::LDPXi:
247 case AArch64::STPDi:
248 case AArch64::STPXi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000249 return 8;
Tim Northover3b0846e2014-05-24 12:50:23 +0000250 case AArch64::LDRQui:
251 case AArch64::LDURQi:
Chad Rosiera4d32172015-09-29 14:57:10 +0000252 case AArch64::STRQui:
253 case AArch64::STURQi:
Chad Rosier32d4d372015-09-29 16:07:32 +0000254 case AArch64::LDPQi:
255 case AArch64::STPQi:
Tim Northover3b0846e2014-05-24 12:50:23 +0000256 return 16;
Tim Northover3b0846e2014-05-24 12:50:23 +0000257 }
258}
259
Quentin Colombet66b61632015-03-06 22:42:10 +0000260static unsigned getMatchingNonSExtOpcode(unsigned Opc,
261 bool *IsValidLdStrOpc = nullptr) {
262 if (IsValidLdStrOpc)
263 *IsValidLdStrOpc = true;
264 switch (Opc) {
265 default:
266 if (IsValidLdStrOpc)
267 *IsValidLdStrOpc = false;
Eugene Zelenko11f69072017-01-25 00:29:26 +0000268 return std::numeric_limits<unsigned>::max();
Quentin Colombet66b61632015-03-06 22:42:10 +0000269 case AArch64::STRDui:
270 case AArch64::STURDi:
271 case AArch64::STRQui:
272 case AArch64::STURQi:
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000273 case AArch64::STRBBui:
274 case AArch64::STURBBi:
275 case AArch64::STRHHui:
276 case AArch64::STURHHi:
Quentin Colombet66b61632015-03-06 22:42:10 +0000277 case AArch64::STRWui:
278 case AArch64::STURWi:
279 case AArch64::STRXui:
280 case AArch64::STURXi:
281 case AArch64::LDRDui:
282 case AArch64::LDURDi:
283 case AArch64::LDRQui:
284 case AArch64::LDURQi:
285 case AArch64::LDRWui:
286 case AArch64::LDURWi:
287 case AArch64::LDRXui:
288 case AArch64::LDURXi:
289 case AArch64::STRSui:
290 case AArch64::STURSi:
291 case AArch64::LDRSui:
292 case AArch64::LDURSi:
293 return Opc;
294 case AArch64::LDRSWui:
295 return AArch64::LDRWui;
296 case AArch64::LDURSWi:
297 return AArch64::LDURWi;
298 }
299}
300
Jun Bum Lim1de2d442016-02-05 20:02:03 +0000301static unsigned getMatchingWideOpcode(unsigned Opc) {
302 switch (Opc) {
303 default:
304 llvm_unreachable("Opcode has no wide equivalent!");
305 case AArch64::STRBBui:
306 return AArch64::STRHHui;
307 case AArch64::STRHHui:
308 return AArch64::STRWui;
309 case AArch64::STURBBi:
310 return AArch64::STURHHi;
311 case AArch64::STURHHi:
312 return AArch64::STURWi;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +0000313 case AArch64::STURWi:
314 return AArch64::STURXi;
315 case AArch64::STRWui:
316 return AArch64::STRXui;
Jun Bum Lim1de2d442016-02-05 20:02:03 +0000317 }
318}
319
Tim Northover3b0846e2014-05-24 12:50:23 +0000320static unsigned getMatchingPairOpcode(unsigned Opc) {
321 switch (Opc) {
322 default:
323 llvm_unreachable("Opcode has no pairwise equivalent!");
324 case AArch64::STRSui:
325 case AArch64::STURSi:
326 return AArch64::STPSi;
327 case AArch64::STRDui:
328 case AArch64::STURDi:
329 return AArch64::STPDi;
330 case AArch64::STRQui:
331 case AArch64::STURQi:
332 return AArch64::STPQi;
333 case AArch64::STRWui:
334 case AArch64::STURWi:
335 return AArch64::STPWi;
336 case AArch64::STRXui:
337 case AArch64::STURXi:
338 return AArch64::STPXi;
339 case AArch64::LDRSui:
340 case AArch64::LDURSi:
341 return AArch64::LDPSi;
342 case AArch64::LDRDui:
343 case AArch64::LDURDi:
344 return AArch64::LDPDi;
345 case AArch64::LDRQui:
346 case AArch64::LDURQi:
347 return AArch64::LDPQi;
348 case AArch64::LDRWui:
349 case AArch64::LDURWi:
350 return AArch64::LDPWi;
351 case AArch64::LDRXui:
352 case AArch64::LDURXi:
353 return AArch64::LDPXi;
Quentin Colombet29f55332015-01-24 01:25:54 +0000354 case AArch64::LDRSWui:
355 case AArch64::LDURSWi:
356 return AArch64::LDPSWi;
Tim Northover3b0846e2014-05-24 12:50:23 +0000357 }
358}
359
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000360static unsigned isMatchingStore(MachineInstr &LoadInst,
361 MachineInstr &StoreInst) {
362 unsigned LdOpc = LoadInst.getOpcode();
363 unsigned StOpc = StoreInst.getOpcode();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000364 switch (LdOpc) {
365 default:
366 llvm_unreachable("Unsupported load instruction!");
367 case AArch64::LDRBBui:
368 return StOpc == AArch64::STRBBui || StOpc == AArch64::STRHHui ||
369 StOpc == AArch64::STRWui || StOpc == AArch64::STRXui;
370 case AArch64::LDURBBi:
371 return StOpc == AArch64::STURBBi || StOpc == AArch64::STURHHi ||
372 StOpc == AArch64::STURWi || StOpc == AArch64::STURXi;
373 case AArch64::LDRHHui:
374 return StOpc == AArch64::STRHHui || StOpc == AArch64::STRWui ||
375 StOpc == AArch64::STRXui;
376 case AArch64::LDURHHi:
377 return StOpc == AArch64::STURHHi || StOpc == AArch64::STURWi ||
378 StOpc == AArch64::STURXi;
379 case AArch64::LDRWui:
380 return StOpc == AArch64::STRWui || StOpc == AArch64::STRXui;
381 case AArch64::LDURWi:
382 return StOpc == AArch64::STURWi || StOpc == AArch64::STURXi;
383 case AArch64::LDRXui:
384 return StOpc == AArch64::STRXui;
385 case AArch64::LDURXi:
386 return StOpc == AArch64::STURXi;
387 }
388}
389
Tim Northover3b0846e2014-05-24 12:50:23 +0000390static unsigned getPreIndexedOpcode(unsigned Opc) {
391 switch (Opc) {
392 default:
393 llvm_unreachable("Opcode has no pre-indexed equivalent!");
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000394 case AArch64::STRSui:
395 return AArch64::STRSpre;
396 case AArch64::STRDui:
397 return AArch64::STRDpre;
398 case AArch64::STRQui:
399 return AArch64::STRQpre;
Chad Rosierdabe2532015-09-29 18:26:15 +0000400 case AArch64::STRBBui:
401 return AArch64::STRBBpre;
402 case AArch64::STRHHui:
403 return AArch64::STRHHpre;
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000404 case AArch64::STRWui:
405 return AArch64::STRWpre;
406 case AArch64::STRXui:
407 return AArch64::STRXpre;
408 case AArch64::LDRSui:
409 return AArch64::LDRSpre;
410 case AArch64::LDRDui:
411 return AArch64::LDRDpre;
412 case AArch64::LDRQui:
413 return AArch64::LDRQpre;
Chad Rosierdabe2532015-09-29 18:26:15 +0000414 case AArch64::LDRBBui:
415 return AArch64::LDRBBpre;
416 case AArch64::LDRHHui:
417 return AArch64::LDRHHpre;
Tilmann Scheller5d8d72c2014-06-04 12:40:35 +0000418 case AArch64::LDRWui:
419 return AArch64::LDRWpre;
420 case AArch64::LDRXui:
421 return AArch64::LDRXpre;
Quentin Colombet29f55332015-01-24 01:25:54 +0000422 case AArch64::LDRSWui:
423 return AArch64::LDRSWpre;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000424 case AArch64::LDPSi:
425 return AArch64::LDPSpre;
Chad Rosier43150122015-09-29 20:39:55 +0000426 case AArch64::LDPSWi:
427 return AArch64::LDPSWpre;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000428 case AArch64::LDPDi:
429 return AArch64::LDPDpre;
430 case AArch64::LDPQi:
431 return AArch64::LDPQpre;
432 case AArch64::LDPWi:
433 return AArch64::LDPWpre;
434 case AArch64::LDPXi:
435 return AArch64::LDPXpre;
436 case AArch64::STPSi:
437 return AArch64::STPSpre;
438 case AArch64::STPDi:
439 return AArch64::STPDpre;
440 case AArch64::STPQi:
441 return AArch64::STPQpre;
442 case AArch64::STPWi:
443 return AArch64::STPWpre;
444 case AArch64::STPXi:
445 return AArch64::STPXpre;
Tim Northover3b0846e2014-05-24 12:50:23 +0000446 }
447}
448
449static unsigned getPostIndexedOpcode(unsigned Opc) {
450 switch (Opc) {
451 default:
452 llvm_unreachable("Opcode has no post-indexed wise equivalent!");
453 case AArch64::STRSui:
454 return AArch64::STRSpost;
455 case AArch64::STRDui:
456 return AArch64::STRDpost;
457 case AArch64::STRQui:
458 return AArch64::STRQpost;
Chad Rosierdabe2532015-09-29 18:26:15 +0000459 case AArch64::STRBBui:
460 return AArch64::STRBBpost;
461 case AArch64::STRHHui:
462 return AArch64::STRHHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000463 case AArch64::STRWui:
464 return AArch64::STRWpost;
465 case AArch64::STRXui:
466 return AArch64::STRXpost;
467 case AArch64::LDRSui:
468 return AArch64::LDRSpost;
469 case AArch64::LDRDui:
470 return AArch64::LDRDpost;
471 case AArch64::LDRQui:
472 return AArch64::LDRQpost;
Chad Rosierdabe2532015-09-29 18:26:15 +0000473 case AArch64::LDRBBui:
474 return AArch64::LDRBBpost;
475 case AArch64::LDRHHui:
476 return AArch64::LDRHHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000477 case AArch64::LDRWui:
478 return AArch64::LDRWpost;
479 case AArch64::LDRXui:
480 return AArch64::LDRXpost;
Quentin Colombet29f55332015-01-24 01:25:54 +0000481 case AArch64::LDRSWui:
482 return AArch64::LDRSWpost;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000483 case AArch64::LDPSi:
484 return AArch64::LDPSpost;
Chad Rosier43150122015-09-29 20:39:55 +0000485 case AArch64::LDPSWi:
486 return AArch64::LDPSWpost;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000487 case AArch64::LDPDi:
488 return AArch64::LDPDpost;
489 case AArch64::LDPQi:
490 return AArch64::LDPQpost;
491 case AArch64::LDPWi:
492 return AArch64::LDPWpost;
493 case AArch64::LDPXi:
494 return AArch64::LDPXpost;
495 case AArch64::STPSi:
496 return AArch64::STPSpost;
497 case AArch64::STPDi:
498 return AArch64::STPDpost;
499 case AArch64::STPQi:
500 return AArch64::STPQpost;
501 case AArch64::STPWi:
502 return AArch64::STPWpost;
503 case AArch64::STPXi:
504 return AArch64::STPXpost;
Tim Northover3b0846e2014-05-24 12:50:23 +0000505 }
506}
507
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000508static bool isPairedLdSt(const MachineInstr &MI) {
509 switch (MI.getOpcode()) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000510 default:
511 return false;
512 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +0000513 case AArch64::LDPSWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000514 case AArch64::LDPDi:
515 case AArch64::LDPQi:
516 case AArch64::LDPWi:
517 case AArch64::LDPXi:
518 case AArch64::STPSi:
519 case AArch64::STPDi:
520 case AArch64::STPQi:
521 case AArch64::STPWi:
522 case AArch64::STPXi:
523 return true;
524 }
525}
526
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000527static const MachineOperand &getLdStRegOp(const MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000528 unsigned PairedRegOp = 0) {
529 assert(PairedRegOp < 2 && "Unexpected register operand idx.");
530 unsigned Idx = isPairedLdSt(MI) ? PairedRegOp : 0;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000531 return MI.getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000532}
533
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000534static const MachineOperand &getLdStBaseOp(const MachineInstr &MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000535 unsigned Idx = isPairedLdSt(MI) ? 2 : 1;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000536 return MI.getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000537}
538
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000539static const MachineOperand &getLdStOffsetOp(const MachineInstr &MI) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +0000540 unsigned Idx = isPairedLdSt(MI) ? 3 : 2;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000541 return MI.getOperand(Idx);
Chad Rosierf77e9092015-08-06 15:50:12 +0000542}
543
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000544static bool isLdOffsetInRangeOfSt(MachineInstr &LoadInst,
545 MachineInstr &StoreInst,
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000546 const AArch64InstrInfo *TII) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000547 assert(isMatchingStore(LoadInst, StoreInst) && "Expect only matched ld/st.");
548 int LoadSize = getMemScale(LoadInst);
549 int StoreSize = getMemScale(StoreInst);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000550 int UnscaledStOffset = TII->isUnscaledLdSt(StoreInst)
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000551 ? getLdStOffsetOp(StoreInst).getImm()
552 : getLdStOffsetOp(StoreInst).getImm() * StoreSize;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000553 int UnscaledLdOffset = TII->isUnscaledLdSt(LoadInst)
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000554 ? getLdStOffsetOp(LoadInst).getImm()
555 : getLdStOffsetOp(LoadInst).getImm() * LoadSize;
556 return (UnscaledStOffset <= UnscaledLdOffset) &&
557 (UnscaledLdOffset + LoadSize <= (UnscaledStOffset + StoreSize));
558}
559
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000560static bool isPromotableZeroStoreInst(MachineInstr &MI) {
Chad Rosierd6daac42016-11-07 15:27:22 +0000561 unsigned Opc = MI.getOpcode();
562 return (Opc == AArch64::STRWui || Opc == AArch64::STURWi ||
563 isNarrowStore(Opc)) &&
Jun Bum Lim397eb7b2016-02-12 15:25:39 +0000564 getLdStRegOp(MI).getReg() == AArch64::WZR;
565}
566
Tim Northover3b0846e2014-05-24 12:50:23 +0000567MachineBasicBlock::iterator
Chad Rosierd6daac42016-11-07 15:27:22 +0000568AArch64LoadStoreOpt::mergeNarrowZeroStores(MachineBasicBlock::iterator I,
569 MachineBasicBlock::iterator MergeMI,
570 const LdStPairFlags &Flags) {
571 assert(isPromotableZeroStoreInst(*I) && isPromotableZeroStoreInst(*MergeMI) &&
572 "Expected promotable zero stores.");
573
Tim Northover3b0846e2014-05-24 12:50:23 +0000574 MachineBasicBlock::iterator NextI = I;
575 ++NextI;
576 // If NextI is the second of the two instructions to be merged, we need
577 // to skip one further. Either way we merge will invalidate the iterator,
578 // and we don't need to scan the new instruction, as it's a pairwise
579 // instruction, which we're not considering for further action anyway.
Chad Rosierd7363db2016-02-09 19:09:22 +0000580 if (NextI == MergeMI)
Tim Northover3b0846e2014-05-24 12:50:23 +0000581 ++NextI;
582
Chad Rosierb5933d72016-02-09 19:02:12 +0000583 unsigned Opc = I->getOpcode();
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000584 bool IsScaled = !TII->isUnscaledLdSt(Opc);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000585 int OffsetStride = IsScaled ? 1 : getMemScale(*I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000586
Chad Rosier96a18a92015-07-21 17:42:04 +0000587 bool MergeForward = Flags.getMergeForward();
Tim Northover3b0846e2014-05-24 12:50:23 +0000588 // Insert our new paired instruction after whichever of the paired
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000589 // instructions MergeForward indicates.
Chad Rosierd7363db2016-02-09 19:09:22 +0000590 MachineBasicBlock::iterator InsertionPoint = MergeForward ? MergeMI : I;
Tilmann Scheller4aad3bd2014-06-04 12:36:28 +0000591 // Also based on MergeForward is from where we copy the base register operand
Tim Northover3b0846e2014-05-24 12:50:23 +0000592 // so we get the flags compatible with the input code.
Chad Rosierf77e9092015-08-06 15:50:12 +0000593 const MachineOperand &BaseRegOp =
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000594 MergeForward ? getLdStBaseOp(*MergeMI) : getLdStBaseOp(*I);
Tim Northover3b0846e2014-05-24 12:50:23 +0000595
596 // Which register is Rt and which is Rt2 depends on the offset order.
Davide Italiano5df60662016-11-07 19:11:25 +0000597 MachineInstr *RtMI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000598 if (getLdStOffsetOp(*I).getImm() ==
Davide Italiano5df60662016-11-07 19:11:25 +0000599 getLdStOffsetOp(*MergeMI).getImm() + OffsetStride)
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000600 RtMI = &*MergeMI;
Davide Italiano5df60662016-11-07 19:11:25 +0000601 else
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000602 RtMI = &*I;
Jun Bum Limc9879ec2015-10-27 19:16:03 +0000603
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000604 int OffsetImm = getLdStOffsetOp(*RtMI).getImm();
Chad Rosier11eedc92016-02-09 19:17:18 +0000605 // Change the scaled offset from small to large type.
606 if (IsScaled) {
607 assert(((OffsetImm & 1) == 0) && "Unexpected offset to merge");
608 OffsetImm /= 2;
609 }
610
Chad Rosierd6daac42016-11-07 15:27:22 +0000611 // Construct the new instruction.
Chad Rosierc46ef882016-02-09 19:33:42 +0000612 DebugLoc DL = I->getDebugLoc();
613 MachineBasicBlock *MBB = I->getParent();
Jun Bum Lim80ec0d32015-11-20 21:14:07 +0000614 MachineInstrBuilder MIB;
Chad Rosierc46ef882016-02-09 19:33:42 +0000615 MIB = BuildMI(*MBB, InsertionPoint, DL, TII->get(getMatchingWideOpcode(Opc)))
Jun Bum Lim397eb7b2016-02-12 15:25:39 +0000616 .addReg(isNarrowStore(Opc) ? AArch64::WZR : AArch64::XZR)
Diana Picus116bbab2017-01-13 09:58:52 +0000617 .add(BaseRegOp)
Chad Rosierb5933d72016-02-09 19:02:12 +0000618 .addImm(OffsetImm)
Chad Rosierd7363db2016-02-09 19:09:22 +0000619 .setMemRefs(I->mergeMemRefsWith(*MergeMI));
Tim Northover3b0846e2014-05-24 12:50:23 +0000620 (void)MIB;
621
Chad Rosierd6daac42016-11-07 15:27:22 +0000622 DEBUG(dbgs() << "Creating wider store. Replacing instructions:\n ");
Chad Rosierb5933d72016-02-09 19:02:12 +0000623 DEBUG(I->print(dbgs()));
624 DEBUG(dbgs() << " ");
Chad Rosierd7363db2016-02-09 19:09:22 +0000625 DEBUG(MergeMI->print(dbgs()));
Chad Rosierb5933d72016-02-09 19:02:12 +0000626 DEBUG(dbgs() << " with instruction:\n ");
627 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
628 DEBUG(dbgs() << "\n");
629
630 // Erase the old instructions.
631 I->eraseFromParent();
Chad Rosierd7363db2016-02-09 19:09:22 +0000632 MergeMI->eraseFromParent();
Chad Rosierb5933d72016-02-09 19:02:12 +0000633 return NextI;
634}
635
636MachineBasicBlock::iterator
637AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
638 MachineBasicBlock::iterator Paired,
639 const LdStPairFlags &Flags) {
640 MachineBasicBlock::iterator NextI = I;
641 ++NextI;
642 // If NextI is the second of the two instructions to be merged, we need
643 // to skip one further. Either way we merge will invalidate the iterator,
644 // and we don't need to scan the new instruction, as it's a pairwise
645 // instruction, which we're not considering for further action anyway.
646 if (NextI == Paired)
647 ++NextI;
648
649 int SExtIdx = Flags.getSExtIdx();
650 unsigned Opc =
651 SExtIdx == -1 ? I->getOpcode() : getMatchingNonSExtOpcode(I->getOpcode());
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000652 bool IsUnscaled = TII->isUnscaledLdSt(Opc);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000653 int OffsetStride = IsUnscaled ? getMemScale(*I) : 1;
Chad Rosierb5933d72016-02-09 19:02:12 +0000654
655 bool MergeForward = Flags.getMergeForward();
656 // Insert our new paired instruction after whichever of the paired
657 // instructions MergeForward indicates.
658 MachineBasicBlock::iterator InsertionPoint = MergeForward ? Paired : I;
659 // Also based on MergeForward is from where we copy the base register operand
660 // so we get the flags compatible with the input code.
661 const MachineOperand &BaseRegOp =
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000662 MergeForward ? getLdStBaseOp(*Paired) : getLdStBaseOp(*I);
Chad Rosierb5933d72016-02-09 19:02:12 +0000663
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000664 int Offset = getLdStOffsetOp(*I).getImm();
665 int PairedOffset = getLdStOffsetOp(*Paired).getImm();
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000666 bool PairedIsUnscaled = TII->isUnscaledLdSt(Paired->getOpcode());
Chad Rosier00f9d232016-02-11 14:25:08 +0000667 if (IsUnscaled != PairedIsUnscaled) {
668 // We're trying to pair instructions that differ in how they are scaled. If
669 // I is scaled then scale the offset of Paired accordingly. Otherwise, do
670 // the opposite (i.e., make Paired's offset unscaled).
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000671 int MemSize = getMemScale(*Paired);
Chad Rosier00f9d232016-02-11 14:25:08 +0000672 if (PairedIsUnscaled) {
673 // If the unscaled offset isn't a multiple of the MemSize, we can't
674 // pair the operations together.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000675 assert(!(PairedOffset % getMemScale(*Paired)) &&
Chad Rosier00f9d232016-02-11 14:25:08 +0000676 "Offset should be a multiple of the stride!");
677 PairedOffset /= MemSize;
678 } else {
679 PairedOffset *= MemSize;
680 }
681 }
682
Chad Rosierb5933d72016-02-09 19:02:12 +0000683 // Which register is Rt and which is Rt2 depends on the offset order.
684 MachineInstr *RtMI, *Rt2MI;
Chad Rosier00f9d232016-02-11 14:25:08 +0000685 if (Offset == PairedOffset + OffsetStride) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000686 RtMI = &*Paired;
687 Rt2MI = &*I;
Chad Rosierb5933d72016-02-09 19:02:12 +0000688 // Here we swapped the assumption made for SExtIdx.
689 // I.e., we turn ldp I, Paired into ldp Paired, I.
690 // Update the index accordingly.
691 if (SExtIdx != -1)
692 SExtIdx = (SExtIdx + 1) % 2;
693 } else {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000694 RtMI = &*I;
695 Rt2MI = &*Paired;
Chad Rosierb5933d72016-02-09 19:02:12 +0000696 }
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000697 int OffsetImm = getLdStOffsetOp(*RtMI).getImm();
Chad Rosier00f9d232016-02-11 14:25:08 +0000698 // Scale the immediate offset, if necessary.
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000699 if (TII->isUnscaledLdSt(RtMI->getOpcode())) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000700 assert(!(OffsetImm % getMemScale(*RtMI)) &&
Chad Rosier00f9d232016-02-11 14:25:08 +0000701 "Unscaled offset cannot be scaled.");
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000702 OffsetImm /= getMemScale(*RtMI);
Chad Rosier87e33412016-02-09 20:18:07 +0000703 }
Chad Rosierb5933d72016-02-09 19:02:12 +0000704
705 // Construct the new instruction.
706 MachineInstrBuilder MIB;
Chad Rosierc46ef882016-02-09 19:33:42 +0000707 DebugLoc DL = I->getDebugLoc();
708 MachineBasicBlock *MBB = I->getParent();
Matthias Braun2e8c11e2017-01-20 18:04:27 +0000709 MachineOperand RegOp0 = getLdStRegOp(*RtMI);
710 MachineOperand RegOp1 = getLdStRegOp(*Rt2MI);
711 // Kill flags may become invalid when moving stores for pairing.
712 if (RegOp0.isUse()) {
713 if (!MergeForward) {
714 // Clear kill flags on store if moving upwards. Example:
715 // STRWui %w0, ...
716 // USE %w1
717 // STRWui kill %w1 ; need to clear kill flag when moving STRWui upwards
718 RegOp0.setIsKill(false);
719 RegOp1.setIsKill(false);
720 } else {
721 // Clear kill flags of the first stores register. Example:
722 // STRWui %w1, ...
723 // USE kill %w1 ; need to clear kill flag when moving STRWui downwards
724 // STRW %w0
725 unsigned Reg = getLdStRegOp(*I).getReg();
726 for (MachineInstr &MI : make_range(std::next(I), Paired))
727 MI.clearRegisterKills(Reg, TRI);
728 }
729 }
Chad Rosierc46ef882016-02-09 19:33:42 +0000730 MIB = BuildMI(*MBB, InsertionPoint, DL, TII->get(getMatchingPairOpcode(Opc)))
Matthias Braun2e8c11e2017-01-20 18:04:27 +0000731 .add(RegOp0)
732 .add(RegOp1)
Diana Picus116bbab2017-01-13 09:58:52 +0000733 .add(BaseRegOp)
Chad Rosiere40b9512016-03-08 17:16:38 +0000734 .addImm(OffsetImm)
735 .setMemRefs(I->mergeMemRefsWith(*Paired));
Chad Rosierb5933d72016-02-09 19:02:12 +0000736
737 (void)MIB;
Tim Northover3b0846e2014-05-24 12:50:23 +0000738
739 DEBUG(dbgs() << "Creating pair load/store. Replacing instructions:\n ");
740 DEBUG(I->print(dbgs()));
741 DEBUG(dbgs() << " ");
742 DEBUG(Paired->print(dbgs()));
743 DEBUG(dbgs() << " with instruction:\n ");
Quentin Colombet66b61632015-03-06 22:42:10 +0000744 if (SExtIdx != -1) {
745 // Generate the sign extension for the proper result of the ldp.
746 // I.e., with X1, that would be:
747 // %W1<def> = KILL %W1, %X1<imp-def>
748 // %X1<def> = SBFMXri %X1<kill>, 0, 31
749 MachineOperand &DstMO = MIB->getOperand(SExtIdx);
750 // Right now, DstMO has the extended register, since it comes from an
751 // extended opcode.
752 unsigned DstRegX = DstMO.getReg();
753 // Get the W variant of that register.
754 unsigned DstRegW = TRI->getSubReg(DstRegX, AArch64::sub_32);
755 // Update the result of LDP to use the W instead of the X variant.
756 DstMO.setReg(DstRegW);
757 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
758 DEBUG(dbgs() << "\n");
759 // Make the machine verifier happy by providing a definition for
760 // the X register.
761 // Insert this definition right after the generated LDP, i.e., before
762 // InsertionPoint.
763 MachineInstrBuilder MIBKill =
Chad Rosierc46ef882016-02-09 19:33:42 +0000764 BuildMI(*MBB, InsertionPoint, DL, TII->get(TargetOpcode::KILL), DstRegW)
Quentin Colombet66b61632015-03-06 22:42:10 +0000765 .addReg(DstRegW)
766 .addReg(DstRegX, RegState::Define);
767 MIBKill->getOperand(2).setImplicit();
768 // Create the sign extension.
769 MachineInstrBuilder MIBSXTW =
Chad Rosierc46ef882016-02-09 19:33:42 +0000770 BuildMI(*MBB, InsertionPoint, DL, TII->get(AArch64::SBFMXri), DstRegX)
Quentin Colombet66b61632015-03-06 22:42:10 +0000771 .addReg(DstRegX)
772 .addImm(0)
773 .addImm(31);
774 (void)MIBSXTW;
775 DEBUG(dbgs() << " Extend operand:\n ");
776 DEBUG(((MachineInstr *)MIBSXTW)->print(dbgs()));
Quentin Colombet66b61632015-03-06 22:42:10 +0000777 } else {
778 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
Quentin Colombet66b61632015-03-06 22:42:10 +0000779 }
Chad Rosier1c44c5982016-02-09 20:27:45 +0000780 DEBUG(dbgs() << "\n");
Tim Northover3b0846e2014-05-24 12:50:23 +0000781
782 // Erase the old instructions.
783 I->eraseFromParent();
784 Paired->eraseFromParent();
785
786 return NextI;
787}
788
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000789MachineBasicBlock::iterator
790AArch64LoadStoreOpt::promoteLoadFromStore(MachineBasicBlock::iterator LoadI,
791 MachineBasicBlock::iterator StoreI) {
792 MachineBasicBlock::iterator NextI = LoadI;
793 ++NextI;
794
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000795 int LoadSize = getMemScale(*LoadI);
796 int StoreSize = getMemScale(*StoreI);
797 unsigned LdRt = getLdStRegOp(*LoadI).getReg();
Florian Hahn80e48512017-06-21 08:47:23 +0000798 const MachineOperand &StMO = getLdStRegOp(*StoreI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000799 unsigned StRt = getLdStRegOp(*StoreI).getReg();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000800 bool IsStoreXReg = TRI->getRegClass(AArch64::GPR64RegClassID)->contains(StRt);
801
802 assert((IsStoreXReg ||
803 TRI->getRegClass(AArch64::GPR32RegClassID)->contains(StRt)) &&
804 "Unexpected RegClass");
805
806 MachineInstr *BitExtMI;
807 if (LoadSize == StoreSize && (LoadSize == 4 || LoadSize == 8)) {
808 // Remove the load, if the destination register of the loads is the same
809 // register for stored value.
810 if (StRt == LdRt && LoadSize == 8) {
Matthias Braun76bb4132016-12-16 23:55:43 +0000811 StoreI->clearRegisterKills(StRt, TRI);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000812 DEBUG(dbgs() << "Remove load instruction:\n ");
813 DEBUG(LoadI->print(dbgs()));
814 DEBUG(dbgs() << "\n");
815 LoadI->eraseFromParent();
816 return NextI;
817 }
818 // Replace the load with a mov if the load and store are in the same size.
819 BitExtMI =
820 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(),
821 TII->get(IsStoreXReg ? AArch64::ORRXrs : AArch64::ORRWrs), LdRt)
822 .addReg(IsStoreXReg ? AArch64::XZR : AArch64::WZR)
Florian Hahn80e48512017-06-21 08:47:23 +0000823 .add(StMO)
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000824 .addImm(AArch64_AM::getShifterImm(AArch64_AM::LSL, 0));
825 } else {
826 // FIXME: Currently we disable this transformation in big-endian targets as
827 // performance and correctness are verified only in little-endian.
828 if (!Subtarget->isLittleEndian())
829 return NextI;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000830 bool IsUnscaled = TII->isUnscaledLdSt(*LoadI);
831 assert(IsUnscaled == TII->isUnscaledLdSt(*StoreI) &&
Chad Rosiere4e15ba2016-03-09 17:29:48 +0000832 "Unsupported ld/st match");
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000833 assert(LoadSize <= StoreSize && "Invalid load size");
834 int UnscaledLdOffset = IsUnscaled
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000835 ? getLdStOffsetOp(*LoadI).getImm()
836 : getLdStOffsetOp(*LoadI).getImm() * LoadSize;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000837 int UnscaledStOffset = IsUnscaled
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000838 ? getLdStOffsetOp(*StoreI).getImm()
839 : getLdStOffsetOp(*StoreI).getImm() * StoreSize;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000840 int Width = LoadSize * 8;
841 int Immr = 8 * (UnscaledLdOffset - UnscaledStOffset);
842 int Imms = Immr + Width - 1;
843 unsigned DestReg = IsStoreXReg
844 ? TRI->getMatchingSuperReg(LdRt, AArch64::sub_32,
845 &AArch64::GPR64RegClass)
846 : LdRt;
847
848 assert((UnscaledLdOffset >= UnscaledStOffset &&
849 (UnscaledLdOffset + LoadSize) <= UnscaledStOffset + StoreSize) &&
850 "Invalid offset");
851
852 Immr = 8 * (UnscaledLdOffset - UnscaledStOffset);
853 Imms = Immr + Width - 1;
854 if (UnscaledLdOffset == UnscaledStOffset) {
855 uint32_t AndMaskEncoded = ((IsStoreXReg ? 1 : 0) << 12) // N
856 | ((Immr) << 6) // immr
857 | ((Imms) << 0) // imms
858 ;
859
860 BitExtMI =
861 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(),
862 TII->get(IsStoreXReg ? AArch64::ANDXri : AArch64::ANDWri),
863 DestReg)
Florian Hahn80e48512017-06-21 08:47:23 +0000864 .add(StMO)
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000865 .addImm(AndMaskEncoded);
866 } else {
867 BitExtMI =
868 BuildMI(*LoadI->getParent(), LoadI, LoadI->getDebugLoc(),
869 TII->get(IsStoreXReg ? AArch64::UBFMXri : AArch64::UBFMWri),
870 DestReg)
Florian Hahn80e48512017-06-21 08:47:23 +0000871 .add(StMO)
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000872 .addImm(Immr)
873 .addImm(Imms);
874 }
875 }
Matthias Braun76bb4132016-12-16 23:55:43 +0000876
Matthias Braund9a59a82017-02-17 23:15:03 +0000877 // Clear kill flags between store and load.
878 for (MachineInstr &MI : make_range(StoreI->getIterator(),
879 BitExtMI->getIterator()))
Florian Hahn8552e592017-06-21 09:51:52 +0000880 if (MI.killsRegister(StRt, TRI)) {
881 MI.clearRegisterKills(StRt, TRI);
882 break;
883 }
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000884
885 DEBUG(dbgs() << "Promoting load by replacing :\n ");
886 DEBUG(StoreI->print(dbgs()));
887 DEBUG(dbgs() << " ");
888 DEBUG(LoadI->print(dbgs()));
889 DEBUG(dbgs() << " with instructions:\n ");
890 DEBUG(StoreI->print(dbgs()));
891 DEBUG(dbgs() << " ");
892 DEBUG((BitExtMI)->print(dbgs()));
893 DEBUG(dbgs() << "\n");
894
895 // Erase the old instructions.
896 LoadI->eraseFromParent();
897 return NextI;
898}
899
Tim Northover3b0846e2014-05-24 12:50:23 +0000900/// trackRegDefsUses - Remember what registers the specified instruction uses
901/// and modifies.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000902static void trackRegDefsUses(const MachineInstr &MI, BitVector &ModifiedRegs,
Tim Northover3b0846e2014-05-24 12:50:23 +0000903 BitVector &UsedRegs,
904 const TargetRegisterInfo *TRI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000905 for (const MachineOperand &MO : MI.operands()) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000906 if (MO.isRegMask())
907 ModifiedRegs.setBitsNotInMask(MO.getRegMask());
908
909 if (!MO.isReg())
910 continue;
911 unsigned Reg = MO.getReg();
Geoff Berry173b14d2016-02-09 20:47:21 +0000912 if (!Reg)
913 continue;
Tim Northover3b0846e2014-05-24 12:50:23 +0000914 if (MO.isDef()) {
Geoff Berrye0bf52f2016-11-21 22:51:10 +0000915 // WZR/XZR are not modified even when used as a destination register.
916 if (Reg != AArch64::WZR && Reg != AArch64::XZR)
917 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
918 ModifiedRegs.set(*AI);
Tim Northover3b0846e2014-05-24 12:50:23 +0000919 } else {
920 assert(MO.isUse() && "Reg operand not a def and not a use?!?");
921 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
922 UsedRegs.set(*AI);
923 }
924 }
925}
926
927static bool inBoundsForPair(bool IsUnscaled, int Offset, int OffsetStride) {
Chad Rosier3dd0e942015-08-18 16:20:03 +0000928 // Convert the byte-offset used by unscaled into an "element" offset used
929 // by the scaled pair load/store instructions.
Chad Rosier00f9d232016-02-11 14:25:08 +0000930 if (IsUnscaled) {
931 // If the byte-offset isn't a multiple of the stride, there's no point
932 // trying to match it.
933 if (Offset % OffsetStride)
934 return false;
Chad Rosier3dd0e942015-08-18 16:20:03 +0000935 Offset /= OffsetStride;
Chad Rosier00f9d232016-02-11 14:25:08 +0000936 }
Chad Rosier3dd0e942015-08-18 16:20:03 +0000937 return Offset <= 63 && Offset >= -64;
Tim Northover3b0846e2014-05-24 12:50:23 +0000938}
939
940// Do alignment, specialized to power of 2 and for signed ints,
941// avoiding having to do a C-style cast from uint_64t to int when
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000942// using alignTo from include/llvm/Support/MathExtras.h.
Tim Northover3b0846e2014-05-24 12:50:23 +0000943// FIXME: Move this function to include/MathExtras.h?
944static int alignTo(int Num, int PowOf2) {
945 return (Num + PowOf2 - 1) & ~(PowOf2 - 1);
946}
947
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000948static bool mayAlias(MachineInstr &MIa, MachineInstr &MIb,
Chad Rosiera69dcb62017-03-17 14:19:55 +0000949 AliasAnalysis *AA) {
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000950 // One of the instructions must modify memory.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000951 if (!MIa.mayStore() && !MIb.mayStore())
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000952 return false;
953
954 // Both instructions must be memory operations.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000955 if (!MIa.mayLoadOrStore() && !MIb.mayLoadOrStore())
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000956 return false;
957
Chad Rosiera69dcb62017-03-17 14:19:55 +0000958 return MIa.mayAlias(AA, MIb, /*UseTBAA*/false);
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000959}
960
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000961static bool mayAlias(MachineInstr &MIa,
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000962 SmallVectorImpl<MachineInstr *> &MemInsns,
Chad Rosiera69dcb62017-03-17 14:19:55 +0000963 AliasAnalysis *AA) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000964 for (MachineInstr *MIb : MemInsns)
Chad Rosiera69dcb62017-03-17 14:19:55 +0000965 if (mayAlias(MIa, *MIb, AA))
Chad Rosierce8e5ab2015-05-21 21:36:46 +0000966 return true;
967
968 return false;
969}
970
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000971bool AArch64LoadStoreOpt::findMatchingStore(
972 MachineBasicBlock::iterator I, unsigned Limit,
973 MachineBasicBlock::iterator &StoreI) {
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000974 MachineBasicBlock::iterator B = I->getParent()->begin();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000975 MachineBasicBlock::iterator MBBI = I;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000976 MachineInstr &LoadMI = *I;
Chad Rosier5c6a66c2016-02-09 15:59:57 +0000977 unsigned BaseReg = getLdStBaseOp(LoadMI).getReg();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000978
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000979 // If the load is the first instruction in the block, there's obviously
980 // not any matching store.
981 if (MBBI == B)
982 return false;
983
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000984 // Track which registers have been modified and used between the first insn
985 // and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +0000986 ModifiedRegs.reset();
987 UsedRegs.reset();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000988
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000989 unsigned Count = 0;
990 do {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000991 --MBBI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +0000992 MachineInstr &MI = *MBBI;
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000993
Geoff Berry4ff2e362016-07-21 15:20:25 +0000994 // Don't count transient instructions towards the search limit since there
995 // may be different numbers of them if e.g. debug information is present.
996 if (!MI.isTransient())
Jun Bum Lim633b2d82016-02-11 16:18:24 +0000997 ++Count;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +0000998
999 // If the load instruction reads directly from the address to which the
1000 // store instruction writes and the stored value is not modified, we can
1001 // promote the load. Since we do not handle stores with pre-/post-index,
1002 // it's unnecessary to check if BaseReg is modified by the store itself.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001003 if (MI.mayStore() && isMatchingStore(LoadMI, MI) &&
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001004 BaseReg == getLdStBaseOp(MI).getReg() &&
Chad Rosiere4e15ba2016-03-09 17:29:48 +00001005 isLdOffsetInRangeOfSt(LoadMI, MI, TII) &&
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001006 !ModifiedRegs[getLdStRegOp(MI).getReg()]) {
1007 StoreI = MBBI;
1008 return true;
1009 }
1010
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001011 if (MI.isCall())
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001012 return false;
1013
1014 // Update modified / uses register lists.
1015 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1016
1017 // Otherwise, if the base register is modified, we have no match, so
1018 // return early.
1019 if (ModifiedRegs[BaseReg])
1020 return false;
1021
1022 // If we encounter a store aliased with the load, return early.
Chad Rosiera69dcb62017-03-17 14:19:55 +00001023 if (MI.mayStore() && mayAlias(LoadMI, MI, AA))
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001024 return false;
Jun Bum Lim633b2d82016-02-11 16:18:24 +00001025 } while (MBBI != B && Count < Limit);
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001026 return false;
1027}
1028
Chad Rosierc5083c22016-06-10 20:47:14 +00001029// Returns true if FirstMI and MI are candidates for merging or pairing.
1030// Otherwise, returns false.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001031static bool areCandidatesToMergeOrPair(MachineInstr &FirstMI, MachineInstr &MI,
Chad Rosierc5083c22016-06-10 20:47:14 +00001032 LdStPairFlags &Flags,
1033 const AArch64InstrInfo *TII) {
1034 // If this is volatile or if pairing is suppressed, not a candidate.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001035 if (MI.hasOrderedMemoryRef() || TII->isLdStPairSuppressed(MI))
Chad Rosierc5083c22016-06-10 20:47:14 +00001036 return false;
1037
1038 // We should have already checked FirstMI for pair suppression and volatility.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001039 assert(!FirstMI.hasOrderedMemoryRef() &&
1040 !TII->isLdStPairSuppressed(FirstMI) &&
Chad Rosierc5083c22016-06-10 20:47:14 +00001041 "FirstMI shouldn't get here if either of these checks are true.");
1042
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001043 unsigned OpcA = FirstMI.getOpcode();
1044 unsigned OpcB = MI.getOpcode();
Chad Rosierc5083c22016-06-10 20:47:14 +00001045
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001046 // Opcodes match: nothing more to check.
1047 if (OpcA == OpcB)
1048 return true;
1049
1050 // Try to match a sign-extended load/store with a zero-extended load/store.
1051 bool IsValidLdStrOpc, PairIsValidLdStrOpc;
1052 unsigned NonSExtOpc = getMatchingNonSExtOpcode(OpcA, &IsValidLdStrOpc);
1053 assert(IsValidLdStrOpc &&
1054 "Given Opc should be a Load or Store with an immediate");
1055 // OpcA will be the first instruction in the pair.
1056 if (NonSExtOpc == getMatchingNonSExtOpcode(OpcB, &PairIsValidLdStrOpc)) {
1057 Flags.setSExtIdx(NonSExtOpc == (unsigned)OpcA ? 1 : 0);
1058 return true;
1059 }
Chad Rosier00f9d232016-02-11 14:25:08 +00001060
Chad Rosierd6daac42016-11-07 15:27:22 +00001061 // If the second instruction isn't even a mergable/pairable load/store, bail
1062 // out.
Chad Rosier00f9d232016-02-11 14:25:08 +00001063 if (!PairIsValidLdStrOpc)
1064 return false;
1065
Chad Rosierd6daac42016-11-07 15:27:22 +00001066 // FIXME: We don't support merging narrow stores with mixed scaled/unscaled
1067 // offsets.
1068 if (isNarrowStore(OpcA) || isNarrowStore(OpcB))
Chad Rosier00f9d232016-02-11 14:25:08 +00001069 return false;
1070
1071 // Try to match an unscaled load/store with a scaled load/store.
Chad Rosiere4e15ba2016-03-09 17:29:48 +00001072 return TII->isUnscaledLdSt(OpcA) != TII->isUnscaledLdSt(OpcB) &&
Chad Rosier00f9d232016-02-11 14:25:08 +00001073 getMatchingPairOpcode(OpcA) == getMatchingPairOpcode(OpcB);
1074
1075 // FIXME: Can we also match a mixed sext/zext unscaled/scaled pair?
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001076}
1077
Chad Rosier9f4ec2e2016-02-10 18:49:28 +00001078/// Scan the instructions looking for a load/store that can be combined with the
1079/// current instruction into a wider equivalent or a load/store pair.
Tim Northover3b0846e2014-05-24 12:50:23 +00001080MachineBasicBlock::iterator
1081AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
Jun Bum Limcf974432016-03-31 14:47:24 +00001082 LdStPairFlags &Flags, unsigned Limit,
1083 bool FindNarrowMerge) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001084 MachineBasicBlock::iterator E = I->getParent()->end();
1085 MachineBasicBlock::iterator MBBI = I;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001086 MachineInstr &FirstMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001087 ++MBBI;
1088
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001089 bool MayLoad = FirstMI.mayLoad();
1090 bool IsUnscaled = TII->isUnscaledLdSt(FirstMI);
Chad Rosierf77e9092015-08-06 15:50:12 +00001091 unsigned Reg = getLdStRegOp(FirstMI).getReg();
1092 unsigned BaseReg = getLdStBaseOp(FirstMI).getReg();
1093 int Offset = getLdStOffsetOp(FirstMI).getImm();
Chad Rosierf11d0402015-10-01 18:17:12 +00001094 int OffsetStride = IsUnscaled ? getMemScale(FirstMI) : 1;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +00001095 bool IsPromotableZeroStore = isPromotableZeroStoreInst(FirstMI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001096
1097 // Track which registers have been modified and used between the first insn
1098 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001099 ModifiedRegs.reset();
1100 UsedRegs.reset();
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001101
1102 // Remember any instructions that read/write memory between FirstMI and MI.
1103 SmallVector<MachineInstr *, 4> MemInsns;
1104
Tim Northover3b0846e2014-05-24 12:50:23 +00001105 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001106 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001107
Geoff Berry4ff2e362016-07-21 15:20:25 +00001108 // Don't count transient instructions towards the search limit since there
1109 // may be different numbers of them if e.g. debug information is present.
1110 if (!MI.isTransient())
1111 ++Count;
Tim Northover3b0846e2014-05-24 12:50:23 +00001112
Chad Rosier18896c02016-02-04 16:01:40 +00001113 Flags.setSExtIdx(-1);
Chad Rosierc5083c22016-06-10 20:47:14 +00001114 if (areCandidatesToMergeOrPair(FirstMI, MI, Flags, TII) &&
Chad Rosierc3f6cb92016-02-10 19:45:48 +00001115 getLdStOffsetOp(MI).isImm()) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001116 assert(MI.mayLoadOrStore() && "Expected memory operation.");
Tim Northover3b0846e2014-05-24 12:50:23 +00001117 // If we've found another instruction with the same opcode, check to see
1118 // if the base and offset are compatible with our starting instruction.
1119 // These instructions all have scaled immediate operands, so we just
1120 // check for +1/-1. Make sure to check the new instruction offset is
1121 // actually an immediate and not a symbolic reference destined for
1122 // a relocation.
Chad Rosierf77e9092015-08-06 15:50:12 +00001123 unsigned MIBaseReg = getLdStBaseOp(MI).getReg();
1124 int MIOffset = getLdStOffsetOp(MI).getImm();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001125 bool MIIsUnscaled = TII->isUnscaledLdSt(MI);
Chad Rosier00f9d232016-02-11 14:25:08 +00001126 if (IsUnscaled != MIIsUnscaled) {
1127 // We're trying to pair instructions that differ in how they are scaled.
1128 // If FirstMI is scaled then scale the offset of MI accordingly.
1129 // Otherwise, do the opposite (i.e., make MI's offset unscaled).
1130 int MemSize = getMemScale(MI);
1131 if (MIIsUnscaled) {
1132 // If the unscaled offset isn't a multiple of the MemSize, we can't
1133 // pair the operations together: bail and keep looking.
Eli Friedmanf184e4b2016-08-12 20:39:51 +00001134 if (MIOffset % MemSize) {
1135 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1136 MemInsns.push_back(&MI);
Chad Rosier00f9d232016-02-11 14:25:08 +00001137 continue;
Eli Friedmanf184e4b2016-08-12 20:39:51 +00001138 }
Chad Rosier00f9d232016-02-11 14:25:08 +00001139 MIOffset /= MemSize;
1140 } else {
1141 MIOffset *= MemSize;
1142 }
1143 }
1144
Tim Northover3b0846e2014-05-24 12:50:23 +00001145 if (BaseReg == MIBaseReg && ((Offset == MIOffset + OffsetStride) ||
1146 (Offset + OffsetStride == MIOffset))) {
1147 int MinOffset = Offset < MIOffset ? Offset : MIOffset;
Jun Bum Limcf974432016-03-31 14:47:24 +00001148 if (FindNarrowMerge) {
Jun Bum Lim80ec0d32015-11-20 21:14:07 +00001149 // If the alignment requirements of the scaled wide load/store
Jun Bum Limcf974432016-03-31 14:47:24 +00001150 // instruction can't express the offset of the scaled narrow input,
1151 // bail and keep looking. For promotable zero stores, allow only when
1152 // the stored value is the same (i.e., WZR).
1153 if ((!IsUnscaled && alignTo(MinOffset, 2) != MinOffset) ||
1154 (IsPromotableZeroStore && Reg != getLdStRegOp(MI).getReg())) {
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001155 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001156 MemInsns.push_back(&MI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001157 continue;
1158 }
1159 } else {
Chad Rosierd1f6c842016-06-10 20:49:18 +00001160 // Pairwise instructions have a 7-bit signed offset field. Single
1161 // insns have a 12-bit unsigned offset field. If the resultant
1162 // immediate offset of merging these instructions is out of range for
1163 // a pairwise instruction, bail and keep looking.
Jun Bum Limcf974432016-03-31 14:47:24 +00001164 if (!inBoundsForPair(IsUnscaled, MinOffset, OffsetStride)) {
1165 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001166 MemInsns.push_back(&MI);
Jun Bum Limcf974432016-03-31 14:47:24 +00001167 continue;
1168 }
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001169 // If the alignment requirements of the paired (scaled) instruction
1170 // can't express the offset of the unscaled input, bail and keep
1171 // looking.
1172 if (IsUnscaled && (alignTo(MinOffset, OffsetStride) != MinOffset)) {
1173 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001174 MemInsns.push_back(&MI);
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001175 continue;
1176 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001177 }
1178 // If the destination register of the loads is the same register, bail
1179 // and keep looking. A load-pair instruction with both destination
1180 // registers the same is UNPREDICTABLE and will result in an exception.
Jun Bum Limcf974432016-03-31 14:47:24 +00001181 if (MayLoad && Reg == getLdStRegOp(MI).getReg()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001182 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001183 MemInsns.push_back(&MI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001184 continue;
1185 }
1186
1187 // If the Rt of the second instruction was not modified or used between
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001188 // the two instructions and none of the instructions between the second
1189 // and first alias with the second, we can combine the second into the
1190 // first.
Chad Rosierf77e9092015-08-06 15:50:12 +00001191 if (!ModifiedRegs[getLdStRegOp(MI).getReg()] &&
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001192 !(MI.mayLoad() && UsedRegs[getLdStRegOp(MI).getReg()]) &&
Chad Rosiera69dcb62017-03-17 14:19:55 +00001193 !mayAlias(MI, MemInsns, AA)) {
Chad Rosier96a18a92015-07-21 17:42:04 +00001194 Flags.setMergeForward(false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001195 return MBBI;
1196 }
1197
1198 // Likewise, if the Rt of the first instruction is not modified or used
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001199 // between the two instructions and none of the instructions between the
1200 // first and the second alias with the first, we can combine the first
1201 // into the second.
Chad Rosierf77e9092015-08-06 15:50:12 +00001202 if (!ModifiedRegs[getLdStRegOp(FirstMI).getReg()] &&
Chad Rosier5f668e12015-09-03 14:19:43 +00001203 !(MayLoad && UsedRegs[getLdStRegOp(FirstMI).getReg()]) &&
Chad Rosiera69dcb62017-03-17 14:19:55 +00001204 !mayAlias(FirstMI, MemInsns, AA)) {
Chad Rosier96a18a92015-07-21 17:42:04 +00001205 Flags.setMergeForward(true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001206 return MBBI;
1207 }
1208 // Unable to combine these instructions due to interference in between.
1209 // Keep looking.
1210 }
1211 }
1212
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001213 // If the instruction wasn't a matching load or store. Stop searching if we
1214 // encounter a call instruction that might modify memory.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001215 if (MI.isCall())
Tim Northover3b0846e2014-05-24 12:50:23 +00001216 return E;
1217
1218 // Update modified / uses register lists.
1219 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1220
1221 // Otherwise, if the base register is modified, we have no match, so
1222 // return early.
1223 if (ModifiedRegs[BaseReg])
1224 return E;
Chad Rosierce8e5ab2015-05-21 21:36:46 +00001225
1226 // Update list of instructions that read/write memory.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001227 if (MI.mayLoadOrStore())
1228 MemInsns.push_back(&MI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001229 }
1230 return E;
1231}
1232
1233MachineBasicBlock::iterator
Chad Rosier2dfd3542015-09-23 13:51:44 +00001234AArch64LoadStoreOpt::mergeUpdateInsn(MachineBasicBlock::iterator I,
1235 MachineBasicBlock::iterator Update,
1236 bool IsPreIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001237 assert((Update->getOpcode() == AArch64::ADDXri ||
1238 Update->getOpcode() == AArch64::SUBXri) &&
1239 "Unexpected base register update instruction to merge!");
1240 MachineBasicBlock::iterator NextI = I;
1241 // Return the instruction following the merged instruction, which is
1242 // the instruction following our unmerged load. Unless that's the add/sub
1243 // instruction we're merging, in which case it's the one after that.
1244 if (++NextI == Update)
1245 ++NextI;
1246
1247 int Value = Update->getOperand(2).getImm();
1248 assert(AArch64_AM::getShiftValue(Update->getOperand(3).getImm()) == 0 &&
Chad Rosier2dfd3542015-09-23 13:51:44 +00001249 "Can't merge 1 << 12 offset into pre-/post-indexed load / store");
Tim Northover3b0846e2014-05-24 12:50:23 +00001250 if (Update->getOpcode() == AArch64::SUBXri)
1251 Value = -Value;
1252
Chad Rosier2dfd3542015-09-23 13:51:44 +00001253 unsigned NewOpc = IsPreIdx ? getPreIndexedOpcode(I->getOpcode())
1254 : getPostIndexedOpcode(I->getOpcode());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001255 MachineInstrBuilder MIB;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001256 if (!isPairedLdSt(*I)) {
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001257 // Non-paired instruction.
1258 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001259 .add(getLdStRegOp(*Update))
1260 .add(getLdStRegOp(*I))
1261 .add(getLdStBaseOp(*I))
Chad Rosier3ada75f2016-01-28 15:38:24 +00001262 .addImm(Value)
1263 .setMemRefs(I->memoperands_begin(), I->memoperands_end());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001264 } else {
1265 // Paired instruction.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001266 int Scale = getMemScale(*I);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001267 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
Diana Picus116bbab2017-01-13 09:58:52 +00001268 .add(getLdStRegOp(*Update))
1269 .add(getLdStRegOp(*I, 0))
1270 .add(getLdStRegOp(*I, 1))
1271 .add(getLdStBaseOp(*I))
Chad Rosier3ada75f2016-01-28 15:38:24 +00001272 .addImm(Value / Scale)
1273 .setMemRefs(I->memoperands_begin(), I->memoperands_end());
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001274 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001275 (void)MIB;
1276
Chad Rosier2dfd3542015-09-23 13:51:44 +00001277 if (IsPreIdx)
1278 DEBUG(dbgs() << "Creating pre-indexed load/store.");
1279 else
1280 DEBUG(dbgs() << "Creating post-indexed load/store.");
Tim Northover3b0846e2014-05-24 12:50:23 +00001281 DEBUG(dbgs() << " Replacing instructions:\n ");
1282 DEBUG(I->print(dbgs()));
1283 DEBUG(dbgs() << " ");
1284 DEBUG(Update->print(dbgs()));
1285 DEBUG(dbgs() << " with instruction:\n ");
1286 DEBUG(((MachineInstr *)MIB)->print(dbgs()));
1287 DEBUG(dbgs() << "\n");
1288
1289 // Erase the old instructions for the block.
1290 I->eraseFromParent();
1291 Update->eraseFromParent();
1292
1293 return NextI;
1294}
1295
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001296bool AArch64LoadStoreOpt::isMatchingUpdateInsn(MachineInstr &MemMI,
1297 MachineInstr &MI,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001298 unsigned BaseReg, int Offset) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001299 switch (MI.getOpcode()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001300 default:
1301 break;
1302 case AArch64::SUBXri:
Tim Northover3b0846e2014-05-24 12:50:23 +00001303 case AArch64::ADDXri:
1304 // Make sure it's a vanilla immediate operand, not a relocation or
1305 // anything else we can't handle.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001306 if (!MI.getOperand(2).isImm())
Tim Northover3b0846e2014-05-24 12:50:23 +00001307 break;
1308 // Watch out for 1 << 12 shifted value.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001309 if (AArch64_AM::getShiftValue(MI.getOperand(3).getImm()))
Tim Northover3b0846e2014-05-24 12:50:23 +00001310 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001311
1312 // The update instruction source and destination register must be the
1313 // same as the load/store base register.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001314 if (MI.getOperand(0).getReg() != BaseReg ||
1315 MI.getOperand(1).getReg() != BaseReg)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001316 break;
1317
1318 bool IsPairedInsn = isPairedLdSt(MemMI);
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001319 int UpdateOffset = MI.getOperand(2).getImm();
Eli Friedman8585e9d2016-08-12 20:28:02 +00001320 if (MI.getOpcode() == AArch64::SUBXri)
1321 UpdateOffset = -UpdateOffset;
1322
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001323 // For non-paired load/store instructions, the immediate must fit in a
1324 // signed 9-bit integer.
1325 if (!IsPairedInsn && (UpdateOffset > 255 || UpdateOffset < -256))
1326 break;
1327
1328 // For paired load/store instructions, the immediate must be a multiple of
1329 // the scaling factor. The scaled offset must also fit into a signed 7-bit
1330 // integer.
1331 if (IsPairedInsn) {
Chad Rosier32d4d372015-09-29 16:07:32 +00001332 int Scale = getMemScale(MemMI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001333 if (UpdateOffset % Scale != 0)
1334 break;
1335
1336 int ScaledOffset = UpdateOffset / Scale;
Eli Friedman8585e9d2016-08-12 20:28:02 +00001337 if (ScaledOffset > 63 || ScaledOffset < -64)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001338 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00001339 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001340
1341 // If we have a non-zero Offset, we check that it matches the amount
1342 // we're adding to the register.
Eli Friedman8585e9d2016-08-12 20:28:02 +00001343 if (!Offset || Offset == UpdateOffset)
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001344 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001345 break;
1346 }
1347 return false;
1348}
1349
1350MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward(
Chad Rosier35706ad2016-02-04 21:26:02 +00001351 MachineBasicBlock::iterator I, int UnscaledOffset, unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001352 MachineBasicBlock::iterator E = I->getParent()->end();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001353 MachineInstr &MemMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001354 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001355
Chad Rosierf77e9092015-08-06 15:50:12 +00001356 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001357 int MIUnscaledOffset = getLdStOffsetOp(MemMI).getImm() * getMemScale(MemMI);
Tim Northover3b0846e2014-05-24 12:50:23 +00001358
Chad Rosierb7c5b912015-10-01 13:43:05 +00001359 // Scan forward looking for post-index opportunities. Updating instructions
1360 // can't be formed if the memory instruction doesn't have the offset we're
1361 // looking for.
1362 if (MIUnscaledOffset != UnscaledOffset)
1363 return E;
1364
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001365 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +00001366 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001367 bool IsPairedInsn = isPairedLdSt(MemMI);
1368 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
1369 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
1370 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
1371 return E;
1372 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001373
Tim Northover3b0846e2014-05-24 12:50:23 +00001374 // Track which registers have been modified and used between the first insn
1375 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001376 ModifiedRegs.reset();
1377 UsedRegs.reset();
Tim Northover3b0846e2014-05-24 12:50:23 +00001378 ++MBBI;
Chad Rosier35706ad2016-02-04 21:26:02 +00001379 for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001380 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001381
Geoff Berry4ff2e362016-07-21 15:20:25 +00001382 // Don't count transient instructions towards the search limit since there
1383 // may be different numbers of them if e.g. debug information is present.
1384 if (!MI.isTransient())
1385 ++Count;
Chad Rosier35706ad2016-02-04 21:26:02 +00001386
Tim Northover3b0846e2014-05-24 12:50:23 +00001387 // If we found a match, return it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001388 if (isMatchingUpdateInsn(*I, MI, BaseReg, UnscaledOffset))
Tim Northover3b0846e2014-05-24 12:50:23 +00001389 return MBBI;
1390
1391 // Update the status of what the instruction clobbered and used.
1392 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1393
1394 // Otherwise, if the base register is used or modified, we have no match, so
1395 // return early.
1396 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
1397 return E;
1398 }
1399 return E;
1400}
1401
1402MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnBackward(
Chad Rosier35706ad2016-02-04 21:26:02 +00001403 MachineBasicBlock::iterator I, unsigned Limit) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001404 MachineBasicBlock::iterator B = I->getParent()->begin();
1405 MachineBasicBlock::iterator E = I->getParent()->end();
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001406 MachineInstr &MemMI = *I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001407 MachineBasicBlock::iterator MBBI = I;
Tim Northover3b0846e2014-05-24 12:50:23 +00001408
Chad Rosierf77e9092015-08-06 15:50:12 +00001409 unsigned BaseReg = getLdStBaseOp(MemMI).getReg();
1410 int Offset = getLdStOffsetOp(MemMI).getImm();
Tim Northover3b0846e2014-05-24 12:50:23 +00001411
1412 // If the load/store is the first instruction in the block, there's obviously
1413 // not any matching update. Ditto if the memory offset isn't zero.
1414 if (MBBI == B || Offset != 0)
1415 return E;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001416 // If the base register overlaps a destination register, we can't
Tim Northover3b0846e2014-05-24 12:50:23 +00001417 // merge the update.
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001418 bool IsPairedInsn = isPairedLdSt(MemMI);
1419 for (unsigned i = 0, e = IsPairedInsn ? 2 : 1; i != e; ++i) {
1420 unsigned DestReg = getLdStRegOp(MemMI, i).getReg();
1421 if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
1422 return E;
1423 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001424
1425 // Track which registers have been modified and used between the first insn
1426 // (inclusive) and the second insn.
Chad Rosierbba881e2016-02-02 15:02:30 +00001427 ModifiedRegs.reset();
1428 UsedRegs.reset();
Geoff Berry173b14d2016-02-09 20:47:21 +00001429 unsigned Count = 0;
1430 do {
1431 --MBBI;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001432 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001433
Geoff Berry4ff2e362016-07-21 15:20:25 +00001434 // Don't count transient instructions towards the search limit since there
1435 // may be different numbers of them if e.g. debug information is present.
1436 if (!MI.isTransient())
Geoff Berry173b14d2016-02-09 20:47:21 +00001437 ++Count;
Chad Rosier35706ad2016-02-04 21:26:02 +00001438
Tim Northover3b0846e2014-05-24 12:50:23 +00001439 // If we found a match, return it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001440 if (isMatchingUpdateInsn(*I, MI, BaseReg, Offset))
Tim Northover3b0846e2014-05-24 12:50:23 +00001441 return MBBI;
1442
1443 // Update the status of what the instruction clobbered and used.
1444 trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
1445
1446 // Otherwise, if the base register is used or modified, we have no match, so
1447 // return early.
1448 if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
1449 return E;
Geoff Berry173b14d2016-02-09 20:47:21 +00001450 } while (MBBI != B && Count < Limit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001451 return E;
1452}
1453
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001454bool AArch64LoadStoreOpt::tryToPromoteLoadFromStore(
1455 MachineBasicBlock::iterator &MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001456 MachineInstr &MI = *MBBI;
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001457 // If this is a volatile load, don't mess with it.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001458 if (MI.hasOrderedMemoryRef())
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001459 return false;
1460
1461 // Make sure this is a reg+imm.
1462 // FIXME: It is possible to extend it to handle reg+reg cases.
1463 if (!getLdStOffsetOp(MI).isImm())
1464 return false;
1465
Chad Rosier35706ad2016-02-04 21:26:02 +00001466 // Look backward up to LdStLimit instructions.
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001467 MachineBasicBlock::iterator StoreI;
Chad Rosier35706ad2016-02-04 21:26:02 +00001468 if (findMatchingStore(MBBI, LdStLimit, StoreI)) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001469 ++NumLoadsFromStoresPromoted;
1470 // Promote the load. Keeping the iterator straight is a
1471 // pain, so we let the merge routine tell us what the next instruction
1472 // is after it's done mucking about.
1473 MBBI = promoteLoadFromStore(MBBI, StoreI);
1474 return true;
1475 }
1476 return false;
1477}
1478
Chad Rosierd6daac42016-11-07 15:27:22 +00001479// Merge adjacent zero stores into a wider store.
1480bool AArch64LoadStoreOpt::tryToMergeZeroStInst(
Chad Rosier24c46ad2016-02-09 18:10:20 +00001481 MachineBasicBlock::iterator &MBBI) {
Chad Rosierd6daac42016-11-07 15:27:22 +00001482 assert(isPromotableZeroStoreInst(*MBBI) && "Expected narrow store.");
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001483 MachineInstr &MI = *MBBI;
1484 MachineBasicBlock::iterator E = MI.getParent()->end();
Chad Rosier24c46ad2016-02-09 18:10:20 +00001485
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001486 if (!TII->isCandidateToMergeOrPair(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001487 return false;
1488
1489 // Look ahead up to LdStLimit instructions for a mergable instruction.
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001490 LdStPairFlags Flags;
Jun Bum Lim397eb7b2016-02-12 15:25:39 +00001491 MachineBasicBlock::iterator MergeMI =
Jun Bum Limcf974432016-03-31 14:47:24 +00001492 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ true);
Chad Rosierd7363db2016-02-09 19:09:22 +00001493 if (MergeMI != E) {
Chad Rosierd6daac42016-11-07 15:27:22 +00001494 ++NumZeroStoresPromoted;
1495
Chad Rosier24c46ad2016-02-09 18:10:20 +00001496 // Keeping the iterator straight is a pain, so we let the merge routine tell
1497 // us what the next instruction is after it's done mucking about.
Chad Rosierd6daac42016-11-07 15:27:22 +00001498 MBBI = mergeNarrowZeroStores(MBBI, MergeMI, Flags);
Chad Rosier24c46ad2016-02-09 18:10:20 +00001499 return true;
1500 }
1501 return false;
1502}
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001503
Chad Rosier24c46ad2016-02-09 18:10:20 +00001504// Find loads and stores that can be merged into a single load or store pair
1505// instruction.
1506bool AArch64LoadStoreOpt::tryToPairLdStInst(MachineBasicBlock::iterator &MBBI) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001507 MachineInstr &MI = *MBBI;
1508 MachineBasicBlock::iterator E = MI.getParent()->end();
Chad Rosier24c46ad2016-02-09 18:10:20 +00001509
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001510 if (!TII->isCandidateToMergeOrPair(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001511 return false;
1512
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001513 // Early exit if the offset is not possible to match. (6 bits of positive
1514 // range, plus allow an extra one in case we find a later insn that matches
1515 // with Offset-1)
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001516 bool IsUnscaled = TII->isUnscaledLdSt(MI);
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001517 int Offset = getLdStOffsetOp(MI).getImm();
1518 int OffsetStride = IsUnscaled ? getMemScale(MI) : 1;
Nirav Dave0f9d1112017-01-04 21:21:46 +00001519 // Allow one more for offset.
1520 if (Offset > 0)
1521 Offset -= OffsetStride;
Chad Rosierfc3bf1f2016-02-10 15:52:46 +00001522 if (!inBoundsForPair(IsUnscaled, Offset, OffsetStride))
1523 return false;
1524
Chad Rosier24c46ad2016-02-09 18:10:20 +00001525 // Look ahead up to LdStLimit instructions for a pairable instruction.
1526 LdStPairFlags Flags;
Jun Bum Limcf974432016-03-31 14:47:24 +00001527 MachineBasicBlock::iterator Paired =
1528 findMatchingInsn(MBBI, Flags, LdStLimit, /* FindNarrowMerge = */ false);
Chad Rosier24c46ad2016-02-09 18:10:20 +00001529 if (Paired != E) {
1530 ++NumPairCreated;
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001531 if (TII->isUnscaledLdSt(MI))
Chad Rosier24c46ad2016-02-09 18:10:20 +00001532 ++NumUnscaledPairCreated;
1533 // Keeping the iterator straight is a pain, so we let the merge routine tell
1534 // us what the next instruction is after it's done mucking about.
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001535 MBBI = mergePairedInsns(MBBI, Paired, Flags);
1536 return true;
1537 }
1538 return false;
1539}
1540
Jun Bum Lim22fe15e2015-11-06 16:27:47 +00001541bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB,
Chad Rosierd6daac42016-11-07 15:27:22 +00001542 bool EnableNarrowZeroStOpt) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001543 bool Modified = false;
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001544 // Four tranformations to do here:
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001545 // 1) Find loads that directly read from stores and promote them by
1546 // replacing with mov instructions. If the store is wider than the load,
1547 // the load will be replaced with a bitfield extract.
1548 // e.g.,
1549 // str w1, [x0, #4]
1550 // ldrh w2, [x0, #6]
1551 // ; becomes
1552 // str w1, [x0, #4]
NAKAMURA Takumife1202c2016-06-20 00:37:41 +00001553 // lsr w2, w1, #16
Tim Northover3b0846e2014-05-24 12:50:23 +00001554 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001555 MBBI != E;) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001556 MachineInstr &MI = *MBBI;
1557 switch (MI.getOpcode()) {
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001558 default:
1559 // Just move on to the next instruction.
1560 ++MBBI;
1561 break;
1562 // Scaled instructions.
1563 case AArch64::LDRBBui:
1564 case AArch64::LDRHHui:
1565 case AArch64::LDRWui:
1566 case AArch64::LDRXui:
1567 // Unscaled instructions.
1568 case AArch64::LDURBBi:
1569 case AArch64::LDURHHi:
1570 case AArch64::LDURWi:
Eugene Zelenko11f69072017-01-25 00:29:26 +00001571 case AArch64::LDURXi:
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001572 if (tryToPromoteLoadFromStore(MBBI)) {
1573 Modified = true;
1574 break;
1575 }
1576 ++MBBI;
1577 break;
1578 }
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001579 }
Chad Rosierd6daac42016-11-07 15:27:22 +00001580 // 2) Merge adjacent zero stores into a wider store.
Jun Bum Lim1de2d442016-02-05 20:02:03 +00001581 // e.g.,
1582 // strh wzr, [x0]
1583 // strh wzr, [x0, #2]
1584 // ; becomes
1585 // str wzr, [x0]
Chad Rosierd6daac42016-11-07 15:27:22 +00001586 // e.g.,
1587 // str wzr, [x0]
1588 // str wzr, [x0, #4]
1589 // ; becomes
1590 // str xzr, [x0]
Jun Bum Lim6755c3b2015-12-22 16:36:16 +00001591 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Chad Rosierd6daac42016-11-07 15:27:22 +00001592 EnableNarrowZeroStOpt && MBBI != E;) {
1593 if (isPromotableZeroStoreInst(*MBBI)) {
1594 if (tryToMergeZeroStInst(MBBI)) {
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001595 Modified = true;
Jun Bum Lim33be4992016-05-06 15:08:57 +00001596 } else
1597 ++MBBI;
1598 } else
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001599 ++MBBI;
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001600 }
Jun Bum Lim33be4992016-05-06 15:08:57 +00001601
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001602 // 3) Find loads and stores that can be merged into a single load or store
1603 // pair instruction.
1604 // e.g.,
1605 // ldr x0, [x2]
1606 // ldr x1, [x2, #8]
1607 // ; becomes
1608 // ldp x0, x1, [x2]
Jun Bum Limc9879ec2015-10-27 19:16:03 +00001609 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
Tim Northover3b0846e2014-05-24 12:50:23 +00001610 MBBI != E;) {
Geoff Berry22dfbc52016-08-12 15:26:00 +00001611 if (TII->isPairableLdStInst(*MBBI) && tryToPairLdStInst(MBBI))
1612 Modified = true;
1613 else
Tim Northover3b0846e2014-05-24 12:50:23 +00001614 ++MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001615 }
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001616 // 4) Find base register updates that can be merged into the load or store
1617 // as a base-reg writeback.
1618 // e.g.,
1619 // ldr x0, [x2]
1620 // add x2, x2, #4
1621 // ; becomes
1622 // ldr x0, [x2], #4
Tim Northover3b0846e2014-05-24 12:50:23 +00001623 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
1624 MBBI != E;) {
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001625 MachineInstr &MI = *MBBI;
Tim Northover3b0846e2014-05-24 12:50:23 +00001626 // Do update merging. It's simpler to keep this separate from the above
Chad Rosierdbdb1d62016-02-01 21:38:31 +00001627 // switchs, though not strictly necessary.
Duncan P. N. Exon Smithab53fd92016-07-08 20:29:42 +00001628 unsigned Opc = MI.getOpcode();
Tim Northover3b0846e2014-05-24 12:50:23 +00001629 switch (Opc) {
1630 default:
1631 // Just move on to the next instruction.
1632 ++MBBI;
1633 break;
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001634 // Scaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001635 case AArch64::STRSui:
1636 case AArch64::STRDui:
1637 case AArch64::STRQui:
1638 case AArch64::STRXui:
1639 case AArch64::STRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001640 case AArch64::STRHHui:
1641 case AArch64::STRBBui:
Tim Northover3b0846e2014-05-24 12:50:23 +00001642 case AArch64::LDRSui:
1643 case AArch64::LDRDui:
1644 case AArch64::LDRQui:
1645 case AArch64::LDRXui:
1646 case AArch64::LDRWui:
Chad Rosierdabe2532015-09-29 18:26:15 +00001647 case AArch64::LDRHHui:
1648 case AArch64::LDRBBui:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001649 // Unscaled instructions.
Tim Northover3b0846e2014-05-24 12:50:23 +00001650 case AArch64::STURSi:
1651 case AArch64::STURDi:
1652 case AArch64::STURQi:
1653 case AArch64::STURWi:
1654 case AArch64::STURXi:
1655 case AArch64::LDURSi:
1656 case AArch64::LDURDi:
1657 case AArch64::LDURQi:
1658 case AArch64::LDURWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001659 case AArch64::LDURXi:
1660 // Paired instructions.
1661 case AArch64::LDPSi:
Chad Rosier43150122015-09-29 20:39:55 +00001662 case AArch64::LDPSWi:
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001663 case AArch64::LDPDi:
1664 case AArch64::LDPQi:
1665 case AArch64::LDPWi:
1666 case AArch64::LDPXi:
1667 case AArch64::STPSi:
1668 case AArch64::STPDi:
1669 case AArch64::STPQi:
1670 case AArch64::STPWi:
1671 case AArch64::STPXi: {
Tim Northover3b0846e2014-05-24 12:50:23 +00001672 // Make sure this is a reg+imm (as opposed to an address reloc).
Chad Rosierf77e9092015-08-06 15:50:12 +00001673 if (!getLdStOffsetOp(MI).isImm()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001674 ++MBBI;
1675 break;
1676 }
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001677 // Look forward to try to form a post-index instruction. For example,
1678 // ldr x0, [x20]
1679 // add x20, x20, #32
1680 // merged into:
1681 // ldr x0, [x20], #32
Tim Northover3b0846e2014-05-24 12:50:23 +00001682 MachineBasicBlock::iterator Update =
Chad Rosier35706ad2016-02-04 21:26:02 +00001683 findMatchingUpdateInsnForward(MBBI, 0, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001684 if (Update != E) {
1685 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001686 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/false);
Tim Northover3b0846e2014-05-24 12:50:23 +00001687 Modified = true;
1688 ++NumPostFolded;
1689 break;
1690 }
1691 // Don't know how to handle pre/post-index versions, so move to the next
1692 // instruction.
Chad Rosiere4e15ba2016-03-09 17:29:48 +00001693 if (TII->isUnscaledLdSt(Opc)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001694 ++MBBI;
1695 break;
1696 }
1697
1698 // Look back to try to find a pre-index instruction. For example,
1699 // add x0, x0, #8
1700 // ldr x1, [x0]
1701 // merged into:
1702 // ldr x1, [x0, #8]!
Chad Rosier35706ad2016-02-04 21:26:02 +00001703 Update = findMatchingUpdateInsnBackward(MBBI, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001704 if (Update != E) {
1705 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001706 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001707 Modified = true;
1708 ++NumPreFolded;
1709 break;
1710 }
Chad Rosier7a83d772015-10-01 13:09:44 +00001711 // The immediate in the load/store is scaled by the size of the memory
1712 // operation. The immediate in the add we're looking for,
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001713 // however, is not, so adjust here.
Chad Rosier0b15e7c2015-10-01 13:33:31 +00001714 int UnscaledOffset = getLdStOffsetOp(MI).getImm() * getMemScale(MI);
Chad Rosier1bbd7fb2015-09-25 17:48:17 +00001715
Tim Northover3b0846e2014-05-24 12:50:23 +00001716 // Look forward to try to find a post-index instruction. For example,
1717 // ldr x1, [x0, #64]
1718 // add x0, x0, #64
1719 // merged into:
1720 // ldr x1, [x0, #64]!
Chad Rosier35706ad2016-02-04 21:26:02 +00001721 Update = findMatchingUpdateInsnForward(MBBI, UnscaledOffset, UpdateLimit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001722 if (Update != E) {
1723 // Merge the update into the ld/st.
Chad Rosier2dfd3542015-09-23 13:51:44 +00001724 MBBI = mergeUpdateInsn(MBBI, Update, /*IsPreIdx=*/true);
Tim Northover3b0846e2014-05-24 12:50:23 +00001725 Modified = true;
1726 ++NumPreFolded;
1727 break;
1728 }
1729
1730 // Nothing found. Just move to the next instruction.
1731 ++MBBI;
1732 break;
1733 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001734 }
1735 }
1736
1737 return Modified;
1738}
1739
1740bool AArch64LoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
Andrew Kaylor1ac98bb2016-04-25 21:58:52 +00001741 if (skipFunction(*Fn.getFunction()))
1742 return false;
1743
Oliver Stannardd414c992015-11-10 11:04:18 +00001744 Subtarget = &static_cast<const AArch64Subtarget &>(Fn.getSubtarget());
1745 TII = static_cast<const AArch64InstrInfo *>(Subtarget->getInstrInfo());
1746 TRI = Subtarget->getRegisterInfo();
Chad Rosiera69dcb62017-03-17 14:19:55 +00001747 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Tim Northover3b0846e2014-05-24 12:50:23 +00001748
Chad Rosierbba881e2016-02-02 15:02:30 +00001749 // Resize the modified and used register bitfield trackers. We do this once
1750 // per function and then clear the bitfield each time we optimize a load or
1751 // store.
1752 ModifiedRegs.resize(TRI->getNumRegs());
1753 UsedRegs.resize(TRI->getNumRegs());
1754
Tim Northover3b0846e2014-05-24 12:50:23 +00001755 bool Modified = false;
Chad Rosier10c7aaa2016-11-11 14:10:12 +00001756 bool enableNarrowZeroStOpt = !Subtarget->requiresStrictAlign();
Tim Northover3b0846e2014-05-24 12:50:23 +00001757 for (auto &MBB : Fn)
Chad Rosierd6daac42016-11-07 15:27:22 +00001758 Modified |= optimizeBlock(MBB, enableNarrowZeroStOpt);
Tim Northover3b0846e2014-05-24 12:50:23 +00001759
1760 return Modified;
1761}
1762
Chad Rosier8ade0342016-11-11 19:52:45 +00001763// FIXME: Do we need/want a pre-alloc pass like ARM has to try to keep loads and
1764// stores near one another? Note: The pre-RA instruction scheduler already has
1765// hooks to try and schedule pairable loads/stores together to improve pairing
1766// opportunities. Thus, pre-RA pairing pass may not be worth the effort.
Tim Northover3b0846e2014-05-24 12:50:23 +00001767
Chad Rosier3f8b09d2016-02-09 19:42:19 +00001768// FIXME: When pairing store instructions it's very possible for this pass to
1769// hoist a store with a KILL marker above another use (without a KILL marker).
1770// The resulting IR is invalid, but nothing uses the KILL markers after this
1771// pass, so it's never caused a problem in practice.
1772
Chad Rosier43f5c842015-08-05 12:40:13 +00001773/// createAArch64LoadStoreOptimizationPass - returns an instance of the
1774/// load / store optimization pass.
Tim Northover3b0846e2014-05-24 12:50:23 +00001775FunctionPass *llvm::createAArch64LoadStoreOptimizationPass() {
1776 return new AArch64LoadStoreOpt();
1777}