1 //===- IA64RegisterInfo.cpp - IA64 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 IA64 implementation of the TargetRegisterInfo class.
11 // This file is responsible for the frame pointer elimination optimization
14 //===----------------------------------------------------------------------===//
17 #include "IA64RegisterInfo.h"
18 #include "IA64InstrBuilder.h"
19 #include "IA64MachineFunctionInfo.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Type.h"
22 #include "llvm/CodeGen/ValueTypes.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineLocation.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Target/TargetFrameInfo.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/ADT/BitVector.h"
34 #include "llvm/ADT/STLExtras.h"
37 IA64RegisterInfo::IA64RegisterInfo(const TargetInstrInfo &tii)
38 : IA64GenRegisterInfo(IA64::ADJUSTCALLSTACKDOWN, IA64::ADJUSTCALLSTACKUP),
41 const unsigned* IA64RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
43 static const unsigned CalleeSavedRegs[] = {
46 return CalleeSavedRegs;
49 const TargetRegisterClass* const*
50 IA64RegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
51 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
54 return CalleeSavedRegClasses;
57 BitVector IA64RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
58 BitVector Reserved(getNumRegs());
59 Reserved.set(IA64::r0);
60 Reserved.set(IA64::r1);
61 Reserved.set(IA64::r2);
62 Reserved.set(IA64::r5);
63 Reserved.set(IA64::r12);
64 Reserved.set(IA64::r13);
65 Reserved.set(IA64::r22);
66 Reserved.set(IA64::rp);
70 //===----------------------------------------------------------------------===//
71 // Stack Frame Processing methods
72 //===----------------------------------------------------------------------===//
74 // hasFP - Return true if the specified function should have a dedicated frame
75 // pointer register. This is true if the function has variable sized allocas or
76 // if frame pointer elimination is disabled.
78 bool IA64RegisterInfo::hasFP(const MachineFunction &MF) const {
79 const MachineFrameInfo *MFI = MF.getFrameInfo();
80 return NoFramePointerElim || MFI->hasVarSizedObjects();
83 void IA64RegisterInfo::
84 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
85 MachineBasicBlock::iterator I) const {
87 // If we have a frame pointer, turn the adjcallstackup instruction into a
88 // 'sub SP, <amt>' and the adjcallstackdown instruction into 'add SP,
90 MachineInstr *Old = I;
91 unsigned Amount = Old->getOperand(0).getImm();
92 DebugLoc dl = Old->getDebugLoc();
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 = MF.getTarget().getFrameInfo()->getStackAlignment();
98 Amount = (Amount+Align-1)/Align*Align;
100 // Replace the pseudo instruction with a new instruction...
101 if (Old->getOpcode() == IA64::ADJUSTCALLSTACKDOWN) {
102 BuildMI(MBB, I, dl, TII.get(IA64::ADDIMM22), IA64::r12)
103 .addReg(IA64::r12).addImm(-Amount);
105 assert(Old->getOpcode() == IA64::ADJUSTCALLSTACKUP);
106 BuildMI(MBB, I, dl, TII.get(IA64::ADDIMM22), IA64::r12)
107 .addReg(IA64::r12).addImm(Amount);
115 void IA64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
116 int SPAdj, RegScavenger *RS)const{
117 assert(SPAdj == 0 && "Unexpected");
120 MachineInstr &MI = *II;
121 MachineBasicBlock &MBB = *MI.getParent();
122 MachineFunction &MF = *MBB.getParent();
123 DebugLoc dl = MI.getDebugLoc();
127 while (!MI.getOperand(i).isFI()) {
129 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
132 int FrameIndex = MI.getOperand(i).getIndex();
134 // choose a base register: ( hasFP? framepointer : stack pointer )
135 unsigned BaseRegister = FP ? IA64::r5 : IA64::r12;
136 // Add the base register
137 MI.getOperand(i).ChangeToRegister(BaseRegister, false);
139 // Now add the frame object offset to the offset from r1.
140 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
142 // If we're not using a Frame Pointer that has been set to the value of the
143 // SP before having the stack size subtracted from it, then add the stack size
144 // to Offset to get the correct offset.
145 Offset += MF.getFrameInfo()->getStackSize();
147 // XXX: we use 'r22' as another hack+slash temporary register here :(
148 if (Offset <= 8191 && Offset >= -8192) { // smallish offset
150 MI.getOperand(i).ChangeToRegister(IA64::r22, false);
152 BuildMI(MBB, II, dl, TII.get(IA64::ADDIMM22), IA64::r22)
153 .addReg(BaseRegister).addImm(Offset);
156 MI.getOperand(i).ChangeToRegister(IA64::r22, false);
157 BuildMI(MBB, II, dl, TII.get(IA64::MOVLIMM64), IA64::r22).addImm(Offset);
158 BuildMI(MBB, II, dl, TII.get(IA64::ADD), IA64::r22).addReg(BaseRegister)
164 void IA64RegisterInfo::emitPrologue(MachineFunction &MF) const {
165 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
166 MachineBasicBlock::iterator MBBI = MBB.begin();
167 MachineFrameInfo *MFI = MF.getFrameInfo();
169 DebugLoc dl = (MBBI != MBB.end() ?
170 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
172 // first, we handle the 'alloc' instruction, that should be right up the
173 // top of any function
174 static const unsigned RegsInOrder[96] = { // there are 96 GPRs the
176 IA64::r32, IA64::r33, IA64::r34, IA64::r35,
177 IA64::r36, IA64::r37, IA64::r38, IA64::r39, IA64::r40, IA64::r41,
178 IA64::r42, IA64::r43, IA64::r44, IA64::r45, IA64::r46, IA64::r47,
179 IA64::r48, IA64::r49, IA64::r50, IA64::r51, IA64::r52, IA64::r53,
180 IA64::r54, IA64::r55, IA64::r56, IA64::r57, IA64::r58, IA64::r59,
181 IA64::r60, IA64::r61, IA64::r62, IA64::r63, IA64::r64, IA64::r65,
182 IA64::r66, IA64::r67, IA64::r68, IA64::r69, IA64::r70, IA64::r71,
183 IA64::r72, IA64::r73, IA64::r74, IA64::r75, IA64::r76, IA64::r77,
184 IA64::r78, IA64::r79, IA64::r80, IA64::r81, IA64::r82, IA64::r83,
185 IA64::r84, IA64::r85, IA64::r86, IA64::r87, IA64::r88, IA64::r89,
186 IA64::r90, IA64::r91, IA64::r92, IA64::r93, IA64::r94, IA64::r95,
187 IA64::r96, IA64::r97, IA64::r98, IA64::r99, IA64::r100, IA64::r101,
188 IA64::r102, IA64::r103, IA64::r104, IA64::r105, IA64::r106, IA64::r107,
189 IA64::r108, IA64::r109, IA64::r110, IA64::r111, IA64::r112, IA64::r113,
190 IA64::r114, IA64::r115, IA64::r116, IA64::r117, IA64::r118, IA64::r119,
191 IA64::r120, IA64::r121, IA64::r122, IA64::r123, IA64::r124, IA64::r125,
192 IA64::r126, IA64::r127 };
194 unsigned numStackedGPRsUsed=0;
195 for (int i=0; i != 96; i++) {
196 if (MF.getRegInfo().isPhysRegUsed(RegsInOrder[i]))
197 numStackedGPRsUsed=i+1; // (i+1 and not ++ - consider fn(fp, fp, int)
200 unsigned numOutRegsUsed=MF.getInfo<IA64FunctionInfo>()->outRegsUsed;
202 // XXX FIXME : this code should be a bit more reliable (in case there _isn't_
203 // a pseudo_alloc in the MBB)
204 unsigned dstRegOfPseudoAlloc;
205 for(MBBI = MBB.begin(); /*MBBI->getOpcode() != IA64::PSEUDO_ALLOC*/; ++MBBI) {
206 assert(MBBI != MBB.end());
207 if(MBBI->getOpcode() == IA64::PSEUDO_ALLOC) {
208 dstRegOfPseudoAlloc=MBBI->getOperand(0).getReg();
213 if (MBBI != MBB.end()) dl = MBBI->getDebugLoc();
215 BuildMI(MBB, MBBI, dl, TII.get(IA64::ALLOC)).
216 addReg(dstRegOfPseudoAlloc).addImm(0).
217 addImm(numStackedGPRsUsed).addImm(numOutRegsUsed).addImm(0);
219 // Get the number of bytes to allocate from the FrameInfo
220 unsigned NumBytes = MFI->getStackSize();
223 NumBytes += 8; // reserve space for the old FP
225 // Do we need to allocate space on the stack?
229 // Add 16 bytes at the bottom of the stack (scratch area)
230 // and round the size to a multiple of the alignment.
231 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
232 unsigned Size = 16 + (FP ? 8 : 0);
233 NumBytes = (NumBytes+Size+Align-1)/Align*Align;
235 // Update frame info to pretend that this is part of the stack...
236 MFI->setStackSize(NumBytes);
238 // adjust stack pointer: r12 -= numbytes
239 if (NumBytes <= 8191) {
240 BuildMI(MBB, MBBI, dl, TII.get(IA64::ADDIMM22),IA64::r12).addReg(IA64::r12).
242 } else { // we use r22 as a scratch register here
243 // first load the decrement into r22
244 BuildMI(MBB, MBBI, dl, TII.get(IA64::MOVLIMM64), IA64::r22).
246 // FIXME: MOVLSI32 expects a _u_32imm
247 // then add (subtract) it to r12 (stack ptr)
248 BuildMI(MBB, MBBI, dl, TII.get(IA64::ADD), IA64::r12)
249 .addReg(IA64::r12).addReg(IA64::r22);
253 // now if we need to, save the old FP and set the new
255 BuildMI(MBB, MBBI,dl,TII.get(IA64::ST8)).addReg(IA64::r12).addReg(IA64::r5);
256 // this must be the last instr in the prolog ? (XXX: why??)
257 BuildMI(MBB, MBBI, dl, TII.get(IA64::MOV), IA64::r5).addReg(IA64::r12);
262 void IA64RegisterInfo::emitEpilogue(MachineFunction &MF,
263 MachineBasicBlock &MBB) const {
264 const MachineFrameInfo *MFI = MF.getFrameInfo();
265 MachineBasicBlock::iterator MBBI = prior(MBB.end());
266 assert(MBBI->getOpcode() == IA64::RET &&
267 "Can only insert epilog into returning blocks");
268 DebugLoc dl = MBBI->getDebugLoc();
271 // Get the number of bytes allocated from the FrameInfo...
272 unsigned NumBytes = MFI->getStackSize();
274 //now if we need to, restore the old FP
276 //copy the FP into the SP (discards allocas)
277 BuildMI(MBB, MBBI, dl, TII.get(IA64::MOV), IA64::r12).addReg(IA64::r5);
279 BuildMI(MBB, MBBI, dl, TII.get(IA64::LD8), IA64::r5).addReg(IA64::r5);
283 if (NumBytes <= 8191) {
284 BuildMI(MBB, MBBI, dl, TII.get(IA64::ADDIMM22),IA64::r12).
285 addReg(IA64::r12).addImm(NumBytes);
287 BuildMI(MBB, MBBI, dl, TII.get(IA64::MOVLIMM64), IA64::r22).
289 BuildMI(MBB, MBBI, dl, TII.get(IA64::ADD), IA64::r12).addReg(IA64::r12).
295 unsigned IA64RegisterInfo::getRARegister() const {
296 llvm_unreachable("What is the return address register");
300 unsigned IA64RegisterInfo::getFrameRegister(MachineFunction &MF) const {
301 return hasFP(MF) ? IA64::r5 : IA64::r12;
304 unsigned IA64RegisterInfo::getEHExceptionRegister() const {
305 llvm_unreachable("What is the exception register");
309 unsigned IA64RegisterInfo::getEHHandlerRegister() const {
310 llvm_unreachable("What is the exception handler register");
314 int IA64RegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
315 llvm_unreachable("What is the dwarf register number");
319 #include "IA64GenRegisterInfo.inc"