blob: a53d8cbfd705d56b4fa990e3793ee350276bc3b4 [file] [log] [blame]
Jim Grosbach31c24bf2009-11-07 22:00:39 +00001//===- ARMBaseInstrInfo.cpp - ARM Instruction Information -------*- C++ -*-===//
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"
16#include "ARMAddressingModes.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"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000020#include "ARMRegisterInfo.h"
Chris Lattner4dbbe342010-07-20 21:17:29 +000021#include "ARMGenInstrInfo.inc"
Evan Chengfdc83402009-11-08 00:15:23 +000022#include "llvm/Constants.h"
23#include "llvm/Function.h"
24#include "llvm/GlobalValue.h"
David Goodwin334c2642009-07-08 16:09:28 +000025#include "llvm/CodeGen/LiveVariables.h"
Evan Chengd457e6e2009-11-07 04:04:34 +000026#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwin334c2642009-07-08 16:09:28 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov249fb332009-10-07 00:06:35 +000030#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng2457f2c2010-05-22 01:47:14 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov249fb332009-10-07 00:06:35 +000032#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000033#include "llvm/MC/MCAsmInfo.h"
David Goodwin334c2642009-07-08 16:09:28 +000034#include "llvm/Support/CommandLine.h"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000035#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
Bill Wendling40a5eb12010-11-01 20:41:43 +000037#include "llvm/ADT/STLExtras.h"
David Goodwin334c2642009-07-08 16:09:28 +000038using namespace llvm;
39
40static cl::opt<bool>
41EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
42 cl::desc("Enable ARM 2-addr to 3-addr conv"));
43
Andrew Trick2da8bc82010-12-24 05:03:26 +000044// Other targets already have a hazard recognizer enabled by default, so this
45// flag currently only affects ARM. It will be generalized when it becomes a
46// disabled flag.
47static cl::opt<bool> EnableHazardRecognizer(
48 "enable-sched-hazard", cl::Hidden,
49 cl::desc("Enable hazard detection during preRA scheduling"),
50 cl::init(false));
Evan Cheng48575f62010-12-05 22:04:16 +000051
52/// ARM_MLxEntry - Record information about MLA / MLS instructions.
53struct ARM_MLxEntry {
54 unsigned MLxOpc; // MLA / MLS opcode
55 unsigned MulOpc; // Expanded multiplication opcode
56 unsigned AddSubOpc; // Expanded add / sub opcode
57 bool NegAcc; // True if the acc is negated before the add / sub.
58 bool HasLane; // True if instruction has an extra "lane" operand.
59};
60
61static const ARM_MLxEntry ARM_MLxTable[] = {
62 // MLxOpc, MulOpc, AddSubOpc, NegAcc, HasLane
63 // fp scalar ops
64 { ARM::VMLAS, ARM::VMULS, ARM::VADDS, false, false },
65 { ARM::VMLSS, ARM::VMULS, ARM::VSUBS, false, false },
66 { ARM::VMLAD, ARM::VMULD, ARM::VADDD, false, false },
67 { ARM::VMLSD, ARM::VMULD, ARM::VSUBD, false, false },
Evan Cheng48575f62010-12-05 22:04:16 +000068 { ARM::VNMLAS, ARM::VNMULS, ARM::VSUBS, true, false },
69 { ARM::VNMLSS, ARM::VMULS, ARM::VSUBS, true, false },
70 { ARM::VNMLAD, ARM::VNMULD, ARM::VSUBD, true, false },
71 { ARM::VNMLSD, ARM::VMULD, ARM::VSUBD, true, false },
72
73 // fp SIMD ops
74 { ARM::VMLAfd, ARM::VMULfd, ARM::VADDfd, false, false },
75 { ARM::VMLSfd, ARM::VMULfd, ARM::VSUBfd, false, false },
76 { ARM::VMLAfq, ARM::VMULfq, ARM::VADDfq, false, false },
77 { ARM::VMLSfq, ARM::VMULfq, ARM::VSUBfq, false, false },
78 { ARM::VMLAslfd, ARM::VMULslfd, ARM::VADDfd, false, true },
79 { ARM::VMLSslfd, ARM::VMULslfd, ARM::VSUBfd, false, true },
80 { ARM::VMLAslfq, ARM::VMULslfq, ARM::VADDfq, false, true },
81 { ARM::VMLSslfq, ARM::VMULslfq, ARM::VSUBfq, false, true },
82};
83
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000084ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
85 : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)),
86 Subtarget(STI) {
Evan Cheng48575f62010-12-05 22:04:16 +000087 for (unsigned i = 0, e = array_lengthof(ARM_MLxTable); i != e; ++i) {
88 if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
89 assert(false && "Duplicated entries?");
90 MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
91 MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
92 }
93}
94
Andrew Trick2da8bc82010-12-24 05:03:26 +000095// Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
96// currently defaults to no prepass hazard recognizer.
Evan Cheng48575f62010-12-05 22:04:16 +000097ScheduleHazardRecognizer *ARMBaseInstrInfo::
Andrew Trick2da8bc82010-12-24 05:03:26 +000098CreateTargetHazardRecognizer(const TargetMachine *TM,
99 const ScheduleDAG *DAG) const {
100 if (EnableHazardRecognizer) {
101 const InstrItineraryData *II = TM->getInstrItineraryData();
102 return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
103 }
104 return TargetInstrInfoImpl::CreateTargetHazardRecognizer(TM, DAG);
105}
106
107ScheduleHazardRecognizer *ARMBaseInstrInfo::
108CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
109 const ScheduleDAG *DAG) const {
Evan Cheng48575f62010-12-05 22:04:16 +0000110 if (Subtarget.isThumb2() || Subtarget.hasVFP2())
111 return (ScheduleHazardRecognizer *)
Andrew Trick2da8bc82010-12-24 05:03:26 +0000112 new ARMHazardRecognizer(II, *this, getRegisterInfo(), Subtarget, DAG);
113 return TargetInstrInfoImpl::CreateTargetPostRAHazardRecognizer(II, DAG);
David Goodwin334c2642009-07-08 16:09:28 +0000114}
115
116MachineInstr *
117ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
118 MachineBasicBlock::iterator &MBBI,
119 LiveVariables *LV) const {
Evan Cheng78703dd2009-07-27 18:44:00 +0000120 // FIXME: Thumb2 support.
121
David Goodwin334c2642009-07-08 16:09:28 +0000122 if (!EnableARM3Addr)
123 return NULL;
124
125 MachineInstr *MI = MBBI;
126 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000127 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwin334c2642009-07-08 16:09:28 +0000128 bool isPre = false;
129 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
130 default: return NULL;
131 case ARMII::IndexModePre:
132 isPre = true;
133 break;
134 case ARMII::IndexModePost:
135 break;
136 }
137
138 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
139 // operation.
140 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
141 if (MemOpc == 0)
142 return NULL;
143
144 MachineInstr *UpdateMI = NULL;
145 MachineInstr *MemMI = NULL;
146 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
147 const TargetInstrDesc &TID = MI->getDesc();
148 unsigned NumOps = TID.getNumOperands();
149 bool isLoad = !TID.mayStore();
150 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
151 const MachineOperand &Base = MI->getOperand(2);
152 const MachineOperand &Offset = MI->getOperand(NumOps-3);
153 unsigned WBReg = WB.getReg();
154 unsigned BaseReg = Base.getReg();
155 unsigned OffReg = Offset.getReg();
156 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
157 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
158 switch (AddrMode) {
159 default:
160 assert(false && "Unknown indexed op!");
161 return NULL;
162 case ARMII::AddrMode2: {
163 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
164 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
165 if (OffReg == 0) {
Evan Chenge7cbe412009-07-08 21:03:57 +0000166 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwin334c2642009-07-08 16:09:28 +0000167 // Can't encode it in a so_imm operand. This transformation will
168 // add more than 1 instruction. Abandon!
169 return NULL;
170 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000171 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge7cbe412009-07-08 21:03:57 +0000172 .addReg(BaseReg).addImm(Amt)
David Goodwin334c2642009-07-08 16:09:28 +0000173 .addImm(Pred).addReg(0).addReg(0);
174 } else if (Amt != 0) {
175 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
176 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
177 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000178 get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000179 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
180 .addImm(Pred).addReg(0).addReg(0);
181 } else
182 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000183 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000184 .addReg(BaseReg).addReg(OffReg)
185 .addImm(Pred).addReg(0).addReg(0);
186 break;
187 }
188 case ARMII::AddrMode3 : {
189 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
190 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
191 if (OffReg == 0)
192 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
193 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000194 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000195 .addReg(BaseReg).addImm(Amt)
196 .addImm(Pred).addReg(0).addReg(0);
197 else
198 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000199 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000200 .addReg(BaseReg).addReg(OffReg)
201 .addImm(Pred).addReg(0).addReg(0);
202 break;
203 }
204 }
205
206 std::vector<MachineInstr*> NewMIs;
207 if (isPre) {
208 if (isLoad)
209 MemMI = BuildMI(MF, MI->getDebugLoc(),
210 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach3e556122010-10-26 22:37:02 +0000211 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwin334c2642009-07-08 16:09:28 +0000212 else
213 MemMI = BuildMI(MF, MI->getDebugLoc(),
214 get(MemOpc)).addReg(MI->getOperand(1).getReg())
215 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
216 NewMIs.push_back(MemMI);
217 NewMIs.push_back(UpdateMI);
218 } else {
219 if (isLoad)
220 MemMI = BuildMI(MF, MI->getDebugLoc(),
221 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach3e556122010-10-26 22:37:02 +0000222 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwin334c2642009-07-08 16:09:28 +0000223 else
224 MemMI = BuildMI(MF, MI->getDebugLoc(),
225 get(MemOpc)).addReg(MI->getOperand(1).getReg())
226 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
227 if (WB.isDead())
228 UpdateMI->getOperand(0).setIsDead();
229 NewMIs.push_back(UpdateMI);
230 NewMIs.push_back(MemMI);
231 }
232
233 // Transfer LiveVariables states, kill / dead info.
234 if (LV) {
235 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
236 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000237 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
David Goodwin334c2642009-07-08 16:09:28 +0000238 unsigned Reg = MO.getReg();
239
240 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
241 if (MO.isDef()) {
242 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
243 if (MO.isDead())
244 LV->addVirtualRegisterDead(Reg, NewMI);
245 }
246 if (MO.isUse() && MO.isKill()) {
247 for (unsigned j = 0; j < 2; ++j) {
248 // Look at the two new MI's in reverse order.
249 MachineInstr *NewMI = NewMIs[j];
250 if (!NewMI->readsRegister(Reg))
251 continue;
252 LV->addVirtualRegisterKilled(Reg, NewMI);
253 if (VI.removeKill(MI))
254 VI.Kills.push_back(NewMI);
255 break;
256 }
257 }
258 }
259 }
260 }
261
262 MFI->insert(MBBI, NewMIs[1]);
263 MFI->insert(MBBI, NewMIs[0]);
264 return NewMIs[0];
265}
266
267// Branch analysis.
268bool
269ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
270 MachineBasicBlock *&FBB,
271 SmallVectorImpl<MachineOperand> &Cond,
272 bool AllowModify) const {
273 // If the block has no terminators, it just falls into the block after it.
274 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000275 if (I == MBB.begin())
276 return false;
277 --I;
278 while (I->isDebugValue()) {
279 if (I == MBB.begin())
280 return false;
281 --I;
282 }
283 if (!isUnpredicatedTerminator(I))
David Goodwin334c2642009-07-08 16:09:28 +0000284 return false;
285
286 // Get the last instruction in the block.
287 MachineInstr *LastInst = I;
288
289 // If there is only one terminator instruction, process it.
290 unsigned LastOpc = LastInst->getOpcode();
291 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Evan Cheng5ca53a72009-07-27 18:20:05 +0000292 if (isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000293 TBB = LastInst->getOperand(0).getMBB();
294 return false;
295 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000296 if (isCondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000297 // Block ends with fall-through condbranch.
298 TBB = LastInst->getOperand(0).getMBB();
299 Cond.push_back(LastInst->getOperand(1));
300 Cond.push_back(LastInst->getOperand(2));
301 return false;
302 }
303 return true; // Can't handle indirect branch.
304 }
305
306 // Get the instruction before it if it is a terminator.
307 MachineInstr *SecondLastInst = I;
Evan Cheng108c8722010-09-23 06:54:40 +0000308 unsigned SecondLastOpc = SecondLastInst->getOpcode();
309
310 // If AllowModify is true and the block ends with two or more unconditional
311 // branches, delete all but the first unconditional branch.
312 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
313 while (isUncondBranchOpcode(SecondLastOpc)) {
314 LastInst->eraseFromParent();
315 LastInst = SecondLastInst;
316 LastOpc = LastInst->getOpcode();
Evan Cheng676e2582010-09-23 19:42:03 +0000317 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
318 // Return now the only terminator is an unconditional branch.
319 TBB = LastInst->getOperand(0).getMBB();
320 return false;
321 } else {
Evan Cheng108c8722010-09-23 06:54:40 +0000322 SecondLastInst = I;
323 SecondLastOpc = SecondLastInst->getOpcode();
324 }
325 }
326 }
David Goodwin334c2642009-07-08 16:09:28 +0000327
328 // If there are three terminators, we don't know what sort of block this is.
329 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
330 return true;
331
Evan Cheng5ca53a72009-07-27 18:20:05 +0000332 // If the block ends with a B and a Bcc, handle it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000333 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000334 TBB = SecondLastInst->getOperand(0).getMBB();
335 Cond.push_back(SecondLastInst->getOperand(1));
336 Cond.push_back(SecondLastInst->getOperand(2));
337 FBB = LastInst->getOperand(0).getMBB();
338 return false;
339 }
340
341 // If the block ends with two unconditional branches, handle it. The second
342 // one is not executed, so remove it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000343 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000344 TBB = SecondLastInst->getOperand(0).getMBB();
345 I = LastInst;
346 if (AllowModify)
347 I->eraseFromParent();
348 return false;
349 }
350
351 // ...likewise if it ends with a branch table followed by an unconditional
352 // branch. The branch folder can create these, and we must get rid of them for
353 // correctness of Thumb constant islands.
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000354 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
355 isIndirectBranchOpcode(SecondLastOpc)) &&
Evan Cheng5ca53a72009-07-27 18:20:05 +0000356 isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000357 I = LastInst;
358 if (AllowModify)
359 I->eraseFromParent();
360 return true;
361 }
362
363 // Otherwise, can't handle this.
364 return true;
365}
366
367
368unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwin334c2642009-07-08 16:09:28 +0000369 MachineBasicBlock::iterator I = MBB.end();
370 if (I == MBB.begin()) return 0;
371 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000372 while (I->isDebugValue()) {
373 if (I == MBB.begin())
374 return 0;
375 --I;
376 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000377 if (!isUncondBranchOpcode(I->getOpcode()) &&
378 !isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000379 return 0;
380
381 // Remove the branch.
382 I->eraseFromParent();
383
384 I = MBB.end();
385
386 if (I == MBB.begin()) return 1;
387 --I;
Evan Cheng5ca53a72009-07-27 18:20:05 +0000388 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000389 return 1;
390
391 // Remove the branch.
392 I->eraseFromParent();
393 return 2;
394}
395
396unsigned
397ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000398 MachineBasicBlock *FBB,
399 const SmallVectorImpl<MachineOperand> &Cond,
400 DebugLoc DL) const {
Evan Cheng6495f632009-07-28 05:48:47 +0000401 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
402 int BOpc = !AFI->isThumbFunction()
403 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
404 int BccOpc = !AFI->isThumbFunction()
405 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
David Goodwin334c2642009-07-08 16:09:28 +0000406
407 // Shouldn't be a fall through.
408 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
409 assert((Cond.size() == 2 || Cond.size() == 0) &&
410 "ARM branch conditions have two components!");
411
412 if (FBB == 0) {
413 if (Cond.empty()) // Unconditional branch?
Stuart Hastings3bf91252010-06-17 22:43:56 +0000414 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
David Goodwin334c2642009-07-08 16:09:28 +0000415 else
Stuart Hastings3bf91252010-06-17 22:43:56 +0000416 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000417 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
418 return 1;
419 }
420
421 // Two-way conditional branch.
Stuart Hastings3bf91252010-06-17 22:43:56 +0000422 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000423 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
Stuart Hastings3bf91252010-06-17 22:43:56 +0000424 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwin334c2642009-07-08 16:09:28 +0000425 return 2;
426}
427
428bool ARMBaseInstrInfo::
429ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
430 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
431 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
432 return false;
433}
434
David Goodwin334c2642009-07-08 16:09:28 +0000435bool ARMBaseInstrInfo::
436PredicateInstruction(MachineInstr *MI,
437 const SmallVectorImpl<MachineOperand> &Pred) const {
438 unsigned Opc = MI->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000439 if (isUncondBranchOpcode(Opc)) {
440 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
David Goodwin334c2642009-07-08 16:09:28 +0000441 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
442 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
443 return true;
444 }
445
446 int PIdx = MI->findFirstPredOperandIdx();
447 if (PIdx != -1) {
448 MachineOperand &PMO = MI->getOperand(PIdx);
449 PMO.setImm(Pred[0].getImm());
450 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
451 return true;
452 }
453 return false;
454}
455
456bool ARMBaseInstrInfo::
457SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
458 const SmallVectorImpl<MachineOperand> &Pred2) const {
459 if (Pred1.size() > 2 || Pred2.size() > 2)
460 return false;
461
462 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
463 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
464 if (CC1 == CC2)
465 return true;
466
467 switch (CC1) {
468 default:
469 return false;
470 case ARMCC::AL:
471 return true;
472 case ARMCC::HS:
473 return CC2 == ARMCC::HI;
474 case ARMCC::LS:
475 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
476 case ARMCC::GE:
477 return CC2 == ARMCC::GT;
478 case ARMCC::LE:
479 return CC2 == ARMCC::LT;
480 }
481}
482
483bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
484 std::vector<MachineOperand> &Pred) const {
Evan Cheng8fb90362009-08-08 03:20:32 +0000485 // FIXME: This confuses implicit_def with optional CPSR def.
David Goodwin334c2642009-07-08 16:09:28 +0000486 const TargetInstrDesc &TID = MI->getDesc();
487 if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
488 return false;
489
490 bool Found = false;
491 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
492 const MachineOperand &MO = MI->getOperand(i);
493 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
494 Pred.push_back(MO);
495 Found = true;
496 }
497 }
498
499 return Found;
500}
501
Evan Chengac0869d2009-11-21 06:21:52 +0000502/// isPredicable - Return true if the specified instruction can be predicated.
503/// By default, this returns true for every instruction with a
504/// PredicateOperand.
505bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
506 const TargetInstrDesc &TID = MI->getDesc();
507 if (!TID.isPredicable())
508 return false;
509
510 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
511 ARMFunctionInfo *AFI =
512 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
Evan Chengd7f08102009-11-24 08:06:15 +0000513 return AFI->isThumb2Function();
Evan Chengac0869d2009-11-21 06:21:52 +0000514 }
515 return true;
516}
David Goodwin334c2642009-07-08 16:09:28 +0000517
Chris Lattner56856b12009-12-03 06:58:32 +0000518/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
Chandler Carruth19e57022010-10-23 08:40:19 +0000519LLVM_ATTRIBUTE_NOINLINE
David Goodwin334c2642009-07-08 16:09:28 +0000520static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattner56856b12009-12-03 06:58:32 +0000521 unsigned JTI);
David Goodwin334c2642009-07-08 16:09:28 +0000522static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
523 unsigned JTI) {
Chris Lattner56856b12009-12-03 06:58:32 +0000524 assert(JTI < JT.size());
David Goodwin334c2642009-07-08 16:09:28 +0000525 return JT[JTI].MBBs.size();
526}
527
528/// GetInstSize - Return the size of the specified MachineInstr.
529///
530unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
531 const MachineBasicBlock &MBB = *MI->getParent();
532 const MachineFunction *MF = MBB.getParent();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000533 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwin334c2642009-07-08 16:09:28 +0000534
535 // Basic size info comes from the TSFlags field.
536 const TargetInstrDesc &TID = MI->getDesc();
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000537 uint64_t TSFlags = TID.TSFlags;
David Goodwin334c2642009-07-08 16:09:28 +0000538
Evan Chenga0ee8622009-07-31 22:22:22 +0000539 unsigned Opc = MI->getOpcode();
David Goodwin334c2642009-07-08 16:09:28 +0000540 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
541 default: {
542 // If this machine instr is an inline asm, measure it.
543 if (MI->getOpcode() == ARM::INLINEASM)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000544 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Goodwin334c2642009-07-08 16:09:28 +0000545 if (MI->isLabel())
546 return 0;
Evan Chenga0ee8622009-07-31 22:22:22 +0000547 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000548 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000549 llvm_unreachable("Unknown or unset size field for instr!");
Chris Lattner518bb532010-02-09 19:54:29 +0000550 case TargetOpcode::IMPLICIT_DEF:
551 case TargetOpcode::KILL:
Bill Wendling7431bea2010-07-16 22:20:36 +0000552 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000553 case TargetOpcode::EH_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +0000554 case TargetOpcode::DBG_VALUE:
David Goodwin334c2642009-07-08 16:09:28 +0000555 return 0;
556 }
557 break;
558 }
Evan Cheng78947622009-07-24 18:20:44 +0000559 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
560 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
561 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
David Goodwin334c2642009-07-08 16:09:28 +0000562 case ARMII::SizeSpecial: {
Evan Chenga0ee8622009-07-31 22:22:22 +0000563 switch (Opc) {
Jim Grosbach3c38f962010-10-06 22:01:26 +0000564 case ARM::MOVi32imm:
565 case ARM::t2MOVi32imm:
566 return 8;
David Goodwin334c2642009-07-08 16:09:28 +0000567 case ARM::CONSTPOOL_ENTRY:
568 // If this machine instr is a constant pool entry, its size is recorded as
569 // operand #2.
570 return MI->getOperand(2).getImm();
Jim Grosbach5eb19512010-05-22 01:06:18 +0000571 case ARM::Int_eh_sjlj_longjmp:
572 return 16;
573 case ARM::tInt_eh_sjlj_longjmp:
574 return 10;
Evan Cheng78947622009-07-24 18:20:44 +0000575 case ARM::Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000576 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000577 return 20;
Jim Grosbachd1228742009-12-01 18:10:36 +0000578 case ARM::tInt_eh_sjlj_setjmp:
Jim Grosbach5aa16842009-08-11 19:42:21 +0000579 case ARM::t2Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000580 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000581 return 12;
David Goodwin334c2642009-07-08 16:09:28 +0000582 case ARM::BR_JTr:
583 case ARM::BR_JTm:
584 case ARM::BR_JTadd:
Evan Chenga0ee8622009-07-31 22:22:22 +0000585 case ARM::tBR_JTr:
Evan Chengd26b14c2009-07-31 18:28:05 +0000586 case ARM::t2BR_JT:
Jim Grosbachd092a872010-11-29 21:28:32 +0000587 case ARM::t2TBB_JT:
588 case ARM::t2TBH_JT: {
David Goodwin334c2642009-07-08 16:09:28 +0000589 // These are jumptable branches, i.e. a branch followed by an inlined
Evan Chengd26b14c2009-07-31 18:28:05 +0000590 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
591 // entry is one byte; TBH two byte each.
Jim Grosbachd092a872010-11-29 21:28:32 +0000592 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
593 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
David Goodwin334c2642009-07-08 16:09:28 +0000594 unsigned NumOps = TID.getNumOperands();
595 MachineOperand JTOP =
596 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
597 unsigned JTI = JTOP.getIndex();
598 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000599 assert(MJTI != 0);
David Goodwin334c2642009-07-08 16:09:28 +0000600 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
601 assert(JTI < JT.size());
602 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
603 // 4 aligned. The assembler / linker may add 2 byte padding just before
604 // the JT entries. The size does not include this padding; the
605 // constant islands pass does separate bookkeeping for it.
606 // FIXME: If we know the size of the function is less than (1 << 16) *2
607 // bytes, we can use 16-bit entries instead. Then there won't be an
608 // alignment issue.
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000609 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
610 unsigned NumEntries = getNumJTEntries(JT, JTI);
Jim Grosbachd092a872010-11-29 21:28:32 +0000611 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000612 // Make sure the instruction that follows TBB is 2-byte aligned.
613 // FIXME: Constant island pass should insert an "ALIGN" instruction
614 // instead.
615 ++NumEntries;
616 return NumEntries * EntrySize + InstSize;
David Goodwin334c2642009-07-08 16:09:28 +0000617 }
618 default:
619 // Otherwise, pseudo-instruction sizes are zero.
620 return 0;
621 }
622 }
623 }
624 return 0; // Not reached
625}
626
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000627void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
628 MachineBasicBlock::iterator I, DebugLoc DL,
629 unsigned DestReg, unsigned SrcReg,
630 bool KillSrc) const {
631 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
632 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson1665b0a2010-02-16 17:24:15 +0000633
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000634 if (GPRDest && GPRSrc) {
635 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
636 .addReg(SrcReg, getKillRegState(KillSrc))));
637 return;
David Goodwin7bfdca02009-08-05 21:02:22 +0000638 }
David Goodwin334c2642009-07-08 16:09:28 +0000639
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000640 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
641 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
642
643 unsigned Opc;
644 if (SPRDest && SPRSrc)
645 Opc = ARM::VMOVS;
646 else if (GPRDest && SPRSrc)
647 Opc = ARM::VMOVRS;
648 else if (SPRDest && GPRSrc)
649 Opc = ARM::VMOVSR;
650 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
651 Opc = ARM::VMOVD;
652 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
653 Opc = ARM::VMOVQ;
654 else if (ARM::QQPRRegClass.contains(DestReg, SrcReg))
655 Opc = ARM::VMOVQQ;
656 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg))
657 Opc = ARM::VMOVQQQQ;
658 else
659 llvm_unreachable("Impossible reg-to-reg copy");
660
661 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
662 MIB.addReg(SrcReg, getKillRegState(KillSrc));
663 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ)
664 AddDefaultPred(MIB);
David Goodwin334c2642009-07-08 16:09:28 +0000665}
666
Evan Chengc10b5af2010-05-07 00:24:52 +0000667static const
668MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
669 unsigned Reg, unsigned SubIdx, unsigned State,
670 const TargetRegisterInfo *TRI) {
671 if (!SubIdx)
672 return MIB.addReg(Reg, State);
673
674 if (TargetRegisterInfo::isPhysicalRegister(Reg))
675 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
676 return MIB.addReg(Reg, State, SubIdx);
677}
678
David Goodwin334c2642009-07-08 16:09:28 +0000679void ARMBaseInstrInfo::
680storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
681 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000682 const TargetRegisterClass *RC,
683 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000684 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000685 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000686 MachineFunction &MF = *MBB.getParent();
687 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000688 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000689
690 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000691 MF.getMachineMemOperand(MachinePointerInfo(
692 PseudoSourceValue::getFixedStack(FI)),
693 MachineMemOperand::MOStore,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000694 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000695 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000696
Bob Wilson0eb0c742010-02-16 22:01:59 +0000697 // tGPR is used sometimes in ARM instructions that need to avoid using
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000698 // certain registers. Just treat it as GPR here. Likewise, rGPR.
699 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
700 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000701 RC = ARM::GPRRegisterClass;
702
Bob Wilsonebe99b22010-06-18 21:32:42 +0000703 switch (RC->getID()) {
704 case ARM::GPRRegClassID:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000705 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwin334c2642009-07-08 16:09:28 +0000706 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000707 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000708 break;
709 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000710 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
711 .addReg(SrcReg, getKillRegState(isKill))
712 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000713 break;
714 case ARM::DPRRegClassID:
715 case ARM::DPR_VFP2RegClassID:
716 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000717 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000718 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000719 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000720 break;
721 case ARM::QPRRegClassID:
722 case ARM::QPR_VFP2RegClassID:
723 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000724 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000725 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo))
Bob Wilsonf967ca02010-07-06 21:26:18 +0000726 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000727 .addReg(SrcReg, getKillRegState(isKill))
728 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000729 } else {
Bill Wendling73fe34a2010-11-16 01:16:36 +0000730 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng69b9f982010-05-13 01:12:06 +0000731 .addReg(SrcReg, getKillRegState(isKill))
732 .addFrameIndex(FI)
Evan Cheng69b9f982010-05-13 01:12:06 +0000733 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000734 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000735 break;
736 case ARM::QQPRRegClassID:
737 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000738 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng22c687b2010-05-14 02:13:41 +0000739 // FIXME: It's possible to only store part of the QQ register if the
740 // spilled def has a sub-register index.
Bob Wilson168f3822010-09-15 01:48:05 +0000741 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
742 .addFrameIndex(FI).addImm(16)
743 .addReg(SrcReg, getKillRegState(isKill))
744 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000745 } else {
746 MachineInstrBuilder MIB =
Bill Wendling73fe34a2010-11-16 01:16:36 +0000747 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
748 .addFrameIndex(FI))
Evan Cheng435d4992010-05-07 02:04:02 +0000749 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000750 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
751 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
752 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
753 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000754 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000755 break;
756 case ARM::QQQQPRRegClassID: {
Evan Cheng22c687b2010-05-14 02:13:41 +0000757 MachineInstrBuilder MIB =
Bill Wendling73fe34a2010-11-16 01:16:36 +0000758 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
759 .addFrameIndex(FI))
Evan Cheng22c687b2010-05-14 02:13:41 +0000760 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000761 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
762 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
763 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
764 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
765 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
766 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
767 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
768 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
Bob Wilsonebe99b22010-06-18 21:32:42 +0000769 break;
770 }
771 default:
772 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000773 }
774}
775
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000776unsigned
777ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
778 int &FrameIndex) const {
779 switch (MI->getOpcode()) {
780 default: break;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000781 case ARM::STRrs:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000782 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
783 if (MI->getOperand(1).isFI() &&
784 MI->getOperand(2).isReg() &&
785 MI->getOperand(3).isImm() &&
786 MI->getOperand(2).getReg() == 0 &&
787 MI->getOperand(3).getImm() == 0) {
788 FrameIndex = MI->getOperand(1).getIndex();
789 return MI->getOperand(0).getReg();
790 }
791 break;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000792 case ARM::STRi12:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000793 case ARM::t2STRi12:
794 case ARM::tSpill:
795 case ARM::VSTRD:
796 case ARM::VSTRS:
797 if (MI->getOperand(1).isFI() &&
798 MI->getOperand(2).isImm() &&
799 MI->getOperand(2).getImm() == 0) {
800 FrameIndex = MI->getOperand(1).getIndex();
801 return MI->getOperand(0).getReg();
802 }
803 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000804 case ARM::VST1q64Pseudo:
805 if (MI->getOperand(0).isFI() &&
806 MI->getOperand(2).getSubReg() == 0) {
807 FrameIndex = MI->getOperand(0).getIndex();
808 return MI->getOperand(2).getReg();
809 }
Jakob Stoklund Olesen31bbc512010-09-15 21:40:09 +0000810 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +0000811 case ARM::VSTMQIA:
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000812 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000813 MI->getOperand(0).getSubReg() == 0) {
814 FrameIndex = MI->getOperand(1).getIndex();
815 return MI->getOperand(0).getReg();
816 }
817 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000818 }
819
820 return 0;
821}
822
David Goodwin334c2642009-07-08 16:09:28 +0000823void ARMBaseInstrInfo::
824loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
825 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000826 const TargetRegisterClass *RC,
827 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000828 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000829 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000830 MachineFunction &MF = *MBB.getParent();
831 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000832 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000833 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000834 MF.getMachineMemOperand(
835 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
836 MachineMemOperand::MOLoad,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000837 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000838 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000839
Bob Wilson0eb0c742010-02-16 22:01:59 +0000840 // tGPR is used sometimes in ARM instructions that need to avoid using
841 // certain registers. Just treat it as GPR here.
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000842 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
843 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000844 RC = ARM::GPRRegisterClass;
845
Bob Wilsonebe99b22010-06-18 21:32:42 +0000846 switch (RC->getID()) {
847 case ARM::GPRRegClassID:
Jim Grosbach3e556122010-10-26 22:37:02 +0000848 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
849 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000850 break;
851 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000852 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
853 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000854 break;
855 case ARM::DPRRegClassID:
856 case ARM::DPR_VFP2RegClassID:
857 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000858 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000859 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000860 break;
861 case ARM::QPRRegClassID:
862 case ARM::QPR_VFP2RegClassID:
863 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000864 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000865 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg)
Bob Wilsonf967ca02010-07-06 21:26:18 +0000866 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000867 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000868 } else {
Bill Wendling73fe34a2010-11-16 01:16:36 +0000869 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
Evan Cheng69b9f982010-05-13 01:12:06 +0000870 .addFrameIndex(FI)
Evan Cheng69b9f982010-05-13 01:12:06 +0000871 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000872 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000873 break;
874 case ARM::QQPRRegClassID:
875 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000876 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000877 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
878 .addFrameIndex(FI).addImm(16)
879 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000880 } else {
881 MachineInstrBuilder MIB =
Bill Wendling73fe34a2010-11-16 01:16:36 +0000882 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
883 .addFrameIndex(FI))
Evan Cheng435d4992010-05-07 02:04:02 +0000884 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000885 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
886 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
887 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
888 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000889 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000890 break;
891 case ARM::QQQQPRRegClassID: {
892 MachineInstrBuilder MIB =
Bill Wendling73fe34a2010-11-16 01:16:36 +0000893 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
894 .addFrameIndex(FI))
Bob Wilsonebe99b22010-06-18 21:32:42 +0000895 .addMemOperand(MMO);
896 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
897 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
898 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
899 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
900 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
901 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
902 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
903 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
904 break;
905 }
906 default:
907 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000908 }
909}
910
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000911unsigned
912ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
913 int &FrameIndex) const {
914 switch (MI->getOpcode()) {
915 default: break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000916 case ARM::LDRrs:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000917 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
918 if (MI->getOperand(1).isFI() &&
919 MI->getOperand(2).isReg() &&
920 MI->getOperand(3).isImm() &&
921 MI->getOperand(2).getReg() == 0 &&
922 MI->getOperand(3).getImm() == 0) {
923 FrameIndex = MI->getOperand(1).getIndex();
924 return MI->getOperand(0).getReg();
925 }
926 break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000927 case ARM::LDRi12:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000928 case ARM::t2LDRi12:
929 case ARM::tRestore:
930 case ARM::VLDRD:
931 case ARM::VLDRS:
932 if (MI->getOperand(1).isFI() &&
933 MI->getOperand(2).isImm() &&
934 MI->getOperand(2).getImm() == 0) {
935 FrameIndex = MI->getOperand(1).getIndex();
936 return MI->getOperand(0).getReg();
937 }
938 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000939 case ARM::VLD1q64Pseudo:
940 if (MI->getOperand(1).isFI() &&
941 MI->getOperand(0).getSubReg() == 0) {
942 FrameIndex = MI->getOperand(1).getIndex();
943 return MI->getOperand(0).getReg();
944 }
945 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +0000946 case ARM::VLDMQIA:
Jakob Stoklund Olesen06f264e2010-09-15 21:40:11 +0000947 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen06f264e2010-09-15 21:40:11 +0000948 MI->getOperand(0).getSubReg() == 0) {
949 FrameIndex = MI->getOperand(1).getIndex();
950 return MI->getOperand(0).getReg();
951 }
952 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000953 }
954
955 return 0;
956}
957
Evan Cheng62b50652010-04-26 07:39:25 +0000958MachineInstr*
959ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000960 int FrameIx, uint64_t Offset,
Evan Cheng62b50652010-04-26 07:39:25 +0000961 const MDNode *MDPtr,
962 DebugLoc DL) const {
963 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
964 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
965 return &*MIB;
966}
967
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000968/// Create a copy of a const pool value. Update CPI to the new index and return
969/// the label UID.
970static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
971 MachineConstantPool *MCP = MF.getConstantPool();
972 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
973
974 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
975 assert(MCPE.isMachineConstantPoolEntry() &&
976 "Expecting a machine constantpool entry!");
977 ARMConstantPoolValue *ACPV =
978 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
979
980 unsigned PCLabelId = AFI->createConstPoolEntryUId();
981 ARMConstantPoolValue *NewCPV = 0;
Jim Grosbach51f5b672010-09-10 21:38:22 +0000982 // FIXME: The below assumes PIC relocation model and that the function
983 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
984 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
985 // instructions, so that's probably OK, but is PIC always correct when
986 // we get here?
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000987 if (ACPV->isGlobalValue())
988 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
989 ARMCP::CPValue, 4);
990 else if (ACPV->isExtSymbol())
991 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
992 ACPV->getSymbol(), PCLabelId, 4);
993 else if (ACPV->isBlockAddress())
994 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
995 ARMCP::CPBlockAddress, 4);
Jim Grosbach51f5b672010-09-10 21:38:22 +0000996 else if (ACPV->isLSDA())
997 NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId,
998 ARMCP::CPLSDA, 4);
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000999 else
1000 llvm_unreachable("Unexpected ARM constantpool value type!!");
1001 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1002 return PCLabelId;
1003}
1004
Evan Chengfdc83402009-11-08 00:15:23 +00001005void ARMBaseInstrInfo::
1006reMaterialize(MachineBasicBlock &MBB,
1007 MachineBasicBlock::iterator I,
1008 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +00001009 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001010 const TargetRegisterInfo &TRI) const {
Evan Chengfdc83402009-11-08 00:15:23 +00001011 unsigned Opcode = Orig->getOpcode();
1012 switch (Opcode) {
1013 default: {
1014 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001015 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfdc83402009-11-08 00:15:23 +00001016 MBB.insert(I, MI);
1017 break;
1018 }
1019 case ARM::tLDRpci_pic:
1020 case ARM::t2LDRpci_pic: {
1021 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +00001022 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001023 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +00001024 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1025 DestReg)
1026 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1027 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1028 break;
1029 }
1030 }
Evan Chengfdc83402009-11-08 00:15:23 +00001031}
1032
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001033MachineInstr *
1034ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1035 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1036 switch(Orig->getOpcode()) {
1037 case ARM::tLDRpci_pic:
1038 case ARM::t2LDRpci_pic: {
1039 unsigned CPI = Orig->getOperand(1).getIndex();
1040 unsigned PCLabelId = duplicateCPV(MF, CPI);
1041 Orig->getOperand(1).setIndex(CPI);
1042 Orig->getOperand(2).setImm(PCLabelId);
1043 break;
1044 }
1045 }
1046 return MI;
1047}
1048
Evan Cheng506049f2010-03-03 01:44:33 +00001049bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1050 const MachineInstr *MI1) const {
Evan Chengd457e6e2009-11-07 04:04:34 +00001051 int Opcode = MI0->getOpcode();
Evan Cheng9b824252009-11-20 02:10:27 +00001052 if (Opcode == ARM::t2LDRpci ||
1053 Opcode == ARM::t2LDRpci_pic ||
1054 Opcode == ARM::tLDRpci ||
1055 Opcode == ARM::tLDRpci_pic) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001056 if (MI1->getOpcode() != Opcode)
1057 return false;
1058 if (MI0->getNumOperands() != MI1->getNumOperands())
1059 return false;
1060
1061 const MachineOperand &MO0 = MI0->getOperand(1);
1062 const MachineOperand &MO1 = MI1->getOperand(1);
1063 if (MO0.getOffset() != MO1.getOffset())
1064 return false;
1065
1066 const MachineFunction *MF = MI0->getParent()->getParent();
1067 const MachineConstantPool *MCP = MF->getConstantPool();
1068 int CPI0 = MO0.getIndex();
1069 int CPI1 = MO1.getIndex();
1070 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1071 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1072 ARMConstantPoolValue *ACPV0 =
1073 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1074 ARMConstantPoolValue *ACPV1 =
1075 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1076 return ACPV0->hasSameValue(ACPV1);
1077 }
1078
Evan Cheng506049f2010-03-03 01:44:33 +00001079 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001080}
1081
Bill Wendling4b722102010-06-23 23:00:16 +00001082/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1083/// determine if two loads are loading from the same base address. It should
1084/// only return true if the base pointers are the same and the only differences
1085/// between the two addresses is the offset. It also returns the offsets by
1086/// reference.
1087bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1088 int64_t &Offset1,
1089 int64_t &Offset2) const {
1090 // Don't worry about Thumb: just ARM and Thumb2.
1091 if (Subtarget.isThumb1Only()) return false;
1092
1093 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1094 return false;
1095
1096 switch (Load1->getMachineOpcode()) {
1097 default:
1098 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001099 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001100 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001101 case ARM::LDRD:
1102 case ARM::LDRH:
1103 case ARM::LDRSB:
1104 case ARM::LDRSH:
1105 case ARM::VLDRD:
1106 case ARM::VLDRS:
1107 case ARM::t2LDRi8:
1108 case ARM::t2LDRDi8:
1109 case ARM::t2LDRSHi8:
1110 case ARM::t2LDRi12:
1111 case ARM::t2LDRSHi12:
1112 break;
1113 }
1114
1115 switch (Load2->getMachineOpcode()) {
1116 default:
1117 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001118 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001119 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001120 case ARM::LDRD:
1121 case ARM::LDRH:
1122 case ARM::LDRSB:
1123 case ARM::LDRSH:
1124 case ARM::VLDRD:
1125 case ARM::VLDRS:
1126 case ARM::t2LDRi8:
1127 case ARM::t2LDRDi8:
1128 case ARM::t2LDRSHi8:
1129 case ARM::t2LDRi12:
1130 case ARM::t2LDRSHi12:
1131 break;
1132 }
1133
1134 // Check if base addresses and chain operands match.
1135 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1136 Load1->getOperand(4) != Load2->getOperand(4))
1137 return false;
1138
1139 // Index should be Reg0.
1140 if (Load1->getOperand(3) != Load2->getOperand(3))
1141 return false;
1142
1143 // Determine the offsets.
1144 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1145 isa<ConstantSDNode>(Load2->getOperand(1))) {
1146 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1147 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1148 return true;
1149 }
1150
1151 return false;
1152}
1153
1154/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1155/// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1156/// be scheduled togther. On some targets if two loads are loading from
1157/// addresses in the same cache line, it's better if they are scheduled
1158/// together. This function takes two integers that represent the load offsets
1159/// from the common base address. It returns true if it decides it's desirable
1160/// to schedule the two loads together. "NumLoads" is the number of loads that
1161/// have already been scheduled after Load1.
1162bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1163 int64_t Offset1, int64_t Offset2,
1164 unsigned NumLoads) const {
1165 // Don't worry about Thumb: just ARM and Thumb2.
1166 if (Subtarget.isThumb1Only()) return false;
1167
1168 assert(Offset2 > Offset1);
1169
1170 if ((Offset2 - Offset1) / 8 > 64)
1171 return false;
1172
1173 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1174 return false; // FIXME: overly conservative?
1175
1176 // Four loads in a row should be sufficient.
1177 if (NumLoads >= 3)
1178 return false;
1179
1180 return true;
1181}
1182
Evan Cheng86050dc2010-06-18 23:09:54 +00001183bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1184 const MachineBasicBlock *MBB,
1185 const MachineFunction &MF) const {
Jim Grosbach57bb3942010-06-25 18:43:14 +00001186 // Debug info is never a scheduling boundary. It's necessary to be explicit
1187 // due to the special treatment of IT instructions below, otherwise a
1188 // dbg_value followed by an IT will result in the IT instruction being
1189 // considered a scheduling hazard, which is wrong. It should be the actual
1190 // instruction preceding the dbg_value instruction(s), just like it is
1191 // when debug info is not present.
1192 if (MI->isDebugValue())
1193 return false;
1194
Evan Cheng86050dc2010-06-18 23:09:54 +00001195 // Terminators and labels can't be scheduled around.
1196 if (MI->getDesc().isTerminator() || MI->isLabel())
1197 return true;
1198
1199 // Treat the start of the IT block as a scheduling boundary, but schedule
1200 // t2IT along with all instructions following it.
1201 // FIXME: This is a big hammer. But the alternative is to add all potential
1202 // true and anti dependencies to IT block instructions as implicit operands
1203 // to the t2IT instruction. The added compile time and complexity does not
1204 // seem worth it.
1205 MachineBasicBlock::const_iterator I = MI;
Jim Grosbach57bb3942010-06-25 18:43:14 +00001206 // Make sure to skip any dbg_value instructions
1207 while (++I != MBB->end() && I->isDebugValue())
1208 ;
1209 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng86050dc2010-06-18 23:09:54 +00001210 return true;
1211
1212 // Don't attempt to schedule around any instruction that defines
1213 // a stack-oriented pointer, as it's unlikely to be profitable. This
1214 // saves compile time, because it doesn't require every single
1215 // stack slot reference to depend on the instruction that does the
1216 // modification.
1217 if (MI->definesRegister(ARM::SP))
1218 return true;
1219
1220 return false;
1221}
1222
Owen Andersonb20b8512010-09-28 18:32:13 +00001223bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
Evan Cheng8239daf2010-11-03 00:45:17 +00001224 unsigned NumCyles,
1225 unsigned ExtraPredCycles,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001226 float Probability,
1227 float Confidence) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00001228 if (!NumCyles)
Evan Cheng13151432010-06-25 22:42:03 +00001229 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001230
Owen Andersonb20b8512010-09-28 18:32:13 +00001231 // Attempt to estimate the relative costs of predication versus branching.
Evan Cheng8239daf2010-11-03 00:45:17 +00001232 float UnpredCost = Probability * NumCyles;
Owen Anderson654d5442010-09-28 21:57:50 +00001233 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001234 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001235
Evan Cheng8239daf2010-11-03 00:45:17 +00001236 return (float)(NumCyles + ExtraPredCycles) < UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001237}
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001238
Evan Cheng13151432010-06-25 22:42:03 +00001239bool ARMBaseInstrInfo::
Evan Cheng8239daf2010-11-03 00:45:17 +00001240isProfitableToIfCvt(MachineBasicBlock &TMBB,
1241 unsigned TCycles, unsigned TExtra,
1242 MachineBasicBlock &FMBB,
1243 unsigned FCycles, unsigned FExtra,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001244 float Probability, float Confidence) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00001245 if (!TCycles || !FCycles)
Owen Andersonb20b8512010-09-28 18:32:13 +00001246 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001247
Owen Andersonb20b8512010-09-28 18:32:13 +00001248 // Attempt to estimate the relative costs of predication versus branching.
Evan Cheng8239daf2010-11-03 00:45:17 +00001249 float UnpredCost = Probability * TCycles + (1.0 - Probability) * FCycles;
Owen Anderson654d5442010-09-28 21:57:50 +00001250 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001251 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001252
Evan Cheng8239daf2010-11-03 00:45:17 +00001253 return (float)(TCycles + FCycles + TExtra + FExtra) < UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001254}
1255
Evan Cheng8fb90362009-08-08 03:20:32 +00001256/// getInstrPredicate - If instruction is predicated, returns its predicate
1257/// condition, otherwise returns AL. It also returns the condition code
1258/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001259ARMCC::CondCodes
1260llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001261 int PIdx = MI->findFirstPredOperandIdx();
1262 if (PIdx == -1) {
1263 PredReg = 0;
1264 return ARMCC::AL;
1265 }
1266
1267 PredReg = MI->getOperand(PIdx+1).getReg();
1268 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1269}
1270
1271
Evan Cheng6495f632009-07-28 05:48:47 +00001272int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001273 if (Opc == ARM::B)
1274 return ARM::Bcc;
1275 else if (Opc == ARM::tB)
1276 return ARM::tBcc;
1277 else if (Opc == ARM::t2B)
1278 return ARM::t2Bcc;
1279
1280 llvm_unreachable("Unknown unconditional branch opcode!");
1281 return 0;
1282}
1283
Evan Cheng6495f632009-07-28 05:48:47 +00001284
1285void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1286 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1287 unsigned DestReg, unsigned BaseReg, int NumBytes,
1288 ARMCC::CondCodes Pred, unsigned PredReg,
1289 const ARMBaseInstrInfo &TII) {
1290 bool isSub = NumBytes < 0;
1291 if (isSub) NumBytes = -NumBytes;
1292
1293 while (NumBytes) {
1294 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1295 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1296 assert(ThisVal && "Didn't extract field correctly");
1297
1298 // We will handle these bits from offset, clear them.
1299 NumBytes &= ~ThisVal;
1300
1301 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1302
1303 // Build the new ADD / SUB.
1304 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1305 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1306 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1307 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1308 BaseReg = DestReg;
1309 }
1310}
1311
Evan Chengcdbb3f52009-08-27 01:23:50 +00001312bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1313 unsigned FrameReg, int &Offset,
1314 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001315 unsigned Opcode = MI.getOpcode();
1316 const TargetInstrDesc &Desc = MI.getDesc();
1317 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1318 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001319
Evan Cheng6495f632009-07-28 05:48:47 +00001320 // Memory operands in inline assembly always use AddrMode2.
1321 if (Opcode == ARM::INLINEASM)
1322 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001323
Evan Cheng6495f632009-07-28 05:48:47 +00001324 if (Opcode == ARM::ADDri) {
1325 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1326 if (Offset == 0) {
1327 // Turn it into a move.
1328 MI.setDesc(TII.get(ARM::MOVr));
1329 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1330 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001331 Offset = 0;
1332 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001333 } else if (Offset < 0) {
1334 Offset = -Offset;
1335 isSub = true;
1336 MI.setDesc(TII.get(ARM::SUBri));
1337 }
1338
1339 // Common case: small offset, fits into instruction.
1340 if (ARM_AM::getSOImmVal(Offset) != -1) {
1341 // Replace the FrameIndex with sp / fp
1342 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1343 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001344 Offset = 0;
1345 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001346 }
1347
1348 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1349 // as possible.
1350 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1351 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1352
1353 // We will handle these bits from offset, clear them.
1354 Offset &= ~ThisImmVal;
1355
1356 // Get the properly encoded SOImmVal field.
1357 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1358 "Bit extraction didn't work?");
1359 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1360 } else {
1361 unsigned ImmIdx = 0;
1362 int InstrOffs = 0;
1363 unsigned NumBits = 0;
1364 unsigned Scale = 1;
1365 switch (AddrMode) {
Jim Grosbach3e556122010-10-26 22:37:02 +00001366 case ARMII::AddrMode_i12: {
1367 ImmIdx = FrameRegIdx + 1;
1368 InstrOffs = MI.getOperand(ImmIdx).getImm();
1369 NumBits = 12;
1370 break;
1371 }
Evan Cheng6495f632009-07-28 05:48:47 +00001372 case ARMII::AddrMode2: {
1373 ImmIdx = FrameRegIdx+2;
1374 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1375 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1376 InstrOffs *= -1;
1377 NumBits = 12;
1378 break;
1379 }
1380 case ARMII::AddrMode3: {
1381 ImmIdx = FrameRegIdx+2;
1382 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1383 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1384 InstrOffs *= -1;
1385 NumBits = 8;
1386 break;
1387 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001388 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001389 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001390 // Can't fold any offset even if it's zero.
1391 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001392 case ARMII::AddrMode5: {
1393 ImmIdx = FrameRegIdx+1;
1394 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1395 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1396 InstrOffs *= -1;
1397 NumBits = 8;
1398 Scale = 4;
1399 break;
1400 }
1401 default:
1402 llvm_unreachable("Unsupported addressing mode!");
1403 break;
1404 }
1405
1406 Offset += InstrOffs * Scale;
1407 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1408 if (Offset < 0) {
1409 Offset = -Offset;
1410 isSub = true;
1411 }
1412
1413 // Attempt to fold address comp. if opcode has offset bits
1414 if (NumBits > 0) {
1415 // Common case: small offset, fits into instruction.
1416 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1417 int ImmedOffset = Offset / Scale;
1418 unsigned Mask = (1 << NumBits) - 1;
1419 if ((unsigned)Offset <= Mask * Scale) {
1420 // Replace the FrameIndex with sp
1421 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach77aee8e2010-10-27 01:19:41 +00001422 // FIXME: When addrmode2 goes away, this will simplify (like the
1423 // T2 version), as the LDR.i12 versions don't need the encoding
1424 // tricks for the offset value.
1425 if (isSub) {
1426 if (AddrMode == ARMII::AddrMode_i12)
1427 ImmedOffset = -ImmedOffset;
1428 else
1429 ImmedOffset |= 1 << NumBits;
1430 }
Evan Cheng6495f632009-07-28 05:48:47 +00001431 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001432 Offset = 0;
1433 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001434 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001435
Evan Cheng6495f632009-07-28 05:48:47 +00001436 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1437 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach063efbf2010-10-27 16:50:31 +00001438 if (isSub) {
1439 if (AddrMode == ARMII::AddrMode_i12)
1440 ImmedOffset = -ImmedOffset;
1441 else
1442 ImmedOffset |= 1 << NumBits;
1443 }
Evan Cheng6495f632009-07-28 05:48:47 +00001444 ImmOp.ChangeToImmediate(ImmedOffset);
1445 Offset &= ~(Mask*Scale);
1446 }
1447 }
1448
Evan Chengcdbb3f52009-08-27 01:23:50 +00001449 Offset = (isSub) ? -Offset : Offset;
1450 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001451}
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001452
1453bool ARMBaseInstrInfo::
Eric Christophera99c3e92010-09-28 04:18:29 +00001454AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1455 int &CmpValue) const {
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001456 switch (MI->getOpcode()) {
1457 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001458 case ARM::CMPri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001459 case ARM::t2CMPri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001460 SrcReg = MI->getOperand(0).getReg();
Gabor Greif04ac81d2010-09-21 12:01:15 +00001461 CmpMask = ~0;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001462 CmpValue = MI->getOperand(1).getImm();
1463 return true;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001464 case ARM::TSTri:
1465 case ARM::t2TSTri:
1466 SrcReg = MI->getOperand(0).getReg();
1467 CmpMask = MI->getOperand(1).getImm();
1468 CmpValue = 0;
1469 return true;
1470 }
1471
1472 return false;
1473}
1474
Gabor Greif05642a32010-09-29 10:12:08 +00001475/// isSuitableForMask - Identify a suitable 'and' instruction that
1476/// operates on the given source register and applies the same mask
1477/// as a 'tst' instruction. Provide a limited look-through for copies.
1478/// When successful, MI will hold the found instruction.
1479static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001480 int CmpMask, bool CommonUse) {
Gabor Greif05642a32010-09-29 10:12:08 +00001481 switch (MI->getOpcode()) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001482 case ARM::ANDri:
1483 case ARM::t2ANDri:
Gabor Greif05642a32010-09-29 10:12:08 +00001484 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001485 return false;
Gabor Greif05642a32010-09-29 10:12:08 +00001486 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greif04ac81d2010-09-21 12:01:15 +00001487 return true;
1488 break;
Gabor Greif05642a32010-09-29 10:12:08 +00001489 case ARM::COPY: {
1490 // Walk down one instruction which is potentially an 'and'.
1491 const MachineInstr &Copy = *MI;
Michael J. Spencerf000a7a2010-10-05 06:00:43 +00001492 MachineBasicBlock::iterator AND(
1493 llvm::next(MachineBasicBlock::iterator(MI)));
Gabor Greif05642a32010-09-29 10:12:08 +00001494 if (AND == MI->getParent()->end()) return false;
1495 MI = AND;
1496 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1497 CmpMask, true);
1498 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001499 }
1500
1501 return false;
1502}
1503
Bill Wendlinga6556862010-09-11 00:13:50 +00001504/// OptimizeCompareInstr - Convert the instruction supplying the argument to the
Evan Chengeb96a2f2010-11-15 21:20:45 +00001505/// comparison into one that sets the zero bit in the flags register.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001506bool ARMBaseInstrInfo::
Gabor Greif04ac81d2010-09-21 12:01:15 +00001507OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
Evan Chengeb96a2f2010-11-15 21:20:45 +00001508 int CmpValue, const MachineRegisterInfo *MRI) const {
Bill Wendling36656612010-09-10 23:46:12 +00001509 if (CmpValue != 0)
Bill Wendling92ad57f2010-09-10 23:34:19 +00001510 return false;
1511
Bill Wendlingb41ee962010-10-18 21:22:31 +00001512 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1513 if (llvm::next(DI) != MRI->def_end())
Bill Wendling92ad57f2010-09-10 23:34:19 +00001514 // Only support one definition.
1515 return false;
1516
1517 MachineInstr *MI = &*DI;
1518
Gabor Greif04ac81d2010-09-21 12:01:15 +00001519 // Masked compares sometimes use the same register as the corresponding 'and'.
1520 if (CmpMask != ~0) {
Gabor Greif05642a32010-09-29 10:12:08 +00001521 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001522 MI = 0;
Bill Wendlingb41ee962010-10-18 21:22:31 +00001523 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1524 UE = MRI->use_end(); UI != UE; ++UI) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001525 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001526 MachineInstr *PotentialAND = &*UI;
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001527 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
Gabor Greif04ac81d2010-09-21 12:01:15 +00001528 continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001529 MI = PotentialAND;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001530 break;
1531 }
1532 if (!MI) return false;
1533 }
1534 }
1535
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001536 // Conservatively refuse to convert an instruction which isn't in the same BB
1537 // as the comparison.
1538 if (MI->getParent() != CmpInstr->getParent())
1539 return false;
1540
1541 // Check that CPSR isn't set between the comparison instruction and the one we
1542 // want to change.
Evan Cheng691e64a2010-09-21 23:49:07 +00001543 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1544 B = MI->getParent()->begin();
Bill Wendling0aa38b92010-10-09 00:03:48 +00001545
1546 // Early exit if CmpInstr is at the beginning of the BB.
1547 if (I == B) return false;
1548
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001549 --I;
1550 for (; I != E; --I) {
1551 const MachineInstr &Instr = *I;
1552
1553 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1554 const MachineOperand &MO = Instr.getOperand(IO);
Bill Wendling40a5eb12010-11-01 20:41:43 +00001555 if (!MO.isReg()) continue;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001556
Bill Wendling40a5eb12010-11-01 20:41:43 +00001557 // This instruction modifies or uses CPSR after the one we want to
1558 // change. We can't do this transformation.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001559 if (MO.getReg() == ARM::CPSR)
1560 return false;
1561 }
Evan Cheng691e64a2010-09-21 23:49:07 +00001562
1563 if (I == B)
1564 // The 'and' is below the comparison instruction.
1565 return false;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001566 }
1567
1568 // Set the "zero" bit in CPSR.
1569 switch (MI->getOpcode()) {
1570 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001571 case ARM::ADDri:
Bob Wilson3a951822010-09-15 17:12:08 +00001572 case ARM::ANDri:
1573 case ARM::t2ANDri:
Bill Wendling38ae9972010-08-11 00:23:00 +00001574 case ARM::SUBri:
1575 case ARM::t2ADDri:
Bill Wendlingad422712010-08-18 21:32:07 +00001576 case ARM::t2SUBri:
Evan Cheng3642e642010-11-17 08:06:50 +00001577 // Toggle the optional operand to CPSR.
1578 MI->getOperand(5).setReg(ARM::CPSR);
1579 MI->getOperand(5).setIsDef(true);
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001580 CmpInstr->eraseFromParent();
1581 return true;
1582 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001583
1584 return false;
1585}
Evan Cheng5f54ce32010-09-09 18:18:55 +00001586
Evan Chengc4af4632010-11-17 20:13:28 +00001587bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
1588 MachineInstr *DefMI, unsigned Reg,
1589 MachineRegisterInfo *MRI) const {
1590 // Fold large immediates into add, sub, or, xor.
1591 unsigned DefOpc = DefMI->getOpcode();
1592 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
1593 return false;
1594 if (!DefMI->getOperand(1).isImm())
1595 // Could be t2MOVi32imm <ga:xx>
1596 return false;
1597
1598 if (!MRI->hasOneNonDBGUse(Reg))
1599 return false;
1600
1601 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng5c71c7a2010-11-18 01:43:23 +00001602 unsigned NewUseOpc = 0;
Evan Chengc4af4632010-11-17 20:13:28 +00001603 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng5c71c7a2010-11-18 01:43:23 +00001604 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Chengc4af4632010-11-17 20:13:28 +00001605 bool Commute = false;
1606 switch (UseOpc) {
1607 default: return false;
1608 case ARM::SUBrr:
1609 case ARM::ADDrr:
1610 case ARM::ORRrr:
1611 case ARM::EORrr:
1612 case ARM::t2SUBrr:
1613 case ARM::t2ADDrr:
1614 case ARM::t2ORRrr:
1615 case ARM::t2EORrr: {
1616 Commute = UseMI->getOperand(2).getReg() != Reg;
1617 switch (UseOpc) {
1618 default: break;
1619 case ARM::SUBrr: {
1620 if (Commute)
1621 return false;
1622 ImmVal = -ImmVal;
1623 NewUseOpc = ARM::SUBri;
1624 // Fallthrough
1625 }
1626 case ARM::ADDrr:
1627 case ARM::ORRrr:
1628 case ARM::EORrr: {
1629 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
1630 return false;
1631 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
1632 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
1633 switch (UseOpc) {
1634 default: break;
1635 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
1636 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
1637 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
1638 }
1639 break;
1640 }
1641 case ARM::t2SUBrr: {
1642 if (Commute)
1643 return false;
1644 ImmVal = -ImmVal;
1645 NewUseOpc = ARM::t2SUBri;
1646 // Fallthrough
1647 }
1648 case ARM::t2ADDrr:
1649 case ARM::t2ORRrr:
1650 case ARM::t2EORrr: {
1651 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
1652 return false;
1653 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
1654 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
1655 switch (UseOpc) {
1656 default: break;
1657 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
1658 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
1659 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
1660 }
1661 break;
1662 }
1663 }
1664 }
1665 }
1666
1667 unsigned OpIdx = Commute ? 2 : 1;
1668 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
1669 bool isKill = UseMI->getOperand(OpIdx).isKill();
1670 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
1671 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
1672 *UseMI, UseMI->getDebugLoc(),
1673 get(NewUseOpc), NewReg)
1674 .addReg(Reg1, getKillRegState(isKill))
1675 .addImm(SOImmValV1)));
1676 UseMI->setDesc(get(NewUseOpc));
1677 UseMI->getOperand(1).setReg(NewReg);
1678 UseMI->getOperand(1).setIsKill();
1679 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
1680 DefMI->eraseFromParent();
1681 return true;
1682}
1683
Evan Cheng5f54ce32010-09-09 18:18:55 +00001684unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +00001685ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1686 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001687 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +00001688 return 1;
1689
1690 const TargetInstrDesc &Desc = MI->getDesc();
1691 unsigned Class = Desc.getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +00001692 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +00001693 if (UOps)
1694 return UOps;
1695
1696 unsigned Opc = MI->getOpcode();
1697 switch (Opc) {
1698 default:
1699 llvm_unreachable("Unexpected multi-uops instruction!");
1700 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001701 case ARM::VLDMQIA:
1702 case ARM::VLDMQDB:
1703 case ARM::VSTMQIA:
1704 case ARM::VSTMQDB:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001705 return 2;
1706
1707 // The number of uOps for load / store multiple are determined by the number
1708 // registers.
Andrew Trick6e8f4c42010-12-24 04:28:06 +00001709 //
Evan Cheng3ef1c872010-09-10 01:29:16 +00001710 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1711 // same cycle. The scheduling for the first load / store must be done
1712 // separately by assuming the the address is not 64-bit aligned.
Bill Wendling73fe34a2010-11-16 01:16:36 +00001713 //
Evan Cheng3ef1c872010-09-10 01:29:16 +00001714 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendling73fe34a2010-11-16 01:16:36 +00001715 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
1716 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
1717 case ARM::VLDMDIA:
1718 case ARM::VLDMDDB:
1719 case ARM::VLDMDIA_UPD:
1720 case ARM::VLDMDDB_UPD:
1721 case ARM::VLDMSIA:
1722 case ARM::VLDMSDB:
1723 case ARM::VLDMSIA_UPD:
1724 case ARM::VLDMSDB_UPD:
1725 case ARM::VSTMDIA:
1726 case ARM::VSTMDDB:
1727 case ARM::VSTMDIA_UPD:
1728 case ARM::VSTMDDB_UPD:
1729 case ARM::VSTMSIA:
1730 case ARM::VSTMSDB:
1731 case ARM::VSTMSIA_UPD:
1732 case ARM::VSTMSDB_UPD: {
Evan Cheng5f54ce32010-09-09 18:18:55 +00001733 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1734 return (NumRegs / 2) + (NumRegs % 2) + 1;
1735 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00001736
1737 case ARM::LDMIA_RET:
1738 case ARM::LDMIA:
1739 case ARM::LDMDA:
1740 case ARM::LDMDB:
1741 case ARM::LDMIB:
1742 case ARM::LDMIA_UPD:
1743 case ARM::LDMDA_UPD:
1744 case ARM::LDMDB_UPD:
1745 case ARM::LDMIB_UPD:
1746 case ARM::STMIA:
1747 case ARM::STMDA:
1748 case ARM::STMDB:
1749 case ARM::STMIB:
1750 case ARM::STMIA_UPD:
1751 case ARM::STMDA_UPD:
1752 case ARM::STMDB_UPD:
1753 case ARM::STMIB_UPD:
1754 case ARM::tLDMIA:
1755 case ARM::tLDMIA_UPD:
1756 case ARM::tSTMIA:
1757 case ARM::tSTMIA_UPD:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001758 case ARM::tPOP_RET:
1759 case ARM::tPOP:
1760 case ARM::tPUSH:
Bill Wendling73fe34a2010-11-16 01:16:36 +00001761 case ARM::t2LDMIA_RET:
1762 case ARM::t2LDMIA:
1763 case ARM::t2LDMDB:
1764 case ARM::t2LDMIA_UPD:
1765 case ARM::t2LDMDB_UPD:
1766 case ARM::t2STMIA:
1767 case ARM::t2STMDB:
1768 case ARM::t2STMIA_UPD:
1769 case ARM::t2STMDB_UPD: {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001770 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1771 if (Subtarget.isCortexA8()) {
Evan Cheng8239daf2010-11-03 00:45:17 +00001772 if (NumRegs < 4)
1773 return 2;
1774 // 4 registers would be issued: 2, 2.
1775 // 5 registers would be issued: 2, 2, 1.
1776 UOps = (NumRegs / 2);
1777 if (NumRegs % 2)
1778 ++UOps;
1779 return UOps;
Evan Cheng3ef1c872010-09-10 01:29:16 +00001780 } else if (Subtarget.isCortexA9()) {
1781 UOps = (NumRegs / 2);
1782 // If there are odd number of registers or if it's not 64-bit aligned,
1783 // then it takes an extra AGU (Address Generation Unit) cycle.
1784 if ((NumRegs % 2) ||
1785 !MI->hasOneMemOperand() ||
1786 (*MI->memoperands_begin())->getAlignment() < 8)
1787 ++UOps;
1788 return UOps;
1789 } else {
1790 // Assume the worst.
1791 return NumRegs;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001792 }
Evan Cheng5f54ce32010-09-09 18:18:55 +00001793 }
1794 }
1795}
Evan Chenga0792de2010-10-06 06:27:31 +00001796
1797int
Evan Cheng344d9db2010-10-07 23:12:15 +00001798ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1799 const TargetInstrDesc &DefTID,
1800 unsigned DefClass,
1801 unsigned DefIdx, unsigned DefAlign) const {
1802 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1803 if (RegNo <= 0)
1804 // Def is the address writeback.
1805 return ItinData->getOperandCycle(DefClass, DefIdx);
1806
1807 int DefCycle;
1808 if (Subtarget.isCortexA8()) {
1809 // (regno / 2) + (regno % 2) + 1
1810 DefCycle = RegNo / 2 + 1;
1811 if (RegNo % 2)
1812 ++DefCycle;
1813 } else if (Subtarget.isCortexA9()) {
1814 DefCycle = RegNo;
1815 bool isSLoad = false;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001816
Evan Cheng344d9db2010-10-07 23:12:15 +00001817 switch (DefTID.getOpcode()) {
1818 default: break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001819 case ARM::VLDMSIA:
1820 case ARM::VLDMSDB:
1821 case ARM::VLDMSIA_UPD:
1822 case ARM::VLDMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00001823 isSLoad = true;
1824 break;
1825 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00001826
Evan Cheng344d9db2010-10-07 23:12:15 +00001827 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1828 // then it takes an extra cycle.
1829 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
1830 ++DefCycle;
1831 } else {
1832 // Assume the worst.
1833 DefCycle = RegNo + 2;
1834 }
1835
1836 return DefCycle;
1837}
1838
1839int
1840ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
1841 const TargetInstrDesc &DefTID,
1842 unsigned DefClass,
1843 unsigned DefIdx, unsigned DefAlign) const {
1844 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1845 if (RegNo <= 0)
1846 // Def is the address writeback.
1847 return ItinData->getOperandCycle(DefClass, DefIdx);
1848
1849 int DefCycle;
1850 if (Subtarget.isCortexA8()) {
1851 // 4 registers would be issued: 1, 2, 1.
1852 // 5 registers would be issued: 1, 2, 2.
1853 DefCycle = RegNo / 2;
1854 if (DefCycle < 1)
1855 DefCycle = 1;
1856 // Result latency is issue cycle + 2: E2.
1857 DefCycle += 2;
1858 } else if (Subtarget.isCortexA9()) {
1859 DefCycle = (RegNo / 2);
1860 // If there are odd number of registers or if it's not 64-bit aligned,
1861 // then it takes an extra AGU (Address Generation Unit) cycle.
1862 if ((RegNo % 2) || DefAlign < 8)
1863 ++DefCycle;
1864 // Result latency is AGU cycles + 2.
1865 DefCycle += 2;
1866 } else {
1867 // Assume the worst.
1868 DefCycle = RegNo + 2;
1869 }
1870
1871 return DefCycle;
1872}
1873
1874int
1875ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
1876 const TargetInstrDesc &UseTID,
1877 unsigned UseClass,
1878 unsigned UseIdx, unsigned UseAlign) const {
1879 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1880 if (RegNo <= 0)
1881 return ItinData->getOperandCycle(UseClass, UseIdx);
1882
1883 int UseCycle;
1884 if (Subtarget.isCortexA8()) {
1885 // (regno / 2) + (regno % 2) + 1
1886 UseCycle = RegNo / 2 + 1;
1887 if (RegNo % 2)
1888 ++UseCycle;
1889 } else if (Subtarget.isCortexA9()) {
1890 UseCycle = RegNo;
1891 bool isSStore = false;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001892
Evan Cheng344d9db2010-10-07 23:12:15 +00001893 switch (UseTID.getOpcode()) {
1894 default: break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001895 case ARM::VSTMSIA:
1896 case ARM::VSTMSDB:
1897 case ARM::VSTMSIA_UPD:
1898 case ARM::VSTMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00001899 isSStore = true;
1900 break;
1901 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00001902
Evan Cheng344d9db2010-10-07 23:12:15 +00001903 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1904 // then it takes an extra cycle.
1905 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
1906 ++UseCycle;
1907 } else {
1908 // Assume the worst.
1909 UseCycle = RegNo + 2;
1910 }
1911
1912 return UseCycle;
1913}
1914
1915int
1916ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
1917 const TargetInstrDesc &UseTID,
1918 unsigned UseClass,
1919 unsigned UseIdx, unsigned UseAlign) const {
1920 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1921 if (RegNo <= 0)
1922 return ItinData->getOperandCycle(UseClass, UseIdx);
1923
1924 int UseCycle;
1925 if (Subtarget.isCortexA8()) {
1926 UseCycle = RegNo / 2;
1927 if (UseCycle < 2)
1928 UseCycle = 2;
1929 // Read in E3.
1930 UseCycle += 2;
1931 } else if (Subtarget.isCortexA9()) {
1932 UseCycle = (RegNo / 2);
1933 // If there are odd number of registers or if it's not 64-bit aligned,
1934 // then it takes an extra AGU (Address Generation Unit) cycle.
1935 if ((RegNo % 2) || UseAlign < 8)
1936 ++UseCycle;
1937 } else {
1938 // Assume the worst.
1939 UseCycle = 1;
1940 }
1941 return UseCycle;
1942}
1943
1944int
Evan Chenga0792de2010-10-06 06:27:31 +00001945ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1946 const TargetInstrDesc &DefTID,
1947 unsigned DefIdx, unsigned DefAlign,
1948 const TargetInstrDesc &UseTID,
1949 unsigned UseIdx, unsigned UseAlign) const {
1950 unsigned DefClass = DefTID.getSchedClass();
1951 unsigned UseClass = UseTID.getSchedClass();
1952
1953 if (DefIdx < DefTID.getNumDefs() && UseIdx < UseTID.getNumOperands())
1954 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1955
1956 // This may be a def / use of a variable_ops instruction, the operand
1957 // latency might be determinable dynamically. Let the target try to
1958 // figure it out.
Evan Cheng9e08ee52010-10-28 02:00:25 +00001959 int DefCycle = -1;
Evan Cheng7e2fe912010-10-28 06:47:08 +00001960 bool LdmBypass = false;
Evan Chenga0792de2010-10-06 06:27:31 +00001961 switch (DefTID.getOpcode()) {
1962 default:
1963 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
1964 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001965
1966 case ARM::VLDMDIA:
1967 case ARM::VLDMDDB:
1968 case ARM::VLDMDIA_UPD:
1969 case ARM::VLDMDDB_UPD:
1970 case ARM::VLDMSIA:
1971 case ARM::VLDMSDB:
1972 case ARM::VLDMSIA_UPD:
1973 case ARM::VLDMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00001974 DefCycle = getVLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00001975 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001976
1977 case ARM::LDMIA_RET:
1978 case ARM::LDMIA:
1979 case ARM::LDMDA:
1980 case ARM::LDMDB:
1981 case ARM::LDMIB:
1982 case ARM::LDMIA_UPD:
1983 case ARM::LDMDA_UPD:
1984 case ARM::LDMDB_UPD:
1985 case ARM::LDMIB_UPD:
1986 case ARM::tLDMIA:
1987 case ARM::tLDMIA_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00001988 case ARM::tPUSH:
Bill Wendling73fe34a2010-11-16 01:16:36 +00001989 case ARM::t2LDMIA_RET:
1990 case ARM::t2LDMIA:
1991 case ARM::t2LDMDB:
1992 case ARM::t2LDMIA_UPD:
1993 case ARM::t2LDMDB_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00001994 LdmBypass = 1;
Evan Cheng344d9db2010-10-07 23:12:15 +00001995 DefCycle = getLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
1996 break;
Evan Chenga0792de2010-10-06 06:27:31 +00001997 }
Evan Chenga0792de2010-10-06 06:27:31 +00001998
1999 if (DefCycle == -1)
2000 // We can't seem to determine the result latency of the def, assume it's 2.
2001 DefCycle = 2;
2002
2003 int UseCycle = -1;
2004 switch (UseTID.getOpcode()) {
2005 default:
2006 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
2007 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002008
2009 case ARM::VSTMDIA:
2010 case ARM::VSTMDDB:
2011 case ARM::VSTMDIA_UPD:
2012 case ARM::VSTMDDB_UPD:
2013 case ARM::VSTMSIA:
2014 case ARM::VSTMSDB:
2015 case ARM::VSTMSIA_UPD:
2016 case ARM::VSTMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002017 UseCycle = getVSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002018 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002019
2020 case ARM::STMIA:
2021 case ARM::STMDA:
2022 case ARM::STMDB:
2023 case ARM::STMIB:
2024 case ARM::STMIA_UPD:
2025 case ARM::STMDA_UPD:
2026 case ARM::STMDB_UPD:
2027 case ARM::STMIB_UPD:
2028 case ARM::tSTMIA:
2029 case ARM::tSTMIA_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00002030 case ARM::tPOP_RET:
2031 case ARM::tPOP:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002032 case ARM::t2STMIA:
2033 case ARM::t2STMDB:
2034 case ARM::t2STMIA_UPD:
2035 case ARM::t2STMDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002036 UseCycle = getSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002037 break;
Evan Chenga0792de2010-10-06 06:27:31 +00002038 }
Evan Chenga0792de2010-10-06 06:27:31 +00002039
2040 if (UseCycle == -1)
2041 // Assume it's read in the first stage.
2042 UseCycle = 1;
2043
2044 UseCycle = DefCycle - UseCycle + 1;
2045 if (UseCycle > 0) {
2046 if (LdmBypass) {
2047 // It's a variable_ops instruction so we can't use DefIdx here. Just use
2048 // first def operand.
2049 if (ItinData->hasPipelineForwarding(DefClass, DefTID.getNumOperands()-1,
2050 UseClass, UseIdx))
2051 --UseCycle;
2052 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendling73fe34a2010-11-16 01:16:36 +00002053 UseClass, UseIdx)) {
Evan Chenga0792de2010-10-06 06:27:31 +00002054 --UseCycle;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002055 }
Evan Chenga0792de2010-10-06 06:27:31 +00002056 }
2057
2058 return UseCycle;
2059}
2060
2061int
2062ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2063 const MachineInstr *DefMI, unsigned DefIdx,
2064 const MachineInstr *UseMI, unsigned UseIdx) const {
2065 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
2066 DefMI->isRegSequence() || DefMI->isImplicitDef())
2067 return 1;
2068
2069 const TargetInstrDesc &DefTID = DefMI->getDesc();
2070 if (!ItinData || ItinData->isEmpty())
2071 return DefTID.mayLoad() ? 3 : 1;
2072
2073 const TargetInstrDesc &UseTID = UseMI->getDesc();
Evan Chengdd9dd6f2010-10-23 02:04:38 +00002074 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
Evan Chenge09206d2010-10-29 23:16:55 +00002075 if (DefMO.getReg() == ARM::CPSR) {
2076 if (DefMI->getOpcode() == ARM::FMSTAT) {
2077 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
2078 return Subtarget.isCortexA9() ? 1 : 20;
2079 }
2080
Evan Chengdd9dd6f2010-10-23 02:04:38 +00002081 // CPSR set and branch can be paired in the same cycle.
Evan Chenge09206d2010-10-29 23:16:55 +00002082 if (UseTID.isBranch())
2083 return 0;
2084 }
Evan Chengdd9dd6f2010-10-23 02:04:38 +00002085
Evan Chenga0792de2010-10-06 06:27:31 +00002086 unsigned DefAlign = DefMI->hasOneMemOperand()
2087 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
2088 unsigned UseAlign = UseMI->hasOneMemOperand()
2089 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00002090 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2091 UseTID, UseIdx, UseAlign);
2092
2093 if (Latency > 1 &&
2094 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2095 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2096 // variants are one cycle cheaper.
2097 switch (DefTID.getOpcode()) {
2098 default: break;
2099 case ARM::LDRrs:
2100 case ARM::LDRBrs: {
2101 unsigned ShOpVal = DefMI->getOperand(3).getImm();
2102 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2103 if (ShImm == 0 ||
2104 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2105 --Latency;
2106 break;
2107 }
2108 case ARM::t2LDRs:
2109 case ARM::t2LDRBs:
2110 case ARM::t2LDRHs:
2111 case ARM::t2LDRSHs: {
2112 // Thumb2 mode: lsl only.
2113 unsigned ShAmt = DefMI->getOperand(3).getImm();
2114 if (ShAmt == 0 || ShAmt == 2)
2115 --Latency;
2116 break;
2117 }
2118 }
2119 }
2120
2121 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00002122}
2123
2124int
2125ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2126 SDNode *DefNode, unsigned DefIdx,
2127 SDNode *UseNode, unsigned UseIdx) const {
2128 if (!DefNode->isMachineOpcode())
2129 return 1;
2130
2131 const TargetInstrDesc &DefTID = get(DefNode->getMachineOpcode());
2132 if (!ItinData || ItinData->isEmpty())
2133 return DefTID.mayLoad() ? 3 : 1;
2134
Evan Cheng08975152010-10-29 18:09:28 +00002135 if (!UseNode->isMachineOpcode()) {
2136 int Latency = ItinData->getOperandCycle(DefTID.getSchedClass(), DefIdx);
2137 if (Subtarget.isCortexA9())
2138 return Latency <= 2 ? 1 : Latency - 1;
2139 else
2140 return Latency <= 3 ? 1 : Latency - 2;
2141 }
Evan Chenga0792de2010-10-06 06:27:31 +00002142
2143 const TargetInstrDesc &UseTID = get(UseNode->getMachineOpcode());
2144 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
2145 unsigned DefAlign = !DefMN->memoperands_empty()
2146 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
2147 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
2148 unsigned UseAlign = !UseMN->memoperands_empty()
2149 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00002150 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2151 UseTID, UseIdx, UseAlign);
2152
2153 if (Latency > 1 &&
2154 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2155 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2156 // variants are one cycle cheaper.
2157 switch (DefTID.getOpcode()) {
2158 default: break;
2159 case ARM::LDRrs:
2160 case ARM::LDRBrs: {
2161 unsigned ShOpVal =
2162 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2163 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2164 if (ShImm == 0 ||
2165 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2166 --Latency;
2167 break;
2168 }
2169 case ARM::t2LDRs:
2170 case ARM::t2LDRBs:
2171 case ARM::t2LDRHs:
2172 case ARM::t2LDRSHs: {
2173 // Thumb2 mode: lsl only.
2174 unsigned ShAmt =
2175 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2176 if (ShAmt == 0 || ShAmt == 2)
2177 --Latency;
2178 break;
2179 }
2180 }
2181 }
2182
2183 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00002184}
Evan Cheng23128422010-10-19 18:58:51 +00002185
Evan Cheng8239daf2010-11-03 00:45:17 +00002186int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2187 const MachineInstr *MI,
2188 unsigned *PredCost) const {
2189 if (MI->isCopyLike() || MI->isInsertSubreg() ||
2190 MI->isRegSequence() || MI->isImplicitDef())
2191 return 1;
2192
2193 if (!ItinData || ItinData->isEmpty())
2194 return 1;
2195
2196 const TargetInstrDesc &TID = MI->getDesc();
2197 unsigned Class = TID.getSchedClass();
2198 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
2199 if (PredCost && TID.hasImplicitDefOfPhysReg(ARM::CPSR))
2200 // When predicated, CPSR is an additional source operand for CPSR updating
2201 // instructions, this apparently increases their latencies.
2202 *PredCost = 1;
2203 if (UOps)
2204 return ItinData->getStageLatency(Class);
2205 return getNumMicroOps(ItinData, MI);
2206}
2207
2208int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2209 SDNode *Node) const {
2210 if (!Node->isMachineOpcode())
2211 return 1;
2212
2213 if (!ItinData || ItinData->isEmpty())
2214 return 1;
2215
2216 unsigned Opcode = Node->getMachineOpcode();
2217 switch (Opcode) {
2218 default:
2219 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendling73fe34a2010-11-16 01:16:36 +00002220 case ARM::VLDMQIA:
2221 case ARM::VLDMQDB:
2222 case ARM::VSTMQIA:
2223 case ARM::VSTMQDB:
Evan Cheng8239daf2010-11-03 00:45:17 +00002224 return 2;
Eric Christopher8b3ca622010-11-18 19:40:05 +00002225 }
Evan Cheng8239daf2010-11-03 00:45:17 +00002226}
2227
Evan Cheng23128422010-10-19 18:58:51 +00002228bool ARMBaseInstrInfo::
2229hasHighOperandLatency(const InstrItineraryData *ItinData,
2230 const MachineRegisterInfo *MRI,
2231 const MachineInstr *DefMI, unsigned DefIdx,
2232 const MachineInstr *UseMI, unsigned UseIdx) const {
2233 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2234 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2235 if (Subtarget.isCortexA8() &&
2236 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2237 // CortexA8 VFP instructions are not pipelined.
2238 return true;
2239
2240 // Hoist VFP / NEON instructions with 4 or higher latency.
2241 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2242 if (Latency <= 3)
2243 return false;
2244 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2245 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2246}
Evan Chengc8141df2010-10-26 02:08:50 +00002247
2248bool ARMBaseInstrInfo::
2249hasLowDefLatency(const InstrItineraryData *ItinData,
2250 const MachineInstr *DefMI, unsigned DefIdx) const {
2251 if (!ItinData || ItinData->isEmpty())
2252 return false;
2253
2254 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2255 if (DDomain == ARMII::DomainGeneral) {
2256 unsigned DefClass = DefMI->getDesc().getSchedClass();
2257 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2258 return (DefCycle != -1 && DefCycle <= 2);
2259 }
2260 return false;
2261}
Evan Cheng48575f62010-12-05 22:04:16 +00002262
2263bool
2264ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
2265 unsigned &AddSubOpc,
2266 bool &NegAcc, bool &HasLane) const {
2267 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
2268 if (I == MLxEntryMap.end())
2269 return false;
2270
2271 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
2272 MulOpc = Entry.MulOpc;
2273 AddSubOpc = Entry.AddSubOpc;
2274 NegAcc = Entry.NegAcc;
2275 HasLane = Entry.HasLane;
2276 return true;
2277}