Tidy up #includes, deleting a bunch of unnecessary #includes.
[oota-llvm.git] / lib / Target / Alpha / AlphaInstrInfo.cpp
1 //===- AlphaInstrInfo.cpp - Alpha Instruction Information -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 using namespace llvm;
21
22 AlphaInstrInfo::AlphaInstrInfo()
23   : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
24     RI(*this) { }
25
26
27 bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
28                                  unsigned& sourceReg,
29                                  unsigned& destReg) const {
30   unsigned oc = MI.getOpcode();
31   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 &&
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();
46       return true;
47     }
48   }
49   return false;
50 }
51
52 unsigned 
53 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
54                                     int &FrameIndex) const {
55   switch (MI->getOpcode()) {
56   case Alpha::LDL:
57   case Alpha::LDQ:
58   case Alpha::LDBU:
59   case Alpha::LDWU:
60   case Alpha::LDS:
61   case Alpha::LDT:
62     if (MI->getOperand(1).isFI()) {
63       FrameIndex = MI->getOperand(1).getIndex();
64       return MI->getOperand(0).getReg();
65     }
66     break;
67   }
68   return 0;
69 }
70
71 unsigned 
72 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
73                                    int &FrameIndex) const {
74   switch (MI->getOpcode()) {
75   case Alpha::STL:
76   case Alpha::STQ:
77   case Alpha::STB:
78   case Alpha::STW:
79   case Alpha::STS:
80   case Alpha::STT:
81     if (MI->getOperand(1).isFI()) {
82       FrameIndex = MI->getOperand(1).getIndex();
83       return MI->getOperand(0).getReg();
84     }
85     break;
86   }
87   return 0;
88 }
89
90 static bool isAlphaIntCondCode(unsigned Opcode) {
91   switch (Opcode) {
92   case Alpha::BEQ: 
93   case Alpha::BNE: 
94   case Alpha::BGE: 
95   case Alpha::BGT: 
96   case Alpha::BLE: 
97   case Alpha::BLT: 
98   case Alpha::BLBC: 
99   case Alpha::BLBS:
100     return true;
101   default:
102     return false;
103   }
104 }
105
106 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
107                             MachineBasicBlock *TBB,
108                             MachineBasicBlock *FBB,
109                             const SmallVectorImpl<MachineOperand> &Cond) const {
110   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
111   assert((Cond.size() == 2 || Cond.size() == 0) && 
112          "Alpha branch conditions have two components!");
113
114   // One-way branch.
115   if (FBB == 0) {
116     if (Cond.empty())   // Unconditional branch
117       BuildMI(&MBB, get(Alpha::BR)).addMBB(TBB);
118     else                // Conditional branch
119       if (isAlphaIntCondCode(Cond[0].getImm()))
120         BuildMI(&MBB, get(Alpha::COND_BRANCH_I))
121           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
122       else
123         BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
124           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
125     return 1;
126   }
127   
128   // Two-way Conditional Branch.
129   if (isAlphaIntCondCode(Cond[0].getImm()))
130     BuildMI(&MBB, get(Alpha::COND_BRANCH_I))
131       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
132   else
133     BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
134       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
135   BuildMI(&MBB, get(Alpha::BR)).addMBB(FBB);
136   return 2;
137 }
138
139 bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
140                                      MachineBasicBlock::iterator MI,
141                                      unsigned DestReg, unsigned SrcReg,
142                                      const TargetRegisterClass *DestRC,
143                                      const TargetRegisterClass *SrcRC) const {
144   //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
145   if (DestRC != SrcRC) {
146     // Not yet supported!
147     return false;
148   }
149
150   if (DestRC == Alpha::GPRCRegisterClass) {
151     BuildMI(MBB, MI, get(Alpha::BISr), DestReg).addReg(SrcReg).addReg(SrcReg);
152   } else if (DestRC == Alpha::F4RCRegisterClass) {
153     BuildMI(MBB, MI, get(Alpha::CPYSS), DestReg).addReg(SrcReg).addReg(SrcReg);
154   } else if (DestRC == Alpha::F8RCRegisterClass) {
155     BuildMI(MBB, MI, get(Alpha::CPYST), DestReg).addReg(SrcReg).addReg(SrcReg);
156   } else {
157     // Attempt to copy register that is not GPR or FPR
158     return false;
159   }
160   
161   return true;
162 }
163
164 void
165 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
166                                        MachineBasicBlock::iterator MI,
167                                      unsigned SrcReg, bool isKill, int FrameIdx,
168                                      const TargetRegisterClass *RC) const {
169   //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
170   //     << FrameIdx << "\n";
171   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
172   if (RC == Alpha::F4RCRegisterClass)
173     BuildMI(MBB, MI, get(Alpha::STS))
174       .addReg(SrcReg, false, false, isKill)
175       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
176   else if (RC == Alpha::F8RCRegisterClass)
177     BuildMI(MBB, MI, get(Alpha::STT))
178       .addReg(SrcReg, false, false, isKill)
179       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
180   else if (RC == Alpha::GPRCRegisterClass)
181     BuildMI(MBB, MI, get(Alpha::STQ))
182       .addReg(SrcReg, false, false, isKill)
183       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
184   else
185     abort();
186 }
187
188 void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
189                                        bool isKill,
190                                        SmallVectorImpl<MachineOperand> &Addr,
191                                        const TargetRegisterClass *RC,
192                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
193   unsigned Opc = 0;
194   if (RC == Alpha::F4RCRegisterClass)
195     Opc = Alpha::STS;
196   else if (RC == Alpha::F8RCRegisterClass)
197     Opc = Alpha::STT;
198   else if (RC == Alpha::GPRCRegisterClass)
199     Opc = Alpha::STQ;
200   else
201     abort();
202   MachineInstrBuilder MIB = 
203     BuildMI(MF, get(Opc)).addReg(SrcReg, false, false, isKill);
204   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
205     MachineOperand &MO = Addr[i];
206     if (MO.isReg())
207       MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
208     else
209       MIB.addImm(MO.getImm());
210   }
211   NewMIs.push_back(MIB);
212 }
213
214 void
215 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
216                                         MachineBasicBlock::iterator MI,
217                                         unsigned DestReg, int FrameIdx,
218                                         const TargetRegisterClass *RC) const {
219   //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
220   //     << FrameIdx << "\n";
221   if (RC == Alpha::F4RCRegisterClass)
222     BuildMI(MBB, MI, get(Alpha::LDS), DestReg)
223       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
224   else if (RC == Alpha::F8RCRegisterClass)
225     BuildMI(MBB, MI, get(Alpha::LDT), DestReg)
226       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
227   else if (RC == Alpha::GPRCRegisterClass)
228     BuildMI(MBB, MI, get(Alpha::LDQ), DestReg)
229       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
230   else
231     abort();
232 }
233
234 void AlphaInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
235                                         SmallVectorImpl<MachineOperand> &Addr,
236                                         const TargetRegisterClass *RC,
237                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
238   unsigned Opc = 0;
239   if (RC == Alpha::F4RCRegisterClass)
240     Opc = Alpha::LDS;
241   else if (RC == Alpha::F8RCRegisterClass)
242     Opc = Alpha::LDT;
243   else if (RC == Alpha::GPRCRegisterClass)
244     Opc = Alpha::LDQ;
245   else
246     abort();
247   MachineInstrBuilder MIB = 
248     BuildMI(MF, get(Opc), DestReg);
249   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
250     MachineOperand &MO = Addr[i];
251     if (MO.isReg())
252       MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
253     else
254       MIB.addImm(MO.getImm());
255   }
256   NewMIs.push_back(MIB);
257 }
258
259 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
260                                                     MachineInstr *MI,
261                                           const SmallVectorImpl<unsigned> &Ops,
262                                                     int FrameIndex) const {
263    if (Ops.size() != 1) return NULL;
264
265    // Make sure this is a reg-reg copy.
266    unsigned Opc = MI->getOpcode();
267
268    MachineInstr *NewMI = NULL;
269    switch(Opc) {
270    default:
271      break;
272    case Alpha::BISr:
273    case Alpha::CPYSS:
274    case Alpha::CPYST:
275      if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
276        if (Ops[0] == 0) {  // move -> store
277          unsigned InReg = MI->getOperand(1).getReg();
278          bool isKill = MI->getOperand(1).isKill();
279          Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 
280            ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
281          NewMI = BuildMI(MF, get(Opc)).addReg(InReg, false, false, isKill)
282            .addFrameIndex(FrameIndex)
283            .addReg(Alpha::F31);
284        } else {           // load -> move
285          unsigned OutReg = MI->getOperand(0).getReg();
286          bool isDead = MI->getOperand(0).isDead();
287          Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 
288            ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
289          NewMI = BuildMI(MF, get(Opc)).addReg(OutReg, true, false, false, isDead)
290            .addFrameIndex(FrameIndex)
291            .addReg(Alpha::F31);
292        }
293      }
294      break;
295    }
296   return NewMI;
297 }
298
299 static unsigned AlphaRevCondCode(unsigned Opcode) {
300   switch (Opcode) {
301   case Alpha::BEQ: return Alpha::BNE;
302   case Alpha::BNE: return Alpha::BEQ;
303   case Alpha::BGE: return Alpha::BLT;
304   case Alpha::BGT: return Alpha::BLE;
305   case Alpha::BLE: return Alpha::BGT;
306   case Alpha::BLT: return Alpha::BGE;
307   case Alpha::BLBC: return Alpha::BLBS;
308   case Alpha::BLBS: return Alpha::BLBC;
309   case Alpha::FBEQ: return Alpha::FBNE;
310   case Alpha::FBNE: return Alpha::FBEQ;
311   case Alpha::FBGE: return Alpha::FBLT;
312   case Alpha::FBGT: return Alpha::FBLE;
313   case Alpha::FBLE: return Alpha::FBGT;
314   case Alpha::FBLT: return Alpha::FBGE;
315   default:
316     assert(0 && "Unknown opcode");
317   }
318   return 0; // Not reached
319 }
320
321 // Branch analysis.
322 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
323                                  MachineBasicBlock *&FBB,
324                                  SmallVectorImpl<MachineOperand> &Cond) const {
325   // If the block has no terminators, it just falls into the block after it.
326   MachineBasicBlock::iterator I = MBB.end();
327   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
328     return false;
329
330   // Get the last instruction in the block.
331   MachineInstr *LastInst = I;
332   
333   // If there is only one terminator instruction, process it.
334   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
335     if (LastInst->getOpcode() == Alpha::BR) {
336       TBB = LastInst->getOperand(0).getMBB();
337       return false;
338     } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
339                LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
340       // Block ends with fall-through condbranch.
341       TBB = LastInst->getOperand(2).getMBB();
342       Cond.push_back(LastInst->getOperand(0));
343       Cond.push_back(LastInst->getOperand(1));
344       return false;
345     }
346     // Otherwise, don't know what this is.
347     return true;
348   }
349   
350   // Get the instruction before it if it's a terminator.
351   MachineInstr *SecondLastInst = I;
352
353   // If there are three terminators, we don't know what sort of block this is.
354   if (SecondLastInst && I != MBB.begin() &&
355       isUnpredicatedTerminator(--I))
356     return true;
357   
358   // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
359   if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
360       SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && 
361       LastInst->getOpcode() == Alpha::BR) {
362     TBB =  SecondLastInst->getOperand(2).getMBB();
363     Cond.push_back(SecondLastInst->getOperand(0));
364     Cond.push_back(SecondLastInst->getOperand(1));
365     FBB = LastInst->getOperand(0).getMBB();
366     return false;
367   }
368   
369   // If the block ends with two Alpha::BRs, handle it.  The second one is not
370   // executed, so remove it.
371   if (SecondLastInst->getOpcode() == Alpha::BR && 
372       LastInst->getOpcode() == Alpha::BR) {
373     TBB = SecondLastInst->getOperand(0).getMBB();
374     I = LastInst;
375     I->eraseFromParent();
376     return false;
377   }
378
379   // Otherwise, can't handle this.
380   return true;
381 }
382
383 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
384   MachineBasicBlock::iterator I = MBB.end();
385   if (I == MBB.begin()) return 0;
386   --I;
387   if (I->getOpcode() != Alpha::BR && 
388       I->getOpcode() != Alpha::COND_BRANCH_I &&
389       I->getOpcode() != Alpha::COND_BRANCH_F)
390     return 0;
391   
392   // Remove the branch.
393   I->eraseFromParent();
394   
395   I = MBB.end();
396
397   if (I == MBB.begin()) return 1;
398   --I;
399   if (I->getOpcode() != Alpha::COND_BRANCH_I && 
400       I->getOpcode() != Alpha::COND_BRANCH_F)
401     return 1;
402   
403   // Remove the branch.
404   I->eraseFromParent();
405   return 2;
406 }
407
408 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, 
409                                 MachineBasicBlock::iterator MI) const {
410   BuildMI(MBB, MI, get(Alpha::BISr), Alpha::R31).addReg(Alpha::R31)
411     .addReg(Alpha::R31);
412 }
413
414 bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
415   if (MBB.empty()) return false;
416   
417   switch (MBB.back().getOpcode()) {
418   case Alpha::RETDAG: // Return.
419   case Alpha::RETDAGp:
420   case Alpha::BR:     // Uncond branch.
421   case Alpha::JMP:  // Indirect branch.
422     return true;
423   default: return false;
424   }
425 }
426 bool AlphaInstrInfo::
427 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
428   assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
429   Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
430   return false;
431 }
432