blob: 8e537d8b6280139fceef86004ebc3562b80290d7 [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();
207 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
208 return false;
209
210 // Get the last instruction in the block.
211 MachineInstr *LastInst = I;
212
213 // If there is only one terminator instruction, process it.
214 unsigned LastOpc = LastInst->getOpcode();
215 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Evan Cheng5ca53a72009-07-27 18:20:05 +0000216 if (isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000217 TBB = LastInst->getOperand(0).getMBB();
218 return false;
219 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000220 if (isCondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000221 // Block ends with fall-through condbranch.
222 TBB = LastInst->getOperand(0).getMBB();
223 Cond.push_back(LastInst->getOperand(1));
224 Cond.push_back(LastInst->getOperand(2));
225 return false;
226 }
227 return true; // Can't handle indirect branch.
228 }
229
230 // Get the instruction before it if it is a terminator.
231 MachineInstr *SecondLastInst = I;
232
233 // If there are three terminators, we don't know what sort of block this is.
234 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
235 return true;
236
Evan Cheng5ca53a72009-07-27 18:20:05 +0000237 // If the block ends with a B and a Bcc, handle it.
David Goodwin334c2642009-07-08 16:09:28 +0000238 unsigned SecondLastOpc = SecondLastInst->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000239 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000240 TBB = SecondLastInst->getOperand(0).getMBB();
241 Cond.push_back(SecondLastInst->getOperand(1));
242 Cond.push_back(SecondLastInst->getOperand(2));
243 FBB = LastInst->getOperand(0).getMBB();
244 return false;
245 }
246
247 // If the block ends with two unconditional branches, handle it. The second
248 // one is not executed, so remove it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000249 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000250 TBB = SecondLastInst->getOperand(0).getMBB();
251 I = LastInst;
252 if (AllowModify)
253 I->eraseFromParent();
254 return false;
255 }
256
257 // ...likewise if it ends with a branch table followed by an unconditional
258 // branch. The branch folder can create these, and we must get rid of them for
259 // correctness of Thumb constant islands.
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000260 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
261 isIndirectBranchOpcode(SecondLastOpc)) &&
Evan Cheng5ca53a72009-07-27 18:20:05 +0000262 isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000263 I = LastInst;
264 if (AllowModify)
265 I->eraseFromParent();
266 return true;
267 }
268
269 // Otherwise, can't handle this.
270 return true;
271}
272
273
274unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwin334c2642009-07-08 16:09:28 +0000275 MachineBasicBlock::iterator I = MBB.end();
276 if (I == MBB.begin()) return 0;
277 --I;
Evan Cheng5ca53a72009-07-27 18:20:05 +0000278 if (!isUncondBranchOpcode(I->getOpcode()) &&
279 !isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000280 return 0;
281
282 // Remove the branch.
283 I->eraseFromParent();
284
285 I = MBB.end();
286
287 if (I == MBB.begin()) return 1;
288 --I;
Evan Cheng5ca53a72009-07-27 18:20:05 +0000289 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000290 return 1;
291
292 // Remove the branch.
293 I->eraseFromParent();
294 return 2;
295}
296
297unsigned
298ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
299 MachineBasicBlock *FBB,
300 const SmallVectorImpl<MachineOperand> &Cond) const {
301 // FIXME this should probably have a DebugLoc argument
302 DebugLoc dl = DebugLoc::getUnknownLoc();
Evan Cheng6495f632009-07-28 05:48:47 +0000303
304 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
305 int BOpc = !AFI->isThumbFunction()
306 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
307 int BccOpc = !AFI->isThumbFunction()
308 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
David Goodwin334c2642009-07-08 16:09:28 +0000309
310 // Shouldn't be a fall through.
311 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
312 assert((Cond.size() == 2 || Cond.size() == 0) &&
313 "ARM branch conditions have two components!");
314
315 if (FBB == 0) {
316 if (Cond.empty()) // Unconditional branch?
317 BuildMI(&MBB, dl, get(BOpc)).addMBB(TBB);
318 else
319 BuildMI(&MBB, dl, get(BccOpc)).addMBB(TBB)
320 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
321 return 1;
322 }
323
324 // Two-way conditional branch.
325 BuildMI(&MBB, dl, get(BccOpc)).addMBB(TBB)
326 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
327 BuildMI(&MBB, dl, get(BOpc)).addMBB(FBB);
328 return 2;
329}
330
331bool ARMBaseInstrInfo::
332ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
333 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
334 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
335 return false;
336}
337
David Goodwin334c2642009-07-08 16:09:28 +0000338bool ARMBaseInstrInfo::
339PredicateInstruction(MachineInstr *MI,
340 const SmallVectorImpl<MachineOperand> &Pred) const {
341 unsigned Opc = MI->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000342 if (isUncondBranchOpcode(Opc)) {
343 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
David Goodwin334c2642009-07-08 16:09:28 +0000344 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
345 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
346 return true;
347 }
348
349 int PIdx = MI->findFirstPredOperandIdx();
350 if (PIdx != -1) {
351 MachineOperand &PMO = MI->getOperand(PIdx);
352 PMO.setImm(Pred[0].getImm());
353 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
354 return true;
355 }
356 return false;
357}
358
359bool ARMBaseInstrInfo::
360SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
361 const SmallVectorImpl<MachineOperand> &Pred2) const {
362 if (Pred1.size() > 2 || Pred2.size() > 2)
363 return false;
364
365 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
366 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
367 if (CC1 == CC2)
368 return true;
369
370 switch (CC1) {
371 default:
372 return false;
373 case ARMCC::AL:
374 return true;
375 case ARMCC::HS:
376 return CC2 == ARMCC::HI;
377 case ARMCC::LS:
378 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
379 case ARMCC::GE:
380 return CC2 == ARMCC::GT;
381 case ARMCC::LE:
382 return CC2 == ARMCC::LT;
383 }
384}
385
386bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
387 std::vector<MachineOperand> &Pred) const {
Evan Cheng8fb90362009-08-08 03:20:32 +0000388 // FIXME: This confuses implicit_def with optional CPSR def.
David Goodwin334c2642009-07-08 16:09:28 +0000389 const TargetInstrDesc &TID = MI->getDesc();
390 if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
391 return false;
392
393 bool Found = false;
394 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
395 const MachineOperand &MO = MI->getOperand(i);
396 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
397 Pred.push_back(MO);
398 Found = true;
399 }
400 }
401
402 return Found;
403}
404
Evan Chengac0869d2009-11-21 06:21:52 +0000405/// isPredicable - Return true if the specified instruction can be predicated.
406/// By default, this returns true for every instruction with a
407/// PredicateOperand.
408bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
409 const TargetInstrDesc &TID = MI->getDesc();
410 if (!TID.isPredicable())
411 return false;
412
413 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
414 ARMFunctionInfo *AFI =
415 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
Evan Chengd7f08102009-11-24 08:06:15 +0000416 return AFI->isThumb2Function();
Evan Chengac0869d2009-11-21 06:21:52 +0000417 }
418 return true;
419}
David Goodwin334c2642009-07-08 16:09:28 +0000420
Chris Lattner56856b12009-12-03 06:58:32 +0000421/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
422DISABLE_INLINE
David Goodwin334c2642009-07-08 16:09:28 +0000423static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattner56856b12009-12-03 06:58:32 +0000424 unsigned JTI);
David Goodwin334c2642009-07-08 16:09:28 +0000425static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
426 unsigned JTI) {
Chris Lattner56856b12009-12-03 06:58:32 +0000427 assert(JTI < JT.size());
David Goodwin334c2642009-07-08 16:09:28 +0000428 return JT[JTI].MBBs.size();
429}
430
431/// GetInstSize - Return the size of the specified MachineInstr.
432///
433unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
434 const MachineBasicBlock &MBB = *MI->getParent();
435 const MachineFunction *MF = MBB.getParent();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000436 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwin334c2642009-07-08 16:09:28 +0000437
438 // Basic size info comes from the TSFlags field.
439 const TargetInstrDesc &TID = MI->getDesc();
440 unsigned TSFlags = TID.TSFlags;
441
Evan Chenga0ee8622009-07-31 22:22:22 +0000442 unsigned Opc = MI->getOpcode();
David Goodwin334c2642009-07-08 16:09:28 +0000443 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
444 default: {
445 // If this machine instr is an inline asm, measure it.
446 if (MI->getOpcode() == ARM::INLINEASM)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000447 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Goodwin334c2642009-07-08 16:09:28 +0000448 if (MI->isLabel())
449 return 0;
Evan Chenga0ee8622009-07-31 22:22:22 +0000450 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000451 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000452 llvm_unreachable("Unknown or unset size field for instr!");
Chris Lattner518bb532010-02-09 19:54:29 +0000453 case TargetOpcode::IMPLICIT_DEF:
454 case TargetOpcode::KILL:
455 case TargetOpcode::DBG_LABEL:
456 case TargetOpcode::EH_LABEL:
David Goodwin334c2642009-07-08 16:09:28 +0000457 return 0;
458 }
459 break;
460 }
Evan Cheng78947622009-07-24 18:20:44 +0000461 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
462 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
463 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
David Goodwin334c2642009-07-08 16:09:28 +0000464 case ARMII::SizeSpecial: {
Evan Chenga0ee8622009-07-31 22:22:22 +0000465 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000466 case ARM::CONSTPOOL_ENTRY:
467 // If this machine instr is a constant pool entry, its size is recorded as
468 // operand #2.
469 return MI->getOperand(2).getImm();
Evan Cheng78947622009-07-24 18:20:44 +0000470 case ARM::Int_eh_sjlj_setjmp:
Jim Grosbachcdc17eb2009-08-11 17:08:15 +0000471 return 24;
Jim Grosbachd1228742009-12-01 18:10:36 +0000472 case ARM::tInt_eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +0000473 return 14;
Jim Grosbach5aa16842009-08-11 19:42:21 +0000474 case ARM::t2Int_eh_sjlj_setjmp:
Jim Grosbacha87ded22010-02-08 23:22:00 +0000475 return 14;
David Goodwin334c2642009-07-08 16:09:28 +0000476 case ARM::BR_JTr:
477 case ARM::BR_JTm:
478 case ARM::BR_JTadd:
Evan Chenga0ee8622009-07-31 22:22:22 +0000479 case ARM::tBR_JTr:
Evan Chengd26b14c2009-07-31 18:28:05 +0000480 case ARM::t2BR_JT:
481 case ARM::t2TBB:
482 case ARM::t2TBH: {
David Goodwin334c2642009-07-08 16:09:28 +0000483 // These are jumptable branches, i.e. a branch followed by an inlined
Evan Chengd26b14c2009-07-31 18:28:05 +0000484 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
485 // entry is one byte; TBH two byte each.
Evan Chenga0ee8622009-07-31 22:22:22 +0000486 unsigned EntrySize = (Opc == ARM::t2TBB)
487 ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4);
David Goodwin334c2642009-07-08 16:09:28 +0000488 unsigned NumOps = TID.getNumOperands();
489 MachineOperand JTOP =
490 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
491 unsigned JTI = JTOP.getIndex();
492 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000493 assert(MJTI != 0);
David Goodwin334c2642009-07-08 16:09:28 +0000494 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
495 assert(JTI < JT.size());
496 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
497 // 4 aligned. The assembler / linker may add 2 byte padding just before
498 // the JT entries. The size does not include this padding; the
499 // constant islands pass does separate bookkeeping for it.
500 // FIXME: If we know the size of the function is less than (1 << 16) *2
501 // bytes, we can use 16-bit entries instead. Then there won't be an
502 // alignment issue.
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000503 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
504 unsigned NumEntries = getNumJTEntries(JT, JTI);
505 if (Opc == ARM::t2TBB && (NumEntries & 1))
506 // Make sure the instruction that follows TBB is 2-byte aligned.
507 // FIXME: Constant island pass should insert an "ALIGN" instruction
508 // instead.
509 ++NumEntries;
510 return NumEntries * EntrySize + InstSize;
David Goodwin334c2642009-07-08 16:09:28 +0000511 }
512 default:
513 // Otherwise, pseudo-instruction sizes are zero.
514 return 0;
515 }
516 }
517 }
518 return 0; // Not reached
519}
520
521/// Return true if the instruction is a register to register move and
522/// leave the source and dest operands in the passed parameters.
523///
524bool
525ARMBaseInstrInfo::isMoveInstr(const MachineInstr &MI,
526 unsigned &SrcReg, unsigned &DstReg,
527 unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
528 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
529
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000530 switch (MI.getOpcode()) {
Evan Chengdced03f2009-07-27 00:24:36 +0000531 default: break;
Jim Grosbache5165492009-11-09 00:11:35 +0000532 case ARM::VMOVS:
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000533 case ARM::VMOVD:
Jim Grosbache5165492009-11-09 00:11:35 +0000534 case ARM::VMOVDneon:
Anton Korobeynikovf95215f2009-11-02 00:10:38 +0000535 case ARM::VMOVQ: {
David Goodwin334c2642009-07-08 16:09:28 +0000536 SrcReg = MI.getOperand(1).getReg();
537 DstReg = MI.getOperand(0).getReg();
538 return true;
539 }
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000540 case ARM::MOVr:
541 case ARM::tMOVr:
542 case ARM::tMOVgpr2tgpr:
543 case ARM::tMOVtgpr2gpr:
544 case ARM::tMOVgpr2gpr:
545 case ARM::t2MOVr: {
David Goodwin334c2642009-07-08 16:09:28 +0000546 assert(MI.getDesc().getNumOperands() >= 2 &&
547 MI.getOperand(0).isReg() &&
548 MI.getOperand(1).isReg() &&
549 "Invalid ARM MOV instruction");
550 SrcReg = MI.getOperand(1).getReg();
551 DstReg = MI.getOperand(0).getReg();
552 return true;
553 }
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000554 }
David Goodwin334c2642009-07-08 16:09:28 +0000555
556 return false;
557}
558
Jim Grosbach764ab522009-08-11 15:33:49 +0000559unsigned
David Goodwin334c2642009-07-08 16:09:28 +0000560ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
561 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000562 switch (MI->getOpcode()) {
563 default: break;
564 case ARM::LDR:
565 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000566 if (MI->getOperand(1).isFI() &&
567 MI->getOperand(2).isReg() &&
568 MI->getOperand(3).isImm() &&
569 MI->getOperand(2).getReg() == 0 &&
570 MI->getOperand(3).getImm() == 0) {
571 FrameIndex = MI->getOperand(1).getIndex();
572 return MI->getOperand(0).getReg();
573 }
Evan Chengdced03f2009-07-27 00:24:36 +0000574 break;
575 case ARM::t2LDRi12:
576 case ARM::tRestore:
David Goodwin5ff58b52009-07-24 00:16:18 +0000577 if (MI->getOperand(1).isFI() &&
578 MI->getOperand(2).isImm() &&
579 MI->getOperand(2).getImm() == 0) {
580 FrameIndex = MI->getOperand(1).getIndex();
581 return MI->getOperand(0).getReg();
582 }
Evan Chengdced03f2009-07-27 00:24:36 +0000583 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000584 case ARM::VLDRD:
585 case ARM::VLDRS:
David Goodwin334c2642009-07-08 16:09:28 +0000586 if (MI->getOperand(1).isFI() &&
587 MI->getOperand(2).isImm() &&
588 MI->getOperand(2).getImm() == 0) {
589 FrameIndex = MI->getOperand(1).getIndex();
590 return MI->getOperand(0).getReg();
591 }
Evan Chengdced03f2009-07-27 00:24:36 +0000592 break;
David Goodwin334c2642009-07-08 16:09:28 +0000593 }
594
595 return 0;
596}
597
598unsigned
599ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
600 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000601 switch (MI->getOpcode()) {
602 default: break;
603 case ARM::STR:
604 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000605 if (MI->getOperand(1).isFI() &&
606 MI->getOperand(2).isReg() &&
607 MI->getOperand(3).isImm() &&
608 MI->getOperand(2).getReg() == 0 &&
609 MI->getOperand(3).getImm() == 0) {
610 FrameIndex = MI->getOperand(1).getIndex();
611 return MI->getOperand(0).getReg();
612 }
Evan Chengdced03f2009-07-27 00:24:36 +0000613 break;
614 case ARM::t2STRi12:
615 case ARM::tSpill:
David Goodwin5ff58b52009-07-24 00:16:18 +0000616 if (MI->getOperand(1).isFI() &&
617 MI->getOperand(2).isImm() &&
618 MI->getOperand(2).getImm() == 0) {
619 FrameIndex = MI->getOperand(1).getIndex();
620 return MI->getOperand(0).getReg();
621 }
Evan Chengdced03f2009-07-27 00:24:36 +0000622 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000623 case ARM::VSTRD:
624 case ARM::VSTRS:
David Goodwin334c2642009-07-08 16:09:28 +0000625 if (MI->getOperand(1).isFI() &&
626 MI->getOperand(2).isImm() &&
627 MI->getOperand(2).getImm() == 0) {
628 FrameIndex = MI->getOperand(1).getIndex();
629 return MI->getOperand(0).getReg();
630 }
Evan Chengdced03f2009-07-27 00:24:36 +0000631 break;
David Goodwin334c2642009-07-08 16:09:28 +0000632 }
633
634 return 0;
635}
636
637bool
638ARMBaseInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
639 MachineBasicBlock::iterator I,
640 unsigned DestReg, unsigned SrcReg,
641 const TargetRegisterClass *DestRC,
642 const TargetRegisterClass *SrcRC) const {
643 DebugLoc DL = DebugLoc::getUnknownLoc();
644 if (I != MBB.end()) DL = I->getDebugLoc();
645
Bob Wilson1665b0a2010-02-16 17:24:15 +0000646 // tGPR is used sometimes in ARM instructions that need to avoid using
647 // certain registers. Just treat it as GPR here.
648 if (DestRC == ARM::tGPRRegisterClass)
649 DestRC = ARM::GPRRegisterClass;
650 if (SrcRC == ARM::tGPRRegisterClass)
651 SrcRC = ARM::GPRRegisterClass;
652
David Goodwin334c2642009-07-08 16:09:28 +0000653 if (DestRC != SrcRC) {
Evan Chengb4db6a42009-11-03 05:51:39 +0000654 if (DestRC->getSize() != SrcRC->getSize())
655 return false;
656
657 // Allow DPR / DPR_VFP2 / DPR_8 cross-class copies.
658 // Allow QPR / QPR_VFP2 / QPR_8 cross-class copies.
659 if (DestRC->getSize() != 8 && DestRC->getSize() != 16)
David Goodwin7bfdca02009-08-05 21:02:22 +0000660 return false;
David Goodwin334c2642009-07-08 16:09:28 +0000661 }
662
David Goodwin7bfdca02009-08-05 21:02:22 +0000663 if (DestRC == ARM::GPRRegisterClass) {
Evan Cheng08b93c62009-07-27 00:33:08 +0000664 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr),
Evan Chengdd6f6322009-07-11 06:37:27 +0000665 DestReg).addReg(SrcReg)));
David Goodwin7bfdca02009-08-05 21:02:22 +0000666 } else if (DestRC == ARM::SPRRegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000667 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VMOVS), DestReg)
David Goodwin334c2642009-07-08 16:09:28 +0000668 .addReg(SrcReg));
Evan Chengb4db6a42009-11-03 05:51:39 +0000669 } else if (DestRC == ARM::DPRRegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000670 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VMOVD), DestReg)
Evan Chengb4db6a42009-11-03 05:51:39 +0000671 .addReg(SrcReg));
Anton Korobeynikovf95215f2009-11-02 00:10:38 +0000672 } else if (DestRC == ARM::DPR_VFP2RegisterClass ||
673 DestRC == ARM::DPR_8RegisterClass ||
674 SrcRC == ARM::DPR_VFP2RegisterClass ||
675 SrcRC == ARM::DPR_8RegisterClass) {
676 // Always use neon reg-reg move if source or dest is NEON-only regclass.
Evan Chengac0869d2009-11-21 06:21:52 +0000677 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VMOVDneon),
678 DestReg).addReg(SrcReg));
Anton Korobeynikove56f9082009-09-12 22:21:08 +0000679 } else if (DestRC == ARM::QPRRegisterClass ||
Evan Chengb4db6a42009-11-03 05:51:39 +0000680 DestRC == ARM::QPR_VFP2RegisterClass ||
681 DestRC == ARM::QPR_8RegisterClass) {
Evan Chengac0869d2009-11-21 06:21:52 +0000682 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VMOVQ),
683 DestReg).addReg(SrcReg));
David Goodwin7bfdca02009-08-05 21:02:22 +0000684 } else {
David Goodwin334c2642009-07-08 16:09:28 +0000685 return false;
David Goodwin7bfdca02009-08-05 21:02:22 +0000686 }
David Goodwin334c2642009-07-08 16:09:28 +0000687
688 return true;
689}
690
691void ARMBaseInstrInfo::
692storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
693 unsigned SrcReg, bool isKill, int FI,
694 const TargetRegisterClass *RC) const {
695 DebugLoc DL = DebugLoc::getUnknownLoc();
696 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000697 MachineFunction &MF = *MBB.getParent();
698 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000699 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000700
701 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000702 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000703 MachineMemOperand::MOStore, 0,
704 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000705 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000706
Bob Wilson0eb0c742010-02-16 22:01:59 +0000707 // tGPR is used sometimes in ARM instructions that need to avoid using
708 // certain registers. Just treat it as GPR here.
709 if (RC == ARM::tGPRRegisterClass)
710 RC = ARM::GPRRegisterClass;
711
David Goodwin334c2642009-07-08 16:09:28 +0000712 if (RC == ARM::GPRRegisterClass) {
Evan Cheng5732ca02009-07-27 03:14:20 +0000713 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STR))
David Goodwin334c2642009-07-08 16:09:28 +0000714 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000715 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Anton Korobeynikov6ca0b9e2009-09-08 15:22:32 +0000716 } else if (RC == ARM::DPRRegisterClass ||
717 RC == ARM::DPR_VFP2RegisterClass ||
718 RC == ARM::DPR_8RegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000719 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000720 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000721 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000722 } else if (RC == ARM::SPRRegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000723 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
David Goodwin334c2642009-07-08 16:09:28 +0000724 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000725 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000726 } else {
Anton Korobeynikove56f9082009-09-12 22:21:08 +0000727 assert((RC == ARM::QPRRegisterClass ||
728 RC == ARM::QPR_VFP2RegisterClass) && "Unknown regclass!");
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000729 // FIXME: Neon instructions should support predicates
Jim Grosbach31bc8492009-11-08 00:27:19 +0000730 if (Align >= 16
Jim Grosbache45ab8a2010-01-19 18:31:11 +0000731 && (getRegisterInfo().canRealignStack(MF))) {
Evan Chengac0869d2009-11-21 06:21:52 +0000732 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64))
733 .addFrameIndex(FI).addImm(0).addImm(0).addImm(128)
734 .addMemOperand(MMO)
735 .addReg(SrcReg, getKillRegState(isKill)));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000736 } else {
Evan Chengac0869d2009-11-21 06:21:52 +0000737 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRQ)).
738 addReg(SrcReg, getKillRegState(isKill))
739 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000740 }
David Goodwin334c2642009-07-08 16:09:28 +0000741 }
742}
743
David Goodwin334c2642009-07-08 16:09:28 +0000744void ARMBaseInstrInfo::
745loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
746 unsigned DestReg, int FI,
747 const TargetRegisterClass *RC) const {
748 DebugLoc DL = DebugLoc::getUnknownLoc();
749 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000750 MachineFunction &MF = *MBB.getParent();
751 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000752 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000753
754 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000755 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000756 MachineMemOperand::MOLoad, 0,
757 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000758 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000759
Bob Wilson0eb0c742010-02-16 22:01:59 +0000760 // tGPR is used sometimes in ARM instructions that need to avoid using
761 // certain registers. Just treat it as GPR here.
762 if (RC == ARM::tGPRRegisterClass)
763 RC = ARM::GPRRegisterClass;
764
David Goodwin334c2642009-07-08 16:09:28 +0000765 if (RC == ARM::GPRRegisterClass) {
Evan Cheng5732ca02009-07-27 03:14:20 +0000766 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDR), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000767 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Anton Korobeynikov6ca0b9e2009-09-08 15:22:32 +0000768 } else if (RC == ARM::DPRRegisterClass ||
769 RC == ARM::DPR_VFP2RegisterClass ||
770 RC == ARM::DPR_8RegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000771 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000772 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000773 } else if (RC == ARM::SPRRegisterClass) {
Jim Grosbache5165492009-11-09 00:11:35 +0000774 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000775 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000776 } else {
Anton Korobeynikove56f9082009-09-12 22:21:08 +0000777 assert((RC == ARM::QPRRegisterClass ||
Evan Chengb4db6a42009-11-03 05:51:39 +0000778 RC == ARM::QPR_VFP2RegisterClass ||
779 RC == ARM::QPR_8RegisterClass) && "Unknown regclass!");
Jim Grosbach31bc8492009-11-08 00:27:19 +0000780 if (Align >= 16
Jim Grosbache45ab8a2010-01-19 18:31:11 +0000781 && (getRegisterInfo().canRealignStack(MF))) {
Evan Chengac0869d2009-11-21 06:21:52 +0000782 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
783 .addFrameIndex(FI).addImm(0).addImm(0).addImm(128)
784 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000785 } else {
Evan Chengac0869d2009-11-21 06:21:52 +0000786 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRQ), DestReg)
787 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000788 }
David Goodwin334c2642009-07-08 16:09:28 +0000789 }
790}
791
David Goodwin334c2642009-07-08 16:09:28 +0000792MachineInstr *ARMBaseInstrInfo::
793foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
794 const SmallVectorImpl<unsigned> &Ops, int FI) const {
795 if (Ops.size() != 1) return NULL;
796
797 unsigned OpNum = Ops[0];
798 unsigned Opc = MI->getOpcode();
799 MachineInstr *NewMI = NULL;
Evan Cheng19068ba2009-08-10 06:32:05 +0000800 if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
David Goodwin334c2642009-07-08 16:09:28 +0000801 // If it is updating CPSR, then it cannot be folded.
Evan Cheng19068ba2009-08-10 06:32:05 +0000802 if (MI->getOperand(4).getReg() == ARM::CPSR && !MI->getOperand(4).isDead())
803 return NULL;
804 unsigned Pred = MI->getOperand(2).getImm();
805 unsigned PredReg = MI->getOperand(3).getReg();
806 if (OpNum == 0) { // move -> store
807 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000808 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000809 bool isKill = MI->getOperand(1).isKill();
810 bool isUndef = MI->getOperand(1).isUndef();
811 if (Opc == ARM::MOVr)
812 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::STR))
Evan Chenged3ad212009-10-25 07:52:27 +0000813 .addReg(SrcReg,
814 getKillRegState(isKill) | getUndefRegState(isUndef),
815 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000816 .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
817 else // ARM::t2MOVr
818 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
Evan Chenged3ad212009-10-25 07:52:27 +0000819 .addReg(SrcReg,
820 getKillRegState(isKill) | getUndefRegState(isUndef),
821 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000822 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
823 } else { // move -> load
824 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000825 unsigned DstSubReg = MI->getOperand(0).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000826 bool isDead = MI->getOperand(0).isDead();
827 bool isUndef = MI->getOperand(0).isUndef();
828 if (Opc == ARM::MOVr)
829 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::LDR))
830 .addReg(DstReg,
831 RegState::Define |
832 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +0000833 getUndefRegState(isUndef), DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000834 .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
835 else // ARM::t2MOVr
836 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
837 .addReg(DstReg,
838 RegState::Define |
839 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +0000840 getUndefRegState(isUndef), DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000841 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
David Goodwin334c2642009-07-08 16:09:28 +0000842 }
Evan Cheng19068ba2009-08-10 06:32:05 +0000843 } else if (Opc == ARM::tMOVgpr2gpr ||
844 Opc == ARM::tMOVtgpr2gpr ||
845 Opc == ARM::tMOVgpr2tgpr) {
846 if (OpNum == 0) { // move -> store
847 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000848 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000849 bool isKill = MI->getOperand(1).isKill();
850 bool isUndef = MI->getOperand(1).isUndef();
851 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
Evan Chenged3ad212009-10-25 07:52:27 +0000852 .addReg(SrcReg,
853 getKillRegState(isKill) | getUndefRegState(isUndef),
854 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000855 .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
856 } else { // move -> load
857 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000858 unsigned DstSubReg = MI->getOperand(0).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +0000859 bool isDead = MI->getOperand(0).isDead();
860 bool isUndef = MI->getOperand(0).isUndef();
861 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
862 .addReg(DstReg,
863 RegState::Define |
864 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +0000865 getUndefRegState(isUndef),
866 DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +0000867 .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
868 }
Jim Grosbache5165492009-11-09 00:11:35 +0000869 } else if (Opc == ARM::VMOVS) {
David Goodwin334c2642009-07-08 16:09:28 +0000870 unsigned Pred = MI->getOperand(2).getImm();
871 unsigned PredReg = MI->getOperand(3).getReg();
872 if (OpNum == 0) { // move -> store
873 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000874 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000875 bool isKill = MI->getOperand(1).isKill();
876 bool isUndef = MI->getOperand(1).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +0000877 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTRS))
Evan Chenged3ad212009-10-25 07:52:27 +0000878 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef),
879 SrcSubReg)
David Goodwin334c2642009-07-08 16:09:28 +0000880 .addFrameIndex(FI)
881 .addImm(0).addImm(Pred).addReg(PredReg);
882 } else { // move -> load
883 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000884 unsigned DstSubReg = MI->getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000885 bool isDead = MI->getOperand(0).isDead();
886 bool isUndef = MI->getOperand(0).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +0000887 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDRS))
David Goodwin334c2642009-07-08 16:09:28 +0000888 .addReg(DstReg,
889 RegState::Define |
890 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +0000891 getUndefRegState(isUndef),
892 DstSubReg)
David Goodwin334c2642009-07-08 16:09:28 +0000893 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
894 }
895 }
Jim Grosbache5165492009-11-09 00:11:35 +0000896 else if (Opc == ARM::VMOVD) {
David Goodwin334c2642009-07-08 16:09:28 +0000897 unsigned Pred = MI->getOperand(2).getImm();
898 unsigned PredReg = MI->getOperand(3).getReg();
899 if (OpNum == 0) { // move -> store
900 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000901 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000902 bool isKill = MI->getOperand(1).isKill();
903 bool isUndef = MI->getOperand(1).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +0000904 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTRD))
Evan Chenged3ad212009-10-25 07:52:27 +0000905 .addReg(SrcReg,
906 getKillRegState(isKill) | getUndefRegState(isUndef),
907 SrcSubReg)
David Goodwin334c2642009-07-08 16:09:28 +0000908 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
909 } else { // move -> load
910 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +0000911 unsigned DstSubReg = MI->getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000912 bool isDead = MI->getOperand(0).isDead();
913 bool isUndef = MI->getOperand(0).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +0000914 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDRD))
David Goodwin334c2642009-07-08 16:09:28 +0000915 .addReg(DstReg,
916 RegState::Define |
917 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +0000918 getUndefRegState(isUndef),
919 DstSubReg)
David Goodwin334c2642009-07-08 16:09:28 +0000920 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
921 }
922 }
923
924 return NewMI;
925}
926
Jim Grosbach764ab522009-08-11 15:33:49 +0000927MachineInstr*
David Goodwin334c2642009-07-08 16:09:28 +0000928ARMBaseInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
929 MachineInstr* MI,
930 const SmallVectorImpl<unsigned> &Ops,
931 MachineInstr* LoadMI) const {
Evan Cheng1f5c9882009-07-27 04:18:04 +0000932 // FIXME
David Goodwin334c2642009-07-08 16:09:28 +0000933 return 0;
934}
935
936bool
937ARMBaseInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
Evan Cheng22946452009-08-10 05:51:48 +0000938 const SmallVectorImpl<unsigned> &Ops) const {
David Goodwin334c2642009-07-08 16:09:28 +0000939 if (Ops.size() != 1) return false;
940
941 unsigned Opc = MI->getOpcode();
Evan Cheng5732ca02009-07-27 03:14:20 +0000942 if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
David Goodwin334c2642009-07-08 16:09:28 +0000943 // If it is updating CPSR, then it cannot be folded.
Evan Cheng22946452009-08-10 05:51:48 +0000944 return MI->getOperand(4).getReg() != ARM::CPSR ||
945 MI->getOperand(4).isDead();
Evan Cheng19068ba2009-08-10 06:32:05 +0000946 } else if (Opc == ARM::tMOVgpr2gpr ||
947 Opc == ARM::tMOVtgpr2gpr ||
948 Opc == ARM::tMOVgpr2tgpr) {
949 return true;
Jim Grosbache5165492009-11-09 00:11:35 +0000950 } else if (Opc == ARM::VMOVS || Opc == ARM::VMOVD) {
David Goodwin334c2642009-07-08 16:09:28 +0000951 return true;
Jim Grosbache5165492009-11-09 00:11:35 +0000952 } else if (Opc == ARM::VMOVDneon || Opc == ARM::VMOVQ) {
David Goodwin334c2642009-07-08 16:09:28 +0000953 return false; // FIXME
954 }
955
956 return false;
957}
Evan Cheng5ca53a72009-07-27 18:20:05 +0000958
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000959/// Create a copy of a const pool value. Update CPI to the new index and return
960/// the label UID.
961static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
962 MachineConstantPool *MCP = MF.getConstantPool();
963 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
964
965 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
966 assert(MCPE.isMachineConstantPoolEntry() &&
967 "Expecting a machine constantpool entry!");
968 ARMConstantPoolValue *ACPV =
969 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
970
971 unsigned PCLabelId = AFI->createConstPoolEntryUId();
972 ARMConstantPoolValue *NewCPV = 0;
973 if (ACPV->isGlobalValue())
974 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
975 ARMCP::CPValue, 4);
976 else if (ACPV->isExtSymbol())
977 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
978 ACPV->getSymbol(), PCLabelId, 4);
979 else if (ACPV->isBlockAddress())
980 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
981 ARMCP::CPBlockAddress, 4);
982 else
983 llvm_unreachable("Unexpected ARM constantpool value type!!");
984 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
985 return PCLabelId;
986}
987
Evan Chengfdc83402009-11-08 00:15:23 +0000988void ARMBaseInstrInfo::
989reMaterialize(MachineBasicBlock &MBB,
990 MachineBasicBlock::iterator I,
991 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +0000992 const MachineInstr *Orig,
993 const TargetRegisterInfo *TRI) const {
Evan Chengd57cdd52009-11-14 02:55:43 +0000994 if (SubIdx && TargetRegisterInfo::isPhysicalRegister(DestReg)) {
995 DestReg = TRI->getSubReg(DestReg, SubIdx);
996 SubIdx = 0;
997 }
998
Evan Chengfdc83402009-11-08 00:15:23 +0000999 unsigned Opcode = Orig->getOpcode();
1000 switch (Opcode) {
1001 default: {
1002 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
1003 MI->getOperand(0).setReg(DestReg);
1004 MBB.insert(I, MI);
1005 break;
1006 }
1007 case ARM::tLDRpci_pic:
1008 case ARM::t2LDRpci_pic: {
1009 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +00001010 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001011 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +00001012 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1013 DestReg)
1014 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1015 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1016 break;
1017 }
1018 }
1019
1020 MachineInstr *NewMI = prior(I);
1021 NewMI->getOperand(0).setSubReg(SubIdx);
1022}
1023
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001024MachineInstr *
1025ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1026 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1027 switch(Orig->getOpcode()) {
1028 case ARM::tLDRpci_pic:
1029 case ARM::t2LDRpci_pic: {
1030 unsigned CPI = Orig->getOperand(1).getIndex();
1031 unsigned PCLabelId = duplicateCPV(MF, CPI);
1032 Orig->getOperand(1).setIndex(CPI);
1033 Orig->getOperand(2).setImm(PCLabelId);
1034 break;
1035 }
1036 }
1037 return MI;
1038}
1039
Evan Cheng506049f2010-03-03 01:44:33 +00001040bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1041 const MachineInstr *MI1) const {
Evan Chengd457e6e2009-11-07 04:04:34 +00001042 int Opcode = MI0->getOpcode();
Evan Cheng9b824252009-11-20 02:10:27 +00001043 if (Opcode == ARM::t2LDRpci ||
1044 Opcode == ARM::t2LDRpci_pic ||
1045 Opcode == ARM::tLDRpci ||
1046 Opcode == ARM::tLDRpci_pic) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001047 if (MI1->getOpcode() != Opcode)
1048 return false;
1049 if (MI0->getNumOperands() != MI1->getNumOperands())
1050 return false;
1051
1052 const MachineOperand &MO0 = MI0->getOperand(1);
1053 const MachineOperand &MO1 = MI1->getOperand(1);
1054 if (MO0.getOffset() != MO1.getOffset())
1055 return false;
1056
1057 const MachineFunction *MF = MI0->getParent()->getParent();
1058 const MachineConstantPool *MCP = MF->getConstantPool();
1059 int CPI0 = MO0.getIndex();
1060 int CPI1 = MO1.getIndex();
1061 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1062 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1063 ARMConstantPoolValue *ACPV0 =
1064 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1065 ARMConstantPoolValue *ACPV1 =
1066 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1067 return ACPV0->hasSameValue(ACPV1);
1068 }
1069
Evan Cheng506049f2010-03-03 01:44:33 +00001070 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001071}
1072
Evan Cheng8fb90362009-08-08 03:20:32 +00001073/// getInstrPredicate - If instruction is predicated, returns its predicate
1074/// condition, otherwise returns AL. It also returns the condition code
1075/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001076ARMCC::CondCodes
1077llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001078 int PIdx = MI->findFirstPredOperandIdx();
1079 if (PIdx == -1) {
1080 PredReg = 0;
1081 return ARMCC::AL;
1082 }
1083
1084 PredReg = MI->getOperand(PIdx+1).getReg();
1085 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1086}
1087
1088
Evan Cheng6495f632009-07-28 05:48:47 +00001089int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001090 if (Opc == ARM::B)
1091 return ARM::Bcc;
1092 else if (Opc == ARM::tB)
1093 return ARM::tBcc;
1094 else if (Opc == ARM::t2B)
1095 return ARM::t2Bcc;
1096
1097 llvm_unreachable("Unknown unconditional branch opcode!");
1098 return 0;
1099}
1100
Evan Cheng6495f632009-07-28 05:48:47 +00001101
1102void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1103 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1104 unsigned DestReg, unsigned BaseReg, int NumBytes,
1105 ARMCC::CondCodes Pred, unsigned PredReg,
1106 const ARMBaseInstrInfo &TII) {
1107 bool isSub = NumBytes < 0;
1108 if (isSub) NumBytes = -NumBytes;
1109
1110 while (NumBytes) {
1111 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1112 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1113 assert(ThisVal && "Didn't extract field correctly");
1114
1115 // We will handle these bits from offset, clear them.
1116 NumBytes &= ~ThisVal;
1117
1118 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1119
1120 // Build the new ADD / SUB.
1121 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1122 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1123 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1124 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1125 BaseReg = DestReg;
1126 }
1127}
1128
Evan Chengcdbb3f52009-08-27 01:23:50 +00001129bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1130 unsigned FrameReg, int &Offset,
1131 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001132 unsigned Opcode = MI.getOpcode();
1133 const TargetInstrDesc &Desc = MI.getDesc();
1134 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1135 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001136
Evan Cheng6495f632009-07-28 05:48:47 +00001137 // Memory operands in inline assembly always use AddrMode2.
1138 if (Opcode == ARM::INLINEASM)
1139 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001140
Evan Cheng6495f632009-07-28 05:48:47 +00001141 if (Opcode == ARM::ADDri) {
1142 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1143 if (Offset == 0) {
1144 // Turn it into a move.
1145 MI.setDesc(TII.get(ARM::MOVr));
1146 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1147 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001148 Offset = 0;
1149 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001150 } else if (Offset < 0) {
1151 Offset = -Offset;
1152 isSub = true;
1153 MI.setDesc(TII.get(ARM::SUBri));
1154 }
1155
1156 // Common case: small offset, fits into instruction.
1157 if (ARM_AM::getSOImmVal(Offset) != -1) {
1158 // Replace the FrameIndex with sp / fp
1159 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1160 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001161 Offset = 0;
1162 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001163 }
1164
1165 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1166 // as possible.
1167 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1168 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1169
1170 // We will handle these bits from offset, clear them.
1171 Offset &= ~ThisImmVal;
1172
1173 // Get the properly encoded SOImmVal field.
1174 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1175 "Bit extraction didn't work?");
1176 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1177 } else {
1178 unsigned ImmIdx = 0;
1179 int InstrOffs = 0;
1180 unsigned NumBits = 0;
1181 unsigned Scale = 1;
1182 switch (AddrMode) {
1183 case ARMII::AddrMode2: {
1184 ImmIdx = FrameRegIdx+2;
1185 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1186 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1187 InstrOffs *= -1;
1188 NumBits = 12;
1189 break;
1190 }
1191 case ARMII::AddrMode3: {
1192 ImmIdx = FrameRegIdx+2;
1193 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1194 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1195 InstrOffs *= -1;
1196 NumBits = 8;
1197 break;
1198 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001199 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001200 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001201 // Can't fold any offset even if it's zero.
1202 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001203 case ARMII::AddrMode5: {
1204 ImmIdx = FrameRegIdx+1;
1205 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1206 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1207 InstrOffs *= -1;
1208 NumBits = 8;
1209 Scale = 4;
1210 break;
1211 }
1212 default:
1213 llvm_unreachable("Unsupported addressing mode!");
1214 break;
1215 }
1216
1217 Offset += InstrOffs * Scale;
1218 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1219 if (Offset < 0) {
1220 Offset = -Offset;
1221 isSub = true;
1222 }
1223
1224 // Attempt to fold address comp. if opcode has offset bits
1225 if (NumBits > 0) {
1226 // Common case: small offset, fits into instruction.
1227 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1228 int ImmedOffset = Offset / Scale;
1229 unsigned Mask = (1 << NumBits) - 1;
1230 if ((unsigned)Offset <= Mask * Scale) {
1231 // Replace the FrameIndex with sp
1232 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1233 if (isSub)
1234 ImmedOffset |= 1 << NumBits;
1235 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001236 Offset = 0;
1237 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001238 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001239
Evan Cheng6495f632009-07-28 05:48:47 +00001240 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1241 ImmedOffset = ImmedOffset & Mask;
1242 if (isSub)
1243 ImmedOffset |= 1 << NumBits;
1244 ImmOp.ChangeToImmediate(ImmedOffset);
1245 Offset &= ~(Mask*Scale);
1246 }
1247 }
1248
Evan Chengcdbb3f52009-08-27 01:23:50 +00001249 Offset = (isSub) ? -Offset : Offset;
1250 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001251}