Add argument TargetRegisterInfo to loadRegFromStackSlot and storeRegToStackSlot.
[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) const {
114   // FIXME this should probably have a DebugLoc argument
115   DebugLoc dl;
116   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
117   assert((Cond.size() == 2 || Cond.size() == 0) && 
118          "Alpha branch conditions have two components!");
119
120   // One-way branch.
121   if (FBB == 0) {
122     if (Cond.empty())   // Unconditional branch
123       BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(TBB);
124     else                // Conditional branch
125       if (isAlphaIntCondCode(Cond[0].getImm()))
126         BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
127           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
128       else
129         BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
130           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
131     return 1;
132   }
133   
134   // Two-way Conditional Branch.
135   if (isAlphaIntCondCode(Cond[0].getImm()))
136     BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
137       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
138   else
139     BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
140       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
141   BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(FBB);
142   return 2;
143 }
144
145 bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
146                                   MachineBasicBlock::iterator MI,
147                                   unsigned DestReg, unsigned SrcReg,
148                                   const TargetRegisterClass *DestRC,
149                                   const TargetRegisterClass *SrcRC) const {
150   //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
151   if (DestRC != SrcRC) {
152     // Not yet supported!
153     return false;
154   }
155
156   DebugLoc DL;
157   if (MI != MBB.end()) DL = MI->getDebugLoc();
158
159   if (DestRC == Alpha::GPRCRegisterClass) {
160     BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
161       .addReg(SrcReg)
162       .addReg(SrcReg);
163   } else if (DestRC == Alpha::F4RCRegisterClass) {
164     BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
165       .addReg(SrcReg)
166       .addReg(SrcReg);
167   } else if (DestRC == Alpha::F8RCRegisterClass) {
168     BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
169       .addReg(SrcReg)
170       .addReg(SrcReg);
171   } else {
172     // Attempt to copy register that is not GPR or FPR
173     return false;
174   }
175   
176   return true;
177 }
178
179 void
180 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
181                                     MachineBasicBlock::iterator MI,
182                                     unsigned SrcReg, bool isKill, int FrameIdx,
183                                     const TargetRegisterClass *RC,
184                                     const TargetRegisterInfo *TRI) const {
185   //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
186   //     << FrameIdx << "\n";
187   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
188
189   DebugLoc DL;
190   if (MI != MBB.end()) DL = MI->getDebugLoc();
191
192   if (RC == Alpha::F4RCRegisterClass)
193     BuildMI(MBB, MI, DL, get(Alpha::STS))
194       .addReg(SrcReg, getKillRegState(isKill))
195       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
196   else if (RC == Alpha::F8RCRegisterClass)
197     BuildMI(MBB, MI, DL, get(Alpha::STT))
198       .addReg(SrcReg, getKillRegState(isKill))
199       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
200   else if (RC == Alpha::GPRCRegisterClass)
201     BuildMI(MBB, MI, DL, get(Alpha::STQ))
202       .addReg(SrcReg, getKillRegState(isKill))
203       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
204   else
205     llvm_unreachable("Unhandled register class");
206 }
207
208 void
209 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
210                                         MachineBasicBlock::iterator MI,
211                                         unsigned DestReg, int FrameIdx,
212                                      const TargetRegisterClass *RC,
213                                      const TargetRegisterInfo *TRI) const {
214   //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
215   //     << FrameIdx << "\n";
216   DebugLoc DL;
217   if (MI != MBB.end()) DL = MI->getDebugLoc();
218
219   if (RC == Alpha::F4RCRegisterClass)
220     BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
221       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
222   else if (RC == Alpha::F8RCRegisterClass)
223     BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
224       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
225   else if (RC == Alpha::GPRCRegisterClass)
226     BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
227       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
228   else
229     llvm_unreachable("Unhandled register class");
230 }
231
232 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
233                                                     MachineInstr *MI,
234                                           const SmallVectorImpl<unsigned> &Ops,
235                                                     int FrameIndex) const {
236    if (Ops.size() != 1) return NULL;
237
238    // Make sure this is a reg-reg copy.
239    unsigned Opc = MI->getOpcode();
240
241    MachineInstr *NewMI = NULL;
242    switch(Opc) {
243    default:
244      break;
245    case Alpha::BISr:
246    case Alpha::CPYSS:
247    case Alpha::CPYST:
248      if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
249        if (Ops[0] == 0) {  // move -> store
250          unsigned InReg = MI->getOperand(1).getReg();
251          bool isKill = MI->getOperand(1).isKill();
252          bool isUndef = MI->getOperand(1).isUndef();
253          Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 
254            ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
255          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
256            .addReg(InReg, getKillRegState(isKill) | getUndefRegState(isUndef))
257            .addFrameIndex(FrameIndex)
258            .addReg(Alpha::F31);
259        } else {           // load -> move
260          unsigned OutReg = MI->getOperand(0).getReg();
261          bool isDead = MI->getOperand(0).isDead();
262          bool isUndef = MI->getOperand(0).isUndef();
263          Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 
264            ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
265          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
266            .addReg(OutReg, RegState::Define | getDeadRegState(isDead) |
267                    getUndefRegState(isUndef))
268            .addFrameIndex(FrameIndex)
269            .addReg(Alpha::F31);
270        }
271      }
272      break;
273    }
274   return NewMI;
275 }
276
277 static unsigned AlphaRevCondCode(unsigned Opcode) {
278   switch (Opcode) {
279   case Alpha::BEQ: return Alpha::BNE;
280   case Alpha::BNE: return Alpha::BEQ;
281   case Alpha::BGE: return Alpha::BLT;
282   case Alpha::BGT: return Alpha::BLE;
283   case Alpha::BLE: return Alpha::BGT;
284   case Alpha::BLT: return Alpha::BGE;
285   case Alpha::BLBC: return Alpha::BLBS;
286   case Alpha::BLBS: return Alpha::BLBC;
287   case Alpha::FBEQ: return Alpha::FBNE;
288   case Alpha::FBNE: return Alpha::FBEQ;
289   case Alpha::FBGE: return Alpha::FBLT;
290   case Alpha::FBGT: return Alpha::FBLE;
291   case Alpha::FBLE: return Alpha::FBGT;
292   case Alpha::FBLT: return Alpha::FBGE;
293   default:
294     llvm_unreachable("Unknown opcode");
295   }
296   return 0; // Not reached
297 }
298
299 // Branch analysis.
300 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
301                                    MachineBasicBlock *&FBB,
302                                    SmallVectorImpl<MachineOperand> &Cond,
303                                    bool AllowModify) const {
304   // If the block has no terminators, it just falls into the block after it.
305   MachineBasicBlock::iterator I = MBB.end();
306   if (I == MBB.begin())
307     return false;
308   --I;
309   while (I->isDebugValue()) {
310     if (I == MBB.begin())
311       return false;
312     --I;
313   }
314   if (!isUnpredicatedTerminator(I))
315     return false;
316
317   // Get the last instruction in the block.
318   MachineInstr *LastInst = I;
319   
320   // If there is only one terminator instruction, process it.
321   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
322     if (LastInst->getOpcode() == Alpha::BR) {
323       TBB = LastInst->getOperand(0).getMBB();
324       return false;
325     } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
326                LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
327       // Block ends with fall-through condbranch.
328       TBB = LastInst->getOperand(2).getMBB();
329       Cond.push_back(LastInst->getOperand(0));
330       Cond.push_back(LastInst->getOperand(1));
331       return false;
332     }
333     // Otherwise, don't know what this is.
334     return true;
335   }
336   
337   // Get the instruction before it if it's a terminator.
338   MachineInstr *SecondLastInst = I;
339
340   // If there are three terminators, we don't know what sort of block this is.
341   if (SecondLastInst && I != MBB.begin() &&
342       isUnpredicatedTerminator(--I))
343     return true;
344   
345   // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
346   if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
347       SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && 
348       LastInst->getOpcode() == Alpha::BR) {
349     TBB =  SecondLastInst->getOperand(2).getMBB();
350     Cond.push_back(SecondLastInst->getOperand(0));
351     Cond.push_back(SecondLastInst->getOperand(1));
352     FBB = LastInst->getOperand(0).getMBB();
353     return false;
354   }
355   
356   // If the block ends with two Alpha::BRs, handle it.  The second one is not
357   // executed, so remove it.
358   if (SecondLastInst->getOpcode() == Alpha::BR && 
359       LastInst->getOpcode() == Alpha::BR) {
360     TBB = SecondLastInst->getOperand(0).getMBB();
361     I = LastInst;
362     if (AllowModify)
363       I->eraseFromParent();
364     return false;
365   }
366
367   // Otherwise, can't handle this.
368   return true;
369 }
370
371 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
372   MachineBasicBlock::iterator I = MBB.end();
373   if (I == MBB.begin()) return 0;
374   --I;
375   while (I->isDebugValue()) {
376     if (I == MBB.begin())
377       return 0;
378     --I;
379   }
380   if (I->getOpcode() != Alpha::BR && 
381       I->getOpcode() != Alpha::COND_BRANCH_I &&
382       I->getOpcode() != Alpha::COND_BRANCH_F)
383     return 0;
384   
385   // Remove the branch.
386   I->eraseFromParent();
387   
388   I = MBB.end();
389
390   if (I == MBB.begin()) return 1;
391   --I;
392   if (I->getOpcode() != Alpha::COND_BRANCH_I && 
393       I->getOpcode() != Alpha::COND_BRANCH_F)
394     return 1;
395   
396   // Remove the branch.
397   I->eraseFromParent();
398   return 2;
399 }
400
401 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, 
402                                 MachineBasicBlock::iterator MI) const {
403   DebugLoc DL;
404   BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
405     .addReg(Alpha::R31)
406     .addReg(Alpha::R31);
407 }
408
409 bool AlphaInstrInfo::
410 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
411   assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
412   Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
413   return false;
414 }
415
416 /// getGlobalBaseReg - Return a virtual register initialized with the
417 /// the global base register value. Output instructions required to
418 /// initialize the register in the function entry block, if necessary.
419 ///
420 unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
421   AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
422   unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg();
423   if (GlobalBaseReg != 0)
424     return GlobalBaseReg;
425
426   // Insert the set of GlobalBaseReg into the first MBB of the function
427   MachineBasicBlock &FirstMBB = MF->front();
428   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
429   MachineRegisterInfo &RegInfo = MF->getRegInfo();
430   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
431
432   GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
433   bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalBaseReg, Alpha::R29,
434                               &Alpha::GPRCRegClass, &Alpha::GPRCRegClass);
435   assert(Ok && "Couldn't assign to global base register!");
436   Ok = Ok; // Silence warning when assertions are turned off.
437   RegInfo.addLiveIn(Alpha::R29);
438
439   AlphaFI->setGlobalBaseReg(GlobalBaseReg);
440   return GlobalBaseReg;
441 }
442
443 /// getGlobalRetAddr - Return a virtual register initialized with the
444 /// the global base register value. Output instructions required to
445 /// initialize the register in the function entry block, if necessary.
446 ///
447 unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const {
448   AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>();
449   unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr();
450   if (GlobalRetAddr != 0)
451     return GlobalRetAddr;
452
453   // Insert the set of GlobalRetAddr into the first MBB of the function
454   MachineBasicBlock &FirstMBB = MF->front();
455   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
456   MachineRegisterInfo &RegInfo = MF->getRegInfo();
457   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
458
459   GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass);
460   bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalRetAddr, Alpha::R26,
461                               &Alpha::GPRCRegClass, &Alpha::GPRCRegClass);
462   assert(Ok && "Couldn't assign to global return address register!");
463   Ok = Ok; // Silence warning when assertions are turned off.
464   RegInfo.addLiveIn(Alpha::R26);
465
466   AlphaFI->setGlobalRetAddr(GlobalRetAddr);
467   return GlobalRetAddr;
468 }