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