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