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(MipsTargetMachine &TM,
00086                                                    const MipsSubtarget &ST) {
00087   if (TM.getSubtargetImpl()->inMips16Mode())
00088     return llvm::createMips16FrameLowering(ST);
00089 
00090   return llvm::createMipsSEFrameLowering(ST);
00091 }
00092 
00093 // hasFP - Return true if the specified function should have a dedicated frame
00094 // pointer register.  This is true if the function has variable sized allocas or
00095 // if frame pointer elimination is disabled.
00096 bool MipsFrameLowering::hasFP(const MachineFunction &MF) const {
00097   const MachineFrameInfo *MFI = MF.getFrameInfo();
00098   return MF.getTarget().Options.DisableFramePointerElim(MF) ||
00099       MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
00100 }
00101 
00102 uint64_t MipsFrameLowering::estimateStackSize(const MachineFunction &MF) const {
00103   const MachineFrameInfo *MFI = MF.getFrameInfo();
00104   const TargetRegisterInfo &TRI = *MF.getTarget().getRegisterInfo();
00105 
00106   int64_t Offset = 0;
00107 
00108   // Iterate over fixed sized objects.
00109   for (int I = MFI->getObjectIndexBegin(); I != 0; ++I)
00110     Offset = std::max(Offset, -MFI->getObjectOffset(I));
00111 
00112   // Conservatively assume all callee-saved registers will be saved.
00113   for (const uint16_t *R = TRI.getCalleeSavedRegs(&MF); *R; ++R) {
00114     unsigned Size = TRI.getMinimalPhysRegClass(*R)->getSize();
00115     Offset = RoundUpToAlignment(Offset + Size, Size);
00116   }
00117 
00118   unsigned MaxAlign = MFI->getMaxAlignment();
00119 
00120   // Check that MaxAlign is not zero if there is a stack object that is not a
00121   // callee-saved spill.
00122   assert(!MFI->getObjectIndexEnd() || MaxAlign);
00123 
00124   // Iterate over other objects.
00125   for (unsigned I = 0, E = MFI->getObjectIndexEnd(); I != E; ++I)
00126     Offset = RoundUpToAlignment(Offset + MFI->getObjectSize(I), MaxAlign);
00127 
00128   // Call frame.
00129   if (MFI->adjustsStack() && hasReservedCallFrame(MF))
00130     Offset = RoundUpToAlignment(Offset + MFI->getMaxCallFrameSize(),
00131                                 std::max(MaxAlign, getStackAlignment()));
00132 
00133   return RoundUpToAlignment(Offset, getStackAlignment());
00134 }