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.isImmediate() && 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) const
35 // addu $dst, $src, $zero || addu $dst, $zero, $src
36 // or $dst, $src, $zero || or $dst, $zero, $src
37 if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) {
38 if (MI.getOperand(1).getReg() == Mips::ZERO) {
39 DstReg = MI.getOperand(0).getReg();
40 SrcReg = MI.getOperand(2).getReg();
42 } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
43 DstReg = MI.getOperand(0).getReg();
44 SrcReg = MI.getOperand(1).getReg();
52 if (MI.getOpcode() == Mips::FMOV_SO32 || MI.getOpcode() == Mips::FMOV_AS32 ||
53 MI.getOpcode() == Mips::FMOV_D32 || MI.getOpcode() == Mips::MFC1A ||
54 MI.getOpcode() == Mips::MFC1 || MI.getOpcode() == Mips::MTC1A ||
55 MI.getOpcode() == Mips::MTC1 ) {
56 DstReg = MI.getOperand(0).getReg();
57 SrcReg = MI.getOperand(1).getReg();
61 // addiu $dst, $src, 0
62 if (MI.getOpcode() == Mips::ADDiu) {
63 if ((MI.getOperand(1).isRegister()) && (isZeroImm(MI.getOperand(2)))) {
64 DstReg = MI.getOperand(0).getReg();
65 SrcReg = MI.getOperand(1).getReg();
72 /// isLoadFromStackSlot - If the specified machine instruction is a direct
73 /// load from a stack slot, return the virtual or physical register number of
74 /// the destination along with the FrameIndex of the loaded stack slot. If
75 /// not, return 0. This predicate must return 0 if the instruction has
76 /// any side effects other than loading from the stack slot.
77 unsigned MipsInstrInfo::
78 isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const
80 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
81 (MI->getOpcode() == Mips::LWC1A) || (MI->getOpcode() == Mips::LDC1)) {
82 if ((MI->getOperand(2).isFrameIndex()) && // is a stack slot
83 (MI->getOperand(1).isImmediate()) && // the imm is zero
84 (isZeroImm(MI->getOperand(1)))) {
85 FrameIndex = MI->getOperand(2).getIndex();
86 return MI->getOperand(0).getReg();
93 /// isStoreToStackSlot - If the specified machine instruction is a direct
94 /// store to a stack slot, return the virtual or physical register number of
95 /// the source reg along with the FrameIndex of the loaded stack slot. If
96 /// not, return 0. This predicate must return 0 if the instruction has
97 /// any side effects other than storing to the stack slot.
98 unsigned MipsInstrInfo::
99 isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const
101 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
102 (MI->getOpcode() == Mips::SWC1A) || (MI->getOpcode() == Mips::SDC1)) {
103 if ((MI->getOperand(2).isFrameIndex()) && // is a stack slot
104 (MI->getOperand(1).isImmediate()) && // the imm is zero
105 (isZeroImm(MI->getOperand(1)))) {
106 FrameIndex = MI->getOperand(2).getIndex();
107 return MI->getOperand(0).getReg();
113 /// insertNoop - If data hazard condition is found insert the target nop
116 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
118 BuildMI(MBB, MI, get(Mips::NOP));
122 copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
123 unsigned DestReg, unsigned SrcReg,
124 const TargetRegisterClass *DestRC,
125 const TargetRegisterClass *SrcRC) const {
126 if (DestRC != SrcRC) {
127 if ((DestRC == Mips::CPURegsRegisterClass) &&
128 (SrcRC == Mips::FGR32RegisterClass))
129 BuildMI(MBB, I, get(Mips::MFC1), DestReg).addReg(SrcReg);
130 else if ((DestRC == Mips::CPURegsRegisterClass) &&
131 (SrcRC == Mips::AFGR32RegisterClass))
132 BuildMI(MBB, I, get(Mips::MFC1A), DestReg).addReg(SrcReg);
133 else if ((DestRC == Mips::FGR32RegisterClass) &&
134 (SrcRC == Mips::CPURegsRegisterClass))
135 BuildMI(MBB, I, get(Mips::MTC1), DestReg).addReg(SrcReg);
136 else if ((DestRC == Mips::AFGR32RegisterClass) &&
137 (SrcRC == Mips::CPURegsRegisterClass))
138 BuildMI(MBB, I, get(Mips::MTC1A), DestReg).addReg(SrcReg);
139 else if ((DestRC == Mips::AFGR32RegisterClass) &&
140 (SrcRC == Mips::CPURegsRegisterClass))
141 BuildMI(MBB, I, get(Mips::MTC1A), DestReg).addReg(SrcReg);
142 else if ((SrcRC == Mips::CCRRegisterClass) &&
143 (SrcReg == Mips::FCR31))
144 return; // This register is used implicitly, no copy needed.
145 else if ((DestRC == Mips::CCRRegisterClass) &&
146 (DestReg == Mips::FCR31))
147 return; // This register is used implicitly, no copy needed.
148 else if ((DestRC == Mips::HILORegisterClass) &&
149 (SrcRC == Mips::CPURegsRegisterClass)) {
150 unsigned Opc = (DestReg == Mips::HI) ? Mips::MTHI : Mips::MTLO;
151 BuildMI(MBB, I, get(Opc), DestReg);
152 } else if ((SrcRC == Mips::HILORegisterClass) &&
153 (DestRC == Mips::CPURegsRegisterClass)) {
154 unsigned Opc = (SrcReg == Mips::HI) ? Mips::MFHI : Mips::MFLO;
155 BuildMI(MBB, I, get(Opc), DestReg);
157 assert (0 && "DestRC != SrcRC, Can't copy this register");
162 if (DestRC == Mips::CPURegsRegisterClass)
163 BuildMI(MBB, I, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
165 else if (DestRC == Mips::FGR32RegisterClass)
166 BuildMI(MBB, I, get(Mips::FMOV_SO32), DestReg).addReg(SrcReg);
167 else if (DestRC == Mips::AFGR32RegisterClass)
168 BuildMI(MBB, I, get(Mips::FMOV_AS32), DestReg).addReg(SrcReg);
169 else if (DestRC == Mips::AFGR64RegisterClass)
170 BuildMI(MBB, I, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
172 assert (0 && "Can't copy this register");
176 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
177 unsigned SrcReg, bool isKill, int FI,
178 const TargetRegisterClass *RC) const
181 if (RC == Mips::CPURegsRegisterClass)
183 else if (RC == Mips::FGR32RegisterClass)
185 else if (RC == Mips::AFGR32RegisterClass)
187 else if (RC == Mips::AFGR64RegisterClass)
190 assert(0 && "Can't store this register to stack slot");
192 BuildMI(MBB, I, get(Opc)).addReg(SrcReg, false, false, isKill)
193 .addImm(0).addFrameIndex(FI);
196 void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
197 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
198 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
201 if (RC == Mips::CPURegsRegisterClass)
203 else if (RC == Mips::FGR32RegisterClass)
205 else if (RC == Mips::AFGR32RegisterClass)
207 else if (RC == Mips::AFGR64RegisterClass)
210 assert(0 && "Can't store this register");
212 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
213 .addReg(SrcReg, false, false, isKill);
214 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
215 MachineOperand &MO = Addr[i];
217 MIB.addReg(MO.getReg());
218 else if (MO.isImmediate())
219 MIB.addImm(MO.getImm());
221 MIB.addFrameIndex(MO.getIndex());
223 NewMIs.push_back(MIB);
228 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
229 unsigned DestReg, int FI,
230 const TargetRegisterClass *RC) const
233 if (RC == Mips::CPURegsRegisterClass)
235 else if (RC == Mips::FGR32RegisterClass)
237 else if (RC == Mips::AFGR32RegisterClass)
239 else if (RC == Mips::AFGR64RegisterClass)
242 assert(0 && "Can't load this register from stack slot");
244 BuildMI(MBB, I, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
247 void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
248 SmallVectorImpl<MachineOperand> &Addr,
249 const TargetRegisterClass *RC,
250 SmallVectorImpl<MachineInstr*> &NewMIs) const {
252 if (RC == Mips::CPURegsRegisterClass)
254 else if (RC == Mips::FGR32RegisterClass)
256 else if (RC == Mips::AFGR32RegisterClass)
258 else if (RC == Mips::AFGR64RegisterClass)
261 assert(0 && "Can't load this register");
263 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
264 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
265 MachineOperand &MO = Addr[i];
267 MIB.addReg(MO.getReg());
268 else if (MO.isImmediate())
269 MIB.addImm(MO.getImm());
271 MIB.addFrameIndex(MO.getIndex());
273 NewMIs.push_back(MIB);
277 MachineInstr *MipsInstrInfo::
278 foldMemoryOperand(MachineFunction &MF,
280 SmallVectorImpl<unsigned> &Ops, int FI) const
282 if (Ops.size() != 1) return NULL;
284 MachineInstr *NewMI = NULL;
286 switch (MI->getOpcode()) {
288 if ((MI->getOperand(0).isRegister()) &&
289 (MI->getOperand(1).isRegister()) &&
290 (MI->getOperand(1).getReg() == Mips::ZERO) &&
291 (MI->getOperand(2).isRegister())) {
292 if (Ops[0] == 0) { // COPY -> STORE
293 unsigned SrcReg = MI->getOperand(2).getReg();
294 bool isKill = MI->getOperand(2).isKill();
295 NewMI = BuildMI(MF, get(Mips::SW)).addReg(SrcReg, false, false, isKill)
296 .addImm(0).addFrameIndex(FI);
297 } else { // COPY -> LOAD
298 unsigned DstReg = MI->getOperand(0).getReg();
299 bool isDead = MI->getOperand(0).isDead();
300 NewMI = BuildMI(MF, get(Mips::LW))
301 .addReg(DstReg, true, false, false, isDead)
302 .addImm(0).addFrameIndex(FI);
306 case Mips::FMOV_SO32:
307 case Mips::FMOV_AS32:
309 if ((MI->getOperand(0).isRegister()) &&
310 (MI->getOperand(1).isRegister())) {
311 const TargetRegisterClass
312 *RC = RI.getRegClass(MI->getOperand(0).getReg());
313 unsigned StoreOpc, LoadOpc;
315 if (RC == Mips::FGR32RegisterClass) {
316 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
317 } else if (RC == Mips::AFGR32RegisterClass) {
318 LoadOpc = Mips::LWC1A; StoreOpc = Mips::SWC1A;
319 } else if (RC == Mips::AFGR64RegisterClass) {
320 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
322 assert(0 && "foldMemoryOperand register unknown");
324 if (Ops[0] == 0) { // COPY -> STORE
325 unsigned SrcReg = MI->getOperand(1).getReg();
326 bool isKill = MI->getOperand(1).isKill();
327 NewMI = BuildMI(MF, get(StoreOpc)).addReg(SrcReg, false, false, isKill)
328 .addImm(0).addFrameIndex(FI) ;
329 } else { // COPY -> LOAD
330 unsigned DstReg = MI->getOperand(0).getReg();
331 bool isDead = MI->getOperand(0).isDead();
332 NewMI = BuildMI(MF, get(LoadOpc))
333 .addReg(DstReg, true, false, false, isDead)
334 .addImm(0).addFrameIndex(FI);
343 //===----------------------------------------------------------------------===//
345 //===----------------------------------------------------------------------===//
347 /// GetCondFromBranchOpc - Return the Mips CC that matches
348 /// the correspondent Branch instruction opcode.
349 static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
352 default: return Mips::COND_INVALID;
353 case Mips::BEQ : return Mips::COND_E;
354 case Mips::BNE : return Mips::COND_NE;
355 case Mips::BGTZ : return Mips::COND_GZ;
356 case Mips::BGEZ : return Mips::COND_GEZ;
357 case Mips::BLTZ : return Mips::COND_LZ;
358 case Mips::BLEZ : return Mips::COND_LEZ;
360 // We dont do fp branch analysis yet!
362 case Mips::BC1F : return Mips::COND_INVALID;
366 /// GetCondBranchFromCond - Return the Branch instruction
367 /// opcode that matches the cc.
368 unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
371 default: assert(0 && "Illegal condition code!");
372 case Mips::COND_E : return Mips::BEQ;
373 case Mips::COND_NE : return Mips::BNE;
374 case Mips::COND_GZ : return Mips::BGTZ;
375 case Mips::COND_GEZ : return Mips::BGEZ;
376 case Mips::COND_LZ : return Mips::BLTZ;
377 case Mips::COND_LEZ : return Mips::BLEZ;
382 case Mips::FCOND_UEQ:
383 case Mips::FCOND_OLT:
384 case Mips::FCOND_ULT:
385 case Mips::FCOND_OLE:
386 case Mips::FCOND_ULE:
388 case Mips::FCOND_NGLE:
389 case Mips::FCOND_SEQ:
390 case Mips::FCOND_NGL:
392 case Mips::FCOND_NGE:
394 case Mips::FCOND_NGT: return Mips::BC1T;
398 case Mips::FCOND_NEQ:
399 case Mips::FCOND_OGL:
400 case Mips::FCOND_UGE:
401 case Mips::FCOND_OGE:
402 case Mips::FCOND_UGT:
403 case Mips::FCOND_OGT:
405 case Mips::FCOND_GLE:
406 case Mips::FCOND_SNE:
408 case Mips::FCOND_NLT:
410 case Mips::FCOND_NLE:
411 case Mips::FCOND_GT: return Mips::BC1F;
415 /// GetOppositeBranchCondition - Return the inverse of the specified
416 /// condition, e.g. turning COND_E to COND_NE.
417 Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
420 default: assert(0 && "Illegal condition code!");
421 case Mips::COND_E : return Mips::COND_NE;
422 case Mips::COND_NE : return Mips::COND_E;
423 case Mips::COND_GZ : return Mips::COND_LEZ;
424 case Mips::COND_GEZ : return Mips::COND_LZ;
425 case Mips::COND_LZ : return Mips::COND_GEZ;
426 case Mips::COND_LEZ : return Mips::COND_GZ;
427 case Mips::FCOND_F : return Mips::FCOND_T;
428 case Mips::FCOND_UN : return Mips::FCOND_OR;
429 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
430 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
431 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
432 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
433 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
434 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
435 case Mips::FCOND_SF: return Mips::FCOND_ST;
436 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
437 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
438 case Mips::FCOND_NGL: return Mips::FCOND_GL;
439 case Mips::FCOND_LT: return Mips::FCOND_NLT;
440 case Mips::FCOND_NGE: return Mips::FCOND_GE;
441 case Mips::FCOND_LE: return Mips::FCOND_NLE;
442 case Mips::FCOND_NGT: return Mips::FCOND_GT;
446 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
447 MachineBasicBlock *&TBB,
448 MachineBasicBlock *&FBB,
449 std::vector<MachineOperand> &Cond) const
451 // If the block has no terminators, it just falls into the block after it.
452 MachineBasicBlock::iterator I = MBB.end();
453 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
456 // Get the last instruction in the block.
457 MachineInstr *LastInst = I;
459 // If there is only one terminator instruction, process it.
460 unsigned LastOpc = LastInst->getOpcode();
461 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
462 if (!LastInst->getDesc().isBranch())
465 // Unconditional branch
466 if (LastOpc == Mips::J) {
467 TBB = LastInst->getOperand(0).getMBB();
471 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
472 if (BranchCode == Mips::COND_INVALID)
473 return true; // Can't handle indirect branch.
475 // Conditional branch
476 // Block ends with fall-through condbranch.
477 if (LastOpc != Mips::COND_INVALID) {
478 int LastNumOp = LastInst->getNumOperands();
480 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
481 Cond.push_back(MachineOperand::CreateImm(BranchCode));
483 for (int i=0; i<LastNumOp-1; i++) {
484 Cond.push_back(LastInst->getOperand(i));
491 // Get the instruction before it if it is a terminator.
492 MachineInstr *SecondLastInst = I;
494 // If there are three terminators, we don't know what sort of block this is.
495 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
498 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
499 unsigned SecondLastOpc = SecondLastInst->getOpcode();
500 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
502 if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
503 int SecondNumOp = SecondLastInst->getNumOperands();
505 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
506 Cond.push_back(MachineOperand::CreateImm(BranchCode));
508 for (int i=0; i<SecondNumOp-1; i++) {
509 Cond.push_back(SecondLastInst->getOperand(i));
512 FBB = LastInst->getOperand(0).getMBB();
516 // If the block ends with two unconditional branches, handle it. The last
517 // one is not executed, so remove it.
518 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
519 TBB = SecondLastInst->getOperand(0).getMBB();
521 I->eraseFromParent();
525 // Otherwise, can't handle this.
529 unsigned MipsInstrInfo::
530 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
531 MachineBasicBlock *FBB, const std::vector<MachineOperand> &Cond)
534 // Shouldn't be a fall through.
535 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
536 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
537 "Mips branch conditions can have two|three components!");
539 if (FBB == 0) { // One way branch.
541 // Unconditional branch?
542 BuildMI(&MBB, get(Mips::J)).addMBB(TBB);
544 // Conditional branch.
545 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
546 const TargetInstrDesc &TID = get(Opc);
548 if (TID.getNumOperands() == 3)
549 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
550 .addReg(Cond[2].getReg())
553 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
560 // Two-way Conditional branch.
561 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
562 const TargetInstrDesc &TID = get(Opc);
564 if (TID.getNumOperands() == 3)
565 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
568 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addMBB(TBB);
570 BuildMI(&MBB, get(Mips::J)).addMBB(FBB);
574 unsigned MipsInstrInfo::
575 RemoveBranch(MachineBasicBlock &MBB) const
577 MachineBasicBlock::iterator I = MBB.end();
578 if (I == MBB.begin()) return 0;
580 if (I->getOpcode() != Mips::J &&
581 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
584 // Remove the branch.
585 I->eraseFromParent();
589 if (I == MBB.begin()) return 1;
591 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
594 // Remove the branch.
595 I->eraseFromParent();
599 /// BlockHasNoFallThrough - Analyze if MachineBasicBlock does not
600 /// fall-through into its successor block.
602 BlockHasNoFallThrough(MachineBasicBlock &MBB) const
604 if (MBB.empty()) return false;
606 switch (MBB.back().getOpcode()) {
607 case Mips::RET: // Return.
608 case Mips::JR: // Indirect branch.
609 case Mips::J: // Uncond branch.
611 default: return false;
615 /// ReverseBranchCondition - Return the inverse opcode of the
616 /// specified Branch instruction.
618 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const
620 assert( (Cond.size() == 3 || Cond.size() == 2) &&
621 "Invalid Mips branch condition!");
622 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));