[mips][msa] Added spill/reload support
[oota-llvm.git] / lib / Target / Mips / MipsSEInstrInfo.cpp
1 //===-- MipsSEInstrInfo.cpp - Mips32/64 Instruction 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 the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsSEInstrInfo.h"
15 #include "InstPrinter/MipsInstPrinter.h"
16 #include "MipsMachineFunction.h"
17 #include "MipsTargetMachine.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/TargetRegistry.h"
24
25 using namespace llvm;
26
27 static cl::opt<bool> NoDPLoadStore("mno-ldc1-sdc1", cl::init(false),
28                                    cl::desc("Expand double precision loads and "
29                                             "stores to their single precision "
30                                             "counterparts."));
31
32 MipsSEInstrInfo::MipsSEInstrInfo(MipsTargetMachine &tm)
33   : MipsInstrInfo(tm,
34                   tm.getRelocationModel() == Reloc::PIC_ ? Mips::B : Mips::J),
35     RI(*tm.getSubtargetImpl()),
36     IsN64(tm.getSubtarget<MipsSubtarget>().isABI_N64()) {}
37
38 const MipsRegisterInfo &MipsSEInstrInfo::getRegisterInfo() const {
39   return RI;
40 }
41
42 /// isLoadFromStackSlot - If the specified machine instruction is a direct
43 /// load from a stack slot, return the virtual or physical register number of
44 /// the destination along with the FrameIndex of the loaded stack slot.  If
45 /// not, return 0.  This predicate must return 0 if the instruction has
46 /// any side effects other than loading from the stack slot.
47 unsigned MipsSEInstrInfo::
48 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
49 {
50   unsigned Opc = MI->getOpcode();
51
52   if ((Opc == Mips::LW)   || (Opc == Mips::LD)   ||
53       (Opc == Mips::LWC1) || (Opc == Mips::LDC1) || (Opc == Mips::LDC164)) {
54     if ((MI->getOperand(1).isFI()) && // is a stack slot
55         (MI->getOperand(2).isImm()) &&  // the imm is zero
56         (isZeroImm(MI->getOperand(2)))) {
57       FrameIndex = MI->getOperand(1).getIndex();
58       return MI->getOperand(0).getReg();
59     }
60   }
61
62   return 0;
63 }
64
65 /// isStoreToStackSlot - If the specified machine instruction is a direct
66 /// store to a stack slot, return the virtual or physical register number of
67 /// the source reg along with the FrameIndex of the loaded stack slot.  If
68 /// not, return 0.  This predicate must return 0 if the instruction has
69 /// any side effects other than storing to the stack slot.
70 unsigned MipsSEInstrInfo::
71 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
72 {
73   unsigned Opc = MI->getOpcode();
74
75   if ((Opc == Mips::SW)   || (Opc == Mips::SD)   ||
76       (Opc == Mips::SWC1) || (Opc == Mips::SDC1) || (Opc == Mips::SDC164)) {
77     if ((MI->getOperand(1).isFI()) && // is a stack slot
78         (MI->getOperand(2).isImm()) &&  // the imm is zero
79         (isZeroImm(MI->getOperand(2)))) {
80       FrameIndex = MI->getOperand(1).getIndex();
81       return MI->getOperand(0).getReg();
82     }
83   }
84   return 0;
85 }
86
87 void MipsSEInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
88                                   MachineBasicBlock::iterator I, DebugLoc DL,
89                                   unsigned DestReg, unsigned SrcReg,
90                                   bool KillSrc) const {
91   unsigned Opc = 0, ZeroReg = 0;
92
93   if (Mips::GPR32RegClass.contains(DestReg)) { // Copy to CPU Reg.
94     if (Mips::GPR32RegClass.contains(SrcReg))
95       Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
96     else if (Mips::CCRRegClass.contains(SrcReg))
97       Opc = Mips::CFC1;
98     else if (Mips::FGR32RegClass.contains(SrcReg))
99       Opc = Mips::MFC1;
100     else if (Mips::HI32RegClass.contains(SrcReg))
101       Opc = Mips::MFHI, SrcReg = 0;
102     else if (Mips::LO32RegClass.contains(SrcReg))
103       Opc = Mips::MFLO, SrcReg = 0;
104     else if (Mips::HI32DSPRegClass.contains(SrcReg))
105       Opc = Mips::MFHI_DSP;
106     else if (Mips::LO32DSPRegClass.contains(SrcReg))
107       Opc = Mips::MFLO_DSP;
108     else if (Mips::DSPCCRegClass.contains(SrcReg)) {
109       BuildMI(MBB, I, DL, get(Mips::RDDSP), DestReg).addImm(1 << 4)
110         .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
111       return;
112     }
113   }
114   else if (Mips::GPR32RegClass.contains(SrcReg)) { // Copy from CPU Reg.
115     if (Mips::CCRRegClass.contains(DestReg))
116       Opc = Mips::CTC1;
117     else if (Mips::FGR32RegClass.contains(DestReg))
118       Opc = Mips::MTC1;
119     else if (Mips::HI32RegClass.contains(DestReg))
120       Opc = Mips::MTHI, DestReg = 0;
121     else if (Mips::LO32RegClass.contains(DestReg))
122       Opc = Mips::MTLO, DestReg = 0;
123     else if (Mips::HI32DSPRegClass.contains(DestReg))
124       Opc = Mips::MTHI_DSP;
125     else if (Mips::LO32DSPRegClass.contains(DestReg))
126       Opc = Mips::MTLO_DSP;
127     else if (Mips::DSPCCRegClass.contains(DestReg)) {
128       BuildMI(MBB, I, DL, get(Mips::WRDSP))
129         .addReg(SrcReg, getKillRegState(KillSrc)).addImm(1 << 4)
130         .addReg(DestReg, RegState::ImplicitDefine);
131       return;
132     }
133   }
134   else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
135     Opc = Mips::FMOV_S;
136   else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
137     Opc = Mips::FMOV_D32;
138   else if (Mips::FGR64RegClass.contains(DestReg, SrcReg))
139     Opc = Mips::FMOV_D64;
140   else if (Mips::GPR64RegClass.contains(DestReg)) { // Copy to CPU64 Reg.
141     if (Mips::GPR64RegClass.contains(SrcReg))
142       Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
143     else if (Mips::HI64RegClass.contains(SrcReg))
144       Opc = Mips::MFHI64, SrcReg = 0;
145     else if (Mips::LO64RegClass.contains(SrcReg))
146       Opc = Mips::MFLO64, SrcReg = 0;
147     else if (Mips::FGR64RegClass.contains(SrcReg))
148       Opc = Mips::DMFC1;
149   }
150   else if (Mips::GPR64RegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
151     if (Mips::HI64RegClass.contains(DestReg))
152       Opc = Mips::MTHI64, DestReg = 0;
153     else if (Mips::LO64RegClass.contains(DestReg))
154       Opc = Mips::MTLO64, DestReg = 0;
155     else if (Mips::FGR64RegClass.contains(DestReg))
156       Opc = Mips::DMTC1;
157   }
158
159   assert(Opc && "Cannot copy registers");
160
161   MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
162
163   if (DestReg)
164     MIB.addReg(DestReg, RegState::Define);
165
166   if (SrcReg)
167     MIB.addReg(SrcReg, getKillRegState(KillSrc));
168
169   if (ZeroReg)
170     MIB.addReg(ZeroReg);
171 }
172
173 void MipsSEInstrInfo::
174 storeRegToStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
175                 unsigned SrcReg, bool isKill, int FI,
176                 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI,
177                 int64_t Offset) const {
178   DebugLoc DL;
179   if (I != MBB.end()) DL = I->getDebugLoc();
180   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
181
182   unsigned Opc = 0;
183
184   if (Mips::GPR32RegClass.hasSubClassEq(RC))
185     Opc = Mips::SW;
186   else if (Mips::GPR64RegClass.hasSubClassEq(RC))
187     Opc = Mips::SD;
188   else if (Mips::ACC64RegClass.hasSubClassEq(RC))
189     Opc = Mips::STORE_ACC64;
190   else if (Mips::ACC64DSPRegClass.hasSubClassEq(RC))
191     Opc = Mips::STORE_ACC64DSP;
192   else if (Mips::ACC128RegClass.hasSubClassEq(RC))
193     Opc = Mips::STORE_ACC128;
194   else if (Mips::DSPCCRegClass.hasSubClassEq(RC))
195     Opc = Mips::STORE_CCOND_DSP;
196   else if (Mips::FGR32RegClass.hasSubClassEq(RC))
197     Opc = Mips::SWC1;
198   else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
199     Opc = Mips::SDC1;
200   else if (Mips::FGR64RegClass.hasSubClassEq(RC))
201     Opc = Mips::SDC164;
202   else if (RC->hasType(MVT::v16i8))
203     Opc = Mips::ST_B;
204   else if (RC->hasType(MVT::v8i16) || RC->hasType(MVT::v8f16))
205     Opc = Mips::ST_H;
206   else if (RC->hasType(MVT::v4i32) || RC->hasType(MVT::v4f32))
207     Opc = Mips::ST_W;
208   else if (RC->hasType(MVT::v2i64) || RC->hasType(MVT::v2f64))
209     Opc = Mips::ST_D;
210
211   assert(Opc && "Register class not handled!");
212   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
213     .addFrameIndex(FI).addImm(Offset).addMemOperand(MMO);
214 }
215
216 void MipsSEInstrInfo::
217 loadRegFromStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
218                  unsigned DestReg, int FI, const TargetRegisterClass *RC,
219                  const TargetRegisterInfo *TRI, int64_t Offset) const {
220   DebugLoc DL;
221   if (I != MBB.end()) DL = I->getDebugLoc();
222   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
223   unsigned Opc = 0;
224
225   if (Mips::GPR32RegClass.hasSubClassEq(RC))
226     Opc = Mips::LW;
227   else if (Mips::GPR64RegClass.hasSubClassEq(RC))
228     Opc = Mips::LD;
229   else if (Mips::ACC64RegClass.hasSubClassEq(RC))
230     Opc = Mips::LOAD_ACC64;
231   else if (Mips::ACC64DSPRegClass.hasSubClassEq(RC))
232     Opc = Mips::LOAD_ACC64DSP;
233   else if (Mips::ACC128RegClass.hasSubClassEq(RC))
234     Opc = Mips::LOAD_ACC128;
235   else if (Mips::DSPCCRegClass.hasSubClassEq(RC))
236     Opc = Mips::LOAD_CCOND_DSP;
237   else if (Mips::FGR32RegClass.hasSubClassEq(RC))
238     Opc = Mips::LWC1;
239   else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
240     Opc = Mips::LDC1;
241   else if (Mips::FGR64RegClass.hasSubClassEq(RC))
242     Opc = Mips::LDC164;
243   else if (RC->hasType(MVT::v16i8))
244     Opc = Mips::LD_B;
245   else if (RC->hasType(MVT::v8i16) || RC->hasType(MVT::v8f16))
246     Opc = Mips::LD_H;
247   else if (RC->hasType(MVT::v4i32) || RC->hasType(MVT::v4f32))
248     Opc = Mips::LD_W;
249   else if (RC->hasType(MVT::v2i64) || RC->hasType(MVT::v2f64))
250     Opc = Mips::LD_D;
251
252   assert(Opc && "Register class not handled!");
253   BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(Offset)
254     .addMemOperand(MMO);
255 }
256
257 bool MipsSEInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
258   MachineBasicBlock &MBB = *MI->getParent();
259
260   switch(MI->getDesc().getOpcode()) {
261   default:
262     return false;
263   case Mips::RetRA:
264     expandRetRA(MBB, MI, Mips::RET);
265     break;
266   case Mips::PseudoCVT_S_W:
267     expandCvtFPInt(MBB, MI, Mips::CVT_S_W, Mips::MTC1, false);
268     break;
269   case Mips::PseudoCVT_D32_W:
270     expandCvtFPInt(MBB, MI, Mips::CVT_D32_W, Mips::MTC1, false);
271     break;
272   case Mips::PseudoCVT_S_L:
273     expandCvtFPInt(MBB, MI, Mips::CVT_S_L, Mips::DMTC1, true);
274     break;
275   case Mips::PseudoCVT_D64_W:
276     expandCvtFPInt(MBB, MI, Mips::CVT_D64_W, Mips::MTC1, true);
277     break;
278   case Mips::PseudoCVT_D64_L:
279     expandCvtFPInt(MBB, MI, Mips::CVT_D64_L, Mips::DMTC1, true);
280     break;
281   case Mips::BuildPairF64:
282     expandBuildPairF64(MBB, MI, false);
283     break;
284   case Mips::BuildPairF64_64:
285     expandBuildPairF64(MBB, MI, true);
286     break;
287   case Mips::ExtractElementF64:
288     expandExtractElementF64(MBB, MI, false);
289     break;
290   case Mips::ExtractElementF64_64:
291     expandExtractElementF64(MBB, MI, true);
292     break;
293   case Mips::PseudoLDC1:
294     expandDPLoadStore(MBB, MI, Mips::LDC1, Mips::LWC1);
295     break;
296   case Mips::PseudoSDC1:
297     expandDPLoadStore(MBB, MI, Mips::SDC1, Mips::SWC1);
298     break;
299   case Mips::MIPSeh_return32:
300   case Mips::MIPSeh_return64:
301     expandEhReturn(MBB, MI);
302     break;
303   }
304
305   MBB.erase(MI);
306   return true;
307 }
308
309 /// getOppositeBranchOpc - Return the inverse of the specified
310 /// opcode, e.g. turning BEQ to BNE.
311 unsigned MipsSEInstrInfo::getOppositeBranchOpc(unsigned Opc) const {
312   switch (Opc) {
313   default:           llvm_unreachable("Illegal opcode!");
314   case Mips::BEQ:    return Mips::BNE;
315   case Mips::BNE:    return Mips::BEQ;
316   case Mips::BGTZ:   return Mips::BLEZ;
317   case Mips::BGEZ:   return Mips::BLTZ;
318   case Mips::BLTZ:   return Mips::BGEZ;
319   case Mips::BLEZ:   return Mips::BGTZ;
320   case Mips::BEQ64:  return Mips::BNE64;
321   case Mips::BNE64:  return Mips::BEQ64;
322   case Mips::BGTZ64: return Mips::BLEZ64;
323   case Mips::BGEZ64: return Mips::BLTZ64;
324   case Mips::BLTZ64: return Mips::BGEZ64;
325   case Mips::BLEZ64: return Mips::BGTZ64;
326   case Mips::BC1T:   return Mips::BC1F;
327   case Mips::BC1F:   return Mips::BC1T;
328   }
329 }
330
331 /// Adjust SP by Amount bytes.
332 void MipsSEInstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
333                                      MachineBasicBlock &MBB,
334                                      MachineBasicBlock::iterator I) const {
335   const MipsSubtarget &STI = TM.getSubtarget<MipsSubtarget>();
336   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
337   unsigned ADDu = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
338   unsigned ADDiu = STI.isABI_N64() ? Mips::DADDiu : Mips::ADDiu;
339
340   if (isInt<16>(Amount))// addi sp, sp, amount
341     BuildMI(MBB, I, DL, get(ADDiu), SP).addReg(SP).addImm(Amount);
342   else { // Expand immediate that doesn't fit in 16-bit.
343     unsigned Reg = loadImmediate(Amount, MBB, I, DL, 0);
344     BuildMI(MBB, I, DL, get(ADDu), SP).addReg(SP).addReg(Reg, RegState::Kill);
345   }
346 }
347
348 /// This function generates the sequence of instructions needed to get the
349 /// result of adding register REG and immediate IMM.
350 unsigned
351 MipsSEInstrInfo::loadImmediate(int64_t Imm, MachineBasicBlock &MBB,
352                                MachineBasicBlock::iterator II, DebugLoc DL,
353                                unsigned *NewImm) const {
354   MipsAnalyzeImmediate AnalyzeImm;
355   const MipsSubtarget &STI = TM.getSubtarget<MipsSubtarget>();
356   MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
357   unsigned Size = STI.isABI_N64() ? 64 : 32;
358   unsigned LUi = STI.isABI_N64() ? Mips::LUi64 : Mips::LUi;
359   unsigned ZEROReg = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
360   const TargetRegisterClass *RC = STI.isABI_N64() ?
361     &Mips::GPR64RegClass : &Mips::GPR32RegClass;
362   bool LastInstrIsADDiu = NewImm;
363
364   const MipsAnalyzeImmediate::InstSeq &Seq =
365     AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu);
366   MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
367
368   assert(Seq.size() && (!LastInstrIsADDiu || (Seq.size() > 1)));
369
370   // The first instruction can be a LUi, which is different from other
371   // instructions (ADDiu, ORI and SLL) in that it does not have a register
372   // operand.
373   unsigned Reg = RegInfo.createVirtualRegister(RC);
374
375   if (Inst->Opc == LUi)
376     BuildMI(MBB, II, DL, get(LUi), Reg).addImm(SignExtend64<16>(Inst->ImmOpnd));
377   else
378     BuildMI(MBB, II, DL, get(Inst->Opc), Reg).addReg(ZEROReg)
379       .addImm(SignExtend64<16>(Inst->ImmOpnd));
380
381   // Build the remaining instructions in Seq.
382   for (++Inst; Inst != Seq.end() - LastInstrIsADDiu; ++Inst)
383     BuildMI(MBB, II, DL, get(Inst->Opc), Reg).addReg(Reg, RegState::Kill)
384       .addImm(SignExtend64<16>(Inst->ImmOpnd));
385
386   if (LastInstrIsADDiu)
387     *NewImm = Inst->ImmOpnd;
388
389   return Reg;
390 }
391
392 unsigned MipsSEInstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
393   return (Opc == Mips::BEQ    || Opc == Mips::BNE    || Opc == Mips::BGTZ   ||
394           Opc == Mips::BGEZ   || Opc == Mips::BLTZ   || Opc == Mips::BLEZ   ||
395           Opc == Mips::BEQ64  || Opc == Mips::BNE64  || Opc == Mips::BGTZ64 ||
396           Opc == Mips::BGEZ64 || Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 ||
397           Opc == Mips::BC1T   || Opc == Mips::BC1F   || Opc == Mips::B      ||
398           Opc == Mips::J) ?
399          Opc : 0;
400 }
401
402 void MipsSEInstrInfo::expandRetRA(MachineBasicBlock &MBB,
403                                 MachineBasicBlock::iterator I,
404                                 unsigned Opc) const {
405   BuildMI(MBB, I, I->getDebugLoc(), get(Opc)).addReg(Mips::RA);
406 }
407
408 std::pair<bool, bool>
409 MipsSEInstrInfo::compareOpndSize(unsigned Opc,
410                                  const MachineFunction &MF) const {
411   const MCInstrDesc &Desc = get(Opc);
412   assert(Desc.NumOperands == 2 && "Unary instruction expected.");
413   const MipsRegisterInfo *RI = &getRegisterInfo();
414   unsigned DstRegSize = getRegClass(Desc, 0, RI, MF)->getSize();
415   unsigned SrcRegSize = getRegClass(Desc, 1, RI, MF)->getSize();
416
417   return std::make_pair(DstRegSize > SrcRegSize, DstRegSize < SrcRegSize);
418 }
419
420 void MipsSEInstrInfo::expandCvtFPInt(MachineBasicBlock &MBB,
421                                      MachineBasicBlock::iterator I,
422                                      unsigned CvtOpc, unsigned MovOpc,
423                                      bool IsI64) const {
424   const MCInstrDesc &CvtDesc = get(CvtOpc), &MovDesc = get(MovOpc);
425   const MachineOperand &Dst = I->getOperand(0), &Src = I->getOperand(1);
426   unsigned DstReg = Dst.getReg(), SrcReg = Src.getReg(), TmpReg = DstReg;
427   unsigned KillSrc =  getKillRegState(Src.isKill());
428   DebugLoc DL = I->getDebugLoc();
429   bool DstIsLarger, SrcIsLarger;
430
431   tie(DstIsLarger, SrcIsLarger) = compareOpndSize(CvtOpc, *MBB.getParent());
432
433   if (DstIsLarger)
434     TmpReg = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
435
436   if (SrcIsLarger)
437     DstReg = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
438
439   BuildMI(MBB, I, DL, MovDesc, TmpReg).addReg(SrcReg, KillSrc);
440   BuildMI(MBB, I, DL, CvtDesc, DstReg).addReg(TmpReg, RegState::Kill);
441 }
442
443 void MipsSEInstrInfo::expandExtractElementF64(MachineBasicBlock &MBB,
444                                               MachineBasicBlock::iterator I,
445                                               bool FP64) const {
446   unsigned DstReg = I->getOperand(0).getReg();
447   unsigned SrcReg = I->getOperand(1).getReg();
448   unsigned N = I->getOperand(2).getImm();
449   DebugLoc dl = I->getDebugLoc();
450
451   assert(N < 2 && "Invalid immediate");
452   unsigned SubIdx = N ? Mips::sub_hi : Mips::sub_lo;
453   unsigned SubReg = getRegisterInfo().getSubReg(SrcReg, SubIdx);
454
455   if (SubIdx == Mips::sub_hi && FP64)
456     BuildMI(MBB, I, dl, get(Mips::MFHC1), DstReg).addReg(SubReg);
457   else
458     BuildMI(MBB, I, dl, get(Mips::MFC1), DstReg).addReg(SubReg);
459 }
460
461 void MipsSEInstrInfo::expandBuildPairF64(MachineBasicBlock &MBB,
462                                          MachineBasicBlock::iterator I,
463                                          bool FP64) const {
464   unsigned DstReg = I->getOperand(0).getReg();
465   unsigned LoReg = I->getOperand(1).getReg(), HiReg = I->getOperand(2).getReg();
466   const MCInstrDesc& Mtc1Tdd = get(Mips::MTC1);
467   DebugLoc dl = I->getDebugLoc();
468   const TargetRegisterInfo &TRI = getRegisterInfo();
469
470   // mtc1 Lo, $fp
471   // mtc1 Hi, $fp + 1
472   BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_lo))
473     .addReg(LoReg);
474
475   if (FP64)
476     BuildMI(MBB, I, dl, get(Mips::MTHC1), TRI.getSubReg(DstReg, Mips::sub_hi))
477       .addReg(HiReg);
478   else
479     BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_hi))
480       .addReg(HiReg);
481 }
482
483 /// Add 4 to the displacement of operand MO.
484 static void fixDisp(MachineOperand &MO) {
485   switch (MO.getType()) {
486   default:
487     llvm_unreachable("Unhandled operand type.");
488   case MachineOperand::MO_Immediate:
489     MO.setImm(MO.getImm() + 4);
490     break;
491   case MachineOperand::MO_GlobalAddress:
492   case MachineOperand::MO_ConstantPoolIndex:
493   case MachineOperand::MO_BlockAddress:
494   case MachineOperand::MO_TargetIndex:
495   case MachineOperand::MO_ExternalSymbol:
496     MO.setOffset(MO.getOffset() + 4);
497     break;
498   }
499 }
500
501 void MipsSEInstrInfo::expandDPLoadStore(MachineBasicBlock &MBB,
502                                         MachineBasicBlock::iterator I,
503                                         unsigned OpcD, unsigned OpcS) const {
504   // If NoDPLoadStore is false, just change the opcode.
505   if (!NoDPLoadStore) {
506     genInstrWithNewOpc(OpcD, I);
507     return;
508   }
509
510   // Expand a double precision FP load or store to two single precision
511   // instructions.
512
513   const TargetRegisterInfo &TRI = getRegisterInfo();
514   const MachineOperand &ValReg = I->getOperand(0);
515   unsigned LoReg = TRI.getSubReg(ValReg.getReg(), Mips::sub_lo);
516   unsigned HiReg = TRI.getSubReg(ValReg.getReg(), Mips::sub_hi);
517
518   if (!TM.getSubtarget<MipsSubtarget>().isLittle())
519     std::swap(LoReg, HiReg);
520
521   // Create an instruction which loads from or stores to the lower memory
522   // address.
523   MachineInstrBuilder MIB = genInstrWithNewOpc(OpcS, I);
524   MIB->getOperand(0).setReg(LoReg);
525
526   // Create an instruction which loads from or stores to the higher memory
527   // address.
528   MIB = genInstrWithNewOpc(OpcS, I);
529   MIB->getOperand(0).setReg(HiReg);
530   fixDisp(MIB->getOperand(2));
531 }
532
533 void MipsSEInstrInfo::expandEhReturn(MachineBasicBlock &MBB,
534                                      MachineBasicBlock::iterator I) const {
535   // This pseudo instruction is generated as part of the lowering of
536   // ISD::EH_RETURN. We convert it to a stack increment by OffsetReg, and
537   // indirect jump to TargetReg
538   const MipsSubtarget &STI = TM.getSubtarget<MipsSubtarget>();
539   unsigned ADDU = STI.isABI_N64() ? Mips::DADDu : Mips::ADDu;
540   unsigned JR = STI.isABI_N64() ? Mips::JR64 : Mips::JR;
541   unsigned SP = STI.isABI_N64() ? Mips::SP_64 : Mips::SP;
542   unsigned RA = STI.isABI_N64() ? Mips::RA_64 : Mips::RA;
543   unsigned T9 = STI.isABI_N64() ? Mips::T9_64 : Mips::T9;
544   unsigned ZERO = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
545   unsigned OffsetReg = I->getOperand(0).getReg();
546   unsigned TargetReg = I->getOperand(1).getReg();
547
548   // addu $ra, $v0, $zero
549   // addu $sp, $sp, $v1
550   // jr   $ra
551   if (TM.getRelocationModel() == Reloc::PIC_)
552     BuildMI(MBB, I, I->getDebugLoc(), TM.getInstrInfo()->get(ADDU), T9)
553         .addReg(TargetReg).addReg(ZERO);
554   BuildMI(MBB, I, I->getDebugLoc(), TM.getInstrInfo()->get(ADDU), RA)
555       .addReg(TargetReg).addReg(ZERO);
556   BuildMI(MBB, I, I->getDebugLoc(), TM.getInstrInfo()->get(ADDU), SP)
557       .addReg(SP).addReg(OffsetReg);
558   BuildMI(MBB, I, I->getDebugLoc(), TM.getInstrInfo()->get(JR)).addReg(RA);
559 }
560
561 const MipsInstrInfo *llvm::createMipsSEInstrInfo(MipsTargetMachine &TM) {
562   return new MipsSEInstrInfo(TM);
563 }