blob: 949bd4fb2c6765f4d29fa3938b923d805fc2a847 [file] [log] [blame]
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001//=====- AlphaFrameLowering.cpp - Alpha Frame Information ------*- C++ -*-====//
Anton Korobeynikov33464912010-11-15 00:06:54 +00002//
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//
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000010// This file contains the Alpha implementation of TargetFrameLowering class.
Anton Korobeynikov33464912010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000014#include "AlphaFrameLowering.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000015#include "AlphaInstrInfo.h"
16#include "AlphaMachineFunctionInfo.h"
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/ADT/Twine.h"
22
23using namespace llvm;
24
25static long getUpper16(long l) {
26 long y = l / Alpha::IMM_MULT;
27 if (l % Alpha::IMM_MULT > Alpha::IMM_HIGH)
28 ++y;
29 return y;
30}
31
32static long getLower16(long l) {
33 long h = getUpper16(l);
34 return l - h * Alpha::IMM_MULT;
35}
36
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000037// hasFP - Return true if the specified function should have a dedicated frame
38// pointer register. This is true if the function has variable sized allocas or
39// if frame pointer elimination is disabled.
40//
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000041bool AlphaFrameLowering::hasFP(const MachineFunction &MF) const {
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000042 const MachineFrameInfo *MFI = MF.getFrameInfo();
43 return MFI->hasVarSizedObjects();
44}
45
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000046void AlphaFrameLowering::emitPrologue(MachineFunction &MF) const {
Anton Korobeynikov33464912010-11-15 00:06:54 +000047 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
48 MachineBasicBlock::iterator MBBI = MBB.begin();
49 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000050 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +000051
52 DebugLoc dl = (MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc());
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000053 bool FP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +000054
55 // Handle GOP offset
56 BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAHg), Alpha::R29)
57 .addGlobalAddress(MF.getFunction()).addReg(Alpha::R27).addImm(++curgpdist);
58 BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAg), Alpha::R29)
59 .addGlobalAddress(MF.getFunction()).addReg(Alpha::R29).addImm(curgpdist);
60
61 BuildMI(MBB, MBBI, dl, TII.get(Alpha::ALTENT))
62 .addGlobalAddress(MF.getFunction());
63
64 // Get the number of bytes to allocate from the FrameInfo
65 long NumBytes = MFI->getStackSize();
66
67 if (FP)
68 NumBytes += 8; //reserve space for the old FP
69
70 // Do we need to allocate space on the stack?
71 if (NumBytes == 0) return;
72
73 unsigned Align = getStackAlignment();
74 NumBytes = (NumBytes+Align-1)/Align*Align;
75
76 // Update frame info to pretend that this is part of the stack...
77 MFI->setStackSize(NumBytes);
78
79 // adjust stack pointer: r30 -= numbytes
80 NumBytes = -NumBytes;
81 if (NumBytes >= Alpha::IMM_LOW) {
82 BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
83 .addReg(Alpha::R30);
84 } else if (getUpper16(NumBytes) >= Alpha::IMM_LOW) {
85 BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAH), Alpha::R30)
86 .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
87 BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30)
88 .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
89 } else {
90 report_fatal_error("Too big a stack frame at " + Twine(NumBytes));
91 }
92
93 // Now if we need to, save the old FP and set the new
94 if (FP) {
95 BuildMI(MBB, MBBI, dl, TII.get(Alpha::STQ))
96 .addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
97 // This must be the last instr in the prolog
98 BuildMI(MBB, MBBI, dl, TII.get(Alpha::BISr), Alpha::R15)
99 .addReg(Alpha::R30).addReg(Alpha::R30);
100 }
101
102}
103
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000104void AlphaFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikov33464912010-11-15 00:06:54 +0000105 MachineBasicBlock &MBB) const {
106 const MachineFrameInfo *MFI = MF.getFrameInfo();
107 MachineBasicBlock::iterator MBBI = prior(MBB.end());
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000108 const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000109
110 assert((MBBI->getOpcode() == Alpha::RETDAG ||
111 MBBI->getOpcode() == Alpha::RETDAGp)
112 && "Can only insert epilog into returning blocks");
113 DebugLoc dl = MBBI->getDebugLoc();
114
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000115 bool FP = hasFP(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000116
117 // Get the number of bytes allocated from the FrameInfo...
118 long NumBytes = MFI->getStackSize();
119
120 //now if we need to, restore the old FP
121 if (FP) {
122 //copy the FP into the SP (discards allocas)
123 BuildMI(MBB, MBBI, dl, TII.get(Alpha::BISr), Alpha::R30).addReg(Alpha::R15)
124 .addReg(Alpha::R15);
125 //restore the FP
126 BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDQ), Alpha::R15)
127 .addImm(0).addReg(Alpha::R15);
128 }
129
130 if (NumBytes != 0) {
131 if (NumBytes <= Alpha::IMM_HIGH) {
132 BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
133 .addReg(Alpha::R30);
134 } else if (getUpper16(NumBytes) <= Alpha::IMM_HIGH) {
135 BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAH), Alpha::R30)
136 .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
137 BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30)
138 .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
139 } else {
140 report_fatal_error("Too big a stack frame at " + Twine(NumBytes));
141 }
142 }
143}