blob: 1c6c210dae8d93ef7e0f47827c96883f398870cb [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Constants.h"
32#include "llvm/IR/Function.h"
33#include "llvm/IR/GlobalValue.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000034#include "llvm/MC/MCAsmInfo.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000035#include "llvm/MC/MCExpr.h"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000036#include "llvm/Support/BranchProbability.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000037#include "llvm/Support/CommandLine.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000038#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000040
David Goodwinaf7451b2009-07-08 16:09:28 +000041using namespace llvm;
42
Chandler Carruthe96dd892014-04-21 22:55:11 +000043#define DEBUG_TYPE "arm-instrinfo"
44
Chandler Carruthd174b722014-04-22 02:03:14 +000045#define GET_INSTRINFO_CTOR_DTOR
46#include "ARMGenInstrInfo.inc"
47
David Goodwinaf7451b2009-07-08 16:09:28 +000048static cl::opt<bool>
49EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
50 cl::desc("Enable ARM 2-addr to 3-addr conv"));
51
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000052static cl::opt<bool>
Jakob Stoklund Olesen653183f2011-11-15 23:53:18 +000053WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000054 cl::desc("Widen ARM vmovs to vmovd when possible"));
55
Bob Wilsone8a549c2012-09-29 21:43:49 +000056static cl::opt<unsigned>
57SwiftPartialUpdateClearance("swift-partial-update-clearance",
58 cl::Hidden, cl::init(12),
59 cl::desc("Clearance before partial register updates"));
60
Evan Cheng62c7b5b2010-12-05 22:04:16 +000061/// ARM_MLxEntry - Record information about MLA / MLS instructions.
62struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000063 uint16_t MLxOpc; // MLA / MLS opcode
64 uint16_t MulOpc; // Expanded multiplication opcode
65 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000066 bool NegAcc; // True if the acc is negated before the add / sub.
67 bool HasLane; // True if instruction has an extra "lane" operand.
68};
69
70static const ARM_MLxEntry ARM_MLxTable[] = {
71 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
72 // fp scalar ops
73 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
74 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
75 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
76 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000077 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
78 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
79 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
80 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
81
82 // fp SIMD ops
83 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
84 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
85 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
86 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
87 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
88 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
89 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
90 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
91};
92
Anton Korobeynikov14635da2009-11-02 00:10:38 +000093ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000094 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000095 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000096 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
97 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
98 assert(false && "Duplicated entries?");
99 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
100 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
101 }
102}
103
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000104// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
105// currently defaults to no prepass hazard recognizer.
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000106ScheduleHazardRecognizer *ARMBaseInstrInfo::
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000107CreateTargetHazardRecognizer(const TargetMachine *TM,
108 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000109 if (usePreRAHazardRecognizer()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000110 const InstrItineraryData *II = TM->getInstrItineraryData();
111 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
112 }
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000113 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000114}
115
116ScheduleHazardRecognizer *ARMBaseInstrInfo::
117CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
118 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000119 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000120 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000121 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000122}
123
124MachineInstr *
125ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
126 MachineBasicBlock::iterator &MBBI,
127 LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000128 // FIXME: Thumb2 support.
129
David Goodwinaf7451b2009-07-08 16:09:28 +0000130 if (!EnableARM3Addr)
Craig Topper062a2ba2014-04-25 05:30:21 +0000131 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000132
133 MachineInstr *MI = MBBI;
134 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000135 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000136 bool isPre = false;
137 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000138 default: return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000139 case ARMII::IndexModePre:
140 isPre = true;
141 break;
142 case ARMII::IndexModePost:
143 break;
144 }
145
146 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
147 // operation.
148 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
149 if (MemOpc == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000150 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000151
Craig Topper062a2ba2014-04-25 05:30:21 +0000152 MachineInstr *UpdateMI = nullptr;
153 MachineInstr *MemMI = nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000154 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000155 const MCInstrDesc &MCID = MI->getDesc();
156 unsigned NumOps = MCID.getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000157 bool isLoad = !MI->mayStore();
David Goodwinaf7451b2009-07-08 16:09:28 +0000158 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
159 const MachineOperand &Base = MI->getOperand(2);
160 const MachineOperand &Offset = MI->getOperand(NumOps-3);
161 unsigned WBReg = WB.getReg();
162 unsigned BaseReg = Base.getReg();
163 unsigned OffReg = Offset.getReg();
164 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
165 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
166 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000167 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000168 case ARMII::AddrMode2: {
169 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
170 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
171 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000172 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000173 // Can't encode it in a so_imm operand. This transformation will
174 // add more than 1 instruction. Abandon!
Craig Topper062a2ba2014-04-25 05:30:21 +0000175 return nullptr;
David Goodwinaf7451b2009-07-08 16:09:28 +0000176 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000177 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge3a53c42009-07-08 21:03:57 +0000178 .addReg(BaseReg).addImm(Amt)
David Goodwinaf7451b2009-07-08 16:09:28 +0000179 .addImm(Pred).addReg(0).addReg(0);
180 } else if (Amt != 0) {
181 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
182 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
183 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000184 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000185 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
186 .addImm(Pred).addReg(0).addReg(0);
187 } else
188 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000189 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000190 .addReg(BaseReg).addReg(OffReg)
191 .addImm(Pred).addReg(0).addReg(0);
192 break;
193 }
194 case ARMII::AddrMode3 : {
195 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
196 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
197 if (OffReg == 0)
198 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
199 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000200 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000201 .addReg(BaseReg).addImm(Amt)
202 .addImm(Pred).addReg(0).addReg(0);
203 else
204 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000205 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000206 .addReg(BaseReg).addReg(OffReg)
207 .addImm(Pred).addReg(0).addReg(0);
208 break;
209 }
210 }
211
212 std::vector<MachineInstr*> NewMIs;
213 if (isPre) {
214 if (isLoad)
215 MemMI = BuildMI(MF, MI->getDebugLoc(),
216 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000217 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000218 else
219 MemMI = BuildMI(MF, MI->getDebugLoc(),
220 get(MemOpc)).addReg(MI->getOperand(1).getReg())
221 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
222 NewMIs.push_back(MemMI);
223 NewMIs.push_back(UpdateMI);
224 } else {
225 if (isLoad)
226 MemMI = BuildMI(MF, MI->getDebugLoc(),
227 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000228 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000229 else
230 MemMI = BuildMI(MF, MI->getDebugLoc(),
231 get(MemOpc)).addReg(MI->getOperand(1).getReg())
232 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
233 if (WB.isDead())
234 UpdateMI->getOperand(0).setIsDead();
235 NewMIs.push_back(UpdateMI);
236 NewMIs.push_back(MemMI);
237 }
238
239 // Transfer LiveVariables states, kill / dead info.
240 if (LV) {
241 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
242 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000243 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000244 unsigned Reg = MO.getReg();
245
246 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
247 if (MO.isDef()) {
248 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
249 if (MO.isDead())
250 LV->addVirtualRegisterDead(Reg, NewMI);
251 }
252 if (MO.isUse() && MO.isKill()) {
253 for (unsigned j = 0; j < 2; ++j) {
254 // Look at the two new MI's in reverse order.
255 MachineInstr *NewMI = NewMIs[j];
256 if (!NewMI->readsRegister(Reg))
257 continue;
258 LV->addVirtualRegisterKilled(Reg, NewMI);
259 if (VI.removeKill(MI))
260 VI.Kills.push_back(NewMI);
261 break;
262 }
263 }
264 }
265 }
266 }
267
268 MFI->insert(MBBI, NewMIs[1]);
269 MFI->insert(MBBI, NewMIs[0]);
270 return NewMIs[0];
271}
272
273// Branch analysis.
274bool
275ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
276 MachineBasicBlock *&FBB,
277 SmallVectorImpl<MachineOperand> &Cond,
278 bool AllowModify) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000279 TBB = nullptr;
280 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000281
David Goodwinaf7451b2009-07-08 16:09:28 +0000282 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000283 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000284 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000285 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000286
287 // Walk backwards from the end of the basic block until the branch is
288 // analyzed or we give up.
Lang Hames18c98a52013-12-20 20:27:51 +0000289 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000290
291 // Flag to be raised on unanalyzeable instructions. This is useful in cases
292 // where we want to clean up on the end of the basic block before we bail
293 // out.
294 bool CantAnalyze = false;
295
296 // Skip over DEBUG values and predicated nonterminators.
297 while (I->isDebugValue() || !I->isTerminator()) {
298 if (I == MBB.begin())
299 return false;
300 --I;
301 }
302
303 if (isIndirectBranchOpcode(I->getOpcode()) ||
304 isJumpTableBranchOpcode(I->getOpcode())) {
305 // Indirect branches and jump tables can't be analyzed, but we still want
306 // to clean up any instructions at the tail of the basic block.
307 CantAnalyze = true;
308 } else if (isUncondBranchOpcode(I->getOpcode())) {
309 TBB = I->getOperand(0).getMBB();
310 } else if (isCondBranchOpcode(I->getOpcode())) {
311 // Bail out if we encounter multiple conditional branches.
312 if (!Cond.empty())
313 return true;
314
315 assert(!FBB && "FBB should have been null.");
316 FBB = TBB;
317 TBB = I->getOperand(0).getMBB();
318 Cond.push_back(I->getOperand(1));
319 Cond.push_back(I->getOperand(2));
320 } else if (I->isReturn()) {
321 // Returns can't be analyzed, but we should run cleanup.
322 CantAnalyze = !isPredicated(I);
323 } else {
324 // We encountered other unrecognized terminator. Bail out immediately.
325 return true;
326 }
327
328 // Cleanup code - to be run for unpredicated unconditional branches and
329 // returns.
330 if (!isPredicated(I) &&
331 (isUncondBranchOpcode(I->getOpcode()) ||
332 isIndirectBranchOpcode(I->getOpcode()) ||
333 isJumpTableBranchOpcode(I->getOpcode()) ||
334 I->isReturn())) {
335 // Forget any previous condition branch information - it no longer applies.
336 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000337 FBB = nullptr;
Lang Hames24864fe2013-07-19 23:52:47 +0000338
339 // If we can modify the function, delete everything below this
340 // unconditional branch.
341 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000342 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000343 while (DI != MBB.end()) {
344 MachineInstr *InstToDelete = DI;
345 ++DI;
346 InstToDelete->eraseFromParent();
347 }
348 }
349 }
350
351 if (CantAnalyze)
352 return true;
353
Dale Johannesen4244d122010-04-02 01:38:09 +0000354 if (I == MBB.begin())
355 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000356
Dale Johannesen4244d122010-04-02 01:38:09 +0000357 --I;
358 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000359
Lang Hames24864fe2013-07-19 23:52:47 +0000360 // We made it past the terminators without bailing out - we must have
361 // analyzed this branch successfully.
362 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000363}
364
365
366unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000367 MachineBasicBlock::iterator I = MBB.end();
368 if (I == MBB.begin()) return 0;
369 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000370 while (I->isDebugValue()) {
371 if (I == MBB.begin())
372 return 0;
373 --I;
374 }
Evan Cheng056c6692009-07-27 18:20:05 +0000375 if (!isUncondBranchOpcode(I->getOpcode()) &&
376 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000377 return 0;
378
379 // Remove the branch.
380 I->eraseFromParent();
381
382 I = MBB.end();
383
384 if (I == MBB.begin()) return 1;
385 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000386 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000387 return 1;
388
389 // Remove the branch.
390 I->eraseFromParent();
391 return 2;
392}
393
394unsigned
395ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000396 MachineBasicBlock *FBB,
397 const SmallVectorImpl<MachineOperand> &Cond,
398 DebugLoc DL) const {
Evan Cheng780748d2009-07-28 05:48:47 +0000399 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
400 int BOpc = !AFI->isThumbFunction()
401 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
402 int BccOpc = !AFI->isThumbFunction()
403 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000404 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000405
David Goodwinaf7451b2009-07-08 16:09:28 +0000406 // Shouldn't be a fall through.
407 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
408 assert((Cond.size() == 2 || Cond.size() == 0) &&
409 "ARM branch conditions have two components!");
410
Craig Topper062a2ba2014-04-25 05:30:21 +0000411 if (!FBB) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000412 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000413 if (isThumb)
414 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
415 else
416 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000417 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000418 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwinaf7451b2009-07-08 16:09:28 +0000419 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
420 return 1;
421 }
422
423 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000424 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwinaf7451b2009-07-08 16:09:28 +0000425 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000426 if (isThumb)
427 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
428 else
429 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000430 return 2;
431}
432
433bool ARMBaseInstrInfo::
434ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
435 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
436 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
437 return false;
438}
439
Evan Cheng7fae11b2011-12-14 02:11:42 +0000440bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
441 if (MI->isBundle()) {
442 MachineBasicBlock::const_instr_iterator I = MI;
443 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
444 while (++I != E && I->isInsideBundle()) {
445 int PIdx = I->findFirstPredOperandIdx();
446 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
447 return true;
448 }
449 return false;
450 }
451
452 int PIdx = MI->findFirstPredOperandIdx();
453 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
454}
455
David Goodwinaf7451b2009-07-08 16:09:28 +0000456bool ARMBaseInstrInfo::
457PredicateInstruction(MachineInstr *MI,
458 const SmallVectorImpl<MachineOperand> &Pred) const {
459 unsigned Opc = MI->getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000460 if (isUncondBranchOpcode(Opc)) {
461 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000462 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
463 .addImm(Pred[0].getImm())
464 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000465 return true;
466 }
467
468 int PIdx = MI->findFirstPredOperandIdx();
469 if (PIdx != -1) {
470 MachineOperand &PMO = MI->getOperand(PIdx);
471 PMO.setImm(Pred[0].getImm());
472 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
473 return true;
474 }
475 return false;
476}
477
478bool ARMBaseInstrInfo::
479SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
480 const SmallVectorImpl<MachineOperand> &Pred2) const {
481 if (Pred1.size() > 2 || Pred2.size() > 2)
482 return false;
483
484 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
485 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
486 if (CC1 == CC2)
487 return true;
488
489 switch (CC1) {
490 default:
491 return false;
492 case ARMCC::AL:
493 return true;
494 case ARMCC::HS:
495 return CC2 == ARMCC::HI;
496 case ARMCC::LS:
497 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
498 case ARMCC::GE:
499 return CC2 == ARMCC::GT;
500 case ARMCC::LE:
501 return CC2 == ARMCC::LT;
502 }
503}
504
505bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
506 std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000507 bool Found = false;
508 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
509 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000510 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
511 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000512 Pred.push_back(MO);
513 Found = true;
514 }
515 }
516
517 return Found;
518}
519
Evan Chenga33fc862009-11-21 06:21:52 +0000520/// isPredicable - Return true if the specified instruction can be predicated.
521/// By default, this returns true for every instruction with a
522/// PredicateOperand.
523bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000524 if (!MI->isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000525 return false;
526
Joey Goulya5153cb2013-09-09 14:21:49 +0000527 ARMFunctionInfo *AFI =
528 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
529
530 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000531 if (getSubtarget().restrictIT())
Joey Goulya5153cb2013-09-09 14:21:49 +0000532 return isV8EligibleForIT(MI);
533 } else { // non-Thumb
534 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
535 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000536 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000537
Evan Chenga33fc862009-11-21 06:21:52 +0000538 return true;
539}
David Goodwinaf7451b2009-07-08 16:09:28 +0000540
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000541namespace llvm {
542template <> bool IsCPSRDead<MachineInstr>(MachineInstr *MI) {
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000543 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
544 const MachineOperand &MO = MI->getOperand(i);
545 if (!MO.isReg() || MO.isUndef() || MO.isUse())
546 continue;
547 if (MO.getReg() != ARM::CPSR)
548 continue;
549 if (!MO.isDead())
550 return false;
551 }
552 // all definitions of CPSR are dead
553 return true;
554}
Benjamin Kramer44a53da2014-04-12 18:45:24 +0000555}
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000556
Chris Lattnerc831fac2009-12-03 06:58:32 +0000557/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
Chandler Carruth82058c02010-10-23 08:40:19 +0000558LLVM_ATTRIBUTE_NOINLINE
David Goodwinaf7451b2009-07-08 16:09:28 +0000559static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattnerc831fac2009-12-03 06:58:32 +0000560 unsigned JTI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000561static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
562 unsigned JTI) {
Chris Lattnerc831fac2009-12-03 06:58:32 +0000563 assert(JTI < JT.size());
David Goodwinaf7451b2009-07-08 16:09:28 +0000564 return JT[JTI].MBBs.size();
565}
566
567/// GetInstSize - Return the size of the specified MachineInstr.
568///
569unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
570 const MachineBasicBlock &MBB = *MI->getParent();
571 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000572 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000573
Evan Cheng6cc775f2011-06-28 19:10:37 +0000574 const MCInstrDesc &MCID = MI->getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000575 if (MCID.getSize())
576 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000577
David Blaikie46a9f012012-01-20 21:51:11 +0000578 // If this machine instr is an inline asm, measure it.
579 if (MI->getOpcode() == ARM::INLINEASM)
580 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Blaikie46a9f012012-01-20 21:51:11 +0000581 unsigned Opc = MI->getOpcode();
582 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000583 default:
584 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000585 return 0;
586 case TargetOpcode::BUNDLE:
587 return getInstBundleLength(MI);
588 case ARM::MOVi16_ga_pcrel:
589 case ARM::MOVTi16_ga_pcrel:
590 case ARM::t2MOVi16_ga_pcrel:
591 case ARM::t2MOVTi16_ga_pcrel:
592 return 4;
593 case ARM::MOVi32imm:
594 case ARM::t2MOVi32imm:
595 return 8;
596 case ARM::CONSTPOOL_ENTRY:
597 // If this machine instr is a constant pool entry, its size is recorded as
598 // operand #2.
599 return MI->getOperand(2).getImm();
600 case ARM::Int_eh_sjlj_longjmp:
601 return 16;
602 case ARM::tInt_eh_sjlj_longjmp:
603 return 10;
604 case ARM::Int_eh_sjlj_setjmp:
605 case ARM::Int_eh_sjlj_setjmp_nofp:
606 return 20;
607 case ARM::tInt_eh_sjlj_setjmp:
608 case ARM::t2Int_eh_sjlj_setjmp:
609 case ARM::t2Int_eh_sjlj_setjmp_nofp:
610 return 12;
611 case ARM::BR_JTr:
612 case ARM::BR_JTm:
613 case ARM::BR_JTadd:
614 case ARM::tBR_JTr:
615 case ARM::t2BR_JT:
616 case ARM::t2TBB_JT:
617 case ARM::t2TBH_JT: {
618 // These are jumptable branches, i.e. a branch followed by an inlined
619 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
620 // entry is one byte; TBH two byte each.
621 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
622 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
623 unsigned NumOps = MCID.getNumOperands();
624 MachineOperand JTOP =
625 MI->getOperand(NumOps - (MI->isPredicable() ? 3 : 2));
626 unsigned JTI = JTOP.getIndex();
627 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Craig Toppere73658d2014-04-28 04:05:08 +0000628 assert(MJTI != nullptr);
David Blaikie46a9f012012-01-20 21:51:11 +0000629 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
630 assert(JTI < JT.size());
631 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
632 // 4 aligned. The assembler / linker may add 2 byte padding just before
633 // the JT entries. The size does not include this padding; the
634 // constant islands pass does separate bookkeeping for it.
635 // FIXME: If we know the size of the function is less than (1 << 16) *2
636 // bytes, we can use 16-bit entries instead. Then there won't be an
637 // alignment issue.
638 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
639 unsigned NumEntries = getNumJTEntries(JT, JTI);
640 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
641 // Make sure the instruction that follows TBB is 2-byte aligned.
642 // FIXME: Constant island pass should insert an "ALIGN" instruction
643 // instead.
644 ++NumEntries;
645 return NumEntries * EntrySize + InstSize;
646 }
David Blaikie46a9f012012-01-20 21:51:11 +0000647 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000648}
649
Evan Cheng7fae11b2011-12-14 02:11:42 +0000650unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
651 unsigned Size = 0;
652 MachineBasicBlock::const_instr_iterator I = MI;
653 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
654 while (++I != E && I->isInsideBundle()) {
655 assert(!I->isBundle() && "No nested bundle!");
656 Size += GetInstSizeInBytes(&*I);
657 }
658 return Size;
659}
660
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000661void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
662 MachineBasicBlock::iterator I, DebugLoc DL,
663 unsigned DestReg, unsigned SrcReg,
664 bool KillSrc) const {
665 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000666 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000667
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000668 if (GPRDest && GPRSrc) {
669 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000670 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000671 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000672 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000673
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000674 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000675 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000676
Chad Rosierbe762512011-08-20 00:17:25 +0000677 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000678 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000679 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000680 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000681 Opc = ARM::VMOVRS;
682 else if (SPRDest && GPRSrc)
683 Opc = ARM::VMOVSR;
684 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
685 Opc = ARM::VMOVD;
686 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000687 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000688
Chad Rosierbe762512011-08-20 00:17:25 +0000689 if (Opc) {
690 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000691 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000692 if (Opc == ARM::VORRq)
693 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000694 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000695 return;
696 }
697
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000698 // Handle register classes that require multiple instructions.
699 unsigned BeginIdx = 0;
700 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000701 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000702
703 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000704 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
705 Opc = ARM::VORRq;
706 BeginIdx = ARM::qsub_0;
707 SubRegs = 2;
708 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
709 Opc = ARM::VORRq;
710 BeginIdx = ARM::qsub_0;
711 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000712 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000713 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
714 Opc = ARM::VMOVD;
715 BeginIdx = ARM::dsub_0;
716 SubRegs = 2;
717 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
718 Opc = ARM::VMOVD;
719 BeginIdx = ARM::dsub_0;
720 SubRegs = 3;
721 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
722 Opc = ARM::VMOVD;
723 BeginIdx = ARM::dsub_0;
724 SubRegs = 4;
725 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000726 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000727 BeginIdx = ARM::gsub_0;
728 SubRegs = 2;
729 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
730 Opc = ARM::VMOVD;
731 BeginIdx = ARM::dsub_0;
732 SubRegs = 2;
733 Spacing = 2;
734 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
735 Opc = ARM::VMOVD;
736 BeginIdx = ARM::dsub_0;
737 SubRegs = 3;
738 Spacing = 2;
739 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
740 Opc = ARM::VMOVD;
741 BeginIdx = ARM::dsub_0;
742 SubRegs = 4;
743 Spacing = 2;
744 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000745
Andrew Trickb57e2252012-08-29 04:41:37 +0000746 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000747
Andrew Trick4cc69492012-08-29 01:58:52 +0000748 const TargetRegisterInfo *TRI = &getRegisterInfo();
749 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000750
751 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
752 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000753 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000754 Spacing = -Spacing;
755 }
756#ifndef NDEBUG
757 SmallSet<unsigned, 4> DstRegs;
758#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000759 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000760 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
761 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000762 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000763#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000764 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000765 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000766#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000767 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000768 // VORR takes two source operands.
769 if (Opc == ARM::VORRq)
770 Mov.addReg(Src);
771 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000772 // MOVr can set CC.
773 if (Opc == ARM::MOVr)
774 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000775 }
776 // Add implicit super-register defs and kills to the last instruction.
777 Mov->addRegisterDefined(DestReg, TRI);
778 if (KillSrc)
779 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000780}
781
Tim Northover798697d2013-04-21 11:57:07 +0000782const MachineInstrBuilder &
783ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
784 unsigned SubIdx, unsigned State,
785 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000786 if (!SubIdx)
787 return MIB.addReg(Reg, State);
788
789 if (TargetRegisterInfo::isPhysicalRegister(Reg))
790 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
791 return MIB.addReg(Reg, State, SubIdx);
792}
793
David Goodwinaf7451b2009-07-08 16:09:28 +0000794void ARMBaseInstrInfo::
795storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
796 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000797 const TargetRegisterClass *RC,
798 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000799 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000800 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000801 MachineFunction &MF = *MBB.getParent();
802 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000803 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000804
805 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000806 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000807 MachineMemOperand::MOStore,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000808 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000809 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000810
Owen Anderson732f82c2011-08-10 17:21:20 +0000811 switch (RC->getSize()) {
812 case 4:
813 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
814 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000815 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000816 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000817 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
818 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000819 .addReg(SrcReg, getKillRegState(isKill))
820 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000821 } else
822 llvm_unreachable("Unknown reg class!");
823 break;
824 case 8:
825 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
826 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000827 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000828 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000829 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000830 if (Subtarget.hasV5TEOps()) {
831 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
832 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
833 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
834 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
835
836 AddDefaultPred(MIB);
837 } else {
838 // Fallback to STM instruction, which has existed since the dawn of
839 // time.
840 MachineInstrBuilder MIB =
841 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
842 .addFrameIndex(FI).addMemOperand(MMO));
843 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
844 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
845 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000846 } else
847 llvm_unreachable("Unknown reg class!");
848 break;
849 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000850 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000851 // Use aligned spills if the stack can be realigned.
852 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000853 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000854 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000855 .addReg(SrcReg, getKillRegState(isKill))
856 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000857 } else {
858 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000859 .addReg(SrcReg, getKillRegState(isKill))
860 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000861 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000862 }
863 } else
864 llvm_unreachable("Unknown reg class!");
865 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000866 case 24:
867 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
868 // Use aligned spills if the stack can be realigned.
869 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
870 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
871 .addFrameIndex(FI).addImm(16)
872 .addReg(SrcReg, getKillRegState(isKill))
873 .addMemOperand(MMO));
874 } else {
875 MachineInstrBuilder MIB =
876 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
877 .addFrameIndex(FI))
878 .addMemOperand(MMO);
879 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
880 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
881 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
882 }
883 } else
884 llvm_unreachable("Unknown reg class!");
885 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000886 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000887 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000888 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
889 // FIXME: It's possible to only store part of the QQ register if the
890 // spilled def has a sub-register index.
891 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000892 .addFrameIndex(FI).addImm(16)
893 .addReg(SrcReg, getKillRegState(isKill))
894 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000895 } else {
896 MachineInstrBuilder MIB =
897 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000898 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000899 .addMemOperand(MMO);
900 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
901 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
902 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
903 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
904 }
905 } else
906 llvm_unreachable("Unknown reg class!");
907 break;
908 case 64:
909 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
910 MachineInstrBuilder MIB =
911 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
912 .addFrameIndex(FI))
913 .addMemOperand(MMO);
914 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
915 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
916 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
917 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
918 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
919 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
920 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
921 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
922 } else
923 llvm_unreachable("Unknown reg class!");
924 break;
925 default:
926 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000927 }
928}
929
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000930unsigned
931ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
932 int &FrameIndex) const {
933 switch (MI->getOpcode()) {
934 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000935 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000936 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
937 if (MI->getOperand(1).isFI() &&
938 MI->getOperand(2).isReg() &&
939 MI->getOperand(3).isImm() &&
940 MI->getOperand(2).getReg() == 0 &&
941 MI->getOperand(3).getImm() == 0) {
942 FrameIndex = MI->getOperand(1).getIndex();
943 return MI->getOperand(0).getReg();
944 }
945 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000946 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000947 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +0000948 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000949 case ARM::VSTRD:
950 case ARM::VSTRS:
951 if (MI->getOperand(1).isFI() &&
952 MI->getOperand(2).isImm() &&
953 MI->getOperand(2).getImm() == 0) {
954 FrameIndex = MI->getOperand(1).getIndex();
955 return MI->getOperand(0).getReg();
956 }
957 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000958 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +0000959 case ARM::VST1d64TPseudo:
960 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +0000961 if (MI->getOperand(0).isFI() &&
962 MI->getOperand(2).getSubReg() == 0) {
963 FrameIndex = MI->getOperand(0).getIndex();
964 return MI->getOperand(2).getReg();
965 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +0000966 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000967 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +0000968 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +0000969 MI->getOperand(0).getSubReg() == 0) {
970 FrameIndex = MI->getOperand(1).getIndex();
971 return MI->getOperand(0).getReg();
972 }
973 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000974 }
975
976 return 0;
977}
978
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +0000979unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
980 int &FrameIndex) const {
981 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000982 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +0000983}
984
David Goodwinaf7451b2009-07-08 16:09:28 +0000985void ARMBaseInstrInfo::
986loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
987 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000988 const TargetRegisterClass *RC,
989 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000990 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000991 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000992 MachineFunction &MF = *MBB.getParent();
993 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000994 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000995 MachineMemOperand *MMO =
Chris Lattnere3d864b2010-09-21 04:39:43 +0000996 MF.getMachineMemOperand(
Jay Foad465101b2011-11-15 07:34:52 +0000997 MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000998 MachineMemOperand::MOLoad,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000999 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +00001000 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +00001001
Owen Anderson732f82c2011-08-10 17:21:20 +00001002 switch (RC->getSize()) {
1003 case 4:
1004 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1005 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1006 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001007
Owen Anderson732f82c2011-08-10 17:21:20 +00001008 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1009 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001010 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001011 } else
1012 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001013 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001014 case 8:
1015 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1016 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001017 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001018 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001019 MachineInstrBuilder MIB;
1020
1021 if (Subtarget.hasV5TEOps()) {
1022 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1023 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1024 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1025 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1026
1027 AddDefaultPred(MIB);
1028 } else {
1029 // Fallback to LDM instruction, which has existed since the dawn of
1030 // time.
1031 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1032 .addFrameIndex(FI).addMemOperand(MMO));
1033 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1034 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1035 }
1036
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001037 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1038 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001039 } else
1040 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001041 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001042 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001043 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001044 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001045 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001046 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001047 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001048 } else {
1049 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1050 .addFrameIndex(FI)
1051 .addMemOperand(MMO));
1052 }
1053 } else
1054 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001055 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001056 case 24:
1057 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1058 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1059 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1060 .addFrameIndex(FI).addImm(16)
1061 .addMemOperand(MMO));
1062 } else {
1063 MachineInstrBuilder MIB =
1064 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1065 .addFrameIndex(FI)
1066 .addMemOperand(MMO));
1067 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1068 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1069 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1070 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1071 MIB.addReg(DestReg, RegState::ImplicitDefine);
1072 }
1073 } else
1074 llvm_unreachable("Unknown reg class!");
1075 break;
1076 case 32:
1077 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001078 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1079 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001080 .addFrameIndex(FI).addImm(16)
1081 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001082 } else {
1083 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001084 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1085 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001086 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001087 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1088 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1089 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1090 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001091 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1092 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001093 }
1094 } else
1095 llvm_unreachable("Unknown reg class!");
1096 break;
1097 case 64:
1098 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1099 MachineInstrBuilder MIB =
1100 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1101 .addFrameIndex(FI))
1102 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001103 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1104 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1105 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1106 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1107 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1108 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1109 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1110 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001111 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1112 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001113 } else
1114 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001115 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001116 default:
1117 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001118 }
1119}
1120
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001121unsigned
1122ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1123 int &FrameIndex) const {
1124 switch (MI->getOpcode()) {
1125 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001126 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001127 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1128 if (MI->getOperand(1).isFI() &&
1129 MI->getOperand(2).isReg() &&
1130 MI->getOperand(3).isImm() &&
1131 MI->getOperand(2).getReg() == 0 &&
1132 MI->getOperand(3).getImm() == 0) {
1133 FrameIndex = MI->getOperand(1).getIndex();
1134 return MI->getOperand(0).getReg();
1135 }
1136 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001137 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001138 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001139 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001140 case ARM::VLDRD:
1141 case ARM::VLDRS:
1142 if (MI->getOperand(1).isFI() &&
1143 MI->getOperand(2).isImm() &&
1144 MI->getOperand(2).getImm() == 0) {
1145 FrameIndex = MI->getOperand(1).getIndex();
1146 return MI->getOperand(0).getReg();
1147 }
1148 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001149 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001150 case ARM::VLD1d64TPseudo:
1151 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001152 if (MI->getOperand(1).isFI() &&
1153 MI->getOperand(0).getSubReg() == 0) {
1154 FrameIndex = MI->getOperand(1).getIndex();
1155 return MI->getOperand(0).getReg();
1156 }
1157 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001158 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001159 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001160 MI->getOperand(0).getSubReg() == 0) {
1161 FrameIndex = MI->getOperand(1).getIndex();
1162 return MI->getOperand(0).getReg();
1163 }
1164 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001165 }
1166
1167 return 0;
1168}
1169
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001170unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1171 int &FrameIndex) const {
1172 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001173 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001174}
1175
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001176bool ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const{
1177 // This hook gets to expand COPY instructions before they become
1178 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1179 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1180 // changed into a VORR that can go down the NEON pipeline.
Silviu Baranga82dd6ac2013-03-15 18:28:25 +00001181 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001182 return false;
1183
1184 // Look for a copy between even S-registers. That is where we keep floats
1185 // when using NEON v2f32 instructions for f32 arithmetic.
1186 unsigned DstRegS = MI->getOperand(0).getReg();
1187 unsigned SrcRegS = MI->getOperand(1).getReg();
1188 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1189 return false;
1190
1191 const TargetRegisterInfo *TRI = &getRegisterInfo();
1192 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1193 &ARM::DPRRegClass);
1194 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1195 &ARM::DPRRegClass);
1196 if (!DstRegD || !SrcRegD)
1197 return false;
1198
1199 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1200 // legal if the COPY already defines the full DstRegD, and it isn't a
1201 // sub-register insertion.
1202 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1203 return false;
1204
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001205 // A dead copy shouldn't show up here, but reject it just in case.
1206 if (MI->getOperand(0).isDead())
1207 return false;
1208
1209 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001210 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001211 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001212
1213 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1214 // or some other super-register.
1215 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1216 if (ImpDefIdx != -1)
1217 MI->RemoveOperand(ImpDefIdx);
1218
1219 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001220 MI->setDesc(get(ARM::VMOVD));
1221 MI->getOperand(0).setReg(DstRegD);
1222 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001223 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001224
1225 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1226 // register scavenger and machine verifier, so we need to indicate that we
1227 // are reading an undefined value from SrcRegD, but a proper value from
1228 // SrcRegS.
1229 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001230 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001231
1232 // SrcRegD may actually contain an unrelated value in the ssub_1
1233 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1234 if (MI->getOperand(1).isKill()) {
1235 MI->getOperand(1).setIsKill(false);
1236 MI->addRegisterKilled(SrcRegS, TRI, true);
1237 }
1238
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001239 DEBUG(dbgs() << "replaced by: " << *MI);
1240 return true;
1241}
1242
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001243/// Create a copy of a const pool value. Update CPI to the new index and return
1244/// the label UID.
1245static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1246 MachineConstantPool *MCP = MF.getConstantPool();
1247 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1248
1249 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1250 assert(MCPE.isMachineConstantPoolEntry() &&
1251 "Expecting a machine constantpool entry!");
1252 ARMConstantPoolValue *ACPV =
1253 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1254
Evan Chengdfce83c2011-01-17 08:03:18 +00001255 unsigned PCLabelId = AFI->createPICLabelUId();
Craig Topper062a2ba2014-04-25 05:30:21 +00001256 ARMConstantPoolValue *NewCPV = nullptr;
Oliver Stannard8f859942014-01-29 16:01:24 +00001257
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001258 // FIXME: The below assumes PIC relocation model and that the function
1259 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1260 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1261 // instructions, so that's probably OK, but is PIC always correct when
1262 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001263 if (ACPV->isGlobalValue())
Bill Wendling7753d662011-10-01 08:00:54 +00001264 NewCPV = ARMConstantPoolConstant::
1265 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1266 ARMCP::CPValue, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001267 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001268 NewCPV = ARMConstantPoolSymbol::
1269 Create(MF.getFunction()->getContext(),
1270 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001271 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001272 NewCPV = ARMConstantPoolConstant::
1273 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1274 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001275 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001276 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1277 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001278 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001279 NewCPV = ARMConstantPoolMBB::
1280 Create(MF.getFunction()->getContext(),
1281 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001282 else
1283 llvm_unreachable("Unexpected ARM constantpool value type!!");
1284 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1285 return PCLabelId;
1286}
1287
Evan Chengfe864422009-11-08 00:15:23 +00001288void ARMBaseInstrInfo::
1289reMaterialize(MachineBasicBlock &MBB,
1290 MachineBasicBlock::iterator I,
1291 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001292 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001293 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001294 unsigned Opcode = Orig->getOpcode();
1295 switch (Opcode) {
1296 default: {
1297 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001298 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001299 MBB.insert(I, MI);
1300 break;
1301 }
1302 case ARM::tLDRpci_pic:
1303 case ARM::t2LDRpci_pic: {
1304 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001305 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001306 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001307 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1308 DestReg)
1309 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001310 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001311 break;
1312 }
1313 }
Evan Chengfe864422009-11-08 00:15:23 +00001314}
1315
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001316MachineInstr *
1317ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001318 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001319 switch(Orig->getOpcode()) {
1320 case ARM::tLDRpci_pic:
1321 case ARM::t2LDRpci_pic: {
1322 unsigned CPI = Orig->getOperand(1).getIndex();
1323 unsigned PCLabelId = duplicateCPV(MF, CPI);
1324 Orig->getOperand(1).setIndex(CPI);
1325 Orig->getOperand(2).setImm(PCLabelId);
1326 break;
1327 }
1328 }
1329 return MI;
1330}
1331
Evan Chenge9c46c22010-03-03 01:44:33 +00001332bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001333 const MachineInstr *MI1,
1334 const MachineRegisterInfo *MRI) const {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001335 int Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001336 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001337 Opcode == ARM::t2LDRpci_pic ||
1338 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001339 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001340 Opcode == ARM::LDRLIT_ga_pcrel ||
1341 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1342 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001343 Opcode == ARM::MOV_ga_pcrel ||
1344 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001345 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001346 if (MI1->getOpcode() != Opcode)
1347 return false;
1348 if (MI0->getNumOperands() != MI1->getNumOperands())
1349 return false;
1350
1351 const MachineOperand &MO0 = MI0->getOperand(1);
1352 const MachineOperand &MO1 = MI1->getOperand(1);
1353 if (MO0.getOffset() != MO1.getOffset())
1354 return false;
1355
Tim Northover72360d22013-12-02 10:35:41 +00001356 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1357 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1358 Opcode == ARM::tLDRLIT_ga_pcrel ||
1359 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001360 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001361 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001362 // Ignore the PC labels.
1363 return MO0.getGlobal() == MO1.getGlobal();
1364
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001365 const MachineFunction *MF = MI0->getParent()->getParent();
1366 const MachineConstantPool *MCP = MF->getConstantPool();
1367 int CPI0 = MO0.getIndex();
1368 int CPI1 = MO1.getIndex();
1369 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1370 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001371 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1372 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1373 if (isARMCP0 && isARMCP1) {
1374 ARMConstantPoolValue *ACPV0 =
1375 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1376 ARMConstantPoolValue *ACPV1 =
1377 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1378 return ACPV0->hasSameValue(ACPV1);
1379 } else if (!isARMCP0 && !isARMCP1) {
1380 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1381 }
1382 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001383 } else if (Opcode == ARM::PICLDR) {
1384 if (MI1->getOpcode() != Opcode)
1385 return false;
1386 if (MI0->getNumOperands() != MI1->getNumOperands())
1387 return false;
1388
1389 unsigned Addr0 = MI0->getOperand(1).getReg();
1390 unsigned Addr1 = MI1->getOperand(1).getReg();
1391 if (Addr0 != Addr1) {
1392 if (!MRI ||
1393 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1394 !TargetRegisterInfo::isVirtualRegister(Addr1))
1395 return false;
1396
1397 // This assumes SSA form.
1398 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1399 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1400 // Check if the loaded value, e.g. a constantpool of a global address, are
1401 // the same.
1402 if (!produceSameValue(Def0, Def1, MRI))
1403 return false;
1404 }
1405
1406 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1407 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1408 const MachineOperand &MO0 = MI0->getOperand(i);
1409 const MachineOperand &MO1 = MI1->getOperand(i);
1410 if (!MO0.isIdenticalTo(MO1))
1411 return false;
1412 }
1413 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001414 }
1415
Evan Chenge9c46c22010-03-03 01:44:33 +00001416 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001417}
1418
Bill Wendlingf4707472010-06-23 23:00:16 +00001419/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1420/// determine if two loads are loading from the same base address. It should
1421/// only return true if the base pointers are the same and the only differences
1422/// between the two addresses is the offset. It also returns the offsets by
1423/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001424///
1425/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1426/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001427bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1428 int64_t &Offset1,
1429 int64_t &Offset2) const {
1430 // Don't worry about Thumb: just ARM and Thumb2.
1431 if (Subtarget.isThumb1Only()) return false;
1432
1433 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1434 return false;
1435
1436 switch (Load1->getMachineOpcode()) {
1437 default:
1438 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001439 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001440 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001441 case ARM::LDRD:
1442 case ARM::LDRH:
1443 case ARM::LDRSB:
1444 case ARM::LDRSH:
1445 case ARM::VLDRD:
1446 case ARM::VLDRS:
1447 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001448 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001449 case ARM::t2LDRDi8:
1450 case ARM::t2LDRSHi8:
1451 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001452 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001453 case ARM::t2LDRSHi12:
1454 break;
1455 }
1456
1457 switch (Load2->getMachineOpcode()) {
1458 default:
1459 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001460 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001461 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001462 case ARM::LDRD:
1463 case ARM::LDRH:
1464 case ARM::LDRSB:
1465 case ARM::LDRSH:
1466 case ARM::VLDRD:
1467 case ARM::VLDRS:
1468 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001469 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001470 case ARM::t2LDRSHi8:
1471 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001472 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001473 case ARM::t2LDRSHi12:
1474 break;
1475 }
1476
1477 // Check if base addresses and chain operands match.
1478 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1479 Load1->getOperand(4) != Load2->getOperand(4))
1480 return false;
1481
1482 // Index should be Reg0.
1483 if (Load1->getOperand(3) != Load2->getOperand(3))
1484 return false;
1485
1486 // Determine the offsets.
1487 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1488 isa<ConstantSDNode>(Load2->getOperand(1))) {
1489 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1490 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1491 return true;
1492 }
1493
1494 return false;
1495}
1496
1497/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001498/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001499/// be scheduled togther. On some targets if two loads are loading from
1500/// addresses in the same cache line, it's better if they are scheduled
1501/// together. This function takes two integers that represent the load offsets
1502/// from the common base address. It returns true if it decides it's desirable
1503/// to schedule the two loads together. "NumLoads" is the number of loads that
1504/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001505///
1506/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1507/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001508bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1509 int64_t Offset1, int64_t Offset2,
1510 unsigned NumLoads) const {
1511 // Don't worry about Thumb: just ARM and Thumb2.
1512 if (Subtarget.isThumb1Only()) return false;
1513
1514 assert(Offset2 > Offset1);
1515
1516 if ((Offset2 - Offset1) / 8 > 64)
1517 return false;
1518
Renato Golinb184cd92013-08-14 16:35:29 +00001519 // Check if the machine opcodes are different. If they are different
1520 // then we consider them to not be of the same base address,
1521 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1522 // In this case, they are considered to be the same because they are different
1523 // encoding forms of the same basic instruction.
1524 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1525 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1526 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1527 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1528 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001529 return false; // FIXME: overly conservative?
1530
1531 // Four loads in a row should be sufficient.
1532 if (NumLoads >= 3)
1533 return false;
1534
1535 return true;
1536}
1537
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001538bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1539 const MachineBasicBlock *MBB,
1540 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001541 // Debug info is never a scheduling boundary. It's necessary to be explicit
1542 // due to the special treatment of IT instructions below, otherwise a
1543 // dbg_value followed by an IT will result in the IT instruction being
1544 // considered a scheduling hazard, which is wrong. It should be the actual
1545 // instruction preceding the dbg_value instruction(s), just like it is
1546 // when debug info is not present.
1547 if (MI->isDebugValue())
1548 return false;
1549
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001550 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001551 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001552 return true;
1553
1554 // Treat the start of the IT block as a scheduling boundary, but schedule
1555 // t2IT along with all instructions following it.
1556 // FIXME: This is a big hammer. But the alternative is to add all potential
1557 // true and anti dependencies to IT block instructions as implicit operands
1558 // to the t2IT instruction. The added compile time and complexity does not
1559 // seem worth it.
1560 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001561 // Make sure to skip any dbg_value instructions
1562 while (++I != MBB->end() && I->isDebugValue())
1563 ;
1564 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001565 return true;
1566
1567 // Don't attempt to schedule around any instruction that defines
1568 // a stack-oriented pointer, as it's unlikely to be profitable. This
1569 // saves compile time, because it doesn't require every single
1570 // stack slot reference to depend on the instruction that does the
1571 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001572 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001573 // No ARM calling conventions change the stack pointer. (X86 calling
1574 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001575 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001576 return true;
1577
1578 return false;
1579}
1580
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001581bool ARMBaseInstrInfo::
1582isProfitableToIfCvt(MachineBasicBlock &MBB,
1583 unsigned NumCycles, unsigned ExtraPredCycles,
1584 const BranchProbability &Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001585 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001586 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001587
Owen Anderson88af7d02010-09-28 18:32:13 +00001588 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001589 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1590 UnpredCost /= Probability.getDenominator();
1591 UnpredCost += 1; // The branch itself
1592 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001593
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001594 return (NumCycles + ExtraPredCycles) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001595}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001596
Evan Cheng02b184d2010-06-25 22:42:03 +00001597bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001598isProfitableToIfCvt(MachineBasicBlock &TMBB,
1599 unsigned TCycles, unsigned TExtra,
1600 MachineBasicBlock &FMBB,
1601 unsigned FCycles, unsigned FExtra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001602 const BranchProbability &Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001603 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001604 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001605
Owen Anderson88af7d02010-09-28 18:32:13 +00001606 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001607 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1608 TUnpredCost /= Probability.getDenominator();
Andrew Trick3f1fdf12011-09-21 02:17:37 +00001609
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001610 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1611 unsigned FUnpredCost = Comp * FCycles;
1612 FUnpredCost /= Probability.getDenominator();
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001613
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001614 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1615 UnpredCost += 1; // The branch itself
1616 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1617
1618 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001619}
1620
Bob Wilsone8a549c2012-09-29 21:43:49 +00001621bool
1622ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1623 MachineBasicBlock &FMBB) const {
1624 // Reduce false anti-dependencies to let Swift's out-of-order execution
1625 // engine do its thing.
1626 return Subtarget.isSwift();
1627}
1628
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001629/// getInstrPredicate - If instruction is predicated, returns its predicate
1630/// condition, otherwise returns AL. It also returns the condition code
1631/// register by reference.
Evan Cheng83e0d482009-09-28 09:14:39 +00001632ARMCC::CondCodes
1633llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001634 int PIdx = MI->findFirstPredOperandIdx();
1635 if (PIdx == -1) {
1636 PredReg = 0;
1637 return ARMCC::AL;
1638 }
1639
1640 PredReg = MI->getOperand(PIdx+1).getReg();
1641 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1642}
1643
1644
Evan Cheng780748d2009-07-28 05:48:47 +00001645int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001646 if (Opc == ARM::B)
1647 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001648 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001649 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001650 if (Opc == ARM::t2B)
1651 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001652
1653 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001654}
1655
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001656/// commuteInstruction - Handle commutable instructions.
1657MachineInstr *
1658ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1659 switch (MI->getOpcode()) {
1660 case ARM::MOVCCr:
1661 case ARM::t2MOVCCr: {
1662 // MOVCC can be commuted by inverting the condition.
1663 unsigned PredReg = 0;
1664 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1665 // MOVCC AL can't be inverted. Shouldn't happen.
1666 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
Craig Topper062a2ba2014-04-25 05:30:21 +00001667 return nullptr;
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001668 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001669 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001670 return nullptr;
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001671 // After swapping the MOVCC operands, also invert the condition.
1672 MI->getOperand(MI->findFirstPredOperandIdx())
1673 .setImm(ARMCC::getOppositeCondition(CC));
1674 return MI;
1675 }
1676 }
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001677 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001678}
Evan Cheng780748d2009-07-28 05:48:47 +00001679
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001680/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001681/// return the defining instruction.
1682static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1683 const MachineRegisterInfo &MRI,
1684 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001685 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001686 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001687 if (!MRI.hasOneNonDBGUse(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +00001688 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001689 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001690 if (!MI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001691 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001692 // MI is folded into the MOVCC by predicating it.
1693 if (!MI->isPredicable())
Craig Topper062a2ba2014-04-25 05:30:21 +00001694 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001695 // Check if MI has any non-dead defs or physreg uses. This also detects
1696 // predicated instructions which will be reading CPSR.
1697 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1698 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001699 // Reject frame index operands, PEI can't handle the predicated pseudos.
1700 if (MO.isFI() || MO.isCPI() || MO.isJTI())
Craig Topper062a2ba2014-04-25 05:30:21 +00001701 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001702 if (!MO.isReg())
1703 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001704 // MI can't have any tied operands, that would conflict with predication.
1705 if (MO.isTied())
Craig Topper062a2ba2014-04-25 05:30:21 +00001706 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001707 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Craig Topper062a2ba2014-04-25 05:30:21 +00001708 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001709 if (MO.isDef() && !MO.isDead())
Craig Topper062a2ba2014-04-25 05:30:21 +00001710 return nullptr;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001711 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001712 bool DontMoveAcrossStores = true;
Craig Topper062a2ba2014-04-25 05:30:21 +00001713 if (!MI->isSafeToMove(TII, /* AliasAnalysis = */ nullptr,
1714 DontMoveAcrossStores))
1715 return nullptr;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001716 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001717}
1718
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001719bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1720 SmallVectorImpl<MachineOperand> &Cond,
1721 unsigned &TrueOp, unsigned &FalseOp,
1722 bool &Optimizable) const {
1723 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1724 "Unknown select instruction");
1725 // MOVCC operands:
1726 // 0: Def.
1727 // 1: True use.
1728 // 2: False use.
1729 // 3: Condition code.
1730 // 4: CPSR use.
1731 TrueOp = 1;
1732 FalseOp = 2;
1733 Cond.push_back(MI->getOperand(3));
1734 Cond.push_back(MI->getOperand(4));
1735 // We can always fold a def.
1736 Optimizable = true;
1737 return false;
1738}
1739
1740MachineInstr *ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1741 bool PreferFalse) const {
1742 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1743 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001744 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001745 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1746 bool Invert = !DefMI;
1747 if (!DefMI)
1748 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1749 if (!DefMI)
Craig Topper062a2ba2014-04-25 05:30:21 +00001750 return nullptr;
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001751
Matthias Braun2f169f92013-10-04 16:52:56 +00001752 // Find new register class to use.
1753 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1754 unsigned DestReg = MI->getOperand(0).getReg();
1755 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1756 if (!MRI.constrainRegClass(DestReg, PreviousClass))
Craig Topper062a2ba2014-04-25 05:30:21 +00001757 return nullptr;
Matthias Braun2f169f92013-10-04 16:52:56 +00001758
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001759 // Create a new predicated version of DefMI.
1760 // Rfalse is the first use.
1761 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001762 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001763
1764 // Copy all the DefMI operands, excluding its (null) predicate.
1765 const MCInstrDesc &DefDesc = DefMI->getDesc();
1766 for (unsigned i = 1, e = DefDesc.getNumOperands();
1767 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1768 NewMI.addOperand(DefMI->getOperand(i));
1769
1770 unsigned CondCode = MI->getOperand(3).getImm();
1771 if (Invert)
1772 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1773 else
1774 NewMI.addImm(CondCode);
1775 NewMI.addOperand(MI->getOperand(4));
1776
1777 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1778 if (NewMI->hasOptionalDef())
1779 AddDefaultCC(NewMI);
1780
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001781 // The output register value when the predicate is false is an implicit
1782 // register operand tied to the first def.
1783 // The tie makes the register allocator ensure the FalseReg is allocated the
1784 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001785 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001786 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001787 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1788
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001789 // The caller will erase MI, but not DefMI.
1790 DefMI->eraseFromParent();
1791 return NewMI;
1792}
1793
Andrew Trick924123a2011-09-21 02:20:46 +00001794/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1795/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1796/// def operand.
1797///
1798/// This will go away once we can teach tblgen how to set the optional CPSR def
1799/// operand itself.
1800struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001801 uint16_t PseudoOpc;
1802 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001803};
1804
Craig Topper2fbd1302012-05-24 03:59:11 +00001805static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001806 {ARM::ADDSri, ARM::ADDri},
1807 {ARM::ADDSrr, ARM::ADDrr},
1808 {ARM::ADDSrsi, ARM::ADDrsi},
1809 {ARM::ADDSrsr, ARM::ADDrsr},
1810
1811 {ARM::SUBSri, ARM::SUBri},
1812 {ARM::SUBSrr, ARM::SUBrr},
1813 {ARM::SUBSrsi, ARM::SUBrsi},
1814 {ARM::SUBSrsr, ARM::SUBrsr},
1815
1816 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001817 {ARM::RSBSrsi, ARM::RSBrsi},
1818 {ARM::RSBSrsr, ARM::RSBrsr},
1819
1820 {ARM::t2ADDSri, ARM::t2ADDri},
1821 {ARM::t2ADDSrr, ARM::t2ADDrr},
1822 {ARM::t2ADDSrs, ARM::t2ADDrs},
1823
1824 {ARM::t2SUBSri, ARM::t2SUBri},
1825 {ARM::t2SUBSrr, ARM::t2SUBrr},
1826 {ARM::t2SUBSrs, ARM::t2SUBrs},
1827
1828 {ARM::t2RSBSri, ARM::t2RSBri},
1829 {ARM::t2RSBSrs, ARM::t2RSBrs},
1830};
1831
1832unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001833 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1834 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1835 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001836 return 0;
1837}
1838
Evan Cheng780748d2009-07-28 05:48:47 +00001839void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1840 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1841 unsigned DestReg, unsigned BaseReg, int NumBytes,
1842 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001843 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00001844 if (NumBytes == 0 && DestReg != BaseReg) {
1845 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1846 .addReg(BaseReg, RegState::Kill)
1847 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1848 .setMIFlags(MIFlags);
1849 return;
1850 }
1851
Evan Cheng780748d2009-07-28 05:48:47 +00001852 bool isSub = NumBytes < 0;
1853 if (isSub) NumBytes = -NumBytes;
1854
1855 while (NumBytes) {
1856 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1857 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1858 assert(ThisVal && "Didn't extract field correctly");
1859
1860 // We will handle these bits from offset, clear them.
1861 NumBytes &= ~ThisVal;
1862
1863 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1864
1865 // Build the new ADD / SUB.
1866 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1867 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1868 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001869 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1870 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00001871 BaseReg = DestReg;
1872 }
1873}
1874
Weiming Zhao01524852014-03-20 23:28:16 +00001875static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
1876 MachineInstr *MI) {
1877 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
1878 Subreg.isValid(); ++Subreg)
1879 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
1880 MachineBasicBlock::LQR_Dead)
1881 return true;
1882 return false;
1883}
Tim Northoverdee86042013-12-02 14:46:26 +00001884bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
1885 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00001886 unsigned NumBytes) {
1887 // This optimisation potentially adds lots of load and store
1888 // micro-operations, it's only really a great benefit to code-size.
Tim Northoverdee86042013-12-02 14:46:26 +00001889 if (!Subtarget.isMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00001890 return false;
1891
1892 // If only one register is pushed/popped, LLVM can use an LDR/STR
1893 // instead. We can't modify those so make sure we're dealing with an
1894 // instruction we understand.
1895 bool IsPop = isPopOpcode(MI->getOpcode());
1896 bool IsPush = isPushOpcode(MI->getOpcode());
1897 if (!IsPush && !IsPop)
1898 return false;
1899
1900 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
1901 MI->getOpcode() == ARM::VLDMDIA_UPD;
1902 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
1903 MI->getOpcode() == ARM::tPOP ||
1904 MI->getOpcode() == ARM::tPOP_RET;
1905
1906 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
1907 MI->getOperand(1).getReg() == ARM::SP)) &&
1908 "trying to fold sp update into non-sp-updating push/pop");
1909
1910 // The VFP push & pop act on D-registers, so we can only fold an adjustment
1911 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
1912 // if this is violated.
1913 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
1914 return false;
1915
1916 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
1917 // pred) so the list starts at 4. Thumb1 starts after the predicate.
1918 int RegListIdx = IsT1PushPop ? 2 : 4;
1919
1920 // Calculate the space we'll need in terms of registers.
1921 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
1922 unsigned RD0Reg, RegsNeeded;
1923 if (IsVFPPushPop) {
1924 RD0Reg = ARM::D0;
1925 RegsNeeded = NumBytes / 8;
1926 } else {
1927 RD0Reg = ARM::R0;
1928 RegsNeeded = NumBytes / 4;
1929 }
1930
1931 // We're going to have to strip all list operands off before
1932 // re-adding them since the order matters, so save the existing ones
1933 // for later.
1934 SmallVector<MachineOperand, 4> RegList;
1935 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
1936 RegList.push_back(MI->getOperand(i));
1937
Tim Northover93bcc662013-11-08 17:18:07 +00001938 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00001939 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00001940
1941 // Now try to find enough space in the reglist to allocate NumBytes.
1942 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00001943 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00001944 if (!IsPop) {
1945 // Pushing any register is completely harmless, mark the
1946 // register involved as undef since we don't care about it in
1947 // the slightest.
1948 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
1949 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00001950 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00001951 continue;
1952 }
1953
Tim Northover45479dc2013-12-01 14:16:24 +00001954 // However, we can only pop an extra register if it's not live. For
1955 // registers live within the function we might clobber a return value
1956 // register; the other way a register can be live here is if it's
1957 // callee-saved.
Weiming Zhao01524852014-03-20 23:28:16 +00001958 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
1959 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
1960 // can replace isAnySubRegLive().
Tim Northover45479dc2013-12-01 14:16:24 +00001961 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Weiming Zhao01524852014-03-20 23:28:16 +00001962 isAnySubRegLive(CurReg, TRI, MI)) {
Tim Northover45479dc2013-12-01 14:16:24 +00001963 // VFP pops don't allow holes in the register list, so any skip is fatal
1964 // for our transformation. GPR pops do, so we should just keep looking.
1965 if (IsVFPPushPop)
1966 return false;
1967 else
1968 continue;
1969 }
Tim Northover93bcc662013-11-08 17:18:07 +00001970
1971 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00001972 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
1973 true));
Tim Northover45479dc2013-12-01 14:16:24 +00001974 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00001975 }
1976
1977 if (RegsNeeded > 0)
1978 return false;
1979
1980 // Finally we know we can profitably perform the optimisation so go
1981 // ahead: strip all existing registers off and add them back again
1982 // in the right order.
1983 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
1984 MI->RemoveOperand(i);
1985
1986 // Add the complete list back in.
1987 MachineInstrBuilder MIB(MF, &*MI);
1988 for (int i = RegList.size() - 1; i >= 0; --i)
1989 MIB.addOperand(RegList[i]);
1990
1991 return true;
1992}
1993
Evan Cheng7a37b1a2009-08-27 01:23:50 +00001994bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1995 unsigned FrameReg, int &Offset,
1996 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00001997 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001998 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00001999 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
2000 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002001
Evan Cheng780748d2009-07-28 05:48:47 +00002002 // Memory operands in inline assembly always use AddrMode2.
2003 if (Opcode == ARM::INLINEASM)
2004 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002005
Evan Cheng780748d2009-07-28 05:48:47 +00002006 if (Opcode == ARM::ADDri) {
2007 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2008 if (Offset == 0) {
2009 // Turn it into a move.
2010 MI.setDesc(TII.get(ARM::MOVr));
2011 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2012 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002013 Offset = 0;
2014 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002015 } else if (Offset < 0) {
2016 Offset = -Offset;
2017 isSub = true;
2018 MI.setDesc(TII.get(ARM::SUBri));
2019 }
2020
2021 // Common case: small offset, fits into instruction.
2022 if (ARM_AM::getSOImmVal(Offset) != -1) {
2023 // Replace the FrameIndex with sp / fp
2024 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2025 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002026 Offset = 0;
2027 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002028 }
2029
2030 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2031 // as possible.
2032 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2033 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2034
2035 // We will handle these bits from offset, clear them.
2036 Offset &= ~ThisImmVal;
2037
2038 // Get the properly encoded SOImmVal field.
2039 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2040 "Bit extraction didn't work?");
2041 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2042 } else {
2043 unsigned ImmIdx = 0;
2044 int InstrOffs = 0;
2045 unsigned NumBits = 0;
2046 unsigned Scale = 1;
2047 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002048 case ARMII::AddrMode_i12: {
2049 ImmIdx = FrameRegIdx + 1;
2050 InstrOffs = MI.getOperand(ImmIdx).getImm();
2051 NumBits = 12;
2052 break;
2053 }
Evan Cheng780748d2009-07-28 05:48:47 +00002054 case ARMII::AddrMode2: {
2055 ImmIdx = FrameRegIdx+2;
2056 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2057 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2058 InstrOffs *= -1;
2059 NumBits = 12;
2060 break;
2061 }
2062 case ARMII::AddrMode3: {
2063 ImmIdx = FrameRegIdx+2;
2064 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2065 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2066 InstrOffs *= -1;
2067 NumBits = 8;
2068 break;
2069 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002070 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002071 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002072 // Can't fold any offset even if it's zero.
2073 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002074 case ARMII::AddrMode5: {
2075 ImmIdx = FrameRegIdx+1;
2076 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2077 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2078 InstrOffs *= -1;
2079 NumBits = 8;
2080 Scale = 4;
2081 break;
2082 }
2083 default:
2084 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002085 }
2086
2087 Offset += InstrOffs * Scale;
2088 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2089 if (Offset < 0) {
2090 Offset = -Offset;
2091 isSub = true;
2092 }
2093
2094 // Attempt to fold address comp. if opcode has offset bits
2095 if (NumBits > 0) {
2096 // Common case: small offset, fits into instruction.
2097 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2098 int ImmedOffset = Offset / Scale;
2099 unsigned Mask = (1 << NumBits) - 1;
2100 if ((unsigned)Offset <= Mask * Scale) {
2101 // Replace the FrameIndex with sp
2102 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002103 // FIXME: When addrmode2 goes away, this will simplify (like the
2104 // T2 version), as the LDR.i12 versions don't need the encoding
2105 // tricks for the offset value.
2106 if (isSub) {
2107 if (AddrMode == ARMII::AddrMode_i12)
2108 ImmedOffset = -ImmedOffset;
2109 else
2110 ImmedOffset |= 1 << NumBits;
2111 }
Evan Cheng780748d2009-07-28 05:48:47 +00002112 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002113 Offset = 0;
2114 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002115 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002116
Evan Cheng780748d2009-07-28 05:48:47 +00002117 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2118 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002119 if (isSub) {
2120 if (AddrMode == ARMII::AddrMode_i12)
2121 ImmedOffset = -ImmedOffset;
2122 else
2123 ImmedOffset |= 1 << NumBits;
2124 }
Evan Cheng780748d2009-07-28 05:48:47 +00002125 ImmOp.ChangeToImmediate(ImmedOffset);
2126 Offset &= ~(Mask*Scale);
2127 }
2128 }
2129
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002130 Offset = (isSub) ? -Offset : Offset;
2131 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002132}
Bill Wendling7de9d522010-08-06 01:32:48 +00002133
Manman Ren6fa76dc2012-06-29 21:33:59 +00002134/// analyzeCompare - For a comparison instruction, return the source registers
2135/// in SrcReg and SrcReg2 if having two register operands, and the value it
2136/// compares against in CmpValue. Return true if the comparison instruction
2137/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002138bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002139analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2140 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002141 switch (MI->getOpcode()) {
2142 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002143 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002144 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002145 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002146 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002147 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002148 CmpValue = MI->getOperand(1).getImm();
2149 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002150 case ARM::CMPrr:
2151 case ARM::t2CMPrr:
2152 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002153 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002154 CmpMask = ~0;
2155 CmpValue = 0;
2156 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002157 case ARM::TSTri:
2158 case ARM::t2TSTri:
2159 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002160 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002161 CmpMask = MI->getOperand(1).getImm();
2162 CmpValue = 0;
2163 return true;
2164 }
2165
2166 return false;
2167}
2168
Gabor Greifd36e3e82010-09-29 10:12:08 +00002169/// isSuitableForMask - Identify a suitable 'and' instruction that
2170/// operates on the given source register and applies the same mask
2171/// as a 'tst' instruction. Provide a limited look-through for copies.
2172/// When successful, MI will hold the found instruction.
2173static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002174 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002175 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002176 case ARM::ANDri:
2177 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002178 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002179 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002180 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002181 return true;
2182 break;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002183 case ARM::COPY: {
2184 // Walk down one instruction which is potentially an 'and'.
2185 const MachineInstr &Copy = *MI;
Michael J. Spencer70ac5fa2010-10-05 06:00:43 +00002186 MachineBasicBlock::iterator AND(
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002187 std::next(MachineBasicBlock::iterator(MI)));
Gabor Greifd36e3e82010-09-29 10:12:08 +00002188 if (AND == MI->getParent()->end()) return false;
2189 MI = AND;
2190 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
2191 CmpMask, true);
2192 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002193 }
2194
2195 return false;
2196}
2197
Manman Renb1b3db62012-06-29 22:06:19 +00002198/// getSwappedCondition - assume the flags are set by MI(a,b), return
2199/// the condition code if we modify the instructions such that flags are
2200/// set by MI(b,a).
2201inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2202 switch (CC) {
2203 default: return ARMCC::AL;
2204 case ARMCC::EQ: return ARMCC::EQ;
2205 case ARMCC::NE: return ARMCC::NE;
2206 case ARMCC::HS: return ARMCC::LS;
2207 case ARMCC::LO: return ARMCC::HI;
2208 case ARMCC::HI: return ARMCC::LO;
2209 case ARMCC::LS: return ARMCC::HS;
2210 case ARMCC::GE: return ARMCC::LE;
2211 case ARMCC::LT: return ARMCC::GT;
2212 case ARMCC::GT: return ARMCC::LT;
2213 case ARMCC::LE: return ARMCC::GE;
2214 }
2215}
2216
2217/// isRedundantFlagInstr - check whether the first instruction, whose only
2218/// purpose is to update flags, can be made redundant.
2219/// CMPrr can be made redundant by SUBrr if the operands are the same.
2220/// CMPri can be made redundant by SUBri if the operands are the same.
2221/// This function can be extended later on.
2222inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2223 unsigned SrcReg2, int ImmValue,
2224 MachineInstr *OI) {
2225 if ((CmpI->getOpcode() == ARM::CMPrr ||
2226 CmpI->getOpcode() == ARM::t2CMPrr) &&
2227 (OI->getOpcode() == ARM::SUBrr ||
2228 OI->getOpcode() == ARM::t2SUBrr) &&
2229 ((OI->getOperand(1).getReg() == SrcReg &&
2230 OI->getOperand(2).getReg() == SrcReg2) ||
2231 (OI->getOperand(1).getReg() == SrcReg2 &&
2232 OI->getOperand(2).getReg() == SrcReg)))
2233 return true;
2234
2235 if ((CmpI->getOpcode() == ARM::CMPri ||
2236 CmpI->getOpcode() == ARM::t2CMPri) &&
2237 (OI->getOpcode() == ARM::SUBri ||
2238 OI->getOpcode() == ARM::t2SUBri) &&
2239 OI->getOperand(1).getReg() == SrcReg &&
2240 OI->getOperand(2).getImm() == ImmValue)
2241 return true;
2242 return false;
2243}
2244
Manman Ren6fa76dc2012-06-29 21:33:59 +00002245/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2246/// comparison into one that sets the zero bit in the flags register;
2247/// Remove a redundant Compare instruction if an earlier instruction can set the
2248/// flags in the same way as Compare.
2249/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2250/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2251/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002252bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002253optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2254 int CmpMask, int CmpValue,
2255 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002256 // Get the unique definition of SrcReg.
2257 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2258 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002259
Gabor Greifadbbb932010-09-21 12:01:15 +00002260 // Masked compares sometimes use the same register as the corresponding 'and'.
2261 if (CmpMask != ~0) {
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002262 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002263 MI = nullptr;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002264 for (MachineRegisterInfo::use_instr_iterator
2265 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2266 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002267 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002268 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002269 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2270 isPredicated(PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002271 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002272 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002273 break;
2274 }
2275 if (!MI) return false;
2276 }
2277 }
2278
Manman Rendc8ad002012-05-11 01:30:47 +00002279 // Get ready to iterate backward from CmpInstr.
2280 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2281 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002282
2283 // Early exit if CmpInstr is at the beginning of the BB.
2284 if (I == B) return false;
2285
Manman Rendc8ad002012-05-11 01:30:47 +00002286 // There are two possible candidates which can be changed to set CPSR:
2287 // One is MI, the other is a SUB instruction.
2288 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2289 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
Craig Topper062a2ba2014-04-25 05:30:21 +00002290 MachineInstr *Sub = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002291 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002292 // MI is not a candidate for CMPrr.
Craig Topper062a2ba2014-04-25 05:30:21 +00002293 MI = nullptr;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002294 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002295 // Conservatively refuse to convert an instruction which isn't in the same
2296 // BB as the comparison.
2297 // For CMPri, we need to check Sub, thus we can't return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002298 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002299 CmpInstr->getOpcode() == ARM::t2CMPri)
Craig Topper062a2ba2014-04-25 05:30:21 +00002300 MI = nullptr;
Manman Rendc8ad002012-05-11 01:30:47 +00002301 else
2302 return false;
2303 }
2304
2305 // Check that CPSR isn't set between the comparison instruction and the one we
2306 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002307 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002308 --I;
2309 for (; I != E; --I) {
2310 const MachineInstr &Instr = *I;
2311
Manman Renb1b3db62012-06-29 22:06:19 +00002312 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2313 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002314 // This instruction modifies or uses CPSR after the one we want to
2315 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002316 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002317
Manman Renb1b3db62012-06-29 22:06:19 +00002318 // Check whether CmpInstr can be made redundant by the current instruction.
2319 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002320 Sub = &*I;
2321 break;
2322 }
2323
Evan Chengd757c882010-09-21 23:49:07 +00002324 if (I == B)
2325 // The 'and' is below the comparison instruction.
2326 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002327 }
2328
Manman Rendc8ad002012-05-11 01:30:47 +00002329 // Return false if no candidates exist.
2330 if (!MI && !Sub)
2331 return false;
2332
2333 // The single candidate is called MI.
2334 if (!MI) MI = Sub;
2335
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002336 // We can't use a predicated instruction - it doesn't always write the flags.
2337 if (isPredicated(MI))
2338 return false;
2339
Bill Wendling7de9d522010-08-06 01:32:48 +00002340 switch (MI->getOpcode()) {
2341 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002342 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002343 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002344 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002345 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002346 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002347 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002348 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002349 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002350 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002351 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002352 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002353 case ARM::SBCri:
2354 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002355 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002356 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002357 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002358 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002359 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002360 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002361 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002362 case ARM::t2SBCri:
2363 case ARM::ANDrr:
2364 case ARM::ANDri:
2365 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002366 case ARM::t2ANDri:
2367 case ARM::ORRrr:
2368 case ARM::ORRri:
2369 case ARM::t2ORRrr:
2370 case ARM::t2ORRri:
2371 case ARM::EORrr:
2372 case ARM::EORri:
2373 case ARM::t2EORrr:
2374 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002375 // Scan forward for the use of CPSR
2376 // When checking against MI: if it's a conditional code requires
Manman Ren34cb93e2012-07-11 22:51:44 +00002377 // checking of V bit, then this is not safe to do.
2378 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2379 // If we are done with the basic block, we need to check whether CPSR is
2380 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002381 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2382 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002383 bool isSafe = false;
2384 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002385 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002386 while (!isSafe && ++I != E) {
2387 const MachineInstr &Instr = *I;
2388 for (unsigned IO = 0, EO = Instr.getNumOperands();
2389 !isSafe && IO != EO; ++IO) {
2390 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002391 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2392 isSafe = true;
2393 break;
2394 }
Evan Cheng425489d2011-03-23 22:52:04 +00002395 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2396 continue;
2397 if (MO.isDef()) {
2398 isSafe = true;
2399 break;
2400 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002401 // Condition code is after the operand before CPSR except for VSELs.
2402 ARMCC::CondCodes CC;
2403 bool IsInstrVSel = true;
2404 switch (Instr.getOpcode()) {
2405 default:
2406 IsInstrVSel = false;
2407 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2408 break;
2409 case ARM::VSELEQD:
2410 case ARM::VSELEQS:
2411 CC = ARMCC::EQ;
2412 break;
2413 case ARM::VSELGTD:
2414 case ARM::VSELGTS:
2415 CC = ARMCC::GT;
2416 break;
2417 case ARM::VSELGED:
2418 case ARM::VSELGES:
2419 CC = ARMCC::GE;
2420 break;
2421 case ARM::VSELVSS:
2422 case ARM::VSELVSD:
2423 CC = ARMCC::VS;
2424 break;
2425 }
2426
Manman Renb1b3db62012-06-29 22:06:19 +00002427 if (Sub) {
2428 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2429 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002430 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002431 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2432 // on CMP needs to be updated to be based on SUB.
2433 // Push the condition code operands to OperandsToUpdate.
2434 // If it is safe to remove CmpInstr, the condition code of these
2435 // operands will be modified.
2436 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002437 Sub->getOperand(2).getReg() == SrcReg) {
2438 // VSel doesn't support condition code update.
2439 if (IsInstrVSel)
2440 return false;
2441 OperandsToUpdate.push_back(
2442 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2443 }
2444 } else
Manman Rendc8ad002012-05-11 01:30:47 +00002445 switch (CC) {
2446 default:
Manman Ren88a0d332012-07-11 23:47:00 +00002447 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002448 break;
2449 case ARMCC::VS:
2450 case ARMCC::VC:
2451 case ARMCC::GE:
2452 case ARMCC::LT:
2453 case ARMCC::GT:
2454 case ARMCC::LE:
2455 return false;
2456 }
Evan Cheng425489d2011-03-23 22:52:04 +00002457 }
2458 }
2459
Manman Ren34cb93e2012-07-11 22:51:44 +00002460 // If CPSR is not killed nor re-defined, we should check whether it is
2461 // live-out. If it is live-out, do not optimize.
2462 if (!isSafe) {
2463 MachineBasicBlock *MBB = CmpInstr->getParent();
2464 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2465 SE = MBB->succ_end(); SI != SE; ++SI)
2466 if ((*SI)->isLiveIn(ARM::CPSR))
2467 return false;
2468 }
Evan Cheng425489d2011-03-23 22:52:04 +00002469
Evan Cheng65536472010-11-17 08:06:50 +00002470 // Toggle the optional operand to CPSR.
2471 MI->getOperand(5).setReg(ARM::CPSR);
2472 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002473 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002474 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002475
2476 // Modify the condition code of operands in OperandsToUpdate.
2477 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2478 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002479 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2480 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002481 return true;
2482 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002483 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002484
2485 return false;
2486}
Evan Cheng367a5df2010-09-09 18:18:55 +00002487
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002488bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2489 MachineInstr *DefMI, unsigned Reg,
2490 MachineRegisterInfo *MRI) const {
2491 // Fold large immediates into add, sub, or, xor.
2492 unsigned DefOpc = DefMI->getOpcode();
2493 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2494 return false;
2495 if (!DefMI->getOperand(1).isImm())
2496 // Could be t2MOVi32imm <ga:xx>
2497 return false;
2498
2499 if (!MRI->hasOneNonDBGUse(Reg))
2500 return false;
2501
Evan Chenga2b48d92012-03-26 23:31:00 +00002502 const MCInstrDesc &DefMCID = DefMI->getDesc();
2503 if (DefMCID.hasOptionalDef()) {
2504 unsigned NumOps = DefMCID.getNumOperands();
2505 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2506 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2507 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2508 // to delete DefMI.
2509 return false;
2510 }
2511
2512 const MCInstrDesc &UseMCID = UseMI->getDesc();
2513 if (UseMCID.hasOptionalDef()) {
2514 unsigned NumOps = UseMCID.getNumOperands();
2515 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2516 // If the instruction sets the flag, do not attempt this optimization
2517 // since it may change the semantics of the code.
2518 return false;
2519 }
2520
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002521 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002522 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002523 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002524 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002525 bool Commute = false;
2526 switch (UseOpc) {
2527 default: return false;
2528 case ARM::SUBrr:
2529 case ARM::ADDrr:
2530 case ARM::ORRrr:
2531 case ARM::EORrr:
2532 case ARM::t2SUBrr:
2533 case ARM::t2ADDrr:
2534 case ARM::t2ORRrr:
2535 case ARM::t2EORrr: {
2536 Commute = UseMI->getOperand(2).getReg() != Reg;
2537 switch (UseOpc) {
2538 default: break;
2539 case ARM::SUBrr: {
2540 if (Commute)
2541 return false;
2542 ImmVal = -ImmVal;
2543 NewUseOpc = ARM::SUBri;
2544 // Fallthrough
2545 }
2546 case ARM::ADDrr:
2547 case ARM::ORRrr:
2548 case ARM::EORrr: {
2549 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2550 return false;
2551 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2552 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2553 switch (UseOpc) {
2554 default: break;
2555 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2556 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2557 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2558 }
2559 break;
2560 }
2561 case ARM::t2SUBrr: {
2562 if (Commute)
2563 return false;
2564 ImmVal = -ImmVal;
2565 NewUseOpc = ARM::t2SUBri;
2566 // Fallthrough
2567 }
2568 case ARM::t2ADDrr:
2569 case ARM::t2ORRrr:
2570 case ARM::t2EORrr: {
2571 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2572 return false;
2573 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2574 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2575 switch (UseOpc) {
2576 default: break;
2577 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2578 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2579 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2580 }
2581 break;
2582 }
2583 }
2584 }
2585 }
2586
2587 unsigned OpIdx = Commute ? 2 : 1;
2588 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2589 bool isKill = UseMI->getOperand(OpIdx).isKill();
2590 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2591 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002592 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002593 get(NewUseOpc), NewReg)
2594 .addReg(Reg1, getKillRegState(isKill))
2595 .addImm(SOImmValV1)));
2596 UseMI->setDesc(get(NewUseOpc));
2597 UseMI->getOperand(1).setReg(NewReg);
2598 UseMI->getOperand(1).setIsKill();
2599 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2600 DefMI->eraseFromParent();
2601 return true;
2602}
2603
Bob Wilsone8a549c2012-09-29 21:43:49 +00002604static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2605 const MachineInstr *MI) {
2606 switch (MI->getOpcode()) {
2607 default: {
2608 const MCInstrDesc &Desc = MI->getDesc();
2609 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2610 assert(UOps >= 0 && "bad # UOps");
2611 return UOps;
2612 }
2613
2614 case ARM::LDRrs:
2615 case ARM::LDRBrs:
2616 case ARM::STRrs:
2617 case ARM::STRBrs: {
2618 unsigned ShOpVal = MI->getOperand(3).getImm();
2619 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2620 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2621 if (!isSub &&
2622 (ShImm == 0 ||
2623 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2624 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2625 return 1;
2626 return 2;
2627 }
2628
2629 case ARM::LDRH:
2630 case ARM::STRH: {
2631 if (!MI->getOperand(2).getReg())
2632 return 1;
2633
2634 unsigned ShOpVal = MI->getOperand(3).getImm();
2635 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2636 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2637 if (!isSub &&
2638 (ShImm == 0 ||
2639 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2640 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2641 return 1;
2642 return 2;
2643 }
2644
2645 case ARM::LDRSB:
2646 case ARM::LDRSH:
2647 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2648
2649 case ARM::LDRSB_POST:
2650 case ARM::LDRSH_POST: {
2651 unsigned Rt = MI->getOperand(0).getReg();
2652 unsigned Rm = MI->getOperand(3).getReg();
2653 return (Rt == Rm) ? 4 : 3;
2654 }
2655
2656 case ARM::LDR_PRE_REG:
2657 case ARM::LDRB_PRE_REG: {
2658 unsigned Rt = MI->getOperand(0).getReg();
2659 unsigned Rm = MI->getOperand(3).getReg();
2660 if (Rt == Rm)
2661 return 3;
2662 unsigned ShOpVal = MI->getOperand(4).getImm();
2663 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2664 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2665 if (!isSub &&
2666 (ShImm == 0 ||
2667 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2668 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2669 return 2;
2670 return 3;
2671 }
2672
2673 case ARM::STR_PRE_REG:
2674 case ARM::STRB_PRE_REG: {
2675 unsigned ShOpVal = MI->getOperand(4).getImm();
2676 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2677 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2678 if (!isSub &&
2679 (ShImm == 0 ||
2680 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2681 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2682 return 2;
2683 return 3;
2684 }
2685
2686 case ARM::LDRH_PRE:
2687 case ARM::STRH_PRE: {
2688 unsigned Rt = MI->getOperand(0).getReg();
2689 unsigned Rm = MI->getOperand(3).getReg();
2690 if (!Rm)
2691 return 2;
2692 if (Rt == Rm)
2693 return 3;
2694 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2695 ? 3 : 2;
2696 }
2697
2698 case ARM::LDR_POST_REG:
2699 case ARM::LDRB_POST_REG:
2700 case ARM::LDRH_POST: {
2701 unsigned Rt = MI->getOperand(0).getReg();
2702 unsigned Rm = MI->getOperand(3).getReg();
2703 return (Rt == Rm) ? 3 : 2;
2704 }
2705
2706 case ARM::LDR_PRE_IMM:
2707 case ARM::LDRB_PRE_IMM:
2708 case ARM::LDR_POST_IMM:
2709 case ARM::LDRB_POST_IMM:
2710 case ARM::STRB_POST_IMM:
2711 case ARM::STRB_POST_REG:
2712 case ARM::STRB_PRE_IMM:
2713 case ARM::STRH_POST:
2714 case ARM::STR_POST_IMM:
2715 case ARM::STR_POST_REG:
2716 case ARM::STR_PRE_IMM:
2717 return 2;
2718
2719 case ARM::LDRSB_PRE:
2720 case ARM::LDRSH_PRE: {
2721 unsigned Rm = MI->getOperand(3).getReg();
2722 if (Rm == 0)
2723 return 3;
2724 unsigned Rt = MI->getOperand(0).getReg();
2725 if (Rt == Rm)
2726 return 4;
2727 unsigned ShOpVal = MI->getOperand(4).getImm();
2728 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2729 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2730 if (!isSub &&
2731 (ShImm == 0 ||
2732 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2733 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2734 return 3;
2735 return 4;
2736 }
2737
2738 case ARM::LDRD: {
2739 unsigned Rt = MI->getOperand(0).getReg();
2740 unsigned Rn = MI->getOperand(2).getReg();
2741 unsigned Rm = MI->getOperand(3).getReg();
2742 if (Rm)
2743 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2744 return (Rt == Rn) ? 3 : 2;
2745 }
2746
2747 case ARM::STRD: {
2748 unsigned Rm = MI->getOperand(3).getReg();
2749 if (Rm)
2750 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2751 return 2;
2752 }
2753
2754 case ARM::LDRD_POST:
2755 case ARM::t2LDRD_POST:
2756 return 3;
2757
2758 case ARM::STRD_POST:
2759 case ARM::t2STRD_POST:
2760 return 4;
2761
2762 case ARM::LDRD_PRE: {
2763 unsigned Rt = MI->getOperand(0).getReg();
2764 unsigned Rn = MI->getOperand(3).getReg();
2765 unsigned Rm = MI->getOperand(4).getReg();
2766 if (Rm)
2767 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2768 return (Rt == Rn) ? 4 : 3;
2769 }
2770
2771 case ARM::t2LDRD_PRE: {
2772 unsigned Rt = MI->getOperand(0).getReg();
2773 unsigned Rn = MI->getOperand(3).getReg();
2774 return (Rt == Rn) ? 4 : 3;
2775 }
2776
2777 case ARM::STRD_PRE: {
2778 unsigned Rm = MI->getOperand(4).getReg();
2779 if (Rm)
2780 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2781 return 3;
2782 }
2783
2784 case ARM::t2STRD_PRE:
2785 return 3;
2786
2787 case ARM::t2LDR_POST:
2788 case ARM::t2LDRB_POST:
2789 case ARM::t2LDRB_PRE:
2790 case ARM::t2LDRSBi12:
2791 case ARM::t2LDRSBi8:
2792 case ARM::t2LDRSBpci:
2793 case ARM::t2LDRSBs:
2794 case ARM::t2LDRH_POST:
2795 case ARM::t2LDRH_PRE:
2796 case ARM::t2LDRSBT:
2797 case ARM::t2LDRSB_POST:
2798 case ARM::t2LDRSB_PRE:
2799 case ARM::t2LDRSH_POST:
2800 case ARM::t2LDRSH_PRE:
2801 case ARM::t2LDRSHi12:
2802 case ARM::t2LDRSHi8:
2803 case ARM::t2LDRSHpci:
2804 case ARM::t2LDRSHs:
2805 return 2;
2806
2807 case ARM::t2LDRDi8: {
2808 unsigned Rt = MI->getOperand(0).getReg();
2809 unsigned Rn = MI->getOperand(2).getReg();
2810 return (Rt == Rn) ? 3 : 2;
2811 }
2812
2813 case ARM::t2STRB_POST:
2814 case ARM::t2STRB_PRE:
2815 case ARM::t2STRBs:
2816 case ARM::t2STRDi8:
2817 case ARM::t2STRH_POST:
2818 case ARM::t2STRH_PRE:
2819 case ARM::t2STRHs:
2820 case ARM::t2STR_POST:
2821 case ARM::t2STR_PRE:
2822 case ARM::t2STRs:
2823 return 2;
2824 }
2825}
2826
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002827// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2828// can't be easily determined return 0 (missing MachineMemOperand).
2829//
2830// FIXME: The current MachineInstr design does not support relying on machine
2831// mem operands to determine the width of a memory access. Instead, we expect
2832// the target to provide this information based on the instruction opcode and
2833// operands. However, using MachineMemOperand is a the best solution now for
2834// two reasons:
2835//
2836// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2837// operands. This is much more dangerous than using the MachineMemOperand
2838// sizes because CodeGen passes can insert/remove optional machine operands. In
2839// fact, it's totally incorrect for preRA passes and appears to be wrong for
2840// postRA passes as well.
2841//
2842// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2843// machine model that calls this should handle the unknown (zero size) case.
2844//
2845// Long term, we should require a target hook that verifies MachineMemOperand
2846// sizes during MC lowering. That target hook should be local to MC lowering
2847// because we can't ensure that it is aware of other MI forms. Doing this will
2848// ensure that MachineMemOperands are correctly propagated through all passes.
2849unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2850 unsigned Size = 0;
2851 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2852 E = MI->memoperands_end(); I != E; ++I) {
2853 Size += (*I)->getSize();
2854 }
2855 return Size / 4;
2856}
2857
Evan Cheng367a5df2010-09-09 18:18:55 +00002858unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00002859ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2860 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00002861 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00002862 return 1;
2863
Evan Cheng6cc775f2011-06-28 19:10:37 +00002864 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00002865 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00002866 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00002867 if (ItinUOps >= 0) {
2868 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2869 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2870
Andrew Trickf161e392012-07-02 18:10:42 +00002871 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002872 }
Evan Cheng367a5df2010-09-09 18:18:55 +00002873
2874 unsigned Opc = MI->getOpcode();
2875 switch (Opc) {
2876 default:
2877 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002878 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002879 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00002880 return 2;
2881
2882 // The number of uOps for load / store multiple are determined by the number
2883 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00002884 //
Evan Chengbf407072010-09-10 01:29:16 +00002885 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2886 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00002887 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002888 //
Evan Chengbf407072010-09-10 01:29:16 +00002889 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002890 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
2891 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
2892 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002893 case ARM::VLDMDIA_UPD:
2894 case ARM::VLDMDDB_UPD:
2895 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002896 case ARM::VLDMSIA_UPD:
2897 case ARM::VLDMSDB_UPD:
2898 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002899 case ARM::VSTMDIA_UPD:
2900 case ARM::VSTMDDB_UPD:
2901 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002902 case ARM::VSTMSIA_UPD:
2903 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00002904 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
2905 return (NumRegs / 2) + (NumRegs % 2) + 1;
2906 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002907
2908 case ARM::LDMIA_RET:
2909 case ARM::LDMIA:
2910 case ARM::LDMDA:
2911 case ARM::LDMDB:
2912 case ARM::LDMIB:
2913 case ARM::LDMIA_UPD:
2914 case ARM::LDMDA_UPD:
2915 case ARM::LDMDB_UPD:
2916 case ARM::LDMIB_UPD:
2917 case ARM::STMIA:
2918 case ARM::STMDA:
2919 case ARM::STMDB:
2920 case ARM::STMIB:
2921 case ARM::STMIA_UPD:
2922 case ARM::STMDA_UPD:
2923 case ARM::STMDB_UPD:
2924 case ARM::STMIB_UPD:
2925 case ARM::tLDMIA:
2926 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002927 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00002928 case ARM::tPOP_RET:
2929 case ARM::tPOP:
2930 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002931 case ARM::t2LDMIA_RET:
2932 case ARM::t2LDMIA:
2933 case ARM::t2LDMDB:
2934 case ARM::t2LDMIA_UPD:
2935 case ARM::t2LDMDB_UPD:
2936 case ARM::t2STMIA:
2937 case ARM::t2STMDB:
2938 case ARM::t2STMIA_UPD:
2939 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00002940 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002941 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00002942 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
2943 switch (Opc) {
2944 default: break;
2945 case ARM::VLDMDIA_UPD:
2946 case ARM::VLDMDDB_UPD:
2947 case ARM::VLDMSIA_UPD:
2948 case ARM::VLDMSDB_UPD:
2949 case ARM::VSTMDIA_UPD:
2950 case ARM::VSTMDDB_UPD:
2951 case ARM::VSTMSIA_UPD:
2952 case ARM::VSTMSDB_UPD:
2953 case ARM::LDMIA_UPD:
2954 case ARM::LDMDA_UPD:
2955 case ARM::LDMDB_UPD:
2956 case ARM::LDMIB_UPD:
2957 case ARM::STMIA_UPD:
2958 case ARM::STMDA_UPD:
2959 case ARM::STMDB_UPD:
2960 case ARM::STMIB_UPD:
2961 case ARM::tLDMIA_UPD:
2962 case ARM::tSTMIA_UPD:
2963 case ARM::t2LDMIA_UPD:
2964 case ARM::t2LDMDB_UPD:
2965 case ARM::t2STMIA_UPD:
2966 case ARM::t2STMDB_UPD:
2967 ++UOps; // One for base register writeback.
2968 break;
2969 case ARM::LDMIA_RET:
2970 case ARM::tPOP_RET:
2971 case ARM::t2LDMIA_RET:
2972 UOps += 2; // One for base reg wb, one for write to pc.
2973 break;
2974 }
2975 return UOps;
Tim Northover0feb91e2014-04-01 14:10:07 +00002976 } else if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00002977 if (NumRegs < 4)
2978 return 2;
2979 // 4 registers would be issued: 2, 2.
2980 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00002981 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00002982 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00002983 ++A8UOps;
2984 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002985 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00002986 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00002987 // If there are odd number of registers or if it's not 64-bit aligned,
2988 // then it takes an extra AGU (Address Generation Unit) cycle.
2989 if ((NumRegs % 2) ||
2990 !MI->hasOneMemOperand() ||
2991 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00002992 ++A9UOps;
2993 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00002994 } else {
2995 // Assume the worst.
2996 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00002997 }
Evan Cheng367a5df2010-09-09 18:18:55 +00002998 }
2999 }
3000}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003001
3002int
Evan Cheng412e37b2010-10-07 23:12:15 +00003003ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003004 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003005 unsigned DefClass,
3006 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003007 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003008 if (RegNo <= 0)
3009 // Def is the address writeback.
3010 return ItinData->getOperandCycle(DefClass, DefIdx);
3011
3012 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003013 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003014 // (regno / 2) + (regno % 2) + 1
3015 DefCycle = RegNo / 2 + 1;
3016 if (RegNo % 2)
3017 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003018 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003019 DefCycle = RegNo;
3020 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003021
Evan Cheng6cc775f2011-06-28 19:10:37 +00003022 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003023 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003024 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003025 case ARM::VLDMSIA_UPD:
3026 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003027 isSLoad = true;
3028 break;
3029 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003030
Evan Cheng412e37b2010-10-07 23:12:15 +00003031 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3032 // then it takes an extra cycle.
3033 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3034 ++DefCycle;
3035 } else {
3036 // Assume the worst.
3037 DefCycle = RegNo + 2;
3038 }
3039
3040 return DefCycle;
3041}
3042
3043int
3044ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003045 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003046 unsigned DefClass,
3047 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003048 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003049 if (RegNo <= 0)
3050 // Def is the address writeback.
3051 return ItinData->getOperandCycle(DefClass, DefIdx);
3052
3053 int DefCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003054 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003055 // 4 registers would be issued: 1, 2, 1.
3056 // 5 registers would be issued: 1, 2, 2.
3057 DefCycle = RegNo / 2;
3058 if (DefCycle < 1)
3059 DefCycle = 1;
3060 // Result latency is issue cycle + 2: E2.
3061 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003062 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003063 DefCycle = (RegNo / 2);
3064 // If there are odd number of registers or if it's not 64-bit aligned,
3065 // then it takes an extra AGU (Address Generation Unit) cycle.
3066 if ((RegNo % 2) || DefAlign < 8)
3067 ++DefCycle;
3068 // Result latency is AGU cycles + 2.
3069 DefCycle += 2;
3070 } else {
3071 // Assume the worst.
3072 DefCycle = RegNo + 2;
3073 }
3074
3075 return DefCycle;
3076}
3077
3078int
3079ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003080 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003081 unsigned UseClass,
3082 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003083 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003084 if (RegNo <= 0)
3085 return ItinData->getOperandCycle(UseClass, UseIdx);
3086
3087 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003088 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003089 // (regno / 2) + (regno % 2) + 1
3090 UseCycle = RegNo / 2 + 1;
3091 if (RegNo % 2)
3092 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003093 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003094 UseCycle = RegNo;
3095 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003096
Evan Cheng6cc775f2011-06-28 19:10:37 +00003097 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003098 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003099 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003100 case ARM::VSTMSIA_UPD:
3101 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003102 isSStore = true;
3103 break;
3104 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003105
Evan Cheng412e37b2010-10-07 23:12:15 +00003106 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3107 // then it takes an extra cycle.
3108 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3109 ++UseCycle;
3110 } else {
3111 // Assume the worst.
3112 UseCycle = RegNo + 2;
3113 }
3114
3115 return UseCycle;
3116}
3117
3118int
3119ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003120 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003121 unsigned UseClass,
3122 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003123 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003124 if (RegNo <= 0)
3125 return ItinData->getOperandCycle(UseClass, UseIdx);
3126
3127 int UseCycle;
Tim Northover0feb91e2014-04-01 14:10:07 +00003128 if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003129 UseCycle = RegNo / 2;
3130 if (UseCycle < 2)
3131 UseCycle = 2;
3132 // Read in E3.
3133 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003134 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003135 UseCycle = (RegNo / 2);
3136 // If there are odd number of registers or if it's not 64-bit aligned,
3137 // then it takes an extra AGU (Address Generation Unit) cycle.
3138 if ((RegNo % 2) || UseAlign < 8)
3139 ++UseCycle;
3140 } else {
3141 // Assume the worst.
3142 UseCycle = 1;
3143 }
3144 return UseCycle;
3145}
3146
3147int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003148ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003149 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003150 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003151 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003152 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003153 unsigned DefClass = DefMCID.getSchedClass();
3154 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003155
Evan Cheng6cc775f2011-06-28 19:10:37 +00003156 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003157 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3158
3159 // This may be a def / use of a variable_ops instruction, the operand
3160 // latency might be determinable dynamically. Let the target try to
3161 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003162 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003163 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003164 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003165 default:
3166 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3167 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003168
3169 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003170 case ARM::VLDMDIA_UPD:
3171 case ARM::VLDMDDB_UPD:
3172 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003173 case ARM::VLDMSIA_UPD:
3174 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003175 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003176 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003177
3178 case ARM::LDMIA_RET:
3179 case ARM::LDMIA:
3180 case ARM::LDMDA:
3181 case ARM::LDMDB:
3182 case ARM::LDMIB:
3183 case ARM::LDMIA_UPD:
3184 case ARM::LDMDA_UPD:
3185 case ARM::LDMDB_UPD:
3186 case ARM::LDMIB_UPD:
3187 case ARM::tLDMIA:
3188 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003189 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003190 case ARM::t2LDMIA_RET:
3191 case ARM::t2LDMIA:
3192 case ARM::t2LDMDB:
3193 case ARM::t2LDMIA_UPD:
3194 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003195 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003196 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003197 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003198 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003199
3200 if (DefCycle == -1)
3201 // We can't seem to determine the result latency of the def, assume it's 2.
3202 DefCycle = 2;
3203
3204 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003205 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003206 default:
3207 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3208 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003209
3210 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003211 case ARM::VSTMDIA_UPD:
3212 case ARM::VSTMDDB_UPD:
3213 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003214 case ARM::VSTMSIA_UPD:
3215 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003216 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003217 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003218
3219 case ARM::STMIA:
3220 case ARM::STMDA:
3221 case ARM::STMDB:
3222 case ARM::STMIB:
3223 case ARM::STMIA_UPD:
3224 case ARM::STMDA_UPD:
3225 case ARM::STMDB_UPD:
3226 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003227 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003228 case ARM::tPOP_RET:
3229 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003230 case ARM::t2STMIA:
3231 case ARM::t2STMDB:
3232 case ARM::t2STMIA_UPD:
3233 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003234 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003235 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003236 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003237
3238 if (UseCycle == -1)
3239 // Assume it's read in the first stage.
3240 UseCycle = 1;
3241
3242 UseCycle = DefCycle - UseCycle + 1;
3243 if (UseCycle > 0) {
3244 if (LdmBypass) {
3245 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3246 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003247 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003248 UseClass, UseIdx))
3249 --UseCycle;
3250 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003251 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003252 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003253 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003254 }
3255
3256 return UseCycle;
3257}
3258
Evan Cheng7fae11b2011-12-14 02:11:42 +00003259static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003260 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003261 unsigned &DefIdx, unsigned &Dist) {
3262 Dist = 0;
3263
3264 MachineBasicBlock::const_iterator I = MI; ++I;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003265 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003266 assert(II->isInsideBundle() && "Empty bundle?");
3267
3268 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003269 while (II->isInsideBundle()) {
3270 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3271 if (Idx != -1)
3272 break;
3273 --II;
3274 ++Dist;
3275 }
3276
3277 assert(Idx != -1 && "Cannot find bundled definition!");
3278 DefIdx = Idx;
3279 return II;
3280}
3281
3282static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003283 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003284 unsigned &UseIdx, unsigned &Dist) {
3285 Dist = 0;
3286
3287 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3288 assert(II->isInsideBundle() && "Empty bundle?");
3289 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3290
3291 // FIXME: This doesn't properly handle multiple uses.
3292 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003293 while (II != E && II->isInsideBundle()) {
3294 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3295 if (Idx != -1)
3296 break;
3297 if (II->getOpcode() != ARM::t2IT)
3298 ++Dist;
3299 ++II;
3300 }
3301
Evan Chengda103bf2011-12-14 20:00:08 +00003302 if (Idx == -1) {
3303 Dist = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00003304 return nullptr;
Evan Chengda103bf2011-12-14 20:00:08 +00003305 }
3306
Evan Cheng7fae11b2011-12-14 02:11:42 +00003307 UseIdx = Idx;
3308 return II;
3309}
3310
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003311/// Return the number of cycles to add to (or subtract from) the static
3312/// itinerary based on the def opcode and alignment. The caller will ensure that
3313/// adjusted latency is at least one cycle.
3314static int adjustDefLatency(const ARMSubtarget &Subtarget,
3315 const MachineInstr *DefMI,
3316 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3317 int Adjust = 0;
Tim Northover0feb91e2014-04-01 14:10:07 +00003318 if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
Evan Chengff310732010-10-28 06:47:08 +00003319 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3320 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003321 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003322 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003323 case ARM::LDRrs:
3324 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003325 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3326 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3327 if (ShImm == 0 ||
3328 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003329 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003330 break;
3331 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003332 case ARM::t2LDRs:
3333 case ARM::t2LDRBs:
3334 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003335 case ARM::t2LDRSHs: {
3336 // Thumb2 mode: lsl only.
3337 unsigned ShAmt = DefMI->getOperand(3).getImm();
3338 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003339 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003340 break;
3341 }
3342 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003343 } else if (Subtarget.isSwift()) {
3344 // FIXME: Properly handle all of the latency adjustments for address
3345 // writeback.
3346 switch (DefMCID->getOpcode()) {
3347 default: break;
3348 case ARM::LDRrs:
3349 case ARM::LDRBrs: {
3350 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3351 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3352 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3353 if (!isSub &&
3354 (ShImm == 0 ||
3355 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3356 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3357 Adjust -= 2;
3358 else if (!isSub &&
3359 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3360 --Adjust;
3361 break;
3362 }
3363 case ARM::t2LDRs:
3364 case ARM::t2LDRBs:
3365 case ARM::t2LDRHs:
3366 case ARM::t2LDRSHs: {
3367 // Thumb2 mode: lsl only.
3368 unsigned ShAmt = DefMI->getOperand(3).getImm();
3369 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3370 Adjust -= 2;
3371 break;
3372 }
3373 }
Evan Chengff310732010-10-28 06:47:08 +00003374 }
3375
Silviu Barangab47bb942012-09-13 15:05:10 +00003376 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003377 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003378 default: break;
3379 case ARM::VLD1q8:
3380 case ARM::VLD1q16:
3381 case ARM::VLD1q32:
3382 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003383 case ARM::VLD1q8wb_fixed:
3384 case ARM::VLD1q16wb_fixed:
3385 case ARM::VLD1q32wb_fixed:
3386 case ARM::VLD1q64wb_fixed:
3387 case ARM::VLD1q8wb_register:
3388 case ARM::VLD1q16wb_register:
3389 case ARM::VLD1q32wb_register:
3390 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003391 case ARM::VLD2d8:
3392 case ARM::VLD2d16:
3393 case ARM::VLD2d32:
3394 case ARM::VLD2q8:
3395 case ARM::VLD2q16:
3396 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003397 case ARM::VLD2d8wb_fixed:
3398 case ARM::VLD2d16wb_fixed:
3399 case ARM::VLD2d32wb_fixed:
3400 case ARM::VLD2q8wb_fixed:
3401 case ARM::VLD2q16wb_fixed:
3402 case ARM::VLD2q32wb_fixed:
3403 case ARM::VLD2d8wb_register:
3404 case ARM::VLD2d16wb_register:
3405 case ARM::VLD2d32wb_register:
3406 case ARM::VLD2q8wb_register:
3407 case ARM::VLD2q16wb_register:
3408 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003409 case ARM::VLD3d8:
3410 case ARM::VLD3d16:
3411 case ARM::VLD3d32:
3412 case ARM::VLD1d64T:
3413 case ARM::VLD3d8_UPD:
3414 case ARM::VLD3d16_UPD:
3415 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003416 case ARM::VLD1d64Twb_fixed:
3417 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003418 case ARM::VLD3q8_UPD:
3419 case ARM::VLD3q16_UPD:
3420 case ARM::VLD3q32_UPD:
3421 case ARM::VLD4d8:
3422 case ARM::VLD4d16:
3423 case ARM::VLD4d32:
3424 case ARM::VLD1d64Q:
3425 case ARM::VLD4d8_UPD:
3426 case ARM::VLD4d16_UPD:
3427 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003428 case ARM::VLD1d64Qwb_fixed:
3429 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003430 case ARM::VLD4q8_UPD:
3431 case ARM::VLD4q16_UPD:
3432 case ARM::VLD4q32_UPD:
3433 case ARM::VLD1DUPq8:
3434 case ARM::VLD1DUPq16:
3435 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003436 case ARM::VLD1DUPq8wb_fixed:
3437 case ARM::VLD1DUPq16wb_fixed:
3438 case ARM::VLD1DUPq32wb_fixed:
3439 case ARM::VLD1DUPq8wb_register:
3440 case ARM::VLD1DUPq16wb_register:
3441 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003442 case ARM::VLD2DUPd8:
3443 case ARM::VLD2DUPd16:
3444 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003445 case ARM::VLD2DUPd8wb_fixed:
3446 case ARM::VLD2DUPd16wb_fixed:
3447 case ARM::VLD2DUPd32wb_fixed:
3448 case ARM::VLD2DUPd8wb_register:
3449 case ARM::VLD2DUPd16wb_register:
3450 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003451 case ARM::VLD4DUPd8:
3452 case ARM::VLD4DUPd16:
3453 case ARM::VLD4DUPd32:
3454 case ARM::VLD4DUPd8_UPD:
3455 case ARM::VLD4DUPd16_UPD:
3456 case ARM::VLD4DUPd32_UPD:
3457 case ARM::VLD1LNd8:
3458 case ARM::VLD1LNd16:
3459 case ARM::VLD1LNd32:
3460 case ARM::VLD1LNd8_UPD:
3461 case ARM::VLD1LNd16_UPD:
3462 case ARM::VLD1LNd32_UPD:
3463 case ARM::VLD2LNd8:
3464 case ARM::VLD2LNd16:
3465 case ARM::VLD2LNd32:
3466 case ARM::VLD2LNq16:
3467 case ARM::VLD2LNq32:
3468 case ARM::VLD2LNd8_UPD:
3469 case ARM::VLD2LNd16_UPD:
3470 case ARM::VLD2LNd32_UPD:
3471 case ARM::VLD2LNq16_UPD:
3472 case ARM::VLD2LNq32_UPD:
3473 case ARM::VLD4LNd8:
3474 case ARM::VLD4LNd16:
3475 case ARM::VLD4LNd32:
3476 case ARM::VLD4LNq16:
3477 case ARM::VLD4LNq32:
3478 case ARM::VLD4LNd8_UPD:
3479 case ARM::VLD4LNd16_UPD:
3480 case ARM::VLD4LNd32_UPD:
3481 case ARM::VLD4LNq16_UPD:
3482 case ARM::VLD4LNq32_UPD:
3483 // If the address is not 64-bit aligned, the latencies of these
3484 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003485 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003486 break;
3487 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003488 }
3489 return Adjust;
3490}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003491
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003492
3493
3494int
3495ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3496 const MachineInstr *DefMI, unsigned DefIdx,
3497 const MachineInstr *UseMI,
3498 unsigned UseIdx) const {
3499 // No operand latency. The caller may fall back to getInstrLatency.
3500 if (!ItinData || ItinData->isEmpty())
3501 return -1;
3502
3503 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3504 unsigned Reg = DefMO.getReg();
3505 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3506 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3507
3508 unsigned DefAdj = 0;
3509 if (DefMI->isBundle()) {
3510 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3511 DefMCID = &DefMI->getDesc();
3512 }
3513 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3514 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3515 return 1;
3516 }
3517
3518 unsigned UseAdj = 0;
3519 if (UseMI->isBundle()) {
3520 unsigned NewUseIdx;
3521 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3522 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003523 if (!NewUseMI)
3524 return -1;
3525
3526 UseMI = NewUseMI;
3527 UseIdx = NewUseIdx;
3528 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003529 }
3530
3531 if (Reg == ARM::CPSR) {
3532 if (DefMI->getOpcode() == ARM::FMSTAT) {
3533 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003534 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003535 }
3536
3537 // CPSR set and branch can be paired in the same cycle.
3538 if (UseMI->isBranch())
3539 return 0;
3540
3541 // Otherwise it takes the instruction latency (generally one).
3542 unsigned Latency = getInstrLatency(ItinData, DefMI);
3543
3544 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3545 // its uses. Instructions which are otherwise scheduled between them may
3546 // incur a code size penalty (not able to use the CPSR setting 16-bit
3547 // instructions).
3548 if (Latency > 0 && Subtarget.isThumb2()) {
3549 const MachineFunction *MF = DefMI->getParent()->getParent();
Bill Wendling698e84f2012-12-30 10:32:01 +00003550 if (MF->getFunction()->getAttributes().
3551 hasAttribute(AttributeSet::FunctionIndex,
3552 Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003553 --Latency;
3554 }
3555 return Latency;
3556 }
3557
Andrew Trick77d0b882012-06-22 02:50:33 +00003558 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3559 return -1;
3560
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003561 unsigned DefAlign = DefMI->hasOneMemOperand()
3562 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3563 unsigned UseAlign = UseMI->hasOneMemOperand()
3564 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3565
3566 // Get the itinerary's latency if possible, and handle variable_ops.
3567 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3568 *UseMCID, UseIdx, UseAlign);
3569 // Unable to find operand latency. The caller may resort to getInstrLatency.
3570 if (Latency < 0)
3571 return Latency;
3572
3573 // Adjust for IT block position.
3574 int Adj = DefAdj + UseAdj;
3575
3576 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3577 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3578 if (Adj >= 0 || (int)Latency > -Adj) {
3579 return Latency + Adj;
3580 }
3581 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003582 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003583}
3584
3585int
3586ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3587 SDNode *DefNode, unsigned DefIdx,
3588 SDNode *UseNode, unsigned UseIdx) const {
3589 if (!DefNode->isMachineOpcode())
3590 return 1;
3591
Evan Cheng6cc775f2011-06-28 19:10:37 +00003592 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003593
Evan Cheng6cc775f2011-06-28 19:10:37 +00003594 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003595 return 0;
3596
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003597 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003598 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003599
Evan Cheng6c1414f2010-10-29 18:09:28 +00003600 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003601 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003602 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003603 return Latency <= 2 ? 1 : Latency - 1;
3604 else
3605 return Latency <= 3 ? 1 : Latency - 2;
3606 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003607
Evan Cheng6cc775f2011-06-28 19:10:37 +00003608 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003609 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3610 unsigned DefAlign = !DefMN->memoperands_empty()
3611 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3612 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3613 unsigned UseAlign = !UseMN->memoperands_empty()
3614 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003615 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3616 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003617
3618 if (Latency > 1 &&
Tim Northover0feb91e2014-04-01 14:10:07 +00003619 (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
3620 Subtarget.isCortexA7())) {
Evan Chengff310732010-10-28 06:47:08 +00003621 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3622 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003623 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003624 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003625 case ARM::LDRrs:
3626 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003627 unsigned ShOpVal =
3628 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3629 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3630 if (ShImm == 0 ||
3631 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3632 --Latency;
3633 break;
3634 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003635 case ARM::t2LDRs:
3636 case ARM::t2LDRBs:
3637 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003638 case ARM::t2LDRSHs: {
3639 // Thumb2 mode: lsl only.
3640 unsigned ShAmt =
3641 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3642 if (ShAmt == 0 || ShAmt == 2)
3643 --Latency;
3644 break;
3645 }
3646 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003647 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3648 // FIXME: Properly handle all of the latency adjustments for address
3649 // writeback.
3650 switch (DefMCID.getOpcode()) {
3651 default: break;
3652 case ARM::LDRrs:
3653 case ARM::LDRBrs: {
3654 unsigned ShOpVal =
3655 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3656 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3657 if (ShImm == 0 ||
3658 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3659 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3660 Latency -= 2;
3661 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3662 --Latency;
3663 break;
3664 }
3665 case ARM::t2LDRs:
3666 case ARM::t2LDRBs:
3667 case ARM::t2LDRHs:
3668 case ARM::t2LDRSHs: {
3669 // Thumb2 mode: lsl 0-3 only.
3670 Latency -= 2;
3671 break;
3672 }
3673 }
Evan Chengff310732010-10-28 06:47:08 +00003674 }
3675
Silviu Barangab47bb942012-09-13 15:05:10 +00003676 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003677 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003678 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003679 case ARM::VLD1q8:
3680 case ARM::VLD1q16:
3681 case ARM::VLD1q32:
3682 case ARM::VLD1q64:
3683 case ARM::VLD1q8wb_register:
3684 case ARM::VLD1q16wb_register:
3685 case ARM::VLD1q32wb_register:
3686 case ARM::VLD1q64wb_register:
3687 case ARM::VLD1q8wb_fixed:
3688 case ARM::VLD1q16wb_fixed:
3689 case ARM::VLD1q32wb_fixed:
3690 case ARM::VLD1q64wb_fixed:
3691 case ARM::VLD2d8:
3692 case ARM::VLD2d16:
3693 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003694 case ARM::VLD2q8Pseudo:
3695 case ARM::VLD2q16Pseudo:
3696 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003697 case ARM::VLD2d8wb_fixed:
3698 case ARM::VLD2d16wb_fixed:
3699 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003700 case ARM::VLD2q8PseudoWB_fixed:
3701 case ARM::VLD2q16PseudoWB_fixed:
3702 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003703 case ARM::VLD2d8wb_register:
3704 case ARM::VLD2d16wb_register:
3705 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003706 case ARM::VLD2q8PseudoWB_register:
3707 case ARM::VLD2q16PseudoWB_register:
3708 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003709 case ARM::VLD3d8Pseudo:
3710 case ARM::VLD3d16Pseudo:
3711 case ARM::VLD3d32Pseudo:
3712 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003713 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003714 case ARM::VLD3d8Pseudo_UPD:
3715 case ARM::VLD3d16Pseudo_UPD:
3716 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003717 case ARM::VLD3q8Pseudo_UPD:
3718 case ARM::VLD3q16Pseudo_UPD:
3719 case ARM::VLD3q32Pseudo_UPD:
3720 case ARM::VLD3q8oddPseudo:
3721 case ARM::VLD3q16oddPseudo:
3722 case ARM::VLD3q32oddPseudo:
3723 case ARM::VLD3q8oddPseudo_UPD:
3724 case ARM::VLD3q16oddPseudo_UPD:
3725 case ARM::VLD3q32oddPseudo_UPD:
3726 case ARM::VLD4d8Pseudo:
3727 case ARM::VLD4d16Pseudo:
3728 case ARM::VLD4d32Pseudo:
3729 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003730 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003731 case ARM::VLD4d8Pseudo_UPD:
3732 case ARM::VLD4d16Pseudo_UPD:
3733 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003734 case ARM::VLD4q8Pseudo_UPD:
3735 case ARM::VLD4q16Pseudo_UPD:
3736 case ARM::VLD4q32Pseudo_UPD:
3737 case ARM::VLD4q8oddPseudo:
3738 case ARM::VLD4q16oddPseudo:
3739 case ARM::VLD4q32oddPseudo:
3740 case ARM::VLD4q8oddPseudo_UPD:
3741 case ARM::VLD4q16oddPseudo_UPD:
3742 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003743 case ARM::VLD1DUPq8:
3744 case ARM::VLD1DUPq16:
3745 case ARM::VLD1DUPq32:
3746 case ARM::VLD1DUPq8wb_fixed:
3747 case ARM::VLD1DUPq16wb_fixed:
3748 case ARM::VLD1DUPq32wb_fixed:
3749 case ARM::VLD1DUPq8wb_register:
3750 case ARM::VLD1DUPq16wb_register:
3751 case ARM::VLD1DUPq32wb_register:
3752 case ARM::VLD2DUPd8:
3753 case ARM::VLD2DUPd16:
3754 case ARM::VLD2DUPd32:
3755 case ARM::VLD2DUPd8wb_fixed:
3756 case ARM::VLD2DUPd16wb_fixed:
3757 case ARM::VLD2DUPd32wb_fixed:
3758 case ARM::VLD2DUPd8wb_register:
3759 case ARM::VLD2DUPd16wb_register:
3760 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003761 case ARM::VLD4DUPd8Pseudo:
3762 case ARM::VLD4DUPd16Pseudo:
3763 case ARM::VLD4DUPd32Pseudo:
3764 case ARM::VLD4DUPd8Pseudo_UPD:
3765 case ARM::VLD4DUPd16Pseudo_UPD:
3766 case ARM::VLD4DUPd32Pseudo_UPD:
3767 case ARM::VLD1LNq8Pseudo:
3768 case ARM::VLD1LNq16Pseudo:
3769 case ARM::VLD1LNq32Pseudo:
3770 case ARM::VLD1LNq8Pseudo_UPD:
3771 case ARM::VLD1LNq16Pseudo_UPD:
3772 case ARM::VLD1LNq32Pseudo_UPD:
3773 case ARM::VLD2LNd8Pseudo:
3774 case ARM::VLD2LNd16Pseudo:
3775 case ARM::VLD2LNd32Pseudo:
3776 case ARM::VLD2LNq16Pseudo:
3777 case ARM::VLD2LNq32Pseudo:
3778 case ARM::VLD2LNd8Pseudo_UPD:
3779 case ARM::VLD2LNd16Pseudo_UPD:
3780 case ARM::VLD2LNd32Pseudo_UPD:
3781 case ARM::VLD2LNq16Pseudo_UPD:
3782 case ARM::VLD2LNq32Pseudo_UPD:
3783 case ARM::VLD4LNd8Pseudo:
3784 case ARM::VLD4LNd16Pseudo:
3785 case ARM::VLD4LNd32Pseudo:
3786 case ARM::VLD4LNq16Pseudo:
3787 case ARM::VLD4LNq32Pseudo:
3788 case ARM::VLD4LNd8Pseudo_UPD:
3789 case ARM::VLD4LNd16Pseudo_UPD:
3790 case ARM::VLD4LNd32Pseudo_UPD:
3791 case ARM::VLD4LNq16Pseudo_UPD:
3792 case ARM::VLD4LNq32Pseudo_UPD:
3793 // If the address is not 64-bit aligned, the latencies of these
3794 // instructions increases by one.
3795 ++Latency;
3796 break;
3797 }
3798
Evan Chengff310732010-10-28 06:47:08 +00003799 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003800}
Evan Cheng63c76082010-10-19 18:58:51 +00003801
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003802unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3803 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3804 MI->isRegSequence() || MI->isImplicitDef())
3805 return 0;
3806
3807 if (MI->isBundle())
3808 return 0;
3809
3810 const MCInstrDesc &MCID = MI->getDesc();
3811
3812 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3813 // When predicated, CPSR is an additional source operand for CPSR updating
3814 // instructions, this apparently increases their latencies.
3815 return 1;
3816 }
3817 return 0;
3818}
3819
Andrew Trick45446062012-06-05 21:11:27 +00003820unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3821 const MachineInstr *MI,
3822 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003823 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3824 MI->isRegSequence() || MI->isImplicitDef())
3825 return 1;
3826
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003827 // An instruction scheduler typically runs on unbundled instructions, however
3828 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003829 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003830 unsigned Latency = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003831 MachineBasicBlock::const_instr_iterator I = MI;
3832 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3833 while (++I != E && I->isInsideBundle()) {
3834 if (I->getOpcode() != ARM::t2IT)
3835 Latency += getInstrLatency(ItinData, I, PredCost);
3836 }
3837 return Latency;
3838 }
3839
Evan Cheng6cc775f2011-06-28 19:10:37 +00003840 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003841 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003842 // When predicated, CPSR is an additional source operand for CPSR updating
3843 // instructions, this apparently increases their latencies.
3844 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003845 }
3846 // Be sure to call getStageLatency for an empty itinerary in case it has a
3847 // valid MinLatency property.
3848 if (!ItinData)
3849 return MI->mayLoad() ? 3 : 1;
3850
3851 unsigned Class = MCID.getSchedClass();
3852
3853 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00003854 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003855 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00003856
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003857 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003858 unsigned Latency = ItinData->getStageLatency(Class);
3859
3860 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3861 unsigned DefAlign = MI->hasOneMemOperand()
3862 ? (*MI->memoperands_begin())->getAlignment() : 0;
3863 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3864 if (Adj >= 0 || (int)Latency > -Adj) {
3865 return Latency + Adj;
3866 }
3867 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00003868}
3869
3870int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3871 SDNode *Node) const {
3872 if (!Node->isMachineOpcode())
3873 return 1;
3874
3875 if (!ItinData || ItinData->isEmpty())
3876 return 1;
3877
3878 unsigned Opcode = Node->getMachineOpcode();
3879 switch (Opcode) {
3880 default:
3881 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003882 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003883 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00003884 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00003885 }
Evan Chengdebf9c52010-11-03 00:45:17 +00003886}
3887
Evan Cheng63c76082010-10-19 18:58:51 +00003888bool ARMBaseInstrInfo::
3889hasHighOperandLatency(const InstrItineraryData *ItinData,
3890 const MachineRegisterInfo *MRI,
3891 const MachineInstr *DefMI, unsigned DefIdx,
3892 const MachineInstr *UseMI, unsigned UseIdx) const {
3893 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3894 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
3895 if (Subtarget.isCortexA8() &&
3896 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
3897 // CortexA8 VFP instructions are not pipelined.
3898 return true;
3899
3900 // Hoist VFP / NEON instructions with 4 or higher latency.
Andrew Trickde2109e2013-06-15 04:49:57 +00003901 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
Andrew Trick3564bdf2012-06-07 19:41:58 +00003902 if (Latency < 0)
3903 Latency = getInstrLatency(ItinData, DefMI);
Evan Cheng63c76082010-10-19 18:58:51 +00003904 if (Latency <= 3)
3905 return false;
3906 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
3907 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
3908}
Evan Chenge96b8d72010-10-26 02:08:50 +00003909
3910bool ARMBaseInstrInfo::
3911hasLowDefLatency(const InstrItineraryData *ItinData,
3912 const MachineInstr *DefMI, unsigned DefIdx) const {
3913 if (!ItinData || ItinData->isEmpty())
3914 return false;
3915
3916 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3917 if (DDomain == ARMII::DomainGeneral) {
3918 unsigned DefClass = DefMI->getDesc().getSchedClass();
3919 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3920 return (DefCycle != -1 && DefCycle <= 2);
3921 }
3922 return false;
3923}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00003924
Andrew Trick924123a2011-09-21 02:20:46 +00003925bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
3926 StringRef &ErrInfo) const {
3927 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
3928 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
3929 return false;
3930 }
3931 return true;
3932}
3933
Evan Cheng62c7b5b2010-12-05 22:04:16 +00003934bool
3935ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
3936 unsigned &AddSubOpc,
3937 bool &NegAcc, bool &HasLane) const {
3938 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
3939 if (I == MLxEntryMap.end())
3940 return false;
3941
3942 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
3943 MulOpc = Entry.MulOpc;
3944 AddSubOpc = Entry.AddSubOpc;
3945 NegAcc = Entry.NegAcc;
3946 HasLane = Entry.HasLane;
3947 return true;
3948}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003949
3950//===----------------------------------------------------------------------===//
3951// Execution domains.
3952//===----------------------------------------------------------------------===//
3953//
3954// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
3955// and some can go down both. The vmov instructions go down the VFP pipeline,
3956// but they can be changed to vorr equivalents that are executed by the NEON
3957// pipeline.
3958//
3959// We use the following execution domain numbering:
3960//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003961enum ARMExeDomain {
3962 ExeGeneric = 0,
3963 ExeVFP = 1,
3964 ExeNEON = 2
3965};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003966//
3967// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
3968//
3969std::pair<uint16_t, uint16_t>
3970ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Tim Northoverf6618152012-08-17 11:32:52 +00003971 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
3972 // if they are not predicated.
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003973 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003974 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003975
Silviu Barangadc453362013-03-27 12:38:44 +00003976 // CortexA9 is particularly picky about mixing the two and wants these
Tim Northoverf6618152012-08-17 11:32:52 +00003977 // converted.
Silviu Barangadc453362013-03-27 12:38:44 +00003978 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
Tim Northoverf6618152012-08-17 11:32:52 +00003979 (MI->getOpcode() == ARM::VMOVRS ||
Tim Northoverca9f3842012-08-30 10:17:45 +00003980 MI->getOpcode() == ARM::VMOVSR ||
3981 MI->getOpcode() == ARM::VMOVS))
Tim Northoverf6618152012-08-17 11:32:52 +00003982 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
3983
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003984 // No other instructions can be swizzled, so just determine their domain.
3985 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
3986
3987 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003988 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003989
3990 // Certain instructions can go either way on Cortex-A8.
3991 // Treat them as NEON instructions.
3992 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003993 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003994
3995 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003996 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003997
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003998 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003999}
4000
Tim Northover771f1602012-08-29 16:36:07 +00004001static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
4002 unsigned SReg, unsigned &Lane) {
4003 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
4004 Lane = 0;
4005
4006 if (DReg != ARM::NoRegister)
4007 return DReg;
4008
4009 Lane = 1;
4010 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4011
4012 assert(DReg && "S-register with no D super-register?");
4013 return DReg;
4014}
4015
Andrew Trickd9296ec2012-10-10 05:43:01 +00004016/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004017/// set ImplicitSReg to a register number that must be marked as implicit-use or
4018/// zero if no register needs to be defined as implicit-use.
4019///
4020/// If the function cannot determine if an SPR should be marked implicit use or
4021/// not, it returns false.
4022///
4023/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004024/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004025/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4026/// lane of the DPR).
4027///
4028/// If the other SPR is defined, an implicit-use of it should be added. Else,
4029/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004030///
James Molloyea052562012-09-18 08:31:15 +00004031static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4032 MachineInstr *MI,
4033 unsigned DReg, unsigned Lane,
4034 unsigned &ImplicitSReg) {
4035 // If the DPR is defined or used already, the other SPR lane will be chained
4036 // correctly, so there is nothing to be done.
4037 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4038 ImplicitSReg = 0;
4039 return true;
4040 }
4041
4042 // Otherwise we need to go searching to see if the SPR is set explicitly.
4043 ImplicitSReg = TRI->getSubReg(DReg,
4044 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4045 MachineBasicBlock::LivenessQueryResult LQR =
4046 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4047
4048 if (LQR == MachineBasicBlock::LQR_Live)
4049 return true;
4050 else if (LQR == MachineBasicBlock::LQR_Unknown)
4051 return false;
4052
4053 // If the register is known not to be live, there is no need to add an
4054 // implicit-use.
4055 ImplicitSReg = 0;
4056 return true;
4057}
Tim Northover771f1602012-08-29 16:36:07 +00004058
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004059void
4060ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004061 unsigned DstReg, SrcReg, DReg;
4062 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004063 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004064 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004065 switch (MI->getOpcode()) {
4066 default:
4067 llvm_unreachable("cannot handle opcode!");
4068 break;
4069 case ARM::VMOVD:
4070 if (Domain != ExeNEON)
4071 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004072
Tim Northoverf6618152012-08-17 11:32:52 +00004073 // Zap the predicate operands.
4074 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004075
Tim Northover771f1602012-08-29 16:36:07 +00004076 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4077 DstReg = MI->getOperand(0).getReg();
4078 SrcReg = MI->getOperand(1).getReg();
4079
4080 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4081 MI->RemoveOperand(i-1);
4082
4083 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004084 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004085 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4086 .addReg(SrcReg)
4087 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004088 break;
4089 case ARM::VMOVRS:
4090 if (Domain != ExeNEON)
4091 break;
4092 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4093
Tim Northover771f1602012-08-29 16:36:07 +00004094 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004095 DstReg = MI->getOperand(0).getReg();
4096 SrcReg = MI->getOperand(1).getReg();
4097
Tim Northover771f1602012-08-29 16:36:07 +00004098 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4099 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004100
Tim Northover771f1602012-08-29 16:36:07 +00004101 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004102
Tim Northover771f1602012-08-29 16:36:07 +00004103 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4104 // Note that DSrc has been widened and the other lane may be undef, which
4105 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004106 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004107 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4108 .addReg(DReg, RegState::Undef)
4109 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004110
Tim Northover771f1602012-08-29 16:36:07 +00004111 // The old source should be an implicit use, otherwise we might think it
4112 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004113 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004114 break;
James Molloyea052562012-09-18 08:31:15 +00004115 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004116 if (Domain != ExeNEON)
4117 break;
4118 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4119
Tim Northover771f1602012-08-29 16:36:07 +00004120 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004121 DstReg = MI->getOperand(0).getReg();
4122 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004123
Tim Northover771f1602012-08-29 16:36:07 +00004124 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4125
James Molloyea052562012-09-18 08:31:15 +00004126 unsigned ImplicitSReg;
4127 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4128 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004129
Tim Northoverc8d867d2012-09-05 18:37:53 +00004130 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4131 MI->RemoveOperand(i-1);
4132
Tim Northover771f1602012-08-29 16:36:07 +00004133 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4134 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004135 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004136 MIB.addReg(DReg, RegState::Define)
4137 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4138 .addReg(SrcReg)
4139 .addImm(Lane);
4140 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004141
Tim Northover726d32c2012-09-01 18:07:29 +00004142 // The narrower destination must be marked as set to keep previous chains
4143 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004144 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004145 if (ImplicitSReg != 0)
4146 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004147 break;
James Molloyea052562012-09-18 08:31:15 +00004148 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004149 case ARM::VMOVS: {
4150 if (Domain != ExeNEON)
4151 break;
4152
4153 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4154 DstReg = MI->getOperand(0).getReg();
4155 SrcReg = MI->getOperand(1).getReg();
4156
Tim Northoverca9f3842012-08-30 10:17:45 +00004157 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4158 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4159 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4160
James Molloyea052562012-09-18 08:31:15 +00004161 unsigned ImplicitSReg;
4162 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4163 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004164
Tim Northoverc8d867d2012-09-05 18:37:53 +00004165 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4166 MI->RemoveOperand(i-1);
4167
Tim Northoverca9f3842012-08-30 10:17:45 +00004168 if (DSrc == DDst) {
4169 // Destination can be:
4170 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4171 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004172 MIB.addReg(DDst, RegState::Define)
4173 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4174 .addImm(SrcLane);
4175 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004176
4177 // Neither the source or the destination are naturally represented any
4178 // more, so add them in manually.
4179 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4180 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004181 if (ImplicitSReg != 0)
4182 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004183 break;
4184 }
4185
4186 // In general there's no single instruction that can perform an S <-> S
4187 // move in NEON space, but a pair of VEXT instructions *can* do the
4188 // job. It turns out that the VEXTs needed will only use DSrc once, with
4189 // the position based purely on the combination of lane-0 and lane-1
4190 // involved. For example
4191 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4192 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4193 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4194 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4195 //
4196 // Pattern of the MachineInstrs is:
4197 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4198 MachineInstrBuilder NewMIB;
4199 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4200 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004201
4202 // On the first instruction, both DSrc and DDst may be <undef> if present.
4203 // Specifically when the original instruction didn't have them as an
4204 // <imp-use>.
4205 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4206 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4207 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4208
4209 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4210 CurUndef = !MI->readsRegister(CurReg, TRI);
4211 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4212
Tim Northoverca9f3842012-08-30 10:17:45 +00004213 NewMIB.addImm(1);
4214 AddDefaultPred(NewMIB);
4215
4216 if (SrcLane == DstLane)
4217 NewMIB.addReg(SrcReg, RegState::Implicit);
4218
4219 MI->setDesc(get(ARM::VEXTd32));
4220 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004221
4222 // On the second instruction, DDst has definitely been defined above, so
4223 // it is not <undef>. DSrc, if present, can be <undef> as above.
4224 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4225 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4226 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4227
4228 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4229 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4230 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4231
Tim Northoverca9f3842012-08-30 10:17:45 +00004232 MIB.addImm(1);
4233 AddDefaultPred(MIB);
4234
4235 if (SrcLane != DstLane)
4236 MIB.addReg(SrcReg, RegState::Implicit);
4237
4238 // As before, the original destination is no longer represented, add it
4239 // implicitly.
4240 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004241 if (ImplicitSReg != 0)
4242 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004243 break;
4244 }
Tim Northoverf6618152012-08-17 11:32:52 +00004245 }
4246
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004247}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004248
Bob Wilsone8a549c2012-09-29 21:43:49 +00004249//===----------------------------------------------------------------------===//
4250// Partial register updates
4251//===----------------------------------------------------------------------===//
4252//
4253// Swift renames NEON registers with 64-bit granularity. That means any
4254// instruction writing an S-reg implicitly reads the containing D-reg. The
4255// problem is mostly avoided by translating f32 operations to v2f32 operations
4256// on D-registers, but f32 loads are still a problem.
4257//
4258// These instructions can load an f32 into a NEON register:
4259//
4260// VLDRS - Only writes S, partial D update.
4261// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4262// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4263//
4264// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004265unsigned ARMBaseInstrInfo::
4266getPartialRegUpdateClearance(const MachineInstr *MI,
4267 unsigned OpNum,
4268 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004269 if (!SwiftPartialUpdateClearance ||
4270 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004271 return 0;
4272
4273 assert(TRI && "Need TRI instance");
4274
4275 const MachineOperand &MO = MI->getOperand(OpNum);
4276 if (MO.readsReg())
4277 return 0;
4278 unsigned Reg = MO.getReg();
4279 int UseOp = -1;
4280
4281 switch(MI->getOpcode()) {
4282 // Normal instructions writing only an S-register.
4283 case ARM::VLDRS:
4284 case ARM::FCONSTS:
4285 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004286 case ARM::VMOVv8i8:
4287 case ARM::VMOVv4i16:
4288 case ARM::VMOVv2i32:
4289 case ARM::VMOVv2f32:
4290 case ARM::VMOVv1i64:
4291 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4292 break;
4293
4294 // Explicitly reads the dependency.
4295 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004296 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004297 break;
4298 default:
4299 return 0;
4300 }
4301
4302 // If this instruction actually reads a value from Reg, there is no unwanted
4303 // dependency.
4304 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4305 return 0;
4306
4307 // We must be able to clobber the whole D-reg.
4308 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4309 // Virtual register must be a foo:ssub_0<def,undef> operand.
4310 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4311 return 0;
4312 } else if (ARM::SPRRegClass.contains(Reg)) {
4313 // Physical register: MI must define the full D-reg.
4314 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4315 &ARM::DPRRegClass);
4316 if (!DReg || !MI->definesRegister(DReg, TRI))
4317 return 0;
4318 }
4319
4320 // MI has an unwanted D-register dependency.
4321 // Avoid defs in the previous N instructrions.
4322 return SwiftPartialUpdateClearance;
4323}
4324
4325// Break a partial register dependency after getPartialRegUpdateClearance
4326// returned non-zero.
4327void ARMBaseInstrInfo::
4328breakPartialRegDependency(MachineBasicBlock::iterator MI,
4329 unsigned OpNum,
4330 const TargetRegisterInfo *TRI) const {
4331 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4332 assert(TRI && "Need TRI instance");
4333
4334 const MachineOperand &MO = MI->getOperand(OpNum);
4335 unsigned Reg = MO.getReg();
4336 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4337 "Can't break virtual register dependencies.");
4338 unsigned DReg = Reg;
4339
4340 // If MI defines an S-reg, find the corresponding D super-register.
4341 if (ARM::SPRRegClass.contains(Reg)) {
4342 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4343 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4344 }
4345
4346 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4347 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4348
4349 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4350 // the full D-register by loading the same value to both lanes. The
4351 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004352 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004353 // too big regressions.
4354
4355 // Insert the dependency-breaking FCONSTD before MI.
4356 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4357 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4358 get(ARM::FCONSTD), DReg).addImm(96));
4359 MI->addRegisterKilled(DReg, TRI, true);
4360}
4361
Tom Roeder44cb65f2014-06-05 19:29:43 +00004362void ARMBaseInstrInfo::getUnconditionalBranch(
4363 MCInst &Branch, const MCSymbolRefExpr *BranchTarget) const {
4364 if (Subtarget.isThumb())
4365 Branch.setOpcode(ARM::tB);
4366 else if (Subtarget.isThumb2())
4367 Branch.setOpcode(ARM::t2B);
4368 else
4369 Branch.setOpcode(ARM::Bcc);
4370
4371 Branch.addOperand(MCOperand::CreateExpr(BranchTarget));
4372 Branch.addOperand(MCOperand::CreateImm(ARMCC::AL));
4373 Branch.addOperand(MCOperand::CreateReg(0));
4374}
4375
4376void ARMBaseInstrInfo::getTrap(MCInst &MI) const {
4377 if (Subtarget.isThumb())
4378 MI.setOpcode(ARM::tTRAP);
4379 else if (Subtarget.useNaClTrap())
4380 MI.setOpcode(ARM::TRAPNaCl);
4381 else
4382 MI.setOpcode(ARM::TRAP);
4383}
4384
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004385bool ARMBaseInstrInfo::hasNOP() const {
4386 return (Subtarget.getFeatureBits() & ARM::HasV6T2Ops) != 0;
4387}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004388
4389bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004390 if (MI->getNumOperands() < 4)
4391 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004392 unsigned ShOpVal = MI->getOperand(3).getImm();
4393 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4394 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4395 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4396 ((ShImm == 1 || ShImm == 2) &&
4397 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4398 return true;
4399
4400 return false;
4401}