blob: b023379e7ba8c8ab877fa3ee615bac24b1b79718 [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
211 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
212 unsigned Reg = CSI[i].getReg();
213 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
228 // Insert the spill to the stack frame. The register is killed at the spill
Michael J. Spencer2bbb7692010-10-05 06:00:33 +0000229 //
Rafael Espindola42d075c2010-06-02 20:02:30 +0000230 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000231 storeRegToStackSlot(MBB, MI, Reg, isKill,
Rafael Espindola42d075c2010-06-02 20:02:30 +0000232 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
Bill Wendling92ad57f2010-09-10 23:34:19 +00001487/// comparison into one that sets the zero bit in the flags register. Update the
1488/// iterator *only* if a transformation took place.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001489bool ARMBaseInstrInfo::
Gabor Greif04ac81d2010-09-21 12:01:15 +00001490OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpMask,
Bill Wendlingb41ee962010-10-18 21:22:31 +00001491 int CmpValue, const MachineRegisterInfo *MRI,
1492 MachineBasicBlock::iterator &MII) const {
Bill Wendling36656612010-09-10 23:46:12 +00001493 if (CmpValue != 0)
Bill Wendling92ad57f2010-09-10 23:34:19 +00001494 return false;
1495
Bill Wendlingb41ee962010-10-18 21:22:31 +00001496 MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
1497 if (llvm::next(DI) != MRI->def_end())
Bill Wendling92ad57f2010-09-10 23:34:19 +00001498 // Only support one definition.
1499 return false;
1500
1501 MachineInstr *MI = &*DI;
1502
Gabor Greif04ac81d2010-09-21 12:01:15 +00001503 // Masked compares sometimes use the same register as the corresponding 'and'.
1504 if (CmpMask != ~0) {
Gabor Greif05642a32010-09-29 10:12:08 +00001505 if (!isSuitableForMask(MI, SrcReg, CmpMask, false)) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001506 MI = 0;
Bill Wendlingb41ee962010-10-18 21:22:31 +00001507 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SrcReg),
1508 UE = MRI->use_end(); UI != UE; ++UI) {
Gabor Greif04ac81d2010-09-21 12:01:15 +00001509 if (UI->getParent() != CmpInstr->getParent()) continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001510 MachineInstr *PotentialAND = &*UI;
Gabor Greif8ff9bb12010-09-21 13:30:57 +00001511 if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true))
Gabor Greif04ac81d2010-09-21 12:01:15 +00001512 continue;
Gabor Greif05642a32010-09-29 10:12:08 +00001513 MI = PotentialAND;
Gabor Greif04ac81d2010-09-21 12:01:15 +00001514 break;
1515 }
1516 if (!MI) return false;
1517 }
1518 }
1519
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001520 // Conservatively refuse to convert an instruction which isn't in the same BB
1521 // as the comparison.
1522 if (MI->getParent() != CmpInstr->getParent())
1523 return false;
1524
1525 // Check that CPSR isn't set between the comparison instruction and the one we
1526 // want to change.
Evan Cheng691e64a2010-09-21 23:49:07 +00001527 MachineBasicBlock::const_iterator I = CmpInstr, E = MI,
1528 B = MI->getParent()->begin();
Bill Wendling0aa38b92010-10-09 00:03:48 +00001529
1530 // Early exit if CmpInstr is at the beginning of the BB.
1531 if (I == B) return false;
1532
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001533 --I;
1534 for (; I != E; --I) {
1535 const MachineInstr &Instr = *I;
1536
1537 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1538 const MachineOperand &MO = Instr.getOperand(IO);
Bill Wendling40a5eb12010-11-01 20:41:43 +00001539 if (!MO.isReg()) continue;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001540
Bill Wendling40a5eb12010-11-01 20:41:43 +00001541 // This instruction modifies or uses CPSR after the one we want to
1542 // change. We can't do this transformation.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001543 if (MO.getReg() == ARM::CPSR)
1544 return false;
1545 }
Evan Cheng691e64a2010-09-21 23:49:07 +00001546
1547 if (I == B)
1548 // The 'and' is below the comparison instruction.
1549 return false;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001550 }
1551
1552 // Set the "zero" bit in CPSR.
1553 switch (MI->getOpcode()) {
1554 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001555 case ARM::ADDri:
Bob Wilson3a951822010-09-15 17:12:08 +00001556 case ARM::ANDri:
1557 case ARM::t2ANDri:
Bill Wendling38ae9972010-08-11 00:23:00 +00001558 case ARM::SUBri:
1559 case ARM::t2ADDri:
Bill Wendlingad422712010-08-18 21:32:07 +00001560 case ARM::t2SUBri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001561 MI->RemoveOperand(5);
Bill Wendlingad422712010-08-18 21:32:07 +00001562 MachineInstrBuilder(MI)
1563 .addReg(ARM::CPSR, RegState::Define | RegState::Implicit);
Bill Wendling220e2402010-09-10 21:55:43 +00001564 MII = llvm::next(MachineBasicBlock::iterator(CmpInstr));
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001565 CmpInstr->eraseFromParent();
1566 return true;
1567 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001568
1569 return false;
1570}
Evan Cheng5f54ce32010-09-09 18:18:55 +00001571
1572unsigned
Evan Cheng8239daf2010-11-03 00:45:17 +00001573ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
1574 const MachineInstr *MI) const {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001575 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +00001576 return 1;
1577
1578 const TargetInstrDesc &Desc = MI->getDesc();
1579 unsigned Class = Desc.getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +00001580 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +00001581 if (UOps)
1582 return UOps;
1583
1584 unsigned Opc = MI->getOpcode();
1585 switch (Opc) {
1586 default:
1587 llvm_unreachable("Unexpected multi-uops instruction!");
1588 break;
Evan Cheng3ef1c872010-09-10 01:29:16 +00001589 case ARM::VLDMQ:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001590 case ARM::VSTMQ:
1591 return 2;
1592
1593 // The number of uOps for load / store multiple are determined by the number
1594 // registers.
Evan Cheng3ef1c872010-09-10 01:29:16 +00001595 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1596 // same cycle. The scheduling for the first load / store must be done
1597 // separately by assuming the the address is not 64-bit aligned.
1598 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
1599 // is not 64-bit aligned, then AGU would take an extra cycle.
1600 // For VFP / NEON load / store multiple, the formula is
Evan Cheng5f54ce32010-09-09 18:18:55 +00001601 // (#reg / 2) + (#reg % 2) + 1.
Evan Cheng5f54ce32010-09-09 18:18:55 +00001602 case ARM::VLDMD:
1603 case ARM::VLDMS:
1604 case ARM::VLDMD_UPD:
1605 case ARM::VLDMS_UPD:
1606 case ARM::VSTMD:
1607 case ARM::VSTMS:
1608 case ARM::VSTMD_UPD:
1609 case ARM::VSTMS_UPD: {
1610 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1611 return (NumRegs / 2) + (NumRegs % 2) + 1;
1612 }
1613 case ARM::LDM_RET:
1614 case ARM::LDM:
1615 case ARM::LDM_UPD:
1616 case ARM::STM:
1617 case ARM::STM_UPD:
1618 case ARM::tLDM:
1619 case ARM::tLDM_UPD:
1620 case ARM::tSTM_UPD:
1621 case ARM::tPOP_RET:
1622 case ARM::tPOP:
1623 case ARM::tPUSH:
1624 case ARM::t2LDM_RET:
1625 case ARM::t2LDM:
1626 case ARM::t2LDM_UPD:
1627 case ARM::t2STM:
1628 case ARM::t2STM_UPD: {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001629 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1630 if (Subtarget.isCortexA8()) {
Evan Cheng8239daf2010-11-03 00:45:17 +00001631 if (NumRegs < 4)
1632 return 2;
1633 // 4 registers would be issued: 2, 2.
1634 // 5 registers would be issued: 2, 2, 1.
1635 UOps = (NumRegs / 2);
1636 if (NumRegs % 2)
1637 ++UOps;
1638 return UOps;
Evan Cheng3ef1c872010-09-10 01:29:16 +00001639 } else if (Subtarget.isCortexA9()) {
1640 UOps = (NumRegs / 2);
1641 // If there are odd number of registers or if it's not 64-bit aligned,
1642 // then it takes an extra AGU (Address Generation Unit) cycle.
1643 if ((NumRegs % 2) ||
1644 !MI->hasOneMemOperand() ||
1645 (*MI->memoperands_begin())->getAlignment() < 8)
1646 ++UOps;
1647 return UOps;
1648 } else {
1649 // Assume the worst.
1650 return NumRegs;
Michael J. Spencer2bbb7692010-10-05 06:00:33 +00001651 }
Evan Cheng5f54ce32010-09-09 18:18:55 +00001652 }
1653 }
1654}
Evan Chenga0792de2010-10-06 06:27:31 +00001655
1656int
Evan Cheng344d9db2010-10-07 23:12:15 +00001657ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
1658 const TargetInstrDesc &DefTID,
1659 unsigned DefClass,
1660 unsigned DefIdx, unsigned DefAlign) const {
1661 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1662 if (RegNo <= 0)
1663 // Def is the address writeback.
1664 return ItinData->getOperandCycle(DefClass, DefIdx);
1665
1666 int DefCycle;
1667 if (Subtarget.isCortexA8()) {
1668 // (regno / 2) + (regno % 2) + 1
1669 DefCycle = RegNo / 2 + 1;
1670 if (RegNo % 2)
1671 ++DefCycle;
1672 } else if (Subtarget.isCortexA9()) {
1673 DefCycle = RegNo;
1674 bool isSLoad = false;
1675 switch (DefTID.getOpcode()) {
1676 default: break;
1677 case ARM::VLDMS:
1678 case ARM::VLDMS_UPD:
1679 isSLoad = true;
1680 break;
1681 }
1682 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1683 // then it takes an extra cycle.
1684 if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
1685 ++DefCycle;
1686 } else {
1687 // Assume the worst.
1688 DefCycle = RegNo + 2;
1689 }
1690
1691 return DefCycle;
1692}
1693
1694int
1695ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
1696 const TargetInstrDesc &DefTID,
1697 unsigned DefClass,
1698 unsigned DefIdx, unsigned DefAlign) const {
1699 int RegNo = (int)(DefIdx+1) - DefTID.getNumOperands() + 1;
1700 if (RegNo <= 0)
1701 // Def is the address writeback.
1702 return ItinData->getOperandCycle(DefClass, DefIdx);
1703
1704 int DefCycle;
1705 if (Subtarget.isCortexA8()) {
1706 // 4 registers would be issued: 1, 2, 1.
1707 // 5 registers would be issued: 1, 2, 2.
1708 DefCycle = RegNo / 2;
1709 if (DefCycle < 1)
1710 DefCycle = 1;
1711 // Result latency is issue cycle + 2: E2.
1712 DefCycle += 2;
1713 } else if (Subtarget.isCortexA9()) {
1714 DefCycle = (RegNo / 2);
1715 // If there are odd number of registers or if it's not 64-bit aligned,
1716 // then it takes an extra AGU (Address Generation Unit) cycle.
1717 if ((RegNo % 2) || DefAlign < 8)
1718 ++DefCycle;
1719 // Result latency is AGU cycles + 2.
1720 DefCycle += 2;
1721 } else {
1722 // Assume the worst.
1723 DefCycle = RegNo + 2;
1724 }
1725
1726 return DefCycle;
1727}
1728
1729int
1730ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
1731 const TargetInstrDesc &UseTID,
1732 unsigned UseClass,
1733 unsigned UseIdx, unsigned UseAlign) const {
1734 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1735 if (RegNo <= 0)
1736 return ItinData->getOperandCycle(UseClass, UseIdx);
1737
1738 int UseCycle;
1739 if (Subtarget.isCortexA8()) {
1740 // (regno / 2) + (regno % 2) + 1
1741 UseCycle = RegNo / 2 + 1;
1742 if (RegNo % 2)
1743 ++UseCycle;
1744 } else if (Subtarget.isCortexA9()) {
1745 UseCycle = RegNo;
1746 bool isSStore = false;
1747 switch (UseTID.getOpcode()) {
1748 default: break;
1749 case ARM::VSTMS:
1750 case ARM::VSTMS_UPD:
1751 isSStore = true;
1752 break;
1753 }
1754 // If there are odd number of 'S' registers or if it's not 64-bit aligned,
1755 // then it takes an extra cycle.
1756 if ((isSStore && (RegNo % 2)) || UseAlign < 8)
1757 ++UseCycle;
1758 } else {
1759 // Assume the worst.
1760 UseCycle = RegNo + 2;
1761 }
1762
1763 return UseCycle;
1764}
1765
1766int
1767ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
1768 const TargetInstrDesc &UseTID,
1769 unsigned UseClass,
1770 unsigned UseIdx, unsigned UseAlign) const {
1771 int RegNo = (int)(UseIdx+1) - UseTID.getNumOperands() + 1;
1772 if (RegNo <= 0)
1773 return ItinData->getOperandCycle(UseClass, UseIdx);
1774
1775 int UseCycle;
1776 if (Subtarget.isCortexA8()) {
1777 UseCycle = RegNo / 2;
1778 if (UseCycle < 2)
1779 UseCycle = 2;
1780 // Read in E3.
1781 UseCycle += 2;
1782 } else if (Subtarget.isCortexA9()) {
1783 UseCycle = (RegNo / 2);
1784 // If there are odd number of registers or if it's not 64-bit aligned,
1785 // then it takes an extra AGU (Address Generation Unit) cycle.
1786 if ((RegNo % 2) || UseAlign < 8)
1787 ++UseCycle;
1788 } else {
1789 // Assume the worst.
1790 UseCycle = 1;
1791 }
1792 return UseCycle;
1793}
1794
1795int
Evan Chenga0792de2010-10-06 06:27:31 +00001796ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1797 const TargetInstrDesc &DefTID,
1798 unsigned DefIdx, unsigned DefAlign,
1799 const TargetInstrDesc &UseTID,
1800 unsigned UseIdx, unsigned UseAlign) const {
1801 unsigned DefClass = DefTID.getSchedClass();
1802 unsigned UseClass = UseTID.getSchedClass();
1803
1804 if (DefIdx < DefTID.getNumDefs() && UseIdx < UseTID.getNumOperands())
1805 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
1806
1807 // This may be a def / use of a variable_ops instruction, the operand
1808 // latency might be determinable dynamically. Let the target try to
1809 // figure it out.
Evan Cheng9e08ee52010-10-28 02:00:25 +00001810 int DefCycle = -1;
Evan Cheng7e2fe912010-10-28 06:47:08 +00001811 bool LdmBypass = false;
Evan Chenga0792de2010-10-06 06:27:31 +00001812 switch (DefTID.getOpcode()) {
1813 default:
1814 DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
1815 break;
Evan Cheng5a50cee2010-10-07 01:50:48 +00001816 case ARM::VLDMD:
1817 case ARM::VLDMS:
1818 case ARM::VLDMD_UPD:
1819 case ARM::VLDMS_UPD: {
Evan Cheng344d9db2010-10-07 23:12:15 +00001820 DefCycle = getVLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00001821 break;
1822 }
Evan Chenga0792de2010-10-06 06:27:31 +00001823 case ARM::LDM_RET:
1824 case ARM::LDM:
1825 case ARM::LDM_UPD:
1826 case ARM::tLDM:
1827 case ARM::tLDM_UPD:
1828 case ARM::tPUSH:
1829 case ARM::t2LDM_RET:
1830 case ARM::t2LDM:
1831 case ARM::t2LDM_UPD: {
1832 LdmBypass = 1;
Evan Cheng344d9db2010-10-07 23:12:15 +00001833 DefCycle = getLDMDefCycle(ItinData, DefTID, DefClass, DefIdx, DefAlign);
1834 break;
Evan Chenga0792de2010-10-06 06:27:31 +00001835 }
1836 }
1837
1838 if (DefCycle == -1)
1839 // We can't seem to determine the result latency of the def, assume it's 2.
1840 DefCycle = 2;
1841
1842 int UseCycle = -1;
1843 switch (UseTID.getOpcode()) {
1844 default:
1845 UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
1846 break;
Evan Cheng5a50cee2010-10-07 01:50:48 +00001847 case ARM::VSTMD:
1848 case ARM::VSTMS:
1849 case ARM::VSTMD_UPD:
1850 case ARM::VSTMS_UPD: {
Evan Cheng344d9db2010-10-07 23:12:15 +00001851 UseCycle = getVSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00001852 break;
1853 }
Evan Chenga0792de2010-10-06 06:27:31 +00001854 case ARM::STM:
1855 case ARM::STM_UPD:
1856 case ARM::tSTM_UPD:
1857 case ARM::tPOP_RET:
1858 case ARM::tPOP:
1859 case ARM::t2STM:
1860 case ARM::t2STM_UPD: {
Evan Cheng344d9db2010-10-07 23:12:15 +00001861 UseCycle = getSTMUseCycle(ItinData, UseTID, UseClass, UseIdx, UseAlign);
Evan Cheng5a50cee2010-10-07 01:50:48 +00001862 break;
Evan Chenga0792de2010-10-06 06:27:31 +00001863 }
1864 }
1865
1866 if (UseCycle == -1)
1867 // Assume it's read in the first stage.
1868 UseCycle = 1;
1869
1870 UseCycle = DefCycle - UseCycle + 1;
1871 if (UseCycle > 0) {
1872 if (LdmBypass) {
1873 // It's a variable_ops instruction so we can't use DefIdx here. Just use
1874 // first def operand.
1875 if (ItinData->hasPipelineForwarding(DefClass, DefTID.getNumOperands()-1,
1876 UseClass, UseIdx))
1877 --UseCycle;
1878 } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
1879 UseClass, UseIdx))
1880 --UseCycle;
1881 }
1882
1883 return UseCycle;
1884}
1885
1886int
1887ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1888 const MachineInstr *DefMI, unsigned DefIdx,
1889 const MachineInstr *UseMI, unsigned UseIdx) const {
1890 if (DefMI->isCopyLike() || DefMI->isInsertSubreg() ||
1891 DefMI->isRegSequence() || DefMI->isImplicitDef())
1892 return 1;
1893
1894 const TargetInstrDesc &DefTID = DefMI->getDesc();
1895 if (!ItinData || ItinData->isEmpty())
1896 return DefTID.mayLoad() ? 3 : 1;
1897
Evan Chengdd9dd6f2010-10-23 02:04:38 +00001898
Evan Chenga0792de2010-10-06 06:27:31 +00001899 const TargetInstrDesc &UseTID = UseMI->getDesc();
Evan Chengdd9dd6f2010-10-23 02:04:38 +00001900 const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
Evan Chenge09206d2010-10-29 23:16:55 +00001901 if (DefMO.getReg() == ARM::CPSR) {
1902 if (DefMI->getOpcode() == ARM::FMSTAT) {
1903 // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
1904 return Subtarget.isCortexA9() ? 1 : 20;
1905 }
1906
Evan Chengdd9dd6f2010-10-23 02:04:38 +00001907 // CPSR set and branch can be paired in the same cycle.
Evan Chenge09206d2010-10-29 23:16:55 +00001908 if (UseTID.isBranch())
1909 return 0;
1910 }
Evan Chengdd9dd6f2010-10-23 02:04:38 +00001911
Evan Chenga0792de2010-10-06 06:27:31 +00001912 unsigned DefAlign = DefMI->hasOneMemOperand()
1913 ? (*DefMI->memoperands_begin())->getAlignment() : 0;
1914 unsigned UseAlign = UseMI->hasOneMemOperand()
1915 ? (*UseMI->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00001916 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
1917 UseTID, UseIdx, UseAlign);
1918
1919 if (Latency > 1 &&
1920 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
1921 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
1922 // variants are one cycle cheaper.
1923 switch (DefTID.getOpcode()) {
1924 default: break;
1925 case ARM::LDRrs:
1926 case ARM::LDRBrs: {
1927 unsigned ShOpVal = DefMI->getOperand(3).getImm();
1928 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
1929 if (ShImm == 0 ||
1930 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
1931 --Latency;
1932 break;
1933 }
1934 case ARM::t2LDRs:
1935 case ARM::t2LDRBs:
1936 case ARM::t2LDRHs:
1937 case ARM::t2LDRSHs: {
1938 // Thumb2 mode: lsl only.
1939 unsigned ShAmt = DefMI->getOperand(3).getImm();
1940 if (ShAmt == 0 || ShAmt == 2)
1941 --Latency;
1942 break;
1943 }
1944 }
1945 }
1946
1947 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00001948}
1949
1950int
1951ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
1952 SDNode *DefNode, unsigned DefIdx,
1953 SDNode *UseNode, unsigned UseIdx) const {
1954 if (!DefNode->isMachineOpcode())
1955 return 1;
1956
1957 const TargetInstrDesc &DefTID = get(DefNode->getMachineOpcode());
1958 if (!ItinData || ItinData->isEmpty())
1959 return DefTID.mayLoad() ? 3 : 1;
1960
Evan Cheng08975152010-10-29 18:09:28 +00001961 if (!UseNode->isMachineOpcode()) {
1962 int Latency = ItinData->getOperandCycle(DefTID.getSchedClass(), DefIdx);
1963 if (Subtarget.isCortexA9())
1964 return Latency <= 2 ? 1 : Latency - 1;
1965 else
1966 return Latency <= 3 ? 1 : Latency - 2;
1967 }
Evan Chenga0792de2010-10-06 06:27:31 +00001968
1969 const TargetInstrDesc &UseTID = get(UseNode->getMachineOpcode());
1970 const MachineSDNode *DefMN = dyn_cast<MachineSDNode>(DefNode);
1971 unsigned DefAlign = !DefMN->memoperands_empty()
1972 ? (*DefMN->memoperands_begin())->getAlignment() : 0;
1973 const MachineSDNode *UseMN = dyn_cast<MachineSDNode>(UseNode);
1974 unsigned UseAlign = !UseMN->memoperands_empty()
1975 ? (*UseMN->memoperands_begin())->getAlignment() : 0;
Evan Cheng7e2fe912010-10-28 06:47:08 +00001976 int Latency = getOperandLatency(ItinData, DefTID, DefIdx, DefAlign,
1977 UseTID, UseIdx, UseAlign);
1978
1979 if (Latency > 1 &&
1980 (Subtarget.isCortexA8() || Subtarget.isCortexA9())) {
1981 // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
1982 // variants are one cycle cheaper.
1983 switch (DefTID.getOpcode()) {
1984 default: break;
1985 case ARM::LDRrs:
1986 case ARM::LDRBrs: {
1987 unsigned ShOpVal =
1988 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
1989 unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
1990 if (ShImm == 0 ||
1991 (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
1992 --Latency;
1993 break;
1994 }
1995 case ARM::t2LDRs:
1996 case ARM::t2LDRBs:
1997 case ARM::t2LDRHs:
1998 case ARM::t2LDRSHs: {
1999 // Thumb2 mode: lsl only.
2000 unsigned ShAmt =
2001 cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
2002 if (ShAmt == 0 || ShAmt == 2)
2003 --Latency;
2004 break;
2005 }
2006 }
2007 }
2008
2009 return Latency;
Evan Chenga0792de2010-10-06 06:27:31 +00002010}
Evan Cheng23128422010-10-19 18:58:51 +00002011
Evan Cheng8239daf2010-11-03 00:45:17 +00002012int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2013 const MachineInstr *MI,
2014 unsigned *PredCost) const {
2015 if (MI->isCopyLike() || MI->isInsertSubreg() ||
2016 MI->isRegSequence() || MI->isImplicitDef())
2017 return 1;
2018
2019 if (!ItinData || ItinData->isEmpty())
2020 return 1;
2021
2022 const TargetInstrDesc &TID = MI->getDesc();
2023 unsigned Class = TID.getSchedClass();
2024 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
2025 if (PredCost && TID.hasImplicitDefOfPhysReg(ARM::CPSR))
2026 // When predicated, CPSR is an additional source operand for CPSR updating
2027 // instructions, this apparently increases their latencies.
2028 *PredCost = 1;
2029 if (UOps)
2030 return ItinData->getStageLatency(Class);
2031 return getNumMicroOps(ItinData, MI);
2032}
2033
2034int ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
2035 SDNode *Node) const {
2036 if (!Node->isMachineOpcode())
2037 return 1;
2038
2039 if (!ItinData || ItinData->isEmpty())
2040 return 1;
2041
2042 unsigned Opcode = Node->getMachineOpcode();
2043 switch (Opcode) {
2044 default:
2045 return ItinData->getStageLatency(get(Opcode).getSchedClass());
2046 case ARM::VLDMQ:
2047 case ARM::VSTMQ:
2048 return 2;
2049 }
2050}
2051
Evan Cheng23128422010-10-19 18:58:51 +00002052bool ARMBaseInstrInfo::
2053hasHighOperandLatency(const InstrItineraryData *ItinData,
2054 const MachineRegisterInfo *MRI,
2055 const MachineInstr *DefMI, unsigned DefIdx,
2056 const MachineInstr *UseMI, unsigned UseIdx) const {
2057 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2058 unsigned UDomain = UseMI->getDesc().TSFlags & ARMII::DomainMask;
2059 if (Subtarget.isCortexA8() &&
2060 (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
2061 // CortexA8 VFP instructions are not pipelined.
2062 return true;
2063
2064 // Hoist VFP / NEON instructions with 4 or higher latency.
2065 int Latency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
2066 if (Latency <= 3)
2067 return false;
2068 return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
2069 UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
2070}
Evan Chengc8141df2010-10-26 02:08:50 +00002071
2072bool ARMBaseInstrInfo::
2073hasLowDefLatency(const InstrItineraryData *ItinData,
2074 const MachineInstr *DefMI, unsigned DefIdx) const {
2075 if (!ItinData || ItinData->isEmpty())
2076 return false;
2077
2078 unsigned DDomain = DefMI->getDesc().TSFlags & ARMII::DomainMask;
2079 if (DDomain == ARMII::DomainGeneral) {
2080 unsigned DefClass = DefMI->getDesc().getSchedClass();
2081 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
2082 return (DefCycle != -1 && DefCycle <= 2);
2083 }
2084 return false;
2085}