Remove unnecessary default cases in switches that cover all enum values.
[oota-llvm.git] / lib / Target / Sparc / SparcInstrInfo.cpp
1 //===- SparcInstrInfo.cpp - Sparc 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 Sparc implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SparcInstrInfo.h"
15 #include "Sparc.h"
16 #include "SparcMachineFunctionInfo.h"
17 #include "SparcSubtarget.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallVector.h"
24
25 #define GET_INSTRINFO_CTOR
26 #include "SparcGenInstrInfo.inc"
27
28 using namespace llvm;
29
30 SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST)
31   : SparcGenInstrInfo(SP::ADJCALLSTACKDOWN, SP::ADJCALLSTACKUP),
32     RI(ST, *this), Subtarget(ST) {
33 }
34
35 /// isLoadFromStackSlot - If the specified machine instruction is a direct
36 /// load from a stack slot, return the virtual or physical register number of
37 /// the destination along with the FrameIndex of the loaded stack slot.  If
38 /// not, return 0.  This predicate must return 0 if the instruction has
39 /// any side effects other than loading from the stack slot.
40 unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
41                                              int &FrameIndex) const {
42   if (MI->getOpcode() == SP::LDri ||
43       MI->getOpcode() == SP::LDFri ||
44       MI->getOpcode() == SP::LDDFri) {
45     if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
46         MI->getOperand(2).getImm() == 0) {
47       FrameIndex = MI->getOperand(1).getIndex();
48       return MI->getOperand(0).getReg();
49     }
50   }
51   return 0;
52 }
53
54 /// isStoreToStackSlot - If the specified machine instruction is a direct
55 /// store to a stack slot, return the virtual or physical register number of
56 /// the source reg along with the FrameIndex of the loaded stack slot.  If
57 /// not, return 0.  This predicate must return 0 if the instruction has
58 /// any side effects other than storing to the stack slot.
59 unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
60                                             int &FrameIndex) const {
61   if (MI->getOpcode() == SP::STri ||
62       MI->getOpcode() == SP::STFri ||
63       MI->getOpcode() == SP::STDFri) {
64     if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
65         MI->getOperand(1).getImm() == 0) {
66       FrameIndex = MI->getOperand(0).getIndex();
67       return MI->getOperand(2).getReg();
68     }
69   }
70   return 0;
71 }
72
73 static bool IsIntegerCC(unsigned CC)
74 {
75   return  (CC <= SPCC::ICC_VC);
76 }
77
78
79 static SPCC::CondCodes GetOppositeBranchCondition(SPCC::CondCodes CC)
80 {
81   switch(CC) {
82   case SPCC::ICC_NE:   return SPCC::ICC_E;
83   case SPCC::ICC_E:    return SPCC::ICC_NE;
84   case SPCC::ICC_G:    return SPCC::ICC_LE;
85   case SPCC::ICC_LE:   return SPCC::ICC_G;
86   case SPCC::ICC_GE:   return SPCC::ICC_L;
87   case SPCC::ICC_L:    return SPCC::ICC_GE;
88   case SPCC::ICC_GU:   return SPCC::ICC_LEU;
89   case SPCC::ICC_LEU:  return SPCC::ICC_GU;
90   case SPCC::ICC_CC:   return SPCC::ICC_CS;
91   case SPCC::ICC_CS:   return SPCC::ICC_CC;
92   case SPCC::ICC_POS:  return SPCC::ICC_NEG;
93   case SPCC::ICC_NEG:  return SPCC::ICC_POS;
94   case SPCC::ICC_VC:   return SPCC::ICC_VS;
95   case SPCC::ICC_VS:   return SPCC::ICC_VC;
96
97   case SPCC::FCC_U:    return SPCC::FCC_O;
98   case SPCC::FCC_O:    return SPCC::FCC_U;
99   case SPCC::FCC_G:    return SPCC::FCC_LE;
100   case SPCC::FCC_LE:   return SPCC::FCC_G;
101   case SPCC::FCC_UG:   return SPCC::FCC_ULE;
102   case SPCC::FCC_ULE:  return SPCC::FCC_UG;
103   case SPCC::FCC_L:    return SPCC::FCC_GE;
104   case SPCC::FCC_GE:   return SPCC::FCC_L;
105   case SPCC::FCC_UL:   return SPCC::FCC_UGE;
106   case SPCC::FCC_UGE:  return SPCC::FCC_UL;
107   case SPCC::FCC_LG:   return SPCC::FCC_UE;
108   case SPCC::FCC_UE:   return SPCC::FCC_LG;
109   case SPCC::FCC_NE:   return SPCC::FCC_E;
110   case SPCC::FCC_E:    return SPCC::FCC_NE;
111   }
112 }
113
114 MachineInstr *
115 SparcInstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
116                                          int FrameIx,
117                                          uint64_t Offset,
118                                          const MDNode *MDPtr,
119                                          DebugLoc dl) const {
120   MachineInstrBuilder MIB = BuildMI(MF, dl, get(SP::DBG_VALUE))
121     .addFrameIndex(FrameIx).addImm(0).addImm(Offset).addMetadata(MDPtr);
122   return &*MIB;
123 }
124
125
126 bool SparcInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
127                                    MachineBasicBlock *&TBB,
128                                    MachineBasicBlock *&FBB,
129                                    SmallVectorImpl<MachineOperand> &Cond,
130                                    bool AllowModify) const
131 {
132
133   MachineBasicBlock::iterator I = MBB.end();
134   MachineBasicBlock::iterator UnCondBrIter = MBB.end();
135   while (I != MBB.begin()) {
136     --I;
137
138     if (I->isDebugValue())
139       continue;
140
141     //When we see a non-terminator, we are done
142     if (!isUnpredicatedTerminator(I))
143       break;
144
145     //Terminator is not a branch
146     if (!I->isBranch())
147       return true;
148
149     //Handle Unconditional branches
150     if (I->getOpcode() == SP::BA) {
151       UnCondBrIter = I;
152
153       if (!AllowModify) {
154         TBB = I->getOperand(0).getMBB();
155         continue;
156       }
157
158       while (llvm::next(I) != MBB.end())
159         llvm::next(I)->eraseFromParent();
160
161       Cond.clear();
162       FBB = 0;
163
164       if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
165         TBB = 0;
166         I->eraseFromParent();
167         I = MBB.end();
168         UnCondBrIter = MBB.end();
169         continue;
170       }
171
172       TBB = I->getOperand(0).getMBB();
173       continue;
174     }
175
176     unsigned Opcode = I->getOpcode();
177     if (Opcode != SP::BCOND && Opcode != SP::FBCOND)
178       return true; //Unknown Opcode
179
180     SPCC::CondCodes BranchCode = (SPCC::CondCodes)I->getOperand(1).getImm();
181
182     if (Cond.empty()) {
183       MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();
184       if (AllowModify && UnCondBrIter != MBB.end() &&
185           MBB.isLayoutSuccessor(TargetBB)) {
186
187         //Transform the code
188         //
189         //    brCC L1
190         //    ba L2
191         // L1:
192         //    ..
193         // L2:
194         //
195         // into
196         //
197         //   brnCC L2
198         // L1:
199         //   ...
200         // L2:
201         //
202         BranchCode = GetOppositeBranchCondition(BranchCode);
203         MachineBasicBlock::iterator OldInst = I;
204         BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(Opcode))
205           .addMBB(UnCondBrIter->getOperand(0).getMBB()).addImm(BranchCode);
206         BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(SP::BA))
207           .addMBB(TargetBB);
208
209         OldInst->eraseFromParent();
210         UnCondBrIter->eraseFromParent();
211
212         UnCondBrIter = MBB.end();
213         I = MBB.end();
214         continue;
215       }
216       FBB = TBB;
217       TBB = I->getOperand(0).getMBB();
218       Cond.push_back(MachineOperand::CreateImm(BranchCode));
219       continue;
220     }
221     //FIXME: Handle subsequent conditional branches
222     //For now, we can't handle multiple conditional branches
223     return true;
224   }
225   return false;
226 }
227
228 unsigned
229 SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB,
230                              MachineBasicBlock *FBB,
231                              const SmallVectorImpl<MachineOperand> &Cond,
232                              DebugLoc DL) const {
233   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
234   assert((Cond.size() == 1 || Cond.size() == 0) &&
235          "Sparc branch conditions should have one component!");
236
237   if (Cond.empty()) {
238     assert(!FBB && "Unconditional branch with multiple successors!");
239     BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB);
240     return 1;
241   }
242
243   //Conditional branch
244   unsigned CC = Cond[0].getImm();
245
246   if (IsIntegerCC(CC))
247     BuildMI(&MBB, DL, get(SP::BCOND)).addMBB(TBB).addImm(CC);
248   else
249     BuildMI(&MBB, DL, get(SP::FBCOND)).addMBB(TBB).addImm(CC);
250   if (!FBB)
251     return 1;
252
253   BuildMI(&MBB, DL, get(SP::BA)).addMBB(FBB);
254   return 2;
255 }
256
257 unsigned SparcInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const
258 {
259   MachineBasicBlock::iterator I = MBB.end();
260   unsigned Count = 0;
261   while (I != MBB.begin()) {
262     --I;
263
264     if (I->isDebugValue())
265       continue;
266
267     if (I->getOpcode() != SP::BA
268         && I->getOpcode() != SP::BCOND
269         && I->getOpcode() != SP::FBCOND)
270       break; // Not a branch
271
272     I->eraseFromParent();
273     I = MBB.end();
274     ++Count;
275   }
276   return Count;
277 }
278
279 void SparcInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
280                                  MachineBasicBlock::iterator I, DebugLoc DL,
281                                  unsigned DestReg, unsigned SrcReg,
282                                  bool KillSrc) const {
283   if (SP::IntRegsRegClass.contains(DestReg, SrcReg))
284     BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0)
285       .addReg(SrcReg, getKillRegState(KillSrc));
286   else if (SP::FPRegsRegClass.contains(DestReg, SrcReg))
287     BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg)
288       .addReg(SrcReg, getKillRegState(KillSrc));
289   else if (SP::DFPRegsRegClass.contains(DestReg, SrcReg))
290     BuildMI(MBB, I, DL, get(Subtarget.isV9() ? SP::FMOVD : SP::FpMOVD), DestReg)
291       .addReg(SrcReg, getKillRegState(KillSrc));
292   else
293     llvm_unreachable("Impossible reg-to-reg copy");
294 }
295
296 void SparcInstrInfo::
297 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
298                     unsigned SrcReg, bool isKill, int FI,
299                     const TargetRegisterClass *RC,
300                     const TargetRegisterInfo *TRI) const {
301   DebugLoc DL;
302   if (I != MBB.end()) DL = I->getDebugLoc();
303
304   // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
305   if (RC == SP::IntRegsRegisterClass)
306     BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
307       .addReg(SrcReg, getKillRegState(isKill));
308   else if (RC == SP::FPRegsRegisterClass)
309     BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
310       .addReg(SrcReg,  getKillRegState(isKill));
311   else if (RC == SP::DFPRegsRegisterClass)
312     BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
313       .addReg(SrcReg,  getKillRegState(isKill));
314   else
315     llvm_unreachable("Can't store this register to stack slot");
316 }
317
318 void SparcInstrInfo::
319 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
320                      unsigned DestReg, int FI,
321                      const TargetRegisterClass *RC,
322                      const TargetRegisterInfo *TRI) const {
323   DebugLoc DL;
324   if (I != MBB.end()) DL = I->getDebugLoc();
325
326   if (RC == SP::IntRegsRegisterClass)
327     BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0);
328   else if (RC == SP::FPRegsRegisterClass)
329     BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0);
330   else if (RC == SP::DFPRegsRegisterClass)
331     BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0);
332   else
333     llvm_unreachable("Can't load this register from stack slot");
334 }
335
336 unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const
337 {
338   SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>();
339   unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg();
340   if (GlobalBaseReg != 0)
341     return GlobalBaseReg;
342
343   // Insert the set of GlobalBaseReg into the first MBB of the function
344   MachineBasicBlock &FirstMBB = MF->front();
345   MachineBasicBlock::iterator MBBI = FirstMBB.begin();
346   MachineRegisterInfo &RegInfo = MF->getRegInfo();
347
348   GlobalBaseReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
349
350
351   DebugLoc dl;
352
353   BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
354   SparcFI->setGlobalBaseReg(GlobalBaseReg);
355   return GlobalBaseReg;
356 }