1 //=======- MBlazeFrameInfo.cpp - MBlaze Frame 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 MBlaze implementation of TargetFrameInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "MBlazeFrameInfo.h"
15 #include "MBlazeInstrInfo.h"
16 #include "MBlazeMachineFunction.h"
17 #include "InstPrinter/MBlazeInstPrinter.h"
18 #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"
31 cl::opt<bool> DisableStackAdjust(
32 "disable-mblaze-stack-adjust",
34 cl::desc("Disable MBlaze stack layout adjustment."),
38 //===----------------------------------------------------------------------===//
40 // Stack Frame Processing methods
41 // +----------------------------+
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
49 //===----------------------------------------------------------------------===//
51 static void analyzeFrameIndexes(MachineFunction &MF) {
52 if (DisableStackAdjust) return;
54 MachineFrameInfo *MFI = MF.getFrameInfo();
55 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
56 const MachineRegisterInfo &MRI = MF.getRegInfo();
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;
63 MachineBasicBlock *MBB = MF.getBlockNumbered(0);
64 MachineBasicBlock::iterator MIB = MBB->begin();
65 MachineBasicBlock::iterator MIE = MBB->end();
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;
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;
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;
88 if (SI->getOperand(0).isKill())
89 EraseInstr.push_back(I);
90 EraseInstr.push_back(SI);
91 MBlazeFI->recordLoadArgsFI(FI, StackOffset);
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;
105 for (MachineRegisterInfo::livein_iterator LI = LII; LI != LIE; ++LI) {
106 if (I->getOperand(0).getReg() == LI->first) {
113 int FI = I->getOperand(1).getIndex();
114 MBlazeFI->recordLiveIn(FI);
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;
129 for (int i = 0, e = EraseInstr.size(); i < e; ++i)
130 MBB->erase(EraseInstr[i]);
132 MBlazeFI->setStackAdjust(StackAdjust);
135 static void interruptFrameLayout(MachineFunction &MF) {
136 const Function *F = MF.getFunction();
137 llvm::CallingConv::ID CallConv = F->getCallingConv();
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)
146 MachineFrameInfo *MFI = MF.getFrameInfo();
147 const MachineRegisterInfo &MRI = MF.getRegInfo();
148 const MBlazeInstrInfo &TII =
149 *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
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;
156 // Determine where to put prologue and epilogue additions
157 MachineBasicBlock &MENT = MF.front();
158 MachineBasicBlock &MEXT = MF.back();
160 MachineBasicBlock::iterator MENTI = MENT.begin();
161 MachineBasicBlock::iterator MEXTI = prior(MEXT.end());
163 DebugLoc ENTDL = MENTI != MENT.end() ? MENTI->getDebugLoc() : DebugLoc();
164 DebugLoc EXTDL = MEXTI != MEXT.end() ? MEXTI->getDebugLoc() : DebugLoc();
166 // Store the frame indexes generated during prologue additions for use
167 // when we are generating the epilogue additions.
168 SmallVector<int, 10> VFI;
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;
175 int FI = MFI->CreateStackObject(4,4,false,false);
178 BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), r)
179 .addFrameIndex(FI).addImm(0);
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);
186 BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R17)
187 .addFrameIndex(R17FI).addImm(0);
189 BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R18)
190 .addFrameIndex(R18FI).addImm(0);
192 // Buid the prologue SWI and the epilogue LWI for RMSR if needed
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);
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);
206 // Build the epilogue LWI for R17, R18
207 BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R18)
208 .addFrameIndex(R18FI).addImm(0);
210 BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R17)
211 .addFrameIndex(R17FI).addImm(0);
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);
221 static void determineFrameLayout(MachineFunction &MF) {
222 MachineFrameInfo *MFI = MF.getFrameInfo();
223 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
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);
231 // Get the number of bytes to allocate from the FrameInfo
232 unsigned FrameSize = MFI->getStackSize();
233 FrameSize -= MBlazeFI->getStackAdjust();
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;
241 // Make sure the frame is aligned.
242 FrameSize = (FrameSize + AlignMask) & ~AlignMask;
243 MFI->setStackSize(FrameSize);
246 // 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.
249 bool MBlazeFrameInfo::hasFP(const MachineFunction &MF) const {
250 const MachineFrameInfo *MFI = MF.getFrameInfo();
251 return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects();
254 void MBlazeFrameInfo::emitPrologue(MachineFunction &MF) const {
255 MachineBasicBlock &MBB = MF.front();
256 MachineFrameInfo *MFI = MF.getFrameInfo();
257 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();
263 llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
264 bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
266 // Determine the correct frame layout
267 determineFrameLayout(MF);
269 // Get the number of bytes to allocate from the FrameInfo.
270 unsigned StackSize = MFI->getStackSize();
272 // No need to allocate space on the stack.
273 if (StackSize == 0 && !MFI->adjustsStack() && !requiresRA) return;
275 int FPOffset = MBlazeFI->getFPStackOffset();
276 int RAOffset = MBlazeFI->getRAStackOffset();
278 // Adjust stack : addi R1, R1, -imm
279 BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADDIK), MBlaze::R1)
280 .addReg(MBlaze::R1).addImm(-StackSize);
282 // swi R15, R1, stack_loc
283 if (MFI->adjustsStack() || requiresRA) {
284 BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
285 .addReg(MBlaze::R15).addReg(MBlaze::R1).addImm(RAOffset);
289 // swi R19, R1, stack_loc
290 BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
291 .addReg(MBlaze::R19).addReg(MBlaze::R1).addImm(FPOffset);
294 BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADD), MBlaze::R19)
295 .addReg(MBlaze::R1).addReg(MBlaze::R0);
299 void MBlazeFrameInfo::emitEpilogue(MachineFunction &MF,
300 MachineBasicBlock &MBB) const {
301 MachineBasicBlock::iterator MBBI = prior(MBB.end());
302 MachineFrameInfo *MFI = MF.getFrameInfo();
303 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
304 const MBlazeInstrInfo &TII =
305 *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
307 DebugLoc dl = MBBI->getDebugLoc();
309 llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
310 bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
312 // Get the FI's where RA and FP are saved.
313 int FPOffset = MBlazeFI->getFPStackOffset();
314 int RAOffset = MBlazeFI->getRAStackOffset();
318 BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADD), MBlaze::R1)
319 .addReg(MBlaze::R19).addReg(MBlaze::R0);
321 // lwi R19, R1, stack_loc
322 BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R19)
323 .addReg(MBlaze::R1).addImm(FPOffset);
326 // lwi R15, R1, stack_loc
327 if (MFI->adjustsStack() || requiresRA) {
328 BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R15)
329 .addReg(MBlaze::R1).addImm(RAOffset);
332 // Get the number of bytes from FrameInfo
333 int StackSize = (int) MFI->getStackSize();
337 BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADDIK), MBlaze::R1)
338 .addReg(MBlaze::R1).addImm(StackSize);
342 void MBlazeFrameInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, RegScavenger *RS)
344 MachineFrameInfo *MFI = MF.getFrameInfo();
345 MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
346 llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
347 bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
349 if (MFI->adjustsStack() || requiresRA) {
350 MBlazeFI->setRAStackOffset(0);
351 MFI->CreateFixedObject(4,0,true);
355 MBlazeFI->setFPStackOffset(4);
356 MFI->CreateFixedObject(4,4,true);
359 interruptFrameLayout(MF);
360 analyzeFrameIndexes(MF);