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