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