blob: ef7458d270ee295a878ed7ac189f75d4945f390b [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 Cheng9fe20092011-01-20 08:34:58 +00001058 if (Opcode == ARM::LDRi12 ||
1059 Opcode == ARM::t2LDRpci ||
Evan Cheng9b824252009-11-20 02:10:27 +00001060 Opcode == ARM::t2LDRpci_pic ||
1061 Opcode == ARM::tLDRpci ||
Evan Cheng9fe20092011-01-20 08:34:58 +00001062 Opcode == ARM::tLDRpci_pic ||
1063 Opcode == ARM::MOV_pic_ga_add_pc ||
1064 Opcode == ARM::t2MOV_pic_ga_add_pc) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001065 if (MI1->getOpcode() != Opcode)
1066 return false;
1067 if (MI0->getNumOperands() != MI1->getNumOperands())
1068 return false;
1069
1070 const MachineOperand &MO0 = MI0->getOperand(1);
1071 const MachineOperand &MO1 = MI1->getOperand(1);
Evan Cheng9fe20092011-01-20 08:34:58 +00001072 if (Opcode == ARM::LDRi12 && (!MO0.isCPI() || !MO1.isCPI()))
1073 return false;
1074
Evan Chengd457e6e2009-11-07 04:04:34 +00001075 if (MO0.getOffset() != MO1.getOffset())
1076 return false;
1077
Evan Cheng9fe20092011-01-20 08:34:58 +00001078 if (Opcode == ARM::MOV_pic_ga_add_pc ||
1079 Opcode == ARM::t2MOV_pic_ga_add_pc)
1080 // Ignore the PC labels.
1081 return MO0.getGlobal() == MO1.getGlobal();
1082
Evan Chengd457e6e2009-11-07 04:04:34 +00001083 const MachineFunction *MF = MI0->getParent()->getParent();
1084 const MachineConstantPool *MCP = MF->getConstantPool();
1085 int CPI0 = MO0.getIndex();
1086 int CPI1 = MO1.getIndex();
1087 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1088 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1089 ARMConstantPoolValue *ACPV0 =
1090 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1091 ARMConstantPoolValue *ACPV1 =
1092 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1093 return ACPV0->hasSameValue(ACPV1);
Evan Cheng9fe20092011-01-20 08:34:58 +00001094 } else if (Opcode == ARM::PICLDR) {
1095 if (MI1->getOpcode() != Opcode)
1096 return false;
1097 if (MI0->getNumOperands() != MI1->getNumOperands())
1098 return false;
1099
1100 unsigned Addr0 = MI0->getOperand(1).getReg();
1101 unsigned Addr1 = MI1->getOperand(1).getReg();
1102 if (Addr0 != Addr1) {
1103 if (!MRI ||
1104 !TargetRegisterInfo::isVirtualRegister(Addr0) ||
1105 !TargetRegisterInfo::isVirtualRegister(Addr1))
1106 return false;
1107
1108 // This assumes SSA form.
1109 MachineInstr *Def0 = MRI->getVRegDef(Addr0);
1110 MachineInstr *Def1 = MRI->getVRegDef(Addr1);
1111 // Check if the loaded value, e.g. a constantpool of a global address, are
1112 // the same.
1113 if (!produceSameValue(Def0, Def1, MRI))
1114 return false;
1115 }
1116
1117 for (unsigned i = 3, e = MI0->getNumOperands(); i != e; ++i) {
1118 // %vreg12<def> = PICLDR %vreg11, 0, pred:14, pred:%noreg
1119 const MachineOperand &MO0 = MI0->getOperand(i);
1120 const MachineOperand &MO1 = MI1->getOperand(i);
1121 if (!MO0.isIdenticalTo(MO1))
1122 return false;
1123 }
1124 return true;
Evan Chengd457e6e2009-11-07 04:04:34 +00001125 }
1126
Evan Cheng506049f2010-03-03 01:44:33 +00001127 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001128}
1129
Bill Wendling4b722102010-06-23 23:00:16 +00001130/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1131/// determine if two loads are loading from the same base address. It should
1132/// only return true if the base pointers are the same and the only differences
1133/// between the two addresses is the offset. It also returns the offsets by
1134/// reference.
1135bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1136 int64_t &Offset1,
1137 int64_t &Offset2) const {
1138 // Don't worry about Thumb: just ARM and Thumb2.
1139 if (Subtarget.isThumb1Only()) return false;
1140
1141 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1142 return false;
1143
1144 switch (Load1->getMachineOpcode()) {
1145 default:
1146 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001147 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001148 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001149 case ARM::LDRD:
1150 case ARM::LDRH:
1151 case ARM::LDRSB:
1152 case ARM::LDRSH:
1153 case ARM::VLDRD:
1154 case ARM::VLDRS:
1155 case ARM::t2LDRi8:
1156 case ARM::t2LDRDi8:
1157 case ARM::t2LDRSHi8:
1158 case ARM::t2LDRi12:
1159 case ARM::t2LDRSHi12:
1160 break;
1161 }
1162
1163 switch (Load2->getMachineOpcode()) {
1164 default:
1165 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001166 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001167 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001168 case ARM::LDRD:
1169 case ARM::LDRH:
1170 case ARM::LDRSB:
1171 case ARM::LDRSH:
1172 case ARM::VLDRD:
1173 case ARM::VLDRS:
1174 case ARM::t2LDRi8:
1175 case ARM::t2LDRDi8:
1176 case ARM::t2LDRSHi8:
1177 case ARM::t2LDRi12:
1178 case ARM::t2LDRSHi12:
1179 break;
1180 }
1181
1182 // Check if base addresses and chain operands match.
1183 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1184 Load1->getOperand(4) != Load2->getOperand(4))
1185 return false;
1186
1187 // Index should be Reg0.
1188 if (Load1->getOperand(3) != Load2->getOperand(3))
1189 return false;
1190
1191 // Determine the offsets.
1192 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1193 isa<ConstantSDNode>(Load2->getOperand(1))) {
1194 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1195 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1196 return true;
1197 }
1198
1199 return false;
1200}
1201
1202/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1203/// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1204/// be scheduled togther. On some targets if two loads are loading from
1205/// addresses in the same cache line, it's better if they are scheduled
1206/// together. This function takes two integers that represent the load offsets
1207/// from the common base address. It returns true if it decides it's desirable
1208/// to schedule the two loads together. "NumLoads" is the number of loads that
1209/// have already been scheduled after Load1.
1210bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1211 int64_t Offset1, int64_t Offset2,
1212 unsigned NumLoads) const {
1213 // Don't worry about Thumb: just ARM and Thumb2.
1214 if (Subtarget.isThumb1Only()) return false;
1215
1216 assert(Offset2 > Offset1);
1217
1218 if ((Offset2 - Offset1) / 8 > 64)
1219 return false;
1220
1221 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1222 return false; // FIXME: overly conservative?
1223
1224 // Four loads in a row should be sufficient.
1225 if (NumLoads >= 3)
1226 return false;
1227
1228 return true;
1229}
1230
Evan Cheng86050dc2010-06-18 23:09:54 +00001231bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1232 const MachineBasicBlock *MBB,
1233 const MachineFunction &MF) const {
Jim Grosbach57bb3942010-06-25 18:43:14 +00001234 // Debug info is never a scheduling boundary. It's necessary to be explicit
1235 // due to the special treatment of IT instructions below, otherwise a
1236 // dbg_value followed by an IT will result in the IT instruction being
1237 // considered a scheduling hazard, which is wrong. It should be the actual
1238 // instruction preceding the dbg_value instruction(s), just like it is
1239 // when debug info is not present.
1240 if (MI->isDebugValue())
1241 return false;
1242
Evan Cheng86050dc2010-06-18 23:09:54 +00001243 // Terminators and labels can't be scheduled around.
1244 if (MI->getDesc().isTerminator() || MI->isLabel())
1245 return true;
1246
1247 // Treat the start of the IT block as a scheduling boundary, but schedule
1248 // t2IT along with all instructions following it.
1249 // FIXME: This is a big hammer. But the alternative is to add all potential
1250 // true and anti dependencies to IT block instructions as implicit operands
1251 // to the t2IT instruction. The added compile time and complexity does not
1252 // seem worth it.
1253 MachineBasicBlock::const_iterator I = MI;
Jim Grosbach57bb3942010-06-25 18:43:14 +00001254 // Make sure to skip any dbg_value instructions
1255 while (++I != MBB->end() && I->isDebugValue())
1256 ;
1257 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng86050dc2010-06-18 23:09:54 +00001258 return true;
1259
1260 // Don't attempt to schedule around any instruction that defines
1261 // a stack-oriented pointer, as it's unlikely to be profitable. This
1262 // saves compile time, because it doesn't require every single
1263 // stack slot reference to depend on the instruction that does the
1264 // modification.
1265 if (MI->definesRegister(ARM::SP))
1266 return true;
1267
1268 return false;
1269}
1270
Owen Andersonb20b8512010-09-28 18:32:13 +00001271bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
Evan Cheng8239daf2010-11-03 00:45:17 +00001272 unsigned NumCyles,
1273 unsigned ExtraPredCycles,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001274 float Probability,
1275 float Confidence) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00001276 if (!NumCyles)
Evan Cheng13151432010-06-25 22:42:03 +00001277 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001278
Owen Andersonb20b8512010-09-28 18:32:13 +00001279 // Attempt to estimate the relative costs of predication versus branching.
Evan Cheng8239daf2010-11-03 00:45:17 +00001280 float UnpredCost = Probability * NumCyles;
Owen Anderson654d5442010-09-28 21:57:50 +00001281 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001282 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001283
Evan Cheng8239daf2010-11-03 00:45:17 +00001284 return (float)(NumCyles + ExtraPredCycles) < UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001285}
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001286
Evan Cheng13151432010-06-25 22:42:03 +00001287bool ARMBaseInstrInfo::
Evan Cheng8239daf2010-11-03 00:45:17 +00001288isProfitableToIfCvt(MachineBasicBlock &TMBB,
1289 unsigned TCycles, unsigned TExtra,
1290 MachineBasicBlock &FMBB,
1291 unsigned FCycles, unsigned FExtra,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001292 float Probability, float Confidence) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00001293 if (!TCycles || !FCycles)
Owen Andersonb20b8512010-09-28 18:32:13 +00001294 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001295
Owen Andersonb20b8512010-09-28 18:32:13 +00001296 // Attempt to estimate the relative costs of predication versus branching.
Evan Cheng8239daf2010-11-03 00:45:17 +00001297 float UnpredCost = Probability * TCycles + (1.0 - Probability) * FCycles;
Owen Anderson654d5442010-09-28 21:57:50 +00001298 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001299 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001300
Evan Cheng8239daf2010-11-03 00:45:17 +00001301 return (float)(TCycles + FCycles + TExtra + FExtra) < UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001302}
1303
Evan Cheng8fb90362009-08-08 03:20:32 +00001304/// getInstrPredicate - If instruction is predicated, returns its predicate
1305/// condition, otherwise returns AL. It also returns the condition code
1306/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001307ARMCC::CondCodes
1308llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001309 int PIdx = MI->findFirstPredOperandIdx();
1310 if (PIdx == -1) {
1311 PredReg = 0;
1312 return ARMCC::AL;
1313 }
1314
1315 PredReg = MI->getOperand(PIdx+1).getReg();
1316 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1317}
1318
1319
Evan Cheng6495f632009-07-28 05:48:47 +00001320int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001321 if (Opc == ARM::B)
1322 return ARM::Bcc;
1323 else if (Opc == ARM::tB)
1324 return ARM::tBcc;
1325 else if (Opc == ARM::t2B)
1326 return ARM::t2Bcc;
1327
1328 llvm_unreachable("Unknown unconditional branch opcode!");
1329 return 0;
1330}
1331
Evan Cheng6495f632009-07-28 05:48:47 +00001332
1333void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1334 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1335 unsigned DestReg, unsigned BaseReg, int NumBytes,
1336 ARMCC::CondCodes Pred, unsigned PredReg,
1337 const ARMBaseInstrInfo &TII) {
1338 bool isSub = NumBytes < 0;
1339 if (isSub) NumBytes = -NumBytes;
1340
1341 while (NumBytes) {
1342 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1343 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1344 assert(ThisVal && "Didn't extract field correctly");
1345
1346 // We will handle these bits from offset, clear them.
1347 NumBytes &= ~ThisVal;
1348
1349 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1350
1351 // Build the new ADD / SUB.
1352 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1353 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1354 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1355 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1356 BaseReg = DestReg;
1357 }
1358}
1359
Evan Chengcdbb3f52009-08-27 01:23:50 +00001360bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1361 unsigned FrameReg, int &Offset,
1362 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001363 unsigned Opcode = MI.getOpcode();
1364 const TargetInstrDesc &Desc = MI.getDesc();
1365 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1366 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001367
Evan Cheng6495f632009-07-28 05:48:47 +00001368 // Memory operands in inline assembly always use AddrMode2.
1369 if (Opcode == ARM::INLINEASM)
1370 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001371
Evan Cheng6495f632009-07-28 05:48:47 +00001372 if (Opcode == ARM::ADDri) {
1373 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1374 if (Offset == 0) {
1375 // Turn it into a move.
1376 MI.setDesc(TII.get(ARM::MOVr));
1377 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1378 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001379 Offset = 0;
1380 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001381 } else if (Offset < 0) {
1382 Offset = -Offset;
1383 isSub = true;
1384 MI.setDesc(TII.get(ARM::SUBri));
1385 }
1386
1387 // Common case: small offset, fits into instruction.
1388 if (ARM_AM::getSOImmVal(Offset) != -1) {
1389 // Replace the FrameIndex with sp / fp
1390 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1391 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001392 Offset = 0;
1393 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001394 }
1395
1396 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1397 // as possible.
1398 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1399 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1400
1401 // We will handle these bits from offset, clear them.
1402 Offset &= ~ThisImmVal;
1403
1404 // Get the properly encoded SOImmVal field.
1405 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1406 "Bit extraction didn't work?");
1407 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1408 } else {
1409 unsigned ImmIdx = 0;
1410 int InstrOffs = 0;
1411 unsigned NumBits = 0;
1412 unsigned Scale = 1;
1413 switch (AddrMode) {
Jim Grosbach3e556122010-10-26 22:37:02 +00001414 case ARMII::AddrMode_i12: {
1415 ImmIdx = FrameRegIdx + 1;
1416 InstrOffs = MI.getOperand(ImmIdx).getImm();
1417 NumBits = 12;
1418 break;
1419 }
Evan Cheng6495f632009-07-28 05:48:47 +00001420 case ARMII::AddrMode2: {
1421 ImmIdx = FrameRegIdx+2;
1422 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1423 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1424 InstrOffs *= -1;
1425 NumBits = 12;
1426 break;
1427 }
1428 case ARMII::AddrMode3: {
1429 ImmIdx = FrameRegIdx+2;
1430 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1431 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1432 InstrOffs *= -1;
1433 NumBits = 8;
1434 break;
1435 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001436 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001437 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001438 // Can't fold any offset even if it's zero.
1439 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001440 case ARMII::AddrMode5: {
1441 ImmIdx = FrameRegIdx+1;
1442 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1443 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1444 InstrOffs *= -1;
1445 NumBits = 8;
1446 Scale = 4;
1447 break;
1448 }
1449 default:
1450 llvm_unreachable("Unsupported addressing mode!");
1451 break;
1452 }
1453
1454 Offset += InstrOffs * Scale;
1455 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1456 if (Offset < 0) {
1457 Offset = -Offset;
1458 isSub = true;
1459 }
1460
1461 // Attempt to fold address comp. if opcode has offset bits
1462 if (NumBits > 0) {
1463 // Common case: small offset, fits into instruction.
1464 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1465 int ImmedOffset = Offset / Scale;
1466 unsigned Mask = (1 << NumBits) - 1;
1467 if ((unsigned)Offset <= Mask * Scale) {
1468 // Replace the FrameIndex with sp
1469 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach77aee8e2010-10-27 01:19:41 +00001470 // FIXME: When addrmode2 goes away, this will simplify (like the
1471 // T2 version), as the LDR.i12 versions don't need the encoding
1472 // tricks for the offset value.
1473 if (isSub) {
1474 if (AddrMode == ARMII::AddrMode_i12)
1475 ImmedOffset = -ImmedOffset;
1476 else
1477 ImmedOffset |= 1 << NumBits;
1478 }
Evan Cheng6495f632009-07-28 05:48:47 +00001479 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001480 Offset = 0;
1481 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001482 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001483
Evan Cheng6495f632009-07-28 05:48:47 +00001484 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1485 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach063efbf2010-10-27 16:50:31 +00001486 if (isSub) {
1487 if (AddrMode == ARMII::AddrMode_i12)
1488 ImmedOffset = -ImmedOffset;
1489 else
1490 ImmedOffset |= 1 << NumBits;
1491 }
Evan Cheng6495f632009-07-28 05:48:47 +00001492 ImmOp.ChangeToImmediate(ImmedOffset);
1493 Offset &= ~(Mask*Scale);
1494 }
1495 }
1496
Evan Chengcdbb3f52009-08-27 01:23:50 +00001497 Offset = (isSub) ? -Offset : Offset;
1498 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001499}
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001500
1501bool ARMBaseInstrInfo::
Eric Christophera99c3e92010-09-28 04:18:29 +00001502AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1503 int &CmpValue) const {
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001504 switch (MI->getOpcode()) {
1505 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001506 case ARM::CMPri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001507 case ARM::t2CMPri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001508 SrcReg = MI->getOperand(0).getReg();
Gabor Greif04ac81d2010-09-21 12:01:15 +00001509 CmpMask = ~0;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001510 CmpValue = MI->getOperand(1).getImm();
1511 return true;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001512 case ARM::TSTri:
1513 case ARM::t2TSTri:
1514 SrcReg = MI->getOperand(0).getReg();
1515 CmpMask = MI->getOperand(1).getImm();
1516 CmpValue = 0;
1517 return true;
1518 }
1519
1520 return false;
1521}
1522
Gabor Greif05642a32010-09-29 10:12:08 +00001523/// isSuitableForMask - Identify a suitable 'and' instruction that
1524/// operates on the given source register and applies the same mask
1525/// as a 'tst' instruction. Provide a limited look-through for copies.
1526/// When successful, MI will hold the found instruction.
1527static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001528 int CmpMask, bool CommonUse) {
Gabor Greif05642a32010-09-29 10:12:08 +00001529 switch (MI->getOpcode()) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001530 case ARM::ANDri:
1531 case ARM::t2ANDri:
Gabor Greif05642a32010-09-29 10:12:08 +00001532 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001533 return false;
Gabor Greif05642a32010-09-29 10:12:08 +00001534 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greif04ac81d2010-09-21 12:01:15 +00001535 return true;
1536 break;
Gabor Greif05642a32010-09-29 10:12:08 +00001537 case ARM::COPY: {
1538 // Walk down one instruction which is potentially an 'and'.
1539 const MachineInstr &Copy = *MI;
Michael J. Spencerf000a7a2010-10-05 06:00:43 +00001540 MachineBasicBlock::iterator AND(
1541 llvm::next(MachineBasicBlock::iterator(MI)));
Gabor Greif05642a32010-09-29 10:12:08 +00001542 if (AND == MI->getParent()->end()) return false;
1543 MI = AND;
1544 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1545 CmpMask, true);
1546 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001547 }
1548
1549 return false;
1550}
1551
Bill Wendlinga6556862010-09-11 00:13:50 +00001552/// OptimizeCompareInstr - Convert the instruction supplying the argument to the
Evan Chengeb96a2f2010-11-15 21:20:45 +00001553/// comparison into one that sets the zero bit in the flags register.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001554bool ARMBaseInstrInfo::
Gabor Greif04ac81d2010-09-21 12:01:15 +00001555OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
Evan Chengeb96a2f2010-11-15 21:20:45 +00001556 int CmpValue, const MachineRegisterInfo *MRI) const {
Bill Wendling36656612010-09-10 23:46:12 +00001557 if (CmpValue != 0)
Bill Wendling92ad57f2010-09-10 23:34:19 +00001558 return false;
1559
Bill Wendlingb41ee962010-10-18 21:22:31 +00001560 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1561 if (llvm::next(DI) != MRI->def_end())
Bill Wendling92ad57f2010-09-10 23:34:19 +00001562 // Only support one definition.
1563 return false;
1564
1565 MachineInstr *MI = &*DI;
1566
Gabor Greif04ac81d2010-09-21 12:01:15 +00001567 // Masked compares sometimes use the same register as the corresponding 'and'.
1568 if (CmpMask != ~0) {
Gabor Greif05642a32010-09-29 10:12:08 +00001569 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001570 MI = 0;
Bill Wendlingb41ee962010-10-18 21:22:31 +00001571 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1572 UE = MRI->use_end(); UI != UE; ++UI) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001573 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001574 MachineInstr *PotentialAND = &*UI;
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001575 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
Gabor Greif04ac81d2010-09-21 12:01:15 +00001576 continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001577 MI = PotentialAND;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001578 break;
1579 }
1580 if (!MI) return false;
1581 }
1582 }
1583
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001584 // Conservatively refuse to convert an instruction which isn't in the same BB
1585 // as the comparison.
1586 if (MI->getParent() != CmpInstr->getParent())
1587 return false;
1588
1589 // Check that CPSR isn't set between the comparison instruction and the one we
1590 // want to change.
Evan Cheng691e64a2010-09-21 23:49:07 +00001591 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1592 B = MI->getParent()->begin();
Bill Wendling0aa38b92010-10-09 00:03:48 +00001593
1594 // Early exit if CmpInstr is at the beginning of the BB.
1595 if (I == B) return false;
1596
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001597 --I;
1598 for (; I != E; --I) {
1599 const MachineInstr &Instr = *I;
1600
1601 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1602 const MachineOperand &MO = Instr.getOperand(IO);
Bill Wendling40a5eb12010-11-01 20:41:43 +00001603 if (!MO.isReg()) continue;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001604
Bill Wendling40a5eb12010-11-01 20:41:43 +00001605 // This instruction modifies or uses CPSR after the one we want to
1606 // change. We can't do this transformation.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001607 if (MO.getReg() == ARM::CPSR)
1608 return false;
1609 }
Evan Cheng691e64a2010-09-21 23:49:07 +00001610
1611 if (I == B)
1612 // The 'and' is below the comparison instruction.
1613 return false;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001614 }
1615
1616 // Set the "zero" bit in CPSR.
1617 switch (MI->getOpcode()) {
1618 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001619 case ARM::ADDri:
Bob Wilson3a951822010-09-15 17:12:08 +00001620 case ARM::ANDri:
1621 case ARM::t2ANDri:
Bill Wendling38ae9972010-08-11 00:23:00 +00001622 case ARM::SUBri:
1623 case ARM::t2ADDri:
Bill Wendlingad422712010-08-18 21:32:07 +00001624 case ARM::t2SUBri:
Evan Cheng3642e642010-11-17 08:06:50 +00001625 // Toggle the optional operand to CPSR.
1626 MI->getOperand(5).setReg(ARM::CPSR);
1627 MI->getOperand(5).setIsDef(true);
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001628 CmpInstr->eraseFromParent();
1629 return true;
1630 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001631
1632 return false;
1633}
Evan Cheng5f54ce32010-09-09 18:18:55 +00001634
Evan Chengc4af4632010-11-17 20:13:28 +00001635bool ARMBaseInstrInfo::FoldImmediate(MachineInstr *UseMI,
1636 MachineInstr *DefMI, unsigned Reg,
1637 MachineRegisterInfo *MRI) const {
1638 // Fold large immediates into add, sub, or, xor.
1639 unsigned DefOpc = DefMI->getOpcode();
1640 if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm)
1641 return false;
1642 if (!DefMI->getOperand(1).isImm())
1643 // Could be t2MOVi32imm <ga:xx>
1644 return false;
1645
1646 if (!MRI->hasOneNonDBGUse(Reg))
1647 return false;
1648
1649 unsigned UseOpc = UseMI->getOpcode();
Evan Cheng5c71c7a2010-11-18 01:43:23 +00001650 unsigned NewUseOpc = 0;
Evan Chengc4af4632010-11-17 20:13:28 +00001651 uint32_t ImmVal = (uint32_t)DefMI->getOperand(1).getImm();
Evan Cheng5c71c7a2010-11-18 01:43:23 +00001652 uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
Evan Chengc4af4632010-11-17 20:13:28 +00001653 bool Commute = false;
1654 switch (UseOpc) {
1655 default: return false;
1656 case ARM::SUBrr:
1657 case ARM::ADDrr:
1658 case ARM::ORRrr:
1659 case ARM::EORrr:
1660 case ARM::t2SUBrr:
1661 case ARM::t2ADDrr:
1662 case ARM::t2ORRrr:
1663 case ARM::t2EORrr: {
1664 Commute = UseMI->getOperand(2).getReg() != Reg;
1665 switch (UseOpc) {
1666 default: break;
1667 case ARM::SUBrr: {
1668 if (Commute)
1669 return false;
1670 ImmVal = -ImmVal;
1671 NewUseOpc = ARM::SUBri;
1672 // Fallthrough
1673 }
1674 case ARM::ADDrr:
1675 case ARM::ORRrr:
1676 case ARM::EORrr: {
1677 if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
1678 return false;
1679 SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
1680 SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
1681 switch (UseOpc) {
1682 default: break;
1683 case ARM::ADDrr: NewUseOpc = ARM::ADDri; break;
1684 case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
1685 case ARM::EORrr: NewUseOpc = ARM::EORri; break;
1686 }
1687 break;
1688 }
1689 case ARM::t2SUBrr: {
1690 if (Commute)
1691 return false;
1692 ImmVal = -ImmVal;
1693 NewUseOpc = ARM::t2SUBri;
1694 // Fallthrough
1695 }
1696 case ARM::t2ADDrr:
1697 case ARM::t2ORRrr:
1698 case ARM::t2EORrr: {
1699 if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
1700 return false;
1701 SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
1702 SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
1703 switch (UseOpc) {
1704 default: break;
1705 case ARM::t2ADDrr: NewUseOpc = ARM::t2ADDri; break;
1706 case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
1707 case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
1708 }
1709 break;
1710 }
1711 }
1712 }
1713 }
1714
1715 unsigned OpIdx = Commute ? 2 : 1;
1716 unsigned Reg1 = UseMI->getOperand(OpIdx).getReg();
1717 bool isKill = UseMI->getOperand(OpIdx).isKill();
1718 unsigned NewReg = MRI->createVirtualRegister(MRI->getRegClass(Reg));
1719 AddDefaultCC(AddDefaultPred(BuildMI(*UseMI->getParent(),
1720 *UseMI, UseMI->getDebugLoc(),
1721 get(NewUseOpc), NewReg)
1722 .addReg(Reg1, getKillRegState(isKill))
1723 .addImm(SOImmValV1)));
1724 UseMI->setDesc(get(NewUseOpc));
1725 UseMI->getOperand(1).setReg(NewReg);
1726 UseMI->getOperand(1).setIsKill();
1727 UseMI->getOperand(2).ChangeToImmediate(SOImmValV2);
1728 DefMI->eraseFromParent();
1729 return true;
1730}
1731
Evan Cheng5f54ce32010-09-09 18:18:55 +00001732unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +00001733ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1734 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001735 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +00001736 return 1;
1737
1738 const TargetInstrDesc &Desc = MI->getDesc();
1739 unsigned Class = Desc.getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +00001740 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +00001741 if (UOps)
1742 return UOps;
1743
1744 unsigned Opc = MI->getOpcode();
1745 switch (Opc) {
1746 default:
1747 llvm_unreachable("Unexpected multi-uops instruction!");
1748 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001749 case ARM::VLDMQIA:
1750 case ARM::VLDMQDB:
1751 case ARM::VSTMQIA:
1752 case ARM::VSTMQDB:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001753 return 2;
1754
1755 // The number of uOps for load / store multiple are determined by the number
1756 // registers.
Andrew Trick6e8f4c42010-12-24 04:28:06 +00001757 //
Evan Cheng3ef1c872010-09-10 01:29:16 +00001758 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1759 // same cycle. The scheduling for the first load / store must be done
1760 // separately by assuming the the address is not 64-bit aligned.
Bill Wendling73fe34a2010-11-16 01:16:36 +00001761 //
Evan Cheng3ef1c872010-09-10 01:29:16 +00001762 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
Bill Wendling73fe34a2010-11-16 01:16:36 +00001763 // is not 64-bit aligned, then AGU would take an extra cycle. For VFP / NEON
1764 // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
1765 case ARM::VLDMDIA:
1766 case ARM::VLDMDDB:
1767 case ARM::VLDMDIA_UPD:
1768 case ARM::VLDMDDB_UPD:
1769 case ARM::VLDMSIA:
1770 case ARM::VLDMSDB:
1771 case ARM::VLDMSIA_UPD:
1772 case ARM::VLDMSDB_UPD:
1773 case ARM::VSTMDIA:
1774 case ARM::VSTMDDB:
1775 case ARM::VSTMDIA_UPD:
1776 case ARM::VSTMDDB_UPD:
1777 case ARM::VSTMSIA:
1778 case ARM::VSTMSDB:
1779 case ARM::VSTMSIA_UPD:
1780 case ARM::VSTMSDB_UPD: {
Evan Cheng5f54ce32010-09-09 18:18:55 +00001781 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1782 return (NumRegs / 2) + (NumRegs % 2) + 1;
1783 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00001784
1785 case ARM::LDMIA_RET:
1786 case ARM::LDMIA:
1787 case ARM::LDMDA:
1788 case ARM::LDMDB:
1789 case ARM::LDMIB:
1790 case ARM::LDMIA_UPD:
1791 case ARM::LDMDA_UPD:
1792 case ARM::LDMDB_UPD:
1793 case ARM::LDMIB_UPD:
1794 case ARM::STMIA:
1795 case ARM::STMDA:
1796 case ARM::STMDB:
1797 case ARM::STMIB:
1798 case ARM::STMIA_UPD:
1799 case ARM::STMDA_UPD:
1800 case ARM::STMDB_UPD:
1801 case ARM::STMIB_UPD:
1802 case ARM::tLDMIA:
1803 case ARM::tLDMIA_UPD:
1804 case ARM::tSTMIA:
1805 case ARM::tSTMIA_UPD:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001806 case ARM::tPOP_RET:
1807 case ARM::tPOP:
1808 case ARM::tPUSH:
Bill Wendling73fe34a2010-11-16 01:16:36 +00001809 case ARM::t2LDMIA_RET:
1810 case ARM::t2LDMIA:
1811 case ARM::t2LDMDB:
1812 case ARM::t2LDMIA_UPD:
1813 case ARM::t2LDMDB_UPD:
1814 case ARM::t2STMIA:
1815 case ARM::t2STMDB:
1816 case ARM::t2STMIA_UPD:
1817 case ARM::t2STMDB_UPD: {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001818 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1819 if (Subtarget.isCortexA8()) {
Evan Cheng8239daf2010-11-03 00:45:17 +00001820 if (NumRegs < 4)
1821 return 2;
1822 // 4 registers would be issued: 2, 2.
1823 // 5 registers would be issued: 2, 2, 1.
1824 UOps = (NumRegs / 2);
1825 if (NumRegs % 2)
1826 ++UOps;
1827 return UOps;
Evan Cheng3ef1c872010-09-10 01:29:16 +00001828 } else if (Subtarget.isCortexA9()) {
1829 UOps = (NumRegs / 2);
1830 // If there are odd number of registers or if it's not 64-bit aligned,
1831 // then it takes an extra AGU (Address Generation Unit) cycle.
1832 if ((NumRegs % 2) ||
1833 !MI->hasOneMemOperand() ||
1834 (*MI->memoperands_begin())->getAlignment() < 8)
1835 ++UOps;
1836 return UOps;
1837 } else {
1838 // Assume the worst.
1839 return NumRegs;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001840 }
Evan Cheng5f54ce32010-09-09 18:18:55 +00001841 }
1842 }
1843}
Evan Chenga0792de2010-10-06 06:27:31 +00001844
1845int
Evan Cheng344d9db2010-10-07 23:12:15 +00001846ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1847 const TargetInstrDesc &DefTID,
1848 unsigned DefClass,
1849 unsigned DefIdx, unsigned DefAlign) const {
1850 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1851 if (RegNo <= 0)
1852 // Def is the address writeback.
1853 return ItinData->getOperandCycle(DefClass, DefIdx);
1854
1855 int DefCycle;
1856 if (Subtarget.isCortexA8()) {
1857 // (regno / 2) + (regno % 2) + 1
1858 DefCycle = RegNo / 2 + 1;
1859 if (RegNo % 2)
1860 ++DefCycle;
1861 } else if (Subtarget.isCortexA9()) {
1862 DefCycle = RegNo;
1863 bool isSLoad = false;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001864
Evan Cheng344d9db2010-10-07 23:12:15 +00001865 switch (DefTID.getOpcode()) {
1866 default: break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001867 case ARM::VLDMSIA:
1868 case ARM::VLDMSDB:
1869 case ARM::VLDMSIA_UPD:
1870 case ARM::VLDMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00001871 isSLoad = true;
1872 break;
1873 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00001874
Evan Cheng344d9db2010-10-07 23:12:15 +00001875 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1876 // then it takes an extra cycle.
1877 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
1878 ++DefCycle;
1879 } else {
1880 // Assume the worst.
1881 DefCycle = RegNo + 2;
1882 }
1883
1884 return DefCycle;
1885}
1886
1887int
1888ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
1889 const TargetInstrDesc &DefTID,
1890 unsigned DefClass,
1891 unsigned DefIdx, unsigned DefAlign) const {
1892 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1893 if (RegNo <= 0)
1894 // Def is the address writeback.
1895 return ItinData->getOperandCycle(DefClass, DefIdx);
1896
1897 int DefCycle;
1898 if (Subtarget.isCortexA8()) {
1899 // 4 registers would be issued: 1, 2, 1.
1900 // 5 registers would be issued: 1, 2, 2.
1901 DefCycle = RegNo / 2;
1902 if (DefCycle < 1)
1903 DefCycle = 1;
1904 // Result latency is issue cycle + 2: E2.
1905 DefCycle += 2;
1906 } else if (Subtarget.isCortexA9()) {
1907 DefCycle = (RegNo / 2);
1908 // If there are odd number of registers or if it's not 64-bit aligned,
1909 // then it takes an extra AGU (Address Generation Unit) cycle.
1910 if ((RegNo % 2) || DefAlign < 8)
1911 ++DefCycle;
1912 // Result latency is AGU cycles + 2.
1913 DefCycle += 2;
1914 } else {
1915 // Assume the worst.
1916 DefCycle = RegNo + 2;
1917 }
1918
1919 return DefCycle;
1920}
1921
1922int
1923ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
1924 const TargetInstrDesc &UseTID,
1925 unsigned UseClass,
1926 unsigned UseIdx, unsigned UseAlign) const {
1927 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1928 if (RegNo <= 0)
1929 return ItinData->getOperandCycle(UseClass, UseIdx);
1930
1931 int UseCycle;
1932 if (Subtarget.isCortexA8()) {
1933 // (regno / 2) + (regno % 2) + 1
1934 UseCycle = RegNo / 2 + 1;
1935 if (RegNo % 2)
1936 ++UseCycle;
1937 } else if (Subtarget.isCortexA9()) {
1938 UseCycle = RegNo;
1939 bool isSStore = false;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001940
Evan Cheng344d9db2010-10-07 23:12:15 +00001941 switch (UseTID.getOpcode()) {
1942 default: break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00001943 case ARM::VSTMSIA:
1944 case ARM::VSTMSDB:
1945 case ARM::VSTMSIA_UPD:
1946 case ARM::VSTMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00001947 isSStore = true;
1948 break;
1949 }
Bill Wendling73fe34a2010-11-16 01:16:36 +00001950
Evan Cheng344d9db2010-10-07 23:12:15 +00001951 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1952 // then it takes an extra cycle.
1953 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
1954 ++UseCycle;
1955 } else {
1956 // Assume the worst.
1957 UseCycle = RegNo + 2;
1958 }
1959
1960 return UseCycle;
1961}
1962
1963int
1964ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
1965 const TargetInstrDesc &UseTID,
1966 unsigned UseClass,
1967 unsigned UseIdx, unsigned UseAlign) const {
1968 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1969 if (RegNo <= 0)
1970 return ItinData->getOperandCycle(UseClass, UseIdx);
1971
1972 int UseCycle;
1973 if (Subtarget.isCortexA8()) {
1974 UseCycle = RegNo / 2;
1975 if (UseCycle < 2)
1976 UseCycle = 2;
1977 // Read in E3.
1978 UseCycle += 2;
1979 } else if (Subtarget.isCortexA9()) {
1980 UseCycle = (RegNo / 2);
1981 // If there are odd number of registers or if it's not 64-bit aligned,
1982 // then it takes an extra AGU (Address Generation Unit) cycle.
1983 if ((RegNo % 2) || UseAlign < 8)
1984 ++UseCycle;
1985 } else {
1986 // Assume the worst.
1987 UseCycle = 1;
1988 }
1989 return UseCycle;
1990}
1991
1992int
Evan Chenga0792de2010-10-06 06:27:31 +00001993ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1994 const TargetInstrDesc &DefTID,
1995 unsigned DefIdx, unsigned DefAlign,
1996 const TargetInstrDesc &UseTID,
1997 unsigned UseIdx, unsigned UseAlign) const {
1998 unsigned DefClass = DefTID.getSchedClass();
1999 unsigned UseClass = UseTID.getSchedClass();
2000
2001 if (DefIdx < DefTID.getNumDefs() && UseIdx < UseTID.getNumOperands())
2002 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
2003
2004 // This may be a def / use of a variable_ops instruction, the operand
2005 // latency might be determinable dynamically. Let the target try to
2006 // figure it out.
Evan Cheng9e08ee52010-10-28 02:00:25 +00002007 int DefCycle = -1;
Evan Cheng7e2fe912010-10-28 06:47:08 +00002008 bool LdmBypass = false;
Evan Chenga0792de2010-10-06 06:27:31 +00002009 switch (DefTID.getOpcode()) {
2010 default:
2011 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2012 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002013
2014 case ARM::VLDMDIA:
2015 case ARM::VLDMDDB:
2016 case ARM::VLDMDIA_UPD:
2017 case ARM::VLDMDDB_UPD:
2018 case ARM::VLDMSIA:
2019 case ARM::VLDMSDB:
2020 case ARM::VLDMSIA_UPD:
2021 case ARM::VLDMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002022 DefCycle = getVLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002023 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002024
2025 case ARM::LDMIA_RET:
2026 case ARM::LDMIA:
2027 case ARM::LDMDA:
2028 case ARM::LDMDB:
2029 case ARM::LDMIB:
2030 case ARM::LDMIA_UPD:
2031 case ARM::LDMDA_UPD:
2032 case ARM::LDMDB_UPD:
2033 case ARM::LDMIB_UPD:
2034 case ARM::tLDMIA:
2035 case ARM::tLDMIA_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00002036 case ARM::tPUSH:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002037 case ARM::t2LDMIA_RET:
2038 case ARM::t2LDMIA:
2039 case ARM::t2LDMDB:
2040 case ARM::t2LDMIA_UPD:
2041 case ARM::t2LDMDB_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00002042 LdmBypass = 1;
Evan Cheng344d9db2010-10-07 23:12:15 +00002043 DefCycle = getLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
2044 break;
Evan Chenga0792de2010-10-06 06:27:31 +00002045 }
Evan Chenga0792de2010-10-06 06:27:31 +00002046
2047 if (DefCycle == -1)
2048 // We can't seem to determine the result latency of the def, assume it's 2.
2049 DefCycle = 2;
2050
2051 int UseCycle = -1;
2052 switch (UseTID.getOpcode()) {
2053 default:
2054 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
2055 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002056
2057 case ARM::VSTMDIA:
2058 case ARM::VSTMDDB:
2059 case ARM::VSTMDIA_UPD:
2060 case ARM::VSTMDDB_UPD:
2061 case ARM::VSTMSIA:
2062 case ARM::VSTMSDB:
2063 case ARM::VSTMSIA_UPD:
2064 case ARM::VSTMSDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002065 UseCycle = getVSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002066 break;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002067
2068 case ARM::STMIA:
2069 case ARM::STMDA:
2070 case ARM::STMDB:
2071 case ARM::STMIB:
2072 case ARM::STMIA_UPD:
2073 case ARM::STMDA_UPD:
2074 case ARM::STMDB_UPD:
2075 case ARM::STMIB_UPD:
2076 case ARM::tSTMIA:
2077 case ARM::tSTMIA_UPD:
Evan Chenga0792de2010-10-06 06:27:31 +00002078 case ARM::tPOP_RET:
2079 case ARM::tPOP:
Bill Wendling73fe34a2010-11-16 01:16:36 +00002080 case ARM::t2STMIA:
2081 case ARM::t2STMDB:
2082 case ARM::t2STMIA_UPD:
2083 case ARM::t2STMDB_UPD:
Evan Cheng344d9db2010-10-07 23:12:15 +00002084 UseCycle = getSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00002085 break;
Evan Chenga0792de2010-10-06 06:27:31 +00002086 }
Evan Chenga0792de2010-10-06 06:27:31 +00002087
2088 if (UseCycle == -1)
2089 // Assume it's read in the first stage.
2090 UseCycle = 1;
2091
2092 UseCycle = DefCycle - UseCycle + 1;
2093 if (UseCycle > 0) {
2094 if (LdmBypass) {
2095 // It's a variable_ops instruction so we can't use DefIdx here. Just use
2096 // first def operand.
2097 if (ItinData->hasPipelineForwarding(DefClass, DefTID.getNumOperands()-1,
2098 UseClass, UseIdx))
2099 --UseCycle;
2100 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
Bill Wendling73fe34a2010-11-16 01:16:36 +00002101 UseClass, UseIdx)) {
Evan Chenga0792de2010-10-06 06:27:31 +00002102 --UseCycle;
Bill Wendling73fe34a2010-11-16 01:16:36 +00002103 }
Evan Chenga0792de2010-10-06 06:27:31 +00002104 }
2105
2106 return UseCycle;
2107}
2108
2109int
2110ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2111 const MachineInstr *DefMI, unsigned DefIdx,
2112 const MachineInstr *UseMI, unsigned UseIdx) const {
2113 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
2114 DefMI->isRegSequence() || DefMI->isImplicitDef())
2115 return 1;
2116
2117 const TargetInstrDesc &DefTID = DefMI->getDesc();
2118 if (!ItinData || ItinData->isEmpty())
2119 return DefTID.mayLoad() ? 3 : 1;
2120
2121 const TargetInstrDesc &UseTID = UseMI->getDesc();
Evan Chengdd9dd6f2010-10-23 02:04:38 +00002122 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
Evan Chenge09206d2010-10-29 23:16:55 +00002123 if (DefMO.getReg() == ARM::CPSR) {
2124 if (DefMI->getOpcode() == ARM::FMSTAT) {
2125 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
2126 return Subtarget.isCortexA9() ? 1 : 20;
2127 }
2128
Evan Chengdd9dd6f2010-10-23 02:04:38 +00002129 // CPSR set and branch can be paired in the same cycle.
Evan Chenge09206d2010-10-29 23:16:55 +00002130 if (UseTID.isBranch())
2131 return 0;
2132 }
Evan Chengdd9dd6f2010-10-23 02:04:38 +00002133
Evan Chenga0792de2010-10-06 06:27:31 +00002134 unsigned DefAlign = DefMI->hasOneMemOperand()
2135 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
2136 unsigned UseAlign = UseMI->hasOneMemOperand()
2137 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00002138 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2139 UseTID, UseIdx, UseAlign);
2140
2141 if (Latency > 1 &&
2142 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2143 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2144 // variants are one cycle cheaper.
2145 switch (DefTID.getOpcode()) {
2146 default: break;
2147 case ARM::LDRrs:
2148 case ARM::LDRBrs: {
2149 unsigned ShOpVal = DefMI->getOperand(3).getImm();
2150 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2151 if (ShImm == 0 ||
2152 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2153 --Latency;
2154 break;
2155 }
2156 case ARM::t2LDRs:
2157 case ARM::t2LDRBs:
2158 case ARM::t2LDRHs:
2159 case ARM::t2LDRSHs: {
2160 // Thumb2 mode: lsl only.
2161 unsigned ShAmt = DefMI->getOperand(3).getImm();
2162 if (ShAmt == 0 || ShAmt == 2)
2163 --Latency;
2164 break;
2165 }
2166 }
2167 }
2168
2169 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00002170}
2171
2172int
2173ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
2174 SDNode *DefNode, unsigned DefIdx,
2175 SDNode *UseNode, unsigned UseIdx) const {
2176 if (!DefNode->isMachineOpcode())
2177 return 1;
2178
2179 const TargetInstrDesc &DefTID = get(DefNode->getMachineOpcode());
2180 if (!ItinData || ItinData->isEmpty())
2181 return DefTID.mayLoad() ? 3 : 1;
2182
Evan Cheng08975152010-10-29 18:09:28 +00002183 if (!UseNode->isMachineOpcode()) {
2184 int Latency = ItinData->getOperandCycle(DefTID.getSchedClass(), DefIdx);
2185 if (Subtarget.isCortexA9())
2186 return Latency <= 2 ? 1 : Latency - 1;
2187 else
2188 return Latency <= 3 ? 1 : Latency - 2;
2189 }
Evan Chenga0792de2010-10-06 06:27:31 +00002190
2191 const TargetInstrDesc &UseTID = get(UseNode->getMachineOpcode());
2192 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
2193 unsigned DefAlign = !DefMN->memoperands_empty()
2194 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
2195 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
2196 unsigned UseAlign = !UseMN->memoperands_empty()
2197 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00002198 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
2199 UseTID, UseIdx, UseAlign);
2200
2201 if (Latency > 1 &&
2202 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
2203 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
2204 // variants are one cycle cheaper.
2205 switch (DefTID.getOpcode()) {
2206 default: break;
2207 case ARM::LDRrs:
2208 case ARM::LDRBrs: {
2209 unsigned ShOpVal =
2210 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2211 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
2212 if (ShImm == 0 ||
2213 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
2214 --Latency;
2215 break;
2216 }
2217 case ARM::t2LDRs:
2218 case ARM::t2LDRBs:
2219 case ARM::t2LDRHs:
2220 case ARM::t2LDRSHs: {
2221 // Thumb2 mode: lsl only.
2222 unsigned ShAmt =
2223 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2224 if (ShAmt == 0 || ShAmt == 2)
2225 --Latency;
2226 break;
2227 }
2228 }
2229 }
2230
2231 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00002232}
Evan Cheng23128422010-10-19 18:58:51 +00002233
Evan Cheng8239daf2010-11-03 00:45:17 +00002234int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2235 const MachineInstr *MI,
2236 unsigned *PredCost) const {
2237 if (MI->isCopyLike() || MI->isInsertSubreg() ||
2238 MI->isRegSequence() || MI->isImplicitDef())
2239 return 1;
2240
2241 if (!ItinData || ItinData->isEmpty())
2242 return 1;
2243
2244 const TargetInstrDesc &TID = MI->getDesc();
2245 unsigned Class = TID.getSchedClass();
2246 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
2247 if (PredCost && TID.hasImplicitDefOfPhysReg(ARM::CPSR))
2248 // When predicated, CPSR is an additional source operand for CPSR updating
2249 // instructions, this apparently increases their latencies.
2250 *PredCost = 1;
2251 if (UOps)
2252 return ItinData->getStageLatency(Class);
2253 return getNumMicroOps(ItinData, MI);
2254}
2255
2256int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2257 SDNode *Node) const {
2258 if (!Node->isMachineOpcode())
2259 return 1;
2260
2261 if (!ItinData || ItinData->isEmpty())
2262 return 1;
2263
2264 unsigned Opcode = Node->getMachineOpcode();
2265 switch (Opcode) {
2266 default:
2267 return ItinData->getStageLatency(get(Opcode).getSchedClass());
Bill Wendling73fe34a2010-11-16 01:16:36 +00002268 case ARM::VLDMQIA:
2269 case ARM::VLDMQDB:
2270 case ARM::VSTMQIA:
2271 case ARM::VSTMQDB:
Evan Cheng8239daf2010-11-03 00:45:17 +00002272 return 2;
Eric Christopher8b3ca622010-11-18 19:40:05 +00002273 }
Evan Cheng8239daf2010-11-03 00:45:17 +00002274}
2275
Evan Cheng23128422010-10-19 18:58:51 +00002276bool ARMBaseInstrInfo::
2277hasHighOperandLatency(const InstrItineraryData *ItinData,
2278 const MachineRegisterInfo *MRI,
2279 const MachineInstr *DefMI, unsigned DefIdx,
2280 const MachineInstr *UseMI, unsigned UseIdx) const {
2281 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2282 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2283 if (Subtarget.isCortexA8() &&
2284 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2285 // CortexA8 VFP instructions are not pipelined.
2286 return true;
2287
2288 // Hoist VFP / NEON instructions with 4 or higher latency.
2289 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2290 if (Latency <= 3)
2291 return false;
2292 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2293 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2294}
Evan Chengc8141df2010-10-26 02:08:50 +00002295
2296bool ARMBaseInstrInfo::
2297hasLowDefLatency(const InstrItineraryData *ItinData,
2298 const MachineInstr *DefMI, unsigned DefIdx) const {
2299 if (!ItinData || ItinData->isEmpty())
2300 return false;
2301
2302 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2303 if (DDomain == ARMII::DomainGeneral) {
2304 unsigned DefClass = DefMI->getDesc().getSchedClass();
2305 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2306 return (DefCycle != -1 && DefCycle <= 2);
2307 }
2308 return false;
2309}
Evan Cheng48575f62010-12-05 22:04:16 +00002310
2311bool
2312ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
2313 unsigned &AddSubOpc,
2314 bool &NegAcc, bool &HasLane) const {
2315 DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
2316 if (I == MLxEntryMap.end())
2317 return false;
2318
2319 const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
2320 MulOpc = Entry.MulOpc;
2321 AddSubOpc = Entry.AddSubOpc;
2322 NegAcc = Entry.NegAcc;
2323 HasLane = Entry.HasLane;
2324 return true;
2325}