31439b4a267cbbd5b674ab0295daa79bf31b24eb
[oota-llvm.git] / lib / Target / ARM / ARMBaseRegisterInfo.cpp
1 //===- ARMBaseRegisterInfo.cpp - ARM Register 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 base ARM implementation of TargetRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARM.h"
15 #include "ARMAddressingModes.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMBaseRegisterInfo.h"
18 #include "ARMInstrInfo.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMSubtarget.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineLocation.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/RegisterScavenging.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetFrameInfo.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetOptions.h"
38 #include "llvm/ADT/BitVector.h"
39 #include "llvm/ADT/SmallVector.h"
40 #include "llvm/Support/CommandLine.h"
41
42 namespace llvm {
43 cl::opt<bool>
44 ReuseFrameIndexVals("arm-reuse-frame-index-vals", cl::Hidden, cl::init(true),
45           cl::desc("Reuse repeated frame index values"));
46 }
47
48 using namespace llvm;
49
50 unsigned ARMBaseRegisterInfo::getRegisterNumbering(unsigned RegEnum,
51                                                    bool *isSPVFP) {
52   if (isSPVFP)
53     *isSPVFP = false;
54
55   using namespace ARM;
56   switch (RegEnum) {
57   default:
58     llvm_unreachable("Unknown ARM register!");
59   case R0:  case D0:  case Q0:  return 0;
60   case R1:  case D1:  case Q1:  return 1;
61   case R2:  case D2:  case Q2:  return 2;
62   case R3:  case D3:  case Q3:  return 3;
63   case R4:  case D4:  case Q4:  return 4;
64   case R5:  case D5:  case Q5:  return 5;
65   case R6:  case D6:  case Q6:  return 6;
66   case R7:  case D7:  case Q7:  return 7;
67   case R8:  case D8:  case Q8:  return 8;
68   case R9:  case D9:  case Q9:  return 9;
69   case R10: case D10: case Q10: return 10;
70   case R11: case D11: case Q11: return 11;
71   case R12: case D12: case Q12: return 12;
72   case SP:  case D13: case Q13: return 13;
73   case LR:  case D14: case Q14: return 14;
74   case PC:  case D15: case Q15: return 15;
75
76   case D16: return 16;
77   case D17: return 17;
78   case D18: return 18;
79   case D19: return 19;
80   case D20: return 20;
81   case D21: return 21;
82   case D22: return 22;
83   case D23: return 23;
84   case D24: return 24;
85   case D25: return 25;
86   case D26: return 26;
87   case D27: return 27;
88   case D28: return 28;
89   case D29: return 29;
90   case D30: return 30;
91   case D31: return 31;
92
93   case S0: case S1: case S2: case S3:
94   case S4: case S5: case S6: case S7:
95   case S8: case S9: case S10: case S11:
96   case S12: case S13: case S14: case S15:
97   case S16: case S17: case S18: case S19:
98   case S20: case S21: case S22: case S23:
99   case S24: case S25: case S26: case S27:
100   case S28: case S29: case S30: case S31: {
101     if (isSPVFP)
102       *isSPVFP = true;
103     switch (RegEnum) {
104     default: return 0; // Avoid compile time warning.
105     case S0: return 0;
106     case S1: return 1;
107     case S2: return 2;
108     case S3: return 3;
109     case S4: return 4;
110     case S5: return 5;
111     case S6: return 6;
112     case S7: return 7;
113     case S8: return 8;
114     case S9: return 9;
115     case S10: return 10;
116     case S11: return 11;
117     case S12: return 12;
118     case S13: return 13;
119     case S14: return 14;
120     case S15: return 15;
121     case S16: return 16;
122     case S17: return 17;
123     case S18: return 18;
124     case S19: return 19;
125     case S20: return 20;
126     case S21: return 21;
127     case S22: return 22;
128     case S23: return 23;
129     case S24: return 24;
130     case S25: return 25;
131     case S26: return 26;
132     case S27: return 27;
133     case S28: return 28;
134     case S29: return 29;
135     case S30: return 30;
136     case S31: return 31;
137     }
138   }
139   }
140 }
141
142 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
143                                          const ARMSubtarget &sti)
144   : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
145     TII(tii), STI(sti),
146     FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11) {
147 }
148
149 const unsigned*
150 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
151   static const unsigned CalleeSavedRegs[] = {
152     ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
153     ARM::R7, ARM::R6,  ARM::R5,  ARM::R4,
154
155     ARM::D15, ARM::D14, ARM::D13, ARM::D12,
156     ARM::D11, ARM::D10, ARM::D9,  ARM::D8,
157     0
158   };
159
160   static const unsigned DarwinCalleeSavedRegs[] = {
161     // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
162     // register.
163     ARM::LR,  ARM::R7,  ARM::R6, ARM::R5, ARM::R4,
164     ARM::R11, ARM::R10, ARM::R8,
165
166     ARM::D15, ARM::D14, ARM::D13, ARM::D12,
167     ARM::D11, ARM::D10, ARM::D9,  ARM::D8,
168     0
169   };
170   return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
171 }
172
173 BitVector ARMBaseRegisterInfo::
174 getReservedRegs(const MachineFunction &MF) const {
175   // FIXME: avoid re-calculating this everytime.
176   BitVector Reserved(getNumRegs());
177   Reserved.set(ARM::SP);
178   Reserved.set(ARM::PC);
179   if (STI.isTargetDarwin() || hasFP(MF))
180     Reserved.set(FramePtr);
181   // Some targets reserve R9.
182   if (STI.isR9Reserved())
183     Reserved.set(ARM::R9);
184   return Reserved;
185 }
186
187 bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
188                                         unsigned Reg) const {
189   switch (Reg) {
190   default: break;
191   case ARM::SP:
192   case ARM::PC:
193     return true;
194   case ARM::R7:
195   case ARM::R11:
196     if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
197       return true;
198     break;
199   case ARM::R9:
200     return STI.isR9Reserved();
201   }
202
203   return false;
204 }
205
206 const TargetRegisterClass *
207 ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
208                                               const TargetRegisterClass *B,
209                                               unsigned SubIdx) const {
210   switch (SubIdx) {
211   default: return 0;
212   case ARM::ssub_0:
213   case ARM::ssub_1:
214   case ARM::ssub_2:
215   case ARM::ssub_3: {
216     // S sub-registers.
217     if (A->getSize() == 8) {
218       if (B == &ARM::SPR_8RegClass)
219         return &ARM::DPR_8RegClass;
220       assert(B == &ARM::SPRRegClass && "Expecting SPR register class!");
221       if (A == &ARM::DPR_8RegClass)
222         return A;
223       return &ARM::DPR_VFP2RegClass;
224     }
225
226     if (A->getSize() == 16) {
227       if (B == &ARM::SPR_8RegClass)
228         return &ARM::QPR_8RegClass;
229       return &ARM::QPR_VFP2RegClass;
230     }
231
232     if (A->getSize() == 32) {
233       if (B == &ARM::SPR_8RegClass)
234         return 0;  // Do not allow coalescing!
235       return &ARM::QQPR_VFP2RegClass;
236     }
237
238     assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
239     return 0;  // Do not allow coalescing!
240   }
241   case ARM::dsub_0:
242   case ARM::dsub_1:
243   case ARM::dsub_2:
244   case ARM::dsub_3: {
245     // D sub-registers.
246     if (A->getSize() == 16) {
247       if (B == &ARM::DPR_VFP2RegClass)
248         return &ARM::QPR_VFP2RegClass;
249       if (B == &ARM::DPR_8RegClass)
250         return 0;  // Do not allow coalescing!
251       return A;
252     }
253
254     if (A->getSize() == 32) {
255       if (B == &ARM::DPR_VFP2RegClass)
256         return &ARM::QQPR_VFP2RegClass;
257       if (B == &ARM::DPR_8RegClass)
258         return 0;  // Do not allow coalescing!
259       return A;
260     }
261
262     assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
263     if (B != &ARM::DPRRegClass)
264       return 0;  // Do not allow coalescing!
265     return A;
266   }
267   case ARM::dsub_4:
268   case ARM::dsub_5:
269   case ARM::dsub_6:
270   case ARM::dsub_7: {
271     // D sub-registers of QQQQ registers.
272     if (A->getSize() == 64 && B == &ARM::DPRRegClass)
273       return A;
274     return 0;  // Do not allow coalescing!
275   }
276
277   case ARM::qsub_0:
278   case ARM::qsub_1: {
279     // Q sub-registers.
280     if (A->getSize() == 32) {
281       if (B == &ARM::QPR_VFP2RegClass)
282         return &ARM::QQPR_VFP2RegClass;
283       if (B == &ARM::QPR_8RegClass)
284         return 0;  // Do not allow coalescing!
285       return A;
286     }
287
288     assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
289     if (B == &ARM::QPRRegClass)
290       return A;
291     return 0;  // Do not allow coalescing!
292   }
293   case ARM::qsub_2:
294   case ARM::qsub_3: {
295     // Q sub-registers of QQQQ registers.
296     if (A->getSize() == 64 && B == &ARM::QPRRegClass)
297       return A;
298     return 0;  // Do not allow coalescing!
299   }
300   }
301   return 0;
302 }
303
304 bool
305 ARMBaseRegisterInfo::canCombineSubRegIndices(const TargetRegisterClass *RC,
306                                           SmallVectorImpl<unsigned> &SubIndices,
307                                           unsigned &NewSubIdx) const {
308
309   unsigned Size = RC->getSize() * 8;
310   if (Size < 6)
311     return 0;
312
313   NewSubIdx = 0;  // Whole register.
314   unsigned NumRegs = SubIndices.size();
315   if (NumRegs == 8) {
316     // 8 D registers -> 1 QQQQ register.
317     return (Size == 512 &&
318             SubIndices[0] == ARM::dsub_0 &&
319             SubIndices[1] == ARM::dsub_1 &&
320             SubIndices[2] == ARM::dsub_2 &&
321             SubIndices[3] == ARM::dsub_3 &&
322             SubIndices[4] == ARM::dsub_4 &&
323             SubIndices[5] == ARM::dsub_5 &&
324             SubIndices[6] == ARM::dsub_6 &&
325             SubIndices[7] == ARM::dsub_7);
326   } else if (NumRegs == 4) {
327     if (SubIndices[0] == ARM::qsub_0) {
328       // 4 Q registers -> 1 QQQQ register.
329       return (Size == 512 &&
330               SubIndices[1] == ARM::qsub_1 &&
331               SubIndices[2] == ARM::qsub_2 &&
332               SubIndices[3] == ARM::qsub_3);
333     } else if (SubIndices[0] == ARM::dsub_0) {
334       // 4 D registers -> 1 QQ register.
335       if (Size >= 256 &&
336           SubIndices[1] == ARM::dsub_1 &&
337           SubIndices[2] == ARM::dsub_2 &&
338           SubIndices[3] == ARM::dsub_3) {
339         if (Size == 512)
340           NewSubIdx = ARM::qqsub_0;
341         return true;
342       }
343     } else if (SubIndices[0] == ARM::dsub_4) {
344       // 4 D registers -> 1 QQ register (2nd).
345       if (Size == 512 &&
346           SubIndices[1] == ARM::dsub_5 &&
347           SubIndices[2] == ARM::dsub_6 &&
348           SubIndices[3] == ARM::dsub_7) {
349         NewSubIdx = ARM::qqsub_1;
350         return true;
351       }
352     } else if (SubIndices[0] == ARM::ssub_0) {
353       // 4 S registers -> 1 Q register.
354       if (Size >= 128 &&
355           SubIndices[1] == ARM::ssub_1 &&
356           SubIndices[2] == ARM::ssub_2 &&
357           SubIndices[3] == ARM::ssub_3) {
358         if (Size >= 256)
359           NewSubIdx = ARM::qsub_0;
360         return true;
361       }
362     }
363   } else if (NumRegs == 2) {
364     if (SubIndices[0] == ARM::qsub_0) {
365       // 2 Q registers -> 1 QQ register.
366       if (Size >= 256 && SubIndices[1] == ARM::qsub_1) {
367         if (Size == 512)
368           NewSubIdx = ARM::qqsub_0;
369         return true;
370       }
371     } else if (SubIndices[0] == ARM::qsub_2) {
372       // 2 Q registers -> 1 QQ register (2nd).
373       if (Size == 512 && SubIndices[1] == ARM::qsub_3) {
374         NewSubIdx = ARM::qqsub_1;
375         return true;
376       }
377     } else if (SubIndices[0] == ARM::dsub_0) {
378       // 2 D registers -> 1 Q register.
379       if (Size >= 128 && SubIndices[1] == ARM::dsub_1) {
380         if (Size >= 256)
381           NewSubIdx = ARM::qsub_0;
382         return true;
383       }
384     } else if (SubIndices[0] == ARM::dsub_2) {
385       // 2 D registers -> 1 Q register (2nd).
386       if (Size >= 256 && SubIndices[1] == ARM::dsub_3) {
387         NewSubIdx = ARM::qsub_1;
388         return true;
389       }
390     } else if (SubIndices[0] == ARM::dsub_4) {
391       // 2 D registers -> 1 Q register (3rd).
392       if (Size == 512 && SubIndices[1] == ARM::dsub_5) {
393         NewSubIdx = ARM::qsub_2;
394         return true;
395       }
396     } else if (SubIndices[0] == ARM::dsub_6) {
397       // 2 D registers -> 1 Q register (3rd).
398       if (Size == 512 && SubIndices[1] == ARM::dsub_7) {
399         NewSubIdx = ARM::qsub_3;
400         return true;
401       }
402     } else if (SubIndices[0] == ARM::ssub_0) {
403       // 2 S registers -> 1 D register.
404       if (SubIndices[1] == ARM::ssub_1) {
405         if (Size >= 128)
406           NewSubIdx = ARM::dsub_0;
407         return true;
408       }
409     } else if (SubIndices[0] == ARM::ssub_2) {
410       // 2 S registers -> 1 D register (2nd).
411       if (Size >= 128 && SubIndices[1] == ARM::ssub_3) {
412         NewSubIdx = ARM::dsub_1;
413         return true;
414       }
415     }
416   }
417   return false;
418 }
419
420
421 const TargetRegisterClass *
422 ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
423   return ARM::GPRRegisterClass;
424 }
425
426 /// getAllocationOrder - Returns the register allocation order for a specified
427 /// register class in the form of a pair of TargetRegisterClass iterators.
428 std::pair<TargetRegisterClass::iterator,TargetRegisterClass::iterator>
429 ARMBaseRegisterInfo::getAllocationOrder(const TargetRegisterClass *RC,
430                                         unsigned HintType, unsigned HintReg,
431                                         const MachineFunction &MF) const {
432   // Alternative register allocation orders when favoring even / odd registers
433   // of register pairs.
434
435   // No FP, R9 is available.
436   static const unsigned GPREven1[] = {
437     ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
438     ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
439     ARM::R9, ARM::R11
440   };
441   static const unsigned GPROdd1[] = {
442     ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
443     ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
444     ARM::R8, ARM::R10
445   };
446
447   // FP is R7, R9 is available.
448   static const unsigned GPREven2[] = {
449     ARM::R0, ARM::R2, ARM::R4,          ARM::R8, ARM::R10,
450     ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
451     ARM::R9, ARM::R11
452   };
453   static const unsigned GPROdd2[] = {
454     ARM::R1, ARM::R3, ARM::R5,          ARM::R9, ARM::R11,
455     ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
456     ARM::R8, ARM::R10
457   };
458
459   // FP is R11, R9 is available.
460   static const unsigned GPREven3[] = {
461     ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
462     ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
463     ARM::R9
464   };
465   static const unsigned GPROdd3[] = {
466     ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
467     ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
468     ARM::R8
469   };
470
471   // No FP, R9 is not available.
472   static const unsigned GPREven4[] = {
473     ARM::R0, ARM::R2, ARM::R4, ARM::R6,          ARM::R10,
474     ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
475     ARM::R11
476   };
477   static const unsigned GPROdd4[] = {
478     ARM::R1, ARM::R3, ARM::R5, ARM::R7,          ARM::R11,
479     ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
480     ARM::R10
481   };
482
483   // FP is R7, R9 is not available.
484   static const unsigned GPREven5[] = {
485     ARM::R0, ARM::R2, ARM::R4,                   ARM::R10,
486     ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
487     ARM::R11
488   };
489   static const unsigned GPROdd5[] = {
490     ARM::R1, ARM::R3, ARM::R5,                   ARM::R11,
491     ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
492     ARM::R10
493   };
494
495   // FP is R11, R9 is not available.
496   static const unsigned GPREven6[] = {
497     ARM::R0, ARM::R2, ARM::R4, ARM::R6,
498     ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
499   };
500   static const unsigned GPROdd6[] = {
501     ARM::R1, ARM::R3, ARM::R5, ARM::R7,
502     ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
503   };
504
505
506   if (HintType == ARMRI::RegPairEven) {
507     if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
508       // It's no longer possible to fulfill this hint. Return the default
509       // allocation order.
510       return std::make_pair(RC->allocation_order_begin(MF),
511                             RC->allocation_order_end(MF));
512
513     if (!STI.isTargetDarwin() && !hasFP(MF)) {
514       if (!STI.isR9Reserved())
515         return std::make_pair(GPREven1,
516                               GPREven1 + (sizeof(GPREven1)/sizeof(unsigned)));
517       else
518         return std::make_pair(GPREven4,
519                               GPREven4 + (sizeof(GPREven4)/sizeof(unsigned)));
520     } else if (FramePtr == ARM::R7) {
521       if (!STI.isR9Reserved())
522         return std::make_pair(GPREven2,
523                               GPREven2 + (sizeof(GPREven2)/sizeof(unsigned)));
524       else
525         return std::make_pair(GPREven5,
526                               GPREven5 + (sizeof(GPREven5)/sizeof(unsigned)));
527     } else { // FramePtr == ARM::R11
528       if (!STI.isR9Reserved())
529         return std::make_pair(GPREven3,
530                               GPREven3 + (sizeof(GPREven3)/sizeof(unsigned)));
531       else
532         return std::make_pair(GPREven6,
533                               GPREven6 + (sizeof(GPREven6)/sizeof(unsigned)));
534     }
535   } else if (HintType == ARMRI::RegPairOdd) {
536     if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
537       // It's no longer possible to fulfill this hint. Return the default
538       // allocation order.
539       return std::make_pair(RC->allocation_order_begin(MF),
540                             RC->allocation_order_end(MF));
541
542     if (!STI.isTargetDarwin() && !hasFP(MF)) {
543       if (!STI.isR9Reserved())
544         return std::make_pair(GPROdd1,
545                               GPROdd1 + (sizeof(GPROdd1)/sizeof(unsigned)));
546       else
547         return std::make_pair(GPROdd4,
548                               GPROdd4 + (sizeof(GPROdd4)/sizeof(unsigned)));
549     } else if (FramePtr == ARM::R7) {
550       if (!STI.isR9Reserved())
551         return std::make_pair(GPROdd2,
552                               GPROdd2 + (sizeof(GPROdd2)/sizeof(unsigned)));
553       else
554         return std::make_pair(GPROdd5,
555                               GPROdd5 + (sizeof(GPROdd5)/sizeof(unsigned)));
556     } else { // FramePtr == ARM::R11
557       if (!STI.isR9Reserved())
558         return std::make_pair(GPROdd3,
559                               GPROdd3 + (sizeof(GPROdd3)/sizeof(unsigned)));
560       else
561         return std::make_pair(GPROdd6,
562                               GPROdd6 + (sizeof(GPROdd6)/sizeof(unsigned)));
563     }
564   }
565   return std::make_pair(RC->allocation_order_begin(MF),
566                         RC->allocation_order_end(MF));
567 }
568
569 /// ResolveRegAllocHint - Resolves the specified register allocation hint
570 /// to a physical register. Returns the physical register if it is successful.
571 unsigned
572 ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
573                                          const MachineFunction &MF) const {
574   if (Reg == 0 || !isPhysicalRegister(Reg))
575     return 0;
576   if (Type == 0)
577     return Reg;
578   else if (Type == (unsigned)ARMRI::RegPairOdd)
579     // Odd register.
580     return getRegisterPairOdd(Reg, MF);
581   else if (Type == (unsigned)ARMRI::RegPairEven)
582     // Even register.
583     return getRegisterPairEven(Reg, MF);
584   return 0;
585 }
586
587 void
588 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
589                                         MachineFunction &MF) const {
590   MachineRegisterInfo *MRI = &MF.getRegInfo();
591   std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
592   if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
593        Hint.first == (unsigned)ARMRI::RegPairEven) &&
594       Hint.second && TargetRegisterInfo::isVirtualRegister(Hint.second)) {
595     // If 'Reg' is one of the even / odd register pair and it's now changed
596     // (e.g. coalesced) into a different register. The other register of the
597     // pair allocation hint must be updated to reflect the relationship
598     // change.
599     unsigned OtherReg = Hint.second;
600     Hint = MRI->getRegAllocationHint(OtherReg);
601     if (Hint.second == Reg)
602       // Make sure the pair has not already divorced.
603       MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
604   }
605 }
606
607 /// hasFP - Return true if the specified function should have a dedicated frame
608 /// pointer register.  This is true if the function has variable sized allocas
609 /// or if frame pointer elimination is disabled.
610 ///
611 bool ARMBaseRegisterInfo::hasFP(const MachineFunction &MF) const {
612   const MachineFrameInfo *MFI = MF.getFrameInfo();
613   return ((DisableFramePointerElim(MF) && MFI->adjustsStack())||
614           needsStackRealignment(MF) ||
615           MFI->hasVarSizedObjects() ||
616           MFI->isFrameAddressTaken());
617 }
618
619 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
620   const MachineFrameInfo *MFI = MF.getFrameInfo();
621   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
622   return (RealignStack &&
623           !AFI->isThumb1OnlyFunction() &&
624           !MFI->hasVarSizedObjects());
625 }
626
627 bool ARMBaseRegisterInfo::
628 needsStackRealignment(const MachineFunction &MF) const {
629   const MachineFrameInfo *MFI = MF.getFrameInfo();
630   const Function *F = MF.getFunction();
631   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
632   unsigned StackAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
633   bool requiresRealignment = ((MFI->getMaxAlignment() > StackAlign) ||
634                                F->hasFnAttr(Attribute::StackAlignment));
635     
636   // FIXME: Currently we don't support stack realignment for functions with
637   //        variable-sized allocas.
638   // FIXME: It's more complicated than this...
639   if (0 && requiresRealignment && MFI->hasVarSizedObjects())
640     report_fatal_error(
641       "Stack realignment in presense of dynamic allocas is not supported");
642   
643   // FIXME: This probably isn't the right place for this.
644   if (0 && requiresRealignment && AFI->isThumb1OnlyFunction())
645     report_fatal_error(
646       "Stack realignment in thumb1 functions is not supported");
647   
648   return requiresRealignment && canRealignStack(MF);
649 }
650
651 bool ARMBaseRegisterInfo::
652 cannotEliminateFrame(const MachineFunction &MF) const {
653   const MachineFrameInfo *MFI = MF.getFrameInfo();
654   if (DisableFramePointerElim(MF) && MFI->adjustsStack())
655     return true;
656   return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
657     || needsStackRealignment(MF);
658 }
659
660 /// estimateStackSize - Estimate and return the size of the frame.
661 static unsigned estimateStackSize(MachineFunction &MF) {
662   const MachineFrameInfo *FFI = MF.getFrameInfo();
663   int Offset = 0;
664   for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
665     int FixedOff = -FFI->getObjectOffset(i);
666     if (FixedOff > Offset) Offset = FixedOff;
667   }
668   for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
669     if (FFI->isDeadObjectIndex(i))
670       continue;
671     Offset += FFI->getObjectSize(i);
672     unsigned Align = FFI->getObjectAlignment(i);
673     // Adjust to alignment boundary
674     Offset = (Offset+Align-1)/Align*Align;
675   }
676   return (unsigned)Offset;
677 }
678
679 /// estimateRSStackSizeLimit - Look at each instruction that references stack
680 /// frames and return the stack size limit beyond which some of these
681 /// instructions will require a scratch register during their expansion later.
682 unsigned
683 ARMBaseRegisterInfo::estimateRSStackSizeLimit(MachineFunction &MF) const {
684   unsigned Limit = (1 << 12) - 1;
685   for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
686     for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
687          I != E; ++I) {
688       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
689         if (!I->getOperand(i).isFI()) continue;
690
691         // When using ADDri to get the address of a stack object, 255 is the
692         // largest offset guaranteed to fit in the immediate offset.
693         if (I->getOpcode() == ARM::ADDri) {
694           Limit = std::min(Limit, (1U << 8) - 1);
695           break;
696         }
697
698         // Otherwise check the addressing mode.
699         switch (I->getDesc().TSFlags & ARMII::AddrModeMask) {
700         case ARMII::AddrMode3:
701         case ARMII::AddrModeT2_i8:
702           Limit = std::min(Limit, (1U << 8) - 1);
703           break;
704         case ARMII::AddrMode5:
705         case ARMII::AddrModeT2_i8s4:
706           Limit = std::min(Limit, ((1U << 8) - 1) * 4);
707           break;
708         case ARMII::AddrModeT2_i12:
709           if (hasFP(MF)) Limit = std::min(Limit, (1U << 8) - 1);
710           break;
711         case ARMII::AddrMode6:
712           // Addressing mode 6 (load/store) instructions can't encode an
713           // immediate offset for stack references.
714           return 0;
715         default:
716           break;
717         }
718         break; // At most one FI per instruction
719       }
720     }
721   }
722
723   return Limit;
724 }
725
726 static unsigned GetFunctionSizeInBytes(const MachineFunction &MF,
727                                        const ARMBaseInstrInfo &TII) {
728   unsigned FnSize = 0;
729   for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end();
730        MBBI != E; ++MBBI) {
731     const MachineBasicBlock &MBB = *MBBI;
732     for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end();
733          I != E; ++I)
734       FnSize += TII.GetInstSizeInBytes(I);
735   }
736   return FnSize;
737 }
738
739 void
740 ARMBaseRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
741                                                        RegScavenger *RS) const {
742   // This tells PEI to spill the FP as if it is any other callee-save register
743   // to take advantage the eliminateFrameIndex machinery. This also ensures it
744   // is spilled in the order specified by getCalleeSavedRegs() to make it easier
745   // to combine multiple loads / stores.
746   bool CanEliminateFrame = true;
747   bool CS1Spilled = false;
748   bool LRSpilled = false;
749   unsigned NumGPRSpills = 0;
750   SmallVector<unsigned, 4> UnspilledCS1GPRs;
751   SmallVector<unsigned, 4> UnspilledCS2GPRs;
752   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
753   MachineFrameInfo *MFI = MF.getFrameInfo();
754
755   // Spill R4 if Thumb2 function requires stack realignment - it will be used as
756   // scratch register.
757   // FIXME: It will be better just to find spare register here.
758   if (needsStackRealignment(MF) &&
759       AFI->isThumb2Function())
760     MF.getRegInfo().setPhysRegUsed(ARM::R4);
761
762   // Spill LR if Thumb1 function uses variable length argument lists.
763   if (AFI->isThumb1OnlyFunction() && AFI->getVarArgsRegSaveSize() > 0)
764     MF.getRegInfo().setPhysRegUsed(ARM::LR);
765
766   // Don't spill FP if the frame can be eliminated. This is determined
767   // by scanning the callee-save registers to see if any is used.
768   const unsigned *CSRegs = getCalleeSavedRegs();
769   for (unsigned i = 0; CSRegs[i]; ++i) {
770     unsigned Reg = CSRegs[i];
771     bool Spilled = false;
772     if (MF.getRegInfo().isPhysRegUsed(Reg)) {
773       AFI->setCSRegisterIsSpilled(Reg);
774       Spilled = true;
775       CanEliminateFrame = false;
776     } else {
777       // Check alias registers too.
778       for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
779         if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
780           Spilled = true;
781           CanEliminateFrame = false;
782         }
783       }
784     }
785
786     if (!ARM::GPRRegisterClass->contains(Reg))
787       continue;
788
789     if (Spilled) {
790       NumGPRSpills++;
791
792       if (!STI.isTargetDarwin()) {
793         if (Reg == ARM::LR)
794           LRSpilled = true;
795         CS1Spilled = true;
796         continue;
797       }
798
799       // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
800       switch (Reg) {
801       case ARM::LR:
802         LRSpilled = true;
803         // Fallthrough
804       case ARM::R4:
805       case ARM::R5:
806       case ARM::R6:
807       case ARM::R7:
808         CS1Spilled = true;
809         break;
810       default:
811         break;
812       }
813     } else {
814       if (!STI.isTargetDarwin()) {
815         UnspilledCS1GPRs.push_back(Reg);
816         continue;
817       }
818
819       switch (Reg) {
820       case ARM::R4:
821       case ARM::R5:
822       case ARM::R6:
823       case ARM::R7:
824       case ARM::LR:
825         UnspilledCS1GPRs.push_back(Reg);
826         break;
827       default:
828         UnspilledCS2GPRs.push_back(Reg);
829         break;
830       }
831     }
832   }
833
834   bool ForceLRSpill = false;
835   if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
836     unsigned FnSize = GetFunctionSizeInBytes(MF, TII);
837     // Force LR to be spilled if the Thumb function size is > 2048. This enables
838     // use of BL to implement far jump. If it turns out that it's not needed
839     // then the branch fix up path will undo it.
840     if (FnSize >= (1 << 11)) {
841       CanEliminateFrame = false;
842       ForceLRSpill = true;
843     }
844   }
845
846   // If any of the stack slot references may be out of range of an immediate
847   // offset, make sure a register (or a spill slot) is available for the
848   // register scavenger. Note that if we're indexing off the frame pointer, the
849   // effective stack size is 4 bytes larger since the FP points to the stack
850   // slot of the previous FP. Also, if we have variable sized objects in the
851   // function, stack slot references will often be negative, and some of
852   // our instructions are positive-offset only, so conservatively consider
853   // that case to want a spill slot (or register) as well.
854   // FIXME: We could add logic to be more precise about negative offsets
855   //        and which instructions will need a scratch register for them. Is it
856   //        worth the effort and added fragility?
857   bool BigStack =
858     (RS && (estimateStackSize(MF) + (hasFP(MF) ? 4:0) >=
859             estimateRSStackSizeLimit(MF))) || MFI->hasVarSizedObjects();
860
861   bool ExtraCSSpill = false;
862   if (BigStack || !CanEliminateFrame || cannotEliminateFrame(MF)) {
863     AFI->setHasStackFrame(true);
864
865     // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
866     // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
867     if (!LRSpilled && CS1Spilled) {
868       MF.getRegInfo().setPhysRegUsed(ARM::LR);
869       AFI->setCSRegisterIsSpilled(ARM::LR);
870       NumGPRSpills++;
871       UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
872                                     UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
873       ForceLRSpill = false;
874       ExtraCSSpill = true;
875     }
876
877     // Darwin ABI requires FP to point to the stack slot that contains the
878     // previous FP.
879     if (STI.isTargetDarwin() || hasFP(MF)) {
880       MF.getRegInfo().setPhysRegUsed(FramePtr);
881       NumGPRSpills++;
882     }
883
884     // If stack and double are 8-byte aligned and we are spilling an odd number
885     // of GPRs. Spill one extra callee save GPR so we won't have to pad between
886     // the integer and double callee save areas.
887     unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
888     if (TargetAlign == 8 && (NumGPRSpills & 1)) {
889       if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
890         for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
891           unsigned Reg = UnspilledCS1GPRs[i];
892           // Don't spill high register if the function is thumb1
893           if (!AFI->isThumb1OnlyFunction() ||
894               isARMLowRegister(Reg) || Reg == ARM::LR) {
895             MF.getRegInfo().setPhysRegUsed(Reg);
896             AFI->setCSRegisterIsSpilled(Reg);
897             if (!isReservedReg(MF, Reg))
898               ExtraCSSpill = true;
899             break;
900           }
901         }
902       } else if (!UnspilledCS2GPRs.empty() &&
903                  !AFI->isThumb1OnlyFunction()) {
904         unsigned Reg = UnspilledCS2GPRs.front();
905         MF.getRegInfo().setPhysRegUsed(Reg);
906         AFI->setCSRegisterIsSpilled(Reg);
907         if (!isReservedReg(MF, Reg))
908           ExtraCSSpill = true;
909       }
910     }
911
912     // Estimate if we might need to scavenge a register at some point in order
913     // to materialize a stack offset. If so, either spill one additional
914     // callee-saved register or reserve a special spill slot to facilitate
915     // register scavenging. Thumb1 needs a spill slot for stack pointer
916     // adjustments also, even when the frame itself is small.
917     if (BigStack && !ExtraCSSpill) {
918       // If any non-reserved CS register isn't spilled, just spill one or two
919       // extra. That should take care of it!
920       unsigned NumExtras = TargetAlign / 4;
921       SmallVector<unsigned, 2> Extras;
922       while (NumExtras && !UnspilledCS1GPRs.empty()) {
923         unsigned Reg = UnspilledCS1GPRs.back();
924         UnspilledCS1GPRs.pop_back();
925         if (!isReservedReg(MF, Reg) &&
926             (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
927              Reg == ARM::LR)) {
928           Extras.push_back(Reg);
929           NumExtras--;
930         }
931       }
932       // For non-Thumb1 functions, also check for hi-reg CS registers
933       if (!AFI->isThumb1OnlyFunction()) {
934         while (NumExtras && !UnspilledCS2GPRs.empty()) {
935           unsigned Reg = UnspilledCS2GPRs.back();
936           UnspilledCS2GPRs.pop_back();
937           if (!isReservedReg(MF, Reg)) {
938             Extras.push_back(Reg);
939             NumExtras--;
940           }
941         }
942       }
943       if (Extras.size() && NumExtras == 0) {
944         for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
945           MF.getRegInfo().setPhysRegUsed(Extras[i]);
946           AFI->setCSRegisterIsSpilled(Extras[i]);
947         }
948       } else if (!AFI->isThumb1OnlyFunction()) {
949         // note: Thumb1 functions spill to R12, not the stack.  Reserve a slot
950         // closest to SP or frame pointer.
951         const TargetRegisterClass *RC = ARM::GPRRegisterClass;
952         RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
953                                                            RC->getAlignment(),
954                                                            false));
955       }
956     }
957   }
958
959   if (ForceLRSpill) {
960     MF.getRegInfo().setPhysRegUsed(ARM::LR);
961     AFI->setCSRegisterIsSpilled(ARM::LR);
962     AFI->setLRIsSpilledForFarJump(true);
963   }
964 }
965
966 unsigned ARMBaseRegisterInfo::getRARegister() const {
967   return ARM::LR;
968 }
969
970 unsigned 
971 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
972   if (STI.isTargetDarwin() || hasFP(MF))
973     return FramePtr;
974   return ARM::SP;
975 }
976
977 int
978 ARMBaseRegisterInfo::getFrameIndexReference(const MachineFunction &MF, int FI,
979                                             unsigned &FrameReg) const {
980   const MachineFrameInfo *MFI = MF.getFrameInfo();
981   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
982   int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
983   bool isFixed = MFI->isFixedObjectIndex(FI);
984
985   FrameReg = ARM::SP;
986   if (AFI->isGPRCalleeSavedArea1Frame(FI))
987     Offset -= AFI->getGPRCalleeSavedArea1Offset();
988   else if (AFI->isGPRCalleeSavedArea2Frame(FI))
989     Offset -= AFI->getGPRCalleeSavedArea2Offset();
990   else if (AFI->isDPRCalleeSavedAreaFrame(FI))
991     Offset -= AFI->getDPRCalleeSavedAreaOffset();
992   else if (needsStackRealignment(MF)) {
993     // When dynamically realigning the stack, use the frame pointer for
994     // parameters, and the stack pointer for locals.
995     assert (hasFP(MF) && "dynamic stack realignment without a FP!");
996     if (isFixed) {
997       FrameReg = getFrameRegister(MF);
998       Offset -= AFI->getFramePtrSpillOffset();
999     }
1000   } else if (hasFP(MF) && AFI->hasStackFrame()) {
1001     if (isFixed || MFI->hasVarSizedObjects()) {
1002       // Use frame pointer to reference fixed objects unless this is a
1003       // frameless function.
1004       FrameReg = getFrameRegister(MF);
1005       Offset -= AFI->getFramePtrSpillOffset();
1006     } else if (AFI->isThumb2Function()) {
1007       // In Thumb2 mode, the negative offset is very limited.
1008       int FPOffset = Offset - AFI->getFramePtrSpillOffset();
1009       if (FPOffset >= -255 && FPOffset < 0) {
1010         FrameReg = getFrameRegister(MF);
1011         Offset = FPOffset;
1012       }
1013     }
1014   }
1015   return Offset;
1016 }
1017
1018
1019 int
1020 ARMBaseRegisterInfo::getFrameIndexOffset(const MachineFunction &MF,
1021                                          int FI) const {
1022   unsigned FrameReg;
1023   return getFrameIndexReference(MF, FI, FrameReg);
1024 }
1025
1026 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
1027   llvm_unreachable("What is the exception register");
1028   return 0;
1029 }
1030
1031 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
1032   llvm_unreachable("What is the exception handler register");
1033   return 0;
1034 }
1035
1036 int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
1037   return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
1038 }
1039
1040 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
1041                                               const MachineFunction &MF) const {
1042   switch (Reg) {
1043   default: break;
1044   // Return 0 if either register of the pair is a special register.
1045   // So no R12, etc.
1046   case ARM::R1:
1047     return ARM::R0;
1048   case ARM::R3:
1049     return ARM::R2;
1050   case ARM::R5:
1051     return ARM::R4;
1052   case ARM::R7:
1053     return isReservedReg(MF, ARM::R7)  ? 0 : ARM::R6;
1054   case ARM::R9:
1055     return isReservedReg(MF, ARM::R9)  ? 0 :ARM::R8;
1056   case ARM::R11:
1057     return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
1058
1059   case ARM::S1:
1060     return ARM::S0;
1061   case ARM::S3:
1062     return ARM::S2;
1063   case ARM::S5:
1064     return ARM::S4;
1065   case ARM::S7:
1066     return ARM::S6;
1067   case ARM::S9:
1068     return ARM::S8;
1069   case ARM::S11:
1070     return ARM::S10;
1071   case ARM::S13:
1072     return ARM::S12;
1073   case ARM::S15:
1074     return ARM::S14;
1075   case ARM::S17:
1076     return ARM::S16;
1077   case ARM::S19:
1078     return ARM::S18;
1079   case ARM::S21:
1080     return ARM::S20;
1081   case ARM::S23:
1082     return ARM::S22;
1083   case ARM::S25:
1084     return ARM::S24;
1085   case ARM::S27:
1086     return ARM::S26;
1087   case ARM::S29:
1088     return ARM::S28;
1089   case ARM::S31:
1090     return ARM::S30;
1091
1092   case ARM::D1:
1093     return ARM::D0;
1094   case ARM::D3:
1095     return ARM::D2;
1096   case ARM::D5:
1097     return ARM::D4;
1098   case ARM::D7:
1099     return ARM::D6;
1100   case ARM::D9:
1101     return ARM::D8;
1102   case ARM::D11:
1103     return ARM::D10;
1104   case ARM::D13:
1105     return ARM::D12;
1106   case ARM::D15:
1107     return ARM::D14;
1108   case ARM::D17:
1109     return ARM::D16;
1110   case ARM::D19:
1111     return ARM::D18;
1112   case ARM::D21:
1113     return ARM::D20;
1114   case ARM::D23:
1115     return ARM::D22;
1116   case ARM::D25:
1117     return ARM::D24;
1118   case ARM::D27:
1119     return ARM::D26;
1120   case ARM::D29:
1121     return ARM::D28;
1122   case ARM::D31:
1123     return ARM::D30;
1124   }
1125
1126   return 0;
1127 }
1128
1129 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
1130                                              const MachineFunction &MF) const {
1131   switch (Reg) {
1132   default: break;
1133   // Return 0 if either register of the pair is a special register.
1134   // So no R12, etc.
1135   case ARM::R0:
1136     return ARM::R1;
1137   case ARM::R2:
1138     return ARM::R3;
1139   case ARM::R4:
1140     return ARM::R5;
1141   case ARM::R6:
1142     return isReservedReg(MF, ARM::R7)  ? 0 : ARM::R7;
1143   case ARM::R8:
1144     return isReservedReg(MF, ARM::R9)  ? 0 :ARM::R9;
1145   case ARM::R10:
1146     return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
1147
1148   case ARM::S0:
1149     return ARM::S1;
1150   case ARM::S2:
1151     return ARM::S3;
1152   case ARM::S4:
1153     return ARM::S5;
1154   case ARM::S6:
1155     return ARM::S7;
1156   case ARM::S8:
1157     return ARM::S9;
1158   case ARM::S10:
1159     return ARM::S11;
1160   case ARM::S12:
1161     return ARM::S13;
1162   case ARM::S14:
1163     return ARM::S15;
1164   case ARM::S16:
1165     return ARM::S17;
1166   case ARM::S18:
1167     return ARM::S19;
1168   case ARM::S20:
1169     return ARM::S21;
1170   case ARM::S22:
1171     return ARM::S23;
1172   case ARM::S24:
1173     return ARM::S25;
1174   case ARM::S26:
1175     return ARM::S27;
1176   case ARM::S28:
1177     return ARM::S29;
1178   case ARM::S30:
1179     return ARM::S31;
1180
1181   case ARM::D0:
1182     return ARM::D1;
1183   case ARM::D2:
1184     return ARM::D3;
1185   case ARM::D4:
1186     return ARM::D5;
1187   case ARM::D6:
1188     return ARM::D7;
1189   case ARM::D8:
1190     return ARM::D9;
1191   case ARM::D10:
1192     return ARM::D11;
1193   case ARM::D12:
1194     return ARM::D13;
1195   case ARM::D14:
1196     return ARM::D15;
1197   case ARM::D16:
1198     return ARM::D17;
1199   case ARM::D18:
1200     return ARM::D19;
1201   case ARM::D20:
1202     return ARM::D21;
1203   case ARM::D22:
1204     return ARM::D23;
1205   case ARM::D24:
1206     return ARM::D25;
1207   case ARM::D26:
1208     return ARM::D27;
1209   case ARM::D28:
1210     return ARM::D29;
1211   case ARM::D30:
1212     return ARM::D31;
1213   }
1214
1215   return 0;
1216 }
1217
1218 /// emitLoadConstPool - Emits a load from constpool to materialize the
1219 /// specified immediate.
1220 void ARMBaseRegisterInfo::
1221 emitLoadConstPool(MachineBasicBlock &MBB,
1222                   MachineBasicBlock::iterator &MBBI,
1223                   DebugLoc dl,
1224                   unsigned DestReg, unsigned SubIdx, int Val,
1225                   ARMCC::CondCodes Pred,
1226                   unsigned PredReg) const {
1227   MachineFunction &MF = *MBB.getParent();
1228   MachineConstantPool *ConstantPool = MF.getConstantPool();
1229   const Constant *C =
1230         ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
1231   unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
1232
1233   BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
1234     .addReg(DestReg, getDefRegState(true), SubIdx)
1235     .addConstantPoolIndex(Idx)
1236     .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
1237 }
1238
1239 bool ARMBaseRegisterInfo::
1240 requiresRegisterScavenging(const MachineFunction &MF) const {
1241   return true;
1242 }
1243
1244 bool ARMBaseRegisterInfo::
1245 requiresFrameIndexScavenging(const MachineFunction &MF) const {
1246   return true;
1247 }
1248
1249 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
1250 // not required, we reserve argument space for call sites in the function
1251 // immediately on entry to the current function. This eliminates the need for
1252 // add/sub sp brackets around call sites. Returns true if the call frame is
1253 // included as part of the stack frame.
1254 bool ARMBaseRegisterInfo::
1255 hasReservedCallFrame(const MachineFunction &MF) const {
1256   const MachineFrameInfo *FFI = MF.getFrameInfo();
1257   unsigned CFSize = FFI->getMaxCallFrameSize();
1258   // It's not always a good idea to include the call frame as part of the
1259   // stack frame. ARM (especially Thumb) has small immediate offset to
1260   // address the stack frame. So a large call frame can cause poor codegen
1261   // and may even makes it impossible to scavenge a register.
1262   if (CFSize >= ((1 << 12) - 1) / 2)  // Half of imm12
1263     return false;
1264
1265   return !MF.getFrameInfo()->hasVarSizedObjects();
1266 }
1267
1268 // canSimplifyCallFramePseudos - If there is a reserved call frame, the
1269 // call frame pseudos can be simplified. Unlike most targets, having a FP
1270 // is not sufficient here since we still may reference some objects via SP
1271 // even when FP is available in Thumb2 mode.
1272 bool ARMBaseRegisterInfo::
1273 canSimplifyCallFramePseudos(const MachineFunction &MF) const {
1274   return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
1275 }
1276
1277 static void
1278 emitSPUpdate(bool isARM,
1279              MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1280              DebugLoc dl, const ARMBaseInstrInfo &TII,
1281              int NumBytes,
1282              ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
1283   if (isARM)
1284     emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1285                             Pred, PredReg, TII);
1286   else
1287     emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1288                            Pred, PredReg, TII);
1289 }
1290
1291
1292 void ARMBaseRegisterInfo::
1293 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1294                               MachineBasicBlock::iterator I) const {
1295   if (!hasReservedCallFrame(MF)) {
1296     // If we have alloca, convert as follows:
1297     // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1298     // ADJCALLSTACKUP   -> add, sp, sp, amount
1299     MachineInstr *Old = I;
1300     DebugLoc dl = Old->getDebugLoc();
1301     unsigned Amount = Old->getOperand(0).getImm();
1302     if (Amount != 0) {
1303       // We need to keep the stack aligned properly.  To do this, we round the
1304       // amount of space needed for the outgoing arguments up to the next
1305       // alignment boundary.
1306       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1307       Amount = (Amount+Align-1)/Align*Align;
1308
1309       ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1310       assert(!AFI->isThumb1OnlyFunction() &&
1311              "This eliminateCallFramePseudoInstr does not support Thumb1!");
1312       bool isARM = !AFI->isThumbFunction();
1313
1314       // Replace the pseudo instruction with a new instruction...
1315       unsigned Opc = Old->getOpcode();
1316       int PIdx = Old->findFirstPredOperandIdx();
1317       ARMCC::CondCodes Pred = (PIdx == -1)
1318         ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
1319       if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1320         // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1321         unsigned PredReg = Old->getOperand(2).getReg();
1322         emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
1323       } else {
1324         // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1325         unsigned PredReg = Old->getOperand(3).getReg();
1326         assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1327         emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
1328       }
1329     }
1330   }
1331   MBB.erase(I);
1332 }
1333
1334 unsigned
1335 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1336                                          int SPAdj, FrameIndexValue *Value,
1337                                          RegScavenger *RS) const {
1338   unsigned i = 0;
1339   MachineInstr &MI = *II;
1340   MachineBasicBlock &MBB = *MI.getParent();
1341   MachineFunction &MF = *MBB.getParent();
1342   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1343   assert(!AFI->isThumb1OnlyFunction() &&
1344          "This eliminateFrameIndex does not support Thumb1!");
1345
1346   while (!MI.getOperand(i).isFI()) {
1347     ++i;
1348     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1349   }
1350
1351   int FrameIndex = MI.getOperand(i).getIndex();
1352   unsigned FrameReg;
1353
1354   int Offset = getFrameIndexReference(MF, FrameIndex, FrameReg);
1355   if (FrameReg != ARM::SP)
1356     SPAdj = 0;
1357   Offset += SPAdj;
1358
1359   // Special handling of dbg_value instructions.
1360   if (MI.isDebugValue()) {
1361     MI.getOperand(i).  ChangeToRegister(FrameReg, false /*isDef*/);
1362     MI.getOperand(i+1).ChangeToImmediate(Offset);
1363     return 0;
1364   }
1365
1366   // Modify MI as necessary to handle as much of 'Offset' as possible
1367   bool Done = false;
1368   if (!AFI->isThumbFunction())
1369     Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1370   else {
1371     assert(AFI->isThumb2Function());
1372     Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1373   }
1374   if (Done)
1375     return 0;
1376
1377   // If we get here, the immediate doesn't fit into the instruction.  We folded
1378   // as much as possible above, handle the rest, providing a register that is
1379   // SP+LargeImm.
1380   assert((Offset ||
1381           (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
1382           (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
1383          "This code isn't needed if offset already handled!");
1384
1385   unsigned ScratchReg = 0;
1386   int PIdx = MI.findFirstPredOperandIdx();
1387   ARMCC::CondCodes Pred = (PIdx == -1)
1388     ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1389   unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1390   if (Offset == 0)
1391     // Must be addrmode4/6.
1392     MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
1393   else {
1394     ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1395     if (Value) {
1396       Value->first = FrameReg; // use the frame register as a kind indicator
1397       Value->second = Offset;
1398     }
1399     if (!AFI->isThumbFunction())
1400       emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1401                               Offset, Pred, PredReg, TII);
1402     else {
1403       assert(AFI->isThumb2Function());
1404       emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1405                              Offset, Pred, PredReg, TII);
1406     }
1407     MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1408     if (!ReuseFrameIndexVals)
1409       ScratchReg = 0;
1410   }
1411   return ScratchReg;
1412 }
1413
1414 /// Move iterator past the next bunch of callee save load / store ops for
1415 /// the particular spill area (1: integer area 1, 2: integer area 2,
1416 /// 3: fp area, 0: don't care).
1417 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1418                                    MachineBasicBlock::iterator &MBBI,
1419                                    int Opc1, int Opc2, unsigned Area,
1420                                    const ARMSubtarget &STI) {
1421   while (MBBI != MBB.end() &&
1422          ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) &&
1423          MBBI->getOperand(1).isFI()) {
1424     if (Area != 0) {
1425       bool Done = false;
1426       unsigned Category = 0;
1427       switch (MBBI->getOperand(0).getReg()) {
1428       case ARM::R4:  case ARM::R5:  case ARM::R6: case ARM::R7:
1429       case ARM::LR:
1430         Category = 1;
1431         break;
1432       case ARM::R8:  case ARM::R9:  case ARM::R10: case ARM::R11:
1433         Category = STI.isTargetDarwin() ? 2 : 1;
1434         break;
1435       case ARM::D8:  case ARM::D9:  case ARM::D10: case ARM::D11:
1436       case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1437         Category = 3;
1438         break;
1439       default:
1440         Done = true;
1441         break;
1442       }
1443       if (Done || Category != Area)
1444         break;
1445     }
1446
1447     ++MBBI;
1448   }
1449 }
1450
1451 void ARMBaseRegisterInfo::
1452 emitPrologue(MachineFunction &MF) const {
1453   MachineBasicBlock &MBB = MF.front();
1454   MachineBasicBlock::iterator MBBI = MBB.begin();
1455   MachineFrameInfo  *MFI = MF.getFrameInfo();
1456   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1457   assert(!AFI->isThumb1OnlyFunction() &&
1458          "This emitPrologue does not support Thumb1!");
1459   bool isARM = !AFI->isThumbFunction();
1460   unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1461   unsigned NumBytes = MFI->getStackSize();
1462   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1463   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1464
1465   // Determine the sizes of each callee-save spill areas and record which frame
1466   // belongs to which callee-save spill areas.
1467   unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1468   int FramePtrSpillFI = 0;
1469
1470   // Allocate the vararg register save area. This is not counted in NumBytes.
1471   if (VARegSaveSize)
1472     emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
1473
1474   if (!AFI->hasStackFrame()) {
1475     if (NumBytes != 0)
1476       emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1477     return;
1478   }
1479
1480   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1481     unsigned Reg = CSI[i].getReg();
1482     int FI = CSI[i].getFrameIdx();
1483     switch (Reg) {
1484     case ARM::R4:
1485     case ARM::R5:
1486     case ARM::R6:
1487     case ARM::R7:
1488     case ARM::LR:
1489       if (Reg == FramePtr)
1490         FramePtrSpillFI = FI;
1491       AFI->addGPRCalleeSavedArea1Frame(FI);
1492       GPRCS1Size += 4;
1493       break;
1494     case ARM::R8:
1495     case ARM::R9:
1496     case ARM::R10:
1497     case ARM::R11:
1498       if (Reg == FramePtr)
1499         FramePtrSpillFI = FI;
1500       if (STI.isTargetDarwin()) {
1501         AFI->addGPRCalleeSavedArea2Frame(FI);
1502         GPRCS2Size += 4;
1503       } else {
1504         AFI->addGPRCalleeSavedArea1Frame(FI);
1505         GPRCS1Size += 4;
1506       }
1507       break;
1508     default:
1509       AFI->addDPRCalleeSavedAreaFrame(FI);
1510       DPRCSSize += 8;
1511     }
1512   }
1513
1514   // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1515   emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size);
1516   movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI);
1517
1518   // Set FP to point to the stack slot that contains the previous FP.
1519   // For Darwin, FP is R7, which has now been stored in spill area 1.
1520   // Otherwise, if this is not Darwin, all the callee-saved registers go
1521   // into spill area 1, including the FP in R11.  In either case, it is
1522   // now safe to emit this assignment.
1523   if (STI.isTargetDarwin() || hasFP(MF)) {
1524     unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
1525     MachineInstrBuilder MIB =
1526       BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
1527       .addFrameIndex(FramePtrSpillFI).addImm(0);
1528     AddDefaultCC(AddDefaultPred(MIB));
1529   }
1530
1531   // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1532   emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size);
1533
1534   // Build the new SUBri to adjust SP for FP callee-save spill area.
1535   movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI);
1536   emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize);
1537
1538   // Determine starting offsets of spill areas.
1539   unsigned DPRCSOffset  = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1540   unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1541   unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1542   if (STI.isTargetDarwin() || hasFP(MF))
1543     AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
1544                                 NumBytes);
1545   AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1546   AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1547   AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1548
1549   movePastCSLoadStoreOps(MBB, MBBI, ARM::VSTRD, 0, 3, STI);
1550   NumBytes = DPRCSOffset;
1551   if (NumBytes) {
1552     // Adjust SP after all the callee-save spills.
1553     emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1554   }
1555
1556   if (STI.isTargetELF() && hasFP(MF)) {
1557     MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1558                              AFI->getFramePtrSpillOffset());
1559   }
1560
1561   AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1562   AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1563   AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1564
1565   // If we need dynamic stack realignment, do it here.
1566   if (needsStackRealignment(MF)) {
1567     unsigned MaxAlign = MFI->getMaxAlignment();
1568     assert (!AFI->isThumb1OnlyFunction());
1569     if (!AFI->isThumbFunction()) {
1570       // Emit bic sp, sp, MaxAlign
1571       AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
1572                                           TII.get(ARM::BICri), ARM::SP)
1573                                   .addReg(ARM::SP, RegState::Kill)
1574                                   .addImm(MaxAlign-1)));
1575     } else {
1576       // We cannot use sp as source/dest register here, thus we're emitting the
1577       // following sequence:
1578       // mov r4, sp
1579       // bic r4, r4, MaxAlign
1580       // mov sp, r4
1581       // FIXME: It will be better just to find spare register here.
1582       BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2tgpr), ARM::R4)
1583         .addReg(ARM::SP, RegState::Kill);
1584       AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
1585                                           TII.get(ARM::t2BICri), ARM::R4)
1586                                   .addReg(ARM::R4, RegState::Kill)
1587                                   .addImm(MaxAlign-1)));
1588       BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
1589         .addReg(ARM::R4, RegState::Kill);
1590     }
1591   }
1592 }
1593
1594 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1595   for (unsigned i = 0; CSRegs[i]; ++i)
1596     if (Reg == CSRegs[i])
1597       return true;
1598   return false;
1599 }
1600
1601 static bool isCSRestore(MachineInstr *MI,
1602                         const ARMBaseInstrInfo &TII,
1603                         const unsigned *CSRegs) {
1604   return ((MI->getOpcode() == (int)ARM::VLDRD ||
1605            MI->getOpcode() == (int)ARM::LDR ||
1606            MI->getOpcode() == (int)ARM::t2LDRi12) &&
1607           MI->getOperand(1).isFI() &&
1608           isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1609 }
1610
1611 void ARMBaseRegisterInfo::
1612 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
1613   MachineBasicBlock::iterator MBBI = prior(MBB.end());
1614   assert(MBBI->getDesc().isReturn() &&
1615          "Can only insert epilog into returning blocks");
1616   unsigned RetOpcode = MBBI->getOpcode();
1617   DebugLoc dl = MBBI->getDebugLoc();
1618   MachineFrameInfo *MFI = MF.getFrameInfo();
1619   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1620   assert(!AFI->isThumb1OnlyFunction() &&
1621          "This emitEpilogue does not support Thumb1!");
1622   bool isARM = !AFI->isThumbFunction();
1623
1624   unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1625   int NumBytes = (int)MFI->getStackSize();
1626
1627   if (!AFI->hasStackFrame()) {
1628     if (NumBytes != 0)
1629       emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1630   } else {
1631     // Unwind MBBI to point to first LDR / VLDRD.
1632     const unsigned *CSRegs = getCalleeSavedRegs();
1633     if (MBBI != MBB.begin()) {
1634       do
1635         --MBBI;
1636       while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
1637       if (!isCSRestore(MBBI, TII, CSRegs))
1638         ++MBBI;
1639     }
1640
1641     // Move SP to start of FP callee save spill area.
1642     NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1643                  AFI->getGPRCalleeSavedArea2Size() +
1644                  AFI->getDPRCalleeSavedAreaSize());
1645
1646     // Darwin ABI requires FP to point to the stack slot that contains the
1647     // previous FP.
1648     bool HasFP = hasFP(MF);
1649     if ((STI.isTargetDarwin() && NumBytes) || HasFP) {
1650       NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1651       // Reset SP based on frame pointer only if the stack frame extends beyond
1652       // frame pointer stack slot or target is ELF and the function has FP.
1653       if (HasFP ||
1654           AFI->getGPRCalleeSavedArea2Size() ||
1655           AFI->getDPRCalleeSavedAreaSize()  ||
1656           AFI->getDPRCalleeSavedAreaOffset()) {
1657         if (NumBytes) {
1658           if (isARM)
1659             emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1660                                     ARMCC::AL, 0, TII);
1661           else
1662             emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1663                                     ARMCC::AL, 0, TII);
1664         } else {
1665           // Thumb2 or ARM.
1666           if (isARM)
1667             BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
1668               .addReg(FramePtr)
1669               .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1670           else
1671             BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
1672               .addReg(FramePtr);
1673         }
1674       }
1675     } else if (NumBytes)
1676       emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1677
1678     // Move SP to start of integer callee save spill area 2.
1679     movePastCSLoadStoreOps(MBB, MBBI, ARM::VLDRD, 0, 3, STI);
1680     emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize());
1681
1682     // Move SP to start of integer callee save spill area 1.
1683     movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI);
1684     emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size());
1685
1686     // Move SP to SP upon entry to the function.
1687     movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI);
1688     emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size());
1689   }
1690
1691   if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND ||
1692       RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) {
1693     // Tail call return: adjust the stack pointer and jump to callee.
1694     MBBI = prior(MBB.end());
1695     MachineOperand &JumpTarget = MBBI->getOperand(0);
1696
1697     // Jump to label or value in register.
1698     if (RetOpcode == ARM::TCRETURNdi) {
1699       BuildMI(MBB, MBBI, dl, 
1700             TII.get(STI.isThumb() ? ARM::TAILJMPdt : ARM::TAILJMPd)).
1701         addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
1702                          JumpTarget.getTargetFlags());
1703     } else if (RetOpcode == ARM::TCRETURNdiND) {
1704       BuildMI(MBB, MBBI, dl,
1705             TII.get(STI.isThumb() ? ARM::TAILJMPdNDt : ARM::TAILJMPdND)).
1706         addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
1707                          JumpTarget.getTargetFlags());
1708     } else if (RetOpcode == ARM::TCRETURNri) {
1709       BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPr)).
1710         addReg(JumpTarget.getReg(), RegState::Kill);
1711     } else if (RetOpcode == ARM::TCRETURNriND) {
1712       BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPrND)).
1713         addReg(JumpTarget.getReg(), RegState::Kill);
1714     } 
1715
1716     MachineInstr *NewMI = prior(MBBI);
1717     for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
1718       NewMI->addOperand(MBBI->getOperand(i));
1719
1720     // Delete the pseudo instruction TCRETURN.
1721     MBB.erase(MBBI);
1722   }
1723
1724   if (VARegSaveSize)
1725     emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
1726 }
1727
1728 #include "ARMGenRegisterInfo.inc"