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::BteqzT8CmpX16: return Mips::BtnezT8CmpX16;
156 case Mips::BteqzT8SltX16: return Mips::BtnezT8SltX16;
157 case Mips::BteqzT8SltiX16: return Mips::BtnezT8SltiX16;
158 case Mips::BtnezX16: return Mips::BteqzX16;
159 case Mips::BtnezT8CmpiX16: return Mips::BteqzT8CmpiX16;
160 case Mips::BtnezT8SltuX16: return Mips::BteqzT8SltuX16;
161 case Mips::BtnezT8SltiuX16: return Mips::BteqzT8SltiuX16;
162 case Mips::BteqzX16: return Mips::BtnezX16;
163 case Mips::BteqzT8CmpiX16: return Mips::BtnezT8CmpiX16;
164 case Mips::BteqzT8SltuX16: return Mips::BtnezT8SltuX16;
165 case Mips::BteqzT8SltiuX16: return Mips::BtnezT8SltiuX16;
166 case Mips::BtnezT8CmpX16: return Mips::BteqzT8CmpX16;
167 case Mips::BtnezT8SltX16: return Mips::BteqzT8SltX16;
168 case Mips::BtnezT8SltiX16: return Mips::BteqzT8SltiX16;
170 assert(false && "Implement this function.");
174 // Adjust SP by FrameSize bytes. Save RA, S0, S1
175 void Mips16InstrInfo::makeFrame(unsigned SP, int64_t FrameSize,
176 MachineBasicBlock &MBB,
177 MachineBasicBlock::iterator I) const {
178 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
179 if (!NeverUseSaveRestore) {
180 if (isUInt<11>(FrameSize))
181 BuildMI(MBB, I, DL, get(Mips::SaveRaF16)).addImm(FrameSize);
183 int Base = 2040; // should create template function like isUInt that
184 // returns largest possible n bit unsigned integer
185 int64_t Remainder = FrameSize - Base;
186 BuildMI(MBB, I, DL, get(Mips::SaveRaF16)). addImm(Base);
187 if (isInt<16>(-Remainder))
188 BuildAddiuSpImm(MBB, I, -Remainder);
190 adjustStackPtrBig(SP, -Remainder, MBB, I, Mips::V0, Mips::V1);
200 MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
202 MIB1.addReg(Mips::SP);
204 MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
206 MIB2.addReg(Mips::SP);
208 MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::SwRxSpImmX16),
210 MIB3.addReg(Mips::SP);
212 adjustStackPtrBig(SP, -FrameSize, MBB, I, Mips::V0, Mips::V1);
216 // Adjust SP by FrameSize bytes. Restore RA, S0, S1
217 void Mips16InstrInfo::restoreFrame(unsigned SP, int64_t FrameSize,
218 MachineBasicBlock &MBB,
219 MachineBasicBlock::iterator I) const {
220 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
221 if (!NeverUseSaveRestore) {
222 if (isUInt<11>(FrameSize))
223 BuildMI(MBB, I, DL, get(Mips::RestoreRaF16)).addImm(FrameSize);
225 int Base = 2040; // should create template function like isUInt that
226 // returns largest possible n bit unsigned integer
227 int64_t Remainder = FrameSize - Base;
228 if (isInt<16>(Remainder))
229 BuildAddiuSpImm(MBB, I, Remainder);
231 adjustStackPtrBig(SP, Remainder, MBB, I, Mips::A0, Mips::A1);
232 BuildMI(MBB, I, DL, get(Mips::RestoreRaF16)). addImm(Base);
236 adjustStackPtrBig(SP, FrameSize, MBB, I, Mips::A0, Mips::A1);
240 MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
242 MIB1.addReg(Mips::SP);
244 MachineInstrBuilder MIB0 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
246 MIB0.addReg(Mips::A0);
247 MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
249 MIB2.addReg(Mips::SP);
251 MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::LwRxSpImmX16),
253 MIB3.addReg(Mips::SP);
259 // Adjust SP by Amount bytes where bytes can be up to 32bit number.
260 // This can only be called at times that we know that there is at least one free
262 // This is clearly safe at prologue and epilogue.
264 void Mips16InstrInfo::adjustStackPtrBig(unsigned SP, int64_t Amount,
265 MachineBasicBlock &MBB,
266 MachineBasicBlock::iterator I,
267 unsigned Reg1, unsigned Reg2) const {
268 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
269 // MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
270 // unsigned Reg1 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
271 // unsigned Reg2 = RegInfo.createVirtualRegister(&Mips::CPU16RegsRegClass);
275 // add reg1, reg1, reg2
279 MachineInstrBuilder MIB1 = BuildMI(MBB, I, DL, get(Mips::LwConstant32), Reg1);
281 MachineInstrBuilder MIB2 = BuildMI(MBB, I, DL, get(Mips::MoveR3216), Reg2);
282 MIB2.addReg(Mips::SP, RegState::Kill);
283 MachineInstrBuilder MIB3 = BuildMI(MBB, I, DL, get(Mips::AdduRxRyRz16), Reg1);
285 MIB3.addReg(Reg2, RegState::Kill);
286 MachineInstrBuilder MIB4 = BuildMI(MBB, I, DL, get(Mips::Move32R16),
288 MIB4.addReg(Reg1, RegState::Kill);
291 void Mips16InstrInfo::adjustStackPtrBigUnrestricted(unsigned SP, int64_t Amount,
292 MachineBasicBlock &MBB,
293 MachineBasicBlock::iterator I) const {
294 assert(false && "adjust stack pointer amount exceeded");
297 /// Adjust SP by Amount bytes.
298 void Mips16InstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
299 MachineBasicBlock &MBB,
300 MachineBasicBlock::iterator I) const {
301 if (isInt<16>(Amount)) // need to change to addiu sp, ....and isInt<16>
302 BuildAddiuSpImm(MBB, I, Amount);
304 adjustStackPtrBigUnrestricted(SP, Amount, MBB, I);
307 /// This function generates the sequence of instructions needed to get the
308 /// result of adding register REG and immediate IMM.
310 Mips16InstrInfo::loadImmediate(unsigned FrameReg,
311 int64_t Imm, MachineBasicBlock &MBB,
312 MachineBasicBlock::iterator II, DebugLoc DL,
313 unsigned &NewImm) const {
315 // given original instruction is:
316 // Instr rx, T[offset] where offset is too big.
318 // lo = offset & 0xFFFF
319 // hi = ((offset >> 16) + (lo >> 15)) & 0xFFFF;
321 // let T = temporary register
327 int32_t lo = Imm & 0xFFFF;
332 rs.enterBasicBlock(&MBB);
335 // We need to know which registers can be used, in the case where there
336 // are not enough free registers. We exclude all registers that
337 // are used in the instruction that we are helping.
338 // // Consider all allocatable registers in the register class initially
339 BitVector Candidates =
341 (*II->getParent()->getParent(), &Mips::CPU16RegsRegClass);
342 // Exclude all the registers being used by the instruction.
343 for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
344 MachineOperand &MO = II->getOperand(i);
345 if (MO.isReg() && MO.getReg() != 0 && !MO.isDef() &&
346 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
347 Candidates.reset(MO.getReg());
350 // If the same register was used and defined in an instruction, then
351 // it will not be in the list of candidates.
353 // we need to analyze the instruction that we are helping.
354 // we need to know if it defines register x but register x is not
355 // present as an operand of the instruction. this tells
356 // whether the register is live before the instruction. if it's not
357 // then we don't need to save it in case there are no free registers.
360 for (unsigned i = 0, e = II->getNumOperands(); i != e; ++i) {
361 MachineOperand &MO = II->getOperand(i);
362 if (MO.isReg() && MO.isDef()) {
363 DefReg = MO.getReg();
368 BitVector Available = rs.getRegsAvailable(&Mips::CPU16RegsRegClass);
370 Available &= Candidates;
372 // we use T0 for the first register, if we need to save something away.
373 // we use T1 for the second register, if we need to save something away.
375 unsigned FirstRegSaved =0, SecondRegSaved=0;
376 unsigned FirstRegSavedTo = 0, SecondRegSavedTo = 0;
379 Reg = Available.find_first();
382 Reg = Candidates.find_first();
383 Candidates.reset(Reg);
386 FirstRegSavedTo = Mips::T0;
387 copyPhysReg(MBB, II, DL, FirstRegSavedTo, FirstRegSaved, true);
391 Available.reset(Reg);
392 BuildMI(MBB, II, DL, get(Mips::LwConstant32), Reg).addImm(Imm);
394 if (FrameReg == Mips::SP) {
395 SpReg = Available.find_first();
397 SpReg = Candidates.find_first();
398 // Candidates.reset(SpReg); // not really needed
399 if (DefReg!= SpReg) {
400 SecondRegSaved = SpReg;
401 SecondRegSavedTo = Mips::T1;
404 copyPhysReg(MBB, II, DL, SecondRegSavedTo, SecondRegSaved, true);
407 Available.reset(SpReg);
408 copyPhysReg(MBB, II, DL, SpReg, Mips::SP, false);
409 BuildMI(MBB, II, DL, get(Mips:: AdduRxRyRz16), Reg).addReg(SpReg, RegState::Kill)
413 BuildMI(MBB, II, DL, get(Mips:: AdduRxRyRz16), Reg).addReg(FrameReg)
414 .addReg(Reg, RegState::Kill);
415 if (FirstRegSaved || SecondRegSaved) {
418 copyPhysReg(MBB, II, DL, FirstRegSaved, FirstRegSavedTo, true);
420 copyPhysReg(MBB, II, DL, SecondRegSaved, SecondRegSavedTo, true);
425 /// This function generates the sequence of instructions needed to get the
426 /// result of adding register REG and immediate IMM.
428 Mips16InstrInfo::basicLoadImmediate(
430 int64_t Imm, MachineBasicBlock &MBB,
431 MachineBasicBlock::iterator II, DebugLoc DL,
432 unsigned &NewImm) const {
433 const TargetRegisterClass *RC = &Mips::CPU16RegsRegClass;
434 MachineRegisterInfo &RegInfo = MBB.getParent()->getRegInfo();
435 unsigned Reg = RegInfo.createVirtualRegister(RC);
436 BuildMI(MBB, II, DL, get(Mips::LwConstant32), Reg).addImm(Imm);
441 unsigned Mips16InstrInfo::getAnalyzableBrOpc(unsigned Opc) const {
442 return (Opc == Mips::BeqzRxImmX16 || Opc == Mips::BimmX16 ||
443 Opc == Mips::Bimm16 ||
444 Opc == Mips::BnezRxImmX16 || Opc == Mips::BteqzX16 ||
445 Opc == Mips::BteqzT8CmpX16 || Opc == Mips::BteqzT8CmpiX16 ||
446 Opc == Mips::BteqzT8SltX16 || Opc == Mips::BteqzT8SltuX16 ||
447 Opc == Mips::BteqzT8SltiX16 || Opc == Mips::BteqzT8SltiuX16 ||
448 Opc == Mips::BtnezX16 || Opc == Mips::BtnezT8CmpX16 ||
449 Opc == Mips::BtnezT8CmpiX16 || Opc == Mips::BtnezT8SltX16 ||
450 Opc == Mips::BtnezT8SltuX16 || Opc == Mips::BtnezT8SltiX16 ||
451 Opc == Mips::BtnezT8SltiuX16 ) ? Opc : 0;
454 void Mips16InstrInfo::ExpandRetRA16(MachineBasicBlock &MBB,
455 MachineBasicBlock::iterator I,
456 unsigned Opc) const {
457 BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
461 const MCInstrDesc &Mips16InstrInfo::AddiuSpImm(int64_t Imm) const {
462 if (validSpImm8(Imm))
463 return get(Mips::AddiuSpImm16);
465 return get(Mips::AddiuSpImmX16);
468 void Mips16InstrInfo::BuildAddiuSpImm
469 (MachineBasicBlock &MBB, MachineBasicBlock::iterator I, int64_t Imm) const {
470 DebugLoc DL = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
471 BuildMI(MBB, I, DL, AddiuSpImm(Imm)).addImm(Imm);
474 const MipsInstrInfo *llvm::createMips16InstrInfo(MipsTargetMachine &TM) {
475 return new Mips16InstrInfo(TM);
478 bool Mips16InstrInfo::validImmediate(unsigned Opcode, unsigned Reg,
481 case Mips::LbRxRyOffMemX16:
482 case Mips::LbuRxRyOffMemX16:
483 case Mips::LhRxRyOffMemX16:
484 case Mips::LhuRxRyOffMemX16:
485 case Mips::SbRxRyOffMemX16:
486 case Mips::ShRxRyOffMemX16:
487 case Mips::LwRxRyOffMemX16:
488 case Mips::SwRxRyOffMemX16:
489 case Mips::SwRxSpImmX16:
490 case Mips::LwRxSpImmX16:
491 return isInt<16>(Amount);
492 case Mips::AddiuRxRyOffMemX16:
493 if ((Reg == Mips::PC) || (Reg == Mips::SP))
494 return isInt<16>(Amount);
495 return isInt<15>(Amount);
497 llvm_unreachable("unexpected Opcode in validImmediate");
500 /// Measure the specified inline asm to determine an approximation of its
502 /// Comments (which run till the next SeparatorString or newline) do not
503 /// count as an instruction.
504 /// Any other non-whitespace text is considered an instruction, with
505 /// multiple instructions separated by SeparatorString or newlines.
506 /// Variable-length instructions are not handled here; this function
507 /// may be overloaded in the target code to do that.
508 /// We implement the special case of the .space directive taking only an
509 /// integer argument, which is the size in bytes. This is used for creating
510 /// inline code spacing for testing purposes using inline assembly.
512 unsigned Mips16InstrInfo::getInlineAsmLength(const char *Str,
513 const MCAsmInfo &MAI) const {
516 // Count the number of instructions in the asm.
517 bool atInsnStart = true;
519 for (; *Str; ++Str) {
520 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
521 strlen(MAI.getSeparatorString())) == 0)
523 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
524 if (strncmp(Str, ".space", 6)==0) {
526 Sz = strtol(Str+6, &EStr, 10);
527 while (isspace(*EStr)) ++EStr;
529 DEBUG(dbgs() << "parsed .space " << Sz << '\n');
533 Length += MAI.getMaxInstLength();
536 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
537 strlen(MAI.getCommentString())) == 0)