blob: 0076910c95aaa9f183ad2624332072c144b96ddd [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
David Goodwin334c2642009-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
14#include "ARMBaseInstrInfo.h"
15#include "ARM.h"
Craig Topper0e5233a2012-03-26 00:45:15 +000016#include "ARMBaseRegisterInfo.h"
Evan Chengd457e6e2009-11-07 04:04:34 +000017#include "ARMConstantPoolValue.h"
Evan Cheng48575f62010-12-05 22:04:16 +000018#include "ARMHazardRecognizer.h"
David Goodwin334c2642009-07-08 16:09:28 +000019#include "ARMMachineFunctionInfo.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000020#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
David Goodwin334c2642009-07-08 16:09:28 +000022#include "llvm/CodeGen/LiveVariables.h"
Evan Chengd457e6e2009-11-07 04:04:34 +000023#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwin334c2642009-07-08 16:09:28 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov249fb332009-10-07 00:06:35 +000027#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng2457f2c2010-05-22 01:47:14 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000029#include "llvm/CodeGen/SelectionDAGNodes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/Constants.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalValue.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000033#include "llvm/MC/MCAsmInfo.h"
Jakub Staszakf81b7f62011-07-10 02:58:07 +000034#include "llvm/Support/BranchProbability.h"
David Goodwin334c2642009-07-08 16:09:28 +000035#include "llvm/Support/CommandLine.h"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000036#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Evan Cheng22fee2d2011-06-28 20:07:07 +000038
Evan Cheng4db3cff2011-07-01 17:57:27 +000039#define GET_INSTRINFO_CTOR
Evan Cheng22fee2d2011-06-28 20:07:07 +000040#include "ARMGenInstrInfo.inc"
41
David Goodwin334c2642009-07-08 16:09:28 +000042using namespace llvm;
43
44static cl::opt<bool>
45EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
46 cl::desc("Enable ARM 2-addr to 3-addr conv"));
47
Jakob Stoklund Olesen61545822011-08-31 17:00:02 +000048static cl::opt<bool>
Jakob Stoklund Olesen3805d852011-11-15 23:53:18 +000049WidenVMOVS("widen-vmovs", cl::Hidden, cl::init(true),
Jakob Stoklund Olesen61545822011-08-31 17:00:02 +000050 cl::desc("Widen ARM vmovs to vmovd when possible"));
51
Bob Wilsoneb1641d2012-09-29 21:43:49 +000052static cl::opt<unsigned>
53SwiftPartialUpdateClearance("swift-partial-update-clearance",
54 cl::Hidden, cl::init(12),
55 cl::desc("Clearance before partial register updates"));
56
Evan Cheng48575f62010-12-05 22:04:16 +000057/// ARM_MLxEntry - Record information about MLA / MLS instructions.
58struct ARM_MLxEntry {
Craig Toppercd2859e2012-05-24 03:59:11 +000059 uint16_t MLxOpc; // MLA / MLS opcode
60 uint16_t MulOpc; // Expanded multiplication opcode
61 uint16_t AddSubOpc; // Expanded add / sub opcode
Evan Cheng48575f62010-12-05 22:04:16 +000062 bool NegAcc; // True if the acc is negated before the add / sub.
63 bool HasLane; // True if instruction has an extra "lane" operand.
64};
65
66static const ARM_MLxEntry ARM_MLxTable[] = {
67 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
68 // fp scalar ops
69 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
70 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
71 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
72 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng48575f62010-12-05 22:04:16 +000073 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
74 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
75 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
76 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
77
78 // fp SIMD ops
79 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
80 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
81 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
82 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
83 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
84 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
85 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
86 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
87};
88
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000089ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
Evan Cheng4db3cff2011-07-01 17:57:27 +000090 : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000091 Subtarget(STI) {
Evan Cheng48575f62010-12-05 22:04:16 +000092 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
93 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
94 assert(false && "Duplicated entries?");
95 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
96 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
97 }
98}
99
Andrew Trick2da8bc82010-12-24 05:03:26 +0000100// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
101// currently defaults to no prepass hazard recognizer.
Evan Cheng48575f62010-12-05 22:04:16 +0000102ScheduleHazardRecognizer *ARMBaseInstrInfo::
Andrew Trick2da8bc82010-12-24 05:03:26 +0000103CreateTargetHazardRecognizer(const TargetMachine *TM,
104 const ScheduleDAG *DAG) const {
Andrew Trickc8bfd1d2011-01-21 05:51:33 +0000105 if (usePreRAHazardRecognizer()) {
Andrew Trick2da8bc82010-12-24 05:03:26 +0000106 const InstrItineraryData *II = TM->getInstrItineraryData();
107 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
108 }
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +0000109 return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
Andrew Trick2da8bc82010-12-24 05:03:26 +0000110}
111
112ScheduleHazardRecognizer *ARMBaseInstrInfo::
113CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
114 const ScheduleDAG *DAG) const {
Evan Cheng48575f62010-12-05 22:04:16 +0000115 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
116 return (ScheduleHazardRecognizer *)
Andrew Trick2da8bc82010-12-24 05:03:26 +0000117 new ARMHazardRecognizer(II, *this, getRegisterInfo(), Subtarget, DAG);
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +0000118 return TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwin334c2642009-07-08 16:09:28 +0000119}
120
121MachineInstr *
122ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
123 MachineBasicBlock::iterator &MBBI,
124 LiveVariables *LV) const {
Evan Cheng78703dd2009-07-27 18:44:00 +0000125 // FIXME: Thumb2 support.
126
David Goodwin334c2642009-07-08 16:09:28 +0000127 if (!EnableARM3Addr)
128 return NULL;
129
130 MachineInstr *MI = MBBI;
131 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000132 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwin334c2642009-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 Chenge837dea2011-06-28 19:10:37 +0000152 const MCInstrDesc &MCID = MI->getDesc();
153 unsigned NumOps = MCID.getNumOperands();
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000154 bool isLoad = !MI->mayStore();
David Goodwin334c2642009-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 Topperbc219812012-02-07 02:50:20 +0000164 default: llvm_unreachable("Unknown indexed op!");
David Goodwin334c2642009-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 Chenge7cbe412009-07-08 21:03:57 +0000169 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwin334c2642009-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 Cheng78703dd2009-07-27 18:44:00 +0000174 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge7cbe412009-07-08 21:03:57 +0000175 .addReg(BaseReg).addImm(Amt)
David Goodwin334c2642009-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 Anderson92a20222011-07-21 18:54:16 +0000181 get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
David Goodwin334c2642009-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 Cheng78703dd2009-07-27 18:44:00 +0000186 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-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 Cheng78703dd2009-07-27 18:44:00 +0000197 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwin334c2642009-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 Cheng78703dd2009-07-27 18:44:00 +0000202 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-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 Grosbach3e556122010-10-26 22:37:02 +0000214 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwin334c2642009-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 Grosbach3e556122010-10-26 22:37:02 +0000225 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwin334c2642009-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 Olesenc9df0252011-01-10 02:58:51 +0000240 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwin334c2642009-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 {
276 // If the block has no terminators, it just falls into the block after it.
277 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000278 if (I == MBB.begin())
279 return false;
280 --I;
281 while (I->isDebugValue()) {
282 if (I == MBB.begin())
283 return false;
284 --I;
285 }
286 if (!isUnpredicatedTerminator(I))
David Goodwin334c2642009-07-08 16:09:28 +0000287 return false;
288
289 // Get the last instruction in the block.
290 MachineInstr *LastInst = I;
291
292 // If there is only one terminator instruction, process it.
293 unsigned LastOpc = LastInst->getOpcode();
294 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Evan Cheng5ca53a72009-07-27 18:20:05 +0000295 if (isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000296 TBB = LastInst->getOperand(0).getMBB();
297 return false;
298 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000299 if (isCondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000300 // Block ends with fall-through condbranch.
301 TBB = LastInst->getOperand(0).getMBB();
302 Cond.push_back(LastInst->getOperand(1));
303 Cond.push_back(LastInst->getOperand(2));
304 return false;
305 }
306 return true; // Can't handle indirect branch.
307 }
308
309 // Get the instruction before it if it is a terminator.
310 MachineInstr *SecondLastInst = I;
Evan Cheng108c8722010-09-23 06:54:40 +0000311 unsigned SecondLastOpc = SecondLastInst->getOpcode();
312
313 // If AllowModify is true and the block ends with two or more unconditional
314 // branches, delete all but the first unconditional branch.
315 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
316 while (isUncondBranchOpcode(SecondLastOpc)) {
317 LastInst->eraseFromParent();
318 LastInst = SecondLastInst;
319 LastOpc = LastInst->getOpcode();
Evan Cheng676e2582010-09-23 19:42:03 +0000320 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
321 // Return now the only terminator is an unconditional branch.
322 TBB = LastInst->getOperand(0).getMBB();
323 return false;
324 } else {
Evan Cheng108c8722010-09-23 06:54:40 +0000325 SecondLastInst = I;
326 SecondLastOpc = SecondLastInst->getOpcode();
327 }
328 }
329 }
David Goodwin334c2642009-07-08 16:09:28 +0000330
331 // If there are three terminators, we don't know what sort of block this is.
332 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
333 return true;
334
Evan Cheng5ca53a72009-07-27 18:20:05 +0000335 // If the block ends with a B and a Bcc, handle it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000336 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000337 TBB = SecondLastInst->getOperand(0).getMBB();
338 Cond.push_back(SecondLastInst->getOperand(1));
339 Cond.push_back(SecondLastInst->getOperand(2));
340 FBB = LastInst->getOperand(0).getMBB();
341 return false;
342 }
343
344 // If the block ends with two unconditional branches, handle it. The second
345 // one is not executed, so remove it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000346 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000347 TBB = SecondLastInst->getOperand(0).getMBB();
348 I = LastInst;
349 if (AllowModify)
350 I->eraseFromParent();
351 return false;
352 }
353
354 // ...likewise if it ends with a branch table followed by an unconditional
355 // branch. The branch folder can create these, and we must get rid of them for
356 // correctness of Thumb constant islands.
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000357 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
358 isIndirectBranchOpcode(SecondLastOpc)) &&
Evan Cheng5ca53a72009-07-27 18:20:05 +0000359 isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000360 I = LastInst;
361 if (AllowModify)
362 I->eraseFromParent();
363 return true;
364 }
365
366 // Otherwise, can't handle this.
367 return true;
368}
369
370
371unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwin334c2642009-07-08 16:09:28 +0000372 MachineBasicBlock::iterator I = MBB.end();
373 if (I == MBB.begin()) return 0;
374 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000375 while (I->isDebugValue()) {
376 if (I == MBB.begin())
377 return 0;
378 --I;
379 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000380 if (!isUncondBranchOpcode(I->getOpcode()) &&
381 !isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000382 return 0;
383
384 // Remove the branch.
385 I->eraseFromParent();
386
387 I = MBB.end();
388
389 if (I == MBB.begin()) return 1;
390 --I;
Evan Cheng5ca53a72009-07-27 18:20:05 +0000391 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000392 return 1;
393
394 // Remove the branch.
395 I->eraseFromParent();
396 return 2;
397}
398
399unsigned
400ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000401 MachineBasicBlock *FBB,
402 const SmallVectorImpl<MachineOperand> &Cond,
403 DebugLoc DL) const {
Evan Cheng6495f632009-07-28 05:48:47 +0000404 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
405 int BOpc = !AFI->isThumbFunction()
406 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
407 int BccOpc = !AFI->isThumbFunction()
408 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000409 bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
Andrew Tricke23dc9c2011-09-21 02:17:37 +0000410
David Goodwin334c2642009-07-08 16:09:28 +0000411 // Shouldn't be a fall through.
412 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
413 assert((Cond.size() == 2 || Cond.size() == 0) &&
414 "ARM branch conditions have two components!");
415
416 if (FBB == 0) {
Owen Anderson112fb732011-09-09 23:13:02 +0000417 if (Cond.empty()) { // Unconditional branch?
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000418 if (isThumb)
419 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).addImm(ARMCC::AL).addReg(0);
420 else
421 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
Owen Anderson112fb732011-09-09 23:13:02 +0000422 } else
Stuart Hastings3bf91252010-06-17 22:43:56 +0000423 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000424 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
425 return 1;
426 }
427
428 // Two-way conditional branch.
Stuart Hastings3bf91252010-06-17 22:43:56 +0000429 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000430 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000431 if (isThumb)
432 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).addImm(ARMCC::AL).addReg(0);
433 else
434 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwin334c2642009-07-08 16:09:28 +0000435 return 2;
436}
437
438bool ARMBaseInstrInfo::
439ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
440 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
441 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
442 return false;
443}
444
Evan Chengddfd1372011-12-14 02:11:42 +0000445bool ARMBaseInstrInfo::isPredicated(const MachineInstr *MI) const {
446 if (MI->isBundle()) {
447 MachineBasicBlock::const_instr_iterator I = MI;
448 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
449 while (++I != E && I->isInsideBundle()) {
450 int PIdx = I->findFirstPredOperandIdx();
451 if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
452 return true;
453 }
454 return false;
455 }
456
457 int PIdx = MI->findFirstPredOperandIdx();
458 return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
459}
460
David Goodwin334c2642009-07-08 16:09:28 +0000461bool ARMBaseInstrInfo::
462PredicateInstruction(MachineInstr *MI,
463 const SmallVectorImpl<MachineOperand> &Pred) const {
464 unsigned Opc = MI->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000465 if (isUncondBranchOpcode(Opc)) {
466 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
Jakob Stoklund Olesenb9efafe2012-12-20 22:53:55 +0000467 MachineInstrBuilder(*MI->getParent()->getParent(), MI)
468 .addImm(Pred[0].getImm())
469 .addReg(Pred[1].getReg());
David Goodwin334c2642009-07-08 16:09:28 +0000470 return true;
471 }
472
473 int PIdx = MI->findFirstPredOperandIdx();
474 if (PIdx != -1) {
475 MachineOperand &PMO = MI->getOperand(PIdx);
476 PMO.setImm(Pred[0].getImm());
477 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
478 return true;
479 }
480 return false;
481}
482
483bool ARMBaseInstrInfo::
484SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
485 const SmallVectorImpl<MachineOperand> &Pred2) const {
486 if (Pred1.size() > 2 || Pred2.size() > 2)
487 return false;
488
489 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
490 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
491 if (CC1 == CC2)
492 return true;
493
494 switch (CC1) {
495 default:
496 return false;
497 case ARMCC::AL:
498 return true;
499 case ARMCC::HS:
500 return CC2 == ARMCC::HI;
501 case ARMCC::LS:
502 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
503 case ARMCC::GE:
504 return CC2 == ARMCC::GT;
505 case ARMCC::LE:
506 return CC2 == ARMCC::LT;
507 }
508}
509
510bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
511 std::vector<MachineOperand> &Pred) const {
David Goodwin334c2642009-07-08 16:09:28 +0000512 bool Found = false;
513 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
514 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen2420b552012-02-17 19:23:15 +0000515 if ((MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) ||
516 (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR)) {
David Goodwin334c2642009-07-08 16:09:28 +0000517 Pred.push_back(MO);
518 Found = true;
519 }
520 }
521
522 return Found;
523}
524
Evan Chengac0869d2009-11-21 06:21:52 +0000525/// isPredicable - Return true if the specified instruction can be predicated.
526/// By default, this returns true for every instruction with a
527/// PredicateOperand.
528bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000529 if (!MI->isPredicable())
Evan Chengac0869d2009-11-21 06:21:52 +0000530 return false;
531
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000532 if ((MI->getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
Evan Chengac0869d2009-11-21 06:21:52 +0000533 ARMFunctionInfo *AFI =
534 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
Evan Chengd7f08102009-11-24 08:06:15 +0000535 return AFI->isThumb2Function();
Evan Chengac0869d2009-11-21 06:21:52 +0000536 }
537 return true;
538}
David Goodwin334c2642009-07-08 16:09:28 +0000539
Chris Lattner56856b12009-12-03 06:58:32 +0000540/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
Chandler Carruth19e57022010-10-23 08:40:19 +0000541LLVM_ATTRIBUTE_NOINLINE
David Goodwin334c2642009-07-08 16:09:28 +0000542static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattner56856b12009-12-03 06:58:32 +0000543 unsigned JTI);
David Goodwin334c2642009-07-08 16:09:28 +0000544static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
545 unsigned JTI) {
Chris Lattner56856b12009-12-03 06:58:32 +0000546 assert(JTI < JT.size());
David Goodwin334c2642009-07-08 16:09:28 +0000547 return JT[JTI].MBBs.size();
548}
549
550/// GetInstSize - Return the size of the specified MachineInstr.
551///
552unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
553 const MachineBasicBlock &MBB = *MI->getParent();
554 const MachineFunction *MF = MBB.getParent();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000555 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwin334c2642009-07-08 16:09:28 +0000556
Evan Chenge837dea2011-06-28 19:10:37 +0000557 const MCInstrDesc &MCID = MI->getDesc();
Owen Anderson16884412011-07-13 23:22:26 +0000558 if (MCID.getSize())
559 return MCID.getSize();
David Goodwin334c2642009-07-08 16:09:28 +0000560
David Blaikie4d6ccb52012-01-20 21:51:11 +0000561 // If this machine instr is an inline asm, measure it.
562 if (MI->getOpcode() == ARM::INLINEASM)
563 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
564 if (MI->isLabel())
565 return 0;
566 unsigned Opc = MI->getOpcode();
567 switch (Opc) {
568 case TargetOpcode::IMPLICIT_DEF:
569 case TargetOpcode::KILL:
570 case TargetOpcode::PROLOG_LABEL:
571 case TargetOpcode::EH_LABEL:
572 case TargetOpcode::DBG_VALUE:
573 return 0;
574 case TargetOpcode::BUNDLE:
575 return getInstBundleLength(MI);
576 case ARM::MOVi16_ga_pcrel:
577 case ARM::MOVTi16_ga_pcrel:
578 case ARM::t2MOVi16_ga_pcrel:
579 case ARM::t2MOVTi16_ga_pcrel:
580 return 4;
581 case ARM::MOVi32imm:
582 case ARM::t2MOVi32imm:
583 return 8;
584 case ARM::CONSTPOOL_ENTRY:
585 // If this machine instr is a constant pool entry, its size is recorded as
586 // operand #2.
587 return MI->getOperand(2).getImm();
588 case ARM::Int_eh_sjlj_longjmp:
589 return 16;
590 case ARM::tInt_eh_sjlj_longjmp:
591 return 10;
592 case ARM::Int_eh_sjlj_setjmp:
593 case ARM::Int_eh_sjlj_setjmp_nofp:
594 return 20;
595 case ARM::tInt_eh_sjlj_setjmp:
596 case ARM::t2Int_eh_sjlj_setjmp:
597 case ARM::t2Int_eh_sjlj_setjmp_nofp:
598 return 12;
599 case ARM::BR_JTr:
600 case ARM::BR_JTm:
601 case ARM::BR_JTadd:
602 case ARM::tBR_JTr:
603 case ARM::t2BR_JT:
604 case ARM::t2TBB_JT:
605 case ARM::t2TBH_JT: {
606 // These are jumptable branches, i.e. a branch followed by an inlined
607 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
608 // entry is one byte; TBH two byte each.
609 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
610 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
611 unsigned NumOps = MCID.getNumOperands();
612 MachineOperand JTOP =
613 MI->getOperand(NumOps - (MI->isPredicable() ? 3 : 2));
614 unsigned JTI = JTOP.getIndex();
615 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
616 assert(MJTI != 0);
617 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
618 assert(JTI < JT.size());
619 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
620 // 4 aligned. The assembler / linker may add 2 byte padding just before
621 // the JT entries. The size does not include this padding; the
622 // constant islands pass does separate bookkeeping for it.
623 // FIXME: If we know the size of the function is less than (1 << 16) *2
624 // bytes, we can use 16-bit entries instead. Then there won't be an
625 // alignment issue.
626 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
627 unsigned NumEntries = getNumJTEntries(JT, JTI);
628 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
629 // Make sure the instruction that follows TBB is 2-byte aligned.
630 // FIXME: Constant island pass should insert an "ALIGN" instruction
631 // instead.
632 ++NumEntries;
633 return NumEntries * EntrySize + InstSize;
634 }
635 default:
636 // Otherwise, pseudo-instruction sizes are zero.
637 return 0;
638 }
David Goodwin334c2642009-07-08 16:09:28 +0000639}
640
Evan Chengddfd1372011-12-14 02:11:42 +0000641unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr *MI) const {
642 unsigned Size = 0;
643 MachineBasicBlock::const_instr_iterator I = MI;
644 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
645 while (++I != E && I->isInsideBundle()) {
646 assert(!I->isBundle() && "No nested bundle!");
647 Size += GetInstSizeInBytes(&*I);
648 }
649 return Size;
650}
651
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000652void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
653 MachineBasicBlock::iterator I, DebugLoc DL,
654 unsigned DestReg, unsigned SrcReg,
655 bool KillSrc) const {
656 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
657 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson1665b0a2010-02-16 17:24:15 +0000658
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000659 if (GPRDest && GPRSrc) {
660 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
661 .addReg(SrcReg, getKillRegState(KillSrc))));
662 return;
David Goodwin7bfdca02009-08-05 21:02:22 +0000663 }
David Goodwin334c2642009-07-08 16:09:28 +0000664
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000665 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
666 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
667
Chad Rosiere5038e12011-08-20 00:17:25 +0000668 unsigned Opc = 0;
Jakob Stoklund Olesen142bd1a2011-10-11 00:59:06 +0000669 if (SPRDest && SPRSrc)
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000670 Opc = ARM::VMOVS;
Jakob Stoklund Olesen142bd1a2011-10-11 00:59:06 +0000671 else if (GPRDest && SPRSrc)
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000672 Opc = ARM::VMOVRS;
673 else if (SPRDest && GPRSrc)
674 Opc = ARM::VMOVSR;
675 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
676 Opc = ARM::VMOVD;
677 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
Owen Anderson43967a92011-07-15 18:46:47 +0000678 Opc = ARM::VORRq;
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000679
Chad Rosiere5038e12011-08-20 00:17:25 +0000680 if (Opc) {
681 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
Owen Anderson43967a92011-07-15 18:46:47 +0000682 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosiere5038e12011-08-20 00:17:25 +0000683 if (Opc == ARM::VORRq)
684 MIB.addReg(SrcReg, getKillRegState(KillSrc));
Chad Rosierfea95c62011-08-20 00:52:40 +0000685 AddDefaultPred(MIB);
Chad Rosiere5038e12011-08-20 00:17:25 +0000686 return;
687 }
688
Jakob Stoklund Olesen85bdf2e2012-03-29 21:10:40 +0000689 // Handle register classes that require multiple instructions.
690 unsigned BeginIdx = 0;
691 unsigned SubRegs = 0;
Andrew Trick7611a882012-08-29 04:41:37 +0000692 int Spacing = 1;
Jakob Stoklund Olesen85bdf2e2012-03-29 21:10:40 +0000693
694 // Use VORRq when possible.
695 if (ARM::QQPRRegClass.contains(DestReg, SrcReg))
696 Opc = ARM::VORRq, BeginIdx = ARM::qsub_0, SubRegs = 2;
697 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg))
698 Opc = ARM::VORRq, BeginIdx = ARM::qsub_0, SubRegs = 4;
699 // Fall back to VMOVD.
700 else if (ARM::DPairRegClass.contains(DestReg, SrcReg))
701 Opc = ARM::VMOVD, BeginIdx = ARM::dsub_0, SubRegs = 2;
702 else if (ARM::DTripleRegClass.contains(DestReg, SrcReg))
703 Opc = ARM::VMOVD, BeginIdx = ARM::dsub_0, SubRegs = 3;
704 else if (ARM::DQuadRegClass.contains(DestReg, SrcReg))
705 Opc = ARM::VMOVD, BeginIdx = ARM::dsub_0, SubRegs = 4;
Jakob Stoklund Olesencd275f52012-10-26 21:29:15 +0000706 else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg))
707 Opc = ARM::MOVr, BeginIdx = ARM::gsub_0, SubRegs = 2;
Jakob Stoklund Olesen85bdf2e2012-03-29 21:10:40 +0000708
709 else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg))
710 Opc = ARM::VMOVD, BeginIdx = ARM::dsub_0, SubRegs = 2, Spacing = 2;
711 else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg))
712 Opc = ARM::VMOVD, BeginIdx = ARM::dsub_0, SubRegs = 3, Spacing = 2;
713 else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg))
714 Opc = ARM::VMOVD, BeginIdx = ARM::dsub_0, SubRegs = 4, Spacing = 2;
715
Andrew Trick7611a882012-08-29 04:41:37 +0000716 assert(Opc && "Impossible reg-to-reg copy");
Jakob Stoklund Olesen85bdf2e2012-03-29 21:10:40 +0000717
Andrew Trickd79dedd2012-08-29 01:58:52 +0000718 const TargetRegisterInfo *TRI = &getRegisterInfo();
719 MachineInstrBuilder Mov;
Andrew Trickf26e43d2012-08-29 01:58:55 +0000720
721 // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
722 if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
723 BeginIdx = BeginIdx + ((SubRegs-1)*Spacing);
724 Spacing = -Spacing;
725 }
726#ifndef NDEBUG
727 SmallSet<unsigned, 4> DstRegs;
728#endif
Andrew Trickd79dedd2012-08-29 01:58:52 +0000729 for (unsigned i = 0; i != SubRegs; ++i) {
730 unsigned Dst = TRI->getSubReg(DestReg, BeginIdx + i*Spacing);
731 unsigned Src = TRI->getSubReg(SrcReg, BeginIdx + i*Spacing);
732 assert(Dst && Src && "Bad sub-register");
Andrew Trickf26e43d2012-08-29 01:58:55 +0000733#ifndef NDEBUG
Andrew Trickf26e43d2012-08-29 01:58:55 +0000734 assert(!DstRegs.count(Src) && "destructive vector copy");
Andrew Trick7611a882012-08-29 04:41:37 +0000735 DstRegs.insert(Dst);
Andrew Trickf26e43d2012-08-29 01:58:55 +0000736#endif
Andrew Trickd79dedd2012-08-29 01:58:52 +0000737 Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst)
738 .addReg(Src);
739 // VORR takes two source operands.
740 if (Opc == ARM::VORRq)
741 Mov.addReg(Src);
742 Mov = AddDefaultPred(Mov);
743 }
744 // Add implicit super-register defs and kills to the last instruction.
745 Mov->addRegisterDefined(DestReg, TRI);
746 if (KillSrc)
747 Mov->addRegisterKilled(SrcReg, TRI);
David Goodwin334c2642009-07-08 16:09:28 +0000748}
749
Evan Chengc10b5af2010-05-07 00:24:52 +0000750static const
751MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
752 unsigned Reg, unsigned SubIdx, unsigned State,
753 const TargetRegisterInfo *TRI) {
754 if (!SubIdx)
755 return MIB.addReg(Reg, State);
756
757 if (TargetRegisterInfo::isPhysicalRegister(Reg))
758 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
759 return MIB.addReg(Reg, State, SubIdx);
760}
761
David Goodwin334c2642009-07-08 16:09:28 +0000762void ARMBaseInstrInfo::
763storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
764 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000765 const TargetRegisterClass *RC,
766 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000767 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000768 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000769 MachineFunction &MF = *MBB.getParent();
770 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000771 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000772
773 MachineMemOperand *MMO =
Jay Foad978e0df2011-11-15 07:34:52 +0000774 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Chris Lattner59db5492010-09-21 04:39:43 +0000775 MachineMemOperand::MOStore,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000776 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000777 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000778
Owen Andersone66ef2d2011-08-10 17:21:20 +0000779 switch (RC->getSize()) {
780 case 4:
781 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
782 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwin334c2642009-07-08 16:09:28 +0000783 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000784 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Andersone66ef2d2011-08-10 17:21:20 +0000785 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
786 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
Evan Chengd31c5492010-05-06 01:34:11 +0000787 .addReg(SrcReg, getKillRegState(isKill))
788 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Andersone66ef2d2011-08-10 17:21:20 +0000789 } else
790 llvm_unreachable("Unknown reg class!");
791 break;
792 case 8:
793 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
794 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000795 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000796 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesencd275f52012-10-26 21:29:15 +0000797 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
798 MachineInstrBuilder MIB =
799 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STMIA))
800 .addFrameIndex(FI))
801 .addMemOperand(MMO);
802 MIB = AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
803 AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
Owen Andersone66ef2d2011-08-10 17:21:20 +0000804 } else
805 llvm_unreachable("Unknown reg class!");
806 break;
807 case 16:
Jakob Stoklund Olesen5b2f9132012-03-28 21:20:32 +0000808 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesen7255a4e2012-01-05 00:26:57 +0000809 // Use aligned spills if the stack can be realigned.
810 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbach28f08c92012-03-05 19:33:30 +0000811 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
Bob Wilsonf967ca02010-07-06 21:26:18 +0000812 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000813 .addReg(SrcReg, getKillRegState(isKill))
814 .addMemOperand(MMO));
Owen Andersone66ef2d2011-08-10 17:21:20 +0000815 } else {
816 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng69b9f982010-05-13 01:12:06 +0000817 .addReg(SrcReg, getKillRegState(isKill))
818 .addFrameIndex(FI)
Evan Cheng69b9f982010-05-13 01:12:06 +0000819 .addMemOperand(MMO));
Owen Andersone66ef2d2011-08-10 17:21:20 +0000820 }
821 } else
822 llvm_unreachable("Unknown reg class!");
823 break;
Anton Korobeynikovb58d7d02012-08-04 13:16:12 +0000824 case 24:
825 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
826 // Use aligned spills if the stack can be realigned.
827 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
828 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64TPseudo))
829 .addFrameIndex(FI).addImm(16)
830 .addReg(SrcReg, getKillRegState(isKill))
831 .addMemOperand(MMO));
832 } else {
833 MachineInstrBuilder MIB =
834 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
835 .addFrameIndex(FI))
836 .addMemOperand(MMO);
837 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
838 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
839 AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
840 }
841 } else
842 llvm_unreachable("Unknown reg class!");
843 break;
Owen Andersone66ef2d2011-08-10 17:21:20 +0000844 case 32:
Anton Korobeynikovb58d7d02012-08-04 13:16:12 +0000845 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Andersone66ef2d2011-08-10 17:21:20 +0000846 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
847 // FIXME: It's possible to only store part of the QQ register if the
848 // spilled def has a sub-register index.
849 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
Bob Wilson168f3822010-09-15 01:48:05 +0000850 .addFrameIndex(FI).addImm(16)
851 .addReg(SrcReg, getKillRegState(isKill))
852 .addMemOperand(MMO));
Owen Andersone66ef2d2011-08-10 17:21:20 +0000853 } else {
854 MachineInstrBuilder MIB =
855 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
Bill Wendling73fe34a2010-11-16 01:16:36 +0000856 .addFrameIndex(FI))
Owen Andersone66ef2d2011-08-10 17:21:20 +0000857 .addMemOperand(MMO);
858 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
859 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
860 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
861 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
862 }
863 } else
864 llvm_unreachable("Unknown reg class!");
865 break;
866 case 64:
867 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
868 MachineInstrBuilder MIB =
869 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
870 .addFrameIndex(FI))
871 .addMemOperand(MMO);
872 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
873 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
874 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
875 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
876 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
877 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
878 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
879 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
880 } else
881 llvm_unreachable("Unknown reg class!");
882 break;
883 default:
884 llvm_unreachable("Unknown reg class!");
David Goodwin334c2642009-07-08 16:09:28 +0000885 }
886}
887
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000888unsigned
889ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
890 int &FrameIndex) const {
891 switch (MI->getOpcode()) {
892 default: break;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000893 case ARM::STRrs:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000894 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
895 if (MI->getOperand(1).isFI() &&
896 MI->getOperand(2).isReg() &&
897 MI->getOperand(3).isImm() &&
898 MI->getOperand(2).getReg() == 0 &&
899 MI->getOperand(3).getImm() == 0) {
900 FrameIndex = MI->getOperand(1).getIndex();
901 return MI->getOperand(0).getReg();
902 }
903 break;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000904 case ARM::STRi12:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000905 case ARM::t2STRi12:
Jim Grosbach74472b42011-06-29 20:26:39 +0000906 case ARM::tSTRspi:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000907 case ARM::VSTRD:
908 case ARM::VSTRS:
909 if (MI->getOperand(1).isFI() &&
910 MI->getOperand(2).isImm() &&
911 MI->getOperand(2).getImm() == 0) {
912 FrameIndex = MI->getOperand(1).getIndex();
913 return MI->getOperand(0).getReg();
914 }
915 break;
Jim Grosbach28f08c92012-03-05 19:33:30 +0000916 case ARM::VST1q64:
Anton Korobeynikov161474d2012-08-04 13:22:14 +0000917 case ARM::VST1d64TPseudo:
918 case ARM::VST1d64QPseudo:
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000919 if (MI->getOperand(0).isFI() &&
920 MI->getOperand(2).getSubReg() == 0) {
921 FrameIndex = MI->getOperand(0).getIndex();
922 return MI->getOperand(2).getReg();
923 }
Jakob Stoklund Olesen31bbc512010-09-15 21:40:09 +0000924 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +0000925 case ARM::VSTMQIA:
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000926 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000927 MI->getOperand(0).getSubReg() == 0) {
928 FrameIndex = MI->getOperand(1).getIndex();
929 return MI->getOperand(0).getReg();
930 }
931 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000932 }
933
934 return 0;
935}
936
Jakob Stoklund Olesen36ee0e62011-08-08 21:45:32 +0000937unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
938 int &FrameIndex) const {
939 const MachineMemOperand *Dummy;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000940 return MI->mayStore() && hasStoreToStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesen36ee0e62011-08-08 21:45:32 +0000941}
942
David Goodwin334c2642009-07-08 16:09:28 +0000943void ARMBaseInstrInfo::
944loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
945 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000946 const TargetRegisterClass *RC,
947 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000948 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000949 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000950 MachineFunction &MF = *MBB.getParent();
Jakob Stoklund Olesencd275f52012-10-26 21:29:15 +0000951 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000952 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000953 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000954 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000955 MF.getMachineMemOperand(
Jay Foad978e0df2011-11-15 07:34:52 +0000956 MachinePointerInfo::getFixedStack(FI),
Chris Lattner59db5492010-09-21 04:39:43 +0000957 MachineMemOperand::MOLoad,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000958 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000959 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000960
Owen Andersone66ef2d2011-08-10 17:21:20 +0000961 switch (RC->getSize()) {
962 case 4:
963 if (ARM::GPRRegClass.hasSubClassEq(RC)) {
964 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
965 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilson0eb0c742010-02-16 22:01:59 +0000966
Owen Andersone66ef2d2011-08-10 17:21:20 +0000967 } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
968 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Jim Grosbach3e556122010-10-26 22:37:02 +0000969 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Owen Andersone66ef2d2011-08-10 17:21:20 +0000970 } else
971 llvm_unreachable("Unknown reg class!");
Bob Wilsonebe99b22010-06-18 21:32:42 +0000972 break;
Owen Andersone66ef2d2011-08-10 17:21:20 +0000973 case 8:
974 if (ARM::DPRRegClass.hasSubClassEq(RC)) {
975 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Evan Chengd31c5492010-05-06 01:34:11 +0000976 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jakob Stoklund Olesencd275f52012-10-26 21:29:15 +0000977 } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
978 unsigned LdmOpc = AFI->isThumbFunction() ? ARM::t2LDMIA : ARM::LDMIA;
979 MachineInstrBuilder MIB =
980 AddDefaultPred(BuildMI(MBB, I, DL, get(LdmOpc))
981 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
982 MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
983 MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
984 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
985 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Andersone66ef2d2011-08-10 17:21:20 +0000986 } else
987 llvm_unreachable("Unknown reg class!");
Bob Wilsonebe99b22010-06-18 21:32:42 +0000988 break;
Owen Andersone66ef2d2011-08-10 17:21:20 +0000989 case 16:
Jakob Stoklund Olesen5b2f9132012-03-28 21:20:32 +0000990 if (ARM::DPairRegClass.hasSubClassEq(RC)) {
Jakob Stoklund Olesen7255a4e2012-01-05 00:26:57 +0000991 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Jim Grosbach28f08c92012-03-05 19:33:30 +0000992 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
Bob Wilsonf967ca02010-07-06 21:26:18 +0000993 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000994 .addMemOperand(MMO));
Owen Andersone66ef2d2011-08-10 17:21:20 +0000995 } else {
996 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
997 .addFrameIndex(FI)
998 .addMemOperand(MMO));
999 }
1000 } else
1001 llvm_unreachable("Unknown reg class!");
Bob Wilsonebe99b22010-06-18 21:32:42 +00001002 break;
Anton Korobeynikovb58d7d02012-08-04 13:16:12 +00001003 case 24:
1004 if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
1005 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1006 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
1007 .addFrameIndex(FI).addImm(16)
1008 .addMemOperand(MMO));
1009 } else {
1010 MachineInstrBuilder MIB =
1011 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1012 .addFrameIndex(FI)
1013 .addMemOperand(MMO));
1014 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1015 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1016 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1017 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1018 MIB.addReg(DestReg, RegState::ImplicitDefine);
1019 }
1020 } else
1021 llvm_unreachable("Unknown reg class!");
1022 break;
1023 case 32:
1024 if (ARM::QQPRRegClass.hasSubClassEq(RC) || ARM::DQuadRegClass.hasSubClassEq(RC)) {
Owen Andersone66ef2d2011-08-10 17:21:20 +00001025 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
1026 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
Bob Wilson168f3822010-09-15 01:48:05 +00001027 .addFrameIndex(FI).addImm(16)
1028 .addMemOperand(MMO));
Owen Andersone66ef2d2011-08-10 17:21:20 +00001029 } else {
1030 MachineInstrBuilder MIB =
Bill Wendling73fe34a2010-11-16 01:16:36 +00001031 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1032 .addFrameIndex(FI))
Owen Andersone66ef2d2011-08-10 17:21:20 +00001033 .addMemOperand(MMO);
Jakob Stoklund Olesenfce711c2012-03-04 18:40:30 +00001034 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1035 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1036 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1037 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesen3247af22012-03-06 02:48:17 +00001038 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1039 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Andersone66ef2d2011-08-10 17:21:20 +00001040 }
1041 } else
1042 llvm_unreachable("Unknown reg class!");
1043 break;
1044 case 64:
1045 if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
1046 MachineInstrBuilder MIB =
1047 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
1048 .addFrameIndex(FI))
1049 .addMemOperand(MMO);
Jakob Stoklund Olesenfce711c2012-03-04 18:40:30 +00001050 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
1051 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
1052 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1053 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1054 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
1055 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
1056 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
1057 MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
Jakob Stoklund Olesen3247af22012-03-06 02:48:17 +00001058 if (TargetRegisterInfo::isPhysicalRegister(DestReg))
1059 MIB.addReg(DestReg, RegState::ImplicitDefine);
Owen Andersone66ef2d2011-08-10 17:21:20 +00001060 } else
1061 llvm_unreachable("Unknown reg class!");
Bob Wilsonebe99b22010-06-18 21:32:42 +00001062 break;
Bob Wilsonebe99b22010-06-18 21:32:42 +00001063 default:
1064 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +00001065 }
1066}
1067
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +00001068unsigned
1069ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
1070 int &FrameIndex) const {
1071 switch (MI->getOpcode()) {
1072 default: break;
Jim Grosbach3e556122010-10-26 22:37:02 +00001073 case ARM::LDRrs:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +00001074 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
1075 if (MI->getOperand(1).isFI() &&
1076 MI->getOperand(2).isReg() &&
1077 MI->getOperand(3).isImm() &&
1078 MI->getOperand(2).getReg() == 0 &&
1079 MI->getOperand(3).getImm() == 0) {
1080 FrameIndex = MI->getOperand(1).getIndex();
1081 return MI->getOperand(0).getReg();
1082 }
1083 break;
Jim Grosbach3e556122010-10-26 22:37:02 +00001084 case ARM::LDRi12:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +00001085 case ARM::t2LDRi12:
Jim Grosbach74472b42011-06-29 20:26:39 +00001086 case ARM::tLDRspi:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +00001087 case ARM::VLDRD:
1088 case ARM::VLDRS:
1089 if (MI->getOperand(1).isFI() &&
1090 MI->getOperand(2).isImm() &&
1091 MI->getOperand(2).getImm() == 0) {
1092 FrameIndex = MI->getOperand(1).getIndex();
1093 return MI->getOperand(0).getReg();
1094 }
1095 break;
Jim Grosbach28f08c92012-03-05 19:33:30 +00001096 case ARM::VLD1q64:
Anton Korobeynikov161474d2012-08-04 13:22:14 +00001097 case ARM::VLD1d64TPseudo:
1098 case ARM::VLD1d64QPseudo:
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +00001099 if (MI->getOperand(1).isFI() &&
1100 MI->getOperand(0).getSubReg() == 0) {
1101 FrameIndex = MI->getOperand(1).getIndex();
1102 return MI->getOperand(0).getReg();
1103 }
1104 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001105 case ARM::VLDMQIA:
Jakob Stoklund Olesen06f264e2010-09-15 21:40:11 +00001106 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen06f264e2010-09-15 21:40:11 +00001107 MI->getOperand(0).getSubReg() == 0) {
1108 FrameIndex = MI->getOperand(1).getIndex();
1109 return MI->getOperand(0).getReg();
1110 }
1111 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +00001112 }
1113
1114 return 0;
1115}
1116
Jakob Stoklund Olesen36ee0e62011-08-08 21:45:32 +00001117unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
1118 int &FrameIndex) const {
1119 const MachineMemOperand *Dummy;
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001120 return MI->mayLoad() && hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Jakob Stoklund Olesen36ee0e62011-08-08 21:45:32 +00001121}
1122
Jakob Stoklund Olesen142bd1a2011-10-11 00:59:06 +00001123bool ARMBaseInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const{
1124 // This hook gets to expand COPY instructions before they become
1125 // copyPhysReg() calls. Look for VMOVS instructions that can legally be
1126 // widened to VMOVD. We prefer the VMOVD when possible because it may be
1127 // changed into a VORR that can go down the NEON pipeline.
1128 if (!WidenVMOVS || !MI->isCopy())
1129 return false;
1130
1131 // Look for a copy between even S-registers. That is where we keep floats
1132 // when using NEON v2f32 instructions for f32 arithmetic.
1133 unsigned DstRegS = MI->getOperand(0).getReg();
1134 unsigned SrcRegS = MI->getOperand(1).getReg();
1135 if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
1136 return false;
1137
1138 const TargetRegisterInfo *TRI = &getRegisterInfo();
1139 unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
1140 &ARM::DPRRegClass);
1141 unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
1142 &ARM::DPRRegClass);
1143 if (!DstRegD || !SrcRegD)
1144 return false;
1145
1146 // We want to widen this into a DstRegD = VMOVD SrcRegD copy. This is only
1147 // legal if the COPY already defines the full DstRegD, and it isn't a
1148 // sub-register insertion.
1149 if (!MI->definesRegister(DstRegD, TRI) || MI->readsRegister(DstRegD, TRI))
1150 return false;
1151
Jakob Stoklund Olesen1c062c22011-10-12 00:06:23 +00001152 // A dead copy shouldn't show up here, but reject it just in case.
1153 if (MI->getOperand(0).isDead())
1154 return false;
1155
1156 // All clear, widen the COPY.
Jakob Stoklund Olesen142bd1a2011-10-11 00:59:06 +00001157 DEBUG(dbgs() << "widening: " << *MI);
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00001158 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Jakob Stoklund Olesen1c062c22011-10-12 00:06:23 +00001159
1160 // Get rid of the old <imp-def> of DstRegD. Leave it if it defines a Q-reg
1161 // or some other super-register.
1162 int ImpDefIdx = MI->findRegisterDefOperandIdx(DstRegD);
1163 if (ImpDefIdx != -1)
1164 MI->RemoveOperand(ImpDefIdx);
1165
1166 // Change the opcode and operands.
Jakob Stoklund Olesen142bd1a2011-10-11 00:59:06 +00001167 MI->setDesc(get(ARM::VMOVD));
1168 MI->getOperand(0).setReg(DstRegD);
1169 MI->getOperand(1).setReg(SrcRegD);
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00001170 AddDefaultPred(MIB);
Jakob Stoklund Olesen1c062c22011-10-12 00:06:23 +00001171
1172 // We are now reading SrcRegD instead of SrcRegS. This may upset the
1173 // register scavenger and machine verifier, so we need to indicate that we
1174 // are reading an undefined value from SrcRegD, but a proper value from
1175 // SrcRegS.
1176 MI->getOperand(1).setIsUndef();
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00001177 MIB.addReg(SrcRegS, RegState::Implicit);
Jakob Stoklund Olesen1c062c22011-10-12 00:06:23 +00001178
1179 // SrcRegD may actually contain an unrelated value in the ssub_1
1180 // sub-register. Don't kill it. Only kill the ssub_0 sub-register.
1181 if (MI->getOperand(1).isKill()) {
1182 MI->getOperand(1).setIsKill(false);
1183 MI->addRegisterKilled(SrcRegS, TRI, true);
1184 }
1185
Jakob Stoklund Olesen142bd1a2011-10-11 00:59:06 +00001186 DEBUG(dbgs() << "replaced by: " << *MI);
1187 return true;
1188}
1189
Evan Cheng62b50652010-04-26 07:39:25 +00001190MachineInstr*
1191ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +00001192 int FrameIx, uint64_t Offset,
Evan Cheng62b50652010-04-26 07:39:25 +00001193 const MDNode *MDPtr,
1194 DebugLoc DL) const {
1195 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
1196 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
1197 return &*MIB;
1198}
1199
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001200/// Create a copy of a const pool value. Update CPI to the new index and return
1201/// the label UID.
1202static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1203 MachineConstantPool *MCP = MF.getConstantPool();
1204 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1205
1206 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1207 assert(MCPE.isMachineConstantPoolEntry() &&
1208 "Expecting a machine constantpool entry!");
1209 ARMConstantPoolValue *ACPV =
1210 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1211
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001212 unsigned PCLabelId = AFI->createPICLabelUId();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001213 ARMConstantPoolValue *NewCPV = 0;
Jim Grosbach51f5b672010-09-10 21:38:22 +00001214 // FIXME: The below assumes PIC relocation model and that the function
1215 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
1216 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
1217 // instructions, so that's probably OK, but is PIC always correct when
1218 // we get here?
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001219 if (ACPV->isGlobalValue())
Bill Wendling5bb77992011-10-01 08:00:54 +00001220 NewCPV = ARMConstantPoolConstant::
1221 Create(cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId,
1222 ARMCP::CPValue, 4);
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001223 else if (ACPV->isExtSymbol())
Bill Wendlingfe31e672011-10-01 08:58:29 +00001224 NewCPV = ARMConstantPoolSymbol::
1225 Create(MF.getFunction()->getContext(),
1226 cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001227 else if (ACPV->isBlockAddress())
Bill Wendling5bb77992011-10-01 08:00:54 +00001228 NewCPV = ARMConstantPoolConstant::
1229 Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
1230 ARMCP::CPBlockAddress, 4);
Jim Grosbach51f5b672010-09-10 21:38:22 +00001231 else if (ACPV->isLSDA())
Bill Wendling5bb77992011-10-01 08:00:54 +00001232 NewCPV = ARMConstantPoolConstant::Create(MF.getFunction(), PCLabelId,
1233 ARMCP::CPLSDA, 4);
Bill Wendlinge00897c2011-09-29 23:50:42 +00001234 else if (ACPV->isMachineBasicBlock())
Bill Wendling3320f2a2011-10-01 09:30:42 +00001235 NewCPV = ARMConstantPoolMBB::
1236 Create(MF.getFunction()->getContext(),
1237 cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001238 else
1239 llvm_unreachable("Unexpected ARM constantpool value type!!");
1240 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1241 return PCLabelId;
1242}
1243
Evan Chengfdc83402009-11-08 00:15:23 +00001244void ARMBaseInstrInfo::
1245reMaterialize(MachineBasicBlock &MBB,
1246 MachineBasicBlock::iterator I,
1247 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +00001248 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001249 const TargetRegisterInfo &TRI) const {
Evan Chengfdc83402009-11-08 00:15:23 +00001250 unsigned Opcode = Orig->getOpcode();
1251 switch (Opcode) {
1252 default: {
1253 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001254 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfdc83402009-11-08 00:15:23 +00001255 MBB.insert(I, MI);
1256 break;
1257 }
1258 case ARM::tLDRpci_pic:
1259 case ARM::t2LDRpci_pic: {
1260 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +00001261 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001262 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +00001263 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1264 DestReg)
1265 .addConstantPoolIndex(CPI).addImm(PCLabelId);
Chris Lattnerd7d030a2011-04-29 05:24:29 +00001266 MIB->setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
Evan Chengfdc83402009-11-08 00:15:23 +00001267 break;
1268 }
1269 }
Evan Chengfdc83402009-11-08 00:15:23 +00001270}
1271
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001272MachineInstr *
1273ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +00001274 MachineInstr *MI = TargetInstrInfo::duplicate(Orig, MF);
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001275 switch(Orig->getOpcode()) {
1276 case ARM::tLDRpci_pic:
1277 case ARM::t2LDRpci_pic: {
1278 unsigned CPI = Orig->getOperand(1).getIndex();
1279 unsigned PCLabelId = duplicateCPV(MF, CPI);
1280 Orig->getOperand(1).setIndex(CPI);
1281 Orig->getOperand(2).setImm(PCLabelId);
1282 break;
1283 }
1284 }
1285 return MI;
1286}
1287
Evan Cheng506049f2010-03-03 01:44:33 +00001288bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Cheng9fe20092011-01-20 08:34:58 +00001289 const MachineInstr *MI1,
1290 const MachineRegisterInfo *MRI) const {
Evan Chengd457e6e2009-11-07 04:04:34 +00001291 int Opcode = MI0->getOpcode();
Evan Chengd7e3cc82011-01-20 23:55:07 +00001292 if (Opcode == ARM::t2LDRpci ||
Evan Cheng9b824252009-11-20 02:10:27 +00001293 Opcode == ARM::t2LDRpci_pic ||
1294 Opcode == ARM::tLDRpci ||
Evan Cheng9fe20092011-01-20 08:34:58 +00001295 Opcode == ARM::tLDRpci_pic ||
Evan Cheng53519f02011-01-21 18:55:51 +00001296 Opcode == ARM::MOV_ga_dyn ||
1297 Opcode == ARM::MOV_ga_pcrel ||
1298 Opcode == ARM::MOV_ga_pcrel_ldr ||
1299 Opcode == ARM::t2MOV_ga_dyn ||
1300 Opcode == ARM::t2MOV_ga_pcrel) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001301 if (MI1->getOpcode() != Opcode)
1302 return false;
1303 if (MI0->getNumOperands() != MI1->getNumOperands())
1304 return false;
1305
1306 const MachineOperand &MO0 = MI0->getOperand(1);
1307 const MachineOperand &MO1 = MI1->getOperand(1);
1308 if (MO0.getOffset() != MO1.getOffset())
1309 return false;
1310
Evan Cheng53519f02011-01-21 18:55:51 +00001311 if (Opcode == ARM::MOV_ga_dyn ||
1312 Opcode == ARM::MOV_ga_pcrel ||
1313 Opcode == ARM::MOV_ga_pcrel_ldr ||
1314 Opcode == ARM::t2MOV_ga_dyn ||
1315 Opcode == ARM::t2MOV_ga_pcrel)
Evan Cheng9fe20092011-01-20 08:34:58 +00001316 // Ignore the PC labels.
1317 return MO0.getGlobal() == MO1.getGlobal();
1318
Evan Chengd457e6e2009-11-07 04:04:34 +00001319 const MachineFunction *MF = MI0->getParent()->getParent();
1320 const MachineConstantPool *MCP = MF->getConstantPool();
1321 int CPI0 = MO0.getIndex();
1322 int CPI1 = MO1.getIndex();
1323 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1324 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
Evan Chengd7006172011-03-24 06:20:03 +00001325 bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
1326 bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
1327 if (isARMCP0 && isARMCP1) {
1328 ARMConstantPoolValue *ACPV0 =
1329 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1330 ARMConstantPoolValue *ACPV1 =
1331 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1332 return ACPV0->hasSameValue(ACPV1);
1333 } else if (!isARMCP0 && !isARMCP1) {
1334 return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
1335 }
1336 return false;
Evan Cheng9fe20092011-01-20 08:34:58 +00001337 } else if (Opcode == ARM::PICLDR) {
1338 if (MI1->getOpcode() != Opcode)
1339 return false;
1340 if (MI0->getNumOperands() != MI1->getNumOperands())
1341 return false;
1342
1343 unsigned Addr0 = MI0->getOperand(1).getReg();
1344 unsigned Addr1 = MI1->getOperand(1).getReg();
1345 if (Addr0 != Addr1) {
1346 if (!MRI ||
1347 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1348 !TargetRegisterInfo::isVirtualRegister(Addr1))
1349 return false;
1350
1351 // This assumes SSA form.
1352 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1353 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1354 // Check if the loaded value, e.g. a constantpool of a global address, are
1355 // the same.
1356 if (!produceSameValue(Def0, Def1, MRI))
1357 return false;
1358 }
1359
1360 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1361 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1362 const MachineOperand &MO0 = MI0->getOperand(i);
1363 const MachineOperand &MO1 = MI1->getOperand(i);
1364 if (!MO0.isIdenticalTo(MO1))
1365 return false;
1366 }
1367 return true;
Evan Chengd457e6e2009-11-07 04:04:34 +00001368 }
1369
Evan Cheng506049f2010-03-03 01:44:33 +00001370 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001371}
1372
Bill Wendling4b722102010-06-23 23:00:16 +00001373/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1374/// determine if two loads are loading from the same base address. It should
1375/// only return true if the base pointers are the same and the only differences
1376/// between the two addresses is the offset. It also returns the offsets by
1377/// reference.
Andrew Trick9b5caaa2012-11-12 19:40:10 +00001378///
1379/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1380/// is permanently disabled.
Bill Wendling4b722102010-06-23 23:00:16 +00001381bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1382 int64_t &Offset1,
1383 int64_t &Offset2) const {
1384 // Don't worry about Thumb: just ARM and Thumb2.
1385 if (Subtarget.isThumb1Only()) return false;
1386
1387 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1388 return false;
1389
1390 switch (Load1->getMachineOpcode()) {
1391 default:
1392 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001393 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001394 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001395 case ARM::LDRD:
1396 case ARM::LDRH:
1397 case ARM::LDRSB:
1398 case ARM::LDRSH:
1399 case ARM::VLDRD:
1400 case ARM::VLDRS:
1401 case ARM::t2LDRi8:
1402 case ARM::t2LDRDi8:
1403 case ARM::t2LDRSHi8:
1404 case ARM::t2LDRi12:
1405 case ARM::t2LDRSHi12:
1406 break;
1407 }
1408
1409 switch (Load2->getMachineOpcode()) {
1410 default:
1411 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001412 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001413 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001414 case ARM::LDRD:
1415 case ARM::LDRH:
1416 case ARM::LDRSB:
1417 case ARM::LDRSH:
1418 case ARM::VLDRD:
1419 case ARM::VLDRS:
1420 case ARM::t2LDRi8:
Bill Wendling4b722102010-06-23 23:00:16 +00001421 case ARM::t2LDRSHi8:
1422 case ARM::t2LDRi12:
1423 case ARM::t2LDRSHi12:
1424 break;
1425 }
1426
1427 // Check if base addresses and chain operands match.
1428 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1429 Load1->getOperand(4) != Load2->getOperand(4))
1430 return false;
1431
1432 // Index should be Reg0.
1433 if (Load1->getOperand(3) != Load2->getOperand(3))
1434 return false;
1435
1436 // Determine the offsets.
1437 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1438 isa<ConstantSDNode>(Load2->getOperand(1))) {
1439 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1440 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1441 return true;
1442 }
1443
1444 return false;
1445}
1446
1447/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001448/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
Bill Wendling4b722102010-06-23 23:00:16 +00001449/// be scheduled togther. On some targets if two loads are loading from
1450/// addresses in the same cache line, it's better if they are scheduled
1451/// together. This function takes two integers that represent the load offsets
1452/// from the common base address. It returns true if it decides it's desirable
1453/// to schedule the two loads together. "NumLoads" is the number of loads that
1454/// have already been scheduled after Load1.
Andrew Trick9b5caaa2012-11-12 19:40:10 +00001455///
1456/// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
1457/// is permanently disabled.
Bill Wendling4b722102010-06-23 23:00:16 +00001458bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1459 int64_t Offset1, int64_t Offset2,
1460 unsigned NumLoads) const {
1461 // Don't worry about Thumb: just ARM and Thumb2.
1462 if (Subtarget.isThumb1Only()) return false;
1463
1464 assert(Offset2 > Offset1);
1465
1466 if ((Offset2 - Offset1) / 8 > 64)
1467 return false;
1468
1469 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1470 return false; // FIXME: overly conservative?
1471
1472 // Four loads in a row should be sufficient.
1473 if (NumLoads >= 3)
1474 return false;
1475
1476 return true;
1477}
1478
Evan Cheng86050dc2010-06-18 23:09:54 +00001479bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1480 const MachineBasicBlock *MBB,
1481 const MachineFunction &MF) const {
Jim Grosbach57bb3942010-06-25 18:43:14 +00001482 // Debug info is never a scheduling boundary. It's necessary to be explicit
1483 // due to the special treatment of IT instructions below, otherwise a
1484 // dbg_value followed by an IT will result in the IT instruction being
1485 // considered a scheduling hazard, which is wrong. It should be the actual
1486 // instruction preceding the dbg_value instruction(s), just like it is
1487 // when debug info is not present.
1488 if (MI->isDebugValue())
1489 return false;
1490
Evan Cheng86050dc2010-06-18 23:09:54 +00001491 // Terminators and labels can't be scheduled around.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001492 if (MI->isTerminator() || MI->isLabel())
Evan Cheng86050dc2010-06-18 23:09:54 +00001493 return true;
1494
1495 // Treat the start of the IT block as a scheduling boundary, but schedule
1496 // t2IT along with all instructions following it.
1497 // FIXME: This is a big hammer. But the alternative is to add all potential
1498 // true and anti dependencies to IT block instructions as implicit operands
1499 // to the t2IT instruction. The added compile time and complexity does not
1500 // seem worth it.
1501 MachineBasicBlock::const_iterator I = MI;
Jim Grosbach57bb3942010-06-25 18:43:14 +00001502 // Make sure to skip any dbg_value instructions
1503 while (++I != MBB->end() && I->isDebugValue())
1504 ;
1505 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng86050dc2010-06-18 23:09:54 +00001506 return true;
1507
1508 // Don't attempt to schedule around any instruction that defines
1509 // a stack-oriented pointer, as it's unlikely to be profitable. This
1510 // saves compile time, because it doesn't require every single
1511 // stack slot reference to depend on the instruction that does the
1512 // modification.
Jakob Stoklund Olesena1aa8db2012-02-21 23:47:43 +00001513 // Calls don't actually change the stack pointer, even if they have imp-defs.
Jakob Stoklund Olesen209600b2012-02-22 01:07:19 +00001514 // No ARM calling conventions change the stack pointer. (X86 calling
1515 // conventions sometimes do).
Jakob Stoklund Olesena1aa8db2012-02-21 23:47:43 +00001516 if (!MI->isCall() && MI->definesRegister(ARM::SP))
Evan Cheng86050dc2010-06-18 23:09:54 +00001517 return true;
1518
1519 return false;
1520}
1521
Jakub Staszakf81b7f62011-07-10 02:58:07 +00001522bool ARMBaseInstrInfo::
1523isProfitableToIfCvt(MachineBasicBlock &MBB,
1524 unsigned NumCycles, unsigned ExtraPredCycles,
1525 const BranchProbability &Probability) const {
Cameron Zwarich5876db72011-04-13 06:39:16 +00001526 if (!NumCycles)
Evan Cheng13151432010-06-25 22:42:03 +00001527 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001528
Owen Andersonb20b8512010-09-28 18:32:13 +00001529 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszakf81b7f62011-07-10 02:58:07 +00001530 unsigned UnpredCost = Probability.getNumerator() * NumCycles;
1531 UnpredCost /= Probability.getDenominator();
1532 UnpredCost += 1; // The branch itself
1533 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001534
Jakub Staszakf81b7f62011-07-10 02:58:07 +00001535 return (NumCycles + ExtraPredCycles) <= UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001536}
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001537
Evan Cheng13151432010-06-25 22:42:03 +00001538bool ARMBaseInstrInfo::
Evan Cheng8239daf2010-11-03 00:45:17 +00001539isProfitableToIfCvt(MachineBasicBlock &TMBB,
1540 unsigned TCycles, unsigned TExtra,
1541 MachineBasicBlock &FMBB,
1542 unsigned FCycles, unsigned FExtra,
Jakub Staszakf81b7f62011-07-10 02:58:07 +00001543 const BranchProbability &Probability) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00001544 if (!TCycles || !FCycles)
Owen Andersonb20b8512010-09-28 18:32:13 +00001545 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001546
Owen Andersonb20b8512010-09-28 18:32:13 +00001547 // Attempt to estimate the relative costs of predication versus branching.
Jakub Staszakf81b7f62011-07-10 02:58:07 +00001548 unsigned TUnpredCost = Probability.getNumerator() * TCycles;
1549 TUnpredCost /= Probability.getDenominator();
Andrew Tricke23dc9c2011-09-21 02:17:37 +00001550
Jakub Staszakf81b7f62011-07-10 02:58:07 +00001551 uint32_t Comp = Probability.getDenominator() - Probability.getNumerator();
1552 unsigned FUnpredCost = Comp * FCycles;
1553 FUnpredCost /= Probability.getDenominator();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001554
Jakub Staszakf81b7f62011-07-10 02:58:07 +00001555 unsigned UnpredCost = TUnpredCost + FUnpredCost;
1556 UnpredCost += 1; // The branch itself
1557 UnpredCost += Subtarget.getMispredictionPenalty() / 10;
1558
1559 return (TCycles + FCycles + TExtra + FExtra) <= UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001560}
1561
Bob Wilsoneb1641d2012-09-29 21:43:49 +00001562bool
1563ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
1564 MachineBasicBlock &FMBB) const {
1565 // Reduce false anti-dependencies to let Swift's out-of-order execution
1566 // engine do its thing.
1567 return Subtarget.isSwift();
1568}
1569
Evan Cheng8fb90362009-08-08 03:20:32 +00001570/// getInstrPredicate - If instruction is predicated, returns its predicate
1571/// condition, otherwise returns AL. It also returns the condition code
1572/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001573ARMCC::CondCodes
1574llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001575 int PIdx = MI->findFirstPredOperandIdx();
1576 if (PIdx == -1) {
1577 PredReg = 0;
1578 return ARMCC::AL;
1579 }
1580
1581 PredReg = MI->getOperand(PIdx+1).getReg();
1582 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1583}
1584
1585
Evan Cheng6495f632009-07-28 05:48:47 +00001586int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001587 if (Opc == ARM::B)
1588 return ARM::Bcc;
David Blaikie4d6ccb52012-01-20 21:51:11 +00001589 if (Opc == ARM::tB)
Evan Cheng5ca53a72009-07-27 18:20:05 +00001590 return ARM::tBcc;
David Blaikie4d6ccb52012-01-20 21:51:11 +00001591 if (Opc == ARM::t2B)
1592 return ARM::t2Bcc;
Evan Cheng5ca53a72009-07-27 18:20:05 +00001593
1594 llvm_unreachable("Unknown unconditional branch opcode!");
Evan Cheng5ca53a72009-07-27 18:20:05 +00001595}
1596
Jakob Stoklund Olesenc5041ca2012-04-04 18:23:42 +00001597/// commuteInstruction - Handle commutable instructions.
1598MachineInstr *
1599ARMBaseInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
1600 switch (MI->getOpcode()) {
1601 case ARM::MOVCCr:
1602 case ARM::t2MOVCCr: {
1603 // MOVCC can be commuted by inverting the condition.
1604 unsigned PredReg = 0;
1605 ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
1606 // MOVCC AL can't be inverted. Shouldn't happen.
1607 if (CC == ARMCC::AL || PredReg != ARM::CPSR)
1608 return NULL;
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +00001609 MI = TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesenc5041ca2012-04-04 18:23:42 +00001610 if (!MI)
1611 return NULL;
1612 // After swapping the MOVCC operands, also invert the condition.
1613 MI->getOperand(MI->findFirstPredOperandIdx())
1614 .setImm(ARMCC::getOppositeCondition(CC));
1615 return MI;
1616 }
1617 }
Jakob Stoklund Olesena9fa4fd2012-11-28 02:35:17 +00001618 return TargetInstrInfo::commuteInstruction(MI, NewMI);
Jakob Stoklund Olesenc5041ca2012-04-04 18:23:42 +00001619}
Evan Cheng6495f632009-07-28 05:48:47 +00001620
Jakob Stoklund Olesen2860b7e2012-08-15 22:16:39 +00001621/// Identify instructions that can be folded into a MOVCC instruction, and
Jakob Stoklund Olesen098c6a52012-09-05 23:58:02 +00001622/// return the defining instruction.
1623static MachineInstr *canFoldIntoMOVCC(unsigned Reg,
1624 const MachineRegisterInfo &MRI,
1625 const TargetInstrInfo *TII) {
Jakob Stoklund Olesen2860b7e2012-08-15 22:16:39 +00001626 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1627 return 0;
1628 if (!MRI.hasOneNonDBGUse(Reg))
1629 return 0;
Jakob Stoklund Olesen098c6a52012-09-05 23:58:02 +00001630 MachineInstr *MI = MRI.getVRegDef(Reg);
Jakob Stoklund Olesen2860b7e2012-08-15 22:16:39 +00001631 if (!MI)
1632 return 0;
Jakob Stoklund Olesen098c6a52012-09-05 23:58:02 +00001633 // MI is folded into the MOVCC by predicating it.
1634 if (!MI->isPredicable())
1635 return 0;
Jakob Stoklund Olesen2860b7e2012-08-15 22:16:39 +00001636 // Check if MI has any non-dead defs or physreg uses. This also detects
1637 // predicated instructions which will be reading CPSR.
1638 for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
1639 const MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesena7fb3f62012-08-17 20:55:34 +00001640 // Reject frame index operands, PEI can't handle the predicated pseudos.
1641 if (MO.isFI() || MO.isCPI() || MO.isJTI())
1642 return 0;
Jakob Stoklund Olesen2860b7e2012-08-15 22:16:39 +00001643 if (!MO.isReg())
1644 continue;
Jakob Stoklund Olesen098c6a52012-09-05 23:58:02 +00001645 // MI can't have any tied operands, that would conflict with predication.
1646 if (MO.isTied())
1647 return 0;
Jakob Stoklund Olesen2860b7e2012-08-15 22:16:39 +00001648 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1649 return 0;
1650 if (MO.isDef() && !MO.isDead())
1651 return 0;
1652 }
Jakob Stoklund Olesen098c6a52012-09-05 23:58:02 +00001653 bool DontMoveAcrossStores = true;
1654 if (!MI->isSafeToMove(TII, /* AliasAnalysis = */ 0, DontMoveAcrossStores))
1655 return 0;
1656 return MI;
Jakob Stoklund Olesen2860b7e2012-08-15 22:16:39 +00001657}
1658
Jakob Stoklund Olesen053b5b02012-08-16 23:14:20 +00001659bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr *MI,
1660 SmallVectorImpl<MachineOperand> &Cond,
1661 unsigned &TrueOp, unsigned &FalseOp,
1662 bool &Optimizable) const {
1663 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1664 "Unknown select instruction");
1665 // MOVCC operands:
1666 // 0: Def.
1667 // 1: True use.
1668 // 2: False use.
1669 // 3: Condition code.
1670 // 4: CPSR use.
1671 TrueOp = 1;
1672 FalseOp = 2;
1673 Cond.push_back(MI->getOperand(3));
1674 Cond.push_back(MI->getOperand(4));
1675 // We can always fold a def.
1676 Optimizable = true;
1677 return false;
1678}
1679
1680MachineInstr *ARMBaseInstrInfo::optimizeSelect(MachineInstr *MI,
1681 bool PreferFalse) const {
1682 assert((MI->getOpcode() == ARM::MOVCCr || MI->getOpcode() == ARM::t2MOVCCr) &&
1683 "Unknown select instruction");
1684 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Jakob Stoklund Olesen098c6a52012-09-05 23:58:02 +00001685 MachineInstr *DefMI = canFoldIntoMOVCC(MI->getOperand(2).getReg(), MRI, this);
1686 bool Invert = !DefMI;
1687 if (!DefMI)
1688 DefMI = canFoldIntoMOVCC(MI->getOperand(1).getReg(), MRI, this);
1689 if (!DefMI)
Jakob Stoklund Olesen053b5b02012-08-16 23:14:20 +00001690 return 0;
1691
1692 // Create a new predicated version of DefMI.
1693 // Rfalse is the first use.
1694 MachineInstrBuilder NewMI = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
Jakob Stoklund Olesen098c6a52012-09-05 23:58:02 +00001695 DefMI->getDesc(),
1696 MI->getOperand(0).getReg());
Jakob Stoklund Olesen053b5b02012-08-16 23:14:20 +00001697
1698 // Copy all the DefMI operands, excluding its (null) predicate.
1699 const MCInstrDesc &DefDesc = DefMI->getDesc();
1700 for (unsigned i = 1, e = DefDesc.getNumOperands();
1701 i != e && !DefDesc.OpInfo[i].isPredicate(); ++i)
1702 NewMI.addOperand(DefMI->getOperand(i));
1703
1704 unsigned CondCode = MI->getOperand(3).getImm();
1705 if (Invert)
1706 NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
1707 else
1708 NewMI.addImm(CondCode);
1709 NewMI.addOperand(MI->getOperand(4));
1710
1711 // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
1712 if (NewMI->hasOptionalDef())
1713 AddDefaultCC(NewMI);
1714
Jakob Stoklund Olesen098c6a52012-09-05 23:58:02 +00001715 // The output register value when the predicate is false is an implicit
1716 // register operand tied to the first def.
1717 // The tie makes the register allocator ensure the FalseReg is allocated the
1718 // same register as operand 0.
1719 MachineOperand FalseReg = MI->getOperand(Invert ? 2 : 1);
1720 FalseReg.setImplicit();
Jakob Stoklund Olesenb9efafe2012-12-20 22:53:55 +00001721 NewMI.addOperand(FalseReg);
Jakob Stoklund Olesen098c6a52012-09-05 23:58:02 +00001722 NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
1723
Jakob Stoklund Olesen053b5b02012-08-16 23:14:20 +00001724 // The caller will erase MI, but not DefMI.
1725 DefMI->eraseFromParent();
1726 return NewMI;
1727}
1728
Andrew Trick3be654f2011-09-21 02:20:46 +00001729/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
1730/// instruction is encoded with an 'S' bit is determined by the optional CPSR
1731/// def operand.
1732///
1733/// This will go away once we can teach tblgen how to set the optional CPSR def
1734/// operand itself.
1735struct AddSubFlagsOpcodePair {
Craig Toppercd2859e2012-05-24 03:59:11 +00001736 uint16_t PseudoOpc;
1737 uint16_t MachineOpc;
Andrew Trick3be654f2011-09-21 02:20:46 +00001738};
1739
Craig Toppercd2859e2012-05-24 03:59:11 +00001740static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
Andrew Trick3be654f2011-09-21 02:20:46 +00001741 {ARM::ADDSri, ARM::ADDri},
1742 {ARM::ADDSrr, ARM::ADDrr},
1743 {ARM::ADDSrsi, ARM::ADDrsi},
1744 {ARM::ADDSrsr, ARM::ADDrsr},
1745
1746 {ARM::SUBSri, ARM::SUBri},
1747 {ARM::SUBSrr, ARM::SUBrr},
1748 {ARM::SUBSrsi, ARM::SUBrsi},
1749 {ARM::SUBSrsr, ARM::SUBrsr},
1750
1751 {ARM::RSBSri, ARM::RSBri},
Andrew Trick3be654f2011-09-21 02:20:46 +00001752 {ARM::RSBSrsi, ARM::RSBrsi},
1753 {ARM::RSBSrsr, ARM::RSBrsr},
1754
1755 {ARM::t2ADDSri, ARM::t2ADDri},
1756 {ARM::t2ADDSrr, ARM::t2ADDrr},
1757 {ARM::t2ADDSrs, ARM::t2ADDrs},
1758
1759 {ARM::t2SUBSri, ARM::t2SUBri},
1760 {ARM::t2SUBSrr, ARM::t2SUBrr},
1761 {ARM::t2SUBSrs, ARM::t2SUBrs},
1762
1763 {ARM::t2RSBSri, ARM::t2RSBri},
1764 {ARM::t2RSBSrs, ARM::t2RSBrs},
1765};
1766
1767unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
Craig Toppercd2859e2012-05-24 03:59:11 +00001768 for (unsigned i = 0, e = array_lengthof(AddSubFlagsOpcodeMap); i != e; ++i)
1769 if (OldOpc == AddSubFlagsOpcodeMap[i].PseudoOpc)
1770 return AddSubFlagsOpcodeMap[i].MachineOpc;
Andrew Trick3be654f2011-09-21 02:20:46 +00001771 return 0;
1772}
1773
Evan Cheng6495f632009-07-28 05:48:47 +00001774void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1775 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1776 unsigned DestReg, unsigned BaseReg, int NumBytes,
1777 ARMCC::CondCodes Pred, unsigned PredReg,
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001778 const ARMBaseInstrInfo &TII, unsigned MIFlags) {
Evan Cheng6495f632009-07-28 05:48:47 +00001779 bool isSub = NumBytes < 0;
1780 if (isSub) NumBytes = -NumBytes;
1781
1782 while (NumBytes) {
1783 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1784 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1785 assert(ThisVal && "Didn't extract field correctly");
1786
1787 // We will handle these bits from offset, clear them.
1788 NumBytes &= ~ThisVal;
1789
1790 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1791
1792 // Build the new ADD / SUB.
1793 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1794 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1795 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001796 .addImm((unsigned)Pred).addReg(PredReg).addReg(0)
1797 .setMIFlags(MIFlags);
Evan Cheng6495f632009-07-28 05:48:47 +00001798 BaseReg = DestReg;
1799 }
1800}
1801
Evan Chengcdbb3f52009-08-27 01:23:50 +00001802bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1803 unsigned FrameReg, int &Offset,
1804 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001805 unsigned Opcode = MI.getOpcode();
Evan Chenge837dea2011-06-28 19:10:37 +00001806 const MCInstrDesc &Desc = MI.getDesc();
Evan Cheng6495f632009-07-28 05:48:47 +00001807 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1808 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001809
Evan Cheng6495f632009-07-28 05:48:47 +00001810 // Memory operands in inline assembly always use AddrMode2.
1811 if (Opcode == ARM::INLINEASM)
1812 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001813
Evan Cheng6495f632009-07-28 05:48:47 +00001814 if (Opcode == ARM::ADDri) {
1815 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1816 if (Offset == 0) {
1817 // Turn it into a move.
1818 MI.setDesc(TII.get(ARM::MOVr));
1819 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1820 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001821 Offset = 0;
1822 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001823 } else if (Offset < 0) {
1824 Offset = -Offset;
1825 isSub = true;
1826 MI.setDesc(TII.get(ARM::SUBri));
1827 }
1828
1829 // Common case: small offset, fits into instruction.
1830 if (ARM_AM::getSOImmVal(Offset) != -1) {
1831 // Replace the FrameIndex with sp / fp
1832 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1833 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001834 Offset = 0;
1835 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001836 }
1837
1838 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1839 // as possible.
1840 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1841 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1842
1843 // We will handle these bits from offset, clear them.
1844 Offset &= ~ThisImmVal;
1845
1846 // Get the properly encoded SOImmVal field.
1847 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1848 "Bit extraction didn't work?");
1849 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1850 } else {
1851 unsigned ImmIdx = 0;
1852 int InstrOffs = 0;
1853 unsigned NumBits = 0;
1854 unsigned Scale = 1;
1855 switch (AddrMode) {
Jim Grosbach3e556122010-10-26 22:37:02 +00001856 case ARMII::AddrMode_i12: {
1857 ImmIdx = FrameRegIdx + 1;
1858 InstrOffs = MI.getOperand(ImmIdx).getImm();
1859 NumBits = 12;
1860 break;
1861 }
Evan Cheng6495f632009-07-28 05:48:47 +00001862 case ARMII::AddrMode2: {
1863 ImmIdx = FrameRegIdx+2;
1864 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1865 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1866 InstrOffs *= -1;
1867 NumBits = 12;
1868 break;
1869 }
1870 case ARMII::AddrMode3: {
1871 ImmIdx = FrameRegIdx+2;
1872 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1873 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1874 InstrOffs *= -1;
1875 NumBits = 8;
1876 break;
1877 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001878 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001879 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001880 // Can't fold any offset even if it's zero.
1881 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001882 case ARMII::AddrMode5: {
1883 ImmIdx = FrameRegIdx+1;
1884 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1885 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1886 InstrOffs *= -1;
1887 NumBits = 8;
1888 Scale = 4;
1889 break;
1890 }
1891 default:
1892 llvm_unreachable("Unsupported addressing mode!");
Evan Cheng6495f632009-07-28 05:48:47 +00001893 }
1894
1895 Offset += InstrOffs * Scale;
1896 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1897 if (Offset < 0) {
1898 Offset = -Offset;
1899 isSub = true;
1900 }
1901
1902 // Attempt to fold address comp. if opcode has offset bits
1903 if (NumBits > 0) {
1904 // Common case: small offset, fits into instruction.
1905 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1906 int ImmedOffset = Offset / Scale;
1907 unsigned Mask = (1 << NumBits) - 1;
1908 if ((unsigned)Offset <= Mask * Scale) {
1909 // Replace the FrameIndex with sp
1910 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach77aee8e2010-10-27 01:19:41 +00001911 // FIXME: When addrmode2 goes away, this will simplify (like the
1912 // T2 version), as the LDR.i12 versions don't need the encoding
1913 // tricks for the offset value.
1914 if (isSub) {
1915 if (AddrMode == ARMII::AddrMode_i12)
1916 ImmedOffset = -ImmedOffset;
1917 else
1918 ImmedOffset |= 1 << NumBits;
1919 }
Evan Cheng6495f632009-07-28 05:48:47 +00001920 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001921 Offset = 0;
1922 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001923 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001924
Evan Cheng6495f632009-07-28 05:48:47 +00001925 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1926 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach063efbf2010-10-27 16:50:31 +00001927 if (isSub) {
1928 if (AddrMode == ARMII::AddrMode_i12)
1929 ImmedOffset = -ImmedOffset;
1930 else
1931 ImmedOffset |= 1 << NumBits;
1932 }
Evan Cheng6495f632009-07-28 05:48:47 +00001933 ImmOp.ChangeToImmediate(ImmedOffset);
1934 Offset &= ~(Mask*Scale);
1935 }
1936 }
1937
Evan Chengcdbb3f52009-08-27 01:23:50 +00001938 Offset = (isSub) ? -Offset : Offset;
1939 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001940}
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001941
Manman Rende7266c2012-06-29 21:33:59 +00001942/// analyzeCompare - For a comparison instruction, return the source registers
1943/// in SrcReg and SrcReg2 if having two register operands, and the value it
1944/// compares against in CmpValue. Return true if the comparison instruction
1945/// can be analyzed.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001946bool ARMBaseInstrInfo::
Manman Rende7266c2012-06-29 21:33:59 +00001947analyzeCompare(const MachineInstr *MI, unsigned &SrcReg, unsigned &SrcReg2,
1948 int &CmpMask, int &CmpValue) const {
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001949 switch (MI->getOpcode()) {
1950 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001951 case ARM::CMPri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001952 case ARM::t2CMPri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001953 SrcReg = MI->getOperand(0).getReg();
Manman Rende7266c2012-06-29 21:33:59 +00001954 SrcReg2 = 0;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001955 CmpMask = ~0;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001956 CmpValue = MI->getOperand(1).getImm();
1957 return true;
Manman Ren247c5ab2012-05-11 01:30:47 +00001958 case ARM::CMPrr:
1959 case ARM::t2CMPrr:
1960 SrcReg = MI->getOperand(0).getReg();
Manman Rende7266c2012-06-29 21:33:59 +00001961 SrcReg2 = MI->getOperand(1).getReg();
Manman Ren247c5ab2012-05-11 01:30:47 +00001962 CmpMask = ~0;
1963 CmpValue = 0;
1964 return true;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001965 case ARM::TSTri:
1966 case ARM::t2TSTri:
1967 SrcReg = MI->getOperand(0).getReg();
Manman Rende7266c2012-06-29 21:33:59 +00001968 SrcReg2 = 0;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001969 CmpMask = MI->getOperand(1).getImm();
1970 CmpValue = 0;
1971 return true;
1972 }
1973
1974 return false;
1975}
1976
Gabor Greif05642a32010-09-29 10:12:08 +00001977/// isSuitableForMask - Identify a suitable 'and' instruction that
1978/// operates on the given source register and applies the same mask
1979/// as a 'tst' instruction. Provide a limited look-through for copies.
1980/// When successful, MI will hold the found instruction.
1981static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001982 int CmpMask, bool CommonUse) {
Gabor Greif05642a32010-09-29 10:12:08 +00001983 switch (MI->getOpcode()) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001984 case ARM::ANDri:
1985 case ARM::t2ANDri:
Gabor Greif05642a32010-09-29 10:12:08 +00001986 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001987 return false;
Gabor Greif05642a32010-09-29 10:12:08 +00001988 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greif04ac81d2010-09-21 12:01:15 +00001989 return true;
1990 break;
Gabor Greif05642a32010-09-29 10:12:08 +00001991 case ARM::COPY: {
1992 // Walk down one instruction which is potentially an 'and'.
1993 const MachineInstr &Copy = *MI;
Michael J. Spencerf000a7a2010-10-05 06:00:43 +00001994 MachineBasicBlock::iterator AND(
1995 llvm::next(MachineBasicBlock::iterator(MI)));
Gabor Greif05642a32010-09-29 10:12:08 +00001996 if (AND == MI->getParent()->end()) return false;
1997 MI = AND;
1998 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1999 CmpMask, true);
2000 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00002001 }
2002
2003 return false;
2004}
2005
Manman Ren76c6ccb2012-06-29 22:06:19 +00002006/// getSwappedCondition - assume the flags are set by MI(a,b), return
2007/// the condition code if we modify the instructions such that flags are
2008/// set by MI(b,a).
2009inline static ARMCC::CondCodes getSwappedCondition(ARMCC::CondCodes CC) {
2010 switch (CC) {
2011 default: return ARMCC::AL;
2012 case ARMCC::EQ: return ARMCC::EQ;
2013 case ARMCC::NE: return ARMCC::NE;
2014 case ARMCC::HS: return ARMCC::LS;
2015 case ARMCC::LO: return ARMCC::HI;
2016 case ARMCC::HI: return ARMCC::LO;
2017 case ARMCC::LS: return ARMCC::HS;
2018 case ARMCC::GE: return ARMCC::LE;
2019 case ARMCC::LT: return ARMCC::GT;
2020 case ARMCC::GT: return ARMCC::LT;
2021 case ARMCC::LE: return ARMCC::GE;
2022 }
2023}
2024
2025/// isRedundantFlagInstr - check whether the first instruction, whose only
2026/// purpose is to update flags, can be made redundant.
2027/// CMPrr can be made redundant by SUBrr if the operands are the same.
2028/// CMPri can be made redundant by SUBri if the operands are the same.
2029/// This function can be extended later on.
2030inline static bool isRedundantFlagInstr(MachineInstr *CmpI, unsigned SrcReg,
2031 unsigned SrcReg2, int ImmValue,
2032 MachineInstr *OI) {
2033 if ((CmpI->getOpcode() == ARM::CMPrr ||
2034 CmpI->getOpcode() == ARM::t2CMPrr) &&
2035 (OI->getOpcode() == ARM::SUBrr ||
2036 OI->getOpcode() == ARM::t2SUBrr) &&
2037 ((OI->getOperand(1).getReg() == SrcReg &&
2038 OI->getOperand(2).getReg() == SrcReg2) ||
2039 (OI->getOperand(1).getReg() == SrcReg2 &&
2040 OI->getOperand(2).getReg() == SrcReg)))
2041 return true;
2042
2043 if ((CmpI->getOpcode() == ARM::CMPri ||
2044 CmpI->getOpcode() == ARM::t2CMPri) &&
2045 (OI->getOpcode() == ARM::SUBri ||
2046 OI->getOpcode() == ARM::t2SUBri) &&
2047 OI->getOperand(1).getReg() == SrcReg &&
2048 OI->getOperand(2).getImm() == ImmValue)
2049 return true;
2050 return false;
2051}
2052
Manman Rende7266c2012-06-29 21:33:59 +00002053/// optimizeCompareInstr - Convert the instruction supplying the argument to the
2054/// comparison into one that sets the zero bit in the flags register;
2055/// Remove a redundant Compare instruction if an earlier instruction can set the
2056/// flags in the same way as Compare.
2057/// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
2058/// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
2059/// condition code of instructions which use the flags.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00002060bool ARMBaseInstrInfo::
Manman Rende7266c2012-06-29 21:33:59 +00002061optimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, unsigned SrcReg2,
2062 int CmpMask, int CmpValue,
2063 const MachineRegisterInfo *MRI) const {
Manman Ren76c6ccb2012-06-29 22:06:19 +00002064 // Get the unique definition of SrcReg.
2065 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
2066 if (!MI) return false;
Bill Wendling92ad57f2010-09-10 23:34:19 +00002067
Gabor Greif04ac81d2010-09-21 12:01:15 +00002068 // Masked compares sometimes use the same register as the corresponding 'and'.
2069 if (CmpMask != ~0) {
Jakob Stoklund Olesen519daf52012-09-10 19:17:25 +00002070 if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(MI)) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00002071 MI = 0;
Bill Wendlingb41ee962010-10-18 21:22:31 +00002072 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
2073 UE = MRI->use_end(); UI != UE; ++UI) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00002074 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greif05642a32010-09-29 10:12:08 +00002075 MachineInstr *PotentialAND = &*UI;
Jakob Stoklund Olesen519daf52012-09-10 19:17:25 +00002076 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
2077 isPredicated(PotentialAND))
Gabor Greif04ac81d2010-09-21 12:01:15 +00002078 continue;
Gabor Greif05642a32010-09-29 10:12:08 +00002079 MI = PotentialAND;
Gabor Greif04ac81d2010-09-21 12:01:15 +00002080 break;
2081 }
2082 if (!MI) return false;
2083 }
2084 }
2085
Manman Ren247c5ab2012-05-11 01:30:47 +00002086 // Get ready to iterate backward from CmpInstr.
2087 MachineBasicBlock::iterator I = CmpInstr, E = MI,
2088 B = CmpInstr->getParent()->begin();
Bill Wendling0aa38b92010-10-09 00:03:48 +00002089
2090 // Early exit if CmpInstr is at the beginning of the BB.
2091 if (I == B) return false;
2092
Manman Ren247c5ab2012-05-11 01:30:47 +00002093 // There are two possible candidates which can be changed to set CPSR:
2094 // One is MI, the other is a SUB instruction.
2095 // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
2096 // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
2097 MachineInstr *Sub = NULL;
Manman Rende7266c2012-06-29 21:33:59 +00002098 if (SrcReg2 != 0)
Manman Ren247c5ab2012-05-11 01:30:47 +00002099 // MI is not a candidate for CMPrr.
2100 MI = NULL;
Manman Rende7266c2012-06-29 21:33:59 +00002101 else if (MI->getParent() != CmpInstr->getParent() || CmpValue != 0) {
Manman Ren247c5ab2012-05-11 01:30:47 +00002102 // Conservatively refuse to convert an instruction which isn't in the same
2103 // BB as the comparison.
2104 // For CMPri, we need to check Sub, thus we can't return here.
Manman Ren4949e982012-05-11 15:36:46 +00002105 if (CmpInstr->getOpcode() == ARM::CMPri ||
Manman Ren247c5ab2012-05-11 01:30:47 +00002106 CmpInstr->getOpcode() == ARM::t2CMPri)
2107 MI = NULL;
2108 else
2109 return false;
2110 }
2111
2112 // Check that CPSR isn't set between the comparison instruction and the one we
2113 // want to change. At the same time, search for Sub.
Manman Ren76c6ccb2012-06-29 22:06:19 +00002114 const TargetRegisterInfo *TRI = &getRegisterInfo();
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00002115 --I;
2116 for (; I != E; --I) {
2117 const MachineInstr &Instr = *I;
2118
Manman Ren76c6ccb2012-06-29 22:06:19 +00002119 if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
2120 Instr.readsRegister(ARM::CPSR, TRI))
Bill Wendling40a5eb12010-11-01 20:41:43 +00002121 // This instruction modifies or uses CPSR after the one we want to
2122 // change. We can't do this transformation.
Manman Ren76c6ccb2012-06-29 22:06:19 +00002123 return false;
Evan Cheng691e64a2010-09-21 23:49:07 +00002124
Manman Ren76c6ccb2012-06-29 22:06:19 +00002125 // Check whether CmpInstr can be made redundant by the current instruction.
2126 if (isRedundantFlagInstr(CmpInstr, SrcReg, SrcReg2, CmpValue, &*I)) {
Manman Ren247c5ab2012-05-11 01:30:47 +00002127 Sub = &*I;
2128 break;
2129 }
2130
Evan Cheng691e64a2010-09-21 23:49:07 +00002131 if (I == B)
2132 // The 'and' is below the comparison instruction.
2133 return false;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00002134 }
2135
Manman Ren247c5ab2012-05-11 01:30:47 +00002136 // Return false if no candidates exist.
2137 if (!MI && !Sub)
2138 return false;
2139
2140 // The single candidate is called MI.
2141 if (!MI) MI = Sub;
2142
Jakob Stoklund Olesen519daf52012-09-10 19:17:25 +00002143 // We can't use a predicated instruction - it doesn't always write the flags.
2144 if (isPredicated(MI))
2145 return false;
2146
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00002147 switch (MI->getOpcode()) {
2148 default: break;
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002149 case ARM::RSBrr:
Owen Andersondf298c92011-04-06 23:35:59 +00002150 case ARM::RSBri:
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002151 case ARM::RSCrr:
Owen Andersondf298c92011-04-06 23:35:59 +00002152 case ARM::RSCri:
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002153 case ARM::ADDrr:
Bill Wendling38ae9972010-08-11 00:23:00 +00002154 case ARM::ADDri:
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002155 case ARM::ADCrr:
Owen Andersondf298c92011-04-06 23:35:59 +00002156 case ARM::ADCri:
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002157 case ARM::SUBrr:
Bill Wendling38ae9972010-08-11 00:23:00 +00002158 case ARM::SUBri:
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002159 case ARM::SBCrr:
Owen Andersondf298c92011-04-06 23:35:59 +00002160 case ARM::SBCri:
2161 case ARM::t2RSBri:
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002162 case ARM::t2ADDrr:
Bill Wendling38ae9972010-08-11 00:23:00 +00002163 case ARM::t2ADDri:
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002164 case ARM::t2ADCrr:
Owen Andersondf298c92011-04-06 23:35:59 +00002165 case ARM::t2ADCri:
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002166 case ARM::t2SUBrr:
Owen Andersondf298c92011-04-06 23:35:59 +00002167 case ARM::t2SUBri:
Cameron Zwarichca3f6a32011-04-15 20:28:28 +00002168 case ARM::t2SBCrr:
Cameron Zwarichb485de52011-04-15 20:45:00 +00002169 case ARM::t2SBCri:
2170 case ARM::ANDrr:
2171 case ARM::ANDri:
2172 case ARM::t2ANDrr:
Cameron Zwarich0cb11ac2011-04-15 21:24:38 +00002173 case ARM::t2ANDri:
2174 case ARM::ORRrr:
2175 case ARM::ORRri:
2176 case ARM::t2ORRrr:
2177 case ARM::t2ORRri:
2178 case ARM::EORrr:
2179 case ARM::EORri:
2180 case ARM::t2EORrr:
2181 case ARM::t2EORri: {
Manman Ren247c5ab2012-05-11 01:30:47 +00002182 // Scan forward for the use of CPSR
2183 // When checking against MI: if it's a conditional code requires
Manman Ren45ed1942012-07-11 22:51:44 +00002184 // checking of V bit, then this is not safe to do.
2185 // It is safe to remove CmpInstr if CPSR is redefined or killed.
2186 // If we are done with the basic block, we need to check whether CPSR is
2187 // live-out.
Manman Ren76c6ccb2012-06-29 22:06:19 +00002188 SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
2189 OperandsToUpdate;
Evan Cheng2c339152011-03-23 22:52:04 +00002190 bool isSafe = false;
2191 I = CmpInstr;
Manman Ren247c5ab2012-05-11 01:30:47 +00002192 E = CmpInstr->getParent()->end();
Evan Cheng2c339152011-03-23 22:52:04 +00002193 while (!isSafe && ++I != E) {
2194 const MachineInstr &Instr = *I;
2195 for (unsigned IO = 0, EO = Instr.getNumOperands();
2196 !isSafe && IO != EO; ++IO) {
2197 const MachineOperand &MO = Instr.getOperand(IO);
Jakob Stoklund Olesen2420b552012-02-17 19:23:15 +00002198 if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
2199 isSafe = true;
2200 break;
2201 }
Evan Cheng2c339152011-03-23 22:52:04 +00002202 if (!MO.isReg() || MO.getReg() != ARM::CPSR)
2203 continue;
2204 if (MO.isDef()) {
2205 isSafe = true;
2206 break;
2207 }
2208 // Condition code is after the operand before CPSR.
2209 ARMCC::CondCodes CC = (ARMCC::CondCodes)Instr.getOperand(IO-1).getImm();
Manman Ren76c6ccb2012-06-29 22:06:19 +00002210 if (Sub) {
2211 ARMCC::CondCodes NewCC = getSwappedCondition(CC);
2212 if (NewCC == ARMCC::AL)
Manman Ren247c5ab2012-05-11 01:30:47 +00002213 return false;
Manman Ren76c6ccb2012-06-29 22:06:19 +00002214 // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
2215 // on CMP needs to be updated to be based on SUB.
2216 // Push the condition code operands to OperandsToUpdate.
2217 // If it is safe to remove CmpInstr, the condition code of these
2218 // operands will be modified.
2219 if (SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
2220 Sub->getOperand(2).getReg() == SrcReg)
2221 OperandsToUpdate.push_back(std::make_pair(&((*I).getOperand(IO-1)),
2222 NewCC));
2223 }
Manman Ren247c5ab2012-05-11 01:30:47 +00002224 else
2225 switch (CC) {
2226 default:
Manman Ren9af64302012-07-11 23:47:00 +00002227 // CPSR can be used multiple times, we should continue.
Manman Ren247c5ab2012-05-11 01:30:47 +00002228 break;
2229 case ARMCC::VS:
2230 case ARMCC::VC:
2231 case ARMCC::GE:
2232 case ARMCC::LT:
2233 case ARMCC::GT:
2234 case ARMCC::LE:
2235 return false;
2236 }
Evan Cheng2c339152011-03-23 22:52:04 +00002237 }
2238 }
2239
Manman Ren45ed1942012-07-11 22:51:44 +00002240 // If CPSR is not killed nor re-defined, we should check whether it is
2241 // live-out. If it is live-out, do not optimize.
2242 if (!isSafe) {
2243 MachineBasicBlock *MBB = CmpInstr->getParent();
2244 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
2245 SE = MBB->succ_end(); SI != SE; ++SI)
2246 if ((*SI)->isLiveIn(ARM::CPSR))
2247 return false;
2248 }
Evan Cheng2c339152011-03-23 22:52:04 +00002249
Evan Cheng3642e642010-11-17 08:06:50 +00002250 // Toggle the optional operand to CPSR.
2251 MI->getOperand(5).setReg(ARM::CPSR);
2252 MI->getOperand(5).setIsDef(true);
Jakob Stoklund Olesen519daf52012-09-10 19:17:25 +00002253 assert(!isPredicated(MI) && "Can't use flags from predicated instruction");
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00002254 CmpInstr->eraseFromParent();
Manman Ren247c5ab2012-05-11 01:30:47 +00002255
2256 // Modify the condition code of operands in OperandsToUpdate.
2257 // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
2258 // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
Manman Ren76c6ccb2012-06-29 22:06:19 +00002259 for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
2260 OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00002261 return true;
2262 }
Cameron Zwarichb485de52011-04-15 20:45:00 +00002263 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00002264
2265 return false;
2266}
Evan Cheng5f54ce32010-09-09 18:18:55 +00002267
Evan Chengc4af4632010-11-17 20:13:28 +00002268bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
2269 MachineInstr *DefMI, unsigned Reg,
2270 MachineRegisterInfo *MRI) const {
2271 // Fold large immediates into add, sub, or, xor.
2272 unsigned DefOpc = DefMI->getOpcode();
2273 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
2274 return false;
2275 if (!DefMI->getOperand(1).isImm())
2276 // Could be t2MOVi32imm <ga:xx>
2277 return false;
2278
2279 if (!MRI->hasOneNonDBGUse(Reg))
2280 return false;
2281
Evan Chenge279f592012-03-26 23:31:00 +00002282 const MCInstrDesc &DefMCID = DefMI->getDesc();
2283 if (DefMCID.hasOptionalDef()) {
2284 unsigned NumOps = DefMCID.getNumOperands();
2285 const MachineOperand &MO = DefMI->getOperand(NumOps-1);
2286 if (MO.getReg() == ARM::CPSR && !MO.isDead())
2287 // If DefMI defines CPSR and it is not dead, it's obviously not safe
2288 // to delete DefMI.
2289 return false;
2290 }
2291
2292 const MCInstrDesc &UseMCID = UseMI->getDesc();
2293 if (UseMCID.hasOptionalDef()) {
2294 unsigned NumOps = UseMCID.getNumOperands();
2295 if (UseMI->getOperand(NumOps-1).getReg() == ARM::CPSR)
2296 // If the instruction sets the flag, do not attempt this optimization
2297 // since it may change the semantics of the code.
2298 return false;
2299 }
2300
Evan Chengc4af4632010-11-17 20:13:28 +00002301 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng5c71c7a2010-11-18 01:43:23 +00002302 unsigned NewUseOpc = 0;
Evan Chengc4af4632010-11-17 20:13:28 +00002303 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng5c71c7a2010-11-18 01:43:23 +00002304 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Chengc4af4632010-11-17 20:13:28 +00002305 bool Commute = false;
2306 switch (UseOpc) {
2307 default: return false;
2308 case ARM::SUBrr:
2309 case ARM::ADDrr:
2310 case ARM::ORRrr:
2311 case ARM::EORrr:
2312 case ARM::t2SUBrr:
2313 case ARM::t2ADDrr:
2314 case ARM::t2ORRrr:
2315 case ARM::t2EORrr: {
2316 Commute = UseMI->getOperand(2).getReg() != Reg;
2317 switch (UseOpc) {
2318 default: break;
2319 case ARM::SUBrr: {
2320 if (Commute)
2321 return false;
2322 ImmVal = -ImmVal;
2323 NewUseOpc = ARM::SUBri;
2324 // Fallthrough
2325 }
2326 case ARM::ADDrr:
2327 case ARM::ORRrr:
2328 case ARM::EORrr: {
2329 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
2330 return false;
2331 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
2332 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
2333 switch (UseOpc) {
2334 default: break;
2335 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
2336 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
2337 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
2338 }
2339 break;
2340 }
2341 case ARM::t2SUBrr: {
2342 if (Commute)
2343 return false;
2344 ImmVal = -ImmVal;
2345 NewUseOpc = ARM::t2SUBri;
2346 // Fallthrough
2347 }
2348 case ARM::t2ADDrr:
2349 case ARM::t2ORRrr:
2350 case ARM::t2EORrr: {
2351 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
2352 return false;
2353 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
2354 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
2355 switch (UseOpc) {
2356 default: break;
2357 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
2358 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
2359 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
2360 }
2361 break;
2362 }
2363 }
2364 }
2365 }
2366
2367 unsigned OpIdx = Commute ? 2 : 1;
2368 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
2369 bool isKill = UseMI->getOperand(OpIdx).isKill();
2370 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
2371 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
Evan Chengddfd1372011-12-14 02:11:42 +00002372 UseMI, UseMI->getDebugLoc(),
Evan Chengc4af4632010-11-17 20:13:28 +00002373 get(NewUseOpc), NewReg)
2374 .addReg(Reg1, getKillRegState(isKill))
2375 .addImm(SOImmValV1)));
2376 UseMI->setDesc(get(NewUseOpc));
2377 UseMI->getOperand(1).setReg(NewReg);
2378 UseMI->getOperand(1).setIsKill();
2379 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
2380 DefMI->eraseFromParent();
2381 return true;
2382}
2383
Bob Wilsoneb1641d2012-09-29 21:43:49 +00002384static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
2385 const MachineInstr *MI) {
2386 switch (MI->getOpcode()) {
2387 default: {
2388 const MCInstrDesc &Desc = MI->getDesc();
2389 int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
2390 assert(UOps >= 0 && "bad # UOps");
2391 return UOps;
2392 }
2393
2394 case ARM::LDRrs:
2395 case ARM::LDRBrs:
2396 case ARM::STRrs:
2397 case ARM::STRBrs: {
2398 unsigned ShOpVal = MI->getOperand(3).getImm();
2399 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2400 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2401 if (!isSub &&
2402 (ShImm == 0 ||
2403 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2404 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2405 return 1;
2406 return 2;
2407 }
2408
2409 case ARM::LDRH:
2410 case ARM::STRH: {
2411 if (!MI->getOperand(2).getReg())
2412 return 1;
2413
2414 unsigned ShOpVal = MI->getOperand(3).getImm();
2415 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2416 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2417 if (!isSub &&
2418 (ShImm == 0 ||
2419 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2420 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2421 return 1;
2422 return 2;
2423 }
2424
2425 case ARM::LDRSB:
2426 case ARM::LDRSH:
2427 return (ARM_AM::getAM3Op(MI->getOperand(3).getImm()) == ARM_AM::sub) ? 3:2;
2428
2429 case ARM::LDRSB_POST:
2430 case ARM::LDRSH_POST: {
2431 unsigned Rt = MI->getOperand(0).getReg();
2432 unsigned Rm = MI->getOperand(3).getReg();
2433 return (Rt == Rm) ? 4 : 3;
2434 }
2435
2436 case ARM::LDR_PRE_REG:
2437 case ARM::LDRB_PRE_REG: {
2438 unsigned Rt = MI->getOperand(0).getReg();
2439 unsigned Rm = MI->getOperand(3).getReg();
2440 if (Rt == Rm)
2441 return 3;
2442 unsigned ShOpVal = MI->getOperand(4).getImm();
2443 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2444 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2445 if (!isSub &&
2446 (ShImm == 0 ||
2447 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2448 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2449 return 2;
2450 return 3;
2451 }
2452
2453 case ARM::STR_PRE_REG:
2454 case ARM::STRB_PRE_REG: {
2455 unsigned ShOpVal = MI->getOperand(4).getImm();
2456 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2457 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2458 if (!isSub &&
2459 (ShImm == 0 ||
2460 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2461 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2462 return 2;
2463 return 3;
2464 }
2465
2466 case ARM::LDRH_PRE:
2467 case ARM::STRH_PRE: {
2468 unsigned Rt = MI->getOperand(0).getReg();
2469 unsigned Rm = MI->getOperand(3).getReg();
2470 if (!Rm)
2471 return 2;
2472 if (Rt == Rm)
2473 return 3;
2474 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub)
2475 ? 3 : 2;
2476 }
2477
2478 case ARM::LDR_POST_REG:
2479 case ARM::LDRB_POST_REG:
2480 case ARM::LDRH_POST: {
2481 unsigned Rt = MI->getOperand(0).getReg();
2482 unsigned Rm = MI->getOperand(3).getReg();
2483 return (Rt == Rm) ? 3 : 2;
2484 }
2485
2486 case ARM::LDR_PRE_IMM:
2487 case ARM::LDRB_PRE_IMM:
2488 case ARM::LDR_POST_IMM:
2489 case ARM::LDRB_POST_IMM:
2490 case ARM::STRB_POST_IMM:
2491 case ARM::STRB_POST_REG:
2492 case ARM::STRB_PRE_IMM:
2493 case ARM::STRH_POST:
2494 case ARM::STR_POST_IMM:
2495 case ARM::STR_POST_REG:
2496 case ARM::STR_PRE_IMM:
2497 return 2;
2498
2499 case ARM::LDRSB_PRE:
2500 case ARM::LDRSH_PRE: {
2501 unsigned Rm = MI->getOperand(3).getReg();
2502 if (Rm == 0)
2503 return 3;
2504 unsigned Rt = MI->getOperand(0).getReg();
2505 if (Rt == Rm)
2506 return 4;
2507 unsigned ShOpVal = MI->getOperand(4).getImm();
2508 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
2509 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2510 if (!isSub &&
2511 (ShImm == 0 ||
2512 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
2513 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
2514 return 3;
2515 return 4;
2516 }
2517
2518 case ARM::LDRD: {
2519 unsigned Rt = MI->getOperand(0).getReg();
2520 unsigned Rn = MI->getOperand(2).getReg();
2521 unsigned Rm = MI->getOperand(3).getReg();
2522 if (Rm)
2523 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2524 return (Rt == Rn) ? 3 : 2;
2525 }
2526
2527 case ARM::STRD: {
2528 unsigned Rm = MI->getOperand(3).getReg();
2529 if (Rm)
2530 return (ARM_AM::getAM3Op(MI->getOperand(4).getImm()) == ARM_AM::sub) ?4:3;
2531 return 2;
2532 }
2533
2534 case ARM::LDRD_POST:
2535 case ARM::t2LDRD_POST:
2536 return 3;
2537
2538 case ARM::STRD_POST:
2539 case ARM::t2STRD_POST:
2540 return 4;
2541
2542 case ARM::LDRD_PRE: {
2543 unsigned Rt = MI->getOperand(0).getReg();
2544 unsigned Rn = MI->getOperand(3).getReg();
2545 unsigned Rm = MI->getOperand(4).getReg();
2546 if (Rm)
2547 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2548 return (Rt == Rn) ? 4 : 3;
2549 }
2550
2551 case ARM::t2LDRD_PRE: {
2552 unsigned Rt = MI->getOperand(0).getReg();
2553 unsigned Rn = MI->getOperand(3).getReg();
2554 return (Rt == Rn) ? 4 : 3;
2555 }
2556
2557 case ARM::STRD_PRE: {
2558 unsigned Rm = MI->getOperand(4).getReg();
2559 if (Rm)
2560 return (ARM_AM::getAM3Op(MI->getOperand(5).getImm()) == ARM_AM::sub) ?5:4;
2561 return 3;
2562 }
2563
2564 case ARM::t2STRD_PRE:
2565 return 3;
2566
2567 case ARM::t2LDR_POST:
2568 case ARM::t2LDRB_POST:
2569 case ARM::t2LDRB_PRE:
2570 case ARM::t2LDRSBi12:
2571 case ARM::t2LDRSBi8:
2572 case ARM::t2LDRSBpci:
2573 case ARM::t2LDRSBs:
2574 case ARM::t2LDRH_POST:
2575 case ARM::t2LDRH_PRE:
2576 case ARM::t2LDRSBT:
2577 case ARM::t2LDRSB_POST:
2578 case ARM::t2LDRSB_PRE:
2579 case ARM::t2LDRSH_POST:
2580 case ARM::t2LDRSH_PRE:
2581 case ARM::t2LDRSHi12:
2582 case ARM::t2LDRSHi8:
2583 case ARM::t2LDRSHpci:
2584 case ARM::t2LDRSHs:
2585 return 2;
2586
2587 case ARM::t2LDRDi8: {
2588 unsigned Rt = MI->getOperand(0).getReg();
2589 unsigned Rn = MI->getOperand(2).getReg();
2590 return (Rt == Rn) ? 3 : 2;
2591 }
2592
2593 case ARM::t2STRB_POST:
2594 case ARM::t2STRB_PRE:
2595 case ARM::t2STRBs:
2596 case ARM::t2STRDi8:
2597 case ARM::t2STRH_POST:
2598 case ARM::t2STRH_PRE:
2599 case ARM::t2STRHs:
2600 case ARM::t2STR_POST:
2601 case ARM::t2STR_PRE:
2602 case ARM::t2STRs:
2603 return 2;
2604 }
2605}
2606
Andrew Trick9eed5332012-09-14 18:48:46 +00002607// Return the number of 32-bit words loaded by LDM or stored by STM. If this
2608// can't be easily determined return 0 (missing MachineMemOperand).
2609//
2610// FIXME: The current MachineInstr design does not support relying on machine
2611// mem operands to determine the width of a memory access. Instead, we expect
2612// the target to provide this information based on the instruction opcode and
2613// operands. However, using MachineMemOperand is a the best solution now for
2614// two reasons:
2615//
2616// 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
2617// operands. This is much more dangerous than using the MachineMemOperand
2618// sizes because CodeGen passes can insert/remove optional machine operands. In
2619// fact, it's totally incorrect for preRA passes and appears to be wrong for
2620// postRA passes as well.
2621//
2622// 2) getNumLDMAddresses is only used by the scheduling machine model and any
2623// machine model that calls this should handle the unknown (zero size) case.
2624//
2625// Long term, we should require a target hook that verifies MachineMemOperand
2626// sizes during MC lowering. That target hook should be local to MC lowering
2627// because we can't ensure that it is aware of other MI forms. Doing this will
2628// ensure that MachineMemOperands are correctly propagated through all passes.
2629unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr *MI) const {
2630 unsigned Size = 0;
2631 for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
2632 E = MI->memoperands_end(); I != E; ++I) {
2633 Size += (*I)->getSize();
2634 }
2635 return Size / 4;
2636}
2637
Evan Cheng5f54ce32010-09-09 18:18:55 +00002638unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +00002639ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
2640 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +00002641 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +00002642 return 1;
2643
Evan Chenge837dea2011-06-28 19:10:37 +00002644 const MCInstrDesc &Desc = MI->getDesc();
Evan Cheng5f54ce32010-09-09 18:18:55 +00002645 unsigned Class = Desc.getSchedClass();
Andrew Trick218ee742012-07-02 18:10:42 +00002646 int ItinUOps = ItinData->getNumMicroOps(Class);
Bob Wilsoneb1641d2012-09-29 21:43:49 +00002647 if (ItinUOps >= 0) {
2648 if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
2649 return getNumMicroOpsSwiftLdSt(ItinData, MI);
2650
Andrew Trick218ee742012-07-02 18:10:42 +00002651 return ItinUOps;
Bob Wilsoneb1641d2012-09-29 21:43:49 +00002652 }
Evan Cheng5f54ce32010-09-09 18:18:55 +00002653
2654 unsigned Opc = MI->getOpcode();
2655 switch (Opc) {
2656 default:
2657 llvm_unreachable("Unexpected multi-uops instruction!");
Bill Wendling73fe34a2010-11-16 01:16:36 +00002658 case ARM::VLDMQIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002659 case ARM::VSTMQIA:
Evan Cheng5f54ce32010-09-09 18:18:55 +00002660 return 2;
2661
2662 // The number of uOps for load / store multiple are determined by the number
2663 // registers.
Andrew Trick6e8f4c42010-12-24 04:28:06 +00002664 //
Evan Cheng3ef1c872010-09-10 01:29:16 +00002665 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
2666 // same cycle. The scheduling for the first load / store must be done
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00002667 // separately by assuming the address is not 64-bit aligned.
Bill Wendling73fe34a2010-11-16 01:16:36 +00002668 //
Evan Cheng3ef1c872010-09-10 01:29:16 +00002669 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendling73fe34a2010-11-16 01:16:36 +00002670 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
2671 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
2672 case ARM::VLDMDIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002673 case ARM::VLDMDIA_UPD:
2674 case ARM::VLDMDDB_UPD:
2675 case ARM::VLDMSIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002676 case ARM::VLDMSIA_UPD:
2677 case ARM::VLDMSDB_UPD:
2678 case ARM::VSTMDIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002679 case ARM::VSTMDIA_UPD:
2680 case ARM::VSTMDDB_UPD:
2681 case ARM::VSTMSIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002682 case ARM::VSTMSIA_UPD:
2683 case ARM::VSTMSDB_UPD: {
Evan Cheng5f54ce32010-09-09 18:18:55 +00002684 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
2685 return (NumRegs / 2) + (NumRegs % 2) + 1;
2686 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00002687
2688 case ARM::LDMIA_RET:
2689 case ARM::LDMIA:
2690 case ARM::LDMDA:
2691 case ARM::LDMDB:
2692 case ARM::LDMIB:
2693 case ARM::LDMIA_UPD:
2694 case ARM::LDMDA_UPD:
2695 case ARM::LDMDB_UPD:
2696 case ARM::LDMIB_UPD:
2697 case ARM::STMIA:
2698 case ARM::STMDA:
2699 case ARM::STMDB:
2700 case ARM::STMIB:
2701 case ARM::STMIA_UPD:
2702 case ARM::STMDA_UPD:
2703 case ARM::STMDB_UPD:
2704 case ARM::STMIB_UPD:
2705 case ARM::tLDMIA:
2706 case ARM::tLDMIA_UPD:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002707 case ARM::tSTMIA_UPD:
Evan Cheng5f54ce32010-09-09 18:18:55 +00002708 case ARM::tPOP_RET:
2709 case ARM::tPOP:
2710 case ARM::tPUSH:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002711 case ARM::t2LDMIA_RET:
2712 case ARM::t2LDMIA:
2713 case ARM::t2LDMDB:
2714 case ARM::t2LDMIA_UPD:
2715 case ARM::t2LDMDB_UPD:
2716 case ARM::t2STMIA:
2717 case ARM::t2STMDB:
2718 case ARM::t2STMIA_UPD:
2719 case ARM::t2STMDB_UPD: {
Evan Cheng3ef1c872010-09-10 01:29:16 +00002720 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
Bob Wilsoneb1641d2012-09-29 21:43:49 +00002721 if (Subtarget.isSwift()) {
2722 // rdar://8402126
2723 int UOps = 1 + NumRegs; // One for address computation, one for each ld / st.
2724 switch (Opc) {
2725 default: break;
2726 case ARM::VLDMDIA_UPD:
2727 case ARM::VLDMDDB_UPD:
2728 case ARM::VLDMSIA_UPD:
2729 case ARM::VLDMSDB_UPD:
2730 case ARM::VSTMDIA_UPD:
2731 case ARM::VSTMDDB_UPD:
2732 case ARM::VSTMSIA_UPD:
2733 case ARM::VSTMSDB_UPD:
2734 case ARM::LDMIA_UPD:
2735 case ARM::LDMDA_UPD:
2736 case ARM::LDMDB_UPD:
2737 case ARM::LDMIB_UPD:
2738 case ARM::STMIA_UPD:
2739 case ARM::STMDA_UPD:
2740 case ARM::STMDB_UPD:
2741 case ARM::STMIB_UPD:
2742 case ARM::tLDMIA_UPD:
2743 case ARM::tSTMIA_UPD:
2744 case ARM::t2LDMIA_UPD:
2745 case ARM::t2LDMDB_UPD:
2746 case ARM::t2STMIA_UPD:
2747 case ARM::t2STMDB_UPD:
2748 ++UOps; // One for base register writeback.
2749 break;
2750 case ARM::LDMIA_RET:
2751 case ARM::tPOP_RET:
2752 case ARM::t2LDMIA_RET:
2753 UOps += 2; // One for base reg wb, one for write to pc.
2754 break;
2755 }
2756 return UOps;
2757 } else if (Subtarget.isCortexA8()) {
Evan Cheng8239daf2010-11-03 00:45:17 +00002758 if (NumRegs < 4)
2759 return 2;
2760 // 4 registers would be issued: 2, 2.
2761 // 5 registers would be issued: 2, 2, 1.
Andrew Trick218ee742012-07-02 18:10:42 +00002762 int A8UOps = (NumRegs / 2);
Evan Cheng8239daf2010-11-03 00:45:17 +00002763 if (NumRegs % 2)
Andrew Trick218ee742012-07-02 18:10:42 +00002764 ++A8UOps;
2765 return A8UOps;
Bob Wilsoneb1641d2012-09-29 21:43:49 +00002766 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Andrew Trick218ee742012-07-02 18:10:42 +00002767 int A9UOps = (NumRegs / 2);
Evan Cheng3ef1c872010-09-10 01:29:16 +00002768 // If there are odd number of registers or if it's not 64-bit aligned,
2769 // then it takes an extra AGU (Address Generation Unit) cycle.
2770 if ((NumRegs % 2) ||
2771 !MI->hasOneMemOperand() ||
2772 (*MI->memoperands_begin())->getAlignment() < 8)
Andrew Trick218ee742012-07-02 18:10:42 +00002773 ++A9UOps;
2774 return A9UOps;
Evan Cheng3ef1c872010-09-10 01:29:16 +00002775 } else {
2776 // Assume the worst.
2777 return NumRegs;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00002778 }
Evan Cheng5f54ce32010-09-09 18:18:55 +00002779 }
2780 }
2781}
Evan Chenga0792de2010-10-06 06:27:31 +00002782
2783int
Evan Cheng344d9db2010-10-07 23:12:15 +00002784ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
Evan Chenge837dea2011-06-28 19:10:37 +00002785 const MCInstrDesc &DefMCID,
Evan Cheng344d9db2010-10-07 23:12:15 +00002786 unsigned DefClass,
2787 unsigned DefIdx, unsigned DefAlign) const {
Evan Chenge837dea2011-06-28 19:10:37 +00002788 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng344d9db2010-10-07 23:12:15 +00002789 if (RegNo <= 0)
2790 // Def is the address writeback.
2791 return ItinData->getOperandCycle(DefClass, DefIdx);
2792
2793 int DefCycle;
2794 if (Subtarget.isCortexA8()) {
2795 // (regno / 2) + (regno % 2) + 1
2796 DefCycle = RegNo / 2 + 1;
2797 if (RegNo % 2)
2798 ++DefCycle;
Bob Wilsoneb1641d2012-09-29 21:43:49 +00002799 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng344d9db2010-10-07 23:12:15 +00002800 DefCycle = RegNo;
2801 bool isSLoad = false;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002802
Evan Chenge837dea2011-06-28 19:10:37 +00002803 switch (DefMCID.getOpcode()) {
Evan Cheng344d9db2010-10-07 23:12:15 +00002804 default: break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002805 case ARM::VLDMSIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002806 case ARM::VLDMSIA_UPD:
2807 case ARM::VLDMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002808 isSLoad = true;
2809 break;
2810 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00002811
Evan Cheng344d9db2010-10-07 23:12:15 +00002812 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
2813 // then it takes an extra cycle.
2814 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
2815 ++DefCycle;
2816 } else {
2817 // Assume the worst.
2818 DefCycle = RegNo + 2;
2819 }
2820
2821 return DefCycle;
2822}
2823
2824int
2825ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
Evan Chenge837dea2011-06-28 19:10:37 +00002826 const MCInstrDesc &DefMCID,
Evan Cheng344d9db2010-10-07 23:12:15 +00002827 unsigned DefClass,
2828 unsigned DefIdx, unsigned DefAlign) const {
Evan Chenge837dea2011-06-28 19:10:37 +00002829 int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
Evan Cheng344d9db2010-10-07 23:12:15 +00002830 if (RegNo <= 0)
2831 // Def is the address writeback.
2832 return ItinData->getOperandCycle(DefClass, DefIdx);
2833
2834 int DefCycle;
2835 if (Subtarget.isCortexA8()) {
2836 // 4 registers would be issued: 1, 2, 1.
2837 // 5 registers would be issued: 1, 2, 2.
2838 DefCycle = RegNo / 2;
2839 if (DefCycle < 1)
2840 DefCycle = 1;
2841 // Result latency is issue cycle + 2: E2.
2842 DefCycle += 2;
Bob Wilsoneb1641d2012-09-29 21:43:49 +00002843 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng344d9db2010-10-07 23:12:15 +00002844 DefCycle = (RegNo / 2);
2845 // If there are odd number of registers or if it's not 64-bit aligned,
2846 // then it takes an extra AGU (Address Generation Unit) cycle.
2847 if ((RegNo % 2) || DefAlign < 8)
2848 ++DefCycle;
2849 // Result latency is AGU cycles + 2.
2850 DefCycle += 2;
2851 } else {
2852 // Assume the worst.
2853 DefCycle = RegNo + 2;
2854 }
2855
2856 return DefCycle;
2857}
2858
2859int
2860ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
Evan Chenge837dea2011-06-28 19:10:37 +00002861 const MCInstrDesc &UseMCID,
Evan Cheng344d9db2010-10-07 23:12:15 +00002862 unsigned UseClass,
2863 unsigned UseIdx, unsigned UseAlign) const {
Evan Chenge837dea2011-06-28 19:10:37 +00002864 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng344d9db2010-10-07 23:12:15 +00002865 if (RegNo <= 0)
2866 return ItinData->getOperandCycle(UseClass, UseIdx);
2867
2868 int UseCycle;
2869 if (Subtarget.isCortexA8()) {
2870 // (regno / 2) + (regno % 2) + 1
2871 UseCycle = RegNo / 2 + 1;
2872 if (RegNo % 2)
2873 ++UseCycle;
Bob Wilsoneb1641d2012-09-29 21:43:49 +00002874 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng344d9db2010-10-07 23:12:15 +00002875 UseCycle = RegNo;
2876 bool isSStore = false;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002877
Evan Chenge837dea2011-06-28 19:10:37 +00002878 switch (UseMCID.getOpcode()) {
Evan Cheng344d9db2010-10-07 23:12:15 +00002879 default: break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002880 case ARM::VSTMSIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002881 case ARM::VSTMSIA_UPD:
2882 case ARM::VSTMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002883 isSStore = true;
2884 break;
2885 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00002886
Evan Cheng344d9db2010-10-07 23:12:15 +00002887 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
2888 // then it takes an extra cycle.
2889 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
2890 ++UseCycle;
2891 } else {
2892 // Assume the worst.
2893 UseCycle = RegNo + 2;
2894 }
2895
2896 return UseCycle;
2897}
2898
2899int
2900ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
Evan Chenge837dea2011-06-28 19:10:37 +00002901 const MCInstrDesc &UseMCID,
Evan Cheng344d9db2010-10-07 23:12:15 +00002902 unsigned UseClass,
2903 unsigned UseIdx, unsigned UseAlign) const {
Evan Chenge837dea2011-06-28 19:10:37 +00002904 int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
Evan Cheng344d9db2010-10-07 23:12:15 +00002905 if (RegNo <= 0)
2906 return ItinData->getOperandCycle(UseClass, UseIdx);
2907
2908 int UseCycle;
2909 if (Subtarget.isCortexA8()) {
2910 UseCycle = RegNo / 2;
2911 if (UseCycle < 2)
2912 UseCycle = 2;
2913 // Read in E3.
2914 UseCycle += 2;
Bob Wilsoneb1641d2012-09-29 21:43:49 +00002915 } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
Evan Cheng344d9db2010-10-07 23:12:15 +00002916 UseCycle = (RegNo / 2);
2917 // If there are odd number of registers or if it's not 64-bit aligned,
2918 // then it takes an extra AGU (Address Generation Unit) cycle.
2919 if ((RegNo % 2) || UseAlign < 8)
2920 ++UseCycle;
2921 } else {
2922 // Assume the worst.
2923 UseCycle = 1;
2924 }
2925 return UseCycle;
2926}
2927
2928int
Evan Chenga0792de2010-10-06 06:27:31 +00002929ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
Evan Chenge837dea2011-06-28 19:10:37 +00002930 const MCInstrDesc &DefMCID,
Evan Chenga0792de2010-10-06 06:27:31 +00002931 unsigned DefIdx, unsigned DefAlign,
Evan Chenge837dea2011-06-28 19:10:37 +00002932 const MCInstrDesc &UseMCID,
Evan Chenga0792de2010-10-06 06:27:31 +00002933 unsigned UseIdx, unsigned UseAlign) const {
Evan Chenge837dea2011-06-28 19:10:37 +00002934 unsigned DefClass = DefMCID.getSchedClass();
2935 unsigned UseClass = UseMCID.getSchedClass();
Evan Chenga0792de2010-10-06 06:27:31 +00002936
Evan Chenge837dea2011-06-28 19:10:37 +00002937 if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
Evan Chenga0792de2010-10-06 06:27:31 +00002938 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
2939
2940 // This may be a def / use of a variable_ops instruction, the operand
2941 // latency might be determinable dynamically. Let the target try to
2942 // figure it out.
Evan Cheng9e08ee52010-10-28 02:00:25 +00002943 int DefCycle = -1;
Evan Cheng7e2fe912010-10-28 06:47:08 +00002944 bool LdmBypass = false;
Evan Chenge837dea2011-06-28 19:10:37 +00002945 switch (DefMCID.getOpcode()) {
Evan Chenga0792de2010-10-06 06:27:31 +00002946 default:
2947 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2948 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002949
2950 case ARM::VLDMDIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002951 case ARM::VLDMDIA_UPD:
2952 case ARM::VLDMDDB_UPD:
2953 case ARM::VLDMSIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002954 case ARM::VLDMSIA_UPD:
2955 case ARM::VLDMSDB_UPD:
Evan Chenge837dea2011-06-28 19:10:37 +00002956 DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002957 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002958
2959 case ARM::LDMIA_RET:
2960 case ARM::LDMIA:
2961 case ARM::LDMDA:
2962 case ARM::LDMDB:
2963 case ARM::LDMIB:
2964 case ARM::LDMIA_UPD:
2965 case ARM::LDMDA_UPD:
2966 case ARM::LDMDB_UPD:
2967 case ARM::LDMIB_UPD:
2968 case ARM::tLDMIA:
2969 case ARM::tLDMIA_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00002970 case ARM::tPUSH:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002971 case ARM::t2LDMIA_RET:
2972 case ARM::t2LDMIA:
2973 case ARM::t2LDMDB:
2974 case ARM::t2LDMIA_UPD:
2975 case ARM::t2LDMDB_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00002976 LdmBypass = 1;
Evan Chenge837dea2011-06-28 19:10:37 +00002977 DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
Evan Cheng344d9db2010-10-07 23:12:15 +00002978 break;
Evan Chenga0792de2010-10-06 06:27:31 +00002979 }
Evan Chenga0792de2010-10-06 06:27:31 +00002980
2981 if (DefCycle == -1)
2982 // We can't seem to determine the result latency of the def, assume it's 2.
2983 DefCycle = 2;
2984
2985 int UseCycle = -1;
Evan Chenge837dea2011-06-28 19:10:37 +00002986 switch (UseMCID.getOpcode()) {
Evan Chenga0792de2010-10-06 06:27:31 +00002987 default:
2988 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
2989 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002990
2991 case ARM::VSTMDIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002992 case ARM::VSTMDIA_UPD:
2993 case ARM::VSTMDDB_UPD:
2994 case ARM::VSTMSIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002995 case ARM::VSTMSIA_UPD:
2996 case ARM::VSTMSDB_UPD:
Evan Chenge837dea2011-06-28 19:10:37 +00002997 UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002998 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002999
3000 case ARM::STMIA:
3001 case ARM::STMDA:
3002 case ARM::STMDB:
3003 case ARM::STMIB:
3004 case ARM::STMIA_UPD:
3005 case ARM::STMDA_UPD:
3006 case ARM::STMDB_UPD:
3007 case ARM::STMIB_UPD:
Bill Wendling73fe34a2010-11-16 01:16:36 +00003008 case ARM::tSTMIA_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00003009 case ARM::tPOP_RET:
3010 case ARM::tPOP:
Bill Wendling73fe34a2010-11-16 01:16:36 +00003011 case ARM::t2STMIA:
3012 case ARM::t2STMDB:
3013 case ARM::t2STMIA_UPD:
3014 case ARM::t2STMDB_UPD:
Evan Chenge837dea2011-06-28 19:10:37 +00003015 UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00003016 break;
Evan Chenga0792de2010-10-06 06:27:31 +00003017 }
Evan Chenga0792de2010-10-06 06:27:31 +00003018
3019 if (UseCycle == -1)
3020 // Assume it's read in the first stage.
3021 UseCycle = 1;
3022
3023 UseCycle = DefCycle - UseCycle + 1;
3024 if (UseCycle > 0) {
3025 if (LdmBypass) {
3026 // It's a variable_ops instruction so we can't use DefIdx here. Just use
3027 // first def operand.
Evan Chenge837dea2011-06-28 19:10:37 +00003028 if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
Evan Chenga0792de2010-10-06 06:27:31 +00003029 UseClass, UseIdx))
3030 --UseCycle;
3031 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendling73fe34a2010-11-16 01:16:36 +00003032 UseClass, UseIdx)) {
Evan Chenga0792de2010-10-06 06:27:31 +00003033 --UseCycle;
Bill Wendling73fe34a2010-11-16 01:16:36 +00003034 }
Evan Chenga0792de2010-10-06 06:27:31 +00003035 }
3036
3037 return UseCycle;
3038}
3039
Evan Chengddfd1372011-12-14 02:11:42 +00003040static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
Evan Cheng020f4102011-12-14 20:00:08 +00003041 const MachineInstr *MI, unsigned Reg,
Evan Chengddfd1372011-12-14 02:11:42 +00003042 unsigned &DefIdx, unsigned &Dist) {
3043 Dist = 0;
3044
3045 MachineBasicBlock::const_iterator I = MI; ++I;
3046 MachineBasicBlock::const_instr_iterator II =
3047 llvm::prior(I.getInstrIterator());
3048 assert(II->isInsideBundle() && "Empty bundle?");
3049
3050 int Idx = -1;
Evan Chengddfd1372011-12-14 02:11:42 +00003051 while (II->isInsideBundle()) {
3052 Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
3053 if (Idx != -1)
3054 break;
3055 --II;
3056 ++Dist;
3057 }
3058
3059 assert(Idx != -1 && "Cannot find bundled definition!");
3060 DefIdx = Idx;
3061 return II;
3062}
3063
3064static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
Evan Cheng020f4102011-12-14 20:00:08 +00003065 const MachineInstr *MI, unsigned Reg,
Evan Chengddfd1372011-12-14 02:11:42 +00003066 unsigned &UseIdx, unsigned &Dist) {
3067 Dist = 0;
3068
3069 MachineBasicBlock::const_instr_iterator II = MI; ++II;
3070 assert(II->isInsideBundle() && "Empty bundle?");
3071 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3072
3073 // FIXME: This doesn't properly handle multiple uses.
3074 int Idx = -1;
Evan Chengddfd1372011-12-14 02:11:42 +00003075 while (II != E && II->isInsideBundle()) {
3076 Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
3077 if (Idx != -1)
3078 break;
3079 if (II->getOpcode() != ARM::t2IT)
3080 ++Dist;
3081 ++II;
3082 }
3083
Evan Cheng020f4102011-12-14 20:00:08 +00003084 if (Idx == -1) {
3085 Dist = 0;
3086 return 0;
3087 }
3088
Evan Chengddfd1372011-12-14 02:11:42 +00003089 UseIdx = Idx;
3090 return II;
3091}
3092
Andrew Trick68b16542012-06-07 19:42:00 +00003093/// Return the number of cycles to add to (or subtract from) the static
3094/// itinerary based on the def opcode and alignment. The caller will ensure that
3095/// adjusted latency is at least one cycle.
3096static int adjustDefLatency(const ARMSubtarget &Subtarget,
3097 const MachineInstr *DefMI,
3098 const MCInstrDesc *DefMCID, unsigned DefAlign) {
3099 int Adjust = 0;
Silviu Baranga616471d2012-09-13 15:05:10 +00003100 if (Subtarget.isCortexA8() || Subtarget.isLikeA9()) {
Evan Cheng7e2fe912010-10-28 06:47:08 +00003101 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3102 // variants are one cycle cheaper.
Evan Chengddfd1372011-12-14 02:11:42 +00003103 switch (DefMCID->getOpcode()) {
Evan Cheng7e2fe912010-10-28 06:47:08 +00003104 default: break;
Jakob Stoklund Olesencff9baa2012-08-28 03:11:27 +00003105 case ARM::LDRrs:
3106 case ARM::LDRBrs: {
Evan Cheng7e2fe912010-10-28 06:47:08 +00003107 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3108 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3109 if (ShImm == 0 ||
3110 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
Andrew Trick68b16542012-06-07 19:42:00 +00003111 --Adjust;
Evan Cheng7e2fe912010-10-28 06:47:08 +00003112 break;
3113 }
Jakob Stoklund Olesencff9baa2012-08-28 03:11:27 +00003114 case ARM::t2LDRs:
3115 case ARM::t2LDRBs:
3116 case ARM::t2LDRHs:
Evan Cheng7e2fe912010-10-28 06:47:08 +00003117 case ARM::t2LDRSHs: {
3118 // Thumb2 mode: lsl only.
3119 unsigned ShAmt = DefMI->getOperand(3).getImm();
3120 if (ShAmt == 0 || ShAmt == 2)
Andrew Trick68b16542012-06-07 19:42:00 +00003121 --Adjust;
Evan Cheng7e2fe912010-10-28 06:47:08 +00003122 break;
3123 }
3124 }
Bob Wilsoneb1641d2012-09-29 21:43:49 +00003125 } else if (Subtarget.isSwift()) {
3126 // FIXME: Properly handle all of the latency adjustments for address
3127 // writeback.
3128 switch (DefMCID->getOpcode()) {
3129 default: break;
3130 case ARM::LDRrs:
3131 case ARM::LDRBrs: {
3132 unsigned ShOpVal = DefMI->getOperand(3).getImm();
3133 bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
3134 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3135 if (!isSub &&
3136 (ShImm == 0 ||
3137 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3138 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
3139 Adjust -= 2;
3140 else if (!isSub &&
3141 ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3142 --Adjust;
3143 break;
3144 }
3145 case ARM::t2LDRs:
3146 case ARM::t2LDRBs:
3147 case ARM::t2LDRHs:
3148 case ARM::t2LDRSHs: {
3149 // Thumb2 mode: lsl only.
3150 unsigned ShAmt = DefMI->getOperand(3).getImm();
3151 if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
3152 Adjust -= 2;
3153 break;
3154 }
3155 }
Evan Cheng7e2fe912010-10-28 06:47:08 +00003156 }
3157
Silviu Baranga616471d2012-09-13 15:05:10 +00003158 if (DefAlign < 8 && Subtarget.isLikeA9()) {
Evan Chengddfd1372011-12-14 02:11:42 +00003159 switch (DefMCID->getOpcode()) {
Evan Cheng75b41f12011-04-19 01:21:49 +00003160 default: break;
3161 case ARM::VLD1q8:
3162 case ARM::VLD1q16:
3163 case ARM::VLD1q32:
3164 case ARM::VLD1q64:
Jim Grosbach10b90a92011-10-24 21:45:13 +00003165 case ARM::VLD1q8wb_fixed:
3166 case ARM::VLD1q16wb_fixed:
3167 case ARM::VLD1q32wb_fixed:
3168 case ARM::VLD1q64wb_fixed:
3169 case ARM::VLD1q8wb_register:
3170 case ARM::VLD1q16wb_register:
3171 case ARM::VLD1q32wb_register:
3172 case ARM::VLD1q64wb_register:
Evan Cheng75b41f12011-04-19 01:21:49 +00003173 case ARM::VLD2d8:
3174 case ARM::VLD2d16:
3175 case ARM::VLD2d32:
3176 case ARM::VLD2q8:
3177 case ARM::VLD2q16:
3178 case ARM::VLD2q32:
Jim Grosbacha4e3c7f2011-12-09 21:28:25 +00003179 case ARM::VLD2d8wb_fixed:
3180 case ARM::VLD2d16wb_fixed:
3181 case ARM::VLD2d32wb_fixed:
3182 case ARM::VLD2q8wb_fixed:
3183 case ARM::VLD2q16wb_fixed:
3184 case ARM::VLD2q32wb_fixed:
3185 case ARM::VLD2d8wb_register:
3186 case ARM::VLD2d16wb_register:
3187 case ARM::VLD2d32wb_register:
3188 case ARM::VLD2q8wb_register:
3189 case ARM::VLD2q16wb_register:
3190 case ARM::VLD2q32wb_register:
Evan Cheng75b41f12011-04-19 01:21:49 +00003191 case ARM::VLD3d8:
3192 case ARM::VLD3d16:
3193 case ARM::VLD3d32:
3194 case ARM::VLD1d64T:
3195 case ARM::VLD3d8_UPD:
3196 case ARM::VLD3d16_UPD:
3197 case ARM::VLD3d32_UPD:
Jim Grosbach59216752011-10-24 23:26:05 +00003198 case ARM::VLD1d64Twb_fixed:
3199 case ARM::VLD1d64Twb_register:
Evan Cheng75b41f12011-04-19 01:21:49 +00003200 case ARM::VLD3q8_UPD:
3201 case ARM::VLD3q16_UPD:
3202 case ARM::VLD3q32_UPD:
3203 case ARM::VLD4d8:
3204 case ARM::VLD4d16:
3205 case ARM::VLD4d32:
3206 case ARM::VLD1d64Q:
3207 case ARM::VLD4d8_UPD:
3208 case ARM::VLD4d16_UPD:
3209 case ARM::VLD4d32_UPD:
Jim Grosbach399cdca2011-10-25 00:14:01 +00003210 case ARM::VLD1d64Qwb_fixed:
3211 case ARM::VLD1d64Qwb_register:
Evan Cheng75b41f12011-04-19 01:21:49 +00003212 case ARM::VLD4q8_UPD:
3213 case ARM::VLD4q16_UPD:
3214 case ARM::VLD4q32_UPD:
3215 case ARM::VLD1DUPq8:
3216 case ARM::VLD1DUPq16:
3217 case ARM::VLD1DUPq32:
Jim Grosbach096334e2011-11-30 19:35:44 +00003218 case ARM::VLD1DUPq8wb_fixed:
3219 case ARM::VLD1DUPq16wb_fixed:
3220 case ARM::VLD1DUPq32wb_fixed:
3221 case ARM::VLD1DUPq8wb_register:
3222 case ARM::VLD1DUPq16wb_register:
3223 case ARM::VLD1DUPq32wb_register:
Evan Cheng75b41f12011-04-19 01:21:49 +00003224 case ARM::VLD2DUPd8:
3225 case ARM::VLD2DUPd16:
3226 case ARM::VLD2DUPd32:
Jim Grosbache6949b12011-12-21 19:40:55 +00003227 case ARM::VLD2DUPd8wb_fixed:
3228 case ARM::VLD2DUPd16wb_fixed:
3229 case ARM::VLD2DUPd32wb_fixed:
3230 case ARM::VLD2DUPd8wb_register:
3231 case ARM::VLD2DUPd16wb_register:
3232 case ARM::VLD2DUPd32wb_register:
Evan Cheng75b41f12011-04-19 01:21:49 +00003233 case ARM::VLD4DUPd8:
3234 case ARM::VLD4DUPd16:
3235 case ARM::VLD4DUPd32:
3236 case ARM::VLD4DUPd8_UPD:
3237 case ARM::VLD4DUPd16_UPD:
3238 case ARM::VLD4DUPd32_UPD:
3239 case ARM::VLD1LNd8:
3240 case ARM::VLD1LNd16:
3241 case ARM::VLD1LNd32:
3242 case ARM::VLD1LNd8_UPD:
3243 case ARM::VLD1LNd16_UPD:
3244 case ARM::VLD1LNd32_UPD:
3245 case ARM::VLD2LNd8:
3246 case ARM::VLD2LNd16:
3247 case ARM::VLD2LNd32:
3248 case ARM::VLD2LNq16:
3249 case ARM::VLD2LNq32:
3250 case ARM::VLD2LNd8_UPD:
3251 case ARM::VLD2LNd16_UPD:
3252 case ARM::VLD2LNd32_UPD:
3253 case ARM::VLD2LNq16_UPD:
3254 case ARM::VLD2LNq32_UPD:
3255 case ARM::VLD4LNd8:
3256 case ARM::VLD4LNd16:
3257 case ARM::VLD4LNd32:
3258 case ARM::VLD4LNq16:
3259 case ARM::VLD4LNq32:
3260 case ARM::VLD4LNd8_UPD:
3261 case ARM::VLD4LNd16_UPD:
3262 case ARM::VLD4LNd32_UPD:
3263 case ARM::VLD4LNq16_UPD:
3264 case ARM::VLD4LNq32_UPD:
3265 // If the address is not 64-bit aligned, the latencies of these
3266 // instructions increases by one.
Andrew Trick68b16542012-06-07 19:42:00 +00003267 ++Adjust;
Evan Cheng75b41f12011-04-19 01:21:49 +00003268 break;
3269 }
Andrew Trick68b16542012-06-07 19:42:00 +00003270 }
3271 return Adjust;
3272}
Evan Cheng75b41f12011-04-19 01:21:49 +00003273
Andrew Trick68b16542012-06-07 19:42:00 +00003274
3275
3276int
3277ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3278 const MachineInstr *DefMI, unsigned DefIdx,
3279 const MachineInstr *UseMI,
3280 unsigned UseIdx) const {
3281 // No operand latency. The caller may fall back to getInstrLatency.
3282 if (!ItinData || ItinData->isEmpty())
3283 return -1;
3284
3285 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
3286 unsigned Reg = DefMO.getReg();
3287 const MCInstrDesc *DefMCID = &DefMI->getDesc();
3288 const MCInstrDesc *UseMCID = &UseMI->getDesc();
3289
3290 unsigned DefAdj = 0;
3291 if (DefMI->isBundle()) {
3292 DefMI = getBundledDefMI(&getRegisterInfo(), DefMI, Reg, DefIdx, DefAdj);
3293 DefMCID = &DefMI->getDesc();
3294 }
3295 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
3296 DefMI->isRegSequence() || DefMI->isImplicitDef()) {
3297 return 1;
3298 }
3299
3300 unsigned UseAdj = 0;
3301 if (UseMI->isBundle()) {
3302 unsigned NewUseIdx;
3303 const MachineInstr *NewUseMI = getBundledUseMI(&getRegisterInfo(), UseMI,
3304 Reg, NewUseIdx, UseAdj);
Andrew Tricke2b32bb2012-06-22 02:50:33 +00003305 if (!NewUseMI)
3306 return -1;
3307
3308 UseMI = NewUseMI;
3309 UseIdx = NewUseIdx;
3310 UseMCID = &UseMI->getDesc();
Andrew Trick68b16542012-06-07 19:42:00 +00003311 }
3312
3313 if (Reg == ARM::CPSR) {
3314 if (DefMI->getOpcode() == ARM::FMSTAT) {
3315 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
Silviu Baranga616471d2012-09-13 15:05:10 +00003316 return Subtarget.isLikeA9() ? 1 : 20;
Andrew Trick68b16542012-06-07 19:42:00 +00003317 }
3318
3319 // CPSR set and branch can be paired in the same cycle.
3320 if (UseMI->isBranch())
3321 return 0;
3322
3323 // Otherwise it takes the instruction latency (generally one).
3324 unsigned Latency = getInstrLatency(ItinData, DefMI);
3325
3326 // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
3327 // its uses. Instructions which are otherwise scheduled between them may
3328 // incur a code size penalty (not able to use the CPSR setting 16-bit
3329 // instructions).
3330 if (Latency > 0 && Subtarget.isThumb2()) {
3331 const MachineFunction *MF = DefMI->getParent()->getParent();
Bill Wendling831737d2012-12-30 10:32:01 +00003332 if (MF->getFunction()->getAttributes().
3333 hasAttribute(AttributeSet::FunctionIndex,
3334 Attribute::OptimizeForSize))
Andrew Trick68b16542012-06-07 19:42:00 +00003335 --Latency;
3336 }
3337 return Latency;
3338 }
3339
Andrew Tricke2b32bb2012-06-22 02:50:33 +00003340 if (DefMO.isImplicit() || UseMI->getOperand(UseIdx).isImplicit())
3341 return -1;
3342
Andrew Trick68b16542012-06-07 19:42:00 +00003343 unsigned DefAlign = DefMI->hasOneMemOperand()
3344 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
3345 unsigned UseAlign = UseMI->hasOneMemOperand()
3346 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
3347
3348 // Get the itinerary's latency if possible, and handle variable_ops.
3349 int Latency = getOperandLatency(ItinData, *DefMCID, DefIdx, DefAlign,
3350 *UseMCID, UseIdx, UseAlign);
3351 // Unable to find operand latency. The caller may resort to getInstrLatency.
3352 if (Latency < 0)
3353 return Latency;
3354
3355 // Adjust for IT block position.
3356 int Adj = DefAdj + UseAdj;
3357
3358 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3359 Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
3360 if (Adj >= 0 || (int)Latency > -Adj) {
3361 return Latency + Adj;
3362 }
3363 // Return the itinerary latency, which may be zero but not less than zero.
Evan Cheng7e2fe912010-10-28 06:47:08 +00003364 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00003365}
3366
3367int
3368ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
3369 SDNode *DefNode, unsigned DefIdx,
3370 SDNode *UseNode, unsigned UseIdx) const {
3371 if (!DefNode->isMachineOpcode())
3372 return 1;
3373
Evan Chenge837dea2011-06-28 19:10:37 +00003374 const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
Andrew Trickc8bfd1d2011-01-21 05:51:33 +00003375
Evan Chenge837dea2011-06-28 19:10:37 +00003376 if (isZeroCost(DefMCID.Opcode))
Andrew Trickc8bfd1d2011-01-21 05:51:33 +00003377 return 0;
3378
Evan Chenga0792de2010-10-06 06:27:31 +00003379 if (!ItinData || ItinData->isEmpty())
Evan Chenge837dea2011-06-28 19:10:37 +00003380 return DefMCID.mayLoad() ? 3 : 1;
Evan Chenga0792de2010-10-06 06:27:31 +00003381
Evan Cheng08975152010-10-29 18:09:28 +00003382 if (!UseNode->isMachineOpcode()) {
Evan Chenge837dea2011-06-28 19:10:37 +00003383 int Latency = ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
Bob Wilsoneb1641d2012-09-29 21:43:49 +00003384 if (Subtarget.isLikeA9() || Subtarget.isSwift())
Evan Cheng08975152010-10-29 18:09:28 +00003385 return Latency <= 2 ? 1 : Latency - 1;
3386 else
3387 return Latency <= 3 ? 1 : Latency - 2;
3388 }
Evan Chenga0792de2010-10-06 06:27:31 +00003389
Evan Chenge837dea2011-06-28 19:10:37 +00003390 const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
Evan Chenga0792de2010-10-06 06:27:31 +00003391 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
3392 unsigned DefAlign = !DefMN->memoperands_empty()
3393 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
3394 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
3395 unsigned UseAlign = !UseMN->memoperands_empty()
3396 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Chenge837dea2011-06-28 19:10:37 +00003397 int Latency = getOperandLatency(ItinData, DefMCID, DefIdx, DefAlign,
3398 UseMCID, UseIdx, UseAlign);
Evan Cheng7e2fe912010-10-28 06:47:08 +00003399
3400 if (Latency > 1 &&
Silviu Baranga616471d2012-09-13 15:05:10 +00003401 (Subtarget.isCortexA8() || Subtarget.isLikeA9())) {
Evan Cheng7e2fe912010-10-28 06:47:08 +00003402 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
3403 // variants are one cycle cheaper.
Evan Chenge837dea2011-06-28 19:10:37 +00003404 switch (DefMCID.getOpcode()) {
Evan Cheng7e2fe912010-10-28 06:47:08 +00003405 default: break;
Jakob Stoklund Olesencff9baa2012-08-28 03:11:27 +00003406 case ARM::LDRrs:
3407 case ARM::LDRBrs: {
Evan Cheng7e2fe912010-10-28 06:47:08 +00003408 unsigned ShOpVal =
3409 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3410 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3411 if (ShImm == 0 ||
3412 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3413 --Latency;
3414 break;
3415 }
Jakob Stoklund Olesencff9baa2012-08-28 03:11:27 +00003416 case ARM::t2LDRs:
3417 case ARM::t2LDRBs:
3418 case ARM::t2LDRHs:
Evan Cheng7e2fe912010-10-28 06:47:08 +00003419 case ARM::t2LDRSHs: {
3420 // Thumb2 mode: lsl only.
3421 unsigned ShAmt =
3422 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3423 if (ShAmt == 0 || ShAmt == 2)
3424 --Latency;
3425 break;
3426 }
3427 }
Bob Wilsoneb1641d2012-09-29 21:43:49 +00003428 } else if (DefIdx == 0 && Latency > 2 && Subtarget.isSwift()) {
3429 // FIXME: Properly handle all of the latency adjustments for address
3430 // writeback.
3431 switch (DefMCID.getOpcode()) {
3432 default: break;
3433 case ARM::LDRrs:
3434 case ARM::LDRBrs: {
3435 unsigned ShOpVal =
3436 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
3437 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
3438 if (ShImm == 0 ||
3439 ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
3440 ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
3441 Latency -= 2;
3442 else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
3443 --Latency;
3444 break;
3445 }
3446 case ARM::t2LDRs:
3447 case ARM::t2LDRBs:
3448 case ARM::t2LDRHs:
3449 case ARM::t2LDRSHs: {
3450 // Thumb2 mode: lsl 0-3 only.
3451 Latency -= 2;
3452 break;
3453 }
3454 }
Evan Cheng7e2fe912010-10-28 06:47:08 +00003455 }
3456
Silviu Baranga616471d2012-09-13 15:05:10 +00003457 if (DefAlign < 8 && Subtarget.isLikeA9())
Evan Chenge837dea2011-06-28 19:10:37 +00003458 switch (DefMCID.getOpcode()) {
Evan Cheng75b41f12011-04-19 01:21:49 +00003459 default: break;
Jim Grosbach28f08c92012-03-05 19:33:30 +00003460 case ARM::VLD1q8:
3461 case ARM::VLD1q16:
3462 case ARM::VLD1q32:
3463 case ARM::VLD1q64:
3464 case ARM::VLD1q8wb_register:
3465 case ARM::VLD1q16wb_register:
3466 case ARM::VLD1q32wb_register:
3467 case ARM::VLD1q64wb_register:
3468 case ARM::VLD1q8wb_fixed:
3469 case ARM::VLD1q16wb_fixed:
3470 case ARM::VLD1q32wb_fixed:
3471 case ARM::VLD1q64wb_fixed:
3472 case ARM::VLD2d8:
3473 case ARM::VLD2d16:
3474 case ARM::VLD2d32:
Evan Cheng75b41f12011-04-19 01:21:49 +00003475 case ARM::VLD2q8Pseudo:
3476 case ARM::VLD2q16Pseudo:
3477 case ARM::VLD2q32Pseudo:
Jim Grosbach28f08c92012-03-05 19:33:30 +00003478 case ARM::VLD2d8wb_fixed:
3479 case ARM::VLD2d16wb_fixed:
3480 case ARM::VLD2d32wb_fixed:
Jim Grosbacha4e3c7f2011-12-09 21:28:25 +00003481 case ARM::VLD2q8PseudoWB_fixed:
3482 case ARM::VLD2q16PseudoWB_fixed:
3483 case ARM::VLD2q32PseudoWB_fixed:
Jim Grosbach28f08c92012-03-05 19:33:30 +00003484 case ARM::VLD2d8wb_register:
3485 case ARM::VLD2d16wb_register:
3486 case ARM::VLD2d32wb_register:
Jim Grosbacha4e3c7f2011-12-09 21:28:25 +00003487 case ARM::VLD2q8PseudoWB_register:
3488 case ARM::VLD2q16PseudoWB_register:
3489 case ARM::VLD2q32PseudoWB_register:
Evan Cheng75b41f12011-04-19 01:21:49 +00003490 case ARM::VLD3d8Pseudo:
3491 case ARM::VLD3d16Pseudo:
3492 case ARM::VLD3d32Pseudo:
3493 case ARM::VLD1d64TPseudo:
3494 case ARM::VLD3d8Pseudo_UPD:
3495 case ARM::VLD3d16Pseudo_UPD:
3496 case ARM::VLD3d32Pseudo_UPD:
Evan Cheng75b41f12011-04-19 01:21:49 +00003497 case ARM::VLD3q8Pseudo_UPD:
3498 case ARM::VLD3q16Pseudo_UPD:
3499 case ARM::VLD3q32Pseudo_UPD:
3500 case ARM::VLD3q8oddPseudo:
3501 case ARM::VLD3q16oddPseudo:
3502 case ARM::VLD3q32oddPseudo:
3503 case ARM::VLD3q8oddPseudo_UPD:
3504 case ARM::VLD3q16oddPseudo_UPD:
3505 case ARM::VLD3q32oddPseudo_UPD:
3506 case ARM::VLD4d8Pseudo:
3507 case ARM::VLD4d16Pseudo:
3508 case ARM::VLD4d32Pseudo:
3509 case ARM::VLD1d64QPseudo:
3510 case ARM::VLD4d8Pseudo_UPD:
3511 case ARM::VLD4d16Pseudo_UPD:
3512 case ARM::VLD4d32Pseudo_UPD:
Evan Cheng75b41f12011-04-19 01:21:49 +00003513 case ARM::VLD4q8Pseudo_UPD:
3514 case ARM::VLD4q16Pseudo_UPD:
3515 case ARM::VLD4q32Pseudo_UPD:
3516 case ARM::VLD4q8oddPseudo:
3517 case ARM::VLD4q16oddPseudo:
3518 case ARM::VLD4q32oddPseudo:
3519 case ARM::VLD4q8oddPseudo_UPD:
3520 case ARM::VLD4q16oddPseudo_UPD:
3521 case ARM::VLD4q32oddPseudo_UPD:
Jim Grosbachc0fc4502012-03-06 22:01:44 +00003522 case ARM::VLD1DUPq8:
3523 case ARM::VLD1DUPq16:
3524 case ARM::VLD1DUPq32:
3525 case ARM::VLD1DUPq8wb_fixed:
3526 case ARM::VLD1DUPq16wb_fixed:
3527 case ARM::VLD1DUPq32wb_fixed:
3528 case ARM::VLD1DUPq8wb_register:
3529 case ARM::VLD1DUPq16wb_register:
3530 case ARM::VLD1DUPq32wb_register:
3531 case ARM::VLD2DUPd8:
3532 case ARM::VLD2DUPd16:
3533 case ARM::VLD2DUPd32:
3534 case ARM::VLD2DUPd8wb_fixed:
3535 case ARM::VLD2DUPd16wb_fixed:
3536 case ARM::VLD2DUPd32wb_fixed:
3537 case ARM::VLD2DUPd8wb_register:
3538 case ARM::VLD2DUPd16wb_register:
3539 case ARM::VLD2DUPd32wb_register:
Evan Cheng75b41f12011-04-19 01:21:49 +00003540 case ARM::VLD4DUPd8Pseudo:
3541 case ARM::VLD4DUPd16Pseudo:
3542 case ARM::VLD4DUPd32Pseudo:
3543 case ARM::VLD4DUPd8Pseudo_UPD:
3544 case ARM::VLD4DUPd16Pseudo_UPD:
3545 case ARM::VLD4DUPd32Pseudo_UPD:
3546 case ARM::VLD1LNq8Pseudo:
3547 case ARM::VLD1LNq16Pseudo:
3548 case ARM::VLD1LNq32Pseudo:
3549 case ARM::VLD1LNq8Pseudo_UPD:
3550 case ARM::VLD1LNq16Pseudo_UPD:
3551 case ARM::VLD1LNq32Pseudo_UPD:
3552 case ARM::VLD2LNd8Pseudo:
3553 case ARM::VLD2LNd16Pseudo:
3554 case ARM::VLD2LNd32Pseudo:
3555 case ARM::VLD2LNq16Pseudo:
3556 case ARM::VLD2LNq32Pseudo:
3557 case ARM::VLD2LNd8Pseudo_UPD:
3558 case ARM::VLD2LNd16Pseudo_UPD:
3559 case ARM::VLD2LNd32Pseudo_UPD:
3560 case ARM::VLD2LNq16Pseudo_UPD:
3561 case ARM::VLD2LNq32Pseudo_UPD:
3562 case ARM::VLD4LNd8Pseudo:
3563 case ARM::VLD4LNd16Pseudo:
3564 case ARM::VLD4LNd32Pseudo:
3565 case ARM::VLD4LNq16Pseudo:
3566 case ARM::VLD4LNq32Pseudo:
3567 case ARM::VLD4LNd8Pseudo_UPD:
3568 case ARM::VLD4LNd16Pseudo_UPD:
3569 case ARM::VLD4LNd32Pseudo_UPD:
3570 case ARM::VLD4LNq16Pseudo_UPD:
3571 case ARM::VLD4LNq32Pseudo_UPD:
3572 // If the address is not 64-bit aligned, the latencies of these
3573 // instructions increases by one.
3574 ++Latency;
3575 break;
3576 }
3577
Evan Cheng7e2fe912010-10-28 06:47:08 +00003578 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00003579}
Evan Cheng23128422010-10-19 18:58:51 +00003580
Andrew Trickb7e02892012-06-05 21:11:27 +00003581unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3582 const MachineInstr *MI,
3583 unsigned *PredCost) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00003584 if (MI->isCopyLike() || MI->isInsertSubreg() ||
3585 MI->isRegSequence() || MI->isImplicitDef())
3586 return 1;
3587
Andrew Tricked7a51e2012-06-07 19:41:55 +00003588 // An instruction scheduler typically runs on unbundled instructions, however
3589 // other passes may query the latency of a bundled instruction.
Evan Chengddfd1372011-12-14 02:11:42 +00003590 if (MI->isBundle()) {
Andrew Tricked7a51e2012-06-07 19:41:55 +00003591 unsigned Latency = 0;
Evan Chengddfd1372011-12-14 02:11:42 +00003592 MachineBasicBlock::const_instr_iterator I = MI;
3593 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
3594 while (++I != E && I->isInsideBundle()) {
3595 if (I->getOpcode() != ARM::t2IT)
3596 Latency += getInstrLatency(ItinData, I, PredCost);
3597 }
3598 return Latency;
3599 }
3600
Evan Chenge837dea2011-06-28 19:10:37 +00003601 const MCInstrDesc &MCID = MI->getDesc();
Andrew Tricked7a51e2012-06-07 19:41:55 +00003602 if (PredCost && (MCID.isCall() || MCID.hasImplicitDefOfPhysReg(ARM::CPSR))) {
Evan Cheng8239daf2010-11-03 00:45:17 +00003603 // When predicated, CPSR is an additional source operand for CPSR updating
3604 // instructions, this apparently increases their latencies.
3605 *PredCost = 1;
Andrew Tricked7a51e2012-06-07 19:41:55 +00003606 }
3607 // Be sure to call getStageLatency for an empty itinerary in case it has a
3608 // valid MinLatency property.
3609 if (!ItinData)
3610 return MI->mayLoad() ? 3 : 1;
3611
3612 unsigned Class = MCID.getSchedClass();
3613
3614 // For instructions with variable uops, use uops as latency.
Andrew Trick14ccc7b2012-07-02 19:12:29 +00003615 if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
Andrew Tricked7a51e2012-06-07 19:41:55 +00003616 return getNumMicroOps(ItinData, MI);
Andrew Trick14ccc7b2012-07-02 19:12:29 +00003617
Andrew Tricked7a51e2012-06-07 19:41:55 +00003618 // For the common case, fall back on the itinerary's latency.
Andrew Trick68b16542012-06-07 19:42:00 +00003619 unsigned Latency = ItinData->getStageLatency(Class);
3620
3621 // Adjust for dynamic def-side opcode variants not captured by the itinerary.
3622 unsigned DefAlign = MI->hasOneMemOperand()
3623 ? (*MI->memoperands_begin())->getAlignment() : 0;
3624 int Adj = adjustDefLatency(Subtarget, MI, &MCID, DefAlign);
3625 if (Adj >= 0 || (int)Latency > -Adj) {
3626 return Latency + Adj;
3627 }
3628 return Latency;
Evan Cheng8239daf2010-11-03 00:45:17 +00003629}
3630
3631int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
3632 SDNode *Node) const {
3633 if (!Node->isMachineOpcode())
3634 return 1;
3635
3636 if (!ItinData || ItinData->isEmpty())
3637 return 1;
3638
3639 unsigned Opcode = Node->getMachineOpcode();
3640 switch (Opcode) {
3641 default:
3642 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendling73fe34a2010-11-16 01:16:36 +00003643 case ARM::VLDMQIA:
Bill Wendling73fe34a2010-11-16 01:16:36 +00003644 case ARM::VSTMQIA:
Evan Cheng8239daf2010-11-03 00:45:17 +00003645 return 2;
Eric Christopher8b3ca622010-11-18 19:40:05 +00003646 }
Evan Cheng8239daf2010-11-03 00:45:17 +00003647}
3648
Evan Cheng23128422010-10-19 18:58:51 +00003649bool ARMBaseInstrInfo::
3650hasHighOperandLatency(const InstrItineraryData *ItinData,
3651 const MachineRegisterInfo *MRI,
3652 const MachineInstr *DefMI, unsigned DefIdx,
3653 const MachineInstr *UseMI, unsigned UseIdx) const {
3654 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3655 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
3656 if (Subtarget.isCortexA8() &&
3657 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
3658 // CortexA8 VFP instructions are not pipelined.
3659 return true;
3660
3661 // Hoist VFP / NEON instructions with 4 or higher latency.
Andrew Trick397f4e32012-06-07 19:42:04 +00003662 int Latency = computeOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx,
3663 /*FindMin=*/false);
Andrew Trickf3770712012-06-07 19:41:58 +00003664 if (Latency < 0)
3665 Latency = getInstrLatency(ItinData, DefMI);
Evan Cheng23128422010-10-19 18:58:51 +00003666 if (Latency <= 3)
3667 return false;
3668 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
3669 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
3670}
Evan Chengc8141df2010-10-26 02:08:50 +00003671
3672bool ARMBaseInstrInfo::
3673hasLowDefLatency(const InstrItineraryData *ItinData,
3674 const MachineInstr *DefMI, unsigned DefIdx) const {
3675 if (!ItinData || ItinData->isEmpty())
3676 return false;
3677
3678 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
3679 if (DDomain == ARMII::DomainGeneral) {
3680 unsigned DefClass = DefMI->getDesc().getSchedClass();
3681 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
3682 return (DefCycle != -1 && DefCycle <= 2);
3683 }
3684 return false;
3685}
Evan Cheng48575f62010-12-05 22:04:16 +00003686
Andrew Trick3be654f2011-09-21 02:20:46 +00003687bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr *MI,
3688 StringRef &ErrInfo) const {
3689 if (convertAddSubFlagsOpcode(MI->getOpcode())) {
3690 ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
3691 return false;
3692 }
3693 return true;
3694}
3695
Evan Cheng48575f62010-12-05 22:04:16 +00003696bool
3697ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
3698 unsigned &AddSubOpc,
3699 bool &NegAcc, bool &HasLane) const {
3700 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
3701 if (I == MLxEntryMap.end())
3702 return false;
3703
3704 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
3705 MulOpc = Entry.MulOpc;
3706 AddSubOpc = Entry.AddSubOpc;
3707 NegAcc = Entry.NegAcc;
3708 HasLane = Entry.HasLane;
3709 return true;
3710}
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003711
3712//===----------------------------------------------------------------------===//
3713// Execution domains.
3714//===----------------------------------------------------------------------===//
3715//
3716// Some instructions go down the NEON pipeline, some go down the VFP pipeline,
3717// and some can go down both. The vmov instructions go down the VFP pipeline,
3718// but they can be changed to vorr equivalents that are executed by the NEON
3719// pipeline.
3720//
3721// We use the following execution domain numbering:
3722//
Jakob Stoklund Olesen8bb3d3c2011-09-29 02:48:41 +00003723enum ARMExeDomain {
3724 ExeGeneric = 0,
3725 ExeVFP = 1,
3726 ExeNEON = 2
3727};
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003728//
3729// Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
3730//
3731std::pair<uint16_t, uint16_t>
3732ARMBaseInstrInfo::getExecutionDomain(const MachineInstr *MI) const {
Tim Northover3c8ad922012-08-17 11:32:52 +00003733 // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
3734 // if they are not predicated.
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003735 if (MI->getOpcode() == ARM::VMOVD && !isPredicated(MI))
Jakob Stoklund Olesen8bb3d3c2011-09-29 02:48:41 +00003736 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003737
Silviu Baranga616471d2012-09-13 15:05:10 +00003738 // A9-like cores are particularly picky about mixing the two and want these
Tim Northover3c8ad922012-08-17 11:32:52 +00003739 // converted.
Silviu Baranga616471d2012-09-13 15:05:10 +00003740 if (Subtarget.isLikeA9() && !isPredicated(MI) &&
Tim Northover3c8ad922012-08-17 11:32:52 +00003741 (MI->getOpcode() == ARM::VMOVRS ||
Tim Northoverc4a32e62012-08-30 10:17:45 +00003742 MI->getOpcode() == ARM::VMOVSR ||
3743 MI->getOpcode() == ARM::VMOVS))
Tim Northover3c8ad922012-08-17 11:32:52 +00003744 return std::make_pair(ExeVFP, (1<<ExeVFP) | (1<<ExeNEON));
3745
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003746 // No other instructions can be swizzled, so just determine their domain.
3747 unsigned Domain = MI->getDesc().TSFlags & ARMII::DomainMask;
3748
3749 if (Domain & ARMII::DomainNEON)
Jakob Stoklund Olesen8bb3d3c2011-09-29 02:48:41 +00003750 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003751
3752 // Certain instructions can go either way on Cortex-A8.
3753 // Treat them as NEON instructions.
3754 if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
Jakob Stoklund Olesen8bb3d3c2011-09-29 02:48:41 +00003755 return std::make_pair(ExeNEON, 0);
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003756
3757 if (Domain & ARMII::DomainVFP)
Jakob Stoklund Olesen8bb3d3c2011-09-29 02:48:41 +00003758 return std::make_pair(ExeVFP, 0);
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003759
Jakob Stoklund Olesen8bb3d3c2011-09-29 02:48:41 +00003760 return std::make_pair(ExeGeneric, 0);
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003761}
3762
Tim Northover20599ea2012-08-29 16:36:07 +00003763static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
3764 unsigned SReg, unsigned &Lane) {
3765 unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
3766 Lane = 0;
3767
3768 if (DReg != ARM::NoRegister)
3769 return DReg;
3770
3771 Lane = 1;
3772 DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
3773
3774 assert(DReg && "S-register with no D super-register?");
3775 return DReg;
3776}
3777
Andrew Trick2d15d642012-10-10 05:43:01 +00003778/// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
James Molloy97ecb832012-09-18 08:31:15 +00003779/// set ImplicitSReg to a register number that must be marked as implicit-use or
3780/// zero if no register needs to be defined as implicit-use.
3781///
3782/// If the function cannot determine if an SPR should be marked implicit use or
3783/// not, it returns false.
3784///
3785/// This function handles cases where an instruction is being modified from taking
Andrew Trick2d15d642012-10-10 05:43:01 +00003786/// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
James Molloy97ecb832012-09-18 08:31:15 +00003787/// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
3788/// lane of the DPR).
3789///
3790/// If the other SPR is defined, an implicit-use of it should be added. Else,
3791/// (including the case where the DPR itself is defined), it should not.
Andrew Trick2d15d642012-10-10 05:43:01 +00003792///
James Molloy97ecb832012-09-18 08:31:15 +00003793static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
3794 MachineInstr *MI,
3795 unsigned DReg, unsigned Lane,
3796 unsigned &ImplicitSReg) {
3797 // If the DPR is defined or used already, the other SPR lane will be chained
3798 // correctly, so there is nothing to be done.
3799 if (MI->definesRegister(DReg, TRI) || MI->readsRegister(DReg, TRI)) {
3800 ImplicitSReg = 0;
3801 return true;
3802 }
3803
3804 // Otherwise we need to go searching to see if the SPR is set explicitly.
3805 ImplicitSReg = TRI->getSubReg(DReg,
3806 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
3807 MachineBasicBlock::LivenessQueryResult LQR =
3808 MI->getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
3809
3810 if (LQR == MachineBasicBlock::LQR_Live)
3811 return true;
3812 else if (LQR == MachineBasicBlock::LQR_Unknown)
3813 return false;
3814
3815 // If the register is known not to be live, there is no need to add an
3816 // implicit-use.
3817 ImplicitSReg = 0;
3818 return true;
3819}
Tim Northover20599ea2012-08-29 16:36:07 +00003820
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003821void
3822ARMBaseInstrInfo::setExecutionDomain(MachineInstr *MI, unsigned Domain) const {
Tim Northover3c8ad922012-08-17 11:32:52 +00003823 unsigned DstReg, SrcReg, DReg;
3824 unsigned Lane;
Jakob Stoklund Olesen37a942c2012-12-19 21:31:56 +00003825 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Tim Northover3c8ad922012-08-17 11:32:52 +00003826 const TargetRegisterInfo *TRI = &getRegisterInfo();
Tim Northover3c8ad922012-08-17 11:32:52 +00003827 switch (MI->getOpcode()) {
3828 default:
3829 llvm_unreachable("cannot handle opcode!");
3830 break;
3831 case ARM::VMOVD:
3832 if (Domain != ExeNEON)
3833 break;
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00003834
Tim Northover3c8ad922012-08-17 11:32:52 +00003835 // Zap the predicate operands.
3836 assert(!isPredicated(MI) && "Cannot predicate a VORRd");
Jakob Stoklund Olesen8bb3d3c2011-09-29 02:48:41 +00003837
Tim Northover20599ea2012-08-29 16:36:07 +00003838 // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
3839 DstReg = MI->getOperand(0).getReg();
3840 SrcReg = MI->getOperand(1).getReg();
3841
3842 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
3843 MI->RemoveOperand(i-1);
3844
3845 // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
Tim Northover3c8ad922012-08-17 11:32:52 +00003846 MI->setDesc(get(ARM::VORRd));
Tim Northover20599ea2012-08-29 16:36:07 +00003847 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
3848 .addReg(SrcReg)
3849 .addReg(SrcReg));
Tim Northover3c8ad922012-08-17 11:32:52 +00003850 break;
3851 case ARM::VMOVRS:
3852 if (Domain != ExeNEON)
3853 break;
3854 assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
3855
Tim Northover20599ea2012-08-29 16:36:07 +00003856 // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
Tim Northover3c8ad922012-08-17 11:32:52 +00003857 DstReg = MI->getOperand(0).getReg();
3858 SrcReg = MI->getOperand(1).getReg();
3859
Tim Northover20599ea2012-08-29 16:36:07 +00003860 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
3861 MI->RemoveOperand(i-1);
Tim Northover3c8ad922012-08-17 11:32:52 +00003862
Tim Northover20599ea2012-08-29 16:36:07 +00003863 DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
Tim Northover3c8ad922012-08-17 11:32:52 +00003864
Tim Northover20599ea2012-08-29 16:36:07 +00003865 // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
3866 // Note that DSrc has been widened and the other lane may be undef, which
3867 // contaminates the entire register.
Tim Northover3c8ad922012-08-17 11:32:52 +00003868 MI->setDesc(get(ARM::VGETLNi32));
Tim Northover20599ea2012-08-29 16:36:07 +00003869 AddDefaultPred(MIB.addReg(DstReg, RegState::Define)
3870 .addReg(DReg, RegState::Undef)
3871 .addImm(Lane));
Tim Northover3c8ad922012-08-17 11:32:52 +00003872
Tim Northover20599ea2012-08-29 16:36:07 +00003873 // The old source should be an implicit use, otherwise we might think it
3874 // was dead before here.
Tim Northover3c8ad922012-08-17 11:32:52 +00003875 MIB.addReg(SrcReg, RegState::Implicit);
Tim Northover3c8ad922012-08-17 11:32:52 +00003876 break;
James Molloy97ecb832012-09-18 08:31:15 +00003877 case ARM::VMOVSR: {
Tim Northover3c8ad922012-08-17 11:32:52 +00003878 if (Domain != ExeNEON)
3879 break;
3880 assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
3881
Tim Northover20599ea2012-08-29 16:36:07 +00003882 // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
Tim Northover3c8ad922012-08-17 11:32:52 +00003883 DstReg = MI->getOperand(0).getReg();
3884 SrcReg = MI->getOperand(1).getReg();
Tim Northover3c8ad922012-08-17 11:32:52 +00003885
Tim Northover20599ea2012-08-29 16:36:07 +00003886 DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
3887
James Molloy97ecb832012-09-18 08:31:15 +00003888 unsigned ImplicitSReg;
3889 if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
3890 break;
Tim Northover89f49802012-09-01 18:07:29 +00003891
Tim Northover7bebddf2012-09-05 18:37:53 +00003892 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
3893 MI->RemoveOperand(i-1);
3894
Tim Northover20599ea2012-08-29 16:36:07 +00003895 // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
3896 // Again DDst may be undefined at the beginning of this instruction.
Tim Northover3c8ad922012-08-17 11:32:52 +00003897 MI->setDesc(get(ARM::VSETLNi32));
Tim Northover89f49802012-09-01 18:07:29 +00003898 MIB.addReg(DReg, RegState::Define)
3899 .addReg(DReg, getUndefRegState(!MI->readsRegister(DReg, TRI)))
3900 .addReg(SrcReg)
3901 .addImm(Lane);
3902 AddDefaultPred(MIB);
Tim Northoverc4a32e62012-08-30 10:17:45 +00003903
Tim Northover89f49802012-09-01 18:07:29 +00003904 // The narrower destination must be marked as set to keep previous chains
3905 // in place.
Tim Northover20599ea2012-08-29 16:36:07 +00003906 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloy97ecb832012-09-18 08:31:15 +00003907 if (ImplicitSReg != 0)
3908 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northover3c8ad922012-08-17 11:32:52 +00003909 break;
James Molloy97ecb832012-09-18 08:31:15 +00003910 }
Tim Northoverc4a32e62012-08-30 10:17:45 +00003911 case ARM::VMOVS: {
3912 if (Domain != ExeNEON)
3913 break;
3914
3915 // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
3916 DstReg = MI->getOperand(0).getReg();
3917 SrcReg = MI->getOperand(1).getReg();
3918
Tim Northoverc4a32e62012-08-30 10:17:45 +00003919 unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
3920 DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
3921 DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
3922
James Molloy97ecb832012-09-18 08:31:15 +00003923 unsigned ImplicitSReg;
3924 if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
3925 break;
Tim Northover89f49802012-09-01 18:07:29 +00003926
Tim Northover7bebddf2012-09-05 18:37:53 +00003927 for (unsigned i = MI->getDesc().getNumOperands(); i; --i)
3928 MI->RemoveOperand(i-1);
3929
Tim Northoverc4a32e62012-08-30 10:17:45 +00003930 if (DSrc == DDst) {
3931 // Destination can be:
3932 // %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
3933 MI->setDesc(get(ARM::VDUPLN32d));
Tim Northover89f49802012-09-01 18:07:29 +00003934 MIB.addReg(DDst, RegState::Define)
3935 .addReg(DDst, getUndefRegState(!MI->readsRegister(DDst, TRI)))
3936 .addImm(SrcLane);
3937 AddDefaultPred(MIB);
Tim Northoverc4a32e62012-08-30 10:17:45 +00003938
3939 // Neither the source or the destination are naturally represented any
3940 // more, so add them in manually.
3941 MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
3942 MIB.addReg(SrcReg, RegState::Implicit);
James Molloy97ecb832012-09-18 08:31:15 +00003943 if (ImplicitSReg != 0)
3944 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverc4a32e62012-08-30 10:17:45 +00003945 break;
3946 }
3947
3948 // In general there's no single instruction that can perform an S <-> S
3949 // move in NEON space, but a pair of VEXT instructions *can* do the
3950 // job. It turns out that the VEXTs needed will only use DSrc once, with
3951 // the position based purely on the combination of lane-0 and lane-1
3952 // involved. For example
3953 // vmov s0, s2 -> vext.32 d0, d0, d1, #1 vext.32 d0, d0, d0, #1
3954 // vmov s1, s3 -> vext.32 d0, d1, d0, #1 vext.32 d0, d0, d0, #1
3955 // vmov s0, s3 -> vext.32 d0, d0, d0, #1 vext.32 d0, d1, d0, #1
3956 // vmov s1, s2 -> vext.32 d0, d0, d0, #1 vext.32 d0, d0, d1, #1
3957 //
3958 // Pattern of the MachineInstrs is:
3959 // %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
3960 MachineInstrBuilder NewMIB;
3961 NewMIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
3962 get(ARM::VEXTd32), DDst);
Tim Northover89f49802012-09-01 18:07:29 +00003963
3964 // On the first instruction, both DSrc and DDst may be <undef> if present.
3965 // Specifically when the original instruction didn't have them as an
3966 // <imp-use>.
3967 unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
3968 bool CurUndef = !MI->readsRegister(CurReg, TRI);
3969 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
3970
3971 CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
3972 CurUndef = !MI->readsRegister(CurReg, TRI);
3973 NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
3974
Tim Northoverc4a32e62012-08-30 10:17:45 +00003975 NewMIB.addImm(1);
3976 AddDefaultPred(NewMIB);
3977
3978 if (SrcLane == DstLane)
3979 NewMIB.addReg(SrcReg, RegState::Implicit);
3980
3981 MI->setDesc(get(ARM::VEXTd32));
3982 MIB.addReg(DDst, RegState::Define);
Tim Northover89f49802012-09-01 18:07:29 +00003983
3984 // On the second instruction, DDst has definitely been defined above, so
3985 // it is not <undef>. DSrc, if present, can be <undef> as above.
3986 CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
3987 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
3988 MIB.addReg(CurReg, getUndefRegState(CurUndef));
3989
3990 CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
3991 CurUndef = CurReg == DSrc && !MI->readsRegister(CurReg, TRI);
3992 MIB.addReg(CurReg, getUndefRegState(CurUndef));
3993
Tim Northoverc4a32e62012-08-30 10:17:45 +00003994 MIB.addImm(1);
3995 AddDefaultPred(MIB);
3996
3997 if (SrcLane != DstLane)
3998 MIB.addReg(SrcReg, RegState::Implicit);
3999
4000 // As before, the original destination is no longer represented, add it
4001 // implicitly.
4002 MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
James Molloy97ecb832012-09-18 08:31:15 +00004003 if (ImplicitSReg != 0)
4004 MIB.addReg(ImplicitSReg, RegState::Implicit);
Tim Northoverc4a32e62012-08-30 10:17:45 +00004005 break;
4006 }
Tim Northover3c8ad922012-08-17 11:32:52 +00004007 }
4008
Jakob Stoklund Olesen13fd6012011-09-27 22:57:21 +00004009}
Jim Grosbachc01810e2012-02-28 23:53:30 +00004010
Bob Wilsoneb1641d2012-09-29 21:43:49 +00004011//===----------------------------------------------------------------------===//
4012// Partial register updates
4013//===----------------------------------------------------------------------===//
4014//
4015// Swift renames NEON registers with 64-bit granularity. That means any
4016// instruction writing an S-reg implicitly reads the containing D-reg. The
4017// problem is mostly avoided by translating f32 operations to v2f32 operations
4018// on D-registers, but f32 loads are still a problem.
4019//
4020// These instructions can load an f32 into a NEON register:
4021//
4022// VLDRS - Only writes S, partial D update.
4023// VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
4024// VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
4025//
4026// FCONSTD can be used as a dependency-breaking instruction.
4027
4028
4029unsigned ARMBaseInstrInfo::
4030getPartialRegUpdateClearance(const MachineInstr *MI,
4031 unsigned OpNum,
4032 const TargetRegisterInfo *TRI) const {
4033 // Only Swift has partial register update problems.
4034 if (!SwiftPartialUpdateClearance || !Subtarget.isSwift())
4035 return 0;
4036
4037 assert(TRI && "Need TRI instance");
4038
4039 const MachineOperand &MO = MI->getOperand(OpNum);
4040 if (MO.readsReg())
4041 return 0;
4042 unsigned Reg = MO.getReg();
4043 int UseOp = -1;
4044
4045 switch(MI->getOpcode()) {
4046 // Normal instructions writing only an S-register.
4047 case ARM::VLDRS:
4048 case ARM::FCONSTS:
4049 case ARM::VMOVSR:
4050 // rdar://problem/8791586
4051 case ARM::VMOVv8i8:
4052 case ARM::VMOVv4i16:
4053 case ARM::VMOVv2i32:
4054 case ARM::VMOVv2f32:
4055 case ARM::VMOVv1i64:
4056 UseOp = MI->findRegisterUseOperandIdx(Reg, false, TRI);
4057 break;
4058
4059 // Explicitly reads the dependency.
4060 case ARM::VLD1LNd32:
4061 UseOp = 1;
4062 break;
4063 default:
4064 return 0;
4065 }
4066
4067 // If this instruction actually reads a value from Reg, there is no unwanted
4068 // dependency.
4069 if (UseOp != -1 && MI->getOperand(UseOp).readsReg())
4070 return 0;
4071
4072 // We must be able to clobber the whole D-reg.
4073 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
4074 // Virtual register must be a foo:ssub_0<def,undef> operand.
4075 if (!MO.getSubReg() || MI->readsVirtualRegister(Reg))
4076 return 0;
4077 } else if (ARM::SPRRegClass.contains(Reg)) {
4078 // Physical register: MI must define the full D-reg.
4079 unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
4080 &ARM::DPRRegClass);
4081 if (!DReg || !MI->definesRegister(DReg, TRI))
4082 return 0;
4083 }
4084
4085 // MI has an unwanted D-register dependency.
4086 // Avoid defs in the previous N instructrions.
4087 return SwiftPartialUpdateClearance;
4088}
4089
4090// Break a partial register dependency after getPartialRegUpdateClearance
4091// returned non-zero.
4092void ARMBaseInstrInfo::
4093breakPartialRegDependency(MachineBasicBlock::iterator MI,
4094 unsigned OpNum,
4095 const TargetRegisterInfo *TRI) const {
4096 assert(MI && OpNum < MI->getDesc().getNumDefs() && "OpNum is not a def");
4097 assert(TRI && "Need TRI instance");
4098
4099 const MachineOperand &MO = MI->getOperand(OpNum);
4100 unsigned Reg = MO.getReg();
4101 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
4102 "Can't break virtual register dependencies.");
4103 unsigned DReg = Reg;
4104
4105 // If MI defines an S-reg, find the corresponding D super-register.
4106 if (ARM::SPRRegClass.contains(Reg)) {
4107 DReg = ARM::D0 + (Reg - ARM::S0) / 2;
4108 assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
4109 }
4110
4111 assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
4112 assert(MI->definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
4113
4114 // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
4115 // the full D-register by loading the same value to both lanes. The
4116 // instruction is micro-coded with 2 uops, so don't do this until we can
4117 // properly schedule micro-coded instuctions. The dispatcher stalls cause
4118 // too big regressions.
4119
4120 // Insert the dependency-breaking FCONSTD before MI.
4121 // 96 is the encoding of 0.5, but the actual value doesn't matter here.
4122 AddDefaultPred(BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
4123 get(ARM::FCONSTD), DReg).addImm(96));
4124 MI->addRegisterKilled(DReg, TRI, true);
4125}
4126
Jim Grosbachc01810e2012-02-28 23:53:30 +00004127bool ARMBaseInstrInfo::hasNOP() const {
4128 return (Subtarget.getFeatureBits() & ARM::HasV6T2Ops) != 0;
4129}