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