1 //===-- MipsInstrInfo.cpp - Mips 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 Mips implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "MipsInstrInfo.h"
15 #include "InstPrinter/MipsInstPrinter.h"
16 #include "MipsMachineFunction.h"
17 #include "MipsSubtarget.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/TargetRegistry.h"
26 #define GET_INSTRINFO_CTOR_DTOR
27 #include "MipsGenInstrInfo.inc"
29 // Pin the vtable to this file.
30 void MipsInstrInfo::anchor() {}
32 MipsInstrInfo::MipsInstrInfo(const MipsSubtarget &STI, unsigned UncondBr)
33 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
34 Subtarget(STI), UncondBrOpc(UncondBr) {}
36 const MipsInstrInfo *MipsInstrInfo::create(MipsSubtarget &STI) {
37 if (STI.inMips16Mode())
38 return llvm::createMips16InstrInfo(STI);
40 return llvm::createMipsSEInstrInfo(STI);
43 bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
44 return op.isImm() && op.getImm() == 0;
47 /// insertNoop - If data hazard condition is found insert the target nop
49 // FIXME: This appears to be dead code.
51 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
54 BuildMI(MBB, MI, DL, get(Mips::NOP));
58 MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
59 MachineMemOperand::Flags Flags) const {
60 MachineFunction &MF = *MBB.getParent();
61 MachineFrameInfo &MFI = *MF.getFrameInfo();
62 unsigned Align = MFI.getObjectAlignment(FI);
64 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, FI),
65 Flags, MFI.getObjectSize(FI), Align);
68 //===----------------------------------------------------------------------===//
70 //===----------------------------------------------------------------------===//
72 void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
73 MachineBasicBlock *&BB,
74 SmallVectorImpl<MachineOperand> &Cond) const {
75 assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch");
76 int NumOp = Inst->getNumExplicitOperands();
78 // for both int and fp branches, the last explicit operand is the
80 BB = Inst->getOperand(NumOp-1).getMBB();
81 Cond.push_back(MachineOperand::CreateImm(Opc));
83 for (int i=0; i<NumOp-1; i++)
84 Cond.push_back(Inst->getOperand(i));
87 bool MipsInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
88 MachineBasicBlock *&TBB,
89 MachineBasicBlock *&FBB,
90 SmallVectorImpl<MachineOperand> &Cond,
91 bool AllowModify) const {
92 SmallVector<MachineInstr*, 2> BranchInstrs;
93 BranchType BT = analyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
95 return (BT == BT_None) || (BT == BT_Indirect);
98 void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
100 ArrayRef<MachineOperand> Cond) const {
101 unsigned Opc = Cond[0].getImm();
102 const MCInstrDesc &MCID = get(Opc);
103 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
105 for (unsigned i = 1; i < Cond.size(); ++i) {
107 MIB.addReg(Cond[i].getReg());
108 else if (Cond[i].isImm())
109 MIB.addImm(Cond[i].getImm());
111 assert(false && "Cannot copy operand");
116 unsigned MipsInstrInfo::InsertBranch(MachineBasicBlock &MBB,
117 MachineBasicBlock *TBB,
118 MachineBasicBlock *FBB,
119 ArrayRef<MachineOperand> Cond,
120 const DebugLoc &DL) const {
121 // Shouldn't be a fall through.
122 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
124 // # of condition operands:
125 // Unconditional branches: 0
126 // Floating point branches: 1 (opc)
127 // Int BranchZero: 2 (opc, reg)
128 // Int Branch: 3 (opc, reg0, reg1)
129 assert((Cond.size() <= 3) &&
130 "# of Mips branch conditions must be <= 3!");
132 // Two-way Conditional branch.
134 BuildCondBr(MBB, TBB, DL, Cond);
135 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
140 // Unconditional branch.
142 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
143 else // Conditional branch.
144 BuildCondBr(MBB, TBB, DL, Cond);
148 unsigned MipsInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
149 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
150 MachineBasicBlock::reverse_iterator FirstBr;
153 // Skip all the debug instructions.
154 while (I != REnd && I->isDebugValue())
159 // Up to 2 branches are removed.
160 // Note that indirect branches are not removed.
161 for (removed = 0; I != REnd && removed < 2; ++I, ++removed)
162 if (!getAnalyzableBrOpc(I->getOpcode()))
165 MBB.erase(I.base(), FirstBr.base());
170 /// ReverseBranchCondition - Return the inverse opcode of the
171 /// specified Branch instruction.
172 bool MipsInstrInfo::ReverseBranchCondition(
173 SmallVectorImpl<MachineOperand> &Cond) const {
174 assert( (Cond.size() && Cond.size() <= 3) &&
175 "Invalid Mips branch condition!");
176 Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
180 MipsInstrInfo::BranchType MipsInstrInfo::analyzeBranch(
181 MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
182 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify,
183 SmallVectorImpl<MachineInstr *> &BranchInstrs) const {
185 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
187 // Skip all the debug instructions.
188 while (I != REnd && I->isDebugValue())
191 if (I == REnd || !isUnpredicatedTerminator(*I)) {
192 // This block ends with no branches (it just falls through to its succ).
193 // Leave TBB/FBB null.
198 MachineInstr *LastInst = &*I;
199 unsigned LastOpc = LastInst->getOpcode();
200 BranchInstrs.push_back(LastInst);
202 // Not an analyzable branch (e.g., indirect jump).
203 if (!getAnalyzableBrOpc(LastOpc))
204 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
206 // Get the second to last instruction in the block.
207 unsigned SecondLastOpc = 0;
208 MachineInstr *SecondLastInst = nullptr;
211 SecondLastInst = &*I;
212 SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
214 // Not an analyzable branch (must be an indirect jump).
215 if (isUnpredicatedTerminator(*SecondLastInst) && !SecondLastOpc)
219 // If there is only one terminator instruction, process it.
220 if (!SecondLastOpc) {
221 // Unconditional branch.
222 if (LastInst->isUnconditionalBranch()) {
223 TBB = LastInst->getOperand(0).getMBB();
227 // Conditional branch
228 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
232 // If we reached here, there are two branches.
233 // If there are three terminators, we don't know what sort of block this is.
234 if (++I != REnd && isUnpredicatedTerminator(*I))
237 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
239 // If second to last instruction is an unconditional branch,
240 // analyze it and remove the last instruction.
241 if (SecondLastInst->isUnconditionalBranch()) {
242 // Return if the last instruction cannot be removed.
246 TBB = SecondLastInst->getOperand(0).getMBB();
247 LastInst->eraseFromParent();
248 BranchInstrs.pop_back();
252 // Conditional branch followed by an unconditional branch.
253 // The last one must be unconditional.
254 if (!LastInst->isUnconditionalBranch())
257 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
258 FBB = LastInst->getOperand(0).getMBB();
260 return BT_CondUncond;
263 /// Return the corresponding compact (no delay slot) form of a branch.
264 unsigned MipsInstrInfo::getEquivalentCompactForm(
265 const MachineBasicBlock::iterator I) const {
266 unsigned Opcode = I->getOpcode();
267 bool canUseShortMicroMipsCTI = false;
269 if (Subtarget.inMicroMipsMode()) {
273 // microMIPS has NE,EQ branches that do not have delay slots provided one
274 // of the operands is zero.
275 if (I->getOperand(1).getReg() == Subtarget.getABI().GetZeroReg())
276 canUseShortMicroMipsCTI = true;
278 // For microMIPS the PseudoReturn and PseudoIndirectBranch are always
279 // expanded to JR_MM, so they can be replaced with JRC16_MM.
281 case Mips::PseudoReturn:
282 case Mips::PseudoIndirectBranch:
283 canUseShortMicroMipsCTI = true;
288 // MIPSR6 forbids both operands being the zero register.
289 if (Subtarget.hasMips32r6() && (I->getNumOperands() > 1) &&
290 (I->getOperand(0).isReg() &&
291 (I->getOperand(0).getReg() == Mips::ZERO ||
292 I->getOperand(0).getReg() == Mips::ZERO_64)) &&
293 (I->getOperand(1).isReg() &&
294 (I->getOperand(1).getReg() == Mips::ZERO ||
295 I->getOperand(1).getReg() == Mips::ZERO_64)))
298 if (Subtarget.hasMips32r6() || canUseShortMicroMipsCTI) {
305 if (canUseShortMicroMipsCTI)
306 return Mips::BEQZC_MM;
307 else if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
311 if (canUseShortMicroMipsCTI)
312 return Mips::BNEZC_MM;
313 else if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
317 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
321 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
331 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
335 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
340 // For MIPSR6, the instruction 'jic' can be used for these cases. Some
341 // tools will accept 'jrc reg' as an alias for 'jic 0, $reg'.
343 case Mips::PseudoReturn:
344 case Mips::PseudoIndirectBranch:
345 if (canUseShortMicroMipsCTI)
346 return Mips::JRC16_MM;
348 case Mips::JALRPseudo:
351 case Mips::PseudoReturn64:
352 case Mips::PseudoIndirectBranch64:
354 case Mips::JALR64Pseudo:
355 return Mips::JIALC64;
364 /// Predicate for distingushing between control transfer instructions and all
365 /// other instructions for handling forbidden slots. Consider inline assembly
366 /// as unsafe as well.
367 bool MipsInstrInfo::SafeInForbiddenSlot(const MachineInstr &MI) const {
368 if (MI.isInlineAsm())
371 return (MI.getDesc().TSFlags & MipsII::IsCTI) == 0;
375 /// Predicate for distingushing instructions that have forbidden slots.
376 bool MipsInstrInfo::HasForbiddenSlot(const MachineInstr &MI) const {
377 return (MI.getDesc().TSFlags & MipsII::HasForbiddenSlot) != 0;
380 /// Return the number of bytes of code the specified instruction may be.
381 unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr &MI) const {
382 switch (MI.getOpcode()) {
384 return MI.getDesc().getSize();
385 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
386 const MachineFunction *MF = MI.getParent()->getParent();
387 const char *AsmStr = MI.getOperand(0).getSymbolName();
388 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
390 case Mips::CONSTPOOL_ENTRY:
391 // If this machine instr is a constant pool entry, its size is recorded as
393 return MI.getOperand(2).getImm();
398 MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
399 MachineBasicBlock::iterator I) const {
400 MachineInstrBuilder MIB;
402 // Certain branches have two forms: e.g beq $1, $zero, dst vs beqz $1, dest
403 // Pick the zero form of the branch for readable assembly and for greater
404 // branch distance in non-microMIPS mode.
405 // FIXME: Certain atomic sequences on mips64 generate 32bit references to
406 // Mips::ZERO, which is incorrect. This test should be updated to use
407 // Subtarget.getABI().GetZeroReg() when those atomic sequences and others
409 bool BranchWithZeroOperand =
410 (I->isBranch() && !I->isPseudo() && I->getOperand(1).isReg() &&
411 (I->getOperand(1).getReg() == Mips::ZERO ||
412 I->getOperand(1).getReg() == Mips::ZERO_64));
414 if (BranchWithZeroOperand) {
417 NewOpc = Mips::BEQZC;
420 NewOpc = Mips::BNEZC;
423 NewOpc = Mips::BGEZC;
426 NewOpc = Mips::BLTZC;
431 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
433 // For MIPSR6 JI*C requires an immediate 0 as an operand, JIALC(64) an
434 // immediate 0 as an operand and requires the removal of it's %RA<imp-def>
435 // implicit operand as copying the implicit operations of the instructio we're
436 // looking at will give us the correct flags.
437 if (NewOpc == Mips::JIC || NewOpc == Mips::JIALC || NewOpc == Mips::JIC64 ||
438 NewOpc == Mips::JIALC64) {
440 if (NewOpc == Mips::JIALC || NewOpc == Mips::JIALC64)
441 MIB->RemoveOperand(0);
443 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) {
444 MIB.addOperand(I->getOperand(J));
449 } else if (BranchWithZeroOperand) {
450 // For MIPSR6 and microMIPS branches with an explicit zero operand, copy
451 // everything after the zero.
452 MIB.addOperand(I->getOperand(0));
454 for (unsigned J = 2, E = I->getDesc().getNumOperands(); J < E; ++J) {
455 MIB.addOperand(I->getOperand(J));
458 // All other cases copy all other operands.
459 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) {
460 MIB.addOperand(I->getOperand(J));
464 MIB.copyImplicitOps(*I);
466 MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());