42dc44802128cf49af1d2effdf60f309cdd1cbbd
[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, unsigned& destReg,
29                                  unsigned& SrcSR, unsigned& DstSR) 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       SrcSR = DstSR = 0;
47       return true;
48     }
49   }
50   return false;
51 }
52
53 unsigned 
54 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
55                                     int &FrameIndex) const {
56   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:
63     if (MI->getOperand(1).isFI()) {
64       FrameIndex = MI->getOperand(1).getIndex();
65       return MI->getOperand(0).getReg();
66     }
67     break;
68   }
69   return 0;
70 }
71
72 unsigned 
73 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
74                                    int &FrameIndex) const {
75   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:
82     if (MI->getOperand(1).isFI()) {
83       FrameIndex = MI->getOperand(1).getIndex();
84       return MI->getOperand(0).getReg();
85     }
86     break;
87   }
88   return 0;
89 }
90
91 static 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
107 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
108                                       MachineBasicBlock *TBB,
109                                       MachineBasicBlock *FBB,
110                             const SmallVectorImpl<MachineOperand> &Cond) const {
111   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
140 bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
141                                   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) {
147     // Not yet supported!
148     return false;
149   }
150
151   DebugLoc DL = DebugLoc::getUnknownLoc();
152   if (MI != MBB.end()) DL = MI->getDebugLoc();
153
154   if (DestRC == Alpha::GPRCRegisterClass) {
155     BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
156       .addReg(SrcReg)
157       .addReg(SrcReg);
158   } else if (DestRC == Alpha::F4RCRegisterClass) {
159     BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
160       .addReg(SrcReg)
161       .addReg(SrcReg);
162   } else if (DestRC == Alpha::F8RCRegisterClass) {
163     BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
164       .addReg(SrcReg)
165       .addReg(SrcReg);
166   } else {
167     // Attempt to copy register that is not GPR or FPR
168     return false;
169   }
170   
171   return true;
172 }
173
174 void
175 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
176                                     MachineBasicBlock::iterator MI,
177                                     unsigned SrcReg, bool isKill, int FrameIdx,
178                                     const TargetRegisterClass *RC) const {
179   //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
180   //     << FrameIdx << "\n";
181   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
182
183   DebugLoc DL = DebugLoc::getUnknownLoc();
184   if (MI != MBB.end()) DL = MI->getDebugLoc();
185
186   if (RC == Alpha::F4RCRegisterClass)
187     BuildMI(MBB, MI, DL, get(Alpha::STS))
188       .addReg(SrcReg, false, false, isKill)
189       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
190   else if (RC == Alpha::F8RCRegisterClass)
191     BuildMI(MBB, MI, DL, get(Alpha::STT))
192       .addReg(SrcReg, false, false, isKill)
193       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
194   else if (RC == Alpha::GPRCRegisterClass)
195     BuildMI(MBB, MI, DL, get(Alpha::STQ))
196       .addReg(SrcReg, false, false, isKill)
197       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
198   else
199     abort();
200 }
201
202 void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
203                                        bool isKill,
204                                        SmallVectorImpl<MachineOperand> &Addr,
205                                        const TargetRegisterClass *RC,
206                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
207   unsigned Opc = 0;
208   if (RC == Alpha::F4RCRegisterClass)
209     Opc = Alpha::STS;
210   else if (RC == Alpha::F8RCRegisterClass)
211     Opc = Alpha::STT;
212   else if (RC == Alpha::GPRCRegisterClass)
213     Opc = Alpha::STQ;
214   else
215     abort();
216   DebugLoc DL = DebugLoc::getUnknownLoc();
217   MachineInstrBuilder MIB = 
218     BuildMI(MF, DL, get(Opc)).addReg(SrcReg, false, false, isKill);
219   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
220     MachineOperand &MO = Addr[i];
221     if (MO.isReg())
222       MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
223     else
224       MIB.addImm(MO.getImm());
225   }
226   NewMIs.push_back(MIB);
227 }
228
229 void
230 AlphaInstrInfo::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";
236   DebugLoc DL = DebugLoc::getUnknownLoc();
237   if (MI != MBB.end()) DL = MI->getDebugLoc();
238
239   if (RC == Alpha::F4RCRegisterClass)
240     BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
241       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
242   else if (RC == Alpha::F8RCRegisterClass)
243     BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
244       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
245   else if (RC == Alpha::GPRCRegisterClass)
246     BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
247       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
248   else
249     abort();
250 }
251
252 void 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
264     abort();
265   DebugLoc DL = DebugLoc::getUnknownLoc();
266   MachineInstrBuilder MIB = 
267     BuildMI(MF, DL, get(Opc), DestReg);
268   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
269     MachineOperand &MO = Addr[i];
270     if (MO.isReg())
271       MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
272     else
273       MIB.addImm(MO.getImm());
274   }
275   NewMIs.push_back(MIB);
276 }
277
278 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
279                                                     MachineInstr *MI,
280                                           const SmallVectorImpl<unsigned> &Ops,
281                                                     int FrameIndex) const {
282    if (Ops.size() != 1) return NULL;
283
284    // Make sure this is a reg-reg copy.
285    unsigned Opc = MI->getOpcode();
286
287    MachineInstr *NewMI = NULL;
288    switch(Opc) {
289    default:
290      break;
291    case Alpha::BISr:
292    case Alpha::CPYSS:
293    case Alpha::CPYST:
294      if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
295        if (Ops[0] == 0) {  // move -> store
296          unsigned InReg = MI->getOperand(1).getReg();
297          bool isKill = MI->getOperand(1).isKill();
298          Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 
299            ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
300          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
301            .addReg(InReg, false, false, isKill)
302            .addFrameIndex(FrameIndex)
303            .addReg(Alpha::F31);
304        } else {           // load -> move
305          unsigned OutReg = MI->getOperand(0).getReg();
306          bool isDead = MI->getOperand(0).isDead();
307          Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 
308            ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
309          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
310            .addReg(OutReg, true, false, false, isDead)
311            .addFrameIndex(FrameIndex)
312            .addReg(Alpha::F31);
313        }
314      }
315      break;
316    }
317   return NewMI;
318 }
319
320 static unsigned AlphaRevCondCode(unsigned Opcode) {
321   switch (Opcode) {
322   case Alpha::BEQ: return Alpha::BNE;
323   case Alpha::BNE: return Alpha::BEQ;
324   case Alpha::BGE: return Alpha::BLT;
325   case Alpha::BGT: return Alpha::BLE;
326   case Alpha::BLE: return Alpha::BGT;
327   case Alpha::BLT: return Alpha::BGE;
328   case Alpha::BLBC: return Alpha::BLBS;
329   case Alpha::BLBS: return Alpha::BLBC;
330   case Alpha::FBEQ: return Alpha::FBNE;
331   case Alpha::FBNE: return Alpha::FBEQ;
332   case Alpha::FBGE: return Alpha::FBLT;
333   case Alpha::FBGT: return Alpha::FBLE;
334   case Alpha::FBLE: return Alpha::FBGT;
335   case Alpha::FBLT: return Alpha::FBGE;
336   default:
337     assert(0 && "Unknown opcode");
338   }
339   return 0; // Not reached
340 }
341
342 // Branch analysis.
343 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
344                                    MachineBasicBlock *&FBB,
345                                    SmallVectorImpl<MachineOperand> &Cond,
346                                    bool AllowModify) const {
347   // If the block has no terminators, it just falls into the block after it.
348   MachineBasicBlock::iterator I = MBB.end();
349   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
350     return false;
351
352   // Get the last instruction in the block.
353   MachineInstr *LastInst = I;
354   
355   // If there is only one terminator instruction, process it.
356   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
357     if (LastInst->getOpcode() == Alpha::BR) {
358       TBB = LastInst->getOperand(0).getMBB();
359       return false;
360     } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
361                LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
362       // Block ends with fall-through condbranch.
363       TBB = LastInst->getOperand(2).getMBB();
364       Cond.push_back(LastInst->getOperand(0));
365       Cond.push_back(LastInst->getOperand(1));
366       return false;
367     }
368     // Otherwise, don't know what this is.
369     return true;
370   }
371   
372   // Get the instruction before it if it's a terminator.
373   MachineInstr *SecondLastInst = I;
374
375   // If there are three terminators, we don't know what sort of block this is.
376   if (SecondLastInst && I != MBB.begin() &&
377       isUnpredicatedTerminator(--I))
378     return true;
379   
380   // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
381   if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
382       SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && 
383       LastInst->getOpcode() == Alpha::BR) {
384     TBB =  SecondLastInst->getOperand(2).getMBB();
385     Cond.push_back(SecondLastInst->getOperand(0));
386     Cond.push_back(SecondLastInst->getOperand(1));
387     FBB = LastInst->getOperand(0).getMBB();
388     return false;
389   }
390   
391   // If the block ends with two Alpha::BRs, handle it.  The second one is not
392   // executed, so remove it.
393   if (SecondLastInst->getOpcode() == Alpha::BR && 
394       LastInst->getOpcode() == Alpha::BR) {
395     TBB = SecondLastInst->getOperand(0).getMBB();
396     I = LastInst;
397     if (AllowModify)
398       I->eraseFromParent();
399     return false;
400   }
401
402   // Otherwise, can't handle this.
403   return true;
404 }
405
406 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
407   MachineBasicBlock::iterator I = MBB.end();
408   if (I == MBB.begin()) return 0;
409   --I;
410   if (I->getOpcode() != Alpha::BR && 
411       I->getOpcode() != Alpha::COND_BRANCH_I &&
412       I->getOpcode() != Alpha::COND_BRANCH_F)
413     return 0;
414   
415   // Remove the branch.
416   I->eraseFromParent();
417   
418   I = MBB.end();
419
420   if (I == MBB.begin()) return 1;
421   --I;
422   if (I->getOpcode() != Alpha::COND_BRANCH_I && 
423       I->getOpcode() != Alpha::COND_BRANCH_F)
424     return 1;
425   
426   // Remove the branch.
427   I->eraseFromParent();
428   return 2;
429 }
430
431 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, 
432                                 MachineBasicBlock::iterator MI) const {
433   DebugLoc DL = DebugLoc::getUnknownLoc();
434   if (MI != MBB.end()) DL = MI->getDebugLoc();
435   BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
436     .addReg(Alpha::R31)
437     .addReg(Alpha::R31);
438 }
439
440 bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
441   if (MBB.empty()) return false;
442   
443   switch (MBB.back().getOpcode()) {
444   case Alpha::RETDAG: // Return.
445   case Alpha::RETDAGp:
446   case Alpha::BR:     // Uncond branch.
447   case Alpha::JMP:  // Indirect branch.
448     return true;
449   default: return false;
450   }
451 }
452 bool AlphaInstrInfo::
453 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
454   assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
455   Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
456   return false;
457 }
458