Add #includes
[oota-llvm.git] / lib / Target / Sparc / SparcRegisterInfo.cpp
1 //===- SparcV8RegisterInfo.cpp - SparcV8 Register Information ---*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the SparcV8 implementation of the MRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SparcV8.h"
15 #include "SparcV8RegisterInfo.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/Type.h"
20 #include "Support/STLExtras.h"
21 #include <iostream>
22 using namespace llvm;
23
24 SparcV8RegisterInfo::SparcV8RegisterInfo()
25   : SparcV8GenRegisterInfo(V8::ADJCALLSTACKDOWN,
26                            V8::ADJCALLSTACKUP) {}
27
28 int SparcV8RegisterInfo::storeRegToStackSlot(
29   MachineBasicBlock &MBB,
30   MachineBasicBlock::iterator I,
31   unsigned SrcReg, int FrameIdx,
32   const TargetRegisterClass *RC) const
33 {
34   // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
35   if (RC == SparcV8::IntRegsRegisterClass) 
36     BuildMI (MBB, I, V8::ST, 3).addFrameIndex (FrameIdx).addSImm (0)
37       .addReg (SrcReg);
38   else if (RC == SparcV8::FPRegsRegisterClass)
39     BuildMI (MBB, I, V8::STFri, 3).addFrameIndex (FrameIdx).addSImm (0)
40       .addReg (SrcReg);
41   else if (RC == SparcV8::DFPRegsRegisterClass)
42     BuildMI (MBB, I, V8::STDFri, 3).addFrameIndex (FrameIdx).addSImm (0)
43       .addReg (SrcReg);
44   else
45     assert (0 && "Can't store this register to stack slot");
46   return 1;
47 }
48
49 int SparcV8RegisterInfo::loadRegFromStackSlot(
50   MachineBasicBlock &MBB,
51   MachineBasicBlock::iterator I,
52   unsigned DestReg, int FrameIdx,
53   const TargetRegisterClass *RC) const
54 {
55   if (RC == SparcV8::IntRegsRegisterClass) 
56     BuildMI (MBB, I, V8::LD, 2, DestReg).addFrameIndex (FrameIdx).addSImm (0);
57   else if (RC == SparcV8::FPRegsRegisterClass)
58     BuildMI (MBB, I, V8::LDFri, 2, DestReg).addFrameIndex (FrameIdx)
59       .addSImm (0);
60   else if (RC == SparcV8::DFPRegsRegisterClass)
61     BuildMI (MBB, I, V8::LDDFri, 2, DestReg).addFrameIndex (FrameIdx)
62       .addSImm (0);
63   else
64     assert (0 && "Can't load this register from stack slot");
65   return 1;
66 }
67
68 int SparcV8RegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
69                                       MachineBasicBlock::iterator I,
70                                       unsigned DestReg, unsigned SrcReg,
71                                       const TargetRegisterClass *RC) const {
72   if (RC == SparcV8::IntRegsRegisterClass) 
73     BuildMI (MBB, I, V8::ORrr, 2, DestReg).addReg (V8::G0).addReg (SrcReg);
74   else if (RC == SparcV8::FPRegsRegisterClass)
75     BuildMI (MBB, I, V8::FMOVS, 1, DestReg).addReg (SrcReg);
76   else
77     assert (0 && "Can't copy this register");
78   return 1;
79 }
80
81 void SparcV8RegisterInfo::
82 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
83                               MachineBasicBlock::iterator I) const {
84   std::cerr
85     << "Sorry, I don't know how to eliminate call frame pseudo instrs yet, in\n"
86     << __FUNCTION__ << " at " << __FILE__ << ":" << __LINE__ << "\n";
87   abort();
88 }
89
90 void
91 SparcV8RegisterInfo::eliminateFrameIndex(MachineFunction &MF,
92                                          MachineBasicBlock::iterator II) const {
93   unsigned i = 0;
94   MachineInstr &MI = *II;
95   while (!MI.getOperand(i).isFrameIndex()) {
96     ++i;
97     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
98   }
99
100   int FrameIndex = MI.getOperand(i).getFrameIndex();
101
102   // Replace frame index with a frame pointer reference
103   MI.SetMachineOperandReg (i, V8::FP);
104
105   // Addressable stack objects are accessed using neg. offsets from %fp
106   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
107                MI.getOperand(i+1).getImmedValue();
108   // note: Offset < 0
109   MI.SetMachineOperandConst (i+1, MachineOperand::MO_SignExtendedImmed, Offset);
110 }
111
112 void SparcV8RegisterInfo::
113 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {}
114
115 void SparcV8RegisterInfo::emitPrologue(MachineFunction &MF) const {
116   MachineBasicBlock &MBB = MF.front();
117   MachineFrameInfo *MFI = MF.getFrameInfo();
118
119   // Get the number of bytes to allocate from the FrameInfo
120   int NumBytes = (int) MFI->getStackSize();
121
122   // Emit the correct save instruction based on the number of bytes in the frame.
123   // Minimum stack frame size according to V8 ABI is:
124   //   16 words for register window spill
125   //    1 word for address of returned aggregate-value
126   // +  6 words for passing parameters on the stack
127   // ----------
128   //   23 words * 4 bytes per word = 92 bytes
129   NumBytes += 92;
130   // Round up to next doubleword boundary -- a double-word boundary
131   // is required by the ABI.
132   NumBytes = (NumBytes + 7) & ~7;
133   BuildMI(MBB, MBB.begin(), V8::SAVEri, 2,
134           V8::SP).addImm(-NumBytes).addReg(V8::SP);
135 }
136
137 void SparcV8RegisterInfo::emitEpilogue(MachineFunction &MF,
138                                        MachineBasicBlock &MBB) const {
139   MachineBasicBlock::iterator MBBI = prior(MBB.end());
140   assert(MBBI->getOpcode() == V8::RETL &&
141          "Can only put epilog before 'retl' instruction!");
142   BuildMI(MBB, MBBI, V8::RESTORErr, 2, V8::G0).addReg(V8::G0).addReg(V8::G0);
143 }
144
145 #include "SparcV8GenRegisterInfo.inc"
146
147 const TargetRegisterClass*
148 SparcV8RegisterInfo::getRegClassForType(const Type* Ty) const {
149   switch (Ty->getTypeID()) {
150   case Type::FloatTyID:  return &FPRegsInstance;
151   case Type::DoubleTyID: return &DFPRegsInstance;
152   case Type::LongTyID:
153   case Type::ULongTyID:  assert(0 && "Long values do not fit in registers!");
154   default:               assert(0 && "Invalid type to getClass!");
155   case Type::BoolTyID:
156   case Type::SByteTyID:
157   case Type::UByteTyID:
158   case Type::ShortTyID:
159   case Type::UShortTyID:
160   case Type::IntTyID:
161   case Type::UIntTyID:
162   case Type::PointerTyID: return &IntRegsInstance;
163   }
164 }
165