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