Merge XXXGenRegisterNames.inc into XXXGenRegisterInfo.inc
[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 "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/Support/ErrorHandling.h"
22
23 #define GET_INSTRINFO_MC_DESC
24 #include "AlphaGenInstrInfo.inc"
25 using namespace llvm;
26
27 AlphaInstrInfo::AlphaInstrInfo()
28   : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
29     RI(*this) { }
30
31
32 unsigned 
33 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
34                                     int &FrameIndex) const {
35   switch (MI->getOpcode()) {
36   case Alpha::LDL:
37   case Alpha::LDQ:
38   case Alpha::LDBU:
39   case Alpha::LDWU:
40   case Alpha::LDS:
41   case Alpha::LDT:
42     if (MI->getOperand(1).isFI()) {
43       FrameIndex = MI->getOperand(1).getIndex();
44       return MI->getOperand(0).getReg();
45     }
46     break;
47   }
48   return 0;
49 }
50
51 unsigned 
52 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
53                                    int &FrameIndex) const {
54   switch (MI->getOpcode()) {
55   case Alpha::STL:
56   case Alpha::STQ:
57   case Alpha::STB:
58   case Alpha::STW:
59   case Alpha::STS:
60   case Alpha::STT:
61     if (MI->getOperand(1).isFI()) {
62       FrameIndex = MI->getOperand(1).getIndex();
63       return MI->getOperand(0).getReg();
64     }
65     break;
66   }
67   return 0;
68 }
69
70 static bool isAlphaIntCondCode(unsigned Opcode) {
71   switch (Opcode) {
72   case Alpha::BEQ: 
73   case Alpha::BNE: 
74   case Alpha::BGE: 
75   case Alpha::BGT: 
76   case Alpha::BLE: 
77   case Alpha::BLT: 
78   case Alpha::BLBC: 
79   case Alpha::BLBS:
80     return true;
81   default:
82     return false;
83   }
84 }
85
86 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
87                                       MachineBasicBlock *TBB,
88                                       MachineBasicBlock *FBB,
89                                       const SmallVectorImpl<MachineOperand> &Cond,
90                                       DebugLoc DL) const {
91   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
92   assert((Cond.size() == 2 || Cond.size() == 0) && 
93          "Alpha branch conditions have two components!");
94
95   // One-way branch.
96   if (FBB == 0) {
97     if (Cond.empty())   // Unconditional branch
98       BuildMI(&MBB, DL, get(Alpha::BR)).addMBB(TBB);
99     else                // Conditional branch
100       if (isAlphaIntCondCode(Cond[0].getImm()))
101         BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_I))
102           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
103       else
104         BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_F))
105           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
106     return 1;
107   }
108   
109   // Two-way Conditional Branch.
110   if (isAlphaIntCondCode(Cond[0].getImm()))
111     BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_I))
112       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
113   else
114     BuildMI(&MBB, DL, get(Alpha::COND_BRANCH_F))
115       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
116   BuildMI(&MBB, DL, get(Alpha::BR)).addMBB(FBB);
117   return 2;
118 }
119
120 void AlphaInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
121                                  MachineBasicBlock::iterator MI, DebugLoc DL,
122                                  unsigned DestReg, unsigned SrcReg,
123                                  bool KillSrc) const {
124   if (Alpha::GPRCRegClass.contains(DestReg, SrcReg)) {
125     BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
126       .addReg(SrcReg)
127       .addReg(SrcReg, getKillRegState(KillSrc));
128   } else if (Alpha::F4RCRegClass.contains(DestReg, SrcReg)) {
129     BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
130       .addReg(SrcReg)
131       .addReg(SrcReg, getKillRegState(KillSrc));
132   } else if (Alpha::F8RCRegClass.contains(DestReg, SrcReg)) {
133     BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
134       .addReg(SrcReg)
135       .addReg(SrcReg, getKillRegState(KillSrc));
136   } else {
137     llvm_unreachable("Attempt to copy register that is not GPR or FPR");
138   }
139 }
140
141 void
142 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
143                                     MachineBasicBlock::iterator MI,
144                                     unsigned SrcReg, bool isKill, int FrameIdx,
145                                     const TargetRegisterClass *RC,
146                                     const TargetRegisterInfo *TRI) const {
147   //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
148   //     << FrameIdx << "\n";
149   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
150
151   DebugLoc DL;
152   if (MI != MBB.end()) DL = MI->getDebugLoc();
153
154   if (RC == Alpha::F4RCRegisterClass)
155     BuildMI(MBB, MI, DL, get(Alpha::STS))
156       .addReg(SrcReg, getKillRegState(isKill))
157       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
158   else if (RC == Alpha::F8RCRegisterClass)
159     BuildMI(MBB, MI, DL, get(Alpha::STT))
160       .addReg(SrcReg, getKillRegState(isKill))
161       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
162   else if (RC == Alpha::GPRCRegisterClass)
163     BuildMI(MBB, MI, DL, get(Alpha::STQ))
164       .addReg(SrcReg, getKillRegState(isKill))
165       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
166   else
167     llvm_unreachable("Unhandled register class");
168 }
169
170 void
171 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
172                                         MachineBasicBlock::iterator MI,
173                                         unsigned DestReg, int FrameIdx,
174                                      const TargetRegisterClass *RC,
175                                      const TargetRegisterInfo *TRI) const {
176   //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
177   //     << FrameIdx << "\n";
178   DebugLoc DL;
179   if (MI != MBB.end()) DL = MI->getDebugLoc();
180
181   if (RC == Alpha::F4RCRegisterClass)
182     BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
183       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
184   else if (RC == Alpha::F8RCRegisterClass)
185     BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
186       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
187   else if (RC == Alpha::GPRCRegisterClass)
188     BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
189       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
190   else
191     llvm_unreachable("Unhandled register class");
192 }
193
194 static unsigned AlphaRevCondCode(unsigned Opcode) {
195   switch (Opcode) {
196   case Alpha::BEQ: return Alpha::BNE;
197   case Alpha::BNE: return Alpha::BEQ;
198   case Alpha::BGE: return Alpha::BLT;
199   case Alpha::BGT: return Alpha::BLE;
200   case Alpha::BLE: return Alpha::BGT;
201   case Alpha::BLT: return Alpha::BGE;
202   case Alpha::BLBC: return Alpha::BLBS;
203   case Alpha::BLBS: return Alpha::BLBC;
204   case Alpha::FBEQ: return Alpha::FBNE;
205   case Alpha::FBNE: return Alpha::FBEQ;
206   case Alpha::FBGE: return Alpha::FBLT;
207   case Alpha::FBGT: return Alpha::FBLE;
208   case Alpha::FBLE: return Alpha::FBGT;
209   case Alpha::FBLT: return Alpha::FBGE;
210   default:
211     llvm_unreachable("Unknown opcode");
212   }
213   return 0; // Not reached
214 }
215
216 // Branch analysis.
217 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
218                                    MachineBasicBlock *&FBB,
219                                    SmallVectorImpl<MachineOperand> &Cond,
220                                    bool AllowModify) const {
221   // If the block has no terminators, it just falls into the block after it.
222   MachineBasicBlock::iterator I = MBB.end();
223   if (I == MBB.begin())
224     return false;
225   --I;
226   while (I->isDebugValue()) {
227     if (I == MBB.begin())
228       return false;
229     --I;
230   }
231   if (!isUnpredicatedTerminator(I))
232     return false;
233
234   // Get the last instruction in the block.
235   MachineInstr *LastInst = I;
236   
237   // If there is only one terminator instruction, process it.
238   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
239     if (LastInst->getOpcode() == Alpha::BR) {
240       TBB = LastInst->getOperand(0).getMBB();
241       return false;
242     } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
243                LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
244       // Block ends with fall-through condbranch.
245       TBB = LastInst->getOperand(2).getMBB();
246       Cond.push_back(LastInst->getOperand(0));
247       Cond.push_back(LastInst->getOperand(1));
248       return false;
249     }
250     // Otherwise, don't know what this is.
251     return true;
252   }
253   
254   // Get the instruction before it if it's a terminator.
255   MachineInstr *SecondLastInst = I;
256
257   // If there are three terminators, we don't know what sort of block this is.
258   if (SecondLastInst && I != MBB.begin() &&
259       isUnpredicatedTerminator(--I))
260     return true;
261   
262   // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
263   if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
264       SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && 
265       LastInst->getOpcode() == Alpha::BR) {
266     TBB =  SecondLastInst->getOperand(2).getMBB();
267     Cond.push_back(SecondLastInst->getOperand(0));
268     Cond.push_back(SecondLastInst->getOperand(1));
269     FBB = LastInst->getOperand(0).getMBB();
270     return false;
271   }
272   
273   // If the block ends with two Alpha::BRs, handle it.  The second one is not
274   // executed, so remove it.
275   if (SecondLastInst->getOpcode() == Alpha::BR && 
276       LastInst->getOpcode() == Alpha::BR) {
277     TBB = SecondLastInst->getOperand(0).getMBB();
278     I = LastInst;
279     if (AllowModify)
280       I->eraseFromParent();
281     return false;
282   }
283
284   // Otherwise, can't handle this.
285   return true;
286 }
287
288 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
289   MachineBasicBlock::iterator I = MBB.end();
290   if (I == MBB.begin()) return 0;
291   --I;
292   while (I->isDebugValue()) {
293     if (I == MBB.begin())
294       return 0;
295     --I;
296   }
297   if (I->getOpcode() != Alpha::BR && 
298       I->getOpcode() != Alpha::COND_BRANCH_I &&
299       I->getOpcode() != Alpha::COND_BRANCH_F)
300     return 0;
301   
302   // Remove the branch.
303   I->eraseFromParent();
304   
305   I = MBB.end();
306
307   if (I == MBB.begin()) return 1;
308   --I;
309   if (I->getOpcode() != Alpha::COND_BRANCH_I && 
310       I->getOpcode() != Alpha::COND_BRANCH_F)
311     return 1;
312   
313   // Remove the branch.
314   I->eraseFromParent();
315   return 2;
316 }
317
318 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, 
319                                 MachineBasicBlock::iterator MI) const {
320   DebugLoc DL;
321   BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
322     .addReg(Alpha::R31)
323     .addReg(Alpha::R31);
324 }
325
326 bool AlphaInstrInfo::
327 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
328   assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
329   Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
330   return false;
331 }
332
333 /// getGlobalBaseReg - Return a virtual register initialized with the
334 /// the global base register value. Output instructions required to
335 /// initialize the register in the function entry block, if necessary.
336 ///
337 unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
338   AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
339   unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg();
340   if (GlobalBaseReg != 0)
341     return GlobalBaseReg;
342
343   // Insert the set of GlobalBaseReg into the first MBB of the function
344   MachineBasicBlock &FirstMBB = MF->front();
345   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
346   MachineRegisterInfo &RegInfo = MF->getRegInfo();
347   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
348
349   GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
350   BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
351           GlobalBaseReg).addReg(Alpha::R29);
352   RegInfo.addLiveIn(Alpha::R29);
353
354   AlphaFI->setGlobalBaseReg(GlobalBaseReg);
355   return GlobalBaseReg;
356 }
357
358 /// getGlobalRetAddr - Return a virtual register initialized with the
359 /// the global base register value. Output instructions required to
360 /// initialize the register in the function entry block, if necessary.
361 ///
362 unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const {
363   AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
364   unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr();
365   if (GlobalRetAddr != 0)
366     return GlobalRetAddr;
367
368   // Insert the set of GlobalRetAddr into the first MBB of the function
369   MachineBasicBlock &FirstMBB = MF->front();
370   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
371   MachineRegisterInfo &RegInfo = MF->getRegInfo();
372   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
373
374   GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
375   BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
376           GlobalRetAddr).addReg(Alpha::R26);
377   RegInfo.addLiveIn(Alpha::R26);
378
379   AlphaFI->setGlobalRetAddr(GlobalRetAddr);
380   return GlobalRetAddr;
381 }