blob: 794ebedf1e6a5be1e327ca2de2aec9e83c5ddc16 [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"
Wesley Peck3d820ba2011-04-11 22:31:52 +000020#include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
21#include "llvm/Support/CommandLine.h"
Wesley Pecka70f28c2010-02-23 19:15:24 +000022#include "llvm/Support/ErrorHandling.h"
23#include "MBlazeGenInstrInfo.inc"
24
25using namespace llvm;
26
27MBlazeInstrInfo::MBlazeInstrInfo(MBlazeTargetMachine &tm)
28 : TargetInstrInfoImpl(MBlazeInsts, array_lengthof(MBlazeInsts)),
29 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
30
31static bool isZeroImm(const MachineOperand &op) {
32 return op.isImm() && op.getImm() == 0;
33}
34
Wesley Pecka70f28c2010-02-23 19:15:24 +000035/// isLoadFromStackSlot - If the specified machine instruction is a direct
36/// load from a stack slot, return the virtual or physical register number of
37/// the destination along with the FrameIndex of the loaded stack slot. If
38/// not, return 0. This predicate must return 0 if the instruction has
39/// any side effects other than loading from the stack slot.
40unsigned MBlazeInstrInfo::
41isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const {
42 if (MI->getOpcode() == MBlaze::LWI) {
Wesley Peck41400da2010-11-12 23:30:17 +000043 if ((MI->getOperand(1).isFI()) && // is a stack slot
44 (MI->getOperand(2).isImm()) && // the imm is zero
45 (isZeroImm(MI->getOperand(2)))) {
46 FrameIndex = MI->getOperand(1).getIndex();
Wesley Pecka70f28c2010-02-23 19:15:24 +000047 return MI->getOperand(0).getReg();
48 }
49 }
50
51 return 0;
52}
53
54/// isStoreToStackSlot - If the specified machine instruction is a direct
55/// store to a stack slot, return the virtual or physical register number of
56/// the source reg along with the FrameIndex of the loaded stack slot. If
57/// not, return 0. This predicate must return 0 if the instruction has
58/// any side effects other than storing to the stack slot.
59unsigned MBlazeInstrInfo::
60isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const {
61 if (MI->getOpcode() == MBlaze::SWI) {
Wesley Peck41400da2010-11-12 23:30:17 +000062 if ((MI->getOperand(1).isFI()) && // is a stack slot
63 (MI->getOperand(2).isImm()) && // the imm is zero
64 (isZeroImm(MI->getOperand(2)))) {
65 FrameIndex = MI->getOperand(1).getIndex();
Wesley Pecka70f28c2010-02-23 19:15:24 +000066 return MI->getOperand(0).getReg();
67 }
68 }
69 return 0;
70}
71
72/// insertNoop - If data hazard condition is found insert the target nop
73/// instruction.
74void MBlazeInstrInfo::
75insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000076 DebugLoc DL;
Wesley Pecka70f28c2010-02-23 19:15:24 +000077 BuildMI(MBB, MI, DL, get(MBlaze::NOP));
78}
79
Jakob Stoklund Olesene6afcf82010-07-11 06:53:27 +000080void MBlazeInstrInfo::
81copyPhysReg(MachineBasicBlock &MBB,
82 MachineBasicBlock::iterator I, DebugLoc DL,
83 unsigned DestReg, unsigned SrcReg,
84 bool KillSrc) const {
Wesley Peck9eb337a2010-12-22 01:29:32 +000085 llvm::BuildMI(MBB, I, DL, get(MBlaze::ADDK), DestReg)
Jakob Stoklund Olesene6afcf82010-07-11 06:53:27 +000086 .addReg(SrcReg, getKillRegState(KillSrc)).addReg(MBlaze::R0);
Wesley Pecka70f28c2010-02-23 19:15:24 +000087}
88
89void MBlazeInstrInfo::
90storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
91 unsigned SrcReg, bool isKill, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +000092 const TargetRegisterClass *RC,
93 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +000094 DebugLoc DL;
95 BuildMI(MBB, I, DL, get(MBlaze::SWI)).addReg(SrcReg,getKillRegState(isKill))
Wesley Peck41400da2010-11-12 23:30:17 +000096 .addFrameIndex(FI).addImm(0); //.addFrameIndex(FI);
Wesley Pecka70f28c2010-02-23 19:15:24 +000097}
98
99void MBlazeInstrInfo::
100loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
101 unsigned DestReg, int FI,
Evan Cheng746ad692010-05-06 19:06:44 +0000102 const TargetRegisterClass *RC,
103 const TargetRegisterInfo *TRI) const {
Chris Lattnerc7f3ace2010-04-02 20:16:16 +0000104 DebugLoc DL;
105 BuildMI(MBB, I, DL, get(MBlaze::LWI), DestReg)
Wesley Peck41400da2010-11-12 23:30:17 +0000106 .addFrameIndex(FI).addImm(0); //.addFrameIndex(FI);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000107}
108
Wesley Pecka70f28c2010-02-23 19:15:24 +0000109//===----------------------------------------------------------------------===//
110// Branch Analysis
111//===----------------------------------------------------------------------===//
Wesley Peck46a928b2010-11-21 21:53:36 +0000112bool MBlazeInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
113 MachineBasicBlock *&TBB,
114 MachineBasicBlock *&FBB,
115 SmallVectorImpl<MachineOperand> &Cond,
116 bool AllowModify) const {
117 // If the block has no terminators, it just falls into the block after it.
118 MachineBasicBlock::iterator I = MBB.end();
119 if (I == MBB.begin())
120 return false;
121 --I;
122 while (I->isDebugValue()) {
123 if (I == MBB.begin())
124 return false;
125 --I;
126 }
127 if (!isUnpredicatedTerminator(I))
128 return false;
129
130 // Get the last instruction in the block.
131 MachineInstr *LastInst = I;
132
133 // If there is only one terminator instruction, process it.
134 unsigned LastOpc = LastInst->getOpcode();
135 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
136 if (MBlaze::isUncondBranchOpcode(LastOpc)) {
137 TBB = LastInst->getOperand(0).getMBB();
138 return false;
139 }
140 if (MBlaze::isCondBranchOpcode(LastOpc)) {
141 // Block ends with fall-through condbranch.
142 TBB = LastInst->getOperand(1).getMBB();
143 Cond.push_back(MachineOperand::CreateImm(LastInst->getOpcode()));
144 Cond.push_back(LastInst->getOperand(0));
145 return false;
146 }
147 // Otherwise, don't know what this is.
148 return true;
149 }
150
151 // Get the instruction before it if it's a terminator.
152 MachineInstr *SecondLastInst = I;
153
154 // If there are three terminators, we don't know what sort of block this is.
155 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
156 return true;
157
158 // If the block ends with something like BEQID then BRID, handle it.
159 if (MBlaze::isCondBranchOpcode(SecondLastInst->getOpcode()) &&
160 MBlaze::isUncondBranchOpcode(LastInst->getOpcode())) {
161 TBB = SecondLastInst->getOperand(1).getMBB();
162 Cond.push_back(MachineOperand::CreateImm(SecondLastInst->getOpcode()));
163 Cond.push_back(SecondLastInst->getOperand(0));
164 FBB = LastInst->getOperand(0).getMBB();
165 return false;
166 }
167
168 // If the block ends with two unconditional branches, handle it.
169 // The second one is not executed, so remove it.
170 if (MBlaze::isUncondBranchOpcode(SecondLastInst->getOpcode()) &&
171 MBlaze::isUncondBranchOpcode(LastInst->getOpcode())) {
172 TBB = SecondLastInst->getOperand(0).getMBB();
173 I = LastInst;
174 if (AllowModify)
175 I->eraseFromParent();
176 return false;
177 }
178
179 // Otherwise, can't handle this.
180 return true;
181}
182
Wesley Pecka70f28c2010-02-23 19:15:24 +0000183unsigned 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 Peck46a928b2010-11-21 21:53:36 +0000188 // Shouldn't be a fall through.
189 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
190 assert((Cond.size() == 2 || Cond.size() == 0) &&
191 "MBlaze branch conditions have two components!");
192
193 unsigned Opc = MBlaze::BRID;
194 if (!Cond.empty())
195 Opc = (unsigned)Cond[0].getImm();
196
197 if (FBB == 0) {
198 if (Cond.empty()) // Unconditional branch
199 BuildMI(&MBB, DL, get(Opc)).addMBB(TBB);
200 else // Conditional branch
201 BuildMI(&MBB, DL, get(Opc)).addReg(Cond[1].getReg()).addMBB(TBB);
202 return 1;
203 }
204
205 BuildMI(&MBB, DL, get(Opc)).addReg(Cond[1].getReg()).addMBB(TBB);
206 BuildMI(&MBB, DL, get(MBlaze::BRID)).addMBB(FBB);
207 return 2;
Wesley Pecka70f28c2010-02-23 19:15:24 +0000208}
209
Wesley Peck46a928b2010-11-21 21:53:36 +0000210unsigned MBlazeInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
211 MachineBasicBlock::iterator I = MBB.end();
212 if (I == MBB.begin()) return 0;
213 --I;
214 while (I->isDebugValue()) {
215 if (I == MBB.begin())
216 return 0;
217 --I;
218 }
219
220 if (!MBlaze::isUncondBranchOpcode(I->getOpcode()) &&
221 !MBlaze::isCondBranchOpcode(I->getOpcode()))
222 return 0;
223
224 // Remove the branch.
225 I->eraseFromParent();
226
227 I = MBB.end();
228
229 if (I == MBB.begin()) return 1;
230 --I;
231 if (!MBlaze::isCondBranchOpcode(I->getOpcode()))
232 return 1;
233
234 // Remove the branch.
235 I->eraseFromParent();
236 return 2;
237}
238
Wesley Peck2e063982010-12-02 16:17:11 +0000239bool MBlazeInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
240 assert(Cond.size() == 2 && "Invalid MBlaze branch opcode!");
241 switch (Cond[0].getImm()) {
242 default: return true;
243 case MBlaze::BEQ: Cond[0].setImm(MBlaze::BNE); return false;
244 case MBlaze::BNE: Cond[0].setImm(MBlaze::BEQ); return false;
245 case MBlaze::BGT: Cond[0].setImm(MBlaze::BLE); return false;
246 case MBlaze::BGE: Cond[0].setImm(MBlaze::BLT); return false;
247 case MBlaze::BLT: Cond[0].setImm(MBlaze::BGE); return false;
248 case MBlaze::BLE: Cond[0].setImm(MBlaze::BGT); return false;
249 case MBlaze::BEQI: Cond[0].setImm(MBlaze::BNEI); return false;
250 case MBlaze::BNEI: Cond[0].setImm(MBlaze::BEQI); return false;
251 case MBlaze::BGTI: Cond[0].setImm(MBlaze::BLEI); return false;
252 case MBlaze::BGEI: Cond[0].setImm(MBlaze::BLTI); return false;
253 case MBlaze::BLTI: Cond[0].setImm(MBlaze::BGEI); return false;
254 case MBlaze::BLEI: Cond[0].setImm(MBlaze::BGTI); return false;
255 case MBlaze::BEQD: Cond[0].setImm(MBlaze::BNED); return false;
256 case MBlaze::BNED: Cond[0].setImm(MBlaze::BEQD); return false;
257 case MBlaze::BGTD: Cond[0].setImm(MBlaze::BLED); return false;
258 case MBlaze::BGED: Cond[0].setImm(MBlaze::BLTD); return false;
259 case MBlaze::BLTD: Cond[0].setImm(MBlaze::BGED); return false;
260 case MBlaze::BLED: Cond[0].setImm(MBlaze::BGTD); return false;
261 case MBlaze::BEQID: Cond[0].setImm(MBlaze::BNEID); return false;
262 case MBlaze::BNEID: Cond[0].setImm(MBlaze::BEQID); return false;
263 case MBlaze::BGTID: Cond[0].setImm(MBlaze::BLEID); return false;
264 case MBlaze::BGEID: Cond[0].setImm(MBlaze::BLTID); return false;
265 case MBlaze::BLTID: Cond[0].setImm(MBlaze::BGEID); return false;
266 case MBlaze::BLEID: Cond[0].setImm(MBlaze::BGTID); return false;
267 }
268}
Wesley Peck46a928b2010-11-21 21:53:36 +0000269
Wesley Pecka70f28c2010-02-23 19:15:24 +0000270/// getGlobalBaseReg - Return a virtual register initialized with the
271/// the global base register value. Output instructions required to
272/// initialize the register in the function entry block, if necessary.
273///
274unsigned MBlazeInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
275 MBlazeFunctionInfo *MBlazeFI = MF->getInfo<MBlazeFunctionInfo>();
276 unsigned GlobalBaseReg = MBlazeFI->getGlobalBaseReg();
277 if (GlobalBaseReg != 0)
278 return GlobalBaseReg;
279
280 // Insert the set of GlobalBaseReg into the first MBB of the function
281 MachineBasicBlock &FirstMBB = MF->front();
282 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
283 MachineRegisterInfo &RegInfo = MF->getRegInfo();
284 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
285
Wesley Peck4da992a2010-10-21 19:48:38 +0000286 GlobalBaseReg = RegInfo.createVirtualRegister(MBlaze::GPRRegisterClass);
Jakob Stoklund Olesen3ecf1f02010-07-10 22:43:03 +0000287 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
288 GlobalBaseReg).addReg(MBlaze::R20);
Wesley Pecka70f28c2010-02-23 19:15:24 +0000289 RegInfo.addLiveIn(MBlaze::R20);
290
291 MBlazeFI->setGlobalBaseReg(GlobalBaseReg);
292 return GlobalBaseReg;
293}