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