1 //===- AlphaInstrInfo.cpp - Alpha 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 Alpha implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "AlphaInstrInfo.h"
16 #include "AlphaMachineFunctionInfo.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/Support/ErrorHandling.h"
23 #define GET_INSTRINFO_MC_DESC
24 #include "AlphaGenInstrInfo.inc"
27 AlphaInstrInfo::AlphaInstrInfo()
28 : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
33 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
34 int &FrameIndex) const {
35 switch (MI->getOpcode()) {
42 if (MI->getOperand(1).isFI()) {
43 FrameIndex = MI->getOperand(1).getIndex();
44 return MI->getOperand(0).getReg();
52 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
53 int &FrameIndex) const {
54 switch (MI->getOpcode()) {
61 if (MI->getOperand(1).isFI()) {
62 FrameIndex = MI->getOperand(1).getIndex();
63 return MI->getOperand(0).getReg();
70 static bool isAlphaIntCondCode(unsigned Opcode) {
86 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
87 MachineBasicBlock *TBB,
88 MachineBasicBlock *FBB,
89 const SmallVectorImpl<MachineOperand> &Cond,
91 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
92 assert((Cond.size() == 2 || Cond.size() == 0) &&
93 "Alpha branch conditions have two components!");
97 if (Cond.empty()) // Unconditional branch
98 BuildMI(&MBB, DL, get(Alpha::BR)).addMBB(TBB);
99 else // Conditional branch
100 if (isAlphaIntCondCode(Cond[0].getImm()))
101 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_I))
102 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
104 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_F))
105 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
109 // Two-way Conditional Branch.
110 if (isAlphaIntCondCode(Cond[0].getImm()))
111 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_I))
112 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
114 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_F))
115 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
116 BuildMI(&MBB, DL, get(Alpha::BR)).addMBB(FBB);
120 void AlphaInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
121 MachineBasicBlock::iterator MI, DebugLoc DL,
122 unsigned DestReg, unsigned SrcReg,
123 bool KillSrc) const {
124 if (Alpha::GPRCRegClass.contains(DestReg, SrcReg)) {
125 BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
127 .addReg(SrcReg, getKillRegState(KillSrc));
128 } else if (Alpha::F4RCRegClass.contains(DestReg, SrcReg)) {
129 BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
131 .addReg(SrcReg, getKillRegState(KillSrc));
132 } else if (Alpha::F8RCRegClass.contains(DestReg, SrcReg)) {
133 BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
135 .addReg(SrcReg, getKillRegState(KillSrc));
137 llvm_unreachable("Attempt to copy register that is not GPR or FPR");
142 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
143 MachineBasicBlock::iterator MI,
144 unsigned SrcReg, bool isKill, int FrameIdx,
145 const TargetRegisterClass *RC,
146 const TargetRegisterInfo *TRI) const {
147 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
148 // << FrameIdx << "\n";
149 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
152 if (MI != MBB.end()) DL = MI->getDebugLoc();
154 if (RC == Alpha::F4RCRegisterClass)
155 BuildMI(MBB, MI, DL, get(Alpha::STS))
156 .addReg(SrcReg, getKillRegState(isKill))
157 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
158 else if (RC == Alpha::F8RCRegisterClass)
159 BuildMI(MBB, MI, DL, get(Alpha::STT))
160 .addReg(SrcReg, getKillRegState(isKill))
161 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
162 else if (RC == Alpha::GPRCRegisterClass)
163 BuildMI(MBB, MI, DL, get(Alpha::STQ))
164 .addReg(SrcReg, getKillRegState(isKill))
165 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
167 llvm_unreachable("Unhandled register class");
171 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
172 MachineBasicBlock::iterator MI,
173 unsigned DestReg, int FrameIdx,
174 const TargetRegisterClass *RC,
175 const TargetRegisterInfo *TRI) const {
176 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
177 // << FrameIdx << "\n";
179 if (MI != MBB.end()) DL = MI->getDebugLoc();
181 if (RC == Alpha::F4RCRegisterClass)
182 BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
183 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
184 else if (RC == Alpha::F8RCRegisterClass)
185 BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
186 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
187 else if (RC == Alpha::GPRCRegisterClass)
188 BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
189 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
191 llvm_unreachable("Unhandled register class");
194 static unsigned AlphaRevCondCode(unsigned Opcode) {
196 case Alpha::BEQ: return Alpha::BNE;
197 case Alpha::BNE: return Alpha::BEQ;
198 case Alpha::BGE: return Alpha::BLT;
199 case Alpha::BGT: return Alpha::BLE;
200 case Alpha::BLE: return Alpha::BGT;
201 case Alpha::BLT: return Alpha::BGE;
202 case Alpha::BLBC: return Alpha::BLBS;
203 case Alpha::BLBS: return Alpha::BLBC;
204 case Alpha::FBEQ: return Alpha::FBNE;
205 case Alpha::FBNE: return Alpha::FBEQ;
206 case Alpha::FBGE: return Alpha::FBLT;
207 case Alpha::FBGT: return Alpha::FBLE;
208 case Alpha::FBLE: return Alpha::FBGT;
209 case Alpha::FBLT: return Alpha::FBGE;
211 llvm_unreachable("Unknown opcode");
213 return 0; // Not reached
217 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
218 MachineBasicBlock *&FBB,
219 SmallVectorImpl<MachineOperand> &Cond,
220 bool AllowModify) const {
221 // If the block has no terminators, it just falls into the block after it.
222 MachineBasicBlock::iterator I = MBB.end();
223 if (I == MBB.begin())
226 while (I->isDebugValue()) {
227 if (I == MBB.begin())
231 if (!isUnpredicatedTerminator(I))
234 // Get the last instruction in the block.
235 MachineInstr *LastInst = I;
237 // If there is only one terminator instruction, process it.
238 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
239 if (LastInst->getOpcode() == Alpha::BR) {
240 TBB = LastInst->getOperand(0).getMBB();
242 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
243 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
244 // Block ends with fall-through condbranch.
245 TBB = LastInst->getOperand(2).getMBB();
246 Cond.push_back(LastInst->getOperand(0));
247 Cond.push_back(LastInst->getOperand(1));
250 // Otherwise, don't know what this is.
254 // Get the instruction before it if it's a terminator.
255 MachineInstr *SecondLastInst = I;
257 // If there are three terminators, we don't know what sort of block this is.
258 if (SecondLastInst && I != MBB.begin() &&
259 isUnpredicatedTerminator(--I))
262 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
263 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
264 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) &&
265 LastInst->getOpcode() == Alpha::BR) {
266 TBB = SecondLastInst->getOperand(2).getMBB();
267 Cond.push_back(SecondLastInst->getOperand(0));
268 Cond.push_back(SecondLastInst->getOperand(1));
269 FBB = LastInst->getOperand(0).getMBB();
273 // If the block ends with two Alpha::BRs, handle it. The second one is not
274 // executed, so remove it.
275 if (SecondLastInst->getOpcode() == Alpha::BR &&
276 LastInst->getOpcode() == Alpha::BR) {
277 TBB = SecondLastInst->getOperand(0).getMBB();
280 I->eraseFromParent();
284 // Otherwise, can't handle this.
288 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
289 MachineBasicBlock::iterator I = MBB.end();
290 if (I == MBB.begin()) return 0;
292 while (I->isDebugValue()) {
293 if (I == MBB.begin())
297 if (I->getOpcode() != Alpha::BR &&
298 I->getOpcode() != Alpha::COND_BRANCH_I &&
299 I->getOpcode() != Alpha::COND_BRANCH_F)
302 // Remove the branch.
303 I->eraseFromParent();
307 if (I == MBB.begin()) return 1;
309 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
310 I->getOpcode() != Alpha::COND_BRANCH_F)
313 // Remove the branch.
314 I->eraseFromParent();
318 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
319 MachineBasicBlock::iterator MI) const {
321 BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
326 bool AlphaInstrInfo::
327 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
328 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
329 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
333 /// getGlobalBaseReg - Return a virtual register initialized with the
334 /// the global base register value. Output instructions required to
335 /// initialize the register in the function entry block, if necessary.
337 unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
338 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
339 unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg();
340 if (GlobalBaseReg != 0)
341 return GlobalBaseReg;
343 // Insert the set of GlobalBaseReg into the first MBB of the function
344 MachineBasicBlock &FirstMBB = MF->front();
345 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
346 MachineRegisterInfo &RegInfo = MF->getRegInfo();
347 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
349 GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
350 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
351 GlobalBaseReg).addReg(Alpha::R29);
352 RegInfo.addLiveIn(Alpha::R29);
354 AlphaFI->setGlobalBaseReg(GlobalBaseReg);
355 return GlobalBaseReg;
358 /// getGlobalRetAddr - Return a virtual register initialized with the
359 /// the global base register value. Output instructions required to
360 /// initialize the register in the function entry block, if necessary.
362 unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const {
363 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
364 unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr();
365 if (GlobalRetAddr != 0)
366 return GlobalRetAddr;
368 // Insert the set of GlobalRetAddr into the first MBB of the function
369 MachineBasicBlock &FirstMBB = MF->front();
370 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
371 MachineRegisterInfo &RegInfo = MF->getRegInfo();
372 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
374 GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
375 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
376 GlobalRetAddr).addReg(Alpha::R26);
377 RegInfo.addLiveIn(Alpha::R26);
379 AlphaFI->setGlobalRetAddr(GlobalRetAddr);
380 return GlobalRetAddr;