blob: 0974699fa0c555c1fa1199b425880e935c668603 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- AlphaInstrInfo.cpp - Alpha Instruction Information -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
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 Anderson1636de92007-09-07 04:06:50 +000017#include "llvm/ADT/STLExtras.h"
Dan Gohmanc24a3f82009-01-05 17:59:02 +000018#include "llvm/ADT/SmallVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20using namespace llvm;
21
22AlphaInstrInfo::AlphaInstrInfo()
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000023 : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024 RI(*this) { }
25
26
27bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
Evan Chengf97496a2009-01-20 19:12:24 +000028 unsigned& sourceReg, unsigned& destReg,
29 unsigned& SrcSR, unsigned& DstSR) const {
Chris Lattner99aa3372008-01-07 02:48:55 +000030 unsigned oc = MI.getOpcode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031 if (oc == Alpha::BISr ||
32 oc == Alpha::CPYSS ||
33 oc == Alpha::CPYST ||
34 oc == Alpha::CPYSSt ||
35 oc == Alpha::CPYSTs) {
36 // or r1, r2, r2
37 // cpys(s|t) r1 r2 r2
38 assert(MI.getNumOperands() >= 3 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000039 MI.getOperand(0).isReg() &&
40 MI.getOperand(1).isReg() &&
41 MI.getOperand(2).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 "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();
Evan Chengf97496a2009-01-20 19:12:24 +000046 SrcSR = DstSR = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 return true;
48 }
49 }
50 return false;
51}
52
53unsigned
Dan Gohman90feee22008-11-18 19:49:32 +000054AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
55 int &FrameIndex) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 switch (MI->getOpcode()) {
57 case Alpha::LDL:
58 case Alpha::LDQ:
59 case Alpha::LDBU:
60 case Alpha::LDWU:
61 case Alpha::LDS:
62 case Alpha::LDT:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000063 if (MI->getOperand(1).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +000064 FrameIndex = MI->getOperand(1).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 return MI->getOperand(0).getReg();
66 }
67 break;
68 }
69 return 0;
70}
71
72unsigned
Dan Gohman90feee22008-11-18 19:49:32 +000073AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
74 int &FrameIndex) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 switch (MI->getOpcode()) {
76 case Alpha::STL:
77 case Alpha::STQ:
78 case Alpha::STB:
79 case Alpha::STW:
80 case Alpha::STS:
81 case Alpha::STT:
Dan Gohmanb9f4fa72008-10-03 15:45:36 +000082 if (MI->getOperand(1).isFI()) {
Chris Lattner6017d482007-12-30 23:10:15 +000083 FrameIndex = MI->getOperand(1).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 return MI->getOperand(0).getReg();
85 }
86 break;
87 }
88 return 0;
89}
90
91static bool isAlphaIntCondCode(unsigned Opcode) {
92 switch (Opcode) {
93 case Alpha::BEQ:
94 case Alpha::BNE:
95 case Alpha::BGE:
96 case Alpha::BGT:
97 case Alpha::BLE:
98 case Alpha::BLT:
99 case Alpha::BLBC:
100 case Alpha::BLBS:
101 return true;
102 default:
103 return false;
104 }
105}
106
Owen Andersond131b5b2008-08-14 22:49:33 +0000107unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
108 MachineBasicBlock *TBB,
109 MachineBasicBlock *FBB,
110 const SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 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!");
114
115 // One-way branch.
116 if (FBB == 0) {
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);
123 else
124 BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
125 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
126 return 1;
127 }
128
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);
133 else
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);
137 return 2;
138}
139
Owen Anderson9fa72d92008-08-26 18:03:31 +0000140bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Owen Anderson8f2c8932007-12-31 06:32:00 +0000141 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) {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000147 // Not yet supported!
148 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000149 }
150
151 if (DestRC == Alpha::GPRCRegisterClass) {
152 BuildMI(MBB, MI, get(Alpha::BISr), DestReg).addReg(SrcReg).addReg(SrcReg);
153 } else if (DestRC == Alpha::F4RCRegisterClass) {
154 BuildMI(MBB, MI, get(Alpha::CPYSS), DestReg).addReg(SrcReg).addReg(SrcReg);
155 } else if (DestRC == Alpha::F8RCRegisterClass) {
156 BuildMI(MBB, MI, get(Alpha::CPYST), DestReg).addReg(SrcReg).addReg(SrcReg);
157 } else {
Owen Anderson9fa72d92008-08-26 18:03:31 +0000158 // Attempt to copy register that is not GPR or FPR
159 return false;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000160 }
Owen Anderson9fa72d92008-08-26 18:03:31 +0000161
162 return true;
Owen Anderson8f2c8932007-12-31 06:32:00 +0000163}
164
Owen Anderson81875432008-01-01 21:11:32 +0000165void
166AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
167 MachineBasicBlock::iterator MI,
168 unsigned SrcReg, bool isKill, int FrameIdx,
169 const TargetRegisterClass *RC) const {
170 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
171 // << FrameIdx << "\n";
172 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
173 if (RC == Alpha::F4RCRegisterClass)
174 BuildMI(MBB, MI, get(Alpha::STS))
175 .addReg(SrcReg, false, false, isKill)
176 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
177 else if (RC == Alpha::F8RCRegisterClass)
178 BuildMI(MBB, MI, get(Alpha::STT))
179 .addReg(SrcReg, false, false, isKill)
180 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
181 else if (RC == Alpha::GPRCRegisterClass)
182 BuildMI(MBB, MI, get(Alpha::STQ))
183 .addReg(SrcReg, false, false, isKill)
184 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
185 else
186 abort();
187}
188
189void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
190 bool isKill,
191 SmallVectorImpl<MachineOperand> &Addr,
192 const TargetRegisterClass *RC,
193 SmallVectorImpl<MachineInstr*> &NewMIs) const {
194 unsigned Opc = 0;
195 if (RC == Alpha::F4RCRegisterClass)
196 Opc = Alpha::STS;
197 else if (RC == Alpha::F8RCRegisterClass)
198 Opc = Alpha::STT;
199 else if (RC == Alpha::GPRCRegisterClass)
200 Opc = Alpha::STQ;
201 else
202 abort();
203 MachineInstrBuilder MIB =
Dan Gohman221a4372008-07-07 23:14:23 +0000204 BuildMI(MF, get(Opc)).addReg(SrcReg, false, false, isKill);
Owen Anderson81875432008-01-01 21:11:32 +0000205 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
206 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000207 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000208 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
209 else
210 MIB.addImm(MO.getImm());
211 }
212 NewMIs.push_back(MIB);
213}
214
215void
216AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
217 MachineBasicBlock::iterator MI,
218 unsigned DestReg, int FrameIdx,
219 const TargetRegisterClass *RC) const {
220 //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
221 // << FrameIdx << "\n";
222 if (RC == Alpha::F4RCRegisterClass)
223 BuildMI(MBB, MI, get(Alpha::LDS), DestReg)
224 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
225 else if (RC == Alpha::F8RCRegisterClass)
226 BuildMI(MBB, MI, get(Alpha::LDT), DestReg)
227 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
228 else if (RC == Alpha::GPRCRegisterClass)
229 BuildMI(MBB, MI, get(Alpha::LDQ), DestReg)
230 .addFrameIndex(FrameIdx).addReg(Alpha::F31);
231 else
232 abort();
233}
234
235void AlphaInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
236 SmallVectorImpl<MachineOperand> &Addr,
237 const TargetRegisterClass *RC,
238 SmallVectorImpl<MachineInstr*> &NewMIs) const {
239 unsigned Opc = 0;
240 if (RC == Alpha::F4RCRegisterClass)
241 Opc = Alpha::LDS;
242 else if (RC == Alpha::F8RCRegisterClass)
243 Opc = Alpha::LDT;
244 else if (RC == Alpha::GPRCRegisterClass)
245 Opc = Alpha::LDQ;
246 else
247 abort();
248 MachineInstrBuilder MIB =
Dan Gohman221a4372008-07-07 23:14:23 +0000249 BuildMI(MF, get(Opc), DestReg);
Owen Anderson81875432008-01-01 21:11:32 +0000250 for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
251 MachineOperand &MO = Addr[i];
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000252 if (MO.isReg())
Owen Anderson81875432008-01-01 21:11:32 +0000253 MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
254 else
255 MIB.addImm(MO.getImm());
256 }
257 NewMIs.push_back(MIB);
258}
259
Dan Gohmanedc83d62008-12-03 18:43:12 +0000260MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
261 MachineInstr *MI,
Dan Gohman46b948e2008-10-16 01:49:15 +0000262 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanedc83d62008-12-03 18:43:12 +0000263 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +0000264 if (Ops.size() != 1) return NULL;
265
266 // Make sure this is a reg-reg copy.
267 unsigned Opc = MI->getOpcode();
268
269 MachineInstr *NewMI = NULL;
270 switch(Opc) {
271 default:
272 break;
273 case Alpha::BISr:
274 case Alpha::CPYSS:
275 case Alpha::CPYST:
276 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
277 if (Ops[0] == 0) { // move -> store
278 unsigned InReg = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000279 bool isKill = MI->getOperand(1).isKill();
Owen Anderson9a184ef2008-01-07 01:35:02 +0000280 Opc = (Opc == Alpha::BISr) ? Alpha::STQ :
281 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
Dan Gohman221a4372008-07-07 23:14:23 +0000282 NewMI = BuildMI(MF, get(Opc)).addReg(InReg, false, false, isKill)
Evan Chenge52c1912008-07-03 09:09:37 +0000283 .addFrameIndex(FrameIndex)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000284 .addReg(Alpha::F31);
285 } else { // load -> move
286 unsigned OutReg = MI->getOperand(0).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +0000287 bool isDead = MI->getOperand(0).isDead();
Owen Anderson9a184ef2008-01-07 01:35:02 +0000288 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ :
289 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
Dan Gohman221a4372008-07-07 23:14:23 +0000290 NewMI = BuildMI(MF, get(Opc)).addReg(OutReg, true, false, false, isDead)
Evan Chenge52c1912008-07-03 09:09:37 +0000291 .addFrameIndex(FrameIndex)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000292 .addReg(Alpha::F31);
293 }
294 }
295 break;
296 }
Evan Chenge52c1912008-07-03 09:09:37 +0000297 return NewMI;
Owen Anderson9a184ef2008-01-07 01:35:02 +0000298}
299
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300static unsigned AlphaRevCondCode(unsigned Opcode) {
301 switch (Opcode) {
302 case Alpha::BEQ: return Alpha::BNE;
303 case Alpha::BNE: return Alpha::BEQ;
304 case Alpha::BGE: return Alpha::BLT;
305 case Alpha::BGT: return Alpha::BLE;
306 case Alpha::BLE: return Alpha::BGT;
307 case Alpha::BLT: return Alpha::BGE;
308 case Alpha::BLBC: return Alpha::BLBS;
309 case Alpha::BLBS: return Alpha::BLBC;
310 case Alpha::FBEQ: return Alpha::FBNE;
311 case Alpha::FBNE: return Alpha::FBEQ;
312 case Alpha::FBGE: return Alpha::FBLT;
313 case Alpha::FBGT: return Alpha::FBLE;
314 case Alpha::FBLE: return Alpha::FBGT;
315 case Alpha::FBLT: return Alpha::FBGE;
316 default:
317 assert(0 && "Unknown opcode");
318 }
Chris Lattner2b06cd32008-03-30 18:22:13 +0000319 return 0; // Not reached
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320}
321
322// Branch analysis.
323bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
Evan Chengeac31642009-02-09 07:14:22 +0000324 MachineBasicBlock *&FBB,
325 SmallVectorImpl<MachineOperand> &Cond,
326 bool AllowModify) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 // If the block has no terminators, it just falls into the block after it.
328 MachineBasicBlock::iterator I = MBB.end();
329 if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
330 return false;
331
332 // Get the last instruction in the block.
333 MachineInstr *LastInst = I;
334
335 // If there is only one terminator instruction, process it.
336 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
337 if (LastInst->getOpcode() == Alpha::BR) {
Chris Lattner6017d482007-12-30 23:10:15 +0000338 TBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 return false;
340 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
341 LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
342 // Block ends with fall-through condbranch.
Chris Lattner6017d482007-12-30 23:10:15 +0000343 TBB = LastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 Cond.push_back(LastInst->getOperand(0));
345 Cond.push_back(LastInst->getOperand(1));
346 return false;
347 }
348 // Otherwise, don't know what this is.
349 return true;
350 }
351
352 // Get the instruction before it if it's a terminator.
353 MachineInstr *SecondLastInst = I;
354
355 // If there are three terminators, we don't know what sort of block this is.
356 if (SecondLastInst && I != MBB.begin() &&
357 isUnpredicatedTerminator(--I))
358 return true;
359
360 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
361 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
362 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) &&
363 LastInst->getOpcode() == Alpha::BR) {
Chris Lattner6017d482007-12-30 23:10:15 +0000364 TBB = SecondLastInst->getOperand(2).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 Cond.push_back(SecondLastInst->getOperand(0));
366 Cond.push_back(SecondLastInst->getOperand(1));
Chris Lattner6017d482007-12-30 23:10:15 +0000367 FBB = LastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 return false;
369 }
370
371 // If the block ends with two Alpha::BRs, handle it. The second one is not
372 // executed, so remove it.
373 if (SecondLastInst->getOpcode() == Alpha::BR &&
374 LastInst->getOpcode() == Alpha::BR) {
Chris Lattner6017d482007-12-30 23:10:15 +0000375 TBB = SecondLastInst->getOperand(0).getMBB();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 I = LastInst;
Evan Chengeac31642009-02-09 07:14:22 +0000377 if (AllowModify)
378 I->eraseFromParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 return false;
380 }
381
382 // Otherwise, can't handle this.
383 return true;
384}
385
386unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
387 MachineBasicBlock::iterator I = MBB.end();
388 if (I == MBB.begin()) return 0;
389 --I;
390 if (I->getOpcode() != Alpha::BR &&
391 I->getOpcode() != Alpha::COND_BRANCH_I &&
392 I->getOpcode() != Alpha::COND_BRANCH_F)
393 return 0;
394
395 // Remove the branch.
396 I->eraseFromParent();
397
398 I = MBB.end();
399
400 if (I == MBB.begin()) return 1;
401 --I;
402 if (I->getOpcode() != Alpha::COND_BRANCH_I &&
403 I->getOpcode() != Alpha::COND_BRANCH_F)
404 return 1;
405
406 // Remove the branch.
407 I->eraseFromParent();
408 return 2;
409}
410
411void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB,
412 MachineBasicBlock::iterator MI) const {
413 BuildMI(MBB, MI, get(Alpha::BISr), Alpha::R31).addReg(Alpha::R31)
414 .addReg(Alpha::R31);
415}
416
Dan Gohman46b948e2008-10-16 01:49:15 +0000417bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 if (MBB.empty()) return false;
419
420 switch (MBB.back().getOpcode()) {
421 case Alpha::RETDAG: // Return.
422 case Alpha::RETDAGp:
423 case Alpha::BR: // Uncond branch.
424 case Alpha::JMP: // Indirect branch.
425 return true;
426 default: return false;
427 }
428}
429bool AlphaInstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +0000430ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
432 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
433 return false;
434}
435