Remove unused AsmPrinter OptLevel argument, and propogate.
[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 "AlphaMachineFunctionInfo.h"
17 #include "AlphaGenInstrInfo.inc"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 using namespace llvm;
23
24 AlphaInstrInfo::AlphaInstrInfo()
25   : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
26     RI(*this) { }
27
28
29 bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
30                                  unsigned& sourceReg, unsigned& destReg,
31                                  unsigned& SrcSR, unsigned& DstSR) const {
32   unsigned oc = MI.getOpcode();
33   if (oc == Alpha::BISr   || 
34       oc == Alpha::CPYSS  || 
35       oc == Alpha::CPYST  ||
36       oc == Alpha::CPYSSt || 
37       oc == Alpha::CPYSTs) {
38     // or r1, r2, r2 
39     // cpys(s|t) r1 r2 r2
40     assert(MI.getNumOperands() >= 3 &&
41            MI.getOperand(0).isReg() &&
42            MI.getOperand(1).isReg() &&
43            MI.getOperand(2).isReg() &&
44            "invalid Alpha BIS instruction!");
45     if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
46       sourceReg = MI.getOperand(1).getReg();
47       destReg = MI.getOperand(0).getReg();
48       SrcSR = DstSR = 0;
49       return true;
50     }
51   }
52   return false;
53 }
54
55 unsigned 
56 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
57                                     int &FrameIndex) const {
58   switch (MI->getOpcode()) {
59   case Alpha::LDL:
60   case Alpha::LDQ:
61   case Alpha::LDBU:
62   case Alpha::LDWU:
63   case Alpha::LDS:
64   case Alpha::LDT:
65     if (MI->getOperand(1).isFI()) {
66       FrameIndex = MI->getOperand(1).getIndex();
67       return MI->getOperand(0).getReg();
68     }
69     break;
70   }
71   return 0;
72 }
73
74 unsigned 
75 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
76                                    int &FrameIndex) const {
77   switch (MI->getOpcode()) {
78   case Alpha::STL:
79   case Alpha::STQ:
80   case Alpha::STB:
81   case Alpha::STW:
82   case Alpha::STS:
83   case Alpha::STT:
84     if (MI->getOperand(1).isFI()) {
85       FrameIndex = MI->getOperand(1).getIndex();
86       return MI->getOperand(0).getReg();
87     }
88     break;
89   }
90   return 0;
91 }
92
93 static bool isAlphaIntCondCode(unsigned Opcode) {
94   switch (Opcode) {
95   case Alpha::BEQ: 
96   case Alpha::BNE: 
97   case Alpha::BGE: 
98   case Alpha::BGT: 
99   case Alpha::BLE: 
100   case Alpha::BLT: 
101   case Alpha::BLBC: 
102   case Alpha::BLBS:
103     return true;
104   default:
105     return false;
106   }
107 }
108
109 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
110                                       MachineBasicBlock *TBB,
111                                       MachineBasicBlock *FBB,
112                             const SmallVectorImpl<MachineOperand> &Cond) const {
113   // FIXME this should probably have a DebugLoc argument
114   DebugLoc dl = DebugLoc::getUnknownLoc();
115   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
116   assert((Cond.size() == 2 || Cond.size() == 0) && 
117          "Alpha branch conditions have two components!");
118
119   // One-way branch.
120   if (FBB == 0) {
121     if (Cond.empty())   // Unconditional branch
122       BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(TBB);
123     else                // Conditional branch
124       if (isAlphaIntCondCode(Cond[0].getImm()))
125         BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
126           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
127       else
128         BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
129           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
130     return 1;
131   }
132   
133   // Two-way Conditional Branch.
134   if (isAlphaIntCondCode(Cond[0].getImm()))
135     BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
136       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
137   else
138     BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
139       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
140   BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(FBB);
141   return 2;
142 }
143
144 bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
145                                   MachineBasicBlock::iterator MI,
146                                   unsigned DestReg, unsigned SrcReg,
147                                   const TargetRegisterClass *DestRC,
148                                   const TargetRegisterClass *SrcRC) const {
149   //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
150   if (DestRC != SrcRC) {
151     // Not yet supported!
152     return false;
153   }
154
155   DebugLoc DL = DebugLoc::getUnknownLoc();
156   if (MI != MBB.end()) DL = MI->getDebugLoc();
157
158   if (DestRC == Alpha::GPRCRegisterClass) {
159     BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
160       .addReg(SrcReg)
161       .addReg(SrcReg);
162   } else if (DestRC == Alpha::F4RCRegisterClass) {
163     BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
164       .addReg(SrcReg)
165       .addReg(SrcReg);
166   } else if (DestRC == Alpha::F8RCRegisterClass) {
167     BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
168       .addReg(SrcReg)
169       .addReg(SrcReg);
170   } else {
171     // Attempt to copy register that is not GPR or FPR
172     return false;
173   }
174   
175   return true;
176 }
177
178 void
179 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
180                                     MachineBasicBlock::iterator MI,
181                                     unsigned SrcReg, bool isKill, int FrameIdx,
182                                     const TargetRegisterClass *RC) const {
183   //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
184   //     << FrameIdx << "\n";
185   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
186
187   DebugLoc DL = DebugLoc::getUnknownLoc();
188   if (MI != MBB.end()) DL = MI->getDebugLoc();
189
190   if (RC == Alpha::F4RCRegisterClass)
191     BuildMI(MBB, MI, DL, get(Alpha::STS))
192       .addReg(SrcReg, getKillRegState(isKill))
193       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
194   else if (RC == Alpha::F8RCRegisterClass)
195     BuildMI(MBB, MI, DL, get(Alpha::STT))
196       .addReg(SrcReg, getKillRegState(isKill))
197       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
198   else if (RC == Alpha::GPRCRegisterClass)
199     BuildMI(MBB, MI, DL, get(Alpha::STQ))
200       .addReg(SrcReg, getKillRegState(isKill))
201       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
202   else
203     abort();
204 }
205
206 void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
207                                        bool isKill,
208                                        SmallVectorImpl<MachineOperand> &Addr,
209                                        const TargetRegisterClass *RC,
210                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
211   unsigned Opc = 0;
212   if (RC == Alpha::F4RCRegisterClass)
213     Opc = Alpha::STS;
214   else if (RC == Alpha::F8RCRegisterClass)
215     Opc = Alpha::STT;
216   else if (RC == Alpha::GPRCRegisterClass)
217     Opc = Alpha::STQ;
218   else
219     abort();
220   DebugLoc DL = DebugLoc::getUnknownLoc();
221   MachineInstrBuilder MIB = 
222     BuildMI(MF, DL, get(Opc)).addReg(SrcReg, getKillRegState(isKill));
223   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
224     MIB.addOperand(Addr[i]);
225   NewMIs.push_back(MIB);
226 }
227
228 void
229 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
230                                         MachineBasicBlock::iterator MI,
231                                         unsigned DestReg, int FrameIdx,
232                                         const TargetRegisterClass *RC) const {
233   //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
234   //     << FrameIdx << "\n";
235   DebugLoc DL = DebugLoc::getUnknownLoc();
236   if (MI != MBB.end()) DL = MI->getDebugLoc();
237
238   if (RC == Alpha::F4RCRegisterClass)
239     BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
240       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
241   else if (RC == Alpha::F8RCRegisterClass)
242     BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
243       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
244   else if (RC == Alpha::GPRCRegisterClass)
245     BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
246       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
247   else
248     abort();
249 }
250
251 void AlphaInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
252                                         SmallVectorImpl<MachineOperand> &Addr,
253                                         const TargetRegisterClass *RC,
254                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
255   unsigned Opc = 0;
256   if (RC == Alpha::F4RCRegisterClass)
257     Opc = Alpha::LDS;
258   else if (RC == Alpha::F8RCRegisterClass)
259     Opc = Alpha::LDT;
260   else if (RC == Alpha::GPRCRegisterClass)
261     Opc = Alpha::LDQ;
262   else
263     abort();
264   DebugLoc DL = DebugLoc::getUnknownLoc();
265   MachineInstrBuilder MIB = 
266     BuildMI(MF, DL, get(Opc), DestReg);
267   for (unsigned i = 0, e = Addr.size(); i != e; ++i)
268     MIB.addOperand(Addr[i]);
269   NewMIs.push_back(MIB);
270 }
271
272 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
273                                                     MachineInstr *MI,
274                                           const SmallVectorImpl<unsigned> &Ops,
275                                                     int FrameIndex) const {
276    if (Ops.size() != 1) return NULL;
277
278    // Make sure this is a reg-reg copy.
279    unsigned Opc = MI->getOpcode();
280
281    MachineInstr *NewMI = NULL;
282    switch(Opc) {
283    default:
284      break;
285    case Alpha::BISr:
286    case Alpha::CPYSS:
287    case Alpha::CPYST:
288      if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
289        if (Ops[0] == 0) {  // move -> store
290          unsigned InReg = MI->getOperand(1).getReg();
291          bool isKill = MI->getOperand(1).isKill();
292          Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 
293            ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
294          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
295            .addReg(InReg, getKillRegState(isKill))
296            .addFrameIndex(FrameIndex)
297            .addReg(Alpha::F31);
298        } else {           // load -> move
299          unsigned OutReg = MI->getOperand(0).getReg();
300          bool isDead = MI->getOperand(0).isDead();
301          Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 
302            ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
303          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
304            .addReg(OutReg, RegState::Define | getDeadRegState(isDead))
305            .addFrameIndex(FrameIndex)
306            .addReg(Alpha::F31);
307        }
308      }
309      break;
310    }
311   return NewMI;
312 }
313
314 static unsigned AlphaRevCondCode(unsigned Opcode) {
315   switch (Opcode) {
316   case Alpha::BEQ: return Alpha::BNE;
317   case Alpha::BNE: return Alpha::BEQ;
318   case Alpha::BGE: return Alpha::BLT;
319   case Alpha::BGT: return Alpha::BLE;
320   case Alpha::BLE: return Alpha::BGT;
321   case Alpha::BLT: return Alpha::BGE;
322   case Alpha::BLBC: return Alpha::BLBS;
323   case Alpha::BLBS: return Alpha::BLBC;
324   case Alpha::FBEQ: return Alpha::FBNE;
325   case Alpha::FBNE: return Alpha::FBEQ;
326   case Alpha::FBGE: return Alpha::FBLT;
327   case Alpha::FBGT: return Alpha::FBLE;
328   case Alpha::FBLE: return Alpha::FBGT;
329   case Alpha::FBLT: return Alpha::FBGE;
330   default:
331     assert(0 && "Unknown opcode");
332   }
333   return 0; // Not reached
334 }
335
336 // Branch analysis.
337 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
338                                    MachineBasicBlock *&FBB,
339                                    SmallVectorImpl<MachineOperand> &Cond,
340                                    bool AllowModify) const {
341   // If the block has no terminators, it just falls into the block after it.
342   MachineBasicBlock::iterator I = MBB.end();
343   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
344     return false;
345
346   // Get the last instruction in the block.
347   MachineInstr *LastInst = I;
348   
349   // If there is only one terminator instruction, process it.
350   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
351     if (LastInst->getOpcode() == Alpha::BR) {
352       TBB = LastInst->getOperand(0).getMBB();
353       return false;
354     } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
355                LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
356       // Block ends with fall-through condbranch.
357       TBB = LastInst->getOperand(2).getMBB();
358       Cond.push_back(LastInst->getOperand(0));
359       Cond.push_back(LastInst->getOperand(1));
360       return false;
361     }
362     // Otherwise, don't know what this is.
363     return true;
364   }
365   
366   // Get the instruction before it if it's a terminator.
367   MachineInstr *SecondLastInst = I;
368
369   // If there are three terminators, we don't know what sort of block this is.
370   if (SecondLastInst && I != MBB.begin() &&
371       isUnpredicatedTerminator(--I))
372     return true;
373   
374   // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
375   if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
376       SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && 
377       LastInst->getOpcode() == Alpha::BR) {
378     TBB =  SecondLastInst->getOperand(2).getMBB();
379     Cond.push_back(SecondLastInst->getOperand(0));
380     Cond.push_back(SecondLastInst->getOperand(1));
381     FBB = LastInst->getOperand(0).getMBB();
382     return false;
383   }
384   
385   // If the block ends with two Alpha::BRs, handle it.  The second one is not
386   // executed, so remove it.
387   if (SecondLastInst->getOpcode() == Alpha::BR && 
388       LastInst->getOpcode() == Alpha::BR) {
389     TBB = SecondLastInst->getOperand(0).getMBB();
390     I = LastInst;
391     if (AllowModify)
392       I->eraseFromParent();
393     return false;
394   }
395
396   // Otherwise, can't handle this.
397   return true;
398 }
399
400 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
401   MachineBasicBlock::iterator I = MBB.end();
402   if (I == MBB.begin()) return 0;
403   --I;
404   if (I->getOpcode() != Alpha::BR && 
405       I->getOpcode() != Alpha::COND_BRANCH_I &&
406       I->getOpcode() != Alpha::COND_BRANCH_F)
407     return 0;
408   
409   // Remove the branch.
410   I->eraseFromParent();
411   
412   I = MBB.end();
413
414   if (I == MBB.begin()) return 1;
415   --I;
416   if (I->getOpcode() != Alpha::COND_BRANCH_I && 
417       I->getOpcode() != Alpha::COND_BRANCH_F)
418     return 1;
419   
420   // Remove the branch.
421   I->eraseFromParent();
422   return 2;
423 }
424
425 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, 
426                                 MachineBasicBlock::iterator MI) const {
427   DebugLoc DL = DebugLoc::getUnknownLoc();
428   if (MI != MBB.end()) DL = MI->getDebugLoc();
429   BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
430     .addReg(Alpha::R31)
431     .addReg(Alpha::R31);
432 }
433
434 bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
435   if (MBB.empty()) return false;
436   
437   switch (MBB.back().getOpcode()) {
438   case Alpha::RETDAG: // Return.
439   case Alpha::RETDAGp:
440   case Alpha::BR:     // Uncond branch.
441   case Alpha::JMP:  // Indirect branch.
442     return true;
443   default: return false;
444   }
445 }
446 bool AlphaInstrInfo::
447 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
448   assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
449   Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
450   return false;
451 }
452
453 /// getGlobalBaseReg - Return a virtual register initialized with the
454 /// the global base register value. Output instructions required to
455 /// initialize the register in the function entry block, if necessary.
456 ///
457 unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
458   AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
459   unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg();
460   if (GlobalBaseReg != 0)
461     return GlobalBaseReg;
462
463   // Insert the set of GlobalBaseReg into the first MBB of the function
464   MachineBasicBlock &FirstMBB = MF->front();
465   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
466   MachineRegisterInfo &RegInfo = MF->getRegInfo();
467   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
468
469   GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
470   bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalBaseReg, Alpha::R29,
471                               &Alpha::GPRCRegClass, &Alpha::GPRCRegClass);
472   assert(Ok && "Couldn't assign to global base register!");
473   RegInfo.addLiveIn(Alpha::R29);
474
475   AlphaFI->setGlobalBaseReg(GlobalBaseReg);
476   return GlobalBaseReg;
477 }
478
479 /// getGlobalRetAddr - Return a virtual register initialized with the
480 /// the global base register value. Output instructions required to
481 /// initialize the register in the function entry block, if necessary.
482 ///
483 unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const {
484   AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
485   unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr();
486   if (GlobalRetAddr != 0)
487     return GlobalRetAddr;
488
489   // Insert the set of GlobalRetAddr into the first MBB of the function
490   MachineBasicBlock &FirstMBB = MF->front();
491   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
492   MachineRegisterInfo &RegInfo = MF->getRegInfo();
493   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
494
495   GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
496   bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalRetAddr, Alpha::R26,
497                               &Alpha::GPRCRegClass, &Alpha::GPRCRegClass);
498   assert(Ok && "Couldn't assign to global return address register!");
499   RegInfo.addLiveIn(Alpha::R26);
500
501   AlphaFI->setGlobalRetAddr(GlobalRetAddr);
502   return GlobalRetAddr;
503 }