blob: 3b5023d3a75871035331faad6324d68e77371228 [file] [log] [blame]
Anton Korobeynikov33464912010-11-15 00:06:54 +00001//=======- MBlazeFrameInfo.cpp - MBlaze Frame 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 MBlaze implementation of TargetFrameInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MBlazeFrameInfo.h"
15#include "MBlazeInstrInfo.h"
16#include "MBlazeMachineFunction.h"
Wesley Peckeb133822010-12-12 20:52:31 +000017#include "InstPrinter/MBlazeInstPrinter.h"
Anton Korobeynikov33464912010-11-15 00:06:54 +000018#include "llvm/Function.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineModuleInfo.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetOptions.h"
26#include "llvm/Support/CommandLine.h"
27
28using namespace llvm;
29
Wesley Peckeb133822010-12-12 20:52:31 +000030namespace llvm {
31 cl::opt<bool> DisableStackAdjust(
32 "disable-mblaze-stack-adjust",
33 cl::init(false),
34 cl::desc("Disable MBlaze stack layout adjustment."),
35 cl::Hidden);
36}
37
Anton Korobeynikov33464912010-11-15 00:06:54 +000038//===----------------------------------------------------------------------===//
39//
40// Stack Frame Processing methods
41// +----------------------------+
42//
43// The stack is allocated decrementing the stack pointer on
44// the first instruction of a function prologue. Once decremented,
45// all stack references are are done through a positive offset
46// from the stack/frame pointer, so the stack is considered
47// to grow up.
48//
49//===----------------------------------------------------------------------===//
50
Wesley Peckeb133822010-12-12 20:52:31 +000051static void analyzeFrameIndexes(MachineFunction &MF) {
52 if (DisableStackAdjust) return;
53
54 MachineFrameInfo *MFI = MF.getFrameInfo();
55 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
56 const MachineRegisterInfo &MRI = MF.getRegInfo();
57
58 MachineRegisterInfo::livein_iterator LII = MRI.livein_begin();
59 MachineRegisterInfo::livein_iterator LIE = MRI.livein_end();
60 const SmallVector<int, 16> &LiveInFI = MBlazeFI->getLiveIn();
61 SmallVector<MachineInstr*, 16> EraseInstr;
62
63 MachineBasicBlock *MBB = MF.getBlockNumbered(0);
64 MachineBasicBlock::iterator MIB = MBB->begin();
65 MachineBasicBlock::iterator MIE = MBB->end();
66
67 int StackAdjust = 0;
68 int StackOffset = -28;
69 for (unsigned i = 0, e = LiveInFI.size(); i < e; ++i) {
70 for (MachineBasicBlock::iterator I=MIB; I != MIE; ++I) {
71 if (I->getOpcode() != MBlaze::LWI || I->getNumOperands() != 3 ||
72 !I->getOperand(1).isFI() || !I->getOperand(0).isReg() ||
73 I->getOperand(1).getIndex() != LiveInFI[i]) continue;
74
75 unsigned FIReg = I->getOperand(0).getReg();
76 MachineBasicBlock::iterator SI = I;
77 for (SI++; SI != MIE; ++SI) {
78 if (!SI->getOperand(0).isReg()) continue;
79 if (!SI->getOperand(1).isFI()) continue;
80 if (SI->getOpcode() != MBlaze::SWI) continue;
81
82 int FI = SI->getOperand(1).getIndex();
83 if (SI->getOperand(0).getReg() != FIReg) continue;
84 if (MFI->isFixedObjectIndex(FI)) continue;
85 if (MFI->getObjectSize(FI) != 4) continue;
86 if (SI->getOperand(0).isDef()) break;
87
88 if (SI->getOperand(0).isKill())
89 EraseInstr.push_back(I);
90 EraseInstr.push_back(SI);
91 MBlazeFI->recordLoadArgsFI(FI, StackOffset);
92 StackOffset -= 4;
93 StackAdjust += 4;
94 break;
95 }
96 }
97 }
98
99 for (MachineBasicBlock::iterator I=MBB->begin(), E=MBB->end(); I != E; ++I) {
100 if (I->getOpcode() != MBlaze::SWI || I->getNumOperands() != 3 ||
101 !I->getOperand(1).isFI() || !I->getOperand(0).isReg() ||
102 I->getOperand(1).getIndex() < 0) continue;
103
104 unsigned FIReg = 0;
105 for (MachineRegisterInfo::livein_iterator LI = LII; LI != LIE; ++LI) {
106 if (I->getOperand(0).getReg() == LI->first) {
107 FIReg = LI->first;
108 break;
109 }
110 }
111
112 if (FIReg) {
113 int FI = I->getOperand(1).getIndex();
114 MBlazeFI->recordLiveIn(FI);
115
116 StackAdjust += 4;
117 switch (FIReg) {
118 default: llvm_unreachable("invalid incoming parameter!");
119 case MBlaze::R5: MBlazeFI->recordLoadArgsFI(FI, -4); break;
120 case MBlaze::R6: MBlazeFI->recordLoadArgsFI(FI, -8); break;
121 case MBlaze::R7: MBlazeFI->recordLoadArgsFI(FI, -12); break;
122 case MBlaze::R8: MBlazeFI->recordLoadArgsFI(FI, -16); break;
123 case MBlaze::R9: MBlazeFI->recordLoadArgsFI(FI, -20); break;
124 case MBlaze::R10: MBlazeFI->recordLoadArgsFI(FI, -24); break;
125 }
126 }
127 }
128
129 for (int i = 0, e = EraseInstr.size(); i < e; ++i)
130 MBB->erase(EraseInstr[i]);
131
132 MBlazeFI->setStackAdjust(StackAdjust);
133}
134
Wesley Peckdc9d87a2010-12-15 20:27:28 +0000135static void interruptFrameLayout(MachineFunction &MF) {
136 const Function *F = MF.getFunction();
137 llvm::CallingConv::ID CallConv = F->getCallingConv();
138
139 // If this function is not using either the interrupt_handler
140 // calling convention or the save_volatiles calling convention
141 // then we don't need to do any additional frame layout.
142 if (CallConv != llvm::CallingConv::MBLAZE_INTR &&
143 CallConv != llvm::CallingConv::MBLAZE_SVOL)
144 return;
145
146 MachineFrameInfo *MFI = MF.getFrameInfo();
147 const MachineRegisterInfo &MRI = MF.getRegInfo();
148 const MBlazeInstrInfo &TII =
149 *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
150
151 // Determine if the calling convention is the interrupt_handler
152 // calling convention. Some pieces of the prologue and epilogue
153 // only need to be emitted if we are lowering and interrupt handler.
154 bool isIntr = CallConv == llvm::CallingConv::MBLAZE_INTR;
155
156 // Determine where to put prologue and epilogue additions
157 MachineBasicBlock &MENT = MF.front();
158 MachineBasicBlock &MEXT = MF.back();
159
160 MachineBasicBlock::iterator MENTI = MENT.begin();
161 MachineBasicBlock::iterator MEXTI = prior(MEXT.end());
162
163 DebugLoc ENTDL = MENTI != MENT.end() ? MENTI->getDebugLoc() : DebugLoc();
164 DebugLoc EXTDL = MEXTI != MEXT.end() ? MEXTI->getDebugLoc() : DebugLoc();
165
166 // Store the frame indexes generated during prologue additions for use
167 // when we are generating the epilogue additions.
168 SmallVector<int, 10> VFI;
169
170 // Build the prologue SWI for R3 - R12 if needed. Note that R11 must
171 // always have a SWI because it is used when processing RMSR.
172 for (unsigned r = MBlaze::R3; r <= MBlaze::R12; ++r) {
173 if (!MRI.isPhysRegUsed(r) && !(isIntr && r == MBlaze::R11)) continue;
174
175 int FI = MFI->CreateStackObject(4,4,false,false);
176 VFI.push_back(FI);
177
178 BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), r)
179 .addFrameIndex(FI).addImm(0);
180 }
181
182 // Build the prologue SWI for R17, R18
183 int R17FI = MFI->CreateStackObject(4,4,false,false);
184 int R18FI = MFI->CreateStackObject(4,4,false,false);
185
186 BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R17)
187 .addFrameIndex(R17FI).addImm(0);
188
189 BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R18)
190 .addFrameIndex(R18FI).addImm(0);
191
192 // Buid the prologue SWI and the epilogue LWI for RMSR if needed
193 if (isIntr) {
194 int MSRFI = MFI->CreateStackObject(4,4,false,false);
195 BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::MFS), MBlaze::R11)
196 .addReg(MBlaze::RMSR);
197 BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R11)
198 .addFrameIndex(MSRFI).addImm(0);
199
200 BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R11)
201 .addFrameIndex(MSRFI).addImm(0);
202 BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::MTS), MBlaze::RMSR)
203 .addReg(MBlaze::R11);
204 }
205
206 // Build the epilogue LWI for R17, R18
207 BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R18)
208 .addFrameIndex(R18FI).addImm(0);
209
210 BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R17)
211 .addFrameIndex(R17FI).addImm(0);
212
213 // Build the epilogue LWI for R3 - R12 if needed
214 for (unsigned r = MBlaze::R12, i = VFI.size(); r >= MBlaze::R3; --r) {
215 if (!MRI.isPhysRegUsed(r)) continue;
216 BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), r)
217 .addFrameIndex(VFI[--i]).addImm(0);
218 }
219}
220
Wesley Peckeb133822010-12-12 20:52:31 +0000221static void determineFrameLayout(MachineFunction &MF) {
222 MachineFrameInfo *MFI = MF.getFrameInfo();
223 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
224
225 // Replace the dummy '0' SPOffset by the negative offsets, as explained on
226 // LowerFORMAL_ARGUMENTS. Leaving '0' for while is necessary to avoid
227 // the approach done by calculateFrameObjectOffsets to the stack frame.
228 MBlazeFI->adjustLoadArgsFI(MFI);
229 MBlazeFI->adjustStoreVarArgsFI(MFI);
230
231 // Get the number of bytes to allocate from the FrameInfo
232 unsigned FrameSize = MFI->getStackSize();
233 FrameSize -= MBlazeFI->getStackAdjust();
234
235 // Get the alignments provided by the target, and the maximum alignment
236 // (if any) of the fixed frame objects.
237 // unsigned MaxAlign = MFI->getMaxAlignment();
238 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
239 unsigned AlignMask = TargetAlign - 1;
240
241 // Make sure the frame is aligned.
242 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
243 MFI->setStackSize(FrameSize);
244}
245
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000246// hasFP - Return true if the specified function should have a dedicated frame
247// pointer register. This is true if the function has variable sized allocas or
248// if frame pointer elimination is disabled.
249bool MBlazeFrameInfo::hasFP(const MachineFunction &MF) const {
250 const MachineFrameInfo *MFI = MF.getFrameInfo();
251 return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects();
252}
253
Anton Korobeynikov33464912010-11-15 00:06:54 +0000254void MBlazeFrameInfo::emitPrologue(MachineFunction &MF) const {
255 MachineBasicBlock &MBB = MF.front();
256 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000257 const MBlazeInstrInfo &TII =
258 *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
259 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
260 MachineBasicBlock::iterator MBBI = MBB.begin();
261 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
262
Wesley Peckdc9d87a2010-12-15 20:27:28 +0000263 llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
264 bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
265
Wesley Peckeb133822010-12-12 20:52:31 +0000266 // Determine the correct frame layout
267 determineFrameLayout(MF);
Anton Korobeynikov33464912010-11-15 00:06:54 +0000268
269 // Get the number of bytes to allocate from the FrameInfo.
270 unsigned StackSize = MFI->getStackSize();
271
272 // No need to allocate space on the stack.
Wesley Peckdc9d87a2010-12-15 20:27:28 +0000273 if (StackSize == 0 && !MFI->adjustsStack() && !requiresRA) return;
Anton Korobeynikov33464912010-11-15 00:06:54 +0000274
275 int FPOffset = MBlazeFI->getFPStackOffset();
276 int RAOffset = MBlazeFI->getRAStackOffset();
277
278 // Adjust stack : addi R1, R1, -imm
Wesley Pecka7c7b9d2010-12-12 22:02:31 +0000279 BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADDIK), MBlaze::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000280 .addReg(MBlaze::R1).addImm(-StackSize);
281
Anton Korobeynikov33464912010-11-15 00:06:54 +0000282 // swi R15, R1, stack_loc
Wesley Peckdc9d87a2010-12-15 20:27:28 +0000283 if (MFI->adjustsStack() || requiresRA) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000284 BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
285 .addReg(MBlaze::R15).addReg(MBlaze::R1).addImm(RAOffset);
286 }
287
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000288 if (hasFP(MF)) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000289 // swi R19, R1, stack_loc
290 BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
291 .addReg(MBlaze::R19).addReg(MBlaze::R1).addImm(FPOffset);
292
293 // add R19, R1, R0
294 BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADD), MBlaze::R19)
295 .addReg(MBlaze::R1).addReg(MBlaze::R0);
296 }
297}
298
299void MBlazeFrameInfo::emitEpilogue(MachineFunction &MF,
300 MachineBasicBlock &MBB) const {
301 MachineBasicBlock::iterator MBBI = prior(MBB.end());
302 MachineFrameInfo *MFI = MF.getFrameInfo();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000303 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000304 const MBlazeInstrInfo &TII =
305 *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
306
307 DebugLoc dl = MBBI->getDebugLoc();
308
Wesley Peckdc9d87a2010-12-15 20:27:28 +0000309 llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
310 bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
311
Anton Korobeynikov33464912010-11-15 00:06:54 +0000312 // Get the FI's where RA and FP are saved.
313 int FPOffset = MBlazeFI->getFPStackOffset();
314 int RAOffset = MBlazeFI->getRAStackOffset();
315
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000316 if (hasFP(MF)) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000317 // add R1, R19, R0
318 BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADD), MBlaze::R1)
319 .addReg(MBlaze::R19).addReg(MBlaze::R0);
320
321 // lwi R19, R1, stack_loc
322 BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R19)
323 .addReg(MBlaze::R1).addImm(FPOffset);
324 }
325
Anton Korobeynikov33464912010-11-15 00:06:54 +0000326 // lwi R15, R1, stack_loc
Wesley Peckdc9d87a2010-12-15 20:27:28 +0000327 if (MFI->adjustsStack() || requiresRA) {
Anton Korobeynikov33464912010-11-15 00:06:54 +0000328 BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R15)
329 .addReg(MBlaze::R1).addImm(RAOffset);
330 }
331
332 // Get the number of bytes from FrameInfo
333 int StackSize = (int) MFI->getStackSize();
Anton Korobeynikov33464912010-11-15 00:06:54 +0000334
Anton Korobeynikov33464912010-11-15 00:06:54 +0000335 // addi R1, R1, imm
336 if (StackSize) {
Wesley Pecka7c7b9d2010-12-12 22:02:31 +0000337 BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADDIK), MBlaze::R1)
Anton Korobeynikov33464912010-11-15 00:06:54 +0000338 .addReg(MBlaze::R1).addImm(StackSize);
339 }
340}
Wesley Peck8397be02010-12-09 03:42:04 +0000341
342void MBlazeFrameInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, RegScavenger *RS)
343 const {
344 MachineFrameInfo *MFI = MF.getFrameInfo();
345 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
Wesley Peckdc9d87a2010-12-15 20:27:28 +0000346 llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
347 bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
Wesley Peck8397be02010-12-09 03:42:04 +0000348
Wesley Peckdc9d87a2010-12-15 20:27:28 +0000349 if (MFI->adjustsStack() || requiresRA) {
Wesley Peck8397be02010-12-09 03:42:04 +0000350 MBlazeFI->setRAStackOffset(0);
351 MFI->CreateFixedObject(4,0,true);
352 }
353
354 if (hasFP(MF)) {
355 MBlazeFI->setFPStackOffset(4);
356 MFI->CreateFixedObject(4,4,true);
357 }
Wesley Peckeb133822010-12-12 20:52:31 +0000358
Wesley Peckdc9d87a2010-12-15 20:27:28 +0000359 interruptFrameLayout(MF);
Wesley Peckeb133822010-12-12 20:52:31 +0000360 analyzeFrameIndexes(MF);
Wesley Peck8397be02010-12-09 03:42:04 +0000361}