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