blob: 9bda8fd70117aa92feab30920c3008211c32d831 [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/CodeGen/LiveVariables.h"
Evan Chengd457e6e2009-11-07 04:04:34 +000025#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwin334c2642009-07-08 16:09:28 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
28#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov249fb332009-10-07 00:06:35 +000029#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng2457f2c2010-05-22 01:47:14 +000030#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov249fb332009-10-07 00:06:35 +000031#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000032#include "llvm/MC/MCAsmInfo.h"
David Goodwin334c2642009-07-08 16:09:28 +000033#include "llvm/Support/CommandLine.h"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000034#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000035#include "llvm/Support/ErrorHandling.h"
Bill Wendling40a5eb12010-11-01 20:41:43 +000036#include "llvm/ADT/STLExtras.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
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000043ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
44 : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)),
45 Subtarget(STI) {
David Goodwin334c2642009-07-08 16:09:28 +000046}
47
48MachineInstr *
49ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
50 MachineBasicBlock::iterator &MBBI,
51 LiveVariables *LV) const {
Evan Cheng78703dd2009-07-27 18:44:00 +000052 // FIXME: Thumb2 support.
53
David Goodwin334c2642009-07-08 16:09:28 +000054 if (!EnableARM3Addr)
55 return NULL;
56
57 MachineInstr *MI = MBBI;
58 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +000059 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwin334c2642009-07-08 16:09:28 +000060 bool isPre = false;
61 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
62 default: return NULL;
63 case ARMII::IndexModePre:
64 isPre = true;
65 break;
66 case ARMII::IndexModePost:
67 break;
68 }
69
70 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
71 // operation.
72 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
73 if (MemOpc == 0)
74 return NULL;
75
76 MachineInstr *UpdateMI = NULL;
77 MachineInstr *MemMI = NULL;
78 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
79 const TargetInstrDesc &TID = MI->getDesc();
80 unsigned NumOps = TID.getNumOperands();
81 bool isLoad = !TID.mayStore();
82 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
83 const MachineOperand &Base = MI->getOperand(2);
84 const MachineOperand &Offset = MI->getOperand(NumOps-3);
85 unsigned WBReg = WB.getReg();
86 unsigned BaseReg = Base.getReg();
87 unsigned OffReg = Offset.getReg();
88 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
89 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
90 switch (AddrMode) {
91 default:
92 assert(false && "Unknown indexed op!");
93 return NULL;
94 case ARMII::AddrMode2: {
95 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
96 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
97 if (OffReg == 0) {
Evan Chenge7cbe412009-07-08 21:03:57 +000098 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwin334c2642009-07-08 16:09:28 +000099 // Can't encode it in a so_imm operand. This transformation will
100 // add more than 1 instruction. Abandon!
101 return NULL;
102 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000103 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge7cbe412009-07-08 21:03:57 +0000104 .addReg(BaseReg).addImm(Amt)
David Goodwin334c2642009-07-08 16:09:28 +0000105 .addImm(Pred).addReg(0).addReg(0);
106 } else if (Amt != 0) {
107 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
108 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
109 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000110 get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000111 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
112 .addImm(Pred).addReg(0).addReg(0);
113 } else
114 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000115 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000116 .addReg(BaseReg).addReg(OffReg)
117 .addImm(Pred).addReg(0).addReg(0);
118 break;
119 }
120 case ARMII::AddrMode3 : {
121 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
122 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
123 if (OffReg == 0)
124 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
125 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000126 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000127 .addReg(BaseReg).addImm(Amt)
128 .addImm(Pred).addReg(0).addReg(0);
129 else
130 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000131 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000132 .addReg(BaseReg).addReg(OffReg)
133 .addImm(Pred).addReg(0).addReg(0);
134 break;
135 }
136 }
137
138 std::vector<MachineInstr*> NewMIs;
139 if (isPre) {
140 if (isLoad)
141 MemMI = BuildMI(MF, MI->getDebugLoc(),
142 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach3e556122010-10-26 22:37:02 +0000143 .addReg(WBReg).addImm(0).addImm(Pred);
David Goodwin334c2642009-07-08 16:09:28 +0000144 else
145 MemMI = BuildMI(MF, MI->getDebugLoc(),
146 get(MemOpc)).addReg(MI->getOperand(1).getReg())
147 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
148 NewMIs.push_back(MemMI);
149 NewMIs.push_back(UpdateMI);
150 } else {
151 if (isLoad)
152 MemMI = BuildMI(MF, MI->getDebugLoc(),
153 get(MemOpc), MI->getOperand(0).getReg())
Jim Grosbach3e556122010-10-26 22:37:02 +0000154 .addReg(BaseReg).addImm(0).addImm(Pred);
David Goodwin334c2642009-07-08 16:09:28 +0000155 else
156 MemMI = BuildMI(MF, MI->getDebugLoc(),
157 get(MemOpc)).addReg(MI->getOperand(1).getReg())
158 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
159 if (WB.isDead())
160 UpdateMI->getOperand(0).setIsDead();
161 NewMIs.push_back(UpdateMI);
162 NewMIs.push_back(MemMI);
163 }
164
165 // Transfer LiveVariables states, kill / dead info.
166 if (LV) {
167 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
168 MachineOperand &MO = MI->getOperand(i);
169 if (MO.isReg() && MO.getReg() &&
170 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
171 unsigned Reg = MO.getReg();
172
173 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
174 if (MO.isDef()) {
175 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
176 if (MO.isDead())
177 LV->addVirtualRegisterDead(Reg, NewMI);
178 }
179 if (MO.isUse() && MO.isKill()) {
180 for (unsigned j = 0; j < 2; ++j) {
181 // Look at the two new MI's in reverse order.
182 MachineInstr *NewMI = NewMIs[j];
183 if (!NewMI->readsRegister(Reg))
184 continue;
185 LV->addVirtualRegisterKilled(Reg, NewMI);
186 if (VI.removeKill(MI))
187 VI.Kills.push_back(NewMI);
188 break;
189 }
190 }
191 }
192 }
193 }
194
195 MFI->insert(MBBI, NewMIs[1]);
196 MFI->insert(MBBI, NewMIs[0]);
197 return NewMIs[0];
198}
199
Evan Cheng2457f2c2010-05-22 01:47:14 +0000200bool
201ARMBaseInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Jim Grosbach18f30e62010-06-02 21:53:11 +0000202 MachineBasicBlock::iterator MI,
203 const std::vector<CalleeSavedInfo> &CSI,
204 const TargetRegisterInfo *TRI) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +0000205 if (CSI.empty())
206 return false;
207
208 DebugLoc DL;
209 if (MI != MBB.end()) DL = MI->getDebugLoc();
210
Eric Christopher6c501192010-11-11 19:47:02 +0000211 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
212 unsigned Reg = CSI[i].getReg();
Evan Cheng2457f2c2010-05-22 01:47:14 +0000213 bool isKill = true;
214
215 // Add the callee-saved register as live-in unless it's LR and
216 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
217 // then it's already added to the function and entry block live-in sets.
218 if (Reg == ARM::LR) {
219 MachineFunction &MF = *MBB.getParent();
220 if (MF.getFrameInfo()->isReturnAddressTaken() &&
221 MF.getRegInfo().isLiveIn(Reg))
222 isKill = false;
223 }
224
225 if (isKill)
226 MBB.addLiveIn(Reg);
227
Eric Christopher6c501192010-11-11 19:47:02 +0000228 // Insert the spill to the stack frame. The register is killed at the spill
229 //
230 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
231 storeRegToStackSlot(MBB, MI, Reg, isKill,
232 CSI[i].getFrameIdx(), RC, TRI);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000233 }
234 return true;
235}
236
David Goodwin334c2642009-07-08 16:09:28 +0000237// Branch analysis.
238bool
239ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
240 MachineBasicBlock *&FBB,
241 SmallVectorImpl<MachineOperand> &Cond,
242 bool AllowModify) const {
243 // If the block has no terminators, it just falls into the block after it.
244 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000245 if (I == MBB.begin())
246 return false;
247 --I;
248 while (I->isDebugValue()) {
249 if (I == MBB.begin())
250 return false;
251 --I;
252 }
253 if (!isUnpredicatedTerminator(I))
David Goodwin334c2642009-07-08 16:09:28 +0000254 return false;
255
256 // Get the last instruction in the block.
257 MachineInstr *LastInst = I;
258
259 // If there is only one terminator instruction, process it.
260 unsigned LastOpc = LastInst->getOpcode();
261 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Evan Cheng5ca53a72009-07-27 18:20:05 +0000262 if (isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000263 TBB = LastInst->getOperand(0).getMBB();
264 return false;
265 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000266 if (isCondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000267 // Block ends with fall-through condbranch.
268 TBB = LastInst->getOperand(0).getMBB();
269 Cond.push_back(LastInst->getOperand(1));
270 Cond.push_back(LastInst->getOperand(2));
271 return false;
272 }
273 return true; // Can't handle indirect branch.
274 }
275
276 // Get the instruction before it if it is a terminator.
277 MachineInstr *SecondLastInst = I;
Evan Cheng108c8722010-09-23 06:54:40 +0000278 unsigned SecondLastOpc = SecondLastInst->getOpcode();
279
280 // If AllowModify is true and the block ends with two or more unconditional
281 // branches, delete all but the first unconditional branch.
282 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
283 while (isUncondBranchOpcode(SecondLastOpc)) {
284 LastInst->eraseFromParent();
285 LastInst = SecondLastInst;
286 LastOpc = LastInst->getOpcode();
Evan Cheng676e2582010-09-23 19:42:03 +0000287 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
288 // Return now the only terminator is an unconditional branch.
289 TBB = LastInst->getOperand(0).getMBB();
290 return false;
291 } else {
Evan Cheng108c8722010-09-23 06:54:40 +0000292 SecondLastInst = I;
293 SecondLastOpc = SecondLastInst->getOpcode();
294 }
295 }
296 }
David Goodwin334c2642009-07-08 16:09:28 +0000297
298 // If there are three terminators, we don't know what sort of block this is.
299 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
300 return true;
301
Evan Cheng5ca53a72009-07-27 18:20:05 +0000302 // If the block ends with a B and a Bcc, handle it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000303 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000304 TBB = SecondLastInst->getOperand(0).getMBB();
305 Cond.push_back(SecondLastInst->getOperand(1));
306 Cond.push_back(SecondLastInst->getOperand(2));
307 FBB = LastInst->getOperand(0).getMBB();
308 return false;
309 }
310
311 // If the block ends with two unconditional branches, handle it. The second
312 // one is not executed, so remove it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000313 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000314 TBB = SecondLastInst->getOperand(0).getMBB();
315 I = LastInst;
316 if (AllowModify)
317 I->eraseFromParent();
318 return false;
319 }
320
321 // ...likewise if it ends with a branch table followed by an unconditional
322 // branch. The branch folder can create these, and we must get rid of them for
323 // correctness of Thumb constant islands.
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000324 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
325 isIndirectBranchOpcode(SecondLastOpc)) &&
Evan Cheng5ca53a72009-07-27 18:20:05 +0000326 isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000327 I = LastInst;
328 if (AllowModify)
329 I->eraseFromParent();
330 return true;
331 }
332
333 // Otherwise, can't handle this.
334 return true;
335}
336
337
338unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwin334c2642009-07-08 16:09:28 +0000339 MachineBasicBlock::iterator I = MBB.end();
340 if (I == MBB.begin()) return 0;
341 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000342 while (I->isDebugValue()) {
343 if (I == MBB.begin())
344 return 0;
345 --I;
346 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000347 if (!isUncondBranchOpcode(I->getOpcode()) &&
348 !isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000349 return 0;
350
351 // Remove the branch.
352 I->eraseFromParent();
353
354 I = MBB.end();
355
356 if (I == MBB.begin()) return 1;
357 --I;
Evan Cheng5ca53a72009-07-27 18:20:05 +0000358 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000359 return 1;
360
361 // Remove the branch.
362 I->eraseFromParent();
363 return 2;
364}
365
366unsigned
367ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000368 MachineBasicBlock *FBB,
369 const SmallVectorImpl<MachineOperand> &Cond,
370 DebugLoc DL) const {
Evan Cheng6495f632009-07-28 05:48:47 +0000371 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
372 int BOpc = !AFI->isThumbFunction()
373 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
374 int BccOpc = !AFI->isThumbFunction()
375 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
David Goodwin334c2642009-07-08 16:09:28 +0000376
377 // Shouldn't be a fall through.
378 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
379 assert((Cond.size() == 2 || Cond.size() == 0) &&
380 "ARM branch conditions have two components!");
381
382 if (FBB == 0) {
383 if (Cond.empty()) // Unconditional branch?
Stuart Hastings3bf91252010-06-17 22:43:56 +0000384 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
David Goodwin334c2642009-07-08 16:09:28 +0000385 else
Stuart Hastings3bf91252010-06-17 22:43:56 +0000386 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000387 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
388 return 1;
389 }
390
391 // Two-way conditional branch.
Stuart Hastings3bf91252010-06-17 22:43:56 +0000392 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000393 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
Stuart Hastings3bf91252010-06-17 22:43:56 +0000394 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwin334c2642009-07-08 16:09:28 +0000395 return 2;
396}
397
398bool ARMBaseInstrInfo::
399ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
400 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
401 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
402 return false;
403}
404
David Goodwin334c2642009-07-08 16:09:28 +0000405bool ARMBaseInstrInfo::
406PredicateInstruction(MachineInstr *MI,
407 const SmallVectorImpl<MachineOperand> &Pred) const {
408 unsigned Opc = MI->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000409 if (isUncondBranchOpcode(Opc)) {
410 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
David Goodwin334c2642009-07-08 16:09:28 +0000411 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
412 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
413 return true;
414 }
415
416 int PIdx = MI->findFirstPredOperandIdx();
417 if (PIdx != -1) {
418 MachineOperand &PMO = MI->getOperand(PIdx);
419 PMO.setImm(Pred[0].getImm());
420 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
421 return true;
422 }
423 return false;
424}
425
426bool ARMBaseInstrInfo::
427SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
428 const SmallVectorImpl<MachineOperand> &Pred2) const {
429 if (Pred1.size() > 2 || Pred2.size() > 2)
430 return false;
431
432 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
433 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
434 if (CC1 == CC2)
435 return true;
436
437 switch (CC1) {
438 default:
439 return false;
440 case ARMCC::AL:
441 return true;
442 case ARMCC::HS:
443 return CC2 == ARMCC::HI;
444 case ARMCC::LS:
445 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
446 case ARMCC::GE:
447 return CC2 == ARMCC::GT;
448 case ARMCC::LE:
449 return CC2 == ARMCC::LT;
450 }
451}
452
453bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
454 std::vector<MachineOperand> &Pred) const {
Evan Cheng8fb90362009-08-08 03:20:32 +0000455 // FIXME: This confuses implicit_def with optional CPSR def.
David Goodwin334c2642009-07-08 16:09:28 +0000456 const TargetInstrDesc &TID = MI->getDesc();
457 if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
458 return false;
459
460 bool Found = false;
461 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
462 const MachineOperand &MO = MI->getOperand(i);
463 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
464 Pred.push_back(MO);
465 Found = true;
466 }
467 }
468
469 return Found;
470}
471
Evan Chengac0869d2009-11-21 06:21:52 +0000472/// isPredicable - Return true if the specified instruction can be predicated.
473/// By default, this returns true for every instruction with a
474/// PredicateOperand.
475bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
476 const TargetInstrDesc &TID = MI->getDesc();
477 if (!TID.isPredicable())
478 return false;
479
480 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
481 ARMFunctionInfo *AFI =
482 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
Evan Chengd7f08102009-11-24 08:06:15 +0000483 return AFI->isThumb2Function();
Evan Chengac0869d2009-11-21 06:21:52 +0000484 }
485 return true;
486}
David Goodwin334c2642009-07-08 16:09:28 +0000487
Chris Lattner56856b12009-12-03 06:58:32 +0000488/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
Chandler Carruth19e57022010-10-23 08:40:19 +0000489LLVM_ATTRIBUTE_NOINLINE
David Goodwin334c2642009-07-08 16:09:28 +0000490static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattner56856b12009-12-03 06:58:32 +0000491 unsigned JTI);
David Goodwin334c2642009-07-08 16:09:28 +0000492static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
493 unsigned JTI) {
Chris Lattner56856b12009-12-03 06:58:32 +0000494 assert(JTI < JT.size());
David Goodwin334c2642009-07-08 16:09:28 +0000495 return JT[JTI].MBBs.size();
496}
497
498/// GetInstSize - Return the size of the specified MachineInstr.
499///
500unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
501 const MachineBasicBlock &MBB = *MI->getParent();
502 const MachineFunction *MF = MBB.getParent();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000503 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwin334c2642009-07-08 16:09:28 +0000504
505 // Basic size info comes from the TSFlags field.
506 const TargetInstrDesc &TID = MI->getDesc();
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000507 uint64_t TSFlags = TID.TSFlags;
David Goodwin334c2642009-07-08 16:09:28 +0000508
Evan Chenga0ee8622009-07-31 22:22:22 +0000509 unsigned Opc = MI->getOpcode();
David Goodwin334c2642009-07-08 16:09:28 +0000510 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
511 default: {
512 // If this machine instr is an inline asm, measure it.
513 if (MI->getOpcode() == ARM::INLINEASM)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000514 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Goodwin334c2642009-07-08 16:09:28 +0000515 if (MI->isLabel())
516 return 0;
Evan Chenga0ee8622009-07-31 22:22:22 +0000517 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000518 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000519 llvm_unreachable("Unknown or unset size field for instr!");
Chris Lattner518bb532010-02-09 19:54:29 +0000520 case TargetOpcode::IMPLICIT_DEF:
521 case TargetOpcode::KILL:
Bill Wendling7431bea2010-07-16 22:20:36 +0000522 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000523 case TargetOpcode::EH_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +0000524 case TargetOpcode::DBG_VALUE:
David Goodwin334c2642009-07-08 16:09:28 +0000525 return 0;
526 }
527 break;
528 }
Evan Cheng78947622009-07-24 18:20:44 +0000529 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
530 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
531 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
David Goodwin334c2642009-07-08 16:09:28 +0000532 case ARMII::SizeSpecial: {
Evan Chenga0ee8622009-07-31 22:22:22 +0000533 switch (Opc) {
Jim Grosbach3c38f962010-10-06 22:01:26 +0000534 case ARM::MOVi32imm:
535 case ARM::t2MOVi32imm:
536 return 8;
David Goodwin334c2642009-07-08 16:09:28 +0000537 case ARM::CONSTPOOL_ENTRY:
538 // If this machine instr is a constant pool entry, its size is recorded as
539 // operand #2.
540 return MI->getOperand(2).getImm();
Jim Grosbach5eb19512010-05-22 01:06:18 +0000541 case ARM::Int_eh_sjlj_longjmp:
542 return 16;
543 case ARM::tInt_eh_sjlj_longjmp:
544 return 10;
Evan Cheng78947622009-07-24 18:20:44 +0000545 case ARM::Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000546 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000547 return 20;
Jim Grosbachd1228742009-12-01 18:10:36 +0000548 case ARM::tInt_eh_sjlj_setjmp:
Jim Grosbach5aa16842009-08-11 19:42:21 +0000549 case ARM::t2Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000550 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000551 return 12;
David Goodwin334c2642009-07-08 16:09:28 +0000552 case ARM::BR_JTr:
553 case ARM::BR_JTm:
554 case ARM::BR_JTadd:
Evan Chenga0ee8622009-07-31 22:22:22 +0000555 case ARM::tBR_JTr:
Evan Chengd26b14c2009-07-31 18:28:05 +0000556 case ARM::t2BR_JT:
557 case ARM::t2TBB:
558 case ARM::t2TBH: {
David Goodwin334c2642009-07-08 16:09:28 +0000559 // These are jumptable branches, i.e. a branch followed by an inlined
Evan Chengd26b14c2009-07-31 18:28:05 +0000560 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
561 // entry is one byte; TBH two byte each.
Evan Chenga0ee8622009-07-31 22:22:22 +0000562 unsigned EntrySize = (Opc == ARM::t2TBB)
563 ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4);
David Goodwin334c2642009-07-08 16:09:28 +0000564 unsigned NumOps = TID.getNumOperands();
565 MachineOperand JTOP =
566 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
567 unsigned JTI = JTOP.getIndex();
568 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000569 assert(MJTI != 0);
David Goodwin334c2642009-07-08 16:09:28 +0000570 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
571 assert(JTI < JT.size());
572 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
573 // 4 aligned. The assembler / linker may add 2 byte padding just before
574 // the JT entries. The size does not include this padding; the
575 // constant islands pass does separate bookkeeping for it.
576 // FIXME: If we know the size of the function is less than (1 << 16) *2
577 // bytes, we can use 16-bit entries instead. Then there won't be an
578 // alignment issue.
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000579 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
580 unsigned NumEntries = getNumJTEntries(JT, JTI);
581 if (Opc == ARM::t2TBB && (NumEntries & 1))
582 // Make sure the instruction that follows TBB is 2-byte aligned.
583 // FIXME: Constant island pass should insert an "ALIGN" instruction
584 // instead.
585 ++NumEntries;
586 return NumEntries * EntrySize + InstSize;
David Goodwin334c2642009-07-08 16:09:28 +0000587 }
588 default:
589 // Otherwise, pseudo-instruction sizes are zero.
590 return 0;
591 }
592 }
593 }
594 return 0; // Not reached
595}
596
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000597void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
598 MachineBasicBlock::iterator I, DebugLoc DL,
599 unsigned DestReg, unsigned SrcReg,
600 bool KillSrc) const {
601 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
602 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson1665b0a2010-02-16 17:24:15 +0000603
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000604 if (GPRDest && GPRSrc) {
605 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
606 .addReg(SrcReg, getKillRegState(KillSrc))));
607 return;
David Goodwin7bfdca02009-08-05 21:02:22 +0000608 }
David Goodwin334c2642009-07-08 16:09:28 +0000609
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000610 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
611 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
612
613 unsigned Opc;
614 if (SPRDest && SPRSrc)
615 Opc = ARM::VMOVS;
616 else if (GPRDest && SPRSrc)
617 Opc = ARM::VMOVRS;
618 else if (SPRDest && GPRSrc)
619 Opc = ARM::VMOVSR;
620 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
621 Opc = ARM::VMOVD;
622 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
623 Opc = ARM::VMOVQ;
624 else if (ARM::QQPRRegClass.contains(DestReg, SrcReg))
625 Opc = ARM::VMOVQQ;
626 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg))
627 Opc = ARM::VMOVQQQQ;
628 else
629 llvm_unreachable("Impossible reg-to-reg copy");
630
631 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
632 MIB.addReg(SrcReg, getKillRegState(KillSrc));
633 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ)
634 AddDefaultPred(MIB);
David Goodwin334c2642009-07-08 16:09:28 +0000635}
636
Evan Chengc10b5af2010-05-07 00:24:52 +0000637static const
638MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
639 unsigned Reg, unsigned SubIdx, unsigned State,
640 const TargetRegisterInfo *TRI) {
641 if (!SubIdx)
642 return MIB.addReg(Reg, State);
643
644 if (TargetRegisterInfo::isPhysicalRegister(Reg))
645 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
646 return MIB.addReg(Reg, State, SubIdx);
647}
648
David Goodwin334c2642009-07-08 16:09:28 +0000649void ARMBaseInstrInfo::
650storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
651 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000652 const TargetRegisterClass *RC,
653 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000654 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000655 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000656 MachineFunction &MF = *MBB.getParent();
657 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000658 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000659
660 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000661 MF.getMachineMemOperand(MachinePointerInfo(
662 PseudoSourceValue::getFixedStack(FI)),
663 MachineMemOperand::MOStore,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000664 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000665 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000666
Bob Wilson0eb0c742010-02-16 22:01:59 +0000667 // tGPR is used sometimes in ARM instructions that need to avoid using
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000668 // certain registers. Just treat it as GPR here. Likewise, rGPR.
669 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
670 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000671 RC = ARM::GPRRegisterClass;
672
Bob Wilsonebe99b22010-06-18 21:32:42 +0000673 switch (RC->getID()) {
674 case ARM::GPRRegClassID:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000675 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STRi12))
David Goodwin334c2642009-07-08 16:09:28 +0000676 .addReg(SrcReg, getKillRegState(isKill))
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000677 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000678 break;
679 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000680 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
681 .addReg(SrcReg, getKillRegState(isKill))
682 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000683 break;
684 case ARM::DPRRegClassID:
685 case ARM::DPR_VFP2RegClassID:
686 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000687 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000688 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000689 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000690 break;
691 case ARM::QPRRegClassID:
692 case ARM::QPR_VFP2RegClassID:
693 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000694 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000695 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo))
Bob Wilsonf967ca02010-07-06 21:26:18 +0000696 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000697 .addReg(SrcReg, getKillRegState(isKill))
698 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000699 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000700 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQ))
701 .addReg(SrcReg, getKillRegState(isKill))
702 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000703 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
Evan Cheng69b9f982010-05-13 01:12:06 +0000704 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000705 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000706 break;
707 case ARM::QQPRRegClassID:
708 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000709 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng22c687b2010-05-14 02:13:41 +0000710 // FIXME: It's possible to only store part of the QQ register if the
711 // spilled def has a sub-register index.
Bob Wilson168f3822010-09-15 01:48:05 +0000712 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
713 .addFrameIndex(FI).addImm(16)
714 .addReg(SrcReg, getKillRegState(isKill))
715 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000716 } else {
717 MachineInstrBuilder MIB =
718 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
719 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000720 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Evan Cheng435d4992010-05-07 02:04:02 +0000721 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000722 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
723 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
724 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
725 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000726 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000727 break;
728 case ARM::QQQQPRRegClassID: {
Evan Cheng22c687b2010-05-14 02:13:41 +0000729 MachineInstrBuilder MIB =
730 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
731 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000732 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Evan Cheng22c687b2010-05-14 02:13:41 +0000733 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000734 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
735 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
736 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
737 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
738 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
739 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
740 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
741 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
Bob Wilsonebe99b22010-06-18 21:32:42 +0000742 break;
743 }
744 default:
745 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000746 }
747}
748
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000749unsigned
750ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
751 int &FrameIndex) const {
752 switch (MI->getOpcode()) {
753 default: break;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000754 case ARM::STRrs:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000755 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
756 if (MI->getOperand(1).isFI() &&
757 MI->getOperand(2).isReg() &&
758 MI->getOperand(3).isImm() &&
759 MI->getOperand(2).getReg() == 0 &&
760 MI->getOperand(3).getImm() == 0) {
761 FrameIndex = MI->getOperand(1).getIndex();
762 return MI->getOperand(0).getReg();
763 }
764 break;
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000765 case ARM::STRi12:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000766 case ARM::t2STRi12:
767 case ARM::tSpill:
768 case ARM::VSTRD:
769 case ARM::VSTRS:
770 if (MI->getOperand(1).isFI() &&
771 MI->getOperand(2).isImm() &&
772 MI->getOperand(2).getImm() == 0) {
773 FrameIndex = MI->getOperand(1).getIndex();
774 return MI->getOperand(0).getReg();
775 }
776 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000777 case ARM::VST1q64Pseudo:
778 if (MI->getOperand(0).isFI() &&
779 MI->getOperand(2).getSubReg() == 0) {
780 FrameIndex = MI->getOperand(0).getIndex();
781 return MI->getOperand(2).getReg();
782 }
Jakob Stoklund Olesen31bbc512010-09-15 21:40:09 +0000783 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000784 case ARM::VSTMQ:
785 if (MI->getOperand(1).isFI() &&
786 MI->getOperand(2).isImm() &&
787 MI->getOperand(2).getImm() == ARM_AM::getAM4ModeImm(ARM_AM::ia) &&
788 MI->getOperand(0).getSubReg() == 0) {
789 FrameIndex = MI->getOperand(1).getIndex();
790 return MI->getOperand(0).getReg();
791 }
792 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000793 }
794
795 return 0;
796}
797
David Goodwin334c2642009-07-08 16:09:28 +0000798void ARMBaseInstrInfo::
799loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
800 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000801 const TargetRegisterClass *RC,
802 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000803 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000804 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000805 MachineFunction &MF = *MBB.getParent();
806 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000807 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000808 MachineMemOperand *MMO =
Chris Lattner59db5492010-09-21 04:39:43 +0000809 MF.getMachineMemOperand(
810 MachinePointerInfo(PseudoSourceValue::getFixedStack(FI)),
811 MachineMemOperand::MOLoad,
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000812 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000813 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000814
Bob Wilson0eb0c742010-02-16 22:01:59 +0000815 // tGPR is used sometimes in ARM instructions that need to avoid using
816 // certain registers. Just treat it as GPR here.
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000817 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
818 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000819 RC = ARM::GPRRegisterClass;
820
Bob Wilsonebe99b22010-06-18 21:32:42 +0000821 switch (RC->getID()) {
822 case ARM::GPRRegClassID:
Jim Grosbach3e556122010-10-26 22:37:02 +0000823 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
824 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000825 break;
826 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000827 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
828 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000829 break;
830 case ARM::DPRRegClassID:
831 case ARM::DPR_VFP2RegClassID:
832 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000833 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000834 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000835 break;
836 case ARM::QPRRegClassID:
837 case ARM::QPR_VFP2RegClassID:
838 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000839 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000840 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg)
Bob Wilsonf967ca02010-07-06 21:26:18 +0000841 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000842 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000843 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000844 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQ), DestReg)
845 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000846 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
Evan Cheng69b9f982010-05-13 01:12:06 +0000847 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000848 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000849 break;
850 case ARM::QQPRRegClassID:
851 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000852 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000853 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
854 .addFrameIndex(FI).addImm(16)
855 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000856 } else {
857 MachineInstrBuilder MIB =
858 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
859 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000860 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Evan Cheng435d4992010-05-07 02:04:02 +0000861 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000862 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
863 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
864 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
865 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000866 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000867 break;
868 case ARM::QQQQPRRegClassID: {
869 MachineInstrBuilder MIB =
870 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
871 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000872 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Bob Wilsonebe99b22010-06-18 21:32:42 +0000873 .addMemOperand(MMO);
874 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
875 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
876 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
877 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
878 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
879 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
880 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
881 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
882 break;
883 }
884 default:
885 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000886 }
887}
888
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000889unsigned
890ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
891 int &FrameIndex) const {
892 switch (MI->getOpcode()) {
893 default: break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000894 case ARM::LDRrs:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000895 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
896 if (MI->getOperand(1).isFI() &&
897 MI->getOperand(2).isReg() &&
898 MI->getOperand(3).isImm() &&
899 MI->getOperand(2).getReg() == 0 &&
900 MI->getOperand(3).getImm() == 0) {
901 FrameIndex = MI->getOperand(1).getIndex();
902 return MI->getOperand(0).getReg();
903 }
904 break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000905 case ARM::LDRi12:
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000906 case ARM::t2LDRi12:
907 case ARM::tRestore:
908 case ARM::VLDRD:
909 case ARM::VLDRS:
910 if (MI->getOperand(1).isFI() &&
911 MI->getOperand(2).isImm() &&
912 MI->getOperand(2).getImm() == 0) {
913 FrameIndex = MI->getOperand(1).getIndex();
914 return MI->getOperand(0).getReg();
915 }
916 break;
Jakob Stoklund Olesend64816a2010-09-15 17:27:09 +0000917 case ARM::VLD1q64Pseudo:
918 if (MI->getOperand(1).isFI() &&
919 MI->getOperand(0).getSubReg() == 0) {
920 FrameIndex = MI->getOperand(1).getIndex();
921 return MI->getOperand(0).getReg();
922 }
923 break;
Jakob Stoklund Olesen06f264e2010-09-15 21:40:11 +0000924 case ARM::VLDMQ:
925 if (MI->getOperand(1).isFI() &&
926 MI->getOperand(2).isImm() &&
927 MI->getOperand(2).getImm() == ARM_AM::getAM4ModeImm(ARM_AM::ia) &&
928 MI->getOperand(0).getSubReg() == 0) {
929 FrameIndex = MI->getOperand(1).getIndex();
930 return MI->getOperand(0).getReg();
931 }
932 break;
Jakob Stoklund Olesen34327852010-09-15 16:36:26 +0000933 }
934
935 return 0;
936}
937
Evan Cheng62b50652010-04-26 07:39:25 +0000938MachineInstr*
939ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000940 int FrameIx, uint64_t Offset,
Evan Cheng62b50652010-04-26 07:39:25 +0000941 const MDNode *MDPtr,
942 DebugLoc DL) const {
943 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
944 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
945 return &*MIB;
946}
947
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000948/// Create a copy of a const pool value. Update CPI to the new index and return
949/// the label UID.
950static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
951 MachineConstantPool *MCP = MF.getConstantPool();
952 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
953
954 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
955 assert(MCPE.isMachineConstantPoolEntry() &&
956 "Expecting a machine constantpool entry!");
957 ARMConstantPoolValue *ACPV =
958 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
959
960 unsigned PCLabelId = AFI->createConstPoolEntryUId();
961 ARMConstantPoolValue *NewCPV = 0;
Jim Grosbach51f5b672010-09-10 21:38:22 +0000962 // FIXME: The below assumes PIC relocation model and that the function
963 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
964 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
965 // instructions, so that's probably OK, but is PIC always correct when
966 // we get here?
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000967 if (ACPV->isGlobalValue())
968 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
969 ARMCP::CPValue, 4);
970 else if (ACPV->isExtSymbol())
971 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
972 ACPV->getSymbol(), PCLabelId, 4);
973 else if (ACPV->isBlockAddress())
974 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
975 ARMCP::CPBlockAddress, 4);
Jim Grosbach51f5b672010-09-10 21:38:22 +0000976 else if (ACPV->isLSDA())
977 NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId,
978 ARMCP::CPLSDA, 4);
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000979 else
980 llvm_unreachable("Unexpected ARM constantpool value type!!");
981 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
982 return PCLabelId;
983}
984
Evan Chengfdc83402009-11-08 00:15:23 +0000985void ARMBaseInstrInfo::
986reMaterialize(MachineBasicBlock &MBB,
987 MachineBasicBlock::iterator I,
988 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +0000989 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000990 const TargetRegisterInfo &TRI) const {
Evan Chengfdc83402009-11-08 00:15:23 +0000991 unsigned Opcode = Orig->getOpcode();
992 switch (Opcode) {
993 default: {
994 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000995 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfdc83402009-11-08 00:15:23 +0000996 MBB.insert(I, MI);
997 break;
998 }
999 case ARM::tLDRpci_pic:
1000 case ARM::t2LDRpci_pic: {
1001 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +00001002 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001003 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +00001004 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1005 DestReg)
1006 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1007 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1008 break;
1009 }
1010 }
Evan Chengfdc83402009-11-08 00:15:23 +00001011}
1012
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001013MachineInstr *
1014ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1015 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1016 switch(Orig->getOpcode()) {
1017 case ARM::tLDRpci_pic:
1018 case ARM::t2LDRpci_pic: {
1019 unsigned CPI = Orig->getOperand(1).getIndex();
1020 unsigned PCLabelId = duplicateCPV(MF, CPI);
1021 Orig->getOperand(1).setIndex(CPI);
1022 Orig->getOperand(2).setImm(PCLabelId);
1023 break;
1024 }
1025 }
1026 return MI;
1027}
1028
Evan Cheng506049f2010-03-03 01:44:33 +00001029bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1030 const MachineInstr *MI1) const {
Evan Chengd457e6e2009-11-07 04:04:34 +00001031 int Opcode = MI0->getOpcode();
Evan Cheng9b824252009-11-20 02:10:27 +00001032 if (Opcode == ARM::t2LDRpci ||
1033 Opcode == ARM::t2LDRpci_pic ||
1034 Opcode == ARM::tLDRpci ||
1035 Opcode == ARM::tLDRpci_pic) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001036 if (MI1->getOpcode() != Opcode)
1037 return false;
1038 if (MI0->getNumOperands() != MI1->getNumOperands())
1039 return false;
1040
1041 const MachineOperand &MO0 = MI0->getOperand(1);
1042 const MachineOperand &MO1 = MI1->getOperand(1);
1043 if (MO0.getOffset() != MO1.getOffset())
1044 return false;
1045
1046 const MachineFunction *MF = MI0->getParent()->getParent();
1047 const MachineConstantPool *MCP = MF->getConstantPool();
1048 int CPI0 = MO0.getIndex();
1049 int CPI1 = MO1.getIndex();
1050 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1051 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1052 ARMConstantPoolValue *ACPV0 =
1053 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1054 ARMConstantPoolValue *ACPV1 =
1055 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1056 return ACPV0->hasSameValue(ACPV1);
1057 }
1058
Evan Cheng506049f2010-03-03 01:44:33 +00001059 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001060}
1061
Bill Wendling4b722102010-06-23 23:00:16 +00001062/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1063/// determine if two loads are loading from the same base address. It should
1064/// only return true if the base pointers are the same and the only differences
1065/// between the two addresses is the offset. It also returns the offsets by
1066/// reference.
1067bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1068 int64_t &Offset1,
1069 int64_t &Offset2) const {
1070 // Don't worry about Thumb: just ARM and Thumb2.
1071 if (Subtarget.isThumb1Only()) return false;
1072
1073 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1074 return false;
1075
1076 switch (Load1->getMachineOpcode()) {
1077 default:
1078 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001079 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001080 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001081 case ARM::LDRD:
1082 case ARM::LDRH:
1083 case ARM::LDRSB:
1084 case ARM::LDRSH:
1085 case ARM::VLDRD:
1086 case ARM::VLDRS:
1087 case ARM::t2LDRi8:
1088 case ARM::t2LDRDi8:
1089 case ARM::t2LDRSHi8:
1090 case ARM::t2LDRi12:
1091 case ARM::t2LDRSHi12:
1092 break;
1093 }
1094
1095 switch (Load2->getMachineOpcode()) {
1096 default:
1097 return false;
Jim Grosbach3e556122010-10-26 22:37:02 +00001098 case ARM::LDRi12:
Jim Grosbachc1d30212010-10-27 00:19:44 +00001099 case ARM::LDRBi12:
Bill Wendling4b722102010-06-23 23:00:16 +00001100 case ARM::LDRD:
1101 case ARM::LDRH:
1102 case ARM::LDRSB:
1103 case ARM::LDRSH:
1104 case ARM::VLDRD:
1105 case ARM::VLDRS:
1106 case ARM::t2LDRi8:
1107 case ARM::t2LDRDi8:
1108 case ARM::t2LDRSHi8:
1109 case ARM::t2LDRi12:
1110 case ARM::t2LDRSHi12:
1111 break;
1112 }
1113
1114 // Check if base addresses and chain operands match.
1115 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1116 Load1->getOperand(4) != Load2->getOperand(4))
1117 return false;
1118
1119 // Index should be Reg0.
1120 if (Load1->getOperand(3) != Load2->getOperand(3))
1121 return false;
1122
1123 // Determine the offsets.
1124 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1125 isa<ConstantSDNode>(Load2->getOperand(1))) {
1126 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1127 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1128 return true;
1129 }
1130
1131 return false;
1132}
1133
1134/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1135/// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1136/// be scheduled togther. On some targets if two loads are loading from
1137/// addresses in the same cache line, it's better if they are scheduled
1138/// together. This function takes two integers that represent the load offsets
1139/// from the common base address. It returns true if it decides it's desirable
1140/// to schedule the two loads together. "NumLoads" is the number of loads that
1141/// have already been scheduled after Load1.
1142bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1143 int64_t Offset1, int64_t Offset2,
1144 unsigned NumLoads) const {
1145 // Don't worry about Thumb: just ARM and Thumb2.
1146 if (Subtarget.isThumb1Only()) return false;
1147
1148 assert(Offset2 > Offset1);
1149
1150 if ((Offset2 - Offset1) / 8 > 64)
1151 return false;
1152
1153 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1154 return false; // FIXME: overly conservative?
1155
1156 // Four loads in a row should be sufficient.
1157 if (NumLoads >= 3)
1158 return false;
1159
1160 return true;
1161}
1162
Evan Cheng86050dc2010-06-18 23:09:54 +00001163bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1164 const MachineBasicBlock *MBB,
1165 const MachineFunction &MF) const {
Jim Grosbach57bb3942010-06-25 18:43:14 +00001166 // Debug info is never a scheduling boundary. It's necessary to be explicit
1167 // due to the special treatment of IT instructions below, otherwise a
1168 // dbg_value followed by an IT will result in the IT instruction being
1169 // considered a scheduling hazard, which is wrong. It should be the actual
1170 // instruction preceding the dbg_value instruction(s), just like it is
1171 // when debug info is not present.
1172 if (MI->isDebugValue())
1173 return false;
1174
Evan Cheng86050dc2010-06-18 23:09:54 +00001175 // Terminators and labels can't be scheduled around.
1176 if (MI->getDesc().isTerminator() || MI->isLabel())
1177 return true;
1178
1179 // Treat the start of the IT block as a scheduling boundary, but schedule
1180 // t2IT along with all instructions following it.
1181 // FIXME: This is a big hammer. But the alternative is to add all potential
1182 // true and anti dependencies to IT block instructions as implicit operands
1183 // to the t2IT instruction. The added compile time and complexity does not
1184 // seem worth it.
1185 MachineBasicBlock::const_iterator I = MI;
Jim Grosbach57bb3942010-06-25 18:43:14 +00001186 // Make sure to skip any dbg_value instructions
1187 while (++I != MBB->end() && I->isDebugValue())
1188 ;
1189 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng86050dc2010-06-18 23:09:54 +00001190 return true;
1191
1192 // Don't attempt to schedule around any instruction that defines
1193 // a stack-oriented pointer, as it's unlikely to be profitable. This
1194 // saves compile time, because it doesn't require every single
1195 // stack slot reference to depend on the instruction that does the
1196 // modification.
1197 if (MI->definesRegister(ARM::SP))
1198 return true;
1199
1200 return false;
1201}
1202
Owen Andersonb20b8512010-09-28 18:32:13 +00001203bool ARMBaseInstrInfo::isProfitableToIfCvt(MachineBasicBlock &MBB,
Evan Cheng8239daf2010-11-03 00:45:17 +00001204 unsigned NumCyles,
1205 unsigned ExtraPredCycles,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001206 float Probability,
1207 float Confidence) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00001208 if (!NumCyles)
Evan Cheng13151432010-06-25 22:42:03 +00001209 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001210
Owen Andersonb20b8512010-09-28 18:32:13 +00001211 // Attempt to estimate the relative costs of predication versus branching.
Evan Cheng8239daf2010-11-03 00:45:17 +00001212 float UnpredCost = Probability * NumCyles;
Owen Anderson654d5442010-09-28 21:57:50 +00001213 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001214 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001215
Evan Cheng8239daf2010-11-03 00:45:17 +00001216 return (float)(NumCyles + ExtraPredCycles) < UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001217}
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001218
Evan Cheng13151432010-06-25 22:42:03 +00001219bool ARMBaseInstrInfo::
Evan Cheng8239daf2010-11-03 00:45:17 +00001220isProfitableToIfCvt(MachineBasicBlock &TMBB,
1221 unsigned TCycles, unsigned TExtra,
1222 MachineBasicBlock &FMBB,
1223 unsigned FCycles, unsigned FExtra,
Owen Andersone3cc84a2010-10-01 22:45:50 +00001224 float Probability, float Confidence) const {
Evan Cheng8239daf2010-11-03 00:45:17 +00001225 if (!TCycles || !FCycles)
Owen Andersonb20b8512010-09-28 18:32:13 +00001226 return false;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001227
Owen Andersonb20b8512010-09-28 18:32:13 +00001228 // Attempt to estimate the relative costs of predication versus branching.
Evan Cheng8239daf2010-11-03 00:45:17 +00001229 float UnpredCost = Probability * TCycles + (1.0 - Probability) * FCycles;
Owen Anderson654d5442010-09-28 21:57:50 +00001230 UnpredCost += 1.0; // The branch itself
Owen Andersone3cc84a2010-10-01 22:45:50 +00001231 UnpredCost += (1.0 - Confidence) * Subtarget.getMispredictionPenalty();
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001232
Evan Cheng8239daf2010-11-03 00:45:17 +00001233 return (float)(TCycles + FCycles + TExtra + FExtra) < UnpredCost;
Evan Cheng13151432010-06-25 22:42:03 +00001234}
1235
Evan Cheng8fb90362009-08-08 03:20:32 +00001236/// getInstrPredicate - If instruction is predicated, returns its predicate
1237/// condition, otherwise returns AL. It also returns the condition code
1238/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001239ARMCC::CondCodes
1240llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001241 int PIdx = MI->findFirstPredOperandIdx();
1242 if (PIdx == -1) {
1243 PredReg = 0;
1244 return ARMCC::AL;
1245 }
1246
1247 PredReg = MI->getOperand(PIdx+1).getReg();
1248 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1249}
1250
1251
Evan Cheng6495f632009-07-28 05:48:47 +00001252int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001253 if (Opc == ARM::B)
1254 return ARM::Bcc;
1255 else if (Opc == ARM::tB)
1256 return ARM::tBcc;
1257 else if (Opc == ARM::t2B)
1258 return ARM::t2Bcc;
1259
1260 llvm_unreachable("Unknown unconditional branch opcode!");
1261 return 0;
1262}
1263
Evan Cheng6495f632009-07-28 05:48:47 +00001264
1265void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1266 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1267 unsigned DestReg, unsigned BaseReg, int NumBytes,
1268 ARMCC::CondCodes Pred, unsigned PredReg,
1269 const ARMBaseInstrInfo &TII) {
1270 bool isSub = NumBytes < 0;
1271 if (isSub) NumBytes = -NumBytes;
1272
1273 while (NumBytes) {
1274 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1275 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1276 assert(ThisVal && "Didn't extract field correctly");
1277
1278 // We will handle these bits from offset, clear them.
1279 NumBytes &= ~ThisVal;
1280
1281 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1282
1283 // Build the new ADD / SUB.
1284 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1285 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1286 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1287 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1288 BaseReg = DestReg;
1289 }
1290}
1291
Evan Chengcdbb3f52009-08-27 01:23:50 +00001292bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1293 unsigned FrameReg, int &Offset,
1294 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001295 unsigned Opcode = MI.getOpcode();
1296 const TargetInstrDesc &Desc = MI.getDesc();
1297 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1298 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001299
Evan Cheng6495f632009-07-28 05:48:47 +00001300 // Memory operands in inline assembly always use AddrMode2.
1301 if (Opcode == ARM::INLINEASM)
1302 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001303
Evan Cheng6495f632009-07-28 05:48:47 +00001304 if (Opcode == ARM::ADDri) {
1305 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1306 if (Offset == 0) {
1307 // Turn it into a move.
1308 MI.setDesc(TII.get(ARM::MOVr));
1309 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1310 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001311 Offset = 0;
1312 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001313 } else if (Offset < 0) {
1314 Offset = -Offset;
1315 isSub = true;
1316 MI.setDesc(TII.get(ARM::SUBri));
1317 }
1318
1319 // Common case: small offset, fits into instruction.
1320 if (ARM_AM::getSOImmVal(Offset) != -1) {
1321 // Replace the FrameIndex with sp / fp
1322 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1323 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001324 Offset = 0;
1325 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001326 }
1327
1328 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1329 // as possible.
1330 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1331 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1332
1333 // We will handle these bits from offset, clear them.
1334 Offset &= ~ThisImmVal;
1335
1336 // Get the properly encoded SOImmVal field.
1337 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1338 "Bit extraction didn't work?");
1339 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1340 } else {
1341 unsigned ImmIdx = 0;
1342 int InstrOffs = 0;
1343 unsigned NumBits = 0;
1344 unsigned Scale = 1;
1345 switch (AddrMode) {
Jim Grosbach3e556122010-10-26 22:37:02 +00001346 case ARMII::AddrMode_i12: {
1347 ImmIdx = FrameRegIdx + 1;
1348 InstrOffs = MI.getOperand(ImmIdx).getImm();
1349 NumBits = 12;
1350 break;
1351 }
Evan Cheng6495f632009-07-28 05:48:47 +00001352 case ARMII::AddrMode2: {
1353 ImmIdx = FrameRegIdx+2;
1354 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1355 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1356 InstrOffs *= -1;
1357 NumBits = 12;
1358 break;
1359 }
1360 case ARMII::AddrMode3: {
1361 ImmIdx = FrameRegIdx+2;
1362 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1363 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1364 InstrOffs *= -1;
1365 NumBits = 8;
1366 break;
1367 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001368 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001369 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001370 // Can't fold any offset even if it's zero.
1371 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001372 case ARMII::AddrMode5: {
1373 ImmIdx = FrameRegIdx+1;
1374 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1375 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1376 InstrOffs *= -1;
1377 NumBits = 8;
1378 Scale = 4;
1379 break;
1380 }
1381 default:
1382 llvm_unreachable("Unsupported addressing mode!");
1383 break;
1384 }
1385
1386 Offset += InstrOffs * Scale;
1387 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1388 if (Offset < 0) {
1389 Offset = -Offset;
1390 isSub = true;
1391 }
1392
1393 // Attempt to fold address comp. if opcode has offset bits
1394 if (NumBits > 0) {
1395 // Common case: small offset, fits into instruction.
1396 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1397 int ImmedOffset = Offset / Scale;
1398 unsigned Mask = (1 << NumBits) - 1;
1399 if ((unsigned)Offset <= Mask * Scale) {
1400 // Replace the FrameIndex with sp
1401 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
Jim Grosbach77aee8e2010-10-27 01:19:41 +00001402 // FIXME: When addrmode2 goes away, this will simplify (like the
1403 // T2 version), as the LDR.i12 versions don't need the encoding
1404 // tricks for the offset value.
1405 if (isSub) {
1406 if (AddrMode == ARMII::AddrMode_i12)
1407 ImmedOffset = -ImmedOffset;
1408 else
1409 ImmedOffset |= 1 << NumBits;
1410 }
Evan Cheng6495f632009-07-28 05:48:47 +00001411 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001412 Offset = 0;
1413 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001414 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001415
Evan Cheng6495f632009-07-28 05:48:47 +00001416 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1417 ImmedOffset = ImmedOffset & Mask;
Jim Grosbach063efbf2010-10-27 16:50:31 +00001418 if (isSub) {
1419 if (AddrMode == ARMII::AddrMode_i12)
1420 ImmedOffset = -ImmedOffset;
1421 else
1422 ImmedOffset |= 1 << NumBits;
1423 }
Evan Cheng6495f632009-07-28 05:48:47 +00001424 ImmOp.ChangeToImmediate(ImmedOffset);
1425 Offset &= ~(Mask*Scale);
1426 }
1427 }
1428
Evan Chengcdbb3f52009-08-27 01:23:50 +00001429 Offset = (isSub) ? -Offset : Offset;
1430 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001431}
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001432
1433bool ARMBaseInstrInfo::
Eric Christophera99c3e92010-09-28 04:18:29 +00001434AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpMask,
1435 int &CmpValue) const {
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001436 switch (MI->getOpcode()) {
1437 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001438 case ARM::CMPri:
1439 case ARM::CMPzri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001440 case ARM::t2CMPri:
1441 case ARM::t2CMPzri:
1442 SrcReg = MI->getOperand(0).getReg();
Gabor Greif04ac81d2010-09-21 12:01:15 +00001443 CmpMask = ~0;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001444 CmpValue = MI->getOperand(1).getImm();
1445 return true;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001446 case ARM::TSTri:
1447 case ARM::t2TSTri:
1448 SrcReg = MI->getOperand(0).getReg();
1449 CmpMask = MI->getOperand(1).getImm();
1450 CmpValue = 0;
1451 return true;
1452 }
1453
1454 return false;
1455}
1456
Gabor Greif05642a32010-09-29 10:12:08 +00001457/// isSuitableForMask - Identify a suitable 'and' instruction that
1458/// operates on the given source register and applies the same mask
1459/// as a 'tst' instruction. Provide a limited look-through for copies.
1460/// When successful, MI will hold the found instruction.
1461static bool isSuitableForMask(MachineInstr *&MI, unsigned SrcReg,
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001462 int CmpMask, bool CommonUse) {
Gabor Greif05642a32010-09-29 10:12:08 +00001463 switch (MI->getOpcode()) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001464 case ARM::ANDri:
1465 case ARM::t2ANDri:
Gabor Greif05642a32010-09-29 10:12:08 +00001466 if (CmpMask != MI->getOperand(2).getImm())
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001467 return false;
Gabor Greif05642a32010-09-29 10:12:08 +00001468 if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
Gabor Greif04ac81d2010-09-21 12:01:15 +00001469 return true;
1470 break;
Gabor Greif05642a32010-09-29 10:12:08 +00001471 case ARM::COPY: {
1472 // Walk down one instruction which is potentially an 'and'.
1473 const MachineInstr &Copy = *MI;
Michael J. Spencerf000a7a2010-10-05 06:00:43 +00001474 MachineBasicBlock::iterator AND(
1475 llvm::next(MachineBasicBlock::iterator(MI)));
Gabor Greif05642a32010-09-29 10:12:08 +00001476 if (AND == MI->getParent()->end()) return false;
1477 MI = AND;
1478 return isSuitableForMask(MI, Copy.getOperand(0).getReg(),
1479 CmpMask, true);
1480 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001481 }
1482
1483 return false;
1484}
1485
Bill Wendlinga6556862010-09-11 00:13:50 +00001486/// OptimizeCompareInstr - Convert the instruction supplying the argument to the
Evan Chengeb96a2f2010-11-15 21:20:45 +00001487/// comparison into one that sets the zero bit in the flags register.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001488bool ARMBaseInstrInfo::
Gabor Greif04ac81d2010-09-21 12:01:15 +00001489OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
Evan Chengeb96a2f2010-11-15 21:20:45 +00001490 int CmpValue, const MachineRegisterInfo *MRI) const {
Bill Wendling36656612010-09-10 23:46:12 +00001491 if (CmpValue != 0)
Bill Wendling92ad57f2010-09-10 23:34:19 +00001492 return false;
1493
Bill Wendlingb41ee962010-10-18 21:22:31 +00001494 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1495 if (llvm::next(DI) != MRI->def_end())
Bill Wendling92ad57f2010-09-10 23:34:19 +00001496 // Only support one definition.
1497 return false;
1498
1499 MachineInstr *MI = &*DI;
1500
Gabor Greif04ac81d2010-09-21 12:01:15 +00001501 // Masked compares sometimes use the same register as the corresponding 'and'.
1502 if (CmpMask != ~0) {
Gabor Greif05642a32010-09-29 10:12:08 +00001503 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001504 MI = 0;
Bill Wendlingb41ee962010-10-18 21:22:31 +00001505 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1506 UE = MRI->use_end(); UI != UE; ++UI) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001507 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001508 MachineInstr *PotentialAND = &*UI;
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001509 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
Gabor Greif04ac81d2010-09-21 12:01:15 +00001510 continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001511 MI = PotentialAND;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001512 break;
1513 }
1514 if (!MI) return false;
1515 }
1516 }
1517
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001518 // Conservatively refuse to convert an instruction which isn't in the same BB
1519 // as the comparison.
1520 if (MI->getParent() != CmpInstr->getParent())
1521 return false;
1522
1523 // Check that CPSR isn't set between the comparison instruction and the one we
1524 // want to change.
Evan Cheng691e64a2010-09-21 23:49:07 +00001525 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1526 B = MI->getParent()->begin();
Bill Wendling0aa38b92010-10-09 00:03:48 +00001527
1528 // Early exit if CmpInstr is at the beginning of the BB.
1529 if (I == B) return false;
1530
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001531 --I;
1532 for (; I != E; --I) {
1533 const MachineInstr &Instr = *I;
1534
1535 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1536 const MachineOperand &MO = Instr.getOperand(IO);
Bill Wendling40a5eb12010-11-01 20:41:43 +00001537 if (!MO.isReg()) continue;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001538
Bill Wendling40a5eb12010-11-01 20:41:43 +00001539 // This instruction modifies or uses CPSR after the one we want to
1540 // change. We can't do this transformation.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001541 if (MO.getReg() == ARM::CPSR)
1542 return false;
1543 }
Evan Cheng691e64a2010-09-21 23:49:07 +00001544
1545 if (I == B)
1546 // The 'and' is below the comparison instruction.
1547 return false;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001548 }
1549
1550 // Set the "zero" bit in CPSR.
1551 switch (MI->getOpcode()) {
1552 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001553 case ARM::ADDri:
Bob Wilson3a951822010-09-15 17:12:08 +00001554 case ARM::ANDri:
1555 case ARM::t2ANDri:
Bill Wendling38ae9972010-08-11 00:23:00 +00001556 case ARM::SUBri:
1557 case ARM::t2ADDri:
Bill Wendlingad422712010-08-18 21:32:07 +00001558 case ARM::t2SUBri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001559 MI->RemoveOperand(5);
Bill Wendlingad422712010-08-18 21:32:07 +00001560 MachineInstrBuilder(MI)
1561 .addReg(ARM::CPSR, RegState::Define | RegState::Implicit);
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001562 CmpInstr->eraseFromParent();
1563 return true;
1564 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001565
1566 return false;
1567}
Evan Cheng5f54ce32010-09-09 18:18:55 +00001568
1569unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +00001570ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1571 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001572 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +00001573 return 1;
1574
1575 const TargetInstrDesc &Desc = MI->getDesc();
1576 unsigned Class = Desc.getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +00001577 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +00001578 if (UOps)
1579 return UOps;
1580
1581 unsigned Opc = MI->getOpcode();
1582 switch (Opc) {
1583 default:
1584 llvm_unreachable("Unexpected multi-uops instruction!");
1585 break;
Evan Cheng3ef1c872010-09-10 01:29:16 +00001586 case ARM::VLDMQ:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001587 case ARM::VSTMQ:
1588 return 2;
1589
1590 // The number of uOps for load / store multiple are determined by the number
1591 // registers.
Evan Cheng3ef1c872010-09-10 01:29:16 +00001592 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1593 // same cycle. The scheduling for the first load / store must be done
1594 // separately by assuming the the address is not 64-bit aligned.
1595 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
1596 // is not 64-bit aligned, then AGU would take an extra cycle.
1597 // For VFP / NEON load / store multiple, the formula is
Evan Cheng5f54ce32010-09-09 18:18:55 +00001598 // (#reg / 2) + (#reg % 2) + 1.
Evan Cheng5f54ce32010-09-09 18:18:55 +00001599 case ARM::VLDMD:
1600 case ARM::VLDMS:
1601 case ARM::VLDMD_UPD:
1602 case ARM::VLDMS_UPD:
1603 case ARM::VSTMD:
1604 case ARM::VSTMS:
1605 case ARM::VSTMD_UPD:
1606 case ARM::VSTMS_UPD: {
1607 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1608 return (NumRegs / 2) + (NumRegs % 2) + 1;
1609 }
1610 case ARM::LDM_RET:
1611 case ARM::LDM:
1612 case ARM::LDM_UPD:
1613 case ARM::STM:
1614 case ARM::STM_UPD:
1615 case ARM::tLDM:
1616 case ARM::tLDM_UPD:
1617 case ARM::tSTM_UPD:
1618 case ARM::tPOP_RET:
1619 case ARM::tPOP:
1620 case ARM::tPUSH:
1621 case ARM::t2LDM_RET:
1622 case ARM::t2LDM:
1623 case ARM::t2LDM_UPD:
1624 case ARM::t2STM:
1625 case ARM::t2STM_UPD: {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001626 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1627 if (Subtarget.isCortexA8()) {
Evan Cheng8239daf2010-11-03 00:45:17 +00001628 if (NumRegs < 4)
1629 return 2;
1630 // 4 registers would be issued: 2, 2.
1631 // 5 registers would be issued: 2, 2, 1.
1632 UOps = (NumRegs / 2);
1633 if (NumRegs % 2)
1634 ++UOps;
1635 return UOps;
Evan Cheng3ef1c872010-09-10 01:29:16 +00001636 } 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.
Evan Cheng9e08ee52010-10-28 02:00:25 +00001807 int DefCycle = -1;
Evan Cheng7e2fe912010-10-28 06:47:08 +00001808 bool LdmBypass = false;
Evan Chenga0792de2010-10-06 06:27:31 +00001809 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
Evan Chengdd9dd6f2010-10-23 02:04:38 +00001895
Evan Chenga0792de2010-10-06 06:27:31 +00001896 const TargetInstrDesc &UseTID = UseMI->getDesc();
Evan Chengdd9dd6f2010-10-23 02:04:38 +00001897 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
Evan Chenge09206d2010-10-29 23:16:55 +00001898 if (DefMO.getReg() == ARM::CPSR) {
1899 if (DefMI->getOpcode() == ARM::FMSTAT) {
1900 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
1901 return Subtarget.isCortexA9() ? 1 : 20;
1902 }
1903
Evan Chengdd9dd6f2010-10-23 02:04:38 +00001904 // CPSR set and branch can be paired in the same cycle.
Evan Chenge09206d2010-10-29 23:16:55 +00001905 if (UseTID.isBranch())
1906 return 0;
1907 }
Evan Chengdd9dd6f2010-10-23 02:04:38 +00001908
Evan Chenga0792de2010-10-06 06:27:31 +00001909 unsigned DefAlign = DefMI->hasOneMemOperand()
1910 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
1911 unsigned UseAlign = UseMI->hasOneMemOperand()
1912 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00001913 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
1914 UseTID, UseIdx, UseAlign);
1915
1916 if (Latency > 1 &&
1917 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
1918 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
1919 // variants are one cycle cheaper.
1920 switch (DefTID.getOpcode()) {
1921 default: break;
1922 case ARM::LDRrs:
1923 case ARM::LDRBrs: {
1924 unsigned ShOpVal = DefMI->getOperand(3).getImm();
1925 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
1926 if (ShImm == 0 ||
1927 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
1928 --Latency;
1929 break;
1930 }
1931 case ARM::t2LDRs:
1932 case ARM::t2LDRBs:
1933 case ARM::t2LDRHs:
1934 case ARM::t2LDRSHs: {
1935 // Thumb2 mode: lsl only.
1936 unsigned ShAmt = DefMI->getOperand(3).getImm();
1937 if (ShAmt == 0 || ShAmt == 2)
1938 --Latency;
1939 break;
1940 }
1941 }
1942 }
1943
1944 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00001945}
1946
1947int
1948ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1949 SDNode *DefNode, unsigned DefIdx,
1950 SDNode *UseNode, unsigned UseIdx) const {
1951 if (!DefNode->isMachineOpcode())
1952 return 1;
1953
1954 const TargetInstrDesc &DefTID = get(DefNode->getMachineOpcode());
1955 if (!ItinData || ItinData->isEmpty())
1956 return DefTID.mayLoad() ? 3 : 1;
1957
Evan Cheng08975152010-10-29 18:09:28 +00001958 if (!UseNode->isMachineOpcode()) {
1959 int Latency = ItinData->getOperandCycle(DefTID.getSchedClass(), DefIdx);
1960 if (Subtarget.isCortexA9())
1961 return Latency <= 2 ? 1 : Latency - 1;
1962 else
1963 return Latency <= 3 ? 1 : Latency - 2;
1964 }
Evan Chenga0792de2010-10-06 06:27:31 +00001965
1966 const TargetInstrDesc &UseTID = get(UseNode->getMachineOpcode());
1967 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
1968 unsigned DefAlign = !DefMN->memoperands_empty()
1969 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
1970 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
1971 unsigned UseAlign = !UseMN->memoperands_empty()
1972 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00001973 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
1974 UseTID, UseIdx, UseAlign);
1975
1976 if (Latency > 1 &&
1977 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
1978 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
1979 // variants are one cycle cheaper.
1980 switch (DefTID.getOpcode()) {
1981 default: break;
1982 case ARM::LDRrs:
1983 case ARM::LDRBrs: {
1984 unsigned ShOpVal =
1985 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
1986 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
1987 if (ShImm == 0 ||
1988 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
1989 --Latency;
1990 break;
1991 }
1992 case ARM::t2LDRs:
1993 case ARM::t2LDRBs:
1994 case ARM::t2LDRHs:
1995 case ARM::t2LDRSHs: {
1996 // Thumb2 mode: lsl only.
1997 unsigned ShAmt =
1998 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
1999 if (ShAmt == 0 || ShAmt == 2)
2000 --Latency;
2001 break;
2002 }
2003 }
2004 }
2005
2006 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00002007}
Evan Cheng23128422010-10-19 18:58:51 +00002008
Evan Cheng8239daf2010-11-03 00:45:17 +00002009int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2010 const MachineInstr *MI,
2011 unsigned *PredCost) const {
2012 if (MI->isCopyLike() || MI->isInsertSubreg() ||
2013 MI->isRegSequence() || MI->isImplicitDef())
2014 return 1;
2015
2016 if (!ItinData || ItinData->isEmpty())
2017 return 1;
2018
2019 const TargetInstrDesc &TID = MI->getDesc();
2020 unsigned Class = TID.getSchedClass();
2021 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
2022 if (PredCost && TID.hasImplicitDefOfPhysReg(ARM::CPSR))
2023 // When predicated, CPSR is an additional source operand for CPSR updating
2024 // instructions, this apparently increases their latencies.
2025 *PredCost = 1;
2026 if (UOps)
2027 return ItinData->getStageLatency(Class);
2028 return getNumMicroOps(ItinData, MI);
2029}
2030
2031int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2032 SDNode *Node) const {
2033 if (!Node->isMachineOpcode())
2034 return 1;
2035
2036 if (!ItinData || ItinData->isEmpty())
2037 return 1;
2038
2039 unsigned Opcode = Node->getMachineOpcode();
2040 switch (Opcode) {
2041 default:
2042 return ItinData->getStageLatency(get(Opcode).getSchedClass());
2043 case ARM::VLDMQ:
2044 case ARM::VSTMQ:
2045 return 2;
Eric Christopher6c501192010-11-11 19:47:02 +00002046 }
Evan Cheng8239daf2010-11-03 00:45:17 +00002047}
2048
Evan Cheng23128422010-10-19 18:58:51 +00002049bool ARMBaseInstrInfo::
2050hasHighOperandLatency(const InstrItineraryData *ItinData,
2051 const MachineRegisterInfo *MRI,
2052 const MachineInstr *DefMI, unsigned DefIdx,
2053 const MachineInstr *UseMI, unsigned UseIdx) const {
2054 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2055 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2056 if (Subtarget.isCortexA8() &&
2057 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2058 // CortexA8 VFP instructions are not pipelined.
2059 return true;
2060
2061 // Hoist VFP / NEON instructions with 4 or higher latency.
2062 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2063 if (Latency <= 3)
2064 return false;
2065 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2066 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2067}
Evan Chengc8141df2010-10-26 02:08:50 +00002068
2069bool ARMBaseInstrInfo::
2070hasLowDefLatency(const InstrItineraryData *ItinData,
2071 const MachineInstr *DefMI, unsigned DefIdx) const {
2072 if (!ItinData || ItinData->isEmpty())
2073 return false;
2074
2075 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2076 if (DDomain == ARMII::DomainGeneral) {
2077 unsigned DefClass = DefMI->getDesc().getSchedClass();
2078 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2079 return (DefCycle != -1 && DefCycle <= 2);
2080 }
2081 return false;
2082}