LLVM API Documentation

NVPTXPrologEpilogPass.cpp
Go to the documentation of this file.
00001 //===-- NVPTXPrologEpilogPass.cpp - NVPTX prolog/epilog inserter ----------===//
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 is a copy of the generic LLVM PrologEpilogInserter pass, modified
00011 // to remove unneeded functionality and to handle virtual registers. Most code
00012 // here is a copy of PrologEpilogInserter.cpp.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "NVPTX.h"
00017 #include "llvm/CodeGen/MachineFrameInfo.h"
00018 #include "llvm/CodeGen/MachineFunction.h"
00019 #include "llvm/CodeGen/MachineFunctionPass.h"
00020 #include "llvm/Pass.h"
00021 #include "llvm/Support/Debug.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 #include "llvm/Target/TargetFrameLowering.h"
00024 #include "llvm/Target/TargetRegisterInfo.h"
00025 #include "llvm/Target/TargetSubtargetInfo.h"
00026 
00027 using namespace llvm;
00028 
00029 #define DEBUG_TYPE "nvptx-prolog-epilog"
00030 
00031 namespace {
00032 class NVPTXPrologEpilogPass : public MachineFunctionPass {
00033 public:
00034   static char ID;
00035   NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {}
00036 
00037   bool runOnMachineFunction(MachineFunction &MF) override;
00038 
00039 private:
00040   void calculateFrameObjectOffsets(MachineFunction &Fn);
00041 };
00042 }
00043 
00044 MachineFunctionPass *llvm::createNVPTXPrologEpilogPass() {
00045   return new NVPTXPrologEpilogPass();
00046 }
00047 
00048 char NVPTXPrologEpilogPass::ID = 0;
00049 
00050 bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) {
00051   const TargetMachine &TM = MF.getTarget();
00052   const TargetFrameLowering &TFI = *TM.getSubtargetImpl()->getFrameLowering();
00053   const TargetRegisterInfo &TRI = *TM.getSubtargetImpl()->getRegisterInfo();
00054   bool Modified = false;
00055 
00056   calculateFrameObjectOffsets(MF);
00057 
00058   for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB) {
00059     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
00060       MachineInstr *MI = I;
00061       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00062         if (!MI->getOperand(i).isFI())
00063           continue;
00064         TRI.eliminateFrameIndex(MI, 0, i, nullptr);
00065         Modified = true;
00066       }
00067     }
00068   }
00069 
00070   // Add function prolog/epilog
00071   TFI.emitPrologue(MF);
00072 
00073   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
00074     // If last instruction is a return instruction, add an epilogue
00075     if (!I->empty() && I->back().isReturn())
00076       TFI.emitEpilogue(MF, *I);
00077   }
00078 
00079   return Modified;
00080 }
00081 
00082 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
00083 static inline void
00084 AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx,
00085                   bool StackGrowsDown, int64_t &Offset,
00086                   unsigned &MaxAlign) {
00087   // If the stack grows down, add the object size to find the lowest address.
00088   if (StackGrowsDown)
00089     Offset += MFI->getObjectSize(FrameIdx);
00090 
00091   unsigned Align = MFI->getObjectAlignment(FrameIdx);
00092 
00093   // If the alignment of this object is greater than that of the stack, then
00094   // increase the stack alignment to match.
00095   MaxAlign = std::max(MaxAlign, Align);
00096 
00097   // Adjust to alignment boundary.
00098   Offset = (Offset + Align - 1) / Align * Align;
00099 
00100   if (StackGrowsDown) {
00101     DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n");
00102     MFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
00103   } else {
00104     DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n");
00105     MFI->setObjectOffset(FrameIdx, Offset);
00106     Offset += MFI->getObjectSize(FrameIdx);
00107   }
00108 }
00109 
00110 void
00111 NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
00112   const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
00113   const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
00114 
00115   bool StackGrowsDown =
00116     TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
00117 
00118   // Loop over all of the stack objects, assigning sequential addresses...
00119   MachineFrameInfo *MFI = Fn.getFrameInfo();
00120 
00121   // Start at the beginning of the local area.
00122   // The Offset is the distance from the stack top in the direction
00123   // of stack growth -- so it's always nonnegative.
00124   int LocalAreaOffset = TFI.getOffsetOfLocalArea();
00125   if (StackGrowsDown)
00126     LocalAreaOffset = -LocalAreaOffset;
00127   assert(LocalAreaOffset >= 0
00128          && "Local area offset should be in direction of stack growth");
00129   int64_t Offset = LocalAreaOffset;
00130 
00131   // If there are fixed sized objects that are preallocated in the local area,
00132   // non-fixed objects can't be allocated right at the start of local area.
00133   // We currently don't support filling in holes in between fixed sized
00134   // objects, so we adjust 'Offset' to point to the end of last fixed sized
00135   // preallocated object.
00136   for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) {
00137     int64_t FixedOff;
00138     if (StackGrowsDown) {
00139       // The maximum distance from the stack pointer is at lower address of
00140       // the object -- which is given by offset. For down growing stack
00141       // the offset is negative, so we negate the offset to get the distance.
00142       FixedOff = -MFI->getObjectOffset(i);
00143     } else {
00144       // The maximum distance from the start pointer is at the upper
00145       // address of the object.
00146       FixedOff = MFI->getObjectOffset(i) + MFI->getObjectSize(i);
00147     }
00148     if (FixedOff > Offset) Offset = FixedOff;
00149   }
00150 
00151   // NOTE: We do not have a call stack
00152 
00153   unsigned MaxAlign = MFI->getMaxAlignment();
00154 
00155   // No scavenger
00156 
00157   // FIXME: Once this is working, then enable flag will change to a target
00158   // check for whether the frame is large enough to want to use virtual
00159   // frame index registers. Functions which don't want/need this optimization
00160   // will continue to use the existing code path.
00161   if (MFI->getUseLocalStackAllocationBlock()) {
00162     unsigned Align = MFI->getLocalFrameMaxAlign();
00163 
00164     // Adjust to alignment boundary.
00165     Offset = (Offset + Align - 1) / Align * Align;
00166 
00167     DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
00168 
00169     // Resolve offsets for objects in the local block.
00170     for (unsigned i = 0, e = MFI->getLocalFrameObjectCount(); i != e; ++i) {
00171       std::pair<int, int64_t> Entry = MFI->getLocalFrameObjectMap(i);
00172       int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
00173       DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" <<
00174             FIOffset << "]\n");
00175       MFI->setObjectOffset(Entry.first, FIOffset);
00176     }
00177     // Allocate the local block
00178     Offset += MFI->getLocalFrameSize();
00179 
00180     MaxAlign = std::max(Align, MaxAlign);
00181   }
00182 
00183   // No stack protector
00184 
00185   // Then assign frame offsets to stack objects that are not used to spill
00186   // callee saved registers.
00187   for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
00188     if (MFI->isObjectPreAllocated(i) &&
00189         MFI->getUseLocalStackAllocationBlock())
00190       continue;
00191     if (MFI->isDeadObjectIndex(i))
00192       continue;
00193 
00194     AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
00195   }
00196 
00197   // No scavenger
00198 
00199   if (!TFI.targetHandlesStackFrameRounding()) {
00200     // If we have reserved argument space for call sites in the function
00201     // immediately on entry to the current function, count it as part of the
00202     // overall stack size.
00203     if (MFI->adjustsStack() && TFI.hasReservedCallFrame(Fn))
00204       Offset += MFI->getMaxCallFrameSize();
00205 
00206     // Round up the size to a multiple of the alignment.  If the function has
00207     // any calls or alloca's, align to the target's StackAlignment value to
00208     // ensure that the callee's frame or the alloca data is suitably aligned;
00209     // otherwise, for leaf functions, align to the TransientStackAlignment
00210     // value.
00211     unsigned StackAlign;
00212     if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
00213         (RegInfo->needsStackRealignment(Fn) && MFI->getObjectIndexEnd() != 0))
00214       StackAlign = TFI.getStackAlignment();
00215     else
00216       StackAlign = TFI.getTransientStackAlignment();
00217 
00218     // If the frame pointer is eliminated, all frame offsets will be relative to
00219     // SP not FP. Align to MaxAlign so this works.
00220     StackAlign = std::max(StackAlign, MaxAlign);
00221     unsigned AlignMask = StackAlign - 1;
00222     Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
00223   }
00224 
00225   // Update frame info to pretend that this is part of the stack...
00226   int64_t StackSize = Offset - LocalAreaOffset;
00227   MFI->setStackSize(StackSize);
00228 }