blob: 808403067e82ee3c9ac994294030c27342d980c1 [file] [log] [blame]
Bruno Cardoso Lopes972f5892007-06-06 07:42:06 +00001//===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Bruno Cardoso Lopes and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the Mips implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Mips.h"
15#include "MipsInstrInfo.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
17#include "MipsGenInstrInfo.inc"
18
19using namespace llvm;
20
21// TODO: Add the subtarget support on this constructor
22MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
23 : TargetInstrInfo(MipsInsts, sizeof(MipsInsts)/sizeof(MipsInsts[0])),
24 TM(tm), RI(*this) {}
25
26static bool isZeroImm(const MachineOperand &op) {
27 return op.isImmediate() && op.getImmedValue() == 0;
28}
29
30/// Return true if the instruction is a register to register move and
31/// leave the source and dest operands in the passed parameters.
32bool MipsInstrInfo::
33isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg) const
34{
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 {
39 if (MI.getOperand(1).getReg() == Mips::ZERO) {
40 DstReg = MI.getOperand(0).getReg();
41 SrcReg = MI.getOperand(2).getReg();
42 return true;
43 } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
44 DstReg = MI.getOperand(0).getReg();
45 SrcReg = MI.getOperand(1).getReg();
46 return true;
47 }
48 }
49
50 // addiu $dst, $src, 0
51 if (MI.getOpcode() == Mips::ADDiu)
52 {
53 if ((MI.getOperand(1).isRegister()) && (isZeroImm(MI.getOperand(2)))) {
54 DstReg = MI.getOperand(0).getReg();
55 SrcReg = MI.getOperand(1).getReg();
56 return true;
57 }
58 }
59 return false;
60}
61
62/// isLoadFromStackSlot - If the specified machine instruction is a direct
63/// load from a stack slot, return the virtual or physical register number of
64/// the destination along with the FrameIndex of the loaded stack slot. If
65/// not, return 0. This predicate must return 0 if the instruction has
66/// any side effects other than loading from the stack slot.
67unsigned MipsInstrInfo::
68isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const
69{
70 // TODO: add lhu, lbu ???
71 if (MI->getOpcode() == Mips::LW)
72 {
73 if ((MI->getOperand(2).isFrameIndex()) && // is a stack slot
74 (MI->getOperand(1).isImmediate()) && // the imm is zero
75 (isZeroImm(MI->getOperand(1))))
76 {
77 FrameIndex = MI->getOperand(2).getFrameIndex();
78 return MI->getOperand(0).getReg();
79 }
80 }
81
82 return 0;
83}
84
85/// isStoreToStackSlot - If the specified machine instruction is a direct
86/// store to a stack slot, return the virtual or physical register number of
87/// the source reg along with the FrameIndex of the loaded stack slot. If
88/// not, return 0. This predicate must return 0 if the instruction has
89/// any side effects other than storing to the stack slot.
90unsigned MipsInstrInfo::
91isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const
92{
93 // TODO: add sb, sh ???
94 if (MI->getOpcode() == Mips::SW) {
95 if ((MI->getOperand(0).isFrameIndex()) && // is a stack slot
96 (MI->getOperand(1).isImmediate()) && // the imm is zero
97 (isZeroImm(MI->getOperand(1))))
98 {
99 FrameIndex = MI->getOperand(0).getFrameIndex();
100 return MI->getOperand(2).getReg();
101 }
102 }
103 return 0;
104}
105
106unsigned MipsInstrInfo::
107InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
108 MachineBasicBlock *FBB, const std::vector<MachineOperand> &Cond)
109 const
110{
111 // TODO: add Mips::J here.
112 assert(0 && "Cant handle any kind of branches!");
113 return 1;
114}