dbbce075b647f02ada82b9e1716900f017c4c64d
[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 "ARMBaseInstrInfo.h"
16 #include "ARMBaseRegisterInfo.h"
17 #include "ARMFrameLowering.h"
18 #include "ARMInstrInfo.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMSubtarget.h"
21 #include "MCTargetDesc/ARMAddressingModes.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Function.h"
25 #include "llvm/LLVMContext.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.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/TargetFrameLowering.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 #define GET_REGINFO_TARGET_DESC
43 #include "ARMGenRegisterInfo.inc"
44
45 using namespace llvm;
46
47 static cl::opt<bool>
48 ForceAllBaseRegAlloc("arm-force-base-reg-alloc", cl::Hidden, cl::init(false),
49           cl::desc("Force use of virtual base registers for stack load/store"));
50 static cl::opt<bool>
51 EnableLocalStackAlloc("enable-local-stack-alloc", cl::init(true), cl::Hidden,
52           cl::desc("Enable pre-regalloc stack frame index allocation"));
53 static cl::opt<bool>
54 EnableBasePointer("arm-use-base-pointer", cl::Hidden, cl::init(true),
55           cl::desc("Enable use of a base pointer for complex stack frames"));
56
57 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
58                                          const ARMSubtarget &sti)
59   : ARMGenRegisterInfo(ARM::LR), TII(tii), STI(sti),
60     FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11),
61     BasePtr(ARM::R6) {
62 }
63
64 const unsigned*
65 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
66   static const unsigned CalleeSavedRegs[] = {
67     ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
68     ARM::R7, ARM::R6,  ARM::R5,  ARM::R4,
69
70     ARM::D15, ARM::D14, ARM::D13, ARM::D12,
71     ARM::D11, ARM::D10, ARM::D9,  ARM::D8,
72     0
73   };
74
75   static const unsigned DarwinCalleeSavedRegs[] = {
76     // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
77     // register.
78     ARM::LR,  ARM::R7,  ARM::R6, ARM::R5, ARM::R4,
79     ARM::R11, ARM::R10, ARM::R8,
80
81     ARM::D15, ARM::D14, ARM::D13, ARM::D12,
82     ARM::D11, ARM::D10, ARM::D9,  ARM::D8,
83     0
84   };
85   return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
86 }
87
88 BitVector ARMBaseRegisterInfo::
89 getReservedRegs(const MachineFunction &MF) const {
90   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
91
92   // FIXME: avoid re-calculating this every time.
93   BitVector Reserved(getNumRegs());
94   Reserved.set(ARM::SP);
95   Reserved.set(ARM::PC);
96   Reserved.set(ARM::FPSCR);
97   if (TFI->hasFP(MF))
98     Reserved.set(FramePtr);
99   if (hasBasePointer(MF))
100     Reserved.set(BasePtr);
101   // Some targets reserve R9.
102   if (STI.isR9Reserved())
103     Reserved.set(ARM::R9);
104   // Reserve D16-D31 if the subtarget doesn't support them.
105   if (!STI.hasVFP3() || STI.hasD16()) {
106     assert(ARM::D31 == ARM::D16 + 15);
107     for (unsigned i = 0; i != 16; ++i)
108       Reserved.set(ARM::D16 + i);
109   }
110   return Reserved;
111 }
112
113 bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
114                                         unsigned Reg) const {
115   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
116
117   switch (Reg) {
118   default: break;
119   case ARM::SP:
120   case ARM::PC:
121     return true;
122   case ARM::R6:
123     if (hasBasePointer(MF))
124       return true;
125     break;
126   case ARM::R7:
127   case ARM::R11:
128     if (FramePtr == Reg && TFI->hasFP(MF))
129       return true;
130     break;
131   case ARM::R9:
132     return STI.isR9Reserved();
133   }
134
135   return false;
136 }
137
138 const TargetRegisterClass *
139 ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
140                                               const TargetRegisterClass *B,
141                                               unsigned SubIdx) const {
142   switch (SubIdx) {
143   default: return 0;
144   case ARM::ssub_0:
145   case ARM::ssub_1:
146   case ARM::ssub_2:
147   case ARM::ssub_3: {
148     // S sub-registers.
149     if (A->getSize() == 8) {
150       if (B == &ARM::SPR_8RegClass)
151         return &ARM::DPR_8RegClass;
152       assert(B == &ARM::SPRRegClass && "Expecting SPR register class!");
153       if (A == &ARM::DPR_8RegClass)
154         return A;
155       return &ARM::DPR_VFP2RegClass;
156     }
157
158     if (A->getSize() == 16) {
159       if (B == &ARM::SPR_8RegClass)
160         return &ARM::QPR_8RegClass;
161       return &ARM::QPR_VFP2RegClass;
162     }
163
164     if (A->getSize() == 32) {
165       if (B == &ARM::SPR_8RegClass)
166         return 0;  // Do not allow coalescing!
167       return &ARM::QQPR_VFP2RegClass;
168     }
169
170     assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
171     return 0;  // Do not allow coalescing!
172   }
173   case ARM::dsub_0:
174   case ARM::dsub_1:
175   case ARM::dsub_2:
176   case ARM::dsub_3: {
177     // D sub-registers.
178     if (A->getSize() == 16) {
179       if (B == &ARM::DPR_VFP2RegClass)
180         return &ARM::QPR_VFP2RegClass;
181       if (B == &ARM::DPR_8RegClass)
182         return 0;  // Do not allow coalescing!
183       return A;
184     }
185
186     if (A->getSize() == 32) {
187       if (B == &ARM::DPR_VFP2RegClass)
188         return &ARM::QQPR_VFP2RegClass;
189       if (B == &ARM::DPR_8RegClass)
190         return 0;  // Do not allow coalescing!
191       return A;
192     }
193
194     assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
195     if (B != &ARM::DPRRegClass)
196       return 0;  // Do not allow coalescing!
197     return A;
198   }
199   case ARM::dsub_4:
200   case ARM::dsub_5:
201   case ARM::dsub_6:
202   case ARM::dsub_7: {
203     // D sub-registers of QQQQ registers.
204     if (A->getSize() == 64 && B == &ARM::DPRRegClass)
205       return A;
206     return 0;  // Do not allow coalescing!
207   }
208
209   case ARM::qsub_0:
210   case ARM::qsub_1: {
211     // Q sub-registers.
212     if (A->getSize() == 32) {
213       if (B == &ARM::QPR_VFP2RegClass)
214         return &ARM::QQPR_VFP2RegClass;
215       if (B == &ARM::QPR_8RegClass)
216         return 0;  // Do not allow coalescing!
217       return A;
218     }
219
220     assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
221     if (B == &ARM::QPRRegClass)
222       return A;
223     return 0;  // Do not allow coalescing!
224   }
225   case ARM::qsub_2:
226   case ARM::qsub_3: {
227     // Q sub-registers of QQQQ registers.
228     if (A->getSize() == 64 && B == &ARM::QPRRegClass)
229       return A;
230     return 0;  // Do not allow coalescing!
231   }
232   }
233   return 0;
234 }
235
236 bool
237 ARMBaseRegisterInfo::canCombineSubRegIndices(const TargetRegisterClass *RC,
238                                           SmallVectorImpl<unsigned> &SubIndices,
239                                           unsigned &NewSubIdx) const {
240
241   unsigned Size = RC->getSize() * 8;
242   if (Size < 6)
243     return 0;
244
245   NewSubIdx = 0;  // Whole register.
246   unsigned NumRegs = SubIndices.size();
247   if (NumRegs == 8) {
248     // 8 D registers -> 1 QQQQ register.
249     return (Size == 512 &&
250             SubIndices[0] == ARM::dsub_0 &&
251             SubIndices[1] == ARM::dsub_1 &&
252             SubIndices[2] == ARM::dsub_2 &&
253             SubIndices[3] == ARM::dsub_3 &&
254             SubIndices[4] == ARM::dsub_4 &&
255             SubIndices[5] == ARM::dsub_5 &&
256             SubIndices[6] == ARM::dsub_6 &&
257             SubIndices[7] == ARM::dsub_7);
258   } else if (NumRegs == 4) {
259     if (SubIndices[0] == ARM::qsub_0) {
260       // 4 Q registers -> 1 QQQQ register.
261       return (Size == 512 &&
262               SubIndices[1] == ARM::qsub_1 &&
263               SubIndices[2] == ARM::qsub_2 &&
264               SubIndices[3] == ARM::qsub_3);
265     } else if (SubIndices[0] == ARM::dsub_0) {
266       // 4 D registers -> 1 QQ register.
267       if (Size >= 256 &&
268           SubIndices[1] == ARM::dsub_1 &&
269           SubIndices[2] == ARM::dsub_2 &&
270           SubIndices[3] == ARM::dsub_3) {
271         if (Size == 512)
272           NewSubIdx = ARM::qqsub_0;
273         return true;
274       }
275     } else if (SubIndices[0] == ARM::dsub_4) {
276       // 4 D registers -> 1 QQ register (2nd).
277       if (Size == 512 &&
278           SubIndices[1] == ARM::dsub_5 &&
279           SubIndices[2] == ARM::dsub_6 &&
280           SubIndices[3] == ARM::dsub_7) {
281         NewSubIdx = ARM::qqsub_1;
282         return true;
283       }
284     } else if (SubIndices[0] == ARM::ssub_0) {
285       // 4 S registers -> 1 Q register.
286       if (Size >= 128 &&
287           SubIndices[1] == ARM::ssub_1 &&
288           SubIndices[2] == ARM::ssub_2 &&
289           SubIndices[3] == ARM::ssub_3) {
290         if (Size >= 256)
291           NewSubIdx = ARM::qsub_0;
292         return true;
293       }
294     }
295   } else if (NumRegs == 2) {
296     if (SubIndices[0] == ARM::qsub_0) {
297       // 2 Q registers -> 1 QQ register.
298       if (Size >= 256 && SubIndices[1] == ARM::qsub_1) {
299         if (Size == 512)
300           NewSubIdx = ARM::qqsub_0;
301         return true;
302       }
303     } else if (SubIndices[0] == ARM::qsub_2) {
304       // 2 Q registers -> 1 QQ register (2nd).
305       if (Size == 512 && SubIndices[1] == ARM::qsub_3) {
306         NewSubIdx = ARM::qqsub_1;
307         return true;
308       }
309     } else if (SubIndices[0] == ARM::dsub_0) {
310       // 2 D registers -> 1 Q register.
311       if (Size >= 128 && SubIndices[1] == ARM::dsub_1) {
312         if (Size >= 256)
313           NewSubIdx = ARM::qsub_0;
314         return true;
315       }
316     } else if (SubIndices[0] == ARM::dsub_2) {
317       // 2 D registers -> 1 Q register (2nd).
318       if (Size >= 256 && SubIndices[1] == ARM::dsub_3) {
319         NewSubIdx = ARM::qsub_1;
320         return true;
321       }
322     } else if (SubIndices[0] == ARM::dsub_4) {
323       // 2 D registers -> 1 Q register (3rd).
324       if (Size == 512 && SubIndices[1] == ARM::dsub_5) {
325         NewSubIdx = ARM::qsub_2;
326         return true;
327       }
328     } else if (SubIndices[0] == ARM::dsub_6) {
329       // 2 D registers -> 1 Q register (3rd).
330       if (Size == 512 && SubIndices[1] == ARM::dsub_7) {
331         NewSubIdx = ARM::qsub_3;
332         return true;
333       }
334     } else if (SubIndices[0] == ARM::ssub_0) {
335       // 2 S registers -> 1 D register.
336       if (SubIndices[1] == ARM::ssub_1) {
337         if (Size >= 128)
338           NewSubIdx = ARM::dsub_0;
339         return true;
340       }
341     } else if (SubIndices[0] == ARM::ssub_2) {
342       // 2 S registers -> 1 D register (2nd).
343       if (Size >= 128 && SubIndices[1] == ARM::ssub_3) {
344         NewSubIdx = ARM::dsub_1;
345         return true;
346       }
347     }
348   }
349   return false;
350 }
351
352 const TargetRegisterClass*
353 ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC)
354                                                                          const {
355   const TargetRegisterClass *Super = RC;
356   TargetRegisterClass::sc_iterator I = RC->superclasses_begin();
357   do {
358     switch (Super->getID()) {
359     case ARM::GPRRegClassID:
360     case ARM::SPRRegClassID:
361     case ARM::DPRRegClassID:
362     case ARM::QPRRegClassID:
363     case ARM::QQPRRegClassID:
364     case ARM::QQQQPRRegClassID:
365       return Super;
366     }
367     Super = *I++;
368   } while (Super);
369   return RC;
370 }
371
372 const TargetRegisterClass *
373 ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
374   return ARM::GPRRegisterClass;
375 }
376
377 const TargetRegisterClass *
378 ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
379   if (RC == &ARM::CCRRegClass)
380     return 0;  // Can't copy CCR registers.
381   return RC;
382 }
383
384 unsigned
385 ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
386                                          MachineFunction &MF) const {
387   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
388
389   switch (RC->getID()) {
390   default:
391     return 0;
392   case ARM::tGPRRegClassID:
393     return TFI->hasFP(MF) ? 4 : 5;
394   case ARM::GPRRegClassID: {
395     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
396     return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
397   }
398   case ARM::SPRRegClassID:  // Currently not used as 'rep' register class.
399   case ARM::DPRRegClassID:
400     return 32 - 10;
401   }
402 }
403
404 /// getRawAllocationOrder - Returns the register allocation order for a
405 /// specified register class with a target-dependent hint.
406 ArrayRef<unsigned>
407 ARMBaseRegisterInfo::getRawAllocationOrder(const TargetRegisterClass *RC,
408                                            unsigned HintType, unsigned HintReg,
409                                            const MachineFunction &MF) const {
410   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
411   // Alternative register allocation orders when favoring even / odd registers
412   // of register pairs.
413
414   // No FP, R9 is available.
415   static const unsigned GPREven1[] = {
416     ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
417     ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
418     ARM::R9, ARM::R11
419   };
420   static const unsigned GPROdd1[] = {
421     ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
422     ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
423     ARM::R8, ARM::R10
424   };
425
426   // FP is R7, R9 is available.
427   static const unsigned GPREven2[] = {
428     ARM::R0, ARM::R2, ARM::R4,          ARM::R8, ARM::R10,
429     ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
430     ARM::R9, ARM::R11
431   };
432   static const unsigned GPROdd2[] = {
433     ARM::R1, ARM::R3, ARM::R5,          ARM::R9, ARM::R11,
434     ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
435     ARM::R8, ARM::R10
436   };
437
438   // FP is R11, R9 is available.
439   static const unsigned GPREven3[] = {
440     ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
441     ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
442     ARM::R9
443   };
444   static const unsigned GPROdd3[] = {
445     ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
446     ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
447     ARM::R8
448   };
449
450   // No FP, R9 is not available.
451   static const unsigned GPREven4[] = {
452     ARM::R0, ARM::R2, ARM::R4, ARM::R6,          ARM::R10,
453     ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
454     ARM::R11
455   };
456   static const unsigned GPROdd4[] = {
457     ARM::R1, ARM::R3, ARM::R5, ARM::R7,          ARM::R11,
458     ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
459     ARM::R10
460   };
461
462   // FP is R7, R9 is not available.
463   static const unsigned GPREven5[] = {
464     ARM::R0, ARM::R2, ARM::R4,                   ARM::R10,
465     ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
466     ARM::R11
467   };
468   static const unsigned GPROdd5[] = {
469     ARM::R1, ARM::R3, ARM::R5,                   ARM::R11,
470     ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
471     ARM::R10
472   };
473
474   // FP is R11, R9 is not available.
475   static const unsigned GPREven6[] = {
476     ARM::R0, ARM::R2, ARM::R4, ARM::R6,
477     ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
478   };
479   static const unsigned GPROdd6[] = {
480     ARM::R1, ARM::R3, ARM::R5, ARM::R7,
481     ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
482   };
483
484   // We only support even/odd hints for GPR and rGPR.
485   if (RC != ARM::GPRRegisterClass && RC != ARM::rGPRRegisterClass)
486     return RC->getRawAllocationOrder(MF);
487
488   if (HintType == ARMRI::RegPairEven) {
489     if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
490       // It's no longer possible to fulfill this hint. Return the default
491       // allocation order.
492       return RC->getRawAllocationOrder(MF);
493
494     if (!TFI->hasFP(MF)) {
495       if (!STI.isR9Reserved())
496         return makeArrayRef(GPREven1);
497       else
498         return makeArrayRef(GPREven4);
499     } else if (FramePtr == ARM::R7) {
500       if (!STI.isR9Reserved())
501         return makeArrayRef(GPREven2);
502       else
503         return makeArrayRef(GPREven5);
504     } else { // FramePtr == ARM::R11
505       if (!STI.isR9Reserved())
506         return makeArrayRef(GPREven3);
507       else
508         return makeArrayRef(GPREven6);
509     }
510   } else if (HintType == ARMRI::RegPairOdd) {
511     if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
512       // It's no longer possible to fulfill this hint. Return the default
513       // allocation order.
514       return RC->getRawAllocationOrder(MF);
515
516     if (!TFI->hasFP(MF)) {
517       if (!STI.isR9Reserved())
518         return makeArrayRef(GPROdd1);
519       else
520         return makeArrayRef(GPROdd4);
521     } else if (FramePtr == ARM::R7) {
522       if (!STI.isR9Reserved())
523         return makeArrayRef(GPROdd2);
524       else
525         return makeArrayRef(GPROdd5);
526     } else { // FramePtr == ARM::R11
527       if (!STI.isR9Reserved())
528         return makeArrayRef(GPROdd3);
529       else
530         return makeArrayRef(GPROdd6);
531     }
532   }
533   return RC->getRawAllocationOrder(MF);
534 }
535
536 /// ResolveRegAllocHint - Resolves the specified register allocation hint
537 /// to a physical register. Returns the physical register if it is successful.
538 unsigned
539 ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
540                                          const MachineFunction &MF) const {
541   if (Reg == 0 || !isPhysicalRegister(Reg))
542     return 0;
543   if (Type == 0)
544     return Reg;
545   else if (Type == (unsigned)ARMRI::RegPairOdd)
546     // Odd register.
547     return getRegisterPairOdd(Reg, MF);
548   else if (Type == (unsigned)ARMRI::RegPairEven)
549     // Even register.
550     return getRegisterPairEven(Reg, MF);
551   return 0;
552 }
553
554 void
555 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
556                                         MachineFunction &MF) const {
557   MachineRegisterInfo *MRI = &MF.getRegInfo();
558   std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
559   if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
560        Hint.first == (unsigned)ARMRI::RegPairEven) &&
561       TargetRegisterInfo::isVirtualRegister(Hint.second)) {
562     // If 'Reg' is one of the even / odd register pair and it's now changed
563     // (e.g. coalesced) into a different register. The other register of the
564     // pair allocation hint must be updated to reflect the relationship
565     // change.
566     unsigned OtherReg = Hint.second;
567     Hint = MRI->getRegAllocationHint(OtherReg);
568     if (Hint.second == Reg)
569       // Make sure the pair has not already divorced.
570       MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
571   }
572 }
573
574 bool
575 ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
576   // CortexA9 has a Write-after-write hazard for NEON registers.
577   if (!STI.isCortexA9())
578     return false;
579
580   switch (RC->getID()) {
581   case ARM::DPRRegClassID:
582   case ARM::DPR_8RegClassID:
583   case ARM::DPR_VFP2RegClassID:
584   case ARM::QPRRegClassID:
585   case ARM::QPR_8RegClassID:
586   case ARM::QPR_VFP2RegClassID:
587   case ARM::SPRRegClassID:
588   case ARM::SPR_8RegClassID:
589     // Avoid reusing S, D, and Q registers.
590     // Don't increase register pressure for QQ and QQQQ.
591     return true;
592   default:
593     return false;
594   }
595 }
596
597 bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
598   const MachineFrameInfo *MFI = MF.getFrameInfo();
599   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
600
601   if (!EnableBasePointer)
602     return false;
603
604   if (needsStackRealignment(MF) && MFI->hasVarSizedObjects())
605     return true;
606
607   // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
608   // negative range for ldr/str (255), and thumb1 is positive offsets only.
609   // It's going to be better to use the SP or Base Pointer instead. When there
610   // are variable sized objects, we can't reference off of the SP, so we
611   // reserve a Base Pointer.
612   if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) {
613     // Conservatively estimate whether the negative offset from the frame
614     // pointer will be sufficient to reach. If a function has a smallish
615     // frame, it's less likely to have lots of spills and callee saved
616     // space, so it's all more likely to be within range of the frame pointer.
617     // If it's wrong, the scavenger will still enable access to work, it just
618     // won't be optimal.
619     if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128)
620       return false;
621     return true;
622   }
623
624   return false;
625 }
626
627 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
628   const MachineFrameInfo *MFI = MF.getFrameInfo();
629   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
630   // We can't realign the stack if:
631   // 1. Dynamic stack realignment is explicitly disabled,
632   // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
633   // 3. There are VLAs in the function and the base pointer is disabled.
634   return (RealignStack && !AFI->isThumb1OnlyFunction() &&
635           (!MFI->hasVarSizedObjects() || EnableBasePointer));
636 }
637
638 bool ARMBaseRegisterInfo::
639 needsStackRealignment(const MachineFunction &MF) const {
640   const MachineFrameInfo *MFI = MF.getFrameInfo();
641   const Function *F = MF.getFunction();
642   unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
643   bool requiresRealignment = ((MFI->getLocalFrameMaxAlign() > StackAlign) ||
644                                F->hasFnAttr(Attribute::StackAlignment));
645
646   return requiresRealignment && canRealignStack(MF);
647 }
648
649 bool ARMBaseRegisterInfo::
650 cannotEliminateFrame(const MachineFunction &MF) const {
651   const MachineFrameInfo *MFI = MF.getFrameInfo();
652   if (DisableFramePointerElim(MF) && MFI->adjustsStack())
653     return true;
654   return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
655     || needsStackRealignment(MF);
656 }
657
658 unsigned
659 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
660   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
661
662   if (TFI->hasFP(MF))
663     return FramePtr;
664   return ARM::SP;
665 }
666
667 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
668   llvm_unreachable("What is the exception register");
669   return 0;
670 }
671
672 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
673   llvm_unreachable("What is the exception handler register");
674   return 0;
675 }
676
677 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
678                                               const MachineFunction &MF) const {
679   switch (Reg) {
680   default: break;
681   // Return 0 if either register of the pair is a special register.
682   // So no R12, etc.
683   case ARM::R1:
684     return ARM::R0;
685   case ARM::R3:
686     return ARM::R2;
687   case ARM::R5:
688     return ARM::R4;
689   case ARM::R7:
690     return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
691       ? 0 : ARM::R6;
692   case ARM::R9:
693     return isReservedReg(MF, ARM::R9)  ? 0 :ARM::R8;
694   case ARM::R11:
695     return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
696
697   case ARM::S1:
698     return ARM::S0;
699   case ARM::S3:
700     return ARM::S2;
701   case ARM::S5:
702     return ARM::S4;
703   case ARM::S7:
704     return ARM::S6;
705   case ARM::S9:
706     return ARM::S8;
707   case ARM::S11:
708     return ARM::S10;
709   case ARM::S13:
710     return ARM::S12;
711   case ARM::S15:
712     return ARM::S14;
713   case ARM::S17:
714     return ARM::S16;
715   case ARM::S19:
716     return ARM::S18;
717   case ARM::S21:
718     return ARM::S20;
719   case ARM::S23:
720     return ARM::S22;
721   case ARM::S25:
722     return ARM::S24;
723   case ARM::S27:
724     return ARM::S26;
725   case ARM::S29:
726     return ARM::S28;
727   case ARM::S31:
728     return ARM::S30;
729
730   case ARM::D1:
731     return ARM::D0;
732   case ARM::D3:
733     return ARM::D2;
734   case ARM::D5:
735     return ARM::D4;
736   case ARM::D7:
737     return ARM::D6;
738   case ARM::D9:
739     return ARM::D8;
740   case ARM::D11:
741     return ARM::D10;
742   case ARM::D13:
743     return ARM::D12;
744   case ARM::D15:
745     return ARM::D14;
746   case ARM::D17:
747     return ARM::D16;
748   case ARM::D19:
749     return ARM::D18;
750   case ARM::D21:
751     return ARM::D20;
752   case ARM::D23:
753     return ARM::D22;
754   case ARM::D25:
755     return ARM::D24;
756   case ARM::D27:
757     return ARM::D26;
758   case ARM::D29:
759     return ARM::D28;
760   case ARM::D31:
761     return ARM::D30;
762   }
763
764   return 0;
765 }
766
767 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
768                                              const MachineFunction &MF) const {
769   switch (Reg) {
770   default: break;
771   // Return 0 if either register of the pair is a special register.
772   // So no R12, etc.
773   case ARM::R0:
774     return ARM::R1;
775   case ARM::R2:
776     return ARM::R3;
777   case ARM::R4:
778     return ARM::R5;
779   case ARM::R6:
780     return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
781       ? 0 : ARM::R7;
782   case ARM::R8:
783     return isReservedReg(MF, ARM::R9)  ? 0 :ARM::R9;
784   case ARM::R10:
785     return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
786
787   case ARM::S0:
788     return ARM::S1;
789   case ARM::S2:
790     return ARM::S3;
791   case ARM::S4:
792     return ARM::S5;
793   case ARM::S6:
794     return ARM::S7;
795   case ARM::S8:
796     return ARM::S9;
797   case ARM::S10:
798     return ARM::S11;
799   case ARM::S12:
800     return ARM::S13;
801   case ARM::S14:
802     return ARM::S15;
803   case ARM::S16:
804     return ARM::S17;
805   case ARM::S18:
806     return ARM::S19;
807   case ARM::S20:
808     return ARM::S21;
809   case ARM::S22:
810     return ARM::S23;
811   case ARM::S24:
812     return ARM::S25;
813   case ARM::S26:
814     return ARM::S27;
815   case ARM::S28:
816     return ARM::S29;
817   case ARM::S30:
818     return ARM::S31;
819
820   case ARM::D0:
821     return ARM::D1;
822   case ARM::D2:
823     return ARM::D3;
824   case ARM::D4:
825     return ARM::D5;
826   case ARM::D6:
827     return ARM::D7;
828   case ARM::D8:
829     return ARM::D9;
830   case ARM::D10:
831     return ARM::D11;
832   case ARM::D12:
833     return ARM::D13;
834   case ARM::D14:
835     return ARM::D15;
836   case ARM::D16:
837     return ARM::D17;
838   case ARM::D18:
839     return ARM::D19;
840   case ARM::D20:
841     return ARM::D21;
842   case ARM::D22:
843     return ARM::D23;
844   case ARM::D24:
845     return ARM::D25;
846   case ARM::D26:
847     return ARM::D27;
848   case ARM::D28:
849     return ARM::D29;
850   case ARM::D30:
851     return ARM::D31;
852   }
853
854   return 0;
855 }
856
857 /// emitLoadConstPool - Emits a load from constpool to materialize the
858 /// specified immediate.
859 void ARMBaseRegisterInfo::
860 emitLoadConstPool(MachineBasicBlock &MBB,
861                   MachineBasicBlock::iterator &MBBI,
862                   DebugLoc dl,
863                   unsigned DestReg, unsigned SubIdx, int Val,
864                   ARMCC::CondCodes Pred,
865                   unsigned PredReg, unsigned MIFlags) const {
866   MachineFunction &MF = *MBB.getParent();
867   MachineConstantPool *ConstantPool = MF.getConstantPool();
868   const Constant *C =
869         ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
870   unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
871
872   BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
873     .addReg(DestReg, getDefRegState(true), SubIdx)
874     .addConstantPoolIndex(Idx)
875     .addImm(0).addImm(Pred).addReg(PredReg)
876     .setMIFlags(MIFlags);
877 }
878
879 bool ARMBaseRegisterInfo::
880 requiresRegisterScavenging(const MachineFunction &MF) const {
881   return true;
882 }
883
884 bool ARMBaseRegisterInfo::
885 requiresFrameIndexScavenging(const MachineFunction &MF) const {
886   return true;
887 }
888
889 bool ARMBaseRegisterInfo::
890 requiresVirtualBaseRegisters(const MachineFunction &MF) const {
891   return EnableLocalStackAlloc;
892 }
893
894 static void
895 emitSPUpdate(bool isARM,
896              MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
897              DebugLoc dl, const ARMBaseInstrInfo &TII,
898              int NumBytes,
899              ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
900   if (isARM)
901     emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
902                             Pred, PredReg, TII);
903   else
904     emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
905                            Pred, PredReg, TII);
906 }
907
908
909 void ARMBaseRegisterInfo::
910 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
911                               MachineBasicBlock::iterator I) const {
912   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
913   if (!TFI->hasReservedCallFrame(MF)) {
914     // If we have alloca, convert as follows:
915     // ADJCALLSTACKDOWN -> sub, sp, sp, amount
916     // ADJCALLSTACKUP   -> add, sp, sp, amount
917     MachineInstr *Old = I;
918     DebugLoc dl = Old->getDebugLoc();
919     unsigned Amount = Old->getOperand(0).getImm();
920     if (Amount != 0) {
921       // We need to keep the stack aligned properly.  To do this, we round the
922       // amount of space needed for the outgoing arguments up to the next
923       // alignment boundary.
924       unsigned Align = TFI->getStackAlignment();
925       Amount = (Amount+Align-1)/Align*Align;
926
927       ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
928       assert(!AFI->isThumb1OnlyFunction() &&
929              "This eliminateCallFramePseudoInstr does not support Thumb1!");
930       bool isARM = !AFI->isThumbFunction();
931
932       // Replace the pseudo instruction with a new instruction...
933       unsigned Opc = Old->getOpcode();
934       int PIdx = Old->findFirstPredOperandIdx();
935       ARMCC::CondCodes Pred = (PIdx == -1)
936         ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
937       if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
938         // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
939         unsigned PredReg = Old->getOperand(2).getReg();
940         emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
941       } else {
942         // Note: PredReg is operand 3 for ADJCALLSTACKUP.
943         unsigned PredReg = Old->getOperand(3).getReg();
944         assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
945         emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
946       }
947     }
948   }
949   MBB.erase(I);
950 }
951
952 int64_t ARMBaseRegisterInfo::
953 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
954   const MCInstrDesc &Desc = MI->getDesc();
955   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
956   int64_t InstrOffs = 0;;
957   int Scale = 1;
958   unsigned ImmIdx = 0;
959   switch (AddrMode) {
960   case ARMII::AddrModeT2_i8:
961   case ARMII::AddrModeT2_i12:
962   case ARMII::AddrMode_i12:
963     InstrOffs = MI->getOperand(Idx+1).getImm();
964     Scale = 1;
965     break;
966   case ARMII::AddrMode5: {
967     // VFP address mode.
968     const MachineOperand &OffOp = MI->getOperand(Idx+1);
969     InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
970     if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
971       InstrOffs = -InstrOffs;
972     Scale = 4;
973     break;
974   }
975   case ARMII::AddrMode2: {
976     ImmIdx = Idx+2;
977     InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
978     if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
979       InstrOffs = -InstrOffs;
980     break;
981   }
982   case ARMII::AddrMode3: {
983     ImmIdx = Idx+2;
984     InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
985     if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
986       InstrOffs = -InstrOffs;
987     break;
988   }
989   case ARMII::AddrModeT1_s: {
990     ImmIdx = Idx+1;
991     InstrOffs = MI->getOperand(ImmIdx).getImm();
992     Scale = 4;
993     break;
994   }
995   default:
996     llvm_unreachable("Unsupported addressing mode!");
997     break;
998   }
999
1000   return InstrOffs * Scale;
1001 }
1002
1003 /// needsFrameBaseReg - Returns true if the instruction's frame index
1004 /// reference would be better served by a base register other than FP
1005 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
1006 /// references it should create new base registers for.
1007 bool ARMBaseRegisterInfo::
1008 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
1009   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
1010     assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
1011   }
1012
1013   // It's the load/store FI references that cause issues, as it can be difficult
1014   // to materialize the offset if it won't fit in the literal field. Estimate
1015   // based on the size of the local frame and some conservative assumptions
1016   // about the rest of the stack frame (note, this is pre-regalloc, so
1017   // we don't know everything for certain yet) whether this offset is likely
1018   // to be out of range of the immediate. Return true if so.
1019
1020   // We only generate virtual base registers for loads and stores, so
1021   // return false for everything else.
1022   unsigned Opc = MI->getOpcode();
1023   switch (Opc) {
1024   case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
1025   case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
1026   case ARM::t2LDRi12: case ARM::t2LDRi8:
1027   case ARM::t2STRi12: case ARM::t2STRi8:
1028   case ARM::VLDRS: case ARM::VLDRD:
1029   case ARM::VSTRS: case ARM::VSTRD:
1030   case ARM::tSTRspi: case ARM::tLDRspi:
1031     if (ForceAllBaseRegAlloc)
1032       return true;
1033     break;
1034   default:
1035     return false;
1036   }
1037
1038   // Without a virtual base register, if the function has variable sized
1039   // objects, all fixed-size local references will be via the frame pointer,
1040   // Approximate the offset and see if it's legal for the instruction.
1041   // Note that the incoming offset is based on the SP value at function entry,
1042   // so it'll be negative.
1043   MachineFunction &MF = *MI->getParent()->getParent();
1044   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
1045   MachineFrameInfo *MFI = MF.getFrameInfo();
1046   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1047
1048   // Estimate an offset from the frame pointer.
1049   // Conservatively assume all callee-saved registers get pushed. R4-R6
1050   // will be earlier than the FP, so we ignore those.
1051   // R7, LR
1052   int64_t FPOffset = Offset - 8;
1053   // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
1054   if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
1055     FPOffset -= 80;
1056   // Estimate an offset from the stack pointer.
1057   // The incoming offset is relating to the SP at the start of the function,
1058   // but when we access the local it'll be relative to the SP after local
1059   // allocation, so adjust our SP-relative offset by that allocation size.
1060   Offset = -Offset;
1061   Offset += MFI->getLocalFrameSize();
1062   // Assume that we'll have at least some spill slots allocated.
1063   // FIXME: This is a total SWAG number. We should run some statistics
1064   //        and pick a real one.
1065   Offset += 128; // 128 bytes of spill slots
1066
1067   // If there is a frame pointer, try using it.
1068   // The FP is only available if there is no dynamic realignment. We
1069   // don't know for sure yet whether we'll need that, so we guess based
1070   // on whether there are any local variables that would trigger it.
1071   unsigned StackAlign = TFI->getStackAlignment();
1072   if (TFI->hasFP(MF) &&
1073       !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
1074     if (isFrameOffsetLegal(MI, FPOffset))
1075       return false;
1076   }
1077   // If we can reference via the stack pointer, try that.
1078   // FIXME: This (and the code that resolves the references) can be improved
1079   //        to only disallow SP relative references in the live range of
1080   //        the VLA(s). In practice, it's unclear how much difference that
1081   //        would make, but it may be worth doing.
1082   if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset))
1083     return false;
1084
1085   // The offset likely isn't legal, we want to allocate a virtual base register.
1086   return true;
1087 }
1088
1089 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
1090 /// be a pointer to FrameIdx at the beginning of the basic block.
1091 void ARMBaseRegisterInfo::
1092 materializeFrameBaseRegister(MachineBasicBlock *MBB,
1093                              unsigned BaseReg, int FrameIdx,
1094                              int64_t Offset) const {
1095   ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
1096   unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
1097     (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri);
1098
1099   MachineBasicBlock::iterator Ins = MBB->begin();
1100   DebugLoc DL;                  // Defaults to "unknown"
1101   if (Ins != MBB->end())
1102     DL = Ins->getDebugLoc();
1103
1104   const MCInstrDesc &MCID = TII.get(ADDriOpc);
1105   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1106   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this));
1107
1108   MachineInstrBuilder MIB = AddDefaultPred(BuildMI(*MBB, Ins, DL, MCID, BaseReg)
1109     .addFrameIndex(FrameIdx).addImm(Offset));
1110
1111   if (!AFI->isThumb1OnlyFunction())
1112     AddDefaultCC(MIB);
1113 }
1114
1115 void
1116 ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I,
1117                                        unsigned BaseReg, int64_t Offset) const {
1118   MachineInstr &MI = *I;
1119   MachineBasicBlock &MBB = *MI.getParent();
1120   MachineFunction &MF = *MBB.getParent();
1121   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1122   int Off = Offset; // ARM doesn't need the general 64-bit offsets
1123   unsigned i = 0;
1124
1125   assert(!AFI->isThumb1OnlyFunction() &&
1126          "This resolveFrameIndex does not support Thumb1!");
1127
1128   while (!MI.getOperand(i).isFI()) {
1129     ++i;
1130     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1131   }
1132   bool Done = false;
1133   if (!AFI->isThumbFunction())
1134     Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
1135   else {
1136     assert(AFI->isThumb2Function());
1137     Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
1138   }
1139   assert (Done && "Unable to resolve frame index!");
1140   (void)Done;
1141 }
1142
1143 bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
1144                                              int64_t Offset) const {
1145   const MCInstrDesc &Desc = MI->getDesc();
1146   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1147   unsigned i = 0;
1148
1149   while (!MI->getOperand(i).isFI()) {
1150     ++i;
1151     assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
1152   }
1153
1154   // AddrMode4 and AddrMode6 cannot handle any offset.
1155   if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
1156     return Offset == 0;
1157
1158   unsigned NumBits = 0;
1159   unsigned Scale = 1;
1160   bool isSigned = true;
1161   switch (AddrMode) {
1162   case ARMII::AddrModeT2_i8:
1163   case ARMII::AddrModeT2_i12:
1164     // i8 supports only negative, and i12 supports only positive, so
1165     // based on Offset sign, consider the appropriate instruction
1166     Scale = 1;
1167     if (Offset < 0) {
1168       NumBits = 8;
1169       Offset = -Offset;
1170     } else {
1171       NumBits = 12;
1172     }
1173     break;
1174   case ARMII::AddrMode5:
1175     // VFP address mode.
1176     NumBits = 8;
1177     Scale = 4;
1178     break;
1179   case ARMII::AddrMode_i12:
1180   case ARMII::AddrMode2:
1181     NumBits = 12;
1182     break;
1183   case ARMII::AddrMode3:
1184     NumBits = 8;
1185     break;
1186   case ARMII::AddrModeT1_s:
1187     NumBits = 5;
1188     Scale = 4;
1189     isSigned = false;
1190     break;
1191   default:
1192     llvm_unreachable("Unsupported addressing mode!");
1193     break;
1194   }
1195
1196   Offset += getFrameIndexInstrOffset(MI, i);
1197   // Make sure the offset is encodable for instructions that scale the
1198   // immediate.
1199   if ((Offset & (Scale-1)) != 0)
1200     return false;
1201
1202   if (isSigned && Offset < 0)
1203     Offset = -Offset;
1204
1205   unsigned Mask = (1 << NumBits) - 1;
1206   if ((unsigned)Offset <= Mask * Scale)
1207     return true;
1208
1209   return false;
1210 }
1211
1212 void
1213 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1214                                          int SPAdj, RegScavenger *RS) const {
1215   unsigned i = 0;
1216   MachineInstr &MI = *II;
1217   MachineBasicBlock &MBB = *MI.getParent();
1218   MachineFunction &MF = *MBB.getParent();
1219   const ARMFrameLowering *TFI =
1220     static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering());
1221   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1222   assert(!AFI->isThumb1OnlyFunction() &&
1223          "This eliminateFrameIndex does not support Thumb1!");
1224
1225   while (!MI.getOperand(i).isFI()) {
1226     ++i;
1227     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1228   }
1229
1230   int FrameIndex = MI.getOperand(i).getIndex();
1231   unsigned FrameReg;
1232
1233   int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
1234
1235   // Special handling of dbg_value instructions.
1236   if (MI.isDebugValue()) {
1237     MI.getOperand(i).  ChangeToRegister(FrameReg, false /*isDef*/);
1238     MI.getOperand(i+1).ChangeToImmediate(Offset);
1239     return;
1240   }
1241
1242   // Modify MI as necessary to handle as much of 'Offset' as possible
1243   bool Done = false;
1244   if (!AFI->isThumbFunction())
1245     Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1246   else {
1247     assert(AFI->isThumb2Function());
1248     Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1249   }
1250   if (Done)
1251     return;
1252
1253   // If we get here, the immediate doesn't fit into the instruction.  We folded
1254   // as much as possible above, handle the rest, providing a register that is
1255   // SP+LargeImm.
1256   assert((Offset ||
1257           (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
1258           (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
1259          "This code isn't needed if offset already handled!");
1260
1261   unsigned ScratchReg = 0;
1262   int PIdx = MI.findFirstPredOperandIdx();
1263   ARMCC::CondCodes Pred = (PIdx == -1)
1264     ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1265   unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1266   if (Offset == 0)
1267     // Must be addrmode4/6.
1268     MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
1269   else {
1270     ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1271     if (!AFI->isThumbFunction())
1272       emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1273                               Offset, Pred, PredReg, TII);
1274     else {
1275       assert(AFI->isThumb2Function());
1276       emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1277                              Offset, Pred, PredReg, TII);
1278     }
1279     // Update the original instruction to use the scratch register.
1280     MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1281   }
1282 }