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