blob: 139a4db655c859b5de4af193ac5116bb2e3737da [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaInstrInfo.cpp - Alpha Instruction Information -------*- C++ -*-===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains the Alpha implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
15#include "AlphaInstrInfo.h"
Dan Gohman99114052009-06-03 20:30:14 +000016#include "AlphaMachineFunctionInfo.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000017#include "AlphaGenInstrInfo.inc"
Dan Gohman99114052009-06-03 20:30:14 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Owen Anderson718cb662007-09-07 04:06:50 +000019#include "llvm/ADT/STLExtras.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000020#include "llvm/ADT/SmallVector.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Torok Edwin804e0fe2009-07-08 19:04:27 +000022#include "llvm/Support/ErrorHandling.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000023using namespace llvm;
24
25AlphaInstrInfo::AlphaInstrInfo()
Chris Lattner64105522008-01-01 01:03:04 +000026 : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
Evan Cheng7ce45782006-11-13 23:36:35 +000027 RI(*this) { }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000028
29
30bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
Evan Cheng04ee5a12009-01-20 19:12:24 +000031 unsigned& sourceReg, unsigned& destReg,
32 unsigned& SrcSR, unsigned& DstSR) const {
Chris Lattnercc8cd0c2008-01-07 02:48:55 +000033 unsigned oc = MI.getOpcode();
Andrew Lenharth6bbf6b02006-10-31 23:46:56 +000034 if (oc == Alpha::BISr ||
Andrew Lenharthddc877c2006-03-09 18:18:51 +000035 oc == Alpha::CPYSS ||
36 oc == Alpha::CPYST ||
37 oc == Alpha::CPYSSt ||
38 oc == Alpha::CPYSTs) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000039 // or r1, r2, r2
40 // cpys(s|t) r1 r2 r2
Evan Cheng1e3417292007-04-25 07:12:14 +000041 assert(MI.getNumOperands() >= 3 &&
Dan Gohmand735b802008-10-03 15:45:36 +000042 MI.getOperand(0).isReg() &&
43 MI.getOperand(1).isReg() &&
44 MI.getOperand(2).isReg() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +000045 "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();
Evan Cheng04ee5a12009-01-20 19:12:24 +000049 SrcSR = DstSR = 0;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000050 return true;
51 }
52 }
53 return false;
54}
Chris Lattner40839602006-02-02 20:12:32 +000055
56unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000057AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
58 int &FrameIndex) const {
Chris Lattner40839602006-02-02 20:12:32 +000059 switch (MI->getOpcode()) {
60 case Alpha::LDL:
61 case Alpha::LDQ:
62 case Alpha::LDBU:
63 case Alpha::LDWU:
64 case Alpha::LDS:
65 case Alpha::LDT:
Dan Gohmand735b802008-10-03 15:45:36 +000066 if (MI->getOperand(1).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000067 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +000068 return MI->getOperand(0).getReg();
69 }
70 break;
71 }
72 return 0;
73}
74
Andrew Lenharth133d3102006-02-03 03:07:37 +000075unsigned
Dan Gohmancbad42c2008-11-18 19:49:32 +000076AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
77 int &FrameIndex) const {
Andrew Lenharth133d3102006-02-03 03:07:37 +000078 switch (MI->getOpcode()) {
79 case Alpha::STL:
80 case Alpha::STQ:
81 case Alpha::STB:
82 case Alpha::STW:
83 case Alpha::STS:
84 case Alpha::STT:
Dan Gohmand735b802008-10-03 15:45:36 +000085 if (MI->getOperand(1).isFI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000086 FrameIndex = MI->getOperand(1).getIndex();
Andrew Lenharth133d3102006-02-03 03:07:37 +000087 return MI->getOperand(0).getReg();
88 }
89 break;
90 }
91 return 0;
92}
93
Andrew Lenharthf81173f2006-10-31 16:49:55 +000094static bool isAlphaIntCondCode(unsigned Opcode) {
95 switch (Opcode) {
96 case Alpha::BEQ:
97 case Alpha::BNE:
98 case Alpha::BGE:
99 case Alpha::BGT:
100 case Alpha::BLE:
101 case Alpha::BLT:
102 case Alpha::BLBC:
103 case Alpha::BLBS:
104 return true;
105 default:
106 return false;
107 }
108}
109
Owen Anderson44eb65c2008-08-14 22:49:33 +0000110unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000111 MachineBasicBlock *TBB,
112 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000113 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesen01b36e62009-02-13 02:30:42 +0000114 // FIXME this should probably have a DebugLoc argument
115 DebugLoc dl = DebugLoc::getUnknownLoc();
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000116 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
117 assert((Cond.size() == 2 || Cond.size() == 0) &&
118 "Alpha branch conditions have two components!");
119
120 // One-way branch.
121 if (FBB == 0) {
122 if (Cond.empty()) // Unconditional branch
Dale Johannesen01b36e62009-02-13 02:30:42 +0000123 BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(TBB);
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000124 else // Conditional branch
125 if (isAlphaIntCondCode(Cond[0].getImm()))
Dale Johannesen01b36e62009-02-13 02:30:42 +0000126 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000127 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
128 else
Dale Johannesen01b36e62009-02-13 02:30:42 +0000129 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000130 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000131 return 1;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000132 }
133
134 // Two-way Conditional Branch.
135 if (isAlphaIntCondCode(Cond[0].getImm()))
Dale Johannesen01b36e62009-02-13 02:30:42 +0000136 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000137 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
138 else
Dale Johannesen01b36e62009-02-13 02:30:42 +0000139 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000140 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Dale Johannesen01b36e62009-02-13 02:30:42 +0000141 BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000142 return 2;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000143}
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000144
Owen Anderson940f83e2008-08-26 18:03:31 +0000145bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000146 MachineBasicBlock::iterator MI,
147 unsigned DestReg, unsigned SrcReg,
148 const TargetRegisterClass *DestRC,
149 const TargetRegisterClass *SrcRC) const {
Owen Andersond10fd972007-12-31 06:32:00 +0000150 //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
151 if (DestRC != SrcRC) {
Owen Anderson940f83e2008-08-26 18:03:31 +0000152 // Not yet supported!
153 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000154 }
155
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000156 DebugLoc DL = DebugLoc::getUnknownLoc();
157 if (MI != MBB.end()) DL = MI->getDebugLoc();
158
Owen Andersond10fd972007-12-31 06:32:00 +0000159 if (DestRC == Alpha::GPRCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000160 BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
161 .addReg(SrcReg)
162 .addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000163 } else if (DestRC == Alpha::F4RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000164 BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
165 .addReg(SrcReg)
166 .addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000167 } else if (DestRC == Alpha::F8RCRegisterClass) {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000168 BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
169 .addReg(SrcReg)
170 .addReg(SrcReg);
Owen Andersond10fd972007-12-31 06:32:00 +0000171 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +0000172 // Attempt to copy register that is not GPR or FPR
173 return false;
Owen Andersond10fd972007-12-31 06:32:00 +0000174 }
Owen Anderson940f83e2008-08-26 18:03:31 +0000175
176 return true;
Owen Andersond10fd972007-12-31 06:32:00 +0000177}
178
Owen Andersonf6372aa2008-01-01 21:11:32 +0000179void
180AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000181 MachineBasicBlock::iterator MI,
182 unsigned SrcReg, bool isKill, int FrameIdx,
183 const TargetRegisterClass *RC) const {
Owen Andersonf6372aa2008-01-01 21:11:32 +0000184 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
185 // << FrameIdx << "\n";
186 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000187
188 DebugLoc DL = DebugLoc::getUnknownLoc();
189 if (MI != MBB.end()) DL = MI->getDebugLoc();
190
Owen Andersonf6372aa2008-01-01 21:11:32 +0000191 if (RC == Alpha::F4RCRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000192 BuildMI(MBB, MI, DL, get(Alpha::STS))
Bill Wendling587daed2009-05-13 21:33:08 +0000193 .addReg(SrcReg, getKillRegState(isKill))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000194 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
195 else if (RC == Alpha::F8RCRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000196 BuildMI(MBB, MI, DL, get(Alpha::STT))
Bill Wendling587daed2009-05-13 21:33:08 +0000197 .addReg(SrcReg, getKillRegState(isKill))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000198 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
199 else if (RC == Alpha::GPRCRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000200 BuildMI(MBB, MI, DL, get(Alpha::STQ))
Bill Wendling587daed2009-05-13 21:33:08 +0000201 .addReg(SrcReg, getKillRegState(isKill))
Owen Andersonf6372aa2008-01-01 21:11:32 +0000202 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
203 else
Torok Edwindac237e2009-07-08 20:53:28 +0000204 LLVM_UNREACHABLE("Unhandled register class");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000205}
206
207void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
208 bool isKill,
209 SmallVectorImpl<MachineOperand> &Addr,
210 const TargetRegisterClass *RC,
211 SmallVectorImpl<MachineInstr*> &NewMIs) const {
212 unsigned Opc = 0;
213 if (RC == Alpha::F4RCRegisterClass)
214 Opc = Alpha::STS;
215 else if (RC == Alpha::F8RCRegisterClass)
216 Opc = Alpha::STT;
217 else if (RC == Alpha::GPRCRegisterClass)
218 Opc = Alpha::STQ;
219 else
Torok Edwindac237e2009-07-08 20:53:28 +0000220 LLVM_UNREACHABLE("Unhandled register class");
Dale Johannesenc5b50512009-02-12 23:24:44 +0000221 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000222 MachineInstrBuilder MIB =
Bill Wendling587daed2009-05-13 21:33:08 +0000223 BuildMI(MF, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill));
Dan Gohman97357612009-02-18 05:45:50 +0000224 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
225 MIB.addOperand(Addr[i]);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000226 NewMIs.push_back(MIB);
227}
228
229void
230AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
231 MachineBasicBlock::iterator MI,
232 unsigned DestReg, int FrameIdx,
233 const TargetRegisterClass *RC) const {
234 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
235 // << FrameIdx << "\n";
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000236 DebugLoc DL = DebugLoc::getUnknownLoc();
237 if (MI != MBB.end()) DL = MI->getDebugLoc();
238
Owen Andersonf6372aa2008-01-01 21:11:32 +0000239 if (RC == Alpha::F4RCRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000240 BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000241 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
242 else if (RC == Alpha::F8RCRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000243 BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000244 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
245 else if (RC == Alpha::GPRCRegisterClass)
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000246 BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
Owen Andersonf6372aa2008-01-01 21:11:32 +0000247 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
248 else
Torok Edwindac237e2009-07-08 20:53:28 +0000249 LLVM_UNREACHABLE("Unhandled register class");
Owen Andersonf6372aa2008-01-01 21:11:32 +0000250}
251
252void AlphaInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
253 SmallVectorImpl<MachineOperand> &Addr,
254 const TargetRegisterClass *RC,
255 SmallVectorImpl<MachineInstr*> &NewMIs) const {
256 unsigned Opc = 0;
257 if (RC == Alpha::F4RCRegisterClass)
258 Opc = Alpha::LDS;
259 else if (RC == Alpha::F8RCRegisterClass)
260 Opc = Alpha::LDT;
261 else if (RC == Alpha::GPRCRegisterClass)
262 Opc = Alpha::LDQ;
263 else
Torok Edwindac237e2009-07-08 20:53:28 +0000264 LLVM_UNREACHABLE("Unhandled register class");
Dale Johannesenc5b50512009-02-12 23:24:44 +0000265 DebugLoc DL = DebugLoc::getUnknownLoc();
Owen Andersonf6372aa2008-01-01 21:11:32 +0000266 MachineInstrBuilder MIB =
Dale Johannesenc5b50512009-02-12 23:24:44 +0000267 BuildMI(MF, DL, get(Opc), DestReg);
Dan Gohman97357612009-02-18 05:45:50 +0000268 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
269 MIB.addOperand(Addr[i]);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000270 NewMIs.push_back(MIB);
271}
272
Dan Gohmanc54baa22008-12-03 18:43:12 +0000273MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
274 MachineInstr *MI,
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000275 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanc54baa22008-12-03 18:43:12 +0000276 int FrameIndex) const {
Owen Anderson43dbe052008-01-07 01:35:02 +0000277 if (Ops.size() != 1) return NULL;
278
279 // Make sure this is a reg-reg copy.
280 unsigned Opc = MI->getOpcode();
281
282 MachineInstr *NewMI = NULL;
283 switch(Opc) {
284 default:
285 break;
286 case Alpha::BISr:
287 case Alpha::CPYSS:
288 case Alpha::CPYST:
289 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
290 if (Ops[0] == 0) { // move -> store
291 unsigned InReg = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000292 bool isKill = MI->getOperand(1).isKill();
Evan Cheng2578ba22009-07-01 01:59:31 +0000293 bool isUndef = MI->getOperand(1).isUndef();
Owen Anderson43dbe052008-01-07 01:35:02 +0000294 Opc = (Opc == Alpha::BISr) ? Alpha::STQ :
295 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000296 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
Evan Cheng2578ba22009-07-01 01:59:31 +0000297 .addReg(InReg, getKillRegState(isKill) | getUndefRegState(isUndef))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000298 .addFrameIndex(FrameIndex)
Owen Anderson43dbe052008-01-07 01:35:02 +0000299 .addReg(Alpha::F31);
300 } else { // load -> move
301 unsigned OutReg = MI->getOperand(0).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +0000302 bool isDead = MI->getOperand(0).isDead();
Evan Cheng2578ba22009-07-01 01:59:31 +0000303 bool isUndef = MI->getOperand(0).isUndef();
Owen Anderson43dbe052008-01-07 01:35:02 +0000304 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ :
305 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000306 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
Evan Cheng2578ba22009-07-01 01:59:31 +0000307 .addReg(OutReg, RegState::Define | getDeadRegState(isDead) |
308 getUndefRegState(isUndef))
Evan Cheng9f1c8312008-07-03 09:09:37 +0000309 .addFrameIndex(FrameIndex)
Owen Anderson43dbe052008-01-07 01:35:02 +0000310 .addReg(Alpha::F31);
311 }
312 }
313 break;
314 }
Evan Cheng9f1c8312008-07-03 09:09:37 +0000315 return NewMI;
Owen Anderson43dbe052008-01-07 01:35:02 +0000316}
317
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000318static unsigned AlphaRevCondCode(unsigned Opcode) {
319 switch (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;
334 default:
Torok Edwinc25e7582009-07-11 20:10:48 +0000335 LLVM_UNREACHABLE("Unknown opcode");
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000336 }
Chris Lattnerd27c9912008-03-30 18:22:13 +0000337 return 0; // Not reached
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000338}
339
340// Branch analysis.
341bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
Evan Chengdc54d312009-02-09 07:14:22 +0000342 MachineBasicBlock *&FBB,
343 SmallVectorImpl<MachineOperand> &Cond,
344 bool AllowModify) const {
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000345 // If the block has no terminators, it just falls into the block after it.
346 MachineBasicBlock::iterator I = MBB.end();
Evan Chengbfd2ec42007-06-08 21:59:56 +0000347 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000348 return false;
349
350 // Get the last instruction in the block.
351 MachineInstr *LastInst = I;
352
353 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000354 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000355 if (LastInst->getOpcode() == Alpha::BR) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000356 TBB = LastInst->getOperand(0).getMBB();
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000357 return false;
358 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
359 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
360 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000361 TBB = LastInst->getOperand(2).getMBB();
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000362 Cond.push_back(LastInst->getOperand(0));
363 Cond.push_back(LastInst->getOperand(1));
364 return false;
365 }
366 // Otherwise, don't know what this is.
367 return true;
368 }
369
370 // Get the instruction before it if it's a terminator.
371 MachineInstr *SecondLastInst = I;
372
373 // If there are three terminators, we don't know what sort of block this is.
374 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000375 isUnpredicatedTerminator(--I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000376 return true;
377
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) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000382 TBB = SecondLastInst->getOperand(2).getMBB();
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000383 Cond.push_back(SecondLastInst->getOperand(0));
384 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000385 FBB = LastInst->getOperand(0).getMBB();
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000386 return false;
387 }
388
Dale Johannesen13e8b512007-06-13 17:59:52 +0000389 // 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) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000393 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000394 I = LastInst;
Evan Chengdc54d312009-02-09 07:14:22 +0000395 if (AllowModify)
396 I->eraseFromParent();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000397 return false;
398 }
399
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000400 // Otherwise, can't handle this.
401 return true;
402}
403
Evan Chengb5cdaa22007-05-18 00:05:48 +0000404unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000405 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000406 if (I == MBB.begin()) return 0;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000407 --I;
408 if (I->getOpcode() != Alpha::BR &&
409 I->getOpcode() != Alpha::COND_BRANCH_I &&
410 I->getOpcode() != Alpha::COND_BRANCH_F)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000411 return 0;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000412
413 // Remove the branch.
414 I->eraseFromParent();
415
416 I = MBB.end();
417
Evan Chengb5cdaa22007-05-18 00:05:48 +0000418 if (I == MBB.begin()) return 1;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000419 --I;
420 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
421 I->getOpcode() != Alpha::COND_BRANCH_F)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000422 return 1;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000423
424 // Remove the branch.
425 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000426 return 2;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000427}
428
429void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
430 MachineBasicBlock::iterator MI) const {
Bill Wendlingd1c321a2009-02-12 00:02:55 +0000431 DebugLoc DL = DebugLoc::getUnknownLoc();
432 if (MI != MBB.end()) DL = MI->getDebugLoc();
433 BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
434 .addReg(Alpha::R31)
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000435 .addReg(Alpha::R31);
436}
437
Dan Gohman8e8b8a22008-10-16 01:49:15 +0000438bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000439 if (MBB.empty()) return false;
440
441 switch (MBB.back().getOpcode()) {
Evan Cheng126f17a2007-05-21 18:44:17 +0000442 case Alpha::RETDAG: // Return.
443 case Alpha::RETDAGp:
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000444 case Alpha::BR: // Uncond branch.
445 case Alpha::JMP: // Indirect branch.
446 return true;
447 default: return false;
448 }
449}
450bool AlphaInstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +0000451ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000452 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
453 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
454 return false;
455}
456
Dan Gohman99114052009-06-03 20:30:14 +0000457/// getGlobalBaseReg - Return a virtual register initialized with the
458/// the global base register value. Output instructions required to
459/// initialize the register in the function entry block, if necessary.
460///
461unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
462 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
463 unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg();
464 if (GlobalBaseReg != 0)
465 return GlobalBaseReg;
466
467 // Insert the set of GlobalBaseReg into the first MBB of the function
468 MachineBasicBlock &FirstMBB = MF->front();
469 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
470 MachineRegisterInfo &RegInfo = MF->getRegInfo();
471 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
472
473 GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
474 bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalBaseReg, Alpha::R29,
475 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass);
476 assert(Ok && "Couldn't assign to global base register!");
Duncan Sands8d8628a2009-07-03 16:03:33 +0000477 Ok = Ok; // Silence warning when assertions are turned off.
Dan Gohman99114052009-06-03 20:30:14 +0000478 RegInfo.addLiveIn(Alpha::R29);
479
480 AlphaFI->setGlobalBaseReg(GlobalBaseReg);
481 return GlobalBaseReg;
482}
483
484/// getGlobalRetAddr - Return a virtual register initialized with the
485/// the global base register value. Output instructions required to
486/// initialize the register in the function entry block, if necessary.
487///
488unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const {
489 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
490 unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr();
491 if (GlobalRetAddr != 0)
492 return GlobalRetAddr;
493
494 // Insert the set of GlobalRetAddr into the first MBB of the function
495 MachineBasicBlock &FirstMBB = MF->front();
496 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
497 MachineRegisterInfo &RegInfo = MF->getRegInfo();
498 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
499
500 GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
501 bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalRetAddr, Alpha::R26,
502 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass);
503 assert(Ok && "Couldn't assign to global return address register!");
Duncan Sands8d8628a2009-07-03 16:03:33 +0000504 Ok = Ok; // Silence warning when assertions are turned off.
Dan Gohman99114052009-06-03 20:30:14 +0000505 RegInfo.addLiveIn(Alpha::R26);
506
507 AlphaFI->setGlobalRetAddr(GlobalRetAddr);
508 return GlobalRetAddr;
509}