xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Mips/MipsSEInstrInfo.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- MipsSEInstrInfo.cpp - Mips32/64 Instruction Information -----------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file contains the Mips32/64 implementation of the TargetInstrInfo class.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "MipsSEInstrInfo.h"
14*0b57cec5SDimitry Andric #include "MCTargetDesc/MipsInstPrinter.h"
15*0b57cec5SDimitry Andric #include "MipsAnalyzeImmediate.h"
16*0b57cec5SDimitry Andric #include "MipsMachineFunction.h"
17*0b57cec5SDimitry Andric #include "MipsTargetMachine.h"
18*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
19*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
20*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
21*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
22*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
23*0b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric using namespace llvm;
26*0b57cec5SDimitry Andric 
27*0b57cec5SDimitry Andric static unsigned getUnconditionalBranch(const MipsSubtarget &STI) {
28*0b57cec5SDimitry Andric   if (STI.inMicroMipsMode())
29*0b57cec5SDimitry Andric     return STI.isPositionIndependent() ? Mips::B_MM : Mips::J_MM;
30*0b57cec5SDimitry Andric   return STI.isPositionIndependent() ? Mips::B : Mips::J;
31*0b57cec5SDimitry Andric }
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric MipsSEInstrInfo::MipsSEInstrInfo(const MipsSubtarget &STI)
34*0b57cec5SDimitry Andric     : MipsInstrInfo(STI, getUnconditionalBranch(STI)), RI() {}
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric const MipsRegisterInfo &MipsSEInstrInfo::getRegisterInfo() const {
37*0b57cec5SDimitry Andric   return RI;
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric 
40*0b57cec5SDimitry Andric /// isLoadFromStackSlot - If the specified machine instruction is a direct
41*0b57cec5SDimitry Andric /// load from a stack slot, return the virtual or physical register number of
42*0b57cec5SDimitry Andric /// the destination along with the FrameIndex of the loaded stack slot.  If
43*0b57cec5SDimitry Andric /// not, return 0.  This predicate must return 0 if the instruction has
44*0b57cec5SDimitry Andric /// any side effects other than loading from the stack slot.
45*0b57cec5SDimitry Andric unsigned MipsSEInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
46*0b57cec5SDimitry Andric                                               int &FrameIndex) const {
47*0b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric   if ((Opc == Mips::LW)   || (Opc == Mips::LD)   ||
50*0b57cec5SDimitry Andric       (Opc == Mips::LWC1) || (Opc == Mips::LDC1) || (Opc == Mips::LDC164)) {
51*0b57cec5SDimitry Andric     if ((MI.getOperand(1).isFI()) &&  // is a stack slot
52*0b57cec5SDimitry Andric         (MI.getOperand(2).isImm()) && // the imm is zero
53*0b57cec5SDimitry Andric         (isZeroImm(MI.getOperand(2)))) {
54*0b57cec5SDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
55*0b57cec5SDimitry Andric       return MI.getOperand(0).getReg();
56*0b57cec5SDimitry Andric     }
57*0b57cec5SDimitry Andric   }
58*0b57cec5SDimitry Andric 
59*0b57cec5SDimitry Andric   return 0;
60*0b57cec5SDimitry Andric }
61*0b57cec5SDimitry Andric 
62*0b57cec5SDimitry Andric /// isStoreToStackSlot - If the specified machine instruction is a direct
63*0b57cec5SDimitry Andric /// store to a stack slot, return the virtual or physical register number of
64*0b57cec5SDimitry Andric /// the source reg along with the FrameIndex of the loaded stack slot.  If
65*0b57cec5SDimitry Andric /// not, return 0.  This predicate must return 0 if the instruction has
66*0b57cec5SDimitry Andric /// any side effects other than storing to the stack slot.
67*0b57cec5SDimitry Andric unsigned MipsSEInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
68*0b57cec5SDimitry Andric                                              int &FrameIndex) const {
69*0b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric   if ((Opc == Mips::SW)   || (Opc == Mips::SD)   ||
72*0b57cec5SDimitry Andric       (Opc == Mips::SWC1) || (Opc == Mips::SDC1) || (Opc == Mips::SDC164)) {
73*0b57cec5SDimitry Andric     if ((MI.getOperand(1).isFI()) &&  // is a stack slot
74*0b57cec5SDimitry Andric         (MI.getOperand(2).isImm()) && // the imm is zero
75*0b57cec5SDimitry Andric         (isZeroImm(MI.getOperand(2)))) {
76*0b57cec5SDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
77*0b57cec5SDimitry Andric       return MI.getOperand(0).getReg();
78*0b57cec5SDimitry Andric     }
79*0b57cec5SDimitry Andric   }
80*0b57cec5SDimitry Andric   return 0;
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric 
83*0b57cec5SDimitry Andric void MipsSEInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
84*0b57cec5SDimitry Andric                                   MachineBasicBlock::iterator I,
85*0b57cec5SDimitry Andric                                   const DebugLoc &DL, unsigned DestReg,
86*0b57cec5SDimitry Andric                                   unsigned SrcReg, bool KillSrc) const {
87*0b57cec5SDimitry Andric   unsigned Opc = 0, ZeroReg = 0;
88*0b57cec5SDimitry Andric   bool isMicroMips = Subtarget.inMicroMipsMode();
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric   if (Mips::GPR32RegClass.contains(DestReg)) { // Copy to CPU Reg.
91*0b57cec5SDimitry Andric     if (Mips::GPR32RegClass.contains(SrcReg)) {
92*0b57cec5SDimitry Andric       if (isMicroMips)
93*0b57cec5SDimitry Andric         Opc = Mips::MOVE16_MM;
94*0b57cec5SDimitry Andric       else
95*0b57cec5SDimitry Andric         Opc = Mips::OR, ZeroReg = Mips::ZERO;
96*0b57cec5SDimitry Andric     } else if (Mips::CCRRegClass.contains(SrcReg))
97*0b57cec5SDimitry Andric       Opc = Mips::CFC1;
98*0b57cec5SDimitry Andric     else if (Mips::FGR32RegClass.contains(SrcReg))
99*0b57cec5SDimitry Andric       Opc = Mips::MFC1;
100*0b57cec5SDimitry Andric     else if (Mips::HI32RegClass.contains(SrcReg)) {
101*0b57cec5SDimitry Andric       Opc = isMicroMips ? Mips::MFHI16_MM : Mips::MFHI;
102*0b57cec5SDimitry Andric       SrcReg = 0;
103*0b57cec5SDimitry Andric     } else if (Mips::LO32RegClass.contains(SrcReg)) {
104*0b57cec5SDimitry Andric       Opc = isMicroMips ? Mips::MFLO16_MM : Mips::MFLO;
105*0b57cec5SDimitry Andric       SrcReg = 0;
106*0b57cec5SDimitry Andric     } else if (Mips::HI32DSPRegClass.contains(SrcReg))
107*0b57cec5SDimitry Andric       Opc = Mips::MFHI_DSP;
108*0b57cec5SDimitry Andric     else if (Mips::LO32DSPRegClass.contains(SrcReg))
109*0b57cec5SDimitry Andric       Opc = Mips::MFLO_DSP;
110*0b57cec5SDimitry Andric     else if (Mips::DSPCCRegClass.contains(SrcReg)) {
111*0b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(Mips::RDDSP), DestReg).addImm(1 << 4)
112*0b57cec5SDimitry Andric         .addReg(SrcReg, RegState::Implicit | getKillRegState(KillSrc));
113*0b57cec5SDimitry Andric       return;
114*0b57cec5SDimitry Andric     }
115*0b57cec5SDimitry Andric     else if (Mips::MSACtrlRegClass.contains(SrcReg))
116*0b57cec5SDimitry Andric       Opc = Mips::CFCMSA;
117*0b57cec5SDimitry Andric   }
118*0b57cec5SDimitry Andric   else if (Mips::GPR32RegClass.contains(SrcReg)) { // Copy from CPU Reg.
119*0b57cec5SDimitry Andric     if (Mips::CCRRegClass.contains(DestReg))
120*0b57cec5SDimitry Andric       Opc = Mips::CTC1;
121*0b57cec5SDimitry Andric     else if (Mips::FGR32RegClass.contains(DestReg))
122*0b57cec5SDimitry Andric       Opc = Mips::MTC1;
123*0b57cec5SDimitry Andric     else if (Mips::HI32RegClass.contains(DestReg))
124*0b57cec5SDimitry Andric       Opc = Mips::MTHI, DestReg = 0;
125*0b57cec5SDimitry Andric     else if (Mips::LO32RegClass.contains(DestReg))
126*0b57cec5SDimitry Andric       Opc = Mips::MTLO, DestReg = 0;
127*0b57cec5SDimitry Andric     else if (Mips::HI32DSPRegClass.contains(DestReg))
128*0b57cec5SDimitry Andric       Opc = Mips::MTHI_DSP;
129*0b57cec5SDimitry Andric     else if (Mips::LO32DSPRegClass.contains(DestReg))
130*0b57cec5SDimitry Andric       Opc = Mips::MTLO_DSP;
131*0b57cec5SDimitry Andric     else if (Mips::DSPCCRegClass.contains(DestReg)) {
132*0b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(Mips::WRDSP))
133*0b57cec5SDimitry Andric         .addReg(SrcReg, getKillRegState(KillSrc)).addImm(1 << 4)
134*0b57cec5SDimitry Andric         .addReg(DestReg, RegState::ImplicitDefine);
135*0b57cec5SDimitry Andric       return;
136*0b57cec5SDimitry Andric     } else if (Mips::MSACtrlRegClass.contains(DestReg)) {
137*0b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(Mips::CTCMSA))
138*0b57cec5SDimitry Andric           .addReg(DestReg)
139*0b57cec5SDimitry Andric           .addReg(SrcReg, getKillRegState(KillSrc));
140*0b57cec5SDimitry Andric       return;
141*0b57cec5SDimitry Andric     }
142*0b57cec5SDimitry Andric   }
143*0b57cec5SDimitry Andric   else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
144*0b57cec5SDimitry Andric     Opc = Mips::FMOV_S;
145*0b57cec5SDimitry Andric   else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
146*0b57cec5SDimitry Andric     Opc = Mips::FMOV_D32;
147*0b57cec5SDimitry Andric   else if (Mips::FGR64RegClass.contains(DestReg, SrcReg))
148*0b57cec5SDimitry Andric     Opc = Mips::FMOV_D64;
149*0b57cec5SDimitry Andric   else if (Mips::GPR64RegClass.contains(DestReg)) { // Copy to CPU64 Reg.
150*0b57cec5SDimitry Andric     if (Mips::GPR64RegClass.contains(SrcReg))
151*0b57cec5SDimitry Andric       Opc = Mips::OR64, ZeroReg = Mips::ZERO_64;
152*0b57cec5SDimitry Andric     else if (Mips::HI64RegClass.contains(SrcReg))
153*0b57cec5SDimitry Andric       Opc = Mips::MFHI64, SrcReg = 0;
154*0b57cec5SDimitry Andric     else if (Mips::LO64RegClass.contains(SrcReg))
155*0b57cec5SDimitry Andric       Opc = Mips::MFLO64, SrcReg = 0;
156*0b57cec5SDimitry Andric     else if (Mips::FGR64RegClass.contains(SrcReg))
157*0b57cec5SDimitry Andric       Opc = Mips::DMFC1;
158*0b57cec5SDimitry Andric   }
159*0b57cec5SDimitry Andric   else if (Mips::GPR64RegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
160*0b57cec5SDimitry Andric     if (Mips::HI64RegClass.contains(DestReg))
161*0b57cec5SDimitry Andric       Opc = Mips::MTHI64, DestReg = 0;
162*0b57cec5SDimitry Andric     else if (Mips::LO64RegClass.contains(DestReg))
163*0b57cec5SDimitry Andric       Opc = Mips::MTLO64, DestReg = 0;
164*0b57cec5SDimitry Andric     else if (Mips::FGR64RegClass.contains(DestReg))
165*0b57cec5SDimitry Andric       Opc = Mips::DMTC1;
166*0b57cec5SDimitry Andric   }
167*0b57cec5SDimitry Andric   else if (Mips::MSA128BRegClass.contains(DestReg)) { // Copy to MSA reg
168*0b57cec5SDimitry Andric     if (Mips::MSA128BRegClass.contains(SrcReg))
169*0b57cec5SDimitry Andric       Opc = Mips::MOVE_V;
170*0b57cec5SDimitry Andric   }
171*0b57cec5SDimitry Andric 
172*0b57cec5SDimitry Andric   assert(Opc && "Cannot copy registers");
173*0b57cec5SDimitry Andric 
174*0b57cec5SDimitry Andric   MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
175*0b57cec5SDimitry Andric 
176*0b57cec5SDimitry Andric   if (DestReg)
177*0b57cec5SDimitry Andric     MIB.addReg(DestReg, RegState::Define);
178*0b57cec5SDimitry Andric 
179*0b57cec5SDimitry Andric   if (SrcReg)
180*0b57cec5SDimitry Andric     MIB.addReg(SrcReg, getKillRegState(KillSrc));
181*0b57cec5SDimitry Andric 
182*0b57cec5SDimitry Andric   if (ZeroReg)
183*0b57cec5SDimitry Andric     MIB.addReg(ZeroReg);
184*0b57cec5SDimitry Andric }
185*0b57cec5SDimitry Andric 
186*0b57cec5SDimitry Andric static bool isORCopyInst(const MachineInstr &MI) {
187*0b57cec5SDimitry Andric   switch (MI.getOpcode()) {
188*0b57cec5SDimitry Andric   default:
189*0b57cec5SDimitry Andric     break;
190*0b57cec5SDimitry Andric   case Mips::OR_MM:
191*0b57cec5SDimitry Andric   case Mips::OR:
192*0b57cec5SDimitry Andric     if (MI.getOperand(2).getReg() == Mips::ZERO)
193*0b57cec5SDimitry Andric       return true;
194*0b57cec5SDimitry Andric     break;
195*0b57cec5SDimitry Andric   case Mips::OR64:
196*0b57cec5SDimitry Andric     if (MI.getOperand(2).getReg() == Mips::ZERO_64)
197*0b57cec5SDimitry Andric       return true;
198*0b57cec5SDimitry Andric     break;
199*0b57cec5SDimitry Andric   }
200*0b57cec5SDimitry Andric   return false;
201*0b57cec5SDimitry Andric }
202*0b57cec5SDimitry Andric 
203*0b57cec5SDimitry Andric /// If @MI is WRDSP/RRDSP instruction return true with @isWrite set to true
204*0b57cec5SDimitry Andric /// if it is WRDSP instruction.
205*0b57cec5SDimitry Andric static bool isReadOrWriteToDSPReg(const MachineInstr &MI, bool &isWrite) {
206*0b57cec5SDimitry Andric   switch (MI.getOpcode()) {
207*0b57cec5SDimitry Andric   default:
208*0b57cec5SDimitry Andric    return false;
209*0b57cec5SDimitry Andric   case Mips::WRDSP:
210*0b57cec5SDimitry Andric   case Mips::WRDSP_MM:
211*0b57cec5SDimitry Andric     isWrite = true;
212*0b57cec5SDimitry Andric     break;
213*0b57cec5SDimitry Andric   case Mips::RDDSP:
214*0b57cec5SDimitry Andric   case Mips::RDDSP_MM:
215*0b57cec5SDimitry Andric     isWrite = false;
216*0b57cec5SDimitry Andric     break;
217*0b57cec5SDimitry Andric   }
218*0b57cec5SDimitry Andric   return true;
219*0b57cec5SDimitry Andric }
220*0b57cec5SDimitry Andric 
221*0b57cec5SDimitry Andric /// We check for the common case of 'or', as it's MIPS' preferred instruction
222*0b57cec5SDimitry Andric /// for GPRs but we have to check the operands to ensure that is the case.
223*0b57cec5SDimitry Andric /// Other move instructions for MIPS are directly identifiable.
224*0b57cec5SDimitry Andric bool MipsSEInstrInfo::isCopyInstrImpl(const MachineInstr &MI,
225*0b57cec5SDimitry Andric                                       const MachineOperand *&Src,
226*0b57cec5SDimitry Andric                                       const MachineOperand *&Dest) const {
227*0b57cec5SDimitry Andric   bool isDSPControlWrite = false;
228*0b57cec5SDimitry Andric   // Condition is made to match the creation of WRDSP/RDDSP copy instruction
229*0b57cec5SDimitry Andric   // from copyPhysReg function.
230*0b57cec5SDimitry Andric   if (isReadOrWriteToDSPReg(MI, isDSPControlWrite)) {
231*0b57cec5SDimitry Andric     if (!MI.getOperand(1).isImm() || MI.getOperand(1).getImm() != (1<<4))
232*0b57cec5SDimitry Andric       return false;
233*0b57cec5SDimitry Andric     else if (isDSPControlWrite) {
234*0b57cec5SDimitry Andric       Src = &MI.getOperand(0);
235*0b57cec5SDimitry Andric       Dest = &MI.getOperand(2);
236*0b57cec5SDimitry Andric     } else {
237*0b57cec5SDimitry Andric       Dest = &MI.getOperand(0);
238*0b57cec5SDimitry Andric       Src = &MI.getOperand(2);
239*0b57cec5SDimitry Andric     }
240*0b57cec5SDimitry Andric     return true;
241*0b57cec5SDimitry Andric   } else if (MI.isMoveReg() || isORCopyInst(MI)) {
242*0b57cec5SDimitry Andric     Dest = &MI.getOperand(0);
243*0b57cec5SDimitry Andric     Src = &MI.getOperand(1);
244*0b57cec5SDimitry Andric     return true;
245*0b57cec5SDimitry Andric   }
246*0b57cec5SDimitry Andric   return false;
247*0b57cec5SDimitry Andric }
248*0b57cec5SDimitry Andric 
249*0b57cec5SDimitry Andric void MipsSEInstrInfo::
250*0b57cec5SDimitry Andric storeRegToStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
251*0b57cec5SDimitry Andric                 unsigned SrcReg, bool isKill, int FI,
252*0b57cec5SDimitry Andric                 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI,
253*0b57cec5SDimitry Andric                 int64_t Offset) const {
254*0b57cec5SDimitry Andric   DebugLoc DL;
255*0b57cec5SDimitry Andric   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
256*0b57cec5SDimitry Andric 
257*0b57cec5SDimitry Andric   unsigned Opc = 0;
258*0b57cec5SDimitry Andric 
259*0b57cec5SDimitry Andric   if (Mips::GPR32RegClass.hasSubClassEq(RC))
260*0b57cec5SDimitry Andric     Opc = Mips::SW;
261*0b57cec5SDimitry Andric   else if (Mips::GPR64RegClass.hasSubClassEq(RC))
262*0b57cec5SDimitry Andric     Opc = Mips::SD;
263*0b57cec5SDimitry Andric   else if (Mips::ACC64RegClass.hasSubClassEq(RC))
264*0b57cec5SDimitry Andric     Opc = Mips::STORE_ACC64;
265*0b57cec5SDimitry Andric   else if (Mips::ACC64DSPRegClass.hasSubClassEq(RC))
266*0b57cec5SDimitry Andric     Opc = Mips::STORE_ACC64DSP;
267*0b57cec5SDimitry Andric   else if (Mips::ACC128RegClass.hasSubClassEq(RC))
268*0b57cec5SDimitry Andric     Opc = Mips::STORE_ACC128;
269*0b57cec5SDimitry Andric   else if (Mips::DSPCCRegClass.hasSubClassEq(RC))
270*0b57cec5SDimitry Andric     Opc = Mips::STORE_CCOND_DSP;
271*0b57cec5SDimitry Andric   else if (Mips::FGR32RegClass.hasSubClassEq(RC))
272*0b57cec5SDimitry Andric     Opc = Mips::SWC1;
273*0b57cec5SDimitry Andric   else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
274*0b57cec5SDimitry Andric     Opc = Mips::SDC1;
275*0b57cec5SDimitry Andric   else if (Mips::FGR64RegClass.hasSubClassEq(RC))
276*0b57cec5SDimitry Andric     Opc = Mips::SDC164;
277*0b57cec5SDimitry Andric   else if (TRI->isTypeLegalForClass(*RC, MVT::v16i8))
278*0b57cec5SDimitry Andric     Opc = Mips::ST_B;
279*0b57cec5SDimitry Andric   else if (TRI->isTypeLegalForClass(*RC, MVT::v8i16) ||
280*0b57cec5SDimitry Andric            TRI->isTypeLegalForClass(*RC, MVT::v8f16))
281*0b57cec5SDimitry Andric     Opc = Mips::ST_H;
282*0b57cec5SDimitry Andric   else if (TRI->isTypeLegalForClass(*RC, MVT::v4i32) ||
283*0b57cec5SDimitry Andric            TRI->isTypeLegalForClass(*RC, MVT::v4f32))
284*0b57cec5SDimitry Andric     Opc = Mips::ST_W;
285*0b57cec5SDimitry Andric   else if (TRI->isTypeLegalForClass(*RC, MVT::v2i64) ||
286*0b57cec5SDimitry Andric            TRI->isTypeLegalForClass(*RC, MVT::v2f64))
287*0b57cec5SDimitry Andric     Opc = Mips::ST_D;
288*0b57cec5SDimitry Andric   else if (Mips::LO32RegClass.hasSubClassEq(RC))
289*0b57cec5SDimitry Andric     Opc = Mips::SW;
290*0b57cec5SDimitry Andric   else if (Mips::LO64RegClass.hasSubClassEq(RC))
291*0b57cec5SDimitry Andric     Opc = Mips::SD;
292*0b57cec5SDimitry Andric   else if (Mips::HI32RegClass.hasSubClassEq(RC))
293*0b57cec5SDimitry Andric     Opc = Mips::SW;
294*0b57cec5SDimitry Andric   else if (Mips::HI64RegClass.hasSubClassEq(RC))
295*0b57cec5SDimitry Andric     Opc = Mips::SD;
296*0b57cec5SDimitry Andric   else if (Mips::DSPRRegClass.hasSubClassEq(RC))
297*0b57cec5SDimitry Andric     Opc = Mips::SWDSP;
298*0b57cec5SDimitry Andric 
299*0b57cec5SDimitry Andric   // Hi, Lo are normally caller save but they are callee save
300*0b57cec5SDimitry Andric   // for interrupt handling.
301*0b57cec5SDimitry Andric   const Function &Func = MBB.getParent()->getFunction();
302*0b57cec5SDimitry Andric   if (Func.hasFnAttribute("interrupt")) {
303*0b57cec5SDimitry Andric     if (Mips::HI32RegClass.hasSubClassEq(RC)) {
304*0b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(Mips::MFHI), Mips::K0);
305*0b57cec5SDimitry Andric       SrcReg = Mips::K0;
306*0b57cec5SDimitry Andric     } else if (Mips::HI64RegClass.hasSubClassEq(RC)) {
307*0b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(Mips::MFHI64), Mips::K0_64);
308*0b57cec5SDimitry Andric       SrcReg = Mips::K0_64;
309*0b57cec5SDimitry Andric     } else if (Mips::LO32RegClass.hasSubClassEq(RC)) {
310*0b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(Mips::MFLO), Mips::K0);
311*0b57cec5SDimitry Andric       SrcReg = Mips::K0;
312*0b57cec5SDimitry Andric     } else if (Mips::LO64RegClass.hasSubClassEq(RC)) {
313*0b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(Mips::MFLO64), Mips::K0_64);
314*0b57cec5SDimitry Andric       SrcReg = Mips::K0_64;
315*0b57cec5SDimitry Andric     }
316*0b57cec5SDimitry Andric   }
317*0b57cec5SDimitry Andric 
318*0b57cec5SDimitry Andric   assert(Opc && "Register class not handled!");
319*0b57cec5SDimitry Andric   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
320*0b57cec5SDimitry Andric     .addFrameIndex(FI).addImm(Offset).addMemOperand(MMO);
321*0b57cec5SDimitry Andric }
322*0b57cec5SDimitry Andric 
323*0b57cec5SDimitry Andric void MipsSEInstrInfo::
324*0b57cec5SDimitry Andric loadRegFromStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
325*0b57cec5SDimitry Andric                  unsigned DestReg, int FI, const TargetRegisterClass *RC,
326*0b57cec5SDimitry Andric                  const TargetRegisterInfo *TRI, int64_t Offset) const {
327*0b57cec5SDimitry Andric   DebugLoc DL;
328*0b57cec5SDimitry Andric   if (I != MBB.end()) DL = I->getDebugLoc();
329*0b57cec5SDimitry Andric   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
330*0b57cec5SDimitry Andric   unsigned Opc = 0;
331*0b57cec5SDimitry Andric 
332*0b57cec5SDimitry Andric   const Function &Func = MBB.getParent()->getFunction();
333*0b57cec5SDimitry Andric   bool ReqIndirectLoad = Func.hasFnAttribute("interrupt") &&
334*0b57cec5SDimitry Andric                          (DestReg == Mips::LO0 || DestReg == Mips::LO0_64 ||
335*0b57cec5SDimitry Andric                           DestReg == Mips::HI0 || DestReg == Mips::HI0_64);
336*0b57cec5SDimitry Andric 
337*0b57cec5SDimitry Andric   if (Mips::GPR32RegClass.hasSubClassEq(RC))
338*0b57cec5SDimitry Andric     Opc = Mips::LW;
339*0b57cec5SDimitry Andric   else if (Mips::GPR64RegClass.hasSubClassEq(RC))
340*0b57cec5SDimitry Andric     Opc = Mips::LD;
341*0b57cec5SDimitry Andric   else if (Mips::ACC64RegClass.hasSubClassEq(RC))
342*0b57cec5SDimitry Andric     Opc = Mips::LOAD_ACC64;
343*0b57cec5SDimitry Andric   else if (Mips::ACC64DSPRegClass.hasSubClassEq(RC))
344*0b57cec5SDimitry Andric     Opc = Mips::LOAD_ACC64DSP;
345*0b57cec5SDimitry Andric   else if (Mips::ACC128RegClass.hasSubClassEq(RC))
346*0b57cec5SDimitry Andric     Opc = Mips::LOAD_ACC128;
347*0b57cec5SDimitry Andric   else if (Mips::DSPCCRegClass.hasSubClassEq(RC))
348*0b57cec5SDimitry Andric     Opc = Mips::LOAD_CCOND_DSP;
349*0b57cec5SDimitry Andric   else if (Mips::FGR32RegClass.hasSubClassEq(RC))
350*0b57cec5SDimitry Andric     Opc = Mips::LWC1;
351*0b57cec5SDimitry Andric   else if (Mips::AFGR64RegClass.hasSubClassEq(RC))
352*0b57cec5SDimitry Andric     Opc = Mips::LDC1;
353*0b57cec5SDimitry Andric   else if (Mips::FGR64RegClass.hasSubClassEq(RC))
354*0b57cec5SDimitry Andric     Opc = Mips::LDC164;
355*0b57cec5SDimitry Andric   else if (TRI->isTypeLegalForClass(*RC, MVT::v16i8))
356*0b57cec5SDimitry Andric     Opc = Mips::LD_B;
357*0b57cec5SDimitry Andric   else if (TRI->isTypeLegalForClass(*RC, MVT::v8i16) ||
358*0b57cec5SDimitry Andric            TRI->isTypeLegalForClass(*RC, MVT::v8f16))
359*0b57cec5SDimitry Andric     Opc = Mips::LD_H;
360*0b57cec5SDimitry Andric   else if (TRI->isTypeLegalForClass(*RC, MVT::v4i32) ||
361*0b57cec5SDimitry Andric            TRI->isTypeLegalForClass(*RC, MVT::v4f32))
362*0b57cec5SDimitry Andric     Opc = Mips::LD_W;
363*0b57cec5SDimitry Andric   else if (TRI->isTypeLegalForClass(*RC, MVT::v2i64) ||
364*0b57cec5SDimitry Andric            TRI->isTypeLegalForClass(*RC, MVT::v2f64))
365*0b57cec5SDimitry Andric     Opc = Mips::LD_D;
366*0b57cec5SDimitry Andric   else if (Mips::HI32RegClass.hasSubClassEq(RC))
367*0b57cec5SDimitry Andric     Opc = Mips::LW;
368*0b57cec5SDimitry Andric   else if (Mips::HI64RegClass.hasSubClassEq(RC))
369*0b57cec5SDimitry Andric     Opc = Mips::LD;
370*0b57cec5SDimitry Andric   else if (Mips::LO32RegClass.hasSubClassEq(RC))
371*0b57cec5SDimitry Andric     Opc = Mips::LW;
372*0b57cec5SDimitry Andric   else if (Mips::LO64RegClass.hasSubClassEq(RC))
373*0b57cec5SDimitry Andric     Opc = Mips::LD;
374*0b57cec5SDimitry Andric   else if (Mips::DSPRRegClass.hasSubClassEq(RC))
375*0b57cec5SDimitry Andric     Opc = Mips::LWDSP;
376*0b57cec5SDimitry Andric 
377*0b57cec5SDimitry Andric   assert(Opc && "Register class not handled!");
378*0b57cec5SDimitry Andric 
379*0b57cec5SDimitry Andric   if (!ReqIndirectLoad)
380*0b57cec5SDimitry Andric     BuildMI(MBB, I, DL, get(Opc), DestReg)
381*0b57cec5SDimitry Andric         .addFrameIndex(FI)
382*0b57cec5SDimitry Andric         .addImm(Offset)
383*0b57cec5SDimitry Andric         .addMemOperand(MMO);
384*0b57cec5SDimitry Andric   else {
385*0b57cec5SDimitry Andric     // Load HI/LO through K0. Notably the DestReg is encoded into the
386*0b57cec5SDimitry Andric     // instruction itself.
387*0b57cec5SDimitry Andric     unsigned Reg = Mips::K0;
388*0b57cec5SDimitry Andric     unsigned LdOp = Mips::MTLO;
389*0b57cec5SDimitry Andric     if (DestReg == Mips::HI0)
390*0b57cec5SDimitry Andric       LdOp = Mips::MTHI;
391*0b57cec5SDimitry Andric 
392*0b57cec5SDimitry Andric     if (Subtarget.getABI().ArePtrs64bit()) {
393*0b57cec5SDimitry Andric       Reg = Mips::K0_64;
394*0b57cec5SDimitry Andric       if (DestReg == Mips::HI0_64)
395*0b57cec5SDimitry Andric         LdOp = Mips::MTHI64;
396*0b57cec5SDimitry Andric       else
397*0b57cec5SDimitry Andric         LdOp = Mips::MTLO64;
398*0b57cec5SDimitry Andric     }
399*0b57cec5SDimitry Andric 
400*0b57cec5SDimitry Andric     BuildMI(MBB, I, DL, get(Opc), Reg)
401*0b57cec5SDimitry Andric         .addFrameIndex(FI)
402*0b57cec5SDimitry Andric         .addImm(Offset)
403*0b57cec5SDimitry Andric         .addMemOperand(MMO);
404*0b57cec5SDimitry Andric     BuildMI(MBB, I, DL, get(LdOp)).addReg(Reg);
405*0b57cec5SDimitry Andric   }
406*0b57cec5SDimitry Andric }
407*0b57cec5SDimitry Andric 
408*0b57cec5SDimitry Andric bool MipsSEInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
409*0b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
410*0b57cec5SDimitry Andric   bool isMicroMips = Subtarget.inMicroMipsMode();
411*0b57cec5SDimitry Andric   unsigned Opc;
412*0b57cec5SDimitry Andric 
413*0b57cec5SDimitry Andric   switch (MI.getDesc().getOpcode()) {
414*0b57cec5SDimitry Andric   default:
415*0b57cec5SDimitry Andric     return false;
416*0b57cec5SDimitry Andric   case Mips::RetRA:
417*0b57cec5SDimitry Andric     expandRetRA(MBB, MI);
418*0b57cec5SDimitry Andric     break;
419*0b57cec5SDimitry Andric   case Mips::ERet:
420*0b57cec5SDimitry Andric     expandERet(MBB, MI);
421*0b57cec5SDimitry Andric     break;
422*0b57cec5SDimitry Andric   case Mips::PseudoMFHI:
423*0b57cec5SDimitry Andric     expandPseudoMFHiLo(MBB, MI, Mips::MFHI);
424*0b57cec5SDimitry Andric     break;
425*0b57cec5SDimitry Andric   case Mips::PseudoMFHI_MM:
426*0b57cec5SDimitry Andric     expandPseudoMFHiLo(MBB, MI, Mips::MFHI16_MM);
427*0b57cec5SDimitry Andric     break;
428*0b57cec5SDimitry Andric   case Mips::PseudoMFLO:
429*0b57cec5SDimitry Andric     expandPseudoMFHiLo(MBB, MI, Mips::MFLO);
430*0b57cec5SDimitry Andric     break;
431*0b57cec5SDimitry Andric   case Mips::PseudoMFLO_MM:
432*0b57cec5SDimitry Andric     expandPseudoMFHiLo(MBB, MI, Mips::MFLO16_MM);
433*0b57cec5SDimitry Andric     break;
434*0b57cec5SDimitry Andric   case Mips::PseudoMFHI64:
435*0b57cec5SDimitry Andric     expandPseudoMFHiLo(MBB, MI, Mips::MFHI64);
436*0b57cec5SDimitry Andric     break;
437*0b57cec5SDimitry Andric   case Mips::PseudoMFLO64:
438*0b57cec5SDimitry Andric     expandPseudoMFHiLo(MBB, MI, Mips::MFLO64);
439*0b57cec5SDimitry Andric     break;
440*0b57cec5SDimitry Andric   case Mips::PseudoMTLOHI:
441*0b57cec5SDimitry Andric     expandPseudoMTLoHi(MBB, MI, Mips::MTLO, Mips::MTHI, false);
442*0b57cec5SDimitry Andric     break;
443*0b57cec5SDimitry Andric   case Mips::PseudoMTLOHI64:
444*0b57cec5SDimitry Andric     expandPseudoMTLoHi(MBB, MI, Mips::MTLO64, Mips::MTHI64, false);
445*0b57cec5SDimitry Andric     break;
446*0b57cec5SDimitry Andric   case Mips::PseudoMTLOHI_DSP:
447*0b57cec5SDimitry Andric     expandPseudoMTLoHi(MBB, MI, Mips::MTLO_DSP, Mips::MTHI_DSP, true);
448*0b57cec5SDimitry Andric     break;
449*0b57cec5SDimitry Andric   case Mips::PseudoMTLOHI_MM:
450*0b57cec5SDimitry Andric     expandPseudoMTLoHi(MBB, MI, Mips::MTLO_MM, Mips::MTHI_MM, false);
451*0b57cec5SDimitry Andric     break;
452*0b57cec5SDimitry Andric   case Mips::PseudoCVT_S_W:
453*0b57cec5SDimitry Andric     expandCvtFPInt(MBB, MI, Mips::CVT_S_W, Mips::MTC1, false);
454*0b57cec5SDimitry Andric     break;
455*0b57cec5SDimitry Andric   case Mips::PseudoCVT_D32_W:
456*0b57cec5SDimitry Andric     Opc = isMicroMips ? Mips::CVT_D32_W_MM : Mips::CVT_D32_W;
457*0b57cec5SDimitry Andric     expandCvtFPInt(MBB, MI, Opc, Mips::MTC1, false);
458*0b57cec5SDimitry Andric     break;
459*0b57cec5SDimitry Andric   case Mips::PseudoCVT_S_L:
460*0b57cec5SDimitry Andric     expandCvtFPInt(MBB, MI, Mips::CVT_S_L, Mips::DMTC1, true);
461*0b57cec5SDimitry Andric     break;
462*0b57cec5SDimitry Andric   case Mips::PseudoCVT_D64_W:
463*0b57cec5SDimitry Andric     Opc = isMicroMips ? Mips::CVT_D64_W_MM : Mips::CVT_D64_W;
464*0b57cec5SDimitry Andric     expandCvtFPInt(MBB, MI, Opc, Mips::MTC1, true);
465*0b57cec5SDimitry Andric     break;
466*0b57cec5SDimitry Andric   case Mips::PseudoCVT_D64_L:
467*0b57cec5SDimitry Andric     expandCvtFPInt(MBB, MI, Mips::CVT_D64_L, Mips::DMTC1, true);
468*0b57cec5SDimitry Andric     break;
469*0b57cec5SDimitry Andric   case Mips::BuildPairF64:
470*0b57cec5SDimitry Andric     expandBuildPairF64(MBB, MI, isMicroMips, false);
471*0b57cec5SDimitry Andric     break;
472*0b57cec5SDimitry Andric   case Mips::BuildPairF64_64:
473*0b57cec5SDimitry Andric     expandBuildPairF64(MBB, MI, isMicroMips, true);
474*0b57cec5SDimitry Andric     break;
475*0b57cec5SDimitry Andric   case Mips::ExtractElementF64:
476*0b57cec5SDimitry Andric     expandExtractElementF64(MBB, MI, isMicroMips, false);
477*0b57cec5SDimitry Andric     break;
478*0b57cec5SDimitry Andric   case Mips::ExtractElementF64_64:
479*0b57cec5SDimitry Andric     expandExtractElementF64(MBB, MI, isMicroMips, true);
480*0b57cec5SDimitry Andric     break;
481*0b57cec5SDimitry Andric   case Mips::MIPSeh_return32:
482*0b57cec5SDimitry Andric   case Mips::MIPSeh_return64:
483*0b57cec5SDimitry Andric     expandEhReturn(MBB, MI);
484*0b57cec5SDimitry Andric     break;
485*0b57cec5SDimitry Andric   }
486*0b57cec5SDimitry Andric 
487*0b57cec5SDimitry Andric   MBB.erase(MI);
488*0b57cec5SDimitry Andric   return true;
489*0b57cec5SDimitry Andric }
490*0b57cec5SDimitry Andric 
491*0b57cec5SDimitry Andric /// getOppositeBranchOpc - Return the inverse of the specified
492*0b57cec5SDimitry Andric /// opcode, e.g. turning BEQ to BNE.
493*0b57cec5SDimitry Andric unsigned MipsSEInstrInfo::getOppositeBranchOpc(unsigned Opc) const {
494*0b57cec5SDimitry Andric   switch (Opc) {
495*0b57cec5SDimitry Andric   default:           llvm_unreachable("Illegal opcode!");
496*0b57cec5SDimitry Andric   case Mips::BEQ:    return Mips::BNE;
497*0b57cec5SDimitry Andric   case Mips::BEQ_MM: return Mips::BNE_MM;
498*0b57cec5SDimitry Andric   case Mips::BNE:    return Mips::BEQ;
499*0b57cec5SDimitry Andric   case Mips::BNE_MM: return Mips::BEQ_MM;
500*0b57cec5SDimitry Andric   case Mips::BGTZ:   return Mips::BLEZ;
501*0b57cec5SDimitry Andric   case Mips::BGEZ:   return Mips::BLTZ;
502*0b57cec5SDimitry Andric   case Mips::BLTZ:   return Mips::BGEZ;
503*0b57cec5SDimitry Andric   case Mips::BLEZ:   return Mips::BGTZ;
504*0b57cec5SDimitry Andric   case Mips::BGTZ_MM:   return Mips::BLEZ_MM;
505*0b57cec5SDimitry Andric   case Mips::BGEZ_MM:   return Mips::BLTZ_MM;
506*0b57cec5SDimitry Andric   case Mips::BLTZ_MM:   return Mips::BGEZ_MM;
507*0b57cec5SDimitry Andric   case Mips::BLEZ_MM:   return Mips::BGTZ_MM;
508*0b57cec5SDimitry Andric   case Mips::BEQ64:  return Mips::BNE64;
509*0b57cec5SDimitry Andric   case Mips::BNE64:  return Mips::BEQ64;
510*0b57cec5SDimitry Andric   case Mips::BGTZ64: return Mips::BLEZ64;
511*0b57cec5SDimitry Andric   case Mips::BGEZ64: return Mips::BLTZ64;
512*0b57cec5SDimitry Andric   case Mips::BLTZ64: return Mips::BGEZ64;
513*0b57cec5SDimitry Andric   case Mips::BLEZ64: return Mips::BGTZ64;
514*0b57cec5SDimitry Andric   case Mips::BC1T:   return Mips::BC1F;
515*0b57cec5SDimitry Andric   case Mips::BC1F:   return Mips::BC1T;
516*0b57cec5SDimitry Andric   case Mips::BC1T_MM:   return Mips::BC1F_MM;
517*0b57cec5SDimitry Andric   case Mips::BC1F_MM:   return Mips::BC1T_MM;
518*0b57cec5SDimitry Andric   case Mips::BEQZ16_MM: return Mips::BNEZ16_MM;
519*0b57cec5SDimitry Andric   case Mips::BNEZ16_MM: return Mips::BEQZ16_MM;
520*0b57cec5SDimitry Andric   case Mips::BEQZC_MM:  return Mips::BNEZC_MM;
521*0b57cec5SDimitry Andric   case Mips::BNEZC_MM:  return Mips::BEQZC_MM;
522*0b57cec5SDimitry Andric   case Mips::BEQZC:  return Mips::BNEZC;
523*0b57cec5SDimitry Andric   case Mips::BNEZC:  return Mips::BEQZC;
524*0b57cec5SDimitry Andric   case Mips::BLEZC:  return Mips::BGTZC;
525*0b57cec5SDimitry Andric   case Mips::BGEZC:  return Mips::BLTZC;
526*0b57cec5SDimitry Andric   case Mips::BGEC:   return Mips::BLTC;
527*0b57cec5SDimitry Andric   case Mips::BGTZC:  return Mips::BLEZC;
528*0b57cec5SDimitry Andric   case Mips::BLTZC:  return Mips::BGEZC;
529*0b57cec5SDimitry Andric   case Mips::BLTC:   return Mips::BGEC;
530*0b57cec5SDimitry Andric   case Mips::BGEUC:  return Mips::BLTUC;
531*0b57cec5SDimitry Andric   case Mips::BLTUC:  return Mips::BGEUC;
532*0b57cec5SDimitry Andric   case Mips::BEQC:   return Mips::BNEC;
533*0b57cec5SDimitry Andric   case Mips::BNEC:   return Mips::BEQC;
534*0b57cec5SDimitry Andric   case Mips::BC1EQZ: return Mips::BC1NEZ;
535*0b57cec5SDimitry Andric   case Mips::BC1NEZ: return Mips::BC1EQZ;
536*0b57cec5SDimitry Andric   case Mips::BEQZC_MMR6:  return Mips::BNEZC_MMR6;
537*0b57cec5SDimitry Andric   case Mips::BNEZC_MMR6:  return Mips::BEQZC_MMR6;
538*0b57cec5SDimitry Andric   case Mips::BLEZC_MMR6:  return Mips::BGTZC_MMR6;
539*0b57cec5SDimitry Andric   case Mips::BGEZC_MMR6:  return Mips::BLTZC_MMR6;
540*0b57cec5SDimitry Andric   case Mips::BGEC_MMR6:   return Mips::BLTC_MMR6;
541*0b57cec5SDimitry Andric   case Mips::BGTZC_MMR6:  return Mips::BLEZC_MMR6;
542*0b57cec5SDimitry Andric   case Mips::BLTZC_MMR6:  return Mips::BGEZC_MMR6;
543*0b57cec5SDimitry Andric   case Mips::BLTC_MMR6:   return Mips::BGEC_MMR6;
544*0b57cec5SDimitry Andric   case Mips::BGEUC_MMR6:  return Mips::BLTUC_MMR6;
545*0b57cec5SDimitry Andric   case Mips::BLTUC_MMR6:  return Mips::BGEUC_MMR6;
546*0b57cec5SDimitry Andric   case Mips::BEQC_MMR6:   return Mips::BNEC_MMR6;
547*0b57cec5SDimitry Andric   case Mips::BNEC_MMR6:   return Mips::BEQC_MMR6;
548*0b57cec5SDimitry Andric   case Mips::BC1EQZC_MMR6: return Mips::BC1NEZC_MMR6;
549*0b57cec5SDimitry Andric   case Mips::BC1NEZC_MMR6: return Mips::BC1EQZC_MMR6;
550*0b57cec5SDimitry Andric   case Mips::BEQZC64:  return Mips::BNEZC64;
551*0b57cec5SDimitry Andric   case Mips::BNEZC64:  return Mips::BEQZC64;
552*0b57cec5SDimitry Andric   case Mips::BEQC64:   return Mips::BNEC64;
553*0b57cec5SDimitry Andric   case Mips::BNEC64:   return Mips::BEQC64;
554*0b57cec5SDimitry Andric   case Mips::BGEC64:   return Mips::BLTC64;
555*0b57cec5SDimitry Andric   case Mips::BGEUC64:  return Mips::BLTUC64;
556*0b57cec5SDimitry Andric   case Mips::BLTC64:   return Mips::BGEC64;
557*0b57cec5SDimitry Andric   case Mips::BLTUC64:  return Mips::BGEUC64;
558*0b57cec5SDimitry Andric   case Mips::BGTZC64:  return Mips::BLEZC64;
559*0b57cec5SDimitry Andric   case Mips::BGEZC64:  return Mips::BLTZC64;
560*0b57cec5SDimitry Andric   case Mips::BLTZC64:  return Mips::BGEZC64;
561*0b57cec5SDimitry Andric   case Mips::BLEZC64:  return Mips::BGTZC64;
562*0b57cec5SDimitry Andric   case Mips::BBIT0:  return Mips::BBIT1;
563*0b57cec5SDimitry Andric   case Mips::BBIT1:  return Mips::BBIT0;
564*0b57cec5SDimitry Andric   case Mips::BBIT032:  return Mips::BBIT132;
565*0b57cec5SDimitry Andric   case Mips::BBIT132:  return Mips::BBIT032;
566*0b57cec5SDimitry Andric   case Mips::BZ_B:   return Mips::BNZ_B;
567*0b57cec5SDimitry Andric   case Mips::BZ_H:   return Mips::BNZ_H;
568*0b57cec5SDimitry Andric   case Mips::BZ_W:   return Mips::BNZ_W;
569*0b57cec5SDimitry Andric   case Mips::BZ_D:   return Mips::BNZ_D;
570*0b57cec5SDimitry Andric   case Mips::BZ_V:   return Mips::BNZ_V;
571*0b57cec5SDimitry Andric   case Mips::BNZ_B:  return Mips::BZ_B;
572*0b57cec5SDimitry Andric   case Mips::BNZ_H:  return Mips::BZ_H;
573*0b57cec5SDimitry Andric   case Mips::BNZ_W:  return Mips::BZ_W;
574*0b57cec5SDimitry Andric   case Mips::BNZ_D:  return Mips::BZ_D;
575*0b57cec5SDimitry Andric   case Mips::BNZ_V:  return Mips::BZ_V;
576*0b57cec5SDimitry Andric   }
577*0b57cec5SDimitry Andric }
578*0b57cec5SDimitry Andric 
579*0b57cec5SDimitry Andric /// Adjust SP by Amount bytes.
580*0b57cec5SDimitry Andric void MipsSEInstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
581*0b57cec5SDimitry Andric                                      MachineBasicBlock &MBB,
582*0b57cec5SDimitry Andric                                      MachineBasicBlock::iterator I) const {
583*0b57cec5SDimitry Andric   MipsABIInfo ABI = Subtarget.getABI();
584*0b57cec5SDimitry Andric   DebugLoc DL;
585*0b57cec5SDimitry Andric   unsigned ADDiu = ABI.GetPtrAddiuOp();
586*0b57cec5SDimitry Andric 
587*0b57cec5SDimitry Andric   if (Amount == 0)
588*0b57cec5SDimitry Andric     return;
589*0b57cec5SDimitry Andric 
590*0b57cec5SDimitry Andric   if (isInt<16>(Amount)) {
591*0b57cec5SDimitry Andric     // addi sp, sp, amount
592*0b57cec5SDimitry Andric     BuildMI(MBB, I, DL, get(ADDiu), SP).addReg(SP).addImm(Amount);
593*0b57cec5SDimitry Andric   } else {
594*0b57cec5SDimitry Andric     // For numbers which are not 16bit integers we synthesize Amount inline
595*0b57cec5SDimitry Andric     // then add or subtract it from sp.
596*0b57cec5SDimitry Andric     unsigned Opc = ABI.GetPtrAdduOp();
597*0b57cec5SDimitry Andric     if (Amount < 0) {
598*0b57cec5SDimitry Andric       Opc = ABI.GetPtrSubuOp();
599*0b57cec5SDimitry Andric       Amount = -Amount;
600*0b57cec5SDimitry Andric     }
601*0b57cec5SDimitry Andric     unsigned Reg = loadImmediate(Amount, MBB, I, DL, nullptr);
602*0b57cec5SDimitry Andric     BuildMI(MBB, I, DL, get(Opc), SP).addReg(SP).addReg(Reg, RegState::Kill);
603*0b57cec5SDimitry Andric   }
604*0b57cec5SDimitry Andric }
605*0b57cec5SDimitry Andric 
606*0b57cec5SDimitry Andric /// This function generates the sequence of instructions needed to get the
607*0b57cec5SDimitry Andric /// result of adding register REG and immediate IMM.
608*0b57cec5SDimitry Andric unsigned MipsSEInstrInfo::loadImmediate(int64_t Imm, MachineBasicBlock &MBB,
609*0b57cec5SDimitry Andric                                         MachineBasicBlock::iterator II,
610*0b57cec5SDimitry Andric                                         const DebugLoc &DL,
611*0b57cec5SDimitry Andric                                         unsigned *NewImm) const {
612*0b57cec5SDimitry Andric   MipsAnalyzeImmediate AnalyzeImm;
613*0b57cec5SDimitry Andric   const MipsSubtarget &STI = Subtarget;
614*0b57cec5SDimitry Andric   MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
615*0b57cec5SDimitry Andric   unsigned Size = STI.isABI_N64() ? 64 : 32;
616*0b57cec5SDimitry Andric   unsigned LUi = STI.isABI_N64() ? Mips::LUi64 : Mips::LUi;
617*0b57cec5SDimitry Andric   unsigned ZEROReg = STI.isABI_N64() ? Mips::ZERO_64 : Mips::ZERO;
618*0b57cec5SDimitry Andric   const TargetRegisterClass *RC = STI.isABI_N64() ?
619*0b57cec5SDimitry Andric     &Mips::GPR64RegClass : &Mips::GPR32RegClass;
620*0b57cec5SDimitry Andric   bool LastInstrIsADDiu = NewImm;
621*0b57cec5SDimitry Andric 
622*0b57cec5SDimitry Andric   const MipsAnalyzeImmediate::InstSeq &Seq =
623*0b57cec5SDimitry Andric     AnalyzeImm.Analyze(Imm, Size, LastInstrIsADDiu);
624*0b57cec5SDimitry Andric   MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin();
625*0b57cec5SDimitry Andric 
626*0b57cec5SDimitry Andric   assert(Seq.size() && (!LastInstrIsADDiu || (Seq.size() > 1)));
627*0b57cec5SDimitry Andric 
628*0b57cec5SDimitry Andric   // The first instruction can be a LUi, which is different from other
629*0b57cec5SDimitry Andric   // instructions (ADDiu, ORI and SLL) in that it does not have a register
630*0b57cec5SDimitry Andric   // operand.
631*0b57cec5SDimitry Andric   unsigned Reg = RegInfo.createVirtualRegister(RC);
632*0b57cec5SDimitry Andric 
633*0b57cec5SDimitry Andric   if (Inst->Opc == LUi)
634*0b57cec5SDimitry Andric     BuildMI(MBB, II, DL, get(LUi), Reg).addImm(SignExtend64<16>(Inst->ImmOpnd));
635*0b57cec5SDimitry Andric   else
636*0b57cec5SDimitry Andric     BuildMI(MBB, II, DL, get(Inst->Opc), Reg).addReg(ZEROReg)
637*0b57cec5SDimitry Andric       .addImm(SignExtend64<16>(Inst->ImmOpnd));
638*0b57cec5SDimitry Andric 
639*0b57cec5SDimitry Andric   // Build the remaining instructions in Seq.
640*0b57cec5SDimitry Andric   for (++Inst; Inst != Seq.end() - LastInstrIsADDiu; ++Inst)
641*0b57cec5SDimitry Andric     BuildMI(MBB, II, DL, get(Inst->Opc), Reg).addReg(Reg, RegState::Kill)
642*0b57cec5SDimitry Andric       .addImm(SignExtend64<16>(Inst->ImmOpnd));
643*0b57cec5SDimitry Andric 
644*0b57cec5SDimitry Andric   if (LastInstrIsADDiu)
645*0b57cec5SDimitry Andric     *NewImm = Inst->ImmOpnd;
646*0b57cec5SDimitry Andric 
647*0b57cec5SDimitry Andric   return Reg;
648*0b57cec5SDimitry Andric }
649*0b57cec5SDimitry Andric 
650*0b57cec5SDimitry Andric unsigned MipsSEInstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
651*0b57cec5SDimitry Andric   return (Opc == Mips::BEQ    || Opc == Mips::BEQ_MM || Opc == Mips::BNE    ||
652*0b57cec5SDimitry Andric           Opc == Mips::BNE_MM || Opc == Mips::BGTZ   || Opc == Mips::BGEZ   ||
653*0b57cec5SDimitry Andric           Opc == Mips::BLTZ   || Opc == Mips::BLEZ   || Opc == Mips::BEQ64  ||
654*0b57cec5SDimitry Andric           Opc == Mips::BNE64  || Opc == Mips::BGTZ64 || Opc == Mips::BGEZ64 ||
655*0b57cec5SDimitry Andric           Opc == Mips::BLTZ64 || Opc == Mips::BLEZ64 || Opc == Mips::BC1T   ||
656*0b57cec5SDimitry Andric           Opc == Mips::BC1F   || Opc == Mips::B      || Opc == Mips::J      ||
657*0b57cec5SDimitry Andric           Opc == Mips::J_MM   || Opc == Mips::B_MM   || Opc == Mips::BEQZC_MM ||
658*0b57cec5SDimitry Andric           Opc == Mips::BNEZC_MM || Opc == Mips::BEQC || Opc == Mips::BNEC   ||
659*0b57cec5SDimitry Andric           Opc == Mips::BLTC   || Opc == Mips::BGEC   || Opc == Mips::BLTUC  ||
660*0b57cec5SDimitry Andric           Opc == Mips::BGEUC  || Opc == Mips::BGTZC  || Opc == Mips::BLEZC  ||
661*0b57cec5SDimitry Andric           Opc == Mips::BGEZC  || Opc == Mips::BLTZC  || Opc == Mips::BEQZC  ||
662*0b57cec5SDimitry Andric           Opc == Mips::BNEZC  || Opc == Mips::BEQZC64 || Opc == Mips::BNEZC64 ||
663*0b57cec5SDimitry Andric           Opc == Mips::BEQC64 || Opc == Mips::BNEC64 || Opc == Mips::BGEC64 ||
664*0b57cec5SDimitry Andric           Opc == Mips::BGEUC64 || Opc == Mips::BLTC64 || Opc == Mips::BLTUC64 ||
665*0b57cec5SDimitry Andric           Opc == Mips::BGTZC64 || Opc == Mips::BGEZC64 ||
666*0b57cec5SDimitry Andric           Opc == Mips::BLTZC64 || Opc == Mips::BLEZC64 || Opc == Mips::BC ||
667*0b57cec5SDimitry Andric           Opc == Mips::BBIT0 || Opc == Mips::BBIT1 || Opc == Mips::BBIT032 ||
668*0b57cec5SDimitry Andric           Opc == Mips::BBIT132 ||  Opc == Mips::BC_MMR6 ||
669*0b57cec5SDimitry Andric           Opc == Mips::BEQC_MMR6 || Opc == Mips::BNEC_MMR6 ||
670*0b57cec5SDimitry Andric           Opc == Mips::BLTC_MMR6 || Opc == Mips::BGEC_MMR6 ||
671*0b57cec5SDimitry Andric           Opc == Mips::BLTUC_MMR6 || Opc == Mips::BGEUC_MMR6 ||
672*0b57cec5SDimitry Andric           Opc == Mips::BGTZC_MMR6 || Opc == Mips::BLEZC_MMR6 ||
673*0b57cec5SDimitry Andric           Opc == Mips::BGEZC_MMR6 || Opc == Mips::BLTZC_MMR6 ||
674*0b57cec5SDimitry Andric           Opc == Mips::BEQZC_MMR6 || Opc == Mips::BNEZC_MMR6) ? Opc : 0;
675*0b57cec5SDimitry Andric }
676*0b57cec5SDimitry Andric 
677*0b57cec5SDimitry Andric void MipsSEInstrInfo::expandRetRA(MachineBasicBlock &MBB,
678*0b57cec5SDimitry Andric                                   MachineBasicBlock::iterator I) const {
679*0b57cec5SDimitry Andric 
680*0b57cec5SDimitry Andric   MachineInstrBuilder MIB;
681*0b57cec5SDimitry Andric   if (Subtarget.isGP64bit())
682*0b57cec5SDimitry Andric     MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Mips::PseudoReturn64))
683*0b57cec5SDimitry Andric               .addReg(Mips::RA_64, RegState::Undef);
684*0b57cec5SDimitry Andric   else
685*0b57cec5SDimitry Andric     MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Mips::PseudoReturn))
686*0b57cec5SDimitry Andric               .addReg(Mips::RA, RegState::Undef);
687*0b57cec5SDimitry Andric 
688*0b57cec5SDimitry Andric   // Retain any imp-use flags.
689*0b57cec5SDimitry Andric   for (auto & MO : I->operands()) {
690*0b57cec5SDimitry Andric     if (MO.isImplicit())
691*0b57cec5SDimitry Andric       MIB.add(MO);
692*0b57cec5SDimitry Andric   }
693*0b57cec5SDimitry Andric }
694*0b57cec5SDimitry Andric 
695*0b57cec5SDimitry Andric void MipsSEInstrInfo::expandERet(MachineBasicBlock &MBB,
696*0b57cec5SDimitry Andric                                  MachineBasicBlock::iterator I) const {
697*0b57cec5SDimitry Andric   BuildMI(MBB, I, I->getDebugLoc(), get(Mips::ERET));
698*0b57cec5SDimitry Andric }
699*0b57cec5SDimitry Andric 
700*0b57cec5SDimitry Andric std::pair<bool, bool>
701*0b57cec5SDimitry Andric MipsSEInstrInfo::compareOpndSize(unsigned Opc,
702*0b57cec5SDimitry Andric                                  const MachineFunction &MF) const {
703*0b57cec5SDimitry Andric   const MCInstrDesc &Desc = get(Opc);
704*0b57cec5SDimitry Andric   assert(Desc.NumOperands == 2 && "Unary instruction expected.");
705*0b57cec5SDimitry Andric   const MipsRegisterInfo *RI = &getRegisterInfo();
706*0b57cec5SDimitry Andric   unsigned DstRegSize = RI->getRegSizeInBits(*getRegClass(Desc, 0, RI, MF));
707*0b57cec5SDimitry Andric   unsigned SrcRegSize = RI->getRegSizeInBits(*getRegClass(Desc, 1, RI, MF));
708*0b57cec5SDimitry Andric 
709*0b57cec5SDimitry Andric   return std::make_pair(DstRegSize > SrcRegSize, DstRegSize < SrcRegSize);
710*0b57cec5SDimitry Andric }
711*0b57cec5SDimitry Andric 
712*0b57cec5SDimitry Andric void MipsSEInstrInfo::expandPseudoMFHiLo(MachineBasicBlock &MBB,
713*0b57cec5SDimitry Andric                                          MachineBasicBlock::iterator I,
714*0b57cec5SDimitry Andric                                          unsigned NewOpc) const {
715*0b57cec5SDimitry Andric   BuildMI(MBB, I, I->getDebugLoc(), get(NewOpc), I->getOperand(0).getReg());
716*0b57cec5SDimitry Andric }
717*0b57cec5SDimitry Andric 
718*0b57cec5SDimitry Andric void MipsSEInstrInfo::expandPseudoMTLoHi(MachineBasicBlock &MBB,
719*0b57cec5SDimitry Andric                                          MachineBasicBlock::iterator I,
720*0b57cec5SDimitry Andric                                          unsigned LoOpc,
721*0b57cec5SDimitry Andric                                          unsigned HiOpc,
722*0b57cec5SDimitry Andric                                          bool HasExplicitDef) const {
723*0b57cec5SDimitry Andric   // Expand
724*0b57cec5SDimitry Andric   //  lo_hi pseudomtlohi $gpr0, $gpr1
725*0b57cec5SDimitry Andric   // to these two instructions:
726*0b57cec5SDimitry Andric   //  mtlo $gpr0
727*0b57cec5SDimitry Andric   //  mthi $gpr1
728*0b57cec5SDimitry Andric 
729*0b57cec5SDimitry Andric   DebugLoc DL = I->getDebugLoc();
730*0b57cec5SDimitry Andric   const MachineOperand &SrcLo = I->getOperand(1), &SrcHi = I->getOperand(2);
731*0b57cec5SDimitry Andric   MachineInstrBuilder LoInst = BuildMI(MBB, I, DL, get(LoOpc));
732*0b57cec5SDimitry Andric   MachineInstrBuilder HiInst = BuildMI(MBB, I, DL, get(HiOpc));
733*0b57cec5SDimitry Andric 
734*0b57cec5SDimitry Andric   // Add lo/hi registers if the mtlo/hi instructions created have explicit
735*0b57cec5SDimitry Andric   // def registers.
736*0b57cec5SDimitry Andric   if (HasExplicitDef) {
737*0b57cec5SDimitry Andric     unsigned DstReg = I->getOperand(0).getReg();
738*0b57cec5SDimitry Andric     unsigned DstLo = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
739*0b57cec5SDimitry Andric     unsigned DstHi = getRegisterInfo().getSubReg(DstReg, Mips::sub_hi);
740*0b57cec5SDimitry Andric     LoInst.addReg(DstLo, RegState::Define);
741*0b57cec5SDimitry Andric     HiInst.addReg(DstHi, RegState::Define);
742*0b57cec5SDimitry Andric   }
743*0b57cec5SDimitry Andric 
744*0b57cec5SDimitry Andric   LoInst.addReg(SrcLo.getReg(), getKillRegState(SrcLo.isKill()));
745*0b57cec5SDimitry Andric   HiInst.addReg(SrcHi.getReg(), getKillRegState(SrcHi.isKill()));
746*0b57cec5SDimitry Andric }
747*0b57cec5SDimitry Andric 
748*0b57cec5SDimitry Andric void MipsSEInstrInfo::expandCvtFPInt(MachineBasicBlock &MBB,
749*0b57cec5SDimitry Andric                                      MachineBasicBlock::iterator I,
750*0b57cec5SDimitry Andric                                      unsigned CvtOpc, unsigned MovOpc,
751*0b57cec5SDimitry Andric                                      bool IsI64) const {
752*0b57cec5SDimitry Andric   const MCInstrDesc &CvtDesc = get(CvtOpc), &MovDesc = get(MovOpc);
753*0b57cec5SDimitry Andric   const MachineOperand &Dst = I->getOperand(0), &Src = I->getOperand(1);
754*0b57cec5SDimitry Andric   unsigned DstReg = Dst.getReg(), SrcReg = Src.getReg(), TmpReg = DstReg;
755*0b57cec5SDimitry Andric   unsigned KillSrc =  getKillRegState(Src.isKill());
756*0b57cec5SDimitry Andric   DebugLoc DL = I->getDebugLoc();
757*0b57cec5SDimitry Andric   bool DstIsLarger, SrcIsLarger;
758*0b57cec5SDimitry Andric 
759*0b57cec5SDimitry Andric   std::tie(DstIsLarger, SrcIsLarger) =
760*0b57cec5SDimitry Andric       compareOpndSize(CvtOpc, *MBB.getParent());
761*0b57cec5SDimitry Andric 
762*0b57cec5SDimitry Andric   if (DstIsLarger)
763*0b57cec5SDimitry Andric     TmpReg = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
764*0b57cec5SDimitry Andric 
765*0b57cec5SDimitry Andric   if (SrcIsLarger)
766*0b57cec5SDimitry Andric     DstReg = getRegisterInfo().getSubReg(DstReg, Mips::sub_lo);
767*0b57cec5SDimitry Andric 
768*0b57cec5SDimitry Andric   BuildMI(MBB, I, DL, MovDesc, TmpReg).addReg(SrcReg, KillSrc);
769*0b57cec5SDimitry Andric   BuildMI(MBB, I, DL, CvtDesc, DstReg).addReg(TmpReg, RegState::Kill);
770*0b57cec5SDimitry Andric }
771*0b57cec5SDimitry Andric 
772*0b57cec5SDimitry Andric void MipsSEInstrInfo::expandExtractElementF64(MachineBasicBlock &MBB,
773*0b57cec5SDimitry Andric                                               MachineBasicBlock::iterator I,
774*0b57cec5SDimitry Andric                                               bool isMicroMips,
775*0b57cec5SDimitry Andric                                               bool FP64) const {
776*0b57cec5SDimitry Andric   unsigned DstReg = I->getOperand(0).getReg();
777*0b57cec5SDimitry Andric   unsigned SrcReg = I->getOperand(1).getReg();
778*0b57cec5SDimitry Andric   unsigned N = I->getOperand(2).getImm();
779*0b57cec5SDimitry Andric   DebugLoc dl = I->getDebugLoc();
780*0b57cec5SDimitry Andric 
781*0b57cec5SDimitry Andric   assert(N < 2 && "Invalid immediate");
782*0b57cec5SDimitry Andric   unsigned SubIdx = N ? Mips::sub_hi : Mips::sub_lo;
783*0b57cec5SDimitry Andric   unsigned SubReg = getRegisterInfo().getSubReg(SrcReg, SubIdx);
784*0b57cec5SDimitry Andric 
785*0b57cec5SDimitry Andric   // FPXX on MIPS-II or MIPS32r1 should have been handled with a spill/reload
786*0b57cec5SDimitry Andric   // in MipsSEFrameLowering.cpp.
787*0b57cec5SDimitry Andric   assert(!(Subtarget.isABI_FPXX() && !Subtarget.hasMips32r2()));
788*0b57cec5SDimitry Andric 
789*0b57cec5SDimitry Andric   // FP64A (FP64 with nooddspreg) should have been handled with a spill/reload
790*0b57cec5SDimitry Andric   // in MipsSEFrameLowering.cpp.
791*0b57cec5SDimitry Andric   assert(!(Subtarget.isFP64bit() && !Subtarget.useOddSPReg()));
792*0b57cec5SDimitry Andric 
793*0b57cec5SDimitry Andric   if (SubIdx == Mips::sub_hi && Subtarget.hasMTHC1()) {
794*0b57cec5SDimitry Andric     // FIXME: Strictly speaking MFHC1 only reads the top 32-bits however, we
795*0b57cec5SDimitry Andric     //        claim to read the whole 64-bits as part of a white lie used to
796*0b57cec5SDimitry Andric     //        temporarily work around a widespread bug in the -mfp64 support.
797*0b57cec5SDimitry Andric     //        The problem is that none of the 32-bit fpu ops mention the fact
798*0b57cec5SDimitry Andric     //        that they clobber the upper 32-bits of the 64-bit FPR. Fixing that
799*0b57cec5SDimitry Andric     //        requires a major overhaul of the FPU implementation which can't
800*0b57cec5SDimitry Andric     //        be done right now due to time constraints.
801*0b57cec5SDimitry Andric     //        MFHC1 is one of two instructions that are affected since they are
802*0b57cec5SDimitry Andric     //        the only instructions that don't read the lower 32-bits.
803*0b57cec5SDimitry Andric     //        We therefore pretend that it reads the bottom 32-bits to
804*0b57cec5SDimitry Andric     //        artificially create a dependency and prevent the scheduler
805*0b57cec5SDimitry Andric     //        changing the behaviour of the code.
806*0b57cec5SDimitry Andric     BuildMI(MBB, I, dl,
807*0b57cec5SDimitry Andric             get(isMicroMips ? (FP64 ? Mips::MFHC1_D64_MM : Mips::MFHC1_D32_MM)
808*0b57cec5SDimitry Andric                             : (FP64 ? Mips::MFHC1_D64 : Mips::MFHC1_D32)),
809*0b57cec5SDimitry Andric             DstReg)
810*0b57cec5SDimitry Andric         .addReg(SrcReg);
811*0b57cec5SDimitry Andric   } else
812*0b57cec5SDimitry Andric     BuildMI(MBB, I, dl, get(Mips::MFC1), DstReg).addReg(SubReg);
813*0b57cec5SDimitry Andric }
814*0b57cec5SDimitry Andric 
815*0b57cec5SDimitry Andric void MipsSEInstrInfo::expandBuildPairF64(MachineBasicBlock &MBB,
816*0b57cec5SDimitry Andric                                          MachineBasicBlock::iterator I,
817*0b57cec5SDimitry Andric                                          bool isMicroMips, bool FP64) const {
818*0b57cec5SDimitry Andric   unsigned DstReg = I->getOperand(0).getReg();
819*0b57cec5SDimitry Andric   unsigned LoReg = I->getOperand(1).getReg(), HiReg = I->getOperand(2).getReg();
820*0b57cec5SDimitry Andric   const MCInstrDesc& Mtc1Tdd = get(Mips::MTC1);
821*0b57cec5SDimitry Andric   DebugLoc dl = I->getDebugLoc();
822*0b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = getRegisterInfo();
823*0b57cec5SDimitry Andric 
824*0b57cec5SDimitry Andric   // When mthc1 is available, use:
825*0b57cec5SDimitry Andric   //   mtc1 Lo, $fp
826*0b57cec5SDimitry Andric   //   mthc1 Hi, $fp
827*0b57cec5SDimitry Andric   //
828*0b57cec5SDimitry Andric   // Otherwise, for O32 FPXX ABI:
829*0b57cec5SDimitry Andric   //   spill + reload via ldc1
830*0b57cec5SDimitry Andric   // This case is handled by the frame lowering code.
831*0b57cec5SDimitry Andric   //
832*0b57cec5SDimitry Andric   // Otherwise, for FP32:
833*0b57cec5SDimitry Andric   //   mtc1 Lo, $fp
834*0b57cec5SDimitry Andric   //   mtc1 Hi, $fp + 1
835*0b57cec5SDimitry Andric   //
836*0b57cec5SDimitry Andric   // The case where dmtc1 is available doesn't need to be handled here
837*0b57cec5SDimitry Andric   // because it never creates a BuildPairF64 node.
838*0b57cec5SDimitry Andric 
839*0b57cec5SDimitry Andric   // FPXX on MIPS-II or MIPS32r1 should have been handled with a spill/reload
840*0b57cec5SDimitry Andric   // in MipsSEFrameLowering.cpp.
841*0b57cec5SDimitry Andric   assert(!(Subtarget.isABI_FPXX() && !Subtarget.hasMips32r2()));
842*0b57cec5SDimitry Andric 
843*0b57cec5SDimitry Andric   // FP64A (FP64 with nooddspreg) should have been handled with a spill/reload
844*0b57cec5SDimitry Andric   // in MipsSEFrameLowering.cpp.
845*0b57cec5SDimitry Andric   assert(!(Subtarget.isFP64bit() && !Subtarget.useOddSPReg()));
846*0b57cec5SDimitry Andric 
847*0b57cec5SDimitry Andric   BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_lo))
848*0b57cec5SDimitry Andric     .addReg(LoReg);
849*0b57cec5SDimitry Andric 
850*0b57cec5SDimitry Andric   if (Subtarget.hasMTHC1()) {
851*0b57cec5SDimitry Andric     // FIXME: The .addReg(DstReg) is a white lie used to temporarily work
852*0b57cec5SDimitry Andric     //        around a widespread bug in the -mfp64 support.
853*0b57cec5SDimitry Andric     //        The problem is that none of the 32-bit fpu ops mention the fact
854*0b57cec5SDimitry Andric     //        that they clobber the upper 32-bits of the 64-bit FPR. Fixing that
855*0b57cec5SDimitry Andric     //        requires a major overhaul of the FPU implementation which can't
856*0b57cec5SDimitry Andric     //        be done right now due to time constraints.
857*0b57cec5SDimitry Andric     //        MTHC1 is one of two instructions that are affected since they are
858*0b57cec5SDimitry Andric     //        the only instructions that don't read the lower 32-bits.
859*0b57cec5SDimitry Andric     //        We therefore pretend that it reads the bottom 32-bits to
860*0b57cec5SDimitry Andric     //        artificially create a dependency and prevent the scheduler
861*0b57cec5SDimitry Andric     //        changing the behaviour of the code.
862*0b57cec5SDimitry Andric     BuildMI(MBB, I, dl,
863*0b57cec5SDimitry Andric             get(isMicroMips ? (FP64 ? Mips::MTHC1_D64_MM : Mips::MTHC1_D32_MM)
864*0b57cec5SDimitry Andric                             : (FP64 ? Mips::MTHC1_D64 : Mips::MTHC1_D32)),
865*0b57cec5SDimitry Andric             DstReg)
866*0b57cec5SDimitry Andric         .addReg(DstReg)
867*0b57cec5SDimitry Andric         .addReg(HiReg);
868*0b57cec5SDimitry Andric   } else if (Subtarget.isABI_FPXX())
869*0b57cec5SDimitry Andric     llvm_unreachable("BuildPairF64 not expanded in frame lowering code!");
870*0b57cec5SDimitry Andric   else
871*0b57cec5SDimitry Andric     BuildMI(MBB, I, dl, Mtc1Tdd, TRI.getSubReg(DstReg, Mips::sub_hi))
872*0b57cec5SDimitry Andric       .addReg(HiReg);
873*0b57cec5SDimitry Andric }
874*0b57cec5SDimitry Andric 
875*0b57cec5SDimitry Andric void MipsSEInstrInfo::expandEhReturn(MachineBasicBlock &MBB,
876*0b57cec5SDimitry Andric                                      MachineBasicBlock::iterator I) const {
877*0b57cec5SDimitry Andric   // This pseudo instruction is generated as part of the lowering of
878*0b57cec5SDimitry Andric   // ISD::EH_RETURN. We convert it to a stack increment by OffsetReg, and
879*0b57cec5SDimitry Andric   // indirect jump to TargetReg
880*0b57cec5SDimitry Andric   MipsABIInfo ABI = Subtarget.getABI();
881*0b57cec5SDimitry Andric   unsigned ADDU = ABI.GetPtrAdduOp();
882*0b57cec5SDimitry Andric   unsigned SP = Subtarget.isGP64bit() ? Mips::SP_64 : Mips::SP;
883*0b57cec5SDimitry Andric   unsigned RA = Subtarget.isGP64bit() ? Mips::RA_64 : Mips::RA;
884*0b57cec5SDimitry Andric   unsigned T9 = Subtarget.isGP64bit() ? Mips::T9_64 : Mips::T9;
885*0b57cec5SDimitry Andric   unsigned ZERO = Subtarget.isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
886*0b57cec5SDimitry Andric   unsigned OffsetReg = I->getOperand(0).getReg();
887*0b57cec5SDimitry Andric   unsigned TargetReg = I->getOperand(1).getReg();
888*0b57cec5SDimitry Andric 
889*0b57cec5SDimitry Andric   // addu $ra, $v0, $zero
890*0b57cec5SDimitry Andric   // addu $sp, $sp, $v1
891*0b57cec5SDimitry Andric   // jr   $ra (via RetRA)
892*0b57cec5SDimitry Andric   const TargetMachine &TM = MBB.getParent()->getTarget();
893*0b57cec5SDimitry Andric   if (TM.isPositionIndependent())
894*0b57cec5SDimitry Andric     BuildMI(MBB, I, I->getDebugLoc(), get(ADDU), T9)
895*0b57cec5SDimitry Andric         .addReg(TargetReg)
896*0b57cec5SDimitry Andric         .addReg(ZERO);
897*0b57cec5SDimitry Andric   BuildMI(MBB, I, I->getDebugLoc(), get(ADDU), RA)
898*0b57cec5SDimitry Andric       .addReg(TargetReg)
899*0b57cec5SDimitry Andric       .addReg(ZERO);
900*0b57cec5SDimitry Andric   BuildMI(MBB, I, I->getDebugLoc(), get(ADDU), SP).addReg(SP).addReg(OffsetReg);
901*0b57cec5SDimitry Andric   expandRetRA(MBB, I);
902*0b57cec5SDimitry Andric }
903*0b57cec5SDimitry Andric 
904*0b57cec5SDimitry Andric const MipsInstrInfo *llvm::createMipsSEInstrInfo(const MipsSubtarget &STI) {
905*0b57cec5SDimitry Andric   return new MipsSEInstrInfo(STI);
906*0b57cec5SDimitry Andric }
907