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