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 "llvm/ADT/STLExtras.h"
17 #include "llvm/CodeGen/MachineInstrBuilder.h"
18 #include "MipsGenInstrInfo.inc"
22 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
23 : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)),
24 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
26 static bool isZeroImm(const MachineOperand &op) {
27 return op.isImm() && op.getImm() == 0;
30 /// Return true if the instruction is a register to register move and
31 /// leave the source and dest operands in the passed parameters.
33 isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg,
34 unsigned &SrcSubIdx, unsigned &DstSubIdx) const
36 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
38 // addu $dst, $src, $zero || addu $dst, $zero, $src
39 // or $dst, $src, $zero || or $dst, $zero, $src
40 if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) {
41 if (MI.getOperand(1).getReg() == Mips::ZERO) {
42 DstReg = MI.getOperand(0).getReg();
43 SrcReg = MI.getOperand(2).getReg();
45 } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
46 DstReg = MI.getOperand(0).getReg();
47 SrcReg = MI.getOperand(1).getReg();
55 if (MI.getOpcode() == Mips::FMOV_S32 ||
56 MI.getOpcode() == Mips::FMOV_D32 ||
57 MI.getOpcode() == Mips::MFC1 ||
58 MI.getOpcode() == Mips::MTC1 ||
59 MI.getOpcode() == Mips::MOVCCRToCCR) {
60 DstReg = MI.getOperand(0).getReg();
61 SrcReg = MI.getOperand(1).getReg();
65 // addiu $dst, $src, 0
66 if (MI.getOpcode() == Mips::ADDiu) {
67 if ((MI.getOperand(1).isReg()) && (isZeroImm(MI.getOperand(2)))) {
68 DstReg = MI.getOperand(0).getReg();
69 SrcReg = MI.getOperand(1).getReg();
77 /// isLoadFromStackSlot - If the specified machine instruction is a direct
78 /// load from a stack slot, return the virtual or physical register number of
79 /// the destination along with the FrameIndex of the loaded stack slot. If
80 /// not, return 0. This predicate must return 0 if the instruction has
81 /// any side effects other than loading from the stack slot.
82 unsigned MipsInstrInfo::
83 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
85 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
86 (MI->getOpcode() == Mips::LDC1)) {
87 if ((MI->getOperand(2).isFI()) && // is a stack slot
88 (MI->getOperand(1).isImm()) && // the imm is zero
89 (isZeroImm(MI->getOperand(1)))) {
90 FrameIndex = MI->getOperand(2).getIndex();
91 return MI->getOperand(0).getReg();
98 /// isStoreToStackSlot - If the specified machine instruction is a direct
99 /// store to a stack slot, return the virtual or physical register number of
100 /// the source reg along with the FrameIndex of the loaded stack slot. If
101 /// not, return 0. This predicate must return 0 if the instruction has
102 /// any side effects other than storing to the stack slot.
103 unsigned MipsInstrInfo::
104 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
106 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
107 (MI->getOpcode() == Mips::SDC1)) {
108 if ((MI->getOperand(2).isFI()) && // is a stack slot
109 (MI->getOperand(1).isImm()) && // the imm is zero
110 (isZeroImm(MI->getOperand(1)))) {
111 FrameIndex = MI->getOperand(2).getIndex();
112 return MI->getOperand(0).getReg();
118 /// insertNoop - If data hazard condition is found insert the target nop
121 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
123 DebugLoc DL = DebugLoc::getUnknownLoc();
124 if (MI != MBB.end()) DL = MI->getDebugLoc();
125 BuildMI(MBB, MI, DL, get(Mips::NOP));
129 copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
130 unsigned DestReg, unsigned SrcReg,
131 const TargetRegisterClass *DestRC,
132 const TargetRegisterClass *SrcRC) const {
133 DebugLoc DL = DebugLoc::getUnknownLoc();
134 if (I != MBB.end()) DL = I->getDebugLoc();
136 if (DestRC != SrcRC) {
138 // Copy to/from FCR31 condition register
139 if ((DestRC == Mips::CPURegsRegisterClass) &&
140 (SrcRC == Mips::CCRRegisterClass))
141 BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg).addReg(SrcReg);
142 else if ((DestRC == Mips::CCRRegisterClass) &&
143 (SrcRC == Mips::CPURegsRegisterClass))
144 BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg).addReg(SrcReg);
146 // Moves between coprocessors and cpu
147 else if ((DestRC == Mips::CPURegsRegisterClass) &&
148 (SrcRC == Mips::FGR32RegisterClass))
149 BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg).addReg(SrcReg);
150 else if ((DestRC == Mips::FGR32RegisterClass) &&
151 (SrcRC == Mips::CPURegsRegisterClass))
152 BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg).addReg(SrcReg);
154 // Move from/to Hi/Lo registers
155 else if ((DestRC == Mips::HILORegisterClass) &&
156 (SrcRC == Mips::CPURegsRegisterClass)) {
157 unsigned Opc = (DestReg == Mips::HI) ? Mips::MTHI : Mips::MTLO;
158 BuildMI(MBB, I, DL, get(Opc), DestReg);
159 } else if ((SrcRC == Mips::HILORegisterClass) &&
160 (DestRC == Mips::CPURegsRegisterClass)) {
161 unsigned Opc = (SrcReg == Mips::HI) ? Mips::MFHI : Mips::MFLO;
162 BuildMI(MBB, I, DL, get(Opc), DestReg);
164 // Can't copy this register
171 if (DestRC == Mips::CPURegsRegisterClass)
172 BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
174 else if (DestRC == Mips::FGR32RegisterClass)
175 BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg).addReg(SrcReg);
176 else if (DestRC == Mips::AFGR64RegisterClass)
177 BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
178 else if (DestRC == Mips::CCRRegisterClass)
179 BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg).addReg(SrcReg);
181 // Can't copy this register
188 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
189 unsigned SrcReg, bool isKill, int FI,
190 const TargetRegisterClass *RC) const {
193 DebugLoc DL = DebugLoc::getUnknownLoc();
194 if (I != MBB.end()) DL = I->getDebugLoc();
196 if (RC == Mips::CPURegsRegisterClass)
198 else if (RC == Mips::FGR32RegisterClass)
201 assert(RC == Mips::AFGR64RegisterClass);
205 BuildMI(MBB, I, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill))
206 .addImm(0).addFrameIndex(FI);
209 void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
210 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
211 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
214 if (RC == Mips::CPURegsRegisterClass)
216 else if (RC == Mips::FGR32RegisterClass)
219 assert(RC == Mips::AFGR64RegisterClass);
223 DebugLoc DL = DebugLoc::getUnknownLoc();
224 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc))
225 .addReg(SrcReg, getKillRegState(isKill));
226 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
227 MIB.addOperand(Addr[i]);
228 NewMIs.push_back(MIB);
233 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
234 unsigned DestReg, int FI,
235 const TargetRegisterClass *RC) const
238 if (RC == Mips::CPURegsRegisterClass)
240 else if (RC == Mips::FGR32RegisterClass)
243 assert(RC == Mips::AFGR64RegisterClass);
247 DebugLoc DL = DebugLoc::getUnknownLoc();
248 if (I != MBB.end()) DL = I->getDebugLoc();
249 BuildMI(MBB, I, DL, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
252 void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
253 SmallVectorImpl<MachineOperand> &Addr,
254 const TargetRegisterClass *RC,
255 SmallVectorImpl<MachineInstr*> &NewMIs) const {
257 if (RC == Mips::CPURegsRegisterClass)
259 else if (RC == Mips::FGR32RegisterClass)
262 assert(RC == Mips::AFGR64RegisterClass);
266 DebugLoc DL = DebugLoc::getUnknownLoc();
267 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
268 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
269 MIB.addOperand(Addr[i]);
270 NewMIs.push_back(MIB);
274 MachineInstr *MipsInstrInfo::
275 foldMemoryOperandImpl(MachineFunction &MF,
277 const SmallVectorImpl<unsigned> &Ops, int FI) const
279 if (Ops.size() != 1) return NULL;
281 MachineInstr *NewMI = NULL;
283 switch (MI->getOpcode()) {
285 if ((MI->getOperand(0).isReg()) &&
286 (MI->getOperand(1).isReg()) &&
287 (MI->getOperand(1).getReg() == Mips::ZERO) &&
288 (MI->getOperand(2).isReg())) {
289 if (Ops[0] == 0) { // COPY -> STORE
290 unsigned SrcReg = MI->getOperand(2).getReg();
291 bool isKill = MI->getOperand(2).isKill();
292 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::SW))
293 .addReg(SrcReg, getKillRegState(isKill))
294 .addImm(0).addFrameIndex(FI);
295 } else { // COPY -> LOAD
296 unsigned DstReg = MI->getOperand(0).getReg();
297 bool isDead = MI->getOperand(0).isDead();
298 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::LW))
299 .addReg(DstReg, RegState::Define | getDeadRegState(isDead))
300 .addImm(0).addFrameIndex(FI);
306 if ((MI->getOperand(0).isReg()) &&
307 (MI->getOperand(1).isReg())) {
308 const TargetRegisterClass
309 *RC = RI.getRegClass(MI->getOperand(0).getReg());
310 unsigned StoreOpc, LoadOpc;
312 if (RC == Mips::FGR32RegisterClass) {
313 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
315 assert(RC == Mips::AFGR64RegisterClass);
316 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
319 if (Ops[0] == 0) { // COPY -> STORE
320 unsigned SrcReg = MI->getOperand(1).getReg();
321 bool isKill = MI->getOperand(1).isKill();
322 NewMI = BuildMI(MF, MI->getDebugLoc(), get(StoreOpc))
323 .addReg(SrcReg, getKillRegState(isKill))
324 .addImm(0).addFrameIndex(FI) ;
325 } else { // COPY -> LOAD
326 unsigned DstReg = MI->getOperand(0).getReg();
327 bool isDead = MI->getOperand(0).isDead();
328 NewMI = BuildMI(MF, MI->getDebugLoc(), get(LoadOpc))
329 .addReg(DstReg, RegState::Define | getDeadRegState(isDead))
330 .addImm(0).addFrameIndex(FI);
339 //===----------------------------------------------------------------------===//
341 //===----------------------------------------------------------------------===//
343 /// GetCondFromBranchOpc - Return the Mips CC that matches
344 /// the correspondent Branch instruction opcode.
345 static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
348 default: return Mips::COND_INVALID;
349 case Mips::BEQ : return Mips::COND_E;
350 case Mips::BNE : return Mips::COND_NE;
351 case Mips::BGTZ : return Mips::COND_GZ;
352 case Mips::BGEZ : return Mips::COND_GEZ;
353 case Mips::BLTZ : return Mips::COND_LZ;
354 case Mips::BLEZ : return Mips::COND_LEZ;
356 // We dont do fp branch analysis yet!
358 case Mips::BC1F : return Mips::COND_INVALID;
362 /// GetCondBranchFromCond - Return the Branch instruction
363 /// opcode that matches the cc.
364 unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
367 default: assert(0 && "Illegal condition code!");
368 case Mips::COND_E : return Mips::BEQ;
369 case Mips::COND_NE : return Mips::BNE;
370 case Mips::COND_GZ : return Mips::BGTZ;
371 case Mips::COND_GEZ : return Mips::BGEZ;
372 case Mips::COND_LZ : return Mips::BLTZ;
373 case Mips::COND_LEZ : return Mips::BLEZ;
378 case Mips::FCOND_UEQ:
379 case Mips::FCOND_OLT:
380 case Mips::FCOND_ULT:
381 case Mips::FCOND_OLE:
382 case Mips::FCOND_ULE:
384 case Mips::FCOND_NGLE:
385 case Mips::FCOND_SEQ:
386 case Mips::FCOND_NGL:
388 case Mips::FCOND_NGE:
390 case Mips::FCOND_NGT: return Mips::BC1T;
394 case Mips::FCOND_NEQ:
395 case Mips::FCOND_OGL:
396 case Mips::FCOND_UGE:
397 case Mips::FCOND_OGE:
398 case Mips::FCOND_UGT:
399 case Mips::FCOND_OGT:
401 case Mips::FCOND_GLE:
402 case Mips::FCOND_SNE:
404 case Mips::FCOND_NLT:
406 case Mips::FCOND_NLE:
407 case Mips::FCOND_GT: return Mips::BC1F;
411 /// GetOppositeBranchCondition - Return the inverse of the specified
412 /// condition, e.g. turning COND_E to COND_NE.
413 Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
416 default: assert(0 && "Illegal condition code!");
417 case Mips::COND_E : return Mips::COND_NE;
418 case Mips::COND_NE : return Mips::COND_E;
419 case Mips::COND_GZ : return Mips::COND_LEZ;
420 case Mips::COND_GEZ : return Mips::COND_LZ;
421 case Mips::COND_LZ : return Mips::COND_GEZ;
422 case Mips::COND_LEZ : return Mips::COND_GZ;
423 case Mips::FCOND_F : return Mips::FCOND_T;
424 case Mips::FCOND_UN : return Mips::FCOND_OR;
425 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
426 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
427 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
428 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
429 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
430 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
431 case Mips::FCOND_SF: return Mips::FCOND_ST;
432 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
433 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
434 case Mips::FCOND_NGL: return Mips::FCOND_GL;
435 case Mips::FCOND_LT: return Mips::FCOND_NLT;
436 case Mips::FCOND_NGE: return Mips::FCOND_GE;
437 case Mips::FCOND_LE: return Mips::FCOND_NLE;
438 case Mips::FCOND_NGT: return Mips::FCOND_GT;
442 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
443 MachineBasicBlock *&TBB,
444 MachineBasicBlock *&FBB,
445 SmallVectorImpl<MachineOperand> &Cond,
446 bool AllowModify) const
448 // If the block has no terminators, it just falls into the block after it.
449 MachineBasicBlock::iterator I = MBB.end();
450 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
453 // Get the last instruction in the block.
454 MachineInstr *LastInst = I;
456 // If there is only one terminator instruction, process it.
457 unsigned LastOpc = LastInst->getOpcode();
458 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
459 if (!LastInst->getDesc().isBranch())
462 // Unconditional branch
463 if (LastOpc == Mips::J) {
464 TBB = LastInst->getOperand(0).getMBB();
468 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
469 if (BranchCode == Mips::COND_INVALID)
470 return true; // Can't handle indirect branch.
472 // Conditional branch
473 // Block ends with fall-through condbranch.
474 if (LastOpc != Mips::COND_INVALID) {
475 int LastNumOp = LastInst->getNumOperands();
477 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
478 Cond.push_back(MachineOperand::CreateImm(BranchCode));
480 for (int i=0; i<LastNumOp-1; i++) {
481 Cond.push_back(LastInst->getOperand(i));
488 // Get the instruction before it if it is a terminator.
489 MachineInstr *SecondLastInst = I;
491 // If there are three terminators, we don't know what sort of block this is.
492 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
495 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
496 unsigned SecondLastOpc = SecondLastInst->getOpcode();
497 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
499 if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
500 int SecondNumOp = SecondLastInst->getNumOperands();
502 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
503 Cond.push_back(MachineOperand::CreateImm(BranchCode));
505 for (int i=0; i<SecondNumOp-1; i++) {
506 Cond.push_back(SecondLastInst->getOperand(i));
509 FBB = LastInst->getOperand(0).getMBB();
513 // If the block ends with two unconditional branches, handle it. The last
514 // one is not executed, so remove it.
515 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
516 TBB = SecondLastInst->getOperand(0).getMBB();
519 I->eraseFromParent();
523 // Otherwise, can't handle this.
527 unsigned MipsInstrInfo::
528 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
529 MachineBasicBlock *FBB,
530 const SmallVectorImpl<MachineOperand> &Cond) const {
531 // FIXME this should probably have a DebugLoc argument
532 DebugLoc dl = DebugLoc::getUnknownLoc();
533 // Shouldn't be a fall through.
534 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
535 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
536 "Mips branch conditions can have two|three components!");
538 if (FBB == 0) { // One way branch.
540 // Unconditional branch?
541 BuildMI(&MBB, dl, get(Mips::J)).addMBB(TBB);
543 // Conditional branch.
544 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
545 const TargetInstrDesc &TID = get(Opc);
547 if (TID.getNumOperands() == 3)
548 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
549 .addReg(Cond[2].getReg())
552 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg())
559 // Two-way Conditional branch.
560 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
561 const TargetInstrDesc &TID = get(Opc);
563 if (TID.getNumOperands() == 3)
564 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
567 BuildMI(&MBB, dl, TID).addReg(Cond[1].getReg()).addMBB(TBB);
569 BuildMI(&MBB, dl, get(Mips::J)).addMBB(FBB);
573 unsigned MipsInstrInfo::
574 RemoveBranch(MachineBasicBlock &MBB) const
576 MachineBasicBlock::iterator I = MBB.end();
577 if (I == MBB.begin()) return 0;
579 if (I->getOpcode() != Mips::J &&
580 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
583 // Remove the branch.
584 I->eraseFromParent();
588 if (I == MBB.begin()) return 1;
590 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
593 // Remove the branch.
594 I->eraseFromParent();
598 /// BlockHasNoFallThrough - Analyze if MachineBasicBlock does not
599 /// fall-through into its successor block.
601 BlockHasNoFallThrough(const MachineBasicBlock &MBB) const
603 if (MBB.empty()) return false;
605 switch (MBB.back().getOpcode()) {
606 case Mips::RET: // Return.
607 case Mips::JR: // Indirect branch.
608 case Mips::J: // Uncond branch.
610 default: return false;
614 /// ReverseBranchCondition - Return the inverse opcode of the
615 /// specified Branch instruction.
617 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
619 assert( (Cond.size() == 3 || Cond.size() == 2) &&
620 "Invalid Mips branch condition!");
621 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));