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 "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)),
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(0).isFrameIndex()) && // is a stack slot
104 (MI->getOperand(1).isImmediate()) && // the imm is zero
105 (isZeroImm(MI->getOperand(1)))) {
106 FrameIndex = MI->getOperand(0).getIndex();
107 return MI->getOperand(2).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);
140 assert (0 && "DestRC != SrcRC, Can't copy this register");
143 if (DestRC == Mips::CPURegsRegisterClass)
144 BuildMI(MBB, I, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
146 else if (DestRC == Mips::FGR32RegisterClass)
147 BuildMI(MBB, I, get(Mips::FMOV_SO32), DestReg).addReg(SrcReg);
148 else if (DestRC == Mips::AFGR32RegisterClass)
149 BuildMI(MBB, I, get(Mips::FMOV_AS32), DestReg).addReg(SrcReg);
150 else if (DestRC == Mips::AFGR64RegisterClass)
151 BuildMI(MBB, I, get(Mips::FMOV_D32), DestReg).addReg(SrcReg);
153 assert (0 && "Can't copy this register");
157 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
158 unsigned SrcReg, bool isKill, int FI,
159 const TargetRegisterClass *RC) const
162 if (RC == Mips::CPURegsRegisterClass)
164 else if (RC == Mips::FGR32RegisterClass)
166 else if (RC == Mips::AFGR32RegisterClass)
168 else if (RC == Mips::AFGR64RegisterClass)
171 assert(0 && "Can't store this register to stack slot");
173 BuildMI(MBB, I, get(Opc)).addReg(SrcReg, false, false, isKill)
174 .addImm(0).addFrameIndex(FI);
177 void MipsInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
178 bool isKill, SmallVectorImpl<MachineOperand> &Addr,
179 const TargetRegisterClass *RC, SmallVectorImpl<MachineInstr*> &NewMIs) const
182 if (RC == Mips::CPURegsRegisterClass)
184 else if (RC == Mips::FGR32RegisterClass)
186 else if (RC == Mips::AFGR32RegisterClass)
188 else if (RC == Mips::AFGR64RegisterClass)
191 assert(0 && "Can't store this register");
193 MachineInstrBuilder MIB = BuildMI(MF, get(Opc))
194 .addReg(SrcReg, false, false, isKill);
195 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
196 MachineOperand &MO = Addr[i];
198 MIB.addReg(MO.getReg());
199 else if (MO.isImmediate())
200 MIB.addImm(MO.getImm());
202 MIB.addFrameIndex(MO.getIndex());
204 NewMIs.push_back(MIB);
209 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
210 unsigned DestReg, int FI,
211 const TargetRegisterClass *RC) const
214 if (RC == Mips::CPURegsRegisterClass)
216 else if (RC == Mips::FGR32RegisterClass)
218 else if (RC == Mips::AFGR32RegisterClass)
220 else if (RC == Mips::AFGR64RegisterClass)
223 assert(0 && "Can't load this register from stack slot");
225 BuildMI(MBB, I, get(Opc), DestReg).addImm(0).addFrameIndex(FI);
228 void MipsInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
229 SmallVectorImpl<MachineOperand> &Addr,
230 const TargetRegisterClass *RC,
231 SmallVectorImpl<MachineInstr*> &NewMIs) 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");
244 MachineInstrBuilder MIB = BuildMI(MF, get(Opc), DestReg);
245 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
246 MachineOperand &MO = Addr[i];
248 MIB.addReg(MO.getReg());
249 else if (MO.isImmediate())
250 MIB.addImm(MO.getImm());
252 MIB.addFrameIndex(MO.getIndex());
254 NewMIs.push_back(MIB);
258 MachineInstr *MipsInstrInfo::
259 foldMemoryOperand(MachineFunction &MF,
261 SmallVectorImpl<unsigned> &Ops, int FI) const
263 if (Ops.size() != 1) return NULL;
265 MachineInstr *NewMI = NULL;
267 switch (MI->getOpcode()) {
269 if ((MI->getOperand(0).isRegister()) &&
270 (MI->getOperand(1).isRegister()) &&
271 (MI->getOperand(1).getReg() == Mips::ZERO) &&
272 (MI->getOperand(2).isRegister())) {
273 if (Ops[0] == 0) { // COPY -> STORE
274 unsigned SrcReg = MI->getOperand(2).getReg();
275 bool isKill = MI->getOperand(2).isKill();
276 NewMI = BuildMI(MF, get(Mips::SW)).addFrameIndex(FI)
277 .addImm(0).addReg(SrcReg, false, false, isKill);
278 } else { // COPY -> LOAD
279 unsigned DstReg = MI->getOperand(0).getReg();
280 bool isDead = MI->getOperand(0).isDead();
281 NewMI = BuildMI(MF, get(Mips::LW))
282 .addReg(DstReg, true, false, false, isDead)
283 .addImm(0).addFrameIndex(FI);
287 case Mips::FMOV_SO32:
288 case Mips::FMOV_AS32:
290 if ((MI->getOperand(0).isRegister()) &&
291 (MI->getOperand(1).isRegister())) {
292 const TargetRegisterClass
293 *RC = RI.getRegClass(MI->getOperand(0).getReg());
294 unsigned StoreOpc, LoadOpc;
296 if (RC == Mips::FGR32RegisterClass) {
297 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
298 } else if (RC == Mips::AFGR32RegisterClass) {
299 LoadOpc = Mips::LWC1A; StoreOpc = Mips::SWC1A;
300 } else if (RC == Mips::AFGR64RegisterClass) {
301 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
303 assert(0 && "foldMemoryOperand register unknown");
305 if (Ops[0] == 0) { // COPY -> STORE
306 unsigned SrcReg = MI->getOperand(1).getReg();
307 bool isKill = MI->getOperand(1).isKill();
308 NewMI = BuildMI(MF, get(StoreOpc)).addFrameIndex(FI)
309 .addImm(0).addReg(SrcReg, false, false, isKill);
310 } else { // COPY -> LOAD
311 unsigned DstReg = MI->getOperand(0).getReg();
312 bool isDead = MI->getOperand(0).isDead();
313 NewMI = BuildMI(MF, get(LoadOpc))
314 .addReg(DstReg, true, false, false, isDead)
315 .addImm(0).addFrameIndex(FI);
324 //===----------------------------------------------------------------------===//
326 //===----------------------------------------------------------------------===//
328 /// GetCondFromBranchOpc - Return the Mips CC that matches
329 /// the correspondent Branch instruction opcode.
330 static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
333 default: return Mips::COND_INVALID;
334 case Mips::BEQ : return Mips::COND_E;
335 case Mips::BNE : return Mips::COND_NE;
336 case Mips::BGTZ : return Mips::COND_GZ;
337 case Mips::BGEZ : return Mips::COND_GEZ;
338 case Mips::BLTZ : return Mips::COND_LZ;
339 case Mips::BLEZ : return Mips::COND_LEZ;
343 /// GetCondBranchFromCond - Return the Branch instruction
344 /// opcode that matches the cc.
345 unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
348 default: assert(0 && "Illegal condition code!");
349 case Mips::COND_E : return Mips::BEQ;
350 case Mips::COND_NE : return Mips::BNE;
351 case Mips::COND_GZ : return Mips::BGTZ;
352 case Mips::COND_GEZ : return Mips::BGEZ;
353 case Mips::COND_LZ : return Mips::BLTZ;
354 case Mips::COND_LEZ : return Mips::BLEZ;
358 /// GetOppositeBranchCondition - Return the inverse of the specified
359 /// condition, e.g. turning COND_E to COND_NE.
360 Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
363 default: assert(0 && "Illegal condition code!");
364 case Mips::COND_E : return Mips::COND_NE;
365 case Mips::COND_NE : return Mips::COND_E;
366 case Mips::COND_GZ : return Mips::COND_LEZ;
367 case Mips::COND_GEZ : return Mips::COND_LZ;
368 case Mips::COND_LZ : return Mips::COND_GEZ;
369 case Mips::COND_LEZ : return Mips::COND_GZ;
370 case Mips::FCOND_F : return Mips::FCOND_T;
371 case Mips::FCOND_UN : return Mips::FCOND_OR;
372 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
373 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
374 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
375 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
376 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
377 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
378 case Mips::FCOND_SF: return Mips::FCOND_ST;
379 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
380 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
381 case Mips::FCOND_NGL: return Mips::FCOND_GL;
382 case Mips::FCOND_LT: return Mips::FCOND_NLT;
383 case Mips::FCOND_NGE: return Mips::FCOND_GE;
384 case Mips::FCOND_LE: return Mips::FCOND_NLE;
385 case Mips::FCOND_NGT: return Mips::FCOND_GT;
389 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
390 MachineBasicBlock *&TBB,
391 MachineBasicBlock *&FBB,
392 std::vector<MachineOperand> &Cond) const
394 // If the block has no terminators, it just falls into the block after it.
395 MachineBasicBlock::iterator I = MBB.end();
396 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
399 // Get the last instruction in the block.
400 MachineInstr *LastInst = I;
402 // If there is only one terminator instruction, process it.
403 unsigned LastOpc = LastInst->getOpcode();
404 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
405 if (!LastInst->getDesc().isBranch())
408 // Unconditional branch
409 if (LastOpc == Mips::J) {
410 TBB = LastInst->getOperand(0).getMBB();
414 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
415 if (BranchCode == Mips::COND_INVALID)
416 return true; // Can't handle indirect branch.
418 // Conditional branch
419 // Block ends with fall-through condbranch.
420 if (LastOpc != Mips::COND_INVALID) {
421 int LastNumOp = LastInst->getNumOperands();
423 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
424 Cond.push_back(MachineOperand::CreateImm(BranchCode));
426 for (int i=0; i<LastNumOp-1; i++) {
427 Cond.push_back(LastInst->getOperand(i));
434 // Get the instruction before it if it is a terminator.
435 MachineInstr *SecondLastInst = I;
437 // If there are three terminators, we don't know what sort of block this is.
438 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
441 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
442 unsigned SecondLastOpc = SecondLastInst->getOpcode();
443 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
445 if (SecondLastOpc != Mips::COND_INVALID && LastOpc == Mips::J) {
446 int SecondNumOp = SecondLastInst->getNumOperands();
448 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
449 Cond.push_back(MachineOperand::CreateImm(BranchCode));
451 for (int i=0; i<SecondNumOp-1; i++) {
452 Cond.push_back(SecondLastInst->getOperand(i));
455 FBB = LastInst->getOperand(0).getMBB();
459 // If the block ends with two unconditional branches, handle it. The last
460 // one is not executed, so remove it.
461 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
462 TBB = SecondLastInst->getOperand(0).getMBB();
464 I->eraseFromParent();
468 // Otherwise, can't handle this.
472 unsigned MipsInstrInfo::
473 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
474 MachineBasicBlock *FBB, const std::vector<MachineOperand> &Cond)
477 // Shouldn't be a fall through.
478 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
479 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
480 "Mips branch conditions can have two|three components!");
482 if (FBB == 0) { // One way branch.
484 // Unconditional branch?
485 BuildMI(&MBB, get(Mips::J)).addMBB(TBB);
487 // Conditional branch.
488 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
489 const TargetInstrDesc &TID = get(Opc);
491 if (TID.getNumOperands() == 3)
492 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
493 .addReg(Cond[2].getReg())
496 BuildMI(&MBB, TID).addReg(Cond[1].getReg())
503 // Two-way Conditional branch.
504 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
505 const TargetInstrDesc &TID = get(Opc);
507 if (TID.getNumOperands() == 3)
508 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
511 BuildMI(&MBB, TID).addReg(Cond[1].getReg()).addMBB(TBB);
513 BuildMI(&MBB, get(Mips::J)).addMBB(FBB);
517 unsigned MipsInstrInfo::
518 RemoveBranch(MachineBasicBlock &MBB) const
520 MachineBasicBlock::iterator I = MBB.end();
521 if (I == MBB.begin()) return 0;
523 if (I->getOpcode() != Mips::J &&
524 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
527 // Remove the branch.
528 I->eraseFromParent();
532 if (I == MBB.begin()) return 1;
534 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
537 // Remove the branch.
538 I->eraseFromParent();
542 /// BlockHasNoFallThrough - Analyse if MachineBasicBlock does not
543 /// fall-through into its successor block.
545 BlockHasNoFallThrough(MachineBasicBlock &MBB) const
547 if (MBB.empty()) return false;
549 switch (MBB.back().getOpcode()) {
550 case Mips::RET: // Return.
551 case Mips::JR: // Indirect branch.
552 case Mips::J: // Uncond branch.
554 default: return false;
558 /// ReverseBranchCondition - Return the inverse opcode of the
559 /// specified Branch instruction.
561 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const
563 assert( (Cond.size() == 3 || Cond.size() == 2) &&
564 "Invalid Mips branch condition!");
565 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));