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 // FIXME this should probably have a DebugLoc argument
112 DebugLoc dl = DebugLoc::getUnknownLoc();
113 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
114 assert((Cond.size() == 2 || Cond.size() == 0) &&
115 "Alpha branch conditions have two components!");
119 if (Cond.empty()) // Unconditional branch
120 BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(TBB);
121 else // Conditional branch
122 if (isAlphaIntCondCode(Cond[0].getImm()))
123 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
124 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
126 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
127 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
131 // Two-way Conditional Branch.
132 if (isAlphaIntCondCode(Cond[0].getImm()))
133 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
134 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
136 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
137 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
138 BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(FBB);
142 bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
143 MachineBasicBlock::iterator MI,
144 unsigned DestReg, unsigned SrcReg,
145 const TargetRegisterClass *DestRC,
146 const TargetRegisterClass *SrcRC) const {
147 //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
148 if (DestRC != SrcRC) {
149 // Not yet supported!
153 DebugLoc DL = DebugLoc::getUnknownLoc();
154 if (MI != MBB.end()) DL = MI->getDebugLoc();
156 if (DestRC == Alpha::GPRCRegisterClass) {
157 BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
160 } else if (DestRC == Alpha::F4RCRegisterClass) {
161 BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
164 } else if (DestRC == Alpha::F8RCRegisterClass) {
165 BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
169 // Attempt to copy register that is not GPR or FPR
177 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
178 MachineBasicBlock::iterator MI,
179 unsigned SrcReg, bool isKill, int FrameIdx,
180 const TargetRegisterClass *RC) const {
181 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
182 // << FrameIdx << "\n";
183 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
185 DebugLoc DL = DebugLoc::getUnknownLoc();
186 if (MI != MBB.end()) DL = MI->getDebugLoc();
188 if (RC == Alpha::F4RCRegisterClass)
189 BuildMI(MBB, MI, DL, get(Alpha::STS))
190 .addReg(SrcReg, false, false, isKill)
191 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
192 else if (RC == Alpha::F8RCRegisterClass)
193 BuildMI(MBB, MI, DL, get(Alpha::STT))
194 .addReg(SrcReg, false, false, isKill)
195 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
196 else if (RC == Alpha::GPRCRegisterClass)
197 BuildMI(MBB, MI, DL, get(Alpha::STQ))
198 .addReg(SrcReg, false, false, isKill)
199 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
204 void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
206 SmallVectorImpl<MachineOperand> &Addr,
207 const TargetRegisterClass *RC,
208 SmallVectorImpl<MachineInstr*> &NewMIs) const {
210 if (RC == Alpha::F4RCRegisterClass)
212 else if (RC == Alpha::F8RCRegisterClass)
214 else if (RC == Alpha::GPRCRegisterClass)
218 DebugLoc DL = DebugLoc::getUnknownLoc();
219 MachineInstrBuilder MIB =
220 BuildMI(MF, DL, get(Opc)).addReg(SrcReg, false, false, isKill);
221 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
222 MachineOperand &MO = Addr[i];
224 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
226 MIB.addImm(MO.getImm());
228 NewMIs.push_back(MIB);
232 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
233 MachineBasicBlock::iterator MI,
234 unsigned DestReg, int FrameIdx,
235 const TargetRegisterClass *RC) const {
236 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
237 // << FrameIdx << "\n";
238 DebugLoc DL = DebugLoc::getUnknownLoc();
239 if (MI != MBB.end()) DL = MI->getDebugLoc();
241 if (RC == Alpha::F4RCRegisterClass)
242 BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
243 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
244 else if (RC == Alpha::F8RCRegisterClass)
245 BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
246 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
247 else if (RC == Alpha::GPRCRegisterClass)
248 BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
249 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
254 void AlphaInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
255 SmallVectorImpl<MachineOperand> &Addr,
256 const TargetRegisterClass *RC,
257 SmallVectorImpl<MachineInstr*> &NewMIs) const {
259 if (RC == Alpha::F4RCRegisterClass)
261 else if (RC == Alpha::F8RCRegisterClass)
263 else if (RC == Alpha::GPRCRegisterClass)
267 DebugLoc DL = DebugLoc::getUnknownLoc();
268 MachineInstrBuilder MIB =
269 BuildMI(MF, DL, get(Opc), DestReg);
270 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
271 MachineOperand &MO = Addr[i];
273 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
275 MIB.addImm(MO.getImm());
277 NewMIs.push_back(MIB);
280 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
282 const SmallVectorImpl<unsigned> &Ops,
283 int FrameIndex) const {
284 if (Ops.size() != 1) return NULL;
286 // Make sure this is a reg-reg copy.
287 unsigned Opc = MI->getOpcode();
289 MachineInstr *NewMI = NULL;
296 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
297 if (Ops[0] == 0) { // move -> store
298 unsigned InReg = MI->getOperand(1).getReg();
299 bool isKill = MI->getOperand(1).isKill();
300 Opc = (Opc == Alpha::BISr) ? Alpha::STQ :
301 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
302 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
303 .addReg(InReg, false, false, isKill)
304 .addFrameIndex(FrameIndex)
306 } else { // load -> move
307 unsigned OutReg = MI->getOperand(0).getReg();
308 bool isDead = MI->getOperand(0).isDead();
309 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ :
310 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
311 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
312 .addReg(OutReg, true, false, false, isDead)
313 .addFrameIndex(FrameIndex)
322 static unsigned AlphaRevCondCode(unsigned Opcode) {
324 case Alpha::BEQ: return Alpha::BNE;
325 case Alpha::BNE: return Alpha::BEQ;
326 case Alpha::BGE: return Alpha::BLT;
327 case Alpha::BGT: return Alpha::BLE;
328 case Alpha::BLE: return Alpha::BGT;
329 case Alpha::BLT: return Alpha::BGE;
330 case Alpha::BLBC: return Alpha::BLBS;
331 case Alpha::BLBS: return Alpha::BLBC;
332 case Alpha::FBEQ: return Alpha::FBNE;
333 case Alpha::FBNE: return Alpha::FBEQ;
334 case Alpha::FBGE: return Alpha::FBLT;
335 case Alpha::FBGT: return Alpha::FBLE;
336 case Alpha::FBLE: return Alpha::FBGT;
337 case Alpha::FBLT: return Alpha::FBGE;
339 assert(0 && "Unknown opcode");
341 return 0; // Not reached
345 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
346 MachineBasicBlock *&FBB,
347 SmallVectorImpl<MachineOperand> &Cond,
348 bool AllowModify) const {
349 // If the block has no terminators, it just falls into the block after it.
350 MachineBasicBlock::iterator I = MBB.end();
351 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
354 // Get the last instruction in the block.
355 MachineInstr *LastInst = I;
357 // If there is only one terminator instruction, process it.
358 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
359 if (LastInst->getOpcode() == Alpha::BR) {
360 TBB = LastInst->getOperand(0).getMBB();
362 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
363 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
364 // Block ends with fall-through condbranch.
365 TBB = LastInst->getOperand(2).getMBB();
366 Cond.push_back(LastInst->getOperand(0));
367 Cond.push_back(LastInst->getOperand(1));
370 // Otherwise, don't know what this is.
374 // Get the instruction before it if it's a terminator.
375 MachineInstr *SecondLastInst = I;
377 // If there are three terminators, we don't know what sort of block this is.
378 if (SecondLastInst && I != MBB.begin() &&
379 isUnpredicatedTerminator(--I))
382 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
383 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
384 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) &&
385 LastInst->getOpcode() == Alpha::BR) {
386 TBB = SecondLastInst->getOperand(2).getMBB();
387 Cond.push_back(SecondLastInst->getOperand(0));
388 Cond.push_back(SecondLastInst->getOperand(1));
389 FBB = LastInst->getOperand(0).getMBB();
393 // If the block ends with two Alpha::BRs, handle it. The second one is not
394 // executed, so remove it.
395 if (SecondLastInst->getOpcode() == Alpha::BR &&
396 LastInst->getOpcode() == Alpha::BR) {
397 TBB = SecondLastInst->getOperand(0).getMBB();
400 I->eraseFromParent();
404 // Otherwise, can't handle this.
408 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
409 MachineBasicBlock::iterator I = MBB.end();
410 if (I == MBB.begin()) return 0;
412 if (I->getOpcode() != Alpha::BR &&
413 I->getOpcode() != Alpha::COND_BRANCH_I &&
414 I->getOpcode() != Alpha::COND_BRANCH_F)
417 // Remove the branch.
418 I->eraseFromParent();
422 if (I == MBB.begin()) return 1;
424 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
425 I->getOpcode() != Alpha::COND_BRANCH_F)
428 // Remove the branch.
429 I->eraseFromParent();
433 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
434 MachineBasicBlock::iterator MI) const {
435 DebugLoc DL = DebugLoc::getUnknownLoc();
436 if (MI != MBB.end()) DL = MI->getDebugLoc();
437 BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
442 bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
443 if (MBB.empty()) return false;
445 switch (MBB.back().getOpcode()) {
446 case Alpha::RETDAG: // Return.
448 case Alpha::BR: // Uncond branch.
449 case Alpha::JMP: // Indirect branch.
451 default: return false;
454 bool AlphaInstrInfo::
455 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
456 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
457 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));