1 //===- AlphaRegisterInfo.cpp - Alpha Register Information -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Alpha implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "reginfo"
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"
38 AlphaRegisterInfo::AlphaRegisterInfo(const TargetInstrInfo &tii)
39 : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP),
43 static long getUpper16(long l) {
44 long y = l / Alpha::IMM_MULT;
45 if (l % Alpha::IMM_MULT > Alpha::IMM_HIGH)
50 static long getLower16(long l) {
51 long h = getUpper16(l);
52 return l - h * Alpha::IMM_MULT;
55 const unsigned* AlphaRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
57 static const unsigned CalleeSavedRegs[] = {
58 Alpha::R9, Alpha::R10,
59 Alpha::R11, Alpha::R12,
60 Alpha::R13, Alpha::R14,
64 Alpha::F8, Alpha::F9, 0
66 return CalleeSavedRegs;
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);
77 //===----------------------------------------------------------------------===//
78 // Stack Frame Processing methods
79 //===----------------------------------------------------------------------===//
81 void AlphaRegisterInfo::
82 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
83 MachineBasicBlock::iterator I) const {
84 const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
87 // If we have a frame pointer, turn the adjcallstackup instruction into a
88 // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
90 MachineInstr *Old = I;
91 uint64_t Amount = Old->getOperand(0).getImm();
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;
100 if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
101 New=BuildMI(MF, Old->getDebugLoc(), TII.get(Alpha::LDA), Alpha::R30)
102 .addImm(-Amount).addReg(Alpha::R30);
104 assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
105 New=BuildMI(MF, Old->getDebugLoc(), TII.get(Alpha::LDA), Alpha::R30)
106 .addImm(Amount).addReg(Alpha::R30);
109 // Replace the pseudo instruction with a new instruction...
117 //Alpha has a slightly funny stack:
120 //fixed locals (and spills, callee saved, etc)
126 AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
127 int SPAdj, RegScavenger *RS) const {
128 assert(SPAdj == 0 && "Unexpected");
131 MachineInstr &MI = *II;
132 MachineBasicBlock &MBB = *MI.getParent();
133 MachineFunction &MF = *MBB.getParent();
134 const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
136 bool FP = TFI->hasFP(MF);
138 while (!MI.getOperand(i).isFI()) {
140 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
143 int FrameIndex = MI.getOperand(i).getIndex();
145 // Add the base register of R30 (SP) or R15 (FP).
146 MI.getOperand(i + 1).ChangeToRegister(FP ? Alpha::R15 : Alpha::R30, false);
148 // Now add the frame object offset to the offset from the virtual frame index.
149 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
151 DEBUG(errs() << "FI: " << FrameIndex << " Offset: " << Offset << "\n");
153 Offset += MF.getFrameInfo()->getStackSize();
155 DEBUG(errs() << "Corrected Offset " << Offset
156 << " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n");
158 if (Offset > Alpha::IMM_HIGH || Offset < Alpha::IMM_LOW) {
159 DEBUG(errs() << "Unconditionally using R28 for evil purposes Offset: "
161 //so in this case, we need to use a temporary register, and move the
162 //original inst off the SP/FP
164 MI.getOperand(i + 1).ChangeToRegister(Alpha::R28, false);
165 MI.getOperand(i).ChangeToImmediate(getLower16(Offset));
167 MachineInstr* nMI=BuildMI(MF, MI.getDebugLoc(),
168 TII.get(Alpha::LDAH), Alpha::R28)
169 .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
172 MI.getOperand(i).ChangeToImmediate(Offset);
176 unsigned AlphaRegisterInfo::getRARegister() const {
180 unsigned AlphaRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
181 const TargetFrameInfo *TFI = MF.getTarget().getFrameInfo();
183 return TFI->hasFP(MF) ? Alpha::R15 : Alpha::R30;
186 unsigned AlphaRegisterInfo::getEHExceptionRegister() const {
187 llvm_unreachable("What is the exception register");
191 unsigned AlphaRegisterInfo::getEHHandlerRegister() const {
192 llvm_unreachable("What is the exception handler register");
196 int AlphaRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
197 llvm_unreachable("What is the dwarf register number");
201 #include "AlphaGenRegisterInfo.inc"
203 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
205 std::string s(RegisterDescriptors[reg].Name);