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 "AlphaGenInstrInfo.inc"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 AlphaInstrInfo::AlphaInstrInfo()
23 : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
27 bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
28 unsigned& sourceReg, unsigned& destReg,
29 unsigned& SrcSR, unsigned& DstSR) const {
30 unsigned oc = MI.getOpcode();
31 if (oc == Alpha::BISr ||
34 oc == Alpha::CPYSSt ||
35 oc == Alpha::CPYSTs) {
38 assert(MI.getNumOperands() >= 3 &&
39 MI.getOperand(0).isReg() &&
40 MI.getOperand(1).isReg() &&
41 MI.getOperand(2).isReg() &&
42 "invalid Alpha BIS instruction!");
43 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
44 sourceReg = MI.getOperand(1).getReg();
45 destReg = MI.getOperand(0).getReg();
54 AlphaInstrInfo::isLoadFromStackSlot(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();
73 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
74 int &FrameIndex) const {
75 switch (MI->getOpcode()) {
82 if (MI->getOperand(1).isFI()) {
83 FrameIndex = MI->getOperand(1).getIndex();
84 return MI->getOperand(0).getReg();
91 static bool isAlphaIntCondCode(unsigned Opcode) {
107 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
108 MachineBasicBlock *TBB,
109 MachineBasicBlock *FBB,
110 const SmallVectorImpl<MachineOperand> &Cond) const {
111 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
112 assert((Cond.size() == 2 || Cond.size() == 0) &&
113 "Alpha branch conditions have two components!");
117 if (Cond.empty()) // Unconditional branch
118 BuildMI(&MBB, get(Alpha::BR)).addMBB(TBB);
119 else // Conditional branch
120 if (isAlphaIntCondCode(Cond[0].getImm()))
121 BuildMI(&MBB, get(Alpha::COND_BRANCH_I))
122 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
124 BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
125 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
129 // Two-way Conditional Branch.
130 if (isAlphaIntCondCode(Cond[0].getImm()))
131 BuildMI(&MBB, get(Alpha::COND_BRANCH_I))
132 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
134 BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
135 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
136 BuildMI(&MBB, get(Alpha::BR)).addMBB(FBB);
140 bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
141 MachineBasicBlock::iterator MI,
142 unsigned DestReg, unsigned SrcReg,
143 const TargetRegisterClass *DestRC,
144 const TargetRegisterClass *SrcRC) const {
145 //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
146 if (DestRC != SrcRC) {
147 // Not yet supported!
151 DebugLoc DL = DebugLoc::getUnknownLoc();
152 if (MI != MBB.end()) DL = MI->getDebugLoc();
154 if (DestRC == Alpha::GPRCRegisterClass) {
155 BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
158 } else if (DestRC == Alpha::F4RCRegisterClass) {
159 BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
162 } else if (DestRC == Alpha::F8RCRegisterClass) {
163 BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
167 // Attempt to copy register that is not GPR or FPR
175 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
176 MachineBasicBlock::iterator MI,
177 unsigned SrcReg, bool isKill, int FrameIdx,
178 const TargetRegisterClass *RC) const {
179 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
180 // << FrameIdx << "\n";
181 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
183 DebugLoc DL = DebugLoc::getUnknownLoc();
184 if (MI != MBB.end()) DL = MI->getDebugLoc();
186 if (RC == Alpha::F4RCRegisterClass)
187 BuildMI(MBB, MI, DL, get(Alpha::STS))
188 .addReg(SrcReg, false, false, isKill)
189 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
190 else if (RC == Alpha::F8RCRegisterClass)
191 BuildMI(MBB, MI, DL, get(Alpha::STT))
192 .addReg(SrcReg, false, false, isKill)
193 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
194 else if (RC == Alpha::GPRCRegisterClass)
195 BuildMI(MBB, MI, DL, get(Alpha::STQ))
196 .addReg(SrcReg, false, false, isKill)
197 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
202 void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
204 SmallVectorImpl<MachineOperand> &Addr,
205 const TargetRegisterClass *RC,
206 SmallVectorImpl<MachineInstr*> &NewMIs) const {
208 if (RC == Alpha::F4RCRegisterClass)
210 else if (RC == Alpha::F8RCRegisterClass)
212 else if (RC == Alpha::GPRCRegisterClass)
216 MachineInstrBuilder MIB =
217 BuildMI(MF, get(Opc)).addReg(SrcReg, false, false, isKill);
218 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
219 MachineOperand &MO = Addr[i];
221 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
223 MIB.addImm(MO.getImm());
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 MachineInstrBuilder MIB =
265 BuildMI(MF, get(Opc), DestReg);
266 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
267 MachineOperand &MO = Addr[i];
269 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
271 MIB.addImm(MO.getImm());
273 NewMIs.push_back(MIB);
276 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
278 const SmallVectorImpl<unsigned> &Ops,
279 int FrameIndex) const {
280 if (Ops.size() != 1) return NULL;
282 // Make sure this is a reg-reg copy.
283 unsigned Opc = MI->getOpcode();
285 MachineInstr *NewMI = NULL;
292 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
293 if (Ops[0] == 0) { // move -> store
294 unsigned InReg = MI->getOperand(1).getReg();
295 bool isKill = MI->getOperand(1).isKill();
296 Opc = (Opc == Alpha::BISr) ? Alpha::STQ :
297 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
298 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
299 .addReg(InReg, false, false, isKill)
300 .addFrameIndex(FrameIndex)
302 } else { // load -> move
303 unsigned OutReg = MI->getOperand(0).getReg();
304 bool isDead = MI->getOperand(0).isDead();
305 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ :
306 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
307 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
308 .addReg(OutReg, true, false, false, isDead)
309 .addFrameIndex(FrameIndex)
318 static unsigned AlphaRevCondCode(unsigned Opcode) {
320 case Alpha::BEQ: return Alpha::BNE;
321 case Alpha::BNE: return Alpha::BEQ;
322 case Alpha::BGE: return Alpha::BLT;
323 case Alpha::BGT: return Alpha::BLE;
324 case Alpha::BLE: return Alpha::BGT;
325 case Alpha::BLT: return Alpha::BGE;
326 case Alpha::BLBC: return Alpha::BLBS;
327 case Alpha::BLBS: return Alpha::BLBC;
328 case Alpha::FBEQ: return Alpha::FBNE;
329 case Alpha::FBNE: return Alpha::FBEQ;
330 case Alpha::FBGE: return Alpha::FBLT;
331 case Alpha::FBGT: return Alpha::FBLE;
332 case Alpha::FBLE: return Alpha::FBGT;
333 case Alpha::FBLT: return Alpha::FBGE;
335 assert(0 && "Unknown opcode");
337 return 0; // Not reached
341 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
342 MachineBasicBlock *&FBB,
343 SmallVectorImpl<MachineOperand> &Cond,
344 bool AllowModify) const {
345 // If the block has no terminators, it just falls into the block after it.
346 MachineBasicBlock::iterator I = MBB.end();
347 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
350 // Get the last instruction in the block.
351 MachineInstr *LastInst = I;
353 // If there is only one terminator instruction, process it.
354 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
355 if (LastInst->getOpcode() == Alpha::BR) {
356 TBB = LastInst->getOperand(0).getMBB();
358 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
359 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
360 // Block ends with fall-through condbranch.
361 TBB = LastInst->getOperand(2).getMBB();
362 Cond.push_back(LastInst->getOperand(0));
363 Cond.push_back(LastInst->getOperand(1));
366 // Otherwise, don't know what this is.
370 // Get the instruction before it if it's a terminator.
371 MachineInstr *SecondLastInst = I;
373 // If there are three terminators, we don't know what sort of block this is.
374 if (SecondLastInst && I != MBB.begin() &&
375 isUnpredicatedTerminator(--I))
378 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
379 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
380 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) &&
381 LastInst->getOpcode() == Alpha::BR) {
382 TBB = SecondLastInst->getOperand(2).getMBB();
383 Cond.push_back(SecondLastInst->getOperand(0));
384 Cond.push_back(SecondLastInst->getOperand(1));
385 FBB = LastInst->getOperand(0).getMBB();
389 // If the block ends with two Alpha::BRs, handle it. The second one is not
390 // executed, so remove it.
391 if (SecondLastInst->getOpcode() == Alpha::BR &&
392 LastInst->getOpcode() == Alpha::BR) {
393 TBB = SecondLastInst->getOperand(0).getMBB();
396 I->eraseFromParent();
400 // Otherwise, can't handle this.
404 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
405 MachineBasicBlock::iterator I = MBB.end();
406 if (I == MBB.begin()) return 0;
408 if (I->getOpcode() != Alpha::BR &&
409 I->getOpcode() != Alpha::COND_BRANCH_I &&
410 I->getOpcode() != Alpha::COND_BRANCH_F)
413 // Remove the branch.
414 I->eraseFromParent();
418 if (I == MBB.begin()) return 1;
420 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
421 I->getOpcode() != Alpha::COND_BRANCH_F)
424 // Remove the branch.
425 I->eraseFromParent();
429 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
430 MachineBasicBlock::iterator MI) const {
431 DebugLoc DL = DebugLoc::getUnknownLoc();
432 if (MI != MBB.end()) DL = MI->getDebugLoc();
433 BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
438 bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
439 if (MBB.empty()) return false;
441 switch (MBB.back().getOpcode()) {
442 case Alpha::RETDAG: // Return.
444 case Alpha::BR: // Uncond branch.
445 case Alpha::JMP: // Indirect branch.
447 default: return false;
450 bool AlphaInstrInfo::
451 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
452 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
453 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));