blob: bef191fc11e1e5710bccc13afd38ca4fce51690c [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMLoadStoreOptimizer.cpp - ARM load / store opt. pass ------------===//
Evan Cheng10043e22007-01-19 07:51:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
Matthias Braunec50fa62015-06-01 21:26:23 +000010/// \file This file contains a pass that performs load / store related peephole
11/// optimizations. This pass should be run after register allocation.
Evan Cheng10043e22007-01-19 07:51:42 +000012//
13//===----------------------------------------------------------------------===//
14
Evan Cheng10043e22007-01-19 07:51:42 +000015#include "ARM.h"
Evan Cheng2aa91cc2009-08-08 03:20:32 +000016#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000017#include "ARMBaseRegisterInfo.h"
James Molloy556763d2014-05-16 14:14:30 +000018#include "ARMISelLowering.h"
Evan Chengf030f2d2007-03-07 20:30:36 +000019#include "ARMMachineFunctionInfo.h"
Craig Toppera9253262014-03-22 23:51:00 +000020#include "ARMSubtarget.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Eric Christopherae326492015-03-12 22:48:50 +000022#include "ThumbRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallSet.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/Statistic.h"
Evan Cheng10043e22007-01-19 07:51:42 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineFunctionPass.h"
31#include "llvm/CodeGen/MachineInstr.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Cheng185c9ef2009-06-13 09:12:55 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Matthias Brauna4a3182d2015-07-10 18:08:49 +000034#include "llvm/CodeGen/RegisterClassInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000035#include "llvm/CodeGen/SelectionDAGNodes.h"
Matthias Brauna4a3182d2015-07-10 18:08:49 +000036#include "llvm/CodeGen/LivePhysRegs.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/DataLayout.h"
38#include "llvm/IR/DerivedTypes.h"
39#include "llvm/IR/Function.h"
Matthias Brauna4a3182d2015-07-10 18:08:49 +000040#include "llvm/Support/Allocator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000043#include "llvm/Support/raw_ostream.h"
Evan Cheng10043e22007-01-19 07:51:42 +000044#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetMachine.h"
Evan Cheng1283c6a2009-06-15 08:28:29 +000046#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000047using namespace llvm;
48
Chandler Carruth84e68b22014-04-22 02:41:26 +000049#define DEBUG_TYPE "arm-ldst-opt"
50
Evan Cheng10043e22007-01-19 07:51:42 +000051STATISTIC(NumLDMGened , "Number of ldm instructions generated");
52STATISTIC(NumSTMGened , "Number of stm instructions generated");
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000053STATISTIC(NumVLDMGened, "Number of vldm instructions generated");
54STATISTIC(NumVSTMGened, "Number of vstm instructions generated");
Evan Cheng185c9ef2009-06-13 09:12:55 +000055STATISTIC(NumLdStMoved, "Number of load / store instructions moved");
Evan Cheng0e796032009-06-18 02:04:01 +000056STATISTIC(NumLDRDFormed,"Number of ldrd created before allocation");
57STATISTIC(NumSTRDFormed,"Number of strd created before allocation");
58STATISTIC(NumLDRD2LDM, "Number of ldrd instructions turned back into ldm");
59STATISTIC(NumSTRD2STM, "Number of strd instructions turned back into stm");
60STATISTIC(NumLDRD2LDR, "Number of ldrd instructions turned back into ldr's");
61STATISTIC(NumSTRD2STR, "Number of strd instructions turned back into str's");
Evan Cheng185c9ef2009-06-13 09:12:55 +000062
Evan Cheng10043e22007-01-19 07:51:42 +000063namespace {
Matthias Braunec50fa62015-06-01 21:26:23 +000064 /// Post- register allocation pass the combine load / store instructions to
65 /// form ldm / stm instructions.
Nick Lewycky02d5f772009-10-25 06:33:48 +000066 struct ARMLoadStoreOpt : public MachineFunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000067 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +000068 ARMLoadStoreOpt() : MachineFunctionPass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000069
Matthias Brauna4a3182d2015-07-10 18:08:49 +000070 const MachineFunction *MF;
Evan Cheng10043e22007-01-19 07:51:42 +000071 const TargetInstrInfo *TII;
Dan Gohman3a4be0f2008-02-10 18:45:23 +000072 const TargetRegisterInfo *TRI;
Matthias Brauna4a3182d2015-07-10 18:08:49 +000073 const MachineRegisterInfo *MRI;
Evan Chengc3770ac2011-11-08 21:21:09 +000074 const ARMSubtarget *STI;
James Molloy556763d2014-05-16 14:14:30 +000075 const TargetLowering *TL;
Evan Chengf030f2d2007-03-07 20:30:36 +000076 ARMFunctionInfo *AFI;
Matthias Brauna4a3182d2015-07-10 18:08:49 +000077 LivePhysRegs LiveRegs;
78 RegisterClassInfo RegClassInfo;
79 MachineBasicBlock::const_iterator LiveRegPos;
80 bool LiveRegsValid;
81 bool RegClassInfoValid;
James Molloy92a15072014-05-16 14:11:38 +000082 bool isThumb1, isThumb2;
Evan Cheng10043e22007-01-19 07:51:42 +000083
Craig Topper6bc27bf2014-03-10 02:09:33 +000084 bool runOnMachineFunction(MachineFunction &Fn) override;
Evan Cheng10043e22007-01-19 07:51:42 +000085
Craig Topper6bc27bf2014-03-10 02:09:33 +000086 const char *getPassName() const override {
Evan Cheng10043e22007-01-19 07:51:42 +000087 return "ARM load / store optimization pass";
88 }
89
90 private:
Matthias Brauna4a3182d2015-07-10 18:08:49 +000091 /// A set of load/store MachineInstrs with same base register sorted by
92 /// offset.
Evan Cheng10043e22007-01-19 07:51:42 +000093 struct MemOpQueueEntry {
Matthias Brauna4a3182d2015-07-10 18:08:49 +000094 MachineInstr *MI;
95 int Offset; ///< Load/Store offset.
96 unsigned Position; ///< Position as counted from end of basic block.
97 MemOpQueueEntry(MachineInstr *MI, int Offset, unsigned Position)
98 : MI(MI), Offset(Offset), Position(Position) {}
Evan Cheng10043e22007-01-19 07:51:42 +000099 };
100 typedef SmallVector<MemOpQueueEntry,8> MemOpQueue;
Evan Cheng10043e22007-01-19 07:51:42 +0000101
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000102 /// A set of MachineInstrs that fulfill (nearly all) conditions to get
103 /// merged into a LDM/STM.
104 struct MergeCandidate {
105 /// List of instructions ordered by load/store offset.
106 SmallVector<MachineInstr*, 4> Instrs;
107 /// Index in Instrs of the instruction being latest in the schedule.
108 unsigned LatestMIIdx;
109 /// Index in Instrs of the instruction being earliest in the schedule.
110 unsigned EarliestMIIdx;
111 /// Index into the basic block where the merged instruction will be
112 /// inserted. (See MemOpQueueEntry.Position)
113 unsigned InsertPos;
114 };
Matthias Braun22f39602015-07-20 23:17:14 +0000115 BumpPtrAllocator Allocator;
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000116 SmallVector<const MergeCandidate*,4> Candidates;
117
118 void moveLiveRegsBefore(const MachineBasicBlock &MBB,
119 MachineBasicBlock::const_iterator Before);
120 unsigned findFreeReg(const TargetRegisterClass &RegClass);
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000121 void UpdateBaseRegUses(MachineBasicBlock &MBB,
122 MachineBasicBlock::iterator MBBI,
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000123 DebugLoc DL, unsigned Base, unsigned WordOffset,
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000124 ARMCC::CondCodes Pred, unsigned PredReg);
Matthias Braun731e3592015-07-20 23:17:20 +0000125 MachineInstr *MergeOps(MachineBasicBlock &MBB,
126 MachineBasicBlock::iterator InsertBefore, int Offset,
127 unsigned Base, bool BaseKill, unsigned Opcode,
128 ARMCC::CondCodes Pred, unsigned PredReg, DebugLoc DL,
129 ArrayRef<std::pair<unsigned, bool>> Regs);
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000130 void FormCandidates(const MemOpQueue &MemOps);
131 MachineInstr *MergeOpsUpdate(const MergeCandidate &Cand);
Evan Cheng1283c6a2009-06-15 08:28:29 +0000132 bool FixInvalidRegPairOp(MachineBasicBlock &MBB,
133 MachineBasicBlock::iterator &MBBI);
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000134 bool MergeBaseUpdateLoadStore(MachineInstr *MI);
135 bool MergeBaseUpdateLSMultiple(MachineInstr *MI);
Evan Cheng10043e22007-01-19 07:51:42 +0000136 bool LoadStoreMultipleOpti(MachineBasicBlock &MBB);
137 bool MergeReturnIntoLDM(MachineBasicBlock &MBB);
138 };
Devang Patel8c78a0b2007-05-03 01:11:54 +0000139 char ARMLoadStoreOpt::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000140}
Evan Cheng10043e22007-01-19 07:51:42 +0000141
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000142static bool definesCPSR(const MachineInstr *MI) {
143 for (const auto &MO : MI->operands()) {
144 if (!MO.isReg())
145 continue;
146 if (MO.isDef() && MO.getReg() == ARM::CPSR && !MO.isDead())
147 // If the instruction has live CPSR def, then it's not safe to fold it
148 // into load / store.
149 return true;
150 }
151
152 return false;
153}
154
155static int getMemoryOpOffset(const MachineInstr *MI) {
Matthias Braunfa3872e2015-05-18 20:27:55 +0000156 unsigned Opcode = MI->getOpcode();
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000157 bool isAM3 = Opcode == ARM::LDRD || Opcode == ARM::STRD;
158 unsigned NumOperands = MI->getDesc().getNumOperands();
159 unsigned OffField = MI->getOperand(NumOperands-3).getImm();
160
161 if (Opcode == ARM::t2LDRi12 || Opcode == ARM::t2LDRi8 ||
162 Opcode == ARM::t2STRi12 || Opcode == ARM::t2STRi8 ||
163 Opcode == ARM::t2LDRDi8 || Opcode == ARM::t2STRDi8 ||
164 Opcode == ARM::LDRi12 || Opcode == ARM::STRi12)
165 return OffField;
166
167 // Thumb1 immediate offsets are scaled by 4
Renato Golinb9887ef2015-02-25 14:41:06 +0000168 if (Opcode == ARM::tLDRi || Opcode == ARM::tSTRi ||
169 Opcode == ARM::tLDRspi || Opcode == ARM::tSTRspi)
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000170 return OffField * 4;
171
172 int Offset = isAM3 ? ARM_AM::getAM3Offset(OffField)
173 : ARM_AM::getAM5Offset(OffField) * 4;
174 ARM_AM::AddrOpc Op = isAM3 ? ARM_AM::getAM3Op(OffField)
175 : ARM_AM::getAM5Op(OffField);
176
177 if (Op == ARM_AM::sub)
178 return -Offset;
179
180 return Offset;
181}
182
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000183static const MachineOperand &getLoadStoreBaseOp(const MachineInstr &MI) {
184 return MI.getOperand(1);
185}
186
187static const MachineOperand &getLoadStoreRegOp(const MachineInstr &MI) {
188 return MI.getOperand(0);
189}
190
Matthias Braunfa3872e2015-05-18 20:27:55 +0000191static int getLoadStoreMultipleOpcode(unsigned Opcode, ARM_AM::AMSubMode Mode) {
Evan Cheng10043e22007-01-19 07:51:42 +0000192 switch (Opcode) {
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000193 default: llvm_unreachable("Unhandled opcode!");
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000194 case ARM::LDRi12:
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000195 ++NumLDMGened;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000196 switch (Mode) {
197 default: llvm_unreachable("Unhandled submode!");
198 case ARM_AM::ia: return ARM::LDMIA;
199 case ARM_AM::da: return ARM::LDMDA;
200 case ARM_AM::db: return ARM::LDMDB;
201 case ARM_AM::ib: return ARM::LDMIB;
202 }
Jim Grosbach338de3e2010-10-27 23:12:14 +0000203 case ARM::STRi12:
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000204 ++NumSTMGened;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000205 switch (Mode) {
206 default: llvm_unreachable("Unhandled submode!");
207 case ARM_AM::ia: return ARM::STMIA;
208 case ARM_AM::da: return ARM::STMDA;
209 case ARM_AM::db: return ARM::STMDB;
210 case ARM_AM::ib: return ARM::STMIB;
211 }
James Molloy556763d2014-05-16 14:14:30 +0000212 case ARM::tLDRi:
Renato Golinb9887ef2015-02-25 14:41:06 +0000213 case ARM::tLDRspi:
James Molloy556763d2014-05-16 14:14:30 +0000214 // tLDMIA is writeback-only - unless the base register is in the input
215 // reglist.
216 ++NumLDMGened;
217 switch (Mode) {
218 default: llvm_unreachable("Unhandled submode!");
219 case ARM_AM::ia: return ARM::tLDMIA;
220 }
221 case ARM::tSTRi:
Renato Golinb9887ef2015-02-25 14:41:06 +0000222 case ARM::tSTRspi:
James Molloy556763d2014-05-16 14:14:30 +0000223 // There is no non-writeback tSTMIA either.
224 ++NumSTMGened;
225 switch (Mode) {
226 default: llvm_unreachable("Unhandled submode!");
227 case ARM_AM::ia: return ARM::tSTMIA_UPD;
228 }
Evan Cheng4605e8a2009-07-09 23:11:34 +0000229 case ARM::t2LDRi8:
230 case ARM::t2LDRi12:
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000231 ++NumLDMGened;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000232 switch (Mode) {
233 default: llvm_unreachable("Unhandled submode!");
234 case ARM_AM::ia: return ARM::t2LDMIA;
235 case ARM_AM::db: return ARM::t2LDMDB;
236 }
Evan Cheng4605e8a2009-07-09 23:11:34 +0000237 case ARM::t2STRi8:
238 case ARM::t2STRi12:
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000239 ++NumSTMGened;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000240 switch (Mode) {
241 default: llvm_unreachable("Unhandled submode!");
242 case ARM_AM::ia: return ARM::t2STMIA;
243 case ARM_AM::db: return ARM::t2STMDB;
244 }
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000245 case ARM::VLDRS:
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000246 ++NumVLDMGened;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000247 switch (Mode) {
248 default: llvm_unreachable("Unhandled submode!");
249 case ARM_AM::ia: return ARM::VLDMSIA;
Owen Andersond6c5a742011-03-29 16:45:53 +0000250 case ARM_AM::db: return 0; // Only VLDMSDB_UPD exists.
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000251 }
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000252 case ARM::VSTRS:
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000253 ++NumVSTMGened;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000254 switch (Mode) {
255 default: llvm_unreachable("Unhandled submode!");
256 case ARM_AM::ia: return ARM::VSTMSIA;
Owen Andersond6c5a742011-03-29 16:45:53 +0000257 case ARM_AM::db: return 0; // Only VSTMSDB_UPD exists.
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000258 }
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000259 case ARM::VLDRD:
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000260 ++NumVLDMGened;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000261 switch (Mode) {
262 default: llvm_unreachable("Unhandled submode!");
263 case ARM_AM::ia: return ARM::VLDMDIA;
Owen Andersond6c5a742011-03-29 16:45:53 +0000264 case ARM_AM::db: return 0; // Only VLDMDDB_UPD exists.
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000265 }
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000266 case ARM::VSTRD:
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000267 ++NumVSTMGened;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000268 switch (Mode) {
269 default: llvm_unreachable("Unhandled submode!");
270 case ARM_AM::ia: return ARM::VSTMDIA;
Owen Andersond6c5a742011-03-29 16:45:53 +0000271 case ARM_AM::db: return 0; // Only VSTMDDB_UPD exists.
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000272 }
Evan Cheng10043e22007-01-19 07:51:42 +0000273 }
Evan Cheng10043e22007-01-19 07:51:42 +0000274}
275
Benjamin Kramer113b2a92015-06-05 14:32:54 +0000276static ARM_AM::AMSubMode getLoadStoreMultipleSubMode(unsigned Opcode) {
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000277 switch (Opcode) {
278 default: llvm_unreachable("Unhandled opcode!");
Bill Wendlingb9bd5942010-11-18 19:44:29 +0000279 case ARM::LDMIA_RET:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000280 case ARM::LDMIA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000281 case ARM::LDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000282 case ARM::STMIA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000283 case ARM::STMIA_UPD:
James Molloy556763d2014-05-16 14:14:30 +0000284 case ARM::tLDMIA:
285 case ARM::tLDMIA_UPD:
286 case ARM::tSTMIA_UPD:
Bill Wendlingb9bd5942010-11-18 19:44:29 +0000287 case ARM::t2LDMIA_RET:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000288 case ARM::t2LDMIA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000289 case ARM::t2LDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000290 case ARM::t2STMIA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000291 case ARM::t2STMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000292 case ARM::VLDMSIA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000293 case ARM::VLDMSIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000294 case ARM::VSTMSIA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000295 case ARM::VSTMSIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000296 case ARM::VLDMDIA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000297 case ARM::VLDMDIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000298 case ARM::VSTMDIA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000299 case ARM::VSTMDIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000300 return ARM_AM::ia;
301
302 case ARM::LDMDA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000303 case ARM::LDMDA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000304 case ARM::STMDA:
Bill Wendling11cc1762010-11-17 19:16:20 +0000305 case ARM::STMDA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000306 return ARM_AM::da;
307
308 case ARM::LDMDB:
Bill Wendling11cc1762010-11-17 19:16:20 +0000309 case ARM::LDMDB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000310 case ARM::STMDB:
Bill Wendling11cc1762010-11-17 19:16:20 +0000311 case ARM::STMDB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000312 case ARM::t2LDMDB:
Bill Wendling11cc1762010-11-17 19:16:20 +0000313 case ARM::t2LDMDB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000314 case ARM::t2STMDB:
Bill Wendling11cc1762010-11-17 19:16:20 +0000315 case ARM::t2STMDB_UPD:
Bill Wendling11cc1762010-11-17 19:16:20 +0000316 case ARM::VLDMSDB_UPD:
Bill Wendling11cc1762010-11-17 19:16:20 +0000317 case ARM::VSTMSDB_UPD:
Bill Wendling11cc1762010-11-17 19:16:20 +0000318 case ARM::VLDMDDB_UPD:
Bill Wendling11cc1762010-11-17 19:16:20 +0000319 case ARM::VSTMDDB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000320 return ARM_AM::db;
321
322 case ARM::LDMIB:
Bill Wendling11cc1762010-11-17 19:16:20 +0000323 case ARM::LDMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000324 case ARM::STMIB:
Bill Wendling11cc1762010-11-17 19:16:20 +0000325 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000326 return ARM_AM::ib;
327 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000328}
329
James Molloy556763d2014-05-16 14:14:30 +0000330static bool isT1i32Load(unsigned Opc) {
Renato Golinb9887ef2015-02-25 14:41:06 +0000331 return Opc == ARM::tLDRi || Opc == ARM::tLDRspi;
James Molloy556763d2014-05-16 14:14:30 +0000332}
333
Evan Cheng71756e72009-08-04 01:43:45 +0000334static bool isT2i32Load(unsigned Opc) {
335 return Opc == ARM::t2LDRi12 || Opc == ARM::t2LDRi8;
336}
337
Evan Cheng4605e8a2009-07-09 23:11:34 +0000338static bool isi32Load(unsigned Opc) {
James Molloy556763d2014-05-16 14:14:30 +0000339 return Opc == ARM::LDRi12 || isT1i32Load(Opc) || isT2i32Load(Opc) ;
340}
341
342static bool isT1i32Store(unsigned Opc) {
Renato Golinb9887ef2015-02-25 14:41:06 +0000343 return Opc == ARM::tSTRi || Opc == ARM::tSTRspi;
Evan Cheng71756e72009-08-04 01:43:45 +0000344}
345
346static bool isT2i32Store(unsigned Opc) {
347 return Opc == ARM::t2STRi12 || Opc == ARM::t2STRi8;
Evan Cheng4605e8a2009-07-09 23:11:34 +0000348}
349
350static bool isi32Store(unsigned Opc) {
James Molloy556763d2014-05-16 14:14:30 +0000351 return Opc == ARM::STRi12 || isT1i32Store(Opc) || isT2i32Store(Opc);
352}
353
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000354static bool isLoadSingle(unsigned Opc) {
355 return isi32Load(Opc) || Opc == ARM::VLDRS || Opc == ARM::VLDRD;
356}
357
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000358static unsigned getImmScale(unsigned Opc) {
359 switch (Opc) {
360 default: llvm_unreachable("Unhandled opcode!");
361 case ARM::tLDRi:
362 case ARM::tSTRi:
Renato Golinb9887ef2015-02-25 14:41:06 +0000363 case ARM::tLDRspi:
364 case ARM::tSTRspi:
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000365 return 1;
366 case ARM::tLDRHi:
367 case ARM::tSTRHi:
368 return 2;
369 case ARM::tLDRBi:
370 case ARM::tSTRBi:
371 return 4;
372 }
373}
374
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000375static unsigned getLSMultipleTransferSize(const MachineInstr *MI) {
376 switch (MI->getOpcode()) {
377 default: return 0;
378 case ARM::LDRi12:
379 case ARM::STRi12:
380 case ARM::tLDRi:
381 case ARM::tSTRi:
382 case ARM::tLDRspi:
383 case ARM::tSTRspi:
384 case ARM::t2LDRi8:
385 case ARM::t2LDRi12:
386 case ARM::t2STRi8:
387 case ARM::t2STRi12:
388 case ARM::VLDRS:
389 case ARM::VSTRS:
390 return 4;
391 case ARM::VLDRD:
392 case ARM::VSTRD:
393 return 8;
394 case ARM::LDMIA:
395 case ARM::LDMDA:
396 case ARM::LDMDB:
397 case ARM::LDMIB:
398 case ARM::STMIA:
399 case ARM::STMDA:
400 case ARM::STMDB:
401 case ARM::STMIB:
402 case ARM::tLDMIA:
403 case ARM::tLDMIA_UPD:
404 case ARM::tSTMIA_UPD:
405 case ARM::t2LDMIA:
406 case ARM::t2LDMDB:
407 case ARM::t2STMIA:
408 case ARM::t2STMDB:
409 case ARM::VLDMSIA:
410 case ARM::VSTMSIA:
411 return (MI->getNumOperands() - MI->getDesc().getNumOperands() + 1) * 4;
412 case ARM::VLDMDIA:
413 case ARM::VSTMDIA:
414 return (MI->getNumOperands() - MI->getDesc().getNumOperands() + 1) * 8;
415 }
416}
417
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000418/// Update future uses of the base register with the offset introduced
419/// due to writeback. This function only works on Thumb1.
420void
421ARMLoadStoreOpt::UpdateBaseRegUses(MachineBasicBlock &MBB,
422 MachineBasicBlock::iterator MBBI,
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000423 DebugLoc DL, unsigned Base,
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000424 unsigned WordOffset,
425 ARMCC::CondCodes Pred, unsigned PredReg) {
426 assert(isThumb1 && "Can only update base register uses for Thumb1!");
427 // Start updating any instructions with immediate offsets. Insert a SUB before
428 // the first non-updateable instruction (if any).
429 for (; MBBI != MBB.end(); ++MBBI) {
430 bool InsertSub = false;
431 unsigned Opc = MBBI->getOpcode();
432
433 if (MBBI->readsRegister(Base)) {
434 int Offset;
435 bool IsLoad =
436 Opc == ARM::tLDRi || Opc == ARM::tLDRHi || Opc == ARM::tLDRBi;
437 bool IsStore =
438 Opc == ARM::tSTRi || Opc == ARM::tSTRHi || Opc == ARM::tSTRBi;
439
440 if (IsLoad || IsStore) {
441 // Loads and stores with immediate offsets can be updated, but only if
442 // the new offset isn't negative.
443 // The MachineOperand containing the offset immediate is the last one
444 // before predicates.
445 MachineOperand &MO =
446 MBBI->getOperand(MBBI->getDesc().getNumOperands() - 3);
447 // The offsets are scaled by 1, 2 or 4 depending on the Opcode.
448 Offset = MO.getImm() - WordOffset * getImmScale(Opc);
449
450 // If storing the base register, it needs to be reset first.
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000451 unsigned InstrSrcReg = getLoadStoreRegOp(*MBBI).getReg();
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000452
453 if (Offset >= 0 && !(IsStore && InstrSrcReg == Base))
454 MO.setImm(Offset);
455 else
456 InsertSub = true;
457
458 } else if ((Opc == ARM::tSUBi8 || Opc == ARM::tADDi8) &&
459 !definesCPSR(MBBI)) {
460 // SUBS/ADDS using this register, with a dead def of the CPSR.
461 // Merge it with the update; if the merged offset is too large,
462 // insert a new sub instead.
463 MachineOperand &MO =
464 MBBI->getOperand(MBBI->getDesc().getNumOperands() - 3);
465 Offset = (Opc == ARM::tSUBi8) ?
466 MO.getImm() + WordOffset * 4 :
467 MO.getImm() - WordOffset * 4 ;
468 if (Offset >= 0 && TL->isLegalAddImmediate(Offset)) {
469 // FIXME: Swap ADDS<->SUBS if Offset < 0, erase instruction if
470 // Offset == 0.
471 MO.setImm(Offset);
472 // The base register has now been reset, so exit early.
473 return;
474 } else {
475 InsertSub = true;
476 }
477
478 } else {
479 // Can't update the instruction.
480 InsertSub = true;
481 }
482
483 } else if (definesCPSR(MBBI) || MBBI->isCall() || MBBI->isBranch()) {
484 // Since SUBS sets the condition flags, we can't place the base reset
485 // after an instruction that has a live CPSR def.
486 // The base register might also contain an argument for a function call.
487 InsertSub = true;
488 }
489
490 if (InsertSub) {
491 // An instruction above couldn't be updated, so insert a sub.
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000492 AddDefaultT1CC(BuildMI(MBB, MBBI, DL, TII->get(ARM::tSUBi8), Base), true)
Matthias Braunaa9fa352015-05-27 05:12:40 +0000493 .addReg(Base).addImm(WordOffset * 4).addImm(Pred).addReg(PredReg);
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000494 return;
495 }
496
John Brawnd86e0042015-06-23 16:02:11 +0000497 if (MBBI->killsRegister(Base) || MBBI->definesRegister(Base))
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000498 // Register got killed. Stop updating.
499 return;
500 }
501
502 // End of block was reached.
503 if (MBB.succ_size() > 0) {
504 // FIXME: Because of a bug, live registers are sometimes missing from
505 // the successor blocks' live-in sets. This means we can't trust that
506 // information and *always* have to reset at the end of a block.
507 // See PR21029.
508 if (MBBI != MBB.end()) --MBBI;
509 AddDefaultT1CC(
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000510 BuildMI(MBB, MBBI, DL, TII->get(ARM::tSUBi8), Base), true)
Matthias Braunaa9fa352015-05-27 05:12:40 +0000511 .addReg(Base).addImm(WordOffset * 4).addImm(Pred).addReg(PredReg);
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000512 }
513}
514
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000515/// Return the first register of class \p RegClass that is not in \p Regs.
516unsigned ARMLoadStoreOpt::findFreeReg(const TargetRegisterClass &RegClass) {
517 if (!RegClassInfoValid) {
518 RegClassInfo.runOnMachineFunction(*MF);
519 RegClassInfoValid = true;
520 }
521
522 for (unsigned Reg : RegClassInfo.getOrder(&RegClass))
523 if (!LiveRegs.contains(Reg))
524 return Reg;
525 return 0;
526}
527
528/// Compute live registers just before instruction \p Before (in normal schedule
529/// direction). Computes backwards so multiple queries in the same block must
530/// come in reverse order.
531void ARMLoadStoreOpt::moveLiveRegsBefore(const MachineBasicBlock &MBB,
532 MachineBasicBlock::const_iterator Before) {
533 // Initialize if we never queried in this block.
534 if (!LiveRegsValid) {
535 LiveRegs.init(TRI);
536 LiveRegs.addLiveOuts(&MBB, true);
537 LiveRegPos = MBB.end();
538 LiveRegsValid = true;
539 }
540 // Move backward just before the "Before" position.
541 while (LiveRegPos != Before) {
542 --LiveRegPos;
543 LiveRegs.stepBackward(*LiveRegPos);
544 }
545}
546
547static bool ContainsReg(const ArrayRef<std::pair<unsigned, bool>> &Regs,
548 unsigned Reg) {
549 for (const std::pair<unsigned, bool> &R : Regs)
550 if (R.first == Reg)
551 return true;
552 return false;
553}
554
Matthias Braunec50fa62015-06-01 21:26:23 +0000555/// Create and insert a LDM or STM with Base as base register and registers in
556/// Regs as the register operands that would be loaded / stored. It returns
557/// true if the transformation is done.
Matthias Braun731e3592015-07-20 23:17:20 +0000558MachineInstr *
559ARMLoadStoreOpt::MergeOps(MachineBasicBlock &MBB,
560 MachineBasicBlock::iterator InsertBefore, int Offset,
561 unsigned Base, bool BaseKill, unsigned Opcode,
562 ARMCC::CondCodes Pred, unsigned PredReg, DebugLoc DL,
563 ArrayRef<std::pair<unsigned, bool>> Regs) {
Evan Cheng10043e22007-01-19 07:51:42 +0000564 unsigned NumRegs = Regs.size();
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000565 assert(NumRegs > 1);
Evan Cheng10043e22007-01-19 07:51:42 +0000566
Moritz Rotheef9f4d2014-09-16 16:25:07 +0000567 // For Thumb1 targets, it might be necessary to clobber the CPSR to merge.
568 // Compute liveness information for that register to make the decision.
569 bool SafeToClobberCPSR = !isThumb1 ||
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000570 (MBB.computeRegisterLiveness(TRI, ARM::CPSR, InsertBefore, 20) ==
Moritz Rotheef9f4d2014-09-16 16:25:07 +0000571 MachineBasicBlock::LQR_Dead);
572
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000573 bool Writeback = isThumb1; // Thumb1 LDM/STM have base reg writeback.
574
575 // Exception: If the base register is in the input reglist, Thumb1 LDM is
576 // non-writeback.
577 // It's also not possible to merge an STR of the base register in Thumb1.
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000578 if (isThumb1 && isi32Load(Opcode) && ContainsReg(Regs, Base)) {
579 assert(Base != ARM::SP && "Thumb1 does not allow SP in register list");
580 if (Opcode == ARM::tLDRi) {
581 Writeback = false;
582 } else if (Opcode == ARM::tSTRi) {
583 return nullptr;
584 }
585 }
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000586
Evan Cheng10043e22007-01-19 07:51:42 +0000587 ARM_AM::AMSubMode Mode = ARM_AM::ia;
James Molloy556763d2014-05-16 14:14:30 +0000588 // VFP and Thumb2 do not support IB or DA modes. Thumb1 only supports IA.
Bob Wilson13ce07f2010-08-27 23:18:17 +0000589 bool isNotVFP = isi32Load(Opcode) || isi32Store(Opcode);
James Molloy556763d2014-05-16 14:14:30 +0000590 bool haveIBAndDA = isNotVFP && !isThumb2 && !isThumb1;
591
James Molloybb73c232014-05-16 14:08:46 +0000592 if (Offset == 4 && haveIBAndDA) {
Evan Cheng10043e22007-01-19 07:51:42 +0000593 Mode = ARM_AM::ib;
James Molloybb73c232014-05-16 14:08:46 +0000594 } else if (Offset == -4 * (int)NumRegs + 4 && haveIBAndDA) {
Evan Cheng10043e22007-01-19 07:51:42 +0000595 Mode = ARM_AM::da;
James Molloy556763d2014-05-16 14:14:30 +0000596 } else if (Offset == -4 * (int)NumRegs && isNotVFP && !isThumb1) {
Bob Wilsonca5af122010-08-27 23:57:52 +0000597 // VLDM/VSTM do not support DB mode without also updating the base reg.
Evan Cheng10043e22007-01-19 07:51:42 +0000598 Mode = ARM_AM::db;
Renato Golinb9887ef2015-02-25 14:41:06 +0000599 } else if (Offset != 0 || Opcode == ARM::tLDRspi || Opcode == ARM::tSTRspi) {
James Molloybb73c232014-05-16 14:08:46 +0000600 // Check if this is a supported opcode before inserting instructions to
Owen Anderson7ac53ad2011-03-29 20:27:38 +0000601 // calculate a new base register.
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000602 if (!getLoadStoreMultipleOpcode(Opcode, Mode)) return nullptr;
Owen Anderson7ac53ad2011-03-29 20:27:38 +0000603
Evan Cheng10043e22007-01-19 07:51:42 +0000604 // If starting offset isn't zero, insert a MI to materialize a new base.
605 // But only do so if it is cost effective, i.e. merging more than two
606 // loads / stores.
607 if (NumRegs <= 2)
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000608 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +0000609
Moritz Rotheef9f4d2014-09-16 16:25:07 +0000610 // On Thumb1, it's not worth materializing a new base register without
611 // clobbering the CPSR (i.e. not using ADDS/SUBS).
612 if (!SafeToClobberCPSR)
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000613 return nullptr;
Moritz Rotheef9f4d2014-09-16 16:25:07 +0000614
Evan Cheng10043e22007-01-19 07:51:42 +0000615 unsigned NewBase;
James Molloybb73c232014-05-16 14:08:46 +0000616 if (isi32Load(Opcode)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000617 // If it is a load, then just use one of the destination register to
618 // use as the new base.
Evan Cheng41bc2fd2007-03-06 21:59:20 +0000619 NewBase = Regs[NumRegs-1].first;
James Molloybb73c232014-05-16 14:08:46 +0000620 } else {
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000621 // Find a free register that we can use as scratch register.
622 moveLiveRegsBefore(MBB, InsertBefore);
623 // The merged instruction does not exist yet but will use several Regs if
624 // it is a Store.
625 if (!isLoadSingle(Opcode))
626 for (const std::pair<unsigned, bool> &R : Regs)
627 LiveRegs.addReg(R.first);
628
629 NewBase = findFreeReg(isThumb1 ? ARM::tGPRRegClass : ARM::GPRRegClass);
Evan Cheng41bc2fd2007-03-06 21:59:20 +0000630 if (NewBase == 0)
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000631 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +0000632 }
James Molloy556763d2014-05-16 14:14:30 +0000633
634 int BaseOpc =
635 isThumb2 ? ARM::t2ADDri :
Renato Golinb9887ef2015-02-25 14:41:06 +0000636 (isThumb1 && Base == ARM::SP) ? ARM::tADDrSPi :
Moritz Rothdfdda0d2014-08-21 17:11:03 +0000637 (isThumb1 && Offset < 8) ? ARM::tADDi3 :
James Molloy556763d2014-05-16 14:14:30 +0000638 isThumb1 ? ARM::tADDi8 : ARM::ADDri;
639
Evan Cheng10043e22007-01-19 07:51:42 +0000640 if (Offset < 0) {
Moritz Rothdfdda0d2014-08-21 17:11:03 +0000641 Offset = - Offset;
James Molloy556763d2014-05-16 14:14:30 +0000642 BaseOpc =
643 isThumb2 ? ARM::t2SUBri :
Renato Golinb9887ef2015-02-25 14:41:06 +0000644 (isThumb1 && Offset < 8 && Base != ARM::SP) ? ARM::tSUBi3 :
James Molloy556763d2014-05-16 14:14:30 +0000645 isThumb1 ? ARM::tSUBi8 : ARM::SUBri;
Evan Cheng10043e22007-01-19 07:51:42 +0000646 }
Evan Cheng41bc2fd2007-03-06 21:59:20 +0000647
James Molloy556763d2014-05-16 14:14:30 +0000648 if (!TL->isLegalAddImmediate(Offset))
649 // FIXME: Try add with register operand?
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000650 return nullptr; // Probably not worth it then.
651
652 // We can only append a kill flag to the add/sub input if the value is not
653 // used in the register list of the stm as well.
654 bool KillOldBase = BaseKill &&
655 (!isi32Store(Opcode) || !ContainsReg(Regs, Base));
James Molloy556763d2014-05-16 14:14:30 +0000656
657 if (isThumb1) {
Moritz Rothdfdda0d2014-08-21 17:11:03 +0000658 // Thumb1: depending on immediate size, use either
Moritz Rotheef9f4d2014-09-16 16:25:07 +0000659 // ADDS NewBase, Base, #imm3
Moritz Rothdfdda0d2014-08-21 17:11:03 +0000660 // or
Moritz Rotheef9f4d2014-09-16 16:25:07 +0000661 // MOV NewBase, Base
662 // ADDS NewBase, #imm8.
Renato Golinb9887ef2015-02-25 14:41:06 +0000663 if (Base != NewBase &&
664 (BaseOpc == ARM::tADDi8 || BaseOpc == ARM::tSUBi8)) {
James Molloy556763d2014-05-16 14:14:30 +0000665 // Need to insert a MOV to the new base first.
Jonathan Roelofs229eb4c2015-01-21 22:39:43 +0000666 if (isARMLowRegister(NewBase) && isARMLowRegister(Base) &&
Eric Christopher1b21f002015-01-29 00:19:33 +0000667 !STI->hasV6Ops()) {
Jonathan Roelofs229eb4c2015-01-21 22:39:43 +0000668 // thumbv4t doesn't have lo->lo copies, and we can't predicate tMOVSr
669 if (Pred != ARMCC::AL)
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000670 return nullptr;
671 BuildMI(MBB, InsertBefore, DL, TII->get(ARM::tMOVSr), NewBase)
672 .addReg(Base, getKillRegState(KillOldBase));
Jonathan Roelofs229eb4c2015-01-21 22:39:43 +0000673 } else
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000674 BuildMI(MBB, InsertBefore, DL, TII->get(ARM::tMOVr), NewBase)
675 .addReg(Base, getKillRegState(KillOldBase))
Jonathan Roelofs229eb4c2015-01-21 22:39:43 +0000676 .addImm(Pred).addReg(PredReg);
677
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000678 // The following ADDS/SUBS becomes an update.
Moritz Rothdfdda0d2014-08-21 17:11:03 +0000679 Base = NewBase;
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000680 KillOldBase = true;
James Molloy556763d2014-05-16 14:14:30 +0000681 }
Renato Golinb9887ef2015-02-25 14:41:06 +0000682 if (BaseOpc == ARM::tADDrSPi) {
683 assert(Offset % 4 == 0 && "tADDrSPi offset is scaled by 4");
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000684 BuildMI(MBB, InsertBefore, DL, TII->get(BaseOpc), NewBase)
685 .addReg(Base, getKillRegState(KillOldBase)).addImm(Offset/4)
Renato Golinb9887ef2015-02-25 14:41:06 +0000686 .addImm(Pred).addReg(PredReg);
687 } else
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000688 AddDefaultT1CC(
689 BuildMI(MBB, InsertBefore, DL, TII->get(BaseOpc), NewBase), true)
690 .addReg(Base, getKillRegState(KillOldBase)).addImm(Offset)
Renato Golinb9887ef2015-02-25 14:41:06 +0000691 .addImm(Pred).addReg(PredReg);
James Molloy556763d2014-05-16 14:14:30 +0000692 } else {
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000693 BuildMI(MBB, InsertBefore, DL, TII->get(BaseOpc), NewBase)
694 .addReg(Base, getKillRegState(KillOldBase)).addImm(Offset)
James Molloy556763d2014-05-16 14:14:30 +0000695 .addImm(Pred).addReg(PredReg).addReg(0);
696 }
Evan Cheng10043e22007-01-19 07:51:42 +0000697 Base = NewBase;
James Molloybb73c232014-05-16 14:08:46 +0000698 BaseKill = true; // New base is always killed straight away.
Evan Cheng10043e22007-01-19 07:51:42 +0000699 }
700
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000701 bool isDef = isLoadSingle(Opcode);
James Molloy556763d2014-05-16 14:14:30 +0000702
703 // Get LS multiple opcode. Note that for Thumb1 this might be an opcode with
704 // base register writeback.
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000705 Opcode = getLoadStoreMultipleOpcode(Opcode, Mode);
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000706 if (!Opcode)
707 return nullptr;
James Molloy556763d2014-05-16 14:14:30 +0000708
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000709 // Check if a Thumb1 LDM/STM merge is safe. This is the case if:
710 // - There is no writeback (LDM of base register),
711 // - the base register is killed by the merged instruction,
712 // - or it's safe to overwrite the condition flags, i.e. to insert a SUBS
713 // to reset the base register.
714 // Otherwise, don't merge.
715 // It's safe to return here since the code to materialize a new base register
716 // above is also conditional on SafeToClobberCPSR.
717 if (isThumb1 && !SafeToClobberCPSR && Writeback && !BaseKill)
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000718 return nullptr;
Moritz Roth8f376562014-08-15 17:00:30 +0000719
James Molloy556763d2014-05-16 14:14:30 +0000720 MachineInstrBuilder MIB;
721
722 if (Writeback) {
723 if (Opcode == ARM::tLDMIA)
724 // Update tLDMIA with writeback if necessary.
725 Opcode = ARM::tLDMIA_UPD;
726
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000727 MIB = BuildMI(MBB, InsertBefore, DL, TII->get(Opcode));
James Molloy556763d2014-05-16 14:14:30 +0000728
729 // Thumb1: we might need to set base writeback when building the MI.
730 MIB.addReg(Base, getDefRegState(true))
731 .addReg(Base, getKillRegState(BaseKill));
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000732
733 // The base isn't dead after a merged instruction with writeback.
734 // Insert a sub instruction after the newly formed instruction to reset.
735 if (!BaseKill)
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000736 UpdateBaseRegUses(MBB, InsertBefore, DL, Base, NumRegs, Pred, PredReg);
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000737
James Molloy556763d2014-05-16 14:14:30 +0000738 } else {
739 // No writeback, simply build the MachineInstr.
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000740 MIB = BuildMI(MBB, InsertBefore, DL, TII->get(Opcode));
James Molloy556763d2014-05-16 14:14:30 +0000741 MIB.addReg(Base, getKillRegState(BaseKill));
742 }
743
744 MIB.addImm(Pred).addReg(PredReg);
745
Matthias Braunaa9fa352015-05-27 05:12:40 +0000746 for (const std::pair<unsigned, bool> &R : Regs)
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000747 MIB.addReg(R.first, getDefRegState(isDef) | getKillRegState(R.second));
Evan Cheng10043e22007-01-19 07:51:42 +0000748
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000749 return MIB.getInstr();
Tim Northover569f69d2013-10-10 09:28:20 +0000750}
751
Matthias Braunec50fa62015-06-01 21:26:23 +0000752/// Call MergeOps and update MemOps and merges accordingly on success.
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000753MachineInstr *ARMLoadStoreOpt::MergeOpsUpdate(const MergeCandidate &Cand) {
754 const MachineInstr *First = Cand.Instrs.front();
755 unsigned Opcode = First->getOpcode();
756 bool IsLoad = isLoadSingle(Opcode);
Evan Cheng1fb4de82010-06-21 21:21:14 +0000757 SmallVector<std::pair<unsigned, bool>, 8> Regs;
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000758 SmallVector<unsigned, 4> ImpDefs;
759 DenseSet<unsigned> KilledRegs;
Pete Coopere3c81612015-07-16 00:09:18 +0000760 DenseSet<unsigned> UsedRegs;
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000761 // Determine list of registers and list of implicit super-register defs.
762 for (const MachineInstr *MI : Cand.Instrs) {
763 const MachineOperand &MO = getLoadStoreRegOp(*MI);
764 unsigned Reg = MO.getReg();
765 bool IsKill = MO.isKill();
766 if (IsKill)
767 KilledRegs.insert(Reg);
768 Regs.push_back(std::make_pair(Reg, IsKill));
Pete Coopere3c81612015-07-16 00:09:18 +0000769 UsedRegs.insert(Reg);
Jakob Stoklund Olesencdee3262012-03-28 22:50:56 +0000770
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000771 if (IsLoad) {
772 // Collect any implicit defs of super-registers, after merging we can't
773 // be sure anymore that we properly preserved these live ranges and must
774 // removed these implicit operands.
775 for (const MachineOperand &MO : MI->implicit_operands()) {
776 if (!MO.isReg() || !MO.isDef() || MO.isDead())
777 continue;
778 assert(MO.isImplicit());
779 unsigned DefReg = MO.getReg();
780
781 if (std::find(ImpDefs.begin(), ImpDefs.end(), DefReg) != ImpDefs.end())
782 continue;
783 // We can ignore cases where the super-reg is read and written.
784 if (MI->readsRegister(DefReg))
785 continue;
Jakob Stoklund Olesencdee3262012-03-28 22:50:56 +0000786 ImpDefs.push_back(DefReg);
Evan Cheng1fb4de82010-06-21 21:21:14 +0000787 }
788 }
Jakob Stoklund Olesen655e4e62009-12-23 21:28:23 +0000789 }
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000790
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000791 // Attempt the merge.
792 typedef MachineBasicBlock::iterator iterator;
793 MachineInstr *LatestMI = Cand.Instrs[Cand.LatestMIIdx];
794 iterator InsertBefore = std::next(iterator(LatestMI));
795 MachineBasicBlock &MBB = *LatestMI->getParent();
796 unsigned Offset = getMemoryOpOffset(First);
797 unsigned Base = getLoadStoreBaseOp(*First).getReg();
798 bool BaseKill = LatestMI->killsRegister(Base);
799 unsigned PredReg = 0;
800 ARMCC::CondCodes Pred = getInstrPredicate(First, PredReg);
801 DebugLoc DL = First->getDebugLoc();
Matthias Braun731e3592015-07-20 23:17:20 +0000802 MachineInstr *Merged = MergeOps(MBB, InsertBefore, Offset, Base, BaseKill,
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000803 Opcode, Pred, PredReg, DL, Regs);
804 if (!Merged)
805 return nullptr;
806
807 // Determine earliest instruction that will get removed. We then keep an
808 // iterator just above it so the following erases don't invalidated it.
809 iterator EarliestI(Cand.Instrs[Cand.EarliestMIIdx]);
810 bool EarliestAtBegin = false;
811 if (EarliestI == MBB.begin()) {
812 EarliestAtBegin = true;
813 } else {
814 EarliestI = std::prev(EarliestI);
Moritz Rothf5d0c7c2014-09-24 16:35:50 +0000815 }
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000816
817 // Remove instructions which have been merged.
818 for (MachineInstr *MI : Cand.Instrs)
819 MBB.erase(MI);
820
821 // Determine range between the earliest removed instruction and the new one.
822 if (EarliestAtBegin)
823 EarliestI = MBB.begin();
824 else
825 EarliestI = std::next(EarliestI);
826 auto FixupRange = make_range(EarliestI, iterator(Merged));
827
828 if (isLoadSingle(Opcode)) {
829 // If the previous loads defined a super-reg, then we have to mark earlier
830 // operands undef; Replicate the super-reg def on the merged instruction.
831 for (MachineInstr &MI : FixupRange) {
832 for (unsigned &ImpDefReg : ImpDefs) {
833 for (MachineOperand &MO : MI.implicit_operands()) {
834 if (!MO.isReg() || MO.getReg() != ImpDefReg)
835 continue;
836 if (MO.readsReg())
837 MO.setIsUndef();
838 else if (MO.isDef())
839 ImpDefReg = 0;
840 }
841 }
842 }
843
844 MachineInstrBuilder MIB(*Merged->getParent()->getParent(), Merged);
845 for (unsigned ImpDef : ImpDefs)
846 MIB.addReg(ImpDef, RegState::ImplicitDefine);
847 } else {
848 // Remove kill flags: We are possibly storing the values later now.
849 assert(isi32Store(Opcode) || Opcode == ARM::VSTRS || Opcode == ARM::VSTRD);
850 for (MachineInstr &MI : FixupRange) {
851 for (MachineOperand &MO : MI.uses()) {
852 if (!MO.isReg() || !MO.isKill())
853 continue;
Pete Coopere3c81612015-07-16 00:09:18 +0000854 if (UsedRegs.count(MO.getReg()))
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000855 MO.setIsKill(false);
856 }
857 }
858 assert(ImpDefs.empty());
859 }
860
861 return Merged;
Jakob Stoklund Olesen655e4e62009-12-23 21:28:23 +0000862}
863
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000864/// Find candidates for load/store multiple merge in list of MemOpQueueEntries.
865void ARMLoadStoreOpt::FormCandidates(const MemOpQueue &MemOps) {
866 const MachineInstr *FirstMI = MemOps[0].MI;
867 unsigned Opcode = FirstMI->getOpcode();
Bob Wilson13ce07f2010-08-27 23:18:17 +0000868 bool isNotVFP = isi32Load(Opcode) || isi32Store(Opcode);
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000869 unsigned Size = getLSMultipleTransferSize(FirstMI);
Evan Cheng0f7cbe82007-05-15 01:29:07 +0000870
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000871 unsigned SIndex = 0;
872 unsigned EIndex = MemOps.size();
873 do {
874 // Look at the first instruction.
875 const MachineInstr *MI = MemOps[SIndex].MI;
876 int Offset = MemOps[SIndex].Offset;
877 const MachineOperand &PMO = getLoadStoreRegOp(*MI);
878 unsigned PReg = PMO.getReg();
879 unsigned PRegNum = PMO.isUndef() ? UINT_MAX : TRI->getEncodingValue(PReg);
880 unsigned Latest = SIndex;
881 unsigned Earliest = SIndex;
882 unsigned Count = 1;
883
Matthias Braun731e3592015-07-20 23:17:20 +0000884 // Merge additional instructions fulfilling LDM/STM constraints.
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000885 for (unsigned I = SIndex+1; I < EIndex; ++I, ++Count) {
886 int NewOffset = MemOps[I].Offset;
887 if (NewOffset != Offset + (int)Size)
888 break;
889 const MachineOperand &MO = getLoadStoreRegOp(*MemOps[I].MI);
890 unsigned Reg = MO.getReg();
Matthias Braun731e3592015-07-20 23:17:20 +0000891 if (Reg == ARM::SP)
Matthias Braune4ba6b82015-07-10 18:28:49 +0000892 break;
Matthias Braun731e3592015-07-20 23:17:20 +0000893 unsigned RegNum = MO.isUndef() ? UINT_MAX : TRI->getEncodingValue(Reg);
894 // Register numbers must be in ascending order.
895 if (RegNum <= PRegNum)
896 break;
897 // For VFP / NEON load/store multiples, the registers must be consecutive
898 // and within the limit on the number of registers per instruction.
899 if (!isNotVFP && RegNum != PRegNum+1)
900 break;
901 // On Swift we don't want vldm/vstm to start with a odd register num
902 // because Q register unaligned vldm/vstm need more uops.
903 if (!isNotVFP && STI->isSwift() && Count == 1 && (PRegNum % 2) == 1)
904 break;
905
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000906 // Track MemOp with latest and earliest position (Positions are
907 // counted in reverse).
908 unsigned Position = MemOps[I].Position;
909 if (Position < MemOps[Latest].Position)
910 Latest = I;
911 else if (Position > MemOps[Earliest].Position)
912 Earliest = I;
913 // Prepare for next MemOp.
Evan Cheng10043e22007-01-19 07:51:42 +0000914 Offset += Size;
Evan Cheng10043e22007-01-19 07:51:42 +0000915 PRegNum = RegNum;
Evan Cheng10043e22007-01-19 07:51:42 +0000916 }
917
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000918 // Form a candidate from the Ops collected so far.
Matthias Braun22f39602015-07-20 23:17:14 +0000919 MergeCandidate *Candidate = new(Allocator) MergeCandidate;
Matthias Brauna4a3182d2015-07-10 18:08:49 +0000920 for (unsigned C = SIndex, CE = SIndex + Count; C < CE; ++C)
921 Candidate->Instrs.push_back(MemOps[C].MI);
922 Candidate->LatestMIIdx = Latest - SIndex;
923 Candidate->EarliestMIIdx = Earliest - SIndex;
924 Candidate->InsertPos = MemOps[Latest].Position;
925 Candidates.push_back(Candidate);
926 // Continue after the chain.
927 SIndex += Count;
928 } while (SIndex < EIndex);
Evan Cheng10043e22007-01-19 07:51:42 +0000929}
930
Matthias Braun84e28972015-07-20 23:17:16 +0000931static bool isMatchingDecrement(MachineInstr *MI, unsigned Base,
932 unsigned Bytes, unsigned Limit,
933 ARMCC::CondCodes Pred, unsigned PredReg) {
934 unsigned MyPredReg = 0;
935 if (!MI)
936 return false;
937
938 bool CheckCPSRDef = false;
939 switch (MI->getOpcode()) {
940 default: return false;
941 case ARM::tSUBi8:
942 case ARM::t2SUBri:
943 case ARM::SUBri:
944 CheckCPSRDef = true;
945 break;
946 case ARM::tSUBspi:
947 break;
948 }
949
950 // Make sure the offset fits in 8 bits.
951 if (Bytes == 0 || (Limit && Bytes >= Limit))
952 return false;
953
954 unsigned Scale = (MI->getOpcode() == ARM::tSUBspi ||
955 MI->getOpcode() == ARM::tSUBi8) ? 4 : 1; // FIXME
956 if (!(MI->getOperand(0).getReg() == Base &&
957 MI->getOperand(1).getReg() == Base &&
958 (MI->getOperand(2).getImm() * Scale) == Bytes &&
959 getInstrPredicate(MI, MyPredReg) == Pred &&
960 MyPredReg == PredReg))
961 return false;
962
963 return CheckCPSRDef ? !definesCPSR(MI) : true;
964}
965
966static bool isMatchingIncrement(MachineInstr *MI, unsigned Base,
967 unsigned Bytes, unsigned Limit,
968 ARMCC::CondCodes Pred, unsigned PredReg) {
969 unsigned MyPredReg = 0;
970 if (!MI)
971 return false;
972
973 bool CheckCPSRDef = false;
974 switch (MI->getOpcode()) {
975 default: return false;
976 case ARM::tADDi8:
977 case ARM::t2ADDri:
978 case ARM::ADDri:
979 CheckCPSRDef = true;
980 break;
981 case ARM::tADDspi:
982 break;
983 }
984
985 if (Bytes == 0 || (Limit && Bytes >= Limit))
986 // Make sure the offset fits in 8 bits.
987 return false;
988
989 unsigned Scale = (MI->getOpcode() == ARM::tADDspi ||
990 MI->getOpcode() == ARM::tADDi8) ? 4 : 1; // FIXME
991 if (!(MI->getOperand(0).getReg() == Base &&
992 MI->getOperand(1).getReg() == Base &&
993 (MI->getOperand(2).getImm() * Scale) == Bytes &&
994 getInstrPredicate(MI, MyPredReg) == Pred &&
995 MyPredReg == PredReg))
996 return false;
997
998 return CheckCPSRDef ? !definesCPSR(MI) : true;
999}
1000
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001001static unsigned getUpdatingLSMultipleOpcode(unsigned Opc,
1002 ARM_AM::AMSubMode Mode) {
Bob Wilson947f04b2010-03-13 01:08:20 +00001003 switch (Opc) {
Bob Wilson947f04b2010-03-13 01:08:20 +00001004 default: llvm_unreachable("Unhandled opcode!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001005 case ARM::LDMIA:
1006 case ARM::LDMDA:
1007 case ARM::LDMDB:
1008 case ARM::LDMIB:
1009 switch (Mode) {
1010 default: llvm_unreachable("Unhandled submode!");
1011 case ARM_AM::ia: return ARM::LDMIA_UPD;
1012 case ARM_AM::ib: return ARM::LDMIB_UPD;
1013 case ARM_AM::da: return ARM::LDMDA_UPD;
1014 case ARM_AM::db: return ARM::LDMDB_UPD;
1015 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001016 case ARM::STMIA:
1017 case ARM::STMDA:
1018 case ARM::STMDB:
1019 case ARM::STMIB:
1020 switch (Mode) {
1021 default: llvm_unreachable("Unhandled submode!");
1022 case ARM_AM::ia: return ARM::STMIA_UPD;
1023 case ARM_AM::ib: return ARM::STMIB_UPD;
1024 case ARM_AM::da: return ARM::STMDA_UPD;
1025 case ARM_AM::db: return ARM::STMDB_UPD;
1026 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001027 case ARM::t2LDMIA:
1028 case ARM::t2LDMDB:
1029 switch (Mode) {
1030 default: llvm_unreachable("Unhandled submode!");
1031 case ARM_AM::ia: return ARM::t2LDMIA_UPD;
1032 case ARM_AM::db: return ARM::t2LDMDB_UPD;
1033 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001034 case ARM::t2STMIA:
1035 case ARM::t2STMDB:
1036 switch (Mode) {
1037 default: llvm_unreachable("Unhandled submode!");
1038 case ARM_AM::ia: return ARM::t2STMIA_UPD;
1039 case ARM_AM::db: return ARM::t2STMDB_UPD;
1040 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001041 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001042 switch (Mode) {
1043 default: llvm_unreachable("Unhandled submode!");
1044 case ARM_AM::ia: return ARM::VLDMSIA_UPD;
1045 case ARM_AM::db: return ARM::VLDMSDB_UPD;
1046 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001047 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001048 switch (Mode) {
1049 default: llvm_unreachable("Unhandled submode!");
1050 case ARM_AM::ia: return ARM::VLDMDIA_UPD;
1051 case ARM_AM::db: return ARM::VLDMDDB_UPD;
1052 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001053 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001054 switch (Mode) {
1055 default: llvm_unreachable("Unhandled submode!");
1056 case ARM_AM::ia: return ARM::VSTMSIA_UPD;
1057 case ARM_AM::db: return ARM::VSTMSDB_UPD;
1058 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001059 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001060 switch (Mode) {
1061 default: llvm_unreachable("Unhandled submode!");
1062 case ARM_AM::ia: return ARM::VSTMDIA_UPD;
1063 case ARM_AM::db: return ARM::VSTMDDB_UPD;
1064 }
Bob Wilson947f04b2010-03-13 01:08:20 +00001065 }
Bob Wilson947f04b2010-03-13 01:08:20 +00001066}
1067
Matthias Braunec50fa62015-06-01 21:26:23 +00001068/// Fold proceeding/trailing inc/dec of base register into the
1069/// LDM/STM/VLDM{D|S}/VSTM{D|S} op when possible:
Evan Cheng10043e22007-01-19 07:51:42 +00001070///
1071/// stmia rn, <ra, rb, rc>
1072/// rn := rn + 4 * 3;
1073/// =>
1074/// stmia rn!, <ra, rb, rc>
1075///
1076/// rn := rn - 4 * 3;
1077/// ldmia rn, <ra, rb, rc>
1078/// =>
1079/// ldmdb rn!, <ra, rb, rc>
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001080bool ARMLoadStoreOpt::MergeBaseUpdateLSMultiple(MachineInstr *MI) {
James Molloy556763d2014-05-16 14:14:30 +00001081 // Thumb1 is already using updating loads/stores.
1082 if (isThumb1) return false;
1083
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001084 const MachineOperand &BaseOP = MI->getOperand(0);
1085 unsigned Base = BaseOP.getReg();
1086 bool BaseKill = BaseOP.isKill();
Matthias Braun84e28972015-07-20 23:17:16 +00001087 unsigned Bytes = getLSMultipleTransferSize(MI);
Evan Cheng94f04c62007-07-05 07:18:20 +00001088 unsigned PredReg = 0;
Craig Topperf6e7e122012-03-27 07:21:54 +00001089 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
Matthias Braunfa3872e2015-05-18 20:27:55 +00001090 unsigned Opcode = MI->getOpcode();
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001091 DebugLoc DL = MI->getDebugLoc();
Evan Cheng10043e22007-01-19 07:51:42 +00001092
Bob Wilson13ce07f2010-08-27 23:18:17 +00001093 // Can't use an updating ld/st if the base register is also a dest
1094 // register. e.g. ldmdb r0!, {r0, r1, r2}. The behavior is undefined.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001095 for (unsigned i = 2, e = MI->getNumOperands(); i != e; ++i)
Bob Wilson13ce07f2010-08-27 23:18:17 +00001096 if (MI->getOperand(i).getReg() == Base)
1097 return false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001098
Matthias Braun84e28972015-07-20 23:17:16 +00001099 bool DoMerge = false;
Matthias Braund9bd22b2015-07-10 18:37:33 +00001100 ARM_AM::AMSubMode Mode = getLoadStoreMultipleSubMode(Opcode);
Matthias Braun84e28972015-07-20 23:17:16 +00001101
1102 // Try merging with the previous instruction.
1103 MachineBasicBlock &MBB = *MI->getParent();
1104 MachineBasicBlock::iterator BeginMBBI = MBB.begin();
1105 MachineBasicBlock::iterator MBBI(MI);
1106 if (MBBI != BeginMBBI) {
1107 MachineBasicBlock::iterator PrevMBBI = std::prev(MBBI);
1108 while (PrevMBBI != BeginMBBI && PrevMBBI->isDebugValue())
1109 --PrevMBBI;
1110 if (Mode == ARM_AM::ia &&
1111 isMatchingDecrement(PrevMBBI, Base, Bytes, 0, Pred, PredReg)) {
1112 Mode = ARM_AM::db;
1113 DoMerge = true;
1114 } else if (Mode == ARM_AM::ib &&
1115 isMatchingDecrement(PrevMBBI, Base, Bytes, 0, Pred, PredReg)) {
1116 Mode = ARM_AM::da;
1117 DoMerge = true;
1118 }
1119 if (DoMerge)
1120 MBB.erase(PrevMBBI);
Bob Wilson947f04b2010-03-13 01:08:20 +00001121 }
Matthias Braun84e28972015-07-20 23:17:16 +00001122
1123 // Try merging with the next instruction.
1124 MachineBasicBlock::iterator EndMBBI = MBB.end();
1125 if (!DoMerge && MBBI != EndMBBI) {
1126 MachineBasicBlock::iterator NextMBBI = std::next(MBBI);
1127 while (NextMBBI != EndMBBI && NextMBBI->isDebugValue())
1128 ++NextMBBI;
1129 if ((Mode == ARM_AM::ia || Mode == ARM_AM::ib) &&
1130 isMatchingIncrement(NextMBBI, Base, Bytes, 0, Pred, PredReg)) {
1131 DoMerge = true;
1132 } else if ((Mode == ARM_AM::da || Mode == ARM_AM::db) &&
1133 isMatchingDecrement(NextMBBI, Base, Bytes, 0, Pred, PredReg)) {
1134 DoMerge = true;
1135 }
1136 if (DoMerge)
1137 MBB.erase(NextMBBI);
1138 }
1139
1140 if (!DoMerge)
1141 return false;
Bob Wilson947f04b2010-03-13 01:08:20 +00001142
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001143 unsigned NewOpc = getUpdatingLSMultipleOpcode(Opcode, Mode);
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001144 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(NewOpc))
Bob Wilson947f04b2010-03-13 01:08:20 +00001145 .addReg(Base, getDefRegState(true)) // WB base register
Bob Wilson13ce07f2010-08-27 23:18:17 +00001146 .addReg(Base, getKillRegState(BaseKill))
Bob Wilson13ce07f2010-08-27 23:18:17 +00001147 .addImm(Pred).addReg(PredReg);
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001148
Bob Wilson947f04b2010-03-13 01:08:20 +00001149 // Transfer the rest of operands.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001150 for (unsigned OpNum = 3, e = MI->getNumOperands(); OpNum != e; ++OpNum)
Bob Wilson947f04b2010-03-13 01:08:20 +00001151 MIB.addOperand(MI->getOperand(OpNum));
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001152
Bob Wilson947f04b2010-03-13 01:08:20 +00001153 // Transfer memoperands.
Chris Lattner1d0c2572011-04-29 05:24:29 +00001154 MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
Bob Wilson947f04b2010-03-13 01:08:20 +00001155
1156 MBB.erase(MBBI);
1157 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00001158}
1159
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001160static unsigned getPreIndexedLoadStoreOpcode(unsigned Opc,
1161 ARM_AM::AddrOpc Mode) {
Evan Cheng10043e22007-01-19 07:51:42 +00001162 switch (Opc) {
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001163 case ARM::LDRi12:
Owen Anderson16d33f32011-08-26 20:43:14 +00001164 return ARM::LDR_PRE_IMM;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001165 case ARM::STRi12:
Owen Anderson2aedba62011-07-26 20:54:26 +00001166 return ARM::STR_PRE_IMM;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001167 case ARM::VLDRS:
1168 return Mode == ARM_AM::add ? ARM::VLDMSIA_UPD : ARM::VLDMSDB_UPD;
1169 case ARM::VLDRD:
1170 return Mode == ARM_AM::add ? ARM::VLDMDIA_UPD : ARM::VLDMDDB_UPD;
1171 case ARM::VSTRS:
1172 return Mode == ARM_AM::add ? ARM::VSTMSIA_UPD : ARM::VSTMSDB_UPD;
1173 case ARM::VSTRD:
1174 return Mode == ARM_AM::add ? ARM::VSTMDIA_UPD : ARM::VSTMDDB_UPD;
Evan Cheng4605e8a2009-07-09 23:11:34 +00001175 case ARM::t2LDRi8:
1176 case ARM::t2LDRi12:
1177 return ARM::t2LDR_PRE;
1178 case ARM::t2STRi8:
1179 case ARM::t2STRi12:
1180 return ARM::t2STR_PRE;
Torok Edwinfbcc6632009-07-14 16:55:14 +00001181 default: llvm_unreachable("Unhandled opcode!");
Evan Cheng10043e22007-01-19 07:51:42 +00001182 }
Evan Cheng10043e22007-01-19 07:51:42 +00001183}
1184
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001185static unsigned getPostIndexedLoadStoreOpcode(unsigned Opc,
1186 ARM_AM::AddrOpc Mode) {
Evan Cheng10043e22007-01-19 07:51:42 +00001187 switch (Opc) {
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001188 case ARM::LDRi12:
Owen Anderson2aedba62011-07-26 20:54:26 +00001189 return ARM::LDR_POST_IMM;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001190 case ARM::STRi12:
Owen Anderson2aedba62011-07-26 20:54:26 +00001191 return ARM::STR_POST_IMM;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001192 case ARM::VLDRS:
1193 return Mode == ARM_AM::add ? ARM::VLDMSIA_UPD : ARM::VLDMSDB_UPD;
1194 case ARM::VLDRD:
1195 return Mode == ARM_AM::add ? ARM::VLDMDIA_UPD : ARM::VLDMDDB_UPD;
1196 case ARM::VSTRS:
1197 return Mode == ARM_AM::add ? ARM::VSTMSIA_UPD : ARM::VSTMSDB_UPD;
1198 case ARM::VSTRD:
1199 return Mode == ARM_AM::add ? ARM::VSTMDIA_UPD : ARM::VSTMDDB_UPD;
Evan Cheng4605e8a2009-07-09 23:11:34 +00001200 case ARM::t2LDRi8:
1201 case ARM::t2LDRi12:
1202 return ARM::t2LDR_POST;
1203 case ARM::t2STRi8:
1204 case ARM::t2STRi12:
1205 return ARM::t2STR_POST;
Torok Edwinfbcc6632009-07-14 16:55:14 +00001206 default: llvm_unreachable("Unhandled opcode!");
Evan Cheng10043e22007-01-19 07:51:42 +00001207 }
Evan Cheng10043e22007-01-19 07:51:42 +00001208}
1209
Matthias Braunec50fa62015-06-01 21:26:23 +00001210/// Fold proceeding/trailing inc/dec of base register into the
1211/// LDR/STR/FLD{D|S}/FST{D|S} op when possible:
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001212bool ARMLoadStoreOpt::MergeBaseUpdateLoadStore(MachineInstr *MI) {
James Molloy556763d2014-05-16 14:14:30 +00001213 // Thumb1 doesn't have updating LDR/STR.
1214 // FIXME: Use LDM/STM with single register instead.
1215 if (isThumb1) return false;
1216
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001217 unsigned Base = getLoadStoreBaseOp(*MI).getReg();
1218 bool BaseKill = getLoadStoreBaseOp(*MI).isKill();
Matthias Braun84e28972015-07-20 23:17:16 +00001219 unsigned Bytes = getLSMultipleTransferSize(MI);
Matthias Braunfa3872e2015-05-18 20:27:55 +00001220 unsigned Opcode = MI->getOpcode();
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001221 DebugLoc DL = MI->getDebugLoc();
Bob Wilsonaf10d272010-03-12 22:50:09 +00001222 bool isAM5 = (Opcode == ARM::VLDRD || Opcode == ARM::VLDRS ||
1223 Opcode == ARM::VSTRD || Opcode == ARM::VSTRS);
Jim Grosbach338de3e2010-10-27 23:12:14 +00001224 bool isAM2 = (Opcode == ARM::LDRi12 || Opcode == ARM::STRi12);
1225 if (isi32Load(Opcode) || isi32Store(Opcode))
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001226 if (MI->getOperand(2).getImm() != 0)
1227 return false;
Bob Wilsonaf10d272010-03-12 22:50:09 +00001228 if (isAM5 && ARM_AM::getAM5Offset(MI->getOperand(2).getImm()) != 0)
Evan Cheng4605e8a2009-07-09 23:11:34 +00001229 return false;
Evan Cheng10043e22007-01-19 07:51:42 +00001230
Matthias Braun84e28972015-07-20 23:17:16 +00001231 bool isLd = isLoadSingle(Opcode);
Evan Cheng10043e22007-01-19 07:51:42 +00001232 // Can't do the merge if the destination register is the same as the would-be
1233 // writeback register.
Chad Rosierace9c5d2013-03-25 16:29:20 +00001234 if (MI->getOperand(0).getReg() == Base)
Evan Cheng10043e22007-01-19 07:51:42 +00001235 return false;
1236
Evan Cheng94f04c62007-07-05 07:18:20 +00001237 unsigned PredReg = 0;
Craig Topperf6e7e122012-03-27 07:21:54 +00001238 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
Matthias Braun84e28972015-07-20 23:17:16 +00001239 bool DoMerge = false;
1240 ARM_AM::AddrOpc AddSub = ARM_AM::add;
1241 unsigned NewOpc = 0;
1242 // AM2 - 12 bits, thumb2 - 8 bits.
1243 unsigned Limit = isAM5 ? 0 : (isAM2 ? 0x1000 : 0x100);
1244
1245 // Try merging with the previous instruction.
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001246 MachineBasicBlock &MBB = *MI->getParent();
Matthias Braun84e28972015-07-20 23:17:16 +00001247 MachineBasicBlock::iterator BeginMBBI = MBB.begin();
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001248 MachineBasicBlock::iterator MBBI(MI);
Matthias Braun84e28972015-07-20 23:17:16 +00001249 if (MBBI != BeginMBBI) {
1250 MachineBasicBlock::iterator PrevMBBI = std::prev(MBBI);
1251 while (PrevMBBI != BeginMBBI && PrevMBBI->isDebugValue())
1252 --PrevMBBI;
1253 if (isMatchingDecrement(PrevMBBI, Base, Bytes, Limit, Pred, PredReg)) {
1254 DoMerge = true;
1255 AddSub = ARM_AM::sub;
1256 } else if (!isAM5 &&
1257 isMatchingIncrement(PrevMBBI, Base, Bytes, Limit,Pred,PredReg)) {
1258 DoMerge = true;
1259 }
1260 if (DoMerge) {
1261 NewOpc = getPreIndexedLoadStoreOpcode(Opcode, AddSub);
1262 MBB.erase(PrevMBBI);
1263 }
Evan Cheng10043e22007-01-19 07:51:42 +00001264 }
1265
Matthias Braun84e28972015-07-20 23:17:16 +00001266 // Try merging with the next instruction.
1267 MachineBasicBlock::iterator EndMBBI = MBB.end();
1268 if (!DoMerge && MBBI != EndMBBI) {
1269 MachineBasicBlock::iterator NextMBBI = std::next(MBBI);
1270 while (NextMBBI != EndMBBI && NextMBBI->isDebugValue())
1271 ++NextMBBI;
1272 if (!isAM5 &&
1273 isMatchingDecrement(NextMBBI, Base, Bytes, Limit, Pred, PredReg)) {
1274 DoMerge = true;
1275 AddSub = ARM_AM::sub;
1276 } else if (isMatchingIncrement(NextMBBI, Base, Bytes, Limit,Pred,PredReg)) {
1277 DoMerge = true;
1278 }
1279 if (DoMerge) {
1280 NewOpc = getPostIndexedLoadStoreOpcode(Opcode, AddSub);
1281 MBB.erase(NextMBBI);
1282 }
1283 }
Evan Cheng10043e22007-01-19 07:51:42 +00001284
Matthias Braun84e28972015-07-20 23:17:16 +00001285 if (!DoMerge)
1286 return false;
1287
Bob Wilson53149402010-03-13 00:43:32 +00001288 if (isAM5) {
James Molloybb73c232014-05-16 14:08:46 +00001289 // VLDM[SD]_UPD, VSTM[SD]_UPD
Bob Wilson13ce07f2010-08-27 23:18:17 +00001290 // (There are no base-updating versions of VLDR/VSTR instructions, but the
1291 // updating load/store-multiple instructions can be used with only one
1292 // register.)
Bob Wilson53149402010-03-13 00:43:32 +00001293 MachineOperand &MO = MI->getOperand(0);
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001294 BuildMI(MBB, MBBI, DL, TII->get(NewOpc))
Bob Wilson947f04b2010-03-13 01:08:20 +00001295 .addReg(Base, getDefRegState(true)) // WB base register
Bob Wilson53149402010-03-13 00:43:32 +00001296 .addReg(Base, getKillRegState(isLd ? BaseKill : false))
Bob Wilson53149402010-03-13 00:43:32 +00001297 .addImm(Pred).addReg(PredReg)
Bob Wilson53149402010-03-13 00:43:32 +00001298 .addReg(MO.getReg(), (isLd ? getDefRegState(true) :
1299 getKillRegState(MO.isKill())));
1300 } else if (isLd) {
Jim Grosbach23254742011-08-12 22:20:41 +00001301 if (isAM2) {
Owen Anderson63143432011-08-29 17:59:41 +00001302 // LDR_PRE, LDR_POST
1303 if (NewOpc == ARM::LDR_PRE_IMM || NewOpc == ARM::LDRB_PRE_IMM) {
Matthias Braun84e28972015-07-20 23:17:16 +00001304 int Offset = AddSub == ARM_AM::sub ? -Bytes : Bytes;
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001305 BuildMI(MBB, MBBI, DL, TII->get(NewOpc), MI->getOperand(0).getReg())
Owen Anderson63143432011-08-29 17:59:41 +00001306 .addReg(Base, RegState::Define)
1307 .addReg(Base).addImm(Offset).addImm(Pred).addReg(PredReg);
1308 } else {
Matthias Braun84e28972015-07-20 23:17:16 +00001309 int Offset = ARM_AM::getAM2Opc(AddSub, Bytes, ARM_AM::no_shift);
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001310 BuildMI(MBB, MBBI, DL, TII->get(NewOpc), MI->getOperand(0).getReg())
Owen Anderson63143432011-08-29 17:59:41 +00001311 .addReg(Base, RegState::Define)
Matthias Braun84e28972015-07-20 23:17:16 +00001312 .addReg(Base).addReg(0).addImm(Offset).addImm(Pred).addReg(PredReg);
Owen Anderson63143432011-08-29 17:59:41 +00001313 }
Jim Grosbach23254742011-08-12 22:20:41 +00001314 } else {
Matthias Braun84e28972015-07-20 23:17:16 +00001315 int Offset = AddSub == ARM_AM::sub ? -Bytes : Bytes;
Evan Cheng71756e72009-08-04 01:43:45 +00001316 // t2LDR_PRE, t2LDR_POST
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001317 BuildMI(MBB, MBBI, DL, TII->get(NewOpc), MI->getOperand(0).getReg())
Evan Cheng71756e72009-08-04 01:43:45 +00001318 .addReg(Base, RegState::Define)
1319 .addReg(Base).addImm(Offset).addImm(Pred).addReg(PredReg);
Jim Grosbach23254742011-08-12 22:20:41 +00001320 }
Evan Cheng71756e72009-08-04 01:43:45 +00001321 } else {
1322 MachineOperand &MO = MI->getOperand(0);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00001323 // FIXME: post-indexed stores use am2offset_imm, which still encodes
1324 // the vestigal zero-reg offset register. When that's fixed, this clause
1325 // can be removed entirely.
Jim Grosbach23254742011-08-12 22:20:41 +00001326 if (isAM2 && NewOpc == ARM::STR_POST_IMM) {
Matthias Braun84e28972015-07-20 23:17:16 +00001327 int Offset = ARM_AM::getAM2Opc(AddSub, Bytes, ARM_AM::no_shift);
Evan Cheng71756e72009-08-04 01:43:45 +00001328 // STR_PRE, STR_POST
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001329 BuildMI(MBB, MBBI, DL, TII->get(NewOpc), Base)
Evan Cheng71756e72009-08-04 01:43:45 +00001330 .addReg(MO.getReg(), getKillRegState(MO.isKill()))
Matthias Braun84e28972015-07-20 23:17:16 +00001331 .addReg(Base).addReg(0).addImm(Offset).addImm(Pred).addReg(PredReg);
Jim Grosbach23254742011-08-12 22:20:41 +00001332 } else {
Matthias Braun84e28972015-07-20 23:17:16 +00001333 int Offset = AddSub == ARM_AM::sub ? -Bytes : Bytes;
Evan Cheng71756e72009-08-04 01:43:45 +00001334 // t2STR_PRE, t2STR_POST
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001335 BuildMI(MBB, MBBI, DL, TII->get(NewOpc), Base)
Evan Cheng71756e72009-08-04 01:43:45 +00001336 .addReg(MO.getReg(), getKillRegState(MO.isKill()))
1337 .addReg(Base).addImm(Offset).addImm(Pred).addReg(PredReg);
Jim Grosbach23254742011-08-12 22:20:41 +00001338 }
Evan Cheng10043e22007-01-19 07:51:42 +00001339 }
1340 MBB.erase(MBBI);
1341
1342 return true;
1343}
1344
Matthias Braunec50fa62015-06-01 21:26:23 +00001345/// Returns true if instruction is a memory operation that this pass is capable
1346/// of operating on.
Evan Cheng4605e8a2009-07-09 23:11:34 +00001347static bool isMemoryOp(const MachineInstr *MI) {
Jakob Stoklund Olesenc1eccbc2010-06-29 01:13:07 +00001348 // When no memory operands are present, conservatively assume unaligned,
1349 // volatile, unfoldable.
1350 if (!MI->hasOneMemOperand())
1351 return false;
Jakob Stoklund Olesenbff09062010-01-14 00:54:10 +00001352
Jakob Stoklund Olesenc1eccbc2010-06-29 01:13:07 +00001353 const MachineMemOperand *MMO = *MI->memoperands_begin();
Jakob Stoklund Olesenbff09062010-01-14 00:54:10 +00001354
Jakob Stoklund Olesenc1eccbc2010-06-29 01:13:07 +00001355 // Don't touch volatile memory accesses - we may be changing their order.
1356 if (MMO->isVolatile())
1357 return false;
1358
1359 // Unaligned ldr/str is emulated by some kernels, but unaligned ldm/stm is
1360 // not.
1361 if (MMO->getAlignment() < 4)
1362 return false;
Jakob Stoklund Olesenbff09062010-01-14 00:54:10 +00001363
Jakob Stoklund Olesen0b94eb12010-02-24 18:57:08 +00001364 // str <undef> could probably be eliminated entirely, but for now we just want
1365 // to avoid making a mess of it.
1366 // FIXME: Use str <undef> as a wildcard to enable better stm folding.
1367 if (MI->getNumOperands() > 0 && MI->getOperand(0).isReg() &&
1368 MI->getOperand(0).isUndef())
1369 return false;
1370
Bob Wilsoncf6e29a2010-03-04 21:04:38 +00001371 // Likewise don't mess with references to undefined addresses.
1372 if (MI->getNumOperands() > 1 && MI->getOperand(1).isReg() &&
1373 MI->getOperand(1).isUndef())
1374 return false;
1375
Matthias Braunfa3872e2015-05-18 20:27:55 +00001376 unsigned Opcode = MI->getOpcode();
Evan Chengd28de672007-03-06 18:02:41 +00001377 switch (Opcode) {
1378 default: break;
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001379 case ARM::VLDRS:
1380 case ARM::VSTRS:
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001381 return MI->getOperand(1).isReg();
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001382 case ARM::VLDRD:
1383 case ARM::VSTRD:
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001384 return MI->getOperand(1).isReg();
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001385 case ARM::LDRi12:
Jim Grosbach338de3e2010-10-27 23:12:14 +00001386 case ARM::STRi12:
James Molloy556763d2014-05-16 14:14:30 +00001387 case ARM::tLDRi:
1388 case ARM::tSTRi:
Renato Golinb9887ef2015-02-25 14:41:06 +00001389 case ARM::tLDRspi:
1390 case ARM::tSTRspi:
Evan Cheng4605e8a2009-07-09 23:11:34 +00001391 case ARM::t2LDRi8:
1392 case ARM::t2LDRi12:
1393 case ARM::t2STRi8:
1394 case ARM::t2STRi12:
Evan Chenga6b9cab2009-09-27 09:46:04 +00001395 return MI->getOperand(1).isReg();
Evan Chengd28de672007-03-06 18:02:41 +00001396 }
1397 return false;
1398}
1399
Evan Cheng1283c6a2009-06-15 08:28:29 +00001400static void InsertLDR_STR(MachineBasicBlock &MBB,
1401 MachineBasicBlock::iterator &MBBI,
Jim Grosbach338de3e2010-10-27 23:12:14 +00001402 int Offset, bool isDef,
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001403 DebugLoc DL, unsigned NewOpc,
Evan Chenga6b9cab2009-09-27 09:46:04 +00001404 unsigned Reg, bool RegDeadKill, bool RegUndef,
1405 unsigned BaseReg, bool BaseKill, bool BaseUndef,
Jim Grosbach338de3e2010-10-27 23:12:14 +00001406 bool OffKill, bool OffUndef,
Evan Cheng1283c6a2009-06-15 08:28:29 +00001407 ARMCC::CondCodes Pred, unsigned PredReg,
Evan Chenga6b9cab2009-09-27 09:46:04 +00001408 const TargetInstrInfo *TII, bool isT2) {
Evan Chenga6b9cab2009-09-27 09:46:04 +00001409 if (isDef) {
1410 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MBBI->getDebugLoc(),
1411 TII->get(NewOpc))
Evan Cheng5d8df7f2009-06-19 01:59:04 +00001412 .addReg(Reg, getDefRegState(true) | getDeadRegState(RegDeadKill))
Evan Chenga6b9cab2009-09-27 09:46:04 +00001413 .addReg(BaseReg, getKillRegState(BaseKill)|getUndefRegState(BaseUndef));
Evan Chenga6b9cab2009-09-27 09:46:04 +00001414 MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
1415 } else {
1416 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MBBI->getDebugLoc(),
1417 TII->get(NewOpc))
1418 .addReg(Reg, getKillRegState(RegDeadKill) | getUndefRegState(RegUndef))
1419 .addReg(BaseReg, getKillRegState(BaseKill)|getUndefRegState(BaseUndef));
Evan Chenga6b9cab2009-09-27 09:46:04 +00001420 MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
1421 }
Evan Cheng1283c6a2009-06-15 08:28:29 +00001422}
1423
1424bool ARMLoadStoreOpt::FixInvalidRegPairOp(MachineBasicBlock &MBB,
1425 MachineBasicBlock::iterator &MBBI) {
1426 MachineInstr *MI = &*MBBI;
1427 unsigned Opcode = MI->getOpcode();
Matthias Braunba3ecc32015-06-24 20:03:27 +00001428 if (Opcode != ARM::LDRD && Opcode != ARM::STRD && Opcode != ARM::t2LDRDi8)
1429 return false;
Evan Cheng1283c6a2009-06-15 08:28:29 +00001430
Matthias Braunba3ecc32015-06-24 20:03:27 +00001431 const MachineOperand &BaseOp = MI->getOperand(2);
1432 unsigned BaseReg = BaseOp.getReg();
1433 unsigned EvenReg = MI->getOperand(0).getReg();
1434 unsigned OddReg = MI->getOperand(1).getReg();
1435 unsigned EvenRegNum = TRI->getDwarfRegNum(EvenReg, false);
1436 unsigned OddRegNum = TRI->getDwarfRegNum(OddReg, false);
Evan Cheng1283c6a2009-06-15 08:28:29 +00001437
Matthias Braunba3ecc32015-06-24 20:03:27 +00001438 // ARM errata 602117: LDRD with base in list may result in incorrect base
1439 // register when interrupted or faulted.
1440 bool Errata602117 = EvenReg == BaseReg &&
1441 (Opcode == ARM::LDRD || Opcode == ARM::t2LDRDi8) && STI->isCortexM3();
1442 // ARM LDRD/STRD needs consecutive registers.
1443 bool NonConsecutiveRegs = (Opcode == ARM::LDRD || Opcode == ARM::STRD) &&
1444 (EvenRegNum % 2 != 0 || EvenRegNum + 1 != OddRegNum);
1445
1446 if (!Errata602117 && !NonConsecutiveRegs)
1447 return false;
1448
Matthias Braunba3ecc32015-06-24 20:03:27 +00001449 bool isT2 = Opcode == ARM::t2LDRDi8 || Opcode == ARM::t2STRDi8;
1450 bool isLd = Opcode == ARM::LDRD || Opcode == ARM::t2LDRDi8;
1451 bool EvenDeadKill = isLd ?
1452 MI->getOperand(0).isDead() : MI->getOperand(0).isKill();
1453 bool EvenUndef = MI->getOperand(0).isUndef();
1454 bool OddDeadKill = isLd ?
1455 MI->getOperand(1).isDead() : MI->getOperand(1).isKill();
1456 bool OddUndef = MI->getOperand(1).isUndef();
1457 bool BaseKill = BaseOp.isKill();
1458 bool BaseUndef = BaseOp.isUndef();
1459 bool OffKill = isT2 ? false : MI->getOperand(3).isKill();
1460 bool OffUndef = isT2 ? false : MI->getOperand(3).isUndef();
1461 int OffImm = getMemoryOpOffset(MI);
1462 unsigned PredReg = 0;
1463 ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
1464
1465 if (OddRegNum > EvenRegNum && OffImm == 0) {
1466 // Ascending register numbers and no offset. It's safe to change it to a
1467 // ldm or stm.
1468 unsigned NewOpc = (isLd)
1469 ? (isT2 ? ARM::t2LDMIA : ARM::LDMIA)
1470 : (isT2 ? ARM::t2STMIA : ARM::STMIA);
1471 if (isLd) {
1472 BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(NewOpc))
1473 .addReg(BaseReg, getKillRegState(BaseKill))
1474 .addImm(Pred).addReg(PredReg)
1475 .addReg(EvenReg, getDefRegState(isLd) | getDeadRegState(EvenDeadKill))
1476 .addReg(OddReg, getDefRegState(isLd) | getDeadRegState(OddDeadKill));
1477 ++NumLDRD2LDM;
Evan Cheng1283c6a2009-06-15 08:28:29 +00001478 } else {
Matthias Braunba3ecc32015-06-24 20:03:27 +00001479 BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(NewOpc))
1480 .addReg(BaseReg, getKillRegState(BaseKill))
1481 .addImm(Pred).addReg(PredReg)
1482 .addReg(EvenReg,
1483 getKillRegState(EvenDeadKill) | getUndefRegState(EvenUndef))
1484 .addReg(OddReg,
1485 getKillRegState(OddDeadKill) | getUndefRegState(OddUndef));
1486 ++NumSTRD2STM;
Evan Cheng1283c6a2009-06-15 08:28:29 +00001487 }
Matthias Braunba3ecc32015-06-24 20:03:27 +00001488 } else {
1489 // Split into two instructions.
1490 unsigned NewOpc = (isLd)
1491 ? (isT2 ? (OffImm < 0 ? ARM::t2LDRi8 : ARM::t2LDRi12) : ARM::LDRi12)
1492 : (isT2 ? (OffImm < 0 ? ARM::t2STRi8 : ARM::t2STRi12) : ARM::STRi12);
1493 // Be extra careful for thumb2. t2LDRi8 can't reference a zero offset,
1494 // so adjust and use t2LDRi12 here for that.
1495 unsigned NewOpc2 = (isLd)
1496 ? (isT2 ? (OffImm+4 < 0 ? ARM::t2LDRi8 : ARM::t2LDRi12) : ARM::LDRi12)
1497 : (isT2 ? (OffImm+4 < 0 ? ARM::t2STRi8 : ARM::t2STRi12) : ARM::STRi12);
1498 DebugLoc dl = MBBI->getDebugLoc();
1499 // If this is a load and base register is killed, it may have been
1500 // re-defed by the load, make sure the first load does not clobber it.
1501 if (isLd &&
1502 (BaseKill || OffKill) &&
1503 (TRI->regsOverlap(EvenReg, BaseReg))) {
1504 assert(!TRI->regsOverlap(OddReg, BaseReg));
1505 InsertLDR_STR(MBB, MBBI, OffImm+4, isLd, dl, NewOpc2,
1506 OddReg, OddDeadKill, false,
1507 BaseReg, false, BaseUndef, false, OffUndef,
1508 Pred, PredReg, TII, isT2);
Matthias Braunba3ecc32015-06-24 20:03:27 +00001509 InsertLDR_STR(MBB, MBBI, OffImm, isLd, dl, NewOpc,
1510 EvenReg, EvenDeadKill, false,
1511 BaseReg, BaseKill, BaseUndef, OffKill, OffUndef,
1512 Pred, PredReg, TII, isT2);
1513 } else {
1514 if (OddReg == EvenReg && EvenDeadKill) {
1515 // If the two source operands are the same, the kill marker is
1516 // probably on the first one. e.g.
1517 // t2STRDi8 %R5<kill>, %R5, %R9<kill>, 0, 14, %reg0
1518 EvenDeadKill = false;
1519 OddDeadKill = true;
1520 }
1521 // Never kill the base register in the first instruction.
1522 if (EvenReg == BaseReg)
1523 EvenDeadKill = false;
1524 InsertLDR_STR(MBB, MBBI, OffImm, isLd, dl, NewOpc,
1525 EvenReg, EvenDeadKill, EvenUndef,
1526 BaseReg, false, BaseUndef, false, OffUndef,
1527 Pred, PredReg, TII, isT2);
Matthias Braunba3ecc32015-06-24 20:03:27 +00001528 InsertLDR_STR(MBB, MBBI, OffImm+4, isLd, dl, NewOpc2,
1529 OddReg, OddDeadKill, OddUndef,
1530 BaseReg, BaseKill, BaseUndef, OffKill, OffUndef,
1531 Pred, PredReg, TII, isT2);
1532 }
1533 if (isLd)
1534 ++NumLDRD2LDR;
1535 else
1536 ++NumSTRD2STR;
Evan Cheng1283c6a2009-06-15 08:28:29 +00001537 }
Matthias Braunba3ecc32015-06-24 20:03:27 +00001538
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001539 MBBI = MBB.erase(MBBI);
Matthias Braunba3ecc32015-06-24 20:03:27 +00001540 return true;
Evan Cheng1283c6a2009-06-15 08:28:29 +00001541}
1542
Matthias Braunec50fa62015-06-01 21:26:23 +00001543/// An optimization pass to turn multiple LDR / STR ops of the same base and
1544/// incrementing offset into LDM / STM ops.
Evan Cheng10043e22007-01-19 07:51:42 +00001545bool ARMLoadStoreOpt::LoadStoreMultipleOpti(MachineBasicBlock &MBB) {
Evan Cheng10043e22007-01-19 07:51:42 +00001546 MemOpQueue MemOps;
1547 unsigned CurrBase = 0;
Matthias Braunfa3872e2015-05-18 20:27:55 +00001548 unsigned CurrOpc = ~0u;
Evan Cheng0f7cbe82007-05-15 01:29:07 +00001549 ARMCC::CondCodes CurrPred = ARMCC::AL;
Evan Cheng10043e22007-01-19 07:51:42 +00001550 unsigned Position = 0;
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001551 assert(Candidates.size() == 0);
1552 LiveRegsValid = false;
Evan Chengd28de672007-03-06 18:02:41 +00001553
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001554 for (MachineBasicBlock::iterator I = MBB.end(), MBBI; I != MBB.begin();
1555 I = MBBI) {
1556 // The instruction in front of the iterator is the one we look at.
1557 MBBI = std::prev(I);
Evan Cheng1283c6a2009-06-15 08:28:29 +00001558 if (FixInvalidRegPairOp(MBB, MBBI))
1559 continue;
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001560 ++Position;
Evan Cheng1283c6a2009-06-15 08:28:29 +00001561
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001562 if (isMemoryOp(MBBI)) {
Matthias Braunfa3872e2015-05-18 20:27:55 +00001563 unsigned Opcode = MBBI->getOpcode();
Evan Cheng1fb4de82010-06-21 21:21:14 +00001564 const MachineOperand &MO = MBBI->getOperand(0);
1565 unsigned Reg = MO.getReg();
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001566 unsigned Base = getLoadStoreBaseOp(*MBBI).getReg();
Evan Cheng94f04c62007-07-05 07:18:20 +00001567 unsigned PredReg = 0;
Craig Topperf6e7e122012-03-27 07:21:54 +00001568 ARMCC::CondCodes Pred = getInstrPredicate(MBBI, PredReg);
Evan Cheng185c9ef2009-06-13 09:12:55 +00001569 int Offset = getMemoryOpOffset(MBBI);
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001570 if (CurrBase == 0) {
Evan Cheng10043e22007-01-19 07:51:42 +00001571 // Start of a new chain.
1572 CurrBase = Base;
1573 CurrOpc = Opcode;
Evan Cheng0f7cbe82007-05-15 01:29:07 +00001574 CurrPred = Pred;
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001575 MemOps.push_back(MemOpQueueEntry(MBBI, Offset, Position));
1576 continue;
1577 }
1578 // Note: No need to match PredReg in the next if.
1579 if (CurrOpc == Opcode && CurrBase == Base && CurrPred == Pred) {
1580 // Watch out for:
1581 // r4 := ldr [r0, #8]
1582 // r4 := ldr [r0, #4]
1583 // or
1584 // r0 := ldr [r0]
1585 // If a load overrides the base register or a register loaded by
1586 // another load in our chain, we cannot take this instruction.
1587 bool Overlap = false;
1588 if (isLoadSingle(Opcode)) {
1589 Overlap = (Base == Reg);
1590 if (!Overlap) {
1591 for (const MemOpQueueEntry &E : MemOps) {
1592 if (TRI->regsOverlap(Reg, E.MI->getOperand(0).getReg())) {
1593 Overlap = true;
Evan Cheng10043e22007-01-19 07:51:42 +00001594 break;
1595 }
1596 }
1597 }
1598 }
Evan Cheng10043e22007-01-19 07:51:42 +00001599
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001600 if (!Overlap) {
1601 // Check offset and sort memory operation into the current chain.
1602 if (Offset > MemOps.back().Offset) {
1603 MemOps.push_back(MemOpQueueEntry(MBBI, Offset, Position));
1604 continue;
1605 } else {
1606 MemOpQueue::iterator MI, ME;
1607 for (MI = MemOps.begin(), ME = MemOps.end(); MI != ME; ++MI) {
1608 if (Offset < MI->Offset) {
1609 // Found a place to insert.
1610 break;
1611 }
1612 if (Offset == MI->Offset) {
1613 // Collision, abort.
1614 MI = ME;
1615 break;
1616 }
1617 }
1618 if (MI != MemOps.end()) {
1619 MemOps.insert(MI, MemOpQueueEntry(MBBI, Offset, Position));
1620 continue;
1621 }
1622 }
Evan Cheng7f5976e2009-06-04 01:15:28 +00001623 }
Evan Cheng2818fdd2007-03-07 02:38:05 +00001624 }
Evan Cheng10043e22007-01-19 07:51:42 +00001625
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001626 // Don't advance the iterator; The op will start a new chain next.
1627 MBBI = I;
1628 --Position;
1629 // Fallthrough to look into existing chain.
Matthias Braun84e28972015-07-20 23:17:16 +00001630 } else if (MBBI->isDebugValue())
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001631 continue;
1632
1633 // If we are here then the chain is broken; Extract candidates for a merge.
1634 if (MemOps.size() > 0) {
1635 FormCandidates(MemOps);
1636 // Reset for the next chain.
Evan Cheng10043e22007-01-19 07:51:42 +00001637 CurrBase = 0;
Matthias Braunfa3872e2015-05-18 20:27:55 +00001638 CurrOpc = ~0u;
Evan Cheng0f7cbe82007-05-15 01:29:07 +00001639 CurrPred = ARMCC::AL;
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001640 MemOps.clear();
Evan Cheng10043e22007-01-19 07:51:42 +00001641 }
1642 }
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001643 if (MemOps.size() > 0)
1644 FormCandidates(MemOps);
1645
1646 // Sort candidates so they get processed from end to begin of the basic
1647 // block later; This is necessary for liveness calculation.
1648 auto LessThan = [](const MergeCandidate* M0, const MergeCandidate *M1) {
1649 return M0->InsertPos < M1->InsertPos;
1650 };
1651 std::sort(Candidates.begin(), Candidates.end(), LessThan);
1652
1653 // Go through list of candidates and merge.
1654 bool Changed = false;
1655 for (const MergeCandidate *Candidate : Candidates) {
Matthias Braun731e3592015-07-20 23:17:20 +00001656 if (Candidate->Instrs.size() > 1) {
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001657 MachineInstr *Merged = MergeOpsUpdate(*Candidate);
1658 // Merge preceding/trailing base inc/dec into the merged op.
1659 if (Merged) {
Matthias Braun731e3592015-07-20 23:17:20 +00001660 MergeBaseUpdateLSMultiple(Merged);
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001661 Changed = true;
1662 } else {
1663 for (MachineInstr *MI : Candidate->Instrs) {
1664 if (MergeBaseUpdateLoadStore(MI))
1665 Changed = true;
1666 }
1667 }
1668 } else {
1669 assert(Candidate->Instrs.size() == 1);
1670 if (MergeBaseUpdateLoadStore(Candidate->Instrs.front()))
1671 Changed = true;
1672 }
1673 }
1674 Candidates.clear();
1675
1676 return Changed;
Evan Cheng10043e22007-01-19 07:51:42 +00001677}
1678
Matthias Braunec50fa62015-06-01 21:26:23 +00001679/// If this is a exit BB, try merging the return ops ("bx lr" and "mov pc, lr")
1680/// into the preceding stack restore so it directly restore the value of LR
1681/// into pc.
Bob Wilson162242b2010-03-20 22:20:40 +00001682/// ldmfd sp!, {..., lr}
Evan Cheng10043e22007-01-19 07:51:42 +00001683/// bx lr
Bob Wilson162242b2010-03-20 22:20:40 +00001684/// or
1685/// ldmfd sp!, {..., lr}
1686/// mov pc, lr
Evan Cheng10043e22007-01-19 07:51:42 +00001687/// =>
Bob Wilson162242b2010-03-20 22:20:40 +00001688/// ldmfd sp!, {..., pc}
Evan Cheng10043e22007-01-19 07:51:42 +00001689bool ARMLoadStoreOpt::MergeReturnIntoLDM(MachineBasicBlock &MBB) {
James Molloy556763d2014-05-16 14:14:30 +00001690 // Thumb1 LDM doesn't allow high registers.
1691 if (isThumb1) return false;
Evan Cheng10043e22007-01-19 07:51:42 +00001692 if (MBB.empty()) return false;
1693
Jakob Stoklund Olesenbbb1a542011-01-13 22:47:43 +00001694 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Evan Cheng4605e8a2009-07-09 23:11:34 +00001695 if (MBBI != MBB.begin() &&
Bob Wilson162242b2010-03-20 22:20:40 +00001696 (MBBI->getOpcode() == ARM::BX_RET ||
1697 MBBI->getOpcode() == ARM::tBX_RET ||
1698 MBBI->getOpcode() == ARM::MOVPCLR)) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001699 MachineInstr *PrevMI = std::prev(MBBI);
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001700 unsigned Opcode = PrevMI->getOpcode();
1701 if (Opcode == ARM::LDMIA_UPD || Opcode == ARM::LDMDA_UPD ||
1702 Opcode == ARM::LDMDB_UPD || Opcode == ARM::LDMIB_UPD ||
1703 Opcode == ARM::t2LDMIA_UPD || Opcode == ARM::t2LDMDB_UPD) {
Evan Cheng10043e22007-01-19 07:51:42 +00001704 MachineOperand &MO = PrevMI->getOperand(PrevMI->getNumOperands()-1);
Evan Cheng71756e72009-08-04 01:43:45 +00001705 if (MO.getReg() != ARM::LR)
1706 return false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001707 unsigned NewOpc = (isThumb2 ? ARM::t2LDMIA_RET : ARM::LDMIA_RET);
1708 assert(((isThumb2 && Opcode == ARM::t2LDMIA_UPD) ||
1709 Opcode == ARM::LDMIA_UPD) && "Unsupported multiple load-return!");
Evan Cheng71756e72009-08-04 01:43:45 +00001710 PrevMI->setDesc(TII->get(NewOpc));
1711 MO.setReg(ARM::PC);
Jakob Stoklund Olesen33f5d142012-12-20 22:54:02 +00001712 PrevMI->copyImplicitOps(*MBB.getParent(), &*MBBI);
Evan Cheng71756e72009-08-04 01:43:45 +00001713 MBB.erase(MBBI);
1714 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00001715 }
1716 }
1717 return false;
1718}
1719
1720bool ARMLoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001721 MF = &Fn;
Eric Christopher1b21f002015-01-29 00:19:33 +00001722 STI = &static_cast<const ARMSubtarget &>(Fn.getSubtarget());
1723 TL = STI->getTargetLowering();
Evan Chengf030f2d2007-03-07 20:30:36 +00001724 AFI = Fn.getInfo<ARMFunctionInfo>();
Eric Christopher1b21f002015-01-29 00:19:33 +00001725 TII = STI->getInstrInfo();
1726 TRI = STI->getRegisterInfo();
Matthias Brauna4a3182d2015-07-10 18:08:49 +00001727 MRI = &Fn.getRegInfo();
1728 RegClassInfoValid = false;
Evan Cheng4605e8a2009-07-09 23:11:34 +00001729 isThumb2 = AFI->isThumb2Function();
James Molloy92a15072014-05-16 14:11:38 +00001730 isThumb1 = AFI->isThumbFunction() && !isThumb2;
1731
Evan Cheng10043e22007-01-19 07:51:42 +00001732 bool Modified = false;
1733 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
1734 ++MFI) {
1735 MachineBasicBlock &MBB = *MFI;
1736 Modified |= LoadStoreMultipleOpti(MBB);
Eric Christopher1b21f002015-01-29 00:19:33 +00001737 if (STI->hasV5TOps())
Bob Wilson914df822011-01-06 19:24:41 +00001738 Modified |= MergeReturnIntoLDM(MBB);
Evan Cheng10043e22007-01-19 07:51:42 +00001739 }
Evan Chengd28de672007-03-06 18:02:41 +00001740
Matthias Braun22f39602015-07-20 23:17:14 +00001741 Allocator.Reset();
Evan Cheng10043e22007-01-19 07:51:42 +00001742 return Modified;
1743}
Evan Cheng185c9ef2009-06-13 09:12:55 +00001744
Evan Cheng185c9ef2009-06-13 09:12:55 +00001745namespace {
Matthias Braunec50fa62015-06-01 21:26:23 +00001746 /// Pre- register allocation pass that move load / stores from consecutive
1747 /// locations close to make it more likely they will be combined later.
Nick Lewycky02d5f772009-10-25 06:33:48 +00001748 struct ARMPreAllocLoadStoreOpt : public MachineFunctionPass{
Evan Cheng185c9ef2009-06-13 09:12:55 +00001749 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +00001750 ARMPreAllocLoadStoreOpt() : MachineFunctionPass(ID) {}
Evan Cheng185c9ef2009-06-13 09:12:55 +00001751
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001752 const DataLayout *TD;
Evan Cheng185c9ef2009-06-13 09:12:55 +00001753 const TargetInstrInfo *TII;
1754 const TargetRegisterInfo *TRI;
Evan Cheng1283c6a2009-06-15 08:28:29 +00001755 const ARMSubtarget *STI;
Evan Cheng185c9ef2009-06-13 09:12:55 +00001756 MachineRegisterInfo *MRI;
Evan Chengfd6aad72009-09-25 21:44:53 +00001757 MachineFunction *MF;
Evan Cheng185c9ef2009-06-13 09:12:55 +00001758
Craig Topper6bc27bf2014-03-10 02:09:33 +00001759 bool runOnMachineFunction(MachineFunction &Fn) override;
Evan Cheng185c9ef2009-06-13 09:12:55 +00001760
Craig Topper6bc27bf2014-03-10 02:09:33 +00001761 const char *getPassName() const override {
Evan Cheng185c9ef2009-06-13 09:12:55 +00001762 return "ARM pre- register allocation load / store optimization pass";
1763 }
1764
1765 private:
Evan Chengeba57e42009-06-15 20:54:56 +00001766 bool CanFormLdStDWord(MachineInstr *Op0, MachineInstr *Op1, DebugLoc &dl,
1767 unsigned &NewOpc, unsigned &EvenReg,
1768 unsigned &OddReg, unsigned &BaseReg,
Jim Grosbach338de3e2010-10-27 23:12:14 +00001769 int &Offset,
Evan Chengfd6aad72009-09-25 21:44:53 +00001770 unsigned &PredReg, ARMCC::CondCodes &Pred,
1771 bool &isT2);
Evan Cheng185c9ef2009-06-13 09:12:55 +00001772 bool RescheduleOps(MachineBasicBlock *MBB,
Craig Topperaf0dea12013-07-04 01:31:24 +00001773 SmallVectorImpl<MachineInstr *> &Ops,
Evan Cheng185c9ef2009-06-13 09:12:55 +00001774 unsigned Base, bool isLd,
1775 DenseMap<MachineInstr*, unsigned> &MI2LocMap);
1776 bool RescheduleLoadStoreInstrs(MachineBasicBlock *MBB);
1777 };
1778 char ARMPreAllocLoadStoreOpt::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001779}
Evan Cheng185c9ef2009-06-13 09:12:55 +00001780
1781bool ARMPreAllocLoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
Mehdi Aminibd7287e2015-07-16 06:11:10 +00001782 TD = &Fn.getDataLayout();
Eric Christopher7c558cf2014-10-14 08:44:19 +00001783 STI = &static_cast<const ARMSubtarget &>(Fn.getSubtarget());
Eric Christopher1b21f002015-01-29 00:19:33 +00001784 TII = STI->getInstrInfo();
1785 TRI = STI->getRegisterInfo();
Evan Cheng185c9ef2009-06-13 09:12:55 +00001786 MRI = &Fn.getRegInfo();
Evan Chengfd6aad72009-09-25 21:44:53 +00001787 MF = &Fn;
Evan Cheng185c9ef2009-06-13 09:12:55 +00001788
1789 bool Modified = false;
1790 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
1791 ++MFI)
1792 Modified |= RescheduleLoadStoreInstrs(MFI);
1793
1794 return Modified;
1795}
1796
Evan Chengb4b20bb2009-06-19 23:17:27 +00001797static bool IsSafeAndProfitableToMove(bool isLd, unsigned Base,
1798 MachineBasicBlock::iterator I,
1799 MachineBasicBlock::iterator E,
Craig Topper71b7b682014-08-21 05:55:13 +00001800 SmallPtrSetImpl<MachineInstr*> &MemOps,
Evan Chengb4b20bb2009-06-19 23:17:27 +00001801 SmallSet<unsigned, 4> &MemRegs,
1802 const TargetRegisterInfo *TRI) {
Evan Cheng185c9ef2009-06-13 09:12:55 +00001803 // Are there stores / loads / calls between them?
1804 // FIXME: This is overly conservative. We should make use of alias information
1805 // some day.
Evan Chengb4b20bb2009-06-19 23:17:27 +00001806 SmallSet<unsigned, 4> AddedRegPressure;
Evan Cheng185c9ef2009-06-13 09:12:55 +00001807 while (++I != E) {
Jim Grosbach4e5e6a82010-06-04 01:23:30 +00001808 if (I->isDebugValue() || MemOps.count(&*I))
Evan Chengb4b20bb2009-06-19 23:17:27 +00001809 continue;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001810 if (I->isCall() || I->isTerminator() || I->hasUnmodeledSideEffects())
Evan Cheng185c9ef2009-06-13 09:12:55 +00001811 return false;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001812 if (isLd && I->mayStore())
Evan Cheng185c9ef2009-06-13 09:12:55 +00001813 return false;
1814 if (!isLd) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00001815 if (I->mayLoad())
Evan Cheng185c9ef2009-06-13 09:12:55 +00001816 return false;
1817 // It's not safe to move the first 'str' down.
1818 // str r1, [r0]
1819 // strh r5, [r0]
1820 // str r4, [r0, #+4]
Evan Cheng7f8e5632011-12-07 07:15:52 +00001821 if (I->mayStore())
Evan Cheng185c9ef2009-06-13 09:12:55 +00001822 return false;
1823 }
1824 for (unsigned j = 0, NumOps = I->getNumOperands(); j != NumOps; ++j) {
1825 MachineOperand &MO = I->getOperand(j);
Evan Chengb4b20bb2009-06-19 23:17:27 +00001826 if (!MO.isReg())
1827 continue;
1828 unsigned Reg = MO.getReg();
1829 if (MO.isDef() && TRI->regsOverlap(Reg, Base))
Evan Cheng185c9ef2009-06-13 09:12:55 +00001830 return false;
Evan Chengb4b20bb2009-06-19 23:17:27 +00001831 if (Reg != Base && !MemRegs.count(Reg))
1832 AddedRegPressure.insert(Reg);
Evan Cheng185c9ef2009-06-13 09:12:55 +00001833 }
1834 }
Evan Chengb4b20bb2009-06-19 23:17:27 +00001835
1836 // Estimate register pressure increase due to the transformation.
1837 if (MemRegs.size() <= 4)
1838 // Ok if we are moving small number of instructions.
1839 return true;
1840 return AddedRegPressure.size() <= MemRegs.size() * 2;
Evan Cheng185c9ef2009-06-13 09:12:55 +00001841}
1842
Andrew Trick28c1d182011-11-11 22:18:09 +00001843
Matthias Braunec50fa62015-06-01 21:26:23 +00001844/// Copy \p Op0 and \p Op1 operands into a new array assigned to MI.
Andrew Trick28c1d182011-11-11 22:18:09 +00001845static void concatenateMemOperands(MachineInstr *MI, MachineInstr *Op0,
1846 MachineInstr *Op1) {
1847 assert(MI->memoperands_empty() && "expected a new machineinstr");
1848 size_t numMemRefs = (Op0->memoperands_end() - Op0->memoperands_begin())
1849 + (Op1->memoperands_end() - Op1->memoperands_begin());
1850
1851 MachineFunction *MF = MI->getParent()->getParent();
1852 MachineSDNode::mmo_iterator MemBegin = MF->allocateMemRefsArray(numMemRefs);
1853 MachineSDNode::mmo_iterator MemEnd =
1854 std::copy(Op0->memoperands_begin(), Op0->memoperands_end(), MemBegin);
1855 MemEnd =
1856 std::copy(Op1->memoperands_begin(), Op1->memoperands_end(), MemEnd);
1857 MI->setMemRefs(MemBegin, MemEnd);
1858}
1859
Evan Chengeba57e42009-06-15 20:54:56 +00001860bool
1861ARMPreAllocLoadStoreOpt::CanFormLdStDWord(MachineInstr *Op0, MachineInstr *Op1,
Matthias Braun125c9f52015-06-03 16:30:24 +00001862 DebugLoc &dl, unsigned &NewOpc,
1863 unsigned &FirstReg,
1864 unsigned &SecondReg,
1865 unsigned &BaseReg, int &Offset,
1866 unsigned &PredReg,
Evan Chengfd6aad72009-09-25 21:44:53 +00001867 ARMCC::CondCodes &Pred,
1868 bool &isT2) {
Evan Cheng139c3db2009-09-29 07:07:30 +00001869 // Make sure we're allowed to generate LDRD/STRD.
1870 if (!STI->hasV5TEOps())
1871 return false;
1872
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001873 // FIXME: VLDRS / VSTRS -> VLDRD / VSTRD
Evan Chengfd6aad72009-09-25 21:44:53 +00001874 unsigned Scale = 1;
Evan Chengeba57e42009-06-15 20:54:56 +00001875 unsigned Opcode = Op0->getOpcode();
James Molloybb73c232014-05-16 14:08:46 +00001876 if (Opcode == ARM::LDRi12) {
Evan Chengeba57e42009-06-15 20:54:56 +00001877 NewOpc = ARM::LDRD;
James Molloybb73c232014-05-16 14:08:46 +00001878 } else if (Opcode == ARM::STRi12) {
Evan Chengeba57e42009-06-15 20:54:56 +00001879 NewOpc = ARM::STRD;
James Molloybb73c232014-05-16 14:08:46 +00001880 } else if (Opcode == ARM::t2LDRi8 || Opcode == ARM::t2LDRi12) {
Evan Chengfd6aad72009-09-25 21:44:53 +00001881 NewOpc = ARM::t2LDRDi8;
1882 Scale = 4;
1883 isT2 = true;
1884 } else if (Opcode == ARM::t2STRi8 || Opcode == ARM::t2STRi12) {
1885 NewOpc = ARM::t2STRDi8;
1886 Scale = 4;
1887 isT2 = true;
James Molloybb73c232014-05-16 14:08:46 +00001888 } else {
Evan Chengfd6aad72009-09-25 21:44:53 +00001889 return false;
James Molloybb73c232014-05-16 14:08:46 +00001890 }
Evan Chengfd6aad72009-09-25 21:44:53 +00001891
Jim Grosbach9302bfd2010-10-26 19:34:41 +00001892 // Make sure the base address satisfies i64 ld / st alignment requirement.
Quentin Colombet663150f2013-06-20 22:51:44 +00001893 // At the moment, we ignore the memoryoperand's value.
1894 // If we want to use AliasAnalysis, we should check it accordingly.
Evan Chengeba57e42009-06-15 20:54:56 +00001895 if (!Op0->hasOneMemOperand() ||
Dan Gohman48b185d2009-09-25 20:36:54 +00001896 (*Op0->memoperands_begin())->isVolatile())
Evan Cheng1283c6a2009-06-15 08:28:29 +00001897 return false;
1898
Dan Gohman48b185d2009-09-25 20:36:54 +00001899 unsigned Align = (*Op0->memoperands_begin())->getAlignment();
Dan Gohman913c9982010-04-15 04:33:49 +00001900 const Function *Func = MF->getFunction();
Evan Cheng1283c6a2009-06-15 08:28:29 +00001901 unsigned ReqAlign = STI->hasV6Ops()
Jim Grosbach338de3e2010-10-27 23:12:14 +00001902 ? TD->getABITypeAlignment(Type::getInt64Ty(Func->getContext()))
Evan Chengfd6aad72009-09-25 21:44:53 +00001903 : 8; // Pre-v6 need 8-byte align
Evan Chengeba57e42009-06-15 20:54:56 +00001904 if (Align < ReqAlign)
1905 return false;
1906
1907 // Then make sure the immediate offset fits.
1908 int OffImm = getMemoryOpOffset(Op0);
Evan Chenga6b9cab2009-09-27 09:46:04 +00001909 if (isT2) {
Evan Cheng42401d62011-03-15 18:41:52 +00001910 int Limit = (1 << 8) * Scale;
1911 if (OffImm >= Limit || (OffImm <= -Limit) || (OffImm & (Scale-1)))
1912 return false;
Evan Chengfd6aad72009-09-25 21:44:53 +00001913 Offset = OffImm;
Evan Chenga6b9cab2009-09-27 09:46:04 +00001914 } else {
1915 ARM_AM::AddrOpc AddSub = ARM_AM::add;
1916 if (OffImm < 0) {
1917 AddSub = ARM_AM::sub;
1918 OffImm = - OffImm;
1919 }
1920 int Limit = (1 << 8) * Scale;
1921 if (OffImm >= Limit || (OffImm & (Scale-1)))
1922 return false;
Evan Chengfd6aad72009-09-25 21:44:53 +00001923 Offset = ARM_AM::getAM3Opc(AddSub, OffImm);
Evan Chenga6b9cab2009-09-27 09:46:04 +00001924 }
Matthias Braun125c9f52015-06-03 16:30:24 +00001925 FirstReg = Op0->getOperand(0).getReg();
1926 SecondReg = Op1->getOperand(0).getReg();
1927 if (FirstReg == SecondReg)
Evan Chengeba57e42009-06-15 20:54:56 +00001928 return false;
1929 BaseReg = Op0->getOperand(1).getReg();
Craig Topperf6e7e122012-03-27 07:21:54 +00001930 Pred = getInstrPredicate(Op0, PredReg);
Evan Chengeba57e42009-06-15 20:54:56 +00001931 dl = Op0->getDebugLoc();
1932 return true;
Evan Cheng1283c6a2009-06-15 08:28:29 +00001933}
1934
Evan Cheng185c9ef2009-06-13 09:12:55 +00001935bool ARMPreAllocLoadStoreOpt::RescheduleOps(MachineBasicBlock *MBB,
Craig Topperaf0dea12013-07-04 01:31:24 +00001936 SmallVectorImpl<MachineInstr *> &Ops,
Evan Cheng185c9ef2009-06-13 09:12:55 +00001937 unsigned Base, bool isLd,
1938 DenseMap<MachineInstr*, unsigned> &MI2LocMap) {
1939 bool RetVal = false;
1940
1941 // Sort by offset (in reverse order).
Benjamin Kramer3a377bc2014-03-01 11:47:00 +00001942 std::sort(Ops.begin(), Ops.end(),
1943 [](const MachineInstr *LHS, const MachineInstr *RHS) {
1944 int LOffset = getMemoryOpOffset(LHS);
1945 int ROffset = getMemoryOpOffset(RHS);
1946 assert(LHS == RHS || LOffset != ROffset);
1947 return LOffset > ROffset;
1948 });
Evan Cheng185c9ef2009-06-13 09:12:55 +00001949
1950 // The loads / stores of the same base are in order. Scan them from first to
Jim Grosbach1bcdf322010-06-04 00:15:00 +00001951 // last and check for the following:
Evan Cheng185c9ef2009-06-13 09:12:55 +00001952 // 1. Any def of base.
1953 // 2. Any gaps.
1954 while (Ops.size() > 1) {
1955 unsigned FirstLoc = ~0U;
1956 unsigned LastLoc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001957 MachineInstr *FirstOp = nullptr;
1958 MachineInstr *LastOp = nullptr;
Evan Cheng185c9ef2009-06-13 09:12:55 +00001959 int LastOffset = 0;
Evan Cheng0e796032009-06-18 02:04:01 +00001960 unsigned LastOpcode = 0;
Evan Cheng185c9ef2009-06-13 09:12:55 +00001961 unsigned LastBytes = 0;
1962 unsigned NumMove = 0;
1963 for (int i = Ops.size() - 1; i >= 0; --i) {
1964 MachineInstr *Op = Ops[i];
1965 unsigned Loc = MI2LocMap[Op];
1966 if (Loc <= FirstLoc) {
1967 FirstLoc = Loc;
1968 FirstOp = Op;
1969 }
1970 if (Loc >= LastLoc) {
1971 LastLoc = Loc;
1972 LastOp = Op;
1973 }
1974
Andrew Trick642f0f62012-01-11 03:56:08 +00001975 unsigned LSMOpcode
1976 = getLoadStoreMultipleOpcode(Op->getOpcode(), ARM_AM::ia);
1977 if (LastOpcode && LSMOpcode != LastOpcode)
Evan Cheng0e796032009-06-18 02:04:01 +00001978 break;
1979
Evan Cheng185c9ef2009-06-13 09:12:55 +00001980 int Offset = getMemoryOpOffset(Op);
1981 unsigned Bytes = getLSMultipleTransferSize(Op);
1982 if (LastBytes) {
1983 if (Bytes != LastBytes || Offset != (LastOffset + (int)Bytes))
1984 break;
1985 }
1986 LastOffset = Offset;
1987 LastBytes = Bytes;
Andrew Trick642f0f62012-01-11 03:56:08 +00001988 LastOpcode = LSMOpcode;
Evan Chengfd6aad72009-09-25 21:44:53 +00001989 if (++NumMove == 8) // FIXME: Tune this limit.
Evan Cheng185c9ef2009-06-13 09:12:55 +00001990 break;
1991 }
1992
1993 if (NumMove <= 1)
1994 Ops.pop_back();
1995 else {
Evan Chengb4b20bb2009-06-19 23:17:27 +00001996 SmallPtrSet<MachineInstr*, 4> MemOps;
1997 SmallSet<unsigned, 4> MemRegs;
1998 for (int i = NumMove-1; i >= 0; --i) {
1999 MemOps.insert(Ops[i]);
2000 MemRegs.insert(Ops[i]->getOperand(0).getReg());
2001 }
Evan Cheng185c9ef2009-06-13 09:12:55 +00002002
2003 // Be conservative, if the instructions are too far apart, don't
2004 // move them. We want to limit the increase of register pressure.
Evan Chengb4b20bb2009-06-19 23:17:27 +00002005 bool DoMove = (LastLoc - FirstLoc) <= NumMove*4; // FIXME: Tune this.
Evan Cheng185c9ef2009-06-13 09:12:55 +00002006 if (DoMove)
Evan Chengb4b20bb2009-06-19 23:17:27 +00002007 DoMove = IsSafeAndProfitableToMove(isLd, Base, FirstOp, LastOp,
2008 MemOps, MemRegs, TRI);
Evan Cheng185c9ef2009-06-13 09:12:55 +00002009 if (!DoMove) {
2010 for (unsigned i = 0; i != NumMove; ++i)
2011 Ops.pop_back();
2012 } else {
2013 // This is the new location for the loads / stores.
2014 MachineBasicBlock::iterator InsertPos = isLd ? FirstOp : LastOp;
Jim Grosbachf14e08b2010-06-15 00:41:09 +00002015 while (InsertPos != MBB->end()
2016 && (MemOps.count(InsertPos) || InsertPos->isDebugValue()))
Evan Cheng185c9ef2009-06-13 09:12:55 +00002017 ++InsertPos;
Evan Cheng1283c6a2009-06-15 08:28:29 +00002018
2019 // If we are moving a pair of loads / stores, see if it makes sense
2020 // to try to allocate a pair of registers that can form register pairs.
Evan Chengeba57e42009-06-15 20:54:56 +00002021 MachineInstr *Op0 = Ops.back();
2022 MachineInstr *Op1 = Ops[Ops.size()-2];
Matthias Braun125c9f52015-06-03 16:30:24 +00002023 unsigned FirstReg = 0, SecondReg = 0;
Jim Grosbach338de3e2010-10-27 23:12:14 +00002024 unsigned BaseReg = 0, PredReg = 0;
Evan Chengeba57e42009-06-15 20:54:56 +00002025 ARMCC::CondCodes Pred = ARMCC::AL;
Evan Chengfd6aad72009-09-25 21:44:53 +00002026 bool isT2 = false;
Evan Chengeba57e42009-06-15 20:54:56 +00002027 unsigned NewOpc = 0;
Evan Chenga6b9cab2009-09-27 09:46:04 +00002028 int Offset = 0;
Evan Chengeba57e42009-06-15 20:54:56 +00002029 DebugLoc dl;
2030 if (NumMove == 2 && CanFormLdStDWord(Op0, Op1, dl, NewOpc,
Matthias Braun125c9f52015-06-03 16:30:24 +00002031 FirstReg, SecondReg, BaseReg,
Evan Chengfd6aad72009-09-25 21:44:53 +00002032 Offset, PredReg, Pred, isT2)) {
Evan Chengeba57e42009-06-15 20:54:56 +00002033 Ops.pop_back();
2034 Ops.pop_back();
Evan Cheng1283c6a2009-06-15 08:28:29 +00002035
Evan Cheng6cc775f2011-06-28 19:10:37 +00002036 const MCInstrDesc &MCID = TII->get(NewOpc);
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00002037 const TargetRegisterClass *TRC = TII->getRegClass(MCID, 0, TRI, *MF);
Matthias Braun125c9f52015-06-03 16:30:24 +00002038 MRI->constrainRegClass(FirstReg, TRC);
2039 MRI->constrainRegClass(SecondReg, TRC);
Cameron Zwarichec645bf2011-05-18 21:25:14 +00002040
Evan Chengeba57e42009-06-15 20:54:56 +00002041 // Form the pair instruction.
Evan Cheng0e796032009-06-18 02:04:01 +00002042 if (isLd) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00002043 MachineInstrBuilder MIB = BuildMI(*MBB, InsertPos, dl, MCID)
Matthias Braun125c9f52015-06-03 16:30:24 +00002044 .addReg(FirstReg, RegState::Define)
2045 .addReg(SecondReg, RegState::Define)
Evan Chengfd6aad72009-09-25 21:44:53 +00002046 .addReg(BaseReg);
Jim Grosbach338de3e2010-10-27 23:12:14 +00002047 // FIXME: We're converting from LDRi12 to an insn that still
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002048 // uses addrmode2, so we need an explicit offset reg. It should
Jim Grosbach338de3e2010-10-27 23:12:14 +00002049 // always by reg0 since we're transforming LDRi12s.
Evan Chengfd6aad72009-09-25 21:44:53 +00002050 if (!isT2)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002051 MIB.addReg(0);
Evan Chengfd6aad72009-09-25 21:44:53 +00002052 MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
Andrew Trick28c1d182011-11-11 22:18:09 +00002053 concatenateMemOperands(MIB, Op0, Op1);
2054 DEBUG(dbgs() << "Formed " << *MIB << "\n");
Evan Cheng0e796032009-06-18 02:04:01 +00002055 ++NumLDRDFormed;
2056 } else {
Evan Cheng6cc775f2011-06-28 19:10:37 +00002057 MachineInstrBuilder MIB = BuildMI(*MBB, InsertPos, dl, MCID)
Matthias Braun125c9f52015-06-03 16:30:24 +00002058 .addReg(FirstReg)
2059 .addReg(SecondReg)
Evan Chengfd6aad72009-09-25 21:44:53 +00002060 .addReg(BaseReg);
Jim Grosbach338de3e2010-10-27 23:12:14 +00002061 // FIXME: We're converting from LDRi12 to an insn that still
2062 // uses addrmode2, so we need an explicit offset reg. It should
2063 // always by reg0 since we're transforming STRi12s.
Evan Chengfd6aad72009-09-25 21:44:53 +00002064 if (!isT2)
Jim Grosbach338de3e2010-10-27 23:12:14 +00002065 MIB.addReg(0);
Evan Chengfd6aad72009-09-25 21:44:53 +00002066 MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
Andrew Trick28c1d182011-11-11 22:18:09 +00002067 concatenateMemOperands(MIB, Op0, Op1);
2068 DEBUG(dbgs() << "Formed " << *MIB << "\n");
Evan Cheng0e796032009-06-18 02:04:01 +00002069 ++NumSTRDFormed;
2070 }
2071 MBB->erase(Op0);
2072 MBB->erase(Op1);
Evan Cheng1283c6a2009-06-15 08:28:29 +00002073
Matthias Braun125c9f52015-06-03 16:30:24 +00002074 if (!isT2) {
2075 // Add register allocation hints to form register pairs.
2076 MRI->setRegAllocationHint(FirstReg, ARMRI::RegPairEven, SecondReg);
2077 MRI->setRegAllocationHint(SecondReg, ARMRI::RegPairOdd, FirstReg);
2078 }
Evan Chengeba57e42009-06-15 20:54:56 +00002079 } else {
2080 for (unsigned i = 0; i != NumMove; ++i) {
2081 MachineInstr *Op = Ops.back();
2082 Ops.pop_back();
2083 MBB->splice(InsertPos, MBB, Op);
2084 }
Evan Cheng185c9ef2009-06-13 09:12:55 +00002085 }
2086
2087 NumLdStMoved += NumMove;
2088 RetVal = true;
2089 }
2090 }
2091 }
2092
2093 return RetVal;
2094}
2095
2096bool
2097ARMPreAllocLoadStoreOpt::RescheduleLoadStoreInstrs(MachineBasicBlock *MBB) {
2098 bool RetVal = false;
2099
2100 DenseMap<MachineInstr*, unsigned> MI2LocMap;
2101 DenseMap<unsigned, SmallVector<MachineInstr*, 4> > Base2LdsMap;
2102 DenseMap<unsigned, SmallVector<MachineInstr*, 4> > Base2StsMap;
2103 SmallVector<unsigned, 4> LdBases;
2104 SmallVector<unsigned, 4> StBases;
2105
2106 unsigned Loc = 0;
2107 MachineBasicBlock::iterator MBBI = MBB->begin();
2108 MachineBasicBlock::iterator E = MBB->end();
2109 while (MBBI != E) {
2110 for (; MBBI != E; ++MBBI) {
2111 MachineInstr *MI = MBBI;
Evan Cheng7f8e5632011-12-07 07:15:52 +00002112 if (MI->isCall() || MI->isTerminator()) {
Evan Cheng185c9ef2009-06-13 09:12:55 +00002113 // Stop at barriers.
2114 ++MBBI;
2115 break;
2116 }
2117
Jim Grosbach4e5e6a82010-06-04 01:23:30 +00002118 if (!MI->isDebugValue())
2119 MI2LocMap[MI] = ++Loc;
2120
Evan Cheng185c9ef2009-06-13 09:12:55 +00002121 if (!isMemoryOp(MI))
2122 continue;
2123 unsigned PredReg = 0;
Craig Topperf6e7e122012-03-27 07:21:54 +00002124 if (getInstrPredicate(MI, PredReg) != ARMCC::AL)
Evan Cheng185c9ef2009-06-13 09:12:55 +00002125 continue;
2126
Evan Chengfd6aad72009-09-25 21:44:53 +00002127 int Opc = MI->getOpcode();
Matthias Brauna4a3182d2015-07-10 18:08:49 +00002128 bool isLd = isLoadSingle(Opc);
Evan Cheng185c9ef2009-06-13 09:12:55 +00002129 unsigned Base = MI->getOperand(1).getReg();
2130 int Offset = getMemoryOpOffset(MI);
2131
2132 bool StopHere = false;
2133 if (isLd) {
2134 DenseMap<unsigned, SmallVector<MachineInstr*, 4> >::iterator BI =
2135 Base2LdsMap.find(Base);
2136 if (BI != Base2LdsMap.end()) {
2137 for (unsigned i = 0, e = BI->second.size(); i != e; ++i) {
2138 if (Offset == getMemoryOpOffset(BI->second[i])) {
2139 StopHere = true;
2140 break;
2141 }
2142 }
2143 if (!StopHere)
2144 BI->second.push_back(MI);
2145 } else {
Craig Topper9ae47072013-07-10 16:38:35 +00002146 Base2LdsMap[Base].push_back(MI);
Evan Cheng185c9ef2009-06-13 09:12:55 +00002147 LdBases.push_back(Base);
2148 }
2149 } else {
2150 DenseMap<unsigned, SmallVector<MachineInstr*, 4> >::iterator BI =
2151 Base2StsMap.find(Base);
2152 if (BI != Base2StsMap.end()) {
2153 for (unsigned i = 0, e = BI->second.size(); i != e; ++i) {
2154 if (Offset == getMemoryOpOffset(BI->second[i])) {
2155 StopHere = true;
2156 break;
2157 }
2158 }
2159 if (!StopHere)
2160 BI->second.push_back(MI);
2161 } else {
Craig Topper9ae47072013-07-10 16:38:35 +00002162 Base2StsMap[Base].push_back(MI);
Evan Cheng185c9ef2009-06-13 09:12:55 +00002163 StBases.push_back(Base);
2164 }
2165 }
2166
2167 if (StopHere) {
Evan Chengb4b20bb2009-06-19 23:17:27 +00002168 // Found a duplicate (a base+offset combination that's seen earlier).
2169 // Backtrack.
Evan Cheng185c9ef2009-06-13 09:12:55 +00002170 --Loc;
2171 break;
2172 }
2173 }
2174
2175 // Re-schedule loads.
2176 for (unsigned i = 0, e = LdBases.size(); i != e; ++i) {
2177 unsigned Base = LdBases[i];
Craig Topperaf0dea12013-07-04 01:31:24 +00002178 SmallVectorImpl<MachineInstr *> &Lds = Base2LdsMap[Base];
Evan Cheng185c9ef2009-06-13 09:12:55 +00002179 if (Lds.size() > 1)
2180 RetVal |= RescheduleOps(MBB, Lds, Base, true, MI2LocMap);
2181 }
2182
2183 // Re-schedule stores.
2184 for (unsigned i = 0, e = StBases.size(); i != e; ++i) {
2185 unsigned Base = StBases[i];
Craig Topperaf0dea12013-07-04 01:31:24 +00002186 SmallVectorImpl<MachineInstr *> &Sts = Base2StsMap[Base];
Evan Cheng185c9ef2009-06-13 09:12:55 +00002187 if (Sts.size() > 1)
2188 RetVal |= RescheduleOps(MBB, Sts, Base, false, MI2LocMap);
2189 }
2190
2191 if (MBBI != E) {
2192 Base2LdsMap.clear();
2193 Base2StsMap.clear();
2194 LdBases.clear();
2195 StBases.clear();
2196 }
2197 }
2198
2199 return RetVal;
2200}
2201
2202
Matthias Braunec50fa62015-06-01 21:26:23 +00002203/// Returns an instance of the load / store optimization pass.
Evan Cheng185c9ef2009-06-13 09:12:55 +00002204FunctionPass *llvm::createARMLoadStoreOptimizationPass(bool PreAlloc) {
2205 if (PreAlloc)
2206 return new ARMPreAllocLoadStoreOpt();
2207 return new ARMLoadStoreOpt();
2208}