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