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