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 "AlphaGenInstrInfo.inc"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/Support/ErrorHandling.h"
25 AlphaInstrInfo::AlphaInstrInfo()
26 : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
30 bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
31 unsigned& sourceReg, unsigned& destReg,
32 unsigned& SrcSR, unsigned& DstSR) const {
33 unsigned oc = MI.getOpcode();
34 if (oc == Alpha::BISr ||
37 oc == Alpha::CPYSSt ||
38 oc == Alpha::CPYSTs) {
41 assert(MI.getNumOperands() >= 3 &&
42 MI.getOperand(0).isReg() &&
43 MI.getOperand(1).isReg() &&
44 MI.getOperand(2).isReg() &&
45 "invalid Alpha BIS instruction!");
46 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
47 sourceReg = MI.getOperand(1).getReg();
48 destReg = MI.getOperand(0).getReg();
57 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
58 int &FrameIndex) const {
59 switch (MI->getOpcode()) {
66 if (MI->getOperand(1).isFI()) {
67 FrameIndex = MI->getOperand(1).getIndex();
68 return MI->getOperand(0).getReg();
76 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
77 int &FrameIndex) const {
78 switch (MI->getOpcode()) {
85 if (MI->getOperand(1).isFI()) {
86 FrameIndex = MI->getOperand(1).getIndex();
87 return MI->getOperand(0).getReg();
94 static bool isAlphaIntCondCode(unsigned Opcode) {
110 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
111 MachineBasicBlock *TBB,
112 MachineBasicBlock *FBB,
113 const SmallVectorImpl<MachineOperand> &Cond,
115 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
116 assert((Cond.size() == 2 || Cond.size() == 0) &&
117 "Alpha branch conditions have two components!");
121 if (Cond.empty()) // Unconditional branch
122 BuildMI(&MBB, DL, get(Alpha::BR)).addMBB(TBB);
123 else // Conditional branch
124 if (isAlphaIntCondCode(Cond[0].getImm()))
125 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_I))
126 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
128 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_F))
129 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
133 // Two-way Conditional Branch.
134 if (isAlphaIntCondCode(Cond[0].getImm()))
135 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_I))
136 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
138 BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_F))
139 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
140 BuildMI(&MBB, DL, get(Alpha::BR)).addMBB(FBB);
144 void AlphaInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
145 MachineBasicBlock::iterator MI, DebugLoc DL,
146 unsigned DestReg, unsigned SrcReg,
147 bool KillSrc) const {
148 if (Alpha::GPRCRegClass.contains(DestReg, SrcReg)) {
149 BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
151 .addReg(SrcReg, getKillRegState(KillSrc));
152 } else if (Alpha::F4RCRegClass.contains(DestReg, SrcReg)) {
153 BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
155 .addReg(SrcReg, getKillRegState(KillSrc));
156 } else if (Alpha::F8RCRegClass.contains(DestReg, SrcReg)) {
157 BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
159 .addReg(SrcReg, getKillRegState(KillSrc));
161 llvm_unreachable("Attempt to copy register that is not GPR or FPR");
166 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
167 MachineBasicBlock::iterator MI,
168 unsigned SrcReg, bool isKill, int FrameIdx,
169 const TargetRegisterClass *RC,
170 const TargetRegisterInfo *TRI) const {
171 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
172 // << FrameIdx << "\n";
173 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
176 if (MI != MBB.end()) DL = MI->getDebugLoc();
178 if (RC == Alpha::F4RCRegisterClass)
179 BuildMI(MBB, MI, DL, get(Alpha::STS))
180 .addReg(SrcReg, getKillRegState(isKill))
181 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
182 else if (RC == Alpha::F8RCRegisterClass)
183 BuildMI(MBB, MI, DL, get(Alpha::STT))
184 .addReg(SrcReg, getKillRegState(isKill))
185 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
186 else if (RC == Alpha::GPRCRegisterClass)
187 BuildMI(MBB, MI, DL, get(Alpha::STQ))
188 .addReg(SrcReg, getKillRegState(isKill))
189 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
191 llvm_unreachable("Unhandled register class");
195 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
196 MachineBasicBlock::iterator MI,
197 unsigned DestReg, int FrameIdx,
198 const TargetRegisterClass *RC,
199 const TargetRegisterInfo *TRI) const {
200 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
201 // << FrameIdx << "\n";
203 if (MI != MBB.end()) DL = MI->getDebugLoc();
205 if (RC == Alpha::F4RCRegisterClass)
206 BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
207 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
208 else if (RC == Alpha::F8RCRegisterClass)
209 BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
210 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
211 else if (RC == Alpha::GPRCRegisterClass)
212 BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
213 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
215 llvm_unreachable("Unhandled register class");
218 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
220 const SmallVectorImpl<unsigned> &Ops,
221 int FrameIndex) const {
222 if (Ops.size() != 1) return NULL;
224 // Make sure this is a reg-reg copy.
225 unsigned Opc = MI->getOpcode();
227 MachineInstr *NewMI = NULL;
234 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
235 if (Ops[0] == 0) { // move -> store
236 unsigned InReg = MI->getOperand(1).getReg();
237 bool isKill = MI->getOperand(1).isKill();
238 bool isUndef = MI->getOperand(1).isUndef();
239 Opc = (Opc == Alpha::BISr) ? Alpha::STQ :
240 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
241 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
242 .addReg(InReg, getKillRegState(isKill) | getUndefRegState(isUndef))
243 .addFrameIndex(FrameIndex)
245 } else { // load -> move
246 unsigned OutReg = MI->getOperand(0).getReg();
247 bool isDead = MI->getOperand(0).isDead();
248 bool isUndef = MI->getOperand(0).isUndef();
249 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ :
250 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
251 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
252 .addReg(OutReg, RegState::Define | getDeadRegState(isDead) |
253 getUndefRegState(isUndef))
254 .addFrameIndex(FrameIndex)
263 static unsigned AlphaRevCondCode(unsigned Opcode) {
265 case Alpha::BEQ: return Alpha::BNE;
266 case Alpha::BNE: return Alpha::BEQ;
267 case Alpha::BGE: return Alpha::BLT;
268 case Alpha::BGT: return Alpha::BLE;
269 case Alpha::BLE: return Alpha::BGT;
270 case Alpha::BLT: return Alpha::BGE;
271 case Alpha::BLBC: return Alpha::BLBS;
272 case Alpha::BLBS: return Alpha::BLBC;
273 case Alpha::FBEQ: return Alpha::FBNE;
274 case Alpha::FBNE: return Alpha::FBEQ;
275 case Alpha::FBGE: return Alpha::FBLT;
276 case Alpha::FBGT: return Alpha::FBLE;
277 case Alpha::FBLE: return Alpha::FBGT;
278 case Alpha::FBLT: return Alpha::FBGE;
280 llvm_unreachable("Unknown opcode");
282 return 0; // Not reached
286 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
287 MachineBasicBlock *&FBB,
288 SmallVectorImpl<MachineOperand> &Cond,
289 bool AllowModify) const {
290 // If the block has no terminators, it just falls into the block after it.
291 MachineBasicBlock::iterator I = MBB.end();
292 if (I == MBB.begin())
295 while (I->isDebugValue()) {
296 if (I == MBB.begin())
300 if (!isUnpredicatedTerminator(I))
303 // Get the last instruction in the block.
304 MachineInstr *LastInst = I;
306 // If there is only one terminator instruction, process it.
307 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
308 if (LastInst->getOpcode() == Alpha::BR) {
309 TBB = LastInst->getOperand(0).getMBB();
311 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
312 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
313 // Block ends with fall-through condbranch.
314 TBB = LastInst->getOperand(2).getMBB();
315 Cond.push_back(LastInst->getOperand(0));
316 Cond.push_back(LastInst->getOperand(1));
319 // Otherwise, don't know what this is.
323 // Get the instruction before it if it's a terminator.
324 MachineInstr *SecondLastInst = I;
326 // If there are three terminators, we don't know what sort of block this is.
327 if (SecondLastInst && I != MBB.begin() &&
328 isUnpredicatedTerminator(--I))
331 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
332 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
333 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) &&
334 LastInst->getOpcode() == Alpha::BR) {
335 TBB = SecondLastInst->getOperand(2).getMBB();
336 Cond.push_back(SecondLastInst->getOperand(0));
337 Cond.push_back(SecondLastInst->getOperand(1));
338 FBB = LastInst->getOperand(0).getMBB();
342 // If the block ends with two Alpha::BRs, handle it. The second one is not
343 // executed, so remove it.
344 if (SecondLastInst->getOpcode() == Alpha::BR &&
345 LastInst->getOpcode() == Alpha::BR) {
346 TBB = SecondLastInst->getOperand(0).getMBB();
349 I->eraseFromParent();
353 // Otherwise, can't handle this.
357 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
358 MachineBasicBlock::iterator I = MBB.end();
359 if (I == MBB.begin()) return 0;
361 while (I->isDebugValue()) {
362 if (I == MBB.begin())
366 if (I->getOpcode() != Alpha::BR &&
367 I->getOpcode() != Alpha::COND_BRANCH_I &&
368 I->getOpcode() != Alpha::COND_BRANCH_F)
371 // Remove the branch.
372 I->eraseFromParent();
376 if (I == MBB.begin()) return 1;
378 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
379 I->getOpcode() != Alpha::COND_BRANCH_F)
382 // Remove the branch.
383 I->eraseFromParent();
387 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
388 MachineBasicBlock::iterator MI) const {
390 BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
395 bool AlphaInstrInfo::
396 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
397 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
398 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
402 /// getGlobalBaseReg - Return a virtual register initialized with the
403 /// the global base register value. Output instructions required to
404 /// initialize the register in the function entry block, if necessary.
406 unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
407 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
408 unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg();
409 if (GlobalBaseReg != 0)
410 return GlobalBaseReg;
412 // Insert the set of GlobalBaseReg into the first MBB of the function
413 MachineBasicBlock &FirstMBB = MF->front();
414 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
415 MachineRegisterInfo &RegInfo = MF->getRegInfo();
416 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
418 GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
419 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
420 GlobalBaseReg).addReg(Alpha::R29);
421 RegInfo.addLiveIn(Alpha::R29);
423 AlphaFI->setGlobalBaseReg(GlobalBaseReg);
424 return GlobalBaseReg;
427 /// getGlobalRetAddr - Return a virtual register initialized with the
428 /// the global base register value. Output instructions required to
429 /// initialize the register in the function entry block, if necessary.
431 unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const {
432 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
433 unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr();
434 if (GlobalRetAddr != 0)
435 return GlobalRetAddr;
437 // Insert the set of GlobalRetAddr into the first MBB of the function
438 MachineBasicBlock &FirstMBB = MF->front();
439 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
440 MachineRegisterInfo &RegInfo = MF->getRegInfo();
441 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
443 GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
444 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
445 GlobalRetAddr).addReg(Alpha::R26);
446 RegInfo.addLiveIn(Alpha::R26);
448 AlphaFI->setGlobalRetAddr(GlobalRetAddr);
449 return GlobalRetAddr;