Remove the TargetMachine forwards for TargetSubtargetInfo based
[oota-llvm.git] / lib / Target / Mips / MipsSEFrameLowering.cpp
1 //===-- MipsSEFrameLowering.cpp - Mips32/64 Frame Information -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the Mips32/64 implementation of TargetFrameLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsSEFrameLowering.h"
15 #include "MCTargetDesc/MipsBaseInfo.h"
16 #include "MipsAnalyzeImmediate.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsSEInstrInfo.h"
19 #include "MipsSubtarget.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/RegisterScavenging.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Target/TargetOptions.h"
30
31 using namespace llvm;
32
33 namespace {
34 typedef MachineBasicBlock::iterator Iter;
35
36 static std::pair<unsigned, unsigned> getMFHiLoOpc(unsigned Src) {
37   if (Mips::ACC64RegClass.contains(Src))
38     return std::make_pair((unsigned)Mips::PseudoMFHI,
39                           (unsigned)Mips::PseudoMFLO);
40
41   if (Mips::ACC64DSPRegClass.contains(Src))
42     return std::make_pair((unsigned)Mips::MFHI_DSP, (unsigned)Mips::MFLO_DSP);
43
44   if (Mips::ACC128RegClass.contains(Src))
45     return std::make_pair((unsigned)Mips::PseudoMFHI64,
46                           (unsigned)Mips::PseudoMFLO64);
47
48   return std::make_pair(0, 0);
49 }
50
51 /// Helper class to expand pseudos.
52 class ExpandPseudo {
53 public:
54   ExpandPseudo(MachineFunction &MF);
55   bool expand();
56
57 private:
58   bool expandInstr(MachineBasicBlock &MBB, Iter I);
59   void expandLoadCCond(MachineBasicBlock &MBB, Iter I);
60   void expandStoreCCond(MachineBasicBlock &MBB, Iter I);
61   void expandLoadACC(MachineBasicBlock &MBB, Iter I, unsigned RegSize);
62   void expandStoreACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc,
63                       unsigned MFLoOpc, unsigned RegSize);
64   bool expandCopy(MachineBasicBlock &MBB, Iter I);
65   bool expandCopyACC(MachineBasicBlock &MBB, Iter I, unsigned MFHiOpc,
66                      unsigned MFLoOpc);
67   bool expandBuildPairF64(MachineBasicBlock &MBB,
68                           MachineBasicBlock::iterator I, bool FP64) const;
69   bool expandExtractElementF64(MachineBasicBlock &MBB,
70                                MachineBasicBlock::iterator I, bool FP64) const;
71
72   MachineFunction &MF;
73   MachineRegisterInfo &MRI;
74 };
75 }
76
77 ExpandPseudo::ExpandPseudo(MachineFunction &MF_)
78   : MF(MF_), MRI(MF.getRegInfo()) {}
79
80 bool ExpandPseudo::expand() {
81   bool Expanded = false;
82
83   for (MachineFunction::iterator BB = MF.begin(), BBEnd = MF.end();
84        BB != BBEnd; ++BB)
85     for (Iter I = BB->begin(), End = BB->end(); I != End;)
86       Expanded |= expandInstr(*BB, I++);
87
88   return Expanded;
89 }
90
91 bool ExpandPseudo::expandInstr(MachineBasicBlock &MBB, Iter I) {
92   switch(I->getOpcode()) {
93   case Mips::LOAD_CCOND_DSP:
94     expandLoadCCond(MBB, I);
95     break;
96   case Mips::STORE_CCOND_DSP:
97     expandStoreCCond(MBB, I);
98     break;
99   case Mips::LOAD_ACC64:
100   case Mips::LOAD_ACC64DSP:
101     expandLoadACC(MBB, I, 4);
102     break;
103   case Mips::LOAD_ACC128:
104     expandLoadACC(MBB, I, 8);
105     break;
106   case Mips::STORE_ACC64:
107     expandStoreACC(MBB, I, Mips::PseudoMFHI, Mips::PseudoMFLO, 4);
108     break;
109   case Mips::STORE_ACC64DSP:
110     expandStoreACC(MBB, I, Mips::MFHI_DSP, Mips::MFLO_DSP, 4);
111     break;
112   case Mips::STORE_ACC128:
113     expandStoreACC(MBB, I, Mips::PseudoMFHI64, Mips::PseudoMFLO64, 8);
114     break;
115   case Mips::BuildPairF64:
116     if (expandBuildPairF64(MBB, I, false))
117       MBB.erase(I);
118     return false;
119   case Mips::BuildPairF64_64:
120     if (expandBuildPairF64(MBB, I, true))
121       MBB.erase(I);
122     return false;
123   case Mips::ExtractElementF64:
124     if (expandExtractElementF64(MBB, I, false))
125       MBB.erase(I);
126     return false;
127   case Mips::ExtractElementF64_64:
128     if (expandExtractElementF64(MBB, I, true))
129       MBB.erase(I);
130     return false;
131   case TargetOpcode::COPY:
132     if (!expandCopy(MBB, I))
133       return false;
134     break;
135   default:
136     return false;
137   }
138
139   MBB.erase(I);
140   return true;
141 }
142
143 void ExpandPseudo::expandLoadCCond(MachineBasicBlock &MBB, Iter I) {
144   //  load $vr, FI
145   //  copy ccond, $vr
146
147   assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
148
149   const MipsSEInstrInfo &TII =
150       *static_cast<const MipsSEInstrInfo *>(
151           MF.getTarget().getSubtargetImpl()->getInstrInfo());
152   const MipsRegisterInfo &RegInfo =
153       *static_cast<const MipsRegisterInfo *>(
154           MF.getTarget().getSubtargetImpl()->getRegisterInfo());
155
156   const TargetRegisterClass *RC = RegInfo.intRegClass(4);
157   unsigned VR = MRI.createVirtualRegister(RC);
158   unsigned Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
159
160   TII.loadRegFromStack(MBB, I, VR, FI, RC, &RegInfo, 0);
161   BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), Dst)
162     .addReg(VR, RegState::Kill);
163 }
164
165 void ExpandPseudo::expandStoreCCond(MachineBasicBlock &MBB, Iter I) {
166   //  copy $vr, ccond
167   //  store $vr, FI
168
169   assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
170
171   const MipsSEInstrInfo &TII =
172       *static_cast<const MipsSEInstrInfo *>(
173           MF.getTarget().getSubtargetImpl()->getInstrInfo());
174   const MipsRegisterInfo &RegInfo =
175       *static_cast<const MipsRegisterInfo *>(
176           MF.getTarget().getSubtargetImpl()->getRegisterInfo());
177
178   const TargetRegisterClass *RC = RegInfo.intRegClass(4);
179   unsigned VR = MRI.createVirtualRegister(RC);
180   unsigned Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
181
182   BuildMI(MBB, I, I->getDebugLoc(), TII.get(TargetOpcode::COPY), VR)
183     .addReg(Src, getKillRegState(I->getOperand(0).isKill()));
184   TII.storeRegToStack(MBB, I, VR, true, FI, RC, &RegInfo, 0);
185 }
186
187 void ExpandPseudo::expandLoadACC(MachineBasicBlock &MBB, Iter I,
188                                  unsigned RegSize) {
189   //  load $vr0, FI
190   //  copy lo, $vr0
191   //  load $vr1, FI + 4
192   //  copy hi, $vr1
193
194   assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
195
196   const MipsSEInstrInfo &TII =
197       *static_cast<const MipsSEInstrInfo *>(
198           MF.getTarget().getSubtargetImpl()->getInstrInfo());
199   const MipsRegisterInfo &RegInfo =
200       *static_cast<const MipsRegisterInfo *>(
201           MF.getTarget().getSubtargetImpl()->getRegisterInfo());
202
203   const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize);
204   unsigned VR0 = MRI.createVirtualRegister(RC);
205   unsigned VR1 = MRI.createVirtualRegister(RC);
206   unsigned Dst = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
207   unsigned Lo = RegInfo.getSubReg(Dst, Mips::sub_lo);
208   unsigned Hi = RegInfo.getSubReg(Dst, Mips::sub_hi);
209   DebugLoc DL = I->getDebugLoc();
210   const MCInstrDesc &Desc = TII.get(TargetOpcode::COPY);
211
212   TII.loadRegFromStack(MBB, I, VR0, FI, RC, &RegInfo, 0);
213   BuildMI(MBB, I, DL, Desc, Lo).addReg(VR0, RegState::Kill);
214   TII.loadRegFromStack(MBB, I, VR1, FI, RC, &RegInfo, RegSize);
215   BuildMI(MBB, I, DL, Desc, Hi).addReg(VR1, RegState::Kill);
216 }
217
218 void ExpandPseudo::expandStoreACC(MachineBasicBlock &MBB, Iter I,
219                                   unsigned MFHiOpc, unsigned MFLoOpc,
220                                   unsigned RegSize) {
221   //  mflo $vr0, src
222   //  store $vr0, FI
223   //  mfhi $vr1, src
224   //  store $vr1, FI + 4
225
226   assert(I->getOperand(0).isReg() && I->getOperand(1).isFI());
227
228   const MipsSEInstrInfo &TII =
229       *static_cast<const MipsSEInstrInfo *>(
230           MF.getTarget().getSubtargetImpl()->getInstrInfo());
231   const MipsRegisterInfo &RegInfo =
232       *static_cast<const MipsRegisterInfo *>(
233           MF.getTarget().getSubtargetImpl()->getRegisterInfo());
234
235   const TargetRegisterClass *RC = RegInfo.intRegClass(RegSize);
236   unsigned VR0 = MRI.createVirtualRegister(RC);
237   unsigned VR1 = MRI.createVirtualRegister(RC);
238   unsigned Src = I->getOperand(0).getReg(), FI = I->getOperand(1).getIndex();
239   unsigned SrcKill = getKillRegState(I->getOperand(0).isKill());
240   DebugLoc DL = I->getDebugLoc();
241
242   BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src);
243   TII.storeRegToStack(MBB, I, VR0, true, FI, RC, &RegInfo, 0);
244   BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill);
245   TII.storeRegToStack(MBB, I, VR1, true, FI, RC, &RegInfo, RegSize);
246 }
247
248 bool ExpandPseudo::expandCopy(MachineBasicBlock &MBB, Iter I) {
249   unsigned Src = I->getOperand(1).getReg();
250   std::pair<unsigned, unsigned> Opcodes = getMFHiLoOpc(Src);
251
252   if (!Opcodes.first)
253     return false;
254
255   return expandCopyACC(MBB, I, Opcodes.first, Opcodes.second);
256 }
257
258 bool ExpandPseudo::expandCopyACC(MachineBasicBlock &MBB, Iter I,
259                                  unsigned MFHiOpc, unsigned MFLoOpc) {
260   //  mflo $vr0, src
261   //  copy dst_lo, $vr0
262   //  mfhi $vr1, src
263   //  copy dst_hi, $vr1
264
265   const MipsSEInstrInfo &TII =
266       *static_cast<const MipsSEInstrInfo *>(
267           MF.getTarget().getSubtargetImpl()->getInstrInfo());
268   const MipsRegisterInfo &RegInfo =
269       *static_cast<const MipsRegisterInfo *>(
270           MF.getTarget().getSubtargetImpl()->getRegisterInfo());
271
272   unsigned Dst = I->getOperand(0).getReg(), Src = I->getOperand(1).getReg();
273   unsigned VRegSize = RegInfo.getMinimalPhysRegClass(Dst)->getSize() / 2;
274   const TargetRegisterClass *RC = RegInfo.intRegClass(VRegSize);
275   unsigned VR0 = MRI.createVirtualRegister(RC);
276   unsigned VR1 = MRI.createVirtualRegister(RC);
277   unsigned SrcKill = getKillRegState(I->getOperand(1).isKill());
278   unsigned DstLo = RegInfo.getSubReg(Dst, Mips::sub_lo);
279   unsigned DstHi = RegInfo.getSubReg(Dst, Mips::sub_hi);
280   DebugLoc DL = I->getDebugLoc();
281
282   BuildMI(MBB, I, DL, TII.get(MFLoOpc), VR0).addReg(Src);
283   BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstLo)
284     .addReg(VR0, RegState::Kill);
285   BuildMI(MBB, I, DL, TII.get(MFHiOpc), VR1).addReg(Src, SrcKill);
286   BuildMI(MBB, I, DL, TII.get(TargetOpcode::COPY), DstHi)
287     .addReg(VR1, RegState::Kill);
288   return true;
289 }
290
291 /// This method expands the same instruction that MipsSEInstrInfo::
292 /// expandBuildPairF64 does, for the case when ABI is fpxx and mthc1 is not
293 /// available and the case where the ABI is FP64A. It is implemented here
294 /// because frame indexes are eliminated before MipsSEInstrInfo::
295 /// expandBuildPairF64 is called.
296 bool ExpandPseudo::expandBuildPairF64(MachineBasicBlock &MBB,
297                                       MachineBasicBlock::iterator I,
298                                       bool FP64) const {
299   // For fpxx and when mthc1 is not available, use:
300   //   spill + reload via ldc1
301   //
302   // The case where dmtc1 is available doesn't need to be handled here
303   // because it never creates a BuildPairF64 node.
304   //
305   // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence
306   // for odd-numbered double precision values (because the lower 32-bits is
307   // transferred with mtc1 which is redirected to the upper half of the even
308   // register). Unfortunately, we have to make this decision before register
309   // allocation so for now we use a spill/reload sequence for all
310   // double-precision values in regardless of being an odd/even register.
311
312   const TargetMachine &TM = MF.getTarget();
313   const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
314   if ((Subtarget.isABI_FPXX() && !Subtarget.hasMTHC1()) ||
315       (FP64 && !Subtarget.useOddSPReg())) {
316     const MipsSEInstrInfo &TII = *static_cast<const MipsSEInstrInfo *>(
317                                      TM.getSubtargetImpl()->getInstrInfo());
318     const MipsRegisterInfo &TRI = *static_cast<const MipsRegisterInfo *>(
319                                       TM.getSubtargetImpl()->getRegisterInfo());
320
321     unsigned DstReg = I->getOperand(0).getReg();
322     unsigned LoReg = I->getOperand(1).getReg();
323     unsigned HiReg = I->getOperand(2).getReg();
324
325     // It should be impossible to have FGR64 on MIPS-II or MIPS32r1 (which are
326     // the cases where mthc1 is not available). 64-bit architectures and
327     // MIPS32r2 or later can use FGR64 though.
328     assert(Subtarget.isGP64bit() || Subtarget.hasMTHC1() ||
329            !Subtarget.isFP64bit());
330
331     const TargetRegisterClass *RC = &Mips::GPR32RegClass;
332     const TargetRegisterClass *RC2 =
333         FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
334
335     // We re-use the same spill slot each time so that the stack frame doesn't
336     // grow too much in functions with a large number of moves.
337     int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(RC2);
338     TII.storeRegToStack(MBB, I, LoReg, I->getOperand(1).isKill(), FI, RC, &TRI,
339                         0);
340     TII.storeRegToStack(MBB, I, HiReg, I->getOperand(2).isKill(), FI, RC, &TRI,
341                         4);
342     TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, &TRI, 0);
343     return true;
344   }
345
346   return false;
347 }
348
349 /// This method expands the same instruction that MipsSEInstrInfo::
350 /// expandExtractElementF64 does, for the case when ABI is fpxx and mfhc1 is not
351 /// available and the case where the ABI is FP64A. It is implemented here
352 /// because frame indexes are eliminated before MipsSEInstrInfo::
353 /// expandExtractElementF64 is called.
354 bool ExpandPseudo::expandExtractElementF64(MachineBasicBlock &MBB,
355                                            MachineBasicBlock::iterator I,
356                                            bool FP64) const {
357   // For fpxx and when mfhc1 is not available, use:
358   //   spill + reload via ldc1
359   //
360   // The case where dmfc1 is available doesn't need to be handled here
361   // because it never creates a ExtractElementF64 node.
362   //
363   // The FP64A ABI (fp64 with nooddspreg) must also use a spill/reload sequence
364   // for odd-numbered double precision values (because the lower 32-bits is
365   // transferred with mfc1 which is redirected to the upper half of the even
366   // register). Unfortunately, we have to make this decision before register
367   // allocation so for now we use a spill/reload sequence for all
368   // double-precision values in regardless of being an odd/even register.
369
370   const TargetMachine &TM = MF.getTarget();
371   const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>();
372   if ((Subtarget.isABI_FPXX() && !Subtarget.hasMTHC1()) ||
373       (FP64 && !Subtarget.useOddSPReg())) {
374     const MipsSEInstrInfo &TII = *static_cast<const MipsSEInstrInfo *>(
375                                      TM.getSubtargetImpl()->getInstrInfo());
376     const MipsRegisterInfo &TRI = *static_cast<const MipsRegisterInfo *>(
377                                       TM.getSubtargetImpl()->getRegisterInfo());
378
379     unsigned DstReg = I->getOperand(0).getReg();
380     unsigned SrcReg = I->getOperand(1).getReg();
381     unsigned N = I->getOperand(2).getImm();
382
383     // It should be impossible to have FGR64 on MIPS-II or MIPS32r1 (which are
384     // the cases where mfhc1 is not available). 64-bit architectures and
385     // MIPS32r2 or later can use FGR64 though.
386     assert(Subtarget.isGP64bit() || Subtarget.hasMTHC1() ||
387            !Subtarget.isFP64bit());
388
389     const TargetRegisterClass *RC =
390         FP64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
391     const TargetRegisterClass *RC2 = &Mips::GPR32RegClass;
392
393     // We re-use the same spill slot each time so that the stack frame doesn't
394     // grow too much in functions with a large number of moves.
395     int FI = MF.getInfo<MipsFunctionInfo>()->getMoveF64ViaSpillFI(RC);
396     TII.storeRegToStack(MBB, I, SrcReg, I->getOperand(1).isKill(), FI, RC, &TRI,
397                         0);
398     TII.loadRegFromStack(MBB, I, DstReg, FI, RC2, &TRI, N * 4);
399     return true;
400   }
401
402   return false;
403 }
404
405 MipsSEFrameLowering::MipsSEFrameLowering(const MipsSubtarget &STI)
406     : MipsFrameLowering(STI, STI.stackAlignment()) {}
407
408 unsigned MipsSEFrameLowering::ehDataReg(unsigned I) const {
409   static const unsigned EhDataReg[] = {
410     Mips::A0, Mips::A1, Mips::A2, Mips::A3
411   };
412   static const unsigned EhDataReg64[] = {
413     Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64
414   };
415
416   return STI.isABI_N64() ? EhDataReg64[I] : EhDataReg[I];
417 }
418
419 void MipsSEFrameLowering::emitPrologue(MachineFunction &MF) const {
420   MachineBasicBlock &MBB   = MF.front();
421   MachineFrameInfo *MFI    = MF.getFrameInfo();
422   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
423
424   const MipsSEInstrInfo &TII =
425       *static_cast<const MipsSEInstrInfo *>(
426           MF.getTarget().getSubtargetImpl()->getInstrInfo());
427   const MipsRegisterInfo &RegInfo =
428       *static_cast<const MipsRegisterInfo *>(
429           MF.getTarget().getSubtargetImpl()->getRegisterInfo());
430
431   MachineBasicBlock::iterator MBBI = MBB.begin();
432   DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
433   unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
434   unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
435   unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
436   unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
437
438   // First, compute final stack size.
439   uint64_t StackSize = MFI->getStackSize();
440
441   // No need to allocate space on the stack.
442   if (StackSize == 0 && !MFI->adjustsStack()) return;
443
444   MachineModuleInfo &MMI = MF.getMMI();
445   const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo();
446   MachineLocation DstML, SrcML;
447
448   // Adjust stack.
449   TII.adjustStackPtr(SP, -StackSize, MBB, MBBI);
450
451   // emit ".cfi_def_cfa_offset StackSize"
452   unsigned CFIIndex = MMI.addFrameInst(
453       MCCFIInstruction::createDefCfaOffset(nullptr, -StackSize));
454   BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
455       .addCFIIndex(CFIIndex);
456
457   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
458
459   if (CSI.size()) {
460     // Find the instruction past the last instruction that saves a callee-saved
461     // register to the stack.
462     for (unsigned i = 0; i < CSI.size(); ++i)
463       ++MBBI;
464
465     // Iterate over list of callee-saved registers and emit .cfi_offset
466     // directives.
467     for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
468            E = CSI.end(); I != E; ++I) {
469       int64_t Offset = MFI->getObjectOffset(I->getFrameIdx());
470       unsigned Reg = I->getReg();
471
472       // If Reg is a double precision register, emit two cfa_offsets,
473       // one for each of the paired single precision registers.
474       if (Mips::AFGR64RegClass.contains(Reg)) {
475         unsigned Reg0 =
476             MRI->getDwarfRegNum(RegInfo.getSubReg(Reg, Mips::sub_lo), true);
477         unsigned Reg1 =
478             MRI->getDwarfRegNum(RegInfo.getSubReg(Reg, Mips::sub_hi), true);
479
480         if (!STI.isLittle())
481           std::swap(Reg0, Reg1);
482
483         unsigned CFIIndex = MMI.addFrameInst(
484             MCCFIInstruction::createOffset(nullptr, Reg0, Offset));
485         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
486             .addCFIIndex(CFIIndex);
487
488         CFIIndex = MMI.addFrameInst(
489             MCCFIInstruction::createOffset(nullptr, Reg1, Offset + 4));
490         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
491             .addCFIIndex(CFIIndex);
492       } else if (Mips::FGR64RegClass.contains(Reg)) {
493         unsigned Reg0 = MRI->getDwarfRegNum(Reg, true);
494         unsigned Reg1 = MRI->getDwarfRegNum(Reg, true) + 1;
495
496         if (!STI.isLittle())
497           std::swap(Reg0, Reg1);
498
499         unsigned CFIIndex = MMI.addFrameInst(
500           MCCFIInstruction::createOffset(nullptr, Reg0, Offset));
501         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
502             .addCFIIndex(CFIIndex);
503
504         CFIIndex = MMI.addFrameInst(
505           MCCFIInstruction::createOffset(nullptr, Reg1, Offset + 4));
506         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
507             .addCFIIndex(CFIIndex);
508       } else {
509         // Reg is either in GPR32 or FGR32.
510         unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createOffset(
511             nullptr, MRI->getDwarfRegNum(Reg, 1), Offset));
512         BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
513             .addCFIIndex(CFIIndex);
514       }
515     }
516   }
517
518   if (MipsFI->callsEhReturn()) {
519     const TargetRegisterClass *RC = STI.isABI_N64() ?
520         &Mips::GPR64RegClass : &Mips::GPR32RegClass;
521
522     // Insert instructions that spill eh data registers.
523     for (int I = 0; I < 4; ++I) {
524       if (!MBB.isLiveIn(ehDataReg(I)))
525         MBB.addLiveIn(ehDataReg(I));
526       TII.storeRegToStackSlot(MBB, MBBI, ehDataReg(I), false,
527                               MipsFI->getEhDataRegFI(I), RC, &RegInfo);
528     }
529
530     // Emit .cfi_offset directives for eh data registers.
531     for (int I = 0; I < 4; ++I) {
532       int64_t Offset = MFI->getObjectOffset(MipsFI->getEhDataRegFI(I));
533       unsigned Reg = MRI->getDwarfRegNum(ehDataReg(I), true);
534       unsigned CFIIndex = MMI.addFrameInst(
535           MCCFIInstruction::createOffset(nullptr, Reg, Offset));
536       BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
537           .addCFIIndex(CFIIndex);
538     }
539   }
540
541   // if framepointer enabled, set it to point to the stack pointer.
542   if (hasFP(MF)) {
543     // Insert instruction "move $fp, $sp" at this location.
544     BuildMI(MBB, MBBI, dl, TII.get(ADDu), FP).addReg(SP).addReg(ZERO)
545       .setMIFlag(MachineInstr::FrameSetup);
546
547     // emit ".cfi_def_cfa_register $fp"
548     unsigned CFIIndex = MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(
549         nullptr, MRI->getDwarfRegNum(FP, true)));
550     BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
551         .addCFIIndex(CFIIndex);
552   }
553 }
554
555 void MipsSEFrameLowering::emitEpilogue(MachineFunction &MF,
556                                        MachineBasicBlock &MBB) const {
557   MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
558   MachineFrameInfo *MFI            = MF.getFrameInfo();
559   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
560
561   const MipsSEInstrInfo &TII =
562       *static_cast<const MipsSEInstrInfo *>(
563           MF.getTarget().getSubtargetImpl()->getInstrInfo());
564   const MipsRegisterInfo &RegInfo =
565       *static_cast<const MipsRegisterInfo *>(
566           MF.getTarget().getSubtargetImpl()->getRegisterInfo());
567
568   DebugLoc dl = MBBI->getDebugLoc();
569   unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
570   unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
571   unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
572   unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
573
574   // if framepointer enabled, restore the stack pointer.
575   if (hasFP(MF)) {
576     // Find the first instruction that restores a callee-saved register.
577     MachineBasicBlock::iterator I = MBBI;
578
579     for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i)
580       --I;
581
582     // Insert instruction "move $sp, $fp" at this location.
583     BuildMI(MBB, I, dl, TII.get(ADDu), SP).addReg(FP).addReg(ZERO);
584   }
585
586   if (MipsFI->callsEhReturn()) {
587     const TargetRegisterClass *RC = STI.isABI_N64() ?
588         &Mips::GPR64RegClass : &Mips::GPR32RegClass;
589
590     // Find first instruction that restores a callee-saved register.
591     MachineBasicBlock::iterator I = MBBI;
592     for (unsigned i = 0; i < MFI->getCalleeSavedInfo().size(); ++i)
593       --I;
594
595     // Insert instructions that restore eh data registers.
596     for (int J = 0; J < 4; ++J) {
597       TII.loadRegFromStackSlot(MBB, I, ehDataReg(J), MipsFI->getEhDataRegFI(J),
598                                RC, &RegInfo);
599     }
600   }
601
602   // Get the number of bytes from FrameInfo
603   uint64_t StackSize = MFI->getStackSize();
604
605   if (!StackSize)
606     return;
607
608   // Adjust stack.
609   TII.adjustStackPtr(SP, StackSize, MBB, MBBI);
610 }
611
612 bool MipsSEFrameLowering::
613 spillCalleeSavedRegisters(MachineBasicBlock &MBB,
614                           MachineBasicBlock::iterator MI,
615                           const std::vector<CalleeSavedInfo> &CSI,
616                           const TargetRegisterInfo *TRI) const {
617   MachineFunction *MF = MBB.getParent();
618   MachineBasicBlock *EntryBlock = MF->begin();
619   const TargetInstrInfo &TII =
620       *MF->getTarget().getSubtargetImpl()->getInstrInfo();
621
622   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
623     // Add the callee-saved register as live-in. Do not add if the register is
624     // RA and return address is taken, because it has already been added in
625     // method MipsTargetLowering::LowerRETURNADDR.
626     // It's killed at the spill, unless the register is RA and return address
627     // is taken.
628     unsigned Reg = CSI[i].getReg();
629     bool IsRAAndRetAddrIsTaken = (Reg == Mips::RA || Reg == Mips::RA_64)
630         && MF->getFrameInfo()->isReturnAddressTaken();
631     if (!IsRAAndRetAddrIsTaken)
632       EntryBlock->addLiveIn(Reg);
633
634     // Insert the spill to the stack frame.
635     bool IsKill = !IsRAAndRetAddrIsTaken;
636     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
637     TII.storeRegToStackSlot(*EntryBlock, MI, Reg, IsKill,
638                             CSI[i].getFrameIdx(), RC, TRI);
639   }
640
641   return true;
642 }
643
644 bool
645 MipsSEFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
646   const MachineFrameInfo *MFI = MF.getFrameInfo();
647
648   // Reserve call frame if the size of the maximum call frame fits into 16-bit
649   // immediate field and there are no variable sized objects on the stack.
650   // Make sure the second register scavenger spill slot can be accessed with one
651   // instruction.
652   return isInt<16>(MFI->getMaxCallFrameSize() + getStackAlignment()) &&
653     !MFI->hasVarSizedObjects();
654 }
655
656 // Eliminate ADJCALLSTACKDOWN, ADJCALLSTACKUP pseudo instructions
657 void MipsSEFrameLowering::
658 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
659                               MachineBasicBlock::iterator I) const {
660   const MipsSEInstrInfo &TII =
661       *static_cast<const MipsSEInstrInfo *>(
662           MF.getTarget().getSubtargetImpl()->getInstrInfo());
663
664   if (!hasReservedCallFrame(MF)) {
665     int64_t Amount = I->getOperand(0).getImm();
666
667     if (I->getOpcode() == Mips::ADJCALLSTACKDOWN)
668       Amount = -Amount;
669
670     unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
671     TII.adjustStackPtr(SP, Amount, MBB, I);
672   }
673
674   MBB.erase(I);
675 }
676
677 void MipsSEFrameLowering::
678 processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
679                                      RegScavenger *RS) const {
680   MachineRegisterInfo &MRI = MF.getRegInfo();
681   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
682   unsigned FP = STI.isABI_N64() ? Mips::FP_64 : Mips::FP;
683
684   // Mark $fp as used if function has dedicated frame pointer.
685   if (hasFP(MF))
686     MRI.setPhysRegUsed(FP);
687
688   // Create spill slots for eh data registers if function calls eh_return.
689   if (MipsFI->callsEhReturn())
690     MipsFI->createEhDataRegsFI();
691
692   // Expand pseudo instructions which load, store or copy accumulators.
693   // Add an emergency spill slot if a pseudo was expanded.
694   if (ExpandPseudo(MF).expand()) {
695     // The spill slot should be half the size of the accumulator. If target is
696     // mips64, it should be 64-bit, otherwise it should be 32-bt.
697     const TargetRegisterClass *RC = STI.hasMips64() ?
698       &Mips::GPR64RegClass : &Mips::GPR32RegClass;
699     int FI = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
700                                                   RC->getAlignment(), false);
701     RS->addScavengingFrameIndex(FI);
702   }
703
704   // Set scavenging frame index if necessary.
705   uint64_t MaxSPOffset = MF.getInfo<MipsFunctionInfo>()->getIncomingArgSize() +
706     estimateStackSize(MF);
707
708   if (isInt<16>(MaxSPOffset))
709     return;
710
711   const TargetRegisterClass *RC = STI.isABI_N64() ?
712     &Mips::GPR64RegClass : &Mips::GPR32RegClass;
713   int FI = MF.getFrameInfo()->CreateStackObject(RC->getSize(),
714                                                 RC->getAlignment(), false);
715   RS->addScavengingFrameIndex(FI);
716 }
717
718 const MipsFrameLowering *
719 llvm::createMipsSEFrameLowering(const MipsSubtarget &ST) {
720   return new MipsSEFrameLowering(ST);
721 }