Move copyRegToReg from MRegisterInfo to TargetInstrInfo. This is part of the
[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/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineJumpTableInfo.h"
23 #include "llvm/Target/TargetAsmInfo.h"
24 #include "llvm/Support/CommandLine.h"
25 using namespace llvm;
26
27 static cl::opt<bool> EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
28                                   cl::desc("Enable ARM 2-addr to 3-addr conv"));
29
30 static inline
31 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
32   return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
33 }
34
35 static inline
36 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
37   return MIB.addReg(0);
38 }
39
40 ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI)
41   : TargetInstrInfo(ARMInsts, array_lengthof(ARMInsts)),
42     RI(*this, STI) {
43 }
44
45 const TargetRegisterClass *ARMInstrInfo::getPointerRegClass() const {
46   return &ARM::GPRRegClass;
47 }
48
49 /// Return true if the instruction is a register to register move and
50 /// leave the source and dest operands in the passed parameters.
51 ///
52 bool ARMInstrInfo::isMoveInstr(const MachineInstr &MI,
53                                unsigned &SrcReg, unsigned &DstReg) const {
54   MachineOpCode oc = MI.getOpcode();
55   switch (oc) {
56   default:
57     return false;
58   case ARM::FCPYS:
59   case ARM::FCPYD:
60     SrcReg = MI.getOperand(1).getReg();
61     DstReg = MI.getOperand(0).getReg();
62     return true;
63   case ARM::MOVr:
64   case ARM::tMOVr:
65     assert(MI.getInstrDescriptor()->numOperands >= 2 &&
66            MI.getOperand(0).isRegister() &&
67            MI.getOperand(1).isRegister() &&
68            "Invalid ARM MOV instruction");
69     SrcReg = MI.getOperand(1).getReg();
70     DstReg = MI.getOperand(0).getReg();
71     return true;
72   }
73 }
74
75 unsigned ARMInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const{
76   switch (MI->getOpcode()) {
77   default: break;
78   case ARM::LDR:
79     if (MI->getOperand(1).isFrameIndex() &&
80         MI->getOperand(2).isRegister() &&
81         MI->getOperand(3).isImmediate() && 
82         MI->getOperand(2).getReg() == 0 &&
83         MI->getOperand(3).getImm() == 0) {
84       FrameIndex = MI->getOperand(1).getIndex();
85       return MI->getOperand(0).getReg();
86     }
87     break;
88   case ARM::FLDD:
89   case ARM::FLDS:
90     if (MI->getOperand(1).isFrameIndex() &&
91         MI->getOperand(2).isImmediate() && 
92         MI->getOperand(2).getImm() == 0) {
93       FrameIndex = MI->getOperand(1).getIndex();
94       return MI->getOperand(0).getReg();
95     }
96     break;
97   case ARM::tRestore:
98     if (MI->getOperand(1).isFrameIndex() &&
99         MI->getOperand(2).isImmediate() && 
100         MI->getOperand(2).getImm() == 0) {
101       FrameIndex = MI->getOperand(1).getIndex();
102       return MI->getOperand(0).getReg();
103     }
104     break;
105   }
106   return 0;
107 }
108
109 unsigned ARMInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
110   switch (MI->getOpcode()) {
111   default: break;
112   case ARM::STR:
113     if (MI->getOperand(1).isFrameIndex() &&
114         MI->getOperand(2).isRegister() &&
115         MI->getOperand(3).isImmediate() && 
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).isFrameIndex() &&
125         MI->getOperand(2).isImmediate() && 
126         MI->getOperand(2).getImm() == 0) {
127       FrameIndex = MI->getOperand(1).getIndex();
128       return MI->getOperand(0).getReg();
129     }
130     break;
131   case ARM::tSpill:
132     if (MI->getOperand(1).isFrameIndex() &&
133         MI->getOperand(2).isImmediate() && 
134         MI->getOperand(2).getImm() == 0) {
135       FrameIndex = MI->getOperand(1).getIndex();
136       return MI->getOperand(0).getReg();
137     }
138     break;
139   }
140   return 0;
141 }
142
143 static unsigned getUnindexedOpcode(unsigned Opc) {
144   switch (Opc) {
145   default: break;
146   case ARM::LDR_PRE:
147   case ARM::LDR_POST:
148     return ARM::LDR;
149   case ARM::LDRH_PRE:
150   case ARM::LDRH_POST:
151     return ARM::LDRH;
152   case ARM::LDRB_PRE:
153   case ARM::LDRB_POST:
154     return ARM::LDRB;
155   case ARM::LDRSH_PRE:
156   case ARM::LDRSH_POST:
157     return ARM::LDRSH;
158   case ARM::LDRSB_PRE:
159   case ARM::LDRSB_POST:
160     return ARM::LDRSB;
161   case ARM::STR_PRE:
162   case ARM::STR_POST:
163     return ARM::STR;
164   case ARM::STRH_PRE:
165   case ARM::STRH_POST:
166     return ARM::STRH;
167   case ARM::STRB_PRE:
168   case ARM::STRB_POST:
169     return ARM::STRB;
170   }
171   return 0;
172 }
173
174 MachineInstr *
175 ARMInstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
176                                     MachineBasicBlock::iterator &MBBI,
177                                     LiveVariables &LV) const {
178   if (!EnableARM3Addr)
179     return NULL;
180
181   MachineInstr *MI = MBBI;
182   unsigned TSFlags = MI->getInstrDescriptor()->TSFlags;
183   bool isPre = false;
184   switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
185   default: return NULL;
186   case ARMII::IndexModePre:
187     isPre = true;
188     break;
189   case ARMII::IndexModePost:
190     break;
191   }
192
193   // Try spliting an indexed load / store to a un-indexed one plus an add/sub
194   // operation.
195   unsigned MemOpc = getUnindexedOpcode(MI->getOpcode());
196   if (MemOpc == 0)
197     return NULL;
198
199   MachineInstr *UpdateMI = NULL;
200   MachineInstr *MemMI = NULL;
201   unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
202   const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
203   unsigned NumOps = TID->numOperands;
204   bool isLoad = (TID->Flags & M_LOAD_FLAG) != 0;
205   const MachineOperand &WB = isLoad ? MI->getOperand(1) : MI->getOperand(0);
206   const MachineOperand &Base = MI->getOperand(2);
207   const MachineOperand &Offset = MI->getOperand(NumOps-3);
208   unsigned WBReg = WB.getReg();
209   unsigned BaseReg = Base.getReg();
210   unsigned OffReg = Offset.getReg();
211   unsigned OffImm = MI->getOperand(NumOps-2).getImm();
212   ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI->getOperand(NumOps-1).getImm();
213   switch (AddrMode) {
214   default:
215     assert(false && "Unknown indexed op!");
216     return NULL;
217   case ARMII::AddrMode2: {
218     bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
219     unsigned Amt = ARM_AM::getAM2Offset(OffImm);
220     if (OffReg == 0) {
221       int SOImmVal = ARM_AM::getSOImmVal(Amt);
222       if (SOImmVal == -1)
223         // Can't encode it in a so_imm operand. This transformation will
224         // add more than 1 instruction. Abandon!
225         return NULL;
226       UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
227         .addReg(BaseReg).addImm(SOImmVal)
228         .addImm(Pred).addReg(0).addReg(0);
229     } else if (Amt != 0) {
230       ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
231       unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
232       UpdateMI = BuildMI(get(isSub ? ARM::SUBrs : ARM::ADDrs), WBReg)
233         .addReg(BaseReg).addReg(OffReg).addReg(0).addImm(SOOpc)
234         .addImm(Pred).addReg(0).addReg(0);
235     } else 
236       UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
237         .addReg(BaseReg).addReg(OffReg)
238         .addImm(Pred).addReg(0).addReg(0);
239     break;
240   }
241   case ARMII::AddrMode3 : {
242     bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
243     unsigned Amt = ARM_AM::getAM3Offset(OffImm);
244     if (OffReg == 0)
245       // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
246       UpdateMI = BuildMI(get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
247         .addReg(BaseReg).addImm(Amt)
248         .addImm(Pred).addReg(0).addReg(0);
249     else
250       UpdateMI = BuildMI(get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
251         .addReg(BaseReg).addReg(OffReg)
252         .addImm(Pred).addReg(0).addReg(0);
253     break;
254   }
255   }
256
257   std::vector<MachineInstr*> NewMIs;
258   if (isPre) {
259     if (isLoad)
260       MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
261         .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
262     else
263       MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
264         .addReg(WBReg).addReg(0).addImm(0).addImm(Pred);
265     NewMIs.push_back(MemMI);
266     NewMIs.push_back(UpdateMI);
267   } else {
268     if (isLoad)
269       MemMI = BuildMI(get(MemOpc), MI->getOperand(0).getReg())
270         .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
271     else
272       MemMI = BuildMI(get(MemOpc)).addReg(MI->getOperand(1).getReg())
273         .addReg(BaseReg).addReg(0).addImm(0).addImm(Pred);
274     if (WB.isDead())
275       UpdateMI->getOperand(0).setIsDead();
276     NewMIs.push_back(UpdateMI);
277     NewMIs.push_back(MemMI);
278   }
279   
280   // Transfer LiveVariables states, kill / dead info.
281   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
282     MachineOperand &MO = MI->getOperand(i);
283     if (MO.isRegister() && MO.getReg() &&
284         MRegisterInfo::isVirtualRegister(MO.getReg())) {
285       unsigned Reg = MO.getReg();
286       LiveVariables::VarInfo &VI = LV.getVarInfo(Reg);
287       if (MO.isDef()) {
288         MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
289         if (MO.isDead())
290           LV.addVirtualRegisterDead(Reg, NewMI);
291         // Update the defining instruction.
292         if (VI.DefInst == MI)
293           VI.DefInst = NewMI;
294       }
295       if (MO.isUse() && MO.isKill()) {
296         for (unsigned j = 0; j < 2; ++j) {
297           // Look at the two new MI's in reverse order.
298           MachineInstr *NewMI = NewMIs[j];
299           int NIdx = NewMI->findRegisterUseOperandIdx(Reg);
300           if (NIdx == -1)
301             continue;
302           LV.addVirtualRegisterKilled(Reg, NewMI);
303           if (VI.removeKill(MI))
304             VI.Kills.push_back(NewMI);
305           break;
306         }
307       }
308     }
309   }
310
311   MFI->insert(MBBI, NewMIs[1]);
312   MFI->insert(MBBI, NewMIs[0]);
313   return NewMIs[0];
314 }
315
316 // Branch analysis.
317 bool ARMInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
318                                  MachineBasicBlock *&FBB,
319                                  std::vector<MachineOperand> &Cond) const {
320   // If the block has no terminators, it just falls into the block after it.
321   MachineBasicBlock::iterator I = MBB.end();
322   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
323     return false;
324   
325   // Get the last instruction in the block.
326   MachineInstr *LastInst = I;
327   
328   // If there is only one terminator instruction, process it.
329   unsigned LastOpc = LastInst->getOpcode();
330   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
331     if (LastOpc == ARM::B || LastOpc == ARM::tB) {
332       TBB = LastInst->getOperand(0).getMBB();
333       return false;
334     }
335     if (LastOpc == ARM::Bcc || LastOpc == ARM::tBcc) {
336       // Block ends with fall-through condbranch.
337       TBB = LastInst->getOperand(0).getMBB();
338       Cond.push_back(LastInst->getOperand(1));
339       Cond.push_back(LastInst->getOperand(2));
340       return false;
341     }
342     return true;  // Can't handle indirect branch.
343   }
344   
345   // Get the instruction before it if it is a terminator.
346   MachineInstr *SecondLastInst = I;
347   
348   // If there are three terminators, we don't know what sort of block this is.
349   if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
350     return true;
351   
352   // If the block ends with ARM::B/ARM::tB and a ARM::Bcc/ARM::tBcc, handle it.
353   unsigned SecondLastOpc = SecondLastInst->getOpcode();
354   if ((SecondLastOpc == ARM::Bcc && LastOpc == ARM::B) ||
355       (SecondLastOpc == ARM::tBcc && LastOpc == ARM::tB)) {
356     TBB =  SecondLastInst->getOperand(0).getMBB();
357     Cond.push_back(SecondLastInst->getOperand(1));
358     Cond.push_back(SecondLastInst->getOperand(2));
359     FBB = LastInst->getOperand(0).getMBB();
360     return false;
361   }
362   
363   // If the block ends with two unconditional branches, handle it.  The second 
364   // one is not executed, so remove it.
365   if ((SecondLastOpc == ARM::B || SecondLastOpc==ARM::tB) &&
366       (LastOpc == ARM::B || LastOpc == ARM::tB)) {
367     TBB = SecondLastInst->getOperand(0).getMBB();
368     I = LastInst;
369     I->eraseFromParent();
370     return false;
371   }
372
373   // Likewise if it ends with a branch table followed by an unconditional branch.
374   // The branch folder can create these, and we must get rid of them for
375   // correctness of Thumb constant islands.
376   if ((SecondLastOpc == ARM::BR_JTr || SecondLastOpc==ARM::BR_JTm ||
377        SecondLastOpc == ARM::BR_JTadd || SecondLastOpc==ARM::tBR_JTr) &&
378       (LastOpc == ARM::B || LastOpc == ARM::tB)) {
379     I = LastInst;
380     I->eraseFromParent();
381     return true;
382   } 
383
384   // Otherwise, can't handle this.
385   return true;
386 }
387
388
389 unsigned ARMInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
390   MachineFunction &MF = *MBB.getParent();
391   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
392   int BOpc   = AFI->isThumbFunction() ? ARM::tB : ARM::B;
393   int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
394
395   MachineBasicBlock::iterator I = MBB.end();
396   if (I == MBB.begin()) return 0;
397   --I;
398   if (I->getOpcode() != BOpc && I->getOpcode() != BccOpc)
399     return 0;
400   
401   // Remove the branch.
402   I->eraseFromParent();
403   
404   I = MBB.end();
405   
406   if (I == MBB.begin()) return 1;
407   --I;
408   if (I->getOpcode() != BccOpc)
409     return 1;
410   
411   // Remove the branch.
412   I->eraseFromParent();
413   return 2;
414 }
415
416 unsigned ARMInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
417                                 MachineBasicBlock *FBB,
418                                 const std::vector<MachineOperand> &Cond) const {
419   MachineFunction &MF = *MBB.getParent();
420   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
421   int BOpc   = AFI->isThumbFunction() ? ARM::tB : ARM::B;
422   int BccOpc = AFI->isThumbFunction() ? ARM::tBcc : ARM::Bcc;
423
424   // Shouldn't be a fall through.
425   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
426   assert((Cond.size() == 2 || Cond.size() == 0) &&
427          "ARM branch conditions have two components!");
428   
429   if (FBB == 0) {
430     if (Cond.empty()) // Unconditional branch?
431       BuildMI(&MBB, get(BOpc)).addMBB(TBB);
432     else
433       BuildMI(&MBB, get(BccOpc)).addMBB(TBB)
434         .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
435     return 1;
436   }
437   
438   // Two-way conditional branch.
439   BuildMI(&MBB, get(BccOpc)).addMBB(TBB)
440     .addImm(Cond[0].getImm()).addReg(Cond[1].getReg());
441   BuildMI(&MBB, get(BOpc)).addMBB(FBB);
442   return 2;
443 }
444
445 void ARMInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
446                                    MachineBasicBlock::iterator I,
447                                    unsigned DestReg, unsigned SrcReg,
448                                    const TargetRegisterClass *DestRC,
449                                    const TargetRegisterClass *SrcRC) const {
450   if (DestRC != SrcRC) {
451     cerr << "Not yet supported!";
452     abort();
453   }
454
455   if (DestRC == ARM::GPRRegisterClass) {
456     MachineFunction &MF = *MBB.getParent();
457     ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
458     if (AFI->isThumbFunction())
459       BuildMI(MBB, I, get(ARM::tMOVr), DestReg).addReg(SrcReg);
460     else
461       AddDefaultCC(AddDefaultPred(BuildMI(MBB, I, get(ARM::MOVr), DestReg)
462                                   .addReg(SrcReg)));
463   } else if (DestRC == ARM::SPRRegisterClass)
464     AddDefaultPred(BuildMI(MBB, I, get(ARM::FCPYS), DestReg)
465                    .addReg(SrcReg));
466   else if (DestRC == ARM::DPRRegisterClass)
467     AddDefaultPred(BuildMI(MBB, I, get(ARM::FCPYD), DestReg)
468                    .addReg(SrcReg));
469   else
470     abort();
471 }
472
473 bool ARMInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
474   if (MBB.empty()) return false;
475   
476   switch (MBB.back().getOpcode()) {
477   case ARM::BX_RET:   // Return.
478   case ARM::LDM_RET:
479   case ARM::tBX_RET:
480   case ARM::tBX_RET_vararg:
481   case ARM::tPOP_RET:
482   case ARM::B:
483   case ARM::tB:       // Uncond branch.
484   case ARM::tBR_JTr:
485   case ARM::BR_JTr:   // Jumptable branch.
486   case ARM::BR_JTm:   // Jumptable branch through mem.
487   case ARM::BR_JTadd: // Jumptable branch add to pc.
488     return true;
489   default: return false;
490   }
491 }
492
493 bool ARMInstrInfo::
494 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
495   ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
496   Cond[0].setImm(ARMCC::getOppositeCondition(CC));
497   return false;
498 }
499
500 bool ARMInstrInfo::isPredicated(const MachineInstr *MI) const {
501   int PIdx = MI->findFirstPredOperandIdx();
502   return PIdx != -1 && MI->getOperand(PIdx).getImm() != ARMCC::AL;
503 }
504
505 bool ARMInstrInfo::PredicateInstruction(MachineInstr *MI,
506                                 const std::vector<MachineOperand> &Pred) const {
507   unsigned Opc = MI->getOpcode();
508   if (Opc == ARM::B || Opc == ARM::tB) {
509     MI->setInstrDescriptor(get(Opc == ARM::B ? ARM::Bcc : ARM::tBcc));
510     MI->addOperand(MachineOperand::CreateImm(Pred[0].getImm()));
511     MI->addOperand(MachineOperand::CreateReg(Pred[1].getReg(), false));
512     return true;
513   }
514
515   int PIdx = MI->findFirstPredOperandIdx();
516   if (PIdx != -1) {
517     MachineOperand &PMO = MI->getOperand(PIdx);
518     PMO.setImm(Pred[0].getImm());
519     MI->getOperand(PIdx+1).setReg(Pred[1].getReg());
520     return true;
521   }
522   return false;
523 }
524
525 bool
526 ARMInstrInfo::SubsumesPredicate(const std::vector<MachineOperand> &Pred1,
527                                 const std::vector<MachineOperand> &Pred2) const{
528   if (Pred1.size() > 2 || Pred2.size() > 2)
529     return false;
530
531   ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
532   ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
533   if (CC1 == CC2)
534     return true;
535
536   switch (CC1) {
537   default:
538     return false;
539   case ARMCC::AL:
540     return true;
541   case ARMCC::HS:
542     return CC2 == ARMCC::HI;
543   case ARMCC::LS:
544     return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
545   case ARMCC::GE:
546     return CC2 == ARMCC::GT;
547   case ARMCC::LE:
548     return CC2 == ARMCC::LT;
549   }
550 }
551
552 bool ARMInstrInfo::DefinesPredicate(MachineInstr *MI,
553                                     std::vector<MachineOperand> &Pred) const {
554   const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
555   if (!TID->ImplicitDefs && (TID->Flags & M_HAS_OPTIONAL_DEF) == 0)
556     return false;
557
558   bool Found = false;
559   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
560     const MachineOperand &MO = MI->getOperand(i);
561     if (MO.isRegister() && MO.getReg() == ARM::CPSR) {
562       Pred.push_back(MO);
563       Found = true;
564     }
565   }
566
567   return Found;
568 }
569
570
571 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing
572 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
573                                 unsigned JTI) DISABLE_INLINE;
574 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
575                                 unsigned JTI) {
576   return JT[JTI].MBBs.size();
577 }
578
579 /// GetInstSize - Return the size of the specified MachineInstr.
580 ///
581 unsigned ARM::GetInstSize(MachineInstr *MI) {
582   MachineBasicBlock &MBB = *MI->getParent();
583   const MachineFunction *MF = MBB.getParent();
584   const TargetAsmInfo *TAI = MF->getTarget().getTargetAsmInfo();
585
586   // Basic size info comes from the TSFlags field.
587   const TargetInstrDescriptor *TID = MI->getInstrDescriptor();
588   unsigned TSFlags = TID->TSFlags;
589   
590   switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
591   default:
592     // If this machine instr is an inline asm, measure it.
593     if (MI->getOpcode() == ARM::INLINEASM)
594       return TAI->getInlineAsmLength(MI->getOperand(0).getSymbolName());
595     if (MI->getOpcode() == ARM::LABEL)
596       return 0;
597     assert(0 && "Unknown or unset size field for instr!");
598     break;
599   case ARMII::Size8Bytes: return 8;          // Arm instruction x 2.
600   case ARMII::Size4Bytes: return 4;          // Arm instruction.
601   case ARMII::Size2Bytes: return 2;          // Thumb instruction.
602   case ARMII::SizeSpecial: {
603     switch (MI->getOpcode()) {
604     case ARM::CONSTPOOL_ENTRY:
605       // If this machine instr is a constant pool entry, its size is recorded as
606       // operand #2.
607       return MI->getOperand(2).getImm();
608     case ARM::BR_JTr:
609     case ARM::BR_JTm:
610     case ARM::BR_JTadd:
611     case ARM::tBR_JTr: {
612       // These are jumptable branches, i.e. a branch followed by an inlined
613       // jumptable. The size is 4 + 4 * number of entries.
614       unsigned NumOps = TID->numOperands;
615       MachineOperand JTOP =
616         MI->getOperand(NumOps - ((TID->Flags & M_PREDICABLE) ? 3 : 2));
617       unsigned JTI = JTOP.getIndex();
618       MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
619       const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
620       assert(JTI < JT.size());
621       // Thumb instructions are 2 byte aligned, but JT entries are 4 byte
622       // 4 aligned. The assembler / linker may add 2 byte padding just before
623       // the JT entries.  The size does not include this padding; the
624       // constant islands pass does separate bookkeeping for it.
625       // FIXME: If we know the size of the function is less than (1 << 16) *2
626       // bytes, we can use 16-bit entries instead. Then there won't be an
627       // alignment issue.
628       return getNumJTEntries(JT, JTI) * 4 + 
629              (MI->getOpcode()==ARM::tBR_JTr ? 2 : 4);
630     }
631     default:
632       // Otherwise, pseudo-instruction sizes are zero.
633       return 0;
634     }
635   }
636   }
637 }
638
639 /// GetFunctionSize - Returns the size of the specified MachineFunction.
640 ///
641 unsigned ARM::GetFunctionSize(MachineFunction &MF) {
642   unsigned FnSize = 0;
643   for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
644        MBBI != E; ++MBBI) {
645     MachineBasicBlock &MBB = *MBBI;
646     for (MachineBasicBlock::iterator I = MBB.begin(),E = MBB.end(); I != E; ++I)
647       FnSize += ARM::GetInstSize(I);
648   }
649   return FnSize;
650 }