blob: abd7e33a6948efd5e86f671f7ea764296df8d514 [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"
16#include "AlphaGenInstrInfo.inc"
Owen Anderson718cb662007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019using namespace llvm;
20
21AlphaInstrInfo::AlphaInstrInfo()
Chris Lattner64105522008-01-01 01:03:04 +000022 : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
Evan Cheng7ce45782006-11-13 23:36:35 +000023 RI(*this) { }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000024
25
26bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
27 unsigned& sourceReg,
28 unsigned& destReg) const {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000029 MachineOpCode oc = MI.getOpcode();
Andrew Lenharth6bbf6b02006-10-31 23:46:56 +000030 if (oc == Alpha::BISr ||
Andrew Lenharthddc877c2006-03-09 18:18:51 +000031 oc == Alpha::CPYSS ||
32 oc == Alpha::CPYST ||
33 oc == Alpha::CPYSSt ||
34 oc == Alpha::CPYSTs) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +000035 // or r1, r2, r2
36 // cpys(s|t) r1 r2 r2
Evan Cheng1e3417292007-04-25 07:12:14 +000037 assert(MI.getNumOperands() >= 3 &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +000038 MI.getOperand(0).isRegister() &&
39 MI.getOperand(1).isRegister() &&
40 MI.getOperand(2).isRegister() &&
41 "invalid Alpha BIS instruction!");
42 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
43 sourceReg = MI.getOperand(1).getReg();
44 destReg = MI.getOperand(0).getReg();
45 return true;
46 }
47 }
48 return false;
49}
Chris Lattner40839602006-02-02 20:12:32 +000050
51unsigned
52AlphaInstrInfo::isLoadFromStackSlot(MachineInstr *MI, int &FrameIndex) const {
53 switch (MI->getOpcode()) {
54 case Alpha::LDL:
55 case Alpha::LDQ:
56 case Alpha::LDBU:
57 case Alpha::LDWU:
58 case Alpha::LDS:
59 case Alpha::LDT:
60 if (MI->getOperand(1).isFrameIndex()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000061 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +000062 return MI->getOperand(0).getReg();
63 }
64 break;
65 }
66 return 0;
67}
68
Andrew Lenharth133d3102006-02-03 03:07:37 +000069unsigned
70AlphaInstrInfo::isStoreToStackSlot(MachineInstr *MI, int &FrameIndex) const {
71 switch (MI->getOpcode()) {
72 case Alpha::STL:
73 case Alpha::STQ:
74 case Alpha::STB:
75 case Alpha::STW:
76 case Alpha::STS:
77 case Alpha::STT:
78 if (MI->getOperand(1).isFrameIndex()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +000079 FrameIndex = MI->getOperand(1).getIndex();
Andrew Lenharth133d3102006-02-03 03:07:37 +000080 return MI->getOperand(0).getReg();
81 }
82 break;
83 }
84 return 0;
85}
86
Andrew Lenharthf81173f2006-10-31 16:49:55 +000087static bool isAlphaIntCondCode(unsigned Opcode) {
88 switch (Opcode) {
89 case Alpha::BEQ:
90 case Alpha::BNE:
91 case Alpha::BGE:
92 case Alpha::BGT:
93 case Alpha::BLE:
94 case Alpha::BLT:
95 case Alpha::BLBC:
96 case Alpha::BLBS:
97 return true;
98 default:
99 return false;
100 }
101}
102
Evan Chengb5cdaa22007-05-18 00:05:48 +0000103unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
Chris Lattner0476b282006-10-24 16:41:36 +0000104 MachineBasicBlock *FBB,
105 const std::vector<MachineOperand> &Cond)const{
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000106 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
107 assert((Cond.size() == 2 || Cond.size() == 0) &&
108 "Alpha branch conditions have two components!");
109
110 // One-way branch.
111 if (FBB == 0) {
112 if (Cond.empty()) // Unconditional branch
Evan Chengc0f64ff2006-11-27 23:37:22 +0000113 BuildMI(&MBB, get(Alpha::BR)).addMBB(TBB);
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000114 else // Conditional branch
115 if (isAlphaIntCondCode(Cond[0].getImm()))
Evan Chengc0f64ff2006-11-27 23:37:22 +0000116 BuildMI(&MBB, get(Alpha::COND_BRANCH_I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000117 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
118 else
Evan Chengc0f64ff2006-11-27 23:37:22 +0000119 BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000120 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000121 return 1;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000122 }
123
124 // Two-way Conditional Branch.
125 if (isAlphaIntCondCode(Cond[0].getImm()))
Evan Chengc0f64ff2006-11-27 23:37:22 +0000126 BuildMI(&MBB, 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
Evan Chengc0f64ff2006-11-27 23:37:22 +0000129 BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000130 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
Evan Chengc0f64ff2006-11-27 23:37:22 +0000131 BuildMI(&MBB, get(Alpha::BR)).addMBB(FBB);
Evan Chengb5cdaa22007-05-18 00:05:48 +0000132 return 2;
Rafael Espindola3d7d39a2006-10-24 17:07:11 +0000133}
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000134
Owen Andersond10fd972007-12-31 06:32:00 +0000135void AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
136 MachineBasicBlock::iterator MI,
137 unsigned DestReg, unsigned SrcReg,
138 const TargetRegisterClass *DestRC,
139 const TargetRegisterClass *SrcRC) const {
140 //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
141 if (DestRC != SrcRC) {
142 cerr << "Not yet supported!";
143 abort();
144 }
145
146 if (DestRC == Alpha::GPRCRegisterClass) {
147 BuildMI(MBB, MI, get(Alpha::BISr), DestReg).addReg(SrcReg).addReg(SrcReg);
148 } else if (DestRC == Alpha::F4RCRegisterClass) {
149 BuildMI(MBB, MI, get(Alpha::CPYSS), DestReg).addReg(SrcReg).addReg(SrcReg);
150 } else if (DestRC == Alpha::F8RCRegisterClass) {
151 BuildMI(MBB, MI, get(Alpha::CPYST), DestReg).addReg(SrcReg).addReg(SrcReg);
152 } else {
153 cerr << "Attempt to copy register that is not GPR or FPR";
154 abort();
155 }
156}
157
Owen Andersonf6372aa2008-01-01 21:11:32 +0000158void
159AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
160 MachineBasicBlock::iterator MI,
161 unsigned SrcReg, bool isKill, int FrameIdx,
162 const TargetRegisterClass *RC) const {
163 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
164 // << FrameIdx << "\n";
165 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
166 if (RC == Alpha::F4RCRegisterClass)
167 BuildMI(MBB, MI, get(Alpha::STS))
168 .addReg(SrcReg, false, false, isKill)
169 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
170 else if (RC == Alpha::F8RCRegisterClass)
171 BuildMI(MBB, MI, get(Alpha::STT))
172 .addReg(SrcReg, false, false, isKill)
173 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
174 else if (RC == Alpha::GPRCRegisterClass)
175 BuildMI(MBB, MI, get(Alpha::STQ))
176 .addReg(SrcReg, false, false, isKill)
177 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
178 else
179 abort();
180}
181
182void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
183 bool isKill,
184 SmallVectorImpl<MachineOperand> &Addr,
185 const TargetRegisterClass *RC,
186 SmallVectorImpl<MachineInstr*> &NewMIs) const {
187 unsigned Opc = 0;
188 if (RC == Alpha::F4RCRegisterClass)
189 Opc = Alpha::STS;
190 else if (RC == Alpha::F8RCRegisterClass)
191 Opc = Alpha::STT;
192 else if (RC == Alpha::GPRCRegisterClass)
193 Opc = Alpha::STQ;
194 else
195 abort();
196 MachineInstrBuilder MIB =
197 BuildMI(get(Opc)).addReg(SrcReg, false, false, isKill);
198 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
199 MachineOperand &MO = Addr[i];
200 if (MO.isRegister())
201 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
202 else
203 MIB.addImm(MO.getImm());
204 }
205 NewMIs.push_back(MIB);
206}
207
208void
209AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
210 MachineBasicBlock::iterator MI,
211 unsigned DestReg, int FrameIdx,
212 const TargetRegisterClass *RC) const {
213 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
214 // << FrameIdx << "\n";
215 if (RC == Alpha::F4RCRegisterClass)
216 BuildMI(MBB, MI, get(Alpha::LDS), DestReg)
217 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
218 else if (RC == Alpha::F8RCRegisterClass)
219 BuildMI(MBB, MI, get(Alpha::LDT), DestReg)
220 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
221 else if (RC == Alpha::GPRCRegisterClass)
222 BuildMI(MBB, MI, get(Alpha::LDQ), DestReg)
223 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
224 else
225 abort();
226}
227
228void AlphaInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
229 SmallVectorImpl<MachineOperand> &Addr,
230 const TargetRegisterClass *RC,
231 SmallVectorImpl<MachineInstr*> &NewMIs) const {
232 unsigned Opc = 0;
233 if (RC == Alpha::F4RCRegisterClass)
234 Opc = Alpha::LDS;
235 else if (RC == Alpha::F8RCRegisterClass)
236 Opc = Alpha::LDT;
237 else if (RC == Alpha::GPRCRegisterClass)
238 Opc = Alpha::LDQ;
239 else
240 abort();
241 MachineInstrBuilder MIB =
242 BuildMI(get(Opc), DestReg);
243 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
244 MachineOperand &MO = Addr[i];
245 if (MO.isRegister())
246 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
247 else
248 MIB.addImm(MO.getImm());
249 }
250 NewMIs.push_back(MIB);
251}
252
Owen Anderson43dbe052008-01-07 01:35:02 +0000253MachineInstr *AlphaInstrInfo::foldMemoryOperand(MachineInstr *MI,
254 SmallVectorImpl<unsigned> &Ops,
255 int FrameIndex) const {
256 if (Ops.size() != 1) return NULL;
257
258 // Make sure this is a reg-reg copy.
259 unsigned Opc = MI->getOpcode();
260
261 MachineInstr *NewMI = NULL;
262 switch(Opc) {
263 default:
264 break;
265 case Alpha::BISr:
266 case Alpha::CPYSS:
267 case Alpha::CPYST:
268 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
269 if (Ops[0] == 0) { // move -> store
270 unsigned InReg = MI->getOperand(1).getReg();
271 Opc = (Opc == Alpha::BISr) ? Alpha::STQ :
272 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
273 NewMI = BuildMI(get(Opc)).addReg(InReg).addFrameIndex(FrameIndex)
274 .addReg(Alpha::F31);
275 } else { // load -> move
276 unsigned OutReg = MI->getOperand(0).getReg();
277 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ :
278 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
279 NewMI = BuildMI(get(Opc), OutReg).addFrameIndex(FrameIndex)
280 .addReg(Alpha::F31);
281 }
282 }
283 break;
284 }
285 if (NewMI)
286 NewMI->copyKillDeadInfo(MI);
287 return 0;
288}
289
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000290static unsigned AlphaRevCondCode(unsigned Opcode) {
291 switch (Opcode) {
292 case Alpha::BEQ: return Alpha::BNE;
293 case Alpha::BNE: return Alpha::BEQ;
294 case Alpha::BGE: return Alpha::BLT;
295 case Alpha::BGT: return Alpha::BLE;
296 case Alpha::BLE: return Alpha::BGT;
297 case Alpha::BLT: return Alpha::BGE;
298 case Alpha::BLBC: return Alpha::BLBS;
299 case Alpha::BLBS: return Alpha::BLBC;
300 case Alpha::FBEQ: return Alpha::FBNE;
301 case Alpha::FBNE: return Alpha::FBEQ;
302 case Alpha::FBGE: return Alpha::FBLT;
303 case Alpha::FBGT: return Alpha::FBLE;
304 case Alpha::FBLE: return Alpha::FBGT;
305 case Alpha::FBLT: return Alpha::FBGE;
306 default:
307 assert(0 && "Unknown opcode");
308 }
309}
310
311// Branch analysis.
312bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
313 MachineBasicBlock *&FBB,
314 std::vector<MachineOperand> &Cond) const {
315 // If the block has no terminators, it just falls into the block after it.
316 MachineBasicBlock::iterator I = MBB.end();
Evan Chengbfd2ec42007-06-08 21:59:56 +0000317 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000318 return false;
319
320 // Get the last instruction in the block.
321 MachineInstr *LastInst = I;
322
323 // If there is only one terminator instruction, process it.
Evan Chengbfd2ec42007-06-08 21:59:56 +0000324 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000325 if (LastInst->getOpcode() == Alpha::BR) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000326 TBB = LastInst->getOperand(0).getMBB();
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000327 return false;
328 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
329 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
330 // Block ends with fall-through condbranch.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000331 TBB = LastInst->getOperand(2).getMBB();
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000332 Cond.push_back(LastInst->getOperand(0));
333 Cond.push_back(LastInst->getOperand(1));
334 return false;
335 }
336 // Otherwise, don't know what this is.
337 return true;
338 }
339
340 // Get the instruction before it if it's a terminator.
341 MachineInstr *SecondLastInst = I;
342
343 // If there are three terminators, we don't know what sort of block this is.
344 if (SecondLastInst && I != MBB.begin() &&
Evan Chengbfd2ec42007-06-08 21:59:56 +0000345 isUnpredicatedTerminator(--I))
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000346 return true;
347
348 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
349 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
350 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) &&
351 LastInst->getOpcode() == Alpha::BR) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000352 TBB = SecondLastInst->getOperand(2).getMBB();
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000353 Cond.push_back(SecondLastInst->getOperand(0));
354 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner8aa797a2007-12-30 23:10:15 +0000355 FBB = LastInst->getOperand(0).getMBB();
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000356 return false;
357 }
358
Dale Johannesen13e8b512007-06-13 17:59:52 +0000359 // If the block ends with two Alpha::BRs, handle it. The second one is not
360 // executed, so remove it.
361 if (SecondLastInst->getOpcode() == Alpha::BR &&
362 LastInst->getOpcode() == Alpha::BR) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000363 TBB = SecondLastInst->getOperand(0).getMBB();
Dale Johannesen13e8b512007-06-13 17:59:52 +0000364 I = LastInst;
365 I->eraseFromParent();
366 return false;
367 }
368
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000369 // Otherwise, can't handle this.
370 return true;
371}
372
Evan Chengb5cdaa22007-05-18 00:05:48 +0000373unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000374 MachineBasicBlock::iterator I = MBB.end();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000375 if (I == MBB.begin()) return 0;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000376 --I;
377 if (I->getOpcode() != Alpha::BR &&
378 I->getOpcode() != Alpha::COND_BRANCH_I &&
379 I->getOpcode() != Alpha::COND_BRANCH_F)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000380 return 0;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000381
382 // Remove the branch.
383 I->eraseFromParent();
384
385 I = MBB.end();
386
Evan Chengb5cdaa22007-05-18 00:05:48 +0000387 if (I == MBB.begin()) return 1;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000388 --I;
389 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
390 I->getOpcode() != Alpha::COND_BRANCH_F)
Evan Chengb5cdaa22007-05-18 00:05:48 +0000391 return 1;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000392
393 // Remove the branch.
394 I->eraseFromParent();
Evan Chengb5cdaa22007-05-18 00:05:48 +0000395 return 2;
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000396}
397
398void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
399 MachineBasicBlock::iterator MI) const {
Evan Chengc0f64ff2006-11-27 23:37:22 +0000400 BuildMI(MBB, MI, get(Alpha::BISr), Alpha::R31).addReg(Alpha::R31)
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000401 .addReg(Alpha::R31);
402}
403
404bool AlphaInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
405 if (MBB.empty()) return false;
406
407 switch (MBB.back().getOpcode()) {
Evan Cheng126f17a2007-05-21 18:44:17 +0000408 case Alpha::RETDAG: // Return.
409 case Alpha::RETDAGp:
Andrew Lenharthf81173f2006-10-31 16:49:55 +0000410 case Alpha::BR: // Uncond branch.
411 case Alpha::JMP: // Indirect branch.
412 return true;
413 default: return false;
414 }
415}
416bool AlphaInstrInfo::
417ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
418 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
419 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
420 return false;
421}
422