Replace copyRegToReg with copyPhysReg for Alpha.
[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 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
219                                                     MachineInstr *MI,
220                                           const SmallVectorImpl<unsigned> &Ops,
221                                                     int FrameIndex) const {
222    if (Ops.size() != 1) return NULL;
223
224    // Make sure this is a reg-reg copy.
225    unsigned Opc = MI->getOpcode();
226
227    MachineInstr *NewMI = NULL;
228    switch(Opc) {
229    default:
230      break;
231    case Alpha::BISr:
232    case Alpha::CPYSS:
233    case Alpha::CPYST:
234      if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
235        if (Ops[0] == 0) {  // move -> store
236          unsigned InReg = MI->getOperand(1).getReg();
237          bool isKill = MI->getOperand(1).isKill();
238          bool isUndef = MI->getOperand(1).isUndef();
239          Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 
240            ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
241          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
242            .addReg(InReg, getKillRegState(isKill) | getUndefRegState(isUndef))
243            .addFrameIndex(FrameIndex)
244            .addReg(Alpha::F31);
245        } else {           // load -> move
246          unsigned OutReg = MI->getOperand(0).getReg();
247          bool isDead = MI->getOperand(0).isDead();
248          bool isUndef = MI->getOperand(0).isUndef();
249          Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 
250            ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
251          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
252            .addReg(OutReg, RegState::Define | getDeadRegState(isDead) |
253                    getUndefRegState(isUndef))
254            .addFrameIndex(FrameIndex)
255            .addReg(Alpha::F31);
256        }
257      }
258      break;
259    }
260   return NewMI;
261 }
262
263 static unsigned AlphaRevCondCode(unsigned Opcode) {
264   switch (Opcode) {
265   case Alpha::BEQ: return Alpha::BNE;
266   case Alpha::BNE: return Alpha::BEQ;
267   case Alpha::BGE: return Alpha::BLT;
268   case Alpha::BGT: return Alpha::BLE;
269   case Alpha::BLE: return Alpha::BGT;
270   case Alpha::BLT: return Alpha::BGE;
271   case Alpha::BLBC: return Alpha::BLBS;
272   case Alpha::BLBS: return Alpha::BLBC;
273   case Alpha::FBEQ: return Alpha::FBNE;
274   case Alpha::FBNE: return Alpha::FBEQ;
275   case Alpha::FBGE: return Alpha::FBLT;
276   case Alpha::FBGT: return Alpha::FBLE;
277   case Alpha::FBLE: return Alpha::FBGT;
278   case Alpha::FBLT: return Alpha::FBGE;
279   default:
280     llvm_unreachable("Unknown opcode");
281   }
282   return 0; // Not reached
283 }
284
285 // Branch analysis.
286 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
287                                    MachineBasicBlock *&FBB,
288                                    SmallVectorImpl<MachineOperand> &Cond,
289                                    bool AllowModify) const {
290   // If the block has no terminators, it just falls into the block after it.
291   MachineBasicBlock::iterator I = MBB.end();
292   if (I == MBB.begin())
293     return false;
294   --I;
295   while (I->isDebugValue()) {
296     if (I == MBB.begin())
297       return false;
298     --I;
299   }
300   if (!isUnpredicatedTerminator(I))
301     return false;
302
303   // Get the last instruction in the block.
304   MachineInstr *LastInst = I;
305   
306   // If there is only one terminator instruction, process it.
307   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
308     if (LastInst->getOpcode() == Alpha::BR) {
309       TBB = LastInst->getOperand(0).getMBB();
310       return false;
311     } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
312                LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
313       // Block ends with fall-through condbranch.
314       TBB = LastInst->getOperand(2).getMBB();
315       Cond.push_back(LastInst->getOperand(0));
316       Cond.push_back(LastInst->getOperand(1));
317       return false;
318     }
319     // Otherwise, don't know what this is.
320     return true;
321   }
322   
323   // Get the instruction before it if it's a terminator.
324   MachineInstr *SecondLastInst = I;
325
326   // If there are three terminators, we don't know what sort of block this is.
327   if (SecondLastInst && I != MBB.begin() &&
328       isUnpredicatedTerminator(--I))
329     return true;
330   
331   // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
332   if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
333       SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && 
334       LastInst->getOpcode() == Alpha::BR) {
335     TBB =  SecondLastInst->getOperand(2).getMBB();
336     Cond.push_back(SecondLastInst->getOperand(0));
337     Cond.push_back(SecondLastInst->getOperand(1));
338     FBB = LastInst->getOperand(0).getMBB();
339     return false;
340   }
341   
342   // If the block ends with two Alpha::BRs, handle it.  The second one is not
343   // executed, so remove it.
344   if (SecondLastInst->getOpcode() == Alpha::BR && 
345       LastInst->getOpcode() == Alpha::BR) {
346     TBB = SecondLastInst->getOperand(0).getMBB();
347     I = LastInst;
348     if (AllowModify)
349       I->eraseFromParent();
350     return false;
351   }
352
353   // Otherwise, can't handle this.
354   return true;
355 }
356
357 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
358   MachineBasicBlock::iterator I = MBB.end();
359   if (I == MBB.begin()) return 0;
360   --I;
361   while (I->isDebugValue()) {
362     if (I == MBB.begin())
363       return 0;
364     --I;
365   }
366   if (I->getOpcode() != Alpha::BR && 
367       I->getOpcode() != Alpha::COND_BRANCH_I &&
368       I->getOpcode() != Alpha::COND_BRANCH_F)
369     return 0;
370   
371   // Remove the branch.
372   I->eraseFromParent();
373   
374   I = MBB.end();
375
376   if (I == MBB.begin()) return 1;
377   --I;
378   if (I->getOpcode() != Alpha::COND_BRANCH_I && 
379       I->getOpcode() != Alpha::COND_BRANCH_F)
380     return 1;
381   
382   // Remove the branch.
383   I->eraseFromParent();
384   return 2;
385 }
386
387 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, 
388                                 MachineBasicBlock::iterator MI) const {
389   DebugLoc DL;
390   BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
391     .addReg(Alpha::R31)
392     .addReg(Alpha::R31);
393 }
394
395 bool AlphaInstrInfo::
396 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
397   assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
398   Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
399   return false;
400 }
401
402 /// getGlobalBaseReg - Return a virtual register initialized with the
403 /// the global base register value. Output instructions required to
404 /// initialize the register in the function entry block, if necessary.
405 ///
406 unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
407   AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
408   unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg();
409   if (GlobalBaseReg != 0)
410     return GlobalBaseReg;
411
412   // Insert the set of GlobalBaseReg into the first MBB of the function
413   MachineBasicBlock &FirstMBB = MF->front();
414   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
415   MachineRegisterInfo &RegInfo = MF->getRegInfo();
416   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
417
418   GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
419   BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
420           GlobalBaseReg).addReg(Alpha::R29);
421   RegInfo.addLiveIn(Alpha::R29);
422
423   AlphaFI->setGlobalBaseReg(GlobalBaseReg);
424   return GlobalBaseReg;
425 }
426
427 /// getGlobalRetAddr - Return a virtual register initialized with the
428 /// the global base register value. Output instructions required to
429 /// initialize the register in the function entry block, if necessary.
430 ///
431 unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const {
432   AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
433   unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr();
434   if (GlobalRetAddr != 0)
435     return GlobalRetAddr;
436
437   // Insert the set of GlobalRetAddr into the first MBB of the function
438   MachineBasicBlock &FirstMBB = MF->front();
439   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
440   MachineRegisterInfo &RegInfo = MF->getRegInfo();
441   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
442
443   GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
444   BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
445           GlobalRetAddr).addReg(Alpha::R26);
446   RegInfo.addLiveIn(Alpha::R26);
447
448   AlphaFI->setGlobalRetAddr(GlobalRetAddr);
449   return GlobalRetAddr;
450 }