blob: 4c69a90c4b63373efd9b212673cff2afcff769ad [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"
Jakub Staszak9b07c0a2011-07-10 02:58:07 +000035#include "llvm/Support/BranchProbability.h"
David Goodwinaf7451b2009-07-08 16:09:28 +000036#include "llvm/Support/CommandLine.h"
Anton Korobeynikov14635da2009-11-02 00:10:38 +000037#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000038#include "llvm/Support/ErrorHandling.h"
Evan Cheng1e210d02011-06-28 20:07:07 +000039
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000040#define GET_INSTRINFO_CTOR_DTOR
Evan Cheng1e210d02011-06-28 20:07:07 +000041#include "ARMGenInstrInfo.inc"
42
David Goodwinaf7451b2009-07-08 16:09:28 +000043using namespace llvm;
44
45static cl::opt<bool>
46EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
47 cl::desc("Enable ARM 2-addr to 3-addr conv"));
48
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000049static cl::opt<bool>
Jakob Stoklund Olesen653183f2011-11-15 23:53:18 +000050WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesencd893392011-08-31 17:00:02 +000051 cl::desc("Widen ARM vmovs to vmovd when possible"));
52
Bob Wilsone8a549c2012-09-29 21:43:49 +000053static cl::opt<unsigned>
54SwiftPartialUpdateClearance("swift-partial-update-clearance",
55 cl::Hidden, cl::init(12),
56 cl::desc("Clearance before partial register updates"));
57
Evan Cheng62c7b5b2010-12-05 22:04:16 +000058/// ARM_MLxEntry - Record information about MLA / MLS instructions.
59struct ARM_MLxEntry {
Craig Topper2fbd1302012-05-24 03:59:11 +000060 uint16_t MLxOpc; // MLA / MLS opcode
61 uint16_t MulOpc; // Expanded multiplication opcode
62 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng62c7b5b2010-12-05 22:04:16 +000063 bool NegAcc; // True if the acc is negated before the add / sub.
64 bool HasLane; // True if instruction has an extra "lane" operand.
65};
66
67static const ARM_MLxEntry ARM_MLxTable[] = {
68 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
69 // fp scalar ops
70 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
71 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
72 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
73 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng62c7b5b2010-12-05 22:04:16 +000074 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
75 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
76 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
77 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
78
79 // fp SIMD ops
80 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
81 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
82 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
83 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
84 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
85 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
86 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
87 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
88};
89
Anton Korobeynikov14635da2009-11-02 00:10:38 +000090ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng703a0fb2011-07-01 17:57:27 +000091 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikov14635da2009-11-02 00:10:38 +000092 Subtarget(STI) {
Evan Cheng62c7b5b2010-12-05 22:04:16 +000093 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
94 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
95 assert(false && "Duplicated entries?");
96 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
97 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
98 }
99}
100
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000101// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
102// currently defaults to no prepass hazard recognizer.
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000103ScheduleHazardRecognizer *ARMBaseInstrInfo::
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000104CreateTargetHazardRecognizer(const TargetMachine *TM,
105 const ScheduleDAG *DAG) const {
Andrew Trick47ff14b2011-01-21 05:51:33 +0000106 if (usePreRAHazardRecognizer()) {
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000107 const InstrItineraryData *II = TM->getInstrItineraryData();
108 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
109 }
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000110 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000111}
112
113ScheduleHazardRecognizer *ARMBaseInstrInfo::
114CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
115 const ScheduleDAG *DAG) const {
Evan Cheng62c7b5b2010-12-05 22:04:16 +0000116 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
Bill Wendlingf95178e2013-06-07 05:54:19 +0000117 return (ScheduleHazardRecognizer *)new ARMHazardRecognizer(II, DAG);
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +0000118 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwinaf7451b2009-07-08 16:09:28 +0000119}
120
121MachineInstr *
122ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
123 MachineBasicBlock::iterator &MBBI,
124 LiveVariables *LV) const {
Evan Cheng0e075e22009-07-27 18:44:00 +0000125 // FIXME: Thumb2 support.
126
David Goodwinaf7451b2009-07-08 16:09:28 +0000127 if (!EnableARM3Addr)
128 return NULL;
129
130 MachineInstr *MI = MBBI;
131 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopesc2f87b72010-06-08 22:51:23 +0000132 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwinaf7451b2009-07-08 16:09:28 +0000133 bool isPre = false;
134 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
135 default: return NULL;
136 case ARMII::IndexModePre:
137 isPre = true;
138 break;
139 case ARMII::IndexModePost:
140 break;
141 }
142
143 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
144 // operation.
145 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
146 if (MemOpc == 0)
147 return NULL;
148
149 MachineInstr *UpdateMI = NULL;
150 MachineInstr *MemMI = NULL;
151 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
Evan Cheng6cc775f2011-06-28 19:10:37 +0000152 const MCInstrDesc &MCID = MI->getDesc();
153 unsigned NumOps = MCID.getNumOperands();
Evan Cheng7f8e5632011-12-07 07:15:52 +0000154 bool isLoad = !MI->mayStore();
David Goodwinaf7451b2009-07-08 16:09:28 +0000155 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
156 const MachineOperand &Base = MI->getOperand(2);
157 const MachineOperand &Offset = MI->getOperand(NumOps-3);
158 unsigned WBReg = WB.getReg();
159 unsigned BaseReg = Base.getReg();
160 unsigned OffReg = Offset.getReg();
161 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
162 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
163 switch (AddrMode) {
Craig Toppere55c5562012-02-07 02:50:20 +0000164 default: llvm_unreachable("Unknown indexed op!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000165 case ARMII::AddrMode2: {
166 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
167 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
168 if (OffReg == 0) {
Evan Chenge3a53c42009-07-08 21:03:57 +0000169 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwinaf7451b2009-07-08 16:09:28 +0000170 // Can't encode it in a so_imm operand. This transformation will
171 // add more than 1 instruction. Abandon!
172 return NULL;
173 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000174 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge3a53c42009-07-08 21:03:57 +0000175 .addReg(BaseReg).addImm(Amt)
David Goodwinaf7451b2009-07-08 16:09:28 +0000176 .addImm(Pred).addReg(0).addReg(0);
177 } else if (Amt != 0) {
178 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
179 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
180 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Owen Andersonb595ed02011-07-21 18:54:16 +0000181 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000182 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
183 .addImm(Pred).addReg(0).addReg(0);
184 } else
185 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000186 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000187 .addReg(BaseReg).addReg(OffReg)
188 .addImm(Pred).addReg(0).addReg(0);
189 break;
190 }
191 case ARMII::AddrMode3 : {
192 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
193 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
194 if (OffReg == 0)
195 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
196 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000197 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000198 .addReg(BaseReg).addImm(Amt)
199 .addImm(Pred).addReg(0).addReg(0);
200 else
201 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng0e075e22009-07-27 18:44:00 +0000202 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwinaf7451b2009-07-08 16:09:28 +0000203 .addReg(BaseReg).addReg(OffReg)
204 .addImm(Pred).addReg(0).addReg(0);
205 break;
206 }
207 }
208
209 std::vector<MachineInstr*> NewMIs;
210 if (isPre) {
211 if (isLoad)
212 MemMI = BuildMI(MF, MI->getDebugLoc(),
213 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000214 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000215 else
216 MemMI = BuildMI(MF, MI->getDebugLoc(),
217 get(MemOpc)).addReg(MI->getOperand(1).getReg())
218 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
219 NewMIs.push_back(MemMI);
220 NewMIs.push_back(UpdateMI);
221 } else {
222 if (isLoad)
223 MemMI = BuildMI(MF, MI->getDebugLoc(),
224 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach1e4d9a12010-10-26 22:37:02 +0000225 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwinaf7451b2009-07-08 16:09:28 +0000226 else
227 MemMI = BuildMI(MF, MI->getDebugLoc(),
228 get(MemOpc)).addReg(MI->getOperand(1).getReg())
229 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
230 if (WB.isDead())
231 UpdateMI->getOperand(0).setIsDead();
232 NewMIs.push_back(UpdateMI);
233 NewMIs.push_back(MemMI);
234 }
235
236 // Transfer LiveVariables states, kill / dead info.
237 if (LV) {
238 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
239 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000240 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000241 unsigned Reg = MO.getReg();
242
243 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
244 if (MO.isDef()) {
245 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
246 if (MO.isDead())
247 LV->addVirtualRegisterDead(Reg, NewMI);
248 }
249 if (MO.isUse() && MO.isKill()) {
250 for (unsigned j = 0; j < 2; ++j) {
251 // Look at the two new MI's in reverse order.
252 MachineInstr *NewMI = NewMIs[j];
253 if (!NewMI->readsRegister(Reg))
254 continue;
255 LV->addVirtualRegisterKilled(Reg, NewMI);
256 if (VI.removeKill(MI))
257 VI.Kills.push_back(NewMI);
258 break;
259 }
260 }
261 }
262 }
263 }
264
265 MFI->insert(MBBI, NewMIs[1]);
266 MFI->insert(MBBI, NewMIs[0]);
267 return NewMIs[0];
268}
269
270// Branch analysis.
271bool
272ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
273 MachineBasicBlock *&FBB,
274 SmallVectorImpl<MachineOperand> &Cond,
275 bool AllowModify) const {
Lang Hames24864fe2013-07-19 23:52:47 +0000276 TBB = 0;
277 FBB = 0;
278
David Goodwinaf7451b2009-07-08 16:09:28 +0000279 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen4244d122010-04-02 01:38:09 +0000280 if (I == MBB.begin())
Lang Hames24864fe2013-07-19 23:52:47 +0000281 return false; // Empty blocks are easy.
Dale Johannesen4244d122010-04-02 01:38:09 +0000282 --I;
Lang Hames24864fe2013-07-19 23:52:47 +0000283
284 // Walk backwards from the end of the basic block until the branch is
285 // analyzed or we give up.
Lang Hames18c98a52013-12-20 20:27:51 +0000286 while (isPredicated(I) || I->isTerminator() || I->isDebugValue()) {
Lang Hames24864fe2013-07-19 23:52:47 +0000287
288 // Flag to be raised on unanalyzeable instructions. This is useful in cases
289 // where we want to clean up on the end of the basic block before we bail
290 // out.
291 bool CantAnalyze = false;
292
293 // Skip over DEBUG values and predicated nonterminators.
294 while (I->isDebugValue() || !I->isTerminator()) {
295 if (I == MBB.begin())
296 return false;
297 --I;
298 }
299
300 if (isIndirectBranchOpcode(I->getOpcode()) ||
301 isJumpTableBranchOpcode(I->getOpcode())) {
302 // Indirect branches and jump tables can't be analyzed, but we still want
303 // to clean up any instructions at the tail of the basic block.
304 CantAnalyze = true;
305 } else if (isUncondBranchOpcode(I->getOpcode())) {
306 TBB = I->getOperand(0).getMBB();
307 } else if (isCondBranchOpcode(I->getOpcode())) {
308 // Bail out if we encounter multiple conditional branches.
309 if (!Cond.empty())
310 return true;
311
312 assert(!FBB && "FBB should have been null.");
313 FBB = TBB;
314 TBB = I->getOperand(0).getMBB();
315 Cond.push_back(I->getOperand(1));
316 Cond.push_back(I->getOperand(2));
317 } else if (I->isReturn()) {
318 // Returns can't be analyzed, but we should run cleanup.
319 CantAnalyze = !isPredicated(I);
320 } else {
321 // We encountered other unrecognized terminator. Bail out immediately.
322 return true;
323 }
324
325 // Cleanup code - to be run for unpredicated unconditional branches and
326 // returns.
327 if (!isPredicated(I) &&
328 (isUncondBranchOpcode(I->getOpcode()) ||
329 isIndirectBranchOpcode(I->getOpcode()) ||
330 isJumpTableBranchOpcode(I->getOpcode()) ||
331 I->isReturn())) {
332 // Forget any previous condition branch information - it no longer applies.
333 Cond.clear();
334 FBB = 0;
335
336 // If we can modify the function, delete everything below this
337 // unconditional branch.
338 if (AllowModify) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000339 MachineBasicBlock::iterator DI = std::next(I);
Lang Hames24864fe2013-07-19 23:52:47 +0000340 while (DI != MBB.end()) {
341 MachineInstr *InstToDelete = DI;
342 ++DI;
343 InstToDelete->eraseFromParent();
344 }
345 }
346 }
347
348 if (CantAnalyze)
349 return true;
350
Dale Johannesen4244d122010-04-02 01:38:09 +0000351 if (I == MBB.begin())
352 return false;
Lang Hames24864fe2013-07-19 23:52:47 +0000353
Dale Johannesen4244d122010-04-02 01:38:09 +0000354 --I;
355 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000356
Lang Hames24864fe2013-07-19 23:52:47 +0000357 // We made it past the terminators without bailing out - we must have
358 // analyzed this branch successfully.
359 return false;
David Goodwinaf7451b2009-07-08 16:09:28 +0000360}
361
362
363unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000364 MachineBasicBlock::iterator I = MBB.end();
365 if (I == MBB.begin()) return 0;
366 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +0000367 while (I->isDebugValue()) {
368 if (I == MBB.begin())
369 return 0;
370 --I;
371 }
Evan Cheng056c6692009-07-27 18:20:05 +0000372 if (!isUncondBranchOpcode(I->getOpcode()) &&
373 !isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000374 return 0;
375
376 // Remove the branch.
377 I->eraseFromParent();
378
379 I = MBB.end();
380
381 if (I == MBB.begin()) return 1;
382 --I;
Evan Cheng056c6692009-07-27 18:20:05 +0000383 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwinaf7451b2009-07-08 16:09:28 +0000384 return 1;
385
386 // Remove the branch.
387 I->eraseFromParent();
388 return 2;
389}
390
391unsigned
392ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings0125b642010-06-17 22:43:56 +0000393 MachineBasicBlock *FBB,
394 const SmallVectorImpl<MachineOperand> &Cond,
395 DebugLoc DL) const {
Evan Cheng780748d2009-07-28 05:48:47 +0000396 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
397 int BOpc = !AFI->isThumbFunction()
398 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
399 int BccOpc = !AFI->isThumbFunction()
400 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000401 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Trick3f1fdf12011-09-21 02:17:37 +0000402
David Goodwinaf7451b2009-07-08 16:09:28 +0000403 // Shouldn't be a fall through.
404 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
405 assert((Cond.size() == 2 || Cond.size() == 0) &&
406 "ARM branch conditions have two components!");
407
408 if (FBB == 0) {
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000409 if (Cond.empty()) { // Unconditional branch?
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000410 if (isThumb)
411 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
412 else
413 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Andersoneb3f0fb2011-09-09 23:13:02 +0000414 } else
Stuart Hastings0125b642010-06-17 22:43:56 +0000415 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwinaf7451b2009-07-08 16:09:28 +0000416 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
417 return 1;
418 }
419
420 // Two-way conditional branch.
Stuart Hastings0125b642010-06-17 22:43:56 +0000421 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwinaf7451b2009-07-08 16:09:28 +0000422 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
Owen Anderson29cfe6c2011-09-09 21:48:23 +0000423 if (isThumb)
424 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
425 else
426 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwinaf7451b2009-07-08 16:09:28 +0000427 return 2;
428}
429
430bool ARMBaseInstrInfo::
431ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
432 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
433 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
434 return false;
435}
436
Evan Cheng7fae11b2011-12-14 02:11:42 +0000437bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
438 if (MI->isBundle()) {
439 MachineBasicBlock::const_instr_iterator I = MI;
440 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
441 while (++I != E && I->isInsideBundle()) {
442 int PIdx = I->findFirstPredOperandIdx();
443 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
444 return true;
445 }
446 return false;
447 }
448
449 int PIdx = MI->findFirstPredOperandIdx();
450 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
451}
452
David Goodwinaf7451b2009-07-08 16:09:28 +0000453bool ARMBaseInstrInfo::
454PredicateInstruction(MachineInstr *MI,
455 const SmallVectorImpl<MachineOperand> &Pred) const {
456 unsigned Opc = MI->getOpcode();
Evan Cheng056c6692009-07-27 18:20:05 +0000457 if (isUncondBranchOpcode(Opc)) {
458 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +0000459 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
460 .addImm(Pred[0].getImm())
461 .addReg(Pred[1].getReg());
David Goodwinaf7451b2009-07-08 16:09:28 +0000462 return true;
463 }
464
465 int PIdx = MI->findFirstPredOperandIdx();
466 if (PIdx != -1) {
467 MachineOperand &PMO = MI->getOperand(PIdx);
468 PMO.setImm(Pred[0].getImm());
469 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
470 return true;
471 }
472 return false;
473}
474
475bool ARMBaseInstrInfo::
476SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
477 const SmallVectorImpl<MachineOperand> &Pred2) const {
478 if (Pred1.size() > 2 || Pred2.size() > 2)
479 return false;
480
481 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
482 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
483 if (CC1 == CC2)
484 return true;
485
486 switch (CC1) {
487 default:
488 return false;
489 case ARMCC::AL:
490 return true;
491 case ARMCC::HS:
492 return CC2 == ARMCC::HI;
493 case ARMCC::LS:
494 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
495 case ARMCC::GE:
496 return CC2 == ARMCC::GT;
497 case ARMCC::LE:
498 return CC2 == ARMCC::LT;
499 }
500}
501
502bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
503 std::vector<MachineOperand> &Pred) const {
David Goodwinaf7451b2009-07-08 16:09:28 +0000504 bool Found = false;
505 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
506 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +0000507 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
508 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwinaf7451b2009-07-08 16:09:28 +0000509 Pred.push_back(MO);
510 Found = true;
511 }
512 }
513
514 return Found;
515}
516
Evan Chenga33fc862009-11-21 06:21:52 +0000517/// isPredicable - Return true if the specified instruction can be predicated.
518/// By default, this returns true for every instruction with a
519/// PredicateOperand.
520bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000521 if (!MI->isPredicable())
Evan Chenga33fc862009-11-21 06:21:52 +0000522 return false;
523
Joey Goulya5153cb2013-09-09 14:21:49 +0000524 ARMFunctionInfo *AFI =
525 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
526
527 if (AFI->isThumb2Function()) {
Weiming Zhao0da5cc02013-11-13 18:29:49 +0000528 if (getSubtarget().restrictIT())
Joey Goulya5153cb2013-09-09 14:21:49 +0000529 return isV8EligibleForIT(MI);
530 } else { // non-Thumb
531 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
532 return false;
Evan Chenga33fc862009-11-21 06:21:52 +0000533 }
Joey Goulya5153cb2013-09-09 14:21:49 +0000534
Evan Chenga33fc862009-11-21 06:21:52 +0000535 return true;
536}
David Goodwinaf7451b2009-07-08 16:09:28 +0000537
Artyom Skrobov1a6cd1d2014-02-26 11:27:28 +0000538template<> bool IsCPSRDead<MachineInstr>(MachineInstr* MI) {
539 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
540 const MachineOperand &MO = MI->getOperand(i);
541 if (!MO.isReg() || MO.isUndef() || MO.isUse())
542 continue;
543 if (MO.getReg() != ARM::CPSR)
544 continue;
545 if (!MO.isDead())
546 return false;
547 }
548 // all definitions of CPSR are dead
549 return true;
550}
551
Chris Lattnerc831fac2009-12-03 06:58:32 +0000552/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
Chandler Carruth82058c02010-10-23 08:40:19 +0000553LLVM_ATTRIBUTE_NOINLINE
David Goodwinaf7451b2009-07-08 16:09:28 +0000554static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattnerc831fac2009-12-03 06:58:32 +0000555 unsigned JTI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000556static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
557 unsigned JTI) {
Chris Lattnerc831fac2009-12-03 06:58:32 +0000558 assert(JTI < JT.size());
David Goodwinaf7451b2009-07-08 16:09:28 +0000559 return JT[JTI].MBBs.size();
560}
561
562/// GetInstSize - Return the size of the specified MachineInstr.
563///
564unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
565 const MachineBasicBlock &MBB = *MI->getParent();
566 const MachineFunction *MF = MBB.getParent();
Chris Lattnere9a75a62009-08-22 21:43:10 +0000567 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwinaf7451b2009-07-08 16:09:28 +0000568
Evan Cheng6cc775f2011-06-28 19:10:37 +0000569 const MCInstrDesc &MCID = MI->getDesc();
Owen Anderson651b2302011-07-13 23:22:26 +0000570 if (MCID.getSize())
571 return MCID.getSize();
David Goodwinaf7451b2009-07-08 16:09:28 +0000572
David Blaikie46a9f012012-01-20 21:51:11 +0000573 // If this machine instr is an inline asm, measure it.
574 if (MI->getOpcode() == ARM::INLINEASM)
575 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Blaikie46a9f012012-01-20 21:51:11 +0000576 unsigned Opc = MI->getOpcode();
577 switch (Opc) {
Rafael Espindolaafeb01c2014-03-07 04:45:03 +0000578 default:
579 // pseudo-instruction sizes are zero.
David Blaikie46a9f012012-01-20 21:51:11 +0000580 return 0;
581 case TargetOpcode::BUNDLE:
582 return getInstBundleLength(MI);
583 case ARM::MOVi16_ga_pcrel:
584 case ARM::MOVTi16_ga_pcrel:
585 case ARM::t2MOVi16_ga_pcrel:
586 case ARM::t2MOVTi16_ga_pcrel:
587 return 4;
588 case ARM::MOVi32imm:
589 case ARM::t2MOVi32imm:
590 return 8;
591 case ARM::CONSTPOOL_ENTRY:
592 // If this machine instr is a constant pool entry, its size is recorded as
593 // operand #2.
594 return MI->getOperand(2).getImm();
595 case ARM::Int_eh_sjlj_longjmp:
596 return 16;
597 case ARM::tInt_eh_sjlj_longjmp:
598 return 10;
599 case ARM::Int_eh_sjlj_setjmp:
600 case ARM::Int_eh_sjlj_setjmp_nofp:
601 return 20;
602 case ARM::tInt_eh_sjlj_setjmp:
603 case ARM::t2Int_eh_sjlj_setjmp:
604 case ARM::t2Int_eh_sjlj_setjmp_nofp:
605 return 12;
606 case ARM::BR_JTr:
607 case ARM::BR_JTm:
608 case ARM::BR_JTadd:
609 case ARM::tBR_JTr:
610 case ARM::t2BR_JT:
611 case ARM::t2TBB_JT:
612 case ARM::t2TBH_JT: {
613 // These are jumptable branches, i.e. a branch followed by an inlined
614 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
615 // entry is one byte; TBH two byte each.
616 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
617 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
618 unsigned NumOps = MCID.getNumOperands();
619 MachineOperand JTOP =
620 MI->getOperand(NumOps - (MI->isPredicable() ? 3 : 2));
621 unsigned JTI = JTOP.getIndex();
622 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
623 assert(MJTI != 0);
624 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
625 assert(JTI < JT.size());
626 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
627 // 4 aligned. The assembler / linker may add 2 byte padding just before
628 // the JT entries. The size does not include this padding; the
629 // constant islands pass does separate bookkeeping for it.
630 // FIXME: If we know the size of the function is less than (1 << 16) *2
631 // bytes, we can use 16-bit entries instead. Then there won't be an
632 // alignment issue.
633 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
634 unsigned NumEntries = getNumJTEntries(JT, JTI);
635 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
636 // Make sure the instruction that follows TBB is 2-byte aligned.
637 // FIXME: Constant island pass should insert an "ALIGN" instruction
638 // instead.
639 ++NumEntries;
640 return NumEntries * EntrySize + InstSize;
641 }
David Blaikie46a9f012012-01-20 21:51:11 +0000642 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000643}
644
Evan Cheng7fae11b2011-12-14 02:11:42 +0000645unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
646 unsigned Size = 0;
647 MachineBasicBlock::const_instr_iterator I = MI;
648 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
649 while (++I != E && I->isInsideBundle()) {
650 assert(!I->isBundle() && "No nested bundle!");
651 Size += GetInstSizeInBytes(&*I);
652 }
653 return Size;
654}
655
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000656void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
657 MachineBasicBlock::iterator I, DebugLoc DL,
658 unsigned DestReg, unsigned SrcReg,
659 bool KillSrc) const {
660 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000661 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson70aa8d02010-02-16 17:24:15 +0000662
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000663 if (GPRDest && GPRSrc) {
664 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
Jim Grosbach8815bef2013-10-22 02:29:35 +0000665 .addReg(SrcReg, getKillRegState(KillSrc))));
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000666 return;
David Goodwine5b5d8f2009-08-05 21:02:22 +0000667 }
David Goodwinaf7451b2009-07-08 16:09:28 +0000668
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000669 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
Jim Grosbach8815bef2013-10-22 02:29:35 +0000670 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000671
Chad Rosierbe762512011-08-20 00:17:25 +0000672 unsigned Opc = 0;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000673 if (SPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000674 Opc = ARM::VMOVS;
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +0000675 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000676 Opc = ARM::VMOVRS;
677 else if (SPRDest && GPRSrc)
678 Opc = ARM::VMOVSR;
679 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
680 Opc = ARM::VMOVD;
681 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson454e1c72011-07-15 18:46:47 +0000682 Opc = ARM::VORRq;
Jakob Stoklund Olesend7b33002010-07-11 06:33:54 +0000683
Chad Rosierbe762512011-08-20 00:17:25 +0000684 if (Opc) {
685 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson454e1c72011-07-15 18:46:47 +0000686 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierbe762512011-08-20 00:17:25 +0000687 if (Opc == ARM::VORRq)
688 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosier61f92ef2011-08-20 00:52:40 +0000689 AddDefaultPred(MIB);
Chad Rosierbe762512011-08-20 00:17:25 +0000690 return;
691 }
692
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000693 // Handle register classes that require multiple instructions.
694 unsigned BeginIdx = 0;
695 unsigned SubRegs = 0;
Andrew Trickb57e2252012-08-29 04:41:37 +0000696 int Spacing = 1;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000697
698 // Use VORRq when possible.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000699 if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
700 Opc = ARM::VORRq;
701 BeginIdx = ARM::qsub_0;
702 SubRegs = 2;
703 } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
704 Opc = ARM::VORRq;
705 BeginIdx = ARM::qsub_0;
706 SubRegs = 4;
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000707 // Fall back to VMOVD.
Jim Grosbach8815bef2013-10-22 02:29:35 +0000708 } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
709 Opc = ARM::VMOVD;
710 BeginIdx = ARM::dsub_0;
711 SubRegs = 2;
712 } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
713 Opc = ARM::VMOVD;
714 BeginIdx = ARM::dsub_0;
715 SubRegs = 3;
716 } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
717 Opc = ARM::VMOVD;
718 BeginIdx = ARM::dsub_0;
719 SubRegs = 4;
720 } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
Jim Grosbachdba14dd2013-10-22 02:29:37 +0000721 Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
Jim Grosbach8815bef2013-10-22 02:29:35 +0000722 BeginIdx = ARM::gsub_0;
723 SubRegs = 2;
724 } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
725 Opc = ARM::VMOVD;
726 BeginIdx = ARM::dsub_0;
727 SubRegs = 2;
728 Spacing = 2;
729 } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
730 Opc = ARM::VMOVD;
731 BeginIdx = ARM::dsub_0;
732 SubRegs = 3;
733 Spacing = 2;
734 } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
735 Opc = ARM::VMOVD;
736 BeginIdx = ARM::dsub_0;
737 SubRegs = 4;
738 Spacing = 2;
739 }
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000740
Andrew Trickb57e2252012-08-29 04:41:37 +0000741 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesencaa6bd22012-03-29 21:10:40 +0000742
Andrew Trick4cc69492012-08-29 01:58:52 +0000743 const TargetRegisterInfo *TRI = &getRegisterInfo();
744 MachineInstrBuilder Mov;
Andrew Trickbd0073d2012-08-29 01:58:55 +0000745
746 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
747 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000748 BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000749 Spacing = -Spacing;
750 }
751#ifndef NDEBUG
752 SmallSet<unsigned, 4> DstRegs;
753#endif
Andrew Trick4cc69492012-08-29 01:58:52 +0000754 for (unsigned i = 0; i != SubRegs; ++i) {
Jim Grosbach8815bef2013-10-22 02:29:35 +0000755 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
756 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
Andrew Trick4cc69492012-08-29 01:58:52 +0000757 assert(Dst && Src && "Bad sub-register");
Andrew Trickbd0073d2012-08-29 01:58:55 +0000758#ifndef NDEBUG
Andrew Trickbd0073d2012-08-29 01:58:55 +0000759 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trickb57e2252012-08-29 04:41:37 +0000760 DstRegs.insert(Dst);
Andrew Trickbd0073d2012-08-29 01:58:55 +0000761#endif
Jim Grosbach8815bef2013-10-22 02:29:35 +0000762 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
Andrew Trick4cc69492012-08-29 01:58:52 +0000763 // VORR takes two source operands.
764 if (Opc == ARM::VORRq)
765 Mov.addReg(Src);
766 Mov = AddDefaultPred(Mov);
JF Bastien583db652013-07-12 23:33:03 +0000767 // MOVr can set CC.
768 if (Opc == ARM::MOVr)
769 Mov = AddDefaultCC(Mov);
Andrew Trick4cc69492012-08-29 01:58:52 +0000770 }
771 // Add implicit super-register defs and kills to the last instruction.
772 Mov->addRegisterDefined(DestReg, TRI);
773 if (KillSrc)
774 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwinaf7451b2009-07-08 16:09:28 +0000775}
776
Tim Northover798697d2013-04-21 11:57:07 +0000777const MachineInstrBuilder &
778ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
779 unsigned SubIdx, unsigned State,
780 const TargetRegisterInfo *TRI) const {
Evan Chengddc93c72010-05-07 00:24:52 +0000781 if (!SubIdx)
782 return MIB.addReg(Reg, State);
783
784 if (TargetRegisterInfo::isPhysicalRegister(Reg))
785 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
786 return MIB.addReg(Reg, State, SubIdx);
787}
788
David Goodwinaf7451b2009-07-08 16:09:28 +0000789void ARMBaseInstrInfo::
790storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
791 unsigned SrcReg, bool isKill, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000792 const TargetRegisterClass *RC,
793 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000794 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000795 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000796 MachineFunction &MF = *MBB.getParent();
797 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000798 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000799
800 MachineMemOperand *MMO =
Jay Foad465101b2011-11-15 07:34:52 +0000801 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000802 MachineMemOperand::MOStore,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000803 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000804 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000805
Owen Anderson732f82c2011-08-10 17:21:20 +0000806 switch (RC->getSize()) {
807 case 4:
808 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
809 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwinaf7451b2009-07-08 16:09:28 +0000810 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach338de3e2010-10-27 23:12:14 +0000811 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000812 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
813 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Cheng9d768f42010-05-06 01:34:11 +0000814 .addReg(SrcReg, getKillRegState(isKill))
815 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000816 } else
817 llvm_unreachable("Unknown reg class!");
818 break;
819 case 8:
820 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
821 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwinaf7451b2009-07-08 16:09:28 +0000822 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000823 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +0000824 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +0000825 if (Subtarget.hasV5TEOps()) {
826 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::STRD));
827 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
828 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
829 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
830
831 AddDefaultPred(MIB);
832 } else {
833 // Fallback to STM instruction, which has existed since the dawn of
834 // time.
835 MachineInstrBuilder MIB =
836 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
837 .addFrameIndex(FI).addMemOperand(MMO));
838 AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
839 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
840 }
Owen Anderson732f82c2011-08-10 17:21:20 +0000841 } else
842 llvm_unreachable("Unknown reg class!");
843 break;
844 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +0000845 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +0000846 // Use aligned spills if the stack can be realigned.
847 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000848 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilson4c1ca292010-07-06 21:26:18 +0000849 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000850 .addReg(SrcReg, getKillRegState(isKill))
851 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000852 } else {
853 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000854 .addReg(SrcReg, getKillRegState(isKill))
855 .addFrameIndex(FI)
Evan Cheng9de7cfe2010-05-13 01:12:06 +0000856 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000857 }
858 } else
859 llvm_unreachable("Unknown reg class!");
860 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000861 case 24:
862 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
863 // Use aligned spills if the stack can be realigned.
864 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
865 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
866 .addFrameIndex(FI).addImm(16)
867 .addReg(SrcReg, getKillRegState(isKill))
868 .addMemOperand(MMO));
869 } else {
870 MachineInstrBuilder MIB =
871 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
872 .addFrameIndex(FI))
873 .addMemOperand(MMO);
874 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
875 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
876 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
877 }
878 } else
879 llvm_unreachable("Unknown reg class!");
880 break;
Owen Anderson732f82c2011-08-10 17:21:20 +0000881 case 32:
Anton Korobeynikov218aaf62012-08-04 13:16:12 +0000882 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +0000883 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
884 // FIXME: It's possible to only store part of the QQ register if the
885 // spilled def has a sub-register index.
886 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +0000887 .addFrameIndex(FI).addImm(16)
888 .addReg(SrcReg, getKillRegState(isKill))
889 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +0000890 } else {
891 MachineInstrBuilder MIB =
892 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000893 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +0000894 .addMemOperand(MMO);
895 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
896 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
897 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
898 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
899 }
900 } else
901 llvm_unreachable("Unknown reg class!");
902 break;
903 case 64:
904 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
905 MachineInstrBuilder MIB =
906 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
907 .addFrameIndex(FI))
908 .addMemOperand(MMO);
909 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
910 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
911 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
912 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
913 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
914 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
915 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
916 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
917 } else
918 llvm_unreachable("Unknown reg class!");
919 break;
920 default:
921 llvm_unreachable("Unknown reg class!");
David Goodwinaf7451b2009-07-08 16:09:28 +0000922 }
923}
924
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000925unsigned
926ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
927 int &FrameIndex) const {
928 switch (MI->getOpcode()) {
929 default: break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000930 case ARM::STRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000931 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
932 if (MI->getOperand(1).isFI() &&
933 MI->getOperand(2).isReg() &&
934 MI->getOperand(3).isImm() &&
935 MI->getOperand(2).getReg() == 0 &&
936 MI->getOperand(3).getImm() == 0) {
937 FrameIndex = MI->getOperand(1).getIndex();
938 return MI->getOperand(0).getReg();
939 }
940 break;
Jim Grosbach338de3e2010-10-27 23:12:14 +0000941 case ARM::STRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000942 case ARM::t2STRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +0000943 case ARM::tSTRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000944 case ARM::VSTRD:
945 case ARM::VSTRS:
946 if (MI->getOperand(1).isFI() &&
947 MI->getOperand(2).isImm() &&
948 MI->getOperand(2).getImm() == 0) {
949 FrameIndex = MI->getOperand(1).getIndex();
950 return MI->getOperand(0).getReg();
951 }
952 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +0000953 case ARM::VST1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +0000954 case ARM::VST1d64TPseudo:
955 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +0000956 if (MI->getOperand(0).isFI() &&
957 MI->getOperand(2).getSubReg() == 0) {
958 FrameIndex = MI->getOperand(0).getIndex();
959 return MI->getOperand(2).getReg();
960 }
Jakob Stoklund Olesenb929c712010-09-15 21:40:09 +0000961 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +0000962 case ARM::VSTMQIA:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +0000963 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +0000964 MI->getOperand(0).getSubReg() == 0) {
965 FrameIndex = MI->getOperand(1).getIndex();
966 return MI->getOperand(0).getReg();
967 }
968 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +0000969 }
970
971 return 0;
972}
973
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +0000974unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
975 int &FrameIndex) const {
976 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +0000977 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +0000978}
979
David Goodwinaf7451b2009-07-08 16:09:28 +0000980void ARMBaseInstrInfo::
981loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
982 unsigned DestReg, int FI,
Evan Chengefb126a2010-05-06 19:06:44 +0000983 const TargetRegisterClass *RC,
984 const TargetRegisterInfo *TRI) const {
Chris Lattner6f306d72010-04-02 20:16:16 +0000985 DebugLoc DL;
David Goodwinaf7451b2009-07-08 16:09:28 +0000986 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000987 MachineFunction &MF = *MBB.getParent();
988 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000989 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000990 MachineMemOperand *MMO =
Chris Lattnere3d864b2010-09-21 04:39:43 +0000991 MF.getMachineMemOperand(
Jay Foad465101b2011-11-15 07:34:52 +0000992 MachinePointerInfo::getFixedStack(FI),
Chris Lattnere3d864b2010-09-21 04:39:43 +0000993 MachineMemOperand::MOLoad,
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +0000994 MFI.getObjectSize(FI),
Jim Grosbacha15c3b72009-11-08 00:27:19 +0000995 Align);
David Goodwinaf7451b2009-07-08 16:09:28 +0000996
Owen Anderson732f82c2011-08-10 17:21:20 +0000997 switch (RC->getSize()) {
998 case 4:
999 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
1000 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
1001 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson37f106e2010-02-16 22:01:59 +00001002
Owen Anderson732f82c2011-08-10 17:21:20 +00001003 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
1004 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001005 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001006 } else
1007 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001008 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001009 case 8:
1010 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
1011 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Cheng9d768f42010-05-06 01:34:11 +00001012 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001013 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
Tim Northover798697d2013-04-21 11:57:07 +00001014 MachineInstrBuilder MIB;
1015
1016 if (Subtarget.hasV5TEOps()) {
1017 MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
1018 AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1019 AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1020 MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO);
1021
1022 AddDefaultPred(MIB);
1023 } else {
1024 // Fallback to LDM instruction, which has existed since the dawn of
1025 // time.
1026 MIB = AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDMIA))
1027 .addFrameIndex(FI).addMemOperand(MMO));
1028 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
1029 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
1030 }
1031
Jakob Stoklund Olesene46a1042012-10-26 21:29:15 +00001032 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1033 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001034 } else
1035 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001036 break;
Owen Anderson732f82c2011-08-10 17:21:20 +00001037 case 16:
Jakob Stoklund Olesen9e512122012-03-28 21:20:32 +00001038 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesend110e2a2012-01-05 00:26:57 +00001039 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001040 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilson4c1ca292010-07-06 21:26:18 +00001041 .addFrameIndex(FI).addImm(16)
Evan Cheng9de7cfe2010-05-13 01:12:06 +00001042 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001043 } else {
1044 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
1045 .addFrameIndex(FI)
1046 .addMemOperand(MMO));
1047 }
1048 } else
1049 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001050 break;
Anton Korobeynikov218aaf62012-08-04 13:16:12 +00001051 case 24:
1052 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1053 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1054 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1055 .addFrameIndex(FI).addImm(16)
1056 .addMemOperand(MMO));
1057 } else {
1058 MachineInstrBuilder MIB =
1059 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1060 .addFrameIndex(FI)
1061 .addMemOperand(MMO));
1062 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1063 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1064 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1065 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1066 MIB.addReg(DestReg, RegState::ImplicitDefine);
1067 }
1068 } else
1069 llvm_unreachable("Unknown reg class!");
1070 break;
1071 case 32:
1072 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Anderson732f82c2011-08-10 17:21:20 +00001073 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1074 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilsonb1e9d4b2010-09-15 01:48:05 +00001075 .addFrameIndex(FI).addImm(16)
1076 .addMemOperand(MMO));
Owen Anderson732f82c2011-08-10 17:21:20 +00001077 } else {
1078 MachineInstrBuilder MIB =
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001079 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1080 .addFrameIndex(FI))
Owen Anderson732f82c2011-08-10 17:21:20 +00001081 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001082 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1083 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1084 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1085 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001086 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1087 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001088 }
1089 } else
1090 llvm_unreachable("Unknown reg class!");
1091 break;
1092 case 64:
1093 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1094 MachineInstrBuilder MIB =
1095 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1096 .addFrameIndex(FI))
1097 .addMemOperand(MMO);
Jakob Stoklund Olesenf729cea2012-03-04 18:40:30 +00001098 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1099 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1100 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1101 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1102 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1103 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1104 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1105 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesend9b427e2012-03-06 02:48:17 +00001106 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1107 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Anderson732f82c2011-08-10 17:21:20 +00001108 } else
1109 llvm_unreachable("Unknown reg class!");
Bob Wilsona92e41a2010-06-18 21:32:42 +00001110 break;
Bob Wilsona92e41a2010-06-18 21:32:42 +00001111 default:
1112 llvm_unreachable("Unknown regclass!");
David Goodwinaf7451b2009-07-08 16:09:28 +00001113 }
1114}
1115
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001116unsigned
1117ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1118 int &FrameIndex) const {
1119 switch (MI->getOpcode()) {
1120 default: break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001121 case ARM::LDRrs:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001122 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1123 if (MI->getOperand(1).isFI() &&
1124 MI->getOperand(2).isReg() &&
1125 MI->getOperand(3).isImm() &&
1126 MI->getOperand(2).getReg() == 0 &&
1127 MI->getOperand(3).getImm() == 0) {
1128 FrameIndex = MI->getOperand(1).getIndex();
1129 return MI->getOperand(0).getReg();
1130 }
1131 break;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001132 case ARM::LDRi12:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001133 case ARM::t2LDRi12:
Jim Grosbachd86f34d2011-06-29 20:26:39 +00001134 case ARM::tLDRspi:
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001135 case ARM::VLDRD:
1136 case ARM::VLDRS:
1137 if (MI->getOperand(1).isFI() &&
1138 MI->getOperand(2).isImm() &&
1139 MI->getOperand(2).getImm() == 0) {
1140 FrameIndex = MI->getOperand(1).getIndex();
1141 return MI->getOperand(0).getReg();
1142 }
1143 break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00001144 case ARM::VLD1q64:
Anton Korobeynikov3a4fdfe2012-08-04 13:22:14 +00001145 case ARM::VLD1d64TPseudo:
1146 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesen33005d12010-09-15 17:27:09 +00001147 if (MI->getOperand(1).isFI() &&
1148 MI->getOperand(0).getSubReg() == 0) {
1149 FrameIndex = MI->getOperand(1).getIndex();
1150 return MI->getOperand(0).getReg();
1151 }
1152 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00001153 case ARM::VLDMQIA:
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001154 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen44857a32010-09-15 21:40:11 +00001155 MI->getOperand(0).getSubReg() == 0) {
1156 FrameIndex = MI->getOperand(1).getIndex();
1157 return MI->getOperand(0).getReg();
1158 }
1159 break;
Jakob Stoklund Olesen11f5be32010-09-15 16:36:26 +00001160 }
1161
1162 return 0;
1163}
1164
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001165unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1166 int &FrameIndex) const {
1167 const MachineMemOperand *Dummy;
Evan Cheng7f8e5632011-12-07 07:15:52 +00001168 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesenc04a66b2011-08-08 21:45:32 +00001169}
1170
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001171bool ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const{
1172 // This hook gets to expand COPY instructions before they become
1173 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1174 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1175 // changed into a VORR that can go down the NEON pipeline.
Silviu Baranga82dd6ac2013-03-15 18:28:25 +00001176 if (!WidenVMOVS || !MI->isCopy() || Subtarget.isCortexA15())
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001177 return false;
1178
1179 // Look for a copy between even S-registers. That is where we keep floats
1180 // when using NEON v2f32 instructions for f32 arithmetic.
1181 unsigned DstRegS = MI->getOperand(0).getReg();
1182 unsigned SrcRegS = MI->getOperand(1).getReg();
1183 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1184 return false;
1185
1186 const TargetRegisterInfo *TRI = &getRegisterInfo();
1187 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1188 &ARM::DPRRegClass);
1189 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1190 &ARM::DPRRegClass);
1191 if (!DstRegD || !SrcRegD)
1192 return false;
1193
1194 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1195 // legal if the COPY already defines the full DstRegD, and it isn't a
1196 // sub-register insertion.
1197 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1198 return false;
1199
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001200 // A dead copy shouldn't show up here, but reject it just in case.
1201 if (MI->getOperand(0).isDead())
1202 return false;
1203
1204 // All clear, widen the COPY.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001205 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001206 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001207
1208 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1209 // or some other super-register.
1210 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1211 if (ImpDefIdx != -1)
1212 MI->RemoveOperand(ImpDefIdx);
1213
1214 // Change the opcode and operands.
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001215 MI->setDesc(get(ARM::VMOVD));
1216 MI->getOperand(0).setReg(DstRegD);
1217 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001218 AddDefaultPred(MIB);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001219
1220 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1221 // register scavenger and machine verifier, so we need to indicate that we
1222 // are reading an undefined value from SrcRegD, but a proper value from
1223 // SrcRegS.
1224 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00001225 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen39c31a72011-10-12 00:06:23 +00001226
1227 // SrcRegD may actually contain an unrelated value in the ssub_1
1228 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1229 if (MI->getOperand(1).isKill()) {
1230 MI->getOperand(1).setIsKill(false);
1231 MI->addRegisterKilled(SrcRegS, TRI, true);
1232 }
1233
Jakob Stoklund Olesenda7c0f82011-10-11 00:59:06 +00001234 DEBUG(dbgs() << "replaced by: " << *MI);
1235 return true;
1236}
1237
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001238/// Create a copy of a const pool value. Update CPI to the new index and return
1239/// the label UID.
1240static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1241 MachineConstantPool *MCP = MF.getConstantPool();
1242 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1243
1244 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1245 assert(MCPE.isMachineConstantPoolEntry() &&
1246 "Expecting a machine constantpool entry!");
1247 ARMConstantPoolValue *ACPV =
1248 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1249
Evan Chengdfce83c2011-01-17 08:03:18 +00001250 unsigned PCLabelId = AFI->createPICLabelUId();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001251 ARMConstantPoolValue *NewCPV = 0;
Oliver Stannard8f859942014-01-29 16:01:24 +00001252
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001253 // FIXME: The below assumes PIC relocation model and that the function
1254 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1255 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1256 // instructions, so that's probably OK, but is PIC always correct when
1257 // we get here?
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001258 if (ACPV->isGlobalValue())
Bill Wendling7753d662011-10-01 08:00:54 +00001259 NewCPV = ARMConstantPoolConstant::
1260 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1261 ARMCP::CPValue, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001262 else if (ACPV->isExtSymbol())
Bill Wendlingc214cb02011-10-01 08:58:29 +00001263 NewCPV = ARMConstantPoolSymbol::
1264 Create(MF.getFunction()->getContext(),
1265 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001266 else if (ACPV->isBlockAddress())
Bill Wendling7753d662011-10-01 08:00:54 +00001267 NewCPV = ARMConstantPoolConstant::
1268 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1269 ARMCP::CPBlockAddress, 4);
Jim Grosbach1f77ee52010-09-10 21:38:22 +00001270 else if (ACPV->isLSDA())
Bill Wendling7753d662011-10-01 08:00:54 +00001271 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1272 ARMCP::CPLSDA, 4);
Bill Wendling69bc3de2011-09-29 23:50:42 +00001273 else if (ACPV->isMachineBasicBlock())
Bill Wendling4a4772f2011-10-01 09:30:42 +00001274 NewCPV = ARMConstantPoolMBB::
1275 Create(MF.getFunction()->getContext(),
1276 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001277 else
1278 llvm_unreachable("Unexpected ARM constantpool value type!!");
1279 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1280 return PCLabelId;
1281}
1282
Evan Chengfe864422009-11-08 00:15:23 +00001283void ARMBaseInstrInfo::
1284reMaterialize(MachineBasicBlock &MBB,
1285 MachineBasicBlock::iterator I,
1286 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001287 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001288 const TargetRegisterInfo &TRI) const {
Evan Chengfe864422009-11-08 00:15:23 +00001289 unsigned Opcode = Orig->getOpcode();
1290 switch (Opcode) {
1291 default: {
1292 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001293 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfe864422009-11-08 00:15:23 +00001294 MBB.insert(I, MI);
1295 break;
1296 }
1297 case ARM::tLDRpci_pic:
1298 case ARM::t2LDRpci_pic: {
1299 MachineFunction &MF = *MBB.getParent();
Evan Chengfe864422009-11-08 00:15:23 +00001300 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001301 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfe864422009-11-08 00:15:23 +00001302 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1303 DestReg)
1304 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattner1d0c2572011-04-29 05:24:29 +00001305 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfe864422009-11-08 00:15:23 +00001306 break;
1307 }
1308 }
Evan Chengfe864422009-11-08 00:15:23 +00001309}
1310
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001311MachineInstr *
1312ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001313 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen29a64c92010-01-06 23:47:07 +00001314 switch(Orig->getOpcode()) {
1315 case ARM::tLDRpci_pic:
1316 case ARM::t2LDRpci_pic: {
1317 unsigned CPI = Orig->getOperand(1).getIndex();
1318 unsigned PCLabelId = duplicateCPV(MF, CPI);
1319 Orig->getOperand(1).setIndex(CPI);
1320 Orig->getOperand(2).setImm(PCLabelId);
1321 break;
1322 }
1323 }
1324 return MI;
1325}
1326
Evan Chenge9c46c22010-03-03 01:44:33 +00001327bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Chengb8b0ad82011-01-20 08:34:58 +00001328 const MachineInstr *MI1,
1329 const MachineRegisterInfo *MRI) const {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001330 int Opcode = MI0->getOpcode();
Evan Cheng028ccbfc2011-01-20 23:55:07 +00001331 if (Opcode == ARM::t2LDRpci ||
Evan Chengbbd50b02009-11-20 02:10:27 +00001332 Opcode == ARM::t2LDRpci_pic ||
1333 Opcode == ARM::tLDRpci ||
Evan Chengb8b0ad82011-01-20 08:34:58 +00001334 Opcode == ARM::tLDRpci_pic ||
Tim Northover72360d22013-12-02 10:35:41 +00001335 Opcode == ARM::LDRLIT_ga_pcrel ||
1336 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1337 Opcode == ARM::tLDRLIT_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001338 Opcode == ARM::MOV_ga_pcrel ||
1339 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001340 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001341 if (MI1->getOpcode() != Opcode)
1342 return false;
1343 if (MI0->getNumOperands() != MI1->getNumOperands())
1344 return false;
1345
1346 const MachineOperand &MO0 = MI0->getOperand(1);
1347 const MachineOperand &MO1 = MI1->getOperand(1);
1348 if (MO0.getOffset() != MO1.getOffset())
1349 return false;
1350
Tim Northover72360d22013-12-02 10:35:41 +00001351 if (Opcode == ARM::LDRLIT_ga_pcrel ||
1352 Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
1353 Opcode == ARM::tLDRLIT_ga_pcrel ||
1354 Opcode == ARM::MOV_ga_pcrel ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001355 Opcode == ARM::MOV_ga_pcrel_ldr ||
Evan Cheng2f2435d2011-01-21 18:55:51 +00001356 Opcode == ARM::t2MOV_ga_pcrel)
Evan Chengb8b0ad82011-01-20 08:34:58 +00001357 // Ignore the PC labels.
1358 return MO0.getGlobal() == MO1.getGlobal();
1359
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001360 const MachineFunction *MF = MI0->getParent()->getParent();
1361 const MachineConstantPool *MCP = MF->getConstantPool();
1362 int CPI0 = MO0.getIndex();
1363 int CPI1 = MO1.getIndex();
1364 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1365 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengf098bf12011-03-24 06:20:03 +00001366 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1367 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1368 if (isARMCP0 && isARMCP1) {
1369 ARMConstantPoolValue *ACPV0 =
1370 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1371 ARMConstantPoolValue *ACPV1 =
1372 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1373 return ACPV0->hasSameValue(ACPV1);
1374 } else if (!isARMCP0 && !isARMCP1) {
1375 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1376 }
1377 return false;
Evan Chengb8b0ad82011-01-20 08:34:58 +00001378 } else if (Opcode == ARM::PICLDR) {
1379 if (MI1->getOpcode() != Opcode)
1380 return false;
1381 if (MI0->getNumOperands() != MI1->getNumOperands())
1382 return false;
1383
1384 unsigned Addr0 = MI0->getOperand(1).getReg();
1385 unsigned Addr1 = MI1->getOperand(1).getReg();
1386 if (Addr0 != Addr1) {
1387 if (!MRI ||
1388 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1389 !TargetRegisterInfo::isVirtualRegister(Addr1))
1390 return false;
1391
1392 // This assumes SSA form.
1393 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1394 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1395 // Check if the loaded value, e.g. a constantpool of a global address, are
1396 // the same.
1397 if (!produceSameValue(Def0, Def1, MRI))
1398 return false;
1399 }
1400
1401 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1402 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1403 const MachineOperand &MO0 = MI0->getOperand(i);
1404 const MachineOperand &MO1 = MI1->getOperand(i);
1405 if (!MO0.isIdenticalTo(MO1))
1406 return false;
1407 }
1408 return true;
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001409 }
1410
Evan Chenge9c46c22010-03-03 01:44:33 +00001411 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chenga8e8a7c2009-11-07 04:04:34 +00001412}
1413
Bill Wendlingf4707472010-06-23 23:00:16 +00001414/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1415/// determine if two loads are loading from the same base address. It should
1416/// only return true if the base pointers are the same and the only differences
1417/// between the two addresses is the offset. It also returns the offsets by
1418/// reference.
Andrew Tricka7714a02012-11-12 19:40:10 +00001419///
1420/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1421/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001422bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1423 int64_t &Offset1,
1424 int64_t &Offset2) const {
1425 // Don't worry about Thumb: just ARM and Thumb2.
1426 if (Subtarget.isThumb1Only()) return false;
1427
1428 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1429 return false;
1430
1431 switch (Load1->getMachineOpcode()) {
1432 default:
1433 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001434 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001435 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001436 case ARM::LDRD:
1437 case ARM::LDRH:
1438 case ARM::LDRSB:
1439 case ARM::LDRSH:
1440 case ARM::VLDRD:
1441 case ARM::VLDRS:
1442 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001443 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001444 case ARM::t2LDRDi8:
1445 case ARM::t2LDRSHi8:
1446 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001447 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001448 case ARM::t2LDRSHi12:
1449 break;
1450 }
1451
1452 switch (Load2->getMachineOpcode()) {
1453 default:
1454 return false;
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00001455 case ARM::LDRi12:
Jim Grosbach5a7c7152010-10-27 00:19:44 +00001456 case ARM::LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001457 case ARM::LDRD:
1458 case ARM::LDRH:
1459 case ARM::LDRSB:
1460 case ARM::LDRSH:
1461 case ARM::VLDRD:
1462 case ARM::VLDRS:
1463 case ARM::t2LDRi8:
Renato Golinb184cd92013-08-14 16:35:29 +00001464 case ARM::t2LDRBi8:
Bill Wendlingf4707472010-06-23 23:00:16 +00001465 case ARM::t2LDRSHi8:
1466 case ARM::t2LDRi12:
Renato Golinb184cd92013-08-14 16:35:29 +00001467 case ARM::t2LDRBi12:
Bill Wendlingf4707472010-06-23 23:00:16 +00001468 case ARM::t2LDRSHi12:
1469 break;
1470 }
1471
1472 // Check if base addresses and chain operands match.
1473 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1474 Load1->getOperand(4) != Load2->getOperand(4))
1475 return false;
1476
1477 // Index should be Reg0.
1478 if (Load1->getOperand(3) != Load2->getOperand(3))
1479 return false;
1480
1481 // Determine the offsets.
1482 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1483 isa<ConstantSDNode>(Load2->getOperand(1))) {
1484 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1485 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1486 return true;
1487 }
1488
1489 return false;
1490}
1491
1492/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001493/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendlingf4707472010-06-23 23:00:16 +00001494/// be scheduled togther. On some targets if two loads are loading from
1495/// addresses in the same cache line, it's better if they are scheduled
1496/// together. This function takes two integers that represent the load offsets
1497/// from the common base address. It returns true if it decides it's desirable
1498/// to schedule the two loads together. "NumLoads" is the number of loads that
1499/// have already been scheduled after Load1.
Andrew Tricka7714a02012-11-12 19:40:10 +00001500///
1501/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1502/// is permanently disabled.
Bill Wendlingf4707472010-06-23 23:00:16 +00001503bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1504 int64_t Offset1, int64_t Offset2,
1505 unsigned NumLoads) const {
1506 // Don't worry about Thumb: just ARM and Thumb2.
1507 if (Subtarget.isThumb1Only()) return false;
1508
1509 assert(Offset2 > Offset1);
1510
1511 if ((Offset2 - Offset1) / 8 > 64)
1512 return false;
1513
Renato Golinb184cd92013-08-14 16:35:29 +00001514 // Check if the machine opcodes are different. If they are different
1515 // then we consider them to not be of the same base address,
1516 // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
1517 // In this case, they are considered to be the same because they are different
1518 // encoding forms of the same basic instruction.
1519 if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
1520 !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
1521 Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
1522 (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
1523 Load2->getMachineOpcode() == ARM::t2LDRBi8)))
Bill Wendlingf4707472010-06-23 23:00:16 +00001524 return false; // FIXME: overly conservative?
1525
1526 // Four loads in a row should be sufficient.
1527 if (NumLoads >= 3)
1528 return false;
1529
1530 return true;
1531}
1532
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001533bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1534 const MachineBasicBlock *MBB,
1535 const MachineFunction &MF) const {
Jim Grosbachba3ece62010-06-25 18:43:14 +00001536 // Debug info is never a scheduling boundary. It's necessary to be explicit
1537 // due to the special treatment of IT instructions below, otherwise a
1538 // dbg_value followed by an IT will result in the IT instruction being
1539 // considered a scheduling hazard, which is wrong. It should be the actual
1540 // instruction preceding the dbg_value instruction(s), just like it is
1541 // when debug info is not present.
1542 if (MI->isDebugValue())
1543 return false;
1544
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001545 // Terminators and labels can't be scheduled around.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001546 if (MI->isTerminator() || MI->isPosition())
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001547 return true;
1548
1549 // Treat the start of the IT block as a scheduling boundary, but schedule
1550 // t2IT along with all instructions following it.
1551 // FIXME: This is a big hammer. But the alternative is to add all potential
1552 // true and anti dependencies to IT block instructions as implicit operands
1553 // to the t2IT instruction. The added compile time and complexity does not
1554 // seem worth it.
1555 MachineBasicBlock::const_iterator I = MI;
Jim Grosbachba3ece62010-06-25 18:43:14 +00001556 // Make sure to skip any dbg_value instructions
1557 while (++I != MBB->end() && I->isDebugValue())
1558 ;
1559 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001560 return true;
1561
1562 // Don't attempt to schedule around any instruction that defines
1563 // a stack-oriented pointer, as it's unlikely to be profitable. This
1564 // saves compile time, because it doesn't require every single
1565 // stack slot reference to depend on the instruction that does the
1566 // modification.
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001567 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen5f37f1c2012-02-22 01:07:19 +00001568 // No ARM calling conventions change the stack pointer. (X86 calling
1569 // conventions sometimes do).
Jakob Stoklund Olesen6909faa2012-02-21 23:47:43 +00001570 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng2d51c7c2010-06-18 23:09:54 +00001571 return true;
1572
1573 return false;
1574}
1575
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001576bool ARMBaseInstrInfo::
1577isProfitableToIfCvt(MachineBasicBlock &MBB,
1578 unsigned NumCycles, unsigned ExtraPredCycles,
1579 const BranchProbability &Probability) const {
Cameron Zwarich80018502011-04-13 06:39:16 +00001580 if (!NumCycles)
Evan Cheng02b184d2010-06-25 22:42:03 +00001581 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001582
Owen Anderson88af7d02010-09-28 18:32:13 +00001583 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001584 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1585 UnpredCost /= Probability.getDenominator();
1586 UnpredCost += 1; // The branch itself
1587 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001588
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001589 return (NumCycles + ExtraPredCycles) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001590}
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001591
Evan Cheng02b184d2010-06-25 22:42:03 +00001592bool ARMBaseInstrInfo::
Evan Chengdebf9c52010-11-03 00:45:17 +00001593isProfitableToIfCvt(MachineBasicBlock &TMBB,
1594 unsigned TCycles, unsigned TExtra,
1595 MachineBasicBlock &FMBB,
1596 unsigned FCycles, unsigned FExtra,
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001597 const BranchProbability &Probability) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00001598 if (!TCycles || !FCycles)
Owen Anderson88af7d02010-09-28 18:32:13 +00001599 return false;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001600
Owen Anderson88af7d02010-09-28 18:32:13 +00001601 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001602 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1603 TUnpredCost /= Probability.getDenominator();
Andrew Trick3f1fdf12011-09-21 02:17:37 +00001604
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001605 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1606 unsigned FUnpredCost = Comp * FCycles;
1607 FUnpredCost /= Probability.getDenominator();
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00001608
Jakub Staszak9b07c0a2011-07-10 02:58:07 +00001609 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1610 UnpredCost += 1; // The branch itself
1611 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1612
1613 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
Evan Cheng02b184d2010-06-25 22:42:03 +00001614}
1615
Bob Wilsone8a549c2012-09-29 21:43:49 +00001616bool
1617ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1618 MachineBasicBlock &FMBB) const {
1619 // Reduce false anti-dependencies to let Swift's out-of-order execution
1620 // engine do its thing.
1621 return Subtarget.isSwift();
1622}
1623
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001624/// getInstrPredicate - If instruction is predicated, returns its predicate
1625/// condition, otherwise returns AL. It also returns the condition code
1626/// register by reference.
Evan Cheng83e0d482009-09-28 09:14:39 +00001627ARMCC::CondCodes
1628llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng2aa91cc2009-08-08 03:20:32 +00001629 int PIdx = MI->findFirstPredOperandIdx();
1630 if (PIdx == -1) {
1631 PredReg = 0;
1632 return ARMCC::AL;
1633 }
1634
1635 PredReg = MI->getOperand(PIdx+1).getReg();
1636 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1637}
1638
1639
Evan Cheng780748d2009-07-28 05:48:47 +00001640int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng056c6692009-07-27 18:20:05 +00001641 if (Opc == ARM::B)
1642 return ARM::Bcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001643 if (Opc == ARM::tB)
Evan Cheng056c6692009-07-27 18:20:05 +00001644 return ARM::tBcc;
David Blaikie46a9f012012-01-20 21:51:11 +00001645 if (Opc == ARM::t2B)
1646 return ARM::t2Bcc;
Evan Cheng056c6692009-07-27 18:20:05 +00001647
1648 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng056c6692009-07-27 18:20:05 +00001649}
1650
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001651/// commuteInstruction - Handle commutable instructions.
1652MachineInstr *
1653ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1654 switch (MI->getOpcode()) {
1655 case ARM::MOVCCr:
1656 case ARM::t2MOVCCr: {
1657 // MOVCC can be commuted by inverting the condition.
1658 unsigned PredReg = 0;
1659 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1660 // MOVCC AL can't be inverted. Shouldn't happen.
1661 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
1662 return NULL;
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001663 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001664 if (!MI)
1665 return NULL;
1666 // After swapping the MOVCC operands, also invert the condition.
1667 MI->getOperand(MI->findFirstPredOperandIdx())
1668 .setImm(ARMCC::getOppositeCondition(CC));
1669 return MI;
1670 }
1671 }
Jakob Stoklund Olesen9de596e2012-11-28 02:35:17 +00001672 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesen0a5b72f2012-04-04 18:23:42 +00001673}
Evan Cheng780748d2009-07-28 05:48:47 +00001674
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001675/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001676/// return the defining instruction.
1677static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1678 const MachineRegisterInfo &MRI,
1679 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001680 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1681 return 0;
1682 if (!MRI.hasOneNonDBGUse(Reg))
1683 return 0;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001684 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001685 if (!MI)
1686 return 0;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001687 // MI is folded into the MOVCC by predicating it.
1688 if (!MI->isPredicable())
1689 return 0;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001690 // Check if MI has any non-dead defs or physreg uses. This also detects
1691 // predicated instructions which will be reading CPSR.
1692 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1693 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen7b1a2e82012-08-17 20:55:34 +00001694 // Reject frame index operands, PEI can't handle the predicated pseudos.
1695 if (MO.isFI() || MO.isCPI() || MO.isJTI())
1696 return 0;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001697 if (!MO.isReg())
1698 continue;
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001699 // MI can't have any tied operands, that would conflict with predication.
1700 if (MO.isTied())
1701 return 0;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001702 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1703 return 0;
1704 if (MO.isDef() && !MO.isDead())
1705 return 0;
1706 }
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001707 bool DontMoveAcrossStores = true;
1708 if (!MI->isSafeToMove(TII, /* AliasAnalysis = */ 0, DontMoveAcrossStores))
1709 return 0;
1710 return MI;
Jakob Stoklund Olesen6cb96122012-08-15 22:16:39 +00001711}
1712
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001713bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1714 SmallVectorImpl<MachineOperand> &Cond,
1715 unsigned &TrueOp, unsigned &FalseOp,
1716 bool &Optimizable) const {
1717 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1718 "Unknown select instruction");
1719 // MOVCC operands:
1720 // 0: Def.
1721 // 1: True use.
1722 // 2: False use.
1723 // 3: Condition code.
1724 // 4: CPSR use.
1725 TrueOp = 1;
1726 FalseOp = 2;
1727 Cond.push_back(MI->getOperand(3));
1728 Cond.push_back(MI->getOperand(4));
1729 // We can always fold a def.
1730 Optimizable = true;
1731 return false;
1732}
1733
1734MachineInstr *ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1735 bool PreferFalse) const {
1736 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1737 "Unknown select instruction");
Matthias Braun2f169f92013-10-04 16:52:56 +00001738 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001739 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1740 bool Invert = !DefMI;
1741 if (!DefMI)
1742 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1743 if (!DefMI)
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001744 return 0;
1745
Matthias Braun2f169f92013-10-04 16:52:56 +00001746 // Find new register class to use.
1747 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1748 unsigned DestReg = MI->getOperand(0).getReg();
1749 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1750 if (!MRI.constrainRegClass(DestReg, PreviousClass))
1751 return 0;
1752
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001753 // Create a new predicated version of DefMI.
1754 // Rfalse is the first use.
1755 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Matthias Braun2f169f92013-10-04 16:52:56 +00001756 DefMI->getDesc(), DestReg);
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001757
1758 // Copy all the DefMI operands, excluding its (null) predicate.
1759 const MCInstrDesc &DefDesc = DefMI->getDesc();
1760 for (unsigned i = 1, e = DefDesc.getNumOperands();
1761 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1762 NewMI.addOperand(DefMI->getOperand(i));
1763
1764 unsigned CondCode = MI->getOperand(3).getImm();
1765 if (Invert)
1766 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1767 else
1768 NewMI.addImm(CondCode);
1769 NewMI.addOperand(MI->getOperand(4));
1770
1771 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1772 if (NewMI->hasOptionalDef())
1773 AddDefaultCC(NewMI);
1774
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001775 // The output register value when the predicate is false is an implicit
1776 // register operand tied to the first def.
1777 // The tie makes the register allocator ensure the FalseReg is allocated the
1778 // same register as operand 0.
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001779 FalseReg.setImplicit();
Jakob Stoklund Olesen2ea20362012-12-20 22:53:55 +00001780 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesenf8310592012-09-05 23:58:02 +00001781 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1782
Jakob Stoklund Olesenc19bf022012-08-16 23:14:20 +00001783 // The caller will erase MI, but not DefMI.
1784 DefMI->eraseFromParent();
1785 return NewMI;
1786}
1787
Andrew Trick924123a2011-09-21 02:20:46 +00001788/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1789/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1790/// def operand.
1791///
1792/// This will go away once we can teach tblgen how to set the optional CPSR def
1793/// operand itself.
1794struct AddSubFlagsOpcodePair {
Craig Topper2fbd1302012-05-24 03:59:11 +00001795 uint16_t PseudoOpc;
1796 uint16_t MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001797};
1798
Craig Topper2fbd1302012-05-24 03:59:11 +00001799static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick924123a2011-09-21 02:20:46 +00001800 {ARM::ADDSri, ARM::ADDri},
1801 {ARM::ADDSrr, ARM::ADDrr},
1802 {ARM::ADDSrsi, ARM::ADDrsi},
1803 {ARM::ADDSrsr, ARM::ADDrsr},
1804
1805 {ARM::SUBSri, ARM::SUBri},
1806 {ARM::SUBSrr, ARM::SUBrr},
1807 {ARM::SUBSrsi, ARM::SUBrsi},
1808 {ARM::SUBSrsr, ARM::SUBrsr},
1809
1810 {ARM::RSBSri, ARM::RSBri},
Andrew Trick924123a2011-09-21 02:20:46 +00001811 {ARM::RSBSrsi, ARM::RSBrsi},
1812 {ARM::RSBSrsr, ARM::RSBrsr},
1813
1814 {ARM::t2ADDSri, ARM::t2ADDri},
1815 {ARM::t2ADDSrr, ARM::t2ADDrr},
1816 {ARM::t2ADDSrs, ARM::t2ADDrs},
1817
1818 {ARM::t2SUBSri, ARM::t2SUBri},
1819 {ARM::t2SUBSrr, ARM::t2SUBrr},
1820 {ARM::t2SUBSrs, ARM::t2SUBrs},
1821
1822 {ARM::t2RSBSri, ARM::t2RSBri},
1823 {ARM::t2RSBSrs, ARM::t2RSBrs},
1824};
1825
1826unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Topper2fbd1302012-05-24 03:59:11 +00001827 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1828 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1829 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick924123a2011-09-21 02:20:46 +00001830 return 0;
1831}
1832
Evan Cheng780748d2009-07-28 05:48:47 +00001833void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1834 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1835 unsigned DestReg, unsigned BaseReg, int NumBytes,
1836 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001837 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Tim Northoverc9432eb2013-11-04 23:04:15 +00001838 if (NumBytes == 0 && DestReg != BaseReg) {
1839 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
1840 .addReg(BaseReg, RegState::Kill)
1841 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1842 .setMIFlags(MIFlags);
1843 return;
1844 }
1845
Evan Cheng780748d2009-07-28 05:48:47 +00001846 bool isSub = NumBytes < 0;
1847 if (isSub) NumBytes = -NumBytes;
1848
1849 while (NumBytes) {
1850 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1851 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1852 assert(ThisVal && "Didn't extract field correctly");
1853
1854 // We will handle these bits from offset, clear them.
1855 NumBytes &= ~ThisVal;
1856
1857 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1858
1859 // Build the new ADD / SUB.
1860 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1861 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1862 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikove7410dd2011-03-05 18:43:32 +00001863 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1864 .setMIFlags(MIFlags);
Evan Cheng780748d2009-07-28 05:48:47 +00001865 BaseReg = DestReg;
1866 }
1867}
1868
Weiming Zhao01524852014-03-20 23:28:16 +00001869static bool isAnySubRegLive(unsigned Reg, const TargetRegisterInfo *TRI,
1870 MachineInstr *MI) {
1871 for (MCSubRegIterator Subreg(Reg, TRI, /* IncludeSelf */ true);
1872 Subreg.isValid(); ++Subreg)
1873 if (MI->getParent()->computeRegisterLiveness(TRI, *Subreg, MI) !=
1874 MachineBasicBlock::LQR_Dead)
1875 return true;
1876 return false;
1877}
Tim Northoverdee86042013-12-02 14:46:26 +00001878bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
1879 MachineFunction &MF, MachineInstr *MI,
Tim Northover93bcc662013-11-08 17:18:07 +00001880 unsigned NumBytes) {
1881 // This optimisation potentially adds lots of load and store
1882 // micro-operations, it's only really a great benefit to code-size.
Tim Northoverdee86042013-12-02 14:46:26 +00001883 if (!Subtarget.isMinSize())
Tim Northover93bcc662013-11-08 17:18:07 +00001884 return false;
1885
1886 // If only one register is pushed/popped, LLVM can use an LDR/STR
1887 // instead. We can't modify those so make sure we're dealing with an
1888 // instruction we understand.
1889 bool IsPop = isPopOpcode(MI->getOpcode());
1890 bool IsPush = isPushOpcode(MI->getOpcode());
1891 if (!IsPush && !IsPop)
1892 return false;
1893
1894 bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
1895 MI->getOpcode() == ARM::VLDMDIA_UPD;
1896 bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
1897 MI->getOpcode() == ARM::tPOP ||
1898 MI->getOpcode() == ARM::tPOP_RET;
1899
1900 assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
1901 MI->getOperand(1).getReg() == ARM::SP)) &&
1902 "trying to fold sp update into non-sp-updating push/pop");
1903
1904 // The VFP push & pop act on D-registers, so we can only fold an adjustment
1905 // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
1906 // if this is violated.
1907 if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
1908 return false;
1909
1910 // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
1911 // pred) so the list starts at 4. Thumb1 starts after the predicate.
1912 int RegListIdx = IsT1PushPop ? 2 : 4;
1913
1914 // Calculate the space we'll need in terms of registers.
1915 unsigned FirstReg = MI->getOperand(RegListIdx).getReg();
1916 unsigned RD0Reg, RegsNeeded;
1917 if (IsVFPPushPop) {
1918 RD0Reg = ARM::D0;
1919 RegsNeeded = NumBytes / 8;
1920 } else {
1921 RD0Reg = ARM::R0;
1922 RegsNeeded = NumBytes / 4;
1923 }
1924
1925 // We're going to have to strip all list operands off before
1926 // re-adding them since the order matters, so save the existing ones
1927 // for later.
1928 SmallVector<MachineOperand, 4> RegList;
1929 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
1930 RegList.push_back(MI->getOperand(i));
1931
Tim Northover93bcc662013-11-08 17:18:07 +00001932 const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
Tim Northover45479dc2013-12-01 14:16:24 +00001933 const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
Tim Northover93bcc662013-11-08 17:18:07 +00001934
1935 // Now try to find enough space in the reglist to allocate NumBytes.
1936 for (unsigned CurReg = FirstReg - 1; CurReg >= RD0Reg && RegsNeeded;
Tim Northover45479dc2013-12-01 14:16:24 +00001937 --CurReg) {
Tim Northover93bcc662013-11-08 17:18:07 +00001938 if (!IsPop) {
1939 // Pushing any register is completely harmless, mark the
1940 // register involved as undef since we don't care about it in
1941 // the slightest.
1942 RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
1943 false, false, true));
Tim Northover45479dc2013-12-01 14:16:24 +00001944 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00001945 continue;
1946 }
1947
Tim Northover45479dc2013-12-01 14:16:24 +00001948 // However, we can only pop an extra register if it's not live. For
1949 // registers live within the function we might clobber a return value
1950 // register; the other way a register can be live here is if it's
1951 // callee-saved.
Weiming Zhao01524852014-03-20 23:28:16 +00001952 // TODO: Currently, computeRegisterLiveness() does not report "live" if a
1953 // sub reg is live. When computeRegisterLiveness() works for sub reg, it
1954 // can replace isAnySubRegLive().
Tim Northover45479dc2013-12-01 14:16:24 +00001955 if (isCalleeSavedRegister(CurReg, CSRegs) ||
Weiming Zhao01524852014-03-20 23:28:16 +00001956 isAnySubRegLive(CurReg, TRI, MI)) {
Tim Northover45479dc2013-12-01 14:16:24 +00001957 // VFP pops don't allow holes in the register list, so any skip is fatal
1958 // for our transformation. GPR pops do, so we should just keep looking.
1959 if (IsVFPPushPop)
1960 return false;
1961 else
1962 continue;
1963 }
Tim Northover93bcc662013-11-08 17:18:07 +00001964
1965 // Mark the unimportant registers as <def,dead> in the POP.
Lang Hames1ca11232013-11-22 00:46:32 +00001966 RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
1967 true));
Tim Northover45479dc2013-12-01 14:16:24 +00001968 --RegsNeeded;
Tim Northover93bcc662013-11-08 17:18:07 +00001969 }
1970
1971 if (RegsNeeded > 0)
1972 return false;
1973
1974 // Finally we know we can profitably perform the optimisation so go
1975 // ahead: strip all existing registers off and add them back again
1976 // in the right order.
1977 for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
1978 MI->RemoveOperand(i);
1979
1980 // Add the complete list back in.
1981 MachineInstrBuilder MIB(MF, &*MI);
1982 for (int i = RegList.size() - 1; i >= 0; --i)
1983 MIB.addOperand(RegList[i]);
1984
1985 return true;
1986}
1987
Evan Cheng7a37b1a2009-08-27 01:23:50 +00001988bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1989 unsigned FrameReg, int &Offset,
1990 const ARMBaseInstrInfo &TII) {
Evan Cheng780748d2009-07-28 05:48:47 +00001991 unsigned Opcode = MI.getOpcode();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001992 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng780748d2009-07-28 05:48:47 +00001993 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1994 bool isSub = false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001995
Evan Cheng780748d2009-07-28 05:48:47 +00001996 // Memory operands in inline assembly always use AddrMode2.
1997 if (Opcode == ARM::INLINEASM)
1998 AddrMode = ARMII::AddrMode2;
Jim Grosbachf24f9d92009-08-11 15:33:49 +00001999
Evan Cheng780748d2009-07-28 05:48:47 +00002000 if (Opcode == ARM::ADDri) {
2001 Offset += MI.getOperand(FrameRegIdx+1).getImm();
2002 if (Offset == 0) {
2003 // Turn it into a move.
2004 MI.setDesc(TII.get(ARM::MOVr));
2005 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2006 MI.RemoveOperand(FrameRegIdx+1);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002007 Offset = 0;
2008 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002009 } else if (Offset < 0) {
2010 Offset = -Offset;
2011 isSub = true;
2012 MI.setDesc(TII.get(ARM::SUBri));
2013 }
2014
2015 // Common case: small offset, fits into instruction.
2016 if (ARM_AM::getSOImmVal(Offset) != -1) {
2017 // Replace the FrameIndex with sp / fp
2018 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
2019 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002020 Offset = 0;
2021 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002022 }
2023
2024 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
2025 // as possible.
2026 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
2027 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
2028
2029 // We will handle these bits from offset, clear them.
2030 Offset &= ~ThisImmVal;
2031
2032 // Get the properly encoded SOImmVal field.
2033 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
2034 "Bit extraction didn't work?");
2035 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
2036 } else {
2037 unsigned ImmIdx = 0;
2038 int InstrOffs = 0;
2039 unsigned NumBits = 0;
2040 unsigned Scale = 1;
2041 switch (AddrMode) {
Jim Grosbach1e4d9a12010-10-26 22:37:02 +00002042 case ARMII::AddrMode_i12: {
2043 ImmIdx = FrameRegIdx + 1;
2044 InstrOffs = MI.getOperand(ImmIdx).getImm();
2045 NumBits = 12;
2046 break;
2047 }
Evan Cheng780748d2009-07-28 05:48:47 +00002048 case ARMII::AddrMode2: {
2049 ImmIdx = FrameRegIdx+2;
2050 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
2051 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2052 InstrOffs *= -1;
2053 NumBits = 12;
2054 break;
2055 }
2056 case ARMII::AddrMode3: {
2057 ImmIdx = FrameRegIdx+2;
2058 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
2059 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2060 InstrOffs *= -1;
2061 NumBits = 8;
2062 break;
2063 }
Anton Korobeynikov887d05c2009-08-08 13:35:48 +00002064 case ARMII::AddrMode4:
Jim Grosbach01c1cae2009-11-15 21:45:34 +00002065 case ARMII::AddrMode6:
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002066 // Can't fold any offset even if it's zero.
2067 return false;
Evan Cheng780748d2009-07-28 05:48:47 +00002068 case ARMII::AddrMode5: {
2069 ImmIdx = FrameRegIdx+1;
2070 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
2071 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
2072 InstrOffs *= -1;
2073 NumBits = 8;
2074 Scale = 4;
2075 break;
2076 }
2077 default:
2078 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng780748d2009-07-28 05:48:47 +00002079 }
2080
2081 Offset += InstrOffs * Scale;
2082 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
2083 if (Offset < 0) {
2084 Offset = -Offset;
2085 isSub = true;
2086 }
2087
2088 // Attempt to fold address comp. if opcode has offset bits
2089 if (NumBits > 0) {
2090 // Common case: small offset, fits into instruction.
2091 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
2092 int ImmedOffset = Offset / Scale;
2093 unsigned Mask = (1 << NumBits) - 1;
2094 if ((unsigned)Offset <= Mask * Scale) {
2095 // Replace the FrameIndex with sp
2096 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach9d2d1f02010-10-27 01:19:41 +00002097 // FIXME: When addrmode2 goes away, this will simplify (like the
2098 // T2 version), as the LDR.i12 versions don't need the encoding
2099 // tricks for the offset value.
2100 if (isSub) {
2101 if (AddrMode == ARMII::AddrMode_i12)
2102 ImmedOffset = -ImmedOffset;
2103 else
2104 ImmedOffset |= 1 << NumBits;
2105 }
Evan Cheng780748d2009-07-28 05:48:47 +00002106 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002107 Offset = 0;
2108 return true;
Evan Cheng780748d2009-07-28 05:48:47 +00002109 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +00002110
Evan Cheng780748d2009-07-28 05:48:47 +00002111 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
2112 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach8bf14832010-10-27 16:50:31 +00002113 if (isSub) {
2114 if (AddrMode == ARMII::AddrMode_i12)
2115 ImmedOffset = -ImmedOffset;
2116 else
2117 ImmedOffset |= 1 << NumBits;
2118 }
Evan Cheng780748d2009-07-28 05:48:47 +00002119 ImmOp.ChangeToImmediate(ImmedOffset);
2120 Offset &= ~(Mask*Scale);
2121 }
2122 }
2123
Evan Cheng7a37b1a2009-08-27 01:23:50 +00002124 Offset = (isSub) ? -Offset : Offset;
2125 return Offset == 0;
Evan Cheng780748d2009-07-28 05:48:47 +00002126}
Bill Wendling7de9d522010-08-06 01:32:48 +00002127
Manman Ren6fa76dc2012-06-29 21:33:59 +00002128/// analyzeCompare - For a comparison instruction, return the source registers
2129/// in SrcReg and SrcReg2 if having two register operands, and the value it
2130/// compares against in CmpValue. Return true if the comparison instruction
2131/// can be analyzed.
Bill Wendling7de9d522010-08-06 01:32:48 +00002132bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002133analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
2134 int &CmpMask, int &CmpValue) const {
Bill Wendling7de9d522010-08-06 01:32:48 +00002135 switch (MI->getOpcode()) {
2136 default: break;
Bill Wendling79553ba2010-08-11 00:23:00 +00002137 case ARM::CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002138 case ARM::t2CMPri:
Bill Wendling7de9d522010-08-06 01:32:48 +00002139 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002140 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002141 CmpMask = ~0;
Bill Wendling7de9d522010-08-06 01:32:48 +00002142 CmpValue = MI->getOperand(1).getImm();
2143 return true;
Manman Rendc8ad002012-05-11 01:30:47 +00002144 case ARM::CMPrr:
2145 case ARM::t2CMPrr:
2146 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002147 SrcReg2 = MI->getOperand(1).getReg();
Manman Rendc8ad002012-05-11 01:30:47 +00002148 CmpMask = ~0;
2149 CmpValue = 0;
2150 return true;
Gabor Greifadbbb932010-09-21 12:01:15 +00002151 case ARM::TSTri:
2152 case ARM::t2TSTri:
2153 SrcReg = MI->getOperand(0).getReg();
Manman Ren6fa76dc2012-06-29 21:33:59 +00002154 SrcReg2 = 0;
Gabor Greifadbbb932010-09-21 12:01:15 +00002155 CmpMask = MI->getOperand(1).getImm();
2156 CmpValue = 0;
2157 return true;
2158 }
2159
2160 return false;
2161}
2162
Gabor Greifd36e3e82010-09-29 10:12:08 +00002163/// isSuitableForMask - Identify a suitable 'and' instruction that
2164/// operates on the given source register and applies the same mask
2165/// as a 'tst' instruction. Provide a limited look-through for copies.
2166/// When successful, MI will hold the found instruction.
2167static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif1a25ae82010-09-21 13:30:57 +00002168 int CmpMask, bool CommonUse) {
Gabor Greifd36e3e82010-09-29 10:12:08 +00002169 switch (MI->getOpcode()) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002170 case ARM::ANDri:
2171 case ARM::t2ANDri:
Gabor Greifd36e3e82010-09-29 10:12:08 +00002172 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif1a25ae82010-09-21 13:30:57 +00002173 return false;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002174 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greifadbbb932010-09-21 12:01:15 +00002175 return true;
2176 break;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002177 case ARM::COPY: {
2178 // Walk down one instruction which is potentially an 'and'.
2179 const MachineInstr &Copy = *MI;
Michael J. Spencer70ac5fa2010-10-05 06:00:43 +00002180 MachineBasicBlock::iterator AND(
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002181 std::next(MachineBasicBlock::iterator(MI)));
Gabor Greifd36e3e82010-09-29 10:12:08 +00002182 if (AND == MI->getParent()->end()) return false;
2183 MI = AND;
2184 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
2185 CmpMask, true);
2186 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002187 }
2188
2189 return false;
2190}
2191
Manman Renb1b3db62012-06-29 22:06:19 +00002192/// getSwappedCondition - assume the flags are set by MI(a,b), return
2193/// the condition code if we modify the instructions such that flags are
2194/// set by MI(b,a).
2195inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2196 switch (CC) {
2197 default: return ARMCC::AL;
2198 case ARMCC::EQ: return ARMCC::EQ;
2199 case ARMCC::NE: return ARMCC::NE;
2200 case ARMCC::HS: return ARMCC::LS;
2201 case ARMCC::LO: return ARMCC::HI;
2202 case ARMCC::HI: return ARMCC::LO;
2203 case ARMCC::LS: return ARMCC::HS;
2204 case ARMCC::GE: return ARMCC::LE;
2205 case ARMCC::LT: return ARMCC::GT;
2206 case ARMCC::GT: return ARMCC::LT;
2207 case ARMCC::LE: return ARMCC::GE;
2208 }
2209}
2210
2211/// isRedundantFlagInstr - check whether the first instruction, whose only
2212/// purpose is to update flags, can be made redundant.
2213/// CMPrr can be made redundant by SUBrr if the operands are the same.
2214/// CMPri can be made redundant by SUBri if the operands are the same.
2215/// This function can be extended later on.
2216inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2217 unsigned SrcReg2, int ImmValue,
2218 MachineInstr *OI) {
2219 if ((CmpI->getOpcode() == ARM::CMPrr ||
2220 CmpI->getOpcode() == ARM::t2CMPrr) &&
2221 (OI->getOpcode() == ARM::SUBrr ||
2222 OI->getOpcode() == ARM::t2SUBrr) &&
2223 ((OI->getOperand(1).getReg() == SrcReg &&
2224 OI->getOperand(2).getReg() == SrcReg2) ||
2225 (OI->getOperand(1).getReg() == SrcReg2 &&
2226 OI->getOperand(2).getReg() == SrcReg)))
2227 return true;
2228
2229 if ((CmpI->getOpcode() == ARM::CMPri ||
2230 CmpI->getOpcode() == ARM::t2CMPri) &&
2231 (OI->getOpcode() == ARM::SUBri ||
2232 OI->getOpcode() == ARM::t2SUBri) &&
2233 OI->getOperand(1).getReg() == SrcReg &&
2234 OI->getOperand(2).getImm() == ImmValue)
2235 return true;
2236 return false;
2237}
2238
Manman Ren6fa76dc2012-06-29 21:33:59 +00002239/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2240/// comparison into one that sets the zero bit in the flags register;
2241/// Remove a redundant Compare instruction if an earlier instruction can set the
2242/// flags in the same way as Compare.
2243/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2244/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2245/// condition code of instructions which use the flags.
Bill Wendling7de9d522010-08-06 01:32:48 +00002246bool ARMBaseInstrInfo::
Manman Ren6fa76dc2012-06-29 21:33:59 +00002247optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2248 int CmpMask, int CmpValue,
2249 const MachineRegisterInfo *MRI) const {
Manman Renb1b3db62012-06-29 22:06:19 +00002250 // Get the unique definition of SrcReg.
2251 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2252 if (!MI) return false;
Bill Wendling04123002010-09-10 23:34:19 +00002253
Gabor Greifadbbb932010-09-21 12:01:15 +00002254 // Masked compares sometimes use the same register as the corresponding 'and'.
2255 if (CmpMask != ~0) {
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002256 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002257 MI = 0;
Owen Anderson16c6bf42014-03-13 23:12:04 +00002258 for (MachineRegisterInfo::use_instr_iterator
2259 UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
2260 UI != UE; ++UI) {
Gabor Greifadbbb932010-09-21 12:01:15 +00002261 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002262 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002263 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2264 isPredicated(PotentialAND))
Gabor Greifadbbb932010-09-21 12:01:15 +00002265 continue;
Gabor Greifd36e3e82010-09-29 10:12:08 +00002266 MI = PotentialAND;
Gabor Greifadbbb932010-09-21 12:01:15 +00002267 break;
2268 }
2269 if (!MI) return false;
2270 }
2271 }
2272
Manman Rendc8ad002012-05-11 01:30:47 +00002273 // Get ready to iterate backward from CmpInstr.
2274 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2275 B = CmpInstr->getParent()->begin();
Bill Wendling59ebe442010-10-09 00:03:48 +00002276
2277 // Early exit if CmpInstr is at the beginning of the BB.
2278 if (I == B) return false;
2279
Manman Rendc8ad002012-05-11 01:30:47 +00002280 // There are two possible candidates which can be changed to set CPSR:
2281 // One is MI, the other is a SUB instruction.
2282 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2283 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
2284 MachineInstr *Sub = NULL;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002285 if (SrcReg2 != 0)
Manman Rendc8ad002012-05-11 01:30:47 +00002286 // MI is not a candidate for CMPrr.
2287 MI = NULL;
Manman Ren6fa76dc2012-06-29 21:33:59 +00002288 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Rendc8ad002012-05-11 01:30:47 +00002289 // Conservatively refuse to convert an instruction which isn't in the same
2290 // BB as the comparison.
2291 // For CMPri, we need to check Sub, thus we can't return here.
Manman Ren0d5ec282012-05-11 15:36:46 +00002292 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Rendc8ad002012-05-11 01:30:47 +00002293 CmpInstr->getOpcode() == ARM::t2CMPri)
2294 MI = NULL;
2295 else
2296 return false;
2297 }
2298
2299 // Check that CPSR isn't set between the comparison instruction and the one we
2300 // want to change. At the same time, search for Sub.
Manman Renb1b3db62012-06-29 22:06:19 +00002301 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendling7de9d522010-08-06 01:32:48 +00002302 --I;
2303 for (; I != E; --I) {
2304 const MachineInstr &Instr = *I;
2305
Manman Renb1b3db62012-06-29 22:06:19 +00002306 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2307 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendlingc6627ee2010-11-01 20:41:43 +00002308 // This instruction modifies or uses CPSR after the one we want to
2309 // change. We can't do this transformation.
Manman Renb1b3db62012-06-29 22:06:19 +00002310 return false;
Evan Chengd757c882010-09-21 23:49:07 +00002311
Manman Renb1b3db62012-06-29 22:06:19 +00002312 // Check whether CmpInstr can be made redundant by the current instruction.
2313 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Rendc8ad002012-05-11 01:30:47 +00002314 Sub = &*I;
2315 break;
2316 }
2317
Evan Chengd757c882010-09-21 23:49:07 +00002318 if (I == B)
2319 // The 'and' is below the comparison instruction.
2320 return false;
Bill Wendling7de9d522010-08-06 01:32:48 +00002321 }
2322
Manman Rendc8ad002012-05-11 01:30:47 +00002323 // Return false if no candidates exist.
2324 if (!MI && !Sub)
2325 return false;
2326
2327 // The single candidate is called MI.
2328 if (!MI) MI = Sub;
2329
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002330 // We can't use a predicated instruction - it doesn't always write the flags.
2331 if (isPredicated(MI))
2332 return false;
2333
Bill Wendling7de9d522010-08-06 01:32:48 +00002334 switch (MI->getOpcode()) {
2335 default: break;
Cameron Zwarich93eae152011-04-15 20:28:28 +00002336 case ARM::RSBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002337 case ARM::RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002338 case ARM::RSCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002339 case ARM::RSCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002340 case ARM::ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002341 case ARM::ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002342 case ARM::ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002343 case ARM::ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002344 case ARM::SUBrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002345 case ARM::SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002346 case ARM::SBCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002347 case ARM::SBCri:
2348 case ARM::t2RSBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002349 case ARM::t2ADDrr:
Bill Wendling79553ba2010-08-11 00:23:00 +00002350 case ARM::t2ADDri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002351 case ARM::t2ADCrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002352 case ARM::t2ADCri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002353 case ARM::t2SUBrr:
Owen Andersonbdff1c92011-04-06 23:35:59 +00002354 case ARM::t2SUBri:
Cameron Zwarich93eae152011-04-15 20:28:28 +00002355 case ARM::t2SBCrr:
Cameron Zwarich0829b302011-04-15 20:45:00 +00002356 case ARM::t2SBCri:
2357 case ARM::ANDrr:
2358 case ARM::ANDri:
2359 case ARM::t2ANDrr:
Cameron Zwarich9c65e4d2011-04-15 21:24:38 +00002360 case ARM::t2ANDri:
2361 case ARM::ORRrr:
2362 case ARM::ORRri:
2363 case ARM::t2ORRrr:
2364 case ARM::t2ORRri:
2365 case ARM::EORrr:
2366 case ARM::EORri:
2367 case ARM::t2EORrr:
2368 case ARM::t2EORri: {
Manman Rendc8ad002012-05-11 01:30:47 +00002369 // Scan forward for the use of CPSR
2370 // When checking against MI: if it's a conditional code requires
Manman Ren34cb93e2012-07-11 22:51:44 +00002371 // checking of V bit, then this is not safe to do.
2372 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2373 // If we are done with the basic block, we need to check whether CPSR is
2374 // live-out.
Manman Renb1b3db62012-06-29 22:06:19 +00002375 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2376 OperandsToUpdate;
Evan Cheng425489d2011-03-23 22:52:04 +00002377 bool isSafe = false;
2378 I = CmpInstr;
Manman Rendc8ad002012-05-11 01:30:47 +00002379 E = CmpInstr->getParent()->end();
Evan Cheng425489d2011-03-23 22:52:04 +00002380 while (!isSafe && ++I != E) {
2381 const MachineInstr &Instr = *I;
2382 for (unsigned IO = 0, EO = Instr.getNumOperands();
2383 !isSafe && IO != EO; ++IO) {
2384 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen4fad5b22012-02-17 19:23:15 +00002385 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2386 isSafe = true;
2387 break;
2388 }
Evan Cheng425489d2011-03-23 22:52:04 +00002389 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2390 continue;
2391 if (MO.isDef()) {
2392 isSafe = true;
2393 break;
2394 }
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002395 // Condition code is after the operand before CPSR except for VSELs.
2396 ARMCC::CondCodes CC;
2397 bool IsInstrVSel = true;
2398 switch (Instr.getOpcode()) {
2399 default:
2400 IsInstrVSel = false;
2401 CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
2402 break;
2403 case ARM::VSELEQD:
2404 case ARM::VSELEQS:
2405 CC = ARMCC::EQ;
2406 break;
2407 case ARM::VSELGTD:
2408 case ARM::VSELGTS:
2409 CC = ARMCC::GT;
2410 break;
2411 case ARM::VSELGED:
2412 case ARM::VSELGES:
2413 CC = ARMCC::GE;
2414 break;
2415 case ARM::VSELVSS:
2416 case ARM::VSELVSD:
2417 CC = ARMCC::VS;
2418 break;
2419 }
2420
Manman Renb1b3db62012-06-29 22:06:19 +00002421 if (Sub) {
2422 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2423 if (NewCC == ARMCC::AL)
Manman Rendc8ad002012-05-11 01:30:47 +00002424 return false;
Manman Renb1b3db62012-06-29 22:06:19 +00002425 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2426 // on CMP needs to be updated to be based on SUB.
2427 // Push the condition code operands to OperandsToUpdate.
2428 // If it is safe to remove CmpInstr, the condition code of these
2429 // operands will be modified.
2430 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
Weiming Zhao43d8e6c2013-12-06 17:56:48 +00002431 Sub->getOperand(2).getReg() == SrcReg) {
2432 // VSel doesn't support condition code update.
2433 if (IsInstrVSel)
2434 return false;
2435 OperandsToUpdate.push_back(
2436 std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
2437 }
2438 } else
Manman Rendc8ad002012-05-11 01:30:47 +00002439 switch (CC) {
2440 default:
Manman Ren88a0d332012-07-11 23:47:00 +00002441 // CPSR can be used multiple times, we should continue.
Manman Rendc8ad002012-05-11 01:30:47 +00002442 break;
2443 case ARMCC::VS:
2444 case ARMCC::VC:
2445 case ARMCC::GE:
2446 case ARMCC::LT:
2447 case ARMCC::GT:
2448 case ARMCC::LE:
2449 return false;
2450 }
Evan Cheng425489d2011-03-23 22:52:04 +00002451 }
2452 }
2453
Manman Ren34cb93e2012-07-11 22:51:44 +00002454 // If CPSR is not killed nor re-defined, we should check whether it is
2455 // live-out. If it is live-out, do not optimize.
2456 if (!isSafe) {
2457 MachineBasicBlock *MBB = CmpInstr->getParent();
2458 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2459 SE = MBB->succ_end(); SI != SE; ++SI)
2460 if ((*SI)->isLiveIn(ARM::CPSR))
2461 return false;
2462 }
Evan Cheng425489d2011-03-23 22:52:04 +00002463
Evan Cheng65536472010-11-17 08:06:50 +00002464 // Toggle the optional operand to CPSR.
2465 MI->getOperand(5).setReg(ARM::CPSR);
2466 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen8b9dce52012-09-10 19:17:25 +00002467 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendling7de9d522010-08-06 01:32:48 +00002468 CmpInstr->eraseFromParent();
Manman Rendc8ad002012-05-11 01:30:47 +00002469
2470 // Modify the condition code of operands in OperandsToUpdate.
2471 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2472 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Renb1b3db62012-06-29 22:06:19 +00002473 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2474 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendling7de9d522010-08-06 01:32:48 +00002475 return true;
2476 }
Cameron Zwarich0829b302011-04-15 20:45:00 +00002477 }
Bill Wendling7de9d522010-08-06 01:32:48 +00002478
2479 return false;
2480}
Evan Cheng367a5df2010-09-09 18:18:55 +00002481
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002482bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2483 MachineInstr *DefMI, unsigned Reg,
2484 MachineRegisterInfo *MRI) const {
2485 // Fold large immediates into add, sub, or, xor.
2486 unsigned DefOpc = DefMI->getOpcode();
2487 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2488 return false;
2489 if (!DefMI->getOperand(1).isImm())
2490 // Could be t2MOVi32imm <ga:xx>
2491 return false;
2492
2493 if (!MRI->hasOneNonDBGUse(Reg))
2494 return false;
2495
Evan Chenga2b48d92012-03-26 23:31:00 +00002496 const MCInstrDesc &DefMCID = DefMI->getDesc();
2497 if (DefMCID.hasOptionalDef()) {
2498 unsigned NumOps = DefMCID.getNumOperands();
2499 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2500 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2501 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2502 // to delete DefMI.
2503 return false;
2504 }
2505
2506 const MCInstrDesc &UseMCID = UseMI->getDesc();
2507 if (UseMCID.hasOptionalDef()) {
2508 unsigned NumOps = UseMCID.getNumOperands();
2509 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2510 // If the instruction sets the flag, do not attempt this optimization
2511 // since it may change the semantics of the code.
2512 return false;
2513 }
2514
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002515 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002516 unsigned NewUseOpc = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002517 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng2d4e42f2010-11-18 01:43:23 +00002518 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002519 bool Commute = false;
2520 switch (UseOpc) {
2521 default: return false;
2522 case ARM::SUBrr:
2523 case ARM::ADDrr:
2524 case ARM::ORRrr:
2525 case ARM::EORrr:
2526 case ARM::t2SUBrr:
2527 case ARM::t2ADDrr:
2528 case ARM::t2ORRrr:
2529 case ARM::t2EORrr: {
2530 Commute = UseMI->getOperand(2).getReg() != Reg;
2531 switch (UseOpc) {
2532 default: break;
2533 case ARM::SUBrr: {
2534 if (Commute)
2535 return false;
2536 ImmVal = -ImmVal;
2537 NewUseOpc = ARM::SUBri;
2538 // Fallthrough
2539 }
2540 case ARM::ADDrr:
2541 case ARM::ORRrr:
2542 case ARM::EORrr: {
2543 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2544 return false;
2545 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2546 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2547 switch (UseOpc) {
2548 default: break;
2549 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2550 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2551 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2552 }
2553 break;
2554 }
2555 case ARM::t2SUBrr: {
2556 if (Commute)
2557 return false;
2558 ImmVal = -ImmVal;
2559 NewUseOpc = ARM::t2SUBri;
2560 // Fallthrough
2561 }
2562 case ARM::t2ADDrr:
2563 case ARM::t2ORRrr:
2564 case ARM::t2EORrr: {
2565 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2566 return false;
2567 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2568 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2569 switch (UseOpc) {
2570 default: break;
2571 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2572 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2573 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2574 }
2575 break;
2576 }
2577 }
2578 }
2579 }
2580
2581 unsigned OpIdx = Commute ? 2 : 1;
2582 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2583 bool isKill = UseMI->getOperand(OpIdx).isKill();
2584 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2585 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Cheng7fae11b2011-12-14 02:11:42 +00002586 UseMI, UseMI->getDebugLoc(),
Evan Cheng7f8ab6e2010-11-17 20:13:28 +00002587 get(NewUseOpc), NewReg)
2588 .addReg(Reg1, getKillRegState(isKill))
2589 .addImm(SOImmValV1)));
2590 UseMI->setDesc(get(NewUseOpc));
2591 UseMI->getOperand(1).setReg(NewReg);
2592 UseMI->getOperand(1).setIsKill();
2593 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2594 DefMI->eraseFromParent();
2595 return true;
2596}
2597
Bob Wilsone8a549c2012-09-29 21:43:49 +00002598static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2599 const MachineInstr *MI) {
2600 switch (MI->getOpcode()) {
2601 default: {
2602 const MCInstrDesc &Desc = MI->getDesc();
2603 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2604 assert(UOps >= 0 && "bad # UOps");
2605 return UOps;
2606 }
2607
2608 case ARM::LDRrs:
2609 case ARM::LDRBrs:
2610 case ARM::STRrs:
2611 case ARM::STRBrs: {
2612 unsigned ShOpVal = MI->getOperand(3).getImm();
2613 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2614 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2615 if (!isSub &&
2616 (ShImm == 0 ||
2617 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2618 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2619 return 1;
2620 return 2;
2621 }
2622
2623 case ARM::LDRH:
2624 case ARM::STRH: {
2625 if (!MI->getOperand(2).getReg())
2626 return 1;
2627
2628 unsigned ShOpVal = MI->getOperand(3).getImm();
2629 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2630 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2631 if (!isSub &&
2632 (ShImm == 0 ||
2633 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2634 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2635 return 1;
2636 return 2;
2637 }
2638
2639 case ARM::LDRSB:
2640 case ARM::LDRSH:
2641 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2642
2643 case ARM::LDRSB_POST:
2644 case ARM::LDRSH_POST: {
2645 unsigned Rt = MI->getOperand(0).getReg();
2646 unsigned Rm = MI->getOperand(3).getReg();
2647 return (Rt == Rm) ? 4 : 3;
2648 }
2649
2650 case ARM::LDR_PRE_REG:
2651 case ARM::LDRB_PRE_REG: {
2652 unsigned Rt = MI->getOperand(0).getReg();
2653 unsigned Rm = MI->getOperand(3).getReg();
2654 if (Rt == Rm)
2655 return 3;
2656 unsigned ShOpVal = MI->getOperand(4).getImm();
2657 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2658 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2659 if (!isSub &&
2660 (ShImm == 0 ||
2661 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2662 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2663 return 2;
2664 return 3;
2665 }
2666
2667 case ARM::STR_PRE_REG:
2668 case ARM::STRB_PRE_REG: {
2669 unsigned ShOpVal = MI->getOperand(4).getImm();
2670 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2671 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2672 if (!isSub &&
2673 (ShImm == 0 ||
2674 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2675 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2676 return 2;
2677 return 3;
2678 }
2679
2680 case ARM::LDRH_PRE:
2681 case ARM::STRH_PRE: {
2682 unsigned Rt = MI->getOperand(0).getReg();
2683 unsigned Rm = MI->getOperand(3).getReg();
2684 if (!Rm)
2685 return 2;
2686 if (Rt == Rm)
2687 return 3;
2688 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2689 ? 3 : 2;
2690 }
2691
2692 case ARM::LDR_POST_REG:
2693 case ARM::LDRB_POST_REG:
2694 case ARM::LDRH_POST: {
2695 unsigned Rt = MI->getOperand(0).getReg();
2696 unsigned Rm = MI->getOperand(3).getReg();
2697 return (Rt == Rm) ? 3 : 2;
2698 }
2699
2700 case ARM::LDR_PRE_IMM:
2701 case ARM::LDRB_PRE_IMM:
2702 case ARM::LDR_POST_IMM:
2703 case ARM::LDRB_POST_IMM:
2704 case ARM::STRB_POST_IMM:
2705 case ARM::STRB_POST_REG:
2706 case ARM::STRB_PRE_IMM:
2707 case ARM::STRH_POST:
2708 case ARM::STR_POST_IMM:
2709 case ARM::STR_POST_REG:
2710 case ARM::STR_PRE_IMM:
2711 return 2;
2712
2713 case ARM::LDRSB_PRE:
2714 case ARM::LDRSH_PRE: {
2715 unsigned Rm = MI->getOperand(3).getReg();
2716 if (Rm == 0)
2717 return 3;
2718 unsigned Rt = MI->getOperand(0).getReg();
2719 if (Rt == Rm)
2720 return 4;
2721 unsigned ShOpVal = MI->getOperand(4).getImm();
2722 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2723 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2724 if (!isSub &&
2725 (ShImm == 0 ||
2726 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2727 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2728 return 3;
2729 return 4;
2730 }
2731
2732 case ARM::LDRD: {
2733 unsigned Rt = MI->getOperand(0).getReg();
2734 unsigned Rn = MI->getOperand(2).getReg();
2735 unsigned Rm = MI->getOperand(3).getReg();
2736 if (Rm)
2737 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2738 return (Rt == Rn) ? 3 : 2;
2739 }
2740
2741 case ARM::STRD: {
2742 unsigned Rm = MI->getOperand(3).getReg();
2743 if (Rm)
2744 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2745 return 2;
2746 }
2747
2748 case ARM::LDRD_POST:
2749 case ARM::t2LDRD_POST:
2750 return 3;
2751
2752 case ARM::STRD_POST:
2753 case ARM::t2STRD_POST:
2754 return 4;
2755
2756 case ARM::LDRD_PRE: {
2757 unsigned Rt = MI->getOperand(0).getReg();
2758 unsigned Rn = MI->getOperand(3).getReg();
2759 unsigned Rm = MI->getOperand(4).getReg();
2760 if (Rm)
2761 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2762 return (Rt == Rn) ? 4 : 3;
2763 }
2764
2765 case ARM::t2LDRD_PRE: {
2766 unsigned Rt = MI->getOperand(0).getReg();
2767 unsigned Rn = MI->getOperand(3).getReg();
2768 return (Rt == Rn) ? 4 : 3;
2769 }
2770
2771 case ARM::STRD_PRE: {
2772 unsigned Rm = MI->getOperand(4).getReg();
2773 if (Rm)
2774 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2775 return 3;
2776 }
2777
2778 case ARM::t2STRD_PRE:
2779 return 3;
2780
2781 case ARM::t2LDR_POST:
2782 case ARM::t2LDRB_POST:
2783 case ARM::t2LDRB_PRE:
2784 case ARM::t2LDRSBi12:
2785 case ARM::t2LDRSBi8:
2786 case ARM::t2LDRSBpci:
2787 case ARM::t2LDRSBs:
2788 case ARM::t2LDRH_POST:
2789 case ARM::t2LDRH_PRE:
2790 case ARM::t2LDRSBT:
2791 case ARM::t2LDRSB_POST:
2792 case ARM::t2LDRSB_PRE:
2793 case ARM::t2LDRSH_POST:
2794 case ARM::t2LDRSH_PRE:
2795 case ARM::t2LDRSHi12:
2796 case ARM::t2LDRSHi8:
2797 case ARM::t2LDRSHpci:
2798 case ARM::t2LDRSHs:
2799 return 2;
2800
2801 case ARM::t2LDRDi8: {
2802 unsigned Rt = MI->getOperand(0).getReg();
2803 unsigned Rn = MI->getOperand(2).getReg();
2804 return (Rt == Rn) ? 3 : 2;
2805 }
2806
2807 case ARM::t2STRB_POST:
2808 case ARM::t2STRB_PRE:
2809 case ARM::t2STRBs:
2810 case ARM::t2STRDi8:
2811 case ARM::t2STRH_POST:
2812 case ARM::t2STRH_PRE:
2813 case ARM::t2STRHs:
2814 case ARM::t2STR_POST:
2815 case ARM::t2STR_PRE:
2816 case ARM::t2STRs:
2817 return 2;
2818 }
2819}
2820
Andrew Trick2ac6f7d2012-09-14 18:48:46 +00002821// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2822// can't be easily determined return 0 (missing MachineMemOperand).
2823//
2824// FIXME: The current MachineInstr design does not support relying on machine
2825// mem operands to determine the width of a memory access. Instead, we expect
2826// the target to provide this information based on the instruction opcode and
2827// operands. However, using MachineMemOperand is a the best solution now for
2828// two reasons:
2829//
2830// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2831// operands. This is much more dangerous than using the MachineMemOperand
2832// sizes because CodeGen passes can insert/remove optional machine operands. In
2833// fact, it's totally incorrect for preRA passes and appears to be wrong for
2834// postRA passes as well.
2835//
2836// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2837// machine model that calls this should handle the unknown (zero size) case.
2838//
2839// Long term, we should require a target hook that verifies MachineMemOperand
2840// sizes during MC lowering. That target hook should be local to MC lowering
2841// because we can't ensure that it is aware of other MI forms. Doing this will
2842// ensure that MachineMemOperands are correctly propagated through all passes.
2843unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2844 unsigned Size = 0;
2845 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2846 E = MI->memoperands_end(); I != E; ++I) {
2847 Size += (*I)->getSize();
2848 }
2849 return Size / 4;
2850}
2851
Evan Cheng367a5df2010-09-09 18:18:55 +00002852unsigned
Evan Chengdebf9c52010-11-03 00:45:17 +00002853ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2854 const MachineInstr *MI) const {
Evan Chengbf407072010-09-10 01:29:16 +00002855 if (!ItinData || ItinData->isEmpty())
Evan Cheng367a5df2010-09-09 18:18:55 +00002856 return 1;
2857
Evan Cheng6cc775f2011-06-28 19:10:37 +00002858 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng367a5df2010-09-09 18:18:55 +00002859 unsigned Class = Desc.getSchedClass();
Andrew Trickf161e392012-07-02 18:10:42 +00002860 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsone8a549c2012-09-29 21:43:49 +00002861 if (ItinUOps >= 0) {
2862 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2863 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2864
Andrew Trickf161e392012-07-02 18:10:42 +00002865 return ItinUOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002866 }
Evan Cheng367a5df2010-09-09 18:18:55 +00002867
2868 unsigned Opc = MI->getOpcode();
2869 switch (Opc) {
2870 default:
2871 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002872 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002873 case ARM::VSTMQIA:
Evan Cheng367a5df2010-09-09 18:18:55 +00002874 return 2;
2875
2876 // The number of uOps for load / store multiple are determined by the number
2877 // registers.
Andrew Trickc416ba62010-12-24 04:28:06 +00002878 //
Evan Chengbf407072010-09-10 01:29:16 +00002879 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2880 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledru35521e22012-07-23 08:51:15 +00002881 // separately by assuming the address is not 64-bit aligned.
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002882 //
Evan Chengbf407072010-09-10 01:29:16 +00002883 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002884 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
2885 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
2886 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002887 case ARM::VLDMDIA_UPD:
2888 case ARM::VLDMDDB_UPD:
2889 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002890 case ARM::VLDMSIA_UPD:
2891 case ARM::VLDMSDB_UPD:
2892 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002893 case ARM::VSTMDIA_UPD:
2894 case ARM::VSTMDDB_UPD:
2895 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002896 case ARM::VSTMSIA_UPD:
2897 case ARM::VSTMSDB_UPD: {
Evan Cheng367a5df2010-09-09 18:18:55 +00002898 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
2899 return (NumRegs / 2) + (NumRegs % 2) + 1;
2900 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002901
2902 case ARM::LDMIA_RET:
2903 case ARM::LDMIA:
2904 case ARM::LDMDA:
2905 case ARM::LDMDB:
2906 case ARM::LDMIB:
2907 case ARM::LDMIA_UPD:
2908 case ARM::LDMDA_UPD:
2909 case ARM::LDMDB_UPD:
2910 case ARM::LDMIB_UPD:
2911 case ARM::STMIA:
2912 case ARM::STMDA:
2913 case ARM::STMDB:
2914 case ARM::STMIB:
2915 case ARM::STMIA_UPD:
2916 case ARM::STMDA_UPD:
2917 case ARM::STMDB_UPD:
2918 case ARM::STMIB_UPD:
2919 case ARM::tLDMIA:
2920 case ARM::tLDMIA_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002921 case ARM::tSTMIA_UPD:
Evan Cheng367a5df2010-09-09 18:18:55 +00002922 case ARM::tPOP_RET:
2923 case ARM::tPOP:
2924 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00002925 case ARM::t2LDMIA_RET:
2926 case ARM::t2LDMIA:
2927 case ARM::t2LDMDB:
2928 case ARM::t2LDMIA_UPD:
2929 case ARM::t2LDMDB_UPD:
2930 case ARM::t2STMIA:
2931 case ARM::t2STMDB:
2932 case ARM::t2STMIA_UPD:
2933 case ARM::t2STMDB_UPD: {
Evan Chengbf407072010-09-10 01:29:16 +00002934 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002935 if (Subtarget.isSwift()) {
Bob Wilsone8a549c2012-09-29 21:43:49 +00002936 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
2937 switch (Opc) {
2938 default: break;
2939 case ARM::VLDMDIA_UPD:
2940 case ARM::VLDMDDB_UPD:
2941 case ARM::VLDMSIA_UPD:
2942 case ARM::VLDMSDB_UPD:
2943 case ARM::VSTMDIA_UPD:
2944 case ARM::VSTMDDB_UPD:
2945 case ARM::VSTMSIA_UPD:
2946 case ARM::VSTMSDB_UPD:
2947 case ARM::LDMIA_UPD:
2948 case ARM::LDMDA_UPD:
2949 case ARM::LDMDB_UPD:
2950 case ARM::LDMIB_UPD:
2951 case ARM::STMIA_UPD:
2952 case ARM::STMDA_UPD:
2953 case ARM::STMDB_UPD:
2954 case ARM::STMIB_UPD:
2955 case ARM::tLDMIA_UPD:
2956 case ARM::tSTMIA_UPD:
2957 case ARM::t2LDMIA_UPD:
2958 case ARM::t2LDMDB_UPD:
2959 case ARM::t2STMIA_UPD:
2960 case ARM::t2STMDB_UPD:
2961 ++UOps; // One for base register writeback.
2962 break;
2963 case ARM::LDMIA_RET:
2964 case ARM::tPOP_RET:
2965 case ARM::t2LDMIA_RET:
2966 UOps += 2; // One for base reg wb, one for write to pc.
2967 break;
2968 }
2969 return UOps;
2970 } else if (Subtarget.isCortexA8()) {
Evan Chengdebf9c52010-11-03 00:45:17 +00002971 if (NumRegs < 4)
2972 return 2;
2973 // 4 registers would be issued: 2, 2.
2974 // 5 registers would be issued: 2, 2, 1.
Andrew Trickf161e392012-07-02 18:10:42 +00002975 int A8UOps = (NumRegs / 2);
Evan Chengdebf9c52010-11-03 00:45:17 +00002976 if (NumRegs % 2)
Andrew Trickf161e392012-07-02 18:10:42 +00002977 ++A8UOps;
2978 return A8UOps;
Bob Wilsone8a549c2012-09-29 21:43:49 +00002979 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trickf161e392012-07-02 18:10:42 +00002980 int A9UOps = (NumRegs / 2);
Evan Chengbf407072010-09-10 01:29:16 +00002981 // If there are odd number of registers or if it's not 64-bit aligned,
2982 // then it takes an extra AGU (Address Generation Unit) cycle.
2983 if ((NumRegs % 2) ||
2984 !MI->hasOneMemOperand() ||
2985 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trickf161e392012-07-02 18:10:42 +00002986 ++A9UOps;
2987 return A9UOps;
Evan Chengbf407072010-09-10 01:29:16 +00002988 } else {
2989 // Assume the worst.
2990 return NumRegs;
Michael J. Spencere7f00cb2010-10-05 06:00:33 +00002991 }
Evan Cheng367a5df2010-09-09 18:18:55 +00002992 }
2993 }
2994}
Evan Cheng49d4c0b2010-10-06 06:27:31 +00002995
2996int
Evan Cheng412e37b2010-10-07 23:12:15 +00002997ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00002998 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00002999 unsigned DefClass,
3000 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003001 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003002 if (RegNo <= 0)
3003 // Def is the address writeback.
3004 return ItinData->getOperandCycle(DefClass, DefIdx);
3005
3006 int DefCycle;
3007 if (Subtarget.isCortexA8()) {
3008 // (regno / 2) + (regno % 2) + 1
3009 DefCycle = RegNo / 2 + 1;
3010 if (RegNo % 2)
3011 ++DefCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003012 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003013 DefCycle = RegNo;
3014 bool isSLoad = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003015
Evan Cheng6cc775f2011-06-28 19:10:37 +00003016 switch (DefMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003017 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003018 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003019 case ARM::VLDMSIA_UPD:
3020 case ARM::VLDMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003021 isSLoad = true;
3022 break;
3023 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003024
Evan Cheng412e37b2010-10-07 23:12:15 +00003025 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3026 // then it takes an extra cycle.
3027 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
3028 ++DefCycle;
3029 } else {
3030 // Assume the worst.
3031 DefCycle = RegNo + 2;
3032 }
3033
3034 return DefCycle;
3035}
3036
3037int
3038ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003039 const MCInstrDesc &DefMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003040 unsigned DefClass,
3041 unsigned DefIdx, unsigned DefAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003042 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003043 if (RegNo <= 0)
3044 // Def is the address writeback.
3045 return ItinData->getOperandCycle(DefClass, DefIdx);
3046
3047 int DefCycle;
3048 if (Subtarget.isCortexA8()) {
3049 // 4 registers would be issued: 1, 2, 1.
3050 // 5 registers would be issued: 1, 2, 2.
3051 DefCycle = RegNo / 2;
3052 if (DefCycle < 1)
3053 DefCycle = 1;
3054 // Result latency is issue cycle + 2: E2.
3055 DefCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003056 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003057 DefCycle = (RegNo / 2);
3058 // If there are odd number of registers or if it's not 64-bit aligned,
3059 // then it takes an extra AGU (Address Generation Unit) cycle.
3060 if ((RegNo % 2) || DefAlign < 8)
3061 ++DefCycle;
3062 // Result latency is AGU cycles + 2.
3063 DefCycle += 2;
3064 } else {
3065 // Assume the worst.
3066 DefCycle = RegNo + 2;
3067 }
3068
3069 return DefCycle;
3070}
3071
3072int
3073ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003074 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003075 unsigned UseClass,
3076 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003077 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003078 if (RegNo <= 0)
3079 return ItinData->getOperandCycle(UseClass, UseIdx);
3080
3081 int UseCycle;
3082 if (Subtarget.isCortexA8()) {
3083 // (regno / 2) + (regno % 2) + 1
3084 UseCycle = RegNo / 2 + 1;
3085 if (RegNo % 2)
3086 ++UseCycle;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003087 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003088 UseCycle = RegNo;
3089 bool isSStore = false;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003090
Evan Cheng6cc775f2011-06-28 19:10:37 +00003091 switch (UseMCID.getOpcode()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003092 default: break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003093 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003094 case ARM::VSTMSIA_UPD:
3095 case ARM::VSTMSDB_UPD:
Evan Cheng412e37b2010-10-07 23:12:15 +00003096 isSStore = true;
3097 break;
3098 }
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003099
Evan Cheng412e37b2010-10-07 23:12:15 +00003100 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
3101 // then it takes an extra cycle.
3102 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
3103 ++UseCycle;
3104 } else {
3105 // Assume the worst.
3106 UseCycle = RegNo + 2;
3107 }
3108
3109 return UseCycle;
3110}
3111
3112int
3113ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003114 const MCInstrDesc &UseMCID,
Evan Cheng412e37b2010-10-07 23:12:15 +00003115 unsigned UseClass,
3116 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003117 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng412e37b2010-10-07 23:12:15 +00003118 if (RegNo <= 0)
3119 return ItinData->getOperandCycle(UseClass, UseIdx);
3120
3121 int UseCycle;
3122 if (Subtarget.isCortexA8()) {
3123 UseCycle = RegNo / 2;
3124 if (UseCycle < 2)
3125 UseCycle = 2;
3126 // Read in E3.
3127 UseCycle += 2;
Bob Wilsone8a549c2012-09-29 21:43:49 +00003128 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng412e37b2010-10-07 23:12:15 +00003129 UseCycle = (RegNo / 2);
3130 // If there are odd number of registers or if it's not 64-bit aligned,
3131 // then it takes an extra AGU (Address Generation Unit) cycle.
3132 if ((RegNo % 2) || UseAlign < 8)
3133 ++UseCycle;
3134 } else {
3135 // Assume the worst.
3136 UseCycle = 1;
3137 }
3138 return UseCycle;
3139}
3140
3141int
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003142ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003143 const MCInstrDesc &DefMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003144 unsigned DefIdx, unsigned DefAlign,
Evan Cheng6cc775f2011-06-28 19:10:37 +00003145 const MCInstrDesc &UseMCID,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003146 unsigned UseIdx, unsigned UseAlign) const {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003147 unsigned DefClass = DefMCID.getSchedClass();
3148 unsigned UseClass = UseMCID.getSchedClass();
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003149
Evan Cheng6cc775f2011-06-28 19:10:37 +00003150 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003151 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
3152
3153 // This may be a def / use of a variable_ops instruction, the operand
3154 // latency might be determinable dynamically. Let the target try to
3155 // figure it out.
Evan Chenge2c211c2010-10-28 02:00:25 +00003156 int DefCycle = -1;
Evan Chengff310732010-10-28 06:47:08 +00003157 bool LdmBypass = false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003158 switch (DefMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003159 default:
3160 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3161 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003162
3163 case ARM::VLDMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003164 case ARM::VLDMDIA_UPD:
3165 case ARM::VLDMDDB_UPD:
3166 case ARM::VLDMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003167 case ARM::VLDMSIA_UPD:
3168 case ARM::VLDMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003169 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003170 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003171
3172 case ARM::LDMIA_RET:
3173 case ARM::LDMIA:
3174 case ARM::LDMDA:
3175 case ARM::LDMDB:
3176 case ARM::LDMIB:
3177 case ARM::LDMIA_UPD:
3178 case ARM::LDMDA_UPD:
3179 case ARM::LDMDB_UPD:
3180 case ARM::LDMIB_UPD:
3181 case ARM::tLDMIA:
3182 case ARM::tLDMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003183 case ARM::tPUSH:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003184 case ARM::t2LDMIA_RET:
3185 case ARM::t2LDMIA:
3186 case ARM::t2LDMDB:
3187 case ARM::t2LDMIA_UPD:
3188 case ARM::t2LDMDB_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003189 LdmBypass = 1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003190 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng412e37b2010-10-07 23:12:15 +00003191 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003192 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003193
3194 if (DefCycle == -1)
3195 // We can't seem to determine the result latency of the def, assume it's 2.
3196 DefCycle = 2;
3197
3198 int UseCycle = -1;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003199 switch (UseMCID.getOpcode()) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003200 default:
3201 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
3202 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003203
3204 case ARM::VSTMDIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003205 case ARM::VSTMDIA_UPD:
3206 case ARM::VSTMDDB_UPD:
3207 case ARM::VSTMSIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003208 case ARM::VSTMSIA_UPD:
3209 case ARM::VSTMSDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003210 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003211 break;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003212
3213 case ARM::STMIA:
3214 case ARM::STMDA:
3215 case ARM::STMDB:
3216 case ARM::STMIB:
3217 case ARM::STMIA_UPD:
3218 case ARM::STMDA_UPD:
3219 case ARM::STMDB_UPD:
3220 case ARM::STMIB_UPD:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003221 case ARM::tSTMIA_UPD:
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003222 case ARM::tPOP_RET:
3223 case ARM::tPOP:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003224 case ARM::t2STMIA:
3225 case ARM::t2STMDB:
3226 case ARM::t2STMIA_UPD:
3227 case ARM::t2STMDB_UPD:
Evan Cheng6cc775f2011-06-28 19:10:37 +00003228 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng1958cef2010-10-07 01:50:48 +00003229 break;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003230 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003231
3232 if (UseCycle == -1)
3233 // Assume it's read in the first stage.
3234 UseCycle = 1;
3235
3236 UseCycle = DefCycle - UseCycle + 1;
3237 if (UseCycle > 0) {
3238 if (LdmBypass) {
3239 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3240 // first def operand.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003241 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003242 UseClass, UseIdx))
3243 --UseCycle;
3244 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003245 UseClass, UseIdx)) {
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003246 --UseCycle;
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003247 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003248 }
3249
3250 return UseCycle;
3251}
3252
Evan Cheng7fae11b2011-12-14 02:11:42 +00003253static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003254 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003255 unsigned &DefIdx, unsigned &Dist) {
3256 Dist = 0;
3257
3258 MachineBasicBlock::const_iterator I = MI; ++I;
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00003259 MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
Evan Cheng7fae11b2011-12-14 02:11:42 +00003260 assert(II->isInsideBundle() && "Empty bundle?");
3261
3262 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003263 while (II->isInsideBundle()) {
3264 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3265 if (Idx != -1)
3266 break;
3267 --II;
3268 ++Dist;
3269 }
3270
3271 assert(Idx != -1 && "Cannot find bundled definition!");
3272 DefIdx = Idx;
3273 return II;
3274}
3275
3276static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Chengda103bf2011-12-14 20:00:08 +00003277 const MachineInstr *MI, unsigned Reg,
Evan Cheng7fae11b2011-12-14 02:11:42 +00003278 unsigned &UseIdx, unsigned &Dist) {
3279 Dist = 0;
3280
3281 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3282 assert(II->isInsideBundle() && "Empty bundle?");
3283 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3284
3285 // FIXME: This doesn't properly handle multiple uses.
3286 int Idx = -1;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003287 while (II != E && II->isInsideBundle()) {
3288 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3289 if (Idx != -1)
3290 break;
3291 if (II->getOpcode() != ARM::t2IT)
3292 ++Dist;
3293 ++II;
3294 }
3295
Evan Chengda103bf2011-12-14 20:00:08 +00003296 if (Idx == -1) {
3297 Dist = 0;
3298 return 0;
3299 }
3300
Evan Cheng7fae11b2011-12-14 02:11:42 +00003301 UseIdx = Idx;
3302 return II;
3303}
3304
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003305/// Return the number of cycles to add to (or subtract from) the static
3306/// itinerary based on the def opcode and alignment. The caller will ensure that
3307/// adjusted latency is at least one cycle.
3308static int adjustDefLatency(const ARMSubtarget &Subtarget,
3309 const MachineInstr *DefMI,
3310 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3311 int Adjust = 0;
Silviu Barangab47bb942012-09-13 15:05:10 +00003312 if (Subtarget.isCortexA8() || Subtarget.isLikeA9()) {
Evan Chengff310732010-10-28 06:47:08 +00003313 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3314 // variants are one cycle cheaper.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003315 switch (DefMCID->getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003316 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003317 case ARM::LDRrs:
3318 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003319 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3320 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3321 if (ShImm == 0 ||
3322 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003323 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003324 break;
3325 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003326 case ARM::t2LDRs:
3327 case ARM::t2LDRBs:
3328 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003329 case ARM::t2LDRSHs: {
3330 // Thumb2 mode: lsl only.
3331 unsigned ShAmt = DefMI->getOperand(3).getImm();
3332 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003333 --Adjust;
Evan Chengff310732010-10-28 06:47:08 +00003334 break;
3335 }
3336 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003337 } else if (Subtarget.isSwift()) {
3338 // FIXME: Properly handle all of the latency adjustments for address
3339 // writeback.
3340 switch (DefMCID->getOpcode()) {
3341 default: break;
3342 case ARM::LDRrs:
3343 case ARM::LDRBrs: {
3344 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3345 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3346 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3347 if (!isSub &&
3348 (ShImm == 0 ||
3349 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3350 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3351 Adjust -= 2;
3352 else if (!isSub &&
3353 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3354 --Adjust;
3355 break;
3356 }
3357 case ARM::t2LDRs:
3358 case ARM::t2LDRBs:
3359 case ARM::t2LDRHs:
3360 case ARM::t2LDRSHs: {
3361 // Thumb2 mode: lsl only.
3362 unsigned ShAmt = DefMI->getOperand(3).getImm();
3363 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3364 Adjust -= 2;
3365 break;
3366 }
3367 }
Evan Chengff310732010-10-28 06:47:08 +00003368 }
3369
Silviu Barangab47bb942012-09-13 15:05:10 +00003370 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Cheng7fae11b2011-12-14 02:11:42 +00003371 switch (DefMCID->getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003372 default: break;
3373 case ARM::VLD1q8:
3374 case ARM::VLD1q16:
3375 case ARM::VLD1q32:
3376 case ARM::VLD1q64:
Jim Grosbach2098cb12011-10-24 21:45:13 +00003377 case ARM::VLD1q8wb_fixed:
3378 case ARM::VLD1q16wb_fixed:
3379 case ARM::VLD1q32wb_fixed:
3380 case ARM::VLD1q64wb_fixed:
3381 case ARM::VLD1q8wb_register:
3382 case ARM::VLD1q16wb_register:
3383 case ARM::VLD1q32wb_register:
3384 case ARM::VLD1q64wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003385 case ARM::VLD2d8:
3386 case ARM::VLD2d16:
3387 case ARM::VLD2d32:
3388 case ARM::VLD2q8:
3389 case ARM::VLD2q16:
3390 case ARM::VLD2q32:
Jim Grosbachd146a022011-12-09 21:28:25 +00003391 case ARM::VLD2d8wb_fixed:
3392 case ARM::VLD2d16wb_fixed:
3393 case ARM::VLD2d32wb_fixed:
3394 case ARM::VLD2q8wb_fixed:
3395 case ARM::VLD2q16wb_fixed:
3396 case ARM::VLD2q32wb_fixed:
3397 case ARM::VLD2d8wb_register:
3398 case ARM::VLD2d16wb_register:
3399 case ARM::VLD2d32wb_register:
3400 case ARM::VLD2q8wb_register:
3401 case ARM::VLD2q16wb_register:
3402 case ARM::VLD2q32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003403 case ARM::VLD3d8:
3404 case ARM::VLD3d16:
3405 case ARM::VLD3d32:
3406 case ARM::VLD1d64T:
3407 case ARM::VLD3d8_UPD:
3408 case ARM::VLD3d16_UPD:
3409 case ARM::VLD3d32_UPD:
Jim Grosbach92fd05e2011-10-24 23:26:05 +00003410 case ARM::VLD1d64Twb_fixed:
3411 case ARM::VLD1d64Twb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003412 case ARM::VLD3q8_UPD:
3413 case ARM::VLD3q16_UPD:
3414 case ARM::VLD3q32_UPD:
3415 case ARM::VLD4d8:
3416 case ARM::VLD4d16:
3417 case ARM::VLD4d32:
3418 case ARM::VLD1d64Q:
3419 case ARM::VLD4d8_UPD:
3420 case ARM::VLD4d16_UPD:
3421 case ARM::VLD4d32_UPD:
Jim Grosbach17ec1a12011-10-25 00:14:01 +00003422 case ARM::VLD1d64Qwb_fixed:
3423 case ARM::VLD1d64Qwb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003424 case ARM::VLD4q8_UPD:
3425 case ARM::VLD4q16_UPD:
3426 case ARM::VLD4q32_UPD:
3427 case ARM::VLD1DUPq8:
3428 case ARM::VLD1DUPq16:
3429 case ARM::VLD1DUPq32:
Jim Grosbacha68c9a82011-11-30 19:35:44 +00003430 case ARM::VLD1DUPq8wb_fixed:
3431 case ARM::VLD1DUPq16wb_fixed:
3432 case ARM::VLD1DUPq32wb_fixed:
3433 case ARM::VLD1DUPq8wb_register:
3434 case ARM::VLD1DUPq16wb_register:
3435 case ARM::VLD1DUPq32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003436 case ARM::VLD2DUPd8:
3437 case ARM::VLD2DUPd16:
3438 case ARM::VLD2DUPd32:
Jim Grosbachc80a2642011-12-21 19:40:55 +00003439 case ARM::VLD2DUPd8wb_fixed:
3440 case ARM::VLD2DUPd16wb_fixed:
3441 case ARM::VLD2DUPd32wb_fixed:
3442 case ARM::VLD2DUPd8wb_register:
3443 case ARM::VLD2DUPd16wb_register:
3444 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003445 case ARM::VLD4DUPd8:
3446 case ARM::VLD4DUPd16:
3447 case ARM::VLD4DUPd32:
3448 case ARM::VLD4DUPd8_UPD:
3449 case ARM::VLD4DUPd16_UPD:
3450 case ARM::VLD4DUPd32_UPD:
3451 case ARM::VLD1LNd8:
3452 case ARM::VLD1LNd16:
3453 case ARM::VLD1LNd32:
3454 case ARM::VLD1LNd8_UPD:
3455 case ARM::VLD1LNd16_UPD:
3456 case ARM::VLD1LNd32_UPD:
3457 case ARM::VLD2LNd8:
3458 case ARM::VLD2LNd16:
3459 case ARM::VLD2LNd32:
3460 case ARM::VLD2LNq16:
3461 case ARM::VLD2LNq32:
3462 case ARM::VLD2LNd8_UPD:
3463 case ARM::VLD2LNd16_UPD:
3464 case ARM::VLD2LNd32_UPD:
3465 case ARM::VLD2LNq16_UPD:
3466 case ARM::VLD2LNq32_UPD:
3467 case ARM::VLD4LNd8:
3468 case ARM::VLD4LNd16:
3469 case ARM::VLD4LNd32:
3470 case ARM::VLD4LNq16:
3471 case ARM::VLD4LNq32:
3472 case ARM::VLD4LNd8_UPD:
3473 case ARM::VLD4LNd16_UPD:
3474 case ARM::VLD4LNd32_UPD:
3475 case ARM::VLD4LNq16_UPD:
3476 case ARM::VLD4LNq32_UPD:
3477 // If the address is not 64-bit aligned, the latencies of these
3478 // instructions increases by one.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003479 ++Adjust;
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003480 break;
3481 }
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003482 }
3483 return Adjust;
3484}
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003485
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003486
3487
3488int
3489ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3490 const MachineInstr *DefMI, unsigned DefIdx,
3491 const MachineInstr *UseMI,
3492 unsigned UseIdx) const {
3493 // No operand latency. The caller may fall back to getInstrLatency.
3494 if (!ItinData || ItinData->isEmpty())
3495 return -1;
3496
3497 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3498 unsigned Reg = DefMO.getReg();
3499 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3500 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3501
3502 unsigned DefAdj = 0;
3503 if (DefMI->isBundle()) {
3504 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3505 DefMCID = &DefMI->getDesc();
3506 }
3507 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3508 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3509 return 1;
3510 }
3511
3512 unsigned UseAdj = 0;
3513 if (UseMI->isBundle()) {
3514 unsigned NewUseIdx;
3515 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3516 Reg, NewUseIdx, UseAdj);
Andrew Trick77d0b882012-06-22 02:50:33 +00003517 if (!NewUseMI)
3518 return -1;
3519
3520 UseMI = NewUseMI;
3521 UseIdx = NewUseIdx;
3522 UseMCID = &UseMI->getDesc();
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003523 }
3524
3525 if (Reg == ARM::CPSR) {
3526 if (DefMI->getOpcode() == ARM::FMSTAT) {
3527 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Barangab47bb942012-09-13 15:05:10 +00003528 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003529 }
3530
3531 // CPSR set and branch can be paired in the same cycle.
3532 if (UseMI->isBranch())
3533 return 0;
3534
3535 // Otherwise it takes the instruction latency (generally one).
3536 unsigned Latency = getInstrLatency(ItinData, DefMI);
3537
3538 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3539 // its uses. Instructions which are otherwise scheduled between them may
3540 // incur a code size penalty (not able to use the CPSR setting 16-bit
3541 // instructions).
3542 if (Latency > 0 && Subtarget.isThumb2()) {
3543 const MachineFunction *MF = DefMI->getParent()->getParent();
Bill Wendling698e84f2012-12-30 10:32:01 +00003544 if (MF->getFunction()->getAttributes().
3545 hasAttribute(AttributeSet::FunctionIndex,
3546 Attribute::OptimizeForSize))
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003547 --Latency;
3548 }
3549 return Latency;
3550 }
3551
Andrew Trick77d0b882012-06-22 02:50:33 +00003552 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3553 return -1;
3554
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003555 unsigned DefAlign = DefMI->hasOneMemOperand()
3556 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3557 unsigned UseAlign = UseMI->hasOneMemOperand()
3558 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3559
3560 // Get the itinerary's latency if possible, and handle variable_ops.
3561 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3562 *UseMCID, UseIdx, UseAlign);
3563 // Unable to find operand latency. The caller may resort to getInstrLatency.
3564 if (Latency < 0)
3565 return Latency;
3566
3567 // Adjust for IT block position.
3568 int Adj = DefAdj + UseAdj;
3569
3570 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3571 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3572 if (Adj >= 0 || (int)Latency > -Adj) {
3573 return Latency + Adj;
3574 }
3575 // Return the itinerary latency, which may be zero but not less than zero.
Evan Chengff310732010-10-28 06:47:08 +00003576 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003577}
3578
3579int
3580ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3581 SDNode *DefNode, unsigned DefIdx,
3582 SDNode *UseNode, unsigned UseIdx) const {
3583 if (!DefNode->isMachineOpcode())
3584 return 1;
3585
Evan Cheng6cc775f2011-06-28 19:10:37 +00003586 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trick47ff14b2011-01-21 05:51:33 +00003587
Evan Cheng6cc775f2011-06-28 19:10:37 +00003588 if (isZeroCost(DefMCID.Opcode))
Andrew Trick47ff14b2011-01-21 05:51:33 +00003589 return 0;
3590
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003591 if (!ItinData || ItinData->isEmpty())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003592 return DefMCID.mayLoad() ? 3 : 1;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003593
Evan Cheng6c1414f2010-10-29 18:09:28 +00003594 if (!UseNode->isMachineOpcode()) {
Evan Cheng6cc775f2011-06-28 19:10:37 +00003595 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsone8a549c2012-09-29 21:43:49 +00003596 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng6c1414f2010-10-29 18:09:28 +00003597 return Latency <= 2 ? 1 : Latency - 1;
3598 else
3599 return Latency <= 3 ? 1 : Latency - 2;
3600 }
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003601
Evan Cheng6cc775f2011-06-28 19:10:37 +00003602 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003603 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3604 unsigned DefAlign = !DefMN->memoperands_empty()
3605 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3606 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3607 unsigned UseAlign = !UseMN->memoperands_empty()
3608 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng6cc775f2011-06-28 19:10:37 +00003609 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3610 UseMCID, UseIdx, UseAlign);
Evan Chengff310732010-10-28 06:47:08 +00003611
3612 if (Latency > 1 &&
Silviu Barangab47bb942012-09-13 15:05:10 +00003613 (Subtarget.isCortexA8() || Subtarget.isLikeA9())) {
Evan Chengff310732010-10-28 06:47:08 +00003614 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3615 // variants are one cycle cheaper.
Evan Cheng6cc775f2011-06-28 19:10:37 +00003616 switch (DefMCID.getOpcode()) {
Evan Chengff310732010-10-28 06:47:08 +00003617 default: break;
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003618 case ARM::LDRrs:
3619 case ARM::LDRBrs: {
Evan Chengff310732010-10-28 06:47:08 +00003620 unsigned ShOpVal =
3621 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3622 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3623 if (ShImm == 0 ||
3624 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3625 --Latency;
3626 break;
3627 }
Jakob Stoklund Olesenb3de7b12012-08-28 03:11:27 +00003628 case ARM::t2LDRs:
3629 case ARM::t2LDRBs:
3630 case ARM::t2LDRHs:
Evan Chengff310732010-10-28 06:47:08 +00003631 case ARM::t2LDRSHs: {
3632 // Thumb2 mode: lsl only.
3633 unsigned ShAmt =
3634 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3635 if (ShAmt == 0 || ShAmt == 2)
3636 --Latency;
3637 break;
3638 }
3639 }
Bob Wilsone8a549c2012-09-29 21:43:49 +00003640 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3641 // FIXME: Properly handle all of the latency adjustments for address
3642 // writeback.
3643 switch (DefMCID.getOpcode()) {
3644 default: break;
3645 case ARM::LDRrs:
3646 case ARM::LDRBrs: {
3647 unsigned ShOpVal =
3648 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3649 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3650 if (ShImm == 0 ||
3651 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3652 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3653 Latency -= 2;
3654 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3655 --Latency;
3656 break;
3657 }
3658 case ARM::t2LDRs:
3659 case ARM::t2LDRBs:
3660 case ARM::t2LDRHs:
3661 case ARM::t2LDRSHs: {
3662 // Thumb2 mode: lsl 0-3 only.
3663 Latency -= 2;
3664 break;
3665 }
3666 }
Evan Chengff310732010-10-28 06:47:08 +00003667 }
3668
Silviu Barangab47bb942012-09-13 15:05:10 +00003669 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Cheng6cc775f2011-06-28 19:10:37 +00003670 switch (DefMCID.getOpcode()) {
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003671 default: break;
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003672 case ARM::VLD1q8:
3673 case ARM::VLD1q16:
3674 case ARM::VLD1q32:
3675 case ARM::VLD1q64:
3676 case ARM::VLD1q8wb_register:
3677 case ARM::VLD1q16wb_register:
3678 case ARM::VLD1q32wb_register:
3679 case ARM::VLD1q64wb_register:
3680 case ARM::VLD1q8wb_fixed:
3681 case ARM::VLD1q16wb_fixed:
3682 case ARM::VLD1q32wb_fixed:
3683 case ARM::VLD1q64wb_fixed:
3684 case ARM::VLD2d8:
3685 case ARM::VLD2d16:
3686 case ARM::VLD2d32:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003687 case ARM::VLD2q8Pseudo:
3688 case ARM::VLD2q16Pseudo:
3689 case ARM::VLD2q32Pseudo:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003690 case ARM::VLD2d8wb_fixed:
3691 case ARM::VLD2d16wb_fixed:
3692 case ARM::VLD2d32wb_fixed:
Jim Grosbachd146a022011-12-09 21:28:25 +00003693 case ARM::VLD2q8PseudoWB_fixed:
3694 case ARM::VLD2q16PseudoWB_fixed:
3695 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbachc988e0c2012-03-05 19:33:30 +00003696 case ARM::VLD2d8wb_register:
3697 case ARM::VLD2d16wb_register:
3698 case ARM::VLD2d32wb_register:
Jim Grosbachd146a022011-12-09 21:28:25 +00003699 case ARM::VLD2q8PseudoWB_register:
3700 case ARM::VLD2q16PseudoWB_register:
3701 case ARM::VLD2q32PseudoWB_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003702 case ARM::VLD3d8Pseudo:
3703 case ARM::VLD3d16Pseudo:
3704 case ARM::VLD3d32Pseudo:
3705 case ARM::VLD1d64TPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003706 case ARM::VLD1d64TPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003707 case ARM::VLD3d8Pseudo_UPD:
3708 case ARM::VLD3d16Pseudo_UPD:
3709 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003710 case ARM::VLD3q8Pseudo_UPD:
3711 case ARM::VLD3q16Pseudo_UPD:
3712 case ARM::VLD3q32Pseudo_UPD:
3713 case ARM::VLD3q8oddPseudo:
3714 case ARM::VLD3q16oddPseudo:
3715 case ARM::VLD3q32oddPseudo:
3716 case ARM::VLD3q8oddPseudo_UPD:
3717 case ARM::VLD3q16oddPseudo_UPD:
3718 case ARM::VLD3q32oddPseudo_UPD:
3719 case ARM::VLD4d8Pseudo:
3720 case ARM::VLD4d16Pseudo:
3721 case ARM::VLD4d32Pseudo:
3722 case ARM::VLD1d64QPseudo:
Jiangning Liu4df23632014-01-16 09:16:13 +00003723 case ARM::VLD1d64QPseudoWB_fixed:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003724 case ARM::VLD4d8Pseudo_UPD:
3725 case ARM::VLD4d16Pseudo_UPD:
3726 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003727 case ARM::VLD4q8Pseudo_UPD:
3728 case ARM::VLD4q16Pseudo_UPD:
3729 case ARM::VLD4q32Pseudo_UPD:
3730 case ARM::VLD4q8oddPseudo:
3731 case ARM::VLD4q16oddPseudo:
3732 case ARM::VLD4q32oddPseudo:
3733 case ARM::VLD4q8oddPseudo_UPD:
3734 case ARM::VLD4q16oddPseudo_UPD:
3735 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbach13a292c2012-03-06 22:01:44 +00003736 case ARM::VLD1DUPq8:
3737 case ARM::VLD1DUPq16:
3738 case ARM::VLD1DUPq32:
3739 case ARM::VLD1DUPq8wb_fixed:
3740 case ARM::VLD1DUPq16wb_fixed:
3741 case ARM::VLD1DUPq32wb_fixed:
3742 case ARM::VLD1DUPq8wb_register:
3743 case ARM::VLD1DUPq16wb_register:
3744 case ARM::VLD1DUPq32wb_register:
3745 case ARM::VLD2DUPd8:
3746 case ARM::VLD2DUPd16:
3747 case ARM::VLD2DUPd32:
3748 case ARM::VLD2DUPd8wb_fixed:
3749 case ARM::VLD2DUPd16wb_fixed:
3750 case ARM::VLD2DUPd32wb_fixed:
3751 case ARM::VLD2DUPd8wb_register:
3752 case ARM::VLD2DUPd16wb_register:
3753 case ARM::VLD2DUPd32wb_register:
Evan Cheng7d6cd4902011-04-19 01:21:49 +00003754 case ARM::VLD4DUPd8Pseudo:
3755 case ARM::VLD4DUPd16Pseudo:
3756 case ARM::VLD4DUPd32Pseudo:
3757 case ARM::VLD4DUPd8Pseudo_UPD:
3758 case ARM::VLD4DUPd16Pseudo_UPD:
3759 case ARM::VLD4DUPd32Pseudo_UPD:
3760 case ARM::VLD1LNq8Pseudo:
3761 case ARM::VLD1LNq16Pseudo:
3762 case ARM::VLD1LNq32Pseudo:
3763 case ARM::VLD1LNq8Pseudo_UPD:
3764 case ARM::VLD1LNq16Pseudo_UPD:
3765 case ARM::VLD1LNq32Pseudo_UPD:
3766 case ARM::VLD2LNd8Pseudo:
3767 case ARM::VLD2LNd16Pseudo:
3768 case ARM::VLD2LNd32Pseudo:
3769 case ARM::VLD2LNq16Pseudo:
3770 case ARM::VLD2LNq32Pseudo:
3771 case ARM::VLD2LNd8Pseudo_UPD:
3772 case ARM::VLD2LNd16Pseudo_UPD:
3773 case ARM::VLD2LNd32Pseudo_UPD:
3774 case ARM::VLD2LNq16Pseudo_UPD:
3775 case ARM::VLD2LNq32Pseudo_UPD:
3776 case ARM::VLD4LNd8Pseudo:
3777 case ARM::VLD4LNd16Pseudo:
3778 case ARM::VLD4LNd32Pseudo:
3779 case ARM::VLD4LNq16Pseudo:
3780 case ARM::VLD4LNq32Pseudo:
3781 case ARM::VLD4LNd8Pseudo_UPD:
3782 case ARM::VLD4LNd16Pseudo_UPD:
3783 case ARM::VLD4LNd32Pseudo_UPD:
3784 case ARM::VLD4LNq16Pseudo_UPD:
3785 case ARM::VLD4LNq32Pseudo_UPD:
3786 // If the address is not 64-bit aligned, the latencies of these
3787 // instructions increases by one.
3788 ++Latency;
3789 break;
3790 }
3791
Evan Chengff310732010-10-28 06:47:08 +00003792 return Latency;
Evan Cheng49d4c0b2010-10-06 06:27:31 +00003793}
Evan Cheng63c76082010-10-19 18:58:51 +00003794
Arnold Schwaighoferd2f96b92013-09-30 15:28:56 +00003795unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr *MI) const {
3796 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3797 MI->isRegSequence() || MI->isImplicitDef())
3798 return 0;
3799
3800 if (MI->isBundle())
3801 return 0;
3802
3803 const MCInstrDesc &MCID = MI->getDesc();
3804
3805 if (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR)) {
3806 // When predicated, CPSR is an additional source operand for CPSR updating
3807 // instructions, this apparently increases their latencies.
3808 return 1;
3809 }
3810 return 0;
3811}
3812
Andrew Trick45446062012-06-05 21:11:27 +00003813unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3814 const MachineInstr *MI,
3815 unsigned *PredCost) const {
Evan Chengdebf9c52010-11-03 00:45:17 +00003816 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3817 MI->isRegSequence() || MI->isImplicitDef())
3818 return 1;
3819
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003820 // An instruction scheduler typically runs on unbundled instructions, however
3821 // other passes may query the latency of a bundled instruction.
Evan Cheng7fae11b2011-12-14 02:11:42 +00003822 if (MI->isBundle()) {
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003823 unsigned Latency = 0;
Evan Cheng7fae11b2011-12-14 02:11:42 +00003824 MachineBasicBlock::const_instr_iterator I = MI;
3825 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3826 while (++I != E && I->isInsideBundle()) {
3827 if (I->getOpcode() != ARM::t2IT)
3828 Latency += getInstrLatency(ItinData, I, PredCost);
3829 }
3830 return Latency;
3831 }
3832
Evan Cheng6cc775f2011-06-28 19:10:37 +00003833 const MCInstrDesc &MCID = MI->getDesc();
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003834 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Chengdebf9c52010-11-03 00:45:17 +00003835 // When predicated, CPSR is an additional source operand for CPSR updating
3836 // instructions, this apparently increases their latencies.
3837 *PredCost = 1;
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003838 }
3839 // Be sure to call getStageLatency for an empty itinerary in case it has a
3840 // valid MinLatency property.
3841 if (!ItinData)
3842 return MI->mayLoad() ? 3 : 1;
3843
3844 unsigned Class = MCID.getSchedClass();
3845
3846 // For instructions with variable uops, use uops as latency.
Andrew Trick21cca972012-07-02 19:12:29 +00003847 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003848 return getNumMicroOps(ItinData, MI);
Andrew Trick21cca972012-07-02 19:12:29 +00003849
Andrew Trickfb1a74c2012-06-07 19:41:55 +00003850 // For the common case, fall back on the itinerary's latency.
Andrew Trick5b1cadf2012-06-07 19:42:00 +00003851 unsigned Latency = ItinData->getStageLatency(Class);
3852
3853 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3854 unsigned DefAlign = MI->hasOneMemOperand()
3855 ? (*MI->memoperands_begin())->getAlignment() : 0;
3856 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3857 if (Adj >= 0 || (int)Latency > -Adj) {
3858 return Latency + Adj;
3859 }
3860 return Latency;
Evan Chengdebf9c52010-11-03 00:45:17 +00003861}
3862
3863int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3864 SDNode *Node) const {
3865 if (!Node->isMachineOpcode())
3866 return 1;
3867
3868 if (!ItinData || ItinData->isEmpty())
3869 return 1;
3870
3871 unsigned Opcode = Node->getMachineOpcode();
3872 switch (Opcode) {
3873 default:
3874 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003875 case ARM::VLDMQIA:
Bill Wendlinga68e3a52010-11-16 01:16:36 +00003876 case ARM::VSTMQIA:
Evan Chengdebf9c52010-11-03 00:45:17 +00003877 return 2;
Eric Christopherb006fc92010-11-18 19:40:05 +00003878 }
Evan Chengdebf9c52010-11-03 00:45:17 +00003879}
3880
Evan Cheng63c76082010-10-19 18:58:51 +00003881bool ARMBaseInstrInfo::
3882hasHighOperandLatency(const InstrItineraryData *ItinData,
3883 const MachineRegisterInfo *MRI,
3884 const MachineInstr *DefMI, unsigned DefIdx,
3885 const MachineInstr *UseMI, unsigned UseIdx) const {
3886 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3887 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
3888 if (Subtarget.isCortexA8() &&
3889 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
3890 // CortexA8 VFP instructions are not pipelined.
3891 return true;
3892
3893 // Hoist VFP / NEON instructions with 4 or higher latency.
Andrew Trickde2109e2013-06-15 04:49:57 +00003894 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
Andrew Trick3564bdf2012-06-07 19:41:58 +00003895 if (Latency < 0)
3896 Latency = getInstrLatency(ItinData, DefMI);
Evan Cheng63c76082010-10-19 18:58:51 +00003897 if (Latency <= 3)
3898 return false;
3899 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
3900 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
3901}
Evan Chenge96b8d72010-10-26 02:08:50 +00003902
3903bool ARMBaseInstrInfo::
3904hasLowDefLatency(const InstrItineraryData *ItinData,
3905 const MachineInstr *DefMI, unsigned DefIdx) const {
3906 if (!ItinData || ItinData->isEmpty())
3907 return false;
3908
3909 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3910 if (DDomain == ARMII::DomainGeneral) {
3911 unsigned DefClass = DefMI->getDesc().getSchedClass();
3912 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3913 return (DefCycle != -1 && DefCycle <= 2);
3914 }
3915 return false;
3916}
Evan Cheng62c7b5b2010-12-05 22:04:16 +00003917
Andrew Trick924123a2011-09-21 02:20:46 +00003918bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
3919 StringRef &ErrInfo) const {
3920 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
3921 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
3922 return false;
3923 }
3924 return true;
3925}
3926
Evan Cheng62c7b5b2010-12-05 22:04:16 +00003927bool
3928ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
3929 unsigned &AddSubOpc,
3930 bool &NegAcc, bool &HasLane) const {
3931 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
3932 if (I == MLxEntryMap.end())
3933 return false;
3934
3935 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
3936 MulOpc = Entry.MulOpc;
3937 AddSubOpc = Entry.AddSubOpc;
3938 NegAcc = Entry.NegAcc;
3939 HasLane = Entry.HasLane;
3940 return true;
3941}
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003942
3943//===----------------------------------------------------------------------===//
3944// Execution domains.
3945//===----------------------------------------------------------------------===//
3946//
3947// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
3948// and some can go down both. The vmov instructions go down the VFP pipeline,
3949// but they can be changed to vorr equivalents that are executed by the NEON
3950// pipeline.
3951//
3952// We use the following execution domain numbering:
3953//
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003954enum ARMExeDomain {
3955 ExeGeneric = 0,
3956 ExeVFP = 1,
3957 ExeNEON = 2
3958};
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003959//
3960// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
3961//
3962std::pair<uint16_t, uint16_t>
3963ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Tim Northoverf6618152012-08-17 11:32:52 +00003964 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
3965 // if they are not predicated.
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003966 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003967 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003968
Silviu Barangadc453362013-03-27 12:38:44 +00003969 // CortexA9 is particularly picky about mixing the two and wants these
Tim Northoverf6618152012-08-17 11:32:52 +00003970 // converted.
Silviu Barangadc453362013-03-27 12:38:44 +00003971 if (Subtarget.isCortexA9() && !isPredicated(MI) &&
Tim Northoverf6618152012-08-17 11:32:52 +00003972 (MI->getOpcode() == ARM::VMOVRS ||
Tim Northoverca9f3842012-08-30 10:17:45 +00003973 MI->getOpcode() == ARM::VMOVSR ||
3974 MI->getOpcode() == ARM::VMOVS))
Tim Northoverf6618152012-08-17 11:32:52 +00003975 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
3976
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003977 // No other instructions can be swizzled, so just determine their domain.
3978 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
3979
3980 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003981 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003982
3983 // Certain instructions can go either way on Cortex-A8.
3984 // Treat them as NEON instructions.
3985 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003986 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003987
3988 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003989 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003990
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00003991 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00003992}
3993
Tim Northover771f1602012-08-29 16:36:07 +00003994static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
3995 unsigned SReg, unsigned &Lane) {
3996 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
3997 Lane = 0;
3998
3999 if (DReg != ARM::NoRegister)
4000 return DReg;
4001
4002 Lane = 1;
4003 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
4004
4005 assert(DReg && "S-register with no D super-register?");
4006 return DReg;
4007}
4008
Andrew Trickd9296ec2012-10-10 05:43:01 +00004009/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloyea052562012-09-18 08:31:15 +00004010/// set ImplicitSReg to a register number that must be marked as implicit-use or
4011/// zero if no register needs to be defined as implicit-use.
4012///
4013/// If the function cannot determine if an SPR should be marked implicit use or
4014/// not, it returns false.
4015///
4016/// This function handles cases where an instruction is being modified from taking
Andrew Trickd9296ec2012-10-10 05:43:01 +00004017/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloyea052562012-09-18 08:31:15 +00004018/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
4019/// lane of the DPR).
4020///
4021/// If the other SPR is defined, an implicit-use of it should be added. Else,
4022/// (including the case where the DPR itself is defined), it should not.
Andrew Trickd9296ec2012-10-10 05:43:01 +00004023///
James Molloyea052562012-09-18 08:31:15 +00004024static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
4025 MachineInstr *MI,
4026 unsigned DReg, unsigned Lane,
4027 unsigned &ImplicitSReg) {
4028 // If the DPR is defined or used already, the other SPR lane will be chained
4029 // correctly, so there is nothing to be done.
4030 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
4031 ImplicitSReg = 0;
4032 return true;
4033 }
4034
4035 // Otherwise we need to go searching to see if the SPR is set explicitly.
4036 ImplicitSReg = TRI->getSubReg(DReg,
4037 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
4038 MachineBasicBlock::LivenessQueryResult LQR =
4039 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
4040
4041 if (LQR == MachineBasicBlock::LQR_Live)
4042 return true;
4043 else if (LQR == MachineBasicBlock::LQR_Unknown)
4044 return false;
4045
4046 // If the register is known not to be live, there is no need to add an
4047 // implicit-use.
4048 ImplicitSReg = 0;
4049 return true;
4050}
Tim Northover771f1602012-08-29 16:36:07 +00004051
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004052void
4053ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northoverf6618152012-08-17 11:32:52 +00004054 unsigned DstReg, SrcReg, DReg;
4055 unsigned Lane;
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00004056 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northoverf6618152012-08-17 11:32:52 +00004057 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northoverf6618152012-08-17 11:32:52 +00004058 switch (MI->getOpcode()) {
4059 default:
4060 llvm_unreachable("cannot handle opcode!");
4061 break;
4062 case ARM::VMOVD:
4063 if (Domain != ExeNEON)
4064 break;
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004065
Tim Northoverf6618152012-08-17 11:32:52 +00004066 // Zap the predicate operands.
4067 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesenf7ad1892011-09-29 02:48:41 +00004068
Tim Northover771f1602012-08-29 16:36:07 +00004069 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
4070 DstReg = MI->getOperand(0).getReg();
4071 SrcReg = MI->getOperand(1).getReg();
4072
4073 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4074 MI->RemoveOperand(i-1);
4075
4076 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004077 MI->setDesc(get(ARM::VORRd));
Tim Northover771f1602012-08-29 16:36:07 +00004078 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4079 .addReg(SrcReg)
4080 .addReg(SrcReg));
Tim Northoverf6618152012-08-17 11:32:52 +00004081 break;
4082 case ARM::VMOVRS:
4083 if (Domain != ExeNEON)
4084 break;
4085 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
4086
Tim Northover771f1602012-08-29 16:36:07 +00004087 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004088 DstReg = MI->getOperand(0).getReg();
4089 SrcReg = MI->getOperand(1).getReg();
4090
Tim Northover771f1602012-08-29 16:36:07 +00004091 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4092 MI->RemoveOperand(i-1);
Tim Northoverf6618152012-08-17 11:32:52 +00004093
Tim Northover771f1602012-08-29 16:36:07 +00004094 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northoverf6618152012-08-17 11:32:52 +00004095
Tim Northover771f1602012-08-29 16:36:07 +00004096 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
4097 // Note that DSrc has been widened and the other lane may be undef, which
4098 // contaminates the entire register.
Tim Northoverf6618152012-08-17 11:32:52 +00004099 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover771f1602012-08-29 16:36:07 +00004100 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
4101 .addReg(DReg, RegState::Undef)
4102 .addImm(Lane));
Tim Northoverf6618152012-08-17 11:32:52 +00004103
Tim Northover771f1602012-08-29 16:36:07 +00004104 // The old source should be an implicit use, otherwise we might think it
4105 // was dead before here.
Tim Northoverf6618152012-08-17 11:32:52 +00004106 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004107 break;
James Molloyea052562012-09-18 08:31:15 +00004108 case ARM::VMOVSR: {
Tim Northoverf6618152012-08-17 11:32:52 +00004109 if (Domain != ExeNEON)
4110 break;
4111 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
4112
Tim Northover771f1602012-08-29 16:36:07 +00004113 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northoverf6618152012-08-17 11:32:52 +00004114 DstReg = MI->getOperand(0).getReg();
4115 SrcReg = MI->getOperand(1).getReg();
Tim Northoverf6618152012-08-17 11:32:52 +00004116
Tim Northover771f1602012-08-29 16:36:07 +00004117 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
4118
James Molloyea052562012-09-18 08:31:15 +00004119 unsigned ImplicitSReg;
4120 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
4121 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004122
Tim Northoverc8d867d2012-09-05 18:37:53 +00004123 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4124 MI->RemoveOperand(i-1);
4125
Tim Northover771f1602012-08-29 16:36:07 +00004126 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
4127 // Again DDst may be undefined at the beginning of this instruction.
Tim Northoverf6618152012-08-17 11:32:52 +00004128 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover726d32c2012-09-01 18:07:29 +00004129 MIB.addReg(DReg, RegState::Define)
4130 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
4131 .addReg(SrcReg)
4132 .addImm(Lane);
4133 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004134
Tim Northover726d32c2012-09-01 18:07:29 +00004135 // The narrower destination must be marked as set to keep previous chains
4136 // in place.
Tim Northover771f1602012-08-29 16:36:07 +00004137 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004138 if (ImplicitSReg != 0)
4139 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverf6618152012-08-17 11:32:52 +00004140 break;
James Molloyea052562012-09-18 08:31:15 +00004141 }
Tim Northoverca9f3842012-08-30 10:17:45 +00004142 case ARM::VMOVS: {
4143 if (Domain != ExeNEON)
4144 break;
4145
4146 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
4147 DstReg = MI->getOperand(0).getReg();
4148 SrcReg = MI->getOperand(1).getReg();
4149
Tim Northoverca9f3842012-08-30 10:17:45 +00004150 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
4151 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
4152 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
4153
James Molloyea052562012-09-18 08:31:15 +00004154 unsigned ImplicitSReg;
4155 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
4156 break;
Tim Northover726d32c2012-09-01 18:07:29 +00004157
Tim Northoverc8d867d2012-09-05 18:37:53 +00004158 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
4159 MI->RemoveOperand(i-1);
4160
Tim Northoverca9f3842012-08-30 10:17:45 +00004161 if (DSrc == DDst) {
4162 // Destination can be:
4163 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
4164 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover726d32c2012-09-01 18:07:29 +00004165 MIB.addReg(DDst, RegState::Define)
4166 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
4167 .addImm(SrcLane);
4168 AddDefaultPred(MIB);
Tim Northoverca9f3842012-08-30 10:17:45 +00004169
4170 // Neither the source or the destination are naturally represented any
4171 // more, so add them in manually.
4172 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
4173 MIB.addReg(SrcReg, RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004174 if (ImplicitSReg != 0)
4175 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004176 break;
4177 }
4178
4179 // In general there's no single instruction that can perform an S <-> S
4180 // move in NEON space, but a pair of VEXT instructions *can* do the
4181 // job. It turns out that the VEXTs needed will only use DSrc once, with
4182 // the position based purely on the combination of lane-0 and lane-1
4183 // involved. For example
4184 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
4185 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
4186 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
4187 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
4188 //
4189 // Pattern of the MachineInstrs is:
4190 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
4191 MachineInstrBuilder NewMIB;
4192 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4193 get(ARM::VEXTd32), DDst);
Tim Northover726d32c2012-09-01 18:07:29 +00004194
4195 // On the first instruction, both DSrc and DDst may be <undef> if present.
4196 // Specifically when the original instruction didn't have them as an
4197 // <imp-use>.
4198 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
4199 bool CurUndef = !MI->readsRegister(CurReg, TRI);
4200 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4201
4202 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
4203 CurUndef = !MI->readsRegister(CurReg, TRI);
4204 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
4205
Tim Northoverca9f3842012-08-30 10:17:45 +00004206 NewMIB.addImm(1);
4207 AddDefaultPred(NewMIB);
4208
4209 if (SrcLane == DstLane)
4210 NewMIB.addReg(SrcReg, RegState::Implicit);
4211
4212 MI->setDesc(get(ARM::VEXTd32));
4213 MIB.addReg(DDst, RegState::Define);
Tim Northover726d32c2012-09-01 18:07:29 +00004214
4215 // On the second instruction, DDst has definitely been defined above, so
4216 // it is not <undef>. DSrc, if present, can be <undef> as above.
4217 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
4218 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4219 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4220
4221 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
4222 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
4223 MIB.addReg(CurReg, getUndefRegState(CurUndef));
4224
Tim Northoverca9f3842012-08-30 10:17:45 +00004225 MIB.addImm(1);
4226 AddDefaultPred(MIB);
4227
4228 if (SrcLane != DstLane)
4229 MIB.addReg(SrcReg, RegState::Implicit);
4230
4231 // As before, the original destination is no longer represented, add it
4232 // implicitly.
4233 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloyea052562012-09-18 08:31:15 +00004234 if (ImplicitSReg != 0)
4235 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverca9f3842012-08-30 10:17:45 +00004236 break;
4237 }
Tim Northoverf6618152012-08-17 11:32:52 +00004238 }
4239
Jakob Stoklund Olesenf9b71a22011-09-27 22:57:21 +00004240}
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004241
Bob Wilsone8a549c2012-09-29 21:43:49 +00004242//===----------------------------------------------------------------------===//
4243// Partial register updates
4244//===----------------------------------------------------------------------===//
4245//
4246// Swift renames NEON registers with 64-bit granularity. That means any
4247// instruction writing an S-reg implicitly reads the containing D-reg. The
4248// problem is mostly avoided by translating f32 operations to v2f32 operations
4249// on D-registers, but f32 loads are still a problem.
4250//
4251// These instructions can load an f32 into a NEON register:
4252//
4253// VLDRS - Only writes S, partial D update.
4254// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4255// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4256//
4257// FCONSTD can be used as a dependency-breaking instruction.
Bob Wilsone8a549c2012-09-29 21:43:49 +00004258unsigned ARMBaseInstrInfo::
4259getPartialRegUpdateClearance(const MachineInstr *MI,
4260 unsigned OpNum,
4261 const TargetRegisterInfo *TRI) const {
Silviu Barangadc453362013-03-27 12:38:44 +00004262 if (!SwiftPartialUpdateClearance ||
4263 !(Subtarget.isSwift() || Subtarget.isCortexA15()))
Bob Wilsone8a549c2012-09-29 21:43:49 +00004264 return 0;
4265
4266 assert(TRI && "Need TRI instance");
4267
4268 const MachineOperand &MO = MI->getOperand(OpNum);
4269 if (MO.readsReg())
4270 return 0;
4271 unsigned Reg = MO.getReg();
4272 int UseOp = -1;
4273
4274 switch(MI->getOpcode()) {
4275 // Normal instructions writing only an S-register.
4276 case ARM::VLDRS:
4277 case ARM::FCONSTS:
4278 case ARM::VMOVSR:
Bob Wilsone8a549c2012-09-29 21:43:49 +00004279 case ARM::VMOVv8i8:
4280 case ARM::VMOVv4i16:
4281 case ARM::VMOVv2i32:
4282 case ARM::VMOVv2f32:
4283 case ARM::VMOVv1i64:
4284 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4285 break;
4286
4287 // Explicitly reads the dependency.
4288 case ARM::VLD1LNd32:
Silviu Barangadc453362013-03-27 12:38:44 +00004289 UseOp = 3;
Bob Wilsone8a549c2012-09-29 21:43:49 +00004290 break;
4291 default:
4292 return 0;
4293 }
4294
4295 // If this instruction actually reads a value from Reg, there is no unwanted
4296 // dependency.
4297 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4298 return 0;
4299
4300 // We must be able to clobber the whole D-reg.
4301 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4302 // Virtual register must be a foo:ssub_0<def,undef> operand.
4303 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4304 return 0;
4305 } else if (ARM::SPRRegClass.contains(Reg)) {
4306 // Physical register: MI must define the full D-reg.
4307 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4308 &ARM::DPRRegClass);
4309 if (!DReg || !MI->definesRegister(DReg, TRI))
4310 return 0;
4311 }
4312
4313 // MI has an unwanted D-register dependency.
4314 // Avoid defs in the previous N instructrions.
4315 return SwiftPartialUpdateClearance;
4316}
4317
4318// Break a partial register dependency after getPartialRegUpdateClearance
4319// returned non-zero.
4320void ARMBaseInstrInfo::
4321breakPartialRegDependency(MachineBasicBlock::iterator MI,
4322 unsigned OpNum,
4323 const TargetRegisterInfo *TRI) const {
4324 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4325 assert(TRI && "Need TRI instance");
4326
4327 const MachineOperand &MO = MI->getOperand(OpNum);
4328 unsigned Reg = MO.getReg();
4329 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4330 "Can't break virtual register dependencies.");
4331 unsigned DReg = Reg;
4332
4333 // If MI defines an S-reg, find the corresponding D super-register.
4334 if (ARM::SPRRegClass.contains(Reg)) {
4335 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4336 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4337 }
4338
4339 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4340 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4341
4342 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4343 // the full D-register by loading the same value to both lanes. The
4344 // instruction is micro-coded with 2 uops, so don't do this until we can
Robert Wilhelm516be562013-09-14 09:34:24 +00004345 // properly schedule micro-coded instructions. The dispatcher stalls cause
Bob Wilsone8a549c2012-09-29 21:43:49 +00004346 // too big regressions.
4347
4348 // Insert the dependency-breaking FCONSTD before MI.
4349 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4350 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4351 get(ARM::FCONSTD), DReg).addImm(96));
4352 MI->addRegisterKilled(DReg, TRI, true);
4353}
4354
Jim Grosbach617f84dd2012-02-28 23:53:30 +00004355bool ARMBaseInstrInfo::hasNOP() const {
4356 return (Subtarget.getFeatureBits() & ARM::HasV6T2Ops) != 0;
4357}
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004358
4359bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
Arnold Schwaighofere9375922013-06-05 14:59:36 +00004360 if (MI->getNumOperands() < 4)
4361 return true;
Arnold Schwaighofer5dde1f32013-04-05 04:42:00 +00004362 unsigned ShOpVal = MI->getOperand(3).getImm();
4363 unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
4364 // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
4365 if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
4366 ((ShImm == 1 || ShImm == 2) &&
4367 ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
4368 return true;
4369
4370 return false;
4371}