1 //===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===//
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 "MipsTargetMachine.h"
16 #include "MipsMachineFunction.h"
17 #include "InstPrinter/MipsInstPrinter.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/ADT/STLExtras.h"
24 #define GET_INSTRINFO_CTOR
25 #include "MipsGenInstrInfo.inc"
29 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
30 : MipsGenInstrInfo(Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
31 TM(tm), IsN64(TM.getSubtarget<MipsSubtarget>().isABI_N64()),
32 RI(*TM.getSubtargetImpl(), *this) {}
35 const MipsRegisterInfo &MipsInstrInfo::getRegisterInfo() const {
39 static bool isZeroImm(const MachineOperand &op) {
40 return op.isImm() && op.getImm() == 0;
43 /// isLoadFromStackSlot - If the specified machine instruction is a direct
44 /// load from a stack slot, return the virtual or physical register number of
45 /// the destination along with the FrameIndex of the loaded stack slot. If
46 /// not, return 0. This predicate must return 0 if the instruction has
47 /// any side effects other than loading from the stack slot.
48 unsigned MipsInstrInfo::
49 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
51 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
52 (MI->getOpcode() == Mips::LDC1)) {
53 if ((MI->getOperand(1).isFI()) && // is a stack slot
54 (MI->getOperand(2).isImm()) && // the imm is zero
55 (isZeroImm(MI->getOperand(2)))) {
56 FrameIndex = MI->getOperand(1).getIndex();
57 return MI->getOperand(0).getReg();
64 /// isStoreToStackSlot - If the specified machine instruction is a direct
65 /// store to a stack slot, return the virtual or physical register number of
66 /// the source reg along with the FrameIndex of the loaded stack slot. If
67 /// not, return 0. This predicate must return 0 if the instruction has
68 /// any side effects other than storing to the stack slot.
69 unsigned MipsInstrInfo::
70 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
72 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
73 (MI->getOpcode() == Mips::SDC1)) {
74 if ((MI->getOperand(1).isFI()) && // is a stack slot
75 (MI->getOperand(2).isImm()) && // the imm is zero
76 (isZeroImm(MI->getOperand(2)))) {
77 FrameIndex = MI->getOperand(1).getIndex();
78 return MI->getOperand(0).getReg();
84 /// insertNoop - If data hazard condition is found insert the target nop
87 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
90 BuildMI(MBB, MI, DL, get(Mips::NOP));
94 copyPhysReg(MachineBasicBlock &MBB,
95 MachineBasicBlock::iterator I, DebugLoc DL,
96 unsigned DestReg, unsigned SrcReg,
98 unsigned Opc = 0, ZeroReg = 0;
100 if (Mips::CPURegsRegClass.contains(DestReg)) { // Copy to CPU Reg.
101 if (Mips::CPURegsRegClass.contains(SrcReg))
102 Opc = Mips::ADDu, ZeroReg = Mips::ZERO;
103 else if (Mips::CCRRegClass.contains(SrcReg))
105 else if (Mips::FGR32RegClass.contains(SrcReg))
107 else if (SrcReg == Mips::HI)
108 Opc = Mips::MFHI, SrcReg = 0;
109 else if (SrcReg == Mips::LO)
110 Opc = Mips::MFLO, SrcReg = 0;
112 else if (Mips::CPURegsRegClass.contains(SrcReg)) { // Copy from CPU Reg.
113 if (Mips::CCRRegClass.contains(DestReg))
115 else if (Mips::FGR32RegClass.contains(DestReg))
117 else if (DestReg == Mips::HI)
118 Opc = Mips::MTHI, DestReg = 0;
119 else if (DestReg == Mips::LO)
120 Opc = Mips::MTLO, DestReg = 0;
122 else if (Mips::FGR32RegClass.contains(DestReg, SrcReg))
124 else if (Mips::AFGR64RegClass.contains(DestReg, SrcReg))
125 Opc = Mips::FMOV_D32;
126 else if (Mips::CCRRegClass.contains(DestReg, SrcReg))
127 Opc = Mips::MOVCCRToCCR;
128 else if (Mips::CPU64RegsRegClass.contains(DestReg)) { // Copy to CPU64 Reg.
129 if (Mips::CPU64RegsRegClass.contains(SrcReg))
130 Opc = Mips::DADDu, ZeroReg = Mips::ZERO_64;
131 else if (SrcReg == Mips::HI64)
132 Opc = Mips::MFHI64, SrcReg = 0;
133 else if (SrcReg == Mips::LO64)
134 Opc = Mips::MFLO64, SrcReg = 0;
136 else if (Mips::CPU64RegsRegClass.contains(SrcReg)) { // Copy from CPU64 Reg.
137 if (DestReg == Mips::HI64)
138 Opc = Mips::MTHI64, DestReg = 0;
139 else if (DestReg == Mips::LO64)
140 Opc = Mips::MTLO64, DestReg = 0;
143 assert(Opc && "Cannot copy registers");
145 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc));
148 MIB.addReg(DestReg, RegState::Define);
154 MIB.addReg(SrcReg, getKillRegState(KillSrc));
158 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
159 unsigned SrcReg, bool isKill, int FI,
160 const TargetRegisterClass *RC,
161 const TargetRegisterInfo *TRI) const {
163 if (I != MBB.end()) DL = I->getDebugLoc();
166 if (RC == Mips::CPURegsRegisterClass)
167 Opc = IsN64 ? Mips::SW_P8 : Mips::SW;
168 else if (RC == Mips::CPU64RegsRegisterClass)
169 Opc = IsN64 ? Mips::SD_P8 : Mips::SD;
170 else if (RC == Mips::FGR32RegisterClass)
172 else if (RC == Mips::AFGR64RegisterClass)
175 assert(Opc && "Register class not handled!");
176 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
177 .addFrameIndex(FI).addImm(0);
181 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
182 unsigned DestReg, int FI,
183 const TargetRegisterClass *RC,
184 const TargetRegisterInfo *TRI) const
187 if (I != MBB.end()) DL = I->getDebugLoc();
190 if (RC == Mips::CPURegsRegisterClass)
191 Opc = IsN64 ? Mips::LW_P8 : Mips::LW;
192 else if (RC == Mips::CPU64RegsRegisterClass)
193 Opc = IsN64 ? Mips::LD_P8 : Mips::LD;
194 else if (RC == Mips::FGR32RegisterClass)
196 else if (RC == Mips::AFGR64RegisterClass)
199 assert(Opc && "Register class not handled!");
200 BuildMI(MBB, I, DL, get(Opc), DestReg).addFrameIndex(FI).addImm(0);
204 MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
205 uint64_t Offset, const MDNode *MDPtr,
207 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
208 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
212 //===----------------------------------------------------------------------===//
214 //===----------------------------------------------------------------------===//
216 static unsigned GetAnalyzableBrOpc(unsigned Opc) {
217 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
218 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
219 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0;
222 /// GetOppositeBranchOpc - Return the inverse of the specified
223 /// opcode, e.g. turning BEQ to BNE.
224 unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
227 default: llvm_unreachable("Illegal opcode!");
228 case Mips::BEQ : return Mips::BNE;
229 case Mips::BNE : return Mips::BEQ;
230 case Mips::BGTZ : return Mips::BLEZ;
231 case Mips::BGEZ : return Mips::BLTZ;
232 case Mips::BLTZ : return Mips::BGEZ;
233 case Mips::BLEZ : return Mips::BGTZ;
234 case Mips::BC1T : return Mips::BC1F;
235 case Mips::BC1F : return Mips::BC1T;
239 static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
240 MachineBasicBlock *&BB,
241 SmallVectorImpl<MachineOperand>& Cond) {
242 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
243 int NumOp = Inst->getNumExplicitOperands();
245 // for both int and fp branches, the last explicit operand is the
247 BB = Inst->getOperand(NumOp-1).getMBB();
248 Cond.push_back(MachineOperand::CreateImm(Opc));
250 for (int i=0; i<NumOp-1; i++)
251 Cond.push_back(Inst->getOperand(i));
254 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
255 MachineBasicBlock *&TBB,
256 MachineBasicBlock *&FBB,
257 SmallVectorImpl<MachineOperand> &Cond,
258 bool AllowModify) const
260 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
262 // Skip all the debug instructions.
263 while (I != REnd && I->isDebugValue())
266 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
267 // If this block ends with no branches (it just falls through to its succ)
268 // just return false, leaving TBB/FBB null.
273 MachineInstr *LastInst = &*I;
274 unsigned LastOpc = LastInst->getOpcode();
276 // Not an analyzable branch (must be an indirect jump).
277 if (!GetAnalyzableBrOpc(LastOpc))
280 // Get the second to last instruction in the block.
281 unsigned SecondLastOpc = 0;
282 MachineInstr *SecondLastInst = NULL;
285 SecondLastInst = &*I;
286 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
288 // Not an analyzable branch (must be an indirect jump).
289 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
293 // If there is only one terminator instruction, process it.
294 if (!SecondLastOpc) {
295 // Unconditional branch
296 if (LastOpc == Mips::J) {
297 TBB = LastInst->getOperand(0).getMBB();
301 // Conditional branch
302 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
306 // If we reached here, there are two branches.
307 // If there are three terminators, we don't know what sort of block this is.
308 if (++I != REnd && isUnpredicatedTerminator(&*I))
311 // If second to last instruction is an unconditional branch,
312 // analyze it and remove the last instruction.
313 if (SecondLastOpc == Mips::J) {
314 // Return if the last instruction cannot be removed.
318 TBB = SecondLastInst->getOperand(0).getMBB();
319 LastInst->eraseFromParent();
323 // Conditional branch followed by an unconditional branch.
324 // The last one must be unconditional.
325 if (LastOpc != Mips::J)
328 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
329 FBB = LastInst->getOperand(0).getMBB();
334 void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
335 MachineBasicBlock *TBB, DebugLoc DL,
336 const SmallVectorImpl<MachineOperand>& Cond)
338 unsigned Opc = Cond[0].getImm();
339 const MCInstrDesc &MCID = get(Opc);
340 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
342 for (unsigned i = 1; i < Cond.size(); ++i)
343 MIB.addReg(Cond[i].getReg());
348 unsigned MipsInstrInfo::
349 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
350 MachineBasicBlock *FBB,
351 const SmallVectorImpl<MachineOperand> &Cond,
353 // Shouldn't be a fall through.
354 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
356 // # of condition operands:
357 // Unconditional branches: 0
358 // Floating point branches: 1 (opc)
359 // Int BranchZero: 2 (opc, reg)
360 // Int Branch: 3 (opc, reg0, reg1)
361 assert((Cond.size() <= 3) &&
362 "# of Mips branch conditions must be <= 3!");
364 // Two-way Conditional branch.
366 BuildCondBr(MBB, TBB, DL, Cond);
367 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
372 // Unconditional branch.
374 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
375 else // Conditional branch.
376 BuildCondBr(MBB, TBB, DL, Cond);
380 unsigned MipsInstrInfo::
381 RemoveBranch(MachineBasicBlock &MBB) const
383 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
384 MachineBasicBlock::reverse_iterator FirstBr;
387 // Skip all the debug instructions.
388 while (I != REnd && I->isDebugValue())
393 // Up to 2 branches are removed.
394 // Note that indirect branches are not removed.
395 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
396 if (!GetAnalyzableBrOpc(I->getOpcode()))
399 MBB.erase(I.base(), FirstBr.base());
404 /// ReverseBranchCondition - Return the inverse opcode of the
405 /// specified Branch instruction.
407 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
409 assert( (Cond.size() && Cond.size() <= 3) &&
410 "Invalid Mips branch condition!");
411 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
415 /// getGlobalBaseReg - Return a virtual register initialized with the
416 /// the global base register value. Output instructions required to
417 /// initialize the register in the function entry block, if necessary.
419 unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
420 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
421 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
422 if (GlobalBaseReg != 0)
423 return GlobalBaseReg;
425 // Insert the set of GlobalBaseReg into the first MBB of the function
426 MachineBasicBlock &FirstMBB = MF->front();
427 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
428 MachineRegisterInfo &RegInfo = MF->getRegInfo();
429 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
431 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
432 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
433 GlobalBaseReg).addReg(Mips::GP);
434 RegInfo.addLiveIn(Mips::GP);
436 MipsFI->setGlobalBaseReg(GlobalBaseReg);
437 return GlobalBaseReg;