blob: c22bf3f1d85f5549a40dbad1162a535e71fdc799 [file] [log] [blame]
Jim Grosbach31c24bf2009-11-07 22:00:39 +00001//===- ARMBaseInstrInfo.cpp - ARM Instruction Information -------*- C++ -*-===//
David Goodwin334c2642009-07-08 16:09:28 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Base ARM implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARMBaseInstrInfo.h"
15#include "ARM.h"
16#include "ARMAddressingModes.h"
Evan Chengd457e6e2009-11-07 04:04:34 +000017#include "ARMConstantPoolValue.h"
David Goodwin334c2642009-07-08 16:09:28 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikovf95215f2009-11-02 00:10:38 +000019#include "ARMRegisterInfo.h"
Chris Lattner4dbbe342010-07-20 21:17:29 +000020#include "ARMGenInstrInfo.inc"
Evan Chengfdc83402009-11-08 00:15:23 +000021#include "llvm/Constants.h"
22#include "llvm/Function.h"
23#include "llvm/GlobalValue.h"
David Goodwin334c2642009-07-08 16:09:28 +000024#include "llvm/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:
Bill Wendling7431bea2010-07-16 22:20:36 +0000504 case TargetOpcode::PROLOG_LABEL:
Chris Lattner518bb532010-02-09 19:54:29 +0000505 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
Jim Grosbach764ab522009-08-11 15:33:49 +0000576unsigned
David Goodwin334c2642009-07-08 16:09:28 +0000577ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
578 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000579 switch (MI->getOpcode()) {
580 default: break;
581 case ARM::LDR:
582 case ARM::t2LDRs: // FIXME: don't use t2LDRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000583 if (MI->getOperand(1).isFI() &&
584 MI->getOperand(2).isReg() &&
585 MI->getOperand(3).isImm() &&
586 MI->getOperand(2).getReg() == 0 &&
587 MI->getOperand(3).getImm() == 0) {
588 FrameIndex = MI->getOperand(1).getIndex();
589 return MI->getOperand(0).getReg();
590 }
Evan Chengdced03f2009-07-27 00:24:36 +0000591 break;
592 case ARM::t2LDRi12:
593 case ARM::tRestore:
David Goodwin5ff58b52009-07-24 00:16:18 +0000594 if (MI->getOperand(1).isFI() &&
595 MI->getOperand(2).isImm() &&
596 MI->getOperand(2).getImm() == 0) {
597 FrameIndex = MI->getOperand(1).getIndex();
598 return MI->getOperand(0).getReg();
599 }
Evan Chengdced03f2009-07-27 00:24:36 +0000600 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000601 case ARM::VLDRD:
602 case ARM::VLDRS:
David Goodwin334c2642009-07-08 16:09:28 +0000603 if (MI->getOperand(1).isFI() &&
604 MI->getOperand(2).isImm() &&
605 MI->getOperand(2).getImm() == 0) {
606 FrameIndex = MI->getOperand(1).getIndex();
607 return MI->getOperand(0).getReg();
608 }
Evan Chengdced03f2009-07-27 00:24:36 +0000609 break;
David Goodwin334c2642009-07-08 16:09:28 +0000610 }
611
612 return 0;
613}
614
615unsigned
616ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
617 int &FrameIndex) const {
Evan Chengdced03f2009-07-27 00:24:36 +0000618 switch (MI->getOpcode()) {
619 default: break;
620 case ARM::STR:
621 case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
David Goodwin334c2642009-07-08 16:09:28 +0000622 if (MI->getOperand(1).isFI() &&
623 MI->getOperand(2).isReg() &&
624 MI->getOperand(3).isImm() &&
625 MI->getOperand(2).getReg() == 0 &&
626 MI->getOperand(3).getImm() == 0) {
627 FrameIndex = MI->getOperand(1).getIndex();
628 return MI->getOperand(0).getReg();
629 }
Evan Chengdced03f2009-07-27 00:24:36 +0000630 break;
631 case ARM::t2STRi12:
632 case ARM::tSpill:
David Goodwin5ff58b52009-07-24 00:16:18 +0000633 if (MI->getOperand(1).isFI() &&
634 MI->getOperand(2).isImm() &&
635 MI->getOperand(2).getImm() == 0) {
636 FrameIndex = MI->getOperand(1).getIndex();
637 return MI->getOperand(0).getReg();
638 }
Evan Chengdced03f2009-07-27 00:24:36 +0000639 break;
Jim Grosbache5165492009-11-09 00:11:35 +0000640 case ARM::VSTRD:
641 case ARM::VSTRS:
David Goodwin334c2642009-07-08 16:09:28 +0000642 if (MI->getOperand(1).isFI() &&
643 MI->getOperand(2).isImm() &&
644 MI->getOperand(2).getImm() == 0) {
645 FrameIndex = MI->getOperand(1).getIndex();
646 return MI->getOperand(0).getReg();
647 }
Evan Chengdced03f2009-07-27 00:24:36 +0000648 break;
David Goodwin334c2642009-07-08 16:09:28 +0000649 }
650
651 return 0;
652}
653
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000654void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
655 MachineBasicBlock::iterator I, DebugLoc DL,
656 unsigned DestReg, unsigned SrcReg,
657 bool KillSrc) const {
658 bool GPRDest = ARM::GPRRegClass.contains(DestReg);
659 bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
Bob Wilson1665b0a2010-02-16 17:24:15 +0000660
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000661 if (GPRDest && GPRSrc) {
662 AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
663 .addReg(SrcReg, getKillRegState(KillSrc))));
664 return;
David Goodwin7bfdca02009-08-05 21:02:22 +0000665 }
David Goodwin334c2642009-07-08 16:09:28 +0000666
Jakob Stoklund Olesenac273662010-07-11 06:33:54 +0000667 bool SPRDest = ARM::SPRRegClass.contains(DestReg);
668 bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
669
670 unsigned Opc;
671 if (SPRDest && SPRSrc)
672 Opc = ARM::VMOVS;
673 else if (GPRDest && SPRSrc)
674 Opc = ARM::VMOVRS;
675 else if (SPRDest && GPRSrc)
676 Opc = ARM::VMOVSR;
677 else if (ARM::DPRRegClass.contains(DestReg, SrcReg))
678 Opc = ARM::VMOVD;
679 else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
680 Opc = ARM::VMOVQ;
681 else if (ARM::QQPRRegClass.contains(DestReg, SrcReg))
682 Opc = ARM::VMOVQQ;
683 else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg))
684 Opc = ARM::VMOVQQQQ;
685 else
686 llvm_unreachable("Impossible reg-to-reg copy");
687
688 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
689 MIB.addReg(SrcReg, getKillRegState(KillSrc));
690 if (Opc != ARM::VMOVQQ && Opc != ARM::VMOVQQQQ)
691 AddDefaultPred(MIB);
David Goodwin334c2642009-07-08 16:09:28 +0000692}
693
Evan Chengc10b5af2010-05-07 00:24:52 +0000694static const
695MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB,
696 unsigned Reg, unsigned SubIdx, unsigned State,
697 const TargetRegisterInfo *TRI) {
698 if (!SubIdx)
699 return MIB.addReg(Reg, State);
700
701 if (TargetRegisterInfo::isPhysicalRegister(Reg))
702 return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
703 return MIB.addReg(Reg, State, SubIdx);
704}
705
David Goodwin334c2642009-07-08 16:09:28 +0000706void ARMBaseInstrInfo::
707storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
708 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000709 const TargetRegisterClass *RC,
710 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000711 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000712 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000713 MachineFunction &MF = *MBB.getParent();
714 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000715 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000716
717 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000718 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000719 MachineMemOperand::MOStore, 0,
720 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000721 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000722
Bob Wilson0eb0c742010-02-16 22:01:59 +0000723 // tGPR is used sometimes in ARM instructions that need to avoid using
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000724 // certain registers. Just treat it as GPR here. Likewise, rGPR.
725 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
726 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000727 RC = ARM::GPRRegisterClass;
728
Bob Wilsonebe99b22010-06-18 21:32:42 +0000729 switch (RC->getID()) {
730 case ARM::GPRRegClassID:
Evan Cheng5732ca02009-07-27 03:14:20 +0000731 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::STR))
David Goodwin334c2642009-07-08 16:09:28 +0000732 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000733 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000734 break;
735 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000736 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRS))
737 .addReg(SrcReg, getKillRegState(isKill))
738 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000739 break;
740 case ARM::DPRRegClassID:
741 case ARM::DPR_VFP2RegClassID:
742 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000743 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTRD))
David Goodwin334c2642009-07-08 16:09:28 +0000744 .addReg(SrcReg, getKillRegState(isKill))
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000745 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000746 break;
747 case ARM::QPRRegClassID:
748 case ARM::QPR_VFP2RegClassID:
749 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000750 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000751 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1q64Pseudo))
Bob Wilsonf967ca02010-07-06 21:26:18 +0000752 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000753 .addReg(SrcReg, getKillRegState(isKill))
754 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000755 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000756 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMQ))
757 .addReg(SrcReg, getKillRegState(isKill))
758 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000759 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
Evan Cheng69b9f982010-05-13 01:12:06 +0000760 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000761 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000762 break;
763 case ARM::QQPRRegClassID:
764 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000765 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Evan Cheng22c687b2010-05-14 02:13:41 +0000766 // FIXME: It's possible to only store part of the QQ register if the
767 // spilled def has a sub-register index.
Bob Wilson168f3822010-09-15 01:48:05 +0000768 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VST1d64QPseudo))
769 .addFrameIndex(FI).addImm(16)
770 .addReg(SrcReg, getKillRegState(isKill))
771 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000772 } else {
773 MachineInstrBuilder MIB =
774 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
775 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000776 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Evan Cheng435d4992010-05-07 02:04:02 +0000777 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000778 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
779 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
780 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
781 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000782 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000783 break;
784 case ARM::QQQQPRRegClassID: {
Evan Cheng22c687b2010-05-14 02:13:41 +0000785 MachineInstrBuilder MIB =
786 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VSTMD))
787 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000788 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Evan Cheng22c687b2010-05-14 02:13:41 +0000789 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000790 MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
791 MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
792 MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
793 MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
794 MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
795 MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
796 MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
797 AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
Bob Wilsonebe99b22010-06-18 21:32:42 +0000798 break;
799 }
800 default:
801 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000802 }
803}
804
David Goodwin334c2642009-07-08 16:09:28 +0000805void ARMBaseInstrInfo::
806loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
807 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000808 const TargetRegisterClass *RC,
809 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000810 DebugLoc DL;
David Goodwin334c2642009-07-08 16:09:28 +0000811 if (I != MBB.end()) DL = I->getDebugLoc();
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000812 MachineFunction &MF = *MBB.getParent();
813 MachineFrameInfo &MFI = *MF.getFrameInfo();
Jim Grosbach31bc8492009-11-08 00:27:19 +0000814 unsigned Align = MFI.getObjectAlignment(FI);
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000815 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +0000816 MF.getMachineMemOperand(PseudoSourceValue::getFixedStack(FI),
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000817 MachineMemOperand::MOLoad, 0,
818 MFI.getObjectSize(FI),
Jim Grosbach31bc8492009-11-08 00:27:19 +0000819 Align);
David Goodwin334c2642009-07-08 16:09:28 +0000820
Bob Wilson0eb0c742010-02-16 22:01:59 +0000821 // tGPR is used sometimes in ARM instructions that need to avoid using
822 // certain registers. Just treat it as GPR here.
Jim Grosbach6ccfc502010-07-30 02:41:01 +0000823 if (RC == ARM::tGPRRegisterClass || RC == ARM::tcGPRRegisterClass
824 || RC == ARM::rGPRRegisterClass)
Bob Wilson0eb0c742010-02-16 22:01:59 +0000825 RC = ARM::GPRRegisterClass;
826
Bob Wilsonebe99b22010-06-18 21:32:42 +0000827 switch (RC->getID()) {
828 case ARM::GPRRegClassID:
Evan Cheng5732ca02009-07-27 03:14:20 +0000829 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::LDR), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000830 .addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000831 break;
832 case ARM::SPRRegClassID:
Evan Chengd31c5492010-05-06 01:34:11 +0000833 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
834 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000835 break;
836 case ARM::DPRRegClassID:
837 case ARM::DPR_VFP2RegClassID:
838 case ARM::DPR_8RegClassID:
Jim Grosbache5165492009-11-09 00:11:35 +0000839 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
Anton Korobeynikov249fb332009-10-07 00:06:35 +0000840 .addFrameIndex(FI).addImm(0).addMemOperand(MMO));
Bob Wilsonebe99b22010-06-18 21:32:42 +0000841 break;
842 case ARM::QPRRegClassID:
843 case ARM::QPR_VFP2RegClassID:
844 case ARM::QPR_8RegClassID:
Jim Grosbach0cfcf932010-09-08 00:26:59 +0000845 if (Align >= 16 && getRegisterInfo().needsStackRealignment(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000846 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1q64Pseudo), DestReg)
Bob Wilsonf967ca02010-07-06 21:26:18 +0000847 .addFrameIndex(FI).addImm(16)
Evan Cheng69b9f982010-05-13 01:12:06 +0000848 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000849 } else {
Evan Cheng69b9f982010-05-13 01:12:06 +0000850 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMQ), DestReg)
851 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000852 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
Evan Cheng69b9f982010-05-13 01:12:06 +0000853 .addMemOperand(MMO));
Jim Grosbach31bc8492009-11-08 00:27:19 +0000854 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000855 break;
856 case ARM::QQPRRegClassID:
857 case ARM::QQPR_VFP2RegClassID:
Evan Cheng435d4992010-05-07 02:04:02 +0000858 if (Align >= 16 && getRegisterInfo().canRealignStack(MF)) {
Bob Wilson168f3822010-09-15 01:48:05 +0000859 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
860 .addFrameIndex(FI).addImm(16)
861 .addMemOperand(MMO));
Evan Cheng435d4992010-05-07 02:04:02 +0000862 } else {
863 MachineInstrBuilder MIB =
864 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
865 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000866 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Evan Cheng435d4992010-05-07 02:04:02 +0000867 .addMemOperand(MMO);
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000868 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
869 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
870 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
871 AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
Evan Cheng435d4992010-05-07 02:04:02 +0000872 }
Bob Wilsonebe99b22010-06-18 21:32:42 +0000873 break;
874 case ARM::QQQQPRRegClassID: {
875 MachineInstrBuilder MIB =
876 AddDefaultPred(BuildMI(MBB, I, DL, get(ARM::VLDMD))
877 .addFrameIndex(FI)
Bob Wilsond4bfd542010-08-27 23:18:17 +0000878 .addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia)))
Bob Wilsonebe99b22010-06-18 21:32:42 +0000879 .addMemOperand(MMO);
880 MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::Define, TRI);
881 MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::Define, TRI);
882 MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::Define, TRI);
883 MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::Define, TRI);
884 MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::Define, TRI);
885 MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::Define, TRI);
886 MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::Define, TRI);
887 AddDReg(MIB, DestReg, ARM::dsub_7, RegState::Define, TRI);
888 break;
889 }
890 default:
891 llvm_unreachable("Unknown regclass!");
David Goodwin334c2642009-07-08 16:09:28 +0000892 }
893}
894
Evan Cheng62b50652010-04-26 07:39:25 +0000895MachineInstr*
896ARMBaseInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng8601a3d2010-04-29 01:13:30 +0000897 int FrameIx, uint64_t Offset,
Evan Cheng62b50652010-04-26 07:39:25 +0000898 const MDNode *MDPtr,
899 DebugLoc DL) const {
900 MachineInstrBuilder MIB = BuildMI(MF, DL, get(ARM::DBG_VALUE))
901 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
902 return &*MIB;
903}
904
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000905/// Create a copy of a const pool value. Update CPI to the new index and return
906/// the label UID.
907static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
908 MachineConstantPool *MCP = MF.getConstantPool();
909 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
910
911 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
912 assert(MCPE.isMachineConstantPoolEntry() &&
913 "Expecting a machine constantpool entry!");
914 ARMConstantPoolValue *ACPV =
915 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
916
917 unsigned PCLabelId = AFI->createConstPoolEntryUId();
918 ARMConstantPoolValue *NewCPV = 0;
Jim Grosbach51f5b672010-09-10 21:38:22 +0000919 // FIXME: The below assumes PIC relocation model and that the function
920 // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
921 // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
922 // instructions, so that's probably OK, but is PIC always correct when
923 // we get here?
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000924 if (ACPV->isGlobalValue())
925 NewCPV = new ARMConstantPoolValue(ACPV->getGV(), PCLabelId,
926 ARMCP::CPValue, 4);
927 else if (ACPV->isExtSymbol())
928 NewCPV = new ARMConstantPoolValue(MF.getFunction()->getContext(),
929 ACPV->getSymbol(), PCLabelId, 4);
930 else if (ACPV->isBlockAddress())
931 NewCPV = new ARMConstantPoolValue(ACPV->getBlockAddress(), PCLabelId,
932 ARMCP::CPBlockAddress, 4);
Jim Grosbach51f5b672010-09-10 21:38:22 +0000933 else if (ACPV->isLSDA())
934 NewCPV = new ARMConstantPoolValue(MF.getFunction(), PCLabelId,
935 ARMCP::CPLSDA, 4);
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000936 else
937 llvm_unreachable("Unexpected ARM constantpool value type!!");
938 CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlignment());
939 return PCLabelId;
940}
941
Evan Chengfdc83402009-11-08 00:15:23 +0000942void ARMBaseInstrInfo::
943reMaterialize(MachineBasicBlock &MBB,
944 MachineBasicBlock::iterator I,
945 unsigned DestReg, unsigned SubIdx,
Evan Chengd57cdd52009-11-14 02:55:43 +0000946 const MachineInstr *Orig,
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000947 const TargetRegisterInfo &TRI) const {
Evan Chengfdc83402009-11-08 00:15:23 +0000948 unsigned Opcode = Orig->getOpcode();
949 switch (Opcode) {
950 default: {
951 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Jakob Stoklund Olesen9edf7de2010-06-02 22:47:25 +0000952 MI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chengfdc83402009-11-08 00:15:23 +0000953 MBB.insert(I, MI);
954 break;
955 }
956 case ARM::tLDRpci_pic:
957 case ARM::t2LDRpci_pic: {
958 MachineFunction &MF = *MBB.getParent();
Evan Chengfdc83402009-11-08 00:15:23 +0000959 unsigned CPI = Orig->getOperand(1).getIndex();
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000960 unsigned PCLabelId = duplicateCPV(MF, CPI);
Evan Chengfdc83402009-11-08 00:15:23 +0000961 MachineInstrBuilder MIB = BuildMI(MBB, I, Orig->getDebugLoc(), get(Opcode),
962 DestReg)
963 .addConstantPoolIndex(CPI).addImm(PCLabelId);
964 (*MIB).setMemRefs(Orig->memoperands_begin(), Orig->memoperands_end());
965 break;
966 }
967 }
Evan Chengfdc83402009-11-08 00:15:23 +0000968}
969
Jakob Stoklund Olesen30ac0462010-01-06 23:47:07 +0000970MachineInstr *
971ARMBaseInstrInfo::duplicate(MachineInstr *Orig, MachineFunction &MF) const {
972 MachineInstr *MI = TargetInstrInfoImpl::duplicate(Orig, MF);
973 switch(Orig->getOpcode()) {
974 case ARM::tLDRpci_pic:
975 case ARM::t2LDRpci_pic: {
976 unsigned CPI = Orig->getOperand(1).getIndex();
977 unsigned PCLabelId = duplicateCPV(MF, CPI);
978 Orig->getOperand(1).setIndex(CPI);
979 Orig->getOperand(2).setImm(PCLabelId);
980 break;
981 }
982 }
983 return MI;
984}
985
Evan Cheng506049f2010-03-03 01:44:33 +0000986bool ARMBaseInstrInfo::produceSameValue(const MachineInstr *MI0,
987 const MachineInstr *MI1) const {
Evan Chengd457e6e2009-11-07 04:04:34 +0000988 int Opcode = MI0->getOpcode();
Evan Cheng9b824252009-11-20 02:10:27 +0000989 if (Opcode == ARM::t2LDRpci ||
990 Opcode == ARM::t2LDRpci_pic ||
991 Opcode == ARM::tLDRpci ||
992 Opcode == ARM::tLDRpci_pic) {
Evan Chengd457e6e2009-11-07 04:04:34 +0000993 if (MI1->getOpcode() != Opcode)
994 return false;
995 if (MI0->getNumOperands() != MI1->getNumOperands())
996 return false;
997
998 const MachineOperand &MO0 = MI0->getOperand(1);
999 const MachineOperand &MO1 = MI1->getOperand(1);
1000 if (MO0.getOffset() != MO1.getOffset())
1001 return false;
1002
1003 const MachineFunction *MF = MI0->getParent()->getParent();
1004 const MachineConstantPool *MCP = MF->getConstantPool();
1005 int CPI0 = MO0.getIndex();
1006 int CPI1 = MO1.getIndex();
1007 const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
1008 const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
1009 ARMConstantPoolValue *ACPV0 =
1010 static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
1011 ARMConstantPoolValue *ACPV1 =
1012 static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
1013 return ACPV0->hasSameValue(ACPV1);
1014 }
1015
Evan Cheng506049f2010-03-03 01:44:33 +00001016 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
Evan Chengd457e6e2009-11-07 04:04:34 +00001017}
1018
Bill Wendling4b722102010-06-23 23:00:16 +00001019/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
1020/// determine if two loads are loading from the same base address. It should
1021/// only return true if the base pointers are the same and the only differences
1022/// between the two addresses is the offset. It also returns the offsets by
1023/// reference.
1024bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
1025 int64_t &Offset1,
1026 int64_t &Offset2) const {
1027 // Don't worry about Thumb: just ARM and Thumb2.
1028 if (Subtarget.isThumb1Only()) return false;
1029
1030 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
1031 return false;
1032
1033 switch (Load1->getMachineOpcode()) {
1034 default:
1035 return false;
1036 case ARM::LDR:
1037 case ARM::LDRB:
1038 case ARM::LDRD:
1039 case ARM::LDRH:
1040 case ARM::LDRSB:
1041 case ARM::LDRSH:
1042 case ARM::VLDRD:
1043 case ARM::VLDRS:
1044 case ARM::t2LDRi8:
1045 case ARM::t2LDRDi8:
1046 case ARM::t2LDRSHi8:
1047 case ARM::t2LDRi12:
1048 case ARM::t2LDRSHi12:
1049 break;
1050 }
1051
1052 switch (Load2->getMachineOpcode()) {
1053 default:
1054 return false;
1055 case ARM::LDR:
1056 case ARM::LDRB:
1057 case ARM::LDRD:
1058 case ARM::LDRH:
1059 case ARM::LDRSB:
1060 case ARM::LDRSH:
1061 case ARM::VLDRD:
1062 case ARM::VLDRS:
1063 case ARM::t2LDRi8:
1064 case ARM::t2LDRDi8:
1065 case ARM::t2LDRSHi8:
1066 case ARM::t2LDRi12:
1067 case ARM::t2LDRSHi12:
1068 break;
1069 }
1070
1071 // Check if base addresses and chain operands match.
1072 if (Load1->getOperand(0) != Load2->getOperand(0) ||
1073 Load1->getOperand(4) != Load2->getOperand(4))
1074 return false;
1075
1076 // Index should be Reg0.
1077 if (Load1->getOperand(3) != Load2->getOperand(3))
1078 return false;
1079
1080 // Determine the offsets.
1081 if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
1082 isa<ConstantSDNode>(Load2->getOperand(1))) {
1083 Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
1084 Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
1085 return true;
1086 }
1087
1088 return false;
1089}
1090
1091/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
1092/// determine (in conjuction with areLoadsFromSameBasePtr) if two loads should
1093/// be scheduled togther. On some targets if two loads are loading from
1094/// addresses in the same cache line, it's better if they are scheduled
1095/// together. This function takes two integers that represent the load offsets
1096/// from the common base address. It returns true if it decides it's desirable
1097/// to schedule the two loads together. "NumLoads" is the number of loads that
1098/// have already been scheduled after Load1.
1099bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
1100 int64_t Offset1, int64_t Offset2,
1101 unsigned NumLoads) const {
1102 // Don't worry about Thumb: just ARM and Thumb2.
1103 if (Subtarget.isThumb1Only()) return false;
1104
1105 assert(Offset2 > Offset1);
1106
1107 if ((Offset2 - Offset1) / 8 > 64)
1108 return false;
1109
1110 if (Load1->getMachineOpcode() != Load2->getMachineOpcode())
1111 return false; // FIXME: overly conservative?
1112
1113 // Four loads in a row should be sufficient.
1114 if (NumLoads >= 3)
1115 return false;
1116
1117 return true;
1118}
1119
Evan Cheng86050dc2010-06-18 23:09:54 +00001120bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
1121 const MachineBasicBlock *MBB,
1122 const MachineFunction &MF) const {
Jim Grosbach57bb3942010-06-25 18:43:14 +00001123 // Debug info is never a scheduling boundary. It's necessary to be explicit
1124 // due to the special treatment of IT instructions below, otherwise a
1125 // dbg_value followed by an IT will result in the IT instruction being
1126 // considered a scheduling hazard, which is wrong. It should be the actual
1127 // instruction preceding the dbg_value instruction(s), just like it is
1128 // when debug info is not present.
1129 if (MI->isDebugValue())
1130 return false;
1131
Evan Cheng86050dc2010-06-18 23:09:54 +00001132 // Terminators and labels can't be scheduled around.
1133 if (MI->getDesc().isTerminator() || MI->isLabel())
1134 return true;
1135
1136 // Treat the start of the IT block as a scheduling boundary, but schedule
1137 // t2IT along with all instructions following it.
1138 // FIXME: This is a big hammer. But the alternative is to add all potential
1139 // true and anti dependencies to IT block instructions as implicit operands
1140 // to the t2IT instruction. The added compile time and complexity does not
1141 // seem worth it.
1142 MachineBasicBlock::const_iterator I = MI;
Jim Grosbach57bb3942010-06-25 18:43:14 +00001143 // Make sure to skip any dbg_value instructions
1144 while (++I != MBB->end() && I->isDebugValue())
1145 ;
1146 if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
Evan Cheng86050dc2010-06-18 23:09:54 +00001147 return true;
1148
1149 // Don't attempt to schedule around any instruction that defines
1150 // a stack-oriented pointer, as it's unlikely to be profitable. This
1151 // saves compile time, because it doesn't require every single
1152 // stack slot reference to depend on the instruction that does the
1153 // modification.
1154 if (MI->definesRegister(ARM::SP))
1155 return true;
1156
1157 return false;
1158}
1159
Evan Cheng13151432010-06-25 22:42:03 +00001160bool ARMBaseInstrInfo::
1161isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumInstrs) const {
1162 if (!NumInstrs)
1163 return false;
1164 if (Subtarget.getCPUString() == "generic")
1165 // Generic (and overly aggressive) if-conversion limits for testing.
1166 return NumInstrs <= 10;
1167 else if (Subtarget.hasV7Ops())
1168 return NumInstrs <= 3;
1169 return NumInstrs <= 2;
1170}
1171
1172bool ARMBaseInstrInfo::
1173isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
1174 MachineBasicBlock &FMBB, unsigned NumF) const {
1175 return NumT && NumF && NumT <= 2 && NumF <= 2;
1176}
1177
Evan Cheng8fb90362009-08-08 03:20:32 +00001178/// getInstrPredicate - If instruction is predicated, returns its predicate
1179/// condition, otherwise returns AL. It also returns the condition code
1180/// register by reference.
Evan Cheng5adb66a2009-09-28 09:14:39 +00001181ARMCC::CondCodes
1182llvm::getInstrPredicate(const MachineInstr *MI, unsigned &PredReg) {
Evan Cheng8fb90362009-08-08 03:20:32 +00001183 int PIdx = MI->findFirstPredOperandIdx();
1184 if (PIdx == -1) {
1185 PredReg = 0;
1186 return ARMCC::AL;
1187 }
1188
1189 PredReg = MI->getOperand(PIdx+1).getReg();
1190 return (ARMCC::CondCodes)MI->getOperand(PIdx).getImm();
1191}
1192
1193
Evan Cheng6495f632009-07-28 05:48:47 +00001194int llvm::getMatchingCondBranchOpcode(int Opc) {
Evan Cheng5ca53a72009-07-27 18:20:05 +00001195 if (Opc == ARM::B)
1196 return ARM::Bcc;
1197 else if (Opc == ARM::tB)
1198 return ARM::tBcc;
1199 else if (Opc == ARM::t2B)
1200 return ARM::t2Bcc;
1201
1202 llvm_unreachable("Unknown unconditional branch opcode!");
1203 return 0;
1204}
1205
Evan Cheng6495f632009-07-28 05:48:47 +00001206
1207void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
1208 MachineBasicBlock::iterator &MBBI, DebugLoc dl,
1209 unsigned DestReg, unsigned BaseReg, int NumBytes,
1210 ARMCC::CondCodes Pred, unsigned PredReg,
1211 const ARMBaseInstrInfo &TII) {
1212 bool isSub = NumBytes < 0;
1213 if (isSub) NumBytes = -NumBytes;
1214
1215 while (NumBytes) {
1216 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
1217 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
1218 assert(ThisVal && "Didn't extract field correctly");
1219
1220 // We will handle these bits from offset, clear them.
1221 NumBytes &= ~ThisVal;
1222
1223 assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
1224
1225 // Build the new ADD / SUB.
1226 unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
1227 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
1228 .addReg(BaseReg, RegState::Kill).addImm(ThisVal)
1229 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
1230 BaseReg = DestReg;
1231 }
1232}
1233
Evan Chengcdbb3f52009-08-27 01:23:50 +00001234bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
1235 unsigned FrameReg, int &Offset,
1236 const ARMBaseInstrInfo &TII) {
Evan Cheng6495f632009-07-28 05:48:47 +00001237 unsigned Opcode = MI.getOpcode();
1238 const TargetInstrDesc &Desc = MI.getDesc();
1239 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1240 bool isSub = false;
Jim Grosbach764ab522009-08-11 15:33:49 +00001241
Evan Cheng6495f632009-07-28 05:48:47 +00001242 // Memory operands in inline assembly always use AddrMode2.
1243 if (Opcode == ARM::INLINEASM)
1244 AddrMode = ARMII::AddrMode2;
Jim Grosbach764ab522009-08-11 15:33:49 +00001245
Evan Cheng6495f632009-07-28 05:48:47 +00001246 if (Opcode == ARM::ADDri) {
1247 Offset += MI.getOperand(FrameRegIdx+1).getImm();
1248 if (Offset == 0) {
1249 // Turn it into a move.
1250 MI.setDesc(TII.get(ARM::MOVr));
1251 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1252 MI.RemoveOperand(FrameRegIdx+1);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001253 Offset = 0;
1254 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001255 } else if (Offset < 0) {
1256 Offset = -Offset;
1257 isSub = true;
1258 MI.setDesc(TII.get(ARM::SUBri));
1259 }
1260
1261 // Common case: small offset, fits into instruction.
1262 if (ARM_AM::getSOImmVal(Offset) != -1) {
1263 // Replace the FrameIndex with sp / fp
1264 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1265 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001266 Offset = 0;
1267 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001268 }
1269
1270 // Otherwise, pull as much of the immedidate into this ADDri/SUBri
1271 // as possible.
1272 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
1273 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
1274
1275 // We will handle these bits from offset, clear them.
1276 Offset &= ~ThisImmVal;
1277
1278 // Get the properly encoded SOImmVal field.
1279 assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
1280 "Bit extraction didn't work?");
1281 MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
1282 } else {
1283 unsigned ImmIdx = 0;
1284 int InstrOffs = 0;
1285 unsigned NumBits = 0;
1286 unsigned Scale = 1;
1287 switch (AddrMode) {
1288 case ARMII::AddrMode2: {
1289 ImmIdx = FrameRegIdx+2;
1290 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
1291 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1292 InstrOffs *= -1;
1293 NumBits = 12;
1294 break;
1295 }
1296 case ARMII::AddrMode3: {
1297 ImmIdx = FrameRegIdx+2;
1298 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
1299 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1300 InstrOffs *= -1;
1301 NumBits = 8;
1302 break;
1303 }
Anton Korobeynikovbaf31082009-08-08 13:35:48 +00001304 case ARMII::AddrMode4:
Jim Grosbacha4432172009-11-15 21:45:34 +00001305 case ARMII::AddrMode6:
Evan Chengcdbb3f52009-08-27 01:23:50 +00001306 // Can't fold any offset even if it's zero.
1307 return false;
Evan Cheng6495f632009-07-28 05:48:47 +00001308 case ARMII::AddrMode5: {
1309 ImmIdx = FrameRegIdx+1;
1310 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
1311 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
1312 InstrOffs *= -1;
1313 NumBits = 8;
1314 Scale = 4;
1315 break;
1316 }
1317 default:
1318 llvm_unreachable("Unsupported addressing mode!");
1319 break;
1320 }
1321
1322 Offset += InstrOffs * Scale;
1323 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
1324 if (Offset < 0) {
1325 Offset = -Offset;
1326 isSub = true;
1327 }
1328
1329 // Attempt to fold address comp. if opcode has offset bits
1330 if (NumBits > 0) {
1331 // Common case: small offset, fits into instruction.
1332 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
1333 int ImmedOffset = Offset / Scale;
1334 unsigned Mask = (1 << NumBits) - 1;
1335 if ((unsigned)Offset <= Mask * Scale) {
1336 // Replace the FrameIndex with sp
1337 MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
1338 if (isSub)
1339 ImmedOffset |= 1 << NumBits;
1340 ImmOp.ChangeToImmediate(ImmedOffset);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001341 Offset = 0;
1342 return true;
Evan Cheng6495f632009-07-28 05:48:47 +00001343 }
Jim Grosbach764ab522009-08-11 15:33:49 +00001344
Evan Cheng6495f632009-07-28 05:48:47 +00001345 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
1346 ImmedOffset = ImmedOffset & Mask;
1347 if (isSub)
1348 ImmedOffset |= 1 << NumBits;
1349 ImmOp.ChangeToImmediate(ImmedOffset);
1350 Offset &= ~(Mask*Scale);
1351 }
1352 }
1353
Evan Chengcdbb3f52009-08-27 01:23:50 +00001354 Offset = (isSub) ? -Offset : Offset;
1355 return Offset == 0;
Evan Cheng6495f632009-07-28 05:48:47 +00001356}
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001357
1358bool ARMBaseInstrInfo::
Bill Wendlingc98af332010-08-08 05:04:59 +00001359AnalyzeCompare(const MachineInstr *MI, unsigned &SrcReg, int &CmpValue) const {
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001360 switch (MI->getOpcode()) {
1361 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001362 case ARM::CMPri:
1363 case ARM::CMPzri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001364 case ARM::t2CMPri:
1365 case ARM::t2CMPzri:
1366 SrcReg = MI->getOperand(0).getReg();
1367 CmpValue = MI->getOperand(1).getImm();
1368 return true;
Gabor Greifde90bfd2010-09-14 09:23:22 +00001369 case ARM::TSTri: {
Gabor Greif308f64a2010-09-14 22:25:16 +00001370 if (&*MI->getParent()->begin() == MI)
Gabor Greifde90bfd2010-09-14 09:23:22 +00001371 return false;
1372 const MachineInstr *AND = llvm::prior(MI);
1373 if (AND->getOpcode() != ARM::ANDri)
1374 return false;
1375 if (MI->getOperand(0).getReg() == AND->getOperand(1).getReg() &&
1376 MI->getOperand(1).getImm() == AND->getOperand(2).getImm()) {
1377 SrcReg = AND->getOperand(0).getReg();
1378 CmpValue = 0;
1379 return true;
1380 }
1381 }
Gabor Greif308f64a2010-09-14 22:25:16 +00001382 break;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001383 }
1384
1385 return false;
1386}
1387
Bill Wendlinga6556862010-09-11 00:13:50 +00001388/// OptimizeCompareInstr - Convert the instruction supplying the argument to the
Bill Wendling92ad57f2010-09-10 23:34:19 +00001389/// comparison into one that sets the zero bit in the flags register. Update the
1390/// iterator *only* if a transformation took place.
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001391bool ARMBaseInstrInfo::
Bill Wendlinga6556862010-09-11 00:13:50 +00001392OptimizeCompareInstr(MachineInstr *CmpInstr, unsigned SrcReg, int CmpValue,
Bill Wendling220e2402010-09-10 21:55:43 +00001393 MachineBasicBlock::iterator &MII) const {
Bill Wendling36656612010-09-10 23:46:12 +00001394 if (CmpValue != 0)
Bill Wendling92ad57f2010-09-10 23:34:19 +00001395 return false;
1396
1397 MachineRegisterInfo &MRI = CmpInstr->getParent()->getParent()->getRegInfo();
1398 MachineRegisterInfo::def_iterator DI = MRI.def_begin(SrcReg);
1399 if (llvm::next(DI) != MRI.def_end())
1400 // Only support one definition.
1401 return false;
1402
1403 MachineInstr *MI = &*DI;
1404
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001405 // Conservatively refuse to convert an instruction which isn't in the same BB
1406 // as the comparison.
1407 if (MI->getParent() != CmpInstr->getParent())
1408 return false;
1409
1410 // Check that CPSR isn't set between the comparison instruction and the one we
1411 // want to change.
1412 MachineBasicBlock::const_iterator I = CmpInstr, E = MI;
1413 --I;
1414 for (; I != E; --I) {
1415 const MachineInstr &Instr = *I;
1416
1417 for (unsigned IO = 0, EO = Instr.getNumOperands(); IO != EO; ++IO) {
1418 const MachineOperand &MO = Instr.getOperand(IO);
Bill Wendling75486db2010-08-10 21:38:11 +00001419 if (!MO.isReg() || !MO.isDef()) continue;
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001420
1421 // This instruction modifies CPSR before the one we want to change. We
1422 // can't do this transformation.
1423 if (MO.getReg() == ARM::CPSR)
1424 return false;
1425 }
1426 }
1427
1428 // Set the "zero" bit in CPSR.
1429 switch (MI->getOpcode()) {
1430 default: break;
Bill Wendling38ae9972010-08-11 00:23:00 +00001431 case ARM::ADDri:
Gabor Greifde90bfd2010-09-14 09:23:22 +00001432 case ARM::ANDri:
1433 case ARM::t2ANDri:
Bill Wendling38ae9972010-08-11 00:23:00 +00001434 case ARM::SUBri:
1435 case ARM::t2ADDri:
Bill Wendlingad422712010-08-18 21:32:07 +00001436 case ARM::t2SUBri:
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001437 MI->RemoveOperand(5);
Bill Wendlingad422712010-08-18 21:32:07 +00001438 MachineInstrBuilder(MI)
1439 .addReg(ARM::CPSR, RegState::Define | RegState::Implicit);
Bill Wendling220e2402010-09-10 21:55:43 +00001440 MII = llvm::next(MachineBasicBlock::iterator(CmpInstr));
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001441 CmpInstr->eraseFromParent();
1442 return true;
1443 }
Bill Wendlinge4ddbdf2010-08-06 01:32:48 +00001444
1445 return false;
1446}
Evan Cheng5f54ce32010-09-09 18:18:55 +00001447
1448unsigned
1449ARMBaseInstrInfo::getNumMicroOps(const MachineInstr *MI,
Evan Cheng3ef1c872010-09-10 01:29:16 +00001450 const InstrItineraryData *ItinData) const {
1451 if (!ItinData || ItinData->isEmpty())
Evan Cheng5f54ce32010-09-09 18:18:55 +00001452 return 1;
1453
1454 const TargetInstrDesc &Desc = MI->getDesc();
1455 unsigned Class = Desc.getSchedClass();
Bob Wilson064312d2010-09-15 16:28:21 +00001456 unsigned UOps = ItinData->Itineraries[Class].NumMicroOps;
Evan Cheng5f54ce32010-09-09 18:18:55 +00001457 if (UOps)
1458 return UOps;
1459
1460 unsigned Opc = MI->getOpcode();
1461 switch (Opc) {
1462 default:
1463 llvm_unreachable("Unexpected multi-uops instruction!");
1464 break;
Evan Cheng3ef1c872010-09-10 01:29:16 +00001465 case ARM::VLDMQ:
Evan Cheng5f54ce32010-09-09 18:18:55 +00001466 case ARM::VSTMQ:
1467 return 2;
1468
1469 // The number of uOps for load / store multiple are determined by the number
1470 // registers.
Evan Cheng3ef1c872010-09-10 01:29:16 +00001471 // On Cortex-A8, each pair of register loads / stores can be scheduled on the
1472 // same cycle. The scheduling for the first load / store must be done
1473 // separately by assuming the the address is not 64-bit aligned.
1474 // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
1475 // is not 64-bit aligned, then AGU would take an extra cycle.
1476 // For VFP / NEON load / store multiple, the formula is
Evan Cheng5f54ce32010-09-09 18:18:55 +00001477 // (#reg / 2) + (#reg % 2) + 1.
Evan Cheng5f54ce32010-09-09 18:18:55 +00001478 case ARM::VLDMD:
1479 case ARM::VLDMS:
1480 case ARM::VLDMD_UPD:
1481 case ARM::VLDMS_UPD:
1482 case ARM::VSTMD:
1483 case ARM::VSTMS:
1484 case ARM::VSTMD_UPD:
1485 case ARM::VSTMS_UPD: {
1486 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands();
1487 return (NumRegs / 2) + (NumRegs % 2) + 1;
1488 }
1489 case ARM::LDM_RET:
1490 case ARM::LDM:
1491 case ARM::LDM_UPD:
1492 case ARM::STM:
1493 case ARM::STM_UPD:
1494 case ARM::tLDM:
1495 case ARM::tLDM_UPD:
1496 case ARM::tSTM_UPD:
1497 case ARM::tPOP_RET:
1498 case ARM::tPOP:
1499 case ARM::tPUSH:
1500 case ARM::t2LDM_RET:
1501 case ARM::t2LDM:
1502 case ARM::t2LDM_UPD:
1503 case ARM::t2STM:
1504 case ARM::t2STM_UPD: {
Evan Cheng3ef1c872010-09-10 01:29:16 +00001505 unsigned NumRegs = MI->getNumOperands() - Desc.getNumOperands() + 1;
1506 if (Subtarget.isCortexA8()) {
1507 // 4 registers would be issued: 1, 2, 1.
1508 // 5 registers would be issued: 1, 2, 2.
1509 return 1 + (NumRegs / 2);
1510 } else if (Subtarget.isCortexA9()) {
1511 UOps = (NumRegs / 2);
1512 // If there are odd number of registers or if it's not 64-bit aligned,
1513 // then it takes an extra AGU (Address Generation Unit) cycle.
1514 if ((NumRegs % 2) ||
1515 !MI->hasOneMemOperand() ||
1516 (*MI->memoperands_begin())->getAlignment() < 8)
1517 ++UOps;
1518 return UOps;
1519 } else {
1520 // Assume the worst.
1521 return NumRegs;
1522 }
Evan Cheng5f54ce32010-09-09 18:18:55 +00001523 }
1524 }
1525}