3eef05f751c3bf9390e31c9e8028c6460ac2e3c5
[oota-llvm.git] / lib / Target / Alpha / AlphaRegisterInfo.cpp
1 //===- AlphaRegisterInfo.cpp - Alpha Register 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 TargetRegisterInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "reginfo"
15 #include "Alpha.h"
16 #include "AlphaRegisterInfo.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Type.h"
19 #include "llvm/Function.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineLocation.h"
25 #include "llvm/Target/TargetFrameLowering.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetOptions.h"
28 #include "llvm/Target/TargetInstrInfo.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/ADT/BitVector.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include <cstdlib>
36
37 #define GET_REGINFO_MC_DESC
38 #define GET_REGINFO_TARGET_DESC
39 #include "AlphaGenRegisterInfo.inc"
40
41 using namespace llvm;
42
43 AlphaRegisterInfo::AlphaRegisterInfo(const TargetInstrInfo &tii)
44   : AlphaGenRegisterInfo(AlphaRegDesc, AlphaRegInfoDesc,
45                          Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP),
46     TII(tii) {
47 }
48
49 static long getUpper16(long l) {
50   long y = l / Alpha::IMM_MULT;
51   if (l % Alpha::IMM_MULT > Alpha::IMM_HIGH)
52     ++y;
53   return y;
54 }
55
56 static long getLower16(long l) {
57   long h = getUpper16(l);
58   return l - h * Alpha::IMM_MULT;
59 }
60
61 const unsigned* AlphaRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
62                                                                          const {
63   static const unsigned CalleeSavedRegs[] = {
64     Alpha::R9, Alpha::R10,
65     Alpha::R11, Alpha::R12,
66     Alpha::R13, Alpha::R14,
67     Alpha::F2, Alpha::F3,
68     Alpha::F4, Alpha::F5,
69     Alpha::F6, Alpha::F7,
70     Alpha::F8, Alpha::F9,  0
71   };
72   return CalleeSavedRegs;
73 }
74
75 BitVector AlphaRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
76   BitVector Reserved(getNumRegs());
77   Reserved.set(Alpha::R15);
78   Reserved.set(Alpha::R29);
79   Reserved.set(Alpha::R30);
80   Reserved.set(Alpha::R31);
81   return Reserved;
82 }
83
84 //===----------------------------------------------------------------------===//
85 // Stack Frame Processing methods
86 //===----------------------------------------------------------------------===//
87
88 void AlphaRegisterInfo::
89 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
90                               MachineBasicBlock::iterator I) const {
91   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
92
93   if (TFI->hasFP(MF)) {
94     // If we have a frame pointer, turn the adjcallstackup instruction into a
95     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
96     // <amt>'
97     MachineInstr *Old = I;
98     uint64_t Amount = Old->getOperand(0).getImm();
99     if (Amount != 0) {
100       // We need to keep the stack aligned properly.  To do this, we round the
101       // amount of space needed for the outgoing arguments up to the next
102       // alignment boundary.
103       unsigned Align = TFI->getStackAlignment();
104       Amount = (Amount+Align-1)/Align*Align;
105
106       MachineInstr *New;
107       if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
108         New=BuildMI(MF, Old->getDebugLoc(), TII.get(Alpha::LDA), Alpha::R30)
109           .addImm(-Amount).addReg(Alpha::R30);
110       } else {
111          assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
112          New=BuildMI(MF, Old->getDebugLoc(), TII.get(Alpha::LDA), Alpha::R30)
113           .addImm(Amount).addReg(Alpha::R30);
114       }
115
116       // Replace the pseudo instruction with a new instruction...
117       MBB.insert(I, New);
118     }
119   }
120
121   MBB.erase(I);
122 }
123
124 //Alpha has a slightly funny stack:
125 //Args
126 //<- incoming SP
127 //fixed locals (and spills, callee saved, etc)
128 //<- FP
129 //variable locals
130 //<- SP
131
132 void
133 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
134                                        int SPAdj, RegScavenger *RS) const {
135   assert(SPAdj == 0 && "Unexpected");
136
137   unsigned i = 0;
138   MachineInstr &MI = *II;
139   MachineBasicBlock &MBB = *MI.getParent();
140   MachineFunction &MF = *MBB.getParent();
141   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
142
143   bool FP = TFI->hasFP(MF);
144
145   while (!MI.getOperand(i).isFI()) {
146     ++i;
147     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
148   }
149
150   int FrameIndex = MI.getOperand(i).getIndex();
151
152   // Add the base register of R30 (SP) or R15 (FP).
153   MI.getOperand(i + 1).ChangeToRegister(FP ? Alpha::R15 : Alpha::R30, false);
154
155   // Now add the frame object offset to the offset from the virtual frame index.
156   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
157
158   DEBUG(errs() << "FI: " << FrameIndex << " Offset: " << Offset << "\n");
159
160   Offset += MF.getFrameInfo()->getStackSize();
161
162   DEBUG(errs() << "Corrected Offset " << Offset
163        << " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n");
164
165   if (Offset > Alpha::IMM_HIGH || Offset < Alpha::IMM_LOW) {
166     DEBUG(errs() << "Unconditionally using R28 for evil purposes Offset: "
167           << Offset << "\n");
168     //so in this case, we need to use a temporary register, and move the
169     //original inst off the SP/FP
170     //fix up the old:
171     MI.getOperand(i + 1).ChangeToRegister(Alpha::R28, false);
172     MI.getOperand(i).ChangeToImmediate(getLower16(Offset));
173     //insert the new
174     MachineInstr* nMI=BuildMI(MF, MI.getDebugLoc(),
175                               TII.get(Alpha::LDAH), Alpha::R28)
176       .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
177     MBB.insert(II, nMI);
178   } else {
179     MI.getOperand(i).ChangeToImmediate(Offset);
180   }
181 }
182
183 unsigned AlphaRegisterInfo::getRARegister() const {
184   return Alpha::R26;
185 }
186
187 unsigned AlphaRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
188   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
189
190   return TFI->hasFP(MF) ? Alpha::R15 : Alpha::R30;
191 }
192
193 unsigned AlphaRegisterInfo::getEHExceptionRegister() const {
194   llvm_unreachable("What is the exception register");
195   return 0;
196 }
197
198 unsigned AlphaRegisterInfo::getEHHandlerRegister() const {
199   llvm_unreachable("What is the exception handler register");
200   return 0;
201 }
202
203 int AlphaRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
204   llvm_unreachable("What is the dwarf register number");
205   return -1;
206 }
207
208 int AlphaRegisterInfo::getLLVMRegNum(unsigned DwarfRegNum, bool isEH) const {
209   llvm_unreachable("What is the dwarf register number");
210   return -1;
211 }
212
213 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
214 {
215   std::string s(AlphaRegDesc[reg].Name);
216   return s;
217 }