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 "MipsAnalyzeImmediate.h"
16 #include "InstPrinter/MipsInstPrinter.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsTargetMachine.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/TargetRegistry.h"
25 
26 using namespace llvm;
27 
28 MipsSEInstrInfo::MipsSEInstrInfo(const MipsSubtarget &STI)
29     : MipsInstrInfo(STI, STI.getRelocationModel() == Reloc::PIC_ ? Mips::B
30                                                                  : Mips::J),
31       RI() {}
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::isLoadFromStackSlot(const MachineInstr *MI,
43                                               int &FrameIndex) const {
44   unsigned Opc = MI->getOpcode();
45 
46   if ((Opc == Mips::LW)   || (Opc == Mips::LD)   ||
47       (Opc == Mips::LWC1) || (Opc == Mips::LDC1) || (Opc == Mips::LDC164)) {
48     if ((MI->getOperand(1).isFI()) && // is a stack slot
49         (MI->getOperand(2).isImm()) &&  // the imm is zero
50         (isZeroImm(MI->getOperand(2)))) {
51       FrameIndex = MI->getOperand(1).getIndex();
52       return MI->getOperand(0).getReg();
53     }
54   }
55 
56   return 0;
57 }
58 
59 /// isStoreToStackSlot - If the specified machine instruction is a direct
60 /// store to a stack slot, return the virtual or physical register number of
61 /// the source reg along with the FrameIndex of the loaded stack slot.  If
62 /// not, return 0.  This predicate must return 0 if the instruction has
63 /// any side effects other than storing to the stack slot.
64 unsigned MipsSEInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
65                                              int &FrameIndex) const {
66   unsigned Opc = MI->getOpcode();
67 
68   if ((Opc == Mips::SW)   || (Opc == Mips::SD)   ||
69       (Opc == Mips::SWC1) || (Opc == Mips::SDC1) || (Opc == Mips::SDC164)) {
70     if ((MI->getOperand(1).isFI()) && // is a stack slot
71         (MI->getOperand(2).isImm()) &&  // the imm is zero
72         (isZeroImm(MI->getOperand(2)))) {
73       FrameIndex = MI->getOperand(1).getIndex();
74       return MI->getOperand(0).getReg();
75     }
76   }
77   return 0;
78 }
79 
80 void MipsSEInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
81                                   MachineBasicBlock::iterator I, DebugLoc DL,
82                                   unsigned DestReg, unsigned SrcReg,
83                                   bool KillSrc) const {
84   unsigned Opc = 0, ZeroReg = 0;
85   bool isMicroMips = Subtarget.inMicroMipsMode();
86 
87   if (Mips::GPR32RegClass.contains(DestReg)) { // Copy to CPU Reg.
88     if (Mips::GPR32RegClass.contains(SrcReg)) {
89       if (isMicroMips)
90         Opc = Mips::MOVE16_MM;
91       else
92         Opc = Mips::OR, ZeroReg = Mips::ZERO;
93     } else if (Mips::CCRRegClass.contains(SrcReg))
94       Opc = Mips::CFC1;
95     else if (Mips::FGR32RegClass.contains(SrcReg))
96       Opc = Mips::MFC1;
97     else if (Mips::HI32RegClass.contains(SrcReg)) {
98       Opc = isMicroMips ? Mips::MFHI16_MM : Mips::MFHI;
99       SrcReg = 0;
100     } else if (Mips::LO32RegClass.contains(SrcReg)) {
101       Opc = isMicroMips ? Mips::MFLO16_MM : Mips::MFLO;
102       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::OR64, 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   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
187 
188   unsigned Opc = 0;
189 
190   if (Mips::GPR32RegClass.hasSubClassEq(RC))
191     Opc = Mips::SW;
192   else if (Mips::GPR64RegClass.hasSubClassEq(RC))
193     Opc = Mips::SD;
194   else if (Mips::ACC64RegClass.hasSubClassEq(RC))
195     Opc = Mips::STORE_ACC64;
196   else if (Mips::ACC64DSPRegClass.hasSubClassEq(RC))
197     Opc = Mips::STORE_ACC64DSP;
198   else if (Mips::ACC128RegClass.hasSubClassEq(RC))
199     Opc = Mips::STORE_ACC128;
200   else if (Mips::DSPCCRegClass.hasSubClassEq(RC))
201     Opc = Mips::STORE_CCOND_DSP;
202   else if (Mips::FGR32RegClass.hasSubClassEq(RC))
203     Opc = Mips::SWC1;
204   else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
205     Opc = Mips::SDC1;
206   else if (Mips::FGR64RegClass.hasSubClassEq(RC))
207     Opc = Mips::SDC164;
208   else if (RC->hasType(MVT::v16i8))
209     Opc = Mips::ST_B;
210   else if (RC->hasType(MVT::v8i16) || RC->hasType(MVT::v8f16))
211     Opc = Mips::ST_H;
212   else if (RC->hasType(MVT::v4i32) || RC->hasType(MVT::v4f32))
213     Opc = Mips::ST_W;
214   else if (RC->hasType(MVT::v2i64) || RC->hasType(MVT::v2f64))
215     Opc = Mips::ST_D;
216   else if (Mips::LO32RegClass.hasSubClassEq(RC))
217     Opc = Mips::SW;
218   else if (Mips::LO64RegClass.hasSubClassEq(RC))
219     Opc = Mips::SD;
220   else if (Mips::HI32RegClass.hasSubClassEq(RC))
221     Opc = Mips::SW;
222   else if (Mips::HI64RegClass.hasSubClassEq(RC))
223     Opc = Mips::SD;
224 
225   // Hi, Lo are normally caller save but they are callee save
226   // for interrupt handling.
227   const Function *Func = MBB.getParent()->getFunction();
228   if (Func->hasFnAttribute("interrupt")) {
229     if (Mips::HI32RegClass.hasSubClassEq(RC)) {
230       BuildMI(MBB, I, DL, get(Mips::MFHI), Mips::K0);
231       SrcReg = Mips::K0;
232     } else if (Mips::HI64RegClass.hasSubClassEq(RC)) {
233       BuildMI(MBB, I, DL, get(Mips::MFHI64), Mips::K0_64);
234       SrcReg = Mips::K0_64;
235     } else if (Mips::LO32RegClass.hasSubClassEq(RC)) {
236       BuildMI(MBB, I, DL, get(Mips::MFLO), Mips::K0);
237       SrcReg = Mips::K0;
238     } else if (Mips::LO64RegClass.hasSubClassEq(RC)) {
239       BuildMI(MBB, I, DL, get(Mips::MFLO64), Mips::K0_64);
240       SrcReg = Mips::K0_64;
241     }
242   }
243 
244   assert(Opc && "Register class not handled!");
245   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
246     .addFrameIndex(FI).addImm(Offset).addMemOperand(MMO);
247 }
248 
249 void MipsSEInstrInfo::
250 loadRegFromStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
251                  unsigned DestReg, int FI, const TargetRegisterClass *RC,
252                  const TargetRegisterInfo *TRI, int64_t Offset) const {
253   DebugLoc DL;
254   if (I != MBB.end()) DL = I->getDebugLoc();
255   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
256   unsigned Opc = 0;
257 
258   const Function *Func = MBB.getParent()->getFunction();
259   bool ReqIndirectLoad = Func->hasFnAttribute("interrupt") &&
260                          (DestReg == Mips::LO0 || DestReg == Mips::LO0_64 ||
261                           DestReg == Mips::HI0 || DestReg == Mips::HI0_64);
262 
263   if (Mips::GPR32RegClass.hasSubClassEq(RC))
264     Opc = Mips::LW;
265   else if (Mips::GPR64RegClass.hasSubClassEq(RC))
266     Opc = Mips::LD;
267   else if (Mips::ACC64RegClass.hasSubClassEq(RC))
268     Opc = Mips::LOAD_ACC64;
269   else if (Mips::ACC64DSPRegClass.hasSubClassEq(RC))
270     Opc = Mips::LOAD_ACC64DSP;
271   else if (Mips::ACC128RegClass.hasSubClassEq(RC))
272     Opc = Mips::LOAD_ACC128;
273   else if (Mips::DSPCCRegClass.hasSubClassEq(RC))
274     Opc = Mips::LOAD_CCOND_DSP;
275   else if (Mips::FGR32RegClass.hasSubClassEq(RC))
276     Opc = Mips::LWC1;
277   else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
278     Opc = Mips::LDC1;
279   else if (Mips::FGR64RegClass.hasSubClassEq(RC))
280     Opc = Mips::LDC164;
281   else if (RC->hasType(MVT::v16i8))
282     Opc = Mips::LD_B;
283   else if (RC->hasType(MVT::v8i16) || RC->hasType(MVT::v8f16))
284     Opc = Mips::LD_H;
285   else if (RC->hasType(MVT::v4i32) || RC->hasType(MVT::v4f32))
286     Opc = Mips::LD_W;
287   else if (RC->hasType(MVT::v2i64) || RC->hasType(MVT::v2f64))
288     Opc = Mips::LD_D;
289   else if (Mips::HI32RegClass.hasSubClassEq(RC))
290     Opc = Mips::LW;
291   else if (Mips::HI64RegClass.hasSubClassEq(RC))
292     Opc = Mips::LD;
293   else if (Mips::LO32RegClass.hasSubClassEq(RC))
294     Opc = Mips::LW;
295   else if (Mips::LO64RegClass.hasSubClassEq(RC))
296     Opc = Mips::LD;
297 
298   assert(Opc && "Register class not handled!");
299 
300   if (!ReqIndirectLoad)
301     BuildMI(MBB, I, DL, get(Opc), DestReg)
302         .addFrameIndex(FI)
303         .addImm(Offset)
304         .addMemOperand(MMO);
305   else {
306     // Load HI/LO through K0. Notably the DestReg is encoded into the
307     // instruction itself.
308     unsigned Reg = Mips::K0;
309     unsigned LdOp = Mips::MTLO;
310     if (DestReg == Mips::HI0)
311       LdOp = Mips::MTHI;
312 
313     if (Subtarget.getABI().ArePtrs64bit()) {
314       Reg = Mips::K0_64;
315       if (DestReg == Mips::HI0_64)
316         LdOp = Mips::MTHI64;
317       else
318         LdOp = Mips::MTLO64;
319     }
320 
321     BuildMI(MBB, I, DL, get(Opc), Reg)
322         .addFrameIndex(FI)
323         .addImm(Offset)
324         .addMemOperand(MMO);
325     BuildMI(MBB, I, DL, get(LdOp)).addReg(Reg);
326   }
327 }
328 
329 bool MipsSEInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
330   MachineBasicBlock &MBB = *MI->getParent();
331   bool isMicroMips = Subtarget.inMicroMipsMode();
332   unsigned Opc;
333 
334   switch(MI->getDesc().getOpcode()) {
335   default:
336     return false;
337   case Mips::RetRA:
338     expandRetRA(MBB, MI);
339     break;
340   case Mips::ERet:
341     expandERet(MBB, MI);
342     break;
343   case Mips::PseudoMFHI:
344     Opc = isMicroMips ? Mips::MFHI16_MM : Mips::MFHI;
345     expandPseudoMFHiLo(MBB, MI, Opc);
346     break;
347   case Mips::PseudoMFLO:
348     Opc = isMicroMips ? Mips::MFLO16_MM : Mips::MFLO;
349     expandPseudoMFHiLo(MBB, MI, Opc);
350     break;
351   case Mips::PseudoMFHI64:
352     expandPseudoMFHiLo(MBB, MI, Mips::MFHI64);
353     break;
354   case Mips::PseudoMFLO64:
355     expandPseudoMFHiLo(MBB, MI, Mips::MFLO64);
356     break;
357   case Mips::PseudoMTLOHI:
358     expandPseudoMTLoHi(MBB, MI, Mips::MTLO, Mips::MTHI, false);
359     break;
360   case Mips::PseudoMTLOHI64:
361     expandPseudoMTLoHi(MBB, MI, Mips::MTLO64, Mips::MTHI64, false);
362     break;
363   case Mips::PseudoMTLOHI_DSP:
364     expandPseudoMTLoHi(MBB, MI, Mips::MTLO_DSP, Mips::MTHI_DSP, true);
365     break;
366   case Mips::PseudoCVT_S_W:
367     expandCvtFPInt(MBB, MI, Mips::CVT_S_W, Mips::MTC1, false);
368     break;
369   case Mips::PseudoCVT_D32_W:
370     expandCvtFPInt(MBB, MI, Mips::CVT_D32_W, Mips::MTC1, false);
371     break;
372   case Mips::PseudoCVT_S_L:
373     expandCvtFPInt(MBB, MI, Mips::CVT_S_L, Mips::DMTC1, true);
374     break;
375   case Mips::PseudoCVT_D64_W:
376     expandCvtFPInt(MBB, MI, Mips::CVT_D64_W, Mips::MTC1, true);
377     break;
378   case Mips::PseudoCVT_D64_L:
379     expandCvtFPInt(MBB, MI, Mips::CVT_D64_L, Mips::DMTC1, true);
380     break;
381   case Mips::BuildPairF64:
382     expandBuildPairF64(MBB, MI, false);
383     break;
384   case Mips::BuildPairF64_64:
385     expandBuildPairF64(MBB, MI, true);
386     break;
387   case Mips::ExtractElementF64:
388     expandExtractElementF64(MBB, MI, false);
389     break;
390   case Mips::ExtractElementF64_64:
391     expandExtractElementF64(MBB, MI, true);
392     break;
393   case Mips::MIPSeh_return32:
394   case Mips::MIPSeh_return64:
395     expandEhReturn(MBB, MI);
396     break;
397   }
398 
399   MBB.erase(MI);
400   return true;
401 }
402 
403 /// getOppositeBranchOpc - Return the inverse of the specified
404 /// opcode, e.g. turning BEQ to BNE.
405 unsigned MipsSEInstrInfo::getOppositeBranchOpc(unsigned Opc) const {
406   switch (Opc) {
407   default:           llvm_unreachable("Illegal opcode!");
408   case Mips::BEQ:    return Mips::BNE;
409   case Mips::BNE:    return Mips::BEQ;
410   case Mips::BGTZ:   return Mips::BLEZ;
411   case Mips::BGEZ:   return Mips::BLTZ;
412   case Mips::BLTZ:   return Mips::BGEZ;
413   case Mips::BLEZ:   return Mips::BGTZ;
414   case Mips::BEQ64:  return Mips::BNE64;
415   case Mips::BNE64:  return Mips::BEQ64;
416   case Mips::BGTZ64: return Mips::BLEZ64;
417   case Mips::BGEZ64: return Mips::BLTZ64;
418   case Mips::BLTZ64: return Mips::BGEZ64;
419   case Mips::BLEZ64: return Mips::BGTZ64;
420   case Mips::BC1T:   return Mips::BC1F;
421   case Mips::BC1F:   return Mips::BC1T;
422   case Mips::BEQZC_MM: return Mips::BNEZC_MM;
423   case Mips::BNEZC_MM: return Mips::BEQZC_MM;
424   case Mips::BEQZC:  return Mips::BNEZC;
425   case Mips::BNEZC:  return Mips::BEQZC;
426   case Mips::BEQC:   return Mips::BNEC;
427   case Mips::BNEC:   return Mips::BEQC;
428   case Mips::BGTZC:  return Mips::BLEZC;
429   case Mips::BGEZC:  return Mips::BLTZC;
430   case Mips::BLTZC:  return Mips::BGEZC;
431   case Mips::BLEZC:  return Mips::BGTZC;
432   }
433 }
434 
435 /// Adjust SP by Amount bytes.
436 void MipsSEInstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
437                                      MachineBasicBlock &MBB,
438                                      MachineBasicBlock::iterator I) const {
439   MipsABIInfo ABI = Subtarget.getABI();
440   DebugLoc DL;
441   unsigned ADDu = ABI.GetPtrAdduOp();
442   unsigned ADDiu = ABI.GetPtrAddiuOp();
443 
444   if (Amount == 0)
445     return;
446 
447   if (isInt<16>(Amount))// addi sp, sp, amount
448     BuildMI(MBB, I, DL, get(ADDiu), SP).addReg(SP).addImm(Amount);
449   else { // Expand immediate that doesn't fit in 16-bit.
450     unsigned Reg = loadImmediate(Amount, MBB, I, DL, nullptr);
451     BuildMI(MBB, I, DL, get(ADDu), SP).addReg(SP).addReg(Reg, RegState::Kill);
452   }
453 }
454 
455 /// This function generates the sequence of instructions needed to get the
456 /// result of adding register REG and immediate IMM.
457 unsigned
458 MipsSEInstrInfo::loadImmediate(int64_t Imm, MachineBasicBlock &MBB,
459                                MachineBasicBlock::iterator II, DebugLoc DL,
460                                unsigned *NewImm) const {
461   MipsAnalyzeImmediate AnalyzeImm;
462   const MipsSubtarget &STI = Subtarget;
463   MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
464   unsigned Size = STI.isABI_N64() ? 64 : 32;
465   unsigned LUi = STI.isABI_N64() ? Mips::LUi64 : Mips::LUi;
466   unsigned ZEROReg = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
467   const TargetRegisterClass *RC = STI.isABI_N64() ?
468     &Mips::GPR64RegClass : &Mips::GPR32RegClass;
469   bool LastInstrIsADDiu = NewImm;
470 
471   const MipsAnalyzeImmediate::InstSeq &Seq =
472     AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu);
473   MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
474 
475   assert(Seq.size() && (!LastInstrIsADDiu || (Seq.size() > 1)));
476 
477   // The first instruction can be a LUi, which is different from other
478   // instructions (ADDiu, ORI and SLL) in that it does not have a register
479   // operand.
480   unsigned Reg = RegInfo.createVirtualRegister(RC);
481 
482   if (Inst->Opc == LUi)
483     BuildMI(MBB, II, DL, get(LUi), Reg).addImm(SignExtend64<16>(Inst->ImmOpnd));
484   else
485     BuildMI(MBB, II, DL, get(Inst->Opc), Reg).addReg(ZEROReg)
486       .addImm(SignExtend64<16>(Inst->ImmOpnd));
487 
488   // Build the remaining instructions in Seq.
489   for (++Inst; Inst != Seq.end() - LastInstrIsADDiu; ++Inst)
490     BuildMI(MBB, II, DL, get(Inst->Opc), Reg).addReg(Reg, RegState::Kill)
491       .addImm(SignExtend64<16>(Inst->ImmOpnd));
492 
493   if (LastInstrIsADDiu)
494     *NewImm = Inst->ImmOpnd;
495 
496   return Reg;
497 }
498 
499 unsigned MipsSEInstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
500   return (Opc == Mips::BEQ    || Opc == Mips::BNE    || Opc == Mips::BGTZ   ||
501           Opc == Mips::BGEZ   || Opc == Mips::BLTZ   || Opc == Mips::BLEZ   ||
502           Opc == Mips::BEQ64  || Opc == Mips::BNE64  || Opc == Mips::BGTZ64 ||
503           Opc == Mips::BGEZ64 || Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 ||
504           Opc == Mips::BC1T   || Opc == Mips::BC1F   || Opc == Mips::B      ||
505           Opc == Mips::J  || Opc == Mips::BEQZC_MM || Opc == Mips::BNEZC_MM ||
506           Opc == Mips::BEQC   || Opc == Mips::BNEC   || Opc == Mips::BLTC   ||
507           Opc == Mips::BGEC   || Opc == Mips::BLTUC  || Opc == Mips::BGEUC  ||
508           Opc == Mips::BGTZC  || Opc == Mips::BLEZC  || Opc == Mips::BGEZC  ||
509           Opc == Mips::BGTZC  || Opc == Mips::BEQZC  || Opc == Mips::BNEZC  ||
510           Opc == Mips::BC) ? Opc : 0;
511 }
512 
513 void MipsSEInstrInfo::expandRetRA(MachineBasicBlock &MBB,
514                                   MachineBasicBlock::iterator I) const {
515   if (Subtarget.isGP64bit())
516     BuildMI(MBB, I, I->getDebugLoc(), get(Mips::PseudoReturn64))
517         .addReg(Mips::RA_64);
518   else
519     BuildMI(MBB, I, I->getDebugLoc(), get(Mips::PseudoReturn)).addReg(Mips::RA);
520 }
521 
522 void MipsSEInstrInfo::expandERet(MachineBasicBlock &MBB,
523                                  MachineBasicBlock::iterator I) const {
524   BuildMI(MBB, I, I->getDebugLoc(), get(Mips::ERET));
525 }
526 
527 std::pair<bool, bool>
528 MipsSEInstrInfo::compareOpndSize(unsigned Opc,
529                                  const MachineFunction &MF) const {
530   const MCInstrDesc &Desc = get(Opc);
531   assert(Desc.NumOperands == 2 && "Unary instruction expected.");
532   const MipsRegisterInfo *RI = &getRegisterInfo();
533   unsigned DstRegSize = getRegClass(Desc, 0, RI, MF)->getSize();
534   unsigned SrcRegSize = getRegClass(Desc, 1, RI, MF)->getSize();
535 
536   return std::make_pair(DstRegSize > SrcRegSize, DstRegSize < SrcRegSize);
537 }
538 
539 void MipsSEInstrInfo::expandPseudoMFHiLo(MachineBasicBlock &MBB,
540                                          MachineBasicBlock::iterator I,
541                                          unsigned NewOpc) const {
542   BuildMI(MBB, I, I->getDebugLoc(), get(NewOpc), I->getOperand(0).getReg());
543 }
544 
545 void MipsSEInstrInfo::expandPseudoMTLoHi(MachineBasicBlock &MBB,
546                                          MachineBasicBlock::iterator I,
547                                          unsigned LoOpc,
548                                          unsigned HiOpc,
549                                          bool HasExplicitDef) const {
550   // Expand
551   //  lo_hi pseudomtlohi $gpr0, $gpr1
552   // to these two instructions:
553   //  mtlo $gpr0
554   //  mthi $gpr1
555 
556   DebugLoc DL = I->getDebugLoc();
557   const MachineOperand &SrcLo = I->getOperand(1), &SrcHi = I->getOperand(2);
558   MachineInstrBuilder LoInst = BuildMI(MBB, I, DL, get(LoOpc));
559   MachineInstrBuilder HiInst = BuildMI(MBB, I, DL, get(HiOpc));
560 
561   // Add lo/hi registers if the mtlo/hi instructions created have explicit
562   // def registers.
563   if (HasExplicitDef) {
564     unsigned DstReg = I->getOperand(0).getReg();
565     unsigned DstLo = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
566     unsigned DstHi = getRegisterInfo().getSubReg(DstReg, Mips::sub_hi);
567     LoInst.addReg(DstLo, RegState::Define);
568     HiInst.addReg(DstHi, RegState::Define);
569   }
570 
571   LoInst.addReg(SrcLo.getReg(), getKillRegState(SrcLo.isKill()));
572   HiInst.addReg(SrcHi.getReg(), getKillRegState(SrcHi.isKill()));
573 }
574 
575 void MipsSEInstrInfo::expandCvtFPInt(MachineBasicBlock &MBB,
576                                      MachineBasicBlock::iterator I,
577                                      unsigned CvtOpc, unsigned MovOpc,
578                                      bool IsI64) const {
579   const MCInstrDesc &CvtDesc = get(CvtOpc), &MovDesc = get(MovOpc);
580   const MachineOperand &Dst = I->getOperand(0), &Src = I->getOperand(1);
581   unsigned DstReg = Dst.getReg(), SrcReg = Src.getReg(), TmpReg = DstReg;
582   unsigned KillSrc =  getKillRegState(Src.isKill());
583   DebugLoc DL = I->getDebugLoc();
584   bool DstIsLarger, SrcIsLarger;
585 
586   std::tie(DstIsLarger, SrcIsLarger) =
587       compareOpndSize(CvtOpc, *MBB.getParent());
588 
589   if (DstIsLarger)
590     TmpReg = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
591 
592   if (SrcIsLarger)
593     DstReg = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
594 
595   BuildMI(MBB, I, DL, MovDesc, TmpReg).addReg(SrcReg, KillSrc);
596   BuildMI(MBB, I, DL, CvtDesc, DstReg).addReg(TmpReg, RegState::Kill);
597 }
598 
599 void MipsSEInstrInfo::expandExtractElementF64(MachineBasicBlock &MBB,
600                                               MachineBasicBlock::iterator I,
601                                               bool FP64) const {
602   unsigned DstReg = I->getOperand(0).getReg();
603   unsigned SrcReg = I->getOperand(1).getReg();
604   unsigned N = I->getOperand(2).getImm();
605   DebugLoc dl = I->getDebugLoc();
606 
607   assert(N < 2 && "Invalid immediate");
608   unsigned SubIdx = N ? Mips::sub_hi : Mips::sub_lo;
609   unsigned SubReg = getRegisterInfo().getSubReg(SrcReg, SubIdx);
610 
611   // FPXX on MIPS-II or MIPS32r1 should have been handled with a spill/reload
612   // in MipsSEFrameLowering.cpp.
613   assert(!(Subtarget.isABI_FPXX() && !Subtarget.hasMips32r2()));
614 
615   // FP64A (FP64 with nooddspreg) should have been handled with a spill/reload
616   // in MipsSEFrameLowering.cpp.
617   assert(!(Subtarget.isFP64bit() && !Subtarget.useOddSPReg()));
618 
619   if (SubIdx == Mips::sub_hi && Subtarget.hasMTHC1()) {
620     // FIXME: Strictly speaking MFHC1 only reads the top 32-bits however, we
621     //        claim to read the whole 64-bits as part of a white lie used to
622     //        temporarily work around a widespread bug in the -mfp64 support.
623     //        The problem is that none of the 32-bit fpu ops mention the fact
624     //        that they clobber the upper 32-bits of the 64-bit FPR. Fixing that
625     //        requires a major overhaul of the FPU implementation which can't
626     //        be done right now due to time constraints.
627     //        MFHC1 is one of two instructions that are affected since they are
628     //        the only instructions that don't read the lower 32-bits.
629     //        We therefore pretend that it reads the bottom 32-bits to
630     //        artificially create a dependency and prevent the scheduler
631     //        changing the behaviour of the code.
632     BuildMI(MBB, I, dl, get(FP64 ? Mips::MFHC1_D64 : Mips::MFHC1_D32), DstReg)
633         .addReg(SrcReg);
634   } else
635     BuildMI(MBB, I, dl, get(Mips::MFC1), DstReg).addReg(SubReg);
636 }
637 
638 void MipsSEInstrInfo::expandBuildPairF64(MachineBasicBlock &MBB,
639                                          MachineBasicBlock::iterator I,
640                                          bool FP64) const {
641   unsigned DstReg = I->getOperand(0).getReg();
642   unsigned LoReg = I->getOperand(1).getReg(), HiReg = I->getOperand(2).getReg();
643   const MCInstrDesc& Mtc1Tdd = get(Mips::MTC1);
644   DebugLoc dl = I->getDebugLoc();
645   const TargetRegisterInfo &TRI = getRegisterInfo();
646 
647   // When mthc1 is available, use:
648   //   mtc1 Lo, $fp
649   //   mthc1 Hi, $fp
650   //
651   // Otherwise, for O32 FPXX ABI:
652   //   spill + reload via ldc1
653   // This case is handled by the frame lowering code.
654   //
655   // Otherwise, for FP32:
656   //   mtc1 Lo, $fp
657   //   mtc1 Hi, $fp + 1
658   //
659   // The case where dmtc1 is available doesn't need to be handled here
660   // because it never creates a BuildPairF64 node.
661 
662   // FPXX on MIPS-II or MIPS32r1 should have been handled with a spill/reload
663   // in MipsSEFrameLowering.cpp.
664   assert(!(Subtarget.isABI_FPXX() && !Subtarget.hasMips32r2()));
665 
666   // FP64A (FP64 with nooddspreg) should have been handled with a spill/reload
667   // in MipsSEFrameLowering.cpp.
668   assert(!(Subtarget.isFP64bit() && !Subtarget.useOddSPReg()));
669 
670   BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_lo))
671     .addReg(LoReg);
672 
673   if (Subtarget.hasMTHC1()) {
674     // FIXME: The .addReg(DstReg) is a white lie used to temporarily work
675     //        around a widespread bug in the -mfp64 support.
676     //        The problem is that none of the 32-bit fpu ops mention the fact
677     //        that they clobber the upper 32-bits of the 64-bit FPR. Fixing that
678     //        requires a major overhaul of the FPU implementation which can't
679     //        be done right now due to time constraints.
680     //        MTHC1 is one of two instructions that are affected since they are
681     //        the only instructions that don't read the lower 32-bits.
682     //        We therefore pretend that it reads the bottom 32-bits to
683     //        artificially create a dependency and prevent the scheduler
684     //        changing the behaviour of the code.
685     BuildMI(MBB, I, dl, get(FP64 ? Mips::MTHC1_D64 : Mips::MTHC1_D32), DstReg)
686         .addReg(DstReg)
687         .addReg(HiReg);
688   } else if (Subtarget.isABI_FPXX())
689     llvm_unreachable("BuildPairF64 not expanded in frame lowering code!");
690   else
691     BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_hi))
692       .addReg(HiReg);
693 }
694 
695 void MipsSEInstrInfo::expandEhReturn(MachineBasicBlock &MBB,
696                                      MachineBasicBlock::iterator I) const {
697   // This pseudo instruction is generated as part of the lowering of
698   // ISD::EH_RETURN. We convert it to a stack increment by OffsetReg, and
699   // indirect jump to TargetReg
700   MipsABIInfo ABI = Subtarget.getABI();
701   unsigned ADDU = ABI.GetPtrAdduOp();
702   unsigned SP = Subtarget.isGP64bit() ? Mips::SP_64 : Mips::SP;
703   unsigned RA = Subtarget.isGP64bit() ? Mips::RA_64 : Mips::RA;
704   unsigned T9 = Subtarget.isGP64bit() ? Mips::T9_64 : Mips::T9;
705   unsigned ZERO = Subtarget.isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
706   unsigned OffsetReg = I->getOperand(0).getReg();
707   unsigned TargetReg = I->getOperand(1).getReg();
708 
709   // addu $ra, $v0, $zero
710   // addu $sp, $sp, $v1
711   // jr   $ra (via RetRA)
712   const TargetMachine &TM = MBB.getParent()->getTarget();
713   if (TM.getRelocationModel() == Reloc::PIC_)
714     BuildMI(MBB, I, I->getDebugLoc(), get(ADDU), T9)
715         .addReg(TargetReg)
716         .addReg(ZERO);
717   BuildMI(MBB, I, I->getDebugLoc(), get(ADDU), RA)
718       .addReg(TargetReg)
719       .addReg(ZERO);
720   BuildMI(MBB, I, I->getDebugLoc(), get(ADDU), SP).addReg(SP).addReg(OffsetReg);
721   expandRetRA(MBB, I);
722 }
723 
724 const MipsInstrInfo *llvm::createMipsSEInstrInfo(const MipsSubtarget &STI) {
725   return new MipsSEInstrInfo(STI);
726 }
727