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 //===----------------------------------------------------------------------===//
15 #include "MipsInstrInfo.h"
16 #include "MipsTargetMachine.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "MipsGenInstrInfo.inc"
23 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
24 : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)),
25 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
27 static bool isZeroImm(const MachineOperand &op) {
28 return op.isImmediate() && op.getImm() == 0;
31 /// Return true if the instruction is a register to register move and
32 /// leave the source and dest operands in the passed parameters.
34 isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg) const
36 // addu $dst, $src, $zero || addu $dst, $zero, $src
37 // or $dst, $src, $zero || or $dst, $zero, $src
38 if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) {
39 if (MI.getOperand(1).getReg() == Mips::ZERO) {
40 DstReg = MI.getOperand(0).getReg();
41 SrcReg = MI.getOperand(2).getReg();
43 } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
44 DstReg = MI.getOperand(0).getReg();
45 SrcReg = MI.getOperand(1).getReg();
53 if (MI.getOpcode() == Mips::FMOV_SO32 || MI.getOpcode() == Mips::FMOV_AS32 ||
54 MI.getOpcode() == Mips::FMOV_D32 || MI.getOpcode() == Mips::MFC1A ||
55 MI.getOpcode() == Mips::MFC1 || MI.getOpcode() == Mips::MTC1A ||
56 MI.getOpcode() == Mips::MTC1 ) {
57 DstReg = MI.getOperand(0).getReg();
58 SrcReg = MI.getOperand(1).getReg();
62 // addiu $dst, $src, 0
63 if (MI.getOpcode() == Mips::ADDiu) {
64 if ((MI.getOperand(1).isRegister()) && (isZeroImm(MI.getOperand(2)))) {
65 DstReg = MI.getOperand(0).getReg();
66 SrcReg = MI.getOperand(1).getReg();
73 /// isLoadFromStackSlot - If the specified machine instruction is a direct
74 /// load from a stack slot, return the virtual or physical register number of
75 /// the destination along with the FrameIndex of the loaded stack slot. If
76 /// not, return 0. This predicate must return 0 if the instruction has
77 /// any side effects other than loading from the stack slot.
78 unsigned MipsInstrInfo::
79 isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const
81 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
82 (MI->getOpcode() == Mips::LWC1A) || (MI->getOpcode() == Mips::LDC1)) {
83 if ((MI->getOperand(2).isFrameIndex()) && // is a stack slot
84 (MI->getOperand(1).isImmediate()) && // the imm is zero
85 (isZeroImm(MI->getOperand(1)))) {
86 FrameIndex = MI->getOperand(2).getIndex();
87 return MI->getOperand(0).getReg();
94 /// isStoreToStackSlot - If the specified machine instruction is a direct
95 /// store to a stack slot, return the virtual or physical register number of
96 /// the source reg along with the FrameIndex of the loaded stack slot. If
97 /// not, return 0. This predicate must return 0 if the instruction has
98 /// any side effects other than storing to the stack slot.
99 unsigned MipsInstrInfo::
100 isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const
102 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
103 (MI->getOpcode() == Mips::SWC1A) || (MI->getOpcode() == Mips::SDC1)) {
104 if ((MI->getOperand(0).isFrameIndex()) && // is a stack slot
105 (MI->getOperand(1).isImmediate()) && // the imm is zero
106 (isZeroImm(MI->getOperand(1)))) {
107 FrameIndex = MI->getOperand(0).getIndex();
108 return MI->getOperand(2).getReg();
114 /// insertNoop - If data hazard condition is found insert the target nop
117 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
119 BuildMI(MBB, MI, get(Mips::NOP));
123 copyRegToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
124 unsigned DestReg, unsigned SrcReg,
125 const TargetRegisterClass *DestRC,
126 const TargetRegisterClass *SrcRC) const {
127 if (DestRC != SrcRC) {
128 if ((DestRC == Mips::CPURegsRegisterClass) &&
129 (SrcRC == Mips::FGR32RegisterClass))
130 BuildMI(MBB, I, get(Mips::MFC1), DestReg).addReg(SrcReg);
131 else if ((DestRC == Mips::CPURegsRegisterClass) &&
132 (SrcRC == Mips::AFGR32RegisterClass))
133 BuildMI(MBB, I, get(Mips::MFC1A), DestReg).addReg(SrcReg);
134 else if ((DestRC == Mips::FGR32RegisterClass) &&
135 (SrcRC == Mips::CPURegsRegisterClass))
136 BuildMI(MBB, I, get(Mips::MTC1), DestReg).addReg(SrcReg);
137 else if ((DestRC == Mips::AFGR32RegisterClass) &&
138 (SrcRC == Mips::CPURegsRegisterClass))
139 BuildMI(MBB, I, get(Mips::MTC1A), DestReg).addReg(SrcReg);
141 assert (0 && "DestRC != SrcRC, Can't copy this register");
144 if (DestRC == Mips::CPURegsRegisterClass)
145 BuildMI(MBB, I, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
147 else if (DestRC == Mips::FGR32RegisterClass)
148 BuildMI(MBB, I, get(Mips::FMOV_SO32), DestReg).addReg(SrcReg);
149 else if (DestRC == Mips::AFGR32RegisterClass)
150 BuildMI(MBB, I, get(Mips::FMOV_AS32), DestReg).addReg(SrcReg);
151 else if (DestRC == Mips::AFGR64RegisterClass)
152 BuildMI(MBB, I, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
154 assert (0 && "Can't copy this register");
158 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
159 unsigned SrcReg, bool isKill, int FI,
160 const TargetRegisterClass *RC) const
163 if (RC == Mips::CPURegsRegisterClass)
165 else if (RC == Mips::FGR32RegisterClass)
167 else if (RC == Mips::AFGR32RegisterClass)
169 else if (RC == Mips::AFGR64RegisterClass)
172 assert(0 && "Can't store this register to stack slot");
174 BuildMI(MBB, I, get(Opc)).addReg(SrcReg, false, false, isKill)
175 .addImm(0).addFrameIndex(FI);
178 void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
179 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
180 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
183 if (RC == Mips::CPURegsRegisterClass)
185 else if (RC == Mips::FGR32RegisterClass)
187 else if (RC == Mips::AFGR32RegisterClass)
189 else if (RC == Mips::AFGR64RegisterClass)
192 assert(0 && "Can't store this register");
194 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
195 .addReg(SrcReg, false, false, isKill);
196 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
197 MachineOperand &MO = Addr[i];
199 MIB.addReg(MO.getReg());
200 else if (MO.isImmediate())
201 MIB.addImm(MO.getImm());
203 MIB.addFrameIndex(MO.getIndex());
205 NewMIs.push_back(MIB);
210 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
211 unsigned DestReg, int FI,
212 const TargetRegisterClass *RC) const
215 if (RC == Mips::CPURegsRegisterClass)
217 else if (RC == Mips::FGR32RegisterClass)
219 else if (RC == Mips::AFGR32RegisterClass)
221 else if (RC == Mips::AFGR64RegisterClass)
224 assert(0 && "Can't load this register from stack slot");
226 BuildMI(MBB, I, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
229 void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
230 SmallVectorImpl<MachineOperand> &Addr,
231 const TargetRegisterClass *RC,
232 SmallVectorImpl<MachineInstr*> &NewMIs) const {
234 if (RC == Mips::CPURegsRegisterClass)
236 else if (RC == Mips::FGR32RegisterClass)
238 else if (RC == Mips::AFGR32RegisterClass)
240 else if (RC == Mips::AFGR64RegisterClass)
243 assert(0 && "Can't load this register");
245 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
246 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
247 MachineOperand &MO = Addr[i];
249 MIB.addReg(MO.getReg());
250 else if (MO.isImmediate())
251 MIB.addImm(MO.getImm());
253 MIB.addFrameIndex(MO.getIndex());
255 NewMIs.push_back(MIB);
259 MachineInstr *MipsInstrInfo::
260 foldMemoryOperand(MachineFunction &MF,
262 SmallVectorImpl<unsigned> &Ops, int FI) const
264 if (Ops.size() != 1) return NULL;
266 MachineInstr *NewMI = NULL;
268 switch (MI->getOpcode()) {
270 if ((MI->getOperand(0).isRegister()) &&
271 (MI->getOperand(1).isRegister()) &&
272 (MI->getOperand(1).getReg() == Mips::ZERO) &&
273 (MI->getOperand(2).isRegister())) {
274 if (Ops[0] == 0) { // COPY -> STORE
275 unsigned SrcReg = MI->getOperand(2).getReg();
276 bool isKill = MI->getOperand(2).isKill();
277 NewMI = BuildMI(MF, get(Mips::SW)).addFrameIndex(FI)
278 .addImm(0).addReg(SrcReg, false, false, isKill);
279 } else { // COPY -> LOAD
280 unsigned DstReg = MI->getOperand(0).getReg();
281 bool isDead = MI->getOperand(0).isDead();
282 NewMI = BuildMI(MF, get(Mips::LW))
283 .addReg(DstReg, true, false, false, isDead)
284 .addImm(0).addFrameIndex(FI);
288 case Mips::FMOV_SO32:
289 case Mips::FMOV_AS32:
291 if ((MI->getOperand(0).isRegister()) &&
292 (MI->getOperand(1).isRegister())) {
293 const TargetRegisterClass
294 *RC = RI.getRegClass(MI->getOperand(0).getReg());
295 unsigned StoreOpc, LoadOpc;
297 if (RC == Mips::FGR32RegisterClass) {
298 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
299 } else if (RC == Mips::AFGR32RegisterClass) {
300 LoadOpc = Mips::LWC1A; StoreOpc = Mips::SWC1A;
301 } else if (RC == Mips::AFGR64RegisterClass) {
302 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
304 assert(0 && "foldMemoryOperand register unknown");
306 if (Ops[0] == 0) { // COPY -> STORE
307 unsigned SrcReg = MI->getOperand(1).getReg();
308 bool isKill = MI->getOperand(1).isKill();
309 NewMI = BuildMI(MF, get(StoreOpc)).addFrameIndex(FI)
310 .addImm(0).addReg(SrcReg, false, false, isKill);
311 } else { // COPY -> LOAD
312 unsigned DstReg = MI->getOperand(0).getReg();
313 bool isDead = MI->getOperand(0).isDead();
314 NewMI = BuildMI(MF, get(LoadOpc))
315 .addReg(DstReg, true, false, false, isDead)
316 .addImm(0).addFrameIndex(FI);
325 //===----------------------------------------------------------------------===//
327 //===----------------------------------------------------------------------===//
329 /// GetCondFromBranchOpc - Return the Mips CC that matches
330 /// the correspondent Branch instruction opcode.
331 static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
334 default: return Mips::COND_INVALID;
335 case Mips::BEQ : return Mips::COND_E;
336 case Mips::BNE : return Mips::COND_NE;
337 case Mips::BGTZ : return Mips::COND_GZ;
338 case Mips::BGEZ : return Mips::COND_GEZ;
339 case Mips::BLTZ : return Mips::COND_LZ;
340 case Mips::BLEZ : return Mips::COND_LEZ;
344 /// GetCondBranchFromCond - Return the Branch instruction
345 /// opcode that matches the cc.
346 unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
349 default: assert(0 && "Illegal condition code!");
350 case Mips::COND_E : return Mips::BEQ;
351 case Mips::COND_NE : return Mips::BNE;
352 case Mips::COND_GZ : return Mips::BGTZ;
353 case Mips::COND_GEZ : return Mips::BGEZ;
354 case Mips::COND_LZ : return Mips::BLTZ;
355 case Mips::COND_LEZ : return Mips::BLEZ;
359 /// GetOppositeBranchCondition - Return the inverse of the specified
360 /// condition, e.g. turning COND_E to COND_NE.
361 Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
364 default: assert(0 && "Illegal condition code!");
365 case Mips::COND_E : return Mips::COND_NE;
366 case Mips::COND_NE : return Mips::COND_E;
367 case Mips::COND_GZ : return Mips::COND_LEZ;
368 case Mips::COND_GEZ : return Mips::COND_LZ;
369 case Mips::COND_LZ : return Mips::COND_GEZ;
370 case Mips::COND_LEZ : return Mips::COND_GZ;
371 case Mips::FCOND_F : return Mips::FCOND_T;
372 case Mips::FCOND_UN : return Mips::FCOND_OR;
373 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
374 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
375 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
376 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
377 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
378 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
379 case Mips::FCOND_SF: return Mips::FCOND_ST;
380 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
381 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
382 case Mips::FCOND_NGL: return Mips::FCOND_GL;
383 case Mips::FCOND_LT: return Mips::FCOND_NLT;
384 case Mips::FCOND_NGE: return Mips::FCOND_GE;
385 case Mips::FCOND_LE: return Mips::FCOND_NLE;
386 case Mips::FCOND_NGT: return Mips::FCOND_GT;
390 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
391 MachineBasicBlock *&TBB,
392 MachineBasicBlock *&FBB,
393 std::vector<MachineOperand> &Cond) const
395 // If the block has no terminators, it just falls into the block after it.
396 MachineBasicBlock::iterator I = MBB.end();
397 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
400 // Get the last instruction in the block.
401 MachineInstr *LastInst = I;
403 // If there is only one terminator instruction, process it.
404 unsigned LastOpc = LastInst->getOpcode();
405 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
406 if (!LastInst->getDesc().isBranch())
409 // Unconditional branch
410 if (LastOpc == Mips::J) {
411 TBB = LastInst->getOperand(0).getMBB();
415 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
416 if (BranchCode == Mips::COND_INVALID)
417 return true; // Can't handle indirect branch.
419 // Conditional branch
420 // Block ends with fall-through condbranch.
421 if (LastOpc != Mips::COND_INVALID) {
422 int LastNumOp = LastInst->getNumOperands();
424 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
425 Cond.push_back(MachineOperand::CreateImm(BranchCode));
427 for (int i=0; i<LastNumOp-1; i++) {
428 Cond.push_back(LastInst->getOperand(i));
435 // Get the instruction before it if it is a terminator.
436 MachineInstr *SecondLastInst = I;
438 // If there are three terminators, we don't know what sort of block this is.
439 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
442 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
443 unsigned SecondLastOpc = SecondLastInst->getOpcode();
444 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
446 if (SecondLastOpc != Mips::COND_INVALID && LastOpc == Mips::J) {
447 int SecondNumOp = SecondLastInst->getNumOperands();
449 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
450 Cond.push_back(MachineOperand::CreateImm(BranchCode));
452 for (int i=0; i<SecondNumOp-1; i++) {
453 Cond.push_back(SecondLastInst->getOperand(i));
456 FBB = LastInst->getOperand(0).getMBB();
460 // If the block ends with two unconditional branches, handle it. The last
461 // one is not executed, so remove it.
462 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
463 TBB = SecondLastInst->getOperand(0).getMBB();
465 I->eraseFromParent();
469 // Otherwise, can't handle this.
473 unsigned MipsInstrInfo::
474 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
475 MachineBasicBlock *FBB, const std::vector<MachineOperand> &Cond)
478 // Shouldn't be a fall through.
479 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
480 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
481 "Mips branch conditions can have two|three components!");
483 if (FBB == 0) { // One way branch.
485 // Unconditional branch?
486 BuildMI(&MBB, get(Mips::J)).addMBB(TBB);
488 // Conditional branch.
489 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
490 const TargetInstrDesc &TID = get(Opc);
492 if (TID.getNumOperands() == 3)
493 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
494 .addReg(Cond[2].getReg())
497 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
504 // Two-way Conditional branch.
505 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
506 const TargetInstrDesc &TID = get(Opc);
508 if (TID.getNumOperands() == 3)
509 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
512 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addMBB(TBB);
514 BuildMI(&MBB, get(Mips::J)).addMBB(FBB);
518 unsigned MipsInstrInfo::
519 RemoveBranch(MachineBasicBlock &MBB) const
521 MachineBasicBlock::iterator I = MBB.end();
522 if (I == MBB.begin()) return 0;
524 if (I->getOpcode() != Mips::J &&
525 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
528 // Remove the branch.
529 I->eraseFromParent();
533 if (I == MBB.begin()) return 1;
535 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
538 // Remove the branch.
539 I->eraseFromParent();
543 /// BlockHasNoFallThrough - Analyse if MachineBasicBlock does not
544 /// fall-through into its successor block.
546 BlockHasNoFallThrough(MachineBasicBlock &MBB) const
548 if (MBB.empty()) return false;
550 switch (MBB.back().getOpcode()) {
551 case Mips::RET: // Return.
552 case Mips::JR: // Indirect branch.
553 case Mips::J: // Uncond branch.
555 default: return false;
559 /// ReverseBranchCondition - Return the inverse opcode of the
560 /// specified Branch instruction.
562 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const
564 assert( (Cond.size() == 3 || Cond.size() == 2) &&
565 "Invalid Mips branch condition!");
566 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));