blob: 26b4f0e54c125d66417711d7d3057236fab02d80 [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"
David Goodwin334c2642009-07-08 16:09:28 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000019#include "ARMRegisterInfo.h"
Chris Lattner4dbbe342010-07-20 21:17:29 +000020#include "ARMGenInstrInfo.inc"
Evan Chengfdc83402009-11-08 00:15:23 +000021#include "llvm/Constants.h"
22#include "llvm/Function.h"
23#include "llvm/GlobalValue.h"
David Goodwin334c2642009-07-08 16:09:28 +000024#include "llvm/ADT/STLExtras.h"
25#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"
David Goodwin334c2642009-07-08 16:09:28 +000037using namespace llvm;
38
39static cl::opt<bool>
40EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
41 cl::desc("Enable ARM 2-addr to 3-addr conv"));
42
Owen Andersonb3c04ec2010-09-30 23:48:38 +000043static cl::opt<bool>
Owen Anderson00d4f482010-10-01 20:33:47 +000044OldARMIfCvt("old-arm-ifcvt", cl::Hidden,
Owen Andersonb3c04ec2010-09-30 23:48:38 +000045 cl::desc("Use old-style ARM if-conversion heuristics"));
46
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000047ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
48 : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)),
49 Subtarget(STI) {
David Goodwin334c2642009-07-08 16:09:28 +000050}
51
52MachineInstr *
53ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
54 MachineBasicBlock::iterator &MBBI,
55 LiveVariables *LV) const {
Evan Cheng78703dd2009-07-27 18:44:00 +000056 // FIXME: Thumb2 support.
57
David Goodwin334c2642009-07-08 16:09:28 +000058 if (!EnableARM3Addr)
59 return NULL;
60
61 MachineInstr *MI = MBBI;
62 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +000063 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwin334c2642009-07-08 16:09:28 +000064 bool isPre = false;
65 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
66 default: return NULL;
67 case ARMII::IndexModePre:
68 isPre = true;
69 break;
70 case ARMII::IndexModePost:
71 break;
72 }
73
74 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
75 // operation.
76 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
77 if (MemOpc == 0)
78 return NULL;
79
80 MachineInstr *UpdateMI = NULL;
81 MachineInstr *MemMI = NULL;
82 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
83 const TargetInstrDesc &TID = MI->getDesc();
84 unsigned NumOps = TID.getNumOperands();
85 bool isLoad = !TID.mayStore();
86 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
87 const MachineOperand &Base = MI->getOperand(2);
88 const MachineOperand &Offset = MI->getOperand(NumOps-3);
89 unsigned WBReg = WB.getReg();
90 unsigned BaseReg = Base.getReg();
91 unsigned OffReg = Offset.getReg();
92 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
93 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
94 switch (AddrMode) {
95 default:
96 assert(false && "Unknown indexed op!");
97 return NULL;
98 case ARMII::AddrMode2: {
99 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
100 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
101 if (OffReg == 0) {
Evan Chenge7cbe412009-07-08 21:03:57 +0000102 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwin334c2642009-07-08 16:09:28 +0000103 // Can't encode it in a so_imm operand. This transformation will
104 // add more than 1 instruction. Abandon!
105 return NULL;
106 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000107 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge7cbe412009-07-08 21:03:57 +0000108 .addReg(BaseReg).addImm(Amt)
David Goodwin334c2642009-07-08 16:09:28 +0000109 .addImm(Pred).addReg(0).addReg(0);
110 } else if (Amt != 0) {
111 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
112 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
113 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000114 get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000115 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
116 .addImm(Pred).addReg(0).addReg(0);
117 } else
118 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000119 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000120 .addReg(BaseReg).addReg(OffReg)
121 .addImm(Pred).addReg(0).addReg(0);
122 break;
123 }
124 case ARMII::AddrMode3 : {
125 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
126 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
127 if (OffReg == 0)
128 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
129 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000130 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000131 .addReg(BaseReg).addImm(Amt)
132 .addImm(Pred).addReg(0).addReg(0);
133 else
134 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000135 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000136 .addReg(BaseReg).addReg(OffReg)
137 .addImm(Pred).addReg(0).addReg(0);
138 break;
139 }
140 }
141
142 std::vector<MachineInstr*> NewMIs;
143 if (isPre) {
144 if (isLoad)
145 MemMI = BuildMI(MF, MI->getDebugLoc(),
146 get(MemOpc), MI->getOperand(0).getReg())
147 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
148 else
149 MemMI = BuildMI(MF, MI->getDebugLoc(),
150 get(MemOpc)).addReg(MI->getOperand(1).getReg())
151 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
152 NewMIs.push_back(MemMI);
153 NewMIs.push_back(UpdateMI);
154 } else {
155 if (isLoad)
156 MemMI = BuildMI(MF, MI->getDebugLoc(),
157 get(MemOpc), MI->getOperand(0).getReg())
158 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
159 else
160 MemMI = BuildMI(MF, MI->getDebugLoc(),
161 get(MemOpc)).addReg(MI->getOperand(1).getReg())
162 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
163 if (WB.isDead())
164 UpdateMI->getOperand(0).setIsDead();
165 NewMIs.push_back(UpdateMI);
166 NewMIs.push_back(MemMI);
167 }
168
169 // Transfer LiveVariables states, kill / dead info.
170 if (LV) {
171 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
172 MachineOperand &MO = MI->getOperand(i);
173 if (MO.isReg() && MO.getReg() &&
174 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
175 unsigned Reg = MO.getReg();
176
177 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
178 if (MO.isDef()) {
179 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
180 if (MO.isDead())
181 LV->addVirtualRegisterDead(Reg, NewMI);
182 }
183 if (MO.isUse() && MO.isKill()) {
184 for (unsigned j = 0; j < 2; ++j) {
185 // Look at the two new MI's in reverse order.
186 MachineInstr *NewMI = NewMIs[j];
187 if (!NewMI->readsRegister(Reg))
188 continue;
189 LV->addVirtualRegisterKilled(Reg, NewMI);
190 if (VI.removeKill(MI))
191 VI.Kills.push_back(NewMI);
192 break;
193 }
194 }
195 }
196 }
197 }
198
199 MFI->insert(MBBI, NewMIs[1]);
200 MFI->insert(MBBI, NewMIs[0]);
201 return NewMIs[0];
202}
203
Evan Cheng2457f2c2010-05-22 01:47:14 +0000204bool
205ARMBaseInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Jim Grosbach18f30e62010-06-02 21:53:11 +0000206 MachineBasicBlock::iterator MI,
207 const std::vector<CalleeSavedInfo> &CSI,
208 const TargetRegisterInfo *TRI) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +0000209 if (CSI.empty())
210 return false;
211
212 DebugLoc DL;
213 if (MI != MBB.end()) DL = MI->getDebugLoc();
214
215 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
216 unsigned Reg = CSI[i].getReg();
217 bool isKill = true;
218
219 // Add the callee-saved register as live-in unless it's LR and
220 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
221 // then it's already added to the function and entry block live-in sets.
222 if (Reg == ARM::LR) {
223 MachineFunction &MF = *MBB.getParent();
224 if (MF.getFrameInfo()->isReturnAddressTaken() &&
225 MF.getRegInfo().isLiveIn(Reg))
226 isKill = false;
227 }
228
229 if (isKill)
230 MBB.addLiveIn(Reg);
231
232 // Insert the spill to the stack frame. The register is killed at the spill
Michael J. Spencer2bbb7692010-10-05 06:00:33 +0000233 //
Rafael Espindola42d075c2010-06-02 20:02:30 +0000234 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000235 storeRegToStackSlot(MBB, MI, Reg, isKill,
Rafael Espindola42d075c2010-06-02 20:02:30 +0000236 CSI[i].getFrameIdx(), RC, TRI);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000237 }
238 return true;
239}
240
David Goodwin334c2642009-07-08 16:09:28 +0000241// Branch analysis.
242bool
243ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
244 MachineBasicBlock *&FBB,
245 SmallVectorImpl<MachineOperand> &Cond,
246 bool AllowModify) const {
247 // If the block has no terminators, it just falls into the block after it.
248 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000249 if (I == MBB.begin())
250 return false;
251 --I;
252 while (I->isDebugValue()) {
253 if (I == MBB.begin())
254 return false;
255 --I;
256 }
257 if (!isUnpredicatedTerminator(I))
David Goodwin334c2642009-07-08 16:09:28 +0000258 return false;
259
260 // Get the last instruction in the block.
261 MachineInstr *LastInst = I;
262
263 // If there is only one terminator instruction, process it.
264 unsigned LastOpc = LastInst->getOpcode();
265 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Evan Cheng5ca53a72009-07-27 18:20:05 +0000266 if (isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000267 TBB = LastInst->getOperand(0).getMBB();
268 return false;
269 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000270 if (isCondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000271 // Block ends with fall-through condbranch.
272 TBB = LastInst->getOperand(0).getMBB();
273 Cond.push_back(LastInst->getOperand(1));
274 Cond.push_back(LastInst->getOperand(2));
275 return false;
276 }
277 return true; // Can't handle indirect branch.
278 }
279
280 // Get the instruction before it if it is a terminator.
281 MachineInstr *SecondLastInst = I;
Evan Cheng108c8722010-09-23 06:54:40 +0000282 unsigned SecondLastOpc = SecondLastInst->getOpcode();
283
284 // If AllowModify is true and the block ends with two or more unconditional
285 // branches, delete all but the first unconditional branch.
286 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
287 while (isUncondBranchOpcode(SecondLastOpc)) {
288 LastInst->eraseFromParent();
289 LastInst = SecondLastInst;
290 LastOpc = LastInst->getOpcode();
Evan Cheng676e2582010-09-23 19:42:03 +0000291 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
292 // Return now the only terminator is an unconditional branch.
293 TBB = LastInst->getOperand(0).getMBB();
294 return false;
295 } else {
Evan Cheng108c8722010-09-23 06:54:40 +0000296 SecondLastInst = I;
297 SecondLastOpc = SecondLastInst->getOpcode();
298 }
299 }
300 }
David Goodwin334c2642009-07-08 16:09:28 +0000301
302 // If there are three terminators, we don't know what sort of block this is.
303 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
304 return true;
305
Evan Cheng5ca53a72009-07-27 18:20:05 +0000306 // If the block ends with a B and a Bcc, handle it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000307 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000308 TBB = SecondLastInst->getOperand(0).getMBB();
309 Cond.push_back(SecondLastInst->getOperand(1));
310 Cond.push_back(SecondLastInst->getOperand(2));
311 FBB = LastInst->getOperand(0).getMBB();
312 return false;
313 }
314
315 // If the block ends with two unconditional branches, handle it. The second
316 // one is not executed, so remove it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000317 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000318 TBB = SecondLastInst->getOperand(0).getMBB();
319 I = LastInst;
320 if (AllowModify)
321 I->eraseFromParent();
322 return false;
323 }
324
325 // ...likewise if it ends with a branch table followed by an unconditional
326 // branch. The branch folder can create these, and we must get rid of them for
327 // correctness of Thumb constant islands.
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000328 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
329 isIndirectBranchOpcode(SecondLastOpc)) &&
Evan Cheng5ca53a72009-07-27 18:20:05 +0000330 isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000331 I = LastInst;
332 if (AllowModify)
333 I->eraseFromParent();
334 return true;
335 }
336
337 // Otherwise, can't handle this.
338 return true;
339}
340
341
342unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwin334c2642009-07-08 16:09:28 +0000343 MachineBasicBlock::iterator I = MBB.end();
344 if (I == MBB.begin()) return 0;
345 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000346 while (I->isDebugValue()) {
347 if (I == MBB.begin())
348 return 0;
349 --I;
350 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000351 if (!isUncondBranchOpcode(I->getOpcode()) &&
352 !isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000353 return 0;
354
355 // Remove the branch.
356 I->eraseFromParent();
357
358 I = MBB.end();
359
360 if (I == MBB.begin()) return 1;
361 --I;
Evan Cheng5ca53a72009-07-27 18:20:05 +0000362 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000363 return 1;
364
365 // Remove the branch.
366 I->eraseFromParent();
367 return 2;
368}
369
370unsigned
371ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000372 MachineBasicBlock *FBB,
373 const SmallVectorImpl<MachineOperand> &Cond,
374 DebugLoc DL) const {
Evan Cheng6495f632009-07-28 05:48:47 +0000375 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
376 int BOpc = !AFI->isThumbFunction()
377 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
378 int BccOpc = !AFI->isThumbFunction()
379 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
David Goodwin334c2642009-07-08 16:09:28 +0000380
381 // Shouldn't be a fall through.
382 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
383 assert((Cond.size() == 2 || Cond.size() == 0) &&
384 "ARM branch conditions have two components!");
385
386 if (FBB == 0) {
387 if (Cond.empty()) // Unconditional branch?
Stuart Hastings3bf91252010-06-17 22:43:56 +0000388 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
David Goodwin334c2642009-07-08 16:09:28 +0000389 else
Stuart Hastings3bf91252010-06-17 22:43:56 +0000390 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000391 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
392 return 1;
393 }
394
395 // Two-way conditional branch.
Stuart Hastings3bf91252010-06-17 22:43:56 +0000396 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000397 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
Stuart Hastings3bf91252010-06-17 22:43:56 +0000398 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwin334c2642009-07-08 16:09:28 +0000399 return 2;
400}
401
402bool ARMBaseInstrInfo::
403ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
404 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
405 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
406 return false;
407}
408
David Goodwin334c2642009-07-08 16:09:28 +0000409bool ARMBaseInstrInfo::
410PredicateInstruction(MachineInstr *MI,
411 const SmallVectorImpl<MachineOperand> &Pred) const {
412 unsigned Opc = MI->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000413 if (isUncondBranchOpcode(Opc)) {
414 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
David Goodwin334c2642009-07-08 16:09:28 +0000415 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
416 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
417 return true;
418 }
419
420 int PIdx = MI->findFirstPredOperandIdx();
421 if (PIdx != -1) {
422 MachineOperand &PMO = MI->getOperand(PIdx);
423 PMO.setImm(Pred[0].getImm());
424 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
425 return true;
426 }
427 return false;
428}
429
430bool ARMBaseInstrInfo::
431SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
432 const SmallVectorImpl<MachineOperand> &Pred2) const {
433 if (Pred1.size() > 2 || Pred2.size() > 2)
434 return false;
435
436 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
437 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
438 if (CC1 == CC2)
439 return true;
440
441 switch (CC1) {
442 default:
443 return false;
444 case ARMCC::AL:
445 return true;
446 case ARMCC::HS:
447 return CC2 == ARMCC::HI;
448 case ARMCC::LS:
449 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
450 case ARMCC::GE:
451 return CC2 == ARMCC::GT;
452 case ARMCC::LE:
453 return CC2 == ARMCC::LT;
454 }
455}
456
457bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
458 std::vector<MachineOperand> &Pred) const {
Evan Cheng8fb90362009-08-08 03:20:32 +0000459 // FIXME: This confuses implicit_def with optional CPSR def.
David Goodwin334c2642009-07-08 16:09:28 +0000460 const TargetInstrDesc &TID = MI->getDesc();
461 if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
462 return false;
463
464 bool Found = false;
465 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
466 const MachineOperand &MO = MI->getOperand(i);
467 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
468 Pred.push_back(MO);
469 Found = true;
470 }
471 }
472
473 return Found;
474}
475
Evan Chengac0869d2009-11-21 06:21:52 +0000476/// isPredicable - Return true if the specified instruction can be predicated.
477/// By default, this returns true for every instruction with a
478/// PredicateOperand.
479bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
480 const TargetInstrDesc &TID = MI->getDesc();
481 if (!TID.isPredicable())
482 return false;
483
484 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
485 ARMFunctionInfo *AFI =
486 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
Evan Chengd7f08102009-11-24 08:06:15 +0000487 return AFI->isThumb2Function();
Evan Chengac0869d2009-11-21 06:21:52 +0000488 }
489 return true;
490}
David Goodwin334c2642009-07-08 16:09:28 +0000491
Chris Lattner56856b12009-12-03 06:58:32 +0000492/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
493DISABLE_INLINE
David Goodwin334c2642009-07-08 16:09:28 +0000494static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattner56856b12009-12-03 06:58:32 +0000495 unsigned JTI);
David Goodwin334c2642009-07-08 16:09:28 +0000496static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
497 unsigned JTI) {
Chris Lattner56856b12009-12-03 06:58:32 +0000498 assert(JTI < JT.size());
David Goodwin334c2642009-07-08 16:09:28 +0000499 return JT[JTI].MBBs.size();
500}
501
502/// GetInstSize - Return the size of the specified MachineInstr.
503///
504unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
505 const MachineBasicBlock &MBB = *MI->getParent();
506 const MachineFunction *MF = MBB.getParent();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000507 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwin334c2642009-07-08 16:09:28 +0000508
509 // Basic size info comes from the TSFlags field.
510 const TargetInstrDesc &TID = MI->getDesc();
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000511 uint64_t TSFlags = TID.TSFlags;
David Goodwin334c2642009-07-08 16:09:28 +0000512
Evan Chenga0ee8622009-07-31 22:22:22 +0000513 unsigned Opc = MI->getOpcode();
David Goodwin334c2642009-07-08 16:09:28 +0000514 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
515 default: {
516 // If this machine instr is an inline asm, measure it.
517 if (MI->getOpcode() == ARM::INLINEASM)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000518 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Goodwin334c2642009-07-08 16:09:28 +0000519 if (MI->isLabel())
520 return 0;
Evan Chenga0ee8622009-07-31 22:22:22 +0000521 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000522 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000523 llvm_unreachable("Unknown or unset size field for instr!");
Chris Lattner518bb532010-02-09 19:54:29 +0000524 case TargetOpcode::IMPLICIT_DEF:
525 case TargetOpcode::KILL:
Bill Wendling7431bea2010-07-16 22:20:36 +0000526 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000527 case TargetOpcode::EH_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +0000528 case TargetOpcode::DBG_VALUE:
David Goodwin334c2642009-07-08 16:09:28 +0000529 return 0;
530 }
531 break;
532 }
Evan Cheng78947622009-07-24 18:20:44 +0000533 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
534 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
535 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
David Goodwin334c2642009-07-08 16:09:28 +0000536 case ARMII::SizeSpecial: {
Evan Chenga0ee8622009-07-31 22:22:22 +0000537 switch (Opc) {
Jim Grosbach3c38f962010-10-06 22:01:26 +0000538 case ARM::MOVi32imm:
539 case ARM::t2MOVi32imm:
540 return 8;
David Goodwin334c2642009-07-08 16:09:28 +0000541 case ARM::CONSTPOOL_ENTRY:
542 // If this machine instr is a constant pool entry, its size is recorded as
543 // operand #2.
544 return MI->getOperand(2).getImm();
Jim Grosbach5eb19512010-05-22 01:06:18 +0000545 case ARM::Int_eh_sjlj_longjmp:
546 return 16;
547 case ARM::tInt_eh_sjlj_longjmp:
548 return 10;
Evan Cheng78947622009-07-24 18:20:44 +0000549 case ARM::Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000550 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000551 return 20;
Jim Grosbachd1228742009-12-01 18:10:36 +0000552 case ARM::tInt_eh_sjlj_setjmp:
Jim Grosbach5aa16842009-08-11 19:42:21 +0000553 case ARM::t2Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000554 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000555 return 12;
David Goodwin334c2642009-07-08 16:09:28 +0000556 case ARM::BR_JTr:
557 case ARM::BR_JTm:
558 case ARM::BR_JTadd:
Evan Chenga0ee8622009-07-31 22:22:22 +0000559 case ARM::tBR_JTr:
Evan Chengd26b14c2009-07-31 18:28:05 +0000560 case ARM::t2BR_JT:
561 case ARM::t2TBB:
562 case ARM::t2TBH: {
David Goodwin334c2642009-07-08 16:09:28 +0000563 // These are jumptable branches, i.e. a branch followed by an inlined
Evan Chengd26b14c2009-07-31 18:28:05 +0000564 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
565 // entry is one byte; TBH two byte each.
Evan Chenga0ee8622009-07-31 22:22:22 +0000566 unsigned EntrySize = (Opc == ARM::t2TBB)
567 ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4);
David Goodwin334c2642009-07-08 16:09:28 +0000568 unsigned NumOps = TID.getNumOperands();
569 MachineOperand JTOP =
570 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
571 unsigned JTI = JTOP.getIndex();
572 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000573 assert(MJTI != 0);
David Goodwin334c2642009-07-08 16:09:28 +0000574 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
575 assert(JTI < JT.size());
576 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
577 // 4 aligned. The assembler / linker may add 2 byte padding just before
578 // the JT entries. The size does not include this padding; the
579 // constant islands pass does separate bookkeeping for it.
580 // FIXME: If we know the size of the function is less than (1 << 16) *2
581 // bytes, we can use 16-bit entries instead. Then there won't be an
582 // alignment issue.
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000583 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
584 unsigned NumEntries = getNumJTEntries(JT, JTI);
585 if (Opc == ARM::t2TBB && (NumEntries & 1))
586 // Make sure the instruction that follows TBB is 2-byte aligned.
587 // FIXME: Constant island pass should insert an "ALIGN" instruction
588 // instead.
589 ++NumEntries;
590 return NumEntries * EntrySize + InstSize;
David Goodwin334c2642009-07-08 16:09:28 +0000591 }
592 default:
593 // Otherwise, pseudo-instruction sizes are zero.
594 return 0;
595 }
596 }
597 }
598 return 0; // Not reached
599}
600
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000601void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
602 MachineBasicBlock::iterator I, DebugLoc DL,
603 unsigned DestReg, unsigned SrcReg,
604 bool KillSrc) const {
605 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
606 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson1665b0a2010-02-16 17:24:15 +0000607
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000608 if (GPRDest && GPRSrc) {
609 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
610 .addReg(SrcReg, getKillRegState(KillSrc))));
611 return;
David Goodwin7bfdca02009-08-05 21:02:22 +0000612 }
David Goodwin334c2642009-07-08 16:09:28 +0000613
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000614 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
615 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
616
617 unsigned Opc;
618 if (SPRDest && SPRSrc)
619 Opc = ARM::VMOVS;
620 else if (GPRDest && SPRSrc)
621 Opc = ARM::VMOVRS;
622 else if (SPRDest && GPRSrc)
623 Opc = ARM::VMOVSR;
624 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
625 Opc = ARM::VMOVD;
626 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
627 Opc = ARM::VMOVQ;
628 else if (ARM::QQPRRegClass.contains(DestReg, SrcReg))
629 Opc = ARM::VMOVQQ;
630 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg))
631 Opc = ARM::VMOVQQQQ;
632 else
633 llvm_unreachable("Impossible reg-to-reg copy");
634
635 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
636 MIB.addReg(SrcReg, getKillRegState(KillSrc));
637 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ)
638 AddDefaultPred(MIB);
David Goodwin334c2642009-07-08 16:09:28 +0000639}
640
Evan Chengc10b5af2010-05-07 00:24:52 +0000641static const
642MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
643 unsigned Reg, unsigned SubIdx, unsigned State,
644 const TargetRegisterInfo *TRI) {
645 if (!SubIdx)
646 return MIB.addReg(Reg, State);
647
648 if (TargetRegisterInfo::isPhysicalRegister(Reg))
649 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
650 return MIB.addReg(Reg, State, SubIdx);
651}
652
David Goodwin334c2642009-07-08 16:09:28 +0000653void ARMBaseInstrInfo::
654storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
655 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000656 const TargetRegisterClass *RC,
657 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000658 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000659 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000660 MachineFunction &MF = *MBB.getParent();
661 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000662 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000663
664 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000665 MF.getMachineMemOperand(MachinePointerInfo(
666 PseudoSourceValue::getFixedStack(FI)),
667 MachineMemOperand::MOStore,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000668 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000669 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000670
Bob Wilson0eb0c742010-02-16 22:01:59 +0000671 // tGPR is used sometimes in ARM instructions that need to avoid using
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000672 // certain registers. Just treat it as GPR here. Likewise, rGPR.
673 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
674 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000675 RC = ARM::GPRRegisterClass;
676
Bob Wilsonebe99b22010-06-18 21:32:42 +0000677 switch (RC->getID()) {
678 case ARM::GPRRegClassID:
Evan Cheng5732ca02009-07-27 03:14:20 +0000679 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STR))
David Goodwin334c2642009-07-08 16:09:28 +0000680 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000681 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000682 break;
683 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000684 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
685 .addReg(SrcReg, getKillRegState(isKill))
686 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000687 break;
688 case ARM::DPRRegClassID:
689 case ARM::DPR_VFP2RegClassID:
690 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000691 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000692 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000693 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000694 break;
695 case ARM::QPRRegClassID:
696 case ARM::QPR_VFP2RegClassID:
697 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000698 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000699 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo))
Bob Wilsonf967ca02010-07-06 21:26:18 +0000700 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000701 .addReg(SrcReg, getKillRegState(isKill))
702 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000703 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000704 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQ))
705 .addReg(SrcReg, getKillRegState(isKill))
706 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000707 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
Evan Cheng69b9f982010-05-13 01:12:06 +0000708 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000709 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000710 break;
711 case ARM::QQPRRegClassID:
712 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000713 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng22c687b2010-05-14 02:13:41 +0000714 // FIXME: It's possible to only store part of the QQ register if the
715 // spilled def has a sub-register index.
Bob Wilson168f3822010-09-15 01:48:05 +0000716 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
717 .addFrameIndex(FI).addImm(16)
718 .addReg(SrcReg, getKillRegState(isKill))
719 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000720 } else {
721 MachineInstrBuilder MIB =
722 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
723 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000724 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Evan Cheng435d4992010-05-07 02:04:02 +0000725 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000726 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
727 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
728 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
729 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000730 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000731 break;
732 case ARM::QQQQPRRegClassID: {
Evan Cheng22c687b2010-05-14 02:13:41 +0000733 MachineInstrBuilder MIB =
734 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
735 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000736 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Evan Cheng22c687b2010-05-14 02:13:41 +0000737 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000738 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
739 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
740 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
741 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
742 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
743 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
744 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
745 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
Bob Wilsonebe99b22010-06-18 21:32:42 +0000746 break;
747 }
748 default:
749 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000750 }
751}
752
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000753unsigned
754ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
755 int &FrameIndex) const {
756 switch (MI->getOpcode()) {
757 default: break;
758 case ARM::STR:
759 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
760 if (MI->getOperand(1).isFI() &&
761 MI->getOperand(2).isReg() &&
762 MI->getOperand(3).isImm() &&
763 MI->getOperand(2).getReg() == 0 &&
764 MI->getOperand(3).getImm() == 0) {
765 FrameIndex = MI->getOperand(1).getIndex();
766 return MI->getOperand(0).getReg();
767 }
768 break;
769 case ARM::t2STRi12:
770 case ARM::tSpill:
771 case ARM::VSTRD:
772 case ARM::VSTRS:
773 if (MI->getOperand(1).isFI() &&
774 MI->getOperand(2).isImm() &&
775 MI->getOperand(2).getImm() == 0) {
776 FrameIndex = MI->getOperand(1).getIndex();
777 return MI->getOperand(0).getReg();
778 }
779 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000780 case ARM::VST1q64Pseudo:
781 if (MI->getOperand(0).isFI() &&
782 MI->getOperand(2).getSubReg() == 0) {
783 FrameIndex = MI->getOperand(0).getIndex();
784 return MI->getOperand(2).getReg();
785 }
Jakob Stoklund Olesen31bbc512010-09-15 21:40:09 +0000786 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000787 case ARM::VSTMQ:
788 if (MI->getOperand(1).isFI() &&
789 MI->getOperand(2).isImm() &&
790 MI->getOperand(2).getImm() == ARM_AM::getAM4ModeImm(ARM_AM::ia) &&
791 MI->getOperand(0).getSubReg() == 0) {
792 FrameIndex = MI->getOperand(1).getIndex();
793 return MI->getOperand(0).getReg();
794 }
795 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000796 }
797
798 return 0;
799}
800
David Goodwin334c2642009-07-08 16:09:28 +0000801void ARMBaseInstrInfo::
802loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
803 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000804 const TargetRegisterClass *RC,
805 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000806 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000807 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000808 MachineFunction &MF = *MBB.getParent();
809 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000810 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000811 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000812 MF.getMachineMemOperand(
813 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
814 MachineMemOperand::MOLoad,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000815 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000816 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000817
Bob Wilson0eb0c742010-02-16 22:01:59 +0000818 // tGPR is used sometimes in ARM instructions that need to avoid using
819 // certain registers. Just treat it as GPR here.
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000820 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
821 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000822 RC = ARM::GPRRegisterClass;
823
Bob Wilsonebe99b22010-06-18 21:32:42 +0000824 switch (RC->getID()) {
825 case ARM::GPRRegClassID:
Evan Cheng5732ca02009-07-27 03:14:20 +0000826 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDR), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000827 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000828 break;
829 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000830 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
831 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000832 break;
833 case ARM::DPRRegClassID:
834 case ARM::DPR_VFP2RegClassID:
835 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000836 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000837 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000838 break;
839 case ARM::QPRRegClassID:
840 case ARM::QPR_VFP2RegClassID:
841 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000842 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000843 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg)
Bob Wilsonf967ca02010-07-06 21:26:18 +0000844 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000845 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000846 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000847 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQ), DestReg)
848 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000849 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
Evan Cheng69b9f982010-05-13 01:12:06 +0000850 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000851 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000852 break;
853 case ARM::QQPRRegClassID:
854 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000855 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000856 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
857 .addFrameIndex(FI).addImm(16)
858 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000859 } else {
860 MachineInstrBuilder MIB =
861 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
862 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000863 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Evan Cheng435d4992010-05-07 02:04:02 +0000864 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000865 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
866 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
867 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
868 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000869 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000870 break;
871 case ARM::QQQQPRRegClassID: {
872 MachineInstrBuilder MIB =
873 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
874 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000875 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Bob Wilsonebe99b22010-06-18 21:32:42 +0000876 .addMemOperand(MMO);
877 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
878 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
879 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
880 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
881 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
882 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
883 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
884 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
885 break;
886 }
887 default:
888 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000889 }
890}
891
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000892unsigned
893ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
894 int &FrameIndex) const {
895 switch (MI->getOpcode()) {
896 default: break;
897 case ARM::LDR:
898 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
899 if (MI->getOperand(1).isFI() &&
900 MI->getOperand(2).isReg() &&
901 MI->getOperand(3).isImm() &&
902 MI->getOperand(2).getReg() == 0 &&
903 MI->getOperand(3).getImm() == 0) {
904 FrameIndex = MI->getOperand(1).getIndex();
905 return MI->getOperand(0).getReg();
906 }
907 break;
908 case ARM::t2LDRi12:
909 case ARM::tRestore:
910 case ARM::VLDRD:
911 case ARM::VLDRS:
912 if (MI->getOperand(1).isFI() &&
913 MI->getOperand(2).isImm() &&
914 MI->getOperand(2).getImm() == 0) {
915 FrameIndex = MI->getOperand(1).getIndex();
916 return MI->getOperand(0).getReg();
917 }
918 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000919 case ARM::VLD1q64Pseudo:
920 if (MI->getOperand(1).isFI() &&
921 MI->getOperand(0).getSubReg() == 0) {
922 FrameIndex = MI->getOperand(1).getIndex();
923 return MI->getOperand(0).getReg();
924 }
925 break;
Jakob Stoklund Olesen06f264e2010-09-15 21:40:11 +0000926 case ARM::VLDMQ:
927 if (MI->getOperand(1).isFI() &&
928 MI->getOperand(2).isImm() &&
929 MI->getOperand(2).getImm() == ARM_AM::getAM4ModeImm(ARM_AM::ia) &&
930 MI->getOperand(0).getSubReg() == 0) {
931 FrameIndex = MI->getOperand(1).getIndex();
932 return MI->getOperand(0).getReg();
933 }
934 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000935 }
936
937 return 0;
938}
939
Evan Cheng62b50652010-04-26 07:39:25 +0000940MachineInstr*
941ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000942 int FrameIx, uint64_t Offset,
Evan Cheng62b50652010-04-26 07:39:25 +0000943 const MDNode *MDPtr,
944 DebugLoc DL) const {
945 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
946 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
947 return &*MIB;
948}
949
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000950/// Create a copy of a const pool value. Update CPI to the new index and return
951/// the label UID.
952static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
953 MachineConstantPool *MCP = MF.getConstantPool();
954 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
955
956 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
957 assert(MCPE.isMachineConstantPoolEntry() &&
958 "Expecting a machine constantpool entry!");
959 ARMConstantPoolValue *ACPV =
960 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
961
962 unsigned PCLabelId = AFI->createConstPoolEntryUId();
963 ARMConstantPoolValue *NewCPV = 0;
Jim Grosbach51f5b672010-09-10 21:38:22 +0000964 // FIXME: The below assumes PIC relocation model and that the function
965 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
966 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
967 // instructions, so that's probably OK, but is PIC always correct when
968 // we get here?
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000969 if (ACPV->isGlobalValue())
970 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
971 ARMCP::CPValue, 4);
972 else if (ACPV->isExtSymbol())
973 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
974 ACPV->getSymbol(), PCLabelId, 4);
975 else if (ACPV->isBlockAddress())
976 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
977 ARMCP::CPBlockAddress, 4);
Jim Grosbach51f5b672010-09-10 21:38:22 +0000978 else if (ACPV->isLSDA())
979 NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId,
980 ARMCP::CPLSDA, 4);
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000981 else
982 llvm_unreachable("Unexpected ARM constantpool value type!!");
983 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
984 return PCLabelId;
985}
986
Evan Chengfdc83402009-11-08 00:15:23 +0000987void ARMBaseInstrInfo::
988reMaterialize(MachineBasicBlock &MBB,
989 MachineBasicBlock::iterator I,
990 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +0000991 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000992 const TargetRegisterInfo &TRI) const {
Evan Chengfdc83402009-11-08 00:15:23 +0000993 unsigned Opcode = Orig->getOpcode();
994 switch (Opcode) {
995 default: {
996 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000997 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfdc83402009-11-08 00:15:23 +0000998 MBB.insert(I, MI);
999 break;
1000 }
1001 case ARM::tLDRpci_pic:
1002 case ARM::t2LDRpci_pic: {
1003 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +00001004 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001005 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +00001006 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1007 DestReg)
1008 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1009 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1010 break;
1011 }
1012 }
Evan Chengfdc83402009-11-08 00:15:23 +00001013}
1014
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001015MachineInstr *
1016ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1017 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1018 switch(Orig->getOpcode()) {
1019 case ARM::tLDRpci_pic:
1020 case ARM::t2LDRpci_pic: {
1021 unsigned CPI = Orig->getOperand(1).getIndex();
1022 unsigned PCLabelId = duplicateCPV(MF, CPI);
1023 Orig->getOperand(1).setIndex(CPI);
1024 Orig->getOperand(2).setImm(PCLabelId);
1025 break;
1026 }
1027 }
1028 return MI;
1029}
1030
Evan Cheng506049f2010-03-03 01:44:33 +00001031bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1032 const MachineInstr *MI1) const {
Evan Chengd457e6e2009-11-07 04:04:34 +00001033 int Opcode = MI0->getOpcode();
Evan Cheng9b824252009-11-20 02:10:27 +00001034 if (Opcode == ARM::t2LDRpci ||
1035 Opcode == ARM::t2LDRpci_pic ||
1036 Opcode == ARM::tLDRpci ||
1037 Opcode == ARM::tLDRpci_pic) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001038 if (MI1->getOpcode() != Opcode)
1039 return false;
1040 if (MI0->getNumOperands() != MI1->getNumOperands())
1041 return false;
1042
1043 const MachineOperand &MO0 = MI0->getOperand(1);
1044 const MachineOperand &MO1 = MI1->getOperand(1);
1045 if (MO0.getOffset() != MO1.getOffset())
1046 return false;
1047
1048 const MachineFunction *MF = MI0->getParent()->getParent();
1049 const MachineConstantPool *MCP = MF->getConstantPool();
1050 int CPI0 = MO0.getIndex();
1051 int CPI1 = MO1.getIndex();
1052 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1053 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1054 ARMConstantPoolValue *ACPV0 =
1055 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1056 ARMConstantPoolValue *ACPV1 =
1057 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1058 return ACPV0->hasSameValue(ACPV1);
1059 }
1060
Evan Cheng506049f2010-03-03 01:44:33 +00001061 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001062}
1063
Bill Wendling4b722102010-06-23 23:00:16 +00001064/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1065/// determine if two loads are loading from the same base address. It should
1066/// only return true if the base pointers are the same and the only differences
1067/// between the two addresses is the offset. It also returns the offsets by
1068/// reference.
1069bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1070 int64_t &Offset1,
1071 int64_t &Offset2) const {
1072 // Don't worry about Thumb: just ARM and Thumb2.
1073 if (Subtarget.isThumb1Only()) return false;
1074
1075 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1076 return false;
1077
1078 switch (Load1->getMachineOpcode()) {
1079 default:
1080 return false;
1081 case ARM::LDR:
1082 case ARM::LDRB:
1083 case ARM::LDRD:
1084 case ARM::LDRH:
1085 case ARM::LDRSB:
1086 case ARM::LDRSH:
1087 case ARM::VLDRD:
1088 case ARM::VLDRS:
1089 case ARM::t2LDRi8:
1090 case ARM::t2LDRDi8:
1091 case ARM::t2LDRSHi8:
1092 case ARM::t2LDRi12:
1093 case ARM::t2LDRSHi12:
1094 break;
1095 }
1096
1097 switch (Load2->getMachineOpcode()) {
1098 default:
1099 return false;
1100 case ARM::LDR:
1101 case ARM::LDRB:
1102 case ARM::LDRD:
1103 case ARM::LDRH:
1104 case ARM::LDRSB:
1105 case ARM::LDRSH:
1106 case ARM::VLDRD:
1107 case ARM::VLDRS:
1108 case ARM::t2LDRi8:
1109 case ARM::t2LDRDi8:
1110 case ARM::t2LDRSHi8:
1111 case ARM::t2LDRi12:
1112 case ARM::t2LDRSHi12:
1113 break;
1114 }
1115
1116 // Check if base addresses and chain operands match.
1117 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1118 Load1->getOperand(4) != Load2->getOperand(4))
1119 return false;
1120
1121 // Index should be Reg0.
1122 if (Load1->getOperand(3) != Load2->getOperand(3))
1123 return false;
1124
1125 // Determine the offsets.
1126 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1127 isa<ConstantSDNode>(Load2->getOperand(1))) {
1128 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1129 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1130 return true;
1131 }
1132
1133 return false;
1134}
1135
1136/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1137/// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1138/// be scheduled togther. On some targets if two loads are loading from
1139/// addresses in the same cache line, it's better if they are scheduled
1140/// together. This function takes two integers that represent the load offsets
1141/// from the common base address. It returns true if it decides it's desirable
1142/// to schedule the two loads together. "NumLoads" is the number of loads that
1143/// have already been scheduled after Load1.
1144bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1145 int64_t Offset1, int64_t Offset2,
1146 unsigned NumLoads) const {
1147 // Don't worry about Thumb: just ARM and Thumb2.
1148 if (Subtarget.isThumb1Only()) return false;
1149
1150 assert(Offset2 > Offset1);
1151
1152 if ((Offset2 - Offset1) / 8 > 64)
1153 return false;
1154
1155 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1156 return false; // FIXME: overly conservative?
1157
1158 // Four loads in a row should be sufficient.
1159 if (NumLoads >= 3)
1160 return false;
1161
1162 return true;
1163}
1164
Evan Cheng86050dc2010-06-18 23:09:54 +00001165bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1166 const MachineBasicBlock *MBB,
1167 const MachineFunction &MF) const {
Jim Grosbach57bb3942010-06-25 18:43:14 +00001168 // Debug info is never a scheduling boundary. It's necessary to be explicit
1169 // due to the special treatment of IT instructions below, otherwise a
1170 // dbg_value followed by an IT will result in the IT instruction being
1171 // considered a scheduling hazard, which is wrong. It should be the actual
1172 // instruction preceding the dbg_value instruction(s), just like it is
1173 // when debug info is not present.
1174 if (MI->isDebugValue())
1175 return false;
1176
Evan Cheng86050dc2010-06-18 23:09:54 +00001177 // Terminators and labels can't be scheduled around.
1178 if (MI->getDesc().isTerminator() || MI->isLabel())
1179 return true;
1180
1181 // Treat the start of the IT block as a scheduling boundary, but schedule
1182 // t2IT along with all instructions following it.
1183 // FIXME: This is a big hammer. But the alternative is to add all potential
1184 // true and anti dependencies to IT block instructions as implicit operands
1185 // to the t2IT instruction. The added compile time and complexity does not
1186 // seem worth it.
1187 MachineBasicBlock::const_iterator I = MI;
Jim Grosbach57bb3942010-06-25 18:43:14 +00001188 // Make sure to skip any dbg_value instructions
1189 while (++I != MBB->end() && I->isDebugValue())
1190 ;
1191 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng86050dc2010-06-18 23:09:54 +00001192 return true;
1193
1194 // Don't attempt to schedule around any instruction that defines
1195 // a stack-oriented pointer, as it's unlikely to be profitable. This
1196 // saves compile time, because it doesn't require every single
1197 // stack slot reference to depend on the instruction that does the
1198 // modification.
1199 if (MI->definesRegister(ARM::SP))
1200 return true;
1201
1202 return false;
1203}
1204
Owen Andersonb20b8512010-09-28 18:32:13 +00001205bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
1206 unsigned NumInstrs,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001207 float Probability,
1208 float Confidence) const {
Evan Cheng13151432010-06-25 22:42:03 +00001209 if (!NumInstrs)
1210 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001211
Owen Andersonb3c04ec2010-09-30 23:48:38 +00001212 // Use old-style heuristics
Owen Anderson00d4f482010-10-01 20:33:47 +00001213 if (OldARMIfCvt) {
Owen Andersonb3c04ec2010-09-30 23:48:38 +00001214 if (Subtarget.getCPUString() == "generic")
1215 // Generic (and overly aggressive) if-conversion limits for testing.
1216 return NumInstrs <= 10;
Owen Anderson00d4f482010-10-01 20:33:47 +00001217 if (Subtarget.hasV7Ops())
Owen Andersonb3c04ec2010-09-30 23:48:38 +00001218 return NumInstrs <= 3;
1219 return NumInstrs <= 2;
1220 }
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001221
Owen Andersonb20b8512010-09-28 18:32:13 +00001222 // Attempt to estimate the relative costs of predication versus branching.
1223 float UnpredCost = Probability * NumInstrs;
Owen Anderson654d5442010-09-28 21:57:50 +00001224 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001225 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001226
Owen Andersonb20b8512010-09-28 18:32:13 +00001227 float PredCost = NumInstrs;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001228
Owen Andersonb20b8512010-09-28 18:32:13 +00001229 return PredCost < UnpredCost;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001230
Evan Cheng13151432010-06-25 22:42:03 +00001231}
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001232
Evan Cheng13151432010-06-25 22:42:03 +00001233bool ARMBaseInstrInfo::
1234isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
Owen Andersonb20b8512010-09-28 18:32:13 +00001235 MachineBasicBlock &FMBB, unsigned NumF,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001236 float Probability, float Confidence) const {
Owen Andersonb3c04ec2010-09-30 23:48:38 +00001237 // Use old-style if-conversion heuristics
Owen Anderson00d4f482010-10-01 20:33:47 +00001238 if (OldARMIfCvt) {
Owen Andersonb3c04ec2010-09-30 23:48:38 +00001239 return NumT && NumF && NumT <= 2 && NumF <= 2;
1240 }
1241
Owen Andersonb20b8512010-09-28 18:32:13 +00001242 if (!NumT || !NumF)
1243 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001244
Owen Andersonb20b8512010-09-28 18:32:13 +00001245 // Attempt to estimate the relative costs of predication versus branching.
1246 float UnpredCost = Probability * NumT + (1.0 - Probability) * NumF;
Owen Anderson654d5442010-09-28 21:57:50 +00001247 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001248 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001249
Owen Andersonb20b8512010-09-28 18:32:13 +00001250 float PredCost = NumT + NumF;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001251
Owen Andersonb20b8512010-09-28 18:32:13 +00001252 return PredCost < UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001253}
1254
Evan Cheng8fb90362009-08-08 03:20:32 +00001255/// getInstrPredicate - If instruction is predicated, returns its predicate
1256/// condition, otherwise returns AL. It also returns the condition code
1257/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001258ARMCC::CondCodes
1259llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001260 int PIdx = MI->findFirstPredOperandIdx();
1261 if (PIdx == -1) {
1262 PredReg = 0;
1263 return ARMCC::AL;
1264 }
1265
1266 PredReg = MI->getOperand(PIdx+1).getReg();
1267 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1268}
1269
1270
Evan Cheng6495f632009-07-28 05:48:47 +00001271int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001272 if (Opc == ARM::B)
1273 return ARM::Bcc;
1274 else if (Opc == ARM::tB)
1275 return ARM::tBcc;
1276 else if (Opc == ARM::t2B)
1277 return ARM::t2Bcc;
1278
1279 llvm_unreachable("Unknown unconditional branch opcode!");
1280 return 0;
1281}
1282
Evan Cheng6495f632009-07-28 05:48:47 +00001283
1284void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1285 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1286 unsigned DestReg, unsigned BaseReg, int NumBytes,
1287 ARMCC::CondCodes Pred, unsigned PredReg,
1288 const ARMBaseInstrInfo &TII) {
1289 bool isSub = NumBytes < 0;
1290 if (isSub) NumBytes = -NumBytes;
1291
1292 while (NumBytes) {
1293 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1294 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1295 assert(ThisVal && "Didn't extract field correctly");
1296
1297 // We will handle these bits from offset, clear them.
1298 NumBytes &= ~ThisVal;
1299
1300 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1301
1302 // Build the new ADD / SUB.
1303 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1304 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1305 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1306 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1307 BaseReg = DestReg;
1308 }
1309}
1310
Evan Chengcdbb3f52009-08-27 01:23:50 +00001311bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1312 unsigned FrameReg, int &Offset,
1313 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001314 unsigned Opcode = MI.getOpcode();
1315 const TargetInstrDesc &Desc = MI.getDesc();
1316 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1317 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001318
Evan Cheng6495f632009-07-28 05:48:47 +00001319 // Memory operands in inline assembly always use AddrMode2.
1320 if (Opcode == ARM::INLINEASM)
1321 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001322
Evan Cheng6495f632009-07-28 05:48:47 +00001323 if (Opcode == ARM::ADDri) {
1324 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1325 if (Offset == 0) {
1326 // Turn it into a move.
1327 MI.setDesc(TII.get(ARM::MOVr));
1328 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1329 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001330 Offset = 0;
1331 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001332 } else if (Offset < 0) {
1333 Offset = -Offset;
1334 isSub = true;
1335 MI.setDesc(TII.get(ARM::SUBri));
1336 }
1337
1338 // Common case: small offset, fits into instruction.
1339 if (ARM_AM::getSOImmVal(Offset) != -1) {
1340 // Replace the FrameIndex with sp / fp
1341 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1342 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001343 Offset = 0;
1344 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001345 }
1346
1347 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1348 // as possible.
1349 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1350 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1351
1352 // We will handle these bits from offset, clear them.
1353 Offset &= ~ThisImmVal;
1354
1355 // Get the properly encoded SOImmVal field.
1356 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1357 "Bit extraction didn't work?");
1358 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1359 } else {
1360 unsigned ImmIdx = 0;
1361 int InstrOffs = 0;
1362 unsigned NumBits = 0;
1363 unsigned Scale = 1;
1364 switch (AddrMode) {
1365 case ARMII::AddrMode2: {
1366 ImmIdx = FrameRegIdx+2;
1367 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1368 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1369 InstrOffs *= -1;
1370 NumBits = 12;
1371 break;
1372 }
1373 case ARMII::AddrMode3: {
1374 ImmIdx = FrameRegIdx+2;
1375 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1376 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1377 InstrOffs *= -1;
1378 NumBits = 8;
1379 break;
1380 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001381 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001382 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001383 // Can't fold any offset even if it's zero.
1384 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001385 case ARMII::AddrMode5: {
1386 ImmIdx = FrameRegIdx+1;
1387 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1388 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1389 InstrOffs *= -1;
1390 NumBits = 8;
1391 Scale = 4;
1392 break;
1393 }
1394 default:
1395 llvm_unreachable("Unsupported addressing mode!");
1396 break;
1397 }
1398
1399 Offset += InstrOffs * Scale;
1400 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1401 if (Offset < 0) {
1402 Offset = -Offset;
1403 isSub = true;
1404 }
1405
1406 // Attempt to fold address comp. if opcode has offset bits
1407 if (NumBits > 0) {
1408 // Common case: small offset, fits into instruction.
1409 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1410 int ImmedOffset = Offset / Scale;
1411 unsigned Mask = (1 << NumBits) - 1;
1412 if ((unsigned)Offset <= Mask * Scale) {
1413 // Replace the FrameIndex with sp
1414 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1415 if (isSub)
1416 ImmedOffset |= 1 << NumBits;
1417 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001418 Offset = 0;
1419 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001420 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001421
Evan Cheng6495f632009-07-28 05:48:47 +00001422 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1423 ImmedOffset = ImmedOffset & Mask;
1424 if (isSub)
1425 ImmedOffset |= 1 << NumBits;
1426 ImmOp.ChangeToImmediate(ImmedOffset);
1427 Offset &= ~(Mask*Scale);
1428 }
1429 }
1430
Evan Chengcdbb3f52009-08-27 01:23:50 +00001431 Offset = (isSub) ? -Offset : Offset;
1432 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001433}
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001434
1435bool ARMBaseInstrInfo::
Eric Christophera99c3e92010-09-28 04:18:29 +00001436AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1437 int &CmpValue) const {
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001438 switch (MI->getOpcode()) {
1439 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001440 case ARM::CMPri:
1441 case ARM::CMPzri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001442 case ARM::t2CMPri:
1443 case ARM::t2CMPzri:
1444 SrcReg = MI->getOperand(0).getReg();
Gabor Greif04ac81d2010-09-21 12:01:15 +00001445 CmpMask = ~0;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001446 CmpValue = MI->getOperand(1).getImm();
1447 return true;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001448 case ARM::TSTri:
1449 case ARM::t2TSTri:
1450 SrcReg = MI->getOperand(0).getReg();
1451 CmpMask = MI->getOperand(1).getImm();
1452 CmpValue = 0;
1453 return true;
1454 }
1455
1456 return false;
1457}
1458
Gabor Greif05642a32010-09-29 10:12:08 +00001459/// isSuitableForMask - Identify a suitable 'and' instruction that
1460/// operates on the given source register and applies the same mask
1461/// as a 'tst' instruction. Provide a limited look-through for copies.
1462/// When successful, MI will hold the found instruction.
1463static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001464 int CmpMask, bool CommonUse) {
Gabor Greif05642a32010-09-29 10:12:08 +00001465 switch (MI->getOpcode()) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001466 case ARM::ANDri:
1467 case ARM::t2ANDri:
Gabor Greif05642a32010-09-29 10:12:08 +00001468 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001469 return false;
Gabor Greif05642a32010-09-29 10:12:08 +00001470 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greif04ac81d2010-09-21 12:01:15 +00001471 return true;
1472 break;
Gabor Greif05642a32010-09-29 10:12:08 +00001473 case ARM::COPY: {
1474 // Walk down one instruction which is potentially an 'and'.
1475 const MachineInstr &Copy = *MI;
Michael J. Spencerf000a7a2010-10-05 06:00:43 +00001476 MachineBasicBlock::iterator AND(
1477 llvm::next(MachineBasicBlock::iterator(MI)));
Gabor Greif05642a32010-09-29 10:12:08 +00001478 if (AND == MI->getParent()->end()) return false;
1479 MI = AND;
1480 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1481 CmpMask, true);
1482 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001483 }
1484
1485 return false;
1486}
1487
Bill Wendlinga6556862010-09-11 00:13:50 +00001488/// OptimizeCompareInstr - Convert the instruction supplying the argument to the
Bill Wendling92ad57f2010-09-10 23:34:19 +00001489/// comparison into one that sets the zero bit in the flags register. Update the
1490/// iterator *only* if a transformation took place.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001491bool ARMBaseInstrInfo::
Gabor Greif04ac81d2010-09-21 12:01:15 +00001492OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
1493 int CmpValue, MachineBasicBlock::iterator &MII) const {
Bill Wendling36656612010-09-10 23:46:12 +00001494 if (CmpValue != 0)
Bill Wendling92ad57f2010-09-10 23:34:19 +00001495 return false;
1496
1497 MachineRegisterInfo &MRI = CmpInstr->getParent()->getParent()->getRegInfo();
1498 MachineRegisterInfo::def_iterator DI = MRI.def_begin(SrcReg);
1499 if (llvm::next(DI) != MRI.def_end())
1500 // Only support one definition.
1501 return false;
1502
1503 MachineInstr *MI = &*DI;
1504
Gabor Greif04ac81d2010-09-21 12:01:15 +00001505 // Masked compares sometimes use the same register as the corresponding 'and'.
1506 if (CmpMask != ~0) {
Gabor Greif05642a32010-09-29 10:12:08 +00001507 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001508 MI = 0;
1509 for (MachineRegisterInfo::use_iterator UI = MRI.use_begin(SrcReg),
1510 UE = MRI.use_end(); UI != UE; ++UI) {
1511 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001512 MachineInstr *PotentialAND = &*UI;
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001513 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
Gabor Greif04ac81d2010-09-21 12:01:15 +00001514 continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001515 MI = PotentialAND;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001516 break;
1517 }
1518 if (!MI) return false;
1519 }
1520 }
1521
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001522 // Conservatively refuse to convert an instruction which isn't in the same BB
1523 // as the comparison.
1524 if (MI->getParent() != CmpInstr->getParent())
1525 return false;
1526
1527 // Check that CPSR isn't set between the comparison instruction and the one we
1528 // want to change.
Evan Cheng691e64a2010-09-21 23:49:07 +00001529 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1530 B = MI->getParent()->begin();
Bill Wendling0aa38b92010-10-09 00:03:48 +00001531
1532 // Early exit if CmpInstr is at the beginning of the BB.
1533 if (I == B) return false;
1534
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001535 --I;
1536 for (; I != E; --I) {
1537 const MachineInstr &Instr = *I;
1538
1539 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1540 const MachineOperand &MO = Instr.getOperand(IO);
Bill Wendling75486db2010-08-10 21:38:11 +00001541 if (!MO.isReg() || !MO.isDef()) continue;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001542
1543 // This instruction modifies CPSR before the one we want to change. We
1544 // can't do this transformation.
1545 if (MO.getReg() == ARM::CPSR)
1546 return false;
1547 }
Evan Cheng691e64a2010-09-21 23:49:07 +00001548
1549 if (I == B)
1550 // The 'and' is below the comparison instruction.
1551 return false;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001552 }
1553
1554 // Set the "zero" bit in CPSR.
1555 switch (MI->getOpcode()) {
1556 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001557 case ARM::ADDri:
Bob Wilson3a951822010-09-15 17:12:08 +00001558 case ARM::ANDri:
1559 case ARM::t2ANDri:
Bill Wendling38ae9972010-08-11 00:23:00 +00001560 case ARM::SUBri:
1561 case ARM::t2ADDri:
Bill Wendlingad422712010-08-18 21:32:07 +00001562 case ARM::t2SUBri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001563 MI->RemoveOperand(5);
Bill Wendlingad422712010-08-18 21:32:07 +00001564 MachineInstrBuilder(MI)
1565 .addReg(ARM::CPSR, RegState::Define | RegState::Implicit);
Bill Wendling220e2402010-09-10 21:55:43 +00001566 MII = llvm::next(MachineBasicBlock::iterator(CmpInstr));
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001567 CmpInstr->eraseFromParent();
1568 return true;
1569 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001570
1571 return false;
1572}
Evan Cheng5f54ce32010-09-09 18:18:55 +00001573
1574unsigned
1575ARMBaseInstrInfo::getNumMicroOps(const MachineInstr *MI,
Evan Cheng3ef1c872010-09-10 01:29:16 +00001576 const InstrItineraryData *ItinData) const {
1577 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +00001578 return 1;
1579
1580 const TargetInstrDesc &Desc = MI->getDesc();
1581 unsigned Class = Desc.getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +00001582 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +00001583 if (UOps)
1584 return UOps;
1585
1586 unsigned Opc = MI->getOpcode();
1587 switch (Opc) {
1588 default:
1589 llvm_unreachable("Unexpected multi-uops instruction!");
1590 break;
Evan Cheng3ef1c872010-09-10 01:29:16 +00001591 case ARM::VLDMQ:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001592 case ARM::VSTMQ:
1593 return 2;
1594
1595 // The number of uOps for load / store multiple are determined by the number
1596 // registers.
Evan Cheng3ef1c872010-09-10 01:29:16 +00001597 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1598 // same cycle. The scheduling for the first load / store must be done
1599 // separately by assuming the the address is not 64-bit aligned.
1600 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
1601 // is not 64-bit aligned, then AGU would take an extra cycle.
1602 // For VFP / NEON load / store multiple, the formula is
Evan Cheng5f54ce32010-09-09 18:18:55 +00001603 // (#reg / 2) + (#reg % 2) + 1.
Evan Cheng5f54ce32010-09-09 18:18:55 +00001604 case ARM::VLDMD:
1605 case ARM::VLDMS:
1606 case ARM::VLDMD_UPD:
1607 case ARM::VLDMS_UPD:
1608 case ARM::VSTMD:
1609 case ARM::VSTMS:
1610 case ARM::VSTMD_UPD:
1611 case ARM::VSTMS_UPD: {
1612 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1613 return (NumRegs / 2) + (NumRegs % 2) + 1;
1614 }
1615 case ARM::LDM_RET:
1616 case ARM::LDM:
1617 case ARM::LDM_UPD:
1618 case ARM::STM:
1619 case ARM::STM_UPD:
1620 case ARM::tLDM:
1621 case ARM::tLDM_UPD:
1622 case ARM::tSTM_UPD:
1623 case ARM::tPOP_RET:
1624 case ARM::tPOP:
1625 case ARM::tPUSH:
1626 case ARM::t2LDM_RET:
1627 case ARM::t2LDM:
1628 case ARM::t2LDM_UPD:
1629 case ARM::t2STM:
1630 case ARM::t2STM_UPD: {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001631 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1632 if (Subtarget.isCortexA8()) {
1633 // 4 registers would be issued: 1, 2, 1.
1634 // 5 registers would be issued: 1, 2, 2.
1635 return 1 + (NumRegs / 2);
1636 } else if (Subtarget.isCortexA9()) {
1637 UOps = (NumRegs / 2);
1638 // If there are odd number of registers or if it's not 64-bit aligned,
1639 // then it takes an extra AGU (Address Generation Unit) cycle.
1640 if ((NumRegs % 2) ||
1641 !MI->hasOneMemOperand() ||
1642 (*MI->memoperands_begin())->getAlignment() < 8)
1643 ++UOps;
1644 return UOps;
1645 } else {
1646 // Assume the worst.
1647 return NumRegs;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001648 }
Evan Cheng5f54ce32010-09-09 18:18:55 +00001649 }
1650 }
1651}
Evan Chenga0792de2010-10-06 06:27:31 +00001652
1653int
Evan Cheng344d9db2010-10-07 23:12:15 +00001654ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1655 const TargetInstrDesc &DefTID,
1656 unsigned DefClass,
1657 unsigned DefIdx, unsigned DefAlign) const {
1658 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1659 if (RegNo <= 0)
1660 // Def is the address writeback.
1661 return ItinData->getOperandCycle(DefClass, DefIdx);
1662
1663 int DefCycle;
1664 if (Subtarget.isCortexA8()) {
1665 // (regno / 2) + (regno % 2) + 1
1666 DefCycle = RegNo / 2 + 1;
1667 if (RegNo % 2)
1668 ++DefCycle;
1669 } else if (Subtarget.isCortexA9()) {
1670 DefCycle = RegNo;
1671 bool isSLoad = false;
1672 switch (DefTID.getOpcode()) {
1673 default: break;
1674 case ARM::VLDMS:
1675 case ARM::VLDMS_UPD:
1676 isSLoad = true;
1677 break;
1678 }
1679 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1680 // then it takes an extra cycle.
1681 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
1682 ++DefCycle;
1683 } else {
1684 // Assume the worst.
1685 DefCycle = RegNo + 2;
1686 }
1687
1688 return DefCycle;
1689}
1690
1691int
1692ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
1693 const TargetInstrDesc &DefTID,
1694 unsigned DefClass,
1695 unsigned DefIdx, unsigned DefAlign) const {
1696 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1697 if (RegNo <= 0)
1698 // Def is the address writeback.
1699 return ItinData->getOperandCycle(DefClass, DefIdx);
1700
1701 int DefCycle;
1702 if (Subtarget.isCortexA8()) {
1703 // 4 registers would be issued: 1, 2, 1.
1704 // 5 registers would be issued: 1, 2, 2.
1705 DefCycle = RegNo / 2;
1706 if (DefCycle < 1)
1707 DefCycle = 1;
1708 // Result latency is issue cycle + 2: E2.
1709 DefCycle += 2;
1710 } else if (Subtarget.isCortexA9()) {
1711 DefCycle = (RegNo / 2);
1712 // If there are odd number of registers or if it's not 64-bit aligned,
1713 // then it takes an extra AGU (Address Generation Unit) cycle.
1714 if ((RegNo % 2) || DefAlign < 8)
1715 ++DefCycle;
1716 // Result latency is AGU cycles + 2.
1717 DefCycle += 2;
1718 } else {
1719 // Assume the worst.
1720 DefCycle = RegNo + 2;
1721 }
1722
1723 return DefCycle;
1724}
1725
1726int
1727ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
1728 const TargetInstrDesc &UseTID,
1729 unsigned UseClass,
1730 unsigned UseIdx, unsigned UseAlign) const {
1731 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1732 if (RegNo <= 0)
1733 return ItinData->getOperandCycle(UseClass, UseIdx);
1734
1735 int UseCycle;
1736 if (Subtarget.isCortexA8()) {
1737 // (regno / 2) + (regno % 2) + 1
1738 UseCycle = RegNo / 2 + 1;
1739 if (RegNo % 2)
1740 ++UseCycle;
1741 } else if (Subtarget.isCortexA9()) {
1742 UseCycle = RegNo;
1743 bool isSStore = false;
1744 switch (UseTID.getOpcode()) {
1745 default: break;
1746 case ARM::VSTMS:
1747 case ARM::VSTMS_UPD:
1748 isSStore = true;
1749 break;
1750 }
1751 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1752 // then it takes an extra cycle.
1753 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
1754 ++UseCycle;
1755 } else {
1756 // Assume the worst.
1757 UseCycle = RegNo + 2;
1758 }
1759
1760 return UseCycle;
1761}
1762
1763int
1764ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
1765 const TargetInstrDesc &UseTID,
1766 unsigned UseClass,
1767 unsigned UseIdx, unsigned UseAlign) const {
1768 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1769 if (RegNo <= 0)
1770 return ItinData->getOperandCycle(UseClass, UseIdx);
1771
1772 int UseCycle;
1773 if (Subtarget.isCortexA8()) {
1774 UseCycle = RegNo / 2;
1775 if (UseCycle < 2)
1776 UseCycle = 2;
1777 // Read in E3.
1778 UseCycle += 2;
1779 } else if (Subtarget.isCortexA9()) {
1780 UseCycle = (RegNo / 2);
1781 // If there are odd number of registers or if it's not 64-bit aligned,
1782 // then it takes an extra AGU (Address Generation Unit) cycle.
1783 if ((RegNo % 2) || UseAlign < 8)
1784 ++UseCycle;
1785 } else {
1786 // Assume the worst.
1787 UseCycle = 1;
1788 }
1789 return UseCycle;
1790}
1791
1792int
Evan Chenga0792de2010-10-06 06:27:31 +00001793ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1794 const TargetInstrDesc &DefTID,
1795 unsigned DefIdx, unsigned DefAlign,
1796 const TargetInstrDesc &UseTID,
1797 unsigned UseIdx, unsigned UseAlign) const {
1798 unsigned DefClass = DefTID.getSchedClass();
1799 unsigned UseClass = UseTID.getSchedClass();
1800
1801 if (DefIdx < DefTID.getNumDefs() && UseIdx < UseTID.getNumOperands())
1802 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1803
1804 // This may be a def / use of a variable_ops instruction, the operand
1805 // latency might be determinable dynamically. Let the target try to
1806 // figure it out.
1807 bool LdmBypass = false;
1808 int DefCycle = -1;
1809 switch (DefTID.getOpcode()) {
1810 default:
1811 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
1812 break;
Evan Cheng5a50cee2010-10-07 01:50:48 +00001813 case ARM::VLDMD:
1814 case ARM::VLDMS:
1815 case ARM::VLDMD_UPD:
1816 case ARM::VLDMS_UPD: {
Evan Cheng344d9db2010-10-07 23:12:15 +00001817 DefCycle = getVLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00001818 break;
1819 }
Evan Chenga0792de2010-10-06 06:27:31 +00001820 case ARM::LDM_RET:
1821 case ARM::LDM:
1822 case ARM::LDM_UPD:
1823 case ARM::tLDM:
1824 case ARM::tLDM_UPD:
1825 case ARM::tPUSH:
1826 case ARM::t2LDM_RET:
1827 case ARM::t2LDM:
1828 case ARM::t2LDM_UPD: {
1829 LdmBypass = 1;
Evan Cheng344d9db2010-10-07 23:12:15 +00001830 DefCycle = getLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
1831 break;
Evan Chenga0792de2010-10-06 06:27:31 +00001832 }
1833 }
1834
1835 if (DefCycle == -1)
1836 // We can't seem to determine the result latency of the def, assume it's 2.
1837 DefCycle = 2;
1838
1839 int UseCycle = -1;
1840 switch (UseTID.getOpcode()) {
1841 default:
1842 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
1843 break;
Evan Cheng5a50cee2010-10-07 01:50:48 +00001844 case ARM::VSTMD:
1845 case ARM::VSTMS:
1846 case ARM::VSTMD_UPD:
1847 case ARM::VSTMS_UPD: {
Evan Cheng344d9db2010-10-07 23:12:15 +00001848 UseCycle = getVSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00001849 break;
1850 }
Evan Chenga0792de2010-10-06 06:27:31 +00001851 case ARM::STM:
1852 case ARM::STM_UPD:
1853 case ARM::tSTM_UPD:
1854 case ARM::tPOP_RET:
1855 case ARM::tPOP:
1856 case ARM::t2STM:
1857 case ARM::t2STM_UPD: {
Evan Cheng344d9db2010-10-07 23:12:15 +00001858 UseCycle = getSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00001859 break;
Evan Chenga0792de2010-10-06 06:27:31 +00001860 }
1861 }
1862
1863 if (UseCycle == -1)
1864 // Assume it's read in the first stage.
1865 UseCycle = 1;
1866
1867 UseCycle = DefCycle - UseCycle + 1;
1868 if (UseCycle > 0) {
1869 if (LdmBypass) {
1870 // It's a variable_ops instruction so we can't use DefIdx here. Just use
1871 // first def operand.
1872 if (ItinData->hasPipelineForwarding(DefClass, DefTID.getNumOperands()-1,
1873 UseClass, UseIdx))
1874 --UseCycle;
1875 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
1876 UseClass, UseIdx))
1877 --UseCycle;
1878 }
1879
1880 return UseCycle;
1881}
1882
1883int
1884ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1885 const MachineInstr *DefMI, unsigned DefIdx,
1886 const MachineInstr *UseMI, unsigned UseIdx) const {
1887 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
1888 DefMI->isRegSequence() || DefMI->isImplicitDef())
1889 return 1;
1890
1891 const TargetInstrDesc &DefTID = DefMI->getDesc();
1892 if (!ItinData || ItinData->isEmpty())
1893 return DefTID.mayLoad() ? 3 : 1;
1894
1895 const TargetInstrDesc &UseTID = UseMI->getDesc();
1896 unsigned DefAlign = DefMI->hasOneMemOperand()
1897 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
1898 unsigned UseAlign = UseMI->hasOneMemOperand()
1899 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
1900 return getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
1901 UseTID, UseIdx, UseAlign);
1902}
1903
1904int
1905ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1906 SDNode *DefNode, unsigned DefIdx,
1907 SDNode *UseNode, unsigned UseIdx) const {
1908 if (!DefNode->isMachineOpcode())
1909 return 1;
1910
1911 const TargetInstrDesc &DefTID = get(DefNode->getMachineOpcode());
1912 if (!ItinData || ItinData->isEmpty())
1913 return DefTID.mayLoad() ? 3 : 1;
1914
1915 if (!UseNode->isMachineOpcode())
1916 return ItinData->getOperandCycle(DefTID.getSchedClass(), DefIdx);
1917
1918 const TargetInstrDesc &UseTID = get(UseNode->getMachineOpcode());
1919 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
1920 unsigned DefAlign = !DefMN->memoperands_empty()
1921 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
1922 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
1923 unsigned UseAlign = !UseMN->memoperands_empty()
1924 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
1925 return getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
1926 UseTID, UseIdx, UseAlign);
1927}