10b57cec5SDimitry Andric //===-- ARMBaseRegisterInfo.cpp - ARM Register Information ----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the base ARM implementation of TargetRegisterInfo class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "ARMBaseRegisterInfo.h"
140b57cec5SDimitry Andric #include "ARM.h"
150b57cec5SDimitry Andric #include "ARMBaseInstrInfo.h"
160b57cec5SDimitry Andric #include "ARMFrameLowering.h"
170b57cec5SDimitry Andric #include "ARMMachineFunctionInfo.h"
180b57cec5SDimitry Andric #include "ARMSubtarget.h"
190b57cec5SDimitry Andric #include "MCTargetDesc/ARMAddressingModes.h"
200b57cec5SDimitry Andric #include "MCTargetDesc/ARMBaseInfo.h"
210b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
220b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
230b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterScavenging.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
360b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
370b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
380b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
390b57cec5SDimitry Andric #include "llvm/IR/Function.h"
400b57cec5SDimitry Andric #include "llvm/IR/Type.h"
410b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
420b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
430b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
440b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
450b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
460b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
470b57cec5SDimitry Andric #include <cassert>
480b57cec5SDimitry Andric #include <utility>
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric #define DEBUG_TYPE "arm-register-info"
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric #define GET_REGINFO_TARGET_DESC
530b57cec5SDimitry Andric #include "ARMGenRegisterInfo.inc"
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric using namespace llvm;
560b57cec5SDimitry Andric 
ARMBaseRegisterInfo()570b57cec5SDimitry Andric ARMBaseRegisterInfo::ARMBaseRegisterInfo()
58e8d8bef9SDimitry Andric     : ARMGenRegisterInfo(ARM::LR, 0, 0, ARM::PC) {
59e8d8bef9SDimitry Andric   ARM_MC::initLLVMToCVRegMapping(this);
60e8d8bef9SDimitry Andric }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric const MCPhysReg*
getCalleeSavedRegs(const MachineFunction * MF) const630b57cec5SDimitry Andric ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
640b57cec5SDimitry Andric   const ARMSubtarget &STI = MF->getSubtarget<ARMSubtarget>();
650b57cec5SDimitry Andric   bool UseSplitPush = STI.splitFramePushPop(*MF);
660b57cec5SDimitry Andric   const Function &F = MF->getFunction();
6781ad6265SDimitry Andric 
680b57cec5SDimitry Andric   if (F.getCallingConv() == CallingConv::GHC) {
690b57cec5SDimitry Andric     // GHC set of callee saved regs is empty as all those regs are
700b57cec5SDimitry Andric     // used for passing STG regs around
710b57cec5SDimitry Andric     return CSR_NoRegs_SaveList;
7281ad6265SDimitry Andric   } else if (STI.splitFramePointerPush(*MF)) {
7381ad6265SDimitry Andric     return CSR_Win_SplitFP_SaveList;
74480093f4SDimitry Andric   } else if (F.getCallingConv() == CallingConv::CFGuard_Check) {
75480093f4SDimitry Andric     return CSR_Win_AAPCS_CFGuard_Check_SaveList;
76fe6060f1SDimitry Andric   } else if (F.getCallingConv() == CallingConv::SwiftTail) {
77fe6060f1SDimitry Andric     return STI.isTargetDarwin()
78fe6060f1SDimitry Andric                ? CSR_iOS_SwiftTail_SaveList
7981ad6265SDimitry Andric                : (UseSplitPush ? CSR_ATPCS_SplitPush_SwiftTail_SaveList
80fe6060f1SDimitry Andric                                : CSR_AAPCS_SwiftTail_SaveList);
810b57cec5SDimitry Andric   } else if (F.hasFnAttribute("interrupt")) {
820b57cec5SDimitry Andric     if (STI.isMClass()) {
830b57cec5SDimitry Andric       // M-class CPUs have hardware which saves the registers needed to allow a
840b57cec5SDimitry Andric       // function conforming to the AAPCS to function as a handler.
8581ad6265SDimitry Andric       return UseSplitPush ? CSR_ATPCS_SplitPush_SaveList : CSR_AAPCS_SaveList;
860b57cec5SDimitry Andric     } else if (F.getFnAttribute("interrupt").getValueAsString() == "FIQ") {
870b57cec5SDimitry Andric       // Fast interrupt mode gives the handler a private copy of R8-R14, so less
880b57cec5SDimitry Andric       // need to be saved to restore user-mode state.
890b57cec5SDimitry Andric       return CSR_FIQ_SaveList;
900b57cec5SDimitry Andric     } else {
910b57cec5SDimitry Andric       // Generally only R13-R14 (i.e. SP, LR) are automatically preserved by
920b57cec5SDimitry Andric       // exception handling.
930b57cec5SDimitry Andric       return CSR_GenericInt_SaveList;
940b57cec5SDimitry Andric     }
950b57cec5SDimitry Andric   }
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   if (STI.getTargetLowering()->supportSwiftError() &&
980b57cec5SDimitry Andric       F.getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
990b57cec5SDimitry Andric     if (STI.isTargetDarwin())
1000b57cec5SDimitry Andric       return CSR_iOS_SwiftError_SaveList;
1010b57cec5SDimitry Andric 
10281ad6265SDimitry Andric     return UseSplitPush ? CSR_ATPCS_SplitPush_SwiftError_SaveList :
1030b57cec5SDimitry Andric       CSR_AAPCS_SwiftError_SaveList;
1040b57cec5SDimitry Andric   }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   if (STI.isTargetDarwin() && F.getCallingConv() == CallingConv::CXX_FAST_TLS)
1070b57cec5SDimitry Andric     return MF->getInfo<ARMFunctionInfo>()->isSplitCSR()
1080b57cec5SDimitry Andric                ? CSR_iOS_CXX_TLS_PE_SaveList
1090b57cec5SDimitry Andric                : CSR_iOS_CXX_TLS_SaveList;
11081ad6265SDimitry Andric 
11181ad6265SDimitry Andric   if (STI.isTargetDarwin())
11281ad6265SDimitry Andric     return CSR_iOS_SaveList;
11381ad6265SDimitry Andric 
11481ad6265SDimitry Andric   if (UseSplitPush)
11581ad6265SDimitry Andric     return STI.createAAPCSFrameChain() ? CSR_AAPCS_SplitPush_SaveList
11681ad6265SDimitry Andric                                        : CSR_ATPCS_SplitPush_SaveList;
11781ad6265SDimitry Andric 
11881ad6265SDimitry Andric   return CSR_AAPCS_SaveList;
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric 
getCalleeSavedRegsViaCopy(const MachineFunction * MF) const1210b57cec5SDimitry Andric const MCPhysReg *ARMBaseRegisterInfo::getCalleeSavedRegsViaCopy(
1220b57cec5SDimitry Andric     const MachineFunction *MF) const {
1230b57cec5SDimitry Andric   assert(MF && "Invalid MachineFunction pointer.");
1240b57cec5SDimitry Andric   if (MF->getFunction().getCallingConv() == CallingConv::CXX_FAST_TLS &&
1250b57cec5SDimitry Andric       MF->getInfo<ARMFunctionInfo>()->isSplitCSR())
1260b57cec5SDimitry Andric     return CSR_iOS_CXX_TLS_ViaCopy_SaveList;
1270b57cec5SDimitry Andric   return nullptr;
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric const uint32_t *
getCallPreservedMask(const MachineFunction & MF,CallingConv::ID CC) const1310b57cec5SDimitry Andric ARMBaseRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
1320b57cec5SDimitry Andric                                           CallingConv::ID CC) const {
1330b57cec5SDimitry Andric   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
1340b57cec5SDimitry Andric   if (CC == CallingConv::GHC)
1350b57cec5SDimitry Andric     // This is academic because all GHC calls are (supposed to be) tail calls
1360b57cec5SDimitry Andric     return CSR_NoRegs_RegMask;
137480093f4SDimitry Andric   if (CC == CallingConv::CFGuard_Check)
138480093f4SDimitry Andric     return CSR_Win_AAPCS_CFGuard_Check_RegMask;
139fe6060f1SDimitry Andric   if (CC == CallingConv::SwiftTail) {
140fe6060f1SDimitry Andric     return STI.isTargetDarwin() ? CSR_iOS_SwiftTail_RegMask
141fe6060f1SDimitry Andric                                 : CSR_AAPCS_SwiftTail_RegMask;
142fe6060f1SDimitry Andric   }
1430b57cec5SDimitry Andric   if (STI.getTargetLowering()->supportSwiftError() &&
1440b57cec5SDimitry Andric       MF.getFunction().getAttributes().hasAttrSomewhere(Attribute::SwiftError))
1450b57cec5SDimitry Andric     return STI.isTargetDarwin() ? CSR_iOS_SwiftError_RegMask
1460b57cec5SDimitry Andric                                 : CSR_AAPCS_SwiftError_RegMask;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   if (STI.isTargetDarwin() && CC == CallingConv::CXX_FAST_TLS)
1490b57cec5SDimitry Andric     return CSR_iOS_CXX_TLS_RegMask;
1500b57cec5SDimitry Andric   return STI.isTargetDarwin() ? CSR_iOS_RegMask : CSR_AAPCS_RegMask;
1510b57cec5SDimitry Andric }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric const uint32_t*
getNoPreservedMask() const1540b57cec5SDimitry Andric ARMBaseRegisterInfo::getNoPreservedMask() const {
1550b57cec5SDimitry Andric   return CSR_NoRegs_RegMask;
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric const uint32_t *
getTLSCallPreservedMask(const MachineFunction & MF) const1590b57cec5SDimitry Andric ARMBaseRegisterInfo::getTLSCallPreservedMask(const MachineFunction &MF) const {
1600b57cec5SDimitry Andric   assert(MF.getSubtarget<ARMSubtarget>().isTargetDarwin() &&
1610b57cec5SDimitry Andric          "only know about special TLS call on Darwin");
1620b57cec5SDimitry Andric   return CSR_iOS_TLSCall_RegMask;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric const uint32_t *
getSjLjDispatchPreservedMask(const MachineFunction & MF) const1660b57cec5SDimitry Andric ARMBaseRegisterInfo::getSjLjDispatchPreservedMask(const MachineFunction &MF) const {
1670b57cec5SDimitry Andric   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
1680b57cec5SDimitry Andric   if (!STI.useSoftFloat() && STI.hasVFP2Base() && !STI.isThumb1Only())
1690b57cec5SDimitry Andric     return CSR_NoRegs_RegMask;
1700b57cec5SDimitry Andric   else
1710b57cec5SDimitry Andric     return CSR_FPRegs_RegMask;
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric const uint32_t *
getThisReturnPreservedMask(const MachineFunction & MF,CallingConv::ID CC) const1750b57cec5SDimitry Andric ARMBaseRegisterInfo::getThisReturnPreservedMask(const MachineFunction &MF,
1760b57cec5SDimitry Andric                                                 CallingConv::ID CC) const {
1770b57cec5SDimitry Andric   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
1780b57cec5SDimitry Andric   // This should return a register mask that is the same as that returned by
1790b57cec5SDimitry Andric   // getCallPreservedMask but that additionally preserves the register used for
1800b57cec5SDimitry Andric   // the first i32 argument (which must also be the register used to return a
1810b57cec5SDimitry Andric   // single i32 return value)
1820b57cec5SDimitry Andric   //
1830b57cec5SDimitry Andric   // In case that the calling convention does not use the same register for
1840b57cec5SDimitry Andric   // both or otherwise does not want to enable this optimization, the function
1850b57cec5SDimitry Andric   // should return NULL
1860b57cec5SDimitry Andric   if (CC == CallingConv::GHC)
1870b57cec5SDimitry Andric     // This is academic because all GHC calls are (supposed to be) tail calls
1880b57cec5SDimitry Andric     return nullptr;
1890b57cec5SDimitry Andric   return STI.isTargetDarwin() ? CSR_iOS_ThisReturn_RegMask
1900b57cec5SDimitry Andric                               : CSR_AAPCS_ThisReturn_RegMask;
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric 
getIntraCallClobberedRegs(const MachineFunction * MF) const1938bcb0991SDimitry Andric ArrayRef<MCPhysReg> ARMBaseRegisterInfo::getIntraCallClobberedRegs(
1948bcb0991SDimitry Andric     const MachineFunction *MF) const {
1958bcb0991SDimitry Andric   static const MCPhysReg IntraCallClobberedRegs[] = {ARM::R12};
1968bcb0991SDimitry Andric   return ArrayRef<MCPhysReg>(IntraCallClobberedRegs);
1978bcb0991SDimitry Andric }
1988bcb0991SDimitry Andric 
1990b57cec5SDimitry Andric BitVector ARMBaseRegisterInfo::
getReservedRegs(const MachineFunction & MF) const2000b57cec5SDimitry Andric getReservedRegs(const MachineFunction &MF) const {
2010b57cec5SDimitry Andric   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
2020b57cec5SDimitry Andric   const ARMFrameLowering *TFI = getFrameLowering(MF);
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   // FIXME: avoid re-calculating this every time.
2050b57cec5SDimitry Andric   BitVector Reserved(getNumRegs());
2060b57cec5SDimitry Andric   markSuperRegs(Reserved, ARM::SP);
2070b57cec5SDimitry Andric   markSuperRegs(Reserved, ARM::PC);
2080b57cec5SDimitry Andric   markSuperRegs(Reserved, ARM::FPSCR);
2090b57cec5SDimitry Andric   markSuperRegs(Reserved, ARM::APSR_NZCV);
210480093f4SDimitry Andric   if (TFI->hasFP(MF))
211fe6060f1SDimitry Andric     markSuperRegs(Reserved, STI.getFramePointerReg());
2120b57cec5SDimitry Andric   if (hasBasePointer(MF))
2130b57cec5SDimitry Andric     markSuperRegs(Reserved, BasePtr);
2140b57cec5SDimitry Andric   // Some targets reserve R9.
2150b57cec5SDimitry Andric   if (STI.isR9Reserved())
2160b57cec5SDimitry Andric     markSuperRegs(Reserved, ARM::R9);
2170b57cec5SDimitry Andric   // Reserve D16-D31 if the subtarget doesn't support them.
2180b57cec5SDimitry Andric   if (!STI.hasD32()) {
2190b57cec5SDimitry Andric     static_assert(ARM::D31 == ARM::D16 + 15, "Register list not consecutive!");
2200b57cec5SDimitry Andric     for (unsigned R = 0; R < 16; ++R)
2210b57cec5SDimitry Andric       markSuperRegs(Reserved, ARM::D16 + R);
2220b57cec5SDimitry Andric   }
2230b57cec5SDimitry Andric   const TargetRegisterClass &RC = ARM::GPRPairRegClass;
2240b57cec5SDimitry Andric   for (unsigned Reg : RC)
22506c3fb27SDimitry Andric     for (MCPhysReg S : subregs(Reg))
22606c3fb27SDimitry Andric       if (Reserved.test(S))
2270b57cec5SDimitry Andric         markSuperRegs(Reserved, Reg);
2280b57cec5SDimitry Andric   // For v8.1m architecture
2290b57cec5SDimitry Andric   markSuperRegs(Reserved, ARM::ZR);
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
2320b57cec5SDimitry Andric   return Reserved;
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric bool ARMBaseRegisterInfo::
isAsmClobberable(const MachineFunction & MF,MCRegister PhysReg) const2365ffd83dbSDimitry Andric isAsmClobberable(const MachineFunction &MF, MCRegister PhysReg) const {
2370b57cec5SDimitry Andric   return !getReservedRegs(MF).test(PhysReg);
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric 
isInlineAsmReadOnlyReg(const MachineFunction & MF,unsigned PhysReg) const2405ffd83dbSDimitry Andric bool ARMBaseRegisterInfo::isInlineAsmReadOnlyReg(const MachineFunction &MF,
2415ffd83dbSDimitry Andric                                                  unsigned PhysReg) const {
2425ffd83dbSDimitry Andric   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
2435ffd83dbSDimitry Andric   const ARMFrameLowering *TFI = getFrameLowering(MF);
2445ffd83dbSDimitry Andric 
2455ffd83dbSDimitry Andric   BitVector Reserved(getNumRegs());
2465ffd83dbSDimitry Andric   markSuperRegs(Reserved, ARM::PC);
24781ad6265SDimitry Andric   if (TFI->isFPReserved(MF))
248fe6060f1SDimitry Andric     markSuperRegs(Reserved, STI.getFramePointerReg());
2495ffd83dbSDimitry Andric   if (hasBasePointer(MF))
2505ffd83dbSDimitry Andric     markSuperRegs(Reserved, BasePtr);
2515ffd83dbSDimitry Andric   assert(checkAllSuperRegsMarked(Reserved));
2525ffd83dbSDimitry Andric   return Reserved.test(PhysReg);
2535ffd83dbSDimitry Andric }
2545ffd83dbSDimitry Andric 
2550b57cec5SDimitry Andric const TargetRegisterClass *
getLargestLegalSuperClass(const TargetRegisterClass * RC,const MachineFunction & MF) const2560b57cec5SDimitry Andric ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC,
2578bcb0991SDimitry Andric                                                const MachineFunction &MF) const {
2580b57cec5SDimitry Andric   const TargetRegisterClass *Super = RC;
2590b57cec5SDimitry Andric   TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
2600b57cec5SDimitry Andric   do {
2610b57cec5SDimitry Andric     switch (Super->getID()) {
2620b57cec5SDimitry Andric     case ARM::GPRRegClassID:
2630b57cec5SDimitry Andric     case ARM::SPRRegClassID:
2640b57cec5SDimitry Andric     case ARM::DPRRegClassID:
2658bcb0991SDimitry Andric     case ARM::GPRPairRegClassID:
2668bcb0991SDimitry Andric       return Super;
2670b57cec5SDimitry Andric     case ARM::QPRRegClassID:
2680b57cec5SDimitry Andric     case ARM::QQPRRegClassID:
2690b57cec5SDimitry Andric     case ARM::QQQQPRRegClassID:
2708bcb0991SDimitry Andric       if (MF.getSubtarget<ARMSubtarget>().hasNEON())
2710b57cec5SDimitry Andric         return Super;
272349cc55cSDimitry Andric       break;
273349cc55cSDimitry Andric     case ARM::MQPRRegClassID:
274349cc55cSDimitry Andric     case ARM::MQQPRRegClassID:
275349cc55cSDimitry Andric     case ARM::MQQQQPRRegClassID:
276349cc55cSDimitry Andric       if (MF.getSubtarget<ARMSubtarget>().hasMVEIntegerOps())
277349cc55cSDimitry Andric         return Super;
278349cc55cSDimitry Andric       break;
2790b57cec5SDimitry Andric     }
2800b57cec5SDimitry Andric     Super = *I++;
2810b57cec5SDimitry Andric   } while (Super);
2820b57cec5SDimitry Andric   return RC;
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric const TargetRegisterClass *
getPointerRegClass(const MachineFunction & MF,unsigned Kind) const2860b57cec5SDimitry Andric ARMBaseRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
2870b57cec5SDimitry Andric                                                                          const {
2880b57cec5SDimitry Andric   return &ARM::GPRRegClass;
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric const TargetRegisterClass *
getCrossCopyRegClass(const TargetRegisterClass * RC) const2920b57cec5SDimitry Andric ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
2930b57cec5SDimitry Andric   if (RC == &ARM::CCRRegClass)
2940b57cec5SDimitry Andric     return &ARM::rGPRRegClass;  // Can't copy CCR registers.
2950b57cec5SDimitry Andric   return RC;
2960b57cec5SDimitry Andric }
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric unsigned
getRegPressureLimit(const TargetRegisterClass * RC,MachineFunction & MF) const2990b57cec5SDimitry Andric ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
3000b57cec5SDimitry Andric                                          MachineFunction &MF) const {
3010b57cec5SDimitry Andric   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
3020b57cec5SDimitry Andric   const ARMFrameLowering *TFI = getFrameLowering(MF);
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   switch (RC->getID()) {
3050b57cec5SDimitry Andric   default:
3060b57cec5SDimitry Andric     return 0;
3070b57cec5SDimitry Andric   case ARM::tGPRRegClassID: {
3080b57cec5SDimitry Andric     // hasFP ends up calling getMaxCallFrameComputed() which may not be
3090b57cec5SDimitry Andric     // available when getPressureLimit() is called as part of
3100b57cec5SDimitry Andric     // ScheduleDAGRRList.
3110b57cec5SDimitry Andric     bool HasFP = MF.getFrameInfo().isMaxCallFrameSizeComputed()
3120b57cec5SDimitry Andric                  ? TFI->hasFP(MF) : true;
3130b57cec5SDimitry Andric     return 5 - HasFP;
3140b57cec5SDimitry Andric   }
3150b57cec5SDimitry Andric   case ARM::GPRRegClassID: {
3160b57cec5SDimitry Andric     bool HasFP = MF.getFrameInfo().isMaxCallFrameSizeComputed()
3170b57cec5SDimitry Andric                  ? TFI->hasFP(MF) : true;
3180b57cec5SDimitry Andric     return 10 - HasFP - (STI.isR9Reserved() ? 1 : 0);
3190b57cec5SDimitry Andric   }
3200b57cec5SDimitry Andric   case ARM::SPRRegClassID:  // Currently not used as 'rep' register class.
3210b57cec5SDimitry Andric   case ARM::DPRRegClassID:
3220b57cec5SDimitry Andric     return 32 - 10;
3230b57cec5SDimitry Andric   }
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric // Get the other register in a GPRPair.
getPairedGPR(MCPhysReg Reg,bool Odd,const MCRegisterInfo * RI)3275ffd83dbSDimitry Andric static MCPhysReg getPairedGPR(MCPhysReg Reg, bool Odd,
3285ffd83dbSDimitry Andric                               const MCRegisterInfo *RI) {
32906c3fb27SDimitry Andric   for (MCPhysReg Super : RI->superregs(Reg))
33006c3fb27SDimitry Andric     if (ARM::GPRPairRegClass.contains(Super))
33106c3fb27SDimitry Andric       return RI->getSubReg(Super, Odd ? ARM::gsub_1 : ARM::gsub_0);
3320b57cec5SDimitry Andric   return 0;
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric // Resolve the RegPairEven / RegPairOdd register allocator hints.
getRegAllocationHints(Register VirtReg,ArrayRef<MCPhysReg> Order,SmallVectorImpl<MCPhysReg> & Hints,const MachineFunction & MF,const VirtRegMap * VRM,const LiveRegMatrix * Matrix) const3365ffd83dbSDimitry Andric bool ARMBaseRegisterInfo::getRegAllocationHints(
3375ffd83dbSDimitry Andric     Register VirtReg, ArrayRef<MCPhysReg> Order,
3385ffd83dbSDimitry Andric     SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
3395ffd83dbSDimitry Andric     const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
3400b57cec5SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
34106c3fb27SDimitry Andric   std::pair<unsigned, Register> Hint = MRI.getRegAllocationHint(VirtReg);
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   unsigned Odd;
3440b57cec5SDimitry Andric   switch (Hint.first) {
3450b57cec5SDimitry Andric   case ARMRI::RegPairEven:
3460b57cec5SDimitry Andric     Odd = 0;
3470b57cec5SDimitry Andric     break;
3480b57cec5SDimitry Andric   case ARMRI::RegPairOdd:
3490b57cec5SDimitry Andric     Odd = 1;
3500b57cec5SDimitry Andric     break;
351e8d8bef9SDimitry Andric   case ARMRI::RegLR:
3520b57cec5SDimitry Andric     TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
353e8d8bef9SDimitry Andric     if (MRI.getRegClass(VirtReg)->contains(ARM::LR))
354e8d8bef9SDimitry Andric       Hints.push_back(ARM::LR);
3550b57cec5SDimitry Andric     return false;
356e8d8bef9SDimitry Andric   default:
357e8d8bef9SDimitry Andric     return TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
3580b57cec5SDimitry Andric   }
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   // This register should preferably be even (Odd == 0) or odd (Odd == 1).
3610b57cec5SDimitry Andric   // Check if the other part of the pair has already been assigned, and provide
3620b57cec5SDimitry Andric   // the paired register as the first hint.
3635ffd83dbSDimitry Andric   Register Paired = Hint.second;
3645ffd83dbSDimitry Andric   if (!Paired)
3650b57cec5SDimitry Andric     return false;
3660b57cec5SDimitry Andric 
3675ffd83dbSDimitry Andric   Register PairedPhys;
3685ffd83dbSDimitry Andric   if (Paired.isPhysical()) {
3690b57cec5SDimitry Andric     PairedPhys = Paired;
3700b57cec5SDimitry Andric   } else if (VRM && VRM->hasPhys(Paired)) {
3710b57cec5SDimitry Andric     PairedPhys = getPairedGPR(VRM->getPhys(Paired), Odd, this);
3720b57cec5SDimitry Andric   }
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric   // First prefer the paired physreg.
3750b57cec5SDimitry Andric   if (PairedPhys && is_contained(Order, PairedPhys))
3760b57cec5SDimitry Andric     Hints.push_back(PairedPhys);
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric   // Then prefer even or odd registers.
3795ffd83dbSDimitry Andric   for (MCPhysReg Reg : Order) {
3800b57cec5SDimitry Andric     if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd)
3810b57cec5SDimitry Andric       continue;
3820b57cec5SDimitry Andric     // Don't provide hints that are paired to a reserved register.
3835ffd83dbSDimitry Andric     MCPhysReg Paired = getPairedGPR(Reg, !Odd, this);
3840b57cec5SDimitry Andric     if (!Paired || MRI.isReserved(Paired))
3850b57cec5SDimitry Andric       continue;
3860b57cec5SDimitry Andric     Hints.push_back(Reg);
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric   return false;
3890b57cec5SDimitry Andric }
3900b57cec5SDimitry Andric 
updateRegAllocHint(Register Reg,Register NewReg,MachineFunction & MF) const3915ffd83dbSDimitry Andric void ARMBaseRegisterInfo::updateRegAllocHint(Register Reg, Register NewReg,
3920b57cec5SDimitry Andric                                              MachineFunction &MF) const {
3930b57cec5SDimitry Andric   MachineRegisterInfo *MRI = &MF.getRegInfo();
39406c3fb27SDimitry Andric   std::pair<unsigned, Register> Hint = MRI->getRegAllocationHint(Reg);
3955ffd83dbSDimitry Andric   if ((Hint.first == ARMRI::RegPairOdd || Hint.first == ARMRI::RegPairEven) &&
3965ffd83dbSDimitry Andric       Hint.second.isVirtual()) {
3970b57cec5SDimitry Andric     // If 'Reg' is one of the even / odd register pair and it's now changed
3980b57cec5SDimitry Andric     // (e.g. coalesced) into a different register. The other register of the
3990b57cec5SDimitry Andric     // pair allocation hint must be updated to reflect the relationship
4000b57cec5SDimitry Andric     // change.
4015ffd83dbSDimitry Andric     Register OtherReg = Hint.second;
4020b57cec5SDimitry Andric     Hint = MRI->getRegAllocationHint(OtherReg);
4030b57cec5SDimitry Andric     // Make sure the pair has not already divorced.
4040b57cec5SDimitry Andric     if (Hint.second == Reg) {
4050b57cec5SDimitry Andric       MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
406bdd1243dSDimitry Andric       if (NewReg.isVirtual())
4070b57cec5SDimitry Andric         MRI->setRegAllocationHint(NewReg,
4085ffd83dbSDimitry Andric                                   Hint.first == ARMRI::RegPairOdd
4095ffd83dbSDimitry Andric                                       ? ARMRI::RegPairEven
4105ffd83dbSDimitry Andric                                       : ARMRI::RegPairOdd,
4115ffd83dbSDimitry Andric                                   OtherReg);
4120b57cec5SDimitry Andric     }
4130b57cec5SDimitry Andric   }
4140b57cec5SDimitry Andric }
4150b57cec5SDimitry Andric 
hasBasePointer(const MachineFunction & MF) const4160b57cec5SDimitry Andric bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
4170b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4180b57cec5SDimitry Andric   const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
4190b57cec5SDimitry Andric   const ARMFrameLowering *TFI = getFrameLowering(MF);
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   // If we have stack realignment and VLAs, we have no pointer to use to
4220b57cec5SDimitry Andric   // access the stack. If we have stack realignment, and a large call frame,
4230b57cec5SDimitry Andric   // we have no place to allocate the emergency spill slot.
424fe6060f1SDimitry Andric   if (hasStackRealignment(MF) && !TFI->hasReservedCallFrame(MF))
4250b57cec5SDimitry Andric     return true;
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric   // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
428480093f4SDimitry Andric   // negative range for ldr/str (255), and Thumb1 is positive offsets only.
4290b57cec5SDimitry Andric   //
4300b57cec5SDimitry Andric   // It's going to be better to use the SP or Base Pointer instead. When there
4310b57cec5SDimitry Andric   // are variable sized objects, we can't reference off of the SP, so we
4320b57cec5SDimitry Andric   // reserve a Base Pointer.
4330b57cec5SDimitry Andric   //
4340b57cec5SDimitry Andric   // For Thumb2, estimate whether a negative offset from the frame pointer
4350b57cec5SDimitry Andric   // will be sufficient to reach the whole stack frame. If a function has a
4360b57cec5SDimitry Andric   // smallish frame, it's less likely to have lots of spills and callee saved
4370b57cec5SDimitry Andric   // space, so it's all more likely to be within range of the frame pointer.
4380b57cec5SDimitry Andric   // If it's wrong, the scavenger will still enable access to work, it just
4390b57cec5SDimitry Andric   // won't be optimal.  (We should always be able to reach the emergency
4400b57cec5SDimitry Andric   // spill slot from the frame pointer.)
4410b57cec5SDimitry Andric   if (AFI->isThumb2Function() && MFI.hasVarSizedObjects() &&
4420b57cec5SDimitry Andric       MFI.getLocalFrameSize() >= 128)
4430b57cec5SDimitry Andric     return true;
4440b57cec5SDimitry Andric   // For Thumb1, if sp moves, nothing is in range, so force a base pointer.
4450b57cec5SDimitry Andric   // This is necessary for correctness in cases where we need an emergency
4460b57cec5SDimitry Andric   // spill slot. (In Thumb1, we can't use a negative offset from the frame
4470b57cec5SDimitry Andric   // pointer.)
4480b57cec5SDimitry Andric   if (AFI->isThumb1OnlyFunction() && !TFI->hasReservedCallFrame(MF))
4490b57cec5SDimitry Andric     return true;
4500b57cec5SDimitry Andric   return false;
4510b57cec5SDimitry Andric }
4520b57cec5SDimitry Andric 
canRealignStack(const MachineFunction & MF) const4530b57cec5SDimitry Andric bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
4540b57cec5SDimitry Andric   const MachineRegisterInfo *MRI = &MF.getRegInfo();
4550b57cec5SDimitry Andric   const ARMFrameLowering *TFI = getFrameLowering(MF);
456fe6060f1SDimitry Andric   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
4570b57cec5SDimitry Andric   // We can't realign the stack if:
4580b57cec5SDimitry Andric   // 1. Dynamic stack realignment is explicitly disabled,
4590b57cec5SDimitry Andric   // 2. There are VLAs in the function and the base pointer is disabled.
4600b57cec5SDimitry Andric   if (!TargetRegisterInfo::canRealignStack(MF))
4610b57cec5SDimitry Andric     return false;
4620b57cec5SDimitry Andric   // Stack realignment requires a frame pointer.  If we already started
4630b57cec5SDimitry Andric   // register allocation with frame pointer elimination, it is too late now.
464fe6060f1SDimitry Andric   if (!MRI->canReserveReg(STI.getFramePointerReg()))
4650b57cec5SDimitry Andric     return false;
4660b57cec5SDimitry Andric   // We may also need a base pointer if there are dynamic allocas or stack
4670b57cec5SDimitry Andric   // pointer adjustments around calls.
4680b57cec5SDimitry Andric   if (TFI->hasReservedCallFrame(MF))
4690b57cec5SDimitry Andric     return true;
4700b57cec5SDimitry Andric   // A base pointer is required and allowed.  Check that it isn't too late to
4710b57cec5SDimitry Andric   // reserve it.
4720b57cec5SDimitry Andric   return MRI->canReserveReg(BasePtr);
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric bool ARMBaseRegisterInfo::
cannotEliminateFrame(const MachineFunction & MF) const4760b57cec5SDimitry Andric cannotEliminateFrame(const MachineFunction &MF) const {
4770b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4780b57cec5SDimitry Andric   if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI.adjustsStack())
4790b57cec5SDimitry Andric     return true;
480fe6060f1SDimitry Andric   return MFI.hasVarSizedObjects() || MFI.isFrameAddressTaken() ||
481fe6060f1SDimitry Andric          hasStackRealignment(MF);
4820b57cec5SDimitry Andric }
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric Register
getFrameRegister(const MachineFunction & MF) const4850b57cec5SDimitry Andric ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
4860b57cec5SDimitry Andric   const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
4870b57cec5SDimitry Andric   const ARMFrameLowering *TFI = getFrameLowering(MF);
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric   if (TFI->hasFP(MF))
490fe6060f1SDimitry Andric     return STI.getFramePointerReg();
4910b57cec5SDimitry Andric   return ARM::SP;
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric /// emitLoadConstPool - Emits a load from constpool to materialize the
4950b57cec5SDimitry Andric /// specified immediate.
emitLoadConstPool(MachineBasicBlock & MBB,MachineBasicBlock::iterator & MBBI,const DebugLoc & dl,Register DestReg,unsigned SubIdx,int Val,ARMCC::CondCodes Pred,Register PredReg,unsigned MIFlags) const4960b57cec5SDimitry Andric void ARMBaseRegisterInfo::emitLoadConstPool(
4970b57cec5SDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
4985ffd83dbSDimitry Andric     const DebugLoc &dl, Register DestReg, unsigned SubIdx, int Val,
4995ffd83dbSDimitry Andric     ARMCC::CondCodes Pred, Register PredReg, unsigned MIFlags) const {
5000b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
5010b57cec5SDimitry Andric   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
5020b57cec5SDimitry Andric   MachineConstantPool *ConstantPool = MF.getConstantPool();
5030b57cec5SDimitry Andric   const Constant *C =
5040b57cec5SDimitry Andric         ConstantInt::get(Type::getInt32Ty(MF.getFunction().getContext()), Val);
5055ffd83dbSDimitry Andric   unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align(4));
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric   BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
5080b57cec5SDimitry Andric       .addReg(DestReg, getDefRegState(true), SubIdx)
5090b57cec5SDimitry Andric       .addConstantPoolIndex(Idx)
5100b57cec5SDimitry Andric       .addImm(0)
5110b57cec5SDimitry Andric       .add(predOps(Pred, PredReg))
5120b57cec5SDimitry Andric       .setMIFlags(MIFlags);
5130b57cec5SDimitry Andric }
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric bool ARMBaseRegisterInfo::
requiresRegisterScavenging(const MachineFunction & MF) const5160b57cec5SDimitry Andric requiresRegisterScavenging(const MachineFunction &MF) const {
5170b57cec5SDimitry Andric   return true;
5180b57cec5SDimitry Andric }
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric bool ARMBaseRegisterInfo::
requiresFrameIndexScavenging(const MachineFunction & MF) const5210b57cec5SDimitry Andric requiresFrameIndexScavenging(const MachineFunction &MF) const {
5220b57cec5SDimitry Andric   return true;
5230b57cec5SDimitry Andric }
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric bool ARMBaseRegisterInfo::
requiresVirtualBaseRegisters(const MachineFunction & MF) const5260b57cec5SDimitry Andric requiresVirtualBaseRegisters(const MachineFunction &MF) const {
5270b57cec5SDimitry Andric   return true;
5280b57cec5SDimitry Andric }
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric int64_t ARMBaseRegisterInfo::
getFrameIndexInstrOffset(const MachineInstr * MI,int Idx) const5310b57cec5SDimitry Andric getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
5320b57cec5SDimitry Andric   const MCInstrDesc &Desc = MI->getDesc();
5330b57cec5SDimitry Andric   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
5340b57cec5SDimitry Andric   int64_t InstrOffs = 0;
5350b57cec5SDimitry Andric   int Scale = 1;
5360b57cec5SDimitry Andric   unsigned ImmIdx = 0;
5370b57cec5SDimitry Andric   switch (AddrMode) {
5380b57cec5SDimitry Andric   case ARMII::AddrModeT2_i8:
5390eae32dcSDimitry Andric   case ARMII::AddrModeT2_i8neg:
5400eae32dcSDimitry Andric   case ARMII::AddrModeT2_i8pos:
5410b57cec5SDimitry Andric   case ARMII::AddrModeT2_i12:
5420b57cec5SDimitry Andric   case ARMII::AddrMode_i12:
5430b57cec5SDimitry Andric     InstrOffs = MI->getOperand(Idx+1).getImm();
5440b57cec5SDimitry Andric     Scale = 1;
5450b57cec5SDimitry Andric     break;
5460b57cec5SDimitry Andric   case ARMII::AddrMode5: {
5470b57cec5SDimitry Andric     // VFP address mode.
5480b57cec5SDimitry Andric     const MachineOperand &OffOp = MI->getOperand(Idx+1);
5490b57cec5SDimitry Andric     InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
5500b57cec5SDimitry Andric     if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
5510b57cec5SDimitry Andric       InstrOffs = -InstrOffs;
5520b57cec5SDimitry Andric     Scale = 4;
5530b57cec5SDimitry Andric     break;
5540b57cec5SDimitry Andric   }
5550b57cec5SDimitry Andric   case ARMII::AddrMode2:
5560b57cec5SDimitry Andric     ImmIdx = Idx+2;
5570b57cec5SDimitry Andric     InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
5580b57cec5SDimitry Andric     if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
5590b57cec5SDimitry Andric       InstrOffs = -InstrOffs;
5600b57cec5SDimitry Andric     break;
5610b57cec5SDimitry Andric   case ARMII::AddrMode3:
5620b57cec5SDimitry Andric     ImmIdx = Idx+2;
5630b57cec5SDimitry Andric     InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
5640b57cec5SDimitry Andric     if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
5650b57cec5SDimitry Andric       InstrOffs = -InstrOffs;
5660b57cec5SDimitry Andric     break;
5670b57cec5SDimitry Andric   case ARMII::AddrModeT1_s:
5680b57cec5SDimitry Andric     ImmIdx = Idx+1;
5690b57cec5SDimitry Andric     InstrOffs = MI->getOperand(ImmIdx).getImm();
5700b57cec5SDimitry Andric     Scale = 4;
5710b57cec5SDimitry Andric     break;
5720b57cec5SDimitry Andric   default:
5730b57cec5SDimitry Andric     llvm_unreachable("Unsupported addressing mode!");
5740b57cec5SDimitry Andric   }
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric   return InstrOffs * Scale;
5770b57cec5SDimitry Andric }
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric /// needsFrameBaseReg - Returns true if the instruction's frame index
5800b57cec5SDimitry Andric /// reference would be better served by a base register other than FP
5810b57cec5SDimitry Andric /// or SP. Used by LocalStackFrameAllocation to determine which frame index
5820b57cec5SDimitry Andric /// references it should create new base registers for.
5830b57cec5SDimitry Andric bool ARMBaseRegisterInfo::
needsFrameBaseReg(MachineInstr * MI,int64_t Offset) const5840b57cec5SDimitry Andric needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
5850b57cec5SDimitry Andric   for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
5860b57cec5SDimitry Andric     assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
5870b57cec5SDimitry Andric   }
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric   // It's the load/store FI references that cause issues, as it can be difficult
5900b57cec5SDimitry Andric   // to materialize the offset if it won't fit in the literal field. Estimate
5910b57cec5SDimitry Andric   // based on the size of the local frame and some conservative assumptions
5920b57cec5SDimitry Andric   // about the rest of the stack frame (note, this is pre-regalloc, so
5930b57cec5SDimitry Andric   // we don't know everything for certain yet) whether this offset is likely
5940b57cec5SDimitry Andric   // to be out of range of the immediate. Return true if so.
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   // We only generate virtual base registers for loads and stores, so
5970b57cec5SDimitry Andric   // return false for everything else.
5980b57cec5SDimitry Andric   unsigned Opc = MI->getOpcode();
5990b57cec5SDimitry Andric   switch (Opc) {
6000b57cec5SDimitry Andric   case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
6010b57cec5SDimitry Andric   case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
6020b57cec5SDimitry Andric   case ARM::t2LDRi12: case ARM::t2LDRi8:
6030b57cec5SDimitry Andric   case ARM::t2STRi12: case ARM::t2STRi8:
6040b57cec5SDimitry Andric   case ARM::VLDRS: case ARM::VLDRD:
6050b57cec5SDimitry Andric   case ARM::VSTRS: case ARM::VSTRD:
6060b57cec5SDimitry Andric   case ARM::tSTRspi: case ARM::tLDRspi:
6070b57cec5SDimitry Andric     break;
6080b57cec5SDimitry Andric   default:
6090b57cec5SDimitry Andric     return false;
6100b57cec5SDimitry Andric   }
6110b57cec5SDimitry Andric 
6120b57cec5SDimitry Andric   // Without a virtual base register, if the function has variable sized
6130b57cec5SDimitry Andric   // objects, all fixed-size local references will be via the frame pointer,
6140b57cec5SDimitry Andric   // Approximate the offset and see if it's legal for the instruction.
6150b57cec5SDimitry Andric   // Note that the incoming offset is based on the SP value at function entry,
6160b57cec5SDimitry Andric   // so it'll be negative.
6170b57cec5SDimitry Andric   MachineFunction &MF = *MI->getParent()->getParent();
6180b57cec5SDimitry Andric   const ARMFrameLowering *TFI = getFrameLowering(MF);
6190b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
6200b57cec5SDimitry Andric   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric   // Estimate an offset from the frame pointer.
6230b57cec5SDimitry Andric   // Conservatively assume all callee-saved registers get pushed. R4-R6
6240b57cec5SDimitry Andric   // will be earlier than the FP, so we ignore those.
6250b57cec5SDimitry Andric   // R7, LR
6260b57cec5SDimitry Andric   int64_t FPOffset = Offset - 8;
6270b57cec5SDimitry Andric   // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
6280b57cec5SDimitry Andric   if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
6290b57cec5SDimitry Andric     FPOffset -= 80;
6300b57cec5SDimitry Andric   // Estimate an offset from the stack pointer.
6310b57cec5SDimitry Andric   // The incoming offset is relating to the SP at the start of the function,
6320b57cec5SDimitry Andric   // but when we access the local it'll be relative to the SP after local
6330b57cec5SDimitry Andric   // allocation, so adjust our SP-relative offset by that allocation size.
6340b57cec5SDimitry Andric   Offset += MFI.getLocalFrameSize();
6350b57cec5SDimitry Andric   // Assume that we'll have at least some spill slots allocated.
6360b57cec5SDimitry Andric   // FIXME: This is a total SWAG number. We should run some statistics
6370b57cec5SDimitry Andric   //        and pick a real one.
6380b57cec5SDimitry Andric   Offset += 128; // 128 bytes of spill slots
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric   // If there's a frame pointer and the addressing mode allows it, try using it.
6410b57cec5SDimitry Andric   // The FP is only available if there is no dynamic realignment. We
6420b57cec5SDimitry Andric   // don't know for sure yet whether we'll need that, so we guess based
6430b57cec5SDimitry Andric   // on whether there are any local variables that would trigger it.
6440b57cec5SDimitry Andric   if (TFI->hasFP(MF) &&
6455ffd83dbSDimitry Andric       !((MFI.getLocalFrameMaxAlign() > TFI->getStackAlign()) &&
6465ffd83dbSDimitry Andric         canRealignStack(MF))) {
6470b57cec5SDimitry Andric     if (isFrameOffsetLegal(MI, getFrameRegister(MF), FPOffset))
6480b57cec5SDimitry Andric       return false;
6490b57cec5SDimitry Andric   }
6500b57cec5SDimitry Andric   // If we can reference via the stack pointer, try that.
6510b57cec5SDimitry Andric   // FIXME: This (and the code that resolves the references) can be improved
6520b57cec5SDimitry Andric   //        to only disallow SP relative references in the live range of
6530b57cec5SDimitry Andric   //        the VLA(s). In practice, it's unclear how much difference that
6540b57cec5SDimitry Andric   //        would make, but it may be worth doing.
6550b57cec5SDimitry Andric   if (!MFI.hasVarSizedObjects() && isFrameOffsetLegal(MI, ARM::SP, Offset))
6560b57cec5SDimitry Andric     return false;
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric   // The offset likely isn't legal, we want to allocate a virtual base register.
6590b57cec5SDimitry Andric   return true;
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
6630b57cec5SDimitry Andric /// be a pointer to FrameIdx at the beginning of the basic block.
664e8d8bef9SDimitry Andric Register
materializeFrameBaseRegister(MachineBasicBlock * MBB,int FrameIdx,int64_t Offset) const665e8d8bef9SDimitry Andric ARMBaseRegisterInfo::materializeFrameBaseRegister(MachineBasicBlock *MBB,
6665ffd83dbSDimitry Andric                                                   int FrameIdx,
6670b57cec5SDimitry Andric                                                   int64_t Offset) const {
6680b57cec5SDimitry Andric   ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
6690b57cec5SDimitry Andric   unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
6700b57cec5SDimitry Andric     (AFI->isThumb1OnlyFunction() ? ARM::tADDframe : ARM::t2ADDri);
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric   MachineBasicBlock::iterator Ins = MBB->begin();
6730b57cec5SDimitry Andric   DebugLoc DL;                  // Defaults to "unknown"
6740b57cec5SDimitry Andric   if (Ins != MBB->end())
6750b57cec5SDimitry Andric     DL = Ins->getDebugLoc();
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric   const MachineFunction &MF = *MBB->getParent();
6780b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
6790b57cec5SDimitry Andric   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
6800b57cec5SDimitry Andric   const MCInstrDesc &MCID = TII.get(ADDriOpc);
681e8d8bef9SDimitry Andric   Register BaseReg = MRI.createVirtualRegister(&ARM::GPRRegClass);
6820b57cec5SDimitry Andric   MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric   MachineInstrBuilder MIB = BuildMI(*MBB, Ins, DL, MCID, BaseReg)
6850b57cec5SDimitry Andric     .addFrameIndex(FrameIdx).addImm(Offset);
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric   if (!AFI->isThumb1OnlyFunction())
6880b57cec5SDimitry Andric     MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
689e8d8bef9SDimitry Andric 
690e8d8bef9SDimitry Andric   return BaseReg;
6910b57cec5SDimitry Andric }
6920b57cec5SDimitry Andric 
resolveFrameIndex(MachineInstr & MI,Register BaseReg,int64_t Offset) const6935ffd83dbSDimitry Andric void ARMBaseRegisterInfo::resolveFrameIndex(MachineInstr &MI, Register BaseReg,
6940b57cec5SDimitry Andric                                             int64_t Offset) const {
6950b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
6960b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
6970b57cec5SDimitry Andric   const ARMBaseInstrInfo &TII =
6980b57cec5SDimitry Andric       *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
6990b57cec5SDimitry Andric   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
7000b57cec5SDimitry Andric   int Off = Offset; // ARM doesn't need the general 64-bit offsets
7010b57cec5SDimitry Andric   unsigned i = 0;
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric   assert(!AFI->isThumb1OnlyFunction() &&
7040b57cec5SDimitry Andric          "This resolveFrameIndex does not support Thumb1!");
7050b57cec5SDimitry Andric 
7060b57cec5SDimitry Andric   while (!MI.getOperand(i).isFI()) {
7070b57cec5SDimitry Andric     ++i;
7080b57cec5SDimitry Andric     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
7090b57cec5SDimitry Andric   }
7100b57cec5SDimitry Andric   bool Done = false;
7110b57cec5SDimitry Andric   if (!AFI->isThumbFunction())
7120b57cec5SDimitry Andric     Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
7130b57cec5SDimitry Andric   else {
7140b57cec5SDimitry Andric     assert(AFI->isThumb2Function());
7158bcb0991SDimitry Andric     Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII, this);
7160b57cec5SDimitry Andric   }
7170b57cec5SDimitry Andric   assert(Done && "Unable to resolve frame index!");
7180b57cec5SDimitry Andric   (void)Done;
7190b57cec5SDimitry Andric }
7200b57cec5SDimitry Andric 
isFrameOffsetLegal(const MachineInstr * MI,Register BaseReg,int64_t Offset) const7215ffd83dbSDimitry Andric bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
7225ffd83dbSDimitry Andric                                              Register BaseReg,
7230b57cec5SDimitry Andric                                              int64_t Offset) const {
7240b57cec5SDimitry Andric   const MCInstrDesc &Desc = MI->getDesc();
7250b57cec5SDimitry Andric   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
7260b57cec5SDimitry Andric   unsigned i = 0;
7270b57cec5SDimitry Andric   for (; !MI->getOperand(i).isFI(); ++i)
7280b57cec5SDimitry Andric     assert(i+1 < MI->getNumOperands() && "Instr doesn't have FrameIndex operand!");
7290b57cec5SDimitry Andric 
7300b57cec5SDimitry Andric   // AddrMode4 and AddrMode6 cannot handle any offset.
7310b57cec5SDimitry Andric   if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
7320b57cec5SDimitry Andric     return Offset == 0;
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric   unsigned NumBits = 0;
7350b57cec5SDimitry Andric   unsigned Scale = 1;
7360b57cec5SDimitry Andric   bool isSigned = true;
7370b57cec5SDimitry Andric   switch (AddrMode) {
7380b57cec5SDimitry Andric   case ARMII::AddrModeT2_i8:
7390eae32dcSDimitry Andric   case ARMII::AddrModeT2_i8pos:
7400eae32dcSDimitry Andric   case ARMII::AddrModeT2_i8neg:
7410b57cec5SDimitry Andric   case ARMII::AddrModeT2_i12:
7420b57cec5SDimitry Andric     // i8 supports only negative, and i12 supports only positive, so
7430b57cec5SDimitry Andric     // based on Offset sign, consider the appropriate instruction
7440b57cec5SDimitry Andric     Scale = 1;
7450b57cec5SDimitry Andric     if (Offset < 0) {
7460b57cec5SDimitry Andric       NumBits = 8;
7470b57cec5SDimitry Andric       Offset = -Offset;
7480b57cec5SDimitry Andric     } else {
7490b57cec5SDimitry Andric       NumBits = 12;
7500b57cec5SDimitry Andric     }
7510b57cec5SDimitry Andric     break;
7520b57cec5SDimitry Andric   case ARMII::AddrMode5:
7530b57cec5SDimitry Andric     // VFP address mode.
7540b57cec5SDimitry Andric     NumBits = 8;
7550b57cec5SDimitry Andric     Scale = 4;
7560b57cec5SDimitry Andric     break;
7570b57cec5SDimitry Andric   case ARMII::AddrMode_i12:
7580b57cec5SDimitry Andric   case ARMII::AddrMode2:
7590b57cec5SDimitry Andric     NumBits = 12;
7600b57cec5SDimitry Andric     break;
7610b57cec5SDimitry Andric   case ARMII::AddrMode3:
7620b57cec5SDimitry Andric     NumBits = 8;
7630b57cec5SDimitry Andric     break;
7640b57cec5SDimitry Andric   case ARMII::AddrModeT1_s:
7650b57cec5SDimitry Andric     NumBits = (BaseReg == ARM::SP ? 8 : 5);
7660b57cec5SDimitry Andric     Scale = 4;
7670b57cec5SDimitry Andric     isSigned = false;
7680b57cec5SDimitry Andric     break;
7690b57cec5SDimitry Andric   default:
7700b57cec5SDimitry Andric     llvm_unreachable("Unsupported addressing mode!");
7710b57cec5SDimitry Andric   }
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric   Offset += getFrameIndexInstrOffset(MI, i);
7740b57cec5SDimitry Andric   // Make sure the offset is encodable for instructions that scale the
7750b57cec5SDimitry Andric   // immediate.
7760b57cec5SDimitry Andric   if ((Offset & (Scale-1)) != 0)
7770b57cec5SDimitry Andric     return false;
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric   if (isSigned && Offset < 0)
7800b57cec5SDimitry Andric     Offset = -Offset;
7810b57cec5SDimitry Andric 
7820b57cec5SDimitry Andric   unsigned Mask = (1 << NumBits) - 1;
7830b57cec5SDimitry Andric   if ((unsigned)Offset <= Mask * Scale)
7840b57cec5SDimitry Andric     return true;
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric   return false;
7870b57cec5SDimitry Andric }
7880b57cec5SDimitry Andric 
789bdd1243dSDimitry Andric bool
eliminateFrameIndex(MachineBasicBlock::iterator II,int SPAdj,unsigned FIOperandNum,RegScavenger * RS) const7900b57cec5SDimitry Andric ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
7910b57cec5SDimitry Andric                                          int SPAdj, unsigned FIOperandNum,
7920b57cec5SDimitry Andric                                          RegScavenger *RS) const {
7930b57cec5SDimitry Andric   MachineInstr &MI = *II;
7940b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
7950b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
7960b57cec5SDimitry Andric   const ARMBaseInstrInfo &TII =
7970b57cec5SDimitry Andric       *static_cast<const ARMBaseInstrInfo *>(MF.getSubtarget().getInstrInfo());
7980b57cec5SDimitry Andric   const ARMFrameLowering *TFI = getFrameLowering(MF);
7990b57cec5SDimitry Andric   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
8000b57cec5SDimitry Andric   assert(!AFI->isThumb1OnlyFunction() &&
8010b57cec5SDimitry Andric          "This eliminateFrameIndex does not support Thumb1!");
8020b57cec5SDimitry Andric   int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
8035ffd83dbSDimitry Andric   Register FrameReg;
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric   int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric   // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
8080b57cec5SDimitry Andric   // call frame setup/destroy instructions have already been eliminated.  That
8090b57cec5SDimitry Andric   // means the stack pointer cannot be used to access the emergency spill slot
8100b57cec5SDimitry Andric   // when !hasReservedCallFrame().
8110b57cec5SDimitry Andric #ifndef NDEBUG
8120b57cec5SDimitry Andric   if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){
8130b57cec5SDimitry Andric     assert(TFI->hasReservedCallFrame(MF) &&
8140b57cec5SDimitry Andric            "Cannot use SP to access the emergency spill slot in "
8150b57cec5SDimitry Andric            "functions without a reserved call frame");
8160b57cec5SDimitry Andric     assert(!MF.getFrameInfo().hasVarSizedObjects() &&
8170b57cec5SDimitry Andric            "Cannot use SP to access the emergency spill slot in "
8180b57cec5SDimitry Andric            "functions with variable sized frame objects");
8190b57cec5SDimitry Andric   }
8200b57cec5SDimitry Andric #endif // NDEBUG
8210b57cec5SDimitry Andric 
8220b57cec5SDimitry Andric   assert(!MI.isDebugValue() && "DBG_VALUEs should be handled in target-independent code");
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric   // Modify MI as necessary to handle as much of 'Offset' as possible
8250b57cec5SDimitry Andric   bool Done = false;
8260b57cec5SDimitry Andric   if (!AFI->isThumbFunction())
8270b57cec5SDimitry Andric     Done = rewriteARMFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
8280b57cec5SDimitry Andric   else {
8290b57cec5SDimitry Andric     assert(AFI->isThumb2Function());
8308bcb0991SDimitry Andric     Done = rewriteT2FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII, this);
8310b57cec5SDimitry Andric   }
8320b57cec5SDimitry Andric   if (Done)
833bdd1243dSDimitry Andric     return false;
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric   // If we get here, the immediate doesn't fit into the instruction.  We folded
8360b57cec5SDimitry Andric   // as much as possible above, handle the rest, providing a register that is
8370b57cec5SDimitry Andric   // SP+LargeImm.
8388bcb0991SDimitry Andric   assert(
8398bcb0991SDimitry Andric       (Offset ||
8400b57cec5SDimitry Andric        (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
8418bcb0991SDimitry Andric        (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6 ||
8428bcb0991SDimitry Andric        (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrModeT2_i7 ||
8438bcb0991SDimitry Andric        (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrModeT2_i7s2 ||
8448bcb0991SDimitry Andric        (MI.getDesc().TSFlags & ARMII::AddrModeMask) ==
8458bcb0991SDimitry Andric            ARMII::AddrModeT2_i7s4) &&
8460b57cec5SDimitry Andric       "This code isn't needed if offset already handled!");
8470b57cec5SDimitry Andric 
8480b57cec5SDimitry Andric   unsigned ScratchReg = 0;
8490b57cec5SDimitry Andric   int PIdx = MI.findFirstPredOperandIdx();
8500b57cec5SDimitry Andric   ARMCC::CondCodes Pred = (PIdx == -1)
8510b57cec5SDimitry Andric     ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
8520b57cec5SDimitry Andric   Register PredReg = (PIdx == -1) ? Register() : MI.getOperand(PIdx+1).getReg();
8538bcb0991SDimitry Andric 
8548bcb0991SDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
8558bcb0991SDimitry Andric   const TargetRegisterClass *RegClass =
8568bcb0991SDimitry Andric       TII.getRegClass(MCID, FIOperandNum, this, *MI.getParent()->getParent());
8578bcb0991SDimitry Andric 
858bdd1243dSDimitry Andric   if (Offset == 0 && (FrameReg.isVirtual() || RegClass->contains(FrameReg)))
8590b57cec5SDimitry Andric     // Must be addrmode4/6.
8600b57cec5SDimitry Andric     MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false, false);
8610b57cec5SDimitry Andric   else {
8628bcb0991SDimitry Andric     ScratchReg = MF.getRegInfo().createVirtualRegister(RegClass);
8630b57cec5SDimitry Andric     if (!AFI->isThumbFunction())
8640b57cec5SDimitry Andric       emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
8650b57cec5SDimitry Andric                               Offset, Pred, PredReg, TII);
8660b57cec5SDimitry Andric     else {
8670b57cec5SDimitry Andric       assert(AFI->isThumb2Function());
8680b57cec5SDimitry Andric       emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
8690b57cec5SDimitry Andric                              Offset, Pred, PredReg, TII);
8700b57cec5SDimitry Andric     }
8710b57cec5SDimitry Andric     // Update the original instruction to use the scratch register.
8720b57cec5SDimitry Andric     MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false,true);
8730b57cec5SDimitry Andric   }
874bdd1243dSDimitry Andric   return false;
8750b57cec5SDimitry Andric }
8760b57cec5SDimitry Andric 
shouldCoalesce(MachineInstr * MI,const TargetRegisterClass * SrcRC,unsigned SubReg,const TargetRegisterClass * DstRC,unsigned DstSubReg,const TargetRegisterClass * NewRC,LiveIntervals & LIS) const8770b57cec5SDimitry Andric bool ARMBaseRegisterInfo::shouldCoalesce(MachineInstr *MI,
8780b57cec5SDimitry Andric                                   const TargetRegisterClass *SrcRC,
8790b57cec5SDimitry Andric                                   unsigned SubReg,
8800b57cec5SDimitry Andric                                   const TargetRegisterClass *DstRC,
8810b57cec5SDimitry Andric                                   unsigned DstSubReg,
8820b57cec5SDimitry Andric                                   const TargetRegisterClass *NewRC,
8830b57cec5SDimitry Andric                                   LiveIntervals &LIS) const {
8840b57cec5SDimitry Andric   auto MBB = MI->getParent();
8850b57cec5SDimitry Andric   auto MF = MBB->getParent();
8860b57cec5SDimitry Andric   const MachineRegisterInfo &MRI = MF->getRegInfo();
8870b57cec5SDimitry Andric   // If not copying into a sub-register this should be ok because we shouldn't
8880b57cec5SDimitry Andric   // need to split the reg.
8890b57cec5SDimitry Andric   if (!DstSubReg)
8900b57cec5SDimitry Andric     return true;
8910b57cec5SDimitry Andric   // Small registers don't frequently cause a problem, so we can coalesce them.
8920b57cec5SDimitry Andric   if (getRegSizeInBits(*NewRC) < 256 && getRegSizeInBits(*DstRC) < 256 &&
8930b57cec5SDimitry Andric       getRegSizeInBits(*SrcRC) < 256)
8940b57cec5SDimitry Andric     return true;
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric   auto NewRCWeight =
8970b57cec5SDimitry Andric               MRI.getTargetRegisterInfo()->getRegClassWeight(NewRC);
8980b57cec5SDimitry Andric   auto SrcRCWeight =
8990b57cec5SDimitry Andric               MRI.getTargetRegisterInfo()->getRegClassWeight(SrcRC);
9000b57cec5SDimitry Andric   auto DstRCWeight =
9010b57cec5SDimitry Andric               MRI.getTargetRegisterInfo()->getRegClassWeight(DstRC);
9020b57cec5SDimitry Andric   // If the source register class is more expensive than the destination, the
9030b57cec5SDimitry Andric   // coalescing is probably profitable.
9040b57cec5SDimitry Andric   if (SrcRCWeight.RegWeight > NewRCWeight.RegWeight)
9050b57cec5SDimitry Andric     return true;
9060b57cec5SDimitry Andric   if (DstRCWeight.RegWeight > NewRCWeight.RegWeight)
9070b57cec5SDimitry Andric     return true;
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric   // If the register allocator isn't constrained, we can always allow coalescing
9100b57cec5SDimitry Andric   // unfortunately we don't know yet if we will be constrained.
9110b57cec5SDimitry Andric   // The goal of this heuristic is to restrict how many expensive registers
9120b57cec5SDimitry Andric   // we allow to coalesce in a given basic block.
9130b57cec5SDimitry Andric   auto AFI = MF->getInfo<ARMFunctionInfo>();
9140b57cec5SDimitry Andric   auto It = AFI->getCoalescedWeight(MBB);
9150b57cec5SDimitry Andric 
9160b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\tARM::shouldCoalesce - Coalesced Weight: "
9170b57cec5SDimitry Andric                     << It->second << "\n");
9180b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\tARM::shouldCoalesce - Reg Weight: "
9190b57cec5SDimitry Andric                     << NewRCWeight.RegWeight << "\n");
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   // This number is the largest round number that which meets the criteria:
9220b57cec5SDimitry Andric   //  (1) addresses PR18825
9230b57cec5SDimitry Andric   //  (2) generates better code in some test cases (like vldm-shed-a9.ll)
9240b57cec5SDimitry Andric   //  (3) Doesn't regress any test cases (in-tree, test-suite, and SPEC)
9250b57cec5SDimitry Andric   // In practice the SizeMultiplier will only factor in for straight line code
9260b57cec5SDimitry Andric   // that uses a lot of NEON vectors, which isn't terribly common.
9270b57cec5SDimitry Andric   unsigned SizeMultiplier = MBB->size()/100;
9280b57cec5SDimitry Andric   SizeMultiplier = SizeMultiplier ? SizeMultiplier : 1;
9290b57cec5SDimitry Andric   if (It->second < NewRCWeight.WeightLimit * SizeMultiplier) {
9300b57cec5SDimitry Andric     It->second += NewRCWeight.RegWeight;
9310b57cec5SDimitry Andric     return true;
9320b57cec5SDimitry Andric   }
9330b57cec5SDimitry Andric   return false;
9340b57cec5SDimitry Andric }
935fe6060f1SDimitry Andric 
shouldRewriteCopySrc(const TargetRegisterClass * DefRC,unsigned DefSubReg,const TargetRegisterClass * SrcRC,unsigned SrcSubReg) const936fe6060f1SDimitry Andric bool ARMBaseRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
937fe6060f1SDimitry Andric                                                unsigned DefSubReg,
938fe6060f1SDimitry Andric                                                const TargetRegisterClass *SrcRC,
939fe6060f1SDimitry Andric                                                unsigned SrcSubReg) const {
940fe6060f1SDimitry Andric   // We can't extract an SPR from an arbitary DPR (as opposed to a DPR_VFP2).
941fe6060f1SDimitry Andric   if (DefRC == &ARM::SPRRegClass && DefSubReg == 0 &&
942fe6060f1SDimitry Andric       SrcRC == &ARM::DPRRegClass &&
943fe6060f1SDimitry Andric       (SrcSubReg == ARM::ssub_0 || SrcSubReg == ARM::ssub_1))
944fe6060f1SDimitry Andric     return false;
945fe6060f1SDimitry Andric 
946fe6060f1SDimitry Andric   return TargetRegisterInfo::shouldRewriteCopySrc(DefRC, DefSubReg,
947fe6060f1SDimitry Andric                                                   SrcRC, SrcSubReg);
948fe6060f1SDimitry Andric }
949