blob: d7c11f2a6995b8567d084b7ae58b182e4463720c [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
Wesley Pecka70f28c2010-02-23 19:15:24 +000033/// isLoadFromStackSlot - If the specified machine instruction is a direct
34/// load from a stack slot, return the virtual or physical register number of
35/// the destination along with the FrameIndex of the loaded stack slot. If
36/// not, return 0. This predicate must return 0 if the instruction has
37/// any side effects other than loading from the stack slot.
38unsigned MBlazeInstrInfo::
39isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const {
40 if (MI->getOpcode() == MBlaze::LWI) {
Wesley Peck41400da2010-11-12 23:30:17 +000041 if ((MI->getOperand(1).isFI()) && // is a stack slot
42 (MI->getOperand(2).isImm()) && // the imm is zero
43 (isZeroImm(MI->getOperand(2)))) {
44 FrameIndex = MI->getOperand(1).getIndex();
Wesley Pecka70f28c2010-02-23 19:15:24 +000045 return MI->getOperand(0).getReg();
46 }
47 }
48
49 return 0;
50}
51
52/// isStoreToStackSlot - If the specified machine instruction is a direct
53/// store to a stack slot, return the virtual or physical register number of
54/// the source reg along with the FrameIndex of the loaded stack slot. If
55/// not, return 0. This predicate must return 0 if the instruction has
56/// any side effects other than storing to the stack slot.
57unsigned MBlazeInstrInfo::
58isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const {
59 if (MI->getOpcode() == MBlaze::SWI) {
Wesley Peck41400da2010-11-12 23:30:17 +000060 if ((MI->getOperand(1).isFI()) && // is a stack slot
61 (MI->getOperand(2).isImm()) && // the imm is zero
62 (isZeroImm(MI->getOperand(2)))) {
63 FrameIndex = MI->getOperand(1).getIndex();
Wesley Pecka70f28c2010-02-23 19:15:24 +000064 return MI->getOperand(0).getReg();
65 }
66 }
67 return 0;
68}
69
70/// insertNoop - If data hazard condition is found insert the target nop
71/// instruction.
72void MBlazeInstrInfo::
73insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000074 DebugLoc DL;
Wesley Pecka70f28c2010-02-23 19:15:24 +000075 BuildMI(MBB, MI, DL, get(MBlaze::NOP));
76}
77
Jakob Stoklund Olesene6afcf82010-07-11 06:53:27 +000078void MBlazeInstrInfo::
79copyPhysReg(MachineBasicBlock &MBB,
80 MachineBasicBlock::iterator I, DebugLoc DL,
81 unsigned DestReg, unsigned SrcReg,
82 bool KillSrc) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000083 llvm::BuildMI(MBB, I, DL, get(MBlaze::ADD), DestReg)
Jakob Stoklund Olesene6afcf82010-07-11 06:53:27 +000084 .addReg(SrcReg, getKillRegState(KillSrc)).addReg(MBlaze::R0);
Wesley Pecka70f28c2010-02-23 19:15:24 +000085}
86
87void MBlazeInstrInfo::
88storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
89 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +000090 const TargetRegisterClass *RC,
91 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000092 DebugLoc DL;
93 BuildMI(MBB, I, DL, get(MBlaze::SWI)).addReg(SrcReg,getKillRegState(isKill))
Wesley Peck41400da2010-11-12 23:30:17 +000094 .addFrameIndex(FI).addImm(0); //.addFrameIndex(FI);
Wesley Pecka70f28c2010-02-23 19:15:24 +000095}
96
97void MBlazeInstrInfo::
98loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
99 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000100 const TargetRegisterClass *RC,
101 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000102 DebugLoc DL;
103 BuildMI(MBB, I, DL, get(MBlaze::LWI), DestReg)
Wesley Peck41400da2010-11-12 23:30:17 +0000104 .addFrameIndex(FI).addImm(0); //.addFrameIndex(FI);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000105}
106
Wesley Pecka70f28c2010-02-23 19:15:24 +0000107//===----------------------------------------------------------------------===//
108// Branch Analysis
109//===----------------------------------------------------------------------===//
Wesley Peck46a928b2010-11-21 21:53:36 +0000110bool MBlazeInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
111 MachineBasicBlock *&TBB,
112 MachineBasicBlock *&FBB,
113 SmallVectorImpl<MachineOperand> &Cond,
114 bool AllowModify) const {
115 // If the block has no terminators, it just falls into the block after it.
116 MachineBasicBlock::iterator I = MBB.end();
117 if (I == MBB.begin())
118 return false;
119 --I;
120 while (I->isDebugValue()) {
121 if (I == MBB.begin())
122 return false;
123 --I;
124 }
125 if (!isUnpredicatedTerminator(I))
126 return false;
127
128 // Get the last instruction in the block.
129 MachineInstr *LastInst = I;
130
131 // If there is only one terminator instruction, process it.
132 unsigned LastOpc = LastInst->getOpcode();
133 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
134 if (MBlaze::isUncondBranchOpcode(LastOpc)) {
135 TBB = LastInst->getOperand(0).getMBB();
136 return false;
137 }
138 if (MBlaze::isCondBranchOpcode(LastOpc)) {
139 // Block ends with fall-through condbranch.
140 TBB = LastInst->getOperand(1).getMBB();
141 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
142 Cond.push_back(LastInst->getOperand(0));
143 return false;
144 }
145 // Otherwise, don't know what this is.
146 return true;
147 }
148
149 // Get the instruction before it if it's a terminator.
150 MachineInstr *SecondLastInst = I;
151
152 // If there are three terminators, we don't know what sort of block this is.
153 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
154 return true;
155
156 // If the block ends with something like BEQID then BRID, handle it.
157 if (MBlaze::isCondBranchOpcode(SecondLastInst->getOpcode()) &&
158 MBlaze::isUncondBranchOpcode(LastInst->getOpcode())) {
159 TBB = SecondLastInst->getOperand(1).getMBB();
160 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
161 Cond.push_back(SecondLastInst->getOperand(0));
162 FBB = LastInst->getOperand(0).getMBB();
163 return false;
164 }
165
166 // If the block ends with two unconditional branches, handle it.
167 // The second one is not executed, so remove it.
168 if (MBlaze::isUncondBranchOpcode(SecondLastInst->getOpcode()) &&
169 MBlaze::isUncondBranchOpcode(LastInst->getOpcode())) {
170 TBB = SecondLastInst->getOperand(0).getMBB();
171 I = LastInst;
172 if (AllowModify)
173 I->eraseFromParent();
174 return false;
175 }
176
177 // Otherwise, can't handle this.
178 return true;
179}
180
Wesley Pecka70f28c2010-02-23 19:15:24 +0000181unsigned MBlazeInstrInfo::
182InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
183 MachineBasicBlock *FBB,
Stuart Hastings3bf91252010-06-17 22:43:56 +0000184 const SmallVectorImpl<MachineOperand> &Cond,
185 DebugLoc DL) const {
Wesley Peck46a928b2010-11-21 21:53:36 +0000186 // Shouldn't be a fall through.
187 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
188 assert((Cond.size() == 2 || Cond.size() == 0) &&
189 "MBlaze branch conditions have two components!");
190
191 unsigned Opc = MBlaze::BRID;
192 if (!Cond.empty())
193 Opc = (unsigned)Cond[0].getImm();
194
195 if (FBB == 0) {
196 if (Cond.empty()) // Unconditional branch
197 BuildMI(&MBB, DL, get(Opc)).addMBB(TBB);
198 else // Conditional branch
199 BuildMI(&MBB, DL, get(Opc)).addReg(Cond[1].getReg()).addMBB(TBB);
200 return 1;
201 }
202
203 BuildMI(&MBB, DL, get(Opc)).addReg(Cond[1].getReg()).addMBB(TBB);
204 BuildMI(&MBB, DL, get(MBlaze::BRID)).addMBB(FBB);
205 return 2;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000206}
207
Wesley Peck46a928b2010-11-21 21:53:36 +0000208unsigned MBlazeInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
209 MachineBasicBlock::iterator I = MBB.end();
210 if (I == MBB.begin()) return 0;
211 --I;
212 while (I->isDebugValue()) {
213 if (I == MBB.begin())
214 return 0;
215 --I;
216 }
217
218 if (!MBlaze::isUncondBranchOpcode(I->getOpcode()) &&
219 !MBlaze::isCondBranchOpcode(I->getOpcode()))
220 return 0;
221
222 // Remove the branch.
223 I->eraseFromParent();
224
225 I = MBB.end();
226
227 if (I == MBB.begin()) return 1;
228 --I;
229 if (!MBlaze::isCondBranchOpcode(I->getOpcode()))
230 return 1;
231
232 // Remove the branch.
233 I->eraseFromParent();
234 return 2;
235}
236
237
Wesley Pecka70f28c2010-02-23 19:15:24 +0000238/// getGlobalBaseReg - Return a virtual register initialized with the
239/// the global base register value. Output instructions required to
240/// initialize the register in the function entry block, if necessary.
241///
242unsigned MBlazeInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
243 MBlazeFunctionInfo *MBlazeFI = MF->getInfo<MBlazeFunctionInfo>();
244 unsigned GlobalBaseReg = MBlazeFI->getGlobalBaseReg();
245 if (GlobalBaseReg != 0)
246 return GlobalBaseReg;
247
248 // Insert the set of GlobalBaseReg into the first MBB of the function
249 MachineBasicBlock &FirstMBB = MF->front();
250 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
251 MachineRegisterInfo &RegInfo = MF->getRegInfo();
252 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
253
Wesley Peck4da992a2010-10-21 19:48:38 +0000254 GlobalBaseReg = RegInfo.createVirtualRegister(MBlaze::GPRRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000255 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
256 GlobalBaseReg).addReg(MBlaze::R20);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000257 RegInfo.addLiveIn(MBlaze::R20);
258
259 MBlazeFI->setGlobalBaseReg(GlobalBaseReg);
260 return GlobalBaseReg;
261}