blob: aaf4f0d8a9a830c666dc030c54a8b4b42584cf3c [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) {
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000564 case ARM::MOVi16_pic_ga:
565 case ARM::MOVTi16_pic_ga:
566 case ARM::t2MOVi16_pic_ga:
567 case ARM::t2MOVTi16_pic_ga:
568 return 4;
Jim Grosbach3c38f962010-10-06 22:01:26 +0000569 case ARM::MOVi32imm:
570 case ARM::t2MOVi32imm:
571 return 8;
David Goodwin334c2642009-07-08 16:09:28 +0000572 case ARM::CONSTPOOL_ENTRY:
573 // If this machine instr is a constant pool entry, its size is recorded as
574 // operand #2.
575 return MI->getOperand(2).getImm();
Jim Grosbach5eb19512010-05-22 01:06:18 +0000576 case ARM::Int_eh_sjlj_longjmp:
577 return 16;
578 case ARM::tInt_eh_sjlj_longjmp:
579 return 10;
Evan Cheng78947622009-07-24 18:20:44 +0000580 case ARM::Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000581 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000582 return 20;
Jim Grosbachd1228742009-12-01 18:10:36 +0000583 case ARM::tInt_eh_sjlj_setjmp:
Jim Grosbach5aa16842009-08-11 19:42:21 +0000584 case ARM::t2Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000585 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000586 return 12;
David Goodwin334c2642009-07-08 16:09:28 +0000587 case ARM::BR_JTr:
588 case ARM::BR_JTm:
589 case ARM::BR_JTadd:
Evan Chenga0ee8622009-07-31 22:22:22 +0000590 case ARM::tBR_JTr:
Evan Chengd26b14c2009-07-31 18:28:05 +0000591 case ARM::t2BR_JT:
Jim Grosbachd092a872010-11-29 21:28:32 +0000592 case ARM::t2TBB_JT:
593 case ARM::t2TBH_JT: {
David Goodwin334c2642009-07-08 16:09:28 +0000594 // These are jumptable branches, i.e. a branch followed by an inlined
Evan Chengd26b14c2009-07-31 18:28:05 +0000595 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
596 // entry is one byte; TBH two byte each.
Jim Grosbachd092a872010-11-29 21:28:32 +0000597 unsigned EntrySize = (Opc == ARM::t2TBB_JT)
598 ? 1 : ((Opc == ARM::t2TBH_JT) ? 2 : 4);
David Goodwin334c2642009-07-08 16:09:28 +0000599 unsigned NumOps = TID.getNumOperands();
600 MachineOperand JTOP =
601 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
602 unsigned JTI = JTOP.getIndex();
603 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000604 assert(MJTI != 0);
David Goodwin334c2642009-07-08 16:09:28 +0000605 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
606 assert(JTI < JT.size());
607 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
608 // 4 aligned. The assembler / linker may add 2 byte padding just before
609 // the JT entries. The size does not include this padding; the
610 // constant islands pass does separate bookkeeping for it.
611 // FIXME: If we know the size of the function is less than (1 << 16) *2
612 // bytes, we can use 16-bit entries instead. Then there won't be an
613 // alignment issue.
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000614 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
615 unsigned NumEntries = getNumJTEntries(JT, JTI);
Jim Grosbachd092a872010-11-29 21:28:32 +0000616 if (Opc == ARM::t2TBB_JT && (NumEntries & 1))
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000617 // Make sure the instruction that follows TBB is 2-byte aligned.
618 // FIXME: Constant island pass should insert an "ALIGN" instruction
619 // instead.
620 ++NumEntries;
621 return NumEntries * EntrySize + InstSize;
David Goodwin334c2642009-07-08 16:09:28 +0000622 }
623 default:
624 // Otherwise, pseudo-instruction sizes are zero.
625 return 0;
626 }
627 }
628 }
629 return 0; // Not reached
630}
631
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000632void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
633 MachineBasicBlock::iterator I, DebugLoc DL,
634 unsigned DestReg, unsigned SrcReg,
635 bool KillSrc) const {
636 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
637 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson1665b0a2010-02-16 17:24:15 +0000638
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000639 if (GPRDest && GPRSrc) {
640 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
641 .addReg(SrcReg, getKillRegState(KillSrc))));
642 return;
David Goodwin7bfdca02009-08-05 21:02:22 +0000643 }
David Goodwin334c2642009-07-08 16:09:28 +0000644
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000645 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
646 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
647
648 unsigned Opc;
649 if (SPRDest && SPRSrc)
650 Opc = ARM::VMOVS;
651 else if (GPRDest && SPRSrc)
652 Opc = ARM::VMOVRS;
653 else if (SPRDest && GPRSrc)
654 Opc = ARM::VMOVSR;
655 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
656 Opc = ARM::VMOVD;
657 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
658 Opc = ARM::VMOVQ;
659 else if (ARM::QQPRRegClass.contains(DestReg, SrcReg))
660 Opc = ARM::VMOVQQ;
661 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg))
662 Opc = ARM::VMOVQQQQ;
663 else
664 llvm_unreachable("Impossible reg-to-reg copy");
665
666 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
667 MIB.addReg(SrcReg, getKillRegState(KillSrc));
668 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ)
669 AddDefaultPred(MIB);
David Goodwin334c2642009-07-08 16:09:28 +0000670}
671
Evan Chengc10b5af2010-05-07 00:24:52 +0000672static const
673MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
674 unsigned Reg, unsigned SubIdx, unsigned State,
675 const TargetRegisterInfo *TRI) {
676 if (!SubIdx)
677 return MIB.addReg(Reg, State);
678
679 if (TargetRegisterInfo::isPhysicalRegister(Reg))
680 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
681 return MIB.addReg(Reg, State, SubIdx);
682}
683
David Goodwin334c2642009-07-08 16:09:28 +0000684void ARMBaseInstrInfo::
685storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
686 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000687 const TargetRegisterClass *RC,
688 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000689 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000690 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000691 MachineFunction &MF = *MBB.getParent();
692 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000693 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000694
695 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000696 MF.getMachineMemOperand(MachinePointerInfo(
697 PseudoSourceValue::getFixedStack(FI)),
698 MachineMemOperand::MOStore,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000699 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000700 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000701
Bob Wilson0eb0c742010-02-16 22:01:59 +0000702 // tGPR is used sometimes in ARM instructions that need to avoid using
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000703 // certain registers. Just treat it as GPR here. Likewise, rGPR.
704 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
705 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000706 RC = ARM::GPRRegisterClass;
707
Bob Wilsonebe99b22010-06-18 21:32:42 +0000708 switch (RC->getID()) {
709 case ARM::GPRRegClassID:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000710 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwin334c2642009-07-08 16:09:28 +0000711 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000712 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000713 break;
714 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000715 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
716 .addReg(SrcReg, getKillRegState(isKill))
717 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000718 break;
719 case ARM::DPRRegClassID:
720 case ARM::DPR_VFP2RegClassID:
721 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000722 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000723 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000724 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000725 break;
726 case ARM::QPRRegClassID:
727 case ARM::QPR_VFP2RegClassID:
728 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000729 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000730 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo))
Bob Wilsonf967ca02010-07-06 21:26:18 +0000731 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000732 .addReg(SrcReg, getKillRegState(isKill))
733 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000734 } else {
Bill Wendling73fe34a2010-11-16 01:16:36 +0000735 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQIA))
Evan Cheng69b9f982010-05-13 01:12:06 +0000736 .addReg(SrcReg, getKillRegState(isKill))
737 .addFrameIndex(FI)
Evan Cheng69b9f982010-05-13 01:12:06 +0000738 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000739 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000740 break;
741 case ARM::QQPRRegClassID:
742 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000743 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng22c687b2010-05-14 02:13:41 +0000744 // FIXME: It's possible to only store part of the QQ register if the
745 // spilled def has a sub-register index.
Bob Wilson168f3822010-09-15 01:48:05 +0000746 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
747 .addFrameIndex(FI).addImm(16)
748 .addReg(SrcReg, getKillRegState(isKill))
749 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000750 } else {
751 MachineInstrBuilder MIB =
Bill Wendling73fe34a2010-11-16 01:16:36 +0000752 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
753 .addFrameIndex(FI))
Evan Cheng435d4992010-05-07 02:04:02 +0000754 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000755 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
756 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
757 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
758 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000759 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000760 break;
761 case ARM::QQQQPRRegClassID: {
Evan Cheng22c687b2010-05-14 02:13:41 +0000762 MachineInstrBuilder MIB =
Bill Wendling73fe34a2010-11-16 01:16:36 +0000763 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMDIA))
764 .addFrameIndex(FI))
Evan Cheng22c687b2010-05-14 02:13:41 +0000765 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000766 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
767 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
768 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
769 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
770 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
771 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
772 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
773 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
Bob Wilsonebe99b22010-06-18 21:32:42 +0000774 break;
775 }
776 default:
777 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000778 }
779}
780
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000781unsigned
782ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
783 int &FrameIndex) const {
784 switch (MI->getOpcode()) {
785 default: break;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000786 case ARM::STRrs:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000787 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
788 if (MI->getOperand(1).isFI() &&
789 MI->getOperand(2).isReg() &&
790 MI->getOperand(3).isImm() &&
791 MI->getOperand(2).getReg() == 0 &&
792 MI->getOperand(3).getImm() == 0) {
793 FrameIndex = MI->getOperand(1).getIndex();
794 return MI->getOperand(0).getReg();
795 }
796 break;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000797 case ARM::STRi12:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000798 case ARM::t2STRi12:
799 case ARM::tSpill:
800 case ARM::VSTRD:
801 case ARM::VSTRS:
802 if (MI->getOperand(1).isFI() &&
803 MI->getOperand(2).isImm() &&
804 MI->getOperand(2).getImm() == 0) {
805 FrameIndex = MI->getOperand(1).getIndex();
806 return MI->getOperand(0).getReg();
807 }
808 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000809 case ARM::VST1q64Pseudo:
810 if (MI->getOperand(0).isFI() &&
811 MI->getOperand(2).getSubReg() == 0) {
812 FrameIndex = MI->getOperand(0).getIndex();
813 return MI->getOperand(2).getReg();
814 }
Jakob Stoklund Olesen31bbc512010-09-15 21:40:09 +0000815 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +0000816 case ARM::VSTMQIA:
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000817 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000818 MI->getOperand(0).getSubReg() == 0) {
819 FrameIndex = MI->getOperand(1).getIndex();
820 return MI->getOperand(0).getReg();
821 }
822 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000823 }
824
825 return 0;
826}
827
David Goodwin334c2642009-07-08 16:09:28 +0000828void ARMBaseInstrInfo::
829loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
830 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000831 const TargetRegisterClass *RC,
832 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000833 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000834 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000835 MachineFunction &MF = *MBB.getParent();
836 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000837 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000838 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000839 MF.getMachineMemOperand(
840 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
841 MachineMemOperand::MOLoad,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000842 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000843 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000844
Bob Wilson0eb0c742010-02-16 22:01:59 +0000845 // tGPR is used sometimes in ARM instructions that need to avoid using
846 // certain registers. Just treat it as GPR here.
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000847 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
848 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000849 RC = ARM::GPRRegisterClass;
850
Bob Wilsonebe99b22010-06-18 21:32:42 +0000851 switch (RC->getID()) {
852 case ARM::GPRRegClassID:
Jim Grosbach3e556122010-10-26 22:37:02 +0000853 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
854 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000855 break;
856 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000857 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
858 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000859 break;
860 case ARM::DPRRegClassID:
861 case ARM::DPR_VFP2RegClassID:
862 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000863 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000864 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000865 break;
866 case ARM::QPRRegClassID:
867 case ARM::QPR_VFP2RegClassID:
868 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000869 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000870 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg)
Bob Wilsonf967ca02010-07-06 21:26:18 +0000871 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000872 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000873 } else {
Bill Wendling73fe34a2010-11-16 01:16:36 +0000874 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
Evan Cheng69b9f982010-05-13 01:12:06 +0000875 .addFrameIndex(FI)
Evan Cheng69b9f982010-05-13 01:12:06 +0000876 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000877 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000878 break;
879 case ARM::QQPRRegClassID:
880 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000881 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000882 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
883 .addFrameIndex(FI).addImm(16)
884 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000885 } else {
886 MachineInstrBuilder MIB =
Bill Wendling73fe34a2010-11-16 01:16:36 +0000887 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
888 .addFrameIndex(FI))
Evan Cheng435d4992010-05-07 02:04:02 +0000889 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000890 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
891 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
892 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
893 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000894 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000895 break;
896 case ARM::QQQQPRRegClassID: {
897 MachineInstrBuilder MIB =
Bill Wendling73fe34a2010-11-16 01:16:36 +0000898 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
899 .addFrameIndex(FI))
Bob Wilsonebe99b22010-06-18 21:32:42 +0000900 .addMemOperand(MMO);
901 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
902 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
903 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
904 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
905 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
906 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
907 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
908 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
909 break;
910 }
911 default:
912 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000913 }
914}
915
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000916unsigned
917ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
918 int &FrameIndex) const {
919 switch (MI->getOpcode()) {
920 default: break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000921 case ARM::LDRrs:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000922 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
923 if (MI->getOperand(1).isFI() &&
924 MI->getOperand(2).isReg() &&
925 MI->getOperand(3).isImm() &&
926 MI->getOperand(2).getReg() == 0 &&
927 MI->getOperand(3).getImm() == 0) {
928 FrameIndex = MI->getOperand(1).getIndex();
929 return MI->getOperand(0).getReg();
930 }
931 break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000932 case ARM::LDRi12:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000933 case ARM::t2LDRi12:
934 case ARM::tRestore:
935 case ARM::VLDRD:
936 case ARM::VLDRS:
937 if (MI->getOperand(1).isFI() &&
938 MI->getOperand(2).isImm() &&
939 MI->getOperand(2).getImm() == 0) {
940 FrameIndex = MI->getOperand(1).getIndex();
941 return MI->getOperand(0).getReg();
942 }
943 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000944 case ARM::VLD1q64Pseudo:
945 if (MI->getOperand(1).isFI() &&
946 MI->getOperand(0).getSubReg() == 0) {
947 FrameIndex = MI->getOperand(1).getIndex();
948 return MI->getOperand(0).getReg();
949 }
950 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +0000951 case ARM::VLDMQIA:
Jakob Stoklund Olesen06f264e2010-09-15 21:40:11 +0000952 if (MI->getOperand(1).isFI() &&
Jakob Stoklund Olesen06f264e2010-09-15 21:40:11 +0000953 MI->getOperand(0).getSubReg() == 0) {
954 FrameIndex = MI->getOperand(1).getIndex();
955 return MI->getOperand(0).getReg();
956 }
957 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000958 }
959
960 return 0;
961}
962
Evan Cheng62b50652010-04-26 07:39:25 +0000963MachineInstr*
964ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000965 int FrameIx, uint64_t Offset,
Evan Cheng62b50652010-04-26 07:39:25 +0000966 const MDNode *MDPtr,
967 DebugLoc DL) const {
968 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
969 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
970 return &*MIB;
971}
972
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000973/// Create a copy of a const pool value. Update CPI to the new index and return
974/// the label UID.
975static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
976 MachineConstantPool *MCP = MF.getConstantPool();
977 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
978
979 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
980 assert(MCPE.isMachineConstantPoolEntry() &&
981 "Expecting a machine constantpool entry!");
982 ARMConstantPoolValue *ACPV =
983 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
984
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000985 unsigned PCLabelId = AFI->createPICLabelUId();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000986 ARMConstantPoolValue *NewCPV = 0;
Jim Grosbach51f5b672010-09-10 21:38:22 +0000987 // FIXME: The below assumes PIC relocation model and that the function
988 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
989 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
990 // instructions, so that's probably OK, but is PIC always correct when
991 // we get here?
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000992 if (ACPV->isGlobalValue())
993 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
994 ARMCP::CPValue, 4);
995 else if (ACPV->isExtSymbol())
996 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
997 ACPV->getSymbol(), PCLabelId, 4);
998 else if (ACPV->isBlockAddress())
999 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
1000 ARMCP::CPBlockAddress, 4);
Jim Grosbach51f5b672010-09-10 21:38:22 +00001001 else if (ACPV->isLSDA())
1002 NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId,
1003 ARMCP::CPLSDA, 4);
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001004 else
1005 llvm_unreachable("Unexpected ARM constantpool value type!!");
1006 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1007 return PCLabelId;
1008}
1009
Evan Chengfdc83402009-11-08 00:15:23 +00001010void ARMBaseInstrInfo::
1011reMaterialize(MachineBasicBlock &MBB,
1012 MachineBasicBlock::iterator I,
1013 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +00001014 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001015 const TargetRegisterInfo &TRI) const {
Evan Chengfdc83402009-11-08 00:15:23 +00001016 unsigned Opcode = Orig->getOpcode();
1017 switch (Opcode) {
1018 default: {
1019 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001020 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfdc83402009-11-08 00:15:23 +00001021 MBB.insert(I, MI);
1022 break;
1023 }
1024 case ARM::tLDRpci_pic:
1025 case ARM::t2LDRpci_pic: {
1026 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +00001027 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001028 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +00001029 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1030 DestReg)
1031 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1032 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1033 break;
1034 }
1035 }
Evan Chengfdc83402009-11-08 00:15:23 +00001036}
1037
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001038MachineInstr *
1039ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1040 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1041 switch(Orig->getOpcode()) {
1042 case ARM::tLDRpci_pic:
1043 case ARM::t2LDRpci_pic: {
1044 unsigned CPI = Orig->getOperand(1).getIndex();
1045 unsigned PCLabelId = duplicateCPV(MF, CPI);
1046 Orig->getOperand(1).setIndex(CPI);
1047 Orig->getOperand(2).setImm(PCLabelId);
1048 break;
1049 }
1050 }
1051 return MI;
1052}
1053
Evan Cheng506049f2010-03-03 01:44:33 +00001054bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
Evan Cheng9fe20092011-01-20 08:34:58 +00001055 const MachineInstr *MI1,
1056 const MachineRegisterInfo *MRI) const {
Evan Chengd457e6e2009-11-07 04:04:34 +00001057 int Opcode = MI0->getOpcode();
Evan Chengd7e3cc82011-01-20 23:55:07 +00001058 if (Opcode == ARM::t2LDRpci ||
Evan Cheng9b824252009-11-20 02:10:27 +00001059 Opcode == ARM::t2LDRpci_pic ||
1060 Opcode == ARM::tLDRpci ||
Evan Cheng9fe20092011-01-20 08:34:58 +00001061 Opcode == ARM::tLDRpci_pic ||
1062 Opcode == ARM::MOV_pic_ga_add_pc ||
1063 Opcode == ARM::t2MOV_pic_ga_add_pc) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001064 if (MI1->getOpcode() != Opcode)
1065 return false;
1066 if (MI0->getNumOperands() != MI1->getNumOperands())
1067 return false;
1068
1069 const MachineOperand &MO0 = MI0->getOperand(1);
1070 const MachineOperand &MO1 = MI1->getOperand(1);
1071 if (MO0.getOffset() != MO1.getOffset())
1072 return false;
1073
Evan Cheng9fe20092011-01-20 08:34:58 +00001074 if (Opcode == ARM::MOV_pic_ga_add_pc ||
1075 Opcode == ARM::t2MOV_pic_ga_add_pc)
1076 // Ignore the PC labels.
1077 return MO0.getGlobal() == MO1.getGlobal();
1078
Evan Chengd457e6e2009-11-07 04:04:34 +00001079 const MachineFunction *MF = MI0->getParent()->getParent();
1080 const MachineConstantPool *MCP = MF->getConstantPool();
1081 int CPI0 = MO0.getIndex();
1082 int CPI1 = MO1.getIndex();
1083 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1084 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1085 ARMConstantPoolValue *ACPV0 =
1086 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1087 ARMConstantPoolValue *ACPV1 =
1088 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1089 return ACPV0->hasSameValue(ACPV1);
Evan Cheng9fe20092011-01-20 08:34:58 +00001090 } else if (Opcode == ARM::PICLDR) {
1091 if (MI1->getOpcode() != Opcode)
1092 return false;
1093 if (MI0->getNumOperands() != MI1->getNumOperands())
1094 return false;
1095
1096 unsigned Addr0 = MI0->getOperand(1).getReg();
1097 unsigned Addr1 = MI1->getOperand(1).getReg();
1098 if (Addr0 != Addr1) {
1099 if (!MRI ||
1100 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1101 !TargetRegisterInfo::isVirtualRegister(Addr1))
1102 return false;
1103
1104 // This assumes SSA form.
1105 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1106 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1107 // Check if the loaded value, e.g. a constantpool of a global address, are
1108 // the same.
1109 if (!produceSameValue(Def0, Def1, MRI))
1110 return false;
1111 }
1112
1113 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1114 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1115 const MachineOperand &MO0 = MI0->getOperand(i);
1116 const MachineOperand &MO1 = MI1->getOperand(i);
1117 if (!MO0.isIdenticalTo(MO1))
1118 return false;
1119 }
1120 return true;
Evan Chengd457e6e2009-11-07 04:04:34 +00001121 }
1122
Evan Cheng506049f2010-03-03 01:44:33 +00001123 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001124}
1125
Bill Wendling4b722102010-06-23 23:00:16 +00001126/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1127/// determine if two loads are loading from the same base address. It should
1128/// only return true if the base pointers are the same and the only differences
1129/// between the two addresses is the offset. It also returns the offsets by
1130/// reference.
1131bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1132 int64_t &Offset1,
1133 int64_t &Offset2) const {
1134 // Don't worry about Thumb: just ARM and Thumb2.
1135 if (Subtarget.isThumb1Only()) return false;
1136
1137 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1138 return false;
1139
1140 switch (Load1->getMachineOpcode()) {
1141 default:
1142 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001143 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001144 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001145 case ARM::LDRD:
1146 case ARM::LDRH:
1147 case ARM::LDRSB:
1148 case ARM::LDRSH:
1149 case ARM::VLDRD:
1150 case ARM::VLDRS:
1151 case ARM::t2LDRi8:
1152 case ARM::t2LDRDi8:
1153 case ARM::t2LDRSHi8:
1154 case ARM::t2LDRi12:
1155 case ARM::t2LDRSHi12:
1156 break;
1157 }
1158
1159 switch (Load2->getMachineOpcode()) {
1160 default:
1161 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001162 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001163 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001164 case ARM::LDRD:
1165 case ARM::LDRH:
1166 case ARM::LDRSB:
1167 case ARM::LDRSH:
1168 case ARM::VLDRD:
1169 case ARM::VLDRS:
1170 case ARM::t2LDRi8:
1171 case ARM::t2LDRDi8:
1172 case ARM::t2LDRSHi8:
1173 case ARM::t2LDRi12:
1174 case ARM::t2LDRSHi12:
1175 break;
1176 }
1177
1178 // Check if base addresses and chain operands match.
1179 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1180 Load1->getOperand(4) != Load2->getOperand(4))
1181 return false;
1182
1183 // Index should be Reg0.
1184 if (Load1->getOperand(3) != Load2->getOperand(3))
1185 return false;
1186
1187 // Determine the offsets.
1188 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1189 isa<ConstantSDNode>(Load2->getOperand(1))) {
1190 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1191 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1192 return true;
1193 }
1194
1195 return false;
1196}
1197
1198/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1199/// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1200/// be scheduled togther. On some targets if two loads are loading from
1201/// addresses in the same cache line, it's better if they are scheduled
1202/// together. This function takes two integers that represent the load offsets
1203/// from the common base address. It returns true if it decides it's desirable
1204/// to schedule the two loads together. "NumLoads" is the number of loads that
1205/// have already been scheduled after Load1.
1206bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1207 int64_t Offset1, int64_t Offset2,
1208 unsigned NumLoads) const {
1209 // Don't worry about Thumb: just ARM and Thumb2.
1210 if (Subtarget.isThumb1Only()) return false;
1211
1212 assert(Offset2 > Offset1);
1213
1214 if ((Offset2 - Offset1) / 8 > 64)
1215 return false;
1216
1217 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1218 return false; // FIXME: overly conservative?
1219
1220 // Four loads in a row should be sufficient.
1221 if (NumLoads >= 3)
1222 return false;
1223
1224 return true;
1225}
1226
Evan Cheng86050dc2010-06-18 23:09:54 +00001227bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1228 const MachineBasicBlock *MBB,
1229 const MachineFunction &MF) const {
Jim Grosbach57bb3942010-06-25 18:43:14 +00001230 // Debug info is never a scheduling boundary. It's necessary to be explicit
1231 // due to the special treatment of IT instructions below, otherwise a
1232 // dbg_value followed by an IT will result in the IT instruction being
1233 // considered a scheduling hazard, which is wrong. It should be the actual
1234 // instruction preceding the dbg_value instruction(s), just like it is
1235 // when debug info is not present.
1236 if (MI->isDebugValue())
1237 return false;
1238
Evan Cheng86050dc2010-06-18 23:09:54 +00001239 // Terminators and labels can't be scheduled around.
1240 if (MI->getDesc().isTerminator() || MI->isLabel())
1241 return true;
1242
1243 // Treat the start of the IT block as a scheduling boundary, but schedule
1244 // t2IT along with all instructions following it.
1245 // FIXME: This is a big hammer. But the alternative is to add all potential
1246 // true and anti dependencies to IT block instructions as implicit operands
1247 // to the t2IT instruction. The added compile time and complexity does not
1248 // seem worth it.
1249 MachineBasicBlock::const_iterator I = MI;
Jim Grosbach57bb3942010-06-25 18:43:14 +00001250 // Make sure to skip any dbg_value instructions
1251 while (++I != MBB->end() && I->isDebugValue())
1252 ;
1253 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng86050dc2010-06-18 23:09:54 +00001254 return true;
1255
1256 // Don't attempt to schedule around any instruction that defines
1257 // a stack-oriented pointer, as it's unlikely to be profitable. This
1258 // saves compile time, because it doesn't require every single
1259 // stack slot reference to depend on the instruction that does the
1260 // modification.
1261 if (MI->definesRegister(ARM::SP))
1262 return true;
1263
1264 return false;
1265}
1266
Owen Andersonb20b8512010-09-28 18:32:13 +00001267bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
Evan Cheng8239daf2010-11-03 00:45:17 +00001268 unsigned NumCyles,
1269 unsigned ExtraPredCycles,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001270 float Probability,
1271 float Confidence) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00001272 if (!NumCyles)
Evan Cheng13151432010-06-25 22:42:03 +00001273 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001274
Owen Andersonb20b8512010-09-28 18:32:13 +00001275 // Attempt to estimate the relative costs of predication versus branching.
Evan Cheng8239daf2010-11-03 00:45:17 +00001276 float UnpredCost = Probability * NumCyles;
Owen Anderson654d5442010-09-28 21:57:50 +00001277 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001278 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001279
Evan Cheng8239daf2010-11-03 00:45:17 +00001280 return (float)(NumCyles + ExtraPredCycles) < UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001281}
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001282
Evan Cheng13151432010-06-25 22:42:03 +00001283bool ARMBaseInstrInfo::
Evan Cheng8239daf2010-11-03 00:45:17 +00001284isProfitableToIfCvt(MachineBasicBlock &TMBB,
1285 unsigned TCycles, unsigned TExtra,
1286 MachineBasicBlock &FMBB,
1287 unsigned FCycles, unsigned FExtra,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001288 float Probability, float Confidence) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00001289 if (!TCycles || !FCycles)
Owen Andersonb20b8512010-09-28 18:32:13 +00001290 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001291
Owen Andersonb20b8512010-09-28 18:32:13 +00001292 // Attempt to estimate the relative costs of predication versus branching.
Evan Cheng8239daf2010-11-03 00:45:17 +00001293 float UnpredCost = Probability * TCycles + (1.0 - Probability) * FCycles;
Owen Anderson654d5442010-09-28 21:57:50 +00001294 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001295 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001296
Evan Cheng8239daf2010-11-03 00:45:17 +00001297 return (float)(TCycles + FCycles + TExtra + FExtra) < UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001298}
1299
Evan Cheng8fb90362009-08-08 03:20:32 +00001300/// getInstrPredicate - If instruction is predicated, returns its predicate
1301/// condition, otherwise returns AL. It also returns the condition code
1302/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001303ARMCC::CondCodes
1304llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001305 int PIdx = MI->findFirstPredOperandIdx();
1306 if (PIdx == -1) {
1307 PredReg = 0;
1308 return ARMCC::AL;
1309 }
1310
1311 PredReg = MI->getOperand(PIdx+1).getReg();
1312 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1313}
1314
1315
Evan Cheng6495f632009-07-28 05:48:47 +00001316int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001317 if (Opc == ARM::B)
1318 return ARM::Bcc;
1319 else if (Opc == ARM::tB)
1320 return ARM::tBcc;
1321 else if (Opc == ARM::t2B)
1322 return ARM::t2Bcc;
1323
1324 llvm_unreachable("Unknown unconditional branch opcode!");
1325 return 0;
1326}
1327
Evan Cheng6495f632009-07-28 05:48:47 +00001328
1329void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1330 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1331 unsigned DestReg, unsigned BaseReg, int NumBytes,
1332 ARMCC::CondCodes Pred, unsigned PredReg,
1333 const ARMBaseInstrInfo &TII) {
1334 bool isSub = NumBytes < 0;
1335 if (isSub) NumBytes = -NumBytes;
1336
1337 while (NumBytes) {
1338 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1339 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1340 assert(ThisVal && "Didn't extract field correctly");
1341
1342 // We will handle these bits from offset, clear them.
1343 NumBytes &= ~ThisVal;
1344
1345 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1346
1347 // Build the new ADD / SUB.
1348 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1349 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1350 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1351 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1352 BaseReg = DestReg;
1353 }
1354}
1355
Evan Chengcdbb3f52009-08-27 01:23:50 +00001356bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1357 unsigned FrameReg, int &Offset,
1358 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001359 unsigned Opcode = MI.getOpcode();
1360 const TargetInstrDesc &Desc = MI.getDesc();
1361 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1362 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001363
Evan Cheng6495f632009-07-28 05:48:47 +00001364 // Memory operands in inline assembly always use AddrMode2.
1365 if (Opcode == ARM::INLINEASM)
1366 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001367
Evan Cheng6495f632009-07-28 05:48:47 +00001368 if (Opcode == ARM::ADDri) {
1369 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1370 if (Offset == 0) {
1371 // Turn it into a move.
1372 MI.setDesc(TII.get(ARM::MOVr));
1373 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1374 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001375 Offset = 0;
1376 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001377 } else if (Offset < 0) {
1378 Offset = -Offset;
1379 isSub = true;
1380 MI.setDesc(TII.get(ARM::SUBri));
1381 }
1382
1383 // Common case: small offset, fits into instruction.
1384 if (ARM_AM::getSOImmVal(Offset) != -1) {
1385 // Replace the FrameIndex with sp / fp
1386 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1387 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001388 Offset = 0;
1389 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001390 }
1391
1392 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1393 // as possible.
1394 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1395 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1396
1397 // We will handle these bits from offset, clear them.
1398 Offset &= ~ThisImmVal;
1399
1400 // Get the properly encoded SOImmVal field.
1401 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1402 "Bit extraction didn't work?");
1403 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1404 } else {
1405 unsigned ImmIdx = 0;
1406 int InstrOffs = 0;
1407 unsigned NumBits = 0;
1408 unsigned Scale = 1;
1409 switch (AddrMode) {
Jim Grosbach3e556122010-10-26 22:37:02 +00001410 case ARMII::AddrMode_i12: {
1411 ImmIdx = FrameRegIdx + 1;
1412 InstrOffs = MI.getOperand(ImmIdx).getImm();
1413 NumBits = 12;
1414 break;
1415 }
Evan Cheng6495f632009-07-28 05:48:47 +00001416 case ARMII::AddrMode2: {
1417 ImmIdx = FrameRegIdx+2;
1418 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1419 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1420 InstrOffs *= -1;
1421 NumBits = 12;
1422 break;
1423 }
1424 case ARMII::AddrMode3: {
1425 ImmIdx = FrameRegIdx+2;
1426 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1427 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1428 InstrOffs *= -1;
1429 NumBits = 8;
1430 break;
1431 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001432 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001433 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001434 // Can't fold any offset even if it's zero.
1435 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001436 case ARMII::AddrMode5: {
1437 ImmIdx = FrameRegIdx+1;
1438 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1439 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1440 InstrOffs *= -1;
1441 NumBits = 8;
1442 Scale = 4;
1443 break;
1444 }
1445 default:
1446 llvm_unreachable("Unsupported addressing mode!");
1447 break;
1448 }
1449
1450 Offset += InstrOffs * Scale;
1451 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1452 if (Offset < 0) {
1453 Offset = -Offset;
1454 isSub = true;
1455 }
1456
1457 // Attempt to fold address comp. if opcode has offset bits
1458 if (NumBits > 0) {
1459 // Common case: small offset, fits into instruction.
1460 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1461 int ImmedOffset = Offset / Scale;
1462 unsigned Mask = (1 << NumBits) - 1;
1463 if ((unsigned)Offset <= Mask * Scale) {
1464 // Replace the FrameIndex with sp
1465 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach77aee8e2010-10-27 01:19:41 +00001466 // FIXME: When addrmode2 goes away, this will simplify (like the
1467 // T2 version), as the LDR.i12 versions don't need the encoding
1468 // tricks for the offset value.
1469 if (isSub) {
1470 if (AddrMode == ARMII::AddrMode_i12)
1471 ImmedOffset = -ImmedOffset;
1472 else
1473 ImmedOffset |= 1 << NumBits;
1474 }
Evan Cheng6495f632009-07-28 05:48:47 +00001475 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001476 Offset = 0;
1477 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001478 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001479
Evan Cheng6495f632009-07-28 05:48:47 +00001480 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1481 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach063efbf2010-10-27 16:50:31 +00001482 if (isSub) {
1483 if (AddrMode == ARMII::AddrMode_i12)
1484 ImmedOffset = -ImmedOffset;
1485 else
1486 ImmedOffset |= 1 << NumBits;
1487 }
Evan Cheng6495f632009-07-28 05:48:47 +00001488 ImmOp.ChangeToImmediate(ImmedOffset);
1489 Offset &= ~(Mask*Scale);
1490 }
1491 }
1492
Evan Chengcdbb3f52009-08-27 01:23:50 +00001493 Offset = (isSub) ? -Offset : Offset;
1494 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001495}
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001496
1497bool ARMBaseInstrInfo::
Eric Christophera99c3e92010-09-28 04:18:29 +00001498AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1499 int &CmpValue) const {
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001500 switch (MI->getOpcode()) {
1501 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001502 case ARM::CMPri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001503 case ARM::t2CMPri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001504 SrcReg = MI->getOperand(0).getReg();
Gabor Greif04ac81d2010-09-21 12:01:15 +00001505 CmpMask = ~0;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001506 CmpValue = MI->getOperand(1).getImm();
1507 return true;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001508 case ARM::TSTri:
1509 case ARM::t2TSTri:
1510 SrcReg = MI->getOperand(0).getReg();
1511 CmpMask = MI->getOperand(1).getImm();
1512 CmpValue = 0;
1513 return true;
1514 }
1515
1516 return false;
1517}
1518
Gabor Greif05642a32010-09-29 10:12:08 +00001519/// isSuitableForMask - Identify a suitable 'and' instruction that
1520/// operates on the given source register and applies the same mask
1521/// as a 'tst' instruction. Provide a limited look-through for copies.
1522/// When successful, MI will hold the found instruction.
1523static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001524 int CmpMask, bool CommonUse) {
Gabor Greif05642a32010-09-29 10:12:08 +00001525 switch (MI->getOpcode()) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001526 case ARM::ANDri:
1527 case ARM::t2ANDri:
Gabor Greif05642a32010-09-29 10:12:08 +00001528 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001529 return false;
Gabor Greif05642a32010-09-29 10:12:08 +00001530 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greif04ac81d2010-09-21 12:01:15 +00001531 return true;
1532 break;
Gabor Greif05642a32010-09-29 10:12:08 +00001533 case ARM::COPY: {
1534 // Walk down one instruction which is potentially an 'and'.
1535 const MachineInstr &Copy = *MI;
Michael J. Spencerf000a7a2010-10-05 06:00:43 +00001536 MachineBasicBlock::iterator AND(
1537 llvm::next(MachineBasicBlock::iterator(MI)));
Gabor Greif05642a32010-09-29 10:12:08 +00001538 if (AND == MI->getParent()->end()) return false;
1539 MI = AND;
1540 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1541 CmpMask, true);
1542 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001543 }
1544
1545 return false;
1546}
1547
Bill Wendlinga6556862010-09-11 00:13:50 +00001548/// OptimizeCompareInstr - Convert the instruction supplying the argument to the
Evan Chengeb96a2f2010-11-15 21:20:45 +00001549/// comparison into one that sets the zero bit in the flags register.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001550bool ARMBaseInstrInfo::
Gabor Greif04ac81d2010-09-21 12:01:15 +00001551OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
Evan Chengeb96a2f2010-11-15 21:20:45 +00001552 int CmpValue, const MachineRegisterInfo *MRI) const {
Bill Wendling36656612010-09-10 23:46:12 +00001553 if (CmpValue != 0)
Bill Wendling92ad57f2010-09-10 23:34:19 +00001554 return false;
1555
Bill Wendlingb41ee962010-10-18 21:22:31 +00001556 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1557 if (llvm::next(DI) != MRI->def_end())
Bill Wendling92ad57f2010-09-10 23:34:19 +00001558 // Only support one definition.
1559 return false;
1560
1561 MachineInstr *MI = &*DI;
1562
Gabor Greif04ac81d2010-09-21 12:01:15 +00001563 // Masked compares sometimes use the same register as the corresponding 'and'.
1564 if (CmpMask != ~0) {
Gabor Greif05642a32010-09-29 10:12:08 +00001565 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001566 MI = 0;
Bill Wendlingb41ee962010-10-18 21:22:31 +00001567 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1568 UE = MRI->use_end(); UI != UE; ++UI) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001569 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001570 MachineInstr *PotentialAND = &*UI;
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001571 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
Gabor Greif04ac81d2010-09-21 12:01:15 +00001572 continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001573 MI = PotentialAND;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001574 break;
1575 }
1576 if (!MI) return false;
1577 }
1578 }
1579
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001580 // Conservatively refuse to convert an instruction which isn't in the same BB
1581 // as the comparison.
1582 if (MI->getParent() != CmpInstr->getParent())
1583 return false;
1584
1585 // Check that CPSR isn't set between the comparison instruction and the one we
1586 // want to change.
Evan Cheng691e64a2010-09-21 23:49:07 +00001587 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1588 B = MI->getParent()->begin();
Bill Wendling0aa38b92010-10-09 00:03:48 +00001589
1590 // Early exit if CmpInstr is at the beginning of the BB.
1591 if (I == B) return false;
1592
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001593 --I;
1594 for (; I != E; --I) {
1595 const MachineInstr &Instr = *I;
1596
1597 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1598 const MachineOperand &MO = Instr.getOperand(IO);
Bill Wendling40a5eb12010-11-01 20:41:43 +00001599 if (!MO.isReg()) continue;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001600
Bill Wendling40a5eb12010-11-01 20:41:43 +00001601 // This instruction modifies or uses CPSR after the one we want to
1602 // change. We can't do this transformation.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001603 if (MO.getReg() == ARM::CPSR)
1604 return false;
1605 }
Evan Cheng691e64a2010-09-21 23:49:07 +00001606
1607 if (I == B)
1608 // The 'and' is below the comparison instruction.
1609 return false;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001610 }
1611
1612 // Set the "zero" bit in CPSR.
1613 switch (MI->getOpcode()) {
1614 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001615 case ARM::ADDri:
Bob Wilson3a951822010-09-15 17:12:08 +00001616 case ARM::ANDri:
1617 case ARM::t2ANDri:
Bill Wendling38ae9972010-08-11 00:23:00 +00001618 case ARM::SUBri:
1619 case ARM::t2ADDri:
Bill Wendlingad422712010-08-18 21:32:07 +00001620 case ARM::t2SUBri:
Evan Cheng3642e642010-11-17 08:06:50 +00001621 // Toggle the optional operand to CPSR.
1622 MI->getOperand(5).setReg(ARM::CPSR);
1623 MI->getOperand(5).setIsDef(true);
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001624 CmpInstr->eraseFromParent();
1625 return true;
1626 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001627
1628 return false;
1629}
Evan Cheng5f54ce32010-09-09 18:18:55 +00001630
Evan Chengc4af4632010-11-17 20:13:28 +00001631bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
1632 MachineInstr *DefMI, unsigned Reg,
1633 MachineRegisterInfo *MRI) const {
1634 // Fold large immediates into add, sub, or, xor.
1635 unsigned DefOpc = DefMI->getOpcode();
1636 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
1637 return false;
1638 if (!DefMI->getOperand(1).isImm())
1639 // Could be t2MOVi32imm <ga:xx>
1640 return false;
1641
1642 if (!MRI->hasOneNonDBGUse(Reg))
1643 return false;
1644
1645 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng5c71c7a2010-11-18 01:43:23 +00001646 unsigned NewUseOpc = 0;
Evan Chengc4af4632010-11-17 20:13:28 +00001647 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng5c71c7a2010-11-18 01:43:23 +00001648 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Chengc4af4632010-11-17 20:13:28 +00001649 bool Commute = false;
1650 switch (UseOpc) {
1651 default: return false;
1652 case ARM::SUBrr:
1653 case ARM::ADDrr:
1654 case ARM::ORRrr:
1655 case ARM::EORrr:
1656 case ARM::t2SUBrr:
1657 case ARM::t2ADDrr:
1658 case ARM::t2ORRrr:
1659 case ARM::t2EORrr: {
1660 Commute = UseMI->getOperand(2).getReg() != Reg;
1661 switch (UseOpc) {
1662 default: break;
1663 case ARM::SUBrr: {
1664 if (Commute)
1665 return false;
1666 ImmVal = -ImmVal;
1667 NewUseOpc = ARM::SUBri;
1668 // Fallthrough
1669 }
1670 case ARM::ADDrr:
1671 case ARM::ORRrr:
1672 case ARM::EORrr: {
1673 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
1674 return false;
1675 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
1676 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
1677 switch (UseOpc) {
1678 default: break;
1679 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
1680 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
1681 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
1682 }
1683 break;
1684 }
1685 case ARM::t2SUBrr: {
1686 if (Commute)
1687 return false;
1688 ImmVal = -ImmVal;
1689 NewUseOpc = ARM::t2SUBri;
1690 // Fallthrough
1691 }
1692 case ARM::t2ADDrr:
1693 case ARM::t2ORRrr:
1694 case ARM::t2EORrr: {
1695 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
1696 return false;
1697 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
1698 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
1699 switch (UseOpc) {
1700 default: break;
1701 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
1702 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
1703 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
1704 }
1705 break;
1706 }
1707 }
1708 }
1709 }
1710
1711 unsigned OpIdx = Commute ? 2 : 1;
1712 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
1713 bool isKill = UseMI->getOperand(OpIdx).isKill();
1714 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
1715 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
1716 *UseMI, UseMI->getDebugLoc(),
1717 get(NewUseOpc), NewReg)
1718 .addReg(Reg1, getKillRegState(isKill))
1719 .addImm(SOImmValV1)));
1720 UseMI->setDesc(get(NewUseOpc));
1721 UseMI->getOperand(1).setReg(NewReg);
1722 UseMI->getOperand(1).setIsKill();
1723 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
1724 DefMI->eraseFromParent();
1725 return true;
1726}
1727
Evan Cheng5f54ce32010-09-09 18:18:55 +00001728unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +00001729ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1730 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001731 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +00001732 return 1;
1733
1734 const TargetInstrDesc &Desc = MI->getDesc();
1735 unsigned Class = Desc.getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +00001736 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +00001737 if (UOps)
1738 return UOps;
1739
1740 unsigned Opc = MI->getOpcode();
1741 switch (Opc) {
1742 default:
1743 llvm_unreachable("Unexpected multi-uops instruction!");
1744 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001745 case ARM::VLDMQIA:
1746 case ARM::VLDMQDB:
1747 case ARM::VSTMQIA:
1748 case ARM::VSTMQDB:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001749 return 2;
1750
1751 // The number of uOps for load / store multiple are determined by the number
1752 // registers.
Andrew Trick6e8f4c42010-12-24 04:28:06 +00001753 //
Evan Cheng3ef1c872010-09-10 01:29:16 +00001754 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1755 // same cycle. The scheduling for the first load / store must be done
1756 // separately by assuming the the address is not 64-bit aligned.
Bill Wendling73fe34a2010-11-16 01:16:36 +00001757 //
Evan Cheng3ef1c872010-09-10 01:29:16 +00001758 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendling73fe34a2010-11-16 01:16:36 +00001759 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
1760 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
1761 case ARM::VLDMDIA:
1762 case ARM::VLDMDDB:
1763 case ARM::VLDMDIA_UPD:
1764 case ARM::VLDMDDB_UPD:
1765 case ARM::VLDMSIA:
1766 case ARM::VLDMSDB:
1767 case ARM::VLDMSIA_UPD:
1768 case ARM::VLDMSDB_UPD:
1769 case ARM::VSTMDIA:
1770 case ARM::VSTMDDB:
1771 case ARM::VSTMDIA_UPD:
1772 case ARM::VSTMDDB_UPD:
1773 case ARM::VSTMSIA:
1774 case ARM::VSTMSDB:
1775 case ARM::VSTMSIA_UPD:
1776 case ARM::VSTMSDB_UPD: {
Evan Cheng5f54ce32010-09-09 18:18:55 +00001777 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1778 return (NumRegs / 2) + (NumRegs % 2) + 1;
1779 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00001780
1781 case ARM::LDMIA_RET:
1782 case ARM::LDMIA:
1783 case ARM::LDMDA:
1784 case ARM::LDMDB:
1785 case ARM::LDMIB:
1786 case ARM::LDMIA_UPD:
1787 case ARM::LDMDA_UPD:
1788 case ARM::LDMDB_UPD:
1789 case ARM::LDMIB_UPD:
1790 case ARM::STMIA:
1791 case ARM::STMDA:
1792 case ARM::STMDB:
1793 case ARM::STMIB:
1794 case ARM::STMIA_UPD:
1795 case ARM::STMDA_UPD:
1796 case ARM::STMDB_UPD:
1797 case ARM::STMIB_UPD:
1798 case ARM::tLDMIA:
1799 case ARM::tLDMIA_UPD:
1800 case ARM::tSTMIA:
1801 case ARM::tSTMIA_UPD:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001802 case ARM::tPOP_RET:
1803 case ARM::tPOP:
1804 case ARM::tPUSH:
Bill Wendling73fe34a2010-11-16 01:16:36 +00001805 case ARM::t2LDMIA_RET:
1806 case ARM::t2LDMIA:
1807 case ARM::t2LDMDB:
1808 case ARM::t2LDMIA_UPD:
1809 case ARM::t2LDMDB_UPD:
1810 case ARM::t2STMIA:
1811 case ARM::t2STMDB:
1812 case ARM::t2STMIA_UPD:
1813 case ARM::t2STMDB_UPD: {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001814 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1815 if (Subtarget.isCortexA8()) {
Evan Cheng8239daf2010-11-03 00:45:17 +00001816 if (NumRegs < 4)
1817 return 2;
1818 // 4 registers would be issued: 2, 2.
1819 // 5 registers would be issued: 2, 2, 1.
1820 UOps = (NumRegs / 2);
1821 if (NumRegs % 2)
1822 ++UOps;
1823 return UOps;
Evan Cheng3ef1c872010-09-10 01:29:16 +00001824 } else if (Subtarget.isCortexA9()) {
1825 UOps = (NumRegs / 2);
1826 // If there are odd number of registers or if it's not 64-bit aligned,
1827 // then it takes an extra AGU (Address Generation Unit) cycle.
1828 if ((NumRegs % 2) ||
1829 !MI->hasOneMemOperand() ||
1830 (*MI->memoperands_begin())->getAlignment() < 8)
1831 ++UOps;
1832 return UOps;
1833 } else {
1834 // Assume the worst.
1835 return NumRegs;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001836 }
Evan Cheng5f54ce32010-09-09 18:18:55 +00001837 }
1838 }
1839}
Evan Chenga0792de2010-10-06 06:27:31 +00001840
1841int
Evan Cheng344d9db2010-10-07 23:12:15 +00001842ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1843 const TargetInstrDesc &DefTID,
1844 unsigned DefClass,
1845 unsigned DefIdx, unsigned DefAlign) const {
1846 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1847 if (RegNo <= 0)
1848 // Def is the address writeback.
1849 return ItinData->getOperandCycle(DefClass, DefIdx);
1850
1851 int DefCycle;
1852 if (Subtarget.isCortexA8()) {
1853 // (regno / 2) + (regno % 2) + 1
1854 DefCycle = RegNo / 2 + 1;
1855 if (RegNo % 2)
1856 ++DefCycle;
1857 } else if (Subtarget.isCortexA9()) {
1858 DefCycle = RegNo;
1859 bool isSLoad = false;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001860
Evan Cheng344d9db2010-10-07 23:12:15 +00001861 switch (DefTID.getOpcode()) {
1862 default: break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001863 case ARM::VLDMSIA:
1864 case ARM::VLDMSDB:
1865 case ARM::VLDMSIA_UPD:
1866 case ARM::VLDMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00001867 isSLoad = true;
1868 break;
1869 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00001870
Evan Cheng344d9db2010-10-07 23:12:15 +00001871 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1872 // then it takes an extra cycle.
1873 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
1874 ++DefCycle;
1875 } else {
1876 // Assume the worst.
1877 DefCycle = RegNo + 2;
1878 }
1879
1880 return DefCycle;
1881}
1882
1883int
1884ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
1885 const TargetInstrDesc &DefTID,
1886 unsigned DefClass,
1887 unsigned DefIdx, unsigned DefAlign) const {
1888 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1889 if (RegNo <= 0)
1890 // Def is the address writeback.
1891 return ItinData->getOperandCycle(DefClass, DefIdx);
1892
1893 int DefCycle;
1894 if (Subtarget.isCortexA8()) {
1895 // 4 registers would be issued: 1, 2, 1.
1896 // 5 registers would be issued: 1, 2, 2.
1897 DefCycle = RegNo / 2;
1898 if (DefCycle < 1)
1899 DefCycle = 1;
1900 // Result latency is issue cycle + 2: E2.
1901 DefCycle += 2;
1902 } else if (Subtarget.isCortexA9()) {
1903 DefCycle = (RegNo / 2);
1904 // If there are odd number of registers or if it's not 64-bit aligned,
1905 // then it takes an extra AGU (Address Generation Unit) cycle.
1906 if ((RegNo % 2) || DefAlign < 8)
1907 ++DefCycle;
1908 // Result latency is AGU cycles + 2.
1909 DefCycle += 2;
1910 } else {
1911 // Assume the worst.
1912 DefCycle = RegNo + 2;
1913 }
1914
1915 return DefCycle;
1916}
1917
1918int
1919ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
1920 const TargetInstrDesc &UseTID,
1921 unsigned UseClass,
1922 unsigned UseIdx, unsigned UseAlign) const {
1923 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1924 if (RegNo <= 0)
1925 return ItinData->getOperandCycle(UseClass, UseIdx);
1926
1927 int UseCycle;
1928 if (Subtarget.isCortexA8()) {
1929 // (regno / 2) + (regno % 2) + 1
1930 UseCycle = RegNo / 2 + 1;
1931 if (RegNo % 2)
1932 ++UseCycle;
1933 } else if (Subtarget.isCortexA9()) {
1934 UseCycle = RegNo;
1935 bool isSStore = false;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001936
Evan Cheng344d9db2010-10-07 23:12:15 +00001937 switch (UseTID.getOpcode()) {
1938 default: break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001939 case ARM::VSTMSIA:
1940 case ARM::VSTMSDB:
1941 case ARM::VSTMSIA_UPD:
1942 case ARM::VSTMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00001943 isSStore = true;
1944 break;
1945 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00001946
Evan Cheng344d9db2010-10-07 23:12:15 +00001947 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1948 // then it takes an extra cycle.
1949 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
1950 ++UseCycle;
1951 } else {
1952 // Assume the worst.
1953 UseCycle = RegNo + 2;
1954 }
1955
1956 return UseCycle;
1957}
1958
1959int
1960ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
1961 const TargetInstrDesc &UseTID,
1962 unsigned UseClass,
1963 unsigned UseIdx, unsigned UseAlign) const {
1964 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1965 if (RegNo <= 0)
1966 return ItinData->getOperandCycle(UseClass, UseIdx);
1967
1968 int UseCycle;
1969 if (Subtarget.isCortexA8()) {
1970 UseCycle = RegNo / 2;
1971 if (UseCycle < 2)
1972 UseCycle = 2;
1973 // Read in E3.
1974 UseCycle += 2;
1975 } else if (Subtarget.isCortexA9()) {
1976 UseCycle = (RegNo / 2);
1977 // If there are odd number of registers or if it's not 64-bit aligned,
1978 // then it takes an extra AGU (Address Generation Unit) cycle.
1979 if ((RegNo % 2) || UseAlign < 8)
1980 ++UseCycle;
1981 } else {
1982 // Assume the worst.
1983 UseCycle = 1;
1984 }
1985 return UseCycle;
1986}
1987
1988int
Evan Chenga0792de2010-10-06 06:27:31 +00001989ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1990 const TargetInstrDesc &DefTID,
1991 unsigned DefIdx, unsigned DefAlign,
1992 const TargetInstrDesc &UseTID,
1993 unsigned UseIdx, unsigned UseAlign) const {
1994 unsigned DefClass = DefTID.getSchedClass();
1995 unsigned UseClass = UseTID.getSchedClass();
1996
1997 if (DefIdx < DefTID.getNumDefs() && UseIdx < UseTID.getNumOperands())
1998 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1999
2000 // This may be a def / use of a variable_ops instruction, the operand
2001 // latency might be determinable dynamically. Let the target try to
2002 // figure it out.
Evan Cheng9e08ee52010-10-28 02:00:25 +00002003 int DefCycle = -1;
Evan Cheng7e2fe912010-10-28 06:47:08 +00002004 bool LdmBypass = false;
Evan Chenga0792de2010-10-06 06:27:31 +00002005 switch (DefTID.getOpcode()) {
2006 default:
2007 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2008 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002009
2010 case ARM::VLDMDIA:
2011 case ARM::VLDMDDB:
2012 case ARM::VLDMDIA_UPD:
2013 case ARM::VLDMDDB_UPD:
2014 case ARM::VLDMSIA:
2015 case ARM::VLDMSDB:
2016 case ARM::VLDMSIA_UPD:
2017 case ARM::VLDMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002018 DefCycle = getVLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002019 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002020
2021 case ARM::LDMIA_RET:
2022 case ARM::LDMIA:
2023 case ARM::LDMDA:
2024 case ARM::LDMDB:
2025 case ARM::LDMIB:
2026 case ARM::LDMIA_UPD:
2027 case ARM::LDMDA_UPD:
2028 case ARM::LDMDB_UPD:
2029 case ARM::LDMIB_UPD:
2030 case ARM::tLDMIA:
2031 case ARM::tLDMIA_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00002032 case ARM::tPUSH:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002033 case ARM::t2LDMIA_RET:
2034 case ARM::t2LDMIA:
2035 case ARM::t2LDMDB:
2036 case ARM::t2LDMIA_UPD:
2037 case ARM::t2LDMDB_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00002038 LdmBypass = 1;
Evan Cheng344d9db2010-10-07 23:12:15 +00002039 DefCycle = getLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
2040 break;
Evan Chenga0792de2010-10-06 06:27:31 +00002041 }
Evan Chenga0792de2010-10-06 06:27:31 +00002042
2043 if (DefCycle == -1)
2044 // We can't seem to determine the result latency of the def, assume it's 2.
2045 DefCycle = 2;
2046
2047 int UseCycle = -1;
2048 switch (UseTID.getOpcode()) {
2049 default:
2050 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
2051 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002052
2053 case ARM::VSTMDIA:
2054 case ARM::VSTMDDB:
2055 case ARM::VSTMDIA_UPD:
2056 case ARM::VSTMDDB_UPD:
2057 case ARM::VSTMSIA:
2058 case ARM::VSTMSDB:
2059 case ARM::VSTMSIA_UPD:
2060 case ARM::VSTMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002061 UseCycle = getVSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002062 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002063
2064 case ARM::STMIA:
2065 case ARM::STMDA:
2066 case ARM::STMDB:
2067 case ARM::STMIB:
2068 case ARM::STMIA_UPD:
2069 case ARM::STMDA_UPD:
2070 case ARM::STMDB_UPD:
2071 case ARM::STMIB_UPD:
2072 case ARM::tSTMIA:
2073 case ARM::tSTMIA_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00002074 case ARM::tPOP_RET:
2075 case ARM::tPOP:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002076 case ARM::t2STMIA:
2077 case ARM::t2STMDB:
2078 case ARM::t2STMIA_UPD:
2079 case ARM::t2STMDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002080 UseCycle = getSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002081 break;
Evan Chenga0792de2010-10-06 06:27:31 +00002082 }
Evan Chenga0792de2010-10-06 06:27:31 +00002083
2084 if (UseCycle == -1)
2085 // Assume it's read in the first stage.
2086 UseCycle = 1;
2087
2088 UseCycle = DefCycle - UseCycle + 1;
2089 if (UseCycle > 0) {
2090 if (LdmBypass) {
2091 // It's a variable_ops instruction so we can't use DefIdx here. Just use
2092 // first def operand.
2093 if (ItinData->hasPipelineForwarding(DefClass, DefTID.getNumOperands()-1,
2094 UseClass, UseIdx))
2095 --UseCycle;
2096 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendling73fe34a2010-11-16 01:16:36 +00002097 UseClass, UseIdx)) {
Evan Chenga0792de2010-10-06 06:27:31 +00002098 --UseCycle;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002099 }
Evan Chenga0792de2010-10-06 06:27:31 +00002100 }
2101
2102 return UseCycle;
2103}
2104
2105int
2106ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2107 const MachineInstr *DefMI, unsigned DefIdx,
2108 const MachineInstr *UseMI, unsigned UseIdx) const {
2109 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
2110 DefMI->isRegSequence() || DefMI->isImplicitDef())
2111 return 1;
2112
2113 const TargetInstrDesc &DefTID = DefMI->getDesc();
2114 if (!ItinData || ItinData->isEmpty())
2115 return DefTID.mayLoad() ? 3 : 1;
2116
2117 const TargetInstrDesc &UseTID = UseMI->getDesc();
Evan Chengdd9dd6f2010-10-23 02:04:38 +00002118 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
Evan Chenge09206d2010-10-29 23:16:55 +00002119 if (DefMO.getReg() == ARM::CPSR) {
2120 if (DefMI->getOpcode() == ARM::FMSTAT) {
2121 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
2122 return Subtarget.isCortexA9() ? 1 : 20;
2123 }
2124
Evan Chengdd9dd6f2010-10-23 02:04:38 +00002125 // CPSR set and branch can be paired in the same cycle.
Evan Chenge09206d2010-10-29 23:16:55 +00002126 if (UseTID.isBranch())
2127 return 0;
2128 }
Evan Chengdd9dd6f2010-10-23 02:04:38 +00002129
Evan Chenga0792de2010-10-06 06:27:31 +00002130 unsigned DefAlign = DefMI->hasOneMemOperand()
2131 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
2132 unsigned UseAlign = UseMI->hasOneMemOperand()
2133 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00002134 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2135 UseTID, UseIdx, UseAlign);
2136
2137 if (Latency > 1 &&
2138 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2139 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2140 // variants are one cycle cheaper.
2141 switch (DefTID.getOpcode()) {
2142 default: break;
2143 case ARM::LDRrs:
2144 case ARM::LDRBrs: {
2145 unsigned ShOpVal = DefMI->getOperand(3).getImm();
2146 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2147 if (ShImm == 0 ||
2148 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2149 --Latency;
2150 break;
2151 }
2152 case ARM::t2LDRs:
2153 case ARM::t2LDRBs:
2154 case ARM::t2LDRHs:
2155 case ARM::t2LDRSHs: {
2156 // Thumb2 mode: lsl only.
2157 unsigned ShAmt = DefMI->getOperand(3).getImm();
2158 if (ShAmt == 0 || ShAmt == 2)
2159 --Latency;
2160 break;
2161 }
2162 }
2163 }
2164
2165 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00002166}
2167
2168int
2169ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2170 SDNode *DefNode, unsigned DefIdx,
2171 SDNode *UseNode, unsigned UseIdx) const {
2172 if (!DefNode->isMachineOpcode())
2173 return 1;
2174
2175 const TargetInstrDesc &DefTID = get(DefNode->getMachineOpcode());
2176 if (!ItinData || ItinData->isEmpty())
2177 return DefTID.mayLoad() ? 3 : 1;
2178
Evan Cheng08975152010-10-29 18:09:28 +00002179 if (!UseNode->isMachineOpcode()) {
2180 int Latency = ItinData->getOperandCycle(DefTID.getSchedClass(), DefIdx);
2181 if (Subtarget.isCortexA9())
2182 return Latency <= 2 ? 1 : Latency - 1;
2183 else
2184 return Latency <= 3 ? 1 : Latency - 2;
2185 }
Evan Chenga0792de2010-10-06 06:27:31 +00002186
2187 const TargetInstrDesc &UseTID = get(UseNode->getMachineOpcode());
2188 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
2189 unsigned DefAlign = !DefMN->memoperands_empty()
2190 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
2191 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
2192 unsigned UseAlign = !UseMN->memoperands_empty()
2193 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00002194 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2195 UseTID, UseIdx, UseAlign);
2196
2197 if (Latency > 1 &&
2198 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2199 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2200 // variants are one cycle cheaper.
2201 switch (DefTID.getOpcode()) {
2202 default: break;
2203 case ARM::LDRrs:
2204 case ARM::LDRBrs: {
2205 unsigned ShOpVal =
2206 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2207 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2208 if (ShImm == 0 ||
2209 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2210 --Latency;
2211 break;
2212 }
2213 case ARM::t2LDRs:
2214 case ARM::t2LDRBs:
2215 case ARM::t2LDRHs:
2216 case ARM::t2LDRSHs: {
2217 // Thumb2 mode: lsl only.
2218 unsigned ShAmt =
2219 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2220 if (ShAmt == 0 || ShAmt == 2)
2221 --Latency;
2222 break;
2223 }
2224 }
2225 }
2226
2227 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00002228}
Evan Cheng23128422010-10-19 18:58:51 +00002229
Evan Cheng8239daf2010-11-03 00:45:17 +00002230int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2231 const MachineInstr *MI,
2232 unsigned *PredCost) const {
2233 if (MI->isCopyLike() || MI->isInsertSubreg() ||
2234 MI->isRegSequence() || MI->isImplicitDef())
2235 return 1;
2236
2237 if (!ItinData || ItinData->isEmpty())
2238 return 1;
2239
2240 const TargetInstrDesc &TID = MI->getDesc();
2241 unsigned Class = TID.getSchedClass();
2242 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
2243 if (PredCost && TID.hasImplicitDefOfPhysReg(ARM::CPSR))
2244 // When predicated, CPSR is an additional source operand for CPSR updating
2245 // instructions, this apparently increases their latencies.
2246 *PredCost = 1;
2247 if (UOps)
2248 return ItinData->getStageLatency(Class);
2249 return getNumMicroOps(ItinData, MI);
2250}
2251
2252int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2253 SDNode *Node) const {
2254 if (!Node->isMachineOpcode())
2255 return 1;
2256
2257 if (!ItinData || ItinData->isEmpty())
2258 return 1;
2259
2260 unsigned Opcode = Node->getMachineOpcode();
2261 switch (Opcode) {
2262 default:
2263 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendling73fe34a2010-11-16 01:16:36 +00002264 case ARM::VLDMQIA:
2265 case ARM::VLDMQDB:
2266 case ARM::VSTMQIA:
2267 case ARM::VSTMQDB:
Evan Cheng8239daf2010-11-03 00:45:17 +00002268 return 2;
Eric Christopher8b3ca622010-11-18 19:40:05 +00002269 }
Evan Cheng8239daf2010-11-03 00:45:17 +00002270}
2271
Evan Cheng23128422010-10-19 18:58:51 +00002272bool ARMBaseInstrInfo::
2273hasHighOperandLatency(const InstrItineraryData *ItinData,
2274 const MachineRegisterInfo *MRI,
2275 const MachineInstr *DefMI, unsigned DefIdx,
2276 const MachineInstr *UseMI, unsigned UseIdx) const {
2277 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2278 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2279 if (Subtarget.isCortexA8() &&
2280 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2281 // CortexA8 VFP instructions are not pipelined.
2282 return true;
2283
2284 // Hoist VFP / NEON instructions with 4 or higher latency.
2285 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2286 if (Latency <= 3)
2287 return false;
2288 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2289 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2290}
Evan Chengc8141df2010-10-26 02:08:50 +00002291
2292bool ARMBaseInstrInfo::
2293hasLowDefLatency(const InstrItineraryData *ItinData,
2294 const MachineInstr *DefMI, unsigned DefIdx) const {
2295 if (!ItinData || ItinData->isEmpty())
2296 return false;
2297
2298 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2299 if (DDomain == ARMII::DomainGeneral) {
2300 unsigned DefClass = DefMI->getDesc().getSchedClass();
2301 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2302 return (DefCycle != -1 && DefCycle <= 2);
2303 }
2304 return false;
2305}
Evan Cheng48575f62010-12-05 22:04:16 +00002306
2307bool
2308ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
2309 unsigned &AddSubOpc,
2310 bool &NegAcc, bool &HasLane) const {
2311 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
2312 if (I == MLxEntryMap.end())
2313 return false;
2314
2315 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
2316 MulOpc = Entry.MulOpc;
2317 AddSubOpc = Entry.AddSubOpc;
2318 NegAcc = Entry.NegAcc;
2319 HasLane = Entry.HasLane;
2320 return true;
2321}