Remove non-DebugLoc versions of BuildMI from Alpha and Cell.
[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 "AlphaGenInstrInfo.inc"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 using namespace llvm;
21
22 AlphaInstrInfo::AlphaInstrInfo()
23   : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)),
24     RI(*this) { }
25
26
27 bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI,
28                                  unsigned& sourceReg, unsigned& destReg,
29                                  unsigned& SrcSR, unsigned& DstSR) const {
30   unsigned oc = MI.getOpcode();
31   if (oc == Alpha::BISr   || 
32       oc == Alpha::CPYSS  || 
33       oc == Alpha::CPYST  ||
34       oc == Alpha::CPYSSt || 
35       oc == Alpha::CPYSTs) {
36     // or r1, r2, r2 
37     // cpys(s|t) r1 r2 r2
38     assert(MI.getNumOperands() >= 3 &&
39            MI.getOperand(0).isReg() &&
40            MI.getOperand(1).isReg() &&
41            MI.getOperand(2).isReg() &&
42            "invalid Alpha BIS instruction!");
43     if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
44       sourceReg = MI.getOperand(1).getReg();
45       destReg = MI.getOperand(0).getReg();
46       SrcSR = DstSR = 0;
47       return true;
48     }
49   }
50   return false;
51 }
52
53 unsigned 
54 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
55                                     int &FrameIndex) const {
56   switch (MI->getOpcode()) {
57   case Alpha::LDL:
58   case Alpha::LDQ:
59   case Alpha::LDBU:
60   case Alpha::LDWU:
61   case Alpha::LDS:
62   case Alpha::LDT:
63     if (MI->getOperand(1).isFI()) {
64       FrameIndex = MI->getOperand(1).getIndex();
65       return MI->getOperand(0).getReg();
66     }
67     break;
68   }
69   return 0;
70 }
71
72 unsigned 
73 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
74                                    int &FrameIndex) const {
75   switch (MI->getOpcode()) {
76   case Alpha::STL:
77   case Alpha::STQ:
78   case Alpha::STB:
79   case Alpha::STW:
80   case Alpha::STS:
81   case Alpha::STT:
82     if (MI->getOperand(1).isFI()) {
83       FrameIndex = MI->getOperand(1).getIndex();
84       return MI->getOperand(0).getReg();
85     }
86     break;
87   }
88   return 0;
89 }
90
91 static bool isAlphaIntCondCode(unsigned Opcode) {
92   switch (Opcode) {
93   case Alpha::BEQ: 
94   case Alpha::BNE: 
95   case Alpha::BGE: 
96   case Alpha::BGT: 
97   case Alpha::BLE: 
98   case Alpha::BLT: 
99   case Alpha::BLBC: 
100   case Alpha::BLBS:
101     return true;
102   default:
103     return false;
104   }
105 }
106
107 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB,
108                                       MachineBasicBlock *TBB,
109                                       MachineBasicBlock *FBB,
110                             const SmallVectorImpl<MachineOperand> &Cond) const {
111   // FIXME this should probably have a DebugLoc argument
112   DebugLoc dl = DebugLoc::getUnknownLoc();
113   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
114   assert((Cond.size() == 2 || Cond.size() == 0) && 
115          "Alpha branch conditions have two components!");
116
117   // One-way branch.
118   if (FBB == 0) {
119     if (Cond.empty())   // Unconditional branch
120       BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(TBB);
121     else                // Conditional branch
122       if (isAlphaIntCondCode(Cond[0].getImm()))
123         BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
124           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
125       else
126         BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
127           .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
128     return 1;
129   }
130   
131   // Two-way Conditional Branch.
132   if (isAlphaIntCondCode(Cond[0].getImm()))
133     BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I))
134       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
135   else
136     BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F))
137       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
138   BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(FBB);
139   return 2;
140 }
141
142 bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB,
143                                   MachineBasicBlock::iterator MI,
144                                   unsigned DestReg, unsigned SrcReg,
145                                   const TargetRegisterClass *DestRC,
146                                   const TargetRegisterClass *SrcRC) const {
147   //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n";
148   if (DestRC != SrcRC) {
149     // Not yet supported!
150     return false;
151   }
152
153   DebugLoc DL = DebugLoc::getUnknownLoc();
154   if (MI != MBB.end()) DL = MI->getDebugLoc();
155
156   if (DestRC == Alpha::GPRCRegisterClass) {
157     BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg)
158       .addReg(SrcReg)
159       .addReg(SrcReg);
160   } else if (DestRC == Alpha::F4RCRegisterClass) {
161     BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg)
162       .addReg(SrcReg)
163       .addReg(SrcReg);
164   } else if (DestRC == Alpha::F8RCRegisterClass) {
165     BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg)
166       .addReg(SrcReg)
167       .addReg(SrcReg);
168   } else {
169     // Attempt to copy register that is not GPR or FPR
170     return false;
171   }
172   
173   return true;
174 }
175
176 void
177 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
178                                     MachineBasicBlock::iterator MI,
179                                     unsigned SrcReg, bool isKill, int FrameIdx,
180                                     const TargetRegisterClass *RC) const {
181   //cerr << "Trying to store " << getPrettyName(SrcReg) << " to "
182   //     << FrameIdx << "\n";
183   //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg);
184
185   DebugLoc DL = DebugLoc::getUnknownLoc();
186   if (MI != MBB.end()) DL = MI->getDebugLoc();
187
188   if (RC == Alpha::F4RCRegisterClass)
189     BuildMI(MBB, MI, DL, get(Alpha::STS))
190       .addReg(SrcReg, false, false, isKill)
191       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
192   else if (RC == Alpha::F8RCRegisterClass)
193     BuildMI(MBB, MI, DL, get(Alpha::STT))
194       .addReg(SrcReg, false, false, isKill)
195       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
196   else if (RC == Alpha::GPRCRegisterClass)
197     BuildMI(MBB, MI, DL, get(Alpha::STQ))
198       .addReg(SrcReg, false, false, isKill)
199       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
200   else
201     abort();
202 }
203
204 void AlphaInstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
205                                        bool isKill,
206                                        SmallVectorImpl<MachineOperand> &Addr,
207                                        const TargetRegisterClass *RC,
208                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
209   unsigned Opc = 0;
210   if (RC == Alpha::F4RCRegisterClass)
211     Opc = Alpha::STS;
212   else if (RC == Alpha::F8RCRegisterClass)
213     Opc = Alpha::STT;
214   else if (RC == Alpha::GPRCRegisterClass)
215     Opc = Alpha::STQ;
216   else
217     abort();
218   DebugLoc DL = DebugLoc::getUnknownLoc();
219   MachineInstrBuilder MIB = 
220     BuildMI(MF, DL, get(Opc)).addReg(SrcReg, false, false, isKill);
221   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
222     MachineOperand &MO = Addr[i];
223     if (MO.isReg())
224       MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
225     else
226       MIB.addImm(MO.getImm());
227   }
228   NewMIs.push_back(MIB);
229 }
230
231 void
232 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
233                                         MachineBasicBlock::iterator MI,
234                                         unsigned DestReg, int FrameIdx,
235                                         const TargetRegisterClass *RC) const {
236   //cerr << "Trying to load " << getPrettyName(DestReg) << " to "
237   //     << FrameIdx << "\n";
238   DebugLoc DL = DebugLoc::getUnknownLoc();
239   if (MI != MBB.end()) DL = MI->getDebugLoc();
240
241   if (RC == Alpha::F4RCRegisterClass)
242     BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg)
243       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
244   else if (RC == Alpha::F8RCRegisterClass)
245     BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg)
246       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
247   else if (RC == Alpha::GPRCRegisterClass)
248     BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg)
249       .addFrameIndex(FrameIdx).addReg(Alpha::F31);
250   else
251     abort();
252 }
253
254 void AlphaInstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
255                                         SmallVectorImpl<MachineOperand> &Addr,
256                                         const TargetRegisterClass *RC,
257                                  SmallVectorImpl<MachineInstr*> &NewMIs) const {
258   unsigned Opc = 0;
259   if (RC == Alpha::F4RCRegisterClass)
260     Opc = Alpha::LDS;
261   else if (RC == Alpha::F8RCRegisterClass)
262     Opc = Alpha::LDT;
263   else if (RC == Alpha::GPRCRegisterClass)
264     Opc = Alpha::LDQ;
265   else
266     abort();
267   DebugLoc DL = DebugLoc::getUnknownLoc();
268   MachineInstrBuilder MIB = 
269     BuildMI(MF, DL, get(Opc), DestReg);
270   for (unsigned i = 0, e = Addr.size(); i != e; ++i) {
271     MachineOperand &MO = Addr[i];
272     if (MO.isReg())
273       MIB.addReg(MO.getReg(), MO.isDef(), MO.isImplicit());
274     else
275       MIB.addImm(MO.getImm());
276   }
277   NewMIs.push_back(MIB);
278 }
279
280 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
281                                                     MachineInstr *MI,
282                                           const SmallVectorImpl<unsigned> &Ops,
283                                                     int FrameIndex) const {
284    if (Ops.size() != 1) return NULL;
285
286    // Make sure this is a reg-reg copy.
287    unsigned Opc = MI->getOpcode();
288
289    MachineInstr *NewMI = NULL;
290    switch(Opc) {
291    default:
292      break;
293    case Alpha::BISr:
294    case Alpha::CPYSS:
295    case Alpha::CPYST:
296      if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
297        if (Ops[0] == 0) {  // move -> store
298          unsigned InReg = MI->getOperand(1).getReg();
299          bool isKill = MI->getOperand(1).isKill();
300          Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 
301            ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT);
302          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
303            .addReg(InReg, false, false, isKill)
304            .addFrameIndex(FrameIndex)
305            .addReg(Alpha::F31);
306        } else {           // load -> move
307          unsigned OutReg = MI->getOperand(0).getReg();
308          bool isDead = MI->getOperand(0).isDead();
309          Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 
310            ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT);
311          NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
312            .addReg(OutReg, true, false, false, isDead)
313            .addFrameIndex(FrameIndex)
314            .addReg(Alpha::F31);
315        }
316      }
317      break;
318    }
319   return NewMI;
320 }
321
322 static unsigned AlphaRevCondCode(unsigned Opcode) {
323   switch (Opcode) {
324   case Alpha::BEQ: return Alpha::BNE;
325   case Alpha::BNE: return Alpha::BEQ;
326   case Alpha::BGE: return Alpha::BLT;
327   case Alpha::BGT: return Alpha::BLE;
328   case Alpha::BLE: return Alpha::BGT;
329   case Alpha::BLT: return Alpha::BGE;
330   case Alpha::BLBC: return Alpha::BLBS;
331   case Alpha::BLBS: return Alpha::BLBC;
332   case Alpha::FBEQ: return Alpha::FBNE;
333   case Alpha::FBNE: return Alpha::FBEQ;
334   case Alpha::FBGE: return Alpha::FBLT;
335   case Alpha::FBGT: return Alpha::FBLE;
336   case Alpha::FBLE: return Alpha::FBGT;
337   case Alpha::FBLT: return Alpha::FBGE;
338   default:
339     assert(0 && "Unknown opcode");
340   }
341   return 0; // Not reached
342 }
343
344 // Branch analysis.
345 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
346                                    MachineBasicBlock *&FBB,
347                                    SmallVectorImpl<MachineOperand> &Cond,
348                                    bool AllowModify) const {
349   // If the block has no terminators, it just falls into the block after it.
350   MachineBasicBlock::iterator I = MBB.end();
351   if (I == MBB.begin() || !isUnpredicatedTerminator(--I))
352     return false;
353
354   // Get the last instruction in the block.
355   MachineInstr *LastInst = I;
356   
357   // If there is only one terminator instruction, process it.
358   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
359     if (LastInst->getOpcode() == Alpha::BR) {
360       TBB = LastInst->getOperand(0).getMBB();
361       return false;
362     } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I ||
363                LastInst->getOpcode() == Alpha::COND_BRANCH_F) {
364       // Block ends with fall-through condbranch.
365       TBB = LastInst->getOperand(2).getMBB();
366       Cond.push_back(LastInst->getOperand(0));
367       Cond.push_back(LastInst->getOperand(1));
368       return false;
369     }
370     // Otherwise, don't know what this is.
371     return true;
372   }
373   
374   // Get the instruction before it if it's a terminator.
375   MachineInstr *SecondLastInst = I;
376
377   // If there are three terminators, we don't know what sort of block this is.
378   if (SecondLastInst && I != MBB.begin() &&
379       isUnpredicatedTerminator(--I))
380     return true;
381   
382   // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it.
383   if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I ||
384       SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && 
385       LastInst->getOpcode() == Alpha::BR) {
386     TBB =  SecondLastInst->getOperand(2).getMBB();
387     Cond.push_back(SecondLastInst->getOperand(0));
388     Cond.push_back(SecondLastInst->getOperand(1));
389     FBB = LastInst->getOperand(0).getMBB();
390     return false;
391   }
392   
393   // If the block ends with two Alpha::BRs, handle it.  The second one is not
394   // executed, so remove it.
395   if (SecondLastInst->getOpcode() == Alpha::BR && 
396       LastInst->getOpcode() == Alpha::BR) {
397     TBB = SecondLastInst->getOperand(0).getMBB();
398     I = LastInst;
399     if (AllowModify)
400       I->eraseFromParent();
401     return false;
402   }
403
404   // Otherwise, can't handle this.
405   return true;
406 }
407
408 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
409   MachineBasicBlock::iterator I = MBB.end();
410   if (I == MBB.begin()) return 0;
411   --I;
412   if (I->getOpcode() != Alpha::BR && 
413       I->getOpcode() != Alpha::COND_BRANCH_I &&
414       I->getOpcode() != Alpha::COND_BRANCH_F)
415     return 0;
416   
417   // Remove the branch.
418   I->eraseFromParent();
419   
420   I = MBB.end();
421
422   if (I == MBB.begin()) return 1;
423   --I;
424   if (I->getOpcode() != Alpha::COND_BRANCH_I && 
425       I->getOpcode() != Alpha::COND_BRANCH_F)
426     return 1;
427   
428   // Remove the branch.
429   I->eraseFromParent();
430   return 2;
431 }
432
433 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, 
434                                 MachineBasicBlock::iterator MI) const {
435   DebugLoc DL = DebugLoc::getUnknownLoc();
436   if (MI != MBB.end()) DL = MI->getDebugLoc();
437   BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31)
438     .addReg(Alpha::R31)
439     .addReg(Alpha::R31);
440 }
441
442 bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
443   if (MBB.empty()) return false;
444   
445   switch (MBB.back().getOpcode()) {
446   case Alpha::RETDAG: // Return.
447   case Alpha::RETDAGp:
448   case Alpha::BR:     // Uncond branch.
449   case Alpha::JMP:  // Indirect branch.
450     return true;
451   default: return false;
452   }
453 }
454 bool AlphaInstrInfo::
455 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
456   assert(Cond.size() == 2 && "Invalid Alpha branch opcode!");
457   Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm()));
458   return false;
459 }
460