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 /// 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.
38 unsigned MBlazeInstrInfo::
39 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const {
40 if (MI->getOpcode() == MBlaze::LWI) {
41 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();
45 return MI->getOperand(0).getReg();
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.
57 unsigned MBlazeInstrInfo::
58 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const {
59 if (MI->getOpcode() == MBlaze::SWI) {
60 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();
64 return MI->getOperand(0).getReg();
70 /// insertNoop - If data hazard condition is found insert the target nop
72 void MBlazeInstrInfo::
73 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const {
75 BuildMI(MBB, MI, DL, get(MBlaze::NOP));
78 void MBlazeInstrInfo::
79 copyPhysReg(MachineBasicBlock &MBB,
80 MachineBasicBlock::iterator I, DebugLoc DL,
81 unsigned DestReg, unsigned SrcReg,
83 llvm::BuildMI(MBB, I, DL, get(MBlaze::ADD), DestReg)
84 .addReg(SrcReg, getKillRegState(KillSrc)).addReg(MBlaze::R0);
87 void MBlazeInstrInfo::
88 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
89 unsigned SrcReg, bool isKill, int FI,
90 const TargetRegisterClass *RC,
91 const TargetRegisterInfo *TRI) const {
93 BuildMI(MBB, I, DL, get(MBlaze::SWI)).addReg(SrcReg,getKillRegState(isKill))
94 .addFrameIndex(FI).addImm(0); //.addFrameIndex(FI);
97 void MBlazeInstrInfo::
98 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
99 unsigned DestReg, int FI,
100 const TargetRegisterClass *RC,
101 const TargetRegisterInfo *TRI) const {
103 BuildMI(MBB, I, DL, get(MBlaze::LWI), DestReg)
104 .addFrameIndex(FI).addImm(0); //.addFrameIndex(FI);
107 //===----------------------------------------------------------------------===//
109 //===----------------------------------------------------------------------===//
110 bool 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())
120 while (I->isDebugValue()) {
121 if (I == MBB.begin())
125 if (!isUnpredicatedTerminator(I))
128 // Get the last instruction in the block.
129 MachineInstr *LastInst = I;
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();
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));
145 // Otherwise, don't know what this is.
149 // Get the instruction before it if it's a terminator.
150 MachineInstr *SecondLastInst = I;
152 // If there are three terminators, we don't know what sort of block this is.
153 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
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();
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();
173 I->eraseFromParent();
177 // Otherwise, can't handle this.
181 unsigned MBlazeInstrInfo::
182 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
183 MachineBasicBlock *FBB,
184 const SmallVectorImpl<MachineOperand> &Cond,
186 // 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!");
191 unsigned Opc = MBlaze::BRID;
193 Opc = (unsigned)Cond[0].getImm();
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);
203 BuildMI(&MBB, DL, get(Opc)).addReg(Cond[1].getReg()).addMBB(TBB);
204 BuildMI(&MBB, DL, get(MBlaze::BRID)).addMBB(FBB);
208 unsigned MBlazeInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
209 MachineBasicBlock::iterator I = MBB.end();
210 if (I == MBB.begin()) return 0;
212 while (I->isDebugValue()) {
213 if (I == MBB.begin())
218 if (!MBlaze::isUncondBranchOpcode(I->getOpcode()) &&
219 !MBlaze::isCondBranchOpcode(I->getOpcode()))
222 // Remove the branch.
223 I->eraseFromParent();
227 if (I == MBB.begin()) return 1;
229 if (!MBlaze::isCondBranchOpcode(I->getOpcode()))
232 // Remove the branch.
233 I->eraseFromParent();
237 bool MBlazeInstrInfo::ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
238 assert(Cond.size() == 2 && "Invalid MBlaze branch opcode!");
239 switch (Cond[0].getImm()) {
240 default: return true;
241 case MBlaze::BEQ: Cond[0].setImm(MBlaze::BNE); return false;
242 case MBlaze::BNE: Cond[0].setImm(MBlaze::BEQ); return false;
243 case MBlaze::BGT: Cond[0].setImm(MBlaze::BLE); return false;
244 case MBlaze::BGE: Cond[0].setImm(MBlaze::BLT); return false;
245 case MBlaze::BLT: Cond[0].setImm(MBlaze::BGE); return false;
246 case MBlaze::BLE: Cond[0].setImm(MBlaze::BGT); return false;
247 case MBlaze::BEQI: Cond[0].setImm(MBlaze::BNEI); return false;
248 case MBlaze::BNEI: Cond[0].setImm(MBlaze::BEQI); return false;
249 case MBlaze::BGTI: Cond[0].setImm(MBlaze::BLEI); return false;
250 case MBlaze::BGEI: Cond[0].setImm(MBlaze::BLTI); return false;
251 case MBlaze::BLTI: Cond[0].setImm(MBlaze::BGEI); return false;
252 case MBlaze::BLEI: Cond[0].setImm(MBlaze::BGTI); return false;
253 case MBlaze::BEQD: Cond[0].setImm(MBlaze::BNED); return false;
254 case MBlaze::BNED: Cond[0].setImm(MBlaze::BEQD); return false;
255 case MBlaze::BGTD: Cond[0].setImm(MBlaze::BLED); return false;
256 case MBlaze::BGED: Cond[0].setImm(MBlaze::BLTD); return false;
257 case MBlaze::BLTD: Cond[0].setImm(MBlaze::BGED); return false;
258 case MBlaze::BLED: Cond[0].setImm(MBlaze::BGTD); return false;
259 case MBlaze::BEQID: Cond[0].setImm(MBlaze::BNEID); return false;
260 case MBlaze::BNEID: Cond[0].setImm(MBlaze::BEQID); return false;
261 case MBlaze::BGTID: Cond[0].setImm(MBlaze::BLEID); return false;
262 case MBlaze::BGEID: Cond[0].setImm(MBlaze::BLTID); return false;
263 case MBlaze::BLTID: Cond[0].setImm(MBlaze::BGEID); return false;
264 case MBlaze::BLEID: Cond[0].setImm(MBlaze::BGTID); return false;
268 /// getGlobalBaseReg - Return a virtual register initialized with the
269 /// the global base register value. Output instructions required to
270 /// initialize the register in the function entry block, if necessary.
272 unsigned MBlazeInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
273 MBlazeFunctionInfo *MBlazeFI = MF->getInfo<MBlazeFunctionInfo>();
274 unsigned GlobalBaseReg = MBlazeFI->getGlobalBaseReg();
275 if (GlobalBaseReg != 0)
276 return GlobalBaseReg;
278 // Insert the set of GlobalBaseReg into the first MBB of the function
279 MachineBasicBlock &FirstMBB = MF->front();
280 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
281 MachineRegisterInfo &RegInfo = MF->getRegInfo();
282 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
284 GlobalBaseReg = RegInfo.createVirtualRegister(MBlaze::GPRRegisterClass);
285 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
286 GlobalBaseReg).addReg(MBlaze::R20);
287 RegInfo.addLiveIn(MBlaze::R20);
289 MBlazeFI->setGlobalBaseReg(GlobalBaseReg);
290 return GlobalBaseReg;