1 //===- MBlazeInstrInfo.cpp - MBlaze 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 MBlaze implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "MBlazeInstrInfo.h"
15 #include "MBlazeTargetMachine.h"
16 #include "MBlazeMachineFunction.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "MBlazeGenInstrInfo.inc"
25 MBlazeInstrInfo::MBlazeInstrInfo(MBlazeTargetMachine &tm)
26 : TargetInstrInfoImpl(MBlazeInsts, array_lengthof(MBlazeInsts)),
27 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
29 static bool isZeroImm(const MachineOperand &op) {
30 return op.isImm() && op.getImm() == 0;
33 /// Return true if the instruction is a register to register move and
34 /// leave the source and dest operands in the passed parameters.
35 bool MBlazeInstrInfo::
36 isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg,
37 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
38 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
40 // add $dst, $src, $zero || addu $dst, $zero, $src
41 // or $dst, $src, $zero || or $dst, $zero, $src
42 if ((MI.getOpcode() == MBlaze::ADD) || (MI.getOpcode() == MBlaze::OR)) {
43 if (MI.getOperand(1).isReg() && MI.getOperand(1).getReg() == MBlaze::R0) {
44 DstReg = MI.getOperand(0).getReg();
45 SrcReg = MI.getOperand(2).getReg();
47 } else if (MI.getOperand(2).isReg() &&
48 MI.getOperand(2).getReg() == MBlaze::R0) {
49 DstReg = MI.getOperand(0).getReg();
50 SrcReg = MI.getOperand(1).getReg();
57 if ((MI.getOpcode() == MBlaze::ADDI) || (MI.getOpcode() == MBlaze::ORI)) {
58 if ((MI.getOperand(1).isReg()) && (isZeroImm(MI.getOperand(2)))) {
59 DstReg = MI.getOperand(0).getReg();
60 SrcReg = MI.getOperand(1).getReg();
68 /// isLoadFromStackSlot - If the specified machine instruction is a direct
69 /// load from a stack slot, return the virtual or physical register number of
70 /// the destination along with the FrameIndex of the loaded stack slot. If
71 /// not, return 0. This predicate must return 0 if the instruction has
72 /// any side effects other than loading from the stack slot.
73 unsigned MBlazeInstrInfo::
74 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const {
75 if (MI->getOpcode() == MBlaze::LWI) {
76 if ((MI->getOperand(2).isFI()) && // is a stack slot
77 (MI->getOperand(1).isImm()) && // the imm is zero
78 (isZeroImm(MI->getOperand(1)))) {
79 FrameIndex = MI->getOperand(2).getIndex();
80 return MI->getOperand(0).getReg();
87 /// isStoreToStackSlot - If the specified machine instruction is a direct
88 /// store to a stack slot, return the virtual or physical register number of
89 /// the source reg along with the FrameIndex of the loaded stack slot. If
90 /// not, return 0. This predicate must return 0 if the instruction has
91 /// any side effects other than storing to the stack slot.
92 unsigned MBlazeInstrInfo::
93 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const {
94 if (MI->getOpcode() == MBlaze::SWI) {
95 if ((MI->getOperand(2).isFI()) && // is a stack slot
96 (MI->getOperand(1).isImm()) && // the imm is zero
97 (isZeroImm(MI->getOperand(1)))) {
98 FrameIndex = MI->getOperand(2).getIndex();
99 return MI->getOperand(0).getReg();
105 /// insertNoop - If data hazard condition is found insert the target nop
107 void MBlazeInstrInfo::
108 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const {
110 BuildMI(MBB, MI, DL, get(MBlaze::NOP));
113 void MBlazeInstrInfo::
114 copyPhysReg(MachineBasicBlock &MBB,
115 MachineBasicBlock::iterator I, DebugLoc DL,
116 unsigned DestReg, unsigned SrcReg,
117 bool KillSrc) const {
118 llvm::BuildMI(MBB, I, DL, get(MBlaze::ADD), DestReg)
119 .addReg(SrcReg, getKillRegState(KillSrc)).addReg(MBlaze::R0);
122 void MBlazeInstrInfo::
123 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
124 unsigned SrcReg, bool isKill, int FI,
125 const TargetRegisterClass *RC,
126 const TargetRegisterInfo *TRI) const {
128 BuildMI(MBB, I, DL, get(MBlaze::SWI)).addReg(SrcReg,getKillRegState(isKill))
129 .addImm(0).addFrameIndex(FI);
132 void MBlazeInstrInfo::
133 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
134 unsigned DestReg, int FI,
135 const TargetRegisterClass *RC,
136 const TargetRegisterInfo *TRI) const {
138 BuildMI(MBB, I, DL, get(MBlaze::LWI), DestReg)
139 .addImm(0).addFrameIndex(FI);
142 //===----------------------------------------------------------------------===//
144 //===----------------------------------------------------------------------===//
145 unsigned MBlazeInstrInfo::
146 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
147 MachineBasicBlock *FBB,
148 const SmallVectorImpl<MachineOperand> &Cond,
150 // Can only insert uncond branches so far.
151 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
152 BuildMI(&MBB, DL, get(MBlaze::BRI)).addMBB(TBB);
156 /// getGlobalBaseReg - Return a virtual register initialized with the
157 /// the global base register value. Output instructions required to
158 /// initialize the register in the function entry block, if necessary.
160 unsigned MBlazeInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
161 MBlazeFunctionInfo *MBlazeFI = MF->getInfo<MBlazeFunctionInfo>();
162 unsigned GlobalBaseReg = MBlazeFI->getGlobalBaseReg();
163 if (GlobalBaseReg != 0)
164 return GlobalBaseReg;
166 // Insert the set of GlobalBaseReg into the first MBB of the function
167 MachineBasicBlock &FirstMBB = MF->front();
168 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
169 MachineRegisterInfo &RegInfo = MF->getRegInfo();
170 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
172 GlobalBaseReg = RegInfo.createVirtualRegister(MBlaze::CPURegsRegisterClass);
173 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
174 GlobalBaseReg).addReg(MBlaze::R20);
175 RegInfo.addLiveIn(MBlaze::R20);
177 MBlazeFI->setGlobalBaseReg(GlobalBaseReg);
178 return GlobalBaseReg;