blob: 312f187be51f7b4f0a3f7d380fda122fd609f69d [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"
31#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"
David Goodwin334c2642009-07-08 16:09:28 +000036using namespace llvm;
37
38static cl::opt<bool>
39EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
40 cl::desc("Enable ARM 2-addr to 3-addr conv"));
41
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000042ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
43 : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)),
44 Subtarget(STI) {
David Goodwin334c2642009-07-08 16:09:28 +000045}
46
47MachineInstr *
48ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
49 MachineBasicBlock::iterator &MBBI,
50 LiveVariables *LV) const {
Evan Cheng78703dd2009-07-27 18:44:00 +000051 // FIXME: Thumb2 support.
52
David Goodwin334c2642009-07-08 16:09:28 +000053 if (!EnableARM3Addr)
54 return NULL;
55
56 MachineInstr *MI = MBBI;
57 MachineFunction &MF = *MI->getParent()->getParent();
58 unsigned TSFlags = MI->getDesc().TSFlags;
59 bool isPre = false;
60 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
61 default: return NULL;
62 case ARMII::IndexModePre:
63 isPre = true;
64 break;
65 case ARMII::IndexModePost:
66 break;
67 }
68
69 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
70 // operation.
71 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
72 if (MemOpc == 0)
73 return NULL;
74
75 MachineInstr *UpdateMI = NULL;
76 MachineInstr *MemMI = NULL;
77 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
78 const TargetInstrDesc &TID = MI->getDesc();
79 unsigned NumOps = TID.getNumOperands();
80 bool isLoad = !TID.mayStore();
81 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
82 const MachineOperand &Base = MI->getOperand(2);
83 const MachineOperand &Offset = MI->getOperand(NumOps-3);
84 unsigned WBReg = WB.getReg();
85 unsigned BaseReg = Base.getReg();
86 unsigned OffReg = Offset.getReg();
87 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
88 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
89 switch (AddrMode) {
90 default:
91 assert(false && "Unknown indexed op!");
92 return NULL;
93 case ARMII::AddrMode2: {
94 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
95 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
96 if (OffReg == 0) {
Evan Chenge7cbe412009-07-08 21:03:57 +000097 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwin334c2642009-07-08 16:09:28 +000098 // Can't encode it in a so_imm operand. This transformation will
99 // add more than 1 instruction. Abandon!
100 return NULL;
101 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000102 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge7cbe412009-07-08 21:03:57 +0000103 .addReg(BaseReg).addImm(Amt)
David Goodwin334c2642009-07-08 16:09:28 +0000104 .addImm(Pred).addReg(0).addReg(0);
105 } else if (Amt != 0) {
106 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
107 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
108 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000109 get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000110 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
111 .addImm(Pred).addReg(0).addReg(0);
112 } else
113 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000114 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000115 .addReg(BaseReg).addReg(OffReg)
116 .addImm(Pred).addReg(0).addReg(0);
117 break;
118 }
119 case ARMII::AddrMode3 : {
120 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
121 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
122 if (OffReg == 0)
123 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
124 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000125 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000126 .addReg(BaseReg).addImm(Amt)
127 .addImm(Pred).addReg(0).addReg(0);
128 else
129 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000130 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000131 .addReg(BaseReg).addReg(OffReg)
132 .addImm(Pred).addReg(0).addReg(0);
133 break;
134 }
135 }
136
137 std::vector<MachineInstr*> NewMIs;
138 if (isPre) {
139 if (isLoad)
140 MemMI = BuildMI(MF, MI->getDebugLoc(),
141 get(MemOpc), MI->getOperand(0).getReg())
142 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
143 else
144 MemMI = BuildMI(MF, MI->getDebugLoc(),
145 get(MemOpc)).addReg(MI->getOperand(1).getReg())
146 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
147 NewMIs.push_back(MemMI);
148 NewMIs.push_back(UpdateMI);
149 } else {
150 if (isLoad)
151 MemMI = BuildMI(MF, MI->getDebugLoc(),
152 get(MemOpc), MI->getOperand(0).getReg())
153 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
154 else
155 MemMI = BuildMI(MF, MI->getDebugLoc(),
156 get(MemOpc)).addReg(MI->getOperand(1).getReg())
157 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
158 if (WB.isDead())
159 UpdateMI->getOperand(0).setIsDead();
160 NewMIs.push_back(UpdateMI);
161 NewMIs.push_back(MemMI);
162 }
163
164 // Transfer LiveVariables states, kill / dead info.
165 if (LV) {
166 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
167 MachineOperand &MO = MI->getOperand(i);
168 if (MO.isReg() && MO.getReg() &&
169 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
170 unsigned Reg = MO.getReg();
171
172 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
173 if (MO.isDef()) {
174 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
175 if (MO.isDead())
176 LV->addVirtualRegisterDead(Reg, NewMI);
177 }
178 if (MO.isUse() && MO.isKill()) {
179 for (unsigned j = 0; j < 2; ++j) {
180 // Look at the two new MI's in reverse order.
181 MachineInstr *NewMI = NewMIs[j];
182 if (!NewMI->readsRegister(Reg))
183 continue;
184 LV->addVirtualRegisterKilled(Reg, NewMI);
185 if (VI.removeKill(MI))
186 VI.Kills.push_back(NewMI);
187 break;
188 }
189 }
190 }
191 }
192 }
193
194 MFI->insert(MBBI, NewMIs[1]);
195 MFI->insert(MBBI, NewMIs[0]);
196 return NewMIs[0];
197}
198
199// Branch analysis.
200bool
201ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
202 MachineBasicBlock *&FBB,
203 SmallVectorImpl<MachineOperand> &Cond,
204 bool AllowModify) const {
205 // If the block has no terminators, it just falls into the block after it.
206 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000207 if (I == MBB.begin())
208 return false;
209 --I;
210 while (I->isDebugValue()) {
211 if (I == MBB.begin())
212 return false;
213 --I;
214 }
215 if (!isUnpredicatedTerminator(I))
David Goodwin334c2642009-07-08 16:09:28 +0000216 return false;
217
218 // Get the last instruction in the block.
219 MachineInstr *LastInst = I;
220
221 // If there is only one terminator instruction, process it.
222 unsigned LastOpc = LastInst->getOpcode();
223 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Evan Cheng5ca53a72009-07-27 18:20:05 +0000224 if (isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000225 TBB = LastInst->getOperand(0).getMBB();
226 return false;
227 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000228 if (isCondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000229 // Block ends with fall-through condbranch.
230 TBB = LastInst->getOperand(0).getMBB();
231 Cond.push_back(LastInst->getOperand(1));
232 Cond.push_back(LastInst->getOperand(2));
233 return false;
234 }
235 return true; // Can't handle indirect branch.
236 }
237
238 // Get the instruction before it if it is a terminator.
239 MachineInstr *SecondLastInst = I;
240
241 // If there are three terminators, we don't know what sort of block this is.
242 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
243 return true;
244
Evan Cheng5ca53a72009-07-27 18:20:05 +0000245 // If the block ends with a B and a Bcc, handle it.
David Goodwin334c2642009-07-08 16:09:28 +0000246 unsigned SecondLastOpc = SecondLastInst->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000247 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000248 TBB = SecondLastInst->getOperand(0).getMBB();
249 Cond.push_back(SecondLastInst->getOperand(1));
250 Cond.push_back(SecondLastInst->getOperand(2));
251 FBB = LastInst->getOperand(0).getMBB();
252 return false;
253 }
254
255 // If the block ends with two unconditional branches, handle it. The second
256 // one is not executed, so remove it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000257 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000258 TBB = SecondLastInst->getOperand(0).getMBB();
259 I = LastInst;
260 if (AllowModify)
261 I->eraseFromParent();
262 return false;
263 }
264
265 // ...likewise if it ends with a branch table followed by an unconditional
266 // branch. The branch folder can create these, and we must get rid of them for
267 // correctness of Thumb constant islands.
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000268 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
269 isIndirectBranchOpcode(SecondLastOpc)) &&
Evan Cheng5ca53a72009-07-27 18:20:05 +0000270 isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000271 I = LastInst;
272 if (AllowModify)
273 I->eraseFromParent();
274 return true;
275 }
276
277 // Otherwise, can't handle this.
278 return true;
279}
280
281
282unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwin334c2642009-07-08 16:09:28 +0000283 MachineBasicBlock::iterator I = MBB.end();
284 if (I == MBB.begin()) return 0;
285 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000286 while (I->isDebugValue()) {
287 if (I == MBB.begin())
288 return 0;
289 --I;
290 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000291 if (!isUncondBranchOpcode(I->getOpcode()) &&
292 !isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000293 return 0;
294
295 // Remove the branch.
296 I->eraseFromParent();
297
298 I = MBB.end();
299
300 if (I == MBB.begin()) return 1;
301 --I;
Evan Cheng5ca53a72009-07-27 18:20:05 +0000302 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000303 return 1;
304
305 // Remove the branch.
306 I->eraseFromParent();
307 return 2;
308}
309
310unsigned
311ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
312 MachineBasicBlock *FBB,
313 const SmallVectorImpl<MachineOperand> &Cond) const {
314 // FIXME this should probably have a DebugLoc argument
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000315 DebugLoc dl;
Evan Cheng6495f632009-07-28 05:48:47 +0000316
317 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
318 int BOpc = !AFI->isThumbFunction()
319 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
320 int BccOpc = !AFI->isThumbFunction()
321 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
David Goodwin334c2642009-07-08 16:09:28 +0000322
323 // Shouldn't be a fall through.
324 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
325 assert((Cond.size() == 2 || Cond.size() == 0) &&
326 "ARM branch conditions have two components!");
327
328 if (FBB == 0) {
329 if (Cond.empty()) // Unconditional branch?
330 BuildMI(&MBB, dl, get(BOpc)).addMBB(TBB);
331 else
332 BuildMI(&MBB, dl, get(BccOpc)).addMBB(TBB)
333 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
334 return 1;
335 }
336
337 // Two-way conditional branch.
338 BuildMI(&MBB, dl, get(BccOpc)).addMBB(TBB)
339 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
340 BuildMI(&MBB, dl, get(BOpc)).addMBB(FBB);
341 return 2;
342}
343
344bool ARMBaseInstrInfo::
345ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
346 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
347 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
348 return false;
349}
350
David Goodwin334c2642009-07-08 16:09:28 +0000351bool ARMBaseInstrInfo::
352PredicateInstruction(MachineInstr *MI,
353 const SmallVectorImpl<MachineOperand> &Pred) const {
354 unsigned Opc = MI->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000355 if (isUncondBranchOpcode(Opc)) {
356 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
David Goodwin334c2642009-07-08 16:09:28 +0000357 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
358 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
359 return true;
360 }
361
362 int PIdx = MI->findFirstPredOperandIdx();
363 if (PIdx != -1) {
364 MachineOperand &PMO = MI->getOperand(PIdx);
365 PMO.setImm(Pred[0].getImm());
366 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
367 return true;
368 }
369 return false;
370}
371
372bool ARMBaseInstrInfo::
373SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
374 const SmallVectorImpl<MachineOperand> &Pred2) const {
375 if (Pred1.size() > 2 || Pred2.size() > 2)
376 return false;
377
378 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
379 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
380 if (CC1 == CC2)
381 return true;
382
383 switch (CC1) {
384 default:
385 return false;
386 case ARMCC::AL:
387 return true;
388 case ARMCC::HS:
389 return CC2 == ARMCC::HI;
390 case ARMCC::LS:
391 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
392 case ARMCC::GE:
393 return CC2 == ARMCC::GT;
394 case ARMCC::LE:
395 return CC2 == ARMCC::LT;
396 }
397}
398
399bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
400 std::vector<MachineOperand> &Pred) const {
Evan Cheng8fb90362009-08-08 03:20:32 +0000401 // FIXME: This confuses implicit_def with optional CPSR def.
David Goodwin334c2642009-07-08 16:09:28 +0000402 const TargetInstrDesc &TID = MI->getDesc();
403 if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
404 return false;
405
406 bool Found = false;
407 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
408 const MachineOperand &MO = MI->getOperand(i);
409 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
410 Pred.push_back(MO);
411 Found = true;
412 }
413 }
414
415 return Found;
416}
417
Evan Chengac0869d2009-11-21 06:21:52 +0000418/// isPredicable - Return true if the specified instruction can be predicated.
419/// By default, this returns true for every instruction with a
420/// PredicateOperand.
421bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
422 const TargetInstrDesc &TID = MI->getDesc();
423 if (!TID.isPredicable())
424 return false;
425
426 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
427 ARMFunctionInfo *AFI =
428 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
Evan Chengd7f08102009-11-24 08:06:15 +0000429 return AFI->isThumb2Function();
Evan Chengac0869d2009-11-21 06:21:52 +0000430 }
431 return true;
432}
David Goodwin334c2642009-07-08 16:09:28 +0000433
Chris Lattner56856b12009-12-03 06:58:32 +0000434/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
435DISABLE_INLINE
David Goodwin334c2642009-07-08 16:09:28 +0000436static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattner56856b12009-12-03 06:58:32 +0000437 unsigned JTI);
David Goodwin334c2642009-07-08 16:09:28 +0000438static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
439 unsigned JTI) {
Chris Lattner56856b12009-12-03 06:58:32 +0000440 assert(JTI < JT.size());
David Goodwin334c2642009-07-08 16:09:28 +0000441 return JT[JTI].MBBs.size();
442}
443
444/// GetInstSize - Return the size of the specified MachineInstr.
445///
446unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
447 const MachineBasicBlock &MBB = *MI->getParent();
448 const MachineFunction *MF = MBB.getParent();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000449 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwin334c2642009-07-08 16:09:28 +0000450
451 // Basic size info comes from the TSFlags field.
452 const TargetInstrDesc &TID = MI->getDesc();
453 unsigned TSFlags = TID.TSFlags;
454
Evan Chenga0ee8622009-07-31 22:22:22 +0000455 unsigned Opc = MI->getOpcode();
David Goodwin334c2642009-07-08 16:09:28 +0000456 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
457 default: {
458 // If this machine instr is an inline asm, measure it.
459 if (MI->getOpcode() == ARM::INLINEASM)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000460 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Goodwin334c2642009-07-08 16:09:28 +0000461 if (MI->isLabel())
462 return 0;
Evan Chenga0ee8622009-07-31 22:22:22 +0000463 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000464 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000465 llvm_unreachable("Unknown or unset size field for instr!");
Chris Lattner518bb532010-02-09 19:54:29 +0000466 case TargetOpcode::IMPLICIT_DEF:
467 case TargetOpcode::KILL:
468 case TargetOpcode::DBG_LABEL:
469 case TargetOpcode::EH_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +0000470 case TargetOpcode::DBG_VALUE:
David Goodwin334c2642009-07-08 16:09:28 +0000471 return 0;
472 }
473 break;
474 }
Evan Cheng78947622009-07-24 18:20:44 +0000475 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
476 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
477 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
David Goodwin334c2642009-07-08 16:09:28 +0000478 case ARMII::SizeSpecial: {
Evan Chenga0ee8622009-07-31 22:22:22 +0000479 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000480 case ARM::CONSTPOOL_ENTRY:
481 // If this machine instr is a constant pool entry, its size is recorded as
482 // operand #2.
483 return MI->getOperand(2).getImm();
Jim Grosbach5eb19512010-05-22 01:06:18 +0000484 case ARM::Int_eh_sjlj_longjmp:
485 return 16;
486 case ARM::tInt_eh_sjlj_longjmp:
487 return 10;
Evan Cheng78947622009-07-24 18:20:44 +0000488 case ARM::Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000489 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbachcdc17eb2009-08-11 17:08:15 +0000490 return 24;
Jim Grosbachd1228742009-12-01 18:10:36 +0000491 case ARM::tInt_eh_sjlj_setjmp:
Jim Grosbach5aa16842009-08-11 19:42:21 +0000492 case ARM::t2Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000493 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha87ded22010-02-08 23:22:00 +0000494 return 14;
David Goodwin334c2642009-07-08 16:09:28 +0000495 case ARM::BR_JTr:
496 case ARM::BR_JTm:
497 case ARM::BR_JTadd:
Evan Chenga0ee8622009-07-31 22:22:22 +0000498 case ARM::tBR_JTr:
Evan Chengd26b14c2009-07-31 18:28:05 +0000499 case ARM::t2BR_JT:
500 case ARM::t2TBB:
501 case ARM::t2TBH: {
David Goodwin334c2642009-07-08 16:09:28 +0000502 // These are jumptable branches, i.e. a branch followed by an inlined
Evan Chengd26b14c2009-07-31 18:28:05 +0000503 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
504 // entry is one byte; TBH two byte each.
Evan Chenga0ee8622009-07-31 22:22:22 +0000505 unsigned EntrySize = (Opc == ARM::t2TBB)
506 ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4);
David Goodwin334c2642009-07-08 16:09:28 +0000507 unsigned NumOps = TID.getNumOperands();
508 MachineOperand JTOP =
509 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
510 unsigned JTI = JTOP.getIndex();
511 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000512 assert(MJTI != 0);
David Goodwin334c2642009-07-08 16:09:28 +0000513 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
514 assert(JTI < JT.size());
515 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
516 // 4 aligned. The assembler / linker may add 2 byte padding just before
517 // the JT entries. The size does not include this padding; the
518 // constant islands pass does separate bookkeeping for it.
519 // FIXME: If we know the size of the function is less than (1 << 16) *2
520 // bytes, we can use 16-bit entries instead. Then there won't be an
521 // alignment issue.
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000522 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
523 unsigned NumEntries = getNumJTEntries(JT, JTI);
524 if (Opc == ARM::t2TBB && (NumEntries & 1))
525 // Make sure the instruction that follows TBB is 2-byte aligned.
526 // FIXME: Constant island pass should insert an "ALIGN" instruction
527 // instead.
528 ++NumEntries;
529 return NumEntries * EntrySize + InstSize;
David Goodwin334c2642009-07-08 16:09:28 +0000530 }
531 default:
532 // Otherwise, pseudo-instruction sizes are zero.
533 return 0;
534 }
535 }
536 }
537 return 0; // Not reached
538}
539
540/// Return true if the instruction is a register to register move and
541/// leave the source and dest operands in the passed parameters.
542///
543bool
544ARMBaseInstrInfo::isMoveInstr(const MachineInstr &MI,
545 unsigned &SrcReg, unsigned &DstReg,
546 unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000547 switch (MI.getOpcode()) {
Evan Chengdced03f2009-07-27 00:24:36 +0000548 default: break;
Jim Grosbache5165492009-11-09 00:11:35 +0000549 case ARM::VMOVS:
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000550 case ARM::VMOVD:
Jim Grosbache5165492009-11-09 00:11:35 +0000551 case ARM::VMOVDneon:
Evan Chengb63387a2010-05-06 06:36:08 +0000552 case ARM::VMOVQ:
553 case ARM::VMOVQQ : {
David Goodwin334c2642009-07-08 16:09:28 +0000554 SrcReg = MI.getOperand(1).getReg();
555 DstReg = MI.getOperand(0).getReg();
Evan Chengb63387a2010-05-06 06:36:08 +0000556 SrcSubIdx = MI.getOperand(1).getSubReg();
557 DstSubIdx = MI.getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000558 return true;
559 }
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000560 case ARM::MOVr:
561 case ARM::tMOVr:
562 case ARM::tMOVgpr2tgpr:
563 case ARM::tMOVtgpr2gpr:
564 case ARM::tMOVgpr2gpr:
565 case ARM::t2MOVr: {
David Goodwin334c2642009-07-08 16:09:28 +0000566 assert(MI.getDesc().getNumOperands() >= 2 &&
567 MI.getOperand(0).isReg() &&
568 MI.getOperand(1).isReg() &&
569 "Invalid ARM MOV instruction");
570 SrcReg = MI.getOperand(1).getReg();
571 DstReg = MI.getOperand(0).getReg();
Evan Chengb63387a2010-05-06 06:36:08 +0000572 SrcSubIdx = MI.getOperand(1).getSubReg();
573 DstSubIdx = MI.getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000574 return true;
575 }
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000576 }
David Goodwin334c2642009-07-08 16:09:28 +0000577
578 return false;
579}
580
Jim Grosbach764ab522009-08-11 15:33:49 +0000581unsigned
David Goodwin334c2642009-07-08 16:09:28 +0000582ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
583 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000584 switch (MI->getOpcode()) {
585 default: break;
586 case ARM::LDR:
587 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000588 if (MI->getOperand(1).isFI() &&
589 MI->getOperand(2).isReg() &&
590 MI->getOperand(3).isImm() &&
591 MI->getOperand(2).getReg() == 0 &&
592 MI->getOperand(3).getImm() == 0) {
593 FrameIndex = MI->getOperand(1).getIndex();
594 return MI->getOperand(0).getReg();
595 }
Evan Chengdced03f2009-07-27 00:24:36 +0000596 break;
597 case ARM::t2LDRi12:
598 case ARM::tRestore:
David Goodwin5ff58b52009-07-24 00:16:18 +0000599 if (MI->getOperand(1).isFI() &&
600 MI->getOperand(2).isImm() &&
601 MI->getOperand(2).getImm() == 0) {
602 FrameIndex = MI->getOperand(1).getIndex();
603 return MI->getOperand(0).getReg();
604 }
Evan Chengdced03f2009-07-27 00:24:36 +0000605 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000606 case ARM::VLDRD:
607 case ARM::VLDRS:
David Goodwin334c2642009-07-08 16:09:28 +0000608 if (MI->getOperand(1).isFI() &&
609 MI->getOperand(2).isImm() &&
610 MI->getOperand(2).getImm() == 0) {
611 FrameIndex = MI->getOperand(1).getIndex();
612 return MI->getOperand(0).getReg();
613 }
Evan Chengdced03f2009-07-27 00:24:36 +0000614 break;
David Goodwin334c2642009-07-08 16:09:28 +0000615 }
616
617 return 0;
618}
619
620unsigned
621ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
622 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000623 switch (MI->getOpcode()) {
624 default: break;
625 case ARM::STR:
626 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000627 if (MI->getOperand(1).isFI() &&
628 MI->getOperand(2).isReg() &&
629 MI->getOperand(3).isImm() &&
630 MI->getOperand(2).getReg() == 0 &&
631 MI->getOperand(3).getImm() == 0) {
632 FrameIndex = MI->getOperand(1).getIndex();
633 return MI->getOperand(0).getReg();
634 }
Evan Chengdced03f2009-07-27 00:24:36 +0000635 break;
636 case ARM::t2STRi12:
637 case ARM::tSpill:
David Goodwin5ff58b52009-07-24 00:16:18 +0000638 if (MI->getOperand(1).isFI() &&
639 MI->getOperand(2).isImm() &&
640 MI->getOperand(2).getImm() == 0) {
641 FrameIndex = MI->getOperand(1).getIndex();
642 return MI->getOperand(0).getReg();
643 }
Evan Chengdced03f2009-07-27 00:24:36 +0000644 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000645 case ARM::VSTRD:
646 case ARM::VSTRS:
David Goodwin334c2642009-07-08 16:09:28 +0000647 if (MI->getOperand(1).isFI() &&
648 MI->getOperand(2).isImm() &&
649 MI->getOperand(2).getImm() == 0) {
650 FrameIndex = MI->getOperand(1).getIndex();
651 return MI->getOperand(0).getReg();
652 }
Evan Chengdced03f2009-07-27 00:24:36 +0000653 break;
David Goodwin334c2642009-07-08 16:09:28 +0000654 }
655
656 return 0;
657}
658
659bool
660ARMBaseInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
661 MachineBasicBlock::iterator I,
662 unsigned DestReg, unsigned SrcReg,
663 const TargetRegisterClass *DestRC,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000664 const TargetRegisterClass *SrcRC,
665 DebugLoc DL) const {
Bob Wilson1665b0a2010-02-16 17:24:15 +0000666 // tGPR is used sometimes in ARM instructions that need to avoid using
667 // certain registers. Just treat it as GPR here.
668 if (DestRC == ARM::tGPRRegisterClass)
669 DestRC = ARM::GPRRegisterClass;
670 if (SrcRC == ARM::tGPRRegisterClass)
671 SrcRC = ARM::GPRRegisterClass;
672
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000673 // Allow DPR / DPR_VFP2 / DPR_8 cross-class copies.
674 if (DestRC == ARM::DPR_8RegisterClass)
675 DestRC = ARM::DPR_VFP2RegisterClass;
676 if (SrcRC == ARM::DPR_8RegisterClass)
677 SrcRC = ARM::DPR_VFP2RegisterClass;
Evan Chengb4db6a42009-11-03 05:51:39 +0000678
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000679 // Allow QPR / QPR_VFP2 / QPR_8 cross-class copies.
680 if (DestRC == ARM::QPR_VFP2RegisterClass ||
681 DestRC == ARM::QPR_8RegisterClass)
682 DestRC = ARM::QPRRegisterClass;
683 if (SrcRC == ARM::QPR_VFP2RegisterClass ||
684 SrcRC == ARM::QPR_8RegisterClass)
685 SrcRC = ARM::QPRRegisterClass;
686
Evan Cheng22c687b2010-05-14 02:13:41 +0000687 // Allow QQPR / QQPR_VFP2 cross-class copies.
688 if (DestRC == ARM::QQPR_VFP2RegisterClass)
Evan Chengb63387a2010-05-06 06:36:08 +0000689 DestRC = ARM::QQPRRegisterClass;
Evan Cheng22c687b2010-05-14 02:13:41 +0000690 if (SrcRC == ARM::QQPR_VFP2RegisterClass)
Evan Chengb63387a2010-05-06 06:36:08 +0000691 SrcRC = ARM::QQPRRegisterClass;
692
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000693 // Disallow copies of unequal sizes.
694 if (DestRC != SrcRC && DestRC->getSize() != SrcRC->getSize())
695 return false;
David Goodwin334c2642009-07-08 16:09:28 +0000696
David Goodwin7bfdca02009-08-05 21:02:22 +0000697 if (DestRC == ARM::GPRRegisterClass) {
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000698 if (SrcRC == ARM::SPRRegisterClass)
699 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VMOVRS), DestReg)
700 .addReg(SrcReg));
701 else
702 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr),
703 DestReg).addReg(SrcReg)));
David Goodwin7bfdca02009-08-05 21:02:22 +0000704 } else {
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000705 unsigned Opc;
706
707 if (DestRC == ARM::SPRRegisterClass)
708 Opc = (SrcRC == ARM::GPRRegisterClass ? ARM::VMOVSR : ARM::VMOVS);
709 else if (DestRC == ARM::DPRRegisterClass)
710 Opc = ARM::VMOVD;
711 else if (DestRC == ARM::DPR_VFP2RegisterClass ||
712 SrcRC == ARM::DPR_VFP2RegisterClass)
713 // Always use neon reg-reg move if source or dest is NEON-only regclass.
714 Opc = ARM::VMOVDneon;
715 else if (DestRC == ARM::QPRRegisterClass)
716 Opc = ARM::VMOVQ;
Evan Chengb63387a2010-05-06 06:36:08 +0000717 else if (DestRC == ARM::QQPRRegisterClass)
718 Opc = ARM::VMOVQQ;
Evan Cheng22c687b2010-05-14 02:13:41 +0000719 else if (DestRC == ARM::QQQQPRRegisterClass)
720 Opc = ARM::VMOVQQQQ;
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000721 else
722 return false;
723
Evan Chengb63387a2010-05-06 06:36:08 +0000724 AddDefaultPred(BuildMI(MBB, I, DL, get(Opc), DestReg).addReg(SrcReg));
David Goodwin7bfdca02009-08-05 21:02:22 +0000725 }
David Goodwin334c2642009-07-08 16:09:28 +0000726
727 return true;
728}
729
Evan Chengc10b5af2010-05-07 00:24:52 +0000730static const
731MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
732 unsigned Reg, unsigned SubIdx, unsigned State,
733 const TargetRegisterInfo *TRI) {
734 if (!SubIdx)
735 return MIB.addReg(Reg, State);
736
737 if (TargetRegisterInfo::isPhysicalRegister(Reg))
738 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
739 return MIB.addReg(Reg, State, SubIdx);
740}
741
David Goodwin334c2642009-07-08 16:09:28 +0000742void ARMBaseInstrInfo::
743storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
744 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000745 const TargetRegisterClass *RC,
746 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000747 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000748 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000749 MachineFunction &MF = *MBB.getParent();
750 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000751 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000752
753 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000754 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000755 MachineMemOperand::MOStore, 0,
756 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000757 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000758
Bob Wilson0eb0c742010-02-16 22:01:59 +0000759 // tGPR is used sometimes in ARM instructions that need to avoid using
760 // certain registers. Just treat it as GPR here.
761 if (RC == ARM::tGPRRegisterClass)
762 RC = ARM::GPRRegisterClass;
763
David Goodwin334c2642009-07-08 16:09:28 +0000764 if (RC == ARM::GPRRegisterClass) {
Evan Cheng5732ca02009-07-27 03:14:20 +0000765 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STR))
David Goodwin334c2642009-07-08 16:09:28 +0000766 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000767 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Evan Chengd31c5492010-05-06 01:34:11 +0000768 } else if (RC == ARM::SPRRegisterClass) {
769 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
770 .addReg(SrcReg, getKillRegState(isKill))
771 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Anton Korobeynikov6ca0b9e2009-09-08 15:22:32 +0000772 } else if (RC == ARM::DPRRegisterClass ||
773 RC == ARM::DPR_VFP2RegisterClass ||
774 RC == ARM::DPR_8RegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000775 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000776 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000777 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Evan Chengb63387a2010-05-06 06:36:08 +0000778 } else if (RC == ARM::QPRRegisterClass ||
779 RC == ARM::QPR_VFP2RegisterClass ||
780 RC == ARM::QPR_8RegisterClass) {
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000781 // FIXME: Neon instructions should support predicates
Evan Chengb63387a2010-05-06 06:36:08 +0000782 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng69b9f982010-05-13 01:12:06 +0000783 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q))
784 .addFrameIndex(FI).addImm(128)
785 .addReg(SrcReg, getKillRegState(isKill))
786 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000787 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000788 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQ))
789 .addReg(SrcReg, getKillRegState(isKill))
790 .addFrameIndex(FI)
791 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
792 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000793 }
Evan Cheng22c687b2010-05-14 02:13:41 +0000794 } else if (RC == ARM::QQPRRegisterClass || RC == ARM::QQPR_VFP2RegisterClass){
Evan Cheng435d4992010-05-07 02:04:02 +0000795 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng22c687b2010-05-14 02:13:41 +0000796 // FIXME: It's possible to only store part of the QQ register if the
797 // spilled def has a sub-register index.
Evan Cheng435d4992010-05-07 02:04:02 +0000798 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VST2q32))
799 .addFrameIndex(FI).addImm(128);
800 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_0, getKillRegState(isKill), TRI);
801 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_1, 0, TRI);
802 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_2, 0, TRI);
803 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_3, 0, TRI);
804 AddDefaultPred(MIB.addMemOperand(MMO));
805 } else {
806 MachineInstrBuilder MIB =
807 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
808 .addFrameIndex(FI)
809 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
810 .addMemOperand(MMO);
811 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_0, getKillRegState(isKill), TRI);
812 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_1, 0, TRI);
813 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_2, 0, TRI);
814 AddDReg(MIB, SrcReg, ARM::DSUBREG_3, 0, TRI);
815 }
Evan Cheng22c687b2010-05-14 02:13:41 +0000816 } else {
817 assert(RC == ARM::QQQQPRRegisterClass && "Unknown regclass!");
818 MachineInstrBuilder MIB =
819 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
820 .addFrameIndex(FI)
821 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
822 .addMemOperand(MMO);
823 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_0, getKillRegState(isKill), TRI);
824 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_1, 0, TRI);
825 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_2, 0, TRI);
826 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_3, 0, TRI);
827 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_4, 0, TRI);
828 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_5, 0, TRI);
829 MIB = AddDReg(MIB, SrcReg, ARM::DSUBREG_6, 0, TRI);
830 AddDReg(MIB, SrcReg, ARM::DSUBREG_7, 0, TRI);
David Goodwin334c2642009-07-08 16:09:28 +0000831 }
832}
833
David Goodwin334c2642009-07-08 16:09:28 +0000834void ARMBaseInstrInfo::
835loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
836 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000837 const TargetRegisterClass *RC,
838 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000839 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000840 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000841 MachineFunction &MF = *MBB.getParent();
842 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000843 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000844 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000845 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000846 MachineMemOperand::MOLoad, 0,
847 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000848 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000849
Bob Wilson0eb0c742010-02-16 22:01:59 +0000850 // tGPR is used sometimes in ARM instructions that need to avoid using
851 // certain registers. Just treat it as GPR here.
852 if (RC == ARM::tGPRRegisterClass)
853 RC = ARM::GPRRegisterClass;
854
David Goodwin334c2642009-07-08 16:09:28 +0000855 if (RC == ARM::GPRRegisterClass) {
Evan Cheng5732ca02009-07-27 03:14:20 +0000856 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDR), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000857 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Evan Chengd31c5492010-05-06 01:34:11 +0000858 } else if (RC == ARM::SPRRegisterClass) {
859 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
860 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Anton Korobeynikov6ca0b9e2009-09-08 15:22:32 +0000861 } else if (RC == ARM::DPRRegisterClass ||
862 RC == ARM::DPR_VFP2RegisterClass ||
863 RC == ARM::DPR_8RegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000864 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000865 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Evan Chengb63387a2010-05-06 06:36:08 +0000866 } else if (RC == ARM::QPRRegisterClass ||
867 RC == ARM::QPR_VFP2RegisterClass ||
868 RC == ARM::QPR_8RegisterClass) {
869 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng69b9f982010-05-13 01:12:06 +0000870 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q), DestReg)
871 .addFrameIndex(FI).addImm(128)
872 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000873 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000874 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQ), DestReg)
875 .addFrameIndex(FI)
876 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
877 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000878 }
Evan Cheng22c687b2010-05-14 02:13:41 +0000879 } else if (RC == ARM::QQPRRegisterClass || RC == ARM::QQPR_VFP2RegisterClass){
Evan Cheng435d4992010-05-07 02:04:02 +0000880 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
881 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLD2q32));
882 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_0, RegState::Define, TRI);
883 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_1, RegState::Define, TRI);
884 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_2, RegState::Define, TRI);
885 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_3, RegState::Define, TRI);
886 AddDefaultPred(MIB.addFrameIndex(FI).addImm(128).addMemOperand(MMO));
887 } else {
888 MachineInstrBuilder MIB =
889 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
890 .addFrameIndex(FI)
891 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
892 .addMemOperand(MMO);
893 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_0, RegState::Define, TRI);
894 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_1, RegState::Define, TRI);
895 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_2, RegState::Define, TRI);
896 AddDReg(MIB, DestReg, ARM::DSUBREG_3, RegState::Define, TRI);
897 }
Evan Cheng22c687b2010-05-14 02:13:41 +0000898 } else {
899 assert(RC == ARM::QQQQPRRegisterClass && "Unknown regclass!");
900 MachineInstrBuilder MIB =
901 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
902 .addFrameIndex(FI)
903 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
904 .addMemOperand(MMO);
905 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_0, RegState::Define, TRI);
906 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_1, RegState::Define, TRI);
907 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_2, RegState::Define, TRI);
908 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_3, RegState::Define, TRI);
909 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_4, RegState::Define, TRI);
910 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_5, RegState::Define, TRI);
911 MIB = AddDReg(MIB, DestReg, ARM::DSUBREG_6, RegState::Define, TRI);
912 AddDReg(MIB, DestReg, ARM::DSUBREG_7, RegState::Define, TRI);
David Goodwin334c2642009-07-08 16:09:28 +0000913 }
914}
915
Evan Cheng62b50652010-04-26 07:39:25 +0000916MachineInstr*
917ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000918 int FrameIx, uint64_t Offset,
Evan Cheng62b50652010-04-26 07:39:25 +0000919 const MDNode *MDPtr,
920 DebugLoc DL) const {
921 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
922 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
923 return &*MIB;
924}
925
David Goodwin334c2642009-07-08 16:09:28 +0000926MachineInstr *ARMBaseInstrInfo::
927foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
928 const SmallVectorImpl<unsigned> &Ops, int FI) const {
929 if (Ops.size() != 1) return NULL;
930
931 unsigned OpNum = Ops[0];
932 unsigned Opc = MI->getOpcode();
933 MachineInstr *NewMI = NULL;
Evan Cheng19068ba2009-08-10 06:32:05 +0000934 if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
David Goodwin334c2642009-07-08 16:09:28 +0000935 // If it is updating CPSR, then it cannot be folded.
Evan Cheng19068ba2009-08-10 06:32:05 +0000936 if (MI->getOperand(4).getReg() == ARM::CPSR && !MI->getOperand(4).isDead())
937 return NULL;
938 unsigned Pred = MI->getOperand(2).getImm();
939 unsigned PredReg = MI->getOperand(3).getReg();
940 if (OpNum == 0) { // move -> store
941 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000942 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000943 bool isKill = MI->getOperand(1).isKill();
944 bool isUndef = MI->getOperand(1).isUndef();
945 if (Opc == ARM::MOVr)
946 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::STR))
Evan Chenged3ad212009-10-25 07:52:27 +0000947 .addReg(SrcReg,
948 getKillRegState(isKill) | getUndefRegState(isUndef),
949 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000950 .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
951 else // ARM::t2MOVr
952 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
Evan Chenged3ad212009-10-25 07:52:27 +0000953 .addReg(SrcReg,
954 getKillRegState(isKill) | getUndefRegState(isUndef),
955 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000956 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
957 } else { // move -> load
958 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000959 unsigned DstSubReg = MI->getOperand(0).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000960 bool isDead = MI->getOperand(0).isDead();
961 bool isUndef = MI->getOperand(0).isUndef();
962 if (Opc == ARM::MOVr)
963 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::LDR))
964 .addReg(DstReg,
965 RegState::Define |
966 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +0000967 getUndefRegState(isUndef), DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000968 .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
969 else // ARM::t2MOVr
970 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
971 .addReg(DstReg,
972 RegState::Define |
973 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +0000974 getUndefRegState(isUndef), DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000975 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
David Goodwin334c2642009-07-08 16:09:28 +0000976 }
Evan Cheng19068ba2009-08-10 06:32:05 +0000977 } else if (Opc == ARM::tMOVgpr2gpr ||
978 Opc == ARM::tMOVtgpr2gpr ||
979 Opc == ARM::tMOVgpr2tgpr) {
980 if (OpNum == 0) { // move -> store
981 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000982 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000983 bool isKill = MI->getOperand(1).isKill();
984 bool isUndef = MI->getOperand(1).isUndef();
985 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
Evan Chenged3ad212009-10-25 07:52:27 +0000986 .addReg(SrcReg,
987 getKillRegState(isKill) | getUndefRegState(isUndef),
988 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000989 .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
990 } else { // move -> load
991 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000992 unsigned DstSubReg = MI->getOperand(0).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000993 bool isDead = MI->getOperand(0).isDead();
994 bool isUndef = MI->getOperand(0).isUndef();
995 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
996 .addReg(DstReg,
997 RegState::Define |
998 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +0000999 getUndefRegState(isUndef),
1000 DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001001 .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
1002 }
Jim Grosbache5165492009-11-09 00:11:35 +00001003 } else if (Opc == ARM::VMOVS) {
David Goodwin334c2642009-07-08 16:09:28 +00001004 unsigned Pred = MI->getOperand(2).getImm();
1005 unsigned PredReg = MI->getOperand(3).getReg();
1006 if (OpNum == 0) { // move -> store
1007 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001008 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001009 bool isKill = MI->getOperand(1).isKill();
1010 bool isUndef = MI->getOperand(1).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001011 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTRS))
Evan Chenged3ad212009-10-25 07:52:27 +00001012 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef),
1013 SrcSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001014 .addFrameIndex(FI)
1015 .addImm(0).addImm(Pred).addReg(PredReg);
1016 } else { // move -> load
1017 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001018 unsigned DstSubReg = MI->getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001019 bool isDead = MI->getOperand(0).isDead();
1020 bool isUndef = MI->getOperand(0).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001021 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDRS))
David Goodwin334c2642009-07-08 16:09:28 +00001022 .addReg(DstReg,
1023 RegState::Define |
1024 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001025 getUndefRegState(isUndef),
1026 DstSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001027 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1028 }
Evan Cheng69b9f982010-05-13 01:12:06 +00001029 } else if (Opc == ARM::VMOVD || Opc == ARM::VMOVDneon) {
David Goodwin334c2642009-07-08 16:09:28 +00001030 unsigned Pred = MI->getOperand(2).getImm();
1031 unsigned PredReg = MI->getOperand(3).getReg();
1032 if (OpNum == 0) { // move -> store
1033 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001034 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001035 bool isKill = MI->getOperand(1).isKill();
1036 bool isUndef = MI->getOperand(1).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001037 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTRD))
Evan Chenged3ad212009-10-25 07:52:27 +00001038 .addReg(SrcReg,
1039 getKillRegState(isKill) | getUndefRegState(isUndef),
1040 SrcSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001041 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1042 } else { // move -> load
1043 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001044 unsigned DstSubReg = MI->getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001045 bool isDead = MI->getOperand(0).isDead();
1046 bool isUndef = MI->getOperand(0).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001047 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDRD))
David Goodwin334c2642009-07-08 16:09:28 +00001048 .addReg(DstReg,
1049 RegState::Define |
1050 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001051 getUndefRegState(isUndef),
1052 DstSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001053 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1054 }
Evan Cheng69b9f982010-05-13 01:12:06 +00001055 } else if (Opc == ARM::VMOVQ) {
1056 MachineFrameInfo &MFI = *MF.getFrameInfo();
1057 unsigned Pred = MI->getOperand(2).getImm();
1058 unsigned PredReg = MI->getOperand(3).getReg();
1059 if (OpNum == 0) { // move -> store
1060 unsigned SrcReg = MI->getOperand(1).getReg();
1061 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
1062 bool isKill = MI->getOperand(1).isKill();
1063 bool isUndef = MI->getOperand(1).isUndef();
1064 if (MFI.getObjectAlignment(FI) >= 16 &&
1065 getRegisterInfo().canRealignStack(MF)) {
1066 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VST1q))
1067 .addFrameIndex(FI).addImm(128)
1068 .addReg(SrcReg,
1069 getKillRegState(isKill) | getUndefRegState(isUndef),
1070 SrcSubReg)
1071 .addImm(Pred).addReg(PredReg);
1072 } else {
1073 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTMQ))
1074 .addReg(SrcReg,
1075 getKillRegState(isKill) | getUndefRegState(isUndef),
1076 SrcSubReg)
1077 .addFrameIndex(FI).addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
1078 .addImm(Pred).addReg(PredReg);
1079 }
1080 } else { // move -> load
1081 unsigned DstReg = MI->getOperand(0).getReg();
1082 unsigned DstSubReg = MI->getOperand(0).getSubReg();
1083 bool isDead = MI->getOperand(0).isDead();
1084 bool isUndef = MI->getOperand(0).isUndef();
1085 if (MFI.getObjectAlignment(FI) >= 16 &&
1086 getRegisterInfo().canRealignStack(MF)) {
1087 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLD1q))
1088 .addReg(DstReg,
1089 RegState::Define |
1090 getDeadRegState(isDead) |
1091 getUndefRegState(isUndef),
1092 DstSubReg)
1093 .addFrameIndex(FI).addImm(128).addImm(Pred).addReg(PredReg);
1094 } else {
1095 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDMQ))
1096 .addReg(DstReg,
1097 RegState::Define |
1098 getDeadRegState(isDead) |
1099 getUndefRegState(isUndef),
1100 DstSubReg)
1101 .addFrameIndex(FI).addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
1102 .addImm(Pred).addReg(PredReg);
1103 }
1104 }
David Goodwin334c2642009-07-08 16:09:28 +00001105 }
1106
1107 return NewMI;
1108}
1109
Jim Grosbach764ab522009-08-11 15:33:49 +00001110MachineInstr*
David Goodwin334c2642009-07-08 16:09:28 +00001111ARMBaseInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
1112 MachineInstr* MI,
1113 const SmallVectorImpl<unsigned> &Ops,
1114 MachineInstr* LoadMI) const {
Evan Cheng1f5c9882009-07-27 04:18:04 +00001115 // FIXME
David Goodwin334c2642009-07-08 16:09:28 +00001116 return 0;
1117}
1118
1119bool
1120ARMBaseInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
Evan Cheng22946452009-08-10 05:51:48 +00001121 const SmallVectorImpl<unsigned> &Ops) const {
David Goodwin334c2642009-07-08 16:09:28 +00001122 if (Ops.size() != 1) return false;
1123
1124 unsigned Opc = MI->getOpcode();
Evan Cheng5732ca02009-07-27 03:14:20 +00001125 if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
David Goodwin334c2642009-07-08 16:09:28 +00001126 // If it is updating CPSR, then it cannot be folded.
Evan Cheng22946452009-08-10 05:51:48 +00001127 return MI->getOperand(4).getReg() != ARM::CPSR ||
1128 MI->getOperand(4).isDead();
Evan Cheng19068ba2009-08-10 06:32:05 +00001129 } else if (Opc == ARM::tMOVgpr2gpr ||
1130 Opc == ARM::tMOVtgpr2gpr ||
1131 Opc == ARM::tMOVgpr2tgpr) {
1132 return true;
Evan Cheng69b9f982010-05-13 01:12:06 +00001133 } else if (Opc == ARM::VMOVS || Opc == ARM::VMOVD ||
1134 Opc == ARM::VMOVDneon || Opc == ARM::VMOVQ) {
David Goodwin334c2642009-07-08 16:09:28 +00001135 return true;
David Goodwin334c2642009-07-08 16:09:28 +00001136 }
1137
Evan Cheng22c687b2010-05-14 02:13:41 +00001138 // FIXME: VMOVQQ and VMOVQQQQ?
1139
David Goodwin334c2642009-07-08 16:09:28 +00001140 return false;
1141}
Evan Cheng5ca53a72009-07-27 18:20:05 +00001142
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001143/// Create a copy of a const pool value. Update CPI to the new index and return
1144/// the label UID.
1145static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1146 MachineConstantPool *MCP = MF.getConstantPool();
1147 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1148
1149 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1150 assert(MCPE.isMachineConstantPoolEntry() &&
1151 "Expecting a machine constantpool entry!");
1152 ARMConstantPoolValue *ACPV =
1153 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1154
1155 unsigned PCLabelId = AFI->createConstPoolEntryUId();
1156 ARMConstantPoolValue *NewCPV = 0;
1157 if (ACPV->isGlobalValue())
1158 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
1159 ARMCP::CPValue, 4);
1160 else if (ACPV->isExtSymbol())
1161 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
1162 ACPV->getSymbol(), PCLabelId, 4);
1163 else if (ACPV->isBlockAddress())
1164 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
1165 ARMCP::CPBlockAddress, 4);
1166 else
1167 llvm_unreachable("Unexpected ARM constantpool value type!!");
1168 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1169 return PCLabelId;
1170}
1171
Evan Chengfdc83402009-11-08 00:15:23 +00001172void ARMBaseInstrInfo::
1173reMaterialize(MachineBasicBlock &MBB,
1174 MachineBasicBlock::iterator I,
1175 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +00001176 const MachineInstr *Orig,
1177 const TargetRegisterInfo *TRI) const {
Evan Chengd57cdd52009-11-14 02:55:43 +00001178 if (SubIdx && TargetRegisterInfo::isPhysicalRegister(DestReg)) {
1179 DestReg = TRI->getSubReg(DestReg, SubIdx);
1180 SubIdx = 0;
1181 }
1182
Evan Chengfdc83402009-11-08 00:15:23 +00001183 unsigned Opcode = Orig->getOpcode();
1184 switch (Opcode) {
1185 default: {
1186 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1187 MI->getOperand(0).setReg(DestReg);
1188 MBB.insert(I, MI);
1189 break;
1190 }
1191 case ARM::tLDRpci_pic:
1192 case ARM::t2LDRpci_pic: {
1193 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +00001194 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001195 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +00001196 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1197 DestReg)
1198 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1199 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1200 break;
1201 }
1202 }
1203
1204 MachineInstr *NewMI = prior(I);
1205 NewMI->getOperand(0).setSubReg(SubIdx);
1206}
1207
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001208MachineInstr *
1209ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1210 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1211 switch(Orig->getOpcode()) {
1212 case ARM::tLDRpci_pic:
1213 case ARM::t2LDRpci_pic: {
1214 unsigned CPI = Orig->getOperand(1).getIndex();
1215 unsigned PCLabelId = duplicateCPV(MF, CPI);
1216 Orig->getOperand(1).setIndex(CPI);
1217 Orig->getOperand(2).setImm(PCLabelId);
1218 break;
1219 }
1220 }
1221 return MI;
1222}
1223
Evan Cheng506049f2010-03-03 01:44:33 +00001224bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1225 const MachineInstr *MI1) const {
Evan Chengd457e6e2009-11-07 04:04:34 +00001226 int Opcode = MI0->getOpcode();
Evan Cheng9b824252009-11-20 02:10:27 +00001227 if (Opcode == ARM::t2LDRpci ||
1228 Opcode == ARM::t2LDRpci_pic ||
1229 Opcode == ARM::tLDRpci ||
1230 Opcode == ARM::tLDRpci_pic) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001231 if (MI1->getOpcode() != Opcode)
1232 return false;
1233 if (MI0->getNumOperands() != MI1->getNumOperands())
1234 return false;
1235
1236 const MachineOperand &MO0 = MI0->getOperand(1);
1237 const MachineOperand &MO1 = MI1->getOperand(1);
1238 if (MO0.getOffset() != MO1.getOffset())
1239 return false;
1240
1241 const MachineFunction *MF = MI0->getParent()->getParent();
1242 const MachineConstantPool *MCP = MF->getConstantPool();
1243 int CPI0 = MO0.getIndex();
1244 int CPI1 = MO1.getIndex();
1245 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1246 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1247 ARMConstantPoolValue *ACPV0 =
1248 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1249 ARMConstantPoolValue *ACPV1 =
1250 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1251 return ACPV0->hasSameValue(ACPV1);
1252 }
1253
Evan Cheng506049f2010-03-03 01:44:33 +00001254 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001255}
1256
Evan Cheng8fb90362009-08-08 03:20:32 +00001257/// getInstrPredicate - If instruction is predicated, returns its predicate
1258/// condition, otherwise returns AL. It also returns the condition code
1259/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001260ARMCC::CondCodes
1261llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001262 int PIdx = MI->findFirstPredOperandIdx();
1263 if (PIdx == -1) {
1264 PredReg = 0;
1265 return ARMCC::AL;
1266 }
1267
1268 PredReg = MI->getOperand(PIdx+1).getReg();
1269 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1270}
1271
1272
Evan Cheng6495f632009-07-28 05:48:47 +00001273int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001274 if (Opc == ARM::B)
1275 return ARM::Bcc;
1276 else if (Opc == ARM::tB)
1277 return ARM::tBcc;
1278 else if (Opc == ARM::t2B)
1279 return ARM::t2Bcc;
1280
1281 llvm_unreachable("Unknown unconditional branch opcode!");
1282 return 0;
1283}
1284
Evan Cheng6495f632009-07-28 05:48:47 +00001285
1286void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1287 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1288 unsigned DestReg, unsigned BaseReg, int NumBytes,
1289 ARMCC::CondCodes Pred, unsigned PredReg,
1290 const ARMBaseInstrInfo &TII) {
1291 bool isSub = NumBytes < 0;
1292 if (isSub) NumBytes = -NumBytes;
1293
1294 while (NumBytes) {
1295 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1296 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1297 assert(ThisVal && "Didn't extract field correctly");
1298
1299 // We will handle these bits from offset, clear them.
1300 NumBytes &= ~ThisVal;
1301
1302 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1303
1304 // Build the new ADD / SUB.
1305 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1306 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1307 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1308 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1309 BaseReg = DestReg;
1310 }
1311}
1312
Evan Chengcdbb3f52009-08-27 01:23:50 +00001313bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1314 unsigned FrameReg, int &Offset,
1315 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001316 unsigned Opcode = MI.getOpcode();
1317 const TargetInstrDesc &Desc = MI.getDesc();
1318 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1319 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001320
Evan Cheng6495f632009-07-28 05:48:47 +00001321 // Memory operands in inline assembly always use AddrMode2.
1322 if (Opcode == ARM::INLINEASM)
1323 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001324
Evan Cheng6495f632009-07-28 05:48:47 +00001325 if (Opcode == ARM::ADDri) {
1326 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1327 if (Offset == 0) {
1328 // Turn it into a move.
1329 MI.setDesc(TII.get(ARM::MOVr));
1330 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1331 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001332 Offset = 0;
1333 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001334 } else if (Offset < 0) {
1335 Offset = -Offset;
1336 isSub = true;
1337 MI.setDesc(TII.get(ARM::SUBri));
1338 }
1339
1340 // Common case: small offset, fits into instruction.
1341 if (ARM_AM::getSOImmVal(Offset) != -1) {
1342 // Replace the FrameIndex with sp / fp
1343 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1344 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001345 Offset = 0;
1346 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001347 }
1348
1349 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1350 // as possible.
1351 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1352 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1353
1354 // We will handle these bits from offset, clear them.
1355 Offset &= ~ThisImmVal;
1356
1357 // Get the properly encoded SOImmVal field.
1358 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1359 "Bit extraction didn't work?");
1360 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1361 } else {
1362 unsigned ImmIdx = 0;
1363 int InstrOffs = 0;
1364 unsigned NumBits = 0;
1365 unsigned Scale = 1;
1366 switch (AddrMode) {
1367 case ARMII::AddrMode2: {
1368 ImmIdx = FrameRegIdx+2;
1369 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1370 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1371 InstrOffs *= -1;
1372 NumBits = 12;
1373 break;
1374 }
1375 case ARMII::AddrMode3: {
1376 ImmIdx = FrameRegIdx+2;
1377 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1378 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1379 InstrOffs *= -1;
1380 NumBits = 8;
1381 break;
1382 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001383 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001384 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001385 // Can't fold any offset even if it's zero.
1386 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001387 case ARMII::AddrMode5: {
1388 ImmIdx = FrameRegIdx+1;
1389 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1390 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1391 InstrOffs *= -1;
1392 NumBits = 8;
1393 Scale = 4;
1394 break;
1395 }
1396 default:
1397 llvm_unreachable("Unsupported addressing mode!");
1398 break;
1399 }
1400
1401 Offset += InstrOffs * Scale;
1402 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1403 if (Offset < 0) {
1404 Offset = -Offset;
1405 isSub = true;
1406 }
1407
1408 // Attempt to fold address comp. if opcode has offset bits
1409 if (NumBits > 0) {
1410 // Common case: small offset, fits into instruction.
1411 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1412 int ImmedOffset = Offset / Scale;
1413 unsigned Mask = (1 << NumBits) - 1;
1414 if ((unsigned)Offset <= Mask * Scale) {
1415 // Replace the FrameIndex with sp
1416 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1417 if (isSub)
1418 ImmedOffset |= 1 << NumBits;
1419 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001420 Offset = 0;
1421 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001422 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001423
Evan Cheng6495f632009-07-28 05:48:47 +00001424 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1425 ImmedOffset = ImmedOffset & Mask;
1426 if (isSub)
1427 ImmedOffset |= 1 << NumBits;
1428 ImmOp.ChangeToImmediate(ImmedOffset);
1429 Offset &= ~(Mask*Scale);
1430 }
1431 }
1432
Evan Chengcdbb3f52009-08-27 01:23:50 +00001433 Offset = (isSub) ? -Offset : Offset;
1434 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001435}