blob: b9bc0b8f220cf99b367a4dab5111ae3c8c5c07e8 [file] [log] [blame]
Jim Grosbach31c24bf2009-11-07 22:00:39 +00001//===- ARMBaseInstrInfo.cpp - ARM Instruction Information -------*- C++ -*-===//
David Goodwin334c2642009-07-08 16:09:28 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Base ARM implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARMBaseInstrInfo.h"
15#include "ARM.h"
16#include "ARMAddressingModes.h"
Evan Chengd457e6e2009-11-07 04:04:34 +000017#include "ARMConstantPoolValue.h"
David Goodwin334c2642009-07-08 16:09:28 +000018#include "ARMGenInstrInfo.inc"
19#include "ARMMachineFunctionInfo.h"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000020#include "ARMRegisterInfo.h"
Evan Chengfdc83402009-11-08 00:15:23 +000021#include "llvm/Constants.h"
22#include "llvm/Function.h"
23#include "llvm/GlobalValue.h"
David Goodwin334c2642009-07-08 16:09:28 +000024#include "llvm/ADT/STLExtras.h"
25#include "llvm/CodeGen/LiveVariables.h"
Evan Chengd457e6e2009-11-07 04:04:34 +000026#include "llvm/CodeGen/MachineConstantPool.h"
David Goodwin334c2642009-07-08 16:09:28 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov249fb332009-10-07 00:06:35 +000030#include "llvm/CodeGen/MachineMemOperand.h"
Evan Cheng2457f2c2010-05-22 01:47:14 +000031#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikov249fb332009-10-07 00:06:35 +000032#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000033#include "llvm/MC/MCAsmInfo.h"
David Goodwin334c2642009-07-08 16:09:28 +000034#include "llvm/Support/CommandLine.h"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000035#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000036#include "llvm/Support/ErrorHandling.h"
David Goodwin334c2642009-07-08 16:09:28 +000037using namespace llvm;
38
39static cl::opt<bool>
40EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
41 cl::desc("Enable ARM 2-addr to 3-addr conv"));
42
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000043ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
44 : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)),
45 Subtarget(STI) {
David Goodwin334c2642009-07-08 16:09:28 +000046}
47
48MachineInstr *
49ARMBaseInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
50 MachineBasicBlock::iterator &MBBI,
51 LiveVariables *LV) const {
Evan Cheng78703dd2009-07-27 18:44:00 +000052 // FIXME: Thumb2 support.
53
David Goodwin334c2642009-07-08 16:09:28 +000054 if (!EnableARM3Addr)
55 return NULL;
56
57 MachineInstr *MI = MBBI;
58 MachineFunction &MF = *MI->getParent()->getParent();
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +000059 uint64_t TSFlags = MI->getDesc().TSFlags;
David Goodwin334c2642009-07-08 16:09:28 +000060 bool isPre = false;
61 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
62 default: return NULL;
63 case ARMII::IndexModePre:
64 isPre = true;
65 break;
66 case ARMII::IndexModePost:
67 break;
68 }
69
70 // Try splitting an indexed load/store to an un-indexed one plus an add/sub
71 // operation.
72 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
73 if (MemOpc == 0)
74 return NULL;
75
76 MachineInstr *UpdateMI = NULL;
77 MachineInstr *MemMI = NULL;
78 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
79 const TargetInstrDesc &TID = MI->getDesc();
80 unsigned NumOps = TID.getNumOperands();
81 bool isLoad = !TID.mayStore();
82 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
83 const MachineOperand &Base = MI->getOperand(2);
84 const MachineOperand &Offset = MI->getOperand(NumOps-3);
85 unsigned WBReg = WB.getReg();
86 unsigned BaseReg = Base.getReg();
87 unsigned OffReg = Offset.getReg();
88 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
89 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
90 switch (AddrMode) {
91 default:
92 assert(false && "Unknown indexed op!");
93 return NULL;
94 case ARMII::AddrMode2: {
95 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
96 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
97 if (OffReg == 0) {
Evan Chenge7cbe412009-07-08 21:03:57 +000098 if (ARM_AM::getSOImmVal(Amt) == -1)
David Goodwin334c2642009-07-08 16:09:28 +000099 // Can't encode it in a so_imm operand. This transformation will
100 // add more than 1 instruction. Abandon!
101 return NULL;
102 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000103 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
Evan Chenge7cbe412009-07-08 21:03:57 +0000104 .addReg(BaseReg).addImm(Amt)
David Goodwin334c2642009-07-08 16:09:28 +0000105 .addImm(Pred).addReg(0).addReg(0);
106 } else if (Amt != 0) {
107 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
108 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
109 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000110 get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000111 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
112 .addImm(Pred).addReg(0).addReg(0);
113 } else
114 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000115 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000116 .addReg(BaseReg).addReg(OffReg)
117 .addImm(Pred).addReg(0).addReg(0);
118 break;
119 }
120 case ARMII::AddrMode3 : {
121 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
122 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
123 if (OffReg == 0)
124 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
125 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000126 get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000127 .addReg(BaseReg).addImm(Amt)
128 .addImm(Pred).addReg(0).addReg(0);
129 else
130 UpdateMI = BuildMI(MF, MI->getDebugLoc(),
Evan Cheng78703dd2009-07-27 18:44:00 +0000131 get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
David Goodwin334c2642009-07-08 16:09:28 +0000132 .addReg(BaseReg).addReg(OffReg)
133 .addImm(Pred).addReg(0).addReg(0);
134 break;
135 }
136 }
137
138 std::vector<MachineInstr*> NewMIs;
139 if (isPre) {
140 if (isLoad)
141 MemMI = BuildMI(MF, MI->getDebugLoc(),
142 get(MemOpc), MI->getOperand(0).getReg())
143 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
144 else
145 MemMI = BuildMI(MF, MI->getDebugLoc(),
146 get(MemOpc)).addReg(MI->getOperand(1).getReg())
147 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
148 NewMIs.push_back(MemMI);
149 NewMIs.push_back(UpdateMI);
150 } else {
151 if (isLoad)
152 MemMI = BuildMI(MF, MI->getDebugLoc(),
153 get(MemOpc), MI->getOperand(0).getReg())
154 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
155 else
156 MemMI = BuildMI(MF, MI->getDebugLoc(),
157 get(MemOpc)).addReg(MI->getOperand(1).getReg())
158 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
159 if (WB.isDead())
160 UpdateMI->getOperand(0).setIsDead();
161 NewMIs.push_back(UpdateMI);
162 NewMIs.push_back(MemMI);
163 }
164
165 // Transfer LiveVariables states, kill / dead info.
166 if (LV) {
167 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
168 MachineOperand &MO = MI->getOperand(i);
169 if (MO.isReg() && MO.getReg() &&
170 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
171 unsigned Reg = MO.getReg();
172
173 LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
174 if (MO.isDef()) {
175 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
176 if (MO.isDead())
177 LV->addVirtualRegisterDead(Reg, NewMI);
178 }
179 if (MO.isUse() && MO.isKill()) {
180 for (unsigned j = 0; j < 2; ++j) {
181 // Look at the two new MI's in reverse order.
182 MachineInstr *NewMI = NewMIs[j];
183 if (!NewMI->readsRegister(Reg))
184 continue;
185 LV->addVirtualRegisterKilled(Reg, NewMI);
186 if (VI.removeKill(MI))
187 VI.Kills.push_back(NewMI);
188 break;
189 }
190 }
191 }
192 }
193 }
194
195 MFI->insert(MBBI, NewMIs[1]);
196 MFI->insert(MBBI, NewMIs[0]);
197 return NewMIs[0];
198}
199
Evan Cheng2457f2c2010-05-22 01:47:14 +0000200bool
201ARMBaseInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Jim Grosbach18f30e62010-06-02 21:53:11 +0000202 MachineBasicBlock::iterator MI,
203 const std::vector<CalleeSavedInfo> &CSI,
204 const TargetRegisterInfo *TRI) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +0000205 if (CSI.empty())
206 return false;
207
208 DebugLoc DL;
209 if (MI != MBB.end()) DL = MI->getDebugLoc();
210
211 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
212 unsigned Reg = CSI[i].getReg();
213 bool isKill = true;
214
215 // Add the callee-saved register as live-in unless it's LR and
216 // @llvm.returnaddress is called. If LR is returned for @llvm.returnaddress
217 // then it's already added to the function and entry block live-in sets.
218 if (Reg == ARM::LR) {
219 MachineFunction &MF = *MBB.getParent();
220 if (MF.getFrameInfo()->isReturnAddressTaken() &&
221 MF.getRegInfo().isLiveIn(Reg))
222 isKill = false;
223 }
224
225 if (isKill)
226 MBB.addLiveIn(Reg);
227
228 // Insert the spill to the stack frame. The register is killed at the spill
229 //
Rafael Espindola42d075c2010-06-02 20:02:30 +0000230 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000231 storeRegToStackSlot(MBB, MI, Reg, isKill,
Rafael Espindola42d075c2010-06-02 20:02:30 +0000232 CSI[i].getFrameIdx(), RC, TRI);
Evan Cheng2457f2c2010-05-22 01:47:14 +0000233 }
234 return true;
235}
236
David Goodwin334c2642009-07-08 16:09:28 +0000237// Branch analysis.
238bool
239ARMBaseInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
240 MachineBasicBlock *&FBB,
241 SmallVectorImpl<MachineOperand> &Cond,
242 bool AllowModify) const {
243 // If the block has no terminators, it just falls into the block after it.
244 MachineBasicBlock::iterator I = MBB.end();
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000245 if (I == MBB.begin())
246 return false;
247 --I;
248 while (I->isDebugValue()) {
249 if (I == MBB.begin())
250 return false;
251 --I;
252 }
253 if (!isUnpredicatedTerminator(I))
David Goodwin334c2642009-07-08 16:09:28 +0000254 return false;
255
256 // Get the last instruction in the block.
257 MachineInstr *LastInst = I;
258
259 // If there is only one terminator instruction, process it.
260 unsigned LastOpc = LastInst->getOpcode();
261 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Evan Cheng5ca53a72009-07-27 18:20:05 +0000262 if (isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000263 TBB = LastInst->getOperand(0).getMBB();
264 return false;
265 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000266 if (isCondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000267 // Block ends with fall-through condbranch.
268 TBB = LastInst->getOperand(0).getMBB();
269 Cond.push_back(LastInst->getOperand(1));
270 Cond.push_back(LastInst->getOperand(2));
271 return false;
272 }
273 return true; // Can't handle indirect branch.
274 }
275
276 // Get the instruction before it if it is a terminator.
277 MachineInstr *SecondLastInst = I;
278
279 // If there are three terminators, we don't know what sort of block this is.
280 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
281 return true;
282
Evan Cheng5ca53a72009-07-27 18:20:05 +0000283 // If the block ends with a B and a Bcc, handle it.
David Goodwin334c2642009-07-08 16:09:28 +0000284 unsigned SecondLastOpc = SecondLastInst->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000285 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000286 TBB = SecondLastInst->getOperand(0).getMBB();
287 Cond.push_back(SecondLastInst->getOperand(1));
288 Cond.push_back(SecondLastInst->getOperand(2));
289 FBB = LastInst->getOperand(0).getMBB();
290 return false;
291 }
292
293 // If the block ends with two unconditional branches, handle it. The second
294 // one is not executed, so remove it.
Evan Cheng5ca53a72009-07-27 18:20:05 +0000295 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000296 TBB = SecondLastInst->getOperand(0).getMBB();
297 I = LastInst;
298 if (AllowModify)
299 I->eraseFromParent();
300 return false;
301 }
302
303 // ...likewise if it ends with a branch table followed by an unconditional
304 // branch. The branch folder can create these, and we must get rid of them for
305 // correctness of Thumb constant islands.
Bob Wilson8d4de5a2009-10-28 18:26:41 +0000306 if ((isJumpTableBranchOpcode(SecondLastOpc) ||
307 isIndirectBranchOpcode(SecondLastOpc)) &&
Evan Cheng5ca53a72009-07-27 18:20:05 +0000308 isUncondBranchOpcode(LastOpc)) {
David Goodwin334c2642009-07-08 16:09:28 +0000309 I = LastInst;
310 if (AllowModify)
311 I->eraseFromParent();
312 return true;
313 }
314
315 // Otherwise, can't handle this.
316 return true;
317}
318
319
320unsigned ARMBaseInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
David Goodwin334c2642009-07-08 16:09:28 +0000321 MachineBasicBlock::iterator I = MBB.end();
322 if (I == MBB.begin()) return 0;
323 --I;
Dale Johannesen93d6a7e2010-04-02 01:38:09 +0000324 while (I->isDebugValue()) {
325 if (I == MBB.begin())
326 return 0;
327 --I;
328 }
Evan Cheng5ca53a72009-07-27 18:20:05 +0000329 if (!isUncondBranchOpcode(I->getOpcode()) &&
330 !isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000331 return 0;
332
333 // Remove the branch.
334 I->eraseFromParent();
335
336 I = MBB.end();
337
338 if (I == MBB.begin()) return 1;
339 --I;
Evan Cheng5ca53a72009-07-27 18:20:05 +0000340 if (!isCondBranchOpcode(I->getOpcode()))
David Goodwin334c2642009-07-08 16:09:28 +0000341 return 1;
342
343 // Remove the branch.
344 I->eraseFromParent();
345 return 2;
346}
347
348unsigned
349ARMBaseInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000350 MachineBasicBlock *FBB,
351 const SmallVectorImpl<MachineOperand> &Cond,
352 DebugLoc DL) const {
Evan Cheng6495f632009-07-28 05:48:47 +0000353 ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
354 int BOpc = !AFI->isThumbFunction()
355 ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
356 int BccOpc = !AFI->isThumbFunction()
357 ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
David Goodwin334c2642009-07-08 16:09:28 +0000358
359 // Shouldn't be a fall through.
360 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
361 assert((Cond.size() == 2 || Cond.size() == 0) &&
362 "ARM branch conditions have two components!");
363
364 if (FBB == 0) {
365 if (Cond.empty()) // Unconditional branch?
Stuart Hastings3bf91252010-06-17 22:43:56 +0000366 BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
David Goodwin334c2642009-07-08 16:09:28 +0000367 else
Stuart Hastings3bf91252010-06-17 22:43:56 +0000368 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000369 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
370 return 1;
371 }
372
373 // Two-way conditional branch.
Stuart Hastings3bf91252010-06-17 22:43:56 +0000374 BuildMI(&MBB, DL, get(BccOpc)).addMBB(TBB)
David Goodwin334c2642009-07-08 16:09:28 +0000375 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
Stuart Hastings3bf91252010-06-17 22:43:56 +0000376 BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
David Goodwin334c2642009-07-08 16:09:28 +0000377 return 2;
378}
379
380bool ARMBaseInstrInfo::
381ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
382 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
383 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
384 return false;
385}
386
David Goodwin334c2642009-07-08 16:09:28 +0000387bool ARMBaseInstrInfo::
388PredicateInstruction(MachineInstr *MI,
389 const SmallVectorImpl<MachineOperand> &Pred) const {
390 unsigned Opc = MI->getOpcode();
Evan Cheng5ca53a72009-07-27 18:20:05 +0000391 if (isUncondBranchOpcode(Opc)) {
392 MI->setDesc(get(getMatchingCondBranchOpcode(Opc)));
David Goodwin334c2642009-07-08 16:09:28 +0000393 MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
394 MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
395 return true;
396 }
397
398 int PIdx = MI->findFirstPredOperandIdx();
399 if (PIdx != -1) {
400 MachineOperand &PMO = MI->getOperand(PIdx);
401 PMO.setImm(Pred[0].getImm());
402 MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
403 return true;
404 }
405 return false;
406}
407
408bool ARMBaseInstrInfo::
409SubsumesPredicate(const SmallVectorImpl<MachineOperand> &Pred1,
410 const SmallVectorImpl<MachineOperand> &Pred2) const {
411 if (Pred1.size() > 2 || Pred2.size() > 2)
412 return false;
413
414 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
415 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
416 if (CC1 == CC2)
417 return true;
418
419 switch (CC1) {
420 default:
421 return false;
422 case ARMCC::AL:
423 return true;
424 case ARMCC::HS:
425 return CC2 == ARMCC::HI;
426 case ARMCC::LS:
427 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
428 case ARMCC::GE:
429 return CC2 == ARMCC::GT;
430 case ARMCC::LE:
431 return CC2 == ARMCC::LT;
432 }
433}
434
435bool ARMBaseInstrInfo::DefinesPredicate(MachineInstr *MI,
436 std::vector<MachineOperand> &Pred) const {
Evan Cheng8fb90362009-08-08 03:20:32 +0000437 // FIXME: This confuses implicit_def with optional CPSR def.
David Goodwin334c2642009-07-08 16:09:28 +0000438 const TargetInstrDesc &TID = MI->getDesc();
439 if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
440 return false;
441
442 bool Found = false;
443 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
444 const MachineOperand &MO = MI->getOperand(i);
445 if (MO.isReg() && MO.getReg() == ARM::CPSR) {
446 Pred.push_back(MO);
447 Found = true;
448 }
449 }
450
451 return Found;
452}
453
Evan Chengac0869d2009-11-21 06:21:52 +0000454/// isPredicable - Return true if the specified instruction can be predicated.
455/// By default, this returns true for every instruction with a
456/// PredicateOperand.
457bool ARMBaseInstrInfo::isPredicable(MachineInstr *MI) const {
458 const TargetInstrDesc &TID = MI->getDesc();
459 if (!TID.isPredicable())
460 return false;
461
462 if ((TID.TSFlags & ARMII::DomainMask) == ARMII::DomainNEON) {
463 ARMFunctionInfo *AFI =
464 MI->getParent()->getParent()->getInfo<ARMFunctionInfo>();
Evan Chengd7f08102009-11-24 08:06:15 +0000465 return AFI->isThumb2Function();
Evan Chengac0869d2009-11-21 06:21:52 +0000466 }
467 return true;
468}
David Goodwin334c2642009-07-08 16:09:28 +0000469
Chris Lattner56856b12009-12-03 06:58:32 +0000470/// FIXME: Works around a gcc miscompilation with -fstrict-aliasing.
471DISABLE_INLINE
David Goodwin334c2642009-07-08 16:09:28 +0000472static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
Chris Lattner56856b12009-12-03 06:58:32 +0000473 unsigned JTI);
David Goodwin334c2642009-07-08 16:09:28 +0000474static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
475 unsigned JTI) {
Chris Lattner56856b12009-12-03 06:58:32 +0000476 assert(JTI < JT.size());
David Goodwin334c2642009-07-08 16:09:28 +0000477 return JT[JTI].MBBs.size();
478}
479
480/// GetInstSize - Return the size of the specified MachineInstr.
481///
482unsigned ARMBaseInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
483 const MachineBasicBlock &MBB = *MI->getParent();
484 const MachineFunction *MF = MBB.getParent();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000485 const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
David Goodwin334c2642009-07-08 16:09:28 +0000486
487 // Basic size info comes from the TSFlags field.
488 const TargetInstrDesc &TID = MI->getDesc();
Bruno Cardoso Lopes99405df2010-06-08 22:51:23 +0000489 uint64_t TSFlags = TID.TSFlags;
David Goodwin334c2642009-07-08 16:09:28 +0000490
Evan Chenga0ee8622009-07-31 22:22:22 +0000491 unsigned Opc = MI->getOpcode();
David Goodwin334c2642009-07-08 16:09:28 +0000492 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
493 default: {
494 // If this machine instr is an inline asm, measure it.
495 if (MI->getOpcode() == ARM::INLINEASM)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000496 return getInlineAsmLength(MI->getOperand(0).getSymbolName(), *MAI);
David Goodwin334c2642009-07-08 16:09:28 +0000497 if (MI->isLabel())
498 return 0;
Evan Chenga0ee8622009-07-31 22:22:22 +0000499 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000500 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000501 llvm_unreachable("Unknown or unset size field for instr!");
Chris Lattner518bb532010-02-09 19:54:29 +0000502 case TargetOpcode::IMPLICIT_DEF:
503 case TargetOpcode::KILL:
504 case TargetOpcode::DBG_LABEL:
505 case TargetOpcode::EH_LABEL:
Dale Johannesen375be772010-04-07 19:51:44 +0000506 case TargetOpcode::DBG_VALUE:
David Goodwin334c2642009-07-08 16:09:28 +0000507 return 0;
508 }
509 break;
510 }
Evan Cheng78947622009-07-24 18:20:44 +0000511 case ARMII::Size8Bytes: return 8; // ARM instruction x 2.
512 case ARMII::Size4Bytes: return 4; // ARM / Thumb2 instruction.
513 case ARMII::Size2Bytes: return 2; // Thumb1 instruction.
David Goodwin334c2642009-07-08 16:09:28 +0000514 case ARMII::SizeSpecial: {
Evan Chenga0ee8622009-07-31 22:22:22 +0000515 switch (Opc) {
David Goodwin334c2642009-07-08 16:09:28 +0000516 case ARM::CONSTPOOL_ENTRY:
517 // If this machine instr is a constant pool entry, its size is recorded as
518 // operand #2.
519 return MI->getOperand(2).getImm();
Jim Grosbach5eb19512010-05-22 01:06:18 +0000520 case ARM::Int_eh_sjlj_longjmp:
521 return 16;
522 case ARM::tInt_eh_sjlj_longjmp:
523 return 10;
Evan Cheng78947622009-07-24 18:20:44 +0000524 case ARM::Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000525 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000526 return 20;
Jim Grosbachd1228742009-12-01 18:10:36 +0000527 case ARM::tInt_eh_sjlj_setjmp:
Jim Grosbach5aa16842009-08-11 19:42:21 +0000528 case ARM::t2Int_eh_sjlj_setjmp:
Jim Grosbachd1007552010-04-28 20:33:09 +0000529 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbach0798edd2010-05-27 23:49:24 +0000530 return 12;
David Goodwin334c2642009-07-08 16:09:28 +0000531 case ARM::BR_JTr:
532 case ARM::BR_JTm:
533 case ARM::BR_JTadd:
Evan Chenga0ee8622009-07-31 22:22:22 +0000534 case ARM::tBR_JTr:
Evan Chengd26b14c2009-07-31 18:28:05 +0000535 case ARM::t2BR_JT:
536 case ARM::t2TBB:
537 case ARM::t2TBH: {
David Goodwin334c2642009-07-08 16:09:28 +0000538 // These are jumptable branches, i.e. a branch followed by an inlined
Evan Chengd26b14c2009-07-31 18:28:05 +0000539 // jumptable. The size is 4 + 4 * number of entries. For TBB, each
540 // entry is one byte; TBH two byte each.
Evan Chenga0ee8622009-07-31 22:22:22 +0000541 unsigned EntrySize = (Opc == ARM::t2TBB)
542 ? 1 : ((Opc == ARM::t2TBH) ? 2 : 4);
David Goodwin334c2642009-07-08 16:09:28 +0000543 unsigned NumOps = TID.getNumOperands();
544 MachineOperand JTOP =
545 MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
546 unsigned JTI = JTOP.getIndex();
547 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Chris Lattnerb1e80392010-01-25 23:22:00 +0000548 assert(MJTI != 0);
David Goodwin334c2642009-07-08 16:09:28 +0000549 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
550 assert(JTI < JT.size());
551 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
552 // 4 aligned. The assembler / linker may add 2 byte padding just before
553 // the JT entries. The size does not include this padding; the
554 // constant islands pass does separate bookkeeping for it.
555 // FIXME: If we know the size of the function is less than (1 << 16) *2
556 // bytes, we can use 16-bit entries instead. Then there won't be an
557 // alignment issue.
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000558 unsigned InstSize = (Opc == ARM::tBR_JTr || Opc == ARM::t2BR_JT) ? 2 : 4;
559 unsigned NumEntries = getNumJTEntries(JT, JTI);
560 if (Opc == ARM::t2TBB && (NumEntries & 1))
561 // Make sure the instruction that follows TBB is 2-byte aligned.
562 // FIXME: Constant island pass should insert an "ALIGN" instruction
563 // instead.
564 ++NumEntries;
565 return NumEntries * EntrySize + InstSize;
David Goodwin334c2642009-07-08 16:09:28 +0000566 }
567 default:
568 // Otherwise, pseudo-instruction sizes are zero.
569 return 0;
570 }
571 }
572 }
573 return 0; // Not reached
574}
575
576/// Return true if the instruction is a register to register move and
577/// leave the source and dest operands in the passed parameters.
578///
579bool
580ARMBaseInstrInfo::isMoveInstr(const MachineInstr &MI,
581 unsigned &SrcReg, unsigned &DstReg,
582 unsigned& SrcSubIdx, unsigned& DstSubIdx) const {
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000583 switch (MI.getOpcode()) {
Evan Chengdced03f2009-07-27 00:24:36 +0000584 default: break;
Jim Grosbache5165492009-11-09 00:11:35 +0000585 case ARM::VMOVS:
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000586 case ARM::VMOVD:
Jim Grosbache5165492009-11-09 00:11:35 +0000587 case ARM::VMOVDneon:
Evan Chengb63387a2010-05-06 06:36:08 +0000588 case ARM::VMOVQ:
589 case ARM::VMOVQQ : {
David Goodwin334c2642009-07-08 16:09:28 +0000590 SrcReg = MI.getOperand(1).getReg();
591 DstReg = MI.getOperand(0).getReg();
Evan Chengb63387a2010-05-06 06:36:08 +0000592 SrcSubIdx = MI.getOperand(1).getSubReg();
593 DstSubIdx = MI.getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000594 return true;
595 }
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000596 case ARM::MOVr:
Dale Johannesen6470a112010-06-15 22:08:33 +0000597 case ARM::MOVr_TC:
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000598 case ARM::tMOVr:
599 case ARM::tMOVgpr2tgpr:
600 case ARM::tMOVtgpr2gpr:
601 case ARM::tMOVgpr2gpr:
602 case ARM::t2MOVr: {
David Goodwin334c2642009-07-08 16:09:28 +0000603 assert(MI.getDesc().getNumOperands() >= 2 &&
604 MI.getOperand(0).isReg() &&
605 MI.getOperand(1).isReg() &&
606 "Invalid ARM MOV instruction");
607 SrcReg = MI.getOperand(1).getReg();
608 DstReg = MI.getOperand(0).getReg();
Evan Chengb63387a2010-05-06 06:36:08 +0000609 SrcSubIdx = MI.getOperand(1).getSubReg();
610 DstSubIdx = MI.getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +0000611 return true;
612 }
Evan Cheng68e3c6a2009-07-27 00:05:15 +0000613 }
David Goodwin334c2642009-07-08 16:09:28 +0000614
615 return false;
616}
617
Jim Grosbach764ab522009-08-11 15:33:49 +0000618unsigned
David Goodwin334c2642009-07-08 16:09:28 +0000619ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
620 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000621 switch (MI->getOpcode()) {
622 default: break;
623 case ARM::LDR:
624 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000625 if (MI->getOperand(1).isFI() &&
626 MI->getOperand(2).isReg() &&
627 MI->getOperand(3).isImm() &&
628 MI->getOperand(2).getReg() == 0 &&
629 MI->getOperand(3).getImm() == 0) {
630 FrameIndex = MI->getOperand(1).getIndex();
631 return MI->getOperand(0).getReg();
632 }
Evan Chengdced03f2009-07-27 00:24:36 +0000633 break;
634 case ARM::t2LDRi12:
635 case ARM::tRestore:
David Goodwin5ff58b52009-07-24 00:16:18 +0000636 if (MI->getOperand(1).isFI() &&
637 MI->getOperand(2).isImm() &&
638 MI->getOperand(2).getImm() == 0) {
639 FrameIndex = MI->getOperand(1).getIndex();
640 return MI->getOperand(0).getReg();
641 }
Evan Chengdced03f2009-07-27 00:24:36 +0000642 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000643 case ARM::VLDRD:
644 case ARM::VLDRS:
David Goodwin334c2642009-07-08 16:09:28 +0000645 if (MI->getOperand(1).isFI() &&
646 MI->getOperand(2).isImm() &&
647 MI->getOperand(2).getImm() == 0) {
648 FrameIndex = MI->getOperand(1).getIndex();
649 return MI->getOperand(0).getReg();
650 }
Evan Chengdced03f2009-07-27 00:24:36 +0000651 break;
David Goodwin334c2642009-07-08 16:09:28 +0000652 }
653
654 return 0;
655}
656
657unsigned
658ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
659 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000660 switch (MI->getOpcode()) {
661 default: break;
662 case ARM::STR:
663 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000664 if (MI->getOperand(1).isFI() &&
665 MI->getOperand(2).isReg() &&
666 MI->getOperand(3).isImm() &&
667 MI->getOperand(2).getReg() == 0 &&
668 MI->getOperand(3).getImm() == 0) {
669 FrameIndex = MI->getOperand(1).getIndex();
670 return MI->getOperand(0).getReg();
671 }
Evan Chengdced03f2009-07-27 00:24:36 +0000672 break;
673 case ARM::t2STRi12:
674 case ARM::tSpill:
David Goodwin5ff58b52009-07-24 00:16:18 +0000675 if (MI->getOperand(1).isFI() &&
676 MI->getOperand(2).isImm() &&
677 MI->getOperand(2).getImm() == 0) {
678 FrameIndex = MI->getOperand(1).getIndex();
679 return MI->getOperand(0).getReg();
680 }
Evan Chengdced03f2009-07-27 00:24:36 +0000681 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000682 case ARM::VSTRD:
683 case ARM::VSTRS:
David Goodwin334c2642009-07-08 16:09:28 +0000684 if (MI->getOperand(1).isFI() &&
685 MI->getOperand(2).isImm() &&
686 MI->getOperand(2).getImm() == 0) {
687 FrameIndex = MI->getOperand(1).getIndex();
688 return MI->getOperand(0).getReg();
689 }
Evan Chengdced03f2009-07-27 00:24:36 +0000690 break;
David Goodwin334c2642009-07-08 16:09:28 +0000691 }
692
693 return 0;
694}
695
696bool
697ARMBaseInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
698 MachineBasicBlock::iterator I,
699 unsigned DestReg, unsigned SrcReg,
700 const TargetRegisterClass *DestRC,
Dan Gohman34dcc6f2010-05-06 20:33:48 +0000701 const TargetRegisterClass *SrcRC,
702 DebugLoc DL) const {
Dale Johannesen6470a112010-06-15 22:08:33 +0000703 // tGPR or tcGPR is used sometimes in ARM instructions that need to avoid
704 // using certain registers. Just treat them as GPR here.
705 if (DestRC == ARM::tGPRRegisterClass || DestRC == ARM::tcGPRRegisterClass)
Bob Wilson1665b0a2010-02-16 17:24:15 +0000706 DestRC = ARM::GPRRegisterClass;
Dale Johannesen6470a112010-06-15 22:08:33 +0000707 if (SrcRC == ARM::tGPRRegisterClass || SrcRC == ARM::tcGPRRegisterClass)
Bob Wilson1665b0a2010-02-16 17:24:15 +0000708 SrcRC = ARM::GPRRegisterClass;
709
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000710 // Allow DPR / DPR_VFP2 / DPR_8 cross-class copies.
711 if (DestRC == ARM::DPR_8RegisterClass)
712 DestRC = ARM::DPR_VFP2RegisterClass;
713 if (SrcRC == ARM::DPR_8RegisterClass)
714 SrcRC = ARM::DPR_VFP2RegisterClass;
Evan Chengb4db6a42009-11-03 05:51:39 +0000715
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000716 // Allow QPR / QPR_VFP2 / QPR_8 cross-class copies.
717 if (DestRC == ARM::QPR_VFP2RegisterClass ||
718 DestRC == ARM::QPR_8RegisterClass)
719 DestRC = ARM::QPRRegisterClass;
720 if (SrcRC == ARM::QPR_VFP2RegisterClass ||
721 SrcRC == ARM::QPR_8RegisterClass)
722 SrcRC = ARM::QPRRegisterClass;
723
Evan Cheng22c687b2010-05-14 02:13:41 +0000724 // Allow QQPR / QQPR_VFP2 cross-class copies.
725 if (DestRC == ARM::QQPR_VFP2RegisterClass)
Evan Chengb63387a2010-05-06 06:36:08 +0000726 DestRC = ARM::QQPRRegisterClass;
Evan Cheng22c687b2010-05-14 02:13:41 +0000727 if (SrcRC == ARM::QQPR_VFP2RegisterClass)
Evan Chengb63387a2010-05-06 06:36:08 +0000728 SrcRC = ARM::QQPRRegisterClass;
729
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000730 // Disallow copies of unequal sizes.
731 if (DestRC != SrcRC && DestRC->getSize() != SrcRC->getSize())
732 return false;
David Goodwin334c2642009-07-08 16:09:28 +0000733
David Goodwin7bfdca02009-08-05 21:02:22 +0000734 if (DestRC == ARM::GPRRegisterClass) {
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000735 if (SrcRC == ARM::SPRRegisterClass)
736 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VMOVRS), DestReg)
737 .addReg(SrcReg));
738 else
739 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr),
740 DestReg).addReg(SrcReg)));
David Goodwin7bfdca02009-08-05 21:02:22 +0000741 } else {
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000742 unsigned Opc;
743
744 if (DestRC == ARM::SPRRegisterClass)
745 Opc = (SrcRC == ARM::GPRRegisterClass ? ARM::VMOVSR : ARM::VMOVS);
746 else if (DestRC == ARM::DPRRegisterClass)
747 Opc = ARM::VMOVD;
748 else if (DestRC == ARM::DPR_VFP2RegisterClass ||
749 SrcRC == ARM::DPR_VFP2RegisterClass)
750 // Always use neon reg-reg move if source or dest is NEON-only regclass.
751 Opc = ARM::VMOVDneon;
752 else if (DestRC == ARM::QPRRegisterClass)
753 Opc = ARM::VMOVQ;
Evan Chengb63387a2010-05-06 06:36:08 +0000754 else if (DestRC == ARM::QQPRRegisterClass)
755 Opc = ARM::VMOVQQ;
Evan Cheng22c687b2010-05-14 02:13:41 +0000756 else if (DestRC == ARM::QQQQPRRegisterClass)
757 Opc = ARM::VMOVQQQQ;
Anton Korobeynikov6755d972010-03-18 22:35:02 +0000758 else
759 return false;
760
Bob Wilson14f1d4e2010-06-15 05:51:27 +0000761 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
762 MIB.addReg(SrcReg);
763 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ)
764 AddDefaultPred(MIB);
David Goodwin7bfdca02009-08-05 21:02:22 +0000765 }
David Goodwin334c2642009-07-08 16:09:28 +0000766
767 return true;
768}
769
Evan Chengc10b5af2010-05-07 00:24:52 +0000770static const
771MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
772 unsigned Reg, unsigned SubIdx, unsigned State,
773 const TargetRegisterInfo *TRI) {
774 if (!SubIdx)
775 return MIB.addReg(Reg, State);
776
777 if (TargetRegisterInfo::isPhysicalRegister(Reg))
778 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
779 return MIB.addReg(Reg, State, SubIdx);
780}
781
David Goodwin334c2642009-07-08 16:09:28 +0000782void ARMBaseInstrInfo::
783storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
784 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000785 const TargetRegisterClass *RC,
786 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000787 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000788 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000789 MachineFunction &MF = *MBB.getParent();
790 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000791 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000792
793 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000794 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000795 MachineMemOperand::MOStore, 0,
796 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000797 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000798
Bob Wilson0eb0c742010-02-16 22:01:59 +0000799 // tGPR is used sometimes in ARM instructions that need to avoid using
800 // certain registers. Just treat it as GPR here.
Dale Johannesen6470a112010-06-15 22:08:33 +0000801 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000802 RC = ARM::GPRRegisterClass;
803
Bob Wilsonebe99b22010-06-18 21:32:42 +0000804 switch (RC->getID()) {
805 case ARM::GPRRegClassID:
Evan Cheng5732ca02009-07-27 03:14:20 +0000806 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STR))
David Goodwin334c2642009-07-08 16:09:28 +0000807 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000808 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000809 break;
810 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000811 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
812 .addReg(SrcReg, getKillRegState(isKill))
813 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000814 break;
815 case ARM::DPRRegClassID:
816 case ARM::DPR_VFP2RegClassID:
817 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000818 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000819 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000820 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000821 break;
822 case ARM::QPRRegClassID:
823 case ARM::QPR_VFP2RegClassID:
824 case ARM::QPR_8RegClassID:
Anton Korobeynikovbaf31082009-08-08 13:35:48 +0000825 // FIXME: Neon instructions should support predicates
Evan Chengb63387a2010-05-06 06:36:08 +0000826 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng69b9f982010-05-13 01:12:06 +0000827 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q))
828 .addFrameIndex(FI).addImm(128)
829 .addReg(SrcReg, getKillRegState(isKill))
830 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000831 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000832 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQ))
833 .addReg(SrcReg, getKillRegState(isKill))
834 .addFrameIndex(FI)
835 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
836 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000837 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000838 break;
839 case ARM::QQPRRegClassID:
840 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000841 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng22c687b2010-05-14 02:13:41 +0000842 // FIXME: It's possible to only store part of the QQ register if the
843 // spilled def has a sub-register index.
Evan Cheng435d4992010-05-07 02:04:02 +0000844 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VST2q32))
845 .addFrameIndex(FI).addImm(128);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000846 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
847 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
848 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
849 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000850 AddDefaultPred(MIB.addMemOperand(MMO));
851 } else {
852 MachineInstrBuilder MIB =
853 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
854 .addFrameIndex(FI)
855 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
856 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000857 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
858 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
859 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
860 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000861 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000862 break;
863 case ARM::QQQQPRRegClassID: {
Evan Cheng22c687b2010-05-14 02:13:41 +0000864 MachineInstrBuilder MIB =
865 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
866 .addFrameIndex(FI)
867 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
868 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000869 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
870 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
871 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
872 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
873 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
874 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
875 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
876 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
Bob Wilsonebe99b22010-06-18 21:32:42 +0000877 break;
878 }
879 default:
880 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000881 }
882}
883
David Goodwin334c2642009-07-08 16:09:28 +0000884void ARMBaseInstrInfo::
885loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
886 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000887 const TargetRegisterClass *RC,
888 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000889 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000890 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000891 MachineFunction &MF = *MBB.getParent();
892 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000893 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000894 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000895 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000896 MachineMemOperand::MOLoad, 0,
897 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000898 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000899
Bob Wilson0eb0c742010-02-16 22:01:59 +0000900 // tGPR is used sometimes in ARM instructions that need to avoid using
901 // certain registers. Just treat it as GPR here.
Dale Johannesen6470a112010-06-15 22:08:33 +0000902 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000903 RC = ARM::GPRRegisterClass;
904
Bob Wilsonebe99b22010-06-18 21:32:42 +0000905 switch (RC->getID()) {
906 case ARM::GPRRegClassID:
Evan Cheng5732ca02009-07-27 03:14:20 +0000907 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDR), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000908 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000909 break;
910 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000911 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
912 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000913 break;
914 case ARM::DPRRegClassID:
915 case ARM::DPR_VFP2RegClassID:
916 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000917 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000918 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000919 break;
920 case ARM::QPRRegClassID:
921 case ARM::QPR_VFP2RegClassID:
922 case ARM::QPR_8RegClassID:
Evan Chengb63387a2010-05-06 06:36:08 +0000923 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng69b9f982010-05-13 01:12:06 +0000924 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q), DestReg)
925 .addFrameIndex(FI).addImm(128)
926 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000927 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000928 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQ), DestReg)
929 .addFrameIndex(FI)
930 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
931 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000932 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000933 break;
934 case ARM::QQPRRegClassID:
935 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000936 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
937 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLD2q32));
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000938 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
939 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
940 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
941 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000942 AddDefaultPred(MIB.addFrameIndex(FI).addImm(128).addMemOperand(MMO));
943 } else {
944 MachineInstrBuilder MIB =
945 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
946 .addFrameIndex(FI)
947 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
948 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000949 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
950 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
951 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
952 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000953 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000954 break;
955 case ARM::QQQQPRRegClassID: {
956 MachineInstrBuilder MIB =
957 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
958 .addFrameIndex(FI)
959 .addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4)))
960 .addMemOperand(MMO);
961 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
962 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
963 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
964 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
965 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
966 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
967 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
968 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
969 break;
970 }
971 default:
972 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000973 }
974}
975
Evan Cheng62b50652010-04-26 07:39:25 +0000976MachineInstr*
977ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000978 int FrameIx, uint64_t Offset,
Evan Cheng62b50652010-04-26 07:39:25 +0000979 const MDNode *MDPtr,
980 DebugLoc DL) const {
981 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
982 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
983 return &*MIB;
984}
985
David Goodwin334c2642009-07-08 16:09:28 +0000986MachineInstr *ARMBaseInstrInfo::
987foldMemoryOperandImpl(MachineFunction &MF, MachineInstr *MI,
988 const SmallVectorImpl<unsigned> &Ops, int FI) const {
989 if (Ops.size() != 1) return NULL;
990
991 unsigned OpNum = Ops[0];
992 unsigned Opc = MI->getOpcode();
993 MachineInstr *NewMI = NULL;
Evan Cheng19068ba2009-08-10 06:32:05 +0000994 if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
David Goodwin334c2642009-07-08 16:09:28 +0000995 // If it is updating CPSR, then it cannot be folded.
Evan Cheng19068ba2009-08-10 06:32:05 +0000996 if (MI->getOperand(4).getReg() == ARM::CPSR && !MI->getOperand(4).isDead())
997 return NULL;
998 unsigned Pred = MI->getOperand(2).getImm();
999 unsigned PredReg = MI->getOperand(3).getReg();
1000 if (OpNum == 0) { // move -> store
1001 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001002 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +00001003 bool isKill = MI->getOperand(1).isKill();
1004 bool isUndef = MI->getOperand(1).isUndef();
1005 if (Opc == ARM::MOVr)
1006 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::STR))
Evan Chenged3ad212009-10-25 07:52:27 +00001007 .addReg(SrcReg,
1008 getKillRegState(isKill) | getUndefRegState(isUndef),
1009 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001010 .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
1011 else // ARM::t2MOVr
1012 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
Evan Chenged3ad212009-10-25 07:52:27 +00001013 .addReg(SrcReg,
1014 getKillRegState(isKill) | getUndefRegState(isUndef),
1015 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001016 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1017 } else { // move -> load
1018 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001019 unsigned DstSubReg = MI->getOperand(0).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +00001020 bool isDead = MI->getOperand(0).isDead();
1021 bool isUndef = MI->getOperand(0).isUndef();
1022 if (Opc == ARM::MOVr)
1023 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::LDR))
1024 .addReg(DstReg,
1025 RegState::Define |
1026 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001027 getUndefRegState(isUndef), DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001028 .addFrameIndex(FI).addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
1029 else // ARM::t2MOVr
1030 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
1031 .addReg(DstReg,
1032 RegState::Define |
1033 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001034 getUndefRegState(isUndef), DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001035 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
David Goodwin334c2642009-07-08 16:09:28 +00001036 }
Evan Cheng19068ba2009-08-10 06:32:05 +00001037 } else if (Opc == ARM::tMOVgpr2gpr ||
1038 Opc == ARM::tMOVtgpr2gpr ||
1039 Opc == ARM::tMOVgpr2tgpr) {
1040 if (OpNum == 0) { // move -> store
1041 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001042 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +00001043 bool isKill = MI->getOperand(1).isKill();
1044 bool isUndef = MI->getOperand(1).isUndef();
1045 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2STRi12))
Evan Chenged3ad212009-10-25 07:52:27 +00001046 .addReg(SrcReg,
1047 getKillRegState(isKill) | getUndefRegState(isUndef),
1048 SrcSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001049 .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
1050 } else { // move -> load
1051 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001052 unsigned DstSubReg = MI->getOperand(0).getSubReg();
Evan Cheng19068ba2009-08-10 06:32:05 +00001053 bool isDead = MI->getOperand(0).isDead();
1054 bool isUndef = MI->getOperand(0).isUndef();
1055 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::t2LDRi12))
1056 .addReg(DstReg,
1057 RegState::Define |
1058 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001059 getUndefRegState(isUndef),
1060 DstSubReg)
Evan Cheng19068ba2009-08-10 06:32:05 +00001061 .addFrameIndex(FI).addImm(0).addImm(ARMCC::AL).addReg(0);
1062 }
Jim Grosbache5165492009-11-09 00:11:35 +00001063 } else if (Opc == ARM::VMOVS) {
David Goodwin334c2642009-07-08 16:09:28 +00001064 unsigned Pred = MI->getOperand(2).getImm();
1065 unsigned PredReg = MI->getOperand(3).getReg();
1066 if (OpNum == 0) { // move -> store
1067 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001068 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001069 bool isKill = MI->getOperand(1).isKill();
1070 bool isUndef = MI->getOperand(1).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001071 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTRS))
Evan Chenged3ad212009-10-25 07:52:27 +00001072 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef),
1073 SrcSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001074 .addFrameIndex(FI)
1075 .addImm(0).addImm(Pred).addReg(PredReg);
1076 } else { // move -> load
1077 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001078 unsigned DstSubReg = MI->getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001079 bool isDead = MI->getOperand(0).isDead();
1080 bool isUndef = MI->getOperand(0).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001081 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDRS))
David Goodwin334c2642009-07-08 16:09:28 +00001082 .addReg(DstReg,
1083 RegState::Define |
1084 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001085 getUndefRegState(isUndef),
1086 DstSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001087 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1088 }
Evan Cheng69b9f982010-05-13 01:12:06 +00001089 } else if (Opc == ARM::VMOVD || Opc == ARM::VMOVDneon) {
David Goodwin334c2642009-07-08 16:09:28 +00001090 unsigned Pred = MI->getOperand(2).getImm();
1091 unsigned PredReg = MI->getOperand(3).getReg();
1092 if (OpNum == 0) { // move -> store
1093 unsigned SrcReg = MI->getOperand(1).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001094 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001095 bool isKill = MI->getOperand(1).isKill();
1096 bool isUndef = MI->getOperand(1).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001097 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTRD))
Evan Chenged3ad212009-10-25 07:52:27 +00001098 .addReg(SrcReg,
1099 getKillRegState(isKill) | getUndefRegState(isUndef),
1100 SrcSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001101 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1102 } else { // move -> load
1103 unsigned DstReg = MI->getOperand(0).getReg();
Evan Chenged3ad212009-10-25 07:52:27 +00001104 unsigned DstSubReg = MI->getOperand(0).getSubReg();
David Goodwin334c2642009-07-08 16:09:28 +00001105 bool isDead = MI->getOperand(0).isDead();
1106 bool isUndef = MI->getOperand(0).isUndef();
Jim Grosbache5165492009-11-09 00:11:35 +00001107 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDRD))
David Goodwin334c2642009-07-08 16:09:28 +00001108 .addReg(DstReg,
1109 RegState::Define |
1110 getDeadRegState(isDead) |
Evan Chenged3ad212009-10-25 07:52:27 +00001111 getUndefRegState(isUndef),
1112 DstSubReg)
David Goodwin334c2642009-07-08 16:09:28 +00001113 .addFrameIndex(FI).addImm(0).addImm(Pred).addReg(PredReg);
1114 }
Evan Cheng69b9f982010-05-13 01:12:06 +00001115 } else if (Opc == ARM::VMOVQ) {
1116 MachineFrameInfo &MFI = *MF.getFrameInfo();
1117 unsigned Pred = MI->getOperand(2).getImm();
1118 unsigned PredReg = MI->getOperand(3).getReg();
1119 if (OpNum == 0) { // move -> store
1120 unsigned SrcReg = MI->getOperand(1).getReg();
1121 unsigned SrcSubReg = MI->getOperand(1).getSubReg();
1122 bool isKill = MI->getOperand(1).isKill();
1123 bool isUndef = MI->getOperand(1).isUndef();
1124 if (MFI.getObjectAlignment(FI) >= 16 &&
1125 getRegisterInfo().canRealignStack(MF)) {
1126 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VST1q))
1127 .addFrameIndex(FI).addImm(128)
1128 .addReg(SrcReg,
1129 getKillRegState(isKill) | getUndefRegState(isUndef),
1130 SrcSubReg)
1131 .addImm(Pred).addReg(PredReg);
1132 } else {
1133 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VSTMQ))
1134 .addReg(SrcReg,
1135 getKillRegState(isKill) | getUndefRegState(isUndef),
1136 SrcSubReg)
1137 .addFrameIndex(FI).addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
1138 .addImm(Pred).addReg(PredReg);
1139 }
1140 } else { // move -> load
1141 unsigned DstReg = MI->getOperand(0).getReg();
1142 unsigned DstSubReg = MI->getOperand(0).getSubReg();
1143 bool isDead = MI->getOperand(0).isDead();
1144 bool isUndef = MI->getOperand(0).isUndef();
1145 if (MFI.getObjectAlignment(FI) >= 16 &&
1146 getRegisterInfo().canRealignStack(MF)) {
1147 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLD1q))
1148 .addReg(DstReg,
1149 RegState::Define |
1150 getDeadRegState(isDead) |
1151 getUndefRegState(isUndef),
1152 DstSubReg)
1153 .addFrameIndex(FI).addImm(128).addImm(Pred).addReg(PredReg);
1154 } else {
1155 NewMI = BuildMI(MF, MI->getDebugLoc(), get(ARM::VLDMQ))
1156 .addReg(DstReg,
1157 RegState::Define |
1158 getDeadRegState(isDead) |
1159 getUndefRegState(isUndef),
1160 DstSubReg)
1161 .addFrameIndex(FI).addImm(ARM_AM::getAM5Opc(ARM_AM::ia, 4))
1162 .addImm(Pred).addReg(PredReg);
1163 }
1164 }
David Goodwin334c2642009-07-08 16:09:28 +00001165 }
1166
1167 return NewMI;
1168}
1169
Jim Grosbach764ab522009-08-11 15:33:49 +00001170MachineInstr*
David Goodwin334c2642009-07-08 16:09:28 +00001171ARMBaseInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
1172 MachineInstr* MI,
1173 const SmallVectorImpl<unsigned> &Ops,
1174 MachineInstr* LoadMI) const {
Evan Cheng1f5c9882009-07-27 04:18:04 +00001175 // FIXME
David Goodwin334c2642009-07-08 16:09:28 +00001176 return 0;
1177}
1178
1179bool
1180ARMBaseInstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
Evan Cheng22946452009-08-10 05:51:48 +00001181 const SmallVectorImpl<unsigned> &Ops) const {
David Goodwin334c2642009-07-08 16:09:28 +00001182 if (Ops.size() != 1) return false;
1183
1184 unsigned Opc = MI->getOpcode();
Evan Cheng5732ca02009-07-27 03:14:20 +00001185 if (Opc == ARM::MOVr || Opc == ARM::t2MOVr) {
David Goodwin334c2642009-07-08 16:09:28 +00001186 // If it is updating CPSR, then it cannot be folded.
Evan Cheng22946452009-08-10 05:51:48 +00001187 return MI->getOperand(4).getReg() != ARM::CPSR ||
1188 MI->getOperand(4).isDead();
Evan Cheng19068ba2009-08-10 06:32:05 +00001189 } else if (Opc == ARM::tMOVgpr2gpr ||
1190 Opc == ARM::tMOVtgpr2gpr ||
1191 Opc == ARM::tMOVgpr2tgpr) {
1192 return true;
Evan Cheng69b9f982010-05-13 01:12:06 +00001193 } else if (Opc == ARM::VMOVS || Opc == ARM::VMOVD ||
1194 Opc == ARM::VMOVDneon || Opc == ARM::VMOVQ) {
David Goodwin334c2642009-07-08 16:09:28 +00001195 return true;
David Goodwin334c2642009-07-08 16:09:28 +00001196 }
1197
Evan Cheng22c687b2010-05-14 02:13:41 +00001198 // FIXME: VMOVQQ and VMOVQQQQ?
1199
David Goodwin334c2642009-07-08 16:09:28 +00001200 return false;
1201}
Evan Cheng5ca53a72009-07-27 18:20:05 +00001202
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001203/// Create a copy of a const pool value. Update CPI to the new index and return
1204/// the label UID.
1205static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
1206 MachineConstantPool *MCP = MF.getConstantPool();
1207 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1208
1209 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
1210 assert(MCPE.isMachineConstantPoolEntry() &&
1211 "Expecting a machine constantpool entry!");
1212 ARMConstantPoolValue *ACPV =
1213 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
1214
1215 unsigned PCLabelId = AFI->createConstPoolEntryUId();
1216 ARMConstantPoolValue *NewCPV = 0;
1217 if (ACPV->isGlobalValue())
1218 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
1219 ARMCP::CPValue, 4);
1220 else if (ACPV->isExtSymbol())
1221 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
1222 ACPV->getSymbol(), PCLabelId, 4);
1223 else if (ACPV->isBlockAddress())
1224 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
1225 ARMCP::CPBlockAddress, 4);
1226 else
1227 llvm_unreachable("Unexpected ARM constantpool value type!!");
1228 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
1229 return PCLabelId;
1230}
1231
Evan Chengfdc83402009-11-08 00:15:23 +00001232void ARMBaseInstrInfo::
1233reMaterialize(MachineBasicBlock &MBB,
1234 MachineBasicBlock::iterator I,
1235 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +00001236 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001237 const TargetRegisterInfo &TRI) const {
Evan Chengfdc83402009-11-08 00:15:23 +00001238 unsigned Opcode = Orig->getOpcode();
1239 switch (Opcode) {
1240 default: {
1241 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +00001242 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfdc83402009-11-08 00:15:23 +00001243 MBB.insert(I, MI);
1244 break;
1245 }
1246 case ARM::tLDRpci_pic:
1247 case ARM::t2LDRpci_pic: {
1248 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +00001249 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001250 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +00001251 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
1252 DestReg)
1253 .addConstantPoolIndex(CPI).addImm(PCLabelId);
1254 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
1255 break;
1256 }
1257 }
Evan Chengfdc83402009-11-08 00:15:23 +00001258}
1259
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +00001260MachineInstr *
1261ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
1262 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
1263 switch(Orig->getOpcode()) {
1264 case ARM::tLDRpci_pic:
1265 case ARM::t2LDRpci_pic: {
1266 unsigned CPI = Orig->getOperand(1).getIndex();
1267 unsigned PCLabelId = duplicateCPV(MF, CPI);
1268 Orig->getOperand(1).setIndex(CPI);
1269 Orig->getOperand(2).setImm(PCLabelId);
1270 break;
1271 }
1272 }
1273 return MI;
1274}
1275
Evan Cheng506049f2010-03-03 01:44:33 +00001276bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
1277 const MachineInstr *MI1) const {
Evan Chengd457e6e2009-11-07 04:04:34 +00001278 int Opcode = MI0->getOpcode();
Evan Cheng9b824252009-11-20 02:10:27 +00001279 if (Opcode == ARM::t2LDRpci ||
1280 Opcode == ARM::t2LDRpci_pic ||
1281 Opcode == ARM::tLDRpci ||
1282 Opcode == ARM::tLDRpci_pic) {
Evan Chengd457e6e2009-11-07 04:04:34 +00001283 if (MI1->getOpcode() != Opcode)
1284 return false;
1285 if (MI0->getNumOperands() != MI1->getNumOperands())
1286 return false;
1287
1288 const MachineOperand &MO0 = MI0->getOperand(1);
1289 const MachineOperand &MO1 = MI1->getOperand(1);
1290 if (MO0.getOffset() != MO1.getOffset())
1291 return false;
1292
1293 const MachineFunction *MF = MI0->getParent()->getParent();
1294 const MachineConstantPool *MCP = MF->getConstantPool();
1295 int CPI0 = MO0.getIndex();
1296 int CPI1 = MO1.getIndex();
1297 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1298 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1299 ARMConstantPoolValue *ACPV0 =
1300 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1301 ARMConstantPoolValue *ACPV1 =
1302 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1303 return ACPV0->hasSameValue(ACPV1);
1304 }
1305
Evan Cheng506049f2010-03-03 01:44:33 +00001306 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001307}
1308
Bill Wendling4b722102010-06-23 23:00:16 +00001309/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1310/// determine if two loads are loading from the same base address. It should
1311/// only return true if the base pointers are the same and the only differences
1312/// between the two addresses is the offset. It also returns the offsets by
1313/// reference.
1314bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1315 int64_t &Offset1,
1316 int64_t &Offset2) const {
1317 // Don't worry about Thumb: just ARM and Thumb2.
1318 if (Subtarget.isThumb1Only()) return false;
1319
1320 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1321 return false;
1322
1323 switch (Load1->getMachineOpcode()) {
1324 default:
1325 return false;
1326 case ARM::LDR:
1327 case ARM::LDRB:
1328 case ARM::LDRD:
1329 case ARM::LDRH:
1330 case ARM::LDRSB:
1331 case ARM::LDRSH:
1332 case ARM::VLDRD:
1333 case ARM::VLDRS:
1334 case ARM::t2LDRi8:
1335 case ARM::t2LDRDi8:
1336 case ARM::t2LDRSHi8:
1337 case ARM::t2LDRi12:
1338 case ARM::t2LDRSHi12:
1339 break;
1340 }
1341
1342 switch (Load2->getMachineOpcode()) {
1343 default:
1344 return false;
1345 case ARM::LDR:
1346 case ARM::LDRB:
1347 case ARM::LDRD:
1348 case ARM::LDRH:
1349 case ARM::LDRSB:
1350 case ARM::LDRSH:
1351 case ARM::VLDRD:
1352 case ARM::VLDRS:
1353 case ARM::t2LDRi8:
1354 case ARM::t2LDRDi8:
1355 case ARM::t2LDRSHi8:
1356 case ARM::t2LDRi12:
1357 case ARM::t2LDRSHi12:
1358 break;
1359 }
1360
1361 // Check if base addresses and chain operands match.
1362 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1363 Load1->getOperand(4) != Load2->getOperand(4))
1364 return false;
1365
1366 // Index should be Reg0.
1367 if (Load1->getOperand(3) != Load2->getOperand(3))
1368 return false;
1369
1370 // Determine the offsets.
1371 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1372 isa<ConstantSDNode>(Load2->getOperand(1))) {
1373 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1374 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1375 return true;
1376 }
1377
1378 return false;
1379}
1380
1381/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1382/// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1383/// be scheduled togther. On some targets if two loads are loading from
1384/// addresses in the same cache line, it's better if they are scheduled
1385/// together. This function takes two integers that represent the load offsets
1386/// from the common base address. It returns true if it decides it's desirable
1387/// to schedule the two loads together. "NumLoads" is the number of loads that
1388/// have already been scheduled after Load1.
1389bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1390 int64_t Offset1, int64_t Offset2,
1391 unsigned NumLoads) const {
1392 // Don't worry about Thumb: just ARM and Thumb2.
1393 if (Subtarget.isThumb1Only()) return false;
1394
1395 assert(Offset2 > Offset1);
1396
1397 if ((Offset2 - Offset1) / 8 > 64)
1398 return false;
1399
1400 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1401 return false; // FIXME: overly conservative?
1402
1403 // Four loads in a row should be sufficient.
1404 if (NumLoads >= 3)
1405 return false;
1406
1407 return true;
1408}
1409
Evan Cheng86050dc2010-06-18 23:09:54 +00001410bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1411 const MachineBasicBlock *MBB,
1412 const MachineFunction &MF) const {
1413 // Terminators and labels can't be scheduled around.
1414 if (MI->getDesc().isTerminator() || MI->isLabel())
1415 return true;
1416
1417 // Treat the start of the IT block as a scheduling boundary, but schedule
1418 // t2IT along with all instructions following it.
1419 // FIXME: This is a big hammer. But the alternative is to add all potential
1420 // true and anti dependencies to IT block instructions as implicit operands
1421 // to the t2IT instruction. The added compile time and complexity does not
1422 // seem worth it.
1423 MachineBasicBlock::const_iterator I = MI;
1424 if (++I != MBB->end() && I->getOpcode() == ARM::t2IT)
1425 return true;
1426
1427 // Don't attempt to schedule around any instruction that defines
1428 // a stack-oriented pointer, as it's unlikely to be profitable. This
1429 // saves compile time, because it doesn't require every single
1430 // stack slot reference to depend on the instruction that does the
1431 // modification.
1432 if (MI->definesRegister(ARM::SP))
1433 return true;
1434
1435 return false;
1436}
1437
Evan Cheng8fb90362009-08-08 03:20:32 +00001438/// getInstrPredicate - If instruction is predicated, returns its predicate
1439/// condition, otherwise returns AL. It also returns the condition code
1440/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001441ARMCC::CondCodes
1442llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001443 int PIdx = MI->findFirstPredOperandIdx();
1444 if (PIdx == -1) {
1445 PredReg = 0;
1446 return ARMCC::AL;
1447 }
1448
1449 PredReg = MI->getOperand(PIdx+1).getReg();
1450 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1451}
1452
1453
Evan Cheng6495f632009-07-28 05:48:47 +00001454int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001455 if (Opc == ARM::B)
1456 return ARM::Bcc;
1457 else if (Opc == ARM::tB)
1458 return ARM::tBcc;
1459 else if (Opc == ARM::t2B)
1460 return ARM::t2Bcc;
1461
1462 llvm_unreachable("Unknown unconditional branch opcode!");
1463 return 0;
1464}
1465
Evan Cheng6495f632009-07-28 05:48:47 +00001466
1467void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1468 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1469 unsigned DestReg, unsigned BaseReg, int NumBytes,
1470 ARMCC::CondCodes Pred, unsigned PredReg,
1471 const ARMBaseInstrInfo &TII) {
1472 bool isSub = NumBytes < 0;
1473 if (isSub) NumBytes = -NumBytes;
1474
1475 while (NumBytes) {
1476 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1477 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1478 assert(ThisVal && "Didn't extract field correctly");
1479
1480 // We will handle these bits from offset, clear them.
1481 NumBytes &= ~ThisVal;
1482
1483 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1484
1485 // Build the new ADD / SUB.
1486 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1487 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1488 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1489 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1490 BaseReg = DestReg;
1491 }
1492}
1493
Evan Chengcdbb3f52009-08-27 01:23:50 +00001494bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1495 unsigned FrameReg, int &Offset,
1496 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001497 unsigned Opcode = MI.getOpcode();
1498 const TargetInstrDesc &Desc = MI.getDesc();
1499 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1500 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001501
Evan Cheng6495f632009-07-28 05:48:47 +00001502 // Memory operands in inline assembly always use AddrMode2.
1503 if (Opcode == ARM::INLINEASM)
1504 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001505
Evan Cheng6495f632009-07-28 05:48:47 +00001506 if (Opcode == ARM::ADDri) {
1507 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1508 if (Offset == 0) {
1509 // Turn it into a move.
1510 MI.setDesc(TII.get(ARM::MOVr));
1511 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1512 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001513 Offset = 0;
1514 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001515 } else if (Offset < 0) {
1516 Offset = -Offset;
1517 isSub = true;
1518 MI.setDesc(TII.get(ARM::SUBri));
1519 }
1520
1521 // Common case: small offset, fits into instruction.
1522 if (ARM_AM::getSOImmVal(Offset) != -1) {
1523 // Replace the FrameIndex with sp / fp
1524 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1525 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001526 Offset = 0;
1527 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001528 }
1529
1530 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1531 // as possible.
1532 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1533 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1534
1535 // We will handle these bits from offset, clear them.
1536 Offset &= ~ThisImmVal;
1537
1538 // Get the properly encoded SOImmVal field.
1539 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1540 "Bit extraction didn't work?");
1541 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1542 } else {
1543 unsigned ImmIdx = 0;
1544 int InstrOffs = 0;
1545 unsigned NumBits = 0;
1546 unsigned Scale = 1;
1547 switch (AddrMode) {
1548 case ARMII::AddrMode2: {
1549 ImmIdx = FrameRegIdx+2;
1550 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1551 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1552 InstrOffs *= -1;
1553 NumBits = 12;
1554 break;
1555 }
1556 case ARMII::AddrMode3: {
1557 ImmIdx = FrameRegIdx+2;
1558 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1559 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1560 InstrOffs *= -1;
1561 NumBits = 8;
1562 break;
1563 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001564 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001565 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001566 // Can't fold any offset even if it's zero.
1567 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001568 case ARMII::AddrMode5: {
1569 ImmIdx = FrameRegIdx+1;
1570 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1571 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1572 InstrOffs *= -1;
1573 NumBits = 8;
1574 Scale = 4;
1575 break;
1576 }
1577 default:
1578 llvm_unreachable("Unsupported addressing mode!");
1579 break;
1580 }
1581
1582 Offset += InstrOffs * Scale;
1583 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1584 if (Offset < 0) {
1585 Offset = -Offset;
1586 isSub = true;
1587 }
1588
1589 // Attempt to fold address comp. if opcode has offset bits
1590 if (NumBits > 0) {
1591 // Common case: small offset, fits into instruction.
1592 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1593 int ImmedOffset = Offset / Scale;
1594 unsigned Mask = (1 << NumBits) - 1;
1595 if ((unsigned)Offset <= Mask * Scale) {
1596 // Replace the FrameIndex with sp
1597 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1598 if (isSub)
1599 ImmedOffset |= 1 << NumBits;
1600 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001601 Offset = 0;
1602 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001603 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001604
Evan Cheng6495f632009-07-28 05:48:47 +00001605 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1606 ImmedOffset = ImmedOffset & Mask;
1607 if (isSub)
1608 ImmedOffset |= 1 << NumBits;
1609 ImmOp.ChangeToImmediate(ImmedOffset);
1610 Offset &= ~(Mask*Scale);
1611 }
1612 }
1613
Evan Chengcdbb3f52009-08-27 01:23:50 +00001614 Offset = (isSub) ? -Offset : Offset;
1615 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001616}