1 //===-- MipsSEFrameLowering.cpp - Mips32/64 Frame Information -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Mips32/64 implementation of TargetFrameLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/MipsABIInfo.h"
15 #include "MipsMachineFunction.h"
16 #include "MipsRegisterInfo.h"
17 #include "MipsSEFrameLowering.h"
18 #include "MipsSEInstrInfo.h"
19 #include "MipsSubtarget.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/RegisterScavenging.h"
32 #include "llvm/IR/DebugLoc.h"
33 #include "llvm/IR/Function.h"
34 #include "llvm/MC/MCDwarf.h"
35 #include "llvm/MC/MCRegisterInfo.h"
36 #include "llvm/MC/MachineLocation.h"
37 #include "llvm/Support/CodeGen.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/MathExtras.h"
40 #include "llvm/Target/TargetInstrInfo.h"
41 #include "llvm/Target/TargetRegisterInfo.h"
42 #include "llvm/Target/TargetSubtargetInfo.h"
50 static std::pair<unsigned, unsigned> getMFHiLoOpc(unsigned Src) {
51 if (Mips::ACC64RegClass.contains(Src))
52 return std::make_pair((unsigned)Mips::PseudoMFHI,
53 (unsigned)Mips::PseudoMFLO);
55 if (Mips::ACC64DSPRegClass.contains(Src))
56 return std::make_pair((unsigned)Mips::MFHI_DSP, (unsigned)Mips::MFLO_DSP);
58 if (Mips::ACC128RegClass.contains(Src))
59 return std::make_pair((unsigned)Mips::PseudoMFHI64,
60 (unsigned)Mips::PseudoMFLO64);
62 return std::make_pair(0, 0);
67 /// Helper class to expand pseudos.
70 ExpandPseudo(MachineFunction &MF);
74 typedef MachineBasicBlock::iterator Iter;
76 bool expandInstr(MachineBasicBlock &MBB, Iter I);
77 void expandLoadCCond(MachineBasicBlock &MBB, Iter I);
78 void expandStoreCCond(MachineBasicBlock &MBB, Iter I);
79 void expandLoadACC(MachineBasicBlock &MBB, Iter I, unsigned RegSize);
80 void expandStoreACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc,
81 unsigned MFLoOpc, unsigned RegSize);
82 bool expandCopy(MachineBasicBlock &MBB, Iter I);
83 bool expandCopyACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc,
85 bool expandBuildPairF64(MachineBasicBlock &MBB,
86 MachineBasicBlock::iterator I, bool FP64) const;
87 bool expandExtractElementF64(MachineBasicBlock &MBB,
88 MachineBasicBlock::iterator I, bool FP64) const;
91 MachineRegisterInfo &MRI;
92 const MipsSubtarget &Subtarget;
93 const MipsSEInstrInfo &TII;
94 const MipsRegisterInfo &RegInfo;
97 } // end anonymous namespace
99 ExpandPseudo::ExpandPseudo(MachineFunction &MF_)
100 : MF(MF_), MRI(MF.getRegInfo()),
101 Subtarget(static_cast<const MipsSubtarget &>(MF.getSubtarget())),
102 TII(*static_cast<const MipsSEInstrInfo *>(Subtarget.getInstrInfo())),
103 RegInfo(*Subtarget.getRegisterInfo()) {}
105 bool ExpandPseudo::expand() {
106 bool Expanded = false;
108 for (auto &MBB : MF) {
109 for (Iter I = MBB.begin(), End = MBB.end(); I != End;)
110 Expanded |= expandInstr(MBB, I++);
116 bool ExpandPseudo::expandInstr(MachineBasicBlock &MBB, Iter I) {
117 switch(I->getOpcode()) {
118 case Mips::LOAD_CCOND_DSP:
119 expandLoadCCond(MBB, I);
121 case Mips::STORE_CCOND_DSP:
122 expandStoreCCond(MBB, I);
124 case Mips::LOAD_ACC64:
125 case Mips::LOAD_ACC64DSP:
126 expandLoadACC(MBB, I, 4);
128 case Mips::LOAD_ACC128:
129 expandLoadACC(MBB, I, 8);
131 case Mips::STORE_ACC64:
132 expandStoreACC(MBB, I, Mips::PseudoMFHI, Mips::PseudoMFLO, 4);
134 case Mips::STORE_ACC64DSP:
135 expandStoreACC(MBB, I, Mips::MFHI_DSP, Mips::MFLO_DSP, 4);
137 case Mips::STORE_ACC128:
138 expandStoreACC(MBB, I, Mips::PseudoMFHI64, Mips::PseudoMFLO64, 8);
140 case Mips::BuildPairF64:
141 if (expandBuildPairF64(MBB, I, false))
144 case Mips::BuildPairF64_64:
145 if (expandBuildPairF64(MBB, I, true))
148 case Mips::ExtractElementF64:
149 if (expandExtractElementF64(MBB, I, false))
152 case Mips::ExtractElementF64_64:
153 if (expandExtractElementF64(MBB, I, true))
156 case TargetOpcode::COPY:
157 if (!expandCopy(MBB, I))
168 void ExpandPseudo::expandLoadCCond(MachineBasicBlock &MBB, Iter I) {
172 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
174 const TargetRegisterClass *RC = RegInfo.intRegClass(4);
175 unsigned VR = MRI.createVirtualRegister(RC);
176 unsigned Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
178 TII.loadRegFromStack(MBB, I, VR, FI, RC, &RegInfo, 0);
179 BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), Dst)
180 .addReg(VR, RegState::Kill);
183 void ExpandPseudo::expandStoreCCond(MachineBasicBlock &MBB, Iter I) {
187 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
189 const TargetRegisterClass *RC = RegInfo.intRegClass(4);
190 unsigned VR = MRI.createVirtualRegister(RC);
191 unsigned Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
193 BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), VR)
194 .addReg(Src, getKillRegState(I->getOperand(0).isKill()));
195 TII.storeRegToStack(MBB, I, VR, true, FI, RC, &RegInfo, 0);
198 void ExpandPseudo::expandLoadACC(MachineBasicBlock &MBB, Iter I,
205 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
207 const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize);
208 unsigned VR0 = MRI.createVirtualRegister(RC);
209 unsigned VR1 = MRI.createVirtualRegister(RC);
210 unsigned Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
211 unsigned Lo = RegInfo.getSubReg(Dst, Mips::sub_lo);
212 unsigned Hi = RegInfo.getSubReg(Dst, Mips::sub_hi);
213 DebugLoc DL = I->getDebugLoc();
214 const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
216 TII.loadRegFromStack(MBB, I, VR0, FI, RC, &RegInfo, 0);
217 BuildMI(MBB, I, DL, Desc, Lo).addReg(VR0, RegState::Kill);
218 TII.loadRegFromStack(MBB, I, VR1, FI, RC, &RegInfo, RegSize);
219 BuildMI(MBB, I, DL, Desc, Hi).addReg(VR1, RegState::Kill);
222 void ExpandPseudo::expandStoreACC(MachineBasicBlock &MBB, Iter I,
223 unsigned MFHiOpc, unsigned MFLoOpc,
228 // store $vr1, FI + 4
230 assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
232 const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize);
233 unsigned VR0 = MRI.createVirtualRegister(RC);
234 unsigned VR1 = MRI.createVirtualRegister(RC);
235 unsigned Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
236 unsigned SrcKill = getKillRegState(I->getOperand(0).isKill());
237 DebugLoc DL = I->getDebugLoc();
239 BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src);
240 TII.storeRegToStack(MBB, I, VR0, true, FI, RC, &RegInfo, 0);
241 BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill);
242 TII.storeRegToStack(MBB, I, VR1, true, FI, RC, &RegInfo, RegSize);
245 bool ExpandPseudo::expandCopy(MachineBasicBlock &MBB, Iter I) {
246 unsigned Src = I->getOperand(1).getReg();
247 std::pair<unsigned, unsigned> Opcodes = getMFHiLoOpc(Src);
252 return expandCopyACC(MBB, I, Opcodes.first, Opcodes.second);
255 bool ExpandPseudo::expandCopyACC(MachineBasicBlock &MBB, Iter I,
256 unsigned MFHiOpc, unsigned MFLoOpc) {
262 unsigned Dst = I->getOperand(0).getReg(), Src = I->getOperand(1).getReg();
263 unsigned VRegSize = RegInfo.getMinimalPhysRegClass(Dst)->getSize() / 2;
264 const TargetRegisterClass *RC = RegInfo.intRegClass(VRegSize);
265 unsigned VR0 = MRI.createVirtualRegister(RC);
266 unsigned VR1 = MRI.createVirtualRegister(RC);
267 unsigned SrcKill = getKillRegState(I->getOperand(1).isKill());
268 unsigned DstLo = RegInfo.getSubReg(Dst, Mips::sub_lo);
269 unsigned DstHi = RegInfo.getSubReg(Dst, Mips::sub_hi);
270 DebugLoc DL = I->getDebugLoc();
272 BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src);
273 BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstLo)
274 .addReg(VR0, RegState::Kill);
275 BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill);
276 BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstHi)
277 .addReg(VR1, RegState::Kill);
281 /// This method expands the same instruction that MipsSEInstrInfo::
282 /// expandBuildPairF64 does, for the case when ABI is fpxx and mthc1 is not
283 /// available and the case where the ABI is FP64A. It is implemented here
284 /// because frame indexes are eliminated before MipsSEInstrInfo::
285 /// expandBuildPairF64 is called.
286 bool ExpandPseudo::expandBuildPairF64(MachineBasicBlock &MBB,
287 MachineBasicBlock::iterator I,
289 // For fpxx and when mthc1 is not available, use:
290 // spill + reload via ldc1
292 // The case where dmtc1 is available doesn't need to be handled here
293 // because it never creates a BuildPairF64 node.
295 // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence
296 // for odd-numbered double precision values (because the lower 32-bits is
297 // transferred with mtc1 which is redirected to the upper half of the even
298 // register). Unfortunately, we have to make this decision before register
299 // allocation so for now we use a spill/reload sequence for all
300 // double-precision values in regardless of being an odd/even register.
301 if ((Subtarget.isABI_FPXX() && !Subtarget.hasMTHC1()) ||
302 (FP64 && !Subtarget.useOddSPReg())) {
303 unsigned DstReg = I->getOperand(0).getReg();
304 unsigned LoReg = I->getOperand(1).getReg();
305 unsigned HiReg = I->getOperand(2).getReg();
307 // It should be impossible to have FGR64 on MIPS-II or MIPS32r1 (which are
308 // the cases where mthc1 is not available). 64-bit architectures and
309 // MIPS32r2 or later can use FGR64 though.
310 assert(Subtarget.isGP64bit() || Subtarget.hasMTHC1() ||
311 !Subtarget.isFP64bit());
313 const TargetRegisterClass *RC = &Mips::GPR32RegClass;
314 const TargetRegisterClass *RC2 =
315 FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
317 // We re-use the same spill slot each time so that the stack frame doesn't
318 // grow too much in functions with a large number of moves.
319 int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(RC2);
320 if (!Subtarget.isLittle())
321 std::swap(LoReg, HiReg);
322 TII.storeRegToStack(MBB, I, LoReg, I->getOperand(1).isKill(), FI, RC,
324 TII.storeRegToStack(MBB, I, HiReg, I->getOperand(2).isKill(), FI, RC,
326 TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, &RegInfo, 0);
333 /// This method expands the same instruction that MipsSEInstrInfo::
334 /// expandExtractElementF64 does, for the case when ABI is fpxx and mfhc1 is not
335 /// available and the case where the ABI is FP64A. It is implemented here
336 /// because frame indexes are eliminated before MipsSEInstrInfo::
337 /// expandExtractElementF64 is called.
338 bool ExpandPseudo::expandExtractElementF64(MachineBasicBlock &MBB,
339 MachineBasicBlock::iterator I,
341 const MachineOperand &Op1 = I->getOperand(1);
342 const MachineOperand &Op2 = I->getOperand(2);
344 if ((Op1.isReg() && Op1.isUndef()) || (Op2.isReg() && Op2.isUndef())) {
345 unsigned DstReg = I->getOperand(0).getReg();
346 BuildMI(MBB, I, I->getDebugLoc(), TII.get(Mips::IMPLICIT_DEF), DstReg);
350 // For fpxx and when mfhc1 is not available, use:
351 // spill + reload via ldc1
353 // The case where dmfc1 is available doesn't need to be handled here
354 // because it never creates a ExtractElementF64 node.
356 // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence
357 // for odd-numbered double precision values (because the lower 32-bits is
358 // transferred with mfc1 which is redirected to the upper half of the even
359 // register). Unfortunately, we have to make this decision before register
360 // allocation so for now we use a spill/reload sequence for all
361 // double-precision values in regardless of being an odd/even register.
363 if ((Subtarget.isABI_FPXX() && !Subtarget.hasMTHC1()) ||
364 (FP64 && !Subtarget.useOddSPReg())) {
365 unsigned DstReg = I->getOperand(0).getReg();
366 unsigned SrcReg = Op1.getReg();
367 unsigned N = Op2.getImm();
368 int64_t Offset = 4 * (Subtarget.isLittle() ? N : (1 - N));
370 // It should be impossible to have FGR64 on MIPS-II or MIPS32r1 (which are
371 // the cases where mfhc1 is not available). 64-bit architectures and
372 // MIPS32r2 or later can use FGR64 though.
373 assert(Subtarget.isGP64bit() || Subtarget.hasMTHC1() ||
374 !Subtarget.isFP64bit());
376 const TargetRegisterClass *RC =
377 FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
378 const TargetRegisterClass *RC2 = &Mips::GPR32RegClass;
380 // We re-use the same spill slot each time so that the stack frame doesn't
381 // grow too much in functions with a large number of moves.
382 int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(RC);
383 TII.storeRegToStack(MBB, I, SrcReg, Op1.isKill(), FI, RC, &RegInfo, 0);
384 TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, &RegInfo, Offset);
391 MipsSEFrameLowering::MipsSEFrameLowering(const MipsSubtarget &STI)
392 : MipsFrameLowering(STI, STI.stackAlignment()) {}
394 void MipsSEFrameLowering::emitPrologue(MachineFunction &MF,
395 MachineBasicBlock &MBB) const {
396 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
397 MachineFrameInfo &MFI = MF.getFrameInfo();
398 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
400 const MipsSEInstrInfo &TII =
401 *static_cast<const MipsSEInstrInfo *>(STI.getInstrInfo());
402 const MipsRegisterInfo &RegInfo =
403 *static_cast<const MipsRegisterInfo *>(STI.getRegisterInfo());
405 MachineBasicBlock::iterator MBBI = MBB.begin();
407 MipsABIInfo ABI = STI.getABI();
408 unsigned SP = ABI.GetStackPtr();
409 unsigned FP = ABI.GetFramePtr();
410 unsigned ZERO = ABI.GetNullPtr();
411 unsigned MOVE = ABI.GetGPRMoveOp();
412 unsigned ADDiu = ABI.GetPtrAddiuOp();
413 unsigned AND = ABI.IsN64() ? Mips::AND64 : Mips::AND;
415 const TargetRegisterClass *RC = ABI.ArePtrs64bit() ?
416 &Mips::GPR64RegClass : &Mips::GPR32RegClass;
418 // First, compute final stack size.
419 uint64_t StackSize = MFI.getStackSize();
421 // No need to allocate space on the stack.
422 if (StackSize == 0 && !MFI.adjustsStack()) return;
424 MachineModuleInfo &MMI = MF.getMMI();
425 const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
426 MachineLocation DstML, SrcML;
429 TII.adjustStackPtr(SP, -StackSize, MBB, MBBI);
431 // emit ".cfi_def_cfa_offset StackSize"
432 unsigned CFIIndex = MF.addFrameInst(
433 MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
434 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
435 .addCFIIndex(CFIIndex);
437 if (MF.getFunction()->hasFnAttribute("interrupt"))
438 emitInterruptPrologueStub(MF, MBB);
440 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
443 // Find the instruction past the last instruction that saves a callee-saved
444 // register to the stack.
445 for (unsigned i = 0; i < CSI.size(); ++i)
448 // Iterate over list of callee-saved registers and emit .cfi_offset
450 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
451 E = CSI.end(); I != E; ++I) {
452 int64_t Offset = MFI.getObjectOffset(I->getFrameIdx());
453 unsigned Reg = I->getReg();
455 // If Reg is a double precision register, emit two cfa_offsets,
456 // one for each of the paired single precision registers.
457 if (Mips::AFGR64RegClass.contains(Reg)) {
459 MRI->getDwarfRegNum(RegInfo.getSubReg(Reg, Mips::sub_lo), true);
461 MRI->getDwarfRegNum(RegInfo.getSubReg(Reg, Mips::sub_hi), true);
464 std::swap(Reg0, Reg1);
466 unsigned CFIIndex = MF.addFrameInst(
467 MCCFIInstruction::createOffset(nullptr, Reg0, Offset));
468 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
469 .addCFIIndex(CFIIndex);
471 CFIIndex = MF.addFrameInst(
472 MCCFIInstruction::createOffset(nullptr, Reg1, Offset + 4));
473 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
474 .addCFIIndex(CFIIndex);
475 } else if (Mips::FGR64RegClass.contains(Reg)) {
476 unsigned Reg0 = MRI->getDwarfRegNum(Reg, true);
477 unsigned Reg1 = MRI->getDwarfRegNum(Reg, true) + 1;
480 std::swap(Reg0, Reg1);
482 unsigned CFIIndex = MF.addFrameInst(
483 MCCFIInstruction::createOffset(nullptr, Reg0, Offset));
484 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
485 .addCFIIndex(CFIIndex);
487 CFIIndex = MF.addFrameInst(
488 MCCFIInstruction::createOffset(nullptr, Reg1, Offset + 4));
489 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
490 .addCFIIndex(CFIIndex);
492 // Reg is either in GPR32 or FGR32.
493 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createOffset(
494 nullptr, MRI->getDwarfRegNum(Reg, true), Offset));
495 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
496 .addCFIIndex(CFIIndex);
501 if (MipsFI->callsEhReturn()) {
502 // Insert instructions that spill eh data registers.
503 for (int I = 0; I < 4; ++I) {
504 if (!MBB.isLiveIn(ABI.GetEhDataReg(I)))
505 MBB.addLiveIn(ABI.GetEhDataReg(I));
506 TII.storeRegToStackSlot(MBB, MBBI, ABI.GetEhDataReg(I), false,
507 MipsFI->getEhDataRegFI(I), RC, &RegInfo);
510 // Emit .cfi_offset directives for eh data registers.
511 for (int I = 0; I < 4; ++I) {
512 int64_t Offset = MFI.getObjectOffset(MipsFI->getEhDataRegFI(I));
513 unsigned Reg = MRI->getDwarfRegNum(ABI.GetEhDataReg(I), true);
514 unsigned CFIIndex = MF.addFrameInst(
515 MCCFIInstruction::createOffset(nullptr, Reg, Offset));
516 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
517 .addCFIIndex(CFIIndex);
521 // if framepointer enabled, set it to point to the stack pointer.
523 // Insert instruction "move $fp, $sp" at this location.
524 BuildMI(MBB, MBBI, dl, TII.get(MOVE), FP).addReg(SP).addReg(ZERO)
525 .setMIFlag(MachineInstr::FrameSetup);
527 // emit ".cfi_def_cfa_register $fp"
528 unsigned CFIIndex = MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(
529 nullptr, MRI->getDwarfRegNum(FP, true)));
530 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
531 .addCFIIndex(CFIIndex);
533 if (RegInfo.needsStackRealignment(MF)) {
534 // addiu $Reg, $zero, -MaxAlignment
535 // andi $sp, $sp, $Reg
536 unsigned VR = MF.getRegInfo().createVirtualRegister(RC);
537 assert(isInt<16>(MFI.getMaxAlignment()) &&
538 "Function's alignment size requirement is not supported.");
539 int MaxAlign = -(int)MFI.getMaxAlignment();
541 BuildMI(MBB, MBBI, dl, TII.get(ADDiu), VR).addReg(ZERO) .addImm(MaxAlign);
542 BuildMI(MBB, MBBI, dl, TII.get(AND), SP).addReg(SP).addReg(VR);
546 unsigned BP = STI.isABI_N64() ? Mips::S7_64 : Mips::S7;
547 BuildMI(MBB, MBBI, dl, TII.get(MOVE), BP)
555 void MipsSEFrameLowering::emitInterruptPrologueStub(
556 MachineFunction &MF, MachineBasicBlock &MBB) const {
557 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
558 MachineBasicBlock::iterator MBBI = MBB.begin();
559 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
561 // Report an error the target doesn't support Mips32r2 or later.
562 // The epilogue relies on the use of the "ehb" to clear execution
563 // hazards. Pre R2 Mips relies on an implementation defined number
564 // of "ssnop"s to clear the execution hazard. Support for ssnop hazard
565 // clearing is not provided so reject that configuration.
566 if (!STI.hasMips32r2())
568 "\"interrupt\" attribute is not supported on pre-MIPS32R2 or "
571 // The GP register contains the "user" value, so we cannot perform
572 // any gp relative loads until we restore the "kernel" or "system" gp
573 // value. Until support is written we shall only accept the static
575 if ((STI.getRelocationModel() != Reloc::Static))
576 report_fatal_error("\"interrupt\" attribute is only supported for the "
577 "static relocation model on MIPS at the present time.");
579 if (!STI.isABI_O32() || STI.hasMips64())
580 report_fatal_error("\"interrupt\" attribute is only supported for the "
581 "O32 ABI on MIPS32R2+ at the present time.");
583 // Perform ISR handling like GCC
585 MF.getFunction()->getFnAttribute("interrupt").getValueAsString();
586 const TargetRegisterClass *PtrRC = &Mips::GPR32RegClass;
588 // EIC interrupt handling needs to read the Cause register to disable
590 if (IntKind == "eic") {
591 // Coprocessor registers are always live per se.
592 MBB.addLiveIn(Mips::COP013);
593 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MFC0), Mips::K0)
594 .addReg(Mips::COP013)
596 .setMIFlag(MachineInstr::FrameSetup);
598 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::EXT), Mips::K0)
602 .setMIFlag(MachineInstr::FrameSetup);
605 // Fetch and spill EPC
606 MBB.addLiveIn(Mips::COP014);
607 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MFC0), Mips::K1)
608 .addReg(Mips::COP014)
610 .setMIFlag(MachineInstr::FrameSetup);
612 STI.getInstrInfo()->storeRegToStack(MBB, MBBI, Mips::K1, false,
613 MipsFI->getISRRegFI(0), PtrRC,
614 STI.getRegisterInfo(), 0);
616 // Fetch and Spill Status
617 MBB.addLiveIn(Mips::COP012);
618 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MFC0), Mips::K1)
619 .addReg(Mips::COP012)
621 .setMIFlag(MachineInstr::FrameSetup);
623 STI.getInstrInfo()->storeRegToStack(MBB, MBBI, Mips::K1, false,
624 MipsFI->getISRRegFI(1), PtrRC,
625 STI.getRegisterInfo(), 0);
627 // Build the configuration for disabling lower priority interrupts. Non EIC
628 // interrupts need to be masked off with zero, EIC from the Cause register.
629 unsigned InsPosition = 8;
630 unsigned InsSize = 0;
631 unsigned SrcReg = Mips::ZERO;
633 // If the interrupt we're tied to is the EIC, switch the source for the
634 // masking off interrupts to the cause register.
635 if (IntKind == "eic") {
640 InsSize = StringSwitch<unsigned>(IntKind)
650 assert(InsSize != 0 && "Unknown interrupt type!");
652 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::INS), Mips::K1)
657 .setMIFlag(MachineInstr::FrameSetup);
659 // Mask off KSU, ERL, EXL
660 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::INS), Mips::K1)
665 .setMIFlag(MachineInstr::FrameSetup);
667 // Disable the FPU as we are not spilling those register sets.
668 if (!STI.useSoftFloat())
669 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::INS), Mips::K1)
674 .setMIFlag(MachineInstr::FrameSetup);
676 // Set the new status
677 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MTC0), Mips::COP012)
680 .setMIFlag(MachineInstr::FrameSetup);
683 void MipsSEFrameLowering::emitEpilogue(MachineFunction &MF,
684 MachineBasicBlock &MBB) const {
685 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
686 MachineFrameInfo &MFI = MF.getFrameInfo();
687 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
689 const MipsSEInstrInfo &TII =
690 *static_cast<const MipsSEInstrInfo *>(STI.getInstrInfo());
691 const MipsRegisterInfo &RegInfo =
692 *static_cast<const MipsRegisterInfo *>(STI.getRegisterInfo());
694 DebugLoc DL = MBBI->getDebugLoc();
695 MipsABIInfo ABI = STI.getABI();
696 unsigned SP = ABI.GetStackPtr();
697 unsigned FP = ABI.GetFramePtr();
698 unsigned ZERO = ABI.GetNullPtr();
699 unsigned MOVE = ABI.GetGPRMoveOp();
701 // if framepointer enabled, restore the stack pointer.
703 // Find the first instruction that restores a callee-saved register.
704 MachineBasicBlock::iterator I = MBBI;
706 for (unsigned i = 0; i < MFI.getCalleeSavedInfo().size(); ++i)
709 // Insert instruction "move $sp, $fp" at this location.
710 BuildMI(MBB, I, DL, TII.get(MOVE), SP).addReg(FP).addReg(ZERO);
713 if (MipsFI->callsEhReturn()) {
714 const TargetRegisterClass *RC =
715 ABI.ArePtrs64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
717 // Find first instruction that restores a callee-saved register.
718 MachineBasicBlock::iterator I = MBBI;
719 for (unsigned i = 0; i < MFI.getCalleeSavedInfo().size(); ++i)
722 // Insert instructions that restore eh data registers.
723 for (int J = 0; J < 4; ++J) {
724 TII.loadRegFromStackSlot(MBB, I, ABI.GetEhDataReg(J),
725 MipsFI->getEhDataRegFI(J), RC, &RegInfo);
729 if (MF.getFunction()->hasFnAttribute("interrupt"))
730 emitInterruptEpilogueStub(MF, MBB);
732 // Get the number of bytes from FrameInfo
733 uint64_t StackSize = MFI.getStackSize();
739 TII.adjustStackPtr(SP, StackSize, MBB, MBBI);
742 void MipsSEFrameLowering::emitInterruptEpilogueStub(
743 MachineFunction &MF, MachineBasicBlock &MBB) const {
744 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
745 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
746 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
748 // Perform ISR handling like GCC
749 const TargetRegisterClass *PtrRC = &Mips::GPR32RegClass;
751 // Disable Interrupts.
752 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::DI), Mips::ZERO);
753 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::EHB));
756 STI.getInstrInfo()->loadRegFromStackSlot(MBB, MBBI, Mips::K1,
757 MipsFI->getISRRegFI(0), PtrRC,
758 STI.getRegisterInfo());
759 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MTC0), Mips::COP014)
764 STI.getInstrInfo()->loadRegFromStackSlot(MBB, MBBI, Mips::K1,
765 MipsFI->getISRRegFI(1), PtrRC,
766 STI.getRegisterInfo());
767 BuildMI(MBB, MBBI, DL, STI.getInstrInfo()->get(Mips::MTC0), Mips::COP012)
772 int MipsSEFrameLowering::getFrameIndexReference(const MachineFunction &MF,
774 unsigned &FrameReg) const {
775 const MachineFrameInfo &MFI = MF.getFrameInfo();
776 MipsABIInfo ABI = STI.getABI();
778 if (MFI.isFixedObjectIndex(FI))
779 FrameReg = hasFP(MF) ? ABI.GetFramePtr() : ABI.GetStackPtr();
781 FrameReg = hasBP(MF) ? ABI.GetBasePtr() : ABI.GetStackPtr();
783 return MFI.getObjectOffset(FI) + MFI.getStackSize() -
784 getOffsetOfLocalArea() + MFI.getOffsetAdjustment();
787 bool MipsSEFrameLowering::
788 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
789 MachineBasicBlock::iterator MI,
790 const std::vector<CalleeSavedInfo> &CSI,
791 const TargetRegisterInfo *TRI) const {
792 MachineFunction *MF = MBB.getParent();
793 MachineBasicBlock *EntryBlock = &MF->front();
794 const TargetInstrInfo &TII = *STI.getInstrInfo();
796 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
797 // Add the callee-saved register as live-in. Do not add if the register is
798 // RA and return address is taken, because it has already been added in
799 // method MipsTargetLowering::lowerRETURNADDR.
800 // It's killed at the spill, unless the register is RA and return address
802 unsigned Reg = CSI[i].getReg();
803 bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA || Reg == Mips::RA_64)
804 && MF->getFrameInfo().isReturnAddressTaken();
805 if (!IsRAAndRetAddrIsTaken)
806 EntryBlock->addLiveIn(Reg);
808 // ISRs require HI/LO to be spilled into kernel registers to be then
809 // spilled to the stack frame.
810 bool IsLOHI = (Reg == Mips::LO0 || Reg == Mips::LO0_64 ||
811 Reg == Mips::HI0 || Reg == Mips::HI0_64);
812 const Function *Func = MBB.getParent()->getFunction();
813 if (IsLOHI && Func->hasFnAttribute("interrupt")) {
814 DebugLoc DL = MI->getDebugLoc();
817 if (!STI.getABI().ArePtrs64bit()) {
818 Op = (Reg == Mips::HI0) ? Mips::MFHI : Mips::MFLO;
821 Op = (Reg == Mips::HI0) ? Mips::MFHI64 : Mips::MFLO64;
824 BuildMI(MBB, MI, DL, TII.get(Op), Mips::K0)
825 .setMIFlag(MachineInstr::FrameSetup);
828 // Insert the spill to the stack frame.
829 bool IsKill = !IsRAAndRetAddrIsTaken;
830 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
831 TII.storeRegToStackSlot(*EntryBlock, MI, Reg, IsKill,
832 CSI[i].getFrameIdx(), RC, TRI);
839 MipsSEFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
840 const MachineFrameInfo &MFI = MF.getFrameInfo();
841 // Reserve call frame if the size of the maximum call frame fits into 16-bit
842 // immediate field and there are no variable sized objects on the stack.
843 // Make sure the second register scavenger spill slot can be accessed with one
845 return isInt<16>(MFI.getMaxCallFrameSize() + getStackAlignment()) &&
846 !MFI.hasVarSizedObjects();
849 /// Mark \p Reg and all registers aliasing it in the bitset.
850 static void setAliasRegs(MachineFunction &MF, BitVector &SavedRegs,
852 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
853 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
857 void MipsSEFrameLowering::determineCalleeSaves(MachineFunction &MF,
858 BitVector &SavedRegs,
859 RegScavenger *RS) const {
860 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
861 MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
862 MipsABIInfo ABI = STI.getABI();
863 unsigned FP = ABI.GetFramePtr();
864 unsigned BP = ABI.IsN64() ? Mips::S7_64 : Mips::S7;
866 // Mark $fp as used if function has dedicated frame pointer.
868 setAliasRegs(MF, SavedRegs, FP);
869 // Mark $s7 as used if function has dedicated base pointer.
871 setAliasRegs(MF, SavedRegs, BP);
873 // Create spill slots for eh data registers if function calls eh_return.
874 if (MipsFI->callsEhReturn())
875 MipsFI->createEhDataRegsFI();
877 // Create spill slots for Coprocessor 0 registers if function is an ISR.
879 MipsFI->createISRRegFI();
881 // Expand pseudo instructions which load, store or copy accumulators.
882 // Add an emergency spill slot if a pseudo was expanded.
883 if (ExpandPseudo(MF).expand()) {
884 // The spill slot should be half the size of the accumulator. If target is
885 // mips64, it should be 64-bit, otherwise it should be 32-bt.
886 const TargetRegisterClass *RC = STI.hasMips64() ?
887 &Mips::GPR64RegClass : &Mips::GPR32RegClass;
888 int FI = MF.getFrameInfo().CreateStackObject(RC->getSize(),
889 RC->getAlignment(), false);
890 RS->addScavengingFrameIndex(FI);
893 // Set scavenging frame index if necessary.
894 uint64_t MaxSPOffset = MF.getInfo<MipsFunctionInfo>()->getIncomingArgSize() +
895 estimateStackSize(MF);
897 if (isInt<16>(MaxSPOffset))
900 const TargetRegisterClass *RC =
901 ABI.ArePtrs64bit() ? &Mips::GPR64RegClass : &Mips::GPR32RegClass;
902 int FI = MF.getFrameInfo().CreateStackObject(RC->getSize(),
903 RC->getAlignment(), false);
904 RS->addScavengingFrameIndex(FI);
907 const MipsFrameLowering *
908 llvm::createMipsSEFrameLowering(const MipsSubtarget &ST) {
909 return new MipsSEFrameLowering(ST);