Rename findRegisterUseOperand to findRegisterUseOperandIdx to avoid confusion.
[oota-llvm.git] / lib / Target / ARM / ARMInstrInfo.cpp
1 //===- ARMInstrInfo.cpp - ARM Instruction Information -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file contains the ARM implementation of the TargetInstrInfo class.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "ARMInstrInfo.h"
16 #include "ARM.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"
25 using namespace llvm;
26
27 static cl::opt<bool> EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
28                                   cl::desc("Enable ARM 2-addr to 3-addr conv"));
29
30 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
31   : TargetInstrInfo(ARMInsts, sizeof(ARMInsts)/sizeof(ARMInsts[0])),
32     RI(*this, STI) {
33 }
34
35 const TargetRegisterClass *ARMInstrInfo::getPointerRegClass() const {
36   return &ARM::GPRRegClass;
37 }
38
39 /// Return true if the instruction is a register to register move and
40 /// leave the source and dest operands in the passed parameters.
41 ///
42 bool ARMInstrInfo::isMoveInstr(const MachineInstr &MI,
43                                unsigned &SrcReg, unsigned &DstReg) const {
44   MachineOpCode oc = MI.getOpcode();
45   switch (oc) {
46   default:
47     return false;
48   case ARM::FCPYS:
49   case ARM::FCPYD:
50     SrcReg = MI.getOperand(1).getReg();
51     DstReg = MI.getOperand(0).getReg();
52     return true;
53   case ARM::MOVr:
54   case ARM::tMOVr:
55     assert(MI.getNumOperands() >= 2 && MI.getOperand(0).isRegister() &&
56            MI.getOperand(1).isRegister() &&
57            "Invalid ARM MOV instruction");
58     SrcReg = MI.getOperand(1).getReg();
59     DstReg = MI.getOperand(0).getReg();
60     return true;
61   }
62 }
63
64 unsigned ARMInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
65   switch (MI->getOpcode()) {
66   default: break;
67   case ARM::LDR:
68     if (MI->getOperand(1).isFrameIndex() &&
69         MI->getOperand(2).isReg() &&
70         MI->getOperand(3).isImmediate() && 
71         MI->getOperand(2).getReg() == 0 &&
72         MI->getOperand(3).getImmedValue() == 0) {
73       FrameIndex = MI->getOperand(1).getFrameIndex();
74       return MI->getOperand(0).getReg();
75     }
76     break;
77   case ARM::FLDD:
78   case ARM::FLDS:
79     if (MI->getOperand(1).isFrameIndex() &&
80         MI->getOperand(2).isImmediate() && 
81         MI->getOperand(2).getImmedValue() == 0) {
82       FrameIndex = MI->getOperand(1).getFrameIndex();
83       return MI->getOperand(0).getReg();
84     }
85     break;
86   case ARM::tRestore:
87     if (MI->getOperand(1).isFrameIndex() &&
88         MI->getOperand(2).isImmediate() && 
89         MI->getOperand(2).getImmedValue() == 0) {
90       FrameIndex = MI->getOperand(1).getFrameIndex();
91       return MI->getOperand(0).getReg();
92     }
93     break;
94   }
95   return 0;
96 }
97
98 unsigned ARMInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
99   switch (MI->getOpcode()) {
100   default: break;
101   case ARM::STR:
102     if (MI->getOperand(1).isFrameIndex() &&
103         MI->getOperand(2).isReg() &&
104         MI->getOperand(3).isImmediate() && 
105         MI->getOperand(2).getReg() == 0 &&
106         MI->getOperand(3).getImmedValue() == 0) {
107       FrameIndex = MI->getOperand(1).getFrameIndex();
108       return MI->getOperand(0).getReg();
109     }
110     break;
111   case ARM::FSTD:
112   case ARM::FSTS:
113     if (MI->getOperand(1).isFrameIndex() &&
114         MI->getOperand(2).isImmediate() && 
115         MI->getOperand(2).getImmedValue() == 0) {
116       FrameIndex = MI->getOperand(1).getFrameIndex();
117       return MI->getOperand(0).getReg();
118     }
119     break;
120   case ARM::tSpill:
121     if (MI->getOperand(1).isFrameIndex() &&
122         MI->getOperand(2).isImmediate() && 
123         MI->getOperand(2).getImmedValue() == 0) {
124       FrameIndex = MI->getOperand(1).getFrameIndex();
125       return MI->getOperand(0).getReg();
126     }
127     break;
128   }
129   return 0;
130 }
131
132 static unsigned getUnindexedOpcode(unsigned Opc) {
133   switch (Opc) {
134   default: break;
135   case ARM::LDR_PRE:
136   case ARM::LDR_POST:
137     return ARM::LDR;
138   case ARM::LDRH_PRE:
139   case ARM::LDRH_POST:
140     return ARM::LDRH;
141   case ARM::LDRB_PRE:
142   case ARM::LDRB_POST:
143     return ARM::LDRB;
144   case ARM::LDRSH_PRE:
145   case ARM::LDRSH_POST:
146     return ARM::LDRSH;
147   case ARM::LDRSB_PRE:
148   case ARM::LDRSB_POST:
149     return ARM::LDRSB;
150   case ARM::STR_PRE:
151   case ARM::STR_POST:
152     return ARM::STR;
153   case ARM::STRH_PRE:
154   case ARM::STRH_POST:
155     return ARM::STRH;
156   case ARM::STRB_PRE:
157   case ARM::STRB_POST:
158     return ARM::STRB;
159   }
160   return 0;
161 }
162
163 MachineInstr *
164 ARMInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
165                                     MachineBasicBlock::iterator &MBBI,
166                                     LiveVariables &LV) const {
167   if (!EnableARM3Addr)
168     return NULL;
169
170   MachineInstr *MI = MBBI;
171   unsigned TSFlags = MI->getInstrDescriptor()->TSFlags;
172   bool isPre = false;
173   switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
174   default: return NULL;
175   case ARMII::IndexModePre:
176     isPre = true;
177     break;
178   case ARMII::IndexModePost:
179     break;
180   }
181
182   // Try spliting an indexed load / store to a un-indexed one plus an add/sub
183   // operation.
184   unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
185   if (MemOpc == 0)
186     return NULL;
187
188   MachineInstr *UpdateMI = NULL;
189   MachineInstr *MemMI = NULL;
190   unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
191   unsigned NumOps = MI->getNumOperands();
192   bool isLoad = (MI->getInstrDescriptor()->Flags & M_LOAD_FLAG) != 0;
193   const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
194   const MachineOperand &Base = MI->getOperand(2);
195   const MachineOperand &Offset = MI->getOperand(NumOps-2);
196   unsigned WBReg = WB.getReg();
197   unsigned BaseReg = Base.getReg();
198   unsigned OffReg = Offset.getReg();
199   unsigned OffImm = MI->getOperand(NumOps-1).getImm();
200   switch (AddrMode) {
201   default:
202     assert(false && "Unknown indexed op!");
203     return NULL;
204   case ARMII::AddrMode2: {
205     bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
206     unsigned Amt = ARM_AM::getAM2Offset(OffImm);
207     if (OffReg == 0) {
208       int SOImmVal = ARM_AM::getSOImmVal(Amt);
209       if (SOImmVal == -1)
210         // Can't encode it in a so_imm operand. This transformation will
211         // add more than 1 instruction. Abandon!
212         return NULL;
213       UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
214         .addReg(BaseReg).addImm(SOImmVal);
215     } else if (Amt != 0) {
216       ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
217       unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
218       UpdateMI = BuildMI(get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
219         .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc);
220     } else 
221       UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
222         .addReg(BaseReg).addReg(OffReg);
223     break;
224   }
225   case ARMII::AddrMode3 : {
226     bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
227     unsigned Amt = ARM_AM::getAM3Offset(OffImm);
228     if (OffReg == 0)
229       // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
230       UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
231         .addReg(BaseReg).addImm(Amt);
232     else
233       UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
234         .addReg(BaseReg).addReg(OffReg);
235     break;
236   }
237   }
238
239   std::vector<MachineInstr*> NewMIs;
240   if (isPre) {
241     if (isLoad)
242       MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
243         .addReg(WBReg).addReg(0).addImm(0);
244     else
245       MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
246         .addReg(WBReg).addReg(0).addImm(0);
247     NewMIs.push_back(MemMI);
248     NewMIs.push_back(UpdateMI);
249   } else {
250     if (isLoad)
251       MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
252         .addReg(BaseReg).addReg(0).addImm(0);
253     else
254       MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
255         .addReg(BaseReg).addReg(0).addImm(0);
256     if (WB.isDead())
257       UpdateMI->getOperand(0).setIsDead();
258     NewMIs.push_back(UpdateMI);
259     NewMIs.push_back(MemMI);
260   }
261   
262   // Transfer LiveVariables states, kill / dead info.
263   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
264     MachineOperand &MO = MI->getOperand(i);
265     if (MO.isRegister() && MO.getReg() &&
266         MRegisterInfo::isVirtualRegister(MO.getReg())) {
267       unsigned Reg = MO.getReg();
268       LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
269       if (MO.isDef()) {
270         MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
271         if (MO.isDead())
272           LV.addVirtualRegisterDead(Reg, NewMI);
273         // Update the defining instruction.
274         if (VI.DefInst == MI)
275           VI.DefInst = NewMI;
276       }
277       if (MO.isUse() && MO.isKill()) {
278         for (unsigned j = 0; j < 2; ++j) {
279           // Look at the two new MI's in reverse order.
280           MachineInstr *NewMI = NewMIs[j];
281           int NIdx = NewMI->findRegisterUseOperandIdx(Reg);
282           if (NIdx == -1)
283             continue;
284           LV.addVirtualRegisterKilled(Reg, NewMI);
285           if (VI.removeKill(MI))
286             VI.Kills.push_back(NewMI);
287           break;
288         }
289       }
290     }
291   }
292
293   MFI->insert(MBBI, NewMIs[1]);
294   MFI->insert(MBBI, NewMIs[0]);
295   return NewMIs[0];
296 }
297
298 // Branch analysis.
299 bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
300                                  MachineBasicBlock *&FBB,
301                                  std::vector<MachineOperand> &Cond) const {
302   // If the block has no terminators, it just falls into the block after it.
303   MachineBasicBlock::iterator I = MBB.end();
304   if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode()))
305     return false;
306   
307   // Get the last instruction in the block.
308   MachineInstr *LastInst = I;
309   
310   // If there is only one terminator instruction, process it.
311   unsigned LastOpc = LastInst->getOpcode();
312   if (I == MBB.begin() || !isTerminatorInstr((--I)->getOpcode())) {
313     if (LastOpc == ARM::B || LastOpc == ARM::tB) {
314       TBB = LastInst->getOperand(0).getMachineBasicBlock();
315       return false;
316     }
317     if (LastOpc == ARM::Bcc || LastOpc == ARM::tBcc) {
318       // Block ends with fall-through condbranch.
319       TBB = LastInst->getOperand(0).getMachineBasicBlock();
320       Cond.push_back(LastInst->getOperand(1));
321       return false;
322     }
323     return true;  // Can't handle indirect branch.
324   }
325   
326   // Get the instruction before it if it is a terminator.
327   MachineInstr *SecondLastInst = I;
328   
329   // If there are three terminators, we don't know what sort of block this is.
330   if (SecondLastInst && I != MBB.begin() &&
331       isTerminatorInstr((--I)->getOpcode()))
332     return true;
333   
334   // If the block ends with ARM::B/ARM::tB and a ARM::Bcc/ARM::tBcc, handle it.
335   unsigned SecondLastOpc = SecondLastInst->getOpcode();
336   if ((SecondLastOpc == ARM::Bcc && LastOpc == ARM::B) ||
337       (SecondLastOpc == ARM::tBcc && LastOpc == ARM::tB)) {
338     TBB =  SecondLastInst->getOperand(0).getMachineBasicBlock();
339     Cond.push_back(SecondLastInst->getOperand(1));
340     FBB = LastInst->getOperand(0).getMachineBasicBlock();
341     return false;
342   }
343   
344   // Otherwise, can't handle this.
345   return true;
346 }
347
348
349 void ARMInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
350   MachineFunction &MF = *MBB.getParent();
351   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
352   int BOpc   = AFI->isThumbFunction() ? ARM::tB : ARM::B;
353   int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
354
355   MachineBasicBlock::iterator I = MBB.end();
356   if (I == MBB.begin()) return;
357   --I;
358   if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc)
359     return;
360   
361   // Remove the branch.
362   I->eraseFromParent();
363   
364   I = MBB.end();
365   
366   if (I == MBB.begin()) return;
367   --I;
368   if (I->getOpcode() != BccOpc)
369     return;
370   
371   // Remove the branch.
372   I->eraseFromParent();
373 }
374
375 void ARMInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
376                                 MachineBasicBlock *FBB,
377                                 const std::vector<MachineOperand> &Cond) 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;
382
383   // Shouldn't be a fall through.
384   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
385   assert((Cond.size() == 1 || Cond.size() == 0) &&
386          "ARM branch conditions have two components!");
387   
388   if (FBB == 0) {
389     if (Cond.empty()) // Unconditional branch?
390       BuildMI(&MBB, get(BOpc)).addMBB(TBB);
391     else
392       BuildMI(&MBB, get(BccOpc)).addMBB(TBB).addImm(Cond[0].getImm());
393     return;
394   }
395   
396   // Two-way conditional branch.
397   BuildMI(&MBB, get(BccOpc)).addMBB(TBB).addImm(Cond[0].getImm());
398   BuildMI(&MBB, get(BOpc)).addMBB(FBB);
399 }
400
401 bool ARMInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
402   if (MBB.empty()) return false;
403   
404   switch (MBB.back().getOpcode()) {
405   case ARM::B:
406   case ARM::tB:       // Uncond branch.
407   case ARM::tBR_JTr:
408   case ARM::BR_JTr:   // Jumptable branch.
409   case ARM::BR_JTm:   // Jumptable branch through mem.
410   case ARM::BR_JTadd: // Jumptable branch add to pc.
411     return true;
412   default: return false;
413   }
414 }
415
416 bool ARMInstrInfo::
417 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
418   ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
419   Cond[0].setImm(ARMCC::getOppositeCondition(CC));
420   return false;
421 }
422
423
424 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing
425 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
426                                 unsigned JTI) DISABLE_INLINE;
427 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
428                                 unsigned JTI) {
429   return JT[JTI].MBBs.size();
430 }
431
432 /// GetInstSize - Return the size of the specified MachineInstr.
433 ///
434 unsigned ARM::GetInstSize(MachineInstr *MI) {
435   MachineBasicBlock &MBB = *MI->getParent();
436   const MachineFunction *MF = MBB.getParent();
437   const TargetAsmInfo *TAI = MF->getTarget().getTargetAsmInfo();
438
439   // Basic size info comes from the TSFlags field.
440   unsigned TSFlags = MI->getInstrDescriptor()->TSFlags;
441   
442   switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
443   default:
444     // If this machine instr is an inline asm, measure it.
445     if (MI->getOpcode() == ARM::INLINEASM)
446       return TAI->getInlineAsmLength(MI->getOperand(0).getSymbolName());
447     if (MI->getOpcode() == ARM::LABEL)
448       return 0;
449     assert(0 && "Unknown or unset size field for instr!");
450     break;
451   case ARMII::Size8Bytes: return 8;          // Arm instruction x 2.
452   case ARMII::Size4Bytes: return 4;          // Arm instruction.
453   case ARMII::Size2Bytes: return 2;          // Thumb instruction.
454   case ARMII::SizeSpecial: {
455     switch (MI->getOpcode()) {
456     case ARM::CONSTPOOL_ENTRY:
457       // If this machine instr is a constant pool entry, its size is recorded as
458       // operand #2.
459       return MI->getOperand(2).getImm();
460     case ARM::BR_JTr:
461     case ARM::BR_JTm:
462     case ARM::BR_JTadd:
463     case ARM::tBR_JTr: {
464       // These are jumptable branches, i.e. a branch followed by an inlined
465       // jumptable. The size is 4 + 4 * number of entries.
466       unsigned JTI = MI->getOperand(MI->getNumOperands()-2).getJumpTableIndex();
467       MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
468       const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
469       assert(JTI < JT.size());
470       // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
471       // 4 aligned. The assembler / linker may add 2 byte padding just before
472       // the JT entries. Use + 4 even for tBR_JTr to purposely over-estimate
473       // the size the jumptable.
474       // FIXME: If we know the size of the function is less than (1 << 16) *2
475       // bytes, we can use 16-bit entries instead. Then there won't be an
476       // alignment issue.
477       return getNumJTEntries(JT, JTI) * 4 + 4;
478     }
479     default:
480       // Otherwise, pseudo-instruction sizes are zero.
481       return 0;
482     }
483   }
484   }
485 }
486
487 /// GetFunctionSize - Returns the size of the specified MachineFunction.
488 ///
489 unsigned ARM::GetFunctionSize(MachineFunction &MF) {
490   unsigned FnSize = 0;
491   for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
492        MBBI != E; ++MBBI) {
493     MachineBasicBlock &MBB = *MBBI;
494     for (MachineBasicBlock::iterator I = MBB.begin(),E = MBB.end(); I != E; ++I)
495       FnSize += ARM::GetInstSize(I);
496   }
497   return FnSize;
498 }