1 //===-- MipsMachineFunctionInfo.cpp - Private data used for Mips ----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "MipsMachineFunction.h" 10 #include "MCTargetDesc/MipsABIInfo.h" 11 #include "MipsSubtarget.h" 12 #include "MipsTargetMachine.h" 13 #include "llvm/CodeGen/MachineFrameInfo.h" 14 #include "llvm/CodeGen/MachineRegisterInfo.h" 15 #include "llvm/CodeGen/PseudoSourceValue.h" 16 #include "llvm/CodeGen/TargetRegisterInfo.h" 17 #include "llvm/Support/CommandLine.h" 18 19 using namespace llvm; 20 21 static cl::opt<bool> 22 FixGlobalBaseReg("mips-fix-global-base-reg", cl::Hidden, cl::init(true), 23 cl::desc("Always use $gp as the global base register.")); 24 25 MipsFunctionInfo::~MipsFunctionInfo() = default; 26 27 bool MipsFunctionInfo::globalBaseRegSet() const { 28 return GlobalBaseReg; 29 } 30 31 static const TargetRegisterClass &getGlobalBaseRegClass(MachineFunction &MF) { 32 auto &STI = static_cast<const MipsSubtarget &>(MF.getSubtarget()); 33 auto &TM = static_cast<const MipsTargetMachine &>(MF.getTarget()); 34 35 if (STI.inMips16Mode()) 36 return Mips::CPU16RegsRegClass; 37 38 if (STI.inMicroMipsMode()) 39 return Mips::GPRMM16RegClass; 40 41 if (TM.getABI().IsN64()) 42 return Mips::GPR64RegClass; 43 44 return Mips::GPR32RegClass; 45 } 46 47 unsigned MipsFunctionInfo::getGlobalBaseReg() { 48 if (!GlobalBaseReg) 49 GlobalBaseReg = 50 MF.getRegInfo().createVirtualRegister(&getGlobalBaseRegClass(MF)); 51 return GlobalBaseReg; 52 } 53 54 void MipsFunctionInfo::initGlobalBaseReg() { 55 if (!GlobalBaseReg) 56 return; 57 58 MachineBasicBlock &MBB = MF.front(); 59 MachineBasicBlock::iterator I = MBB.begin(); 60 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 61 const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo(); 62 DebugLoc DL; 63 unsigned V0, V1; 64 const TargetRegisterClass *RC; 65 const MipsABIInfo &ABI = 66 static_cast<const MipsTargetMachine &>(MF.getTarget()).getABI(); 67 RC = (ABI.IsN64()) ? &Mips::GPR64RegClass : &Mips::GPR32RegClass; 68 69 V0 = RegInfo.createVirtualRegister(RC); 70 V1 = RegInfo.createVirtualRegister(RC); 71 72 if (ABI.IsN64()) { 73 MF.getRegInfo().addLiveIn(Mips::T9_64); 74 MBB.addLiveIn(Mips::T9_64); 75 76 // lui $v0, %hi(%neg(%gp_rel(fname))) 77 // daddu $v1, $v0, $t9 78 // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname))) 79 const GlobalValue *FName = &MF.getFunction(); 80 BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0) 81 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI); 82 BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0) 83 .addReg(Mips::T9_64); 84 BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1) 85 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO); 86 return; 87 } 88 89 if (!MF.getTarget().isPositionIndependent()) { 90 // Set global register to __gnu_local_gp. 91 // 92 // lui $v0, %hi(__gnu_local_gp) 93 // addiu $globalbasereg, $v0, %lo(__gnu_local_gp) 94 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0) 95 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI); 96 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0) 97 .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO); 98 return; 99 } 100 101 MF.getRegInfo().addLiveIn(Mips::T9); 102 MBB.addLiveIn(Mips::T9); 103 104 if (ABI.IsN32()) { 105 // lui $v0, %hi(%neg(%gp_rel(fname))) 106 // addu $v1, $v0, $t9 107 // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname))) 108 const GlobalValue *FName = &MF.getFunction(); 109 BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0) 110 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI); 111 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9); 112 BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1) 113 .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO); 114 return; 115 } 116 117 assert(ABI.IsO32()); 118 119 // For O32 ABI, the following instruction sequence is emitted to initialize 120 // the global base register: 121 // 122 // 0. lui $2, %hi(_gp_disp) 123 // 1. addiu $2, $2, %lo(_gp_disp) 124 // 2. addu $globalbasereg, $2, $t9 125 // 126 // We emit only the last instruction here. 127 // 128 // GNU linker requires that the first two instructions appear at the beginning 129 // of a function and no instructions be inserted before or between them. 130 // The two instructions are emitted during lowering to MC layer in order to 131 // avoid any reordering. 132 // 133 // Register $2 (Mips::V0) is added to the list of live-in registers to ensure 134 // the value instruction 1 (addiu) defines is valid when instruction 2 (addu) 135 // reads it. 136 MF.getRegInfo().addLiveIn(Mips::V0); 137 MBB.addLiveIn(Mips::V0); 138 BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg) 139 .addReg(Mips::V0).addReg(Mips::T9); 140 } 141 142 void MipsFunctionInfo::createEhDataRegsFI() { 143 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 144 for (int I = 0; I < 4; ++I) { 145 const TargetRegisterClass &RC = 146 static_cast<const MipsTargetMachine &>(MF.getTarget()).getABI().IsN64() 147 ? Mips::GPR64RegClass 148 : Mips::GPR32RegClass; 149 150 EhDataRegFI[I] = MF.getFrameInfo().CreateStackObject(TRI.getSpillSize(RC), 151 TRI.getSpillAlignment(RC), false); 152 } 153 } 154 155 void MipsFunctionInfo::createISRRegFI() { 156 // ISRs require spill slots for Status & ErrorPC Coprocessor 0 registers. 157 // The current implementation only supports Mips32r2+ not Mips64rX. Status 158 // is always 32 bits, ErrorPC is 32 or 64 bits dependent on architecture, 159 // however Mips32r2+ is the supported architecture. 160 const TargetRegisterClass &RC = Mips::GPR32RegClass; 161 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 162 163 for (int I = 0; I < 2; ++I) 164 ISRDataRegFI[I] = MF.getFrameInfo().CreateStackObject( 165 TRI.getSpillSize(RC), TRI.getSpillAlignment(RC), false); 166 } 167 168 bool MipsFunctionInfo::isEhDataRegFI(int FI) const { 169 return CallsEhReturn && (FI == EhDataRegFI[0] || FI == EhDataRegFI[1] 170 || FI == EhDataRegFI[2] || FI == EhDataRegFI[3]); 171 } 172 173 bool MipsFunctionInfo::isISRRegFI(int FI) const { 174 return IsISR && (FI == ISRDataRegFI[0] || FI == ISRDataRegFI[1]); 175 } 176 MachinePointerInfo MipsFunctionInfo::callPtrInfo(const char *ES) { 177 return MachinePointerInfo(MF.getPSVManager().getExternalSymbolCallEntry(ES)); 178 } 179 180 MachinePointerInfo MipsFunctionInfo::callPtrInfo(const GlobalValue *GV) { 181 return MachinePointerInfo(MF.getPSVManager().getGlobalValueCallEntry(GV)); 182 } 183 184 int MipsFunctionInfo::getMoveF64ViaSpillFI(const TargetRegisterClass *RC) { 185 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); 186 if (MoveF64ViaSpillFI == -1) { 187 MoveF64ViaSpillFI = MF.getFrameInfo().CreateStackObject( 188 TRI.getSpillSize(*RC), TRI.getSpillAlignment(*RC), false); 189 } 190 return MoveF64ViaSpillFI; 191 } 192 193 void MipsFunctionInfo::anchor() {} 194