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/MachineInstrBuilder.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/Target/TargetRegistry.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/ErrorHandling.h"
24 #define GET_INSTRINFO_CTOR
25 #include "AlphaGenInstrInfo.inc"
28 AlphaInstrInfo::AlphaInstrInfo()
29 : AlphaGenInstrInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP),
35 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
36 int &FrameIndex) const {
37 switch (MI->getOpcode()) {
44 if (MI->getOperand(1).isFI()) {
45 FrameIndex = MI->getOperand(1).getIndex();
46 return MI->getOperand(0).getReg();
54 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
55 int &FrameIndex) const {
56 switch (MI->getOpcode()) {
63 if (MI->getOperand(1).isFI()) {
64 FrameIndex = MI->getOperand(1).getIndex();
65 return MI->getOperand(0).getReg();
72 static bool isAlphaIntCondCode(unsigned Opcode) {
88 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
89 MachineBasicBlock *TBB,
90 MachineBasicBlock *FBB,
91 const SmallVectorImpl<MachineOperand> &Cond,
93 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
94 assert((Cond.size() == 2 || Cond.size() == 0) &&
95 "Alpha branch conditions have two components!");
99 if (Cond.empty()) // Unconditional branch
100 BuildMI(&MBB, DL, get(Alpha::BR)).addMBB(TBB);
101 else // Conditional branch
102 if (isAlphaIntCondCode(Cond[0].getImm()))
103 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_I))
104 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
106 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_F))
107 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
111 // Two-way Conditional Branch.
112 if (isAlphaIntCondCode(Cond[0].getImm()))
113 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_I))
114 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
116 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_F))
117 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
118 BuildMI(&MBB, DL, get(Alpha::BR)).addMBB(FBB);
122 void AlphaInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
123 MachineBasicBlock::iterator MI, DebugLoc DL,
124 unsigned DestReg, unsigned SrcReg,
125 bool KillSrc) const {
126 if (Alpha::GPRCRegClass.contains(DestReg, SrcReg)) {
127 BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
129 .addReg(SrcReg, getKillRegState(KillSrc));
130 } else if (Alpha::F4RCRegClass.contains(DestReg, SrcReg)) {
131 BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
133 .addReg(SrcReg, getKillRegState(KillSrc));
134 } else if (Alpha::F8RCRegClass.contains(DestReg, SrcReg)) {
135 BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
137 .addReg(SrcReg, getKillRegState(KillSrc));
139 llvm_unreachable("Attempt to copy register that is not GPR or FPR");
144 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
145 MachineBasicBlock::iterator MI,
146 unsigned SrcReg, bool isKill, int FrameIdx,
147 const TargetRegisterClass *RC,
148 const TargetRegisterInfo *TRI) const {
149 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
150 // << FrameIdx << "\n";
151 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
154 if (MI != MBB.end()) DL = MI->getDebugLoc();
156 if (RC == Alpha::F4RCRegisterClass)
157 BuildMI(MBB, MI, DL, get(Alpha::STS))
158 .addReg(SrcReg, getKillRegState(isKill))
159 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
160 else if (RC == Alpha::F8RCRegisterClass)
161 BuildMI(MBB, MI, DL, get(Alpha::STT))
162 .addReg(SrcReg, getKillRegState(isKill))
163 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
164 else if (RC == Alpha::GPRCRegisterClass)
165 BuildMI(MBB, MI, DL, get(Alpha::STQ))
166 .addReg(SrcReg, getKillRegState(isKill))
167 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
169 llvm_unreachable("Unhandled register class");
173 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
174 MachineBasicBlock::iterator MI,
175 unsigned DestReg, int FrameIdx,
176 const TargetRegisterClass *RC,
177 const TargetRegisterInfo *TRI) const {
178 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
179 // << FrameIdx << "\n";
181 if (MI != MBB.end()) DL = MI->getDebugLoc();
183 if (RC == Alpha::F4RCRegisterClass)
184 BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
185 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
186 else if (RC == Alpha::F8RCRegisterClass)
187 BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
188 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
189 else if (RC == Alpha::GPRCRegisterClass)
190 BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
191 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
193 llvm_unreachable("Unhandled register class");
196 static unsigned AlphaRevCondCode(unsigned Opcode) {
198 case Alpha::BEQ: return Alpha::BNE;
199 case Alpha::BNE: return Alpha::BEQ;
200 case Alpha::BGE: return Alpha::BLT;
201 case Alpha::BGT: return Alpha::BLE;
202 case Alpha::BLE: return Alpha::BGT;
203 case Alpha::BLT: return Alpha::BGE;
204 case Alpha::BLBC: return Alpha::BLBS;
205 case Alpha::BLBS: return Alpha::BLBC;
206 case Alpha::FBEQ: return Alpha::FBNE;
207 case Alpha::FBNE: return Alpha::FBEQ;
208 case Alpha::FBGE: return Alpha::FBLT;
209 case Alpha::FBGT: return Alpha::FBLE;
210 case Alpha::FBLE: return Alpha::FBGT;
211 case Alpha::FBLT: return Alpha::FBGE;
213 llvm_unreachable("Unknown opcode");
215 return 0; // Not reached
219 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
220 MachineBasicBlock *&FBB,
221 SmallVectorImpl<MachineOperand> &Cond,
222 bool AllowModify) const {
223 // If the block has no terminators, it just falls into the block after it.
224 MachineBasicBlock::iterator I = MBB.end();
225 if (I == MBB.begin())
228 while (I->isDebugValue()) {
229 if (I == MBB.begin())
233 if (!isUnpredicatedTerminator(I))
236 // Get the last instruction in the block.
237 MachineInstr *LastInst = I;
239 // If there is only one terminator instruction, process it.
240 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
241 if (LastInst->getOpcode() == Alpha::BR) {
242 TBB = LastInst->getOperand(0).getMBB();
244 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
245 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
246 // Block ends with fall-through condbranch.
247 TBB = LastInst->getOperand(2).getMBB();
248 Cond.push_back(LastInst->getOperand(0));
249 Cond.push_back(LastInst->getOperand(1));
252 // Otherwise, don't know what this is.
256 // Get the instruction before it if it's a terminator.
257 MachineInstr *SecondLastInst = I;
259 // If there are three terminators, we don't know what sort of block this is.
260 if (SecondLastInst && I != MBB.begin() &&
261 isUnpredicatedTerminator(--I))
264 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
265 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
266 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) &&
267 LastInst->getOpcode() == Alpha::BR) {
268 TBB = SecondLastInst->getOperand(2).getMBB();
269 Cond.push_back(SecondLastInst->getOperand(0));
270 Cond.push_back(SecondLastInst->getOperand(1));
271 FBB = LastInst->getOperand(0).getMBB();
275 // If the block ends with two Alpha::BRs, handle it. The second one is not
276 // executed, so remove it.
277 if (SecondLastInst->getOpcode() == Alpha::BR &&
278 LastInst->getOpcode() == Alpha::BR) {
279 TBB = SecondLastInst->getOperand(0).getMBB();
282 I->eraseFromParent();
286 // Otherwise, can't handle this.
290 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
291 MachineBasicBlock::iterator I = MBB.end();
292 if (I == MBB.begin()) return 0;
294 while (I->isDebugValue()) {
295 if (I == MBB.begin())
299 if (I->getOpcode() != Alpha::BR &&
300 I->getOpcode() != Alpha::COND_BRANCH_I &&
301 I->getOpcode() != Alpha::COND_BRANCH_F)
304 // Remove the branch.
305 I->eraseFromParent();
309 if (I == MBB.begin()) return 1;
311 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
312 I->getOpcode() != Alpha::COND_BRANCH_F)
315 // Remove the branch.
316 I->eraseFromParent();
320 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
321 MachineBasicBlock::iterator MI) const {
323 BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
328 bool AlphaInstrInfo::
329 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
330 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
331 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
335 /// getGlobalBaseReg - Return a virtual register initialized with the
336 /// the global base register value. Output instructions required to
337 /// initialize the register in the function entry block, if necessary.
339 unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
340 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
341 unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg();
342 if (GlobalBaseReg != 0)
343 return GlobalBaseReg;
345 // Insert the set of GlobalBaseReg into the first MBB of the function
346 MachineBasicBlock &FirstMBB = MF->front();
347 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
348 MachineRegisterInfo &RegInfo = MF->getRegInfo();
349 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
351 GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
352 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
353 GlobalBaseReg).addReg(Alpha::R29);
354 RegInfo.addLiveIn(Alpha::R29);
356 AlphaFI->setGlobalBaseReg(GlobalBaseReg);
357 return GlobalBaseReg;
360 /// getGlobalRetAddr - Return a virtual register initialized with the
361 /// the global base register value. Output instructions required to
362 /// initialize the register in the function entry block, if necessary.
364 unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const {
365 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
366 unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr();
367 if (GlobalRetAddr != 0)
368 return GlobalRetAddr;
370 // Insert the set of GlobalRetAddr into the first MBB of the function
371 MachineBasicBlock &FirstMBB = MF->front();
372 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
373 MachineRegisterInfo &RegInfo = MF->getRegInfo();
374 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
376 GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
377 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
378 GlobalRetAddr).addReg(Alpha::R26);
379 RegInfo.addLiveIn(Alpha::R26);
381 AlphaFI->setGlobalRetAddr(GlobalRetAddr);
382 return GlobalRetAddr;