blob: ccdcbc63075c7f68af527d30125a8b72a5cfea48 [file] [log] [blame]
Wesley Pecka70f28c2010-02-23 19:15:24 +00001//===- MBlazeInstrInfo.cpp - MBlaze Instruction Information -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the MBlaze implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
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"
22
23using namespace llvm;
24
25MBlazeInstrInfo::MBlazeInstrInfo(MBlazeTargetMachine &tm)
26 : TargetInstrInfoImpl(MBlazeInsts, array_lengthof(MBlazeInsts)),
27 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
28
29static bool isZeroImm(const MachineOperand &op) {
30 return op.isImm() && op.getImm() == 0;
31}
32
33/// Return true if the instruction is a register to register move and
34/// leave the source and dest operands in the passed parameters.
35bool MBlazeInstrInfo::
36isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg,
37 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
38 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
39
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();
46 return true;
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();
51 return true;
52 }
53 }
54
55 // addi $dst, $src, 0
56 // ori $dst, $src, 0
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();
61 return true;
62 }
63 }
64
65 return false;
66}
67
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.
73unsigned MBlazeInstrInfo::
74isLoadFromStackSlot(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();
81 }
82 }
83
84 return 0;
85}
86
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.
92unsigned MBlazeInstrInfo::
93isStoreToStackSlot(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();
100 }
101 }
102 return 0;
103}
104
105/// insertNoop - If data hazard condition is found insert the target nop
106/// instruction.
107void MBlazeInstrInfo::
108insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000109 DebugLoc DL;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000110 BuildMI(MBB, MI, DL, get(MBlaze::NOP));
111}
112
Jakob Stoklund Olesene6afcf82010-07-11 06:53:27 +0000113void MBlazeInstrInfo::
114copyPhysReg(MachineBasicBlock &MBB,
115 MachineBasicBlock::iterator I, DebugLoc DL,
116 unsigned DestReg, unsigned SrcReg,
117 bool KillSrc) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000118 llvm::BuildMI(MBB, I, DL, get(MBlaze::ADD), DestReg)
Jakob Stoklund Olesene6afcf82010-07-11 06:53:27 +0000119 .addReg(SrcReg, getKillRegState(KillSrc)).addReg(MBlaze::R0);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000120}
121
122void MBlazeInstrInfo::
123storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
124 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000125 const TargetRegisterClass *RC,
126 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000127 DebugLoc DL;
128 BuildMI(MBB, I, DL, get(MBlaze::SWI)).addReg(SrcReg,getKillRegState(isKill))
Wesley Pecka70f28c2010-02-23 19:15:24 +0000129 .addImm(0).addFrameIndex(FI);
130}
131
132void MBlazeInstrInfo::
133loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
134 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000135 const TargetRegisterClass *RC,
136 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000137 DebugLoc DL;
138 BuildMI(MBB, I, DL, get(MBlaze::LWI), DestReg)
Wesley Pecka70f28c2010-02-23 19:15:24 +0000139 .addImm(0).addFrameIndex(FI);
140}
141
142MachineInstr *MBlazeInstrInfo::
143foldMemoryOperandImpl(MachineFunction &MF,
144 MachineInstr* MI,
145 const SmallVectorImpl<unsigned> &Ops, int FI) const {
146 if (Ops.size() != 1) return NULL;
147
148 MachineInstr *NewMI = NULL;
149
150 switch (MI->getOpcode()) {
151 case MBlaze::OR:
152 case MBlaze::ADD:
153 if ((MI->getOperand(0).isReg()) &&
154 (MI->getOperand(2).isReg()) &&
155 (MI->getOperand(2).getReg() == MBlaze::R0) &&
156 (MI->getOperand(1).isReg())) {
157 if (Ops[0] == 0) { // COPY -> STORE
158 unsigned SrcReg = MI->getOperand(1).getReg();
159 bool isKill = MI->getOperand(1).isKill();
160 bool isUndef = MI->getOperand(1).isUndef();
161 NewMI = BuildMI(MF, MI->getDebugLoc(), get(MBlaze::SW))
162 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
163 .addImm(0).addFrameIndex(FI);
164 } else { // COPY -> LOAD
165 unsigned DstReg = MI->getOperand(0).getReg();
166 bool isDead = MI->getOperand(0).isDead();
167 bool isUndef = MI->getOperand(0).isUndef();
168 NewMI = BuildMI(MF, MI->getDebugLoc(), get(MBlaze::LW))
169 .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
170 getUndefRegState(isUndef))
171 .addImm(0).addFrameIndex(FI);
172 }
173 }
174 break;
175 }
176
177 return NewMI;
178}
179
180//===----------------------------------------------------------------------===//
181// Branch Analysis
182//===----------------------------------------------------------------------===//
183unsigned MBlazeInstrInfo::
184InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
185 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000186 const SmallVectorImpl<MachineOperand> &Cond,
187 DebugLoc DL) const {
Wesley Pecka70f28c2010-02-23 19:15:24 +0000188 // Can only insert uncond branches so far.
189 assert(Cond.empty() && !FBB && TBB && "Can only handle uncond branches!");
Stuart Hastings3bf91252010-06-17 22:43:56 +0000190 BuildMI(&MBB, DL, get(MBlaze::BRI)).addMBB(TBB);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000191 return 1;
192}
193
194/// getGlobalBaseReg - Return a virtual register initialized with the
195/// the global base register value. Output instructions required to
196/// initialize the register in the function entry block, if necessary.
197///
198unsigned MBlazeInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
199 MBlazeFunctionInfo *MBlazeFI = MF->getInfo<MBlazeFunctionInfo>();
200 unsigned GlobalBaseReg = MBlazeFI->getGlobalBaseReg();
201 if (GlobalBaseReg != 0)
202 return GlobalBaseReg;
203
204 // Insert the set of GlobalBaseReg into the first MBB of the function
205 MachineBasicBlock &FirstMBB = MF->front();
206 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
207 MachineRegisterInfo &RegInfo = MF->getRegInfo();
208 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
209
210 GlobalBaseReg = RegInfo.createVirtualRegister(MBlaze::CPURegsRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000211 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
212 GlobalBaseReg).addReg(MBlaze::R20);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000213 RegInfo.addLiveIn(MBlaze::R20);
214
215 MBlazeFI->setGlobalBaseReg(GlobalBaseReg);
216 return GlobalBaseReg;
217}