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