It's not always safe to fold movsd into xorpd, etc. Check the alignment of the load...
[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/CodeGen/MachineInstrBuilder.h"
19 using namespace llvm;
20
21 AlphaInstrInfo::AlphaInstrInfo()
22   : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
23     RI(*this) { }
24
25
26 bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
27                                  unsigned& sourceReg,
28                                  unsigned& destReg) const {
29   unsigned oc = MI.getOpcode();
30   if (oc == Alpha::BISr   || 
31       oc == Alpha::CPYSS  || 
32       oc == Alpha::CPYST  ||
33       oc == Alpha::CPYSSt || 
34       oc == Alpha::CPYSTs) {
35     // or r1, r2, r2 
36     // cpys(s|t) r1 r2 r2
37     assert(MI.getNumOperands() >= 3 &&
38            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 }
50
51 unsigned 
52 AlphaInstrInfo::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()) {
61       FrameIndex = MI->getOperand(1).getIndex();
62       return MI->getOperand(0).getReg();
63     }
64     break;
65   }
66   return 0;
67 }
68
69 unsigned 
70 AlphaInstrInfo::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()) {
79       FrameIndex = MI->getOperand(1).getIndex();
80       return MI->getOperand(0).getReg();
81     }
82     break;
83   }
84   return 0;
85 }
86
87 static 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
103 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
104                                   MachineBasicBlock *FBB,
105                                   const std::vector<MachineOperand> &Cond)const{
106   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
113       BuildMI(&MBB, get(Alpha::BR)).addMBB(TBB);
114     else                // Conditional branch
115       if (isAlphaIntCondCode(Cond[0].getImm()))
116         BuildMI(&MBB, get(Alpha::COND_BRANCH_I))
117           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
118       else
119         BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
120           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
121     return 1;
122   }
123   
124   // Two-way Conditional Branch.
125   if (isAlphaIntCondCode(Cond[0].getImm()))
126     BuildMI(&MBB, get(Alpha::COND_BRANCH_I))
127       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
128   else
129     BuildMI(&MBB, get(Alpha::COND_BRANCH_F))
130       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
131   BuildMI(&MBB, get(Alpha::BR)).addMBB(FBB);
132   return 2;
133 }
134
135 void 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
158 void
159 AlphaInstrInfo::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
182 void 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
208 void
209 AlphaInstrInfo::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
228 void 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
253 MachineInstr *AlphaInstrInfo::foldMemoryOperand(MachineFunction &MF,
254                                                 MachineInstr *MI,
255                                                 SmallVectorImpl<unsigned> &Ops,
256                                                 int FrameIndex) const {
257    if (Ops.size() != 1) return NULL;
258
259    // Make sure this is a reg-reg copy.
260    unsigned Opc = MI->getOpcode();
261
262    MachineInstr *NewMI = NULL;
263    switch(Opc) {
264    default:
265      break;
266    case Alpha::BISr:
267    case Alpha::CPYSS:
268    case Alpha::CPYST:
269      if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
270        if (Ops[0] == 0) {  // move -> store
271          unsigned InReg = MI->getOperand(1).getReg();
272          Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 
273            ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
274          NewMI = BuildMI(get(Opc)).addReg(InReg).addFrameIndex(FrameIndex)
275            .addReg(Alpha::F31);
276        } else {           // load -> move
277          unsigned OutReg = MI->getOperand(0).getReg();
278          Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 
279            ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
280          NewMI = BuildMI(get(Opc), OutReg).addFrameIndex(FrameIndex)
281            .addReg(Alpha::F31);
282        }
283      }
284      break;
285    }
286   if (NewMI)
287     NewMI->copyKillDeadInfo(MI);
288   return 0;
289 }
290
291 static unsigned AlphaRevCondCode(unsigned Opcode) {
292   switch (Opcode) {
293   case Alpha::BEQ: return Alpha::BNE;
294   case Alpha::BNE: return Alpha::BEQ;
295   case Alpha::BGE: return Alpha::BLT;
296   case Alpha::BGT: return Alpha::BLE;
297   case Alpha::BLE: return Alpha::BGT;
298   case Alpha::BLT: return Alpha::BGE;
299   case Alpha::BLBC: return Alpha::BLBS;
300   case Alpha::BLBS: return Alpha::BLBC;
301   case Alpha::FBEQ: return Alpha::FBNE;
302   case Alpha::FBNE: return Alpha::FBEQ;
303   case Alpha::FBGE: return Alpha::FBLT;
304   case Alpha::FBGT: return Alpha::FBLE;
305   case Alpha::FBLE: return Alpha::FBGT;
306   case Alpha::FBLT: return Alpha::FBGE;
307   default:
308     assert(0 && "Unknown opcode");
309   }
310 }
311
312 // Branch analysis.
313 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
314                                  MachineBasicBlock *&FBB,
315                                  std::vector<MachineOperand> &Cond) const {
316   // If the block has no terminators, it just falls into the block after it.
317   MachineBasicBlock::iterator I = MBB.end();
318   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
319     return false;
320
321   // Get the last instruction in the block.
322   MachineInstr *LastInst = I;
323   
324   // If there is only one terminator instruction, process it.
325   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
326     if (LastInst->getOpcode() == Alpha::BR) {
327       TBB = LastInst->getOperand(0).getMBB();
328       return false;
329     } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
330                LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
331       // Block ends with fall-through condbranch.
332       TBB = LastInst->getOperand(2).getMBB();
333       Cond.push_back(LastInst->getOperand(0));
334       Cond.push_back(LastInst->getOperand(1));
335       return false;
336     }
337     // Otherwise, don't know what this is.
338     return true;
339   }
340   
341   // Get the instruction before it if it's a terminator.
342   MachineInstr *SecondLastInst = I;
343
344   // If there are three terminators, we don't know what sort of block this is.
345   if (SecondLastInst && I != MBB.begin() &&
346       isUnpredicatedTerminator(--I))
347     return true;
348   
349   // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
350   if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
351       SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && 
352       LastInst->getOpcode() == Alpha::BR) {
353     TBB =  SecondLastInst->getOperand(2).getMBB();
354     Cond.push_back(SecondLastInst->getOperand(0));
355     Cond.push_back(SecondLastInst->getOperand(1));
356     FBB = LastInst->getOperand(0).getMBB();
357     return false;
358   }
359   
360   // If the block ends with two Alpha::BRs, handle it.  The second one is not
361   // executed, so remove it.
362   if (SecondLastInst->getOpcode() == Alpha::BR && 
363       LastInst->getOpcode() == Alpha::BR) {
364     TBB = SecondLastInst->getOperand(0).getMBB();
365     I = LastInst;
366     I->eraseFromParent();
367     return false;
368   }
369
370   // Otherwise, can't handle this.
371   return true;
372 }
373
374 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
375   MachineBasicBlock::iterator I = MBB.end();
376   if (I == MBB.begin()) return 0;
377   --I;
378   if (I->getOpcode() != Alpha::BR && 
379       I->getOpcode() != Alpha::COND_BRANCH_I &&
380       I->getOpcode() != Alpha::COND_BRANCH_F)
381     return 0;
382   
383   // Remove the branch.
384   I->eraseFromParent();
385   
386   I = MBB.end();
387
388   if (I == MBB.begin()) return 1;
389   --I;
390   if (I->getOpcode() != Alpha::COND_BRANCH_I && 
391       I->getOpcode() != Alpha::COND_BRANCH_F)
392     return 1;
393   
394   // Remove the branch.
395   I->eraseFromParent();
396   return 2;
397 }
398
399 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, 
400                                 MachineBasicBlock::iterator MI) const {
401   BuildMI(MBB, MI, get(Alpha::BISr), Alpha::R31).addReg(Alpha::R31)
402     .addReg(Alpha::R31);
403 }
404
405 bool AlphaInstrInfo::BlockHasNoFallThrough(MachineBasicBlock &MBB) const {
406   if (MBB.empty()) return false;
407   
408   switch (MBB.back().getOpcode()) {
409   case Alpha::RETDAG: // Return.
410   case Alpha::RETDAGp:
411   case Alpha::BR:     // Uncond branch.
412   case Alpha::JMP:  // Indirect branch.
413     return true;
414   default: return false;
415   }
416 }
417 bool AlphaInstrInfo::
418 ReverseBranchCondition(std::vector<MachineOperand> &Cond) const {
419   assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
420   Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
421   return false;
422 }
423