1 //===-- Mips16InstrInfo.cpp - Mips16 Instruction Information --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Mips16 implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
13 #include "Mips16InstrInfo.h"
14 #include "InstPrinter/MipsInstPrinter.h"
15 #include "MipsMachineFunction.h"
16 #include "MipsTargetMachine.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/RegisterScavenging.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/TargetRegistry.h"
31 static cl::opt<bool> NeverUseSaveRestore(
32 "mips16-never-use-save-restore",
34 cl::desc("For testing ability to adjust stack pointer "
35 "without save/restore instruction"),
39 Mips16InstrInfo::Mips16InstrInfo(MipsTargetMachine &tm)
40 : MipsInstrInfo(tm, Mips::Bimm16),
41 RI(*tm.getSubtargetImpl()) {}
43 const MipsRegisterInfo &Mips16InstrInfo::getRegisterInfo() const {
47 /// isLoadFromStackSlot - If the specified machine instruction is a direct
48 /// load from a stack slot, return the virtual or physical register number of
49 /// the destination along with the FrameIndex of the loaded stack slot. If
50 /// not, return 0. This predicate must return 0 if the instruction has
51 /// any side effects other than loading from the stack slot.
52 unsigned Mips16InstrInfo::
53 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
58 /// isStoreToStackSlot - If the specified machine instruction is a direct
59 /// store to a stack slot, return the virtual or physical register number of
60 /// the source reg along with the FrameIndex of the loaded stack slot. If
61 /// not, return 0. This predicate must return 0 if the instruction has
62 /// any side effects other than storing to the stack slot.
63 unsigned Mips16InstrInfo::
64 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
69 void Mips16InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
70 MachineBasicBlock::iterator I, DebugLoc DL,
71 unsigned DestReg, unsigned SrcReg,
75 if (Mips::CPU16RegsRegClass.contains(DestReg) &&
76 Mips::GPR32RegClass.contains(SrcReg))
77 Opc = Mips::MoveR3216;
78 else if (Mips::GPR32RegClass.contains(DestReg) &&
79 Mips::CPU16RegsRegClass.contains(SrcReg))
80 Opc = Mips::Move32R16;
81 else if ((SrcReg == Mips::HI0) &&
82 (Mips::CPU16RegsRegClass.contains(DestReg)))
83 Opc = Mips::Mfhi16, SrcReg = 0;
85 else if ((SrcReg == Mips::LO0) &&
86 (Mips::CPU16RegsRegClass.contains(DestReg)))
87 Opc = Mips::Mflo16, SrcReg = 0;
90 assert(Opc && "Cannot copy registers");
92 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
95 MIB.addReg(DestReg, RegState::Define);
98 MIB.addReg(SrcReg, getKillRegState(KillSrc));
101 void Mips16InstrInfo::
102 storeRegToStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
103 unsigned SrcReg, bool isKill, int FI,
104 const TargetRegisterClass *RC, const TargetRegisterInfo *TRI,
105 int64_t Offset) const {
107 if (I != MBB.end()) DL = I->getDebugLoc();
108 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOStore);
110 if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
111 Opc = Mips::SwRxSpImmX16;
112 assert(Opc && "Register class not handled!");
113 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill)).
114 addFrameIndex(FI).addImm(Offset)
118 void Mips16InstrInfo::
119 loadRegFromStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
120 unsigned DestReg, int FI, const TargetRegisterClass *RC,
121 const TargetRegisterInfo *TRI, int64_t Offset) const {
123 if (I != MBB.end()) DL = I->getDebugLoc();
124 MachineMemOperand *MMO = GetMemOperand(MBB, FI, MachineMemOperand::MOLoad);
127 if (Mips::CPU16RegsRegClass.hasSubClassEq(RC))
128 Opc = Mips::LwRxSpImmX16;
129 assert(Opc && "Register class not handled!");
130 BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(Offset)
134 bool Mips16InstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
135 MachineBasicBlock &MBB = *MI->getParent();
136 switch(MI->getDesc().getOpcode()) {
140 ExpandRetRA16(MBB, MI, Mips::JrcRa16);
148 /// GetOppositeBranchOpc - Return the inverse of the specified
149 /// opcode, e.g. turning BEQ to BNE.
150 unsigned Mips16InstrInfo::getOppositeBranchOpc(unsigned Opc) const {
152 default: llvm_unreachable("Illegal opcode!");
153 case Mips::BeqzRxImmX16: return Mips::BnezRxImmX16;
154 case Mips::BnezRxImmX16: return Mips::BeqzRxImmX16;
155 case Mips::BeqzRxImm16: return Mips::BnezRxImm16;
156 case Mips::BnezRxImm16: return Mips::BeqzRxImm16;
157 case Mips::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
158 case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
159 case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
160 case Mips::Btnez16: return Mips::Bteqz16;
161 case Mips::BtnezX16: return Mips::BteqzX16;
162 case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
163 case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
164 case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
165 case Mips::Bteqz16: return Mips::Btnez16;
166 case Mips::BteqzX16: return Mips::BtnezX16;
167 case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
168 case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
169 case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
170 case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
171 case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
172 case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
174 assert(false && "Implement this function.");
178 // Adjust SP by FrameSize bytes. Save RA, S0, S1
179 void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
180 MachineBasicBlock &MBB,
181 MachineBasicBlock::iterator I) const {
182 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
183 if (!NeverUseSaveRestore) {
184 if (isUInt<11>(FrameSize))
185 //BuildMI(MBB, I, DL, get(Mips::SaveRaF16)).addImm(FrameSize);
186 BuildMI(MBB, I, DL, get(Mips::SaveX16)).addReg(Mips::RA).
188 addReg(Mips::S1).addReg(Mips::S2).addImm(FrameSize);
190 int Base = 2040; // should create template function like isUInt that
191 // returns largest possible n bit unsigned integer
192 int64_t Remainder = FrameSize - Base;
193 BuildMI(MBB, I, DL, get(Mips::SaveX16)).addReg(Mips::RA).
195 addReg(Mips::S1).addReg(Mips::S2).addImm(Base);
196 if (isInt<16>(-Remainder))
197 BuildAddiuSpImm(MBB, I, -Remainder);
199 adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
209 MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
211 MIB1.addReg(Mips::SP);
213 MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
215 MIB2.addReg(Mips::SP);
217 MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
219 MIB3.addReg(Mips::SP);
221 adjustStackPtrBig(SP, -FrameSize, MBB, I, Mips::V0, Mips::V1);
225 // Adjust SP by FrameSize bytes. Restore RA, S0, S1
226 void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
227 MachineBasicBlock &MBB,
228 MachineBasicBlock::iterator I) const {
229 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
230 if (!NeverUseSaveRestore) {
231 if (isUInt<11>(FrameSize))
232 BuildMI(MBB, I, DL, get(Mips::RestoreX16)).addReg(Mips::RA).
234 addReg(Mips::S1).addReg(Mips::S2).addImm(FrameSize);
236 int Base = 2040; // should create template function like isUInt that
237 // returns largest possible n bit unsigned integer
238 int64_t Remainder = FrameSize - Base;
239 if (isInt<16>(Remainder))
240 BuildAddiuSpImm(MBB, I, Remainder);
242 adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
243 BuildMI(MBB, I, DL, get(Mips::RestoreX16)).addReg(Mips::RA).
245 addReg(Mips::S1).addReg(Mips::S2).addImm(Base);
249 adjustStackPtrBig(SP, FrameSize, MBB, I, Mips::A0, Mips::A1);
253 MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
255 MIB1.addReg(Mips::SP);
257 MachineInstrBuilder MIB0 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
259 MIB0.addReg(Mips::A0);
260 MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
262 MIB2.addReg(Mips::SP);
264 MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
266 MIB3.addReg(Mips::SP);
272 // Adjust SP by Amount bytes where bytes can be up to 32bit number.
273 // This can only be called at times that we know that there is at least one free
275 // This is clearly safe at prologue and epilogue.
277 void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
278 MachineBasicBlock &MBB,
279 MachineBasicBlock::iterator I,
280 unsigned Reg1, unsigned Reg2) const {
281 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
282 // MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
283 // unsigned Reg1 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
284 // unsigned Reg2 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
288 // add reg1, reg1, reg2
292 MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
293 MIB1.addImm(Amount).addImm(-1);
294 MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
295 MIB2.addReg(Mips::SP, RegState::Kill);
296 MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
298 MIB3.addReg(Reg2, RegState::Kill);
299 MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
301 MIB4.addReg(Reg1, RegState::Kill);
304 void Mips16InstrInfo::adjustStackPtrBigUnrestricted(unsigned SP, int64_t Amount,
305 MachineBasicBlock &MBB,
306 MachineBasicBlock::iterator I) const {
307 assert(false && "adjust stack pointer amount exceeded");
310 /// Adjust SP by Amount bytes.
311 void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
312 MachineBasicBlock &MBB,
313 MachineBasicBlock::iterator I) const {
314 if (isInt<16>(Amount)) // need to change to addiu sp, ....and isInt<16>
315 BuildAddiuSpImm(MBB, I, Amount);
317 adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
320 /// This function generates the sequence of instructions needed to get the
321 /// result of adding register REG and immediate IMM.
323 Mips16InstrInfo::loadImmediate(unsigned FrameReg,
324 int64_t Imm, MachineBasicBlock &MBB,
325 MachineBasicBlock::iterator II, DebugLoc DL,
326 unsigned &NewImm) const {
328 // given original instruction is:
329 // Instr rx, T[offset] where offset is too big.
331 // lo = offset & 0xFFFF
332 // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
334 // let T = temporary register
340 int32_t lo = Imm & 0xFFFF;
345 rs.enterBasicBlock(&MBB);
348 // We need to know which registers can be used, in the case where there
349 // are not enough free registers. We exclude all registers that
350 // are used in the instruction that we are helping.
351 // // Consider all allocatable registers in the register class initially
352 BitVector Candidates =
354 (*II->getParent()->getParent(), &Mips::CPU16RegsRegClass);
355 // Exclude all the registers being used by the instruction.
356 for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
357 MachineOperand &MO = II->getOperand(i);
358 if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
359 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
360 Candidates.reset(MO.getReg());
363 // If the same register was used and defined in an instruction, then
364 // it will not be in the list of candidates.
366 // we need to analyze the instruction that we are helping.
367 // we need to know if it defines register x but register x is not
368 // present as an operand of the instruction. this tells
369 // whether the register is live before the instruction. if it's not
370 // then we don't need to save it in case there are no free registers.
373 for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
374 MachineOperand &MO = II->getOperand(i);
375 if (MO.isReg() && MO.isDef()) {
376 DefReg = MO.getReg();
381 BitVector Available = rs.getRegsAvailable(&Mips::CPU16RegsRegClass);
383 Available &= Candidates;
385 // we use T0 for the first register, if we need to save something away.
386 // we use T1 for the second register, if we need to save something away.
388 unsigned FirstRegSaved =0, SecondRegSaved=0;
389 unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;
392 Reg = Available.find_first();
395 Reg = Candidates.find_first();
396 Candidates.reset(Reg);
399 FirstRegSavedTo = Mips::T0;
400 copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
404 Available.reset(Reg);
405 BuildMI(MBB, II, DL, get(Mips::LwConstant32), Reg).addImm(Imm).addImm(-1);
407 if (FrameReg == Mips::SP) {
408 SpReg = Available.find_first();
410 SpReg = Candidates.find_first();
411 // Candidates.reset(SpReg); // not really needed
412 if (DefReg!= SpReg) {
413 SecondRegSaved = SpReg;
414 SecondRegSavedTo = Mips::T1;
417 copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
420 Available.reset(SpReg);
421 copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
422 BuildMI(MBB, II, DL, get(Mips:: AdduRxRyRz16), Reg).addReg(SpReg, RegState::Kill)
426 BuildMI(MBB, II, DL, get(Mips:: AdduRxRyRz16), Reg).addReg(FrameReg)
427 .addReg(Reg, RegState::Kill);
428 if (FirstRegSaved || SecondRegSaved) {
431 copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
433 copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
438 unsigned Mips16InstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
439 return (Opc == Mips::BeqzRxImmX16 || Opc == Mips::BimmX16 ||
440 Opc == Mips::Bimm16 ||
441 Opc == Mips::Bteqz16 || Opc == Mips::Btnez16 ||
442 Opc == Mips::BeqzRxImm16 || Opc == Mips::BnezRxImm16 ||
443 Opc == Mips::BnezRxImmX16 || Opc == Mips::BteqzX16 ||
444 Opc == Mips::BteqzT8CmpX16 || Opc == Mips::BteqzT8CmpiX16 ||
445 Opc == Mips::BteqzT8SltX16 || Opc == Mips::BteqzT8SltuX16 ||
446 Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
447 Opc == Mips::BtnezX16 || Opc == Mips::BtnezT8CmpX16 ||
448 Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
449 Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
450 Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
453 void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
454 MachineBasicBlock::iterator I,
455 unsigned Opc) const {
456 BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
460 const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
461 if (validSpImm8(Imm))
462 return get(Mips::AddiuSpImm16);
464 return get(Mips::AddiuSpImmX16);
467 void Mips16InstrInfo::BuildAddiuSpImm
468 (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
469 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
470 BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
473 const MipsInstrInfo *llvm::createMips16InstrInfo(MipsTargetMachine &TM) {
474 return new Mips16InstrInfo(TM);
477 bool Mips16InstrInfo::validImmediate(unsigned Opcode, unsigned Reg,
480 case Mips::LbRxRyOffMemX16:
481 case Mips::LbuRxRyOffMemX16:
482 case Mips::LhRxRyOffMemX16:
483 case Mips::LhuRxRyOffMemX16:
484 case Mips::SbRxRyOffMemX16:
485 case Mips::ShRxRyOffMemX16:
486 case Mips::LwRxRyOffMemX16:
487 case Mips::SwRxRyOffMemX16:
488 case Mips::SwRxSpImmX16:
489 case Mips::LwRxSpImmX16:
490 return isInt<16>(Amount);
491 case Mips::AddiuRxRyOffMemX16:
492 if ((Reg == Mips::PC) || (Reg == Mips::SP))
493 return isInt<16>(Amount);
494 return isInt<15>(Amount);
496 llvm_unreachable("unexpected Opcode in validImmediate");
499 /// Measure the specified inline asm to determine an approximation of its
501 /// Comments (which run till the next SeparatorString or newline) do not
502 /// count as an instruction.
503 /// Any other non-whitespace text is considered an instruction, with
504 /// multiple instructions separated by SeparatorString or newlines.
505 /// Variable-length instructions are not handled here; this function
506 /// may be overloaded in the target code to do that.
507 /// We implement the special case of the .space directive taking only an
508 /// integer argument, which is the size in bytes. This is used for creating
509 /// inline code spacing for testing purposes using inline assembly.
511 unsigned Mips16InstrInfo::getInlineAsmLength(const char *Str,
512 const MCAsmInfo &MAI) const {
515 // Count the number of instructions in the asm.
516 bool atInsnStart = true;
518 for (; *Str; ++Str) {
519 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
520 strlen(MAI.getSeparatorString())) == 0)
522 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
523 if (strncmp(Str, ".space", 6)==0) {
525 Sz = strtol(Str+6, &EStr, 10);
526 while (isspace(*EStr)) ++EStr;
528 DEBUG(dbgs() << "parsed .space " << Sz << '\n');
532 Length += MAI.getMaxInstLength();
535 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
536 strlen(MAI.getCommentString())) == 0)