LLVM API Documentation

MipsFrameLowering.cpp
Go to the documentation of this file.
00001 //===-- MipsFrameLowering.cpp - Mips Frame Information --------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the Mips implementation of TargetFrameLowering class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MipsFrameLowering.h"
00015 #include "MCTargetDesc/MipsBaseInfo.h"
00016 #include "MipsAnalyzeImmediate.h"
00017 #include "MipsInstrInfo.h"
00018 #include "MipsMachineFunction.h"
00019 #include "MipsTargetMachine.h"
00020 #include "llvm/CodeGen/MachineFrameInfo.h"
00021 #include "llvm/CodeGen/MachineFunction.h"
00022 #include "llvm/CodeGen/MachineInstrBuilder.h"
00023 #include "llvm/CodeGen/MachineModuleInfo.h"
00024 #include "llvm/CodeGen/MachineRegisterInfo.h"
00025 #include "llvm/IR/DataLayout.h"
00026 #include "llvm/IR/Function.h"
00027 #include "llvm/Support/CommandLine.h"
00028 #include "llvm/Target/TargetOptions.h"
00029 
00030 using namespace llvm;
00031 
00032 
00033 //===----------------------------------------------------------------------===//
00034 //
00035 // Stack Frame Processing methods
00036 // +----------------------------+
00037 //
00038 // The stack is allocated decrementing the stack pointer on
00039 // the first instruction of a function prologue. Once decremented,
00040 // all stack references are done thought a positive offset
00041 // from the stack/frame pointer, so the stack is considering
00042 // to grow up! Otherwise terrible hacks would have to be made
00043 // to get this stack ABI compliant :)
00044 //
00045 //  The stack frame required by the ABI (after call):
00046 //  Offset
00047 //
00048 //  0                 ----------
00049 //  4                 Args to pass
00050 //  .                 saved $GP  (used in PIC)
00051 //  .                 Alloca allocations
00052 //  .                 Local Area
00053 //  .                 CPU "Callee Saved" Registers
00054 //  .                 saved FP
00055 //  .                 saved RA
00056 //  .                 FPU "Callee Saved" Registers
00057 //  StackSize         -----------
00058 //
00059 // Offset - offset from sp after stack allocation on function prologue
00060 //
00061 // The sp is the stack pointer subtracted/added from the stack size
00062 // at the Prologue/Epilogue
00063 //
00064 // References to the previous stack (to obtain arguments) are done
00065 // with offsets that exceeds the stack size: (stacksize+(4*(num_arg-1))
00066 //
00067 // Examples:
00068 // - reference to the actual stack frame
00069 //   for any local area var there is smt like : FI >= 0, StackOffset: 4
00070 //     sw REGX, 4(SP)
00071 //
00072 // - reference to previous stack frame
00073 //   suppose there's a load to the 5th arguments : FI < 0, StackOffset: 16.
00074 //   The emitted instruction will be something like:
00075 //     lw REGX, 16+StackSize(SP)
00076 //
00077 // Since the total stack size is unknown on LowerFormalArguments, all
00078 // stack references (ObjectOffset) created to reference the function
00079 // arguments, are negative numbers. This way, on eliminateFrameIndex it's
00080 // possible to detect those references and the offsets are adjusted to
00081 // their real location.
00082 //
00083 //===----------------------------------------------------------------------===//
00084 
00085 const MipsFrameLowering *MipsFrameLowering::create(const MipsSubtarget &ST) {
00086   if (ST.inMips16Mode())
00087     return llvm::createMips16FrameLowering(ST);
00088 
00089   return llvm::createMipsSEFrameLowering(ST);
00090 }
00091 
00092 // hasFP - Return true if the specified function should have a dedicated frame
00093 // pointer register.  This is true if the function has variable sized allocas or
00094 // if frame pointer elimination is disabled.
00095 bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
00096   const MachineFrameInfo *MFI = MF.getFrameInfo();
00097   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
00098       MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
00099 }
00100 
00101 uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const {
00102   const MachineFrameInfo *MFI = MF.getFrameInfo();
00103   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
00104 
00105   int64_t Offset = 0;
00106 
00107   // Iterate over fixed sized objects.
00108   for (int I = MFI->getObjectIndexBegin(); I != 0; ++I)
00109     Offset = std::max(Offset, -MFI->getObjectOffset(I));
00110 
00111   // Conservatively assume all callee-saved registers will be saved.
00112   for (const MCPhysReg *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
00113     unsigned Size = TRI.getMinimalPhysRegClass(*R)->getSize();
00114     Offset = RoundUpToAlignment(Offset + Size, Size);
00115   }
00116 
00117   unsigned MaxAlign = MFI->getMaxAlignment();
00118 
00119   // Check that MaxAlign is not zero if there is a stack object that is not a
00120   // callee-saved spill.
00121   assert(!MFI->getObjectIndexEnd() || MaxAlign);
00122 
00123   // Iterate over other objects.
00124   for (unsigned I = 0, E = MFI->getObjectIndexEnd(); I != E; ++I)
00125     Offset = RoundUpToAlignment(Offset + MFI->getObjectSize(I), MaxAlign);
00126 
00127   // Call frame.
00128   if (MFI->adjustsStack() && hasReservedCallFrame(MF))
00129     Offset = RoundUpToAlignment(Offset + MFI->getMaxCallFrameSize(),
00130                                 std::max(MaxAlign, getStackAlignment()));
00131 
00132   return RoundUpToAlignment(Offset, getStackAlignment());
00133 }