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,
121 int *BytesAdded) const {
122 // Shouldn't be a fall through.
123 assert(TBB && "insertBranch must not be told to insert a fallthrough");
124 assert(!BytesAdded && "code size not handled");
126 // # of condition operands:
127 // Unconditional branches: 0
128 // Floating point branches: 1 (opc)
129 // Int BranchZero: 2 (opc, reg)
130 // Int Branch: 3 (opc, reg0, reg1)
131 assert((Cond.size() <= 3) &&
132 "# of Mips branch conditions must be <= 3!");
134 // Two-way Conditional branch.
136 BuildCondBr(MBB, TBB, DL, Cond);
137 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(FBB);
142 // Unconditional branch.
144 BuildMI(&MBB, DL, get(UncondBrOpc)).addMBB(TBB);
145 else // Conditional branch.
146 BuildCondBr(MBB, TBB, DL, Cond);
150 unsigned MipsInstrInfo::removeBranch(MachineBasicBlock &MBB,
151 int *BytesRemoved) const {
152 assert(!BytesRemoved && "code size not handled");
154 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
157 // Skip all the debug instructions.
158 while (I != REnd && I->isDebugValue())
164 MachineBasicBlock::iterator FirstBr = ++I.getReverse();
166 // Up to 2 branches are removed.
167 // Note that indirect branches are not removed.
168 for (removed = 0; I != REnd && removed < 2; ++I, ++removed)
169 if (!getAnalyzableBrOpc(I->getOpcode()))
172 MBB.erase((--I).getReverse(), FirstBr);
177 /// reverseBranchCondition - Return the inverse opcode of the
178 /// specified Branch instruction.
179 bool MipsInstrInfo::reverseBranchCondition(
180 SmallVectorImpl<MachineOperand> &Cond) const {
181 assert( (Cond.size() && Cond.size() <= 3) &&
182 "Invalid Mips branch condition!");
183 Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
187 MipsInstrInfo::BranchType MipsInstrInfo::analyzeBranch(
188 MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB,
189 SmallVectorImpl<MachineOperand> &Cond, bool AllowModify,
190 SmallVectorImpl<MachineInstr *> &BranchInstrs) const {
192 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
194 // Skip all the debug instructions.
195 while (I != REnd && I->isDebugValue())
198 if (I == REnd || !isUnpredicatedTerminator(*I)) {
199 // This block ends with no branches (it just falls through to its succ).
200 // Leave TBB/FBB null.
205 MachineInstr *LastInst = &*I;
206 unsigned LastOpc = LastInst->getOpcode();
207 BranchInstrs.push_back(LastInst);
209 // Not an analyzable branch (e.g., indirect jump).
210 if (!getAnalyzableBrOpc(LastOpc))
211 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
213 // Get the second to last instruction in the block.
214 unsigned SecondLastOpc = 0;
215 MachineInstr *SecondLastInst = nullptr;
218 SecondLastInst = &*I;
219 SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
221 // Not an analyzable branch (must be an indirect jump).
222 if (isUnpredicatedTerminator(*SecondLastInst) && !SecondLastOpc)
226 // If there is only one terminator instruction, process it.
227 if (!SecondLastOpc) {
228 // Unconditional branch.
229 if (LastInst->isUnconditionalBranch()) {
230 TBB = LastInst->getOperand(0).getMBB();
234 // Conditional branch
235 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
239 // If we reached here, there are two branches.
240 // If there are three terminators, we don't know what sort of block this is.
241 if (++I != REnd && isUnpredicatedTerminator(*I))
244 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
246 // If second to last instruction is an unconditional branch,
247 // analyze it and remove the last instruction.
248 if (SecondLastInst->isUnconditionalBranch()) {
249 // Return if the last instruction cannot be removed.
253 TBB = SecondLastInst->getOperand(0).getMBB();
254 LastInst->eraseFromParent();
255 BranchInstrs.pop_back();
259 // Conditional branch followed by an unconditional branch.
260 // The last one must be unconditional.
261 if (!LastInst->isUnconditionalBranch())
264 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
265 FBB = LastInst->getOperand(0).getMBB();
267 return BT_CondUncond;
270 /// Return the corresponding compact (no delay slot) form of a branch.
271 unsigned MipsInstrInfo::getEquivalentCompactForm(
272 const MachineBasicBlock::iterator I) const {
273 unsigned Opcode = I->getOpcode();
274 bool canUseShortMicroMipsCTI = false;
276 if (Subtarget.inMicroMipsMode()) {
282 // microMIPS has NE,EQ branches that do not have delay slots provided one
283 // of the operands is zero.
284 if (I->getOperand(1).getReg() == Subtarget.getABI().GetZeroReg())
285 canUseShortMicroMipsCTI = true;
287 // For microMIPS the PseudoReturn and PseudoIndirectBranch are always
288 // expanded to JR_MM, so they can be replaced with JRC16_MM.
290 case Mips::PseudoReturn:
291 case Mips::PseudoIndirectBranch:
292 case Mips::TAILCALLREG:
293 canUseShortMicroMipsCTI = true;
298 // MIPSR6 forbids both operands being the zero register.
299 if (Subtarget.hasMips32r6() && (I->getNumOperands() > 1) &&
300 (I->getOperand(0).isReg() &&
301 (I->getOperand(0).getReg() == Mips::ZERO ||
302 I->getOperand(0).getReg() == Mips::ZERO_64)) &&
303 (I->getOperand(1).isReg() &&
304 (I->getOperand(1).getReg() == Mips::ZERO ||
305 I->getOperand(1).getReg() == Mips::ZERO_64)))
308 if (Subtarget.hasMips32r6() || canUseShortMicroMipsCTI) {
316 if (canUseShortMicroMipsCTI)
317 return Mips::BEQZC_MM;
318 else if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
323 if (canUseShortMicroMipsCTI)
324 return Mips::BNEZC_MM;
325 else if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
329 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
333 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
343 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
347 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
353 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
357 if (I->getOperand(0).getReg() == I->getOperand(1).getReg())
361 return Mips::BGTZC64;
363 return Mips::BGEZC64;
365 return Mips::BLTZC64;
367 return Mips::BLEZC64;
368 // For MIPSR6, the instruction 'jic' can be used for these cases. Some
369 // tools will accept 'jrc reg' as an alias for 'jic 0, $reg'.
371 case Mips::PseudoReturn:
372 case Mips::PseudoIndirectBranch:
373 case Mips::TAILCALLREG:
374 if (canUseShortMicroMipsCTI)
375 return Mips::JRC16_MM;
377 case Mips::JALRPseudo:
380 case Mips::PseudoReturn64:
381 case Mips::PseudoIndirectBranch64:
382 case Mips::TAILCALLREG64:
384 case Mips::JALR64Pseudo:
385 return Mips::JIALC64;
394 /// Predicate for distingushing between control transfer instructions and all
395 /// other instructions for handling forbidden slots. Consider inline assembly
396 /// as unsafe as well.
397 bool MipsInstrInfo::SafeInForbiddenSlot(const MachineInstr &MI) const {
398 if (MI.isInlineAsm())
401 return (MI.getDesc().TSFlags & MipsII::IsCTI) == 0;
405 /// Predicate for distingushing instructions that have forbidden slots.
406 bool MipsInstrInfo::HasForbiddenSlot(const MachineInstr &MI) const {
407 return (MI.getDesc().TSFlags & MipsII::HasForbiddenSlot) != 0;
410 /// Return the number of bytes of code the specified instruction may be.
411 unsigned MipsInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
412 switch (MI.getOpcode()) {
414 return MI.getDesc().getSize();
415 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
416 const MachineFunction *MF = MI.getParent()->getParent();
417 const char *AsmStr = MI.getOperand(0).getSymbolName();
418 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
420 case Mips::CONSTPOOL_ENTRY:
421 // If this machine instr is a constant pool entry, its size is recorded as
423 return MI.getOperand(2).getImm();
428 MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
429 MachineBasicBlock::iterator I) const {
430 MachineInstrBuilder MIB;
432 // Certain branches have two forms: e.g beq $1, $zero, dest vs beqz $1, dest
433 // Pick the zero form of the branch for readable assembly and for greater
434 // branch distance in non-microMIPS mode.
435 // Additional MIPSR6 does not permit the use of register $zero for compact
437 // FIXME: Certain atomic sequences on mips64 generate 32bit references to
438 // Mips::ZERO, which is incorrect. This test should be updated to use
439 // Subtarget.getABI().GetZeroReg() when those atomic sequences and others
441 int ZeroOperandPosition = -1;
442 bool BranchWithZeroOperand = false;
443 if (I->isBranch() && !I->isPseudo()) {
444 auto TRI = I->getParent()->getParent()->getSubtarget().getRegisterInfo();
445 ZeroOperandPosition = I->findRegisterUseOperandIdx(Mips::ZERO, false, TRI);
446 BranchWithZeroOperand = ZeroOperandPosition != -1;
449 if (BranchWithZeroOperand) {
452 NewOpc = Mips::BEQZC;
455 NewOpc = Mips::BNEZC;
458 NewOpc = Mips::BGEZC;
461 NewOpc = Mips::BLTZC;
464 NewOpc = Mips::BEQZC64;
467 NewOpc = Mips::BNEZC64;
472 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
474 // For MIPSR6 JI*C requires an immediate 0 as an operand, JIALC(64) an
475 // immediate 0 as an operand and requires the removal of it's %RA<imp-def>
476 // implicit operand as copying the implicit operations of the instructio we're
477 // looking at will give us the correct flags.
478 if (NewOpc == Mips::JIC || NewOpc == Mips::JIALC || NewOpc == Mips::JIC64 ||
479 NewOpc == Mips::JIALC64) {
481 if (NewOpc == Mips::JIALC || NewOpc == Mips::JIALC64)
482 MIB->RemoveOperand(0);
484 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) {
485 MIB.addOperand(I->getOperand(J));
491 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J) {
492 if (BranchWithZeroOperand && (unsigned)ZeroOperandPosition == J)
495 MIB.addOperand(I->getOperand(J));
499 MIB.copyImplicitOps(*I);
501 MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());