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 "MipsAnalyzeImmediate.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsTargetMachine.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/TargetRegistry.h"
27 #define GET_INSTRINFO_CTOR_DTOR
28 #include "MipsGenInstrInfo.inc"
30 // Pin the vtable to this file.
31 void MipsInstrInfo::anchor() {}
33 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm, unsigned UncondBr)
34 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
35 TM(tm), UncondBrOpc(UncondBr) {}
37 const MipsInstrInfo *MipsInstrInfo::create(MipsTargetMachine &TM) {
38 if (TM.getSubtargetImpl()->inMips16Mode())
39 return llvm::createMips16InstrInfo(TM);
41 return llvm::createMipsSEInstrInfo(TM);
44 bool MipsInstrInfo::isZeroImm(const MachineOperand &op) const {
45 return op.isImm() && op.getImm() == 0;
48 /// insertNoop - If data hazard condition is found insert the target nop
51 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
54 BuildMI(MBB, MI, DL, get(Mips::NOP));
57 MachineMemOperand *MipsInstrInfo::GetMemOperand(MachineBasicBlock &MBB, int FI,
58 unsigned Flag) const {
59 MachineFunction &MF = *MBB.getParent();
60 MachineFrameInfo &MFI = *MF.getFrameInfo();
61 unsigned Align = MFI.getObjectAlignment(FI);
63 return MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), Flag,
64 MFI.getObjectSize(FI), Align);
67 //===----------------------------------------------------------------------===//
69 //===----------------------------------------------------------------------===//
71 void MipsInstrInfo::AnalyzeCondBr(const MachineInstr *Inst, unsigned Opc,
72 MachineBasicBlock *&BB,
73 SmallVectorImpl<MachineOperand> &Cond) const {
74 assert(getAnalyzableBrOpc(Opc) && "Not an analyzable branch");
75 int NumOp = Inst->getNumExplicitOperands();
77 // for both int and fp branches, the last explicit operand is the
79 BB = Inst->getOperand(NumOp-1).getMBB();
80 Cond.push_back(MachineOperand::CreateImm(Opc));
82 for (int i=0; i<NumOp-1; i++)
83 Cond.push_back(Inst->getOperand(i));
86 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
87 MachineBasicBlock *&TBB,
88 MachineBasicBlock *&FBB,
89 SmallVectorImpl<MachineOperand> &Cond,
90 bool AllowModify) const {
91 SmallVector<MachineInstr*, 2> BranchInstrs;
92 BranchType BT = AnalyzeBranch(MBB, TBB, FBB, Cond, AllowModify, BranchInstrs);
94 return (BT == BT_None) || (BT == BT_Indirect);
97 void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
98 MachineBasicBlock *TBB, DebugLoc DL,
99 const SmallVectorImpl<MachineOperand>& Cond)
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(true && "Cannot copy operand");
116 unsigned MipsInstrInfo::
117 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
118 MachineBasicBlock *FBB,
119 const SmallVectorImpl<MachineOperand> &Cond,
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::
149 RemoveBranch(MachineBasicBlock &MBB) const
151 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
152 MachineBasicBlock::reverse_iterator FirstBr;
155 // Skip all the debug instructions.
156 while (I != REnd && I->isDebugValue())
161 // Up to 2 branches are removed.
162 // Note that indirect branches are not removed.
163 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
164 if (!getAnalyzableBrOpc(I->getOpcode()))
167 MBB.erase(I.base(), FirstBr.base());
172 /// ReverseBranchCondition - Return the inverse opcode of the
173 /// specified Branch instruction.
175 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
177 assert( (Cond.size() && Cond.size() <= 3) &&
178 "Invalid Mips branch condition!");
179 Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
183 MipsInstrInfo::BranchType MipsInstrInfo::
184 AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
185 MachineBasicBlock *&FBB, SmallVectorImpl<MachineOperand> &Cond,
187 SmallVectorImpl<MachineInstr*> &BranchInstrs) const {
189 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
191 // Skip all the debug instructions.
192 while (I != REnd && I->isDebugValue())
195 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
196 // This block ends with no branches (it just falls through to its succ).
197 // Leave TBB/FBB null.
202 MachineInstr *LastInst = &*I;
203 unsigned LastOpc = LastInst->getOpcode();
204 BranchInstrs.push_back(LastInst);
206 // Not an analyzable branch (e.g., indirect jump).
207 if (!getAnalyzableBrOpc(LastOpc))
208 return LastInst->isIndirectBranch() ? BT_Indirect : BT_None;
210 // Get the second to last instruction in the block.
211 unsigned SecondLastOpc = 0;
212 MachineInstr *SecondLastInst = nullptr;
215 SecondLastInst = &*I;
216 SecondLastOpc = getAnalyzableBrOpc(SecondLastInst->getOpcode());
218 // Not an analyzable branch (must be an indirect jump).
219 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
223 // If there is only one terminator instruction, process it.
224 if (!SecondLastOpc) {
225 // Unconditional branch.
226 if (LastOpc == UncondBrOpc) {
227 TBB = LastInst->getOperand(0).getMBB();
231 // Conditional branch
232 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
236 // If we reached here, there are two branches.
237 // If there are three terminators, we don't know what sort of block this is.
238 if (++I != REnd && isUnpredicatedTerminator(&*I))
241 BranchInstrs.insert(BranchInstrs.begin(), SecondLastInst);
243 // If second to last instruction is an unconditional branch,
244 // analyze it and remove the last instruction.
245 if (SecondLastOpc == UncondBrOpc) {
246 // Return if the last instruction cannot be removed.
250 TBB = SecondLastInst->getOperand(0).getMBB();
251 LastInst->eraseFromParent();
252 BranchInstrs.pop_back();
256 // Conditional branch followed by an unconditional branch.
257 // The last one must be unconditional.
258 if (LastOpc != UncondBrOpc)
261 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
262 FBB = LastInst->getOperand(0).getMBB();
264 return BT_CondUncond;
267 /// Return the number of bytes of code the specified instruction may be.
268 unsigned MipsInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
269 switch (MI->getOpcode()) {
271 return MI->getDesc().getSize();
272 case TargetOpcode::INLINEASM: { // Inline Asm: Variable size.
273 const MachineFunction *MF = MI->getParent()->getParent();
274 const char *AsmStr = MI->getOperand(0).getSymbolName();
275 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
277 case Mips::CONSTPOOL_ENTRY:
278 // If this machine instr is a constant pool entry, its size is recorded as
280 return MI->getOperand(2).getImm();
285 MipsInstrInfo::genInstrWithNewOpc(unsigned NewOpc,
286 MachineBasicBlock::iterator I) const {
287 MachineInstrBuilder MIB;
288 MIB = BuildMI(*I->getParent(), I, I->getDebugLoc(), get(NewOpc));
290 for (unsigned J = 0, E = I->getDesc().getNumOperands(); J < E; ++J)
291 MIB.addOperand(I->getOperand(J));
293 MIB.setMemRefs(I->memoperands_begin(), I->memoperands_end());