1 //===-- ARMBaseRegisterInfo.cpp - ARM Register Information ----------------===//
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 "ARMBaseRegisterInfo.h"
15 #include "ARM.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMFrameLowering.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMSubtarget.h"
20 #include "MCTargetDesc/ARMAddressingModes.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/RegisterScavenging.h"
29 #include "llvm/CodeGen/VirtRegMap.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Target/TargetFrameLowering.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Target/TargetOptions.h"
40 
41 #define DEBUG_TYPE "arm-register-info"
42 
43 #define GET_REGINFO_TARGET_DESC
44 #include "ARMGenRegisterInfo.inc"
45 
46 using namespace llvm;
47 
48 ARMBaseRegisterInfo::ARMBaseRegisterInfo()
49     : ARMGenRegisterInfo(ARM::LR, 0, 0, ARM::PC), BasePtr(ARM::R6) {}
50 
51 static unsigned getFramePointerReg(const ARMSubtarget &STI) {
52   return STI.useR7AsFramePointer() ? ARM::R7 : ARM::R11;
53 }
54 
55 const MCPhysReg*
56 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
57   const ARMSubtarget &STI = MF->getSubtarget<ARMSubtarget>();
58   bool UseSplitPush = STI.splitFramePushPop(*MF);
59   const MCPhysReg *RegList =
60       STI.isTargetDarwin()
61           ? CSR_iOS_SaveList
62           : (UseSplitPush ? CSR_AAPCS_SplitPush_SaveList : CSR_AAPCS_SaveList);
63 
64   const Function *F = MF->getFunction();
65   if (F->getCallingConv() == CallingConv::GHC) {
66     // GHC set of callee saved regs is empty as all those regs are
67     // used for passing STG regs around
68     return CSR_NoRegs_SaveList;
69   } else if (F->hasFnAttribute("interrupt")) {
70     if (STI.isMClass()) {
71       // M-class CPUs have hardware which saves the registers needed to allow a
72       // function conforming to the AAPCS to function as a handler.
73       return UseSplitPush ? CSR_AAPCS_SplitPush_SaveList : CSR_AAPCS_SaveList;
74     } else if (F->getFnAttribute("interrupt").getValueAsString() == "FIQ") {
75       // Fast interrupt mode gives the handler a private copy of R8-R14, so less
76       // need to be saved to restore user-mode state.
77       return CSR_FIQ_SaveList;
78     } else {
79       // Generally only R13-R14 (i.e. SP, LR) are automatically preserved by
80       // exception handling.
81       return CSR_GenericInt_SaveList;
82     }
83   }
84 
85   if (STI.isTargetDarwin() && STI.getTargetLowering()->supportSwiftError() &&
86       F->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
87     return CSR_iOS_SwiftError_SaveList;
88 
89   if (STI.isTargetDarwin() && F->getCallingConv() == CallingConv::CXX_FAST_TLS)
90     return MF->getInfo<ARMFunctionInfo>()->isSplitCSR()
91                ? CSR_iOS_CXX_TLS_PE_SaveList
92                : CSR_iOS_CXX_TLS_SaveList;
93   return RegList;
94 }
95 
96 const MCPhysReg *ARMBaseRegisterInfo::getCalleeSavedRegsViaCopy(
97     const MachineFunction *MF) const {
98   assert(MF && "Invalid MachineFunction pointer.");
99   if (MF->getFunction()->getCallingConv() == CallingConv::CXX_FAST_TLS &&
100       MF->getInfo<ARMFunctionInfo>()->isSplitCSR())
101     return CSR_iOS_CXX_TLS_ViaCopy_SaveList;
102   return nullptr;
103 }
104 
105 const uint32_t *
106 ARMBaseRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
107                                           CallingConv::ID CC) const {
108   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
109   if (CC == CallingConv::GHC)
110     // This is academic becase all GHC calls are (supposed to be) tail calls
111     return CSR_NoRegs_RegMask;
112 
113   if (STI.isTargetDarwin() && STI.getTargetLowering()->supportSwiftError() &&
114       MF.getFunction()->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
115     return CSR_iOS_SwiftError_RegMask;
116 
117   if (STI.isTargetDarwin() && CC == CallingConv::CXX_FAST_TLS)
118     return CSR_iOS_CXX_TLS_RegMask;
119   return STI.isTargetDarwin() ? CSR_iOS_RegMask : CSR_AAPCS_RegMask;
120 }
121 
122 const uint32_t*
123 ARMBaseRegisterInfo::getNoPreservedMask() const {
124   return CSR_NoRegs_RegMask;
125 }
126 
127 const uint32_t *
128 ARMBaseRegisterInfo::getTLSCallPreservedMask(const MachineFunction &MF) const {
129   assert(MF.getSubtarget<ARMSubtarget>().isTargetDarwin() &&
130          "only know about special TLS call on Darwin");
131   return CSR_iOS_TLSCall_RegMask;
132 }
133 
134 
135 const uint32_t *
136 ARMBaseRegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
137                                                 CallingConv::ID CC) const {
138   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
139   // This should return a register mask that is the same as that returned by
140   // getCallPreservedMask but that additionally preserves the register used for
141   // the first i32 argument (which must also be the register used to return a
142   // single i32 return value)
143   //
144   // In case that the calling convention does not use the same register for
145   // both or otherwise does not want to enable this optimization, the function
146   // should return NULL
147   if (CC == CallingConv::GHC)
148     // This is academic becase all GHC calls are (supposed to be) tail calls
149     return nullptr;
150   return STI.isTargetDarwin() ? CSR_iOS_ThisReturn_RegMask
151                               : CSR_AAPCS_ThisReturn_RegMask;
152 }
153 
154 BitVector ARMBaseRegisterInfo::
155 getReservedRegs(const MachineFunction &MF) const {
156   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
157   const ARMFrameLowering *TFI = getFrameLowering(MF);
158 
159   // FIXME: avoid re-calculating this every time.
160   BitVector Reserved(getNumRegs());
161   Reserved.set(ARM::SP);
162   Reserved.set(ARM::PC);
163   Reserved.set(ARM::FPSCR);
164   Reserved.set(ARM::APSR_NZCV);
165   if (TFI->hasFP(MF))
166     Reserved.set(getFramePointerReg(STI));
167   if (hasBasePointer(MF))
168     Reserved.set(BasePtr);
169   // Some targets reserve R9.
170   if (STI.isR9Reserved())
171     Reserved.set(ARM::R9);
172   // Reserve D16-D31 if the subtarget doesn't support them.
173   if (!STI.hasVFP3() || STI.hasD16()) {
174     static_assert(ARM::D31 == ARM::D16 + 15, "Register list not consecutive!");
175     Reserved.set(ARM::D16, ARM::D31 + 1);
176   }
177   const TargetRegisterClass *RC  = &ARM::GPRPairRegClass;
178   for(TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); I!=E; ++I)
179     for (MCSubRegIterator SI(*I, this); SI.isValid(); ++SI)
180       if (Reserved.test(*SI)) Reserved.set(*I);
181 
182   return Reserved;
183 }
184 
185 const TargetRegisterClass *
186 ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
187                                                const MachineFunction &) const {
188   const TargetRegisterClass *Super = RC;
189   TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
190   do {
191     switch (Super->getID()) {
192     case ARM::GPRRegClassID:
193     case ARM::SPRRegClassID:
194     case ARM::DPRRegClassID:
195     case ARM::QPRRegClassID:
196     case ARM::QQPRRegClassID:
197     case ARM::QQQQPRRegClassID:
198     case ARM::GPRPairRegClassID:
199       return Super;
200     }
201     Super = *I++;
202   } while (Super);
203   return RC;
204 }
205 
206 const TargetRegisterClass *
207 ARMBaseRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
208                                                                          const {
209   return &ARM::GPRRegClass;
210 }
211 
212 const TargetRegisterClass *
213 ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
214   if (RC == &ARM::CCRRegClass)
215     return &ARM::rGPRRegClass;  // Can't copy CCR registers.
216   return RC;
217 }
218 
219 unsigned
220 ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
221                                          MachineFunction &MF) const {
222   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
223   const ARMFrameLowering *TFI = getFrameLowering(MF);
224 
225   switch (RC->getID()) {
226   default:
227     return 0;
228   case ARM::tGPRRegClassID:
229     return TFI->hasFP(MF) ? 4 : 5;
230   case ARM::GPRRegClassID: {
231     unsigned FP = TFI->hasFP(MF) ? 1 : 0;
232     return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
233   }
234   case ARM::SPRRegClassID:  // Currently not used as 'rep' register class.
235   case ARM::DPRRegClassID:
236     return 32 - 10;
237   }
238 }
239 
240 // Get the other register in a GPRPair.
241 static unsigned getPairedGPR(unsigned Reg, bool Odd, const MCRegisterInfo *RI) {
242   for (MCSuperRegIterator Supers(Reg, RI); Supers.isValid(); ++Supers)
243     if (ARM::GPRPairRegClass.contains(*Supers))
244       return RI->getSubReg(*Supers, Odd ? ARM::gsub_1 : ARM::gsub_0);
245   return 0;
246 }
247 
248 // Resolve the RegPairEven / RegPairOdd register allocator hints.
249 void
250 ARMBaseRegisterInfo::getRegAllocationHints(unsigned VirtReg,
251                                            ArrayRef<MCPhysReg> Order,
252                                            SmallVectorImpl<MCPhysReg> &Hints,
253                                            const MachineFunction &MF,
254                                            const VirtRegMap *VRM,
255                                            const LiveRegMatrix *Matrix) const {
256   const MachineRegisterInfo &MRI = MF.getRegInfo();
257   std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg);
258 
259   unsigned Odd;
260   switch (Hint.first) {
261   case ARMRI::RegPairEven:
262     Odd = 0;
263     break;
264   case ARMRI::RegPairOdd:
265     Odd = 1;
266     break;
267   default:
268     TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
269     return;
270   }
271 
272   // This register should preferably be even (Odd == 0) or odd (Odd == 1).
273   // Check if the other part of the pair has already been assigned, and provide
274   // the paired register as the first hint.
275   unsigned Paired = Hint.second;
276   if (Paired == 0)
277     return;
278 
279   unsigned PairedPhys = 0;
280   if (TargetRegisterInfo::isPhysicalRegister(Paired)) {
281     PairedPhys = Paired;
282   } else if (VRM && VRM->hasPhys(Paired)) {
283     PairedPhys = getPairedGPR(VRM->getPhys(Paired), Odd, this);
284   }
285 
286   // First prefer the paired physreg.
287   if (PairedPhys && is_contained(Order, PairedPhys))
288     Hints.push_back(PairedPhys);
289 
290   // Then prefer even or odd registers.
291   for (unsigned I = 0, E = Order.size(); I != E; ++I) {
292     unsigned Reg = Order[I];
293     if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd)
294       continue;
295     // Don't provide hints that are paired to a reserved register.
296     unsigned Paired = getPairedGPR(Reg, !Odd, this);
297     if (!Paired || MRI.isReserved(Paired))
298       continue;
299     Hints.push_back(Reg);
300   }
301 }
302 
303 void
304 ARMBaseRegisterInfo::updateRegAllocHint(unsigned Reg, unsigned NewReg,
305                                         MachineFunction &MF) const {
306   MachineRegisterInfo *MRI = &MF.getRegInfo();
307   std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
308   if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
309        Hint.first == (unsigned)ARMRI::RegPairEven) &&
310       TargetRegisterInfo::isVirtualRegister(Hint.second)) {
311     // If 'Reg' is one of the even / odd register pair and it's now changed
312     // (e.g. coalesced) into a different register. The other register of the
313     // pair allocation hint must be updated to reflect the relationship
314     // change.
315     unsigned OtherReg = Hint.second;
316     Hint = MRI->getRegAllocationHint(OtherReg);
317     // Make sure the pair has not already divorced.
318     if (Hint.second == Reg) {
319       MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
320       if (TargetRegisterInfo::isVirtualRegister(NewReg))
321         MRI->setRegAllocationHint(NewReg,
322             Hint.first == (unsigned)ARMRI::RegPairOdd ? ARMRI::RegPairEven
323             : ARMRI::RegPairOdd, OtherReg);
324     }
325   }
326 }
327 
328 bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
329   const MachineFrameInfo &MFI = MF.getFrameInfo();
330   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
331   const ARMFrameLowering *TFI = getFrameLowering(MF);
332 
333   // When outgoing call frames are so large that we adjust the stack pointer
334   // around the call, we can no longer use the stack pointer to reach the
335   // emergency spill slot.
336   if (needsStackRealignment(MF) && !TFI->hasReservedCallFrame(MF))
337     return true;
338 
339   // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
340   // negative range for ldr/str (255), and thumb1 is positive offsets only.
341   // It's going to be better to use the SP or Base Pointer instead. When there
342   // are variable sized objects, we can't reference off of the SP, so we
343   // reserve a Base Pointer.
344   if (AFI->isThumbFunction() && MFI.hasVarSizedObjects()) {
345     // Conservatively estimate whether the negative offset from the frame
346     // pointer will be sufficient to reach. If a function has a smallish
347     // frame, it's less likely to have lots of spills and callee saved
348     // space, so it's all more likely to be within range of the frame pointer.
349     // If it's wrong, the scavenger will still enable access to work, it just
350     // won't be optimal.
351     if (AFI->isThumb2Function() && MFI.getLocalFrameSize() < 128)
352       return false;
353     return true;
354   }
355 
356   return false;
357 }
358 
359 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
360   const MachineRegisterInfo *MRI = &MF.getRegInfo();
361   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
362   const ARMFrameLowering *TFI = getFrameLowering(MF);
363   // We can't realign the stack if:
364   // 1. Dynamic stack realignment is explicitly disabled,
365   // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
366   // 3. There are VLAs in the function and the base pointer is disabled.
367   if (!TargetRegisterInfo::canRealignStack(MF))
368     return false;
369   if (AFI->isThumb1OnlyFunction())
370     return false;
371   // Stack realignment requires a frame pointer.  If we already started
372   // register allocation with frame pointer elimination, it is too late now.
373   if (!MRI->canReserveReg(getFramePointerReg(MF.getSubtarget<ARMSubtarget>())))
374     return false;
375   // We may also need a base pointer if there are dynamic allocas or stack
376   // pointer adjustments around calls.
377   if (TFI->hasReservedCallFrame(MF))
378     return true;
379   // A base pointer is required and allowed.  Check that it isn't too late to
380   // reserve it.
381   return MRI->canReserveReg(BasePtr);
382 }
383 
384 bool ARMBaseRegisterInfo::
385 cannotEliminateFrame(const MachineFunction &MF) const {
386   const MachineFrameInfo &MFI = MF.getFrameInfo();
387   if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
388     return true;
389   return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken()
390     || needsStackRealignment(MF);
391 }
392 
393 unsigned
394 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
395   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
396   const ARMFrameLowering *TFI = getFrameLowering(MF);
397 
398   if (TFI->hasFP(MF))
399     return getFramePointerReg(STI);
400   return ARM::SP;
401 }
402 
403 /// emitLoadConstPool - Emits a load from constpool to materialize the
404 /// specified immediate.
405 void ARMBaseRegisterInfo::emitLoadConstPool(
406     MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
407     const DebugLoc &dl, unsigned DestReg, unsigned SubIdx, int Val,
408     ARMCC::CondCodes Pred, unsigned PredReg, unsigned MIFlags) const {
409   MachineFunction &MF = *MBB.getParent();
410   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
411   MachineConstantPool *ConstantPool = MF.getConstantPool();
412   const Constant *C =
413         ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
414   unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
415 
416   BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
417     .addReg(DestReg, getDefRegState(true), SubIdx)
418     .addConstantPoolIndex(Idx)
419     .addImm(0).addImm(Pred).addReg(PredReg)
420     .setMIFlags(MIFlags);
421 }
422 
423 bool ARMBaseRegisterInfo::
424 requiresRegisterScavenging(const MachineFunction &MF) const {
425   return true;
426 }
427 
428 bool ARMBaseRegisterInfo::
429 trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
430   return true;
431 }
432 
433 bool ARMBaseRegisterInfo::
434 requiresFrameIndexScavenging(const MachineFunction &MF) const {
435   return true;
436 }
437 
438 bool ARMBaseRegisterInfo::
439 requiresVirtualBaseRegisters(const MachineFunction &MF) const {
440   return true;
441 }
442 
443 int64_t ARMBaseRegisterInfo::
444 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
445   const MCInstrDesc &Desc = MI->getDesc();
446   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
447   int64_t InstrOffs = 0;
448   int Scale = 1;
449   unsigned ImmIdx = 0;
450   switch (AddrMode) {
451   case ARMII::AddrModeT2_i8:
452   case ARMII::AddrModeT2_i12:
453   case ARMII::AddrMode_i12:
454     InstrOffs = MI->getOperand(Idx+1).getImm();
455     Scale = 1;
456     break;
457   case ARMII::AddrMode5: {
458     // VFP address mode.
459     const MachineOperand &OffOp = MI->getOperand(Idx+1);
460     InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
461     if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
462       InstrOffs = -InstrOffs;
463     Scale = 4;
464     break;
465   }
466   case ARMII::AddrMode2: {
467     ImmIdx = Idx+2;
468     InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
469     if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
470       InstrOffs = -InstrOffs;
471     break;
472   }
473   case ARMII::AddrMode3: {
474     ImmIdx = Idx+2;
475     InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
476     if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
477       InstrOffs = -InstrOffs;
478     break;
479   }
480   case ARMII::AddrModeT1_s: {
481     ImmIdx = Idx+1;
482     InstrOffs = MI->getOperand(ImmIdx).getImm();
483     Scale = 4;
484     break;
485   }
486   default:
487     llvm_unreachable("Unsupported addressing mode!");
488   }
489 
490   return InstrOffs * Scale;
491 }
492 
493 /// needsFrameBaseReg - Returns true if the instruction's frame index
494 /// reference would be better served by a base register other than FP
495 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
496 /// references it should create new base registers for.
497 bool ARMBaseRegisterInfo::
498 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
499   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
500     assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
501   }
502 
503   // It's the load/store FI references that cause issues, as it can be difficult
504   // to materialize the offset if it won't fit in the literal field. Estimate
505   // based on the size of the local frame and some conservative assumptions
506   // about the rest of the stack frame (note, this is pre-regalloc, so
507   // we don't know everything for certain yet) whether this offset is likely
508   // to be out of range of the immediate. Return true if so.
509 
510   // We only generate virtual base registers for loads and stores, so
511   // return false for everything else.
512   unsigned Opc = MI->getOpcode();
513   switch (Opc) {
514   case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
515   case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
516   case ARM::t2LDRi12: case ARM::t2LDRi8:
517   case ARM::t2STRi12: case ARM::t2STRi8:
518   case ARM::VLDRS: case ARM::VLDRD:
519   case ARM::VSTRS: case ARM::VSTRD:
520   case ARM::tSTRspi: case ARM::tLDRspi:
521     break;
522   default:
523     return false;
524   }
525 
526   // Without a virtual base register, if the function has variable sized
527   // objects, all fixed-size local references will be via the frame pointer,
528   // Approximate the offset and see if it's legal for the instruction.
529   // Note that the incoming offset is based on the SP value at function entry,
530   // so it'll be negative.
531   MachineFunction &MF = *MI->getParent()->getParent();
532   const ARMFrameLowering *TFI = getFrameLowering(MF);
533   MachineFrameInfo &MFI = MF.getFrameInfo();
534   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
535 
536   // Estimate an offset from the frame pointer.
537   // Conservatively assume all callee-saved registers get pushed. R4-R6
538   // will be earlier than the FP, so we ignore those.
539   // R7, LR
540   int64_t FPOffset = Offset - 8;
541   // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
542   if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
543     FPOffset -= 80;
544   // Estimate an offset from the stack pointer.
545   // The incoming offset is relating to the SP at the start of the function,
546   // but when we access the local it'll be relative to the SP after local
547   // allocation, so adjust our SP-relative offset by that allocation size.
548   Offset += MFI.getLocalFrameSize();
549   // Assume that we'll have at least some spill slots allocated.
550   // FIXME: This is a total SWAG number. We should run some statistics
551   //        and pick a real one.
552   Offset += 128; // 128 bytes of spill slots
553 
554   // If there's a frame pointer and the addressing mode allows it, try using it.
555   // The FP is only available if there is no dynamic realignment. We
556   // don't know for sure yet whether we'll need that, so we guess based
557   // on whether there are any local variables that would trigger it.
558   unsigned StackAlign = TFI->getStackAlignment();
559   if (TFI->hasFP(MF) &&
560       !((MFI.getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
561     if (isFrameOffsetLegal(MI, getFrameRegister(MF), FPOffset))
562       return false;
563   }
564   // If we can reference via the stack pointer, try that.
565   // FIXME: This (and the code that resolves the references) can be improved
566   //        to only disallow SP relative references in the live range of
567   //        the VLA(s). In practice, it's unclear how much difference that
568   //        would make, but it may be worth doing.
569   if (!MFI.hasVarSizedObjects() && isFrameOffsetLegal(MI, ARM::SP, Offset))
570     return false;
571 
572   // The offset likely isn't legal, we want to allocate a virtual base register.
573   return true;
574 }
575 
576 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
577 /// be a pointer to FrameIdx at the beginning of the basic block.
578 void ARMBaseRegisterInfo::
579 materializeFrameBaseRegister(MachineBasicBlock *MBB,
580                              unsigned BaseReg, int FrameIdx,
581                              int64_t Offset) const {
582   ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
583   unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
584     (AFI->isThumb1OnlyFunction() ? ARM::tADDframe : ARM::t2ADDri);
585 
586   MachineBasicBlock::iterator Ins = MBB->begin();
587   DebugLoc DL;                  // Defaults to "unknown"
588   if (Ins != MBB->end())
589     DL = Ins->getDebugLoc();
590 
591   const MachineFunction &MF = *MBB->getParent();
592   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
593   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
594   const MCInstrDesc &MCID = TII.get(ADDriOpc);
595   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
596 
597   MachineInstrBuilder MIB = BuildMI(*MBB, Ins, DL, MCID, BaseReg)
598     .addFrameIndex(FrameIdx).addImm(Offset);
599 
600   if (!AFI->isThumb1OnlyFunction())
601     AddDefaultCC(AddDefaultPred(MIB));
602 }
603 
604 void ARMBaseRegisterInfo::resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
605                                             int64_t Offset) const {
606   MachineBasicBlock &MBB = *MI.getParent();
607   MachineFunction &MF = *MBB.getParent();
608   const ARMBaseInstrInfo &TII =
609       *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
610   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
611   int Off = Offset; // ARM doesn't need the general 64-bit offsets
612   unsigned i = 0;
613 
614   assert(!AFI->isThumb1OnlyFunction() &&
615          "This resolveFrameIndex does not support Thumb1!");
616 
617   while (!MI.getOperand(i).isFI()) {
618     ++i;
619     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
620   }
621   bool Done = false;
622   if (!AFI->isThumbFunction())
623     Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
624   else {
625     assert(AFI->isThumb2Function());
626     Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
627   }
628   assert (Done && "Unable to resolve frame index!");
629   (void)Done;
630 }
631 
632 bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI, unsigned BaseReg,
633                                              int64_t Offset) const {
634   const MCInstrDesc &Desc = MI->getDesc();
635   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
636   unsigned i = 0;
637 
638   while (!MI->getOperand(i).isFI()) {
639     ++i;
640     assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
641   }
642 
643   // AddrMode4 and AddrMode6 cannot handle any offset.
644   if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
645     return Offset == 0;
646 
647   unsigned NumBits = 0;
648   unsigned Scale = 1;
649   bool isSigned = true;
650   switch (AddrMode) {
651   case ARMII::AddrModeT2_i8:
652   case ARMII::AddrModeT2_i12:
653     // i8 supports only negative, and i12 supports only positive, so
654     // based on Offset sign, consider the appropriate instruction
655     Scale = 1;
656     if (Offset < 0) {
657       NumBits = 8;
658       Offset = -Offset;
659     } else {
660       NumBits = 12;
661     }
662     break;
663   case ARMII::AddrMode5:
664     // VFP address mode.
665     NumBits = 8;
666     Scale = 4;
667     break;
668   case ARMII::AddrMode_i12:
669   case ARMII::AddrMode2:
670     NumBits = 12;
671     break;
672   case ARMII::AddrMode3:
673     NumBits = 8;
674     break;
675   case ARMII::AddrModeT1_s:
676     NumBits = (BaseReg == ARM::SP ? 8 : 5);
677     Scale = 4;
678     isSigned = false;
679     break;
680   default:
681     llvm_unreachable("Unsupported addressing mode!");
682   }
683 
684   Offset += getFrameIndexInstrOffset(MI, i);
685   // Make sure the offset is encodable for instructions that scale the
686   // immediate.
687   if ((Offset & (Scale-1)) != 0)
688     return false;
689 
690   if (isSigned && Offset < 0)
691     Offset = -Offset;
692 
693   unsigned Mask = (1 << NumBits) - 1;
694   if ((unsigned)Offset <= Mask * Scale)
695     return true;
696 
697   return false;
698 }
699 
700 void
701 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
702                                          int SPAdj, unsigned FIOperandNum,
703                                          RegScavenger *RS) const {
704   MachineInstr &MI = *II;
705   MachineBasicBlock &MBB = *MI.getParent();
706   MachineFunction &MF = *MBB.getParent();
707   const ARMBaseInstrInfo &TII =
708       *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
709   const ARMFrameLowering *TFI = getFrameLowering(MF);
710   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
711   assert(!AFI->isThumb1OnlyFunction() &&
712          "This eliminateFrameIndex does not support Thumb1!");
713   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
714   unsigned FrameReg;
715 
716   int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
717 
718   // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
719   // call frame setup/destroy instructions have already been eliminated.  That
720   // means the stack pointer cannot be used to access the emergency spill slot
721   // when !hasReservedCallFrame().
722 #ifndef NDEBUG
723   if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){
724     assert(TFI->hasReservedCallFrame(MF) &&
725            "Cannot use SP to access the emergency spill slot in "
726            "functions without a reserved call frame");
727     assert(!MF.getFrameInfo().hasVarSizedObjects() &&
728            "Cannot use SP to access the emergency spill slot in "
729            "functions with variable sized frame objects");
730   }
731 #endif // NDEBUG
732 
733   assert(!MI.isDebugValue() && "DBG_VALUEs should be handled in target-independent code");
734 
735   // Modify MI as necessary to handle as much of 'Offset' as possible
736   bool Done = false;
737   if (!AFI->isThumbFunction())
738     Done = rewriteARMFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
739   else {
740     assert(AFI->isThumb2Function());
741     Done = rewriteT2FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
742   }
743   if (Done)
744     return;
745 
746   // If we get here, the immediate doesn't fit into the instruction.  We folded
747   // as much as possible above, handle the rest, providing a register that is
748   // SP+LargeImm.
749   assert((Offset ||
750           (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
751           (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
752          "This code isn't needed if offset already handled!");
753 
754   unsigned ScratchReg = 0;
755   int PIdx = MI.findFirstPredOperandIdx();
756   ARMCC::CondCodes Pred = (PIdx == -1)
757     ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
758   unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
759   if (Offset == 0)
760     // Must be addrmode4/6.
761     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false, false);
762   else {
763     ScratchReg = MF.getRegInfo().createVirtualRegister(&ARM::GPRRegClass);
764     if (!AFI->isThumbFunction())
765       emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
766                               Offset, Pred, PredReg, TII);
767     else {
768       assert(AFI->isThumb2Function());
769       emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
770                              Offset, Pred, PredReg, TII);
771     }
772     // Update the original instruction to use the scratch register.
773     MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false,true);
774   }
775 }
776 
777 bool ARMBaseRegisterInfo::shouldCoalesce(MachineInstr *MI,
778                                   const TargetRegisterClass *SrcRC,
779                                   unsigned SubReg,
780                                   const TargetRegisterClass *DstRC,
781                                   unsigned DstSubReg,
782                                   const TargetRegisterClass *NewRC) const {
783   auto MBB = MI->getParent();
784   auto MF = MBB->getParent();
785   const MachineRegisterInfo &MRI = MF->getRegInfo();
786   // If not copying into a sub-register this should be ok because we shouldn't
787   // need to split the reg.
788   if (!DstSubReg)
789     return true;
790   // Small registers don't frequently cause a problem, so we can coalesce them.
791   if (NewRC->getSize() < 32 && DstRC->getSize() < 32 && SrcRC->getSize() < 32)
792     return true;
793 
794   auto NewRCWeight =
795               MRI.getTargetRegisterInfo()->getRegClassWeight(NewRC);
796   auto SrcRCWeight =
797               MRI.getTargetRegisterInfo()->getRegClassWeight(SrcRC);
798   auto DstRCWeight =
799               MRI.getTargetRegisterInfo()->getRegClassWeight(DstRC);
800   // If the source register class is more expensive than the destination, the
801   // coalescing is probably profitable.
802   if (SrcRCWeight.RegWeight > NewRCWeight.RegWeight)
803     return true;
804   if (DstRCWeight.RegWeight > NewRCWeight.RegWeight)
805     return true;
806 
807   // If the register allocator isn't constrained, we can always allow coalescing
808   // unfortunately we don't know yet if we will be constrained.
809   // The goal of this heuristic is to restrict how many expensive registers
810   // we allow to coalesce in a given basic block.
811   auto AFI = MF->getInfo<ARMFunctionInfo>();
812   auto It = AFI->getCoalescedWeight(MBB);
813 
814   DEBUG(dbgs() << "\tARM::shouldCoalesce - Coalesced Weight: "
815     << It->second << "\n");
816   DEBUG(dbgs() << "\tARM::shouldCoalesce - Reg Weight: "
817     << NewRCWeight.RegWeight << "\n");
818 
819   // This number is the largest round number that which meets the criteria:
820   //  (1) addresses PR18825
821   //  (2) generates better code in some test cases (like vldm-shed-a9.ll)
822   //  (3) Doesn't regress any test cases (in-tree, test-suite, and SPEC)
823   // In practice the SizeMultiplier will only factor in for straight line code
824   // that uses a lot of NEON vectors, which isn't terribly common.
825   unsigned SizeMultiplier = MBB->size()/100;
826   SizeMultiplier = SizeMultiplier ? SizeMultiplier : 1;
827   if (It->second < NewRCWeight.WeightLimit * SizeMultiplier) {
828     It->second += NewRCWeight.RegWeight;
829     return true;
830   }
831   return false;
832 }
833