blob: 353cf15ddd2a11c2e3f316484ed61d7d83e71843 [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 "ARMGenInstrInfo.inc"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000020#include "ARMRegisterInfo.h"
Evan Chengfdc83402009-11-08 00:15:23 +000021#include "llvm/Constants.h"
22#include "llvm/Function.h"
23#include "llvm/GlobalValue.h"
David Goodwin334c2642009-07-08 16:09:28 +000024#include "llvm/ADT/STLExtras.h"
25#include "llvm/CodeGen/LiveVariables.h"
Evan Chengd457e6e2009-11-07 04:04:34 +000026#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwin334c2642009-07-08 16:09:28 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov249fb332009-10-07 00:06:35 +000030#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng2457f2c2010-05-22 01:47:14 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov249fb332009-10-07 00:06:35 +000032#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000033#include "llvm/MC/MCAsmInfo.h"
David Goodwin334c2642009-07-08 16:09:28 +000034#include "llvm/Support/CommandLine.h"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000035#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
David Goodwin334c2642009-07-08 16:09:28 +000037using namespace llvm;
38
39static cl::opt<bool>
40EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
41 cl::desc("Enable ARM 2-addr to 3-addr conv"));
42
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();
59 unsigned TSFlags = MI->getDesc().TSFlags;
60 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())
143 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
144 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())
154 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
155 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
229 //
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;
278
279 // If there are three terminators, we don't know what sort of block this is.
280 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
281 return true;
282
Evan Cheng5ca53a72009-07-27 18:20:05 +0000283 // If the block ends with a B and a Bcc, handle it.
David Goodwin334c2642009-07-08 16:09:28 +0000284 unsigned SecondLastOpc = SecondLastInst->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000285 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000286 TBB = SecondLastInst->getOperand(0).getMBB();
287 Cond.push_back(SecondLastInst->getOperand(1));
288 Cond.push_back(SecondLastInst->getOperand(2));
289 FBB = LastInst->getOperand(0).getMBB();
290 return false;
291 }
292
293 // If the block ends with two unconditional branches, handle it. The second
294 // one is not executed, so remove it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000295 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000296 TBB = SecondLastInst->getOperand(0).getMBB();
297 I = LastInst;
298 if (AllowModify)
299 I->eraseFromParent();
300 return false;
301 }
302
303 // ...likewise if it ends with a branch table followed by an unconditional
304 // branch. The branch folder can create these, and we must get rid of them for
305 // correctness of Thumb constant islands.
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000306 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
307 isIndirectBranchOpcode(SecondLastOpc)) &&
Evan Cheng5ca53a72009-07-27 18:20:05 +0000308 isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000309 I = LastInst;
310 if (AllowModify)
311 I->eraseFromParent();
312 return true;
313 }
314
315 // Otherwise, can't handle this.
316 return true;
317}
318
319
320unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwin334c2642009-07-08 16:09:28 +0000321 MachineBasicBlock::iterator I = MBB.end();
322 if (I == MBB.begin()) return 0;
323 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000324 while (I->isDebugValue()) {
325 if (I == MBB.begin())
326 return 0;
327 --I;
328 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000329 if (!isUncondBranchOpcode(I->getOpcode()) &&
330 !isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000331 return 0;
332
333 // Remove the branch.
334 I->eraseFromParent();
335
336 I = MBB.end();
337
338 if (I == MBB.begin()) return 1;
339 --I;
Evan Cheng5ca53a72009-07-27 18:20:05 +0000340 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000341 return 1;
342
343 // Remove the branch.
344 I->eraseFromParent();
345 return 2;
346}
347
348unsigned
349ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Jim Grosbach18f30e62010-06-02 21:53:11 +0000350 MachineBasicBlock *FBB,
351 const SmallVectorImpl<MachineOperand> &Cond) const {
David Goodwin334c2642009-07-08 16:09:28 +0000352 // FIXME this should probably have a DebugLoc argument
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000353 DebugLoc dl;
Evan Cheng6495f632009-07-28 05:48:47 +0000354
355 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
356 int BOpc = !AFI->isThumbFunction()
357 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
358 int BccOpc = !AFI->isThumbFunction()
359 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
David Goodwin334c2642009-07-08 16:09:28 +0000360
361 // Shouldn't be a fall through.
362 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
363 assert((Cond.size() == 2 || Cond.size() == 0) &&
364 "ARM branch conditions have two components!");
365
366 if (FBB == 0) {
367 if (Cond.empty()) // Unconditional branch?
368 BuildMI(&MBB, dl, get(BOpc)).addMBB(TBB);
369 else
370 BuildMI(&MBB, dl, get(BccOpc)).addMBB(TBB)
371 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
372 return 1;
373 }
374
375 // Two-way conditional branch.
376 BuildMI(&MBB, dl, get(BccOpc)).addMBB(TBB)
377 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
378 BuildMI(&MBB, dl, get(BOpc)).addMBB(FBB);
379 return 2;
380}
381
382bool ARMBaseInstrInfo::
383ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
384 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
385 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
386 return false;
387}
388
David Goodwin334c2642009-07-08 16:09:28 +0000389bool ARMBaseInstrInfo::
390PredicateInstruction(MachineInstr *MI,
391 const SmallVectorImpl<MachineOperand> &Pred) const {
392 unsigned Opc = MI->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000393 if (isUncondBranchOpcode(Opc)) {
394 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
David Goodwin334c2642009-07-08 16:09:28 +0000395 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
396 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
397 return true;
398 }
399
400 int PIdx = MI->findFirstPredOperandIdx();
401 if (PIdx != -1) {
402 MachineOperand &PMO = MI->getOperand(PIdx);
403 PMO.setImm(Pred[0].getImm());
404 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
405 return true;
406 }
407 return false;
408}
409
410bool ARMBaseInstrInfo::
411SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
412 const SmallVectorImpl<MachineOperand> &Pred2) const {
413 if (Pred1.size() > 2 || Pred2.size() > 2)
414 return false;
415
416 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
417 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
418 if (CC1 == CC2)
419 return true;
420
421 switch (CC1) {
422 default:
423 return false;
424 case ARMCC::AL:
425 return true;
426 case ARMCC::HS:
427 return CC2 == ARMCC::HI;
428 case ARMCC::LS:
429 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
430 case ARMCC::GE:
431 return CC2 == ARMCC::GT;
432 case ARMCC::LE:
433 return CC2 == ARMCC::LT;
434 }
435}
436
437bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
438 std::vector<MachineOperand> &Pred) const {
Evan Cheng8fb90362009-08-08 03:20:32 +0000439 // FIXME: This confuses implicit_def with optional CPSR def.
David Goodwin334c2642009-07-08 16:09:28 +0000440 const TargetInstrDesc &TID = MI->getDesc();
441 if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
442 return false;
443
444 bool Found = false;
445 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
446 const MachineOperand &MO = MI->getOperand(i);
447 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
448 Pred.push_back(MO);
449 Found = true;
450 }
451 }
452
453 return Found;
454}
455
Evan Chengac0869d2009-11-21 06:21:52 +0000456/// isPredicable - Return true if the specified instruction can be predicated.
457/// By default, this returns true for every instruction with a
458/// PredicateOperand.
459bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
460 const TargetInstrDesc &TID = MI->getDesc();
461 if (!TID.isPredicable())
462 return false;
463
464 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
465 ARMFunctionInfo *AFI =
466 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
Evan Chengd7f08102009-11-24 08:06:15 +0000467 return AFI->isThumb2Function();
Evan Chengac0869d2009-11-21 06:21:52 +0000468 }
469 return true;
470}
David Goodwin334c2642009-07-08 16:09:28 +0000471
Chris Lattner56856b12009-12-03 06:58:32 +0000472/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
473DISABLE_INLINE
David Goodwin334c2642009-07-08 16:09:28 +0000474static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattner56856b12009-12-03 06:58:32 +0000475 unsigned JTI);
David Goodwin334c2642009-07-08 16:09:28 +0000476static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
477 unsigned JTI) {
Chris Lattner56856b12009-12-03 06:58:32 +0000478 assert(JTI < JT.size());
David Goodwin334c2642009-07-08 16:09:28 +0000479 return JT[JTI].MBBs.size();
480}
481
482/// GetInstSize - Return the size of the specified MachineInstr.
483///
484unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
485 const MachineBasicBlock &MBB = *MI->getParent();
486 const MachineFunction *MF = MBB.getParent();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000487 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwin334c2642009-07-08 16:09:28 +0000488
489 // Basic size info comes from the TSFlags field.
490 const TargetInstrDesc &TID = MI->getDesc();
491 unsigned TSFlags = TID.TSFlags;
492
Evan Chenga0ee8622009-07-31 22:22:22 +0000493 unsigned Opc = MI->getOpcode();
David Goodwin334c2642009-07-08 16:09:28 +0000494 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
495 default: {
496 // If this machine instr is an inline asm, measure it.
497 if (MI->getOpcode() == ARM::INLINEASM)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000498 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Goodwin334c2642009-07-08 16:09:28 +0000499 if (MI->isLabel())
500 return 0;
Evan Chenga0ee8622009-07-31 22:22:22 +0000501 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000502 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000503 llvm_unreachable("Unknown or unset size field for instr!");
Chris Lattner518bb532010-02-09 19:54:29 +0000504 case TargetOpcode::IMPLICIT_DEF:
505 case TargetOpcode::KILL:
506 case TargetOpcode::DBG_LABEL:
507 case TargetOpcode::EH_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +0000508 case TargetOpcode::DBG_VALUE:
David Goodwin334c2642009-07-08 16:09:28 +0000509 return 0;
510 }
511 break;
512 }
Evan Cheng78947622009-07-24 18:20:44 +0000513 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
514 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
515 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
David Goodwin334c2642009-07-08 16:09:28 +0000516 case ARMII::SizeSpecial: {
Evan Chenga0ee8622009-07-31 22:22:22 +0000517 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000518 case ARM::CONSTPOOL_ENTRY:
519 // If this machine instr is a constant pool entry, its size is recorded as
520 // operand #2.
521 return MI->getOperand(2).getImm();
Jim Grosbach5eb19512010-05-22 01:06:18 +0000522 case ARM::Int_eh_sjlj_longjmp:
523 return 16;
524 case ARM::tInt_eh_sjlj_longjmp:
525 return 10;
Evan Cheng78947622009-07-24 18:20:44 +0000526 case ARM::Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000527 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000528 return 20;
Jim Grosbachd1228742009-12-01 18:10:36 +0000529 case ARM::tInt_eh_sjlj_setjmp:
Jim Grosbach5aa16842009-08-11 19:42:21 +0000530 case ARM::t2Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000531 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000532 return 12;
David Goodwin334c2642009-07-08 16:09:28 +0000533 case ARM::BR_JTr:
534 case ARM::BR_JTm:
535 case ARM::BR_JTadd:
Evan Chenga0ee8622009-07-31 22:22:22 +0000536 case ARM::tBR_JTr:
Evan Chengd26b14c2009-07-31 18:28:05 +0000537 case ARM::t2BR_JT:
538 case ARM::t2TBB:
539 case ARM::t2TBH: {
David Goodwin334c2642009-07-08 16:09:28 +0000540 // These are jumptable branches, i.e. a branch followed by an inlined
Evan Chengd26b14c2009-07-31 18:28:05 +0000541 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
542 // entry is one byte; TBH two byte each.
Evan Chenga0ee8622009-07-31 22:22:22 +0000543 unsigned EntrySize = (Opc == ARM::t2TBB)
544 ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4);
David Goodwin334c2642009-07-08 16:09:28 +0000545 unsigned NumOps = TID.getNumOperands();
546 MachineOperand JTOP =
547 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
548 unsigned JTI = JTOP.getIndex();
549 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000550 assert(MJTI != 0);
David Goodwin334c2642009-07-08 16:09:28 +0000551 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
552 assert(JTI < JT.size());
553 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
554 // 4 aligned. The assembler / linker may add 2 byte padding just before
555 // the JT entries. The size does not include this padding; the
556 // constant islands pass does separate bookkeeping for it.
557 // FIXME: If we know the size of the function is less than (1 << 16) *2
558 // bytes, we can use 16-bit entries instead. Then there won't be an
559 // alignment issue.
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000560 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
561 unsigned NumEntries = getNumJTEntries(JT, JTI);
562 if (Opc == ARM::t2TBB && (NumEntries & 1))
563 // Make sure the instruction that follows TBB is 2-byte aligned.
564 // FIXME: Constant island pass should insert an "ALIGN" instruction
565 // instead.
566 ++NumEntries;
567 return NumEntries * EntrySize + InstSize;
David Goodwin334c2642009-07-08 16:09:28 +0000568 }
569 default:
570 // Otherwise, pseudo-instruction sizes are zero.
571 return 0;
572 }
573 }
574 }
575 return 0; // Not reached
576}
577
578/// Return true if the instruction is a register to register move and
579/// leave the source and dest operands in the passed parameters.
580///
581bool
582ARMBaseInstrInfo::isMoveInstr(const MachineInstr &MI,
583 unsigned &SrcReg, unsigned &DstReg,
584 unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000585 switch (MI.getOpcode()) {
Evan Chengdced03f2009-07-27 00:24:36 +0000586 default: break;
Jim Grosbache5165492009-11-09 00:11:35 +0000587 case ARM::VMOVS:
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000588 case ARM::VMOVD:
Jim Grosbache5165492009-11-09 00:11:35 +0000589 case ARM::VMOVDneon:
Evan Chengb63387a2010-05-06 06:36:08 +0000590 case ARM::VMOVQ:
591 case ARM::VMOVQQ : {
David Goodwin334c2642009-07-08 16:09:28 +0000592 SrcReg = MI.getOperand(1).getReg();
593 DstReg = MI.getOperand(0).getReg();
Evan Chengb63387a2010-05-06 06:36:08 +0000594 SrcSubIdx = MI.getOperand(1).getSubReg();
595 DstSubIdx = MI.getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000596 return true;
597 }
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000598 case ARM::MOVr:
599 case ARM::tMOVr:
600 case ARM::tMOVgpr2tgpr:
601 case ARM::tMOVtgpr2gpr:
602 case ARM::tMOVgpr2gpr:
603 case ARM::t2MOVr: {
David Goodwin334c2642009-07-08 16:09:28 +0000604 assert(MI.getDesc().getNumOperands() >= 2 &&
605 MI.getOperand(0).isReg() &&
606 MI.getOperand(1).isReg() &&
607 "Invalid ARM MOV instruction");
608 SrcReg = MI.getOperand(1).getReg();
609 DstReg = MI.getOperand(0).getReg();
Evan Chengb63387a2010-05-06 06:36:08 +0000610 SrcSubIdx = MI.getOperand(1).getSubReg();
611 DstSubIdx = MI.getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000612 return true;
613 }
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000614 }
David Goodwin334c2642009-07-08 16:09:28 +0000615
616 return false;
617}
618
Jim Grosbach764ab522009-08-11 15:33:49 +0000619unsigned
David Goodwin334c2642009-07-08 16:09:28 +0000620ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
621 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000622 switch (MI->getOpcode()) {
623 default: break;
624 case ARM::LDR:
625 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000626 if (MI->getOperand(1).isFI() &&
627 MI->getOperand(2).isReg() &&
628 MI->getOperand(3).isImm() &&
629 MI->getOperand(2).getReg() == 0 &&
630 MI->getOperand(3).getImm() == 0) {
631 FrameIndex = MI->getOperand(1).getIndex();
632 return MI->getOperand(0).getReg();
633 }
Evan Chengdced03f2009-07-27 00:24:36 +0000634 break;
635 case ARM::t2LDRi12:
636 case ARM::tRestore:
David Goodwin5ff58b52009-07-24 00:16:18 +0000637 if (MI->getOperand(1).isFI() &&
638 MI->getOperand(2).isImm() &&
639 MI->getOperand(2).getImm() == 0) {
640 FrameIndex = MI->getOperand(1).getIndex();
641 return MI->getOperand(0).getReg();
642 }
Evan Chengdced03f2009-07-27 00:24:36 +0000643 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000644 case ARM::VLDRD:
645 case ARM::VLDRS:
David Goodwin334c2642009-07-08 16:09:28 +0000646 if (MI->getOperand(1).isFI() &&
647 MI->getOperand(2).isImm() &&
648 MI->getOperand(2).getImm() == 0) {
649 FrameIndex = MI->getOperand(1).getIndex();
650 return MI->getOperand(0).getReg();
651 }
Evan Chengdced03f2009-07-27 00:24:36 +0000652 break;
David Goodwin334c2642009-07-08 16:09:28 +0000653 }
654
655 return 0;
656}
657
658unsigned
659ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
660 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000661 switch (MI->getOpcode()) {
662 default: break;
663 case ARM::STR:
664 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000665 if (MI->getOperand(1).isFI() &&
666 MI->getOperand(2).isReg() &&
667 MI->getOperand(3).isImm() &&
668 MI->getOperand(2).getReg() == 0 &&
669 MI->getOperand(3).getImm() == 0) {
670 FrameIndex = MI->getOperand(1).getIndex();
671 return MI->getOperand(0).getReg();
672 }
Evan Chengdced03f2009-07-27 00:24:36 +0000673 break;
674 case ARM::t2STRi12:
675 case ARM::tSpill:
David Goodwin5ff58b52009-07-24 00:16:18 +0000676 if (MI->getOperand(1).isFI() &&
677 MI->getOperand(2).isImm() &&
678 MI->getOperand(2).getImm() == 0) {
679 FrameIndex = MI->getOperand(1).getIndex();
680 return MI->getOperand(0).getReg();
681 }
Evan Chengdced03f2009-07-27 00:24:36 +0000682 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000683 case ARM::VSTRD:
684 case ARM::VSTRS:
David Goodwin334c2642009-07-08 16:09:28 +0000685 if (MI->getOperand(1).isFI() &&
686 MI->getOperand(2).isImm() &&
687 MI->getOperand(2).getImm() == 0) {
688 FrameIndex = MI->getOperand(1).getIndex();
689 return MI->getOperand(0).getReg();
690 }
Evan Chengdced03f2009-07-27 00:24:36 +0000691 break;
David Goodwin334c2642009-07-08 16:09:28 +0000692 }
693
694 return 0;
695}
696
697bool
698ARMBaseInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
699 MachineBasicBlock::iterator I,
700 unsigned DestReg, unsigned SrcReg,
701 const TargetRegisterClass *DestRC,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000702 const TargetRegisterClass *SrcRC,
703 DebugLoc DL) const {
Bob Wilson1665b0a2010-02-16 17:24:15 +0000704 // tGPR is used sometimes in ARM instructions that need to avoid using
705 // certain registers. Just treat it as GPR here.
706 if (DestRC == ARM::tGPRRegisterClass)
707 DestRC = ARM::GPRRegisterClass;
708 if (SrcRC == ARM::tGPRRegisterClass)
709 SrcRC = ARM::GPRRegisterClass;
710
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000711 // Allow DPR / DPR_VFP2 / DPR_8 cross-class copies.
712 if (DestRC == ARM::DPR_8RegisterClass)
713 DestRC = ARM::DPR_VFP2RegisterClass;
714 if (SrcRC == ARM::DPR_8RegisterClass)
715 SrcRC = ARM::DPR_VFP2RegisterClass;
Evan Chengb4db6a42009-11-03 05:51:39 +0000716
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000717 // Allow QPR / QPR_VFP2 / QPR_8 cross-class copies.
718 if (DestRC == ARM::QPR_VFP2RegisterClass ||
719 DestRC == ARM::QPR_8RegisterClass)
720 DestRC = ARM::QPRRegisterClass;
721 if (SrcRC == ARM::QPR_VFP2RegisterClass ||
722 SrcRC == ARM::QPR_8RegisterClass)
723 SrcRC = ARM::QPRRegisterClass;
724
Evan Cheng22c687b2010-05-14 02:13:41 +0000725 // Allow QQPR / QQPR_VFP2 cross-class copies.
726 if (DestRC == ARM::QQPR_VFP2RegisterClass)
Evan Chengb63387a2010-05-06 06:36:08 +0000727 DestRC = ARM::QQPRRegisterClass;
Evan Cheng22c687b2010-05-14 02:13:41 +0000728 if (SrcRC == ARM::QQPR_VFP2RegisterClass)
Evan Chengb63387a2010-05-06 06:36:08 +0000729 SrcRC = ARM::QQPRRegisterClass;
730
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000731 // Disallow copies of unequal sizes.
732 if (DestRC != SrcRC && DestRC->getSize() != SrcRC->getSize())
733 return false;
David Goodwin334c2642009-07-08 16:09:28 +0000734
David Goodwin7bfdca02009-08-05 21:02:22 +0000735 if (DestRC == ARM::GPRRegisterClass) {
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000736 if (SrcRC == ARM::SPRRegisterClass)
737 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VMOVRS), DestReg)
738 .addReg(SrcReg));
739 else
740 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr),
741 DestReg).addReg(SrcReg)));
David Goodwin7bfdca02009-08-05 21:02:22 +0000742 } else {
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000743 unsigned Opc;
744
745 if (DestRC == ARM::SPRRegisterClass)
746 Opc = (SrcRC == ARM::GPRRegisterClass ? ARM::VMOVSR : ARM::VMOVS);
747 else if (DestRC == ARM::DPRRegisterClass)
748 Opc = ARM::VMOVD;
749 else if (DestRC == ARM::DPR_VFP2RegisterClass ||
750 SrcRC == ARM::DPR_VFP2RegisterClass)
751 // Always use neon reg-reg move if source or dest is NEON-only regclass.
752 Opc = ARM::VMOVDneon;
753 else if (DestRC == ARM::QPRRegisterClass)
754 Opc = ARM::VMOVQ;
Evan Chengb63387a2010-05-06 06:36:08 +0000755 else if (DestRC == ARM::QQPRRegisterClass)
756 Opc = ARM::VMOVQQ;
Evan Cheng22c687b2010-05-14 02:13:41 +0000757 else if (DestRC == ARM::QQQQPRRegisterClass)
758 Opc = ARM::VMOVQQQQ;
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000759 else
760 return false;
761
Evan Chengb63387a2010-05-06 06:36:08 +0000762 AddDefaultPred(BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg));
David Goodwin7bfdca02009-08-05 21:02:22 +0000763 }
David Goodwin334c2642009-07-08 16:09:28 +0000764
765 return true;
766}
767
Evan Chengc10b5af2010-05-07 00:24:52 +0000768static const
769MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
770 unsigned Reg, unsigned SubIdx, unsigned State,
771 const TargetRegisterInfo *TRI) {
772 if (!SubIdx)
773 return MIB.addReg(Reg, State);
774
775 if (TargetRegisterInfo::isPhysicalRegister(Reg))
776 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
777 return MIB.addReg(Reg, State, SubIdx);
778}
779
David Goodwin334c2642009-07-08 16:09:28 +0000780void ARMBaseInstrInfo::
781storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
782 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000783 const TargetRegisterClass *RC,
784 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000785 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000786 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000787 MachineFunction &MF = *MBB.getParent();
788 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000789 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000790
791 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000792 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000793 MachineMemOperand::MOStore, 0,
794 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000795 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000796
Bob Wilson0eb0c742010-02-16 22:01:59 +0000797 // tGPR is used sometimes in ARM instructions that need to avoid using
798 // certain registers. Just treat it as GPR here.
799 if (RC == ARM::tGPRRegisterClass)
800 RC = ARM::GPRRegisterClass;
801
David Goodwin334c2642009-07-08 16:09:28 +0000802 if (RC == ARM::GPRRegisterClass) {
Evan Cheng5732ca02009-07-27 03:14:20 +0000803 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STR))
David Goodwin334c2642009-07-08 16:09:28 +0000804 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000805 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Evan Chengd31c5492010-05-06 01:34:11 +0000806 } else if (RC == ARM::SPRRegisterClass) {
807 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
808 .addReg(SrcReg, getKillRegState(isKill))
809 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Anton Korobeynikov6ca0b9e2009-09-08 15:22:32 +0000810 } else if (RC == ARM::DPRRegisterClass ||
811 RC == ARM::DPR_VFP2RegisterClass ||
812 RC == ARM::DPR_8RegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000813 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000814 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000815 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Evan Chengb63387a2010-05-06 06:36:08 +0000816 } else if (RC == ARM::QPRRegisterClass ||
817 RC == ARM::QPR_VFP2RegisterClass ||
818 RC == ARM::QPR_8RegisterClass) {
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000819 // FIXME: Neon instructions should support predicates
Evan Chengb63387a2010-05-06 06:36:08 +0000820 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng69b9f982010-05-13 01:12:06 +0000821 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q))
822 .addFrameIndex(FI).addImm(128)
823 .addReg(SrcReg, getKillRegState(isKill))
824 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000825 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000826 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQ))
827 .addReg(SrcReg, getKillRegState(isKill))
828 .addFrameIndex(FI)
829 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
830 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000831 }
Evan Cheng22c687b2010-05-14 02:13:41 +0000832 } else if (RC == ARM::QQPRRegisterClass || RC == ARM::QQPR_VFP2RegisterClass){
Evan Cheng435d4992010-05-07 02:04:02 +0000833 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng22c687b2010-05-14 02:13:41 +0000834 // FIXME: It's possible to only store part of the QQ register if the
835 // spilled def has a sub-register index.
Evan Cheng435d4992010-05-07 02:04:02 +0000836 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VST2q32))
837 .addFrameIndex(FI).addImm(128);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000838 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
839 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
840 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
841 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000842 AddDefaultPred(MIB.addMemOperand(MMO));
843 } else {
844 MachineInstrBuilder MIB =
845 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
846 .addFrameIndex(FI)
847 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
848 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000849 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
850 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
851 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
852 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000853 }
Evan Cheng22c687b2010-05-14 02:13:41 +0000854 } else {
855 assert(RC == ARM::QQQQPRRegisterClass && "Unknown regclass!");
856 MachineInstrBuilder MIB =
857 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
858 .addFrameIndex(FI)
859 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
860 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000861 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
862 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
863 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
864 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
865 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
866 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
867 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
868 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
David Goodwin334c2642009-07-08 16:09:28 +0000869 }
870}
871
David Goodwin334c2642009-07-08 16:09:28 +0000872void ARMBaseInstrInfo::
873loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
874 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000875 const TargetRegisterClass *RC,
876 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000877 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000878 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000879 MachineFunction &MF = *MBB.getParent();
880 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000881 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000882 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000883 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000884 MachineMemOperand::MOLoad, 0,
885 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000886 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000887
Bob Wilson0eb0c742010-02-16 22:01:59 +0000888 // tGPR is used sometimes in ARM instructions that need to avoid using
889 // certain registers. Just treat it as GPR here.
890 if (RC == ARM::tGPRRegisterClass)
891 RC = ARM::GPRRegisterClass;
892
David Goodwin334c2642009-07-08 16:09:28 +0000893 if (RC == ARM::GPRRegisterClass) {
Evan Cheng5732ca02009-07-27 03:14:20 +0000894 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDR), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000895 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Evan Chengd31c5492010-05-06 01:34:11 +0000896 } else if (RC == ARM::SPRRegisterClass) {
897 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
898 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Anton Korobeynikov6ca0b9e2009-09-08 15:22:32 +0000899 } else if (RC == ARM::DPRRegisterClass ||
900 RC == ARM::DPR_VFP2RegisterClass ||
901 RC == ARM::DPR_8RegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000902 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000903 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Evan Chengb63387a2010-05-06 06:36:08 +0000904 } else if (RC == ARM::QPRRegisterClass ||
905 RC == ARM::QPR_VFP2RegisterClass ||
906 RC == ARM::QPR_8RegisterClass) {
907 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng69b9f982010-05-13 01:12:06 +0000908 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q), DestReg)
909 .addFrameIndex(FI).addImm(128)
910 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000911 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000912 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQ), DestReg)
913 .addFrameIndex(FI)
914 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
915 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000916 }
Evan Cheng22c687b2010-05-14 02:13:41 +0000917 } else if (RC == ARM::QQPRRegisterClass || RC == ARM::QQPR_VFP2RegisterClass){
Evan Cheng435d4992010-05-07 02:04:02 +0000918 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
919 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLD2q32));
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000920 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
921 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
922 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
923 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000924 AddDefaultPred(MIB.addFrameIndex(FI).addImm(128).addMemOperand(MMO));
925 } else {
926 MachineInstrBuilder MIB =
927 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
928 .addFrameIndex(FI)
929 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
930 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000931 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
932 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
933 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
934 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000935 }
Evan Cheng22c687b2010-05-14 02:13:41 +0000936 } else {
937 assert(RC == ARM::QQQQPRRegisterClass && "Unknown regclass!");
938 MachineInstrBuilder MIB =
939 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
940 .addFrameIndex(FI)
941 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
942 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000943 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
944 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
945 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
946 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
947 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
948 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
949 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
950 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
David Goodwin334c2642009-07-08 16:09:28 +0000951 }
952}
953
Evan Cheng62b50652010-04-26 07:39:25 +0000954MachineInstr*
955ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000956 int FrameIx, uint64_t Offset,
Evan Cheng62b50652010-04-26 07:39:25 +0000957 const MDNode *MDPtr,
958 DebugLoc DL) const {
959 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
960 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
961 return &*MIB;
962}
963
David Goodwin334c2642009-07-08 16:09:28 +0000964MachineInstr *ARMBaseInstrInfo::
965foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
966 const SmallVectorImpl<unsigned> &Ops, int FI) const {
967 if (Ops.size() != 1) return NULL;
968
969 unsigned OpNum = Ops[0];
970 unsigned Opc = MI->getOpcode();
971 MachineInstr *NewMI = NULL;
Evan Cheng19068ba2009-08-10 06:32:05 +0000972 if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
David Goodwin334c2642009-07-08 16:09:28 +0000973 // If it is updating CPSR, then it cannot be folded.
Evan Cheng19068ba2009-08-10 06:32:05 +0000974 if (MI->getOperand(4).getReg() == ARM::CPSR && !MI->getOperand(4).isDead())
975 return NULL;
976 unsigned Pred = MI->getOperand(2).getImm();
977 unsigned PredReg = MI->getOperand(3).getReg();
978 if (OpNum == 0) { // move -> store
979 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000980 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000981 bool isKill = MI->getOperand(1).isKill();
982 bool isUndef = MI->getOperand(1).isUndef();
983 if (Opc == ARM::MOVr)
984 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::STR))
Evan Chenged3ad212009-10-25 07:52:27 +0000985 .addReg(SrcReg,
986 getKillRegState(isKill) | getUndefRegState(isUndef),
987 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000988 .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
989 else // ARM::t2MOVr
990 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
Evan Chenged3ad212009-10-25 07:52:27 +0000991 .addReg(SrcReg,
992 getKillRegState(isKill) | getUndefRegState(isUndef),
993 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000994 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
995 } else { // move -> load
996 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000997 unsigned DstSubReg = MI->getOperand(0).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000998 bool isDead = MI->getOperand(0).isDead();
999 bool isUndef = MI->getOperand(0).isUndef();
1000 if (Opc == ARM::MOVr)
1001 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::LDR))
1002 .addReg(DstReg,
1003 RegState::Define |
1004 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001005 getUndefRegState(isUndef), DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001006 .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
1007 else // ARM::t2MOVr
1008 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
1009 .addReg(DstReg,
1010 RegState::Define |
1011 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001012 getUndefRegState(isUndef), DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001013 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
David Goodwin334c2642009-07-08 16:09:28 +00001014 }
Evan Cheng19068ba2009-08-10 06:32:05 +00001015 } else if (Opc == ARM::tMOVgpr2gpr ||
1016 Opc == ARM::tMOVtgpr2gpr ||
1017 Opc == ARM::tMOVgpr2tgpr) {
1018 if (OpNum == 0) { // move -> store
1019 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001020 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +00001021 bool isKill = MI->getOperand(1).isKill();
1022 bool isUndef = MI->getOperand(1).isUndef();
1023 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
Evan Chenged3ad212009-10-25 07:52:27 +00001024 .addReg(SrcReg,
1025 getKillRegState(isKill) | getUndefRegState(isUndef),
1026 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001027 .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
1028 } else { // move -> load
1029 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001030 unsigned DstSubReg = MI->getOperand(0).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +00001031 bool isDead = MI->getOperand(0).isDead();
1032 bool isUndef = MI->getOperand(0).isUndef();
1033 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
1034 .addReg(DstReg,
1035 RegState::Define |
1036 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001037 getUndefRegState(isUndef),
1038 DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001039 .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
1040 }
Jim Grosbache5165492009-11-09 00:11:35 +00001041 } else if (Opc == ARM::VMOVS) {
David Goodwin334c2642009-07-08 16:09:28 +00001042 unsigned Pred = MI->getOperand(2).getImm();
1043 unsigned PredReg = MI->getOperand(3).getReg();
1044 if (OpNum == 0) { // move -> store
1045 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001046 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001047 bool isKill = MI->getOperand(1).isKill();
1048 bool isUndef = MI->getOperand(1).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001049 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTRS))
Evan Chenged3ad212009-10-25 07:52:27 +00001050 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef),
1051 SrcSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001052 .addFrameIndex(FI)
1053 .addImm(0).addImm(Pred).addReg(PredReg);
1054 } else { // move -> load
1055 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001056 unsigned DstSubReg = MI->getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001057 bool isDead = MI->getOperand(0).isDead();
1058 bool isUndef = MI->getOperand(0).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001059 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDRS))
David Goodwin334c2642009-07-08 16:09:28 +00001060 .addReg(DstReg,
1061 RegState::Define |
1062 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001063 getUndefRegState(isUndef),
1064 DstSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001065 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1066 }
Evan Cheng69b9f982010-05-13 01:12:06 +00001067 } else if (Opc == ARM::VMOVD || Opc == ARM::VMOVDneon) {
David Goodwin334c2642009-07-08 16:09:28 +00001068 unsigned Pred = MI->getOperand(2).getImm();
1069 unsigned PredReg = MI->getOperand(3).getReg();
1070 if (OpNum == 0) { // move -> store
1071 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001072 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001073 bool isKill = MI->getOperand(1).isKill();
1074 bool isUndef = MI->getOperand(1).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001075 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTRD))
Evan Chenged3ad212009-10-25 07:52:27 +00001076 .addReg(SrcReg,
1077 getKillRegState(isKill) | getUndefRegState(isUndef),
1078 SrcSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001079 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1080 } else { // move -> load
1081 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001082 unsigned DstSubReg = MI->getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001083 bool isDead = MI->getOperand(0).isDead();
1084 bool isUndef = MI->getOperand(0).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001085 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDRD))
David Goodwin334c2642009-07-08 16:09:28 +00001086 .addReg(DstReg,
1087 RegState::Define |
1088 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001089 getUndefRegState(isUndef),
1090 DstSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001091 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1092 }
Evan Cheng69b9f982010-05-13 01:12:06 +00001093 } else if (Opc == ARM::VMOVQ) {
1094 MachineFrameInfo &MFI = *MF.getFrameInfo();
1095 unsigned Pred = MI->getOperand(2).getImm();
1096 unsigned PredReg = MI->getOperand(3).getReg();
1097 if (OpNum == 0) { // move -> store
1098 unsigned SrcReg = MI->getOperand(1).getReg();
1099 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
1100 bool isKill = MI->getOperand(1).isKill();
1101 bool isUndef = MI->getOperand(1).isUndef();
1102 if (MFI.getObjectAlignment(FI) >= 16 &&
1103 getRegisterInfo().canRealignStack(MF)) {
1104 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VST1q))
1105 .addFrameIndex(FI).addImm(128)
1106 .addReg(SrcReg,
1107 getKillRegState(isKill) | getUndefRegState(isUndef),
1108 SrcSubReg)
1109 .addImm(Pred).addReg(PredReg);
1110 } else {
1111 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTMQ))
1112 .addReg(SrcReg,
1113 getKillRegState(isKill) | getUndefRegState(isUndef),
1114 SrcSubReg)
1115 .addFrameIndex(FI).addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
1116 .addImm(Pred).addReg(PredReg);
1117 }
1118 } else { // move -> load
1119 unsigned DstReg = MI->getOperand(0).getReg();
1120 unsigned DstSubReg = MI->getOperand(0).getSubReg();
1121 bool isDead = MI->getOperand(0).isDead();
1122 bool isUndef = MI->getOperand(0).isUndef();
1123 if (MFI.getObjectAlignment(FI) >= 16 &&
1124 getRegisterInfo().canRealignStack(MF)) {
1125 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLD1q))
1126 .addReg(DstReg,
1127 RegState::Define |
1128 getDeadRegState(isDead) |
1129 getUndefRegState(isUndef),
1130 DstSubReg)
1131 .addFrameIndex(FI).addImm(128).addImm(Pred).addReg(PredReg);
1132 } else {
1133 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDMQ))
1134 .addReg(DstReg,
1135 RegState::Define |
1136 getDeadRegState(isDead) |
1137 getUndefRegState(isUndef),
1138 DstSubReg)
1139 .addFrameIndex(FI).addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
1140 .addImm(Pred).addReg(PredReg);
1141 }
1142 }
David Goodwin334c2642009-07-08 16:09:28 +00001143 }
1144
1145 return NewMI;
1146}
1147
Jim Grosbach764ab522009-08-11 15:33:49 +00001148MachineInstr*
David Goodwin334c2642009-07-08 16:09:28 +00001149ARMBaseInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
1150 MachineInstr* MI,
1151 const SmallVectorImpl<unsigned> &Ops,
1152 MachineInstr* LoadMI) const {
Evan Cheng1f5c9882009-07-27 04:18:04 +00001153 // FIXME
David Goodwin334c2642009-07-08 16:09:28 +00001154 return 0;
1155}
1156
1157bool
1158ARMBaseInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
Evan Cheng22946452009-08-10 05:51:48 +00001159 const SmallVectorImpl<unsigned> &Ops) const {
David Goodwin334c2642009-07-08 16:09:28 +00001160 if (Ops.size() != 1) return false;
1161
1162 unsigned Opc = MI->getOpcode();
Evan Cheng5732ca02009-07-27 03:14:20 +00001163 if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
David Goodwin334c2642009-07-08 16:09:28 +00001164 // If it is updating CPSR, then it cannot be folded.
Evan Cheng22946452009-08-10 05:51:48 +00001165 return MI->getOperand(4).getReg() != ARM::CPSR ||
1166 MI->getOperand(4).isDead();
Evan Cheng19068ba2009-08-10 06:32:05 +00001167 } else if (Opc == ARM::tMOVgpr2gpr ||
1168 Opc == ARM::tMOVtgpr2gpr ||
1169 Opc == ARM::tMOVgpr2tgpr) {
1170 return true;
Evan Cheng69b9f982010-05-13 01:12:06 +00001171 } else if (Opc == ARM::VMOVS || Opc == ARM::VMOVD ||
1172 Opc == ARM::VMOVDneon || Opc == ARM::VMOVQ) {
David Goodwin334c2642009-07-08 16:09:28 +00001173 return true;
David Goodwin334c2642009-07-08 16:09:28 +00001174 }
1175
Evan Cheng22c687b2010-05-14 02:13:41 +00001176 // FIXME: VMOVQQ and VMOVQQQQ?
1177
David Goodwin334c2642009-07-08 16:09:28 +00001178 return false;
1179}
Evan Cheng5ca53a72009-07-27 18:20:05 +00001180
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001181/// Create a copy of a const pool value. Update CPI to the new index and return
1182/// the label UID.
1183static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1184 MachineConstantPool *MCP = MF.getConstantPool();
1185 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1186
1187 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1188 assert(MCPE.isMachineConstantPoolEntry() &&
1189 "Expecting a machine constantpool entry!");
1190 ARMConstantPoolValue *ACPV =
1191 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1192
1193 unsigned PCLabelId = AFI->createConstPoolEntryUId();
1194 ARMConstantPoolValue *NewCPV = 0;
1195 if (ACPV->isGlobalValue())
1196 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
1197 ARMCP::CPValue, 4);
1198 else if (ACPV->isExtSymbol())
1199 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
1200 ACPV->getSymbol(), PCLabelId, 4);
1201 else if (ACPV->isBlockAddress())
1202 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
1203 ARMCP::CPBlockAddress, 4);
1204 else
1205 llvm_unreachable("Unexpected ARM constantpool value type!!");
1206 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1207 return PCLabelId;
1208}
1209
Evan Chengfdc83402009-11-08 00:15:23 +00001210void ARMBaseInstrInfo::
1211reMaterialize(MachineBasicBlock &MBB,
1212 MachineBasicBlock::iterator I,
1213 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +00001214 const MachineInstr *Orig,
1215 const TargetRegisterInfo *TRI) const {
Evan Chengd57cdd52009-11-14 02:55:43 +00001216 if (SubIdx && TargetRegisterInfo::isPhysicalRegister(DestReg)) {
1217 DestReg = TRI->getSubReg(DestReg, SubIdx);
1218 SubIdx = 0;
1219 }
1220
Evan Chengfdc83402009-11-08 00:15:23 +00001221 unsigned Opcode = Orig->getOpcode();
1222 switch (Opcode) {
1223 default: {
1224 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1225 MI->getOperand(0).setReg(DestReg);
1226 MBB.insert(I, MI);
1227 break;
1228 }
1229 case ARM::tLDRpci_pic:
1230 case ARM::t2LDRpci_pic: {
1231 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +00001232 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001233 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +00001234 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1235 DestReg)
1236 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1237 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1238 break;
1239 }
1240 }
1241
1242 MachineInstr *NewMI = prior(I);
1243 NewMI->getOperand(0).setSubReg(SubIdx);
1244}
1245
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001246MachineInstr *
1247ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1248 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1249 switch(Orig->getOpcode()) {
1250 case ARM::tLDRpci_pic:
1251 case ARM::t2LDRpci_pic: {
1252 unsigned CPI = Orig->getOperand(1).getIndex();
1253 unsigned PCLabelId = duplicateCPV(MF, CPI);
1254 Orig->getOperand(1).setIndex(CPI);
1255 Orig->getOperand(2).setImm(PCLabelId);
1256 break;
1257 }
1258 }
1259 return MI;
1260}
1261
Evan Cheng506049f2010-03-03 01:44:33 +00001262bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1263 const MachineInstr *MI1) const {
Evan Chengd457e6e2009-11-07 04:04:34 +00001264 int Opcode = MI0->getOpcode();
Evan Cheng9b824252009-11-20 02:10:27 +00001265 if (Opcode == ARM::t2LDRpci ||
1266 Opcode == ARM::t2LDRpci_pic ||
1267 Opcode == ARM::tLDRpci ||
1268 Opcode == ARM::tLDRpci_pic) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001269 if (MI1->getOpcode() != Opcode)
1270 return false;
1271 if (MI0->getNumOperands() != MI1->getNumOperands())
1272 return false;
1273
1274 const MachineOperand &MO0 = MI0->getOperand(1);
1275 const MachineOperand &MO1 = MI1->getOperand(1);
1276 if (MO0.getOffset() != MO1.getOffset())
1277 return false;
1278
1279 const MachineFunction *MF = MI0->getParent()->getParent();
1280 const MachineConstantPool *MCP = MF->getConstantPool();
1281 int CPI0 = MO0.getIndex();
1282 int CPI1 = MO1.getIndex();
1283 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1284 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1285 ARMConstantPoolValue *ACPV0 =
1286 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1287 ARMConstantPoolValue *ACPV1 =
1288 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1289 return ACPV0->hasSameValue(ACPV1);
1290 }
1291
Evan Cheng506049f2010-03-03 01:44:33 +00001292 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001293}
1294
Evan Cheng8fb90362009-08-08 03:20:32 +00001295/// getInstrPredicate - If instruction is predicated, returns its predicate
1296/// condition, otherwise returns AL. It also returns the condition code
1297/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001298ARMCC::CondCodes
1299llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001300 int PIdx = MI->findFirstPredOperandIdx();
1301 if (PIdx == -1) {
1302 PredReg = 0;
1303 return ARMCC::AL;
1304 }
1305
1306 PredReg = MI->getOperand(PIdx+1).getReg();
1307 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1308}
1309
1310
Evan Cheng6495f632009-07-28 05:48:47 +00001311int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001312 if (Opc == ARM::B)
1313 return ARM::Bcc;
1314 else if (Opc == ARM::tB)
1315 return ARM::tBcc;
1316 else if (Opc == ARM::t2B)
1317 return ARM::t2Bcc;
1318
1319 llvm_unreachable("Unknown unconditional branch opcode!");
1320 return 0;
1321}
1322
Evan Cheng6495f632009-07-28 05:48:47 +00001323
1324void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1325 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1326 unsigned DestReg, unsigned BaseReg, int NumBytes,
1327 ARMCC::CondCodes Pred, unsigned PredReg,
1328 const ARMBaseInstrInfo &TII) {
1329 bool isSub = NumBytes < 0;
1330 if (isSub) NumBytes = -NumBytes;
1331
1332 while (NumBytes) {
1333 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1334 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1335 assert(ThisVal && "Didn't extract field correctly");
1336
1337 // We will handle these bits from offset, clear them.
1338 NumBytes &= ~ThisVal;
1339
1340 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1341
1342 // Build the new ADD / SUB.
1343 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1344 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1345 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1346 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1347 BaseReg = DestReg;
1348 }
1349}
1350
Evan Chengcdbb3f52009-08-27 01:23:50 +00001351bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1352 unsigned FrameReg, int &Offset,
1353 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001354 unsigned Opcode = MI.getOpcode();
1355 const TargetInstrDesc &Desc = MI.getDesc();
1356 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1357 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001358
Evan Cheng6495f632009-07-28 05:48:47 +00001359 // Memory operands in inline assembly always use AddrMode2.
1360 if (Opcode == ARM::INLINEASM)
1361 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001362
Evan Cheng6495f632009-07-28 05:48:47 +00001363 if (Opcode == ARM::ADDri) {
1364 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1365 if (Offset == 0) {
1366 // Turn it into a move.
1367 MI.setDesc(TII.get(ARM::MOVr));
1368 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1369 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001370 Offset = 0;
1371 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001372 } else if (Offset < 0) {
1373 Offset = -Offset;
1374 isSub = true;
1375 MI.setDesc(TII.get(ARM::SUBri));
1376 }
1377
1378 // Common case: small offset, fits into instruction.
1379 if (ARM_AM::getSOImmVal(Offset) != -1) {
1380 // Replace the FrameIndex with sp / fp
1381 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1382 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001383 Offset = 0;
1384 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001385 }
1386
1387 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1388 // as possible.
1389 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1390 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1391
1392 // We will handle these bits from offset, clear them.
1393 Offset &= ~ThisImmVal;
1394
1395 // Get the properly encoded SOImmVal field.
1396 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1397 "Bit extraction didn't work?");
1398 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1399 } else {
1400 unsigned ImmIdx = 0;
1401 int InstrOffs = 0;
1402 unsigned NumBits = 0;
1403 unsigned Scale = 1;
1404 switch (AddrMode) {
1405 case ARMII::AddrMode2: {
1406 ImmIdx = FrameRegIdx+2;
1407 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1408 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1409 InstrOffs *= -1;
1410 NumBits = 12;
1411 break;
1412 }
1413 case ARMII::AddrMode3: {
1414 ImmIdx = FrameRegIdx+2;
1415 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1416 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1417 InstrOffs *= -1;
1418 NumBits = 8;
1419 break;
1420 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001421 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001422 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001423 // Can't fold any offset even if it's zero.
1424 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001425 case ARMII::AddrMode5: {
1426 ImmIdx = FrameRegIdx+1;
1427 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1428 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1429 InstrOffs *= -1;
1430 NumBits = 8;
1431 Scale = 4;
1432 break;
1433 }
1434 default:
1435 llvm_unreachable("Unsupported addressing mode!");
1436 break;
1437 }
1438
1439 Offset += InstrOffs * Scale;
1440 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1441 if (Offset < 0) {
1442 Offset = -Offset;
1443 isSub = true;
1444 }
1445
1446 // Attempt to fold address comp. if opcode has offset bits
1447 if (NumBits > 0) {
1448 // Common case: small offset, fits into instruction.
1449 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1450 int ImmedOffset = Offset / Scale;
1451 unsigned Mask = (1 << NumBits) - 1;
1452 if ((unsigned)Offset <= Mask * Scale) {
1453 // Replace the FrameIndex with sp
1454 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1455 if (isSub)
1456 ImmedOffset |= 1 << NumBits;
1457 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001458 Offset = 0;
1459 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001460 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001461
Evan Cheng6495f632009-07-28 05:48:47 +00001462 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1463 ImmedOffset = ImmedOffset & Mask;
1464 if (isSub)
1465 ImmedOffset |= 1 << NumBits;
1466 ImmOp.ChangeToImmediate(ImmedOffset);
1467 Offset &= ~(Mask*Scale);
1468 }
1469 }
1470
Evan Chengcdbb3f52009-08-27 01:23:50 +00001471 Offset = (isSub) ? -Offset : Offset;
1472 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001473}