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