blob: cd215b40d151aed09aff6762963909a9cd088933 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwinaf7451b2009-07-08 16:09:28 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Base ARM implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
David Goodwinaf7451b2009-07-08 16:09:28 +000014#include "ARM.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000015#include "ARMBaseInstrInfo.h"
Craig Topper5fa0caa2012-03-26 00:45:15 +000016#include "ARMBaseRegisterInfo.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000017#include "ARMConstantPoolValue.h"
Amara Emerson52cfb6a2013-10-03 09:31:51 +000018#include "ARMFeatures.h"
Evan Cheng62c7b5b2010-12-05 22:04:16 +000019#include "ARMHazardRecognizer.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000020#include "ARMMachineFunctionInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000021#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/STLExtras.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000023#include "llvm/CodeGen/LiveVariables.h"
Evan Chenga8e8a7c2009-11-07 04:04:34 +000024#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng168ced92010-05-22 01:47:14 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chenga20cde32011-07-20 23:34:39 +000030#include "llvm/CodeGen/SelectionDAGNodes.h"
Matthias Braun88e21312015-06-13 03:42:11 +000031#include "llvm/CodeGen/TargetSchedule.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000035#include "llvm/MC/MCAsmInfo.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000036#include "llvm/MC/MCExpr.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000037#include "llvm/Support/BranchProbability.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000038#include "llvm/Support/CommandLine.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000039#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000040#include "llvm/Support/ErrorHandling.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000041#include "llvm/Support/raw_ostream.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000042
David Goodwinaf7451b2009-07-08 16:09:28 +000043using namespace llvm;
44
Chandler Carruthe96dd892014-04-21 22:55:11 +000045#define DEBUG_TYPE "arm-instrinfo"
46
Chandler Carruthd174b722014-04-22 02:03:14 +000047#define GET_INSTRINFO_CTOR_DTOR
48#include "ARMGenInstrInfo.inc"
49
David Goodwinaf7451b2009-07-08 16:09:28 +000050static cl::opt<bool>
51EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
52 cl::desc("Enable ARM 2-addr to 3-addr conv"));
53
Evan Cheng62c7b5b2010-12-05 22:04:16 +000054/// ARM_MLxEntry - Record information about MLA / MLS instructions.
55struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000056 uint16_t MLxOpc; // MLA / MLS opcode
57 uint16_t MulOpc; // Expanded multiplication opcode
58 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000059 bool NegAcc; // True if the acc is negated before the add / sub.
60 bool HasLane; // True if instruction has an extra "lane" operand.
61};
62
63static const ARM_MLxEntry ARM_MLxTable[] = {
64 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
65 // fp scalar ops
66 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
67 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
68 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
69 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000070 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
71 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
72 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
73 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
74
75 // fp SIMD ops
76 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
77 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
78 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
79 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
80 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
81 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
82 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
83 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
84};
85
Anton Korobeynikov14635da2009-11-02 00:10:38 +000086ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000087 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000088 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000089 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
90 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
Benjamin Kramer8ceb3232015-10-25 22:28:27 +000091 llvm_unreachable("Duplicated entries?");
Evan Cheng62c7b5b2010-12-05 22:04:16 +000092 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
93 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
94 }
95}
96
Andrew Trick10ffc2b2010-12-24 05:03:26 +000097// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
98// currently defaults to no prepass hazard recognizer.
Eric Christopherf047bfd2014-06-13 22:38:52 +000099ScheduleHazardRecognizer *
100ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
101 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000102 if (usePreRAHazardRecognizer()) {
Eric Christopherf047bfd2014-06-13 22:38:52 +0000103 const InstrItineraryData *II =
Eric Christopherd9134482014-08-04 21:25:23 +0000104 static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000105 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
106 }
Eric Christopherf047bfd2014-06-13 22:38:52 +0000107 return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000108}
109
110ScheduleHazardRecognizer *ARMBaseInstrInfo::
111CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
112 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000113 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000114 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000115 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000116}
117
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000118MachineInstr *ARMBaseInstrInfo::convertToThreeAddress(
119 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000120 // FIXME: Thumb2 support.
121
David Goodwinaf7451b2009-07-08 16:09:28 +0000122 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000123 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000124
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000125 MachineFunction &MF = *MI.getParent()->getParent();
126 uint64_t TSFlags = MI.getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000127 bool isPre = false;
128 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000129 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000130 case ARMII::IndexModePre:
131 isPre = true;
132 break;
133 case ARMII::IndexModePost:
134 break;
135 }
136
137 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
138 // operation.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000139 unsigned MemOpc = getUnindexedOpcode(MI.getOpcode());
David Goodwinaf7451b2009-07-08 16:09:28 +0000140 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000141 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000142
Craig Topper062a2ba2014-04-25 05:30:21 +0000143 MachineInstr *UpdateMI = nullptr;
144 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000145 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000146 const MCInstrDesc &MCID = MI.getDesc();
Evan Cheng6cc775f2011-06-28 19:10:37 +0000147 unsigned NumOps = MCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000148 bool isLoad = !MI.mayStore();
149 const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0);
150 const MachineOperand &Base = MI.getOperand(2);
151 const MachineOperand &Offset = MI.getOperand(NumOps - 3);
David Goodwinaf7451b2009-07-08 16:09:28 +0000152 unsigned WBReg = WB.getReg();
153 unsigned BaseReg = Base.getReg();
154 unsigned OffReg = Offset.getReg();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000155 unsigned OffImm = MI.getOperand(NumOps - 2).getImm();
156 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm();
David Goodwinaf7451b2009-07-08 16:09:28 +0000157 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000158 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000159 case ARMII::AddrMode2: {
160 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
161 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
162 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000163 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000164 // Can't encode it in a so_imm operand. This transformation will
165 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000166 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000167 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000168 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000169 .addReg(BaseReg)
170 .addImm(Amt)
171 .addImm(Pred)
172 .addReg(0)
173 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000174 } else if (Amt != 0) {
175 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
176 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000177 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000178 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000179 .addReg(BaseReg)
180 .addReg(OffReg)
181 .addReg(0)
182 .addImm(SOOpc)
183 .addImm(Pred)
184 .addReg(0)
185 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000186 } else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000187 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000188 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000189 .addReg(BaseReg)
190 .addReg(OffReg)
191 .addImm(Pred)
192 .addReg(0)
193 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000194 break;
195 }
196 case ARMII::AddrMode3 : {
197 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
198 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
199 if (OffReg == 0)
200 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000201 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000202 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000203 .addReg(BaseReg)
204 .addImm(Amt)
205 .addImm(Pred)
206 .addReg(0)
207 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000208 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000209 UpdateMI = BuildMI(MF, MI.getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000210 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000211 .addReg(BaseReg)
212 .addReg(OffReg)
213 .addImm(Pred)
214 .addReg(0)
215 .addReg(0);
David Goodwinaf7451b2009-07-08 16:09:28 +0000216 break;
217 }
218 }
219
220 std::vector<MachineInstr*> NewMIs;
221 if (isPre) {
222 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000223 MemMI =
224 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
225 .addReg(WBReg)
226 .addImm(0)
227 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000228 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000229 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
230 .addReg(MI.getOperand(1).getReg())
231 .addReg(WBReg)
232 .addReg(0)
233 .addImm(0)
234 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000235 NewMIs.push_back(MemMI);
236 NewMIs.push_back(UpdateMI);
237 } else {
238 if (isLoad)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000239 MemMI =
240 BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
241 .addReg(BaseReg)
242 .addImm(0)
243 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000244 else
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000245 MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
246 .addReg(MI.getOperand(1).getReg())
247 .addReg(BaseReg)
248 .addReg(0)
249 .addImm(0)
250 .addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000251 if (WB.isDead())
252 UpdateMI->getOperand(0).setIsDead();
253 NewMIs.push_back(UpdateMI);
254 NewMIs.push_back(MemMI);
255 }
256
257 // Transfer LiveVariables states, kill / dead info.
258 if (LV) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000259 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
260 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000261 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000262 unsigned Reg = MO.getReg();
263
264 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
265 if (MO.isDef()) {
266 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
267 if (MO.isDead())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000268 LV->addVirtualRegisterDead(Reg, *NewMI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000269 }
270 if (MO.isUse() && MO.isKill()) {
271 for (unsigned j = 0; j < 2; ++j) {
272 // Look at the two new MI's in reverse order.
273 MachineInstr *NewMI = NewMIs[j];
274 if (!NewMI->readsRegister(Reg))
275 continue;
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000276 LV->addVirtualRegisterKilled(Reg, *NewMI);
277 if (VI.removeKill(MI))
David Goodwinaf7451b2009-07-08 16:09:28 +0000278 VI.Kills.push_back(NewMI);
279 break;
280 }
281 }
282 }
283 }
284 }
285
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000286 MachineBasicBlock::iterator MBBI = MI.getIterator();
David Goodwinaf7451b2009-07-08 16:09:28 +0000287 MFI->insert(MBBI, NewMIs[1]);
288 MFI->insert(MBBI, NewMIs[0]);
289 return NewMIs[0];
290}
291
292// Branch analysis.
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000293bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
294 MachineBasicBlock *&TBB,
295 MachineBasicBlock *&FBB,
296 SmallVectorImpl<MachineOperand> &Cond,
297 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000298 TBB = nullptr;
299 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000300
David Goodwinaf7451b2009-07-08 16:09:28 +0000301 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000302 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000303 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000304 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000305
306 // Walk backwards from the end of the basic block until the branch is
307 // analyzed or we give up.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000308 while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000309
310 // Flag to be raised on unanalyzeable instructions. This is useful in cases
311 // where we want to clean up on the end of the basic block before we bail
312 // out.
313 bool CantAnalyze = false;
314
315 // Skip over DEBUG values and predicated nonterminators.
316 while (I->isDebugValue() || !I->isTerminator()) {
317 if (I == MBB.begin())
318 return false;
319 --I;
320 }
321
322 if (isIndirectBranchOpcode(I->getOpcode()) ||
323 isJumpTableBranchOpcode(I->getOpcode())) {
324 // Indirect branches and jump tables can't be analyzed, but we still want
325 // to clean up any instructions at the tail of the basic block.
326 CantAnalyze = true;
327 } else if (isUncondBranchOpcode(I->getOpcode())) {
328 TBB = I->getOperand(0).getMBB();
329 } else if (isCondBranchOpcode(I->getOpcode())) {
330 // Bail out if we encounter multiple conditional branches.
331 if (!Cond.empty())
332 return true;
333
334 assert(!FBB && "FBB should have been null.");
335 FBB = TBB;
336 TBB = I->getOperand(0).getMBB();
337 Cond.push_back(I->getOperand(1));
338 Cond.push_back(I->getOperand(2));
339 } else if (I->isReturn()) {
340 // Returns can't be analyzed, but we should run cleanup.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000341 CantAnalyze = !isPredicated(*I);
Lang Hames24864fe2013-07-19 23:52:47 +0000342 } else {
343 // We encountered other unrecognized terminator. Bail out immediately.
344 return true;
345 }
346
347 // Cleanup code - to be run for unpredicated unconditional branches and
348 // returns.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000349 if (!isPredicated(*I) &&
Lang Hames24864fe2013-07-19 23:52:47 +0000350 (isUncondBranchOpcode(I->getOpcode()) ||
351 isIndirectBranchOpcode(I->getOpcode()) ||
352 isJumpTableBranchOpcode(I->getOpcode()) ||
353 I->isReturn())) {
354 // Forget any previous condition branch information - it no longer applies.
355 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000356 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000357
358 // If we can modify the function, delete everything below this
359 // unconditional branch.
360 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000361 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000362 while (DI != MBB.end()) {
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000363 MachineInstr &InstToDelete = *DI;
Lang Hames24864fe2013-07-19 23:52:47 +0000364 ++DI;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +0000365 InstToDelete.eraseFromParent();
Lang Hames24864fe2013-07-19 23:52:47 +0000366 }
367 }
368 }
369
370 if (CantAnalyze)
371 return true;
372
Dale Johannesen4244d122010-04-02 01:38:09 +0000373 if (I == MBB.begin())
374 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000375
Dale Johannesen4244d122010-04-02 01:38:09 +0000376 --I;
377 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000378
Lang Hames24864fe2013-07-19 23:52:47 +0000379 // We made it past the terminators without bailing out - we must have
380 // analyzed this branch successfully.
381 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000382}
383
384
385unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Benjamin Kramere61cbd12015-06-25 13:28:24 +0000386 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
387 if (I == MBB.end())
388 return 0;
389
Evan Cheng056c6692009-07-27 18:20:05 +0000390 if (!isUncondBranchOpcode(I->getOpcode()) &&
391 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000392 return 0;
393
394 // Remove the branch.
395 I->eraseFromParent();
396
397 I = MBB.end();
398
399 if (I == MBB.begin()) return 1;
400 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000401 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000402 return 1;
403
404 // Remove the branch.
405 I->eraseFromParent();
406 return 2;
407}
408
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000409unsigned ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB,
410 MachineBasicBlock *TBB,
411 MachineBasicBlock *FBB,
412 ArrayRef<MachineOperand> Cond,
413 const DebugLoc &DL) const {
Evan Cheng780748d2009-07-28 05:48:47 +0000414 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
415 int BOpc = !AFI->isThumbFunction()
416 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
417 int BccOpc = !AFI->isThumbFunction()
418 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000419 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000420
David Goodwinaf7451b2009-07-08 16:09:28 +0000421 // Shouldn't be a fall through.
422 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
423 assert((Cond.size() == 2 || Cond.size() == 0) &&
424 "ARM branch conditions have two components!");
425
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000426 // For conditional branches, we use addOperand to preserve CPSR flags.
427
Craig Topper062a2ba2014-04-25 05:30:21 +0000428 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000429 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000430 if (isThumb)
431 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
432 else
433 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000434 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000435 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000436 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
David Goodwinaf7451b2009-07-08 16:09:28 +0000437 return 1;
438 }
439
440 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000441 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
Peter Collingbournecfee5b02015-04-23 20:31:32 +0000442 .addImm(Cond[0].getImm()).addOperand(Cond[1]);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000443 if (isThumb)
444 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
445 else
446 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000447 return 2;
448}
449
450bool ARMBaseInstrInfo::
451ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
452 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
453 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
454 return false;
455}
456
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000457bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
458 if (MI.isBundle()) {
459 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
460 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000461 while (++I != E && I->isInsideBundle()) {
462 int PIdx = I->findFirstPredOperandIdx();
463 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
464 return true;
465 }
466 return false;
467 }
468
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000469 int PIdx = MI.findFirstPredOperandIdx();
470 return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
Evan Cheng7fae11b2011-12-14 02:11:42 +0000471}
472
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000473bool ARMBaseInstrInfo::PredicateInstruction(
474 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
475 unsigned Opc = MI.getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000476 if (isUncondBranchOpcode(Opc)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000477 MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
478 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000479 .addImm(Pred[0].getImm())
480 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000481 return true;
482 }
483
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000484 int PIdx = MI.findFirstPredOperandIdx();
David Goodwinaf7451b2009-07-08 16:09:28 +0000485 if (PIdx != -1) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000486 MachineOperand &PMO = MI.getOperand(PIdx);
David Goodwinaf7451b2009-07-08 16:09:28 +0000487 PMO.setImm(Pred[0].getImm());
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000488 MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000489 return true;
490 }
491 return false;
492}
493
Ahmed Bougachac88bf542015-06-11 19:30:37 +0000494bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
495 ArrayRef<MachineOperand> Pred2) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000496 if (Pred1.size() > 2 || Pred2.size() > 2)
497 return false;
498
499 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
500 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
501 if (CC1 == CC2)
502 return true;
503
504 switch (CC1) {
505 default:
506 return false;
507 case ARMCC::AL:
508 return true;
509 case ARMCC::HS:
510 return CC2 == ARMCC::HI;
511 case ARMCC::LS:
512 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
513 case ARMCC::GE:
514 return CC2 == ARMCC::GT;
515 case ARMCC::LE:
516 return CC2 == ARMCC::LT;
517 }
518}
519
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000520bool ARMBaseInstrInfo::DefinesPredicate(
521 MachineInstr &MI, std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000522 bool Found = false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000523 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
524 const MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000525 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
526 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000527 Pred.push_back(MO);
528 Found = true;
529 }
530 }
531
532 return Found;
533}
534
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000535static bool isCPSRDefined(const MachineInstr *MI) {
536 for (const auto &MO : MI->operands())
James Molloy6967e5e2015-08-03 09:24:48 +0000537 if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000538 return true;
539 return false;
540}
541
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000542static bool isEligibleForITBlock(const MachineInstr *MI) {
543 switch (MI->getOpcode()) {
544 default: return true;
545 case ARM::tADC: // ADC (register) T1
546 case ARM::tADDi3: // ADD (immediate) T1
547 case ARM::tADDi8: // ADD (immediate) T2
548 case ARM::tADDrr: // ADD (register) T1
549 case ARM::tAND: // AND (register) T1
550 case ARM::tASRri: // ASR (immediate) T1
551 case ARM::tASRrr: // ASR (register) T1
552 case ARM::tBIC: // BIC (register) T1
553 case ARM::tEOR: // EOR (register) T1
554 case ARM::tLSLri: // LSL (immediate) T1
555 case ARM::tLSLrr: // LSL (register) T1
556 case ARM::tLSRri: // LSR (immediate) T1
557 case ARM::tLSRrr: // LSR (register) T1
558 case ARM::tMUL: // MUL T1
559 case ARM::tMVN: // MVN (register) T1
560 case ARM::tORR: // ORR (register) T1
561 case ARM::tROR: // ROR (register) T1
562 case ARM::tRSB: // RSB (immediate) T1
563 case ARM::tSBC: // SBC (register) T1
564 case ARM::tSUBi3: // SUB (immediate) T1
565 case ARM::tSUBi8: // SUB (immediate) T2
566 case ARM::tSUBrr: // SUB (register) T1
567 return !isCPSRDefined(MI);
568 }
569}
570
Evan Chenga33fc862009-11-21 06:21:52 +0000571/// isPredicable - Return true if the specified instruction can be predicated.
572/// By default, this returns true for every instruction with a
573/// PredicateOperand.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000574bool ARMBaseInstrInfo::isPredicable(MachineInstr &MI) const {
575 if (!MI.isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000576 return false;
577
Saleem Abdulrasoolbfa25bd2016-09-06 04:00:12 +0000578 if (MI.isBundle())
579 return false;
580
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000581 if (!isEligibleForITBlock(&MI))
Saleem Abdulrasool27c78bf2014-08-11 20:13:25 +0000582 return false;
Saleem Abdulrasooled8885b2014-08-10 22:20:37 +0000583
Joey Goulya5153cb2013-09-09 14:21:49 +0000584 ARMFunctionInfo *AFI =
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000585 MI.getParent()->getParent()->getInfo<ARMFunctionInfo>();
Joey Goulya5153cb2013-09-09 14:21:49 +0000586
587 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000588 if (getSubtarget().restrictIT())
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000589 return isV8EligibleForIT(&MI);
Joey Goulya5153cb2013-09-09 14:21:49 +0000590 } else { // non-Thumb
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000591 if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
Joey Goulya5153cb2013-09-09 14:21:49 +0000592 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000593 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000594
Evan Chenga33fc862009-11-21 06:21:52 +0000595 return true;
596}
David Goodwinaf7451b2009-07-08 16:09:28 +0000597
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000598namespace llvm {
599template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000600 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
601 const MachineOperand &MO = MI->getOperand(i);
602 if (!MO.isReg() || MO.isUndef() || MO.isUse())
603 continue;
604 if (MO.getReg() != ARM::CPSR)
605 continue;
606 if (!MO.isDead())
607 return false;
608 }
609 // all definitions of CPSR are dead
610 return true;
611}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000612}
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000613
David Goodwinaf7451b2009-07-08 16:09:28 +0000614/// GetInstSize - Return the size of the specified MachineInstr.
615///
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000616unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000617 const MachineBasicBlock &MBB = *MI.getParent();
David Goodwinaf7451b2009-07-08 16:09:28 +0000618 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000619 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000620
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000621 const MCInstrDesc &MCID = MI.getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000622 if (MCID.getSize())
623 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000624
David Blaikie46a9f012012-01-20 21:51:11 +0000625 // If this machine instr is an inline asm, measure it.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000626 if (MI.getOpcode() == ARM::INLINEASM)
627 return getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
628 unsigned Opc = MI.getOpcode();
David Blaikie46a9f012012-01-20 21:51:11 +0000629 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000630 default:
631 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000632 return 0;
633 case TargetOpcode::BUNDLE:
634 return getInstBundleLength(MI);
635 case ARM::MOVi16_ga_pcrel:
636 case ARM::MOVTi16_ga_pcrel:
637 case ARM::t2MOVi16_ga_pcrel:
638 case ARM::t2MOVTi16_ga_pcrel:
639 return 4;
640 case ARM::MOVi32imm:
641 case ARM::t2MOVi32imm:
642 return 8;
643 case ARM::CONSTPOOL_ENTRY:
Tim Northovera603c402015-05-31 19:22:07 +0000644 case ARM::JUMPTABLE_INSTS:
645 case ARM::JUMPTABLE_ADDRS:
646 case ARM::JUMPTABLE_TBB:
647 case ARM::JUMPTABLE_TBH:
David Blaikie46a9f012012-01-20 21:51:11 +0000648 // If this machine instr is a constant pool entry, its size is recorded as
649 // operand #2.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000650 return MI.getOperand(2).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000651 case ARM::Int_eh_sjlj_longjmp:
652 return 16;
653 case ARM::tInt_eh_sjlj_longjmp:
654 return 10;
Saleem Abdulrasooleb059b02016-07-08 00:48:22 +0000655 case ARM::tInt_WIN_eh_sjlj_longjmp:
656 return 12;
David Blaikie46a9f012012-01-20 21:51:11 +0000657 case ARM::Int_eh_sjlj_setjmp:
658 case ARM::Int_eh_sjlj_setjmp_nofp:
659 return 20;
660 case ARM::tInt_eh_sjlj_setjmp:
661 case ARM::t2Int_eh_sjlj_setjmp:
662 case ARM::t2Int_eh_sjlj_setjmp_nofp:
663 return 12;
Tim Northover650b0ee52014-11-13 17:58:48 +0000664 case ARM::SPACE:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000665 return MI.getOperand(1).getImm();
David Blaikie46a9f012012-01-20 21:51:11 +0000666 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000667}
668
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000669unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
Evan Cheng7fae11b2011-12-14 02:11:42 +0000670 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000671 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
672 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +0000673 while (++I != E && I->isInsideBundle()) {
674 assert(!I->isBundle() && "No nested bundle!");
Sjoerd Meijer89217f82016-07-28 16:32:22 +0000675 Size += getInstSizeInBytes(*I);
Evan Cheng7fae11b2011-12-14 02:11:42 +0000676 }
677 return Size;
678}
679
Tim Northover5d72c5d2014-10-01 19:21:03 +0000680void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
681 MachineBasicBlock::iterator I,
682 unsigned DestReg, bool KillSrc,
683 const ARMSubtarget &Subtarget) const {
684 unsigned Opc = Subtarget.isThumb()
685 ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
686 : ARM::MRS;
687
688 MachineInstrBuilder MIB =
689 BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
690
691 // There is only 1 A/R class MRS instruction, and it always refers to
692 // APSR. However, there are lots of other possibilities on M-class cores.
693 if (Subtarget.isMClass())
694 MIB.addImm(0x800);
695
696 AddDefaultPred(MIB);
697
698 MIB.addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
699}
700
701void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
702 MachineBasicBlock::iterator I,
703 unsigned SrcReg, bool KillSrc,
704 const ARMSubtarget &Subtarget) const {
705 unsigned Opc = Subtarget.isThumb()
706 ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
707 : ARM::MSR;
708
709 MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
710
711 if (Subtarget.isMClass())
712 MIB.addImm(0x800);
713 else
714 MIB.addImm(8);
715
716 MIB.addReg(SrcReg, getKillRegState(KillSrc));
717
718 AddDefaultPred(MIB);
719
720 MIB.addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
721}
722
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000723void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000724 MachineBasicBlock::iterator I,
725 const DebugLoc &DL, unsigned DestReg,
726 unsigned SrcReg, bool KillSrc) const {
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000727 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000728 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000729
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000730 if (GPRDest && GPRSrc) {
731 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000732 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000733 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000734 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000735
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000736 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000737 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000738
Chad Rosierbe762512011-08-20 00:17:25 +0000739 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000740 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000741 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000742 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000743 Opc = ARM::VMOVRS;
744 else if (SPRDest && GPRSrc)
745 Opc = ARM::VMOVSR;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000746 else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && !Subtarget.isFPOnlySP())
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000747 Opc = ARM::VMOVD;
748 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000749 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000750
Chad Rosierbe762512011-08-20 00:17:25 +0000751 if (Opc) {
752 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000753 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000754 if (Opc == ARM::VORRq)
755 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000756 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000757 return;
758 }
759
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000760 // Handle register classes that require multiple instructions.
761 unsigned BeginIdx = 0;
762 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000763 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000764
765 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000766 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
767 Opc = ARM::VORRq;
768 BeginIdx = ARM::qsub_0;
769 SubRegs = 2;
770 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
771 Opc = ARM::VORRq;
772 BeginIdx = ARM::qsub_0;
773 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000774 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000775 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
776 Opc = ARM::VMOVD;
777 BeginIdx = ARM::dsub_0;
778 SubRegs = 2;
779 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
780 Opc = ARM::VMOVD;
781 BeginIdx = ARM::dsub_0;
782 SubRegs = 3;
783 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
784 Opc = ARM::VMOVD;
785 BeginIdx = ARM::dsub_0;
786 SubRegs = 4;
787 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000788 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000789 BeginIdx = ARM::gsub_0;
790 SubRegs = 2;
791 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
792 Opc = ARM::VMOVD;
793 BeginIdx = ARM::dsub_0;
794 SubRegs = 2;
795 Spacing = 2;
796 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
797 Opc = ARM::VMOVD;
798 BeginIdx = ARM::dsub_0;
799 SubRegs = 3;
800 Spacing = 2;
801 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
802 Opc = ARM::VMOVD;
803 BeginIdx = ARM::dsub_0;
804 SubRegs = 4;
805 Spacing = 2;
Oliver Stannard51b1d462014-08-21 12:50:31 +0000806 } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.isFPOnlySP()) {
807 Opc = ARM::VMOVS;
808 BeginIdx = ARM::ssub_0;
809 SubRegs = 2;
Tim Northover5d72c5d2014-10-01 19:21:03 +0000810 } else if (SrcReg == ARM::CPSR) {
811 copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
812 return;
813 } else if (DestReg == ARM::CPSR) {
814 copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
815 return;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000816 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000817
Andrew Trickb57e2252012-08-29 04:41:37 +0000818 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000819
Andrew Trick4cc69492012-08-29 01:58:52 +0000820 const TargetRegisterInfo *TRI = &getRegisterInfo();
821 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000822
823 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
824 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000825 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000826 Spacing = -Spacing;
827 }
828#ifndef NDEBUG
829 SmallSet<unsigned, 4> DstRegs;
830#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000831 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000832 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
833 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000834 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000835#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000836 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000837 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000838#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000839 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000840 // VORR takes two source operands.
841 if (Opc == ARM::VORRq)
842 Mov.addReg(Src);
843 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000844 // MOVr can set CC.
845 if (Opc == ARM::MOVr)
846 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000847 }
848 // Add implicit super-register defs and kills to the last instruction.
849 Mov->addRegisterDefined(DestReg, TRI);
850 if (KillSrc)
851 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000852}
853
Tim Northover798697d2013-04-21 11:57:07 +0000854const MachineInstrBuilder &
855ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
856 unsigned SubIdx, unsigned State,
857 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000858 if (!SubIdx)
859 return MIB.addReg(Reg, State);
860
861 if (TargetRegisterInfo::isPhysicalRegister(Reg))
862 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
863 return MIB.addReg(Reg, State, SubIdx);
864}
865
David Goodwinaf7451b2009-07-08 16:09:28 +0000866void ARMBaseInstrInfo::
867storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
868 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000869 const TargetRegisterClass *RC,
870 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000871 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000872 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000873 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000874 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000875 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000876
Alex Lorenze40c8a22015-08-11 23:09:45 +0000877 MachineMemOperand *MMO = MF.getMachineMemOperand(
878 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
879 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000880
Owen Anderson732f82c2011-08-10 17:21:20 +0000881 switch (RC->getSize()) {
882 case 4:
883 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
884 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000885 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000886 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000887 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
888 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000889 .addReg(SrcReg, getKillRegState(isKill))
890 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000891 } else
892 llvm_unreachable("Unknown reg class!");
893 break;
894 case 8:
895 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
896 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000897 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000898 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000899 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000900 if (Subtarget.hasV5TEOps()) {
901 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
902 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
903 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
904 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
905
906 AddDefaultPred(MIB);
907 } else {
908 // Fallback to STM instruction, which has existed since the dawn of
909 // time.
910 MachineInstrBuilder MIB =
911 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
912 .addFrameIndex(FI).addMemOperand(MMO));
913 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
914 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
915 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000916 } else
917 llvm_unreachable("Unknown reg class!");
918 break;
919 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000920 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000921 // Use aligned spills if the stack can be realigned.
922 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000923 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000924 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000925 .addReg(SrcReg, getKillRegState(isKill))
926 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000927 } else {
928 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000929 .addReg(SrcReg, getKillRegState(isKill))
930 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000931 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000932 }
933 } else
934 llvm_unreachable("Unknown reg class!");
935 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000936 case 24:
937 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
938 // Use aligned spills if the stack can be realigned.
939 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
940 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
941 .addFrameIndex(FI).addImm(16)
942 .addReg(SrcReg, getKillRegState(isKill))
943 .addMemOperand(MMO));
944 } else {
945 MachineInstrBuilder MIB =
946 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
947 .addFrameIndex(FI))
948 .addMemOperand(MMO);
949 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
950 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
951 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
952 }
953 } else
954 llvm_unreachable("Unknown reg class!");
955 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000956 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000957 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000958 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
959 // FIXME: It's possible to only store part of the QQ register if the
960 // spilled def has a sub-register index.
961 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000962 .addFrameIndex(FI).addImm(16)
963 .addReg(SrcReg, getKillRegState(isKill))
964 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000965 } else {
966 MachineInstrBuilder MIB =
967 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000968 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000969 .addMemOperand(MMO);
970 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
971 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
972 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
973 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
974 }
975 } else
976 llvm_unreachable("Unknown reg class!");
977 break;
978 case 64:
979 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
980 MachineInstrBuilder MIB =
981 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
982 .addFrameIndex(FI))
983 .addMemOperand(MMO);
984 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
985 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
986 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
987 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
988 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
989 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
990 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
991 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
992 } else
993 llvm_unreachable("Unknown reg class!");
994 break;
995 default:
996 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000997 }
998}
999
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001000unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
1001 int &FrameIndex) const {
1002 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001003 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001004 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001005 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001006 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1007 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1008 MI.getOperand(3).getImm() == 0) {
1009 FrameIndex = MI.getOperand(1).getIndex();
1010 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001011 }
1012 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +00001013 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001014 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001015 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001016 case ARM::VSTRD:
1017 case ARM::VSTRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001018 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1019 MI.getOperand(2).getImm() == 0) {
1020 FrameIndex = MI.getOperand(1).getIndex();
1021 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001022 }
1023 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001024 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001025 case ARM::VST1d64TPseudo:
1026 case ARM::VST1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001027 if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
1028 FrameIndex = MI.getOperand(0).getIndex();
1029 return MI.getOperand(2).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001030 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +00001031 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001032 case ARM::VSTMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001033 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1034 FrameIndex = MI.getOperand(1).getIndex();
1035 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001036 }
1037 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001038 }
1039
1040 return 0;
1041}
1042
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001043unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001044 int &FrameIndex) const {
1045 const MachineMemOperand *Dummy;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001046 return MI.mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001047}
1048
David Goodwinaf7451b2009-07-08 16:09:28 +00001049void ARMBaseInstrInfo::
1050loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
1051 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +00001052 const TargetRegisterClass *RC,
1053 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +00001054 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +00001055 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00001056 MachineFunction &MF = *MBB.getParent();
Matthias Braun941a7052016-07-28 18:40:00 +00001057 MachineFrameInfo &MFI = MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001058 unsigned Align = MFI.getObjectAlignment(FI);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001059 MachineMemOperand *MMO = MF.getMachineMemOperand(
1060 MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
1061 MFI.getObjectSize(FI), Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001062
Owen Anderson732f82c2011-08-10 17:21:20 +00001063 switch (RC->getSize()) {
1064 case 4:
1065 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1066 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1067 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001068
Owen Anderson732f82c2011-08-10 17:21:20 +00001069 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1070 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001071 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001072 } else
1073 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001074 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001075 case 8:
1076 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1077 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001078 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001079 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001080 MachineInstrBuilder MIB;
1081
1082 if (Subtarget.hasV5TEOps()) {
1083 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1084 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1085 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1086 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1087
1088 AddDefaultPred(MIB);
1089 } else {
1090 // Fallback to LDM instruction, which has existed since the dawn of
1091 // time.
1092 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1093 .addFrameIndex(FI).addMemOperand(MMO));
1094 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1095 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1096 }
1097
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001098 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1099 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001100 } else
1101 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001102 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001103 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001104 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001105 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001106 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001107 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001108 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001109 } else {
1110 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1111 .addFrameIndex(FI)
1112 .addMemOperand(MMO));
1113 }
1114 } else
1115 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001116 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001117 case 24:
1118 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1119 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1120 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1121 .addFrameIndex(FI).addImm(16)
1122 .addMemOperand(MMO));
1123 } else {
1124 MachineInstrBuilder MIB =
1125 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1126 .addFrameIndex(FI)
1127 .addMemOperand(MMO));
1128 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1129 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1130 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1131 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1132 MIB.addReg(DestReg, RegState::ImplicitDefine);
1133 }
1134 } else
1135 llvm_unreachable("Unknown reg class!");
1136 break;
1137 case 32:
1138 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001139 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1140 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001141 .addFrameIndex(FI).addImm(16)
1142 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001143 } else {
1144 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001145 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1146 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001147 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001148 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1149 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1150 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1151 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001152 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1153 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001154 }
1155 } else
1156 llvm_unreachable("Unknown reg class!");
1157 break;
1158 case 64:
1159 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1160 MachineInstrBuilder MIB =
1161 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1162 .addFrameIndex(FI))
1163 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001164 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1165 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1166 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1167 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1168 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1169 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1170 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1171 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001172 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1173 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001174 } else
1175 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001176 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001177 default:
1178 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001179 }
1180}
1181
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001182unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
1183 int &FrameIndex) const {
1184 switch (MI.getOpcode()) {
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001185 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001186 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001187 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001188 if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
1189 MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
1190 MI.getOperand(3).getImm() == 0) {
1191 FrameIndex = MI.getOperand(1).getIndex();
1192 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001193 }
1194 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001195 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001196 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001197 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001198 case ARM::VLDRD:
1199 case ARM::VLDRS:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001200 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
1201 MI.getOperand(2).getImm() == 0) {
1202 FrameIndex = MI.getOperand(1).getIndex();
1203 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001204 }
1205 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001206 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001207 case ARM::VLD1d64TPseudo:
1208 case ARM::VLD1d64QPseudo:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001209 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1210 FrameIndex = MI.getOperand(1).getIndex();
1211 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001212 }
1213 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001214 case ARM::VLDMQIA:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001215 if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
1216 FrameIndex = MI.getOperand(1).getIndex();
1217 return MI.getOperand(0).getReg();
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001218 }
1219 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001220 }
1221
1222 return 0;
1223}
1224
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001225unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
1226 int &FrameIndex) const {
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001227 const MachineMemOperand *Dummy;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001228 return MI.mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001229}
1230
Scott Douglass953f9082015-10-05 14:49:54 +00001231/// \brief Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
1232/// depending on whether the result is used.
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001233void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
Scott Douglass953f9082015-10-05 14:49:54 +00001234 bool isThumb1 = Subtarget.isThumb1Only();
1235 bool isThumb2 = Subtarget.isThumb2();
1236 const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
1237
Scott Douglass953f9082015-10-05 14:49:54 +00001238 DebugLoc dl = MI->getDebugLoc();
1239 MachineBasicBlock *BB = MI->getParent();
1240
1241 MachineInstrBuilder LDM, STM;
1242 if (isThumb1 || !MI->getOperand(1).isDead()) {
1243 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
1244 : isThumb1 ? ARM::tLDMIA_UPD
1245 : ARM::LDMIA_UPD))
1246 .addOperand(MI->getOperand(1));
1247 } else {
1248 LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
1249 }
1250
1251 if (isThumb1 || !MI->getOperand(0).isDead()) {
1252 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
1253 : isThumb1 ? ARM::tSTMIA_UPD
1254 : ARM::STMIA_UPD))
1255 .addOperand(MI->getOperand(0));
1256 } else {
1257 STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
1258 }
1259
1260 AddDefaultPred(LDM.addOperand(MI->getOperand(3)));
1261 AddDefaultPred(STM.addOperand(MI->getOperand(2)));
1262
1263 // Sort the scratch registers into ascending order.
1264 const TargetRegisterInfo &TRI = getRegisterInfo();
1265 llvm::SmallVector<unsigned, 6> ScratchRegs;
1266 for(unsigned I = 5; I < MI->getNumOperands(); ++I)
1267 ScratchRegs.push_back(MI->getOperand(I).getReg());
1268 std::sort(ScratchRegs.begin(), ScratchRegs.end(),
1269 [&TRI](const unsigned &Reg1,
1270 const unsigned &Reg2) -> bool {
1271 return TRI.getEncodingValue(Reg1) <
1272 TRI.getEncodingValue(Reg2);
1273 });
1274
1275 for (const auto &Reg : ScratchRegs) {
1276 LDM.addReg(Reg, RegState::Define);
1277 STM.addReg(Reg, RegState::Kill);
1278 }
1279
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00001280 BB->erase(MI);
Scott Douglass953f9082015-10-05 14:49:54 +00001281}
1282
1283
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001284bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1285 if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
Daniel Sandersfbdab432015-07-06 16:33:18 +00001286 assert(getSubtarget().getTargetTriple().isOSBinFormatMachO() &&
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001287 "LOAD_STACK_GUARD currently supported only for MachO.");
Rafael Espindola82f46312016-06-28 15:18:26 +00001288 expandLoadStackGuard(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001289 MI.getParent()->erase(MI);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00001290 return true;
1291 }
1292
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001293 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00001294 expandMEMCPY(MI);
1295 return true;
1296 }
1297
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001298 // This hook gets to expand COPY instructions before they become
1299 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1300 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1301 // changed into a VORR that can go down the NEON pipeline.
Diana Picusb772e402016-07-06 11:22:11 +00001302 if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || Subtarget.isFPOnlySP())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001303 return false;
1304
1305 // Look for a copy between even S-registers. That is where we keep floats
1306 // when using NEON v2f32 instructions for f32 arithmetic.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001307 unsigned DstRegS = MI.getOperand(0).getReg();
1308 unsigned SrcRegS = MI.getOperand(1).getReg();
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001309 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1310 return false;
1311
1312 const TargetRegisterInfo *TRI = &getRegisterInfo();
1313 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1314 &ARM::DPRRegClass);
1315 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1316 &ARM::DPRRegClass);
1317 if (!DstRegD || !SrcRegD)
1318 return false;
1319
1320 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1321 // legal if the COPY already defines the full DstRegD, and it isn't a
1322 // sub-register insertion.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001323 if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001324 return false;
1325
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001326 // A dead copy shouldn't show up here, but reject it just in case.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001327 if (MI.getOperand(0).isDead())
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001328 return false;
1329
1330 // All clear, widen the COPY.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001331 DEBUG(dbgs() << "widening: " << MI);
1332 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001333
1334 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1335 // or some other super-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001336 int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001337 if (ImpDefIdx != -1)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001338 MI.RemoveOperand(ImpDefIdx);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001339
1340 // Change the opcode and operands.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001341 MI.setDesc(get(ARM::VMOVD));
1342 MI.getOperand(0).setReg(DstRegD);
1343 MI.getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001344 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001345
1346 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1347 // register scavenger and machine verifier, so we need to indicate that we
1348 // are reading an undefined value from SrcRegD, but a proper value from
1349 // SrcRegS.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001350 MI.getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001351 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001352
1353 // SrcRegD may actually contain an unrelated value in the ssub_1
1354 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001355 if (MI.getOperand(1).isKill()) {
1356 MI.getOperand(1).setIsKill(false);
1357 MI.addRegisterKilled(SrcRegS, TRI, true);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001358 }
1359
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001360 DEBUG(dbgs() << "replaced by: " << MI);
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001361 return true;
1362}
1363
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001364/// Create a copy of a const pool value. Update CPI to the new index and return
1365/// the label UID.
1366static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1367 MachineConstantPool *MCP = MF.getConstantPool();
1368 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1369
1370 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1371 assert(MCPE.isMachineConstantPoolEntry() &&
1372 "Expecting a machine constantpool entry!");
1373 ARMConstantPoolValue *ACPV =
1374 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1375
Evan Chengdfce83c2011-01-17 08:03:18 +00001376 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001377 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001378
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001379 // FIXME: The below assumes PIC relocation model and that the function
1380 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1381 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1382 // instructions, so that's probably OK, but is PIC always correct when
1383 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001384 if (ACPV->isGlobalValue())
Peter Collingbourne97aae402015-10-26 18:23:16 +00001385 NewCPV = ARMConstantPoolConstant::Create(
1386 cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
1387 4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001388 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001389 NewCPV = ARMConstantPoolSymbol::
1390 Create(MF.getFunction()->getContext(),
1391 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001392 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001393 NewCPV = ARMConstantPoolConstant::
1394 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1395 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001396 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001397 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1398 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001399 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001400 NewCPV = ARMConstantPoolMBB::
1401 Create(MF.getFunction()->getContext(),
1402 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001403 else
1404 llvm_unreachable("Unexpected ARM constantpool value type!!");
1405 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1406 return PCLabelId;
1407}
1408
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001409void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
1410 MachineBasicBlock::iterator I,
1411 unsigned DestReg, unsigned SubIdx,
1412 const MachineInstr &Orig,
1413 const TargetRegisterInfo &TRI) const {
1414 unsigned Opcode = Orig.getOpcode();
Evan Chengfe864422009-11-08 00:15:23 +00001415 switch (Opcode) {
1416 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001417 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
1418 MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001419 MBB.insert(I, MI);
1420 break;
1421 }
1422 case ARM::tLDRpci_pic:
1423 case ARM::t2LDRpci_pic: {
1424 MachineFunction &MF = *MBB.getParent();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001425 unsigned CPI = Orig.getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001426 unsigned PCLabelId = duplicateCPV(MF, CPI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001427 MachineInstrBuilder MIB =
1428 BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
1429 .addConstantPoolIndex(CPI)
1430 .addImm(PCLabelId);
1431 MIB->setMemRefs(Orig.memoperands_begin(), Orig.memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001432 break;
1433 }
1434 }
Evan Chengfe864422009-11-08 00:15:23 +00001435}
1436
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001437MachineInstr *ARMBaseInstrInfo::duplicate(MachineInstr &Orig,
1438 MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001439 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001440 switch (Orig.getOpcode()) {
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001441 case ARM::tLDRpci_pic:
1442 case ARM::t2LDRpci_pic: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001443 unsigned CPI = Orig.getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001444 unsigned PCLabelId = duplicateCPV(MF, CPI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001445 Orig.getOperand(1).setIndex(CPI);
1446 Orig.getOperand(2).setImm(PCLabelId);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001447 break;
1448 }
1449 }
1450 return MI;
1451}
1452
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001453bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
1454 const MachineInstr &MI1,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001455 const MachineRegisterInfo *MRI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001456 unsigned Opcode = MI0.getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001457 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001458 Opcode == ARM::t2LDRpci_pic ||
1459 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001460 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001461 Opcode == ARM::LDRLIT_ga_pcrel ||
1462 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1463 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001464 Opcode == ARM::MOV_ga_pcrel ||
1465 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001466 Opcode == ARM::t2MOV_ga_pcrel) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001467 if (MI1.getOpcode() != Opcode)
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001468 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001469 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001470 return false;
1471
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001472 const MachineOperand &MO0 = MI0.getOperand(1);
1473 const MachineOperand &MO1 = MI1.getOperand(1);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001474 if (MO0.getOffset() != MO1.getOffset())
1475 return false;
1476
Tim Northover72360d22013-12-02 10:35:41 +00001477 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1478 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1479 Opcode == ARM::tLDRLIT_ga_pcrel ||
1480 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001481 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001482 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001483 // Ignore the PC labels.
1484 return MO0.getGlobal() == MO1.getGlobal();
1485
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001486 const MachineFunction *MF = MI0.getParent()->getParent();
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001487 const MachineConstantPool *MCP = MF->getConstantPool();
1488 int CPI0 = MO0.getIndex();
1489 int CPI1 = MO1.getIndex();
1490 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1491 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001492 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1493 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1494 if (isARMCP0 && isARMCP1) {
1495 ARMConstantPoolValue *ACPV0 =
1496 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1497 ARMConstantPoolValue *ACPV1 =
1498 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1499 return ACPV0->hasSameValue(ACPV1);
1500 } else if (!isARMCP0 && !isARMCP1) {
1501 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1502 }
1503 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001504 } else if (Opcode == ARM::PICLDR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001505 if (MI1.getOpcode() != Opcode)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001506 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001507 if (MI0.getNumOperands() != MI1.getNumOperands())
Evan Chengb8b0ad82011-01-20 08:34:58 +00001508 return false;
1509
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001510 unsigned Addr0 = MI0.getOperand(1).getReg();
1511 unsigned Addr1 = MI1.getOperand(1).getReg();
Evan Chengb8b0ad82011-01-20 08:34:58 +00001512 if (Addr0 != Addr1) {
1513 if (!MRI ||
1514 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1515 !TargetRegisterInfo::isVirtualRegister(Addr1))
1516 return false;
1517
1518 // This assumes SSA form.
1519 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1520 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1521 // Check if the loaded value, e.g. a constantpool of a global address, are
1522 // the same.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001523 if (!produceSameValue(*Def0, *Def1, MRI))
Evan Chengb8b0ad82011-01-20 08:34:58 +00001524 return false;
1525 }
1526
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001527 for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
Evan Chengb8b0ad82011-01-20 08:34:58 +00001528 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001529 const MachineOperand &MO0 = MI0.getOperand(i);
1530 const MachineOperand &MO1 = MI1.getOperand(i);
Evan Chengb8b0ad82011-01-20 08:34:58 +00001531 if (!MO0.isIdenticalTo(MO1))
1532 return false;
1533 }
1534 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001535 }
1536
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001537 return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001538}
1539
Bill Wendlingf4707472010-06-23 23:00:16 +00001540/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1541/// determine if two loads are loading from the same base address. It should
1542/// only return true if the base pointers are the same and the only differences
1543/// between the two addresses is the offset. It also returns the offsets by
1544/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001545///
1546/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1547/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001548bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1549 int64_t &Offset1,
1550 int64_t &Offset2) const {
1551 // Don't worry about Thumb: just ARM and Thumb2.
1552 if (Subtarget.isThumb1Only()) return false;
1553
1554 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1555 return false;
1556
1557 switch (Load1->getMachineOpcode()) {
1558 default:
1559 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001560 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001561 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001562 case ARM::LDRD:
1563 case ARM::LDRH:
1564 case ARM::LDRSB:
1565 case ARM::LDRSH:
1566 case ARM::VLDRD:
1567 case ARM::VLDRS:
1568 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001569 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001570 case ARM::t2LDRDi8:
1571 case ARM::t2LDRSHi8:
1572 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001573 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001574 case ARM::t2LDRSHi12:
1575 break;
1576 }
1577
1578 switch (Load2->getMachineOpcode()) {
1579 default:
1580 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001581 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001582 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001583 case ARM::LDRD:
1584 case ARM::LDRH:
1585 case ARM::LDRSB:
1586 case ARM::LDRSH:
1587 case ARM::VLDRD:
1588 case ARM::VLDRS:
1589 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001590 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001591 case ARM::t2LDRSHi8:
1592 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001593 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001594 case ARM::t2LDRSHi12:
1595 break;
1596 }
1597
1598 // Check if base addresses and chain operands match.
1599 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1600 Load1->getOperand(4) != Load2->getOperand(4))
1601 return false;
1602
1603 // Index should be Reg0.
1604 if (Load1->getOperand(3) != Load2->getOperand(3))
1605 return false;
1606
1607 // Determine the offsets.
1608 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1609 isa<ConstantSDNode>(Load2->getOperand(1))) {
1610 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1611 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1612 return true;
1613 }
1614
1615 return false;
1616}
1617
1618/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001619/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001620/// be scheduled togther. On some targets if two loads are loading from
1621/// addresses in the same cache line, it's better if they are scheduled
1622/// together. This function takes two integers that represent the load offsets
1623/// from the common base address. It returns true if it decides it's desirable
1624/// to schedule the two loads together. "NumLoads" is the number of loads that
1625/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001626///
1627/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1628/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001629bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1630 int64_t Offset1, int64_t Offset2,
1631 unsigned NumLoads) const {
1632 // Don't worry about Thumb: just ARM and Thumb2.
1633 if (Subtarget.isThumb1Only()) return false;
1634
1635 assert(Offset2 > Offset1);
1636
1637 if ((Offset2 - Offset1) / 8 > 64)
1638 return false;
1639
Renato Golinb184cd92013-08-14 16:35:29 +00001640 // Check if the machine opcodes are different. If they are different
1641 // then we consider them to not be of the same base address,
1642 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1643 // In this case, they are considered to be the same because they are different
1644 // encoding forms of the same basic instruction.
1645 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1646 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1647 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1648 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1649 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001650 return false; // FIXME: overly conservative?
1651
1652 // Four loads in a row should be sufficient.
1653 if (NumLoads >= 3)
1654 return false;
1655
1656 return true;
1657}
1658
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001659bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001660 const MachineBasicBlock *MBB,
1661 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001662 // Debug info is never a scheduling boundary. It's necessary to be explicit
1663 // due to the special treatment of IT instructions below, otherwise a
1664 // dbg_value followed by an IT will result in the IT instruction being
1665 // considered a scheduling hazard, which is wrong. It should be the actual
1666 // instruction preceding the dbg_value instruction(s), just like it is
1667 // when debug info is not present.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001668 if (MI.isDebugValue())
Jim Grosbachba3ece62010-06-25 18:43:14 +00001669 return false;
1670
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001671 // Terminators and labels can't be scheduled around.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001672 if (MI.isTerminator() || MI.isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001673 return true;
1674
1675 // Treat the start of the IT block as a scheduling boundary, but schedule
1676 // t2IT along with all instructions following it.
1677 // FIXME: This is a big hammer. But the alternative is to add all potential
1678 // true and anti dependencies to IT block instructions as implicit operands
1679 // to the t2IT instruction. The added compile time and complexity does not
1680 // seem worth it.
1681 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001682 // Make sure to skip any dbg_value instructions
1683 while (++I != MBB->end() && I->isDebugValue())
1684 ;
1685 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001686 return true;
1687
1688 // Don't attempt to schedule around any instruction that defines
1689 // a stack-oriented pointer, as it's unlikely to be profitable. This
1690 // saves compile time, because it doesn't require every single
1691 // stack slot reference to depend on the instruction that does the
1692 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001693 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001694 // No ARM calling conventions change the stack pointer. (X86 calling
1695 // conventions sometimes do).
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001696 if (!MI.isCall() && MI.definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001697 return true;
1698
1699 return false;
1700}
1701
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001702bool ARMBaseInstrInfo::
1703isProfitableToIfCvt(MachineBasicBlock &MBB,
1704 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +00001705 BranchProbability Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001706 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001707 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001708
Peter Collingbourne65295232015-04-23 20:31:30 +00001709 // If we are optimizing for size, see if the branch in the predecessor can be
1710 // lowered to cbn?z by the constant island lowering pass, and return false if
1711 // so. This results in a shorter instruction sequence.
Sanjay Patel924879a2015-08-04 15:49:57 +00001712 if (MBB.getParent()->getFunction()->optForSize()) {
Peter Collingbourne65295232015-04-23 20:31:30 +00001713 MachineBasicBlock *Pred = *MBB.pred_begin();
1714 if (!Pred->empty()) {
1715 MachineInstr *LastMI = &*Pred->rbegin();
1716 if (LastMI->getOpcode() == ARM::t2Bcc) {
1717 MachineBasicBlock::iterator CmpMI = LastMI;
1718 if (CmpMI != Pred->begin()) {
1719 --CmpMI;
1720 if (CmpMI->getOpcode() == ARM::tCMPi8 ||
1721 CmpMI->getOpcode() == ARM::t2CMPri) {
1722 unsigned Reg = CmpMI->getOperand(0).getReg();
1723 unsigned PredReg = 0;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001724 ARMCC::CondCodes P = getInstrPredicate(*CmpMI, PredReg);
Peter Collingbourne65295232015-04-23 20:31:30 +00001725 if (P == ARMCC::AL && CmpMI->getOperand(1).getImm() == 0 &&
1726 isARMLowRegister(Reg))
1727 return false;
1728 }
1729 }
1730 }
1731 }
1732 }
1733
Owen Anderson88af7d02010-09-28 18:32:13 +00001734 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001735 // Here we scale up each component of UnpredCost to avoid precision issue when
1736 // scaling NumCycles by Probability.
1737 const unsigned ScalingUpFactor = 1024;
1738 unsigned UnpredCost = Probability.scale(NumCycles * ScalingUpFactor);
1739 UnpredCost += ScalingUpFactor; // The branch itself
1740 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001741
Cong Houf9f9ffb2015-09-18 18:19:40 +00001742 return (NumCycles + ExtraPredCycles) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001743}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001744
Evan Cheng02b184d2010-06-25 22:42:03 +00001745bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001746isProfitableToIfCvt(MachineBasicBlock &TMBB,
1747 unsigned TCycles, unsigned TExtra,
1748 MachineBasicBlock &FMBB,
1749 unsigned FCycles, unsigned FExtra,
Cong Houc536bd92015-09-10 23:10:42 +00001750 BranchProbability Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001751 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001752 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001753
Owen Anderson88af7d02010-09-28 18:32:13 +00001754 // Attempt to estimate the relative costs of predication versus branching.
Cong Houf9f9ffb2015-09-18 18:19:40 +00001755 // Here we scale up each component of UnpredCost to avoid precision issue when
1756 // scaling TCycles/FCycles by Probability.
1757 const unsigned ScalingUpFactor = 1024;
1758 unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
1759 unsigned FUnpredCost =
1760 Probability.getCompl().scale(FCycles * ScalingUpFactor);
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001761 unsigned UnpredCost = TUnpredCost + FUnpredCost;
Cong Houf9f9ffb2015-09-18 18:19:40 +00001762 UnpredCost += 1 * ScalingUpFactor; // The branch itself
1763 UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001764
Cong Houf9f9ffb2015-09-18 18:19:40 +00001765 return (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001766}
1767
Bob Wilsone8a549c2012-09-29 21:43:49 +00001768bool
1769ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1770 MachineBasicBlock &FMBB) const {
Diana Picusc5baa432016-06-23 07:47:35 +00001771 // Reduce false anti-dependencies to let the target's out-of-order execution
Bob Wilsone8a549c2012-09-29 21:43:49 +00001772 // engine do its thing.
Diana Picusc5baa432016-06-23 07:47:35 +00001773 return Subtarget.isProfitableToUnpredicate();
Bob Wilsone8a549c2012-09-29 21:43:49 +00001774}
1775
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001776/// getInstrPredicate - If instruction is predicated, returns its predicate
1777/// condition, otherwise returns AL. It also returns the condition code
1778/// register by reference.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001779ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
1780 unsigned &PredReg) {
1781 int PIdx = MI.findFirstPredOperandIdx();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001782 if (PIdx == -1) {
1783 PredReg = 0;
1784 return ARMCC::AL;
1785 }
1786
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001787 PredReg = MI.getOperand(PIdx+1).getReg();
1788 return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001789}
1790
1791
Matthias Braunfa3872e2015-05-18 20:27:55 +00001792unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001793 if (Opc == ARM::B)
1794 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001795 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001796 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001797 if (Opc == ARM::t2B)
1798 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001799
1800 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001801}
1802
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001803MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001804 bool NewMI,
1805 unsigned OpIdx1,
1806 unsigned OpIdx2) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001807 switch (MI.getOpcode()) {
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001808 case ARM::MOVCCr:
1809 case ARM::t2MOVCCr: {
1810 // MOVCC can be commuted by inverting the condition.
1811 unsigned PredReg = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001812 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001813 // MOVCC AL can't be inverted. Shouldn't happen.
1814 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001815 return nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001816 MachineInstr *CommutedMI =
1817 TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
1818 if (!CommutedMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001819 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001820 // After swapping the MOVCC operands, also invert the condition.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001821 CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
1822 .setImm(ARMCC::getOppositeCondition(CC));
1823 return CommutedMI;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001824 }
1825 }
Andrew Kaylor16c4da02015-09-28 20:33:22 +00001826 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001827}
Evan Cheng780748d2009-07-28 05:48:47 +00001828
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001829/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001830/// return the defining instruction.
1831static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1832 const MachineRegisterInfo &MRI,
1833 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001834 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001835 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001836 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001837 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001838 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001839 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001840 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001841 // MI is folded into the MOVCC by predicating it.
1842 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001843 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001844 // Check if MI has any non-dead defs or physreg uses. This also detects
1845 // predicated instructions which will be reading CPSR.
1846 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1847 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001848 // Reject frame index operands, PEI can't handle the predicated pseudos.
1849 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001850 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001851 if (!MO.isReg())
1852 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001853 // MI can't have any tied operands, that would conflict with predication.
1854 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001855 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001856 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001857 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001858 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001859 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001860 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001861 bool DontMoveAcrossStores = true;
Matthias Braun07066cc2015-05-19 21:22:20 +00001862 if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
Craig Topper062a2ba2014-04-25 05:30:21 +00001863 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001864 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001865}
1866
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001867bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001868 SmallVectorImpl<MachineOperand> &Cond,
1869 unsigned &TrueOp, unsigned &FalseOp,
1870 bool &Optimizable) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001871 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001872 "Unknown select instruction");
1873 // MOVCC operands:
1874 // 0: Def.
1875 // 1: True use.
1876 // 2: False use.
1877 // 3: Condition code.
1878 // 4: CPSR use.
1879 TrueOp = 1;
1880 FalseOp = 2;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001881 Cond.push_back(MI.getOperand(3));
1882 Cond.push_back(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001883 // We can always fold a def.
1884 Optimizable = true;
1885 return false;
1886}
1887
Mehdi Amini22e59742015-01-13 07:07:13 +00001888MachineInstr *
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001889ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
Mehdi Amini22e59742015-01-13 07:07:13 +00001890 SmallPtrSetImpl<MachineInstr *> &SeenMIs,
1891 bool PreferFalse) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001892 assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001893 "Unknown select instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001894 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1895 MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001896 bool Invert = !DefMI;
1897 if (!DefMI)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001898 DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001899 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001900 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001901
Matthias Braun2f169f92013-10-04 16:52:56 +00001902 // Find new register class to use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001903 MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
1904 unsigned DestReg = MI.getOperand(0).getReg();
Matthias Braun2f169f92013-10-04 16:52:56 +00001905 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1906 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001907 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001908
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001909 // Create a new predicated version of DefMI.
1910 // Rfalse is the first use.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001911 MachineInstrBuilder NewMI =
1912 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001913
1914 // Copy all the DefMI operands, excluding its (null) predicate.
1915 const MCInstrDesc &DefDesc = DefMI->getDesc();
1916 for (unsigned i = 1, e = DefDesc.getNumOperands();
1917 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1918 NewMI.addOperand(DefMI->getOperand(i));
1919
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001920 unsigned CondCode = MI.getOperand(3).getImm();
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001921 if (Invert)
1922 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1923 else
1924 NewMI.addImm(CondCode);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001925 NewMI.addOperand(MI.getOperand(4));
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001926
1927 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1928 if (NewMI->hasOptionalDef())
1929 AddDefaultCC(NewMI);
1930
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001931 // The output register value when the predicate is false is an implicit
1932 // register operand tied to the first def.
1933 // The tie makes the register allocator ensure the FalseReg is allocated the
1934 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001935 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001936 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001937 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1938
Mehdi Amini22e59742015-01-13 07:07:13 +00001939 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1940 SeenMIs.insert(NewMI);
1941 SeenMIs.erase(DefMI);
1942
Pete Cooper2127b002015-04-30 23:57:47 +00001943 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1944 // DefMI would be invalid when tranferred inside the loop. Checking for a
1945 // loop is expensive, but at least remove kill flags if they are in different
1946 // BBs.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001947 if (DefMI->getParent() != MI.getParent())
Pete Cooper2127b002015-04-30 23:57:47 +00001948 NewMI->clearKillInfo();
1949
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001950 // The caller will erase MI, but not DefMI.
1951 DefMI->eraseFromParent();
1952 return NewMI;
1953}
1954
Andrew Trick924123a2011-09-21 02:20:46 +00001955/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1956/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1957/// def operand.
1958///
1959/// This will go away once we can teach tblgen how to set the optional CPSR def
1960/// operand itself.
1961struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001962 uint16_t PseudoOpc;
1963 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001964};
1965
Craig Topper2fbd1302012-05-24 03:59:11 +00001966static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001967 {ARM::ADDSri, ARM::ADDri},
1968 {ARM::ADDSrr, ARM::ADDrr},
1969 {ARM::ADDSrsi, ARM::ADDrsi},
1970 {ARM::ADDSrsr, ARM::ADDrsr},
1971
1972 {ARM::SUBSri, ARM::SUBri},
1973 {ARM::SUBSrr, ARM::SUBrr},
1974 {ARM::SUBSrsi, ARM::SUBrsi},
1975 {ARM::SUBSrsr, ARM::SUBrsr},
1976
1977 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001978 {ARM::RSBSrsi, ARM::RSBrsi},
1979 {ARM::RSBSrsr, ARM::RSBrsr},
1980
1981 {ARM::t2ADDSri, ARM::t2ADDri},
1982 {ARM::t2ADDSrr, ARM::t2ADDrr},
1983 {ARM::t2ADDSrs, ARM::t2ADDrs},
1984
1985 {ARM::t2SUBSri, ARM::t2SUBri},
1986 {ARM::t2SUBSrr, ARM::t2SUBrr},
1987 {ARM::t2SUBSrs, ARM::t2SUBrs},
1988
1989 {ARM::t2RSBSri, ARM::t2RSBri},
1990 {ARM::t2RSBSrs, ARM::t2RSBrs},
1991};
1992
1993unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001994 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1995 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1996 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001997 return 0;
1998}
1999
Evan Cheng780748d2009-07-28 05:48:47 +00002000void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002001 MachineBasicBlock::iterator &MBBI,
2002 const DebugLoc &dl, unsigned DestReg,
2003 unsigned BaseReg, int NumBytes,
2004 ARMCC::CondCodes Pred, unsigned PredReg,
2005 const ARMBaseInstrInfo &TII,
2006 unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00002007 if (NumBytes == 0 && DestReg != BaseReg) {
2008 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
2009 .addReg(BaseReg, RegState::Kill)
2010 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
2011 .setMIFlags(MIFlags);
2012 return;
2013 }
2014
Evan Cheng780748d2009-07-28 05:48:47 +00002015 bool isSub = NumBytes < 0;
2016 if (isSub) NumBytes = -NumBytes;
2017
2018 while (NumBytes) {
2019 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
2020 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
2021 assert(ThisVal && "Didn't extract field correctly");
2022
2023 // We will handle these bits from offset, clear them.
2024 NumBytes &= ~ThisVal;
2025
2026 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
2027
2028 // Build the new ADD / SUB.
2029 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
2030 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
2031 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00002032 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
2033 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00002034 BaseReg = DestReg;
2035 }
2036}
2037
Tim Northoverdee86042013-12-02 14:46:26 +00002038bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
2039 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00002040 unsigned NumBytes) {
2041 // This optimisation potentially adds lots of load and store
2042 // micro-operations, it's only really a great benefit to code-size.
Sanjay Patel924879a2015-08-04 15:49:57 +00002043 if (!MF.getFunction()->optForMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00002044 return false;
2045
2046 // If only one register is pushed/popped, LLVM can use an LDR/STR
2047 // instead. We can't modify those so make sure we're dealing with an
2048 // instruction we understand.
2049 bool IsPop = isPopOpcode(MI->getOpcode());
2050 bool IsPush = isPushOpcode(MI->getOpcode());
2051 if (!IsPush && !IsPop)
2052 return false;
2053
2054 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
2055 MI->getOpcode() == ARM::VLDMDIA_UPD;
2056 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
2057 MI->getOpcode() == ARM::tPOP ||
2058 MI->getOpcode() == ARM::tPOP_RET;
2059
2060 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
2061 MI->getOperand(1).getReg() == ARM::SP)) &&
2062 "trying to fold sp update into non-sp-updating push/pop");
2063
2064 // The VFP push & pop act on D-registers, so we can only fold an adjustment
2065 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
2066 // if this is violated.
2067 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
2068 return false;
2069
2070 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
2071 // pred) so the list starts at 4. Thumb1 starts after the predicate.
2072 int RegListIdx = IsT1PushPop ? 2 : 4;
2073
2074 // Calculate the space we'll need in terms of registers.
2075 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
2076 unsigned RD0Reg, RegsNeeded;
2077 if (IsVFPPushPop) {
2078 RD0Reg = ARM::D0;
2079 RegsNeeded = NumBytes / 8;
2080 } else {
2081 RD0Reg = ARM::R0;
2082 RegsNeeded = NumBytes / 4;
2083 }
2084
2085 // We're going to have to strip all list operands off before
2086 // re-adding them since the order matters, so save the existing ones
2087 // for later.
2088 SmallVector<MachineOperand, 4> RegList;
2089 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2090 RegList.push_back(MI->getOperand(i));
2091
Tim Northover93bcc662013-11-08 17:18:07 +00002092 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00002093 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00002094
2095 // Now try to find enough space in the reglist to allocate NumBytes.
2096 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00002097 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00002098 if (!IsPop) {
2099 // Pushing any register is completely harmless, mark the
2100 // register involved as undef since we don't care about it in
2101 // the slightest.
2102 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
2103 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00002104 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002105 continue;
2106 }
2107
Tim Northover45479dc2013-12-01 14:16:24 +00002108 // However, we can only pop an extra register if it's not live. For
2109 // registers live within the function we might clobber a return value
2110 // register; the other way a register can be live here is if it's
2111 // callee-saved.
2112 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Matthias Braun60d69e22015-12-11 19:42:09 +00002113 MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
2114 MachineBasicBlock::LQR_Dead) {
Tim Northover45479dc2013-12-01 14:16:24 +00002115 // VFP pops don't allow holes in the register list, so any skip is fatal
2116 // for our transformation. GPR pops do, so we should just keep looking.
2117 if (IsVFPPushPop)
2118 return false;
2119 else
2120 continue;
2121 }
Tim Northover93bcc662013-11-08 17:18:07 +00002122
2123 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00002124 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
2125 true));
Tim Northover45479dc2013-12-01 14:16:24 +00002126 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00002127 }
2128
2129 if (RegsNeeded > 0)
2130 return false;
2131
2132 // Finally we know we can profitably perform the optimisation so go
2133 // ahead: strip all existing registers off and add them back again
2134 // in the right order.
2135 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
2136 MI->RemoveOperand(i);
2137
2138 // Add the complete list back in.
2139 MachineInstrBuilder MIB(MF, &*MI);
2140 for (int i = RegList.size() - 1; i >= 0; --i)
2141 MIB.addOperand(RegList[i]);
2142
2143 return true;
2144}
2145
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002146bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
2147 unsigned FrameReg, int &Offset,
2148 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00002149 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00002150 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00002151 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2152 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002153
Evan Cheng780748d2009-07-28 05:48:47 +00002154 // Memory operands in inline assembly always use AddrMode2.
2155 if (Opcode == ARM::INLINEASM)
2156 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002157
Evan Cheng780748d2009-07-28 05:48:47 +00002158 if (Opcode == ARM::ADDri) {
2159 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2160 if (Offset == 0) {
2161 // Turn it into a move.
2162 MI.setDesc(TII.get(ARM::MOVr));
2163 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2164 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002165 Offset = 0;
2166 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002167 } else if (Offset < 0) {
2168 Offset = -Offset;
2169 isSub = true;
2170 MI.setDesc(TII.get(ARM::SUBri));
2171 }
2172
2173 // Common case: small offset, fits into instruction.
2174 if (ARM_AM::getSOImmVal(Offset) != -1) {
2175 // Replace the FrameIndex with sp / fp
2176 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2177 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002178 Offset = 0;
2179 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002180 }
2181
2182 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2183 // as possible.
2184 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2185 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2186
2187 // We will handle these bits from offset, clear them.
2188 Offset &= ~ThisImmVal;
2189
2190 // Get the properly encoded SOImmVal field.
2191 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2192 "Bit extraction didn't work?");
2193 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2194 } else {
2195 unsigned ImmIdx = 0;
2196 int InstrOffs = 0;
2197 unsigned NumBits = 0;
2198 unsigned Scale = 1;
2199 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002200 case ARMII::AddrMode_i12: {
2201 ImmIdx = FrameRegIdx + 1;
2202 InstrOffs = MI.getOperand(ImmIdx).getImm();
2203 NumBits = 12;
2204 break;
2205 }
Evan Cheng780748d2009-07-28 05:48:47 +00002206 case ARMII::AddrMode2: {
2207 ImmIdx = FrameRegIdx+2;
2208 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2209 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2210 InstrOffs *= -1;
2211 NumBits = 12;
2212 break;
2213 }
2214 case ARMII::AddrMode3: {
2215 ImmIdx = FrameRegIdx+2;
2216 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2217 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2218 InstrOffs *= -1;
2219 NumBits = 8;
2220 break;
2221 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002222 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002223 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002224 // Can't fold any offset even if it's zero.
2225 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002226 case ARMII::AddrMode5: {
2227 ImmIdx = FrameRegIdx+1;
2228 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2229 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2230 InstrOffs *= -1;
2231 NumBits = 8;
2232 Scale = 4;
2233 break;
2234 }
2235 default:
2236 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002237 }
2238
2239 Offset += InstrOffs * Scale;
2240 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2241 if (Offset < 0) {
2242 Offset = -Offset;
2243 isSub = true;
2244 }
2245
2246 // Attempt to fold address comp. if opcode has offset bits
2247 if (NumBits > 0) {
2248 // Common case: small offset, fits into instruction.
2249 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2250 int ImmedOffset = Offset / Scale;
2251 unsigned Mask = (1 << NumBits) - 1;
2252 if ((unsigned)Offset <= Mask * Scale) {
2253 // Replace the FrameIndex with sp
2254 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002255 // FIXME: When addrmode2 goes away, this will simplify (like the
2256 // T2 version), as the LDR.i12 versions don't need the encoding
2257 // tricks for the offset value.
2258 if (isSub) {
2259 if (AddrMode == ARMII::AddrMode_i12)
2260 ImmedOffset = -ImmedOffset;
2261 else
2262 ImmedOffset |= 1 << NumBits;
2263 }
Evan Cheng780748d2009-07-28 05:48:47 +00002264 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002265 Offset = 0;
2266 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002267 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002268
Evan Cheng780748d2009-07-28 05:48:47 +00002269 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2270 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002271 if (isSub) {
2272 if (AddrMode == ARMII::AddrMode_i12)
2273 ImmedOffset = -ImmedOffset;
2274 else
2275 ImmedOffset |= 1 << NumBits;
2276 }
Evan Cheng780748d2009-07-28 05:48:47 +00002277 ImmOp.ChangeToImmediate(ImmedOffset);
2278 Offset &= ~(Mask*Scale);
2279 }
2280 }
2281
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002282 Offset = (isSub) ? -Offset : Offset;
2283 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002284}
Bill Wendling7de9d522010-08-06 01:32:48 +00002285
Manman Ren6fa76dc2012-06-29 21:33:59 +00002286/// analyzeCompare - For a comparison instruction, return the source registers
2287/// in SrcReg and SrcReg2 if having two register operands, and the value it
2288/// compares against in CmpValue. Return true if the comparison instruction
2289/// can be analyzed.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002290bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
2291 unsigned &SrcReg2, int &CmpMask,
2292 int &CmpValue) const {
2293 switch (MI.getOpcode()) {
Bill Wendling7de9d522010-08-06 01:32:48 +00002294 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002295 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002296 case ARM::t2CMPri:
James Molloy0f412272016-09-09 09:51:06 +00002297 case ARM::tCMPi8:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002298 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002299 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002300 CmpMask = ~0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002301 CmpValue = MI.getOperand(1).getImm();
Bill Wendling7de9d522010-08-06 01:32:48 +00002302 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002303 case ARM::CMPrr:
2304 case ARM::t2CMPrr:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002305 SrcReg = MI.getOperand(0).getReg();
2306 SrcReg2 = MI.getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002307 CmpMask = ~0;
2308 CmpValue = 0;
2309 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002310 case ARM::TSTri:
2311 case ARM::t2TSTri:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002312 SrcReg = MI.getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002313 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002314 CmpMask = MI.getOperand(1).getImm();
Gabor Greifadbbb932010-09-21 12:01:15 +00002315 CmpValue = 0;
2316 return true;
2317 }
2318
2319 return false;
2320}
2321
Gabor Greifd36e3e82010-09-29 10:12:08 +00002322/// isSuitableForMask - Identify a suitable 'and' instruction that
2323/// operates on the given source register and applies the same mask
2324/// as a 'tst' instruction. Provide a limited look-through for copies.
2325/// When successful, MI will hold the found instruction.
2326static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002327 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002328 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002329 case ARM::ANDri:
2330 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002331 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002332 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002333 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002334 return true;
2335 break;
Bill Wendling7de9d522010-08-06 01:32:48 +00002336 }
2337
2338 return false;
2339}
2340
Manman Renb1b3db62012-06-29 22:06:19 +00002341/// getSwappedCondition - assume the flags are set by MI(a,b), return
2342/// the condition code if we modify the instructions such that flags are
2343/// set by MI(b,a).
2344inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2345 switch (CC) {
2346 default: return ARMCC::AL;
2347 case ARMCC::EQ: return ARMCC::EQ;
2348 case ARMCC::NE: return ARMCC::NE;
2349 case ARMCC::HS: return ARMCC::LS;
2350 case ARMCC::LO: return ARMCC::HI;
2351 case ARMCC::HI: return ARMCC::LO;
2352 case ARMCC::LS: return ARMCC::HS;
2353 case ARMCC::GE: return ARMCC::LE;
2354 case ARMCC::LT: return ARMCC::GT;
2355 case ARMCC::GT: return ARMCC::LT;
2356 case ARMCC::LE: return ARMCC::GE;
2357 }
2358}
2359
2360/// isRedundantFlagInstr - check whether the first instruction, whose only
2361/// purpose is to update flags, can be made redundant.
2362/// CMPrr can be made redundant by SUBrr if the operands are the same.
2363/// CMPri can be made redundant by SUBri if the operands are the same.
2364/// This function can be extended later on.
2365inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2366 unsigned SrcReg2, int ImmValue,
2367 MachineInstr *OI) {
2368 if ((CmpI->getOpcode() == ARM::CMPrr ||
2369 CmpI->getOpcode() == ARM::t2CMPrr) &&
2370 (OI->getOpcode() == ARM::SUBrr ||
2371 OI->getOpcode() == ARM::t2SUBrr) &&
2372 ((OI->getOperand(1).getReg() == SrcReg &&
2373 OI->getOperand(2).getReg() == SrcReg2) ||
2374 (OI->getOperand(1).getReg() == SrcReg2 &&
2375 OI->getOperand(2).getReg() == SrcReg)))
2376 return true;
2377
2378 if ((CmpI->getOpcode() == ARM::CMPri ||
2379 CmpI->getOpcode() == ARM::t2CMPri) &&
2380 (OI->getOpcode() == ARM::SUBri ||
2381 OI->getOpcode() == ARM::t2SUBri) &&
2382 OI->getOperand(1).getReg() == SrcReg &&
2383 OI->getOperand(2).getImm() == ImmValue)
2384 return true;
2385 return false;
2386}
2387
Manman Ren6fa76dc2012-06-29 21:33:59 +00002388/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2389/// comparison into one that sets the zero bit in the flags register;
2390/// Remove a redundant Compare instruction if an earlier instruction can set the
2391/// flags in the same way as Compare.
2392/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2393/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2394/// condition code of instructions which use the flags.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002395bool ARMBaseInstrInfo::optimizeCompareInstr(
2396 MachineInstr &CmpInstr, unsigned SrcReg, unsigned SrcReg2, int CmpMask,
2397 int CmpValue, const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002398 // Get the unique definition of SrcReg.
2399 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2400 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002401
Gabor Greifadbbb932010-09-21 12:01:15 +00002402 // Masked compares sometimes use the same register as the corresponding 'and'.
2403 if (CmpMask != ~0) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002404 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002405 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002406 for (MachineRegisterInfo::use_instr_iterator
2407 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2408 UI != UE; ++UI) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002409 if (UI->getParent() != CmpInstr.getParent())
2410 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002411 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002412 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002413 isPredicated(*PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002414 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002415 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002416 break;
2417 }
2418 if (!MI) return false;
2419 }
2420 }
2421
Manman Rendc8ad002012-05-11 01:30:47 +00002422 // Get ready to iterate backward from CmpInstr.
2423 MachineBasicBlock::iterator I = CmpInstr, E = MI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002424 B = CmpInstr.getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002425
2426 // Early exit if CmpInstr is at the beginning of the BB.
2427 if (I == B) return false;
2428
Manman Rendc8ad002012-05-11 01:30:47 +00002429 // There are two possible candidates which can be changed to set CPSR:
2430 // One is MI, the other is a SUB instruction.
2431 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2432 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002433 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002434 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002435 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002436 MI = nullptr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002437 else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002438 // Conservatively refuse to convert an instruction which isn't in the same
2439 // BB as the comparison.
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002440 // For CMPri w/ CmpValue != 0, a Sub may still be a candidate.
2441 // Thus we cannot return here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002442 if (CmpInstr.getOpcode() == ARM::CMPri ||
2443 CmpInstr.getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002444 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002445 else
2446 return false;
2447 }
2448
2449 // Check that CPSR isn't set between the comparison instruction and the one we
2450 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002451 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002452 --I;
2453 for (; I != E; --I) {
2454 const MachineInstr &Instr = *I;
2455
Manman Renb1b3db62012-06-29 22:06:19 +00002456 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2457 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002458 // This instruction modifies or uses CPSR after the one we want to
2459 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002460 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002461
Manman Renb1b3db62012-06-29 22:06:19 +00002462 // Check whether CmpInstr can be made redundant by the current instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002463 if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002464 Sub = &*I;
2465 break;
2466 }
2467
Evan Chengd757c882010-09-21 23:49:07 +00002468 if (I == B)
2469 // The 'and' is below the comparison instruction.
2470 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002471 }
2472
Manman Rendc8ad002012-05-11 01:30:47 +00002473 // Return false if no candidates exist.
2474 if (!MI && !Sub)
2475 return false;
2476
2477 // The single candidate is called MI.
2478 if (!MI) MI = Sub;
2479
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002480 // We can't use a predicated instruction - it doesn't always write the flags.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002481 if (isPredicated(*MI))
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002482 return false;
2483
James Molloy0f412272016-09-09 09:51:06 +00002484 bool IsThumb1 = false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002485 switch (MI->getOpcode()) {
2486 default: break;
James Molloy0f412272016-09-09 09:51:06 +00002487 case ARM::tLSLri:
2488 case ARM::tLSRri:
2489 case ARM::tLSLrr:
2490 case ARM::tLSRrr:
2491 case ARM::tSUBrr:
2492 case ARM::tADDrr:
2493 case ARM::tADDi3:
2494 case ARM::tADDi8:
2495 case ARM::tSUBi3:
2496 case ARM::tSUBi8:
2497 IsThumb1 = true;
2498 LLVM_FALLTHROUGH;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002499 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002500 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002501 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002502 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002503 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002504 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002505 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002506 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002507 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002508 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002509 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002510 case ARM::SBCri:
2511 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002512 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002513 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002514 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002515 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002516 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002517 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002518 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002519 case ARM::t2SBCri:
2520 case ARM::ANDrr:
2521 case ARM::ANDri:
2522 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002523 case ARM::t2ANDri:
2524 case ARM::ORRrr:
2525 case ARM::ORRri:
2526 case ARM::t2ORRrr:
2527 case ARM::t2ORRri:
2528 case ARM::EORrr:
2529 case ARM::EORri:
2530 case ARM::t2EORrr:
2531 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002532 // Scan forward for the use of CPSR
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002533 // When checking against MI: if it's a conditional code that requires
2534 // checking of the V bit or C bit, then this is not safe to do.
Manman Ren34cb93e2012-07-11 22:51:44 +00002535 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2536 // If we are done with the basic block, we need to check whether CPSR is
2537 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002538 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2539 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002540 bool isSafe = false;
2541 I = CmpInstr;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002542 E = CmpInstr.getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002543 while (!isSafe && ++I != E) {
2544 const MachineInstr &Instr = *I;
2545 for (unsigned IO = 0, EO = Instr.getNumOperands();
2546 !isSafe && IO != EO; ++IO) {
2547 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002548 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2549 isSafe = true;
2550 break;
2551 }
Evan Cheng425489d2011-03-23 22:52:04 +00002552 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2553 continue;
2554 if (MO.isDef()) {
2555 isSafe = true;
2556 break;
2557 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002558 // Condition code is after the operand before CPSR except for VSELs.
2559 ARMCC::CondCodes CC;
2560 bool IsInstrVSel = true;
2561 switch (Instr.getOpcode()) {
2562 default:
2563 IsInstrVSel = false;
2564 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2565 break;
2566 case ARM::VSELEQD:
2567 case ARM::VSELEQS:
2568 CC = ARMCC::EQ;
2569 break;
2570 case ARM::VSELGTD:
2571 case ARM::VSELGTS:
2572 CC = ARMCC::GT;
2573 break;
2574 case ARM::VSELGED:
2575 case ARM::VSELGES:
2576 CC = ARMCC::GE;
2577 break;
2578 case ARM::VSELVSS:
2579 case ARM::VSELVSD:
2580 CC = ARMCC::VS;
2581 break;
2582 }
2583
Manman Renb1b3db62012-06-29 22:06:19 +00002584 if (Sub) {
2585 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2586 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002587 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002588 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2589 // on CMP needs to be updated to be based on SUB.
2590 // Push the condition code operands to OperandsToUpdate.
2591 // If it is safe to remove CmpInstr, the condition code of these
2592 // operands will be modified.
2593 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002594 Sub->getOperand(2).getReg() == SrcReg) {
2595 // VSel doesn't support condition code update.
2596 if (IsInstrVSel)
2597 return false;
2598 OperandsToUpdate.push_back(
2599 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2600 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002601 } else {
2602 // No Sub, so this is x = <op> y, z; cmp x, 0.
Manman Rendc8ad002012-05-11 01:30:47 +00002603 switch (CC) {
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002604 case ARMCC::EQ: // Z
2605 case ARMCC::NE: // Z
2606 case ARMCC::MI: // N
2607 case ARMCC::PL: // N
2608 case ARMCC::AL: // none
Manman Ren88a0d332012-07-11 23:47:00 +00002609 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002610 break;
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002611 case ARMCC::HS: // C
2612 case ARMCC::LO: // C
2613 case ARMCC::VS: // V
2614 case ARMCC::VC: // V
2615 case ARMCC::HI: // C Z
2616 case ARMCC::LS: // C Z
2617 case ARMCC::GE: // N V
2618 case ARMCC::LT: // N V
2619 case ARMCC::GT: // Z N V
2620 case ARMCC::LE: // Z N V
2621 // The instruction uses the V bit or C bit which is not safe.
Manman Rendc8ad002012-05-11 01:30:47 +00002622 return false;
2623 }
Jan Wen Voungd21194f2015-02-02 16:56:50 +00002624 }
Evan Cheng425489d2011-03-23 22:52:04 +00002625 }
2626 }
2627
Manman Ren34cb93e2012-07-11 22:51:44 +00002628 // If CPSR is not killed nor re-defined, we should check whether it is
2629 // live-out. If it is live-out, do not optimize.
2630 if (!isSafe) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002631 MachineBasicBlock *MBB = CmpInstr.getParent();
Manman Ren34cb93e2012-07-11 22:51:44 +00002632 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2633 SE = MBB->succ_end(); SI != SE; ++SI)
2634 if ((*SI)->isLiveIn(ARM::CPSR))
2635 return false;
2636 }
Evan Cheng425489d2011-03-23 22:52:04 +00002637
James Molloy0f412272016-09-09 09:51:06 +00002638 // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
2639 // set CPSR so this is represented as an explicit output)
2640 if (!IsThumb1) {
2641 MI->getOperand(5).setReg(ARM::CPSR);
2642 MI->getOperand(5).setIsDef(true);
2643 }
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00002644 assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002645 CmpInstr.eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002646
2647 // Modify the condition code of operands in OperandsToUpdate.
2648 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2649 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002650 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2651 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002652 return true;
2653 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002654 }
James Molloy0f412272016-09-09 09:51:06 +00002655
Bill Wendling7de9d522010-08-06 01:32:48 +00002656 return false;
2657}
Evan Cheng367a5df2010-09-09 18:18:55 +00002658
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002659bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
2660 unsigned Reg,
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002661 MachineRegisterInfo *MRI) const {
2662 // Fold large immediates into add, sub, or, xor.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002663 unsigned DefOpc = DefMI.getOpcode();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002664 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2665 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002666 if (!DefMI.getOperand(1).isImm())
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002667 // Could be t2MOVi32imm <ga:xx>
2668 return false;
2669
2670 if (!MRI->hasOneNonDBGUse(Reg))
2671 return false;
2672
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002673 const MCInstrDesc &DefMCID = DefMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002674 if (DefMCID.hasOptionalDef()) {
2675 unsigned NumOps = DefMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002676 const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
Evan Chenga2b48d92012-03-26 23:31:00 +00002677 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2678 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2679 // to delete DefMI.
2680 return false;
2681 }
2682
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002683 const MCInstrDesc &UseMCID = UseMI.getDesc();
Evan Chenga2b48d92012-03-26 23:31:00 +00002684 if (UseMCID.hasOptionalDef()) {
2685 unsigned NumOps = UseMCID.getNumOperands();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002686 if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
Evan Chenga2b48d92012-03-26 23:31:00 +00002687 // If the instruction sets the flag, do not attempt this optimization
2688 // since it may change the semantics of the code.
2689 return false;
2690 }
2691
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002692 unsigned UseOpc = UseMI.getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002693 unsigned NewUseOpc = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002694 uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002695 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002696 bool Commute = false;
2697 switch (UseOpc) {
2698 default: return false;
2699 case ARM::SUBrr:
2700 case ARM::ADDrr:
2701 case ARM::ORRrr:
2702 case ARM::EORrr:
2703 case ARM::t2SUBrr:
2704 case ARM::t2ADDrr:
2705 case ARM::t2ORRrr:
2706 case ARM::t2EORrr: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002707 Commute = UseMI.getOperand(2).getReg() != Reg;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002708 switch (UseOpc) {
2709 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002710 case ARM::ADDrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002711 case ARM::SUBrr: {
2712 if (UseOpc == ARM::SUBrr && Commute)
2713 return false;
2714
2715 // ADD/SUB are special because they're essentially the same operation, so
2716 // we can handle a larger range of immediates.
2717 if (ARM_AM::isSOImmTwoPartVal(ImmVal))
2718 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
2719 else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
2720 ImmVal = -ImmVal;
2721 NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
2722 } else
2723 return false;
2724 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2725 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2726 break;
2727 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002728 case ARM::ORRrr:
2729 case ARM::EORrr: {
2730 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2731 return false;
2732 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2733 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2734 switch (UseOpc) {
2735 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002736 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2737 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2738 }
2739 break;
2740 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002741 case ARM::t2ADDrr:
Tim Northoverc08db182016-05-02 18:30:08 +00002742 case ARM::t2SUBrr: {
2743 if (UseOpc == ARM::t2SUBrr && Commute)
2744 return false;
2745
2746 // ADD/SUB are special because they're essentially the same operation, so
2747 // we can handle a larger range of immediates.
2748 if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2749 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2ADDri : ARM::t2SUBri;
2750 else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
2751 ImmVal = -ImmVal;
2752 NewUseOpc = UseOpc == ARM::t2ADDrr ? ARM::t2SUBri : ARM::t2ADDri;
2753 } else
2754 return false;
2755 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2756 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2757 break;
2758 }
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002759 case ARM::t2ORRrr:
2760 case ARM::t2EORrr: {
2761 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2762 return false;
2763 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2764 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2765 switch (UseOpc) {
2766 default: break;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002767 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2768 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2769 }
2770 break;
2771 }
2772 }
2773 }
2774 }
2775
2776 unsigned OpIdx = Commute ? 2 : 1;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002777 unsigned Reg1 = UseMI.getOperand(OpIdx).getReg();
2778 bool isKill = UseMI.getOperand(OpIdx).isKill();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002779 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002780 AddDefaultCC(
2781 AddDefaultPred(BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(),
2782 get(NewUseOpc), NewReg)
2783 .addReg(Reg1, getKillRegState(isKill))
2784 .addImm(SOImmValV1)));
2785 UseMI.setDesc(get(NewUseOpc));
2786 UseMI.getOperand(1).setReg(NewReg);
2787 UseMI.getOperand(1).setIsKill();
2788 UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
2789 DefMI.eraseFromParent();
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002790 return true;
2791}
2792
Bob Wilsone8a549c2012-09-29 21:43:49 +00002793static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002794 const MachineInstr &MI) {
2795 switch (MI.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00002796 default: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002797 const MCInstrDesc &Desc = MI.getDesc();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002798 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2799 assert(UOps >= 0 && "bad # UOps");
2800 return UOps;
2801 }
2802
2803 case ARM::LDRrs:
2804 case ARM::LDRBrs:
2805 case ARM::STRrs:
2806 case ARM::STRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002807 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002808 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2809 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2810 if (!isSub &&
2811 (ShImm == 0 ||
2812 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2813 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2814 return 1;
2815 return 2;
2816 }
2817
2818 case ARM::LDRH:
2819 case ARM::STRH: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002820 if (!MI.getOperand(2).getReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00002821 return 1;
2822
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002823 unsigned ShOpVal = MI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002824 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2825 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2826 if (!isSub &&
2827 (ShImm == 0 ||
2828 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2829 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2830 return 1;
2831 return 2;
2832 }
2833
2834 case ARM::LDRSB:
2835 case ARM::LDRSH:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002836 return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002837
2838 case ARM::LDRSB_POST:
2839 case ARM::LDRSH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002840 unsigned Rt = MI.getOperand(0).getReg();
2841 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002842 return (Rt == Rm) ? 4 : 3;
2843 }
2844
2845 case ARM::LDR_PRE_REG:
2846 case ARM::LDRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002847 unsigned Rt = MI.getOperand(0).getReg();
2848 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002849 if (Rt == Rm)
2850 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002851 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002852 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2853 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2854 if (!isSub &&
2855 (ShImm == 0 ||
2856 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2857 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2858 return 2;
2859 return 3;
2860 }
2861
2862 case ARM::STR_PRE_REG:
2863 case ARM::STRB_PRE_REG: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002864 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002865 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2866 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2867 if (!isSub &&
2868 (ShImm == 0 ||
2869 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2870 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2871 return 2;
2872 return 3;
2873 }
2874
2875 case ARM::LDRH_PRE:
2876 case ARM::STRH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002877 unsigned Rt = MI.getOperand(0).getReg();
2878 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002879 if (!Rm)
2880 return 2;
2881 if (Rt == Rm)
2882 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002883 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002884 }
2885
2886 case ARM::LDR_POST_REG:
2887 case ARM::LDRB_POST_REG:
2888 case ARM::LDRH_POST: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002889 unsigned Rt = MI.getOperand(0).getReg();
2890 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002891 return (Rt == Rm) ? 3 : 2;
2892 }
2893
2894 case ARM::LDR_PRE_IMM:
2895 case ARM::LDRB_PRE_IMM:
2896 case ARM::LDR_POST_IMM:
2897 case ARM::LDRB_POST_IMM:
2898 case ARM::STRB_POST_IMM:
2899 case ARM::STRB_POST_REG:
2900 case ARM::STRB_PRE_IMM:
2901 case ARM::STRH_POST:
2902 case ARM::STR_POST_IMM:
2903 case ARM::STR_POST_REG:
2904 case ARM::STR_PRE_IMM:
2905 return 2;
2906
2907 case ARM::LDRSB_PRE:
2908 case ARM::LDRSH_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002909 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002910 if (Rm == 0)
2911 return 3;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002912 unsigned Rt = MI.getOperand(0).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002913 if (Rt == Rm)
2914 return 4;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002915 unsigned ShOpVal = MI.getOperand(4).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002916 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2917 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2918 if (!isSub &&
2919 (ShImm == 0 ||
2920 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2921 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2922 return 3;
2923 return 4;
2924 }
2925
2926 case ARM::LDRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002927 unsigned Rt = MI.getOperand(0).getReg();
2928 unsigned Rn = MI.getOperand(2).getReg();
2929 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002930 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002931 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
2932 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002933 return (Rt == Rn) ? 3 : 2;
2934 }
2935
2936 case ARM::STRD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002937 unsigned Rm = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002938 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002939 return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
2940 : 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002941 return 2;
2942 }
2943
2944 case ARM::LDRD_POST:
2945 case ARM::t2LDRD_POST:
2946 return 3;
2947
2948 case ARM::STRD_POST:
2949 case ARM::t2STRD_POST:
2950 return 4;
2951
2952 case ARM::LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002953 unsigned Rt = MI.getOperand(0).getReg();
2954 unsigned Rn = MI.getOperand(3).getReg();
2955 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002956 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002957 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
2958 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002959 return (Rt == Rn) ? 4 : 3;
2960 }
2961
2962 case ARM::t2LDRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002963 unsigned Rt = MI.getOperand(0).getReg();
2964 unsigned Rn = MI.getOperand(3).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002965 return (Rt == Rn) ? 4 : 3;
2966 }
2967
2968 case ARM::STRD_PRE: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002969 unsigned Rm = MI.getOperand(4).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00002970 if (Rm)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002971 return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
2972 : 4;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002973 return 3;
2974 }
2975
2976 case ARM::t2STRD_PRE:
2977 return 3;
2978
2979 case ARM::t2LDR_POST:
2980 case ARM::t2LDRB_POST:
2981 case ARM::t2LDRB_PRE:
2982 case ARM::t2LDRSBi12:
2983 case ARM::t2LDRSBi8:
2984 case ARM::t2LDRSBpci:
2985 case ARM::t2LDRSBs:
2986 case ARM::t2LDRH_POST:
2987 case ARM::t2LDRH_PRE:
2988 case ARM::t2LDRSBT:
2989 case ARM::t2LDRSB_POST:
2990 case ARM::t2LDRSB_PRE:
2991 case ARM::t2LDRSH_POST:
2992 case ARM::t2LDRSH_PRE:
2993 case ARM::t2LDRSHi12:
2994 case ARM::t2LDRSHi8:
2995 case ARM::t2LDRSHpci:
2996 case ARM::t2LDRSHs:
2997 return 2;
2998
2999 case ARM::t2LDRDi8: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003000 unsigned Rt = MI.getOperand(0).getReg();
3001 unsigned Rn = MI.getOperand(2).getReg();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003002 return (Rt == Rn) ? 3 : 2;
3003 }
3004
3005 case ARM::t2STRB_POST:
3006 case ARM::t2STRB_PRE:
3007 case ARM::t2STRBs:
3008 case ARM::t2STRDi8:
3009 case ARM::t2STRH_POST:
3010 case ARM::t2STRH_PRE:
3011 case ARM::t2STRHs:
3012 case ARM::t2STR_POST:
3013 case ARM::t2STR_PRE:
3014 case ARM::t2STRs:
3015 return 2;
3016 }
3017}
3018
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003019// Return the number of 32-bit words loaded by LDM or stored by STM. If this
3020// can't be easily determined return 0 (missing MachineMemOperand).
3021//
3022// FIXME: The current MachineInstr design does not support relying on machine
3023// mem operands to determine the width of a memory access. Instead, we expect
3024// the target to provide this information based on the instruction opcode and
Robin Morisset039781e2014-08-29 21:53:01 +00003025// operands. However, using MachineMemOperand is the best solution now for
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003026// two reasons:
3027//
3028// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
3029// operands. This is much more dangerous than using the MachineMemOperand
3030// sizes because CodeGen passes can insert/remove optional machine operands. In
3031// fact, it's totally incorrect for preRA passes and appears to be wrong for
3032// postRA passes as well.
3033//
3034// 2) getNumLDMAddresses is only used by the scheduling machine model and any
3035// machine model that calls this should handle the unknown (zero size) case.
3036//
3037// Long term, we should require a target hook that verifies MachineMemOperand
3038// sizes during MC lowering. That target hook should be local to MC lowering
3039// because we can't ensure that it is aware of other MI forms. Doing this will
3040// ensure that MachineMemOperands are correctly propagated through all passes.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003041unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003042 unsigned Size = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003043 for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
3044 E = MI.memoperands_end();
3045 I != E; ++I) {
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00003046 Size += (*I)->getSize();
3047 }
3048 return Size / 4;
3049}
3050
Diana Picus92423ce2016-06-27 09:08:23 +00003051static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
3052 unsigned NumRegs) {
3053 unsigned UOps = 1 + NumRegs; // 1 for address computation.
3054 switch (Opc) {
3055 default:
3056 break;
3057 case ARM::VLDMDIA_UPD:
3058 case ARM::VLDMDDB_UPD:
3059 case ARM::VLDMSIA_UPD:
3060 case ARM::VLDMSDB_UPD:
3061 case ARM::VSTMDIA_UPD:
3062 case ARM::VSTMDDB_UPD:
3063 case ARM::VSTMSIA_UPD:
3064 case ARM::VSTMSDB_UPD:
3065 case ARM::LDMIA_UPD:
3066 case ARM::LDMDA_UPD:
3067 case ARM::LDMDB_UPD:
3068 case ARM::LDMIB_UPD:
3069 case ARM::STMIA_UPD:
3070 case ARM::STMDA_UPD:
3071 case ARM::STMDB_UPD:
3072 case ARM::STMIB_UPD:
3073 case ARM::tLDMIA_UPD:
3074 case ARM::tSTMIA_UPD:
3075 case ARM::t2LDMIA_UPD:
3076 case ARM::t2LDMDB_UPD:
3077 case ARM::t2STMIA_UPD:
3078 case ARM::t2STMDB_UPD:
3079 ++UOps; // One for base register writeback.
3080 break;
3081 case ARM::LDMIA_RET:
3082 case ARM::tPOP_RET:
3083 case ARM::t2LDMIA_RET:
3084 UOps += 2; // One for base reg wb, one for write to pc.
3085 break;
3086 }
3087 return UOps;
3088}
3089
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003090unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
3091 const MachineInstr &MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00003092 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00003093 return 1;
3094
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003095 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00003096 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00003097 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003098 if (ItinUOps >= 0) {
3099 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
3100 return getNumMicroOpsSwiftLdSt(ItinData, MI);
3101
Andrew Trickf161e392012-07-02 18:10:42 +00003102 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003103 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003104
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003105 unsigned Opc = MI.getOpcode();
Evan Cheng367a5df2010-09-09 18:18:55 +00003106 switch (Opc) {
3107 default:
3108 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003109 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003110 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00003111 return 2;
3112
3113 // The number of uOps for load / store multiple are determined by the number
3114 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00003115 //
Evan Chengbf407072010-09-10 01:29:16 +00003116 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
3117 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00003118 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003119 //
Evan Chengbf407072010-09-10 01:29:16 +00003120 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003121 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
3122 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
3123 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003124 case ARM::VLDMDIA_UPD:
3125 case ARM::VLDMDDB_UPD:
3126 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003127 case ARM::VLDMSIA_UPD:
3128 case ARM::VLDMSDB_UPD:
3129 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003130 case ARM::VSTMDIA_UPD:
3131 case ARM::VSTMDDB_UPD:
3132 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003133 case ARM::VSTMSIA_UPD:
3134 case ARM::VSTMSDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003135 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
Evan Cheng367a5df2010-09-09 18:18:55 +00003136 return (NumRegs / 2) + (NumRegs % 2) + 1;
3137 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003138
3139 case ARM::LDMIA_RET:
3140 case ARM::LDMIA:
3141 case ARM::LDMDA:
3142 case ARM::LDMDB:
3143 case ARM::LDMIB:
3144 case ARM::LDMIA_UPD:
3145 case ARM::LDMDA_UPD:
3146 case ARM::LDMDB_UPD:
3147 case ARM::LDMIB_UPD:
3148 case ARM::STMIA:
3149 case ARM::STMDA:
3150 case ARM::STMDB:
3151 case ARM::STMIB:
3152 case ARM::STMIA_UPD:
3153 case ARM::STMDA_UPD:
3154 case ARM::STMDB_UPD:
3155 case ARM::STMIB_UPD:
3156 case ARM::tLDMIA:
3157 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003158 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00003159 case ARM::tPOP_RET:
3160 case ARM::tPOP:
3161 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003162 case ARM::t2LDMIA_RET:
3163 case ARM::t2LDMIA:
3164 case ARM::t2LDMDB:
3165 case ARM::t2LDMIA_UPD:
3166 case ARM::t2LDMDB_UPD:
3167 case ARM::t2STMIA:
3168 case ARM::t2STMDB:
3169 case ARM::t2STMIA_UPD:
3170 case ARM::t2STMDB_UPD: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003171 unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
Diana Picus92423ce2016-06-27 09:08:23 +00003172 switch (Subtarget.getLdStMultipleTiming()) {
3173 case ARMSubtarget::SingleIssuePlusExtras:
3174 return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
3175 case ARMSubtarget::SingleIssue:
3176 // Assume the worst.
3177 return NumRegs;
3178 case ARMSubtarget::DoubleIssue: {
Evan Chengdebf9c52010-11-03 00:45:17 +00003179 if (NumRegs < 4)
3180 return 2;
3181 // 4 registers would be issued: 2, 2.
3182 // 5 registers would be issued: 2, 2, 1.
Diana Picus92423ce2016-06-27 09:08:23 +00003183 unsigned UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00003184 if (NumRegs % 2)
Diana Picus92423ce2016-06-27 09:08:23 +00003185 ++UOps;
3186 return UOps;
3187 }
3188 case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
3189 unsigned UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00003190 // If there are odd number of registers or if it's not 64-bit aligned,
3191 // then it takes an extra AGU (Address Generation Unit) cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003192 if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
3193 (*MI.memoperands_begin())->getAlignment() < 8)
Diana Picus92423ce2016-06-27 09:08:23 +00003194 ++UOps;
3195 return UOps;
3196 }
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00003197 }
Evan Cheng367a5df2010-09-09 18:18:55 +00003198 }
3199 }
Diana Picus92423ce2016-06-27 09:08:23 +00003200 llvm_unreachable("Didn't find the number of microops");
Evan Cheng367a5df2010-09-09 18:18:55 +00003201}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003202
3203int
Evan Cheng412e37b2010-10-07 23:12:15 +00003204ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003205 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003206 unsigned DefClass,
3207 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003208 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003209 if (RegNo <= 0)
3210 // Def is the address writeback.
3211 return ItinData->getOperandCycle(DefClass, DefIdx);
3212
3213 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003214 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003215 // (regno / 2) + (regno % 2) + 1
3216 DefCycle = RegNo / 2 + 1;
3217 if (RegNo % 2)
3218 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003219 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003220 DefCycle = RegNo;
3221 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003222
Evan Cheng6cc775f2011-06-28 19:10:37 +00003223 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003224 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003225 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003226 case ARM::VLDMSIA_UPD:
3227 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003228 isSLoad = true;
3229 break;
3230 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003231
Evan Cheng412e37b2010-10-07 23:12:15 +00003232 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3233 // then it takes an extra cycle.
3234 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3235 ++DefCycle;
3236 } else {
3237 // Assume the worst.
3238 DefCycle = RegNo + 2;
3239 }
3240
3241 return DefCycle;
3242}
3243
3244int
3245ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003246 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003247 unsigned DefClass,
3248 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003249 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003250 if (RegNo <= 0)
3251 // Def is the address writeback.
3252 return ItinData->getOperandCycle(DefClass, DefIdx);
3253
3254 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003255 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003256 // 4 registers would be issued: 1, 2, 1.
3257 // 5 registers would be issued: 1, 2, 2.
3258 DefCycle = RegNo / 2;
3259 if (DefCycle < 1)
3260 DefCycle = 1;
3261 // Result latency is issue cycle + 2: E2.
3262 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003263 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003264 DefCycle = (RegNo / 2);
3265 // If there are odd number of registers or if it's not 64-bit aligned,
3266 // then it takes an extra AGU (Address Generation Unit) cycle.
3267 if ((RegNo % 2) || DefAlign < 8)
3268 ++DefCycle;
3269 // Result latency is AGU cycles + 2.
3270 DefCycle += 2;
3271 } else {
3272 // Assume the worst.
3273 DefCycle = RegNo + 2;
3274 }
3275
3276 return DefCycle;
3277}
3278
3279int
3280ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003281 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003282 unsigned UseClass,
3283 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003284 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003285 if (RegNo <= 0)
3286 return ItinData->getOperandCycle(UseClass, UseIdx);
3287
3288 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003289 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003290 // (regno / 2) + (regno % 2) + 1
3291 UseCycle = RegNo / 2 + 1;
3292 if (RegNo % 2)
3293 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003294 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003295 UseCycle = RegNo;
3296 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003297
Evan Cheng6cc775f2011-06-28 19:10:37 +00003298 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003299 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003300 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003301 case ARM::VSTMSIA_UPD:
3302 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003303 isSStore = true;
3304 break;
3305 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003306
Evan Cheng412e37b2010-10-07 23:12:15 +00003307 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3308 // then it takes an extra cycle.
3309 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3310 ++UseCycle;
3311 } else {
3312 // Assume the worst.
3313 UseCycle = RegNo + 2;
3314 }
3315
3316 return UseCycle;
3317}
3318
3319int
3320ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003321 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003322 unsigned UseClass,
3323 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003324 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003325 if (RegNo <= 0)
3326 return ItinData->getOperandCycle(UseClass, UseIdx);
3327
3328 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003329 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003330 UseCycle = RegNo / 2;
3331 if (UseCycle < 2)
3332 UseCycle = 2;
3333 // Read in E3.
3334 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003335 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003336 UseCycle = (RegNo / 2);
3337 // If there are odd number of registers or if it's not 64-bit aligned,
3338 // then it takes an extra AGU (Address Generation Unit) cycle.
3339 if ((RegNo % 2) || UseAlign < 8)
3340 ++UseCycle;
3341 } else {
3342 // Assume the worst.
3343 UseCycle = 1;
3344 }
3345 return UseCycle;
3346}
3347
3348int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003349ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003350 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003351 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003352 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003353 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003354 unsigned DefClass = DefMCID.getSchedClass();
3355 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003356
Evan Cheng6cc775f2011-06-28 19:10:37 +00003357 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003358 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3359
3360 // This may be a def / use of a variable_ops instruction, the operand
3361 // latency might be determinable dynamically. Let the target try to
3362 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003363 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003364 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003365 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003366 default:
3367 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3368 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003369
3370 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003371 case ARM::VLDMDIA_UPD:
3372 case ARM::VLDMDDB_UPD:
3373 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003374 case ARM::VLDMSIA_UPD:
3375 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003376 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003377 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003378
3379 case ARM::LDMIA_RET:
3380 case ARM::LDMIA:
3381 case ARM::LDMDA:
3382 case ARM::LDMDB:
3383 case ARM::LDMIB:
3384 case ARM::LDMIA_UPD:
3385 case ARM::LDMDA_UPD:
3386 case ARM::LDMDB_UPD:
3387 case ARM::LDMIB_UPD:
3388 case ARM::tLDMIA:
3389 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003390 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003391 case ARM::t2LDMIA_RET:
3392 case ARM::t2LDMIA:
3393 case ARM::t2LDMDB:
3394 case ARM::t2LDMIA_UPD:
3395 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003396 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003397 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003398 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003399 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003400
3401 if (DefCycle == -1)
3402 // We can't seem to determine the result latency of the def, assume it's 2.
3403 DefCycle = 2;
3404
3405 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003406 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003407 default:
3408 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3409 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003410
3411 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003412 case ARM::VSTMDIA_UPD:
3413 case ARM::VSTMDDB_UPD:
3414 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003415 case ARM::VSTMSIA_UPD:
3416 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003417 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003418 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003419
3420 case ARM::STMIA:
3421 case ARM::STMDA:
3422 case ARM::STMDB:
3423 case ARM::STMIB:
3424 case ARM::STMIA_UPD:
3425 case ARM::STMDA_UPD:
3426 case ARM::STMDB_UPD:
3427 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003428 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003429 case ARM::tPOP_RET:
3430 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003431 case ARM::t2STMIA:
3432 case ARM::t2STMDB:
3433 case ARM::t2STMIA_UPD:
3434 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003435 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003436 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003437 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003438
3439 if (UseCycle == -1)
3440 // Assume it's read in the first stage.
3441 UseCycle = 1;
3442
3443 UseCycle = DefCycle - UseCycle + 1;
3444 if (UseCycle > 0) {
3445 if (LdmBypass) {
3446 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3447 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003448 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003449 UseClass, UseIdx))
3450 --UseCycle;
3451 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003452 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003453 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003454 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003455 }
3456
3457 return UseCycle;
3458}
3459
Evan Cheng7fae11b2011-12-14 02:11:42 +00003460static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003461 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003462 unsigned &DefIdx, unsigned &Dist) {
3463 Dist = 0;
3464
3465 MachineBasicBlock::const_iterator I = MI; ++I;
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +00003466 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003467 assert(II->isInsideBundle() && "Empty bundle?");
3468
3469 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003470 while (II->isInsideBundle()) {
3471 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3472 if (Idx != -1)
3473 break;
3474 --II;
3475 ++Dist;
3476 }
3477
3478 assert(Idx != -1 && "Cannot find bundled definition!");
3479 DefIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003480 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003481}
3482
3483static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003484 const MachineInstr &MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003485 unsigned &UseIdx, unsigned &Dist) {
3486 Dist = 0;
3487
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003488 MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003489 assert(II->isInsideBundle() && "Empty bundle?");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003490 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00003491
3492 // FIXME: This doesn't properly handle multiple uses.
3493 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003494 while (II != E && II->isInsideBundle()) {
3495 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3496 if (Idx != -1)
3497 break;
3498 if (II->getOpcode() != ARM::t2IT)
3499 ++Dist;
3500 ++II;
3501 }
3502
Evan Chengda103bf2011-12-14 20:00:08 +00003503 if (Idx == -1) {
3504 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003505 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003506 }
3507
Evan Cheng7fae11b2011-12-14 02:11:42 +00003508 UseIdx = Idx;
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003509 return &*II;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003510}
3511
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003512/// Return the number of cycles to add to (or subtract from) the static
3513/// itinerary based on the def opcode and alignment. The caller will ensure that
3514/// adjusted latency is at least one cycle.
3515static int adjustDefLatency(const ARMSubtarget &Subtarget,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003516 const MachineInstr &DefMI,
3517 const MCInstrDesc &DefMCID, unsigned DefAlign) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003518 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003519 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003520 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3521 // variants are one cycle cheaper.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003522 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003523 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003524 case ARM::LDRrs:
3525 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003526 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003527 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3528 if (ShImm == 0 ||
3529 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003530 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003531 break;
3532 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003533 case ARM::t2LDRs:
3534 case ARM::t2LDRBs:
3535 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003536 case ARM::t2LDRSHs: {
3537 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003538 unsigned ShAmt = DefMI.getOperand(3).getImm();
Evan Chengff310732010-10-28 06:47:08 +00003539 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003540 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003541 break;
3542 }
3543 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003544 } else if (Subtarget.isSwift()) {
3545 // FIXME: Properly handle all of the latency adjustments for address
3546 // writeback.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003547 switch (DefMCID.getOpcode()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00003548 default: break;
3549 case ARM::LDRrs:
3550 case ARM::LDRBrs: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003551 unsigned ShOpVal = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003552 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3553 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3554 if (!isSub &&
3555 (ShImm == 0 ||
3556 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3557 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3558 Adjust -= 2;
3559 else if (!isSub &&
3560 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3561 --Adjust;
3562 break;
3563 }
3564 case ARM::t2LDRs:
3565 case ARM::t2LDRBs:
3566 case ARM::t2LDRHs:
3567 case ARM::t2LDRSHs: {
3568 // Thumb2 mode: lsl only.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003569 unsigned ShAmt = DefMI.getOperand(3).getImm();
Bob Wilsone8a549c2012-09-29 21:43:49 +00003570 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3571 Adjust -= 2;
3572 break;
3573 }
3574 }
Evan Chengff310732010-10-28 06:47:08 +00003575 }
3576
Diana Picus92423ce2016-06-27 09:08:23 +00003577 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003578 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003579 default: break;
3580 case ARM::VLD1q8:
3581 case ARM::VLD1q16:
3582 case ARM::VLD1q32:
3583 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003584 case ARM::VLD1q8wb_fixed:
3585 case ARM::VLD1q16wb_fixed:
3586 case ARM::VLD1q32wb_fixed:
3587 case ARM::VLD1q64wb_fixed:
3588 case ARM::VLD1q8wb_register:
3589 case ARM::VLD1q16wb_register:
3590 case ARM::VLD1q32wb_register:
3591 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003592 case ARM::VLD2d8:
3593 case ARM::VLD2d16:
3594 case ARM::VLD2d32:
3595 case ARM::VLD2q8:
3596 case ARM::VLD2q16:
3597 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003598 case ARM::VLD2d8wb_fixed:
3599 case ARM::VLD2d16wb_fixed:
3600 case ARM::VLD2d32wb_fixed:
3601 case ARM::VLD2q8wb_fixed:
3602 case ARM::VLD2q16wb_fixed:
3603 case ARM::VLD2q32wb_fixed:
3604 case ARM::VLD2d8wb_register:
3605 case ARM::VLD2d16wb_register:
3606 case ARM::VLD2d32wb_register:
3607 case ARM::VLD2q8wb_register:
3608 case ARM::VLD2q16wb_register:
3609 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003610 case ARM::VLD3d8:
3611 case ARM::VLD3d16:
3612 case ARM::VLD3d32:
3613 case ARM::VLD1d64T:
3614 case ARM::VLD3d8_UPD:
3615 case ARM::VLD3d16_UPD:
3616 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003617 case ARM::VLD1d64Twb_fixed:
3618 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003619 case ARM::VLD3q8_UPD:
3620 case ARM::VLD3q16_UPD:
3621 case ARM::VLD3q32_UPD:
3622 case ARM::VLD4d8:
3623 case ARM::VLD4d16:
3624 case ARM::VLD4d32:
3625 case ARM::VLD1d64Q:
3626 case ARM::VLD4d8_UPD:
3627 case ARM::VLD4d16_UPD:
3628 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003629 case ARM::VLD1d64Qwb_fixed:
3630 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003631 case ARM::VLD4q8_UPD:
3632 case ARM::VLD4q16_UPD:
3633 case ARM::VLD4q32_UPD:
3634 case ARM::VLD1DUPq8:
3635 case ARM::VLD1DUPq16:
3636 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003637 case ARM::VLD1DUPq8wb_fixed:
3638 case ARM::VLD1DUPq16wb_fixed:
3639 case ARM::VLD1DUPq32wb_fixed:
3640 case ARM::VLD1DUPq8wb_register:
3641 case ARM::VLD1DUPq16wb_register:
3642 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003643 case ARM::VLD2DUPd8:
3644 case ARM::VLD2DUPd16:
3645 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003646 case ARM::VLD2DUPd8wb_fixed:
3647 case ARM::VLD2DUPd16wb_fixed:
3648 case ARM::VLD2DUPd32wb_fixed:
3649 case ARM::VLD2DUPd8wb_register:
3650 case ARM::VLD2DUPd16wb_register:
3651 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003652 case ARM::VLD4DUPd8:
3653 case ARM::VLD4DUPd16:
3654 case ARM::VLD4DUPd32:
3655 case ARM::VLD4DUPd8_UPD:
3656 case ARM::VLD4DUPd16_UPD:
3657 case ARM::VLD4DUPd32_UPD:
3658 case ARM::VLD1LNd8:
3659 case ARM::VLD1LNd16:
3660 case ARM::VLD1LNd32:
3661 case ARM::VLD1LNd8_UPD:
3662 case ARM::VLD1LNd16_UPD:
3663 case ARM::VLD1LNd32_UPD:
3664 case ARM::VLD2LNd8:
3665 case ARM::VLD2LNd16:
3666 case ARM::VLD2LNd32:
3667 case ARM::VLD2LNq16:
3668 case ARM::VLD2LNq32:
3669 case ARM::VLD2LNd8_UPD:
3670 case ARM::VLD2LNd16_UPD:
3671 case ARM::VLD2LNd32_UPD:
3672 case ARM::VLD2LNq16_UPD:
3673 case ARM::VLD2LNq32_UPD:
3674 case ARM::VLD4LNd8:
3675 case ARM::VLD4LNd16:
3676 case ARM::VLD4LNd32:
3677 case ARM::VLD4LNq16:
3678 case ARM::VLD4LNq32:
3679 case ARM::VLD4LNd8_UPD:
3680 case ARM::VLD4LNd16_UPD:
3681 case ARM::VLD4LNd32_UPD:
3682 case ARM::VLD4LNq16_UPD:
3683 case ARM::VLD4LNq32_UPD:
3684 // If the address is not 64-bit aligned, the latencies of these
3685 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003686 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003687 break;
3688 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003689 }
3690 return Adjust;
3691}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003692
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003693int ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3694 const MachineInstr &DefMI,
3695 unsigned DefIdx,
3696 const MachineInstr &UseMI,
3697 unsigned UseIdx) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003698 // No operand latency. The caller may fall back to getInstrLatency.
3699 if (!ItinData || ItinData->isEmpty())
3700 return -1;
3701
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003702 const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003703 unsigned Reg = DefMO.getReg();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003704
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003705 const MachineInstr *ResolvedDefMI = &DefMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003706 unsigned DefAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003707 if (DefMI.isBundle())
3708 ResolvedDefMI =
3709 getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
3710 if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
3711 ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003712 return 1;
3713 }
3714
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003715 const MachineInstr *ResolvedUseMI = &UseMI;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003716 unsigned UseAdj = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003717 if (UseMI.isBundle()) {
3718 ResolvedUseMI =
3719 getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
3720 if (!ResolvedUseMI)
Andrew Trick77d0b882012-06-22 02:50:33 +00003721 return -1;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003722 }
3723
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003724 return getOperandLatencyImpl(
3725 ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
3726 Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
3727}
3728
3729int ARMBaseInstrInfo::getOperandLatencyImpl(
3730 const InstrItineraryData *ItinData, const MachineInstr &DefMI,
3731 unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
3732 const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
3733 unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003734 if (Reg == ARM::CPSR) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003735 if (DefMI.getOpcode() == ARM::FMSTAT) {
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003736 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003737 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003738 }
3739
3740 // CPSR set and branch can be paired in the same cycle.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003741 if (UseMI.isBranch())
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003742 return 0;
3743
3744 // Otherwise it takes the instruction latency (generally one).
3745 unsigned Latency = getInstrLatency(ItinData, DefMI);
3746
3747 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3748 // its uses. Instructions which are otherwise scheduled between them may
3749 // incur a code size penalty (not able to use the CPSR setting 16-bit
3750 // instructions).
3751 if (Latency > 0 && Subtarget.isThumb2()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003752 const MachineFunction *MF = DefMI.getParent()->getParent();
Sanjay Patel924879a2015-08-04 15:49:57 +00003753 // FIXME: Use Function::optForSize().
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00003754 if (MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003755 --Latency;
3756 }
3757 return Latency;
3758 }
3759
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003760 if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
Andrew Trick77d0b882012-06-22 02:50:33 +00003761 return -1;
3762
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003763 unsigned DefAlign = DefMI.hasOneMemOperand()
3764 ? (*DefMI.memoperands_begin())->getAlignment()
3765 : 0;
3766 unsigned UseAlign = UseMI.hasOneMemOperand()
3767 ? (*UseMI.memoperands_begin())->getAlignment()
3768 : 0;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003769
3770 // Get the itinerary's latency if possible, and handle variable_ops.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00003771 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign, UseMCID,
3772 UseIdx, UseAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003773 // Unable to find operand latency. The caller may resort to getInstrLatency.
3774 if (Latency < 0)
3775 return Latency;
3776
3777 // Adjust for IT block position.
3778 int Adj = DefAdj + UseAdj;
3779
3780 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3781 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3782 if (Adj >= 0 || (int)Latency > -Adj) {
3783 return Latency + Adj;
3784 }
3785 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003786 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003787}
3788
3789int
3790ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3791 SDNode *DefNode, unsigned DefIdx,
3792 SDNode *UseNode, unsigned UseIdx) const {
3793 if (!DefNode->isMachineOpcode())
3794 return 1;
3795
Evan Cheng6cc775f2011-06-28 19:10:37 +00003796 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003797
Evan Cheng6cc775f2011-06-28 19:10:37 +00003798 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003799 return 0;
3800
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003801 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003802 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003803
Evan Cheng6c1414f2010-10-29 18:09:28 +00003804 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003805 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Diana Picus92423ce2016-06-27 09:08:23 +00003806 int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
3807 int Threshold = 1 + Adj;
3808 return Latency <= Threshold ? 1 : Latency - Adj;
Evan Cheng6c1414f2010-10-29 18:09:28 +00003809 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003810
Evan Cheng6cc775f2011-06-28 19:10:37 +00003811 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003812 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3813 unsigned DefAlign = !DefMN->memoperands_empty()
3814 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3815 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3816 unsigned UseAlign = !UseMN->memoperands_empty()
3817 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003818 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3819 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003820
3821 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003822 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3823 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003824 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3825 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003826 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003827 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003828 case ARM::LDRrs:
3829 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003830 unsigned ShOpVal =
3831 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3832 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3833 if (ShImm == 0 ||
3834 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3835 --Latency;
3836 break;
3837 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003838 case ARM::t2LDRs:
3839 case ARM::t2LDRBs:
3840 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003841 case ARM::t2LDRSHs: {
3842 // Thumb2 mode: lsl only.
3843 unsigned ShAmt =
3844 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3845 if (ShAmt == 0 || ShAmt == 2)
3846 --Latency;
3847 break;
3848 }
3849 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003850 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3851 // FIXME: Properly handle all of the latency adjustments for address
3852 // writeback.
3853 switch (DefMCID.getOpcode()) {
3854 default: break;
3855 case ARM::LDRrs:
3856 case ARM::LDRBrs: {
3857 unsigned ShOpVal =
3858 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3859 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3860 if (ShImm == 0 ||
3861 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3862 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3863 Latency -= 2;
3864 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3865 --Latency;
3866 break;
3867 }
3868 case ARM::t2LDRs:
3869 case ARM::t2LDRBs:
3870 case ARM::t2LDRHs:
3871 case ARM::t2LDRSHs: {
3872 // Thumb2 mode: lsl 0-3 only.
3873 Latency -= 2;
3874 break;
3875 }
3876 }
Evan Chengff310732010-10-28 06:47:08 +00003877 }
3878
Diana Picus92423ce2016-06-27 09:08:23 +00003879 if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003880 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003881 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003882 case ARM::VLD1q8:
3883 case ARM::VLD1q16:
3884 case ARM::VLD1q32:
3885 case ARM::VLD1q64:
3886 case ARM::VLD1q8wb_register:
3887 case ARM::VLD1q16wb_register:
3888 case ARM::VLD1q32wb_register:
3889 case ARM::VLD1q64wb_register:
3890 case ARM::VLD1q8wb_fixed:
3891 case ARM::VLD1q16wb_fixed:
3892 case ARM::VLD1q32wb_fixed:
3893 case ARM::VLD1q64wb_fixed:
3894 case ARM::VLD2d8:
3895 case ARM::VLD2d16:
3896 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003897 case ARM::VLD2q8Pseudo:
3898 case ARM::VLD2q16Pseudo:
3899 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003900 case ARM::VLD2d8wb_fixed:
3901 case ARM::VLD2d16wb_fixed:
3902 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003903 case ARM::VLD2q8PseudoWB_fixed:
3904 case ARM::VLD2q16PseudoWB_fixed:
3905 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003906 case ARM::VLD2d8wb_register:
3907 case ARM::VLD2d16wb_register:
3908 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003909 case ARM::VLD2q8PseudoWB_register:
3910 case ARM::VLD2q16PseudoWB_register:
3911 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003912 case ARM::VLD3d8Pseudo:
3913 case ARM::VLD3d16Pseudo:
3914 case ARM::VLD3d32Pseudo:
3915 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003916 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003917 case ARM::VLD3d8Pseudo_UPD:
3918 case ARM::VLD3d16Pseudo_UPD:
3919 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003920 case ARM::VLD3q8Pseudo_UPD:
3921 case ARM::VLD3q16Pseudo_UPD:
3922 case ARM::VLD3q32Pseudo_UPD:
3923 case ARM::VLD3q8oddPseudo:
3924 case ARM::VLD3q16oddPseudo:
3925 case ARM::VLD3q32oddPseudo:
3926 case ARM::VLD3q8oddPseudo_UPD:
3927 case ARM::VLD3q16oddPseudo_UPD:
3928 case ARM::VLD3q32oddPseudo_UPD:
3929 case ARM::VLD4d8Pseudo:
3930 case ARM::VLD4d16Pseudo:
3931 case ARM::VLD4d32Pseudo:
3932 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003933 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003934 case ARM::VLD4d8Pseudo_UPD:
3935 case ARM::VLD4d16Pseudo_UPD:
3936 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003937 case ARM::VLD4q8Pseudo_UPD:
3938 case ARM::VLD4q16Pseudo_UPD:
3939 case ARM::VLD4q32Pseudo_UPD:
3940 case ARM::VLD4q8oddPseudo:
3941 case ARM::VLD4q16oddPseudo:
3942 case ARM::VLD4q32oddPseudo:
3943 case ARM::VLD4q8oddPseudo_UPD:
3944 case ARM::VLD4q16oddPseudo_UPD:
3945 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003946 case ARM::VLD1DUPq8:
3947 case ARM::VLD1DUPq16:
3948 case ARM::VLD1DUPq32:
3949 case ARM::VLD1DUPq8wb_fixed:
3950 case ARM::VLD1DUPq16wb_fixed:
3951 case ARM::VLD1DUPq32wb_fixed:
3952 case ARM::VLD1DUPq8wb_register:
3953 case ARM::VLD1DUPq16wb_register:
3954 case ARM::VLD1DUPq32wb_register:
3955 case ARM::VLD2DUPd8:
3956 case ARM::VLD2DUPd16:
3957 case ARM::VLD2DUPd32:
3958 case ARM::VLD2DUPd8wb_fixed:
3959 case ARM::VLD2DUPd16wb_fixed:
3960 case ARM::VLD2DUPd32wb_fixed:
3961 case ARM::VLD2DUPd8wb_register:
3962 case ARM::VLD2DUPd16wb_register:
3963 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003964 case ARM::VLD4DUPd8Pseudo:
3965 case ARM::VLD4DUPd16Pseudo:
3966 case ARM::VLD4DUPd32Pseudo:
3967 case ARM::VLD4DUPd8Pseudo_UPD:
3968 case ARM::VLD4DUPd16Pseudo_UPD:
3969 case ARM::VLD4DUPd32Pseudo_UPD:
3970 case ARM::VLD1LNq8Pseudo:
3971 case ARM::VLD1LNq16Pseudo:
3972 case ARM::VLD1LNq32Pseudo:
3973 case ARM::VLD1LNq8Pseudo_UPD:
3974 case ARM::VLD1LNq16Pseudo_UPD:
3975 case ARM::VLD1LNq32Pseudo_UPD:
3976 case ARM::VLD2LNd8Pseudo:
3977 case ARM::VLD2LNd16Pseudo:
3978 case ARM::VLD2LNd32Pseudo:
3979 case ARM::VLD2LNq16Pseudo:
3980 case ARM::VLD2LNq32Pseudo:
3981 case ARM::VLD2LNd8Pseudo_UPD:
3982 case ARM::VLD2LNd16Pseudo_UPD:
3983 case ARM::VLD2LNd32Pseudo_UPD:
3984 case ARM::VLD2LNq16Pseudo_UPD:
3985 case ARM::VLD2LNq32Pseudo_UPD:
3986 case ARM::VLD4LNd8Pseudo:
3987 case ARM::VLD4LNd16Pseudo:
3988 case ARM::VLD4LNd32Pseudo:
3989 case ARM::VLD4LNq16Pseudo:
3990 case ARM::VLD4LNq32Pseudo:
3991 case ARM::VLD4LNd8Pseudo_UPD:
3992 case ARM::VLD4LNd16Pseudo_UPD:
3993 case ARM::VLD4LNd32Pseudo_UPD:
3994 case ARM::VLD4LNq16Pseudo_UPD:
3995 case ARM::VLD4LNq32Pseudo_UPD:
3996 // If the address is not 64-bit aligned, the latencies of these
3997 // instructions increases by one.
3998 ++Latency;
3999 break;
4000 }
4001
Evan Chengff310732010-10-28 06:47:08 +00004002 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00004003}
Evan Cheng63c76082010-10-19 18:58:51 +00004004
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004005unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
4006 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4007 MI.isImplicitDef())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004008 return 0;
4009
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004010 if (MI.isBundle())
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004011 return 0;
4012
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00004013 const MCInstrDesc &MCID = MI.getDesc();
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00004014
4015 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
4016 // When predicated, CPSR is an additional source operand for CPSR updating
4017 // instructions, this apparently increases their latencies.
4018 return 1;
4019 }
4020 return 0;
4021}
4022
Andrew Trick45446062012-06-05 21:11:27 +00004023unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004024 const MachineInstr &MI,
Andrew Trick45446062012-06-05 21:11:27 +00004025 unsigned *PredCost) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004026 if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
4027 MI.isImplicitDef())
Evan Chengdebf9c52010-11-03 00:45:17 +00004028 return 1;
4029
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004030 // An instruction scheduler typically runs on unbundled instructions, however
4031 // other passes may query the latency of a bundled instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004032 if (MI.isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004033 unsigned Latency = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004034 MachineBasicBlock::const_instr_iterator I = MI.getIterator();
4035 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
Evan Cheng7fae11b2011-12-14 02:11:42 +00004036 while (++I != E && I->isInsideBundle()) {
4037 if (I->getOpcode() != ARM::t2IT)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004038 Latency += getInstrLatency(ItinData, *I, PredCost);
Evan Cheng7fae11b2011-12-14 02:11:42 +00004039 }
4040 return Latency;
4041 }
4042
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004043 const MCInstrDesc &MCID = MI.getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004044 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00004045 // When predicated, CPSR is an additional source operand for CPSR updating
4046 // instructions, this apparently increases their latencies.
4047 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004048 }
4049 // Be sure to call getStageLatency for an empty itinerary in case it has a
4050 // valid MinLatency property.
4051 if (!ItinData)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004052 return MI.mayLoad() ? 3 : 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004053
4054 unsigned Class = MCID.getSchedClass();
4055
4056 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00004057 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004058 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00004059
Andrew Trickfb1a74c2012-06-07 19:41:55 +00004060 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004061 unsigned Latency = ItinData->getStageLatency(Class);
4062
4063 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004064 unsigned DefAlign =
4065 MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlignment() : 0;
4066 int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
Andrew Trick5b1cadf2012-06-07 19:42:00 +00004067 if (Adj >= 0 || (int)Latency > -Adj) {
4068 return Latency + Adj;
4069 }
4070 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00004071}
4072
4073int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
4074 SDNode *Node) const {
4075 if (!Node->isMachineOpcode())
4076 return 1;
4077
4078 if (!ItinData || ItinData->isEmpty())
4079 return 1;
4080
4081 unsigned Opcode = Node->getMachineOpcode();
4082 switch (Opcode) {
4083 default:
4084 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004085 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00004086 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00004087 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00004088 }
Evan Chengdebf9c52010-11-03 00:45:17 +00004089}
4090
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004091bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
4092 const MachineRegisterInfo *MRI,
4093 const MachineInstr &DefMI,
4094 unsigned DefIdx,
4095 const MachineInstr &UseMI,
4096 unsigned UseIdx) const {
4097 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
4098 unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
Diana Picus92423ce2016-06-27 09:08:23 +00004099 if (Subtarget.nonpipelinedVFP() &&
Evan Cheng63c76082010-10-19 18:58:51 +00004100 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
Evan Cheng63c76082010-10-19 18:58:51 +00004101 return true;
4102
4103 // Hoist VFP / NEON instructions with 4 or higher latency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004104 unsigned Latency =
4105 SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
Evan Cheng63c76082010-10-19 18:58:51 +00004106 if (Latency <= 3)
4107 return false;
4108 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
4109 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
4110}
Evan Chenge96b8d72010-10-26 02:08:50 +00004111
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004112bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
4113 const MachineInstr &DefMI,
4114 unsigned DefIdx) const {
Matthias Braun88e21312015-06-13 03:42:11 +00004115 const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
Evan Chenge96b8d72010-10-26 02:08:50 +00004116 if (!ItinData || ItinData->isEmpty())
4117 return false;
4118
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004119 unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
Evan Chenge96b8d72010-10-26 02:08:50 +00004120 if (DDomain == ARMII::DomainGeneral) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004121 unsigned DefClass = DefMI.getDesc().getSchedClass();
Evan Chenge96b8d72010-10-26 02:08:50 +00004122 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
4123 return (DefCycle != -1 && DefCycle <= 2);
4124 }
4125 return false;
4126}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004127
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004128bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
Andrew Trick924123a2011-09-21 02:20:46 +00004129 StringRef &ErrInfo) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004130 if (convertAddSubFlagsOpcode(MI.getOpcode())) {
Andrew Trick924123a2011-09-21 02:20:46 +00004131 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
4132 return false;
4133 }
4134 return true;
4135}
4136
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004137// LoadStackGuard has so far only been implemented for MachO. Different code
4138// sequence is needed for other targets.
4139void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
4140 unsigned LoadImmOpc,
Rafael Espindola82f46312016-06-28 15:18:26 +00004141 unsigned LoadOpc) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00004142 assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
4143 "ROPI/RWPI not currently supported with stack guard");
4144
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004145 MachineBasicBlock &MBB = *MI->getParent();
4146 DebugLoc DL = MI->getDebugLoc();
4147 unsigned Reg = MI->getOperand(0).getReg();
4148 const GlobalValue *GV =
4149 cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4150 MachineInstrBuilder MIB;
4151
4152 BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4153 .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
4154
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00004155 if (Subtarget.isGVIndirectSymbol(GV)) {
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004156 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4157 MIB.addReg(Reg, RegState::Kill).addImm(0);
Justin Lebaradbf09e2016-09-11 01:38:58 +00004158 auto Flags = MachineMemOperand::MOLoad |
4159 MachineMemOperand::MODereferenceable |
4160 MachineMemOperand::MOInvariant;
Alex Lorenze40c8a22015-08-11 23:09:45 +00004161 MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
Justin Lebar0af80cd2016-07-15 18:26:59 +00004162 MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, 4);
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +00004163 MIB.addMemOperand(MMO);
4164 AddDefaultPred(MIB);
4165 }
4166
4167 MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
4168 MIB.addReg(Reg, RegState::Kill).addImm(0);
4169 MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
4170 AddDefaultPred(MIB);
4171}
4172
Evan Cheng62c7b5b2010-12-05 22:04:16 +00004173bool
4174ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
4175 unsigned &AddSubOpc,
4176 bool &NegAcc, bool &HasLane) const {
4177 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
4178 if (I == MLxEntryMap.end())
4179 return false;
4180
4181 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
4182 MulOpc = Entry.MulOpc;
4183 AddSubOpc = Entry.AddSubOpc;
4184 NegAcc = Entry.NegAcc;
4185 HasLane = Entry.HasLane;
4186 return true;
4187}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004188
4189//===----------------------------------------------------------------------===//
4190// Execution domains.
4191//===----------------------------------------------------------------------===//
4192//
4193// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
4194// and some can go down both. The vmov instructions go down the VFP pipeline,
4195// but they can be changed to vorr equivalents that are executed by the NEON
4196// pipeline.
4197//
4198// We use the following execution domain numbering:
4199//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004200enum ARMExeDomain {
4201 ExeGeneric = 0,
4202 ExeVFP = 1,
4203 ExeNEON = 2
4204};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004205//
4206// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
4207//
4208std::pair<uint16_t, uint16_t>
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004209ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
Eric Christopher7e70aba2015-03-07 00:12:22 +00004210 // If we don't have access to NEON instructions then we won't be able
4211 // to swizzle anything to the NEON domain. Check to make sure.
4212 if (Subtarget.hasNEON()) {
4213 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
4214 // if they are not predicated.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004215 if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004216 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004217
Eric Christopher7e70aba2015-03-07 00:12:22 +00004218 // CortexA9 is particularly picky about mixing the two and wants these
4219 // converted.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004220 if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
4221 (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
4222 MI.getOpcode() == ARM::VMOVS))
Eric Christopher7e70aba2015-03-07 00:12:22 +00004223 return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
4224 }
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004225 // No other instructions can be swizzled, so just determine their domain.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004226 unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004227
4228 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004229 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004230
4231 // Certain instructions can go either way on Cortex-A8.
4232 // Treat them as NEON instructions.
4233 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004234 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004235
4236 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004237 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004238
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004239 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004240}
4241
Tim Northover771f1602012-08-29 16:36:07 +00004242static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4243 unsigned SReg, unsigned &Lane) {
4244 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4245 Lane = 0;
4246
4247 if (DReg != ARM::NoRegister)
4248 return DReg;
4249
4250 Lane = 1;
4251 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4252
4253 assert(DReg && "S-register with no D super-register?");
4254 return DReg;
4255}
4256
Andrew Trickd9296ec2012-10-10 05:43:01 +00004257/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004258/// set ImplicitSReg to a register number that must be marked as implicit-use or
4259/// zero if no register needs to be defined as implicit-use.
4260///
4261/// If the function cannot determine if an SPR should be marked implicit use or
4262/// not, it returns false.
4263///
4264/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004265/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004266/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4267/// lane of the DPR).
4268///
4269/// If the other SPR is defined, an implicit-use of it should be added. Else,
4270/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004271///
James Molloyea052562012-09-18 08:31:15 +00004272static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004273 MachineInstr &MI, unsigned DReg,
4274 unsigned Lane, unsigned &ImplicitSReg) {
James Molloyea052562012-09-18 08:31:15 +00004275 // If the DPR is defined or used already, the other SPR lane will be chained
4276 // correctly, so there is nothing to be done.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004277 if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
James Molloyea052562012-09-18 08:31:15 +00004278 ImplicitSReg = 0;
4279 return true;
4280 }
4281
4282 // Otherwise we need to go searching to see if the SPR is set explicitly.
4283 ImplicitSReg = TRI->getSubReg(DReg,
4284 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4285 MachineBasicBlock::LivenessQueryResult LQR =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004286 MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
James Molloyea052562012-09-18 08:31:15 +00004287
4288 if (LQR == MachineBasicBlock::LQR_Live)
4289 return true;
4290 else if (LQR == MachineBasicBlock::LQR_Unknown)
4291 return false;
4292
4293 // If the register is known not to be live, there is no need to add an
4294 // implicit-use.
4295 ImplicitSReg = 0;
4296 return true;
4297}
Tim Northover771f1602012-08-29 16:36:07 +00004298
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004299void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
4300 unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004301 unsigned DstReg, SrcReg, DReg;
4302 unsigned Lane;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004303 MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004304 const TargetRegisterInfo *TRI = &getRegisterInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004305 switch (MI.getOpcode()) {
4306 default:
4307 llvm_unreachable("cannot handle opcode!");
4308 break;
4309 case ARM::VMOVD:
4310 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004311 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004312
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004313 // Zap the predicate operands.
4314 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004315
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004316 // Make sure we've got NEON instructions.
4317 assert(Subtarget.hasNEON() && "VORRd requires NEON");
Eric Christopher7e70aba2015-03-07 00:12:22 +00004318
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004319 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4320 DstReg = MI.getOperand(0).getReg();
4321 SrcReg = MI.getOperand(1).getReg();
Tim Northover771f1602012-08-29 16:36:07 +00004322
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004323 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4324 MI.RemoveOperand(i - 1);
Tim Northover771f1602012-08-29 16:36:07 +00004325
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004326 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
4327 MI.setDesc(get(ARM::VORRd));
4328 AddDefaultPred(
4329 MIB.addReg(DstReg, RegState::Define).addReg(SrcReg).addReg(SrcReg));
4330 break;
4331 case ARM::VMOVRS:
4332 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004333 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004334 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004335
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004336 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
4337 DstReg = MI.getOperand(0).getReg();
4338 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004339
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004340 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4341 MI.RemoveOperand(i - 1);
Tim Northoverf6618152012-08-17 11:32:52 +00004342
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004343 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004344
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004345 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4346 // Note that DSrc has been widened and the other lane may be undef, which
4347 // contaminates the entire register.
4348 MI.setDesc(get(ARM::VGETLNi32));
4349 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4350 .addReg(DReg, RegState::Undef)
4351 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004352
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004353 // The old source should be an implicit use, otherwise we might think it
4354 // was dead before here.
4355 MIB.addReg(SrcReg, RegState::Implicit);
4356 break;
4357 case ARM::VMOVSR: {
4358 if (Domain != ExeNEON)
Tim Northoverf6618152012-08-17 11:32:52 +00004359 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004360 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
Tim Northoverf6618152012-08-17 11:32:52 +00004361
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004362 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
4363 DstReg = MI.getOperand(0).getReg();
4364 SrcReg = MI.getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004365
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004366 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
Tim Northover771f1602012-08-29 16:36:07 +00004367
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004368 unsigned ImplicitSReg;
4369 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
Tim Northoverf6618152012-08-17 11:32:52 +00004370 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004371
4372 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4373 MI.RemoveOperand(i - 1);
4374
4375 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4376 // Again DDst may be undefined at the beginning of this instruction.
4377 MI.setDesc(get(ARM::VSETLNi32));
4378 MIB.addReg(DReg, RegState::Define)
4379 .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
4380 .addReg(SrcReg)
4381 .addImm(Lane);
4382 AddDefaultPred(MIB);
4383
4384 // The narrower destination must be marked as set to keep previous chains
4385 // in place.
4386 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
4387 if (ImplicitSReg != 0)
4388 MIB.addReg(ImplicitSReg, RegState::Implicit);
4389 break;
James Molloyea052562012-09-18 08:31:15 +00004390 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004391 case ARM::VMOVS: {
4392 if (Domain != ExeNEON)
4393 break;
4394
4395 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004396 DstReg = MI.getOperand(0).getReg();
4397 SrcReg = MI.getOperand(1).getReg();
Tim Northoverca9f3842012-08-30 10:17:45 +00004398
Tim Northoverca9f3842012-08-30 10:17:45 +00004399 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4400 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4401 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4402
James Molloyea052562012-09-18 08:31:15 +00004403 unsigned ImplicitSReg;
4404 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4405 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004406
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004407 for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
4408 MI.RemoveOperand(i - 1);
Tim Northoverc8d867d2012-09-05 18:37:53 +00004409
Tim Northoverca9f3842012-08-30 10:17:45 +00004410 if (DSrc == DDst) {
4411 // Destination can be:
4412 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004413 MI.setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004414 MIB.addReg(DDst, RegState::Define)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004415 .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
4416 .addImm(SrcLane);
Tim Northover726d32c2012-09-01 18:07:29 +00004417 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004418
4419 // Neither the source or the destination are naturally represented any
4420 // more, so add them in manually.
4421 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4422 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004423 if (ImplicitSReg != 0)
4424 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004425 break;
4426 }
4427
4428 // In general there's no single instruction that can perform an S <-> S
4429 // move in NEON space, but a pair of VEXT instructions *can* do the
4430 // job. It turns out that the VEXTs needed will only use DSrc once, with
4431 // the position based purely on the combination of lane-0 and lane-1
4432 // involved. For example
4433 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4434 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4435 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4436 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4437 //
4438 // Pattern of the MachineInstrs is:
4439 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4440 MachineInstrBuilder NewMIB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004441 NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
4442 DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004443
4444 // On the first instruction, both DSrc and DDst may be <undef> if present.
4445 // Specifically when the original instruction didn't have them as an
4446 // <imp-use>.
4447 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004448 bool CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004449 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4450
4451 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004452 CurUndef = !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004453 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4454
Tim Northoverca9f3842012-08-30 10:17:45 +00004455 NewMIB.addImm(1);
4456 AddDefaultPred(NewMIB);
4457
4458 if (SrcLane == DstLane)
4459 NewMIB.addReg(SrcReg, RegState::Implicit);
4460
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004461 MI.setDesc(get(ARM::VEXTd32));
Tim Northoverca9f3842012-08-30 10:17:45 +00004462 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004463
4464 // On the second instruction, DDst has definitely been defined above, so
4465 // it is not <undef>. DSrc, if present, can be <undef> as above.
4466 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004467 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004468 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4469
4470 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004471 CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
Tim Northover726d32c2012-09-01 18:07:29 +00004472 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4473
Tim Northoverca9f3842012-08-30 10:17:45 +00004474 MIB.addImm(1);
4475 AddDefaultPred(MIB);
4476
4477 if (SrcLane != DstLane)
4478 MIB.addReg(SrcReg, RegState::Implicit);
4479
4480 // As before, the original destination is no longer represented, add it
4481 // implicitly.
4482 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004483 if (ImplicitSReg != 0)
4484 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004485 break;
4486 }
Tim Northoverf6618152012-08-17 11:32:52 +00004487 }
4488
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004489}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004490
Bob Wilsone8a549c2012-09-29 21:43:49 +00004491//===----------------------------------------------------------------------===//
4492// Partial register updates
4493//===----------------------------------------------------------------------===//
4494//
4495// Swift renames NEON registers with 64-bit granularity. That means any
4496// instruction writing an S-reg implicitly reads the containing D-reg. The
4497// problem is mostly avoided by translating f32 operations to v2f32 operations
4498// on D-registers, but f32 loads are still a problem.
4499//
4500// These instructions can load an f32 into a NEON register:
4501//
4502// VLDRS - Only writes S, partial D update.
4503// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4504// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4505//
4506// FCONSTD can be used as a dependency-breaking instruction.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004507unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
4508 const MachineInstr &MI, unsigned OpNum,
4509 const TargetRegisterInfo *TRI) const {
Diana Picusb772e402016-07-06 11:22:11 +00004510 auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
4511 if (!PartialUpdateClearance)
Bob Wilsone8a549c2012-09-29 21:43:49 +00004512 return 0;
4513
4514 assert(TRI && "Need TRI instance");
4515
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004516 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004517 if (MO.readsReg())
4518 return 0;
4519 unsigned Reg = MO.getReg();
4520 int UseOp = -1;
4521
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004522 switch (MI.getOpcode()) {
4523 // Normal instructions writing only an S-register.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004524 case ARM::VLDRS:
4525 case ARM::FCONSTS:
4526 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004527 case ARM::VMOVv8i8:
4528 case ARM::VMOVv4i16:
4529 case ARM::VMOVv2i32:
4530 case ARM::VMOVv2f32:
4531 case ARM::VMOVv1i64:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004532 UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004533 break;
4534
4535 // Explicitly reads the dependency.
4536 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004537 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004538 break;
4539 default:
4540 return 0;
4541 }
4542
4543 // If this instruction actually reads a value from Reg, there is no unwanted
4544 // dependency.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004545 if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
Bob Wilsone8a549c2012-09-29 21:43:49 +00004546 return 0;
4547
4548 // We must be able to clobber the whole D-reg.
4549 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4550 // Virtual register must be a foo:ssub_0<def,undef> operand.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004551 if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004552 return 0;
4553 } else if (ARM::SPRRegClass.contains(Reg)) {
4554 // Physical register: MI must define the full D-reg.
4555 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4556 &ARM::DPRRegClass);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004557 if (!DReg || !MI.definesRegister(DReg, TRI))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004558 return 0;
4559 }
4560
4561 // MI has an unwanted D-register dependency.
4562 // Avoid defs in the previous N instructrions.
Diana Picusb772e402016-07-06 11:22:11 +00004563 return PartialUpdateClearance;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004564}
4565
4566// Break a partial register dependency after getPartialRegUpdateClearance
4567// returned non-zero.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004568void ARMBaseInstrInfo::breakPartialRegDependency(
4569 MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
4570 assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004571 assert(TRI && "Need TRI instance");
4572
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004573 const MachineOperand &MO = MI.getOperand(OpNum);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004574 unsigned Reg = MO.getReg();
4575 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4576 "Can't break virtual register dependencies.");
4577 unsigned DReg = Reg;
4578
4579 // If MI defines an S-reg, find the corresponding D super-register.
4580 if (ARM::SPRRegClass.contains(Reg)) {
4581 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4582 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4583 }
4584
4585 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004586 assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
Bob Wilsone8a549c2012-09-29 21:43:49 +00004587
4588 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4589 // the full D-register by loading the same value to both lanes. The
4590 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004591 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004592 // too big regressions.
4593
4594 // Insert the dependency-breaking FCONSTD before MI.
4595 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00004596 AddDefaultPred(
4597 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
4598 .addImm(96));
4599 MI.addRegisterKilled(DReg, TRI, true);
Bob Wilsone8a549c2012-09-29 21:43:49 +00004600}
4601
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004602bool ARMBaseInstrInfo::hasNOP() const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +00004603 return Subtarget.getFeatureBits()[ARM::HasV6KOps];
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004604}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004605
4606bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004607 if (MI->getNumOperands() < 4)
4608 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004609 unsigned ShOpVal = MI->getOperand(3).getImm();
4610 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4611 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4612 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4613 ((ShImm == 1 || ShImm == 2) &&
4614 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4615 return true;
4616
4617 return false;
4618}
Quentin Colombetd358e842014-08-22 18:05:22 +00004619
4620bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
4621 const MachineInstr &MI, unsigned DefIdx,
4622 SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
4623 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4624 assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
4625
4626 switch (MI.getOpcode()) {
4627 case ARM::VMOVDRR:
4628 // dX = VMOVDRR rY, rZ
4629 // is the same as:
4630 // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
4631 // Populate the InputRegs accordingly.
4632 // rY
4633 const MachineOperand *MOReg = &MI.getOperand(1);
4634 InputRegs.push_back(
4635 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4636 // rZ
4637 MOReg = &MI.getOperand(2);
4638 InputRegs.push_back(
4639 RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4640 return true;
4641 }
4642 llvm_unreachable("Target dependent opcode missing");
4643}
4644
4645bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
4646 const MachineInstr &MI, unsigned DefIdx,
4647 RegSubRegPairAndIdx &InputReg) const {
4648 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4649 assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
4650
4651 switch (MI.getOpcode()) {
4652 case ARM::VMOVRRD:
4653 // rX, rY = VMOVRRD dZ
4654 // is the same as:
4655 // rX = EXTRACT_SUBREG dZ, ssub_0
4656 // rY = EXTRACT_SUBREG dZ, ssub_1
4657 const MachineOperand &MOReg = MI.getOperand(2);
4658 InputReg.Reg = MOReg.getReg();
4659 InputReg.SubReg = MOReg.getSubReg();
4660 InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
4661 return true;
4662 }
4663 llvm_unreachable("Target dependent opcode missing");
4664}
4665
4666bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
4667 const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
4668 RegSubRegPairAndIdx &InsertedReg) const {
4669 assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
4670 assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
4671
4672 switch (MI.getOpcode()) {
4673 case ARM::VSETLNi32:
4674 // dX = VSETLNi32 dY, rZ, imm
4675 const MachineOperand &MOBaseReg = MI.getOperand(1);
4676 const MachineOperand &MOInsertedReg = MI.getOperand(2);
4677 const MachineOperand &MOIndex = MI.getOperand(3);
4678 BaseReg.Reg = MOBaseReg.getReg();
4679 BaseReg.SubReg = MOBaseReg.getSubReg();
4680
4681 InsertedReg.Reg = MOInsertedReg.getReg();
4682 InsertedReg.SubReg = MOInsertedReg.getSubReg();
4683 InsertedReg.SubIdx = MOIndex.getImm() == 0 ? ARM::ssub_0 : ARM::ssub_1;
4684 return true;
4685 }
4686 llvm_unreachable("Target dependent opcode missing");
4687}