LLVM API Documentation
00001 //===-- MipsSEFrameLowering.cpp - Mips32/64 Frame Information -------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the Mips32/64 implementation of TargetFrameLowering class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "MipsSEFrameLowering.h" 00015 #include "MCTargetDesc/MipsBaseInfo.h" 00016 #include "MipsAnalyzeImmediate.h" 00017 #include "MipsMachineFunction.h" 00018 #include "MipsSEInstrInfo.h" 00019 #include "MipsSubtarget.h" 00020 #include "llvm/CodeGen/MachineFrameInfo.h" 00021 #include "llvm/CodeGen/MachineFunction.h" 00022 #include "llvm/CodeGen/MachineInstrBuilder.h" 00023 #include "llvm/CodeGen/MachineModuleInfo.h" 00024 #include "llvm/CodeGen/MachineRegisterInfo.h" 00025 #include "llvm/CodeGen/RegisterScavenging.h" 00026 #include "llvm/IR/DataLayout.h" 00027 #include "llvm/IR/Function.h" 00028 #include "llvm/Support/CommandLine.h" 00029 #include "llvm/Target/TargetOptions.h" 00030 00031 using namespace llvm; 00032 00033 namespace { 00034 typedef MachineBasicBlock::iterator Iter; 00035 00036 static std::pair<unsigned, unsigned> getMFHiLoOpc(unsigned Src) { 00037 if (Mips::ACC64RegClass.contains(Src)) 00038 return std::make_pair((unsigned)Mips::PseudoMFHI, 00039 (unsigned)Mips::PseudoMFLO); 00040 00041 if (Mips::ACC64DSPRegClass.contains(Src)) 00042 return std::make_pair((unsigned)Mips::MFHI_DSP, (unsigned)Mips::MFLO_DSP); 00043 00044 if (Mips::ACC128RegClass.contains(Src)) 00045 return std::make_pair((unsigned)Mips::PseudoMFHI64, 00046 (unsigned)Mips::PseudoMFLO64); 00047 00048 return std::make_pair(0, 0); 00049 } 00050 00051 /// Helper class to expand pseudos. 00052 class ExpandPseudo { 00053 public: 00054 ExpandPseudo(MachineFunction &MF); 00055 bool expand(); 00056 00057 private: 00058 bool expandInstr(MachineBasicBlock &MBB, Iter I); 00059 void expandLoadCCond(MachineBasicBlock &MBB, Iter I); 00060 void expandStoreCCond(MachineBasicBlock &MBB, Iter I); 00061 void expandLoadACC(MachineBasicBlock &MBB, Iter I, unsigned RegSize); 00062 void expandStoreACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc, 00063 unsigned MFLoOpc, unsigned RegSize); 00064 bool expandCopy(MachineBasicBlock &MBB, Iter I); 00065 bool expandCopyACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc, 00066 unsigned MFLoOpc); 00067 bool expandBuildPairF64(MachineBasicBlock &MBB, 00068 MachineBasicBlock::iterator I, bool FP64) const; 00069 bool expandExtractElementF64(MachineBasicBlock &MBB, 00070 MachineBasicBlock::iterator I, bool FP64) const; 00071 00072 MachineFunction &MF; 00073 MachineRegisterInfo &MRI; 00074 }; 00075 } 00076 00077 ExpandPseudo::ExpandPseudo(MachineFunction &MF_) 00078 : MF(MF_), MRI(MF.getRegInfo()) {} 00079 00080 bool ExpandPseudo::expand() { 00081 bool Expanded = false; 00082 00083 for (MachineFunction::iterator BB = MF.begin(), BBEnd = MF.end(); 00084 BB != BBEnd; ++BB) 00085 for (Iter I = BB->begin(), End = BB->end(); I != End;) 00086 Expanded |= expandInstr(*BB, I++); 00087 00088 return Expanded; 00089 } 00090 00091 bool ExpandPseudo::expandInstr(MachineBasicBlock &MBB, Iter I) { 00092 switch(I->getOpcode()) { 00093 case Mips::LOAD_CCOND_DSP: 00094 expandLoadCCond(MBB, I); 00095 break; 00096 case Mips::STORE_CCOND_DSP: 00097 expandStoreCCond(MBB, I); 00098 break; 00099 case Mips::LOAD_ACC64: 00100 case Mips::LOAD_ACC64DSP: 00101 expandLoadACC(MBB, I, 4); 00102 break; 00103 case Mips::LOAD_ACC128: 00104 expandLoadACC(MBB, I, 8); 00105 break; 00106 case Mips::STORE_ACC64: 00107 expandStoreACC(MBB, I, Mips::PseudoMFHI, Mips::PseudoMFLO, 4); 00108 break; 00109 case Mips::STORE_ACC64DSP: 00110 expandStoreACC(MBB, I, Mips::MFHI_DSP, Mips::MFLO_DSP, 4); 00111 break; 00112 case Mips::STORE_ACC128: 00113 expandStoreACC(MBB, I, Mips::PseudoMFHI64, Mips::PseudoMFLO64, 8); 00114 break; 00115 case Mips::BuildPairF64: 00116 if (expandBuildPairF64(MBB, I, false)) 00117 MBB.erase(I); 00118 return false; 00119 case Mips::BuildPairF64_64: 00120 if (expandBuildPairF64(MBB, I, true)) 00121 MBB.erase(I); 00122 return false; 00123 case Mips::ExtractElementF64: 00124 if (expandExtractElementF64(MBB, I, false)) 00125 MBB.erase(I); 00126 return false; 00127 case Mips::ExtractElementF64_64: 00128 if (expandExtractElementF64(MBB, I, true)) 00129 MBB.erase(I); 00130 return false; 00131 case TargetOpcode::COPY: 00132 if (!expandCopy(MBB, I)) 00133 return false; 00134 break; 00135 default: 00136 return false; 00137 } 00138 00139 MBB.erase(I); 00140 return true; 00141 } 00142 00143 void ExpandPseudo::expandLoadCCond(MachineBasicBlock &MBB, Iter I) { 00144 // load $vr, FI 00145 // copy ccond, $vr 00146 00147 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI()); 00148 00149 const MipsSEInstrInfo &TII = 00150 *static_cast<const MipsSEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 00151 const MipsRegisterInfo &RegInfo = *static_cast<const MipsRegisterInfo *>( 00152 MF.getSubtarget().getRegisterInfo()); 00153 00154 const TargetRegisterClass *RC = RegInfo.intRegClass(4); 00155 unsigned VR = MRI.createVirtualRegister(RC); 00156 unsigned Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex(); 00157 00158 TII.loadRegFromStack(MBB, I, VR, FI, RC, &RegInfo, 0); 00159 BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), Dst) 00160 .addReg(VR, RegState::Kill); 00161 } 00162 00163 void ExpandPseudo::expandStoreCCond(MachineBasicBlock &MBB, Iter I) { 00164 // copy $vr, ccond 00165 // store $vr, FI 00166 00167 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI()); 00168 00169 const MipsSEInstrInfo &TII = 00170 *static_cast<const MipsSEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 00171 const MipsRegisterInfo &RegInfo = *static_cast<const MipsRegisterInfo *>( 00172 MF.getSubtarget().getRegisterInfo()); 00173 00174 const TargetRegisterClass *RC = RegInfo.intRegClass(4); 00175 unsigned VR = MRI.createVirtualRegister(RC); 00176 unsigned Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex(); 00177 00178 BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), VR) 00179 .addReg(Src, getKillRegState(I->getOperand(0).isKill())); 00180 TII.storeRegToStack(MBB, I, VR, true, FI, RC, &RegInfo, 0); 00181 } 00182 00183 void ExpandPseudo::expandLoadACC(MachineBasicBlock &MBB, Iter I, 00184 unsigned RegSize) { 00185 // load $vr0, FI 00186 // copy lo, $vr0 00187 // load $vr1, FI + 4 00188 // copy hi, $vr1 00189 00190 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI()); 00191 00192 const MipsSEInstrInfo &TII = 00193 *static_cast<const MipsSEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 00194 const MipsRegisterInfo &RegInfo = *static_cast<const MipsRegisterInfo *>( 00195 MF.getSubtarget().getRegisterInfo()); 00196 00197 const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize); 00198 unsigned VR0 = MRI.createVirtualRegister(RC); 00199 unsigned VR1 = MRI.createVirtualRegister(RC); 00200 unsigned Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex(); 00201 unsigned Lo = RegInfo.getSubReg(Dst, Mips::sub_lo); 00202 unsigned Hi = RegInfo.getSubReg(Dst, Mips::sub_hi); 00203 DebugLoc DL = I->getDebugLoc(); 00204 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY); 00205 00206 TII.loadRegFromStack(MBB, I, VR0, FI, RC, &RegInfo, 0); 00207 BuildMI(MBB, I, DL, Desc, Lo).addReg(VR0, RegState::Kill); 00208 TII.loadRegFromStack(MBB, I, VR1, FI, RC, &RegInfo, RegSize); 00209 BuildMI(MBB, I, DL, Desc, Hi).addReg(VR1, RegState::Kill); 00210 } 00211 00212 void ExpandPseudo::expandStoreACC(MachineBasicBlock &MBB, Iter I, 00213 unsigned MFHiOpc, unsigned MFLoOpc, 00214 unsigned RegSize) { 00215 // mflo $vr0, src 00216 // store $vr0, FI 00217 // mfhi $vr1, src 00218 // store $vr1, FI + 4 00219 00220 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI()); 00221 00222 const MipsSEInstrInfo &TII = 00223 *static_cast<const MipsSEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 00224 const MipsRegisterInfo &RegInfo = *static_cast<const MipsRegisterInfo *>( 00225 MF.getSubtarget().getRegisterInfo()); 00226 00227 const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize); 00228 unsigned VR0 = MRI.createVirtualRegister(RC); 00229 unsigned VR1 = MRI.createVirtualRegister(RC); 00230 unsigned Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex(); 00231 unsigned SrcKill = getKillRegState(I->getOperand(0).isKill()); 00232 DebugLoc DL = I->getDebugLoc(); 00233 00234 BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src); 00235 TII.storeRegToStack(MBB, I, VR0, true, FI, RC, &RegInfo, 0); 00236 BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill); 00237 TII.storeRegToStack(MBB, I, VR1, true, FI, RC, &RegInfo, RegSize); 00238 } 00239 00240 bool ExpandPseudo::expandCopy(MachineBasicBlock &MBB, Iter I) { 00241 unsigned Src = I->getOperand(1).getReg(); 00242 std::pair<unsigned, unsigned> Opcodes = getMFHiLoOpc(Src); 00243 00244 if (!Opcodes.first) 00245 return false; 00246 00247 return expandCopyACC(MBB, I, Opcodes.first, Opcodes.second); 00248 } 00249 00250 bool ExpandPseudo::expandCopyACC(MachineBasicBlock &MBB, Iter I, 00251 unsigned MFHiOpc, unsigned MFLoOpc) { 00252 // mflo $vr0, src 00253 // copy dst_lo, $vr0 00254 // mfhi $vr1, src 00255 // copy dst_hi, $vr1 00256 00257 const MipsSEInstrInfo &TII = 00258 *static_cast<const MipsSEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 00259 const MipsRegisterInfo &RegInfo = *static_cast<const MipsRegisterInfo *>( 00260 MF.getSubtarget().getRegisterInfo()); 00261 00262 unsigned Dst = I->getOperand(0).getReg(), Src = I->getOperand(1).getReg(); 00263 unsigned VRegSize = RegInfo.getMinimalPhysRegClass(Dst)->getSize() / 2; 00264 const TargetRegisterClass *RC = RegInfo.intRegClass(VRegSize); 00265 unsigned VR0 = MRI.createVirtualRegister(RC); 00266 unsigned VR1 = MRI.createVirtualRegister(RC); 00267 unsigned SrcKill = getKillRegState(I->getOperand(1).isKill()); 00268 unsigned DstLo = RegInfo.getSubReg(Dst, Mips::sub_lo); 00269 unsigned DstHi = RegInfo.getSubReg(Dst, Mips::sub_hi); 00270 DebugLoc DL = I->getDebugLoc(); 00271 00272 BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src); 00273 BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstLo) 00274 .addReg(VR0, RegState::Kill); 00275 BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill); 00276 BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstHi) 00277 .addReg(VR1, RegState::Kill); 00278 return true; 00279 } 00280 00281 /// This method expands the same instruction that MipsSEInstrInfo:: 00282 /// expandBuildPairF64 does, for the case when ABI is fpxx and mthc1 is not 00283 /// available and the case where the ABI is FP64A. It is implemented here 00284 /// because frame indexes are eliminated before MipsSEInstrInfo:: 00285 /// expandBuildPairF64 is called. 00286 bool ExpandPseudo::expandBuildPairF64(MachineBasicBlock &MBB, 00287 MachineBasicBlock::iterator I, 00288 bool FP64) const { 00289 // For fpxx and when mthc1 is not available, use: 00290 // spill + reload via ldc1 00291 // 00292 // The case where dmtc1 is available doesn't need to be handled here 00293 // because it never creates a BuildPairF64 node. 00294 // 00295 // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence 00296 // for odd-numbered double precision values (because the lower 32-bits is 00297 // transferred with mtc1 which is redirected to the upper half of the even 00298 // register). Unfortunately, we have to make this decision before register 00299 // allocation so for now we use a spill/reload sequence for all 00300 // double-precision values in regardless of being an odd/even register. 00301 00302 const TargetMachine &TM = MF.getTarget(); 00303 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>(); 00304 if ((Subtarget.isABI_FPXX() && !Subtarget.hasMTHC1()) || 00305 (FP64 && !Subtarget.useOddSPReg())) { 00306 const MipsSEInstrInfo &TII = *static_cast<const MipsSEInstrInfo *>( 00307 TM.getSubtargetImpl()->getInstrInfo()); 00308 const MipsRegisterInfo &TRI = *static_cast<const MipsRegisterInfo *>( 00309 TM.getSubtargetImpl()->getRegisterInfo()); 00310 00311 unsigned DstReg = I->getOperand(0).getReg(); 00312 unsigned LoReg = I->getOperand(1).getReg(); 00313 unsigned HiReg = I->getOperand(2).getReg(); 00314 00315 // It should be impossible to have FGR64 on MIPS-II or MIPS32r1 (which are 00316 // the cases where mthc1 is not available). 64-bit architectures and 00317 // MIPS32r2 or later can use FGR64 though. 00318 assert(Subtarget.isGP64bit() || Subtarget.hasMTHC1() || 00319 !Subtarget.isFP64bit()); 00320 00321 const TargetRegisterClass *RC = &Mips::GPR32RegClass; 00322 const TargetRegisterClass *RC2 = 00323 FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass; 00324 00325 // We re-use the same spill slot each time so that the stack frame doesn't 00326 // grow too much in functions with a large number of moves. 00327 int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(RC2); 00328 TII.storeRegToStack(MBB, I, LoReg, I->getOperand(1).isKill(), FI, RC, &TRI, 00329 0); 00330 TII.storeRegToStack(MBB, I, HiReg, I->getOperand(2).isKill(), FI, RC, &TRI, 00331 4); 00332 TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, &TRI, 0); 00333 return true; 00334 } 00335 00336 return false; 00337 } 00338 00339 /// This method expands the same instruction that MipsSEInstrInfo:: 00340 /// expandExtractElementF64 does, for the case when ABI is fpxx and mfhc1 is not 00341 /// available and the case where the ABI is FP64A. It is implemented here 00342 /// because frame indexes are eliminated before MipsSEInstrInfo:: 00343 /// expandExtractElementF64 is called. 00344 bool ExpandPseudo::expandExtractElementF64(MachineBasicBlock &MBB, 00345 MachineBasicBlock::iterator I, 00346 bool FP64) const { 00347 // For fpxx and when mfhc1 is not available, use: 00348 // spill + reload via ldc1 00349 // 00350 // The case where dmfc1 is available doesn't need to be handled here 00351 // because it never creates a ExtractElementF64 node. 00352 // 00353 // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence 00354 // for odd-numbered double precision values (because the lower 32-bits is 00355 // transferred with mfc1 which is redirected to the upper half of the even 00356 // register). Unfortunately, we have to make this decision before register 00357 // allocation so for now we use a spill/reload sequence for all 00358 // double-precision values in regardless of being an odd/even register. 00359 00360 const TargetMachine &TM = MF.getTarget(); 00361 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>(); 00362 if ((Subtarget.isABI_FPXX() && !Subtarget.hasMTHC1()) || 00363 (FP64 && !Subtarget.useOddSPReg())) { 00364 const MipsSEInstrInfo &TII = *static_cast<const MipsSEInstrInfo *>( 00365 TM.getSubtargetImpl()->getInstrInfo()); 00366 const MipsRegisterInfo &TRI = *static_cast<const MipsRegisterInfo *>( 00367 TM.getSubtargetImpl()->getRegisterInfo()); 00368 00369 unsigned DstReg = I->getOperand(0).getReg(); 00370 unsigned SrcReg = I->getOperand(1).getReg(); 00371 unsigned N = I->getOperand(2).getImm(); 00372 00373 // It should be impossible to have FGR64 on MIPS-II or MIPS32r1 (which are 00374 // the cases where mfhc1 is not available). 64-bit architectures and 00375 // MIPS32r2 or later can use FGR64 though. 00376 assert(Subtarget.isGP64bit() || Subtarget.hasMTHC1() || 00377 !Subtarget.isFP64bit()); 00378 00379 const TargetRegisterClass *RC = 00380 FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass; 00381 const TargetRegisterClass *RC2 = &Mips::GPR32RegClass; 00382 00383 // We re-use the same spill slot each time so that the stack frame doesn't 00384 // grow too much in functions with a large number of moves. 00385 int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(RC); 00386 TII.storeRegToStack(MBB, I, SrcReg, I->getOperand(1).isKill(), FI, RC, &TRI, 00387 0); 00388 TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, &TRI, N * 4); 00389 return true; 00390 } 00391 00392 return false; 00393 } 00394 00395 MipsSEFrameLowering::MipsSEFrameLowering(const MipsSubtarget &STI) 00396 : MipsFrameLowering(STI, STI.stackAlignment()) {} 00397 00398 unsigned MipsSEFrameLowering::ehDataReg(unsigned I) const { 00399 static const unsigned EhDataReg[] = { 00400 Mips::A0, Mips::A1, Mips::A2, Mips::A3 00401 }; 00402 static const unsigned EhDataReg64[] = { 00403 Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64 00404 }; 00405 00406 return STI.isABI_N64() ? EhDataReg64[I] : EhDataReg[I]; 00407 } 00408 00409 void MipsSEFrameLowering::emitPrologue(MachineFunction &MF) const { 00410 MachineBasicBlock &MBB = MF.front(); 00411 MachineFrameInfo *MFI = MF.getFrameInfo(); 00412 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 00413 00414 const MipsSEInstrInfo &TII = 00415 *static_cast<const MipsSEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 00416 const MipsRegisterInfo &RegInfo = *static_cast<const MipsRegisterInfo *>( 00417 MF.getSubtarget().getRegisterInfo()); 00418 00419 MachineBasicBlock::iterator MBBI = MBB.begin(); 00420 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 00421 unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP; 00422 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP; 00423 unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO; 00424 unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu; 00425 00426 // First, compute final stack size. 00427 uint64_t StackSize = MFI->getStackSize(); 00428 00429 // No need to allocate space on the stack. 00430 if (StackSize == 0 && !MFI->adjustsStack()) return; 00431 00432 MachineModuleInfo &MMI = MF.getMMI(); 00433 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 00434 MachineLocation DstML, SrcML; 00435 00436 // Adjust stack. 00437 TII.adjustStackPtr(SP, -StackSize, MBB, MBBI); 00438 00439 // emit ".cfi_def_cfa_offset StackSize" 00440 unsigned CFIIndex = MMI.addFrameInst( 00441 MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize)); 00442 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 00443 .addCFIIndex(CFIIndex); 00444 00445 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 00446 00447 if (CSI.size()) { 00448 // Find the instruction past the last instruction that saves a callee-saved 00449 // register to the stack. 00450 for (unsigned i = 0; i < CSI.size(); ++i) 00451 ++MBBI; 00452 00453 // Iterate over list of callee-saved registers and emit .cfi_offset 00454 // directives. 00455 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(), 00456 E = CSI.end(); I != E; ++I) { 00457 int64_t Offset = MFI->getObjectOffset(I->getFrameIdx()); 00458 unsigned Reg = I->getReg(); 00459 00460 // If Reg is a double precision register, emit two cfa_offsets, 00461 // one for each of the paired single precision registers. 00462 if (Mips::AFGR64RegClass.contains(Reg)) { 00463 unsigned Reg0 = 00464 MRI->getDwarfRegNum(RegInfo.getSubReg(Reg, Mips::sub_lo), true); 00465 unsigned Reg1 = 00466 MRI->getDwarfRegNum(RegInfo.getSubReg(Reg, Mips::sub_hi), true); 00467 00468 if (!STI.isLittle()) 00469 std::swap(Reg0, Reg1); 00470 00471 unsigned CFIIndex = MMI.addFrameInst( 00472 MCCFIInstruction::createOffset(nullptr, Reg0, Offset)); 00473 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 00474 .addCFIIndex(CFIIndex); 00475 00476 CFIIndex = MMI.addFrameInst( 00477 MCCFIInstruction::createOffset(nullptr, Reg1, Offset + 4)); 00478 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 00479 .addCFIIndex(CFIIndex); 00480 } else if (Mips::FGR64RegClass.contains(Reg)) { 00481 unsigned Reg0 = MRI->getDwarfRegNum(Reg, true); 00482 unsigned Reg1 = MRI->getDwarfRegNum(Reg, true) + 1; 00483 00484 if (!STI.isLittle()) 00485 std::swap(Reg0, Reg1); 00486 00487 unsigned CFIIndex = MMI.addFrameInst( 00488 MCCFIInstruction::createOffset(nullptr, Reg0, Offset)); 00489 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 00490 .addCFIIndex(CFIIndex); 00491 00492 CFIIndex = MMI.addFrameInst( 00493 MCCFIInstruction::createOffset(nullptr, Reg1, Offset + 4)); 00494 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 00495 .addCFIIndex(CFIIndex); 00496 } else { 00497 // Reg is either in GPR32 or FGR32. 00498 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset( 00499 nullptr, MRI->getDwarfRegNum(Reg, 1), Offset)); 00500 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 00501 .addCFIIndex(CFIIndex); 00502 } 00503 } 00504 } 00505 00506 if (MipsFI->callsEhReturn()) { 00507 const TargetRegisterClass *RC = STI.isABI_N64() ? 00508 &Mips::GPR64RegClass : &Mips::GPR32RegClass; 00509 00510 // Insert instructions that spill eh data registers. 00511 for (int I = 0; I < 4; ++I) { 00512 if (!MBB.isLiveIn(ehDataReg(I))) 00513 MBB.addLiveIn(ehDataReg(I)); 00514 TII.storeRegToStackSlot(MBB, MBBI, ehDataReg(I), false, 00515 MipsFI->getEhDataRegFI(I), RC, &RegInfo); 00516 } 00517 00518 // Emit .cfi_offset directives for eh data registers. 00519 for (int I = 0; I < 4; ++I) { 00520 int64_t Offset = MFI->getObjectOffset(MipsFI->getEhDataRegFI(I)); 00521 unsigned Reg = MRI->getDwarfRegNum(ehDataReg(I), true); 00522 unsigned CFIIndex = MMI.addFrameInst( 00523 MCCFIInstruction::createOffset(nullptr, Reg, Offset)); 00524 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 00525 .addCFIIndex(CFIIndex); 00526 } 00527 } 00528 00529 // if framepointer enabled, set it to point to the stack pointer. 00530 if (hasFP(MF)) { 00531 // Insert instruction "move $fp, $sp" at this location. 00532 BuildMI(MBB, MBBI, dl, TII.get(ADDu), FP).addReg(SP).addReg(ZERO) 00533 .setMIFlag(MachineInstr::FrameSetup); 00534 00535 // emit ".cfi_def_cfa_register $fp" 00536 unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister( 00537 nullptr, MRI->getDwarfRegNum(FP, true))); 00538 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION)) 00539 .addCFIIndex(CFIIndex); 00540 } 00541 } 00542 00543 void MipsSEFrameLowering::emitEpilogue(MachineFunction &MF, 00544 MachineBasicBlock &MBB) const { 00545 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 00546 MachineFrameInfo *MFI = MF.getFrameInfo(); 00547 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 00548 00549 const MipsSEInstrInfo &TII = 00550 *static_cast<const MipsSEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 00551 const MipsRegisterInfo &RegInfo = *static_cast<const MipsRegisterInfo *>( 00552 MF.getSubtarget().getRegisterInfo()); 00553 00554 DebugLoc dl = MBBI->getDebugLoc(); 00555 unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP; 00556 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP; 00557 unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO; 00558 unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu; 00559 00560 // if framepointer enabled, restore the stack pointer. 00561 if (hasFP(MF)) { 00562 // Find the first instruction that restores a callee-saved register. 00563 MachineBasicBlock::iterator I = MBBI; 00564 00565 for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i) 00566 --I; 00567 00568 // Insert instruction "move $sp, $fp" at this location. 00569 BuildMI(MBB, I, dl, TII.get(ADDu), SP).addReg(FP).addReg(ZERO); 00570 } 00571 00572 if (MipsFI->callsEhReturn()) { 00573 const TargetRegisterClass *RC = STI.isABI_N64() ? 00574 &Mips::GPR64RegClass : &Mips::GPR32RegClass; 00575 00576 // Find first instruction that restores a callee-saved register. 00577 MachineBasicBlock::iterator I = MBBI; 00578 for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i) 00579 --I; 00580 00581 // Insert instructions that restore eh data registers. 00582 for (int J = 0; J < 4; ++J) { 00583 TII.loadRegFromStackSlot(MBB, I, ehDataReg(J), MipsFI->getEhDataRegFI(J), 00584 RC, &RegInfo); 00585 } 00586 } 00587 00588 // Get the number of bytes from FrameInfo 00589 uint64_t StackSize = MFI->getStackSize(); 00590 00591 if (!StackSize) 00592 return; 00593 00594 // Adjust stack. 00595 TII.adjustStackPtr(SP, StackSize, MBB, MBBI); 00596 } 00597 00598 bool MipsSEFrameLowering:: 00599 spillCalleeSavedRegisters(MachineBasicBlock &MBB, 00600 MachineBasicBlock::iterator MI, 00601 const std::vector<CalleeSavedInfo> &CSI, 00602 const TargetRegisterInfo *TRI) const { 00603 MachineFunction *MF = MBB.getParent(); 00604 MachineBasicBlock *EntryBlock = MF->begin(); 00605 const TargetInstrInfo &TII = *MF->getSubtarget().getInstrInfo(); 00606 00607 for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 00608 // Add the callee-saved register as live-in. Do not add if the register is 00609 // RA and return address is taken, because it has already been added in 00610 // method MipsTargetLowering::LowerRETURNADDR. 00611 // It's killed at the spill, unless the register is RA and return address 00612 // is taken. 00613 unsigned Reg = CSI[i].getReg(); 00614 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA || Reg == Mips::RA_64) 00615 && MF->getFrameInfo()->isReturnAddressTaken(); 00616 if (!IsRAAndRetAddrIsTaken) 00617 EntryBlock->addLiveIn(Reg); 00618 00619 // Insert the spill to the stack frame. 00620 bool IsKill = !IsRAAndRetAddrIsTaken; 00621 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 00622 TII.storeRegToStackSlot(*EntryBlock, MI, Reg, IsKill, 00623 CSI[i].getFrameIdx(), RC, TRI); 00624 } 00625 00626 return true; 00627 } 00628 00629 bool 00630 MipsSEFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 00631 const MachineFrameInfo *MFI = MF.getFrameInfo(); 00632 00633 // Reserve call frame if the size of the maximum call frame fits into 16-bit 00634 // immediate field and there are no variable sized objects on the stack. 00635 // Make sure the second register scavenger spill slot can be accessed with one 00636 // instruction. 00637 return isInt<16>(MFI->getMaxCallFrameSize() + getStackAlignment()) && 00638 !MFI->hasVarSizedObjects(); 00639 } 00640 00641 // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions 00642 void MipsSEFrameLowering:: 00643 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 00644 MachineBasicBlock::iterator I) const { 00645 const MipsSEInstrInfo &TII = 00646 *static_cast<const MipsSEInstrInfo *>(MF.getSubtarget().getInstrInfo()); 00647 00648 if (!hasReservedCallFrame(MF)) { 00649 int64_t Amount = I->getOperand(0).getImm(); 00650 00651 if (I->getOpcode() == Mips::ADJCALLSTACKDOWN) 00652 Amount = -Amount; 00653 00654 unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP; 00655 TII.adjustStackPtr(SP, Amount, MBB, I); 00656 } 00657 00658 MBB.erase(I); 00659 } 00660 00661 void MipsSEFrameLowering:: 00662 processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 00663 RegScavenger *RS) const { 00664 MachineRegisterInfo &MRI = MF.getRegInfo(); 00665 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); 00666 unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP; 00667 00668 // Mark $fp as used if function has dedicated frame pointer. 00669 if (hasFP(MF)) 00670 MRI.setPhysRegUsed(FP); 00671 00672 // Create spill slots for eh data registers if function calls eh_return. 00673 if (MipsFI->callsEhReturn()) 00674 MipsFI->createEhDataRegsFI(); 00675 00676 // Expand pseudo instructions which load, store or copy accumulators. 00677 // Add an emergency spill slot if a pseudo was expanded. 00678 if (ExpandPseudo(MF).expand()) { 00679 // The spill slot should be half the size of the accumulator. If target is 00680 // mips64, it should be 64-bit, otherwise it should be 32-bt. 00681 const TargetRegisterClass *RC = STI.hasMips64() ? 00682 &Mips::GPR64RegClass : &Mips::GPR32RegClass; 00683 int FI = MF.getFrameInfo()->CreateStackObject(RC->getSize(), 00684 RC->getAlignment(), false); 00685 RS->addScavengingFrameIndex(FI); 00686 } 00687 00688 // Set scavenging frame index if necessary. 00689 uint64_t MaxSPOffset = MF.getInfo<MipsFunctionInfo>()->getIncomingArgSize() + 00690 estimateStackSize(MF); 00691 00692 if (isInt<16>(MaxSPOffset)) 00693 return; 00694 00695 const TargetRegisterClass *RC = STI.isABI_N64() ? 00696 &Mips::GPR64RegClass : &Mips::GPR32RegClass; 00697 int FI = MF.getFrameInfo()->CreateStackObject(RC->getSize(), 00698 RC->getAlignment(), false); 00699 RS->addScavengingFrameIndex(FI); 00700 } 00701 00702 const MipsFrameLowering * 00703 llvm::createMipsSEFrameLowering(const MipsSubtarget &ST) { 00704 return new MipsSEFrameLowering(ST); 00705 }