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"
24 AlphaInstrInfo::AlphaInstrInfo()
25 : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
29 bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
30 unsigned& sourceReg, unsigned& destReg,
31 unsigned& SrcSR, unsigned& DstSR) const {
32 unsigned oc = MI.getOpcode();
33 if (oc == Alpha::BISr ||
36 oc == Alpha::CPYSSt ||
37 oc == Alpha::CPYSTs) {
40 assert(MI.getNumOperands() >= 3 &&
41 MI.getOperand(0).isReg() &&
42 MI.getOperand(1).isReg() &&
43 MI.getOperand(2).isReg() &&
44 "invalid Alpha BIS instruction!");
45 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
46 sourceReg = MI.getOperand(1).getReg();
47 destReg = MI.getOperand(0).getReg();
56 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
57 int &FrameIndex) const {
58 switch (MI->getOpcode()) {
65 if (MI->getOperand(1).isFI()) {
66 FrameIndex = MI->getOperand(1).getIndex();
67 return MI->getOperand(0).getReg();
75 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
76 int &FrameIndex) const {
77 switch (MI->getOpcode()) {
84 if (MI->getOperand(1).isFI()) {
85 FrameIndex = MI->getOperand(1).getIndex();
86 return MI->getOperand(0).getReg();
93 static bool isAlphaIntCondCode(unsigned Opcode) {
109 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
110 MachineBasicBlock *TBB,
111 MachineBasicBlock *FBB,
112 const SmallVectorImpl<MachineOperand> &Cond) const {
113 // FIXME this should probably have a DebugLoc argument
114 DebugLoc dl = DebugLoc::getUnknownLoc();
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 bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
145 MachineBasicBlock::iterator MI,
146 unsigned DestReg, unsigned SrcReg,
147 const TargetRegisterClass *DestRC,
148 const TargetRegisterClass *SrcRC) const {
149 //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
150 if (DestRC != SrcRC) {
151 // Not yet supported!
155 DebugLoc DL = DebugLoc::getUnknownLoc();
156 if (MI != MBB.end()) DL = MI->getDebugLoc();
158 if (DestRC == Alpha::GPRCRegisterClass) {
159 BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
162 } else if (DestRC == Alpha::F4RCRegisterClass) {
163 BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
166 } else if (DestRC == Alpha::F8RCRegisterClass) {
167 BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
171 // Attempt to copy register that is not GPR or FPR
179 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
180 MachineBasicBlock::iterator MI,
181 unsigned SrcReg, bool isKill, int FrameIdx,
182 const TargetRegisterClass *RC) const {
183 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
184 // << FrameIdx << "\n";
185 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
187 DebugLoc DL = DebugLoc::getUnknownLoc();
188 if (MI != MBB.end()) DL = MI->getDebugLoc();
190 if (RC == Alpha::F4RCRegisterClass)
191 BuildMI(MBB, MI, DL, get(Alpha::STS))
192 .addReg(SrcReg, getKillRegState(isKill))
193 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
194 else if (RC == Alpha::F8RCRegisterClass)
195 BuildMI(MBB, MI, DL, get(Alpha::STT))
196 .addReg(SrcReg, getKillRegState(isKill))
197 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
198 else if (RC == Alpha::GPRCRegisterClass)
199 BuildMI(MBB, MI, DL, get(Alpha::STQ))
200 .addReg(SrcReg, getKillRegState(isKill))
201 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
206 void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
208 SmallVectorImpl<MachineOperand> &Addr,
209 const TargetRegisterClass *RC,
210 SmallVectorImpl<MachineInstr*> &NewMIs) const {
212 if (RC == Alpha::F4RCRegisterClass)
214 else if (RC == Alpha::F8RCRegisterClass)
216 else if (RC == Alpha::GPRCRegisterClass)
220 DebugLoc DL = DebugLoc::getUnknownLoc();
221 MachineInstrBuilder MIB =
222 BuildMI(MF, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill));
223 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
224 MIB.addOperand(Addr[i]);
225 NewMIs.push_back(MIB);
229 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
230 MachineBasicBlock::iterator MI,
231 unsigned DestReg, int FrameIdx,
232 const TargetRegisterClass *RC) const {
233 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
234 // << FrameIdx << "\n";
235 DebugLoc DL = DebugLoc::getUnknownLoc();
236 if (MI != MBB.end()) DL = MI->getDebugLoc();
238 if (RC == Alpha::F4RCRegisterClass)
239 BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
240 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
241 else if (RC == Alpha::F8RCRegisterClass)
242 BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
243 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
244 else if (RC == Alpha::GPRCRegisterClass)
245 BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
246 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
251 void AlphaInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
252 SmallVectorImpl<MachineOperand> &Addr,
253 const TargetRegisterClass *RC,
254 SmallVectorImpl<MachineInstr*> &NewMIs) const {
256 if (RC == Alpha::F4RCRegisterClass)
258 else if (RC == Alpha::F8RCRegisterClass)
260 else if (RC == Alpha::GPRCRegisterClass)
264 DebugLoc DL = DebugLoc::getUnknownLoc();
265 MachineInstrBuilder MIB =
266 BuildMI(MF, DL, get(Opc), DestReg);
267 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
268 MIB.addOperand(Addr[i]);
269 NewMIs.push_back(MIB);
272 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
274 const SmallVectorImpl<unsigned> &Ops,
275 int FrameIndex) const {
276 if (Ops.size() != 1) return NULL;
278 // Make sure this is a reg-reg copy.
279 unsigned Opc = MI->getOpcode();
281 MachineInstr *NewMI = NULL;
288 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
289 if (Ops[0] == 0) { // move -> store
290 unsigned InReg = MI->getOperand(1).getReg();
291 bool isKill = MI->getOperand(1).isKill();
292 Opc = (Opc == Alpha::BISr) ? Alpha::STQ :
293 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
294 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
295 .addReg(InReg, getKillRegState(isKill))
296 .addFrameIndex(FrameIndex)
298 } else { // load -> move
299 unsigned OutReg = MI->getOperand(0).getReg();
300 bool isDead = MI->getOperand(0).isDead();
301 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ :
302 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
303 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
304 .addReg(OutReg, RegState::Define | getDeadRegState(isDead))
305 .addFrameIndex(FrameIndex)
314 static unsigned AlphaRevCondCode(unsigned Opcode) {
316 case Alpha::BEQ: return Alpha::BNE;
317 case Alpha::BNE: return Alpha::BEQ;
318 case Alpha::BGE: return Alpha::BLT;
319 case Alpha::BGT: return Alpha::BLE;
320 case Alpha::BLE: return Alpha::BGT;
321 case Alpha::BLT: return Alpha::BGE;
322 case Alpha::BLBC: return Alpha::BLBS;
323 case Alpha::BLBS: return Alpha::BLBC;
324 case Alpha::FBEQ: return Alpha::FBNE;
325 case Alpha::FBNE: return Alpha::FBEQ;
326 case Alpha::FBGE: return Alpha::FBLT;
327 case Alpha::FBGT: return Alpha::FBLE;
328 case Alpha::FBLE: return Alpha::FBGT;
329 case Alpha::FBLT: return Alpha::FBGE;
331 assert(0 && "Unknown opcode");
333 return 0; // Not reached
337 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
338 MachineBasicBlock *&FBB,
339 SmallVectorImpl<MachineOperand> &Cond,
340 bool AllowModify) const {
341 // If the block has no terminators, it just falls into the block after it.
342 MachineBasicBlock::iterator I = MBB.end();
343 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
346 // Get the last instruction in the block.
347 MachineInstr *LastInst = I;
349 // If there is only one terminator instruction, process it.
350 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
351 if (LastInst->getOpcode() == Alpha::BR) {
352 TBB = LastInst->getOperand(0).getMBB();
354 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
355 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
356 // Block ends with fall-through condbranch.
357 TBB = LastInst->getOperand(2).getMBB();
358 Cond.push_back(LastInst->getOperand(0));
359 Cond.push_back(LastInst->getOperand(1));
362 // Otherwise, don't know what this is.
366 // Get the instruction before it if it's a terminator.
367 MachineInstr *SecondLastInst = I;
369 // If there are three terminators, we don't know what sort of block this is.
370 if (SecondLastInst && I != MBB.begin() &&
371 isUnpredicatedTerminator(--I))
374 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
375 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
376 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) &&
377 LastInst->getOpcode() == Alpha::BR) {
378 TBB = SecondLastInst->getOperand(2).getMBB();
379 Cond.push_back(SecondLastInst->getOperand(0));
380 Cond.push_back(SecondLastInst->getOperand(1));
381 FBB = LastInst->getOperand(0).getMBB();
385 // If the block ends with two Alpha::BRs, handle it. The second one is not
386 // executed, so remove it.
387 if (SecondLastInst->getOpcode() == Alpha::BR &&
388 LastInst->getOpcode() == Alpha::BR) {
389 TBB = SecondLastInst->getOperand(0).getMBB();
392 I->eraseFromParent();
396 // Otherwise, can't handle this.
400 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
401 MachineBasicBlock::iterator I = MBB.end();
402 if (I == MBB.begin()) return 0;
404 if (I->getOpcode() != Alpha::BR &&
405 I->getOpcode() != Alpha::COND_BRANCH_I &&
406 I->getOpcode() != Alpha::COND_BRANCH_F)
409 // Remove the branch.
410 I->eraseFromParent();
414 if (I == MBB.begin()) return 1;
416 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
417 I->getOpcode() != Alpha::COND_BRANCH_F)
420 // Remove the branch.
421 I->eraseFromParent();
425 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
426 MachineBasicBlock::iterator MI) const {
427 DebugLoc DL = DebugLoc::getUnknownLoc();
428 if (MI != MBB.end()) DL = MI->getDebugLoc();
429 BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
434 bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
435 if (MBB.empty()) return false;
437 switch (MBB.back().getOpcode()) {
438 case Alpha::RETDAG: // Return.
440 case Alpha::BR: // Uncond branch.
441 case Alpha::JMP: // Indirect branch.
443 default: return false;
446 bool AlphaInstrInfo::
447 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
448 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
449 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
453 /// getGlobalBaseReg - Return a virtual register initialized with the
454 /// the global base register value. Output instructions required to
455 /// initialize the register in the function entry block, if necessary.
457 unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
458 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
459 unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg();
460 if (GlobalBaseReg != 0)
461 return GlobalBaseReg;
463 // Insert the set of GlobalBaseReg into the first MBB of the function
464 MachineBasicBlock &FirstMBB = MF->front();
465 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
466 MachineRegisterInfo &RegInfo = MF->getRegInfo();
467 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
469 GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
470 bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalBaseReg, Alpha::R29,
471 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass);
472 assert(Ok && "Couldn't assign to global base register!");
473 RegInfo.addLiveIn(Alpha::R29);
475 AlphaFI->setGlobalBaseReg(GlobalBaseReg);
476 return GlobalBaseReg;
479 /// getGlobalRetAddr - Return a virtual register initialized with the
480 /// the global base register value. Output instructions required to
481 /// initialize the register in the function entry block, if necessary.
483 unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const {
484 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
485 unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr();
486 if (GlobalRetAddr != 0)
487 return GlobalRetAddr;
489 // Insert the set of GlobalRetAddr into the first MBB of the function
490 MachineBasicBlock &FirstMBB = MF->front();
491 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
492 MachineRegisterInfo &RegInfo = MF->getRegInfo();
493 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
495 GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
496 bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalRetAddr, Alpha::R26,
497 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass);
498 assert(Ok && "Couldn't assign to global return address register!");
499 RegInfo.addLiveIn(Alpha::R26);
501 AlphaFI->setGlobalRetAddr(GlobalRetAddr);
502 return GlobalRetAddr;