rename MachineInstr::setInstrDescriptor -> setDesc
[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 is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the ARM implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARMInstrInfo.h"
15 #include "ARM.h"
16 #include "ARMAddressingModes.h"
17 #include "ARMGenInstrInfo.inc"
18 #include "ARMMachineFunctionInfo.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/CodeGen/LiveVariables.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineJumpTableInfo.h"
24 #include "llvm/Target/TargetAsmInfo.h"
25 #include "llvm/Support/CommandLine.h"
26 using namespace llvm;
27
28 static cl::opt<bool> EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
29                                   cl::desc("Enable ARM 2-addr to 3-addr conv"));
30
31 static inline
32 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
33   return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
34 }
35
36 static inline
37 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
38   return MIB.addReg(0);
39 }
40
41 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
42   : TargetInstrInfoImpl(ARMInsts, array_lengthof(ARMInsts)),
43     RI(*this, STI) {
44 }
45
46 const TargetRegisterClass *ARMInstrInfo::getPointerRegClass() const {
47   return &ARM::GPRRegClass;
48 }
49
50 /// Return true if the instruction is a register to register move and
51 /// leave the source and dest operands in the passed parameters.
52 ///
53 bool ARMInstrInfo::isMoveInstr(const MachineInstr &MI,
54                                unsigned &SrcReg, unsigned &DstReg) const {
55   unsigned oc = MI.getOpcode();
56   switch (oc) {
57   default:
58     return false;
59   case ARM::FCPYS:
60   case ARM::FCPYD:
61     SrcReg = MI.getOperand(1).getReg();
62     DstReg = MI.getOperand(0).getReg();
63     return true;
64   case ARM::MOVr:
65   case ARM::tMOVr:
66     assert(MI.getDesc().getNumOperands() >= 2 &&
67            MI.getOperand(0).isRegister() &&
68            MI.getOperand(1).isRegister() &&
69            "Invalid ARM MOV instruction");
70     SrcReg = MI.getOperand(1).getReg();
71     DstReg = MI.getOperand(0).getReg();
72     return true;
73   }
74 }
75
76 unsigned ARMInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
77   switch (MI->getOpcode()) {
78   default: break;
79   case ARM::LDR:
80     if (MI->getOperand(1).isFrameIndex() &&
81         MI->getOperand(2).isRegister() &&
82         MI->getOperand(3).isImmediate() && 
83         MI->getOperand(2).getReg() == 0 &&
84         MI->getOperand(3).getImm() == 0) {
85       FrameIndex = MI->getOperand(1).getIndex();
86       return MI->getOperand(0).getReg();
87     }
88     break;
89   case ARM::FLDD:
90   case ARM::FLDS:
91     if (MI->getOperand(1).isFrameIndex() &&
92         MI->getOperand(2).isImmediate() && 
93         MI->getOperand(2).getImm() == 0) {
94       FrameIndex = MI->getOperand(1).getIndex();
95       return MI->getOperand(0).getReg();
96     }
97     break;
98   case ARM::tRestore:
99     if (MI->getOperand(1).isFrameIndex() &&
100         MI->getOperand(2).isImmediate() && 
101         MI->getOperand(2).getImm() == 0) {
102       FrameIndex = MI->getOperand(1).getIndex();
103       return MI->getOperand(0).getReg();
104     }
105     break;
106   }
107   return 0;
108 }
109
110 unsigned ARMInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
111   switch (MI->getOpcode()) {
112   default: break;
113   case ARM::STR:
114     if (MI->getOperand(1).isFrameIndex() &&
115         MI->getOperand(2).isRegister() &&
116         MI->getOperand(3).isImmediate() && 
117         MI->getOperand(2).getReg() == 0 &&
118         MI->getOperand(3).getImm() == 0) {
119       FrameIndex = MI->getOperand(1).getIndex();
120       return MI->getOperand(0).getReg();
121     }
122     break;
123   case ARM::FSTD:
124   case ARM::FSTS:
125     if (MI->getOperand(1).isFrameIndex() &&
126         MI->getOperand(2).isImmediate() && 
127         MI->getOperand(2).getImm() == 0) {
128       FrameIndex = MI->getOperand(1).getIndex();
129       return MI->getOperand(0).getReg();
130     }
131     break;
132   case ARM::tSpill:
133     if (MI->getOperand(1).isFrameIndex() &&
134         MI->getOperand(2).isImmediate() && 
135         MI->getOperand(2).getImm() == 0) {
136       FrameIndex = MI->getOperand(1).getIndex();
137       return MI->getOperand(0).getReg();
138     }
139     break;
140   }
141   return 0;
142 }
143
144 static unsigned getUnindexedOpcode(unsigned Opc) {
145   switch (Opc) {
146   default: break;
147   case ARM::LDR_PRE:
148   case ARM::LDR_POST:
149     return ARM::LDR;
150   case ARM::LDRH_PRE:
151   case ARM::LDRH_POST:
152     return ARM::LDRH;
153   case ARM::LDRB_PRE:
154   case ARM::LDRB_POST:
155     return ARM::LDRB;
156   case ARM::LDRSH_PRE:
157   case ARM::LDRSH_POST:
158     return ARM::LDRSH;
159   case ARM::LDRSB_PRE:
160   case ARM::LDRSB_POST:
161     return ARM::LDRSB;
162   case ARM::STR_PRE:
163   case ARM::STR_POST:
164     return ARM::STR;
165   case ARM::STRH_PRE:
166   case ARM::STRH_POST:
167     return ARM::STRH;
168   case ARM::STRB_PRE:
169   case ARM::STRB_POST:
170     return ARM::STRB;
171   }
172   return 0;
173 }
174
175 MachineInstr *
176 ARMInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
177                                     MachineBasicBlock::iterator &MBBI,
178                                     LiveVariables &LV) const {
179   if (!EnableARM3Addr)
180     return NULL;
181
182   MachineInstr *MI = MBBI;
183   unsigned TSFlags = MI->getDesc().TSFlags;
184   bool isPre = false;
185   switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
186   default: return NULL;
187   case ARMII::IndexModePre:
188     isPre = true;
189     break;
190   case ARMII::IndexModePost:
191     break;
192   }
193
194   // Try spliting an indexed load / store to a un-indexed one plus an add/sub
195   // operation.
196   unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
197   if (MemOpc == 0)
198     return NULL;
199
200   MachineInstr *UpdateMI = NULL;
201   MachineInstr *MemMI = NULL;
202   unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
203   const TargetInstrDesc &TID = MI->getDesc();
204   unsigned NumOps = TID.getNumOperands();
205   bool isLoad = !TID.mayStore();
206   const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
207   const MachineOperand &Base = MI->getOperand(2);
208   const MachineOperand &Offset = MI->getOperand(NumOps-3);
209   unsigned WBReg = WB.getReg();
210   unsigned BaseReg = Base.getReg();
211   unsigned OffReg = Offset.getReg();
212   unsigned OffImm = MI->getOperand(NumOps-2).getImm();
213   ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
214   switch (AddrMode) {
215   default:
216     assert(false && "Unknown indexed op!");
217     return NULL;
218   case ARMII::AddrMode2: {
219     bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
220     unsigned Amt = ARM_AM::getAM2Offset(OffImm);
221     if (OffReg == 0) {
222       int SOImmVal = ARM_AM::getSOImmVal(Amt);
223       if (SOImmVal == -1)
224         // Can't encode it in a so_imm operand. This transformation will
225         // add more than 1 instruction. Abandon!
226         return NULL;
227       UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
228         .addReg(BaseReg).addImm(SOImmVal)
229         .addImm(Pred).addReg(0).addReg(0);
230     } else if (Amt != 0) {
231       ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
232       unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
233       UpdateMI = BuildMI(get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
234         .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
235         .addImm(Pred).addReg(0).addReg(0);
236     } else 
237       UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
238         .addReg(BaseReg).addReg(OffReg)
239         .addImm(Pred).addReg(0).addReg(0);
240     break;
241   }
242   case ARMII::AddrMode3 : {
243     bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
244     unsigned Amt = ARM_AM::getAM3Offset(OffImm);
245     if (OffReg == 0)
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)
249         .addImm(Pred).addReg(0).addReg(0);
250     else
251       UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
252         .addReg(BaseReg).addReg(OffReg)
253         .addImm(Pred).addReg(0).addReg(0);
254     break;
255   }
256   }
257
258   std::vector<MachineInstr*> NewMIs;
259   if (isPre) {
260     if (isLoad)
261       MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
262         .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
263     else
264       MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
265         .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
266     NewMIs.push_back(MemMI);
267     NewMIs.push_back(UpdateMI);
268   } else {
269     if (isLoad)
270       MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
271         .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
272     else
273       MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
274         .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
275     if (WB.isDead())
276       UpdateMI->getOperand(0).setIsDead();
277     NewMIs.push_back(UpdateMI);
278     NewMIs.push_back(MemMI);
279   }
280   
281   // Transfer LiveVariables states, kill / dead info.
282   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
283     MachineOperand &MO = MI->getOperand(i);
284     if (MO.isRegister() && MO.getReg() &&
285         MRegisterInfo::isVirtualRegister(MO.getReg())) {
286       unsigned Reg = MO.getReg();
287       LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
288       if (MO.isDef()) {
289         MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
290         if (MO.isDead())
291           LV.addVirtualRegisterDead(Reg, NewMI);
292         // Update the defining instruction.
293         if (VI.DefInst == MI)
294           VI.DefInst = NewMI;
295       }
296       if (MO.isUse() && MO.isKill()) {
297         for (unsigned j = 0; j < 2; ++j) {
298           // Look at the two new MI's in reverse order.
299           MachineInstr *NewMI = NewMIs[j];
300           int NIdx = NewMI->findRegisterUseOperandIdx(Reg);
301           if (NIdx == -1)
302             continue;
303           LV.addVirtualRegisterKilled(Reg, NewMI);
304           if (VI.removeKill(MI))
305             VI.Kills.push_back(NewMI);
306           break;
307         }
308       }
309     }
310   }
311
312   MFI->insert(MBBI, NewMIs[1]);
313   MFI->insert(MBBI, NewMIs[0]);
314   return NewMIs[0];
315 }
316
317 // Branch analysis.
318 bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
319                                  MachineBasicBlock *&FBB,
320                                  std::vector<MachineOperand> &Cond) const {
321   // If the block has no terminators, it just falls into the block after it.
322   MachineBasicBlock::iterator I = MBB.end();
323   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
324     return false;
325   
326   // Get the last instruction in the block.
327   MachineInstr *LastInst = I;
328   
329   // If there is only one terminator instruction, process it.
330   unsigned LastOpc = LastInst->getOpcode();
331   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
332     if (LastOpc == ARM::B || LastOpc == ARM::tB) {
333       TBB = LastInst->getOperand(0).getMBB();
334       return false;
335     }
336     if (LastOpc == ARM::Bcc || LastOpc == ARM::tBcc) {
337       // Block ends with fall-through condbranch.
338       TBB = LastInst->getOperand(0).getMBB();
339       Cond.push_back(LastInst->getOperand(1));
340       Cond.push_back(LastInst->getOperand(2));
341       return false;
342     }
343     return true;  // Can't handle indirect branch.
344   }
345   
346   // Get the instruction before it if it is a terminator.
347   MachineInstr *SecondLastInst = I;
348   
349   // If there are three terminators, we don't know what sort of block this is.
350   if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
351     return true;
352   
353   // If the block ends with ARM::B/ARM::tB and a ARM::Bcc/ARM::tBcc, handle it.
354   unsigned SecondLastOpc = SecondLastInst->getOpcode();
355   if ((SecondLastOpc == ARM::Bcc && LastOpc == ARM::B) ||
356       (SecondLastOpc == ARM::tBcc && LastOpc == ARM::tB)) {
357     TBB =  SecondLastInst->getOperand(0).getMBB();
358     Cond.push_back(SecondLastInst->getOperand(1));
359     Cond.push_back(SecondLastInst->getOperand(2));
360     FBB = LastInst->getOperand(0).getMBB();
361     return false;
362   }
363   
364   // If the block ends with two unconditional branches, handle it.  The second 
365   // one is not executed, so remove it.
366   if ((SecondLastOpc == ARM::B || SecondLastOpc==ARM::tB) &&
367       (LastOpc == ARM::B || LastOpc == ARM::tB)) {
368     TBB = SecondLastInst->getOperand(0).getMBB();
369     I = LastInst;
370     I->eraseFromParent();
371     return false;
372   }
373
374   // Likewise if it ends with a branch table followed by an unconditional branch.
375   // The branch folder can create these, and we must get rid of them for
376   // correctness of Thumb constant islands.
377   if ((SecondLastOpc == ARM::BR_JTr || SecondLastOpc==ARM::BR_JTm ||
378        SecondLastOpc == ARM::BR_JTadd || SecondLastOpc==ARM::tBR_JTr) &&
379       (LastOpc == ARM::B || LastOpc == ARM::tB)) {
380     I = LastInst;
381     I->eraseFromParent();
382     return true;
383   } 
384
385   // Otherwise, can't handle this.
386   return true;
387 }
388
389
390 unsigned ARMInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
391   MachineFunction &MF = *MBB.getParent();
392   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
393   int BOpc   = AFI->isThumbFunction() ? ARM::tB : ARM::B;
394   int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
395
396   MachineBasicBlock::iterator I = MBB.end();
397   if (I == MBB.begin()) return 0;
398   --I;
399   if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc)
400     return 0;
401   
402   // Remove the branch.
403   I->eraseFromParent();
404   
405   I = MBB.end();
406   
407   if (I == MBB.begin()) return 1;
408   --I;
409   if (I->getOpcode() != BccOpc)
410     return 1;
411   
412   // Remove the branch.
413   I->eraseFromParent();
414   return 2;
415 }
416
417 unsigned ARMInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
418                                 MachineBasicBlock *FBB,
419                                 const std::vector<MachineOperand> &Cond) const {
420   MachineFunction &MF = *MBB.getParent();
421   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
422   int BOpc   = AFI->isThumbFunction() ? ARM::tB : ARM::B;
423   int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
424
425   // Shouldn't be a fall through.
426   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
427   assert((Cond.size() == 2 || Cond.size() == 0) &&
428          "ARM branch conditions have two components!");
429   
430   if (FBB == 0) {
431     if (Cond.empty()) // Unconditional branch?
432       BuildMI(&MBB, get(BOpc)).addMBB(TBB);
433     else
434       BuildMI(&MBB, get(BccOpc)).addMBB(TBB)
435         .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
436     return 1;
437   }
438   
439   // Two-way conditional branch.
440   BuildMI(&MBB, get(BccOpc)).addMBB(TBB)
441     .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
442   BuildMI(&MBB, get(BOpc)).addMBB(FBB);
443   return 2;
444 }
445
446 void ARMInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
447                                    MachineBasicBlock::iterator I,
448                                    unsigned DestReg, unsigned SrcReg,
449                                    const TargetRegisterClass *DestRC,
450                                    const TargetRegisterClass *SrcRC) const {
451   if (DestRC != SrcRC) {
452     cerr << "Not yet supported!";
453     abort();
454   }
455
456   if (DestRC == ARM::GPRRegisterClass) {
457     MachineFunction &MF = *MBB.getParent();
458     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
459     if (AFI->isThumbFunction())
460       BuildMI(MBB, I, get(ARM::tMOVr), DestReg).addReg(SrcReg);
461     else
462       AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, get(ARM::MOVr), DestReg)
463                                   .addReg(SrcReg)));
464   } else if (DestRC == ARM::SPRRegisterClass)
465     AddDefaultPred(BuildMI(MBB, I, get(ARM::FCPYS), DestReg)
466                    .addReg(SrcReg));
467   else if (DestRC == ARM::DPRRegisterClass)
468     AddDefaultPred(BuildMI(MBB, I, get(ARM::FCPYD), DestReg)
469                    .addReg(SrcReg));
470   else
471     abort();
472 }
473
474 static const MachineInstrBuilder &ARMInstrAddOperand(MachineInstrBuilder &MIB,
475                                                      MachineOperand &MO) {
476   if (MO.isRegister())
477     MIB = MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
478   else if (MO.isImmediate())
479     MIB = MIB.addImm(MO.getImm());
480   else if (MO.isFrameIndex())
481     MIB = MIB.addFrameIndex(MO.getIndex());
482   else
483     assert(0 && "Unknown operand for ARMInstrAddOperand!");
484
485   return MIB;
486 }
487
488 void ARMInstrInfo::
489 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
490                     unsigned SrcReg, bool isKill, int FI,
491                     const TargetRegisterClass *RC) const {
492   if (RC == ARM::GPRRegisterClass) {
493     MachineFunction &MF = *MBB.getParent();
494     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
495     if (AFI->isThumbFunction())
496       BuildMI(MBB, I, get(ARM::tSpill)).addReg(SrcReg, false, false, isKill)
497         .addFrameIndex(FI).addImm(0);
498     else
499       AddDefaultPred(BuildMI(MBB, I, get(ARM::STR))
500                      .addReg(SrcReg, false, false, isKill)
501                      .addFrameIndex(FI).addReg(0).addImm(0));
502   } else if (RC == ARM::DPRRegisterClass) {
503     AddDefaultPred(BuildMI(MBB, I, get(ARM::FSTD))
504                    .addReg(SrcReg, false, false, isKill)
505                    .addFrameIndex(FI).addImm(0));
506   } else {
507     assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
508     AddDefaultPred(BuildMI(MBB, I, get(ARM::FSTS))
509                    .addReg(SrcReg, false, false, isKill)
510                    .addFrameIndex(FI).addImm(0));
511   }
512 }
513
514 void ARMInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
515                                      bool isKill,
516                                      SmallVectorImpl<MachineOperand> &Addr,
517                                      const TargetRegisterClass *RC,
518                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
519   unsigned Opc = 0;
520   if (RC == ARM::GPRRegisterClass) {
521     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
522     if (AFI->isThumbFunction()) {
523       Opc = Addr[0].isFrameIndex() ? ARM::tSpill : ARM::tSTR;
524       MachineInstrBuilder MIB = 
525         BuildMI(get(Opc)).addReg(SrcReg, false, false, isKill);
526       for (unsigned i = 0, e = Addr.size(); i != e; ++i)
527         MIB = ARMInstrAddOperand(MIB, Addr[i]);
528       NewMIs.push_back(MIB);
529       return;
530     }
531     Opc = ARM::STR;
532   } else if (RC == ARM::DPRRegisterClass) {
533     Opc = ARM::FSTD;
534   } else {
535     assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
536     Opc = ARM::FSTS;
537   }
538
539   MachineInstrBuilder MIB = 
540     BuildMI(get(Opc)).addReg(SrcReg, false, false, isKill);
541   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
542     MIB = ARMInstrAddOperand(MIB, Addr[i]);
543   AddDefaultPred(MIB);
544   NewMIs.push_back(MIB);
545   return;
546 }
547
548 void ARMInstrInfo::
549 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
550                      unsigned DestReg, int FI,
551                      const TargetRegisterClass *RC) const {
552   if (RC == ARM::GPRRegisterClass) {
553     MachineFunction &MF = *MBB.getParent();
554     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
555     if (AFI->isThumbFunction())
556       BuildMI(MBB, I, get(ARM::tRestore), DestReg)
557         .addFrameIndex(FI).addImm(0);
558     else
559       AddDefaultPred(BuildMI(MBB, I, get(ARM::LDR), DestReg)
560                      .addFrameIndex(FI).addReg(0).addImm(0));
561   } else if (RC == ARM::DPRRegisterClass) {
562     AddDefaultPred(BuildMI(MBB, I, get(ARM::FLDD), DestReg)
563                    .addFrameIndex(FI).addImm(0));
564   } else {
565     assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
566     AddDefaultPred(BuildMI(MBB, I, get(ARM::FLDS), DestReg)
567                    .addFrameIndex(FI).addImm(0));
568   }
569 }
570
571 void ARMInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
572                                       SmallVectorImpl<MachineOperand> &Addr,
573                                       const TargetRegisterClass *RC,
574                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
575   unsigned Opc = 0;
576   if (RC == ARM::GPRRegisterClass) {
577     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
578     if (AFI->isThumbFunction()) {
579       Opc = Addr[0].isFrameIndex() ? ARM::tRestore : ARM::tLDR;
580       MachineInstrBuilder MIB = BuildMI(get(Opc), DestReg);
581       for (unsigned i = 0, e = Addr.size(); i != e; ++i)
582         MIB = ARMInstrAddOperand(MIB, Addr[i]);
583       NewMIs.push_back(MIB);
584       return;
585     }
586     Opc = ARM::LDR;
587   } else if (RC == ARM::DPRRegisterClass) {
588     Opc = ARM::FLDD;
589   } else {
590     assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
591     Opc = ARM::FLDS;
592   }
593
594   MachineInstrBuilder MIB =  BuildMI(get(Opc), DestReg);
595   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
596     MIB = ARMInstrAddOperand(MIB, Addr[i]);
597   AddDefaultPred(MIB);
598   NewMIs.push_back(MIB);
599   return;
600 }
601
602 bool ARMInstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
603                                                 MachineBasicBlock::iterator MI,
604                                 const std::vector<CalleeSavedInfo> &CSI) const {
605   MachineFunction &MF = *MBB.getParent();
606   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
607   if (!AFI->isThumbFunction() || CSI.empty())
608     return false;
609
610   MachineInstrBuilder MIB = BuildMI(MBB, MI, get(ARM::tPUSH));
611   for (unsigned i = CSI.size(); i != 0; --i) {
612     unsigned Reg = CSI[i-1].getReg();
613     // Add the callee-saved register as live-in. It's killed at the spill.
614     MBB.addLiveIn(Reg);
615     MIB.addReg(Reg, false/*isDef*/,false/*isImp*/,true/*isKill*/);
616   }
617   return true;
618 }
619
620 bool ARMInstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
621                                                  MachineBasicBlock::iterator MI,
622                                 const std::vector<CalleeSavedInfo> &CSI) const {
623   MachineFunction &MF = *MBB.getParent();
624   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
625   if (!AFI->isThumbFunction() || CSI.empty())
626     return false;
627
628   bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
629   MachineInstr *PopMI = new MachineInstr(get(ARM::tPOP));
630   MBB.insert(MI, PopMI);
631   for (unsigned i = CSI.size(); i != 0; --i) {
632     unsigned Reg = CSI[i-1].getReg();
633     if (Reg == ARM::LR) {
634       // Special epilogue for vararg functions. See emitEpilogue
635       if (isVarArg)
636         continue;
637       Reg = ARM::PC;
638       PopMI->setDesc(get(ARM::tPOP_RET));
639       MBB.erase(MI);
640     }
641     PopMI->addOperand(MachineOperand::CreateReg(Reg, true));
642   }
643   return true;
644 }
645
646 MachineInstr *ARMInstrInfo::foldMemoryOperand(MachineInstr *MI,
647                                                  SmallVectorImpl<unsigned> &Ops,
648                                                  int FI) const {
649   if (Ops.size() != 1) return NULL;
650
651   unsigned OpNum = Ops[0];
652   unsigned Opc = MI->getOpcode();
653   MachineInstr *NewMI = NULL;
654   switch (Opc) {
655   default: break;
656   case ARM::MOVr: {
657     if (MI->getOperand(4).getReg() == ARM::CPSR)
658       // If it is updating CPSR, then it cannot be foled.
659       break;
660     unsigned Pred = MI->getOperand(2).getImm();
661     unsigned PredReg = MI->getOperand(3).getReg();
662     if (OpNum == 0) { // move -> store
663       unsigned SrcReg = MI->getOperand(1).getReg();
664       NewMI = BuildMI(get(ARM::STR)).addReg(SrcReg).addFrameIndex(FI)
665         .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
666     } else {          // move -> load
667       unsigned DstReg = MI->getOperand(0).getReg();
668       NewMI = BuildMI(get(ARM::LDR), DstReg).addFrameIndex(FI).addReg(0)
669         .addImm(0).addImm(Pred).addReg(PredReg);
670     }
671     break;
672   }
673   case ARM::tMOVr: {
674     if (OpNum == 0) { // move -> store
675       unsigned SrcReg = MI->getOperand(1).getReg();
676       if (RI.isPhysicalRegister(SrcReg) && !RI.isLowRegister(SrcReg))
677         // tSpill cannot take a high register operand.
678         break;
679       NewMI = BuildMI(get(ARM::tSpill)).addReg(SrcReg).addFrameIndex(FI)
680         .addImm(0);
681     } else {          // move -> load
682       unsigned DstReg = MI->getOperand(0).getReg();
683       if (RI.isPhysicalRegister(DstReg) && !RI.isLowRegister(DstReg))
684         // tRestore cannot target a high register operand.
685         break;
686       NewMI = BuildMI(get(ARM::tRestore), DstReg).addFrameIndex(FI)
687         .addImm(0);
688     }
689     break;
690   }
691   case ARM::FCPYS: {
692     unsigned Pred = MI->getOperand(2).getImm();
693     unsigned PredReg = MI->getOperand(3).getReg();
694     if (OpNum == 0) { // move -> store
695       unsigned SrcReg = MI->getOperand(1).getReg();
696       NewMI = BuildMI(get(ARM::FSTS)).addReg(SrcReg).addFrameIndex(FI)
697         .addImm(0).addImm(Pred).addReg(PredReg);
698     } else {          // move -> load
699       unsigned DstReg = MI->getOperand(0).getReg();
700       NewMI = BuildMI(get(ARM::FLDS), DstReg).addFrameIndex(FI)
701         .addImm(0).addImm(Pred).addReg(PredReg);
702     }
703     break;
704   }
705   case ARM::FCPYD: {
706     unsigned Pred = MI->getOperand(2).getImm();
707     unsigned PredReg = MI->getOperand(3).getReg();
708     if (OpNum == 0) { // move -> store
709       unsigned SrcReg = MI->getOperand(1).getReg();
710       NewMI = BuildMI(get(ARM::FSTD)).addReg(SrcReg).addFrameIndex(FI)
711         .addImm(0).addImm(Pred).addReg(PredReg);
712     } else {          // move -> load
713       unsigned DstReg = MI->getOperand(0).getReg();
714       NewMI = BuildMI(get(ARM::FLDD), DstReg).addFrameIndex(FI)
715         .addImm(0).addImm(Pred).addReg(PredReg);
716     }
717     break;
718   }
719   }
720
721   if (NewMI)
722     NewMI->copyKillDeadInfo(MI);
723   return NewMI;
724 }
725
726 bool ARMInstrInfo::canFoldMemoryOperand(MachineInstr *MI,
727                                          SmallVectorImpl<unsigned> &Ops) const {
728   if (Ops.size() != 1) return false;
729
730   unsigned OpNum = Ops[0];
731   unsigned Opc = MI->getOpcode();
732   switch (Opc) {
733   default: break;
734   case ARM::MOVr:
735     // If it is updating CPSR, then it cannot be foled.
736     return MI->getOperand(4).getReg() != ARM::CPSR;
737   case ARM::tMOVr: {
738     if (OpNum == 0) { // move -> store
739       unsigned SrcReg = MI->getOperand(1).getReg();
740       if (RI.isPhysicalRegister(SrcReg) && !RI.isLowRegister(SrcReg))
741         // tSpill cannot take a high register operand.
742         return false;
743     } else {          // move -> load
744       unsigned DstReg = MI->getOperand(0).getReg();
745       if (RI.isPhysicalRegister(DstReg) && !RI.isLowRegister(DstReg))
746         // tRestore cannot target a high register operand.
747         return false;
748     }
749     return true;
750   }
751   case ARM::FCPYS:
752   case ARM::FCPYD:
753     return true;
754   }
755
756   return false;
757 }
758
759 bool ARMInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
760   if (MBB.empty()) return false;
761   
762   switch (MBB.back().getOpcode()) {
763   case ARM::BX_RET:   // Return.
764   case ARM::LDM_RET:
765   case ARM::tBX_RET:
766   case ARM::tBX_RET_vararg:
767   case ARM::tPOP_RET:
768   case ARM::B:
769   case ARM::tB:       // Uncond branch.
770   case ARM::tBR_JTr:
771   case ARM::BR_JTr:   // Jumptable branch.
772   case ARM::BR_JTm:   // Jumptable branch through mem.
773   case ARM::BR_JTadd: // Jumptable branch add to pc.
774     return true;
775   default: return false;
776   }
777 }
778
779 bool ARMInstrInfo::
780 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
781   ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
782   Cond[0].setImm(ARMCC::getOppositeCondition(CC));
783   return false;
784 }
785
786 bool ARMInstrInfo::isPredicated(const MachineInstr *MI) const {
787   int PIdx = MI->findFirstPredOperandIdx();
788   return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
789 }
790
791 bool ARMInstrInfo::PredicateInstruction(MachineInstr *MI,
792                                 const std::vector<MachineOperand> &Pred) const {
793   unsigned Opc = MI->getOpcode();
794   if (Opc == ARM::B || Opc == ARM::tB) {
795     MI->setDesc(get(Opc == ARM::B ? ARM::Bcc : ARM::tBcc));
796     MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
797     MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
798     return true;
799   }
800
801   int PIdx = MI->findFirstPredOperandIdx();
802   if (PIdx != -1) {
803     MachineOperand &PMO = MI->getOperand(PIdx);
804     PMO.setImm(Pred[0].getImm());
805     MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
806     return true;
807   }
808   return false;
809 }
810
811 bool
812 ARMInstrInfo::SubsumesPredicate(const std::vector<MachineOperand> &Pred1,
813                                 const std::vector<MachineOperand> &Pred2) const{
814   if (Pred1.size() > 2 || Pred2.size() > 2)
815     return false;
816
817   ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
818   ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
819   if (CC1 == CC2)
820     return true;
821
822   switch (CC1) {
823   default:
824     return false;
825   case ARMCC::AL:
826     return true;
827   case ARMCC::HS:
828     return CC2 == ARMCC::HI;
829   case ARMCC::LS:
830     return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
831   case ARMCC::GE:
832     return CC2 == ARMCC::GT;
833   case ARMCC::LE:
834     return CC2 == ARMCC::LT;
835   }
836 }
837
838 bool ARMInstrInfo::DefinesPredicate(MachineInstr *MI,
839                                     std::vector<MachineOperand> &Pred) const {
840   const TargetInstrDesc &TID = MI->getDesc();
841   if (!TID.getImplicitDefs() && !TID.hasOptionalDef())
842     return false;
843
844   bool Found = false;
845   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
846     const MachineOperand &MO = MI->getOperand(i);
847     if (MO.isRegister() && MO.getReg() == ARM::CPSR) {
848       Pred.push_back(MO);
849       Found = true;
850     }
851   }
852
853   return Found;
854 }
855
856
857 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing
858 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
859                                 unsigned JTI) DISABLE_INLINE;
860 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
861                                 unsigned JTI) {
862   return JT[JTI].MBBs.size();
863 }
864
865 /// GetInstSize - Return the size of the specified MachineInstr.
866 ///
867 unsigned ARM::GetInstSize(MachineInstr *MI) {
868   MachineBasicBlock &MBB = *MI->getParent();
869   const MachineFunction *MF = MBB.getParent();
870   const TargetAsmInfo *TAI = MF->getTarget().getTargetAsmInfo();
871
872   // Basic size info comes from the TSFlags field.
873   const TargetInstrDesc &TID = MI->getDesc();
874   unsigned TSFlags = TID.TSFlags;
875   
876   switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
877   default:
878     // If this machine instr is an inline asm, measure it.
879     if (MI->getOpcode() == ARM::INLINEASM)
880       return TAI->getInlineAsmLength(MI->getOperand(0).getSymbolName());
881     if (MI->getOpcode() == ARM::LABEL)
882       return 0;
883     assert(0 && "Unknown or unset size field for instr!");
884     break;
885   case ARMII::Size8Bytes: return 8;          // Arm instruction x 2.
886   case ARMII::Size4Bytes: return 4;          // Arm instruction.
887   case ARMII::Size2Bytes: return 2;          // Thumb instruction.
888   case ARMII::SizeSpecial: {
889     switch (MI->getOpcode()) {
890     case ARM::CONSTPOOL_ENTRY:
891       // If this machine instr is a constant pool entry, its size is recorded as
892       // operand #2.
893       return MI->getOperand(2).getImm();
894     case ARM::BR_JTr:
895     case ARM::BR_JTm:
896     case ARM::BR_JTadd:
897     case ARM::tBR_JTr: {
898       // These are jumptable branches, i.e. a branch followed by an inlined
899       // jumptable. The size is 4 + 4 * number of entries.
900       unsigned NumOps = TID.getNumOperands();
901       MachineOperand JTOP =
902         MI->getOperand(NumOps - (TID.isPredicable() ? 3 : 2));
903       unsigned JTI = JTOP.getIndex();
904       MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
905       const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
906       assert(JTI < JT.size());
907       // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
908       // 4 aligned. The assembler / linker may add 2 byte padding just before
909       // the JT entries.  The size does not include this padding; the
910       // constant islands pass does separate bookkeeping for it.
911       // FIXME: If we know the size of the function is less than (1 << 16) *2
912       // bytes, we can use 16-bit entries instead. Then there won't be an
913       // alignment issue.
914       return getNumJTEntries(JT, JTI) * 4 + 
915              (MI->getOpcode()==ARM::tBR_JTr ? 2 : 4);
916     }
917     default:
918       // Otherwise, pseudo-instruction sizes are zero.
919       return 0;
920     }
921   }
922   }
923 }
924
925 /// GetFunctionSize - Returns the size of the specified MachineFunction.
926 ///
927 unsigned ARM::GetFunctionSize(MachineFunction &MF) {
928   unsigned FnSize = 0;
929   for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
930        MBBI != E; ++MBBI) {
931     MachineBasicBlock &MBB = *MBBI;
932     for (MachineBasicBlock::iterator I = MBB.begin(),E = MBB.end(); I != E; ++I)
933       FnSize += ARM::GetInstSize(I);
934   }
935   return FnSize;
936 }