1f4a2713aSLionel Sambuc //===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file contains the Mips implementation of TargetFrameLowering class.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "MipsFrameLowering.h"
15f4a2713aSLionel Sambuc #include "MCTargetDesc/MipsBaseInfo.h"
16f4a2713aSLionel Sambuc #include "MipsAnalyzeImmediate.h"
17f4a2713aSLionel Sambuc #include "MipsInstrInfo.h"
18f4a2713aSLionel Sambuc #include "MipsMachineFunction.h"
19f4a2713aSLionel Sambuc #include "MipsTargetMachine.h"
20f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFrameInfo.h"
21f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunction.h"
22f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstrBuilder.h"
23f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineModuleInfo.h"
24f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h"
25f4a2713aSLionel Sambuc #include "llvm/IR/DataLayout.h"
26f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
27f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
28f4a2713aSLionel Sambuc #include "llvm/Target/TargetOptions.h"
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc using namespace llvm;
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
34f4a2713aSLionel Sambuc //
35f4a2713aSLionel Sambuc // Stack Frame Processing methods
36f4a2713aSLionel Sambuc // +----------------------------+
37f4a2713aSLionel Sambuc //
38f4a2713aSLionel Sambuc // The stack is allocated decrementing the stack pointer on
39f4a2713aSLionel Sambuc // the first instruction of a function prologue. Once decremented,
40f4a2713aSLionel Sambuc // all stack references are done thought a positive offset
41f4a2713aSLionel Sambuc // from the stack/frame pointer, so the stack is considering
42f4a2713aSLionel Sambuc // to grow up! Otherwise terrible hacks would have to be made
43f4a2713aSLionel Sambuc // to get this stack ABI compliant :)
44f4a2713aSLionel Sambuc //
45f4a2713aSLionel Sambuc //  The stack frame required by the ABI (after call):
46f4a2713aSLionel Sambuc //  Offset
47f4a2713aSLionel Sambuc //
48f4a2713aSLionel Sambuc //  0                 ----------
49f4a2713aSLionel Sambuc //  4                 Args to pass
50f4a2713aSLionel Sambuc //  .                 saved $GP  (used in PIC)
51f4a2713aSLionel Sambuc //  .                 Alloca allocations
52f4a2713aSLionel Sambuc //  .                 Local Area
53f4a2713aSLionel Sambuc //  .                 CPU "Callee Saved" Registers
54f4a2713aSLionel Sambuc //  .                 saved FP
55f4a2713aSLionel Sambuc //  .                 saved RA
56f4a2713aSLionel Sambuc //  .                 FPU "Callee Saved" Registers
57f4a2713aSLionel Sambuc //  StackSize         -----------
58f4a2713aSLionel Sambuc //
59f4a2713aSLionel Sambuc // Offset - offset from sp after stack allocation on function prologue
60f4a2713aSLionel Sambuc //
61f4a2713aSLionel Sambuc // The sp is the stack pointer subtracted/added from the stack size
62f4a2713aSLionel Sambuc // at the Prologue/Epilogue
63f4a2713aSLionel Sambuc //
64f4a2713aSLionel Sambuc // References to the previous stack (to obtain arguments) are done
65f4a2713aSLionel Sambuc // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
66f4a2713aSLionel Sambuc //
67f4a2713aSLionel Sambuc // Examples:
68f4a2713aSLionel Sambuc // - reference to the actual stack frame
69f4a2713aSLionel Sambuc //   for any local area var there is smt like : FI >= 0, StackOffset: 4
70f4a2713aSLionel Sambuc //     sw REGX, 4(SP)
71f4a2713aSLionel Sambuc //
72f4a2713aSLionel Sambuc // - reference to previous stack frame
73f4a2713aSLionel Sambuc //   suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
74f4a2713aSLionel Sambuc //   The emitted instruction will be something like:
75f4a2713aSLionel Sambuc //     lw REGX, 16+StackSize(SP)
76f4a2713aSLionel Sambuc //
77f4a2713aSLionel Sambuc // Since the total stack size is unknown on LowerFormalArguments, all
78f4a2713aSLionel Sambuc // stack references (ObjectOffset) created to reference the function
79f4a2713aSLionel Sambuc // arguments, are negative numbers. This way, on eliminateFrameIndex it's
80f4a2713aSLionel Sambuc // possible to detect those references and the offsets are adjusted to
81f4a2713aSLionel Sambuc // their real location.
82f4a2713aSLionel Sambuc //
83f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
84f4a2713aSLionel Sambuc 
create(const MipsSubtarget & ST)85*0a6a1f1dSLionel Sambuc const MipsFrameLowering *MipsFrameLowering::create(const MipsSubtarget &ST) {
86*0a6a1f1dSLionel Sambuc   if (ST.inMips16Mode())
87f4a2713aSLionel Sambuc     return llvm::createMips16FrameLowering(ST);
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc   return llvm::createMipsSEFrameLowering(ST);
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc // hasFP - Return true if the specified function should have a dedicated frame
93f4a2713aSLionel Sambuc // pointer register.  This is true if the function has variable sized allocas or
94f4a2713aSLionel Sambuc // if frame pointer elimination is disabled.
hasFP(const MachineFunction & MF) const95f4a2713aSLionel Sambuc bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
96f4a2713aSLionel Sambuc   const MachineFrameInfo *MFI = MF.getFrameInfo();
97f4a2713aSLionel Sambuc   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
98f4a2713aSLionel Sambuc       MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc 
estimateStackSize(const MachineFunction & MF) const101f4a2713aSLionel Sambuc uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const {
102f4a2713aSLionel Sambuc   const MachineFrameInfo *MFI = MF.getFrameInfo();
103*0a6a1f1dSLionel Sambuc   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc   int64_t Offset = 0;
106f4a2713aSLionel Sambuc 
107f4a2713aSLionel Sambuc   // Iterate over fixed sized objects.
108f4a2713aSLionel Sambuc   for (int I = MFI->getObjectIndexBegin(); I != 0; ++I)
109f4a2713aSLionel Sambuc     Offset = std::max(Offset, -MFI->getObjectOffset(I));
110f4a2713aSLionel Sambuc 
111f4a2713aSLionel Sambuc   // Conservatively assume all callee-saved registers will be saved.
112*0a6a1f1dSLionel Sambuc   for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
113f4a2713aSLionel Sambuc     unsigned Size = TRI.getMinimalPhysRegClass(*R)->getSize();
114f4a2713aSLionel Sambuc     Offset = RoundUpToAlignment(Offset + Size, Size);
115f4a2713aSLionel Sambuc   }
116f4a2713aSLionel Sambuc 
117f4a2713aSLionel Sambuc   unsigned MaxAlign = MFI->getMaxAlignment();
118f4a2713aSLionel Sambuc 
119f4a2713aSLionel Sambuc   // Check that MaxAlign is not zero if there is a stack object that is not a
120f4a2713aSLionel Sambuc   // callee-saved spill.
121f4a2713aSLionel Sambuc   assert(!MFI->getObjectIndexEnd() || MaxAlign);
122f4a2713aSLionel Sambuc 
123f4a2713aSLionel Sambuc   // Iterate over other objects.
124f4a2713aSLionel Sambuc   for (unsigned I = 0, E = MFI->getObjectIndexEnd(); I != E; ++I)
125f4a2713aSLionel Sambuc     Offset = RoundUpToAlignment(Offset + MFI->getObjectSize(I), MaxAlign);
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc   // Call frame.
128f4a2713aSLionel Sambuc   if (MFI->adjustsStack() && hasReservedCallFrame(MF))
129f4a2713aSLionel Sambuc     Offset = RoundUpToAlignment(Offset + MFI->getMaxCallFrameSize(),
130f4a2713aSLionel Sambuc                                 std::max(MaxAlign, getStackAlignment()));
131f4a2713aSLionel Sambuc 
132f4a2713aSLionel Sambuc   return RoundUpToAlignment(Offset, getStackAlignment());
133f4a2713aSLionel Sambuc }
134