1 //===- ARMInstrInfo.cpp - ARM Instruction Information -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the "Instituto Nokia de Tecnologia" and
6 // is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file contains the ARM implementation of the TargetInstrInfo class.
13 //===----------------------------------------------------------------------===//
15 #include "ARMInstrInfo.h"
17 #include "ARMAddressingModes.h"
18 #include "ARMGenInstrInfo.inc"
19 #include "ARMMachineFunctionInfo.h"
20 #include "llvm/CodeGen/LiveVariables.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineJumpTableInfo.h"
23 #include "llvm/Target/TargetAsmInfo.h"
24 #include "llvm/Support/CommandLine.h"
27 static cl::opt<bool> EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
28 cl::desc("Enable ARM 2-addr to 3-addr conv"));
30 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
31 : TargetInstrInfo(ARMInsts, sizeof(ARMInsts)/sizeof(ARMInsts[0])),
35 const TargetRegisterClass *ARMInstrInfo::getPointerRegClass() const {
36 return &ARM::GPRRegClass;
39 /// Return true if the instruction is a register to register move and
40 /// leave the source and dest operands in the passed parameters.
42 bool ARMInstrInfo::isMoveInstr(const MachineInstr &MI,
43 unsigned &SrcReg, unsigned &DstReg) const {
44 MachineOpCode oc = MI.getOpcode();
50 SrcReg = MI.getOperand(1).getReg();
51 DstReg = MI.getOperand(0).getReg();
55 assert(MI.getInstrDescriptor()->numOperands >= 2 &&
56 MI.getOperand(0).isRegister() &&
57 MI.getOperand(1).isRegister() &&
58 "Invalid ARM MOV instruction");
59 SrcReg = MI.getOperand(1).getReg();
60 DstReg = MI.getOperand(0).getReg();
65 unsigned ARMInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
66 switch (MI->getOpcode()) {
69 if (MI->getOperand(1).isFrameIndex() &&
70 MI->getOperand(2).isReg() &&
71 MI->getOperand(3).isImmediate() &&
72 MI->getOperand(2).getReg() == 0 &&
73 MI->getOperand(3).getImmedValue() == 0) {
74 FrameIndex = MI->getOperand(1).getFrameIndex();
75 return MI->getOperand(0).getReg();
80 if (MI->getOperand(1).isFrameIndex() &&
81 MI->getOperand(2).isImmediate() &&
82 MI->getOperand(2).getImmedValue() == 0) {
83 FrameIndex = MI->getOperand(1).getFrameIndex();
84 return MI->getOperand(0).getReg();
88 if (MI->getOperand(1).isFrameIndex() &&
89 MI->getOperand(2).isImmediate() &&
90 MI->getOperand(2).getImmedValue() == 0) {
91 FrameIndex = MI->getOperand(1).getFrameIndex();
92 return MI->getOperand(0).getReg();
99 unsigned ARMInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
100 switch (MI->getOpcode()) {
103 if (MI->getOperand(1).isFrameIndex() &&
104 MI->getOperand(2).isReg() &&
105 MI->getOperand(3).isImmediate() &&
106 MI->getOperand(2).getReg() == 0 &&
107 MI->getOperand(3).getImmedValue() == 0) {
108 FrameIndex = MI->getOperand(1).getFrameIndex();
109 return MI->getOperand(0).getReg();
114 if (MI->getOperand(1).isFrameIndex() &&
115 MI->getOperand(2).isImmediate() &&
116 MI->getOperand(2).getImmedValue() == 0) {
117 FrameIndex = MI->getOperand(1).getFrameIndex();
118 return MI->getOperand(0).getReg();
122 if (MI->getOperand(1).isFrameIndex() &&
123 MI->getOperand(2).isImmediate() &&
124 MI->getOperand(2).getImmedValue() == 0) {
125 FrameIndex = MI->getOperand(1).getFrameIndex();
126 return MI->getOperand(0).getReg();
133 bool ARMInstrInfo::isTriviallyReMaterializable(MachineInstr *MI) const {
134 switch (MI->getOpcode()) {
139 case ARM::MOVi2pieces:
141 // These instructions are always trivially rematerializable.
147 static unsigned getUnindexedOpcode(unsigned Opc) {
160 case ARM::LDRSH_POST:
163 case ARM::LDRSB_POST:
179 ARMInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
180 MachineBasicBlock::iterator &MBBI,
181 LiveVariables &LV) const {
185 MachineInstr *MI = MBBI;
186 unsigned TSFlags = MI->getInstrDescriptor()->TSFlags;
188 switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
189 default: return NULL;
190 case ARMII::IndexModePre:
193 case ARMII::IndexModePost:
197 // Try spliting an indexed load / store to a un-indexed one plus an add/sub
199 unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
203 MachineInstr *UpdateMI = NULL;
204 MachineInstr *MemMI = NULL;
205 unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
206 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
207 unsigned NumOps = TID->numOperands;
208 bool isLoad = (TID->Flags & M_LOAD_FLAG) != 0;
209 const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
210 const MachineOperand &Base = MI->getOperand(2);
211 const MachineOperand &Offset = MI->getOperand(NumOps-3);
212 unsigned WBReg = WB.getReg();
213 unsigned BaseReg = Base.getReg();
214 unsigned OffReg = Offset.getReg();
215 unsigned OffImm = MI->getOperand(NumOps-2).getImm();
216 ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
219 assert(false && "Unknown indexed op!");
221 case ARMII::AddrMode2: {
222 bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
223 unsigned Amt = ARM_AM::getAM2Offset(OffImm);
225 int SOImmVal = ARM_AM::getSOImmVal(Amt);
227 // Can't encode it in a so_imm operand. This transformation will
228 // add more than 1 instruction. Abandon!
230 UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
231 .addReg(BaseReg).addImm(SOImmVal).addImm(Pred);
232 } else if (Amt != 0) {
233 ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
234 unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
235 UpdateMI = BuildMI(get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
236 .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc).addImm(Pred);
238 UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
239 .addReg(BaseReg).addReg(OffReg).addImm(Pred);
242 case ARMII::AddrMode3 : {
243 bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
244 unsigned Amt = ARM_AM::getAM3Offset(OffImm);
246 // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
247 UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
248 .addReg(BaseReg).addImm(Amt).addImm(Pred);
250 UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
251 .addReg(BaseReg).addReg(OffReg).addImm(Pred);
256 std::vector<MachineInstr*> NewMIs;
259 MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
260 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
262 MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
263 .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
264 NewMIs.push_back(MemMI);
265 NewMIs.push_back(UpdateMI);
268 MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
269 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
271 MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
272 .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
274 UpdateMI->getOperand(0).setIsDead();
275 NewMIs.push_back(UpdateMI);
276 NewMIs.push_back(MemMI);
279 // Transfer LiveVariables states, kill / dead info.
280 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
281 MachineOperand &MO = MI->getOperand(i);
282 if (MO.isRegister() && MO.getReg() &&
283 MRegisterInfo::isVirtualRegister(MO.getReg())) {
284 unsigned Reg = MO.getReg();
285 LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
287 MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
289 LV.addVirtualRegisterDead(Reg, NewMI);
290 // Update the defining instruction.
291 if (VI.DefInst == MI)
294 if (MO.isUse() && MO.isKill()) {
295 for (unsigned j = 0; j < 2; ++j) {
296 // Look at the two new MI's in reverse order.
297 MachineInstr *NewMI = NewMIs[j];
298 int NIdx = NewMI->findRegisterUseOperandIdx(Reg);
301 LV.addVirtualRegisterKilled(Reg, NewMI);
302 if (VI.removeKill(MI))
303 VI.Kills.push_back(NewMI);
310 MFI->insert(MBBI, NewMIs[1]);
311 MFI->insert(MBBI, NewMIs[0]);
316 bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
317 MachineBasicBlock *&FBB,
318 std::vector<MachineOperand> &Cond) const {
319 // If the block has no terminators, it just falls into the block after it.
320 MachineBasicBlock::iterator I = MBB.end();
321 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
324 // Get the last instruction in the block.
325 MachineInstr *LastInst = I;
327 // If there is only one terminator instruction, process it.
328 unsigned LastOpc = LastInst->getOpcode();
329 if (I == MBB.begin() ||
330 isPredicated(--I) || !isUnpredicatedTerminator(I)) {
331 if (LastOpc == ARM::B || LastOpc == ARM::tB) {
332 TBB = LastInst->getOperand(0).getMachineBasicBlock();
335 if (LastOpc == ARM::Bcc || LastOpc == ARM::tBcc) {
336 // Block ends with fall-through condbranch.
337 TBB = LastInst->getOperand(0).getMachineBasicBlock();
338 Cond.push_back(LastInst->getOperand(1));
341 return true; // Can't handle indirect branch.
344 // Get the instruction before it if it is a terminator.
345 MachineInstr *SecondLastInst = I;
347 // If there are three terminators, we don't know what sort of block this is.
348 if (SecondLastInst && I != MBB.begin() &&
349 !isPredicated(--I) && isUnpredicatedTerminator(I))
352 // If the block ends with ARM::B/ARM::tB and a ARM::Bcc/ARM::tBcc, handle it.
353 unsigned SecondLastOpc = SecondLastInst->getOpcode();
354 if ((SecondLastOpc == ARM::Bcc && LastOpc == ARM::B) ||
355 (SecondLastOpc == ARM::tBcc && LastOpc == ARM::tB)) {
356 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
357 Cond.push_back(SecondLastInst->getOperand(1));
358 FBB = LastInst->getOperand(0).getMachineBasicBlock();
362 // If the block ends with two B's or tB's, handle it. The second one is not
363 // executed, so remove it.
364 if ((SecondLastOpc == ARM::B || SecondLastOpc==ARM::tB) &&
365 (LastOpc == ARM::B || LastOpc == ARM::tB)) {
366 TBB = SecondLastInst->getOperand(0).getMachineBasicBlock();
368 I->eraseFromParent();
372 // Otherwise, can't handle this.
377 unsigned ARMInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
378 MachineFunction &MF = *MBB.getParent();
379 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
380 int BOpc = AFI->isThumbFunction() ? ARM::tB : ARM::B;
381 int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
383 MachineBasicBlock::iterator I = MBB.end();
384 if (I == MBB.begin()) return 0;
386 if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc)
389 // Remove the branch.
390 I->eraseFromParent();
394 if (I == MBB.begin()) return 1;
396 if (I->getOpcode() != BccOpc)
399 // Remove the branch.
400 I->eraseFromParent();
404 unsigned ARMInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
405 MachineBasicBlock *FBB,
406 const std::vector<MachineOperand> &Cond) const {
407 MachineFunction &MF = *MBB.getParent();
408 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
409 int BOpc = AFI->isThumbFunction() ? ARM::tB : ARM::B;
410 int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
412 // Shouldn't be a fall through.
413 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
414 assert((Cond.size() == 1 || Cond.size() == 0) &&
415 "ARM branch conditions have two components!");
418 if (Cond.empty()) // Unconditional branch?
419 BuildMI(&MBB, get(BOpc)).addMBB(TBB);
421 BuildMI(&MBB, get(BccOpc)).addMBB(TBB).addImm(Cond[0].getImm());
425 // Two-way conditional branch.
426 BuildMI(&MBB, get(BccOpc)).addMBB(TBB).addImm(Cond[0].getImm());
427 BuildMI(&MBB, get(BOpc)).addMBB(FBB);
431 bool ARMInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
432 if (MBB.empty()) return false;
434 switch (MBB.back().getOpcode()) {
435 case ARM::BX_RET: // Return.
438 case ARM::tBX_RET_vararg:
441 case ARM::tB: // Uncond branch.
443 case ARM::BR_JTr: // Jumptable branch.
444 case ARM::BR_JTm: // Jumptable branch through mem.
445 case ARM::BR_JTadd: // Jumptable branch add to pc.
447 default: return false;
452 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
453 ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
454 Cond[0].setImm(ARMCC::getOppositeCondition(CC));
458 bool ARMInstrInfo::isPredicated(const MachineInstr *MI) const {
459 int PIdx = MI->findFirstPredOperandIdx();
460 return PIdx != -1 && MI->getOperand(PIdx).getImmedValue() != ARMCC::AL;
463 bool ARMInstrInfo::PredicateInstruction(MachineInstr *MI,
464 const std::vector<MachineOperand> &Pred) const {
465 unsigned Opc = MI->getOpcode();
466 if (Opc == ARM::B || Opc == ARM::tB) {
467 MI->setInstrDescriptor(get(Opc == ARM::B ? ARM::Bcc : ARM::tBcc));
468 MI->addImmOperand(Pred[0].getImmedValue());
472 int PIdx = MI->findFirstPredOperandIdx();
474 MachineOperand &PMO = MI->getOperand(PIdx);
475 PMO.setImm(Pred[0].getImmedValue());
482 ARMInstrInfo::SubsumesPredicate(const std::vector<MachineOperand> &Pred1,
483 const std::vector<MachineOperand> &Pred2) const{
484 if (Pred1.size() > 1 || Pred2.size() > 1)
487 ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImmedValue();
488 ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImmedValue();
498 return CC2 == ARMCC::HI;
500 return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
502 return CC2 == ARMCC::GT;
504 return CC2 == ARMCC::LT;
508 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing
509 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
510 unsigned JTI) DISABLE_INLINE;
511 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
513 return JT[JTI].MBBs.size();
516 /// GetInstSize - Return the size of the specified MachineInstr.
518 unsigned ARM::GetInstSize(MachineInstr *MI) {
519 MachineBasicBlock &MBB = *MI->getParent();
520 const MachineFunction *MF = MBB.getParent();
521 const TargetAsmInfo *TAI = MF->getTarget().getTargetAsmInfo();
523 // Basic size info comes from the TSFlags field.
524 const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
525 unsigned TSFlags = TID->TSFlags;
527 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
529 // If this machine instr is an inline asm, measure it.
530 if (MI->getOpcode() == ARM::INLINEASM)
531 return TAI->getInlineAsmLength(MI->getOperand(0).getSymbolName());
532 if (MI->getOpcode() == ARM::LABEL)
534 assert(0 && "Unknown or unset size field for instr!");
536 case ARMII::Size8Bytes: return 8; // Arm instruction x 2.
537 case ARMII::Size4Bytes: return 4; // Arm instruction.
538 case ARMII::Size2Bytes: return 2; // Thumb instruction.
539 case ARMII::SizeSpecial: {
540 switch (MI->getOpcode()) {
541 case ARM::CONSTPOOL_ENTRY:
542 // If this machine instr is a constant pool entry, its size is recorded as
544 return MI->getOperand(2).getImm();
549 // These are jumptable branches, i.e. a branch followed by an inlined
550 // jumptable. The size is 4 + 4 * number of entries.
551 unsigned NumOps = TID->numOperands;
552 MachineOperand JTOP =
553 MI->getOperand(NumOps - ((TID->Flags & M_PREDICABLE) ? 3 : 2));
554 unsigned JTI = JTOP.getJumpTableIndex();
555 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
556 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
557 assert(JTI < JT.size());
558 // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
559 // 4 aligned. The assembler / linker may add 2 byte padding just before
560 // the JT entries. The size does not include this padding; the
561 // constant islands pass does separate bookkeeping for it.
562 // FIXME: If we know the size of the function is less than (1 << 16) *2
563 // bytes, we can use 16-bit entries instead. Then there won't be an
565 return getNumJTEntries(JT, JTI) * 4 +
566 (MI->getOpcode()==ARM::tBR_JTr ? 2 : 4);
569 // Otherwise, pseudo-instruction sizes are zero.
576 /// GetFunctionSize - Returns the size of the specified MachineFunction.
578 unsigned ARM::GetFunctionSize(MachineFunction &MF) {
580 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
582 MachineBasicBlock &MBB = *MBBI;
583 for (MachineBasicBlock::iterator I = MBB.begin(),E = MBB.end(); I != E; ++I)
584 FnSize += ARM::GetInstSize(I);