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 "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
22 #define GET_INSTRINFO_MC_DESC
23 #include "MipsGenInstrInfo.inc"
27 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
28 : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts),
29 Mips::ADJCALLSTACKDOWN, Mips::ADJCALLSTACKUP),
30 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
32 static bool isZeroImm(const MachineOperand &op) {
33 return op.isImm() && op.getImm() == 0;
36 /// isLoadFromStackSlot - If the specified machine instruction is a direct
37 /// load from a stack slot, return the virtual or physical register number of
38 /// the destination along with the FrameIndex of the loaded stack slot. If
39 /// not, return 0. This predicate must return 0 if the instruction has
40 /// any side effects other than loading from the stack slot.
41 unsigned MipsInstrInfo::
42 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
44 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
45 (MI->getOpcode() == Mips::LDC1)) {
46 if ((MI->getOperand(2).isFI()) && // is a stack slot
47 (MI->getOperand(1).isImm()) && // the imm is zero
48 (isZeroImm(MI->getOperand(1)))) {
49 FrameIndex = MI->getOperand(2).getIndex();
50 return MI->getOperand(0).getReg();
57 /// isStoreToStackSlot - If the specified machine instruction is a direct
58 /// store to a stack slot, return the virtual or physical register number of
59 /// the source reg along with the FrameIndex of the loaded stack slot. If
60 /// not, return 0. This predicate must return 0 if the instruction has
61 /// any side effects other than storing to the stack slot.
62 unsigned MipsInstrInfo::
63 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
65 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
66 (MI->getOpcode() == Mips::SDC1)) {
67 if ((MI->getOperand(2).isFI()) && // is a stack slot
68 (MI->getOperand(1).isImm()) && // the imm is zero
69 (isZeroImm(MI->getOperand(1)))) {
70 FrameIndex = MI->getOperand(2).getIndex();
71 return MI->getOperand(0).getReg();
77 /// insertNoop - If data hazard condition is found insert the target nop
80 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
83 BuildMI(MBB, MI, DL, get(Mips::NOP));
87 copyPhysReg(MachineBasicBlock &MBB,
88 MachineBasicBlock::iterator I, DebugLoc DL,
89 unsigned DestReg, unsigned SrcReg,
91 bool DestCPU = Mips::CPURegsRegClass.contains(DestReg);
92 bool SrcCPU = Mips::CPURegsRegClass.contains(SrcReg);
94 // CPU-CPU is the most common.
95 if (DestCPU && SrcCPU) {
96 BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
97 .addReg(SrcReg, getKillRegState(KillSrc));
101 // Copy to CPU from other registers.
103 if (Mips::CCRRegClass.contains(SrcReg))
104 BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg)
105 .addReg(SrcReg, getKillRegState(KillSrc));
106 else if (Mips::FGR32RegClass.contains(SrcReg))
107 BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg)
108 .addReg(SrcReg, getKillRegState(KillSrc));
109 else if (SrcReg == Mips::HI)
110 BuildMI(MBB, I, DL, get(Mips::MFHI), DestReg);
111 else if (SrcReg == Mips::LO)
112 BuildMI(MBB, I, DL, get(Mips::MFLO), DestReg);
114 llvm_unreachable("Copy to CPU from invalid register");
118 // Copy to other registers from CPU.
120 if (Mips::CCRRegClass.contains(DestReg))
121 BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg)
122 .addReg(SrcReg, getKillRegState(KillSrc));
123 else if (Mips::FGR32RegClass.contains(DestReg))
124 BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg)
125 .addReg(SrcReg, getKillRegState(KillSrc));
126 else if (DestReg == Mips::HI)
127 BuildMI(MBB, I, DL, get(Mips::MTHI))
128 .addReg(SrcReg, getKillRegState(KillSrc));
129 else if (DestReg == Mips::LO)
130 BuildMI(MBB, I, DL, get(Mips::MTLO))
131 .addReg(SrcReg, getKillRegState(KillSrc));
133 llvm_unreachable("Copy from CPU to invalid register");
137 if (Mips::FGR32RegClass.contains(DestReg, SrcReg)) {
138 BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg)
139 .addReg(SrcReg, getKillRegState(KillSrc));
143 if (Mips::AFGR64RegClass.contains(DestReg, SrcReg)) {
144 BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg)
145 .addReg(SrcReg, getKillRegState(KillSrc));
149 if (Mips::CCRRegClass.contains(DestReg, SrcReg)) {
150 BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg)
151 .addReg(SrcReg, getKillRegState(KillSrc));
154 llvm_unreachable("Cannot copy registers");
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();
165 if (RC == Mips::CPURegsRegisterClass)
166 BuildMI(MBB, I, DL, get(Mips::SW)).addReg(SrcReg, getKillRegState(isKill))
167 .addImm(0).addFrameIndex(FI);
168 else if (RC == Mips::FGR32RegisterClass)
169 BuildMI(MBB, I, DL, get(Mips::SWC1)).addReg(SrcReg, getKillRegState(isKill))
170 .addImm(0).addFrameIndex(FI);
171 else if (RC == Mips::AFGR64RegisterClass) {
172 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
173 BuildMI(MBB, I, DL, get(Mips::SDC1))
174 .addReg(SrcReg, getKillRegState(isKill))
175 .addImm(0).addFrameIndex(FI);
177 const TargetRegisterInfo *TRI =
178 MBB.getParent()->getTarget().getRegisterInfo();
179 const unsigned *SubSet = TRI->getSubRegisters(SrcReg);
180 BuildMI(MBB, I, DL, get(Mips::SWC1))
181 .addReg(SubSet[0], getKillRegState(isKill))
182 .addImm(0).addFrameIndex(FI);
183 BuildMI(MBB, I, DL, get(Mips::SWC1))
184 .addReg(SubSet[1], getKillRegState(isKill))
185 .addImm(4).addFrameIndex(FI);
188 llvm_unreachable("Register class not handled!");
192 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
193 unsigned DestReg, int FI,
194 const TargetRegisterClass *RC,
195 const TargetRegisterInfo *TRI) const
198 if (I != MBB.end()) DL = I->getDebugLoc();
200 if (RC == Mips::CPURegsRegisterClass)
201 BuildMI(MBB, I, DL, get(Mips::LW), DestReg).addImm(0).addFrameIndex(FI);
202 else if (RC == Mips::FGR32RegisterClass)
203 BuildMI(MBB, I, DL, get(Mips::LWC1), DestReg).addImm(0).addFrameIndex(FI);
204 else if (RC == Mips::AFGR64RegisterClass) {
205 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
206 BuildMI(MBB, I, DL, get(Mips::LDC1), DestReg).addImm(0).addFrameIndex(FI);
208 const TargetRegisterInfo *TRI =
209 MBB.getParent()->getTarget().getRegisterInfo();
210 const unsigned *SubSet = TRI->getSubRegisters(DestReg);
211 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[0])
212 .addImm(0).addFrameIndex(FI);
213 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[1])
214 .addImm(4).addFrameIndex(FI);
217 llvm_unreachable("Register class not handled!");
221 MipsInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF, int FrameIx,
222 uint64_t Offset, const MDNode *MDPtr,
224 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Mips::DBG_VALUE))
225 .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
229 //===----------------------------------------------------------------------===//
231 //===----------------------------------------------------------------------===//
233 static unsigned GetAnalyzableBrOpc(unsigned Opc) {
234 return (Opc == Mips::BEQ || Opc == Mips::BNE || Opc == Mips::BGTZ ||
235 Opc == Mips::BGEZ || Opc == Mips::BLTZ || Opc == Mips::BLEZ ||
236 Opc == Mips::BC1T || Opc == Mips::BC1F || Opc == Mips::J) ? Opc : 0;
239 /// GetOppositeBranchOpc - Return the inverse of the specified
240 /// opcode, e.g. turning BEQ to BNE.
241 unsigned Mips::GetOppositeBranchOpc(unsigned Opc)
244 default: llvm_unreachable("Illegal opcode!");
245 case Mips::BEQ : return Mips::BNE;
246 case Mips::BNE : return Mips::BEQ;
247 case Mips::BGTZ : return Mips::BLEZ;
248 case Mips::BGEZ : return Mips::BLTZ;
249 case Mips::BLTZ : return Mips::BGEZ;
250 case Mips::BLEZ : return Mips::BGTZ;
251 case Mips::BC1T : return Mips::BC1F;
252 case Mips::BC1F : return Mips::BC1T;
256 static void AnalyzeCondBr(const MachineInstr* Inst, unsigned Opc,
257 MachineBasicBlock *&BB,
258 SmallVectorImpl<MachineOperand>& Cond) {
259 assert(GetAnalyzableBrOpc(Opc) && "Not an analyzable branch");
260 int NumOp = Inst->getNumExplicitOperands();
262 // for both int and fp branches, the last explicit operand is the
264 BB = Inst->getOperand(NumOp-1).getMBB();
265 Cond.push_back(MachineOperand::CreateImm(Opc));
267 for (int i=0; i<NumOp-1; i++)
268 Cond.push_back(Inst->getOperand(i));
271 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
272 MachineBasicBlock *&TBB,
273 MachineBasicBlock *&FBB,
274 SmallVectorImpl<MachineOperand> &Cond,
275 bool AllowModify) const
277 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
279 // Skip all the debug instructions.
280 while (I != REnd && I->isDebugValue())
283 if (I == REnd || !isUnpredicatedTerminator(&*I)) {
284 // If this block ends with no branches (it just falls through to its succ)
285 // just return false, leaving TBB/FBB null.
290 MachineInstr *LastInst = &*I;
291 unsigned LastOpc = LastInst->getOpcode();
293 // Not an analyzable branch (must be an indirect jump).
294 if (!GetAnalyzableBrOpc(LastOpc))
297 // Get the second to last instruction in the block.
298 unsigned SecondLastOpc = 0;
299 MachineInstr *SecondLastInst = NULL;
302 SecondLastInst = &*I;
303 SecondLastOpc = GetAnalyzableBrOpc(SecondLastInst->getOpcode());
305 // Not an analyzable branch (must be an indirect jump).
306 if (isUnpredicatedTerminator(SecondLastInst) && !SecondLastOpc)
310 // If there is only one terminator instruction, process it.
311 if (!SecondLastOpc) {
312 // Unconditional branch
313 if (LastOpc == Mips::J) {
314 TBB = LastInst->getOperand(0).getMBB();
318 // Conditional branch
319 AnalyzeCondBr(LastInst, LastOpc, TBB, Cond);
323 // If we reached here, there are two branches.
324 // If there are three terminators, we don't know what sort of block this is.
325 if (++I != REnd && isUnpredicatedTerminator(&*I))
328 // If second to last instruction is an unconditional branch,
329 // analyze it and remove the last instruction.
330 if (SecondLastOpc == Mips::J) {
331 // Return if the last instruction cannot be removed.
335 TBB = SecondLastInst->getOperand(0).getMBB();
336 LastInst->eraseFromParent();
340 // Conditional branch followed by an unconditional branch.
341 // The last one must be unconditional.
342 if (LastOpc != Mips::J)
345 AnalyzeCondBr(SecondLastInst, SecondLastOpc, TBB, Cond);
346 FBB = LastInst->getOperand(0).getMBB();
351 void MipsInstrInfo::BuildCondBr(MachineBasicBlock &MBB,
352 MachineBasicBlock *TBB, DebugLoc DL,
353 const SmallVectorImpl<MachineOperand>& Cond)
355 unsigned Opc = Cond[0].getImm();
356 const MCInstrDesc &MCID = get(Opc);
357 MachineInstrBuilder MIB = BuildMI(&MBB, DL, MCID);
359 for (unsigned i = 1; i < Cond.size(); ++i)
360 MIB.addReg(Cond[i].getReg());
365 unsigned MipsInstrInfo::
366 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
367 MachineBasicBlock *FBB,
368 const SmallVectorImpl<MachineOperand> &Cond,
370 // Shouldn't be a fall through.
371 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
373 // # of condition operands:
374 // Unconditional branches: 0
375 // Floating point branches: 1 (opc)
376 // Int BranchZero: 2 (opc, reg)
377 // Int Branch: 3 (opc, reg0, reg1)
378 assert((Cond.size() <= 3) &&
379 "# of Mips branch conditions must be <= 3!");
381 // Two-way Conditional branch.
383 BuildCondBr(MBB, TBB, DL, Cond);
384 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
389 // Unconditional branch.
391 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
392 else // Conditional branch.
393 BuildCondBr(MBB, TBB, DL, Cond);
397 unsigned MipsInstrInfo::
398 RemoveBranch(MachineBasicBlock &MBB) const
400 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), REnd = MBB.rend();
401 MachineBasicBlock::reverse_iterator FirstBr;
404 // Skip all the debug instructions.
405 while (I != REnd && I->isDebugValue())
410 // Up to 2 branches are removed.
411 // Note that indirect branches are not removed.
412 for(removed = 0; I != REnd && removed < 2; ++I, ++removed)
413 if (!GetAnalyzableBrOpc(I->getOpcode()))
416 MBB.erase(I.base(), FirstBr.base());
421 /// ReverseBranchCondition - Return the inverse opcode of the
422 /// specified Branch instruction.
424 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
426 assert( (Cond.size() && Cond.size() <= 3) &&
427 "Invalid Mips branch condition!");
428 Cond[0].setImm(Mips::GetOppositeBranchOpc(Cond[0].getImm()));
432 /// getGlobalBaseReg - Return a virtual register initialized with the
433 /// the global base register value. Output instructions required to
434 /// initialize the register in the function entry block, if necessary.
436 unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
437 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
438 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
439 if (GlobalBaseReg != 0)
440 return GlobalBaseReg;
442 // Insert the set of GlobalBaseReg into the first MBB of the function
443 MachineBasicBlock &FirstMBB = MF->front();
444 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
445 MachineRegisterInfo &RegInfo = MF->getRegInfo();
446 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
448 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
449 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
450 GlobalBaseReg).addReg(Mips::GP);
451 RegInfo.addLiveIn(Mips::GP);
453 MipsFI->setGlobalBaseReg(GlobalBaseReg);
454 return GlobalBaseReg;