xref: /minix3/external/bsd/llvm/dist/llvm/lib/Target/Mips/Mips16InstrInfo.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc 
2f4a2713aSLionel Sambuc //===-- Mips16InstrInfo.cpp - Mips16 Instruction Information --------------===//
3f4a2713aSLionel Sambuc //
4f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
5f4a2713aSLionel Sambuc //
6f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
7f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
8f4a2713aSLionel Sambuc //
9f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
10f4a2713aSLionel Sambuc //
11f4a2713aSLionel Sambuc // This file contains the Mips16 implementation of the TargetInstrInfo class.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc #include "Mips16InstrInfo.h"
15f4a2713aSLionel Sambuc #include "InstPrinter/MipsInstPrinter.h"
16f4a2713aSLionel Sambuc #include "MipsMachineFunction.h"
17f4a2713aSLionel Sambuc #include "MipsTargetMachine.h"
18f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/StringRef.h"
20f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstrBuilder.h"
21f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
22f4a2713aSLionel Sambuc #include "llvm/CodeGen/RegisterScavenging.h"
23f4a2713aSLionel Sambuc #include "llvm/MC/MCAsmInfo.h"
24f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
25f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
27f4a2713aSLionel Sambuc #include "llvm/Support/TargetRegistry.h"
28f4a2713aSLionel Sambuc #include <cctype>
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc using namespace llvm;
31f4a2713aSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "mips16-instrinfo"
33f4a2713aSLionel Sambuc 
Mips16InstrInfo(const MipsSubtarget & STI)34*0a6a1f1dSLionel Sambuc Mips16InstrInfo::Mips16InstrInfo(const MipsSubtarget &STI)
35*0a6a1f1dSLionel Sambuc     : MipsInstrInfo(STI, Mips::Bimm16), RI(STI) {}
36f4a2713aSLionel Sambuc 
getRegisterInfo() const37f4a2713aSLionel Sambuc const MipsRegisterInfo &Mips16InstrInfo::getRegisterInfo() const {
38f4a2713aSLionel Sambuc   return RI;
39f4a2713aSLionel Sambuc }
40f4a2713aSLionel Sambuc 
41f4a2713aSLionel Sambuc /// isLoadFromStackSlot - If the specified machine instruction is a direct
42f4a2713aSLionel Sambuc /// load from a stack slot, return the virtual or physical register number of
43f4a2713aSLionel Sambuc /// the destination along with the FrameIndex of the loaded stack slot.  If
44f4a2713aSLionel Sambuc /// not, return 0.  This predicate must return 0 if the instruction has
45f4a2713aSLionel Sambuc /// any side effects other than loading from the stack slot.
isLoadFromStackSlot(const MachineInstr * MI,int & FrameIndex) const46*0a6a1f1dSLionel Sambuc unsigned Mips16InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
47*0a6a1f1dSLionel Sambuc                                               int &FrameIndex) const {
48f4a2713aSLionel Sambuc   return 0;
49f4a2713aSLionel Sambuc }
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc /// isStoreToStackSlot - If the specified machine instruction is a direct
52f4a2713aSLionel Sambuc /// store to a stack slot, return the virtual or physical register number of
53f4a2713aSLionel Sambuc /// the source reg along with the FrameIndex of the loaded stack slot.  If
54f4a2713aSLionel Sambuc /// not, return 0.  This predicate must return 0 if the instruction has
55f4a2713aSLionel Sambuc /// any side effects other than storing to the stack slot.
isStoreToStackSlot(const MachineInstr * MI,int & FrameIndex) const56*0a6a1f1dSLionel Sambuc unsigned Mips16InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
57*0a6a1f1dSLionel Sambuc                                              int &FrameIndex) const {
58f4a2713aSLionel Sambuc   return 0;
59f4a2713aSLionel Sambuc }
60f4a2713aSLionel Sambuc 
copyPhysReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,DebugLoc DL,unsigned DestReg,unsigned SrcReg,bool KillSrc) const61f4a2713aSLionel Sambuc void Mips16InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
62f4a2713aSLionel Sambuc                                   MachineBasicBlock::iterator I, DebugLoc DL,
63f4a2713aSLionel Sambuc                                   unsigned DestReg, unsigned SrcReg,
64f4a2713aSLionel Sambuc                                   bool KillSrc) const {
65f4a2713aSLionel Sambuc   unsigned Opc = 0;
66f4a2713aSLionel Sambuc 
67f4a2713aSLionel Sambuc   if (Mips::CPU16RegsRegClass.contains(DestReg) &&
68f4a2713aSLionel Sambuc       Mips::GPR32RegClass.contains(SrcReg))
69f4a2713aSLionel Sambuc     Opc = Mips::MoveR3216;
70f4a2713aSLionel Sambuc   else if (Mips::GPR32RegClass.contains(DestReg) &&
71f4a2713aSLionel Sambuc            Mips::CPU16RegsRegClass.contains(SrcReg))
72f4a2713aSLionel Sambuc     Opc = Mips::Move32R16;
73f4a2713aSLionel Sambuc   else if ((SrcReg == Mips::HI0) &&
74f4a2713aSLionel Sambuc            (Mips::CPU16RegsRegClass.contains(DestReg)))
75f4a2713aSLionel Sambuc     Opc = Mips::Mfhi16, SrcReg = 0;
76f4a2713aSLionel Sambuc 
77f4a2713aSLionel Sambuc   else if ((SrcReg == Mips::LO0) &&
78f4a2713aSLionel Sambuc            (Mips::CPU16RegsRegClass.contains(DestReg)))
79f4a2713aSLionel Sambuc     Opc = Mips::Mflo16, SrcReg = 0;
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc 
82f4a2713aSLionel Sambuc   assert(Opc && "Cannot copy registers");
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc   MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc   if (DestReg)
87f4a2713aSLionel Sambuc     MIB.addReg(DestReg, RegState::Define);
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc   if (SrcReg)
90f4a2713aSLionel Sambuc     MIB.addReg(SrcReg, getKillRegState(KillSrc));
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc 
storeRegToStack(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,unsigned SrcReg,bool isKill,int FI,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI,int64_t Offset) const93*0a6a1f1dSLionel Sambuc void Mips16InstrInfo::storeRegToStack(MachineBasicBlock &MBB,
94*0a6a1f1dSLionel Sambuc                                       MachineBasicBlock::iterator I,
95f4a2713aSLionel Sambuc                                       unsigned SrcReg, bool isKill, int FI,
96*0a6a1f1dSLionel Sambuc                                       const TargetRegisterClass *RC,
97*0a6a1f1dSLionel Sambuc                                       const TargetRegisterInfo *TRI,
98f4a2713aSLionel Sambuc                                       int64_t Offset) const {
99f4a2713aSLionel Sambuc   DebugLoc DL;
100f4a2713aSLionel Sambuc   if (I != MBB.end()) DL = I->getDebugLoc();
101f4a2713aSLionel Sambuc   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
102f4a2713aSLionel Sambuc   unsigned Opc = 0;
103f4a2713aSLionel Sambuc   if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
104f4a2713aSLionel Sambuc     Opc = Mips::SwRxSpImmX16;
105f4a2713aSLionel Sambuc   assert(Opc && "Register class not handled!");
106f4a2713aSLionel Sambuc   BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill)).
107f4a2713aSLionel Sambuc       addFrameIndex(FI).addImm(Offset)
108f4a2713aSLionel Sambuc       .addMemOperand(MMO);
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc 
loadRegFromStack(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,unsigned DestReg,int FI,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI,int64_t Offset) const111*0a6a1f1dSLionel Sambuc void Mips16InstrInfo::loadRegFromStack(MachineBasicBlock &MBB,
112*0a6a1f1dSLionel Sambuc                                        MachineBasicBlock::iterator I,
113*0a6a1f1dSLionel Sambuc                                        unsigned DestReg, int FI,
114*0a6a1f1dSLionel Sambuc                                        const TargetRegisterClass *RC,
115*0a6a1f1dSLionel Sambuc                                        const TargetRegisterInfo *TRI,
116*0a6a1f1dSLionel Sambuc                                        int64_t Offset) const {
117f4a2713aSLionel Sambuc   DebugLoc DL;
118f4a2713aSLionel Sambuc   if (I != MBB.end()) DL = I->getDebugLoc();
119f4a2713aSLionel Sambuc   MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
120f4a2713aSLionel Sambuc   unsigned Opc = 0;
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc   if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
123f4a2713aSLionel Sambuc     Opc = Mips::LwRxSpImmX16;
124f4a2713aSLionel Sambuc   assert(Opc && "Register class not handled!");
125f4a2713aSLionel Sambuc   BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(Offset)
126f4a2713aSLionel Sambuc     .addMemOperand(MMO);
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc 
expandPostRAPseudo(MachineBasicBlock::iterator MI) const129f4a2713aSLionel Sambuc bool Mips16InstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
130f4a2713aSLionel Sambuc   MachineBasicBlock &MBB = *MI->getParent();
131f4a2713aSLionel Sambuc   switch(MI->getDesc().getOpcode()) {
132f4a2713aSLionel Sambuc   default:
133f4a2713aSLionel Sambuc     return false;
134f4a2713aSLionel Sambuc   case Mips::RetRA16:
135f4a2713aSLionel Sambuc     ExpandRetRA16(MBB, MI, Mips::JrcRa16);
136f4a2713aSLionel Sambuc     break;
137f4a2713aSLionel Sambuc   }
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc   MBB.erase(MI);
140f4a2713aSLionel Sambuc   return true;
141f4a2713aSLionel Sambuc }
142f4a2713aSLionel Sambuc 
143f4a2713aSLionel Sambuc /// GetOppositeBranchOpc - Return the inverse of the specified
144f4a2713aSLionel Sambuc /// opcode, e.g. turning BEQ to BNE.
getOppositeBranchOpc(unsigned Opc) const145f4a2713aSLionel Sambuc unsigned Mips16InstrInfo::getOppositeBranchOpc(unsigned Opc) const {
146f4a2713aSLionel Sambuc   switch (Opc) {
147f4a2713aSLionel Sambuc   case Mips::BeqzRxImmX16: return Mips::BnezRxImmX16;
148f4a2713aSLionel Sambuc   case Mips::BnezRxImmX16: return Mips::BeqzRxImmX16;
149f4a2713aSLionel Sambuc   case Mips::BeqzRxImm16: return Mips::BnezRxImm16;
150f4a2713aSLionel Sambuc   case Mips::BnezRxImm16: return Mips::BeqzRxImm16;
151f4a2713aSLionel Sambuc   case Mips::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
152f4a2713aSLionel Sambuc   case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
153f4a2713aSLionel Sambuc   case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
154f4a2713aSLionel Sambuc   case Mips::Btnez16: return Mips::Bteqz16;
155f4a2713aSLionel Sambuc   case Mips::BtnezX16: return Mips::BteqzX16;
156f4a2713aSLionel Sambuc   case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
157f4a2713aSLionel Sambuc   case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
158f4a2713aSLionel Sambuc   case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
159f4a2713aSLionel Sambuc   case Mips::Bteqz16: return Mips::Btnez16;
160f4a2713aSLionel Sambuc   case Mips::BteqzX16: return Mips::BtnezX16;
161f4a2713aSLionel Sambuc   case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
162f4a2713aSLionel Sambuc   case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
163f4a2713aSLionel Sambuc   case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
164f4a2713aSLionel Sambuc   case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
165f4a2713aSLionel Sambuc   case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
166f4a2713aSLionel Sambuc   case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
167f4a2713aSLionel Sambuc   }
168*0a6a1f1dSLionel Sambuc   llvm_unreachable("Illegal opcode!");
169f4a2713aSLionel Sambuc }
170f4a2713aSLionel Sambuc 
addSaveRestoreRegs(MachineInstrBuilder & MIB,const std::vector<CalleeSavedInfo> & CSI,unsigned Flags=0)171*0a6a1f1dSLionel Sambuc static void addSaveRestoreRegs(MachineInstrBuilder &MIB,
172*0a6a1f1dSLionel Sambuc                                const std::vector<CalleeSavedInfo> &CSI,
173*0a6a1f1dSLionel Sambuc                                unsigned Flags = 0) {
174*0a6a1f1dSLionel Sambuc   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
175*0a6a1f1dSLionel Sambuc     // Add the callee-saved register as live-in. Do not add if the register is
176*0a6a1f1dSLionel Sambuc     // RA and return address is taken, because it has already been added in
177*0a6a1f1dSLionel Sambuc     // method MipsTargetLowering::LowerRETURNADDR.
178*0a6a1f1dSLionel Sambuc     // It's killed at the spill, unless the register is RA and return address
179*0a6a1f1dSLionel Sambuc     // is taken.
180*0a6a1f1dSLionel Sambuc     unsigned Reg = CSI[e-i-1].getReg();
181*0a6a1f1dSLionel Sambuc     switch (Reg) {
182*0a6a1f1dSLionel Sambuc     case Mips::RA:
183*0a6a1f1dSLionel Sambuc     case Mips::S0:
184*0a6a1f1dSLionel Sambuc     case Mips::S1:
185*0a6a1f1dSLionel Sambuc       MIB.addReg(Reg, Flags);
186*0a6a1f1dSLionel Sambuc       break;
187*0a6a1f1dSLionel Sambuc     case Mips::S2:
188*0a6a1f1dSLionel Sambuc       break;
189*0a6a1f1dSLionel Sambuc     default:
190*0a6a1f1dSLionel Sambuc       llvm_unreachable("unexpected mips16 callee saved register");
191*0a6a1f1dSLionel Sambuc 
192*0a6a1f1dSLionel Sambuc     }
193*0a6a1f1dSLionel Sambuc   }
194*0a6a1f1dSLionel Sambuc }
195f4a2713aSLionel Sambuc // Adjust SP by FrameSize bytes. Save RA, S0, S1
makeFrame(unsigned SP,int64_t FrameSize,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const196f4a2713aSLionel Sambuc void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
197f4a2713aSLionel Sambuc                                 MachineBasicBlock &MBB,
198f4a2713aSLionel Sambuc                                 MachineBasicBlock::iterator I) const {
199f4a2713aSLionel Sambuc   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
200*0a6a1f1dSLionel Sambuc   MachineFunction &MF = *MBB.getParent();
201*0a6a1f1dSLionel Sambuc   MachineFrameInfo *MFI    = MF.getFrameInfo();
202*0a6a1f1dSLionel Sambuc   const BitVector Reserved = RI.getReservedRegs(MF);
203*0a6a1f1dSLionel Sambuc   bool SaveS2 = Reserved[Mips::S2];
204*0a6a1f1dSLionel Sambuc   MachineInstrBuilder MIB;
205*0a6a1f1dSLionel Sambuc   unsigned Opc = ((FrameSize <= 128) && !SaveS2)? Mips::Save16:Mips::SaveX16;
206*0a6a1f1dSLionel Sambuc   MIB = BuildMI(MBB, I, DL, get(Opc));
207*0a6a1f1dSLionel Sambuc   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
208*0a6a1f1dSLionel Sambuc   addSaveRestoreRegs(MIB, CSI);
209*0a6a1f1dSLionel Sambuc   if (SaveS2)
210*0a6a1f1dSLionel Sambuc     MIB.addReg(Mips::S2);
211f4a2713aSLionel Sambuc   if (isUInt<11>(FrameSize))
212*0a6a1f1dSLionel Sambuc     MIB.addImm(FrameSize);
213f4a2713aSLionel Sambuc   else {
214f4a2713aSLionel Sambuc     int Base = 2040; // should create template function like isUInt that
215f4a2713aSLionel Sambuc                      // returns largest possible n bit unsigned integer
216f4a2713aSLionel Sambuc     int64_t Remainder = FrameSize - Base;
217*0a6a1f1dSLionel Sambuc     MIB.addImm(Base);
218f4a2713aSLionel Sambuc     if (isInt<16>(-Remainder))
219f4a2713aSLionel Sambuc       BuildAddiuSpImm(MBB, I, -Remainder);
220f4a2713aSLionel Sambuc     else
221f4a2713aSLionel Sambuc       adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
222f4a2713aSLionel Sambuc   }
223f4a2713aSLionel Sambuc }
224f4a2713aSLionel Sambuc 
225f4a2713aSLionel Sambuc // Adjust SP by FrameSize bytes. Restore RA, S0, S1
restoreFrame(unsigned SP,int64_t FrameSize,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const226f4a2713aSLionel Sambuc void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
227f4a2713aSLionel Sambuc                                    MachineBasicBlock &MBB,
228f4a2713aSLionel Sambuc                                    MachineBasicBlock::iterator I) const {
229f4a2713aSLionel Sambuc   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
230*0a6a1f1dSLionel Sambuc   MachineFunction *MF = MBB.getParent();
231*0a6a1f1dSLionel Sambuc   MachineFrameInfo *MFI    = MF->getFrameInfo();
232*0a6a1f1dSLionel Sambuc   const BitVector Reserved = RI.getReservedRegs(*MF);
233*0a6a1f1dSLionel Sambuc   bool SaveS2 = Reserved[Mips::S2];
234*0a6a1f1dSLionel Sambuc   MachineInstrBuilder MIB;
235*0a6a1f1dSLionel Sambuc   unsigned Opc = ((FrameSize <= 128) && !SaveS2)?
236*0a6a1f1dSLionel Sambuc     Mips::Restore16:Mips::RestoreX16;
237*0a6a1f1dSLionel Sambuc 
238*0a6a1f1dSLionel Sambuc   if (!isUInt<11>(FrameSize)) {
239*0a6a1f1dSLionel Sambuc     unsigned Base = 2040;
240f4a2713aSLionel Sambuc     int64_t Remainder = FrameSize - Base;
241*0a6a1f1dSLionel Sambuc     FrameSize = Base; // should create template function like isUInt that
242*0a6a1f1dSLionel Sambuc                      // returns largest possible n bit unsigned integer
243*0a6a1f1dSLionel Sambuc 
244f4a2713aSLionel Sambuc     if (isInt<16>(Remainder))
245f4a2713aSLionel Sambuc       BuildAddiuSpImm(MBB, I, Remainder);
246f4a2713aSLionel Sambuc     else
247f4a2713aSLionel Sambuc       adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
248f4a2713aSLionel Sambuc   }
249*0a6a1f1dSLionel Sambuc   MIB = BuildMI(MBB, I, DL, get(Opc));
250*0a6a1f1dSLionel Sambuc   const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
251*0a6a1f1dSLionel Sambuc   addSaveRestoreRegs(MIB, CSI, RegState::Define);
252*0a6a1f1dSLionel Sambuc   if (SaveS2)
253*0a6a1f1dSLionel Sambuc     MIB.addReg(Mips::S2, RegState::Define);
254*0a6a1f1dSLionel Sambuc   MIB.addImm(FrameSize);
255f4a2713aSLionel Sambuc }
256f4a2713aSLionel Sambuc 
257f4a2713aSLionel Sambuc // Adjust SP by Amount bytes where bytes can be up to 32bit number.
258f4a2713aSLionel Sambuc // This can only be called at times that we know that there is at least one free
259f4a2713aSLionel Sambuc // register.
260f4a2713aSLionel Sambuc // This is clearly safe at prologue and epilogue.
261f4a2713aSLionel Sambuc //
adjustStackPtrBig(unsigned SP,int64_t Amount,MachineBasicBlock & MBB,MachineBasicBlock::iterator I,unsigned Reg1,unsigned Reg2) const262f4a2713aSLionel Sambuc void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
263f4a2713aSLionel Sambuc                                         MachineBasicBlock &MBB,
264f4a2713aSLionel Sambuc                                         MachineBasicBlock::iterator I,
265f4a2713aSLionel Sambuc                                         unsigned Reg1, unsigned Reg2) const {
266f4a2713aSLionel Sambuc   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
267f4a2713aSLionel Sambuc   //
268f4a2713aSLionel Sambuc   // li reg1, constant
269f4a2713aSLionel Sambuc   // move reg2, sp
270f4a2713aSLionel Sambuc   // add reg1, reg1, reg2
271f4a2713aSLionel Sambuc   // move sp, reg1
272f4a2713aSLionel Sambuc   //
273f4a2713aSLionel Sambuc   //
274f4a2713aSLionel Sambuc   MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
275*0a6a1f1dSLionel Sambuc   MIB1.addImm(Amount).addImm(-1);
276f4a2713aSLionel Sambuc   MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
277f4a2713aSLionel Sambuc   MIB2.addReg(Mips::SP, RegState::Kill);
278f4a2713aSLionel Sambuc   MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
279f4a2713aSLionel Sambuc   MIB3.addReg(Reg1);
280f4a2713aSLionel Sambuc   MIB3.addReg(Reg2, RegState::Kill);
281f4a2713aSLionel Sambuc   MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
282f4a2713aSLionel Sambuc                                                      Mips::SP);
283f4a2713aSLionel Sambuc   MIB4.addReg(Reg1, RegState::Kill);
284f4a2713aSLionel Sambuc }
285f4a2713aSLionel Sambuc 
adjustStackPtrBigUnrestricted(unsigned SP,int64_t Amount,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const286*0a6a1f1dSLionel Sambuc void Mips16InstrInfo::adjustStackPtrBigUnrestricted(
287*0a6a1f1dSLionel Sambuc     unsigned SP, int64_t Amount, MachineBasicBlock &MBB,
288f4a2713aSLionel Sambuc     MachineBasicBlock::iterator I) const {
289*0a6a1f1dSLionel Sambuc    llvm_unreachable("adjust stack pointer amount exceeded");
290f4a2713aSLionel Sambuc }
291f4a2713aSLionel Sambuc 
292f4a2713aSLionel Sambuc /// Adjust SP by Amount bytes.
adjustStackPtr(unsigned SP,int64_t Amount,MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const293f4a2713aSLionel Sambuc void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
294f4a2713aSLionel Sambuc                                      MachineBasicBlock &MBB,
295f4a2713aSLionel Sambuc                                      MachineBasicBlock::iterator I) const {
296*0a6a1f1dSLionel Sambuc   if (Amount == 0)
297*0a6a1f1dSLionel Sambuc     return;
298*0a6a1f1dSLionel Sambuc 
299f4a2713aSLionel Sambuc   if (isInt<16>(Amount))  // need to change to addiu sp, ....and isInt<16>
300f4a2713aSLionel Sambuc     BuildAddiuSpImm(MBB, I, Amount);
301f4a2713aSLionel Sambuc   else
302f4a2713aSLionel Sambuc     adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
303f4a2713aSLionel Sambuc }
304f4a2713aSLionel Sambuc 
305f4a2713aSLionel Sambuc /// This function generates the sequence of instructions needed to get the
306f4a2713aSLionel Sambuc /// result of adding register REG and immediate IMM.
loadImmediate(unsigned FrameReg,int64_t Imm,MachineBasicBlock & MBB,MachineBasicBlock::iterator II,DebugLoc DL,unsigned & NewImm) const307*0a6a1f1dSLionel Sambuc unsigned Mips16InstrInfo::loadImmediate(unsigned FrameReg, int64_t Imm,
308*0a6a1f1dSLionel Sambuc                                         MachineBasicBlock &MBB,
309*0a6a1f1dSLionel Sambuc                                         MachineBasicBlock::iterator II,
310*0a6a1f1dSLionel Sambuc                                         DebugLoc DL, unsigned &NewImm) const {
311f4a2713aSLionel Sambuc   //
312f4a2713aSLionel Sambuc   // given original instruction is:
313f4a2713aSLionel Sambuc   // Instr rx, T[offset] where offset is too big.
314f4a2713aSLionel Sambuc   //
315f4a2713aSLionel Sambuc   // lo = offset & 0xFFFF
316f4a2713aSLionel Sambuc   // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
317f4a2713aSLionel Sambuc   //
318f4a2713aSLionel Sambuc   // let T = temporary register
319f4a2713aSLionel Sambuc   // li T, hi
320f4a2713aSLionel Sambuc   // shl T, 16
321f4a2713aSLionel Sambuc   // add T, Rx, T
322f4a2713aSLionel Sambuc   //
323f4a2713aSLionel Sambuc   RegScavenger rs;
324f4a2713aSLionel Sambuc   int32_t lo = Imm & 0xFFFF;
325f4a2713aSLionel Sambuc   NewImm = lo;
326f4a2713aSLionel Sambuc   int Reg =0;
327f4a2713aSLionel Sambuc   int SpReg = 0;
328f4a2713aSLionel Sambuc 
329f4a2713aSLionel Sambuc   rs.enterBasicBlock(&MBB);
330f4a2713aSLionel Sambuc   rs.forward(II);
331f4a2713aSLionel Sambuc   //
332f4a2713aSLionel Sambuc   // We need to know which registers can be used, in the case where there
333f4a2713aSLionel Sambuc   // are not enough free registers. We exclude all registers that
334f4a2713aSLionel Sambuc   // are used in the instruction that we are helping.
335f4a2713aSLionel Sambuc   //  // Consider all allocatable registers in the register class initially
336f4a2713aSLionel Sambuc   BitVector Candidates =
337f4a2713aSLionel Sambuc       RI.getAllocatableSet
338f4a2713aSLionel Sambuc       (*II->getParent()->getParent(), &Mips::CPU16RegsRegClass);
339f4a2713aSLionel Sambuc   // Exclude all the registers being used by the instruction.
340f4a2713aSLionel Sambuc   for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
341f4a2713aSLionel Sambuc     MachineOperand &MO = II->getOperand(i);
342f4a2713aSLionel Sambuc     if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
343f4a2713aSLionel Sambuc         !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
344f4a2713aSLionel Sambuc       Candidates.reset(MO.getReg());
345f4a2713aSLionel Sambuc   }
346*0a6a1f1dSLionel Sambuc 
347f4a2713aSLionel Sambuc   // If the same register was used and defined in an instruction, then
348f4a2713aSLionel Sambuc   // it will not be in the list of candidates.
349f4a2713aSLionel Sambuc   //
350f4a2713aSLionel Sambuc   // we need to analyze the instruction that we are helping.
351f4a2713aSLionel Sambuc   // we need to know if it defines register x but register x is not
352f4a2713aSLionel Sambuc   // present as an operand of the instruction. this tells
353f4a2713aSLionel Sambuc   // whether the register is live before the instruction. if it's not
354f4a2713aSLionel Sambuc   // then we don't need to save it in case there are no free registers.
355f4a2713aSLionel Sambuc   int DefReg = 0;
356f4a2713aSLionel Sambuc   for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
357f4a2713aSLionel Sambuc     MachineOperand &MO = II->getOperand(i);
358f4a2713aSLionel Sambuc     if (MO.isReg() && MO.isDef()) {
359f4a2713aSLionel Sambuc       DefReg = MO.getReg();
360f4a2713aSLionel Sambuc       break;
361f4a2713aSLionel Sambuc     }
362f4a2713aSLionel Sambuc   }
363f4a2713aSLionel Sambuc 
364*0a6a1f1dSLionel Sambuc   BitVector Available = rs.getRegsAvailable(&Mips::CPU16RegsRegClass);
365f4a2713aSLionel Sambuc   Available &= Candidates;
366f4a2713aSLionel Sambuc   //
367f4a2713aSLionel Sambuc   // we use T0 for the first register, if we need to save something away.
368f4a2713aSLionel Sambuc   // we use T1 for the second register, if we need to save something away.
369f4a2713aSLionel Sambuc   //
370f4a2713aSLionel Sambuc   unsigned FirstRegSaved =0, SecondRegSaved=0;
371f4a2713aSLionel Sambuc   unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;
372f4a2713aSLionel Sambuc 
373f4a2713aSLionel Sambuc   Reg = Available.find_first();
374f4a2713aSLionel Sambuc 
375f4a2713aSLionel Sambuc   if (Reg == -1) {
376f4a2713aSLionel Sambuc     Reg = Candidates.find_first();
377f4a2713aSLionel Sambuc     Candidates.reset(Reg);
378f4a2713aSLionel Sambuc     if (DefReg != Reg) {
379f4a2713aSLionel Sambuc       FirstRegSaved = Reg;
380f4a2713aSLionel Sambuc       FirstRegSavedTo = Mips::T0;
381f4a2713aSLionel Sambuc       copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
382f4a2713aSLionel Sambuc     }
383f4a2713aSLionel Sambuc   }
384f4a2713aSLionel Sambuc   else
385f4a2713aSLionel Sambuc     Available.reset(Reg);
386*0a6a1f1dSLionel Sambuc   BuildMI(MBB, II, DL, get(Mips::LwConstant32), Reg).addImm(Imm).addImm(-1);
387f4a2713aSLionel Sambuc   NewImm = 0;
388f4a2713aSLionel Sambuc   if (FrameReg == Mips::SP) {
389f4a2713aSLionel Sambuc     SpReg = Available.find_first();
390f4a2713aSLionel Sambuc     if (SpReg == -1) {
391f4a2713aSLionel Sambuc       SpReg = Candidates.find_first();
392f4a2713aSLionel Sambuc       // Candidates.reset(SpReg); // not really needed
393f4a2713aSLionel Sambuc       if (DefReg!= SpReg) {
394f4a2713aSLionel Sambuc         SecondRegSaved = SpReg;
395f4a2713aSLionel Sambuc         SecondRegSavedTo = Mips::T1;
396f4a2713aSLionel Sambuc       }
397f4a2713aSLionel Sambuc       if (SecondRegSaved)
398f4a2713aSLionel Sambuc         copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
399f4a2713aSLionel Sambuc     }
400f4a2713aSLionel Sambuc    else
401f4a2713aSLionel Sambuc      Available.reset(SpReg);
402f4a2713aSLionel Sambuc     copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
403f4a2713aSLionel Sambuc     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(SpReg, RegState::Kill)
404f4a2713aSLionel Sambuc       .addReg(Reg);
405f4a2713aSLionel Sambuc   }
406f4a2713aSLionel Sambuc   else
407f4a2713aSLionel Sambuc     BuildMI(MBB, II, DL, get(Mips::  AdduRxRyRz16), Reg).addReg(FrameReg)
408f4a2713aSLionel Sambuc       .addReg(Reg, RegState::Kill);
409f4a2713aSLionel Sambuc   if (FirstRegSaved || SecondRegSaved) {
410*0a6a1f1dSLionel Sambuc     II = std::next(II);
411f4a2713aSLionel Sambuc     if (FirstRegSaved)
412f4a2713aSLionel Sambuc       copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
413f4a2713aSLionel Sambuc     if (SecondRegSaved)
414f4a2713aSLionel Sambuc       copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
415f4a2713aSLionel Sambuc   }
416f4a2713aSLionel Sambuc   return Reg;
417f4a2713aSLionel Sambuc }
418f4a2713aSLionel Sambuc 
getAnalyzableBrOpc(unsigned Opc) const419f4a2713aSLionel Sambuc unsigned Mips16InstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
420f4a2713aSLionel Sambuc   return (Opc == Mips::BeqzRxImmX16   || Opc == Mips::BimmX16  ||
421f4a2713aSLionel Sambuc           Opc == Mips::Bimm16  ||
422f4a2713aSLionel Sambuc           Opc == Mips::Bteqz16        || Opc == Mips::Btnez16 ||
423f4a2713aSLionel Sambuc           Opc == Mips::BeqzRxImm16    || Opc == Mips::BnezRxImm16   ||
424f4a2713aSLionel Sambuc           Opc == Mips::BnezRxImmX16   || Opc == Mips::BteqzX16 ||
425f4a2713aSLionel Sambuc           Opc == Mips::BteqzT8CmpX16  || Opc == Mips::BteqzT8CmpiX16 ||
426f4a2713aSLionel Sambuc           Opc == Mips::BteqzT8SltX16  || Opc == Mips::BteqzT8SltuX16  ||
427f4a2713aSLionel Sambuc           Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
428f4a2713aSLionel Sambuc           Opc == Mips::BtnezX16       || Opc == Mips::BtnezT8CmpX16 ||
429f4a2713aSLionel Sambuc           Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
430f4a2713aSLionel Sambuc           Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
431f4a2713aSLionel Sambuc           Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
432f4a2713aSLionel Sambuc }
433f4a2713aSLionel Sambuc 
ExpandRetRA16(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,unsigned Opc) const434f4a2713aSLionel Sambuc void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
435f4a2713aSLionel Sambuc                                   MachineBasicBlock::iterator I,
436f4a2713aSLionel Sambuc                                   unsigned Opc) const {
437f4a2713aSLionel Sambuc   BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
438f4a2713aSLionel Sambuc }
439f4a2713aSLionel Sambuc 
AddiuSpImm(int64_t Imm) const440f4a2713aSLionel Sambuc const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
441f4a2713aSLionel Sambuc   if (validSpImm8(Imm))
442f4a2713aSLionel Sambuc     return get(Mips::AddiuSpImm16);
443f4a2713aSLionel Sambuc   else
444f4a2713aSLionel Sambuc     return get(Mips::AddiuSpImmX16);
445f4a2713aSLionel Sambuc }
446f4a2713aSLionel Sambuc 
BuildAddiuSpImm(MachineBasicBlock & MBB,MachineBasicBlock::iterator I,int64_t Imm) const447f4a2713aSLionel Sambuc void Mips16InstrInfo::BuildAddiuSpImm
448f4a2713aSLionel Sambuc   (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
449f4a2713aSLionel Sambuc   DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
450f4a2713aSLionel Sambuc   BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
451f4a2713aSLionel Sambuc }
452f4a2713aSLionel Sambuc 
createMips16InstrInfo(const MipsSubtarget & STI)453*0a6a1f1dSLionel Sambuc const MipsInstrInfo *llvm::createMips16InstrInfo(const MipsSubtarget &STI) {
454*0a6a1f1dSLionel Sambuc   return new Mips16InstrInfo(STI);
455f4a2713aSLionel Sambuc }
456f4a2713aSLionel Sambuc 
validImmediate(unsigned Opcode,unsigned Reg,int64_t Amount)457f4a2713aSLionel Sambuc bool Mips16InstrInfo::validImmediate(unsigned Opcode, unsigned Reg,
458f4a2713aSLionel Sambuc                                      int64_t Amount) {
459f4a2713aSLionel Sambuc   switch (Opcode) {
460f4a2713aSLionel Sambuc   case Mips::LbRxRyOffMemX16:
461f4a2713aSLionel Sambuc   case Mips::LbuRxRyOffMemX16:
462f4a2713aSLionel Sambuc   case Mips::LhRxRyOffMemX16:
463f4a2713aSLionel Sambuc   case Mips::LhuRxRyOffMemX16:
464f4a2713aSLionel Sambuc   case Mips::SbRxRyOffMemX16:
465f4a2713aSLionel Sambuc   case Mips::ShRxRyOffMemX16:
466f4a2713aSLionel Sambuc   case Mips::LwRxRyOffMemX16:
467f4a2713aSLionel Sambuc   case Mips::SwRxRyOffMemX16:
468f4a2713aSLionel Sambuc   case Mips::SwRxSpImmX16:
469f4a2713aSLionel Sambuc   case Mips::LwRxSpImmX16:
470f4a2713aSLionel Sambuc     return isInt<16>(Amount);
471f4a2713aSLionel Sambuc   case Mips::AddiuRxRyOffMemX16:
472f4a2713aSLionel Sambuc     if ((Reg == Mips::PC) || (Reg == Mips::SP))
473f4a2713aSLionel Sambuc       return isInt<16>(Amount);
474f4a2713aSLionel Sambuc     return isInt<15>(Amount);
475f4a2713aSLionel Sambuc   }
476f4a2713aSLionel Sambuc   llvm_unreachable("unexpected Opcode in validImmediate");
477f4a2713aSLionel Sambuc }
478f4a2713aSLionel Sambuc 
479f4a2713aSLionel Sambuc /// Measure the specified inline asm to determine an approximation of its
480f4a2713aSLionel Sambuc /// length.
481f4a2713aSLionel Sambuc /// Comments (which run till the next SeparatorString or newline) do not
482f4a2713aSLionel Sambuc /// count as an instruction.
483f4a2713aSLionel Sambuc /// Any other non-whitespace text is considered an instruction, with
484f4a2713aSLionel Sambuc /// multiple instructions separated by SeparatorString or newlines.
485f4a2713aSLionel Sambuc /// Variable-length instructions are not handled here; this function
486f4a2713aSLionel Sambuc /// may be overloaded in the target code to do that.
487f4a2713aSLionel Sambuc /// We implement the special case of the .space directive taking only an
488f4a2713aSLionel Sambuc /// integer argument, which is the size in bytes. This is used for creating
489f4a2713aSLionel Sambuc /// inline code spacing for testing purposes using inline assembly.
490f4a2713aSLionel Sambuc ///
getInlineAsmLength(const char * Str,const MCAsmInfo & MAI) const491f4a2713aSLionel Sambuc unsigned Mips16InstrInfo::getInlineAsmLength(const char *Str,
492f4a2713aSLionel Sambuc                                              const MCAsmInfo &MAI) const {
493f4a2713aSLionel Sambuc 
494f4a2713aSLionel Sambuc   // Count the number of instructions in the asm.
495f4a2713aSLionel Sambuc   bool atInsnStart = true;
496f4a2713aSLionel Sambuc   unsigned Length = 0;
497f4a2713aSLionel Sambuc   for (; *Str; ++Str) {
498f4a2713aSLionel Sambuc     if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
499f4a2713aSLionel Sambuc                                 strlen(MAI.getSeparatorString())) == 0)
500f4a2713aSLionel Sambuc       atInsnStart = true;
501f4a2713aSLionel Sambuc     if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
502f4a2713aSLionel Sambuc       if (strncmp(Str, ".space", 6)==0) {
503f4a2713aSLionel Sambuc         char *EStr; int Sz;
504f4a2713aSLionel Sambuc         Sz = strtol(Str+6, &EStr, 10);
505f4a2713aSLionel Sambuc         while (isspace(*EStr)) ++EStr;
506f4a2713aSLionel Sambuc         if (*EStr=='\0') {
507f4a2713aSLionel Sambuc           DEBUG(dbgs() << "parsed .space " << Sz << '\n');
508f4a2713aSLionel Sambuc           return Sz;
509f4a2713aSLionel Sambuc         }
510f4a2713aSLionel Sambuc       }
511f4a2713aSLionel Sambuc       Length += MAI.getMaxInstLength();
512f4a2713aSLionel Sambuc       atInsnStart = false;
513f4a2713aSLionel Sambuc     }
514f4a2713aSLionel Sambuc     if (atInsnStart && strncmp(Str, MAI.getCommentString(),
515f4a2713aSLionel Sambuc                                strlen(MAI.getCommentString())) == 0)
516f4a2713aSLionel Sambuc       atInsnStart = false;
517f4a2713aSLionel Sambuc   }
518f4a2713aSLionel Sambuc 
519f4a2713aSLionel Sambuc   return Length;
520f4a2713aSLionel Sambuc }
521