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