LCOV - code coverage report
Current view: top level - lib/CodeGen - MachineFrameInfo.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 114 115 99.1 %
Date: 2017-09-14 15:23:50 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- MachineFrameInfo.cpp ---------------------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : /// \file Implements MachineFrameInfo that manages the stack frame.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/CodeGen/MachineFrameInfo.h"
      15             : 
      16             : #include "llvm/ADT/BitVector.h"
      17             : #include "llvm/CodeGen/MachineFunction.h"
      18             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      19             : #include "llvm/Support/Debug.h"
      20             : #include "llvm/Support/raw_ostream.h"
      21             : #include "llvm/Target/TargetFrameLowering.h"
      22             : #include "llvm/Target/TargetInstrInfo.h"
      23             : #include "llvm/Target/TargetRegisterInfo.h"
      24             : #include "llvm/Target/TargetSubtargetInfo.h"
      25             : #include <cassert>
      26             : 
      27             : #define DEBUG_TYPE "codegen"
      28             : 
      29             : using namespace llvm;
      30             : 
      31      317597 : void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
      32             :   if (!StackRealignable)
      33             :     assert(Align <= StackAlignment &&
      34             :            "For targets without stack realignment, Align is out of limit!");
      35      317597 :   if (MaxAlignment < Align) MaxAlignment = Align;
      36      317597 : }
      37             : 
      38             : /// Clamp the alignment if requested and emit a warning.
      39             : static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
      40             :                                            unsigned StackAlign) {
      41      155069 :   if (!ShouldClamp || Align <= StackAlign)
      42             :     return Align;
      43             :   DEBUG(dbgs() << "Warning: requested alignment " << Align
      44             :                << " exceeds the stack alignment " << StackAlign
      45             :                << " when stack realignment is off" << '\n');
      46             :   return StackAlign;
      47             : }
      48             : 
      49       78051 : int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
      50             :                                         bool isSS, const AllocaInst *Alloca,
      51             :                                         uint8_t ID) {
      52             :   assert(Size != 0 && "Cannot allocate zero size stack objects!");
      53      156102 :   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
      54      312204 :   Objects.push_back(StackObject(Size, Alignment, 0, false, isSS, Alloca,
      55       78051 :                                 !isSS, ID));
      56      156102 :   int Index = (int)Objects.size() - NumFixedObjects - 1;
      57             :   assert(Index >= 0 && "Bad frame index!");
      58       78051 :   ensureMaxAlignment(Alignment);
      59       78051 :   return Index;
      60             : }
      61             : 
      62       20864 : int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
      63             :                                              unsigned Alignment) {
      64       41728 :   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
      65       20864 :   CreateStackObject(Size, Alignment, true);
      66       41728 :   int Index = (int)Objects.size() - NumFixedObjects - 1;
      67       20864 :   ensureMaxAlignment(Alignment);
      68       20864 :   return Index;
      69             : }
      70             : 
      71         429 : int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
      72             :                                                 const AllocaInst *Alloca) {
      73         429 :   HasVarSizedObjects = true;
      74         858 :   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
      75        1287 :   Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
      76         429 :   ensureMaxAlignment(Alignment);
      77         858 :   return (int)Objects.size()-NumFixedObjects-1;
      78             : }
      79             : 
      80       29653 : int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
      81             :                                         bool Immutable, bool isAliased) {
      82             :   assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
      83             :   // The alignment of the frame index can be determined from its offset from
      84             :   // the incoming frame position.  If the frame object is at offset 32 and
      85             :   // the stack is guaranteed to be 16-byte aligned, then we know that the
      86             :   // object is 16-byte aligned. Note that unlike the non-fixed case, if the
      87             :   // stack needs realignment, we can't assume that the stack will in fact be
      88             :   // aligned.
      89       59306 :   unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
      90       59306 :   Align = clampStackAlignment(!StackRealignable, Align, StackAlignment);
      91      118612 :   Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
      92             :                                               /*isSS*/   false,
      93       59306 :                                               /*Alloca*/ nullptr, isAliased));
      94       29653 :   return -++NumFixedObjects;
      95             : }
      96             : 
      97       26072 : int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
      98             :                                                   int64_t SPOffset,
      99             :                                                   bool Immutable) {
     100       52144 :   unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
     101       52144 :   Align = clampStackAlignment(!StackRealignable, Align, StackAlignment);
     102      104288 :   Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
     103             :                                               /*isSS*/ true,
     104             :                                               /*Alloca*/ nullptr,
     105       52144 :                                               /*isAliased*/ false));
     106       26072 :   return -++NumFixedObjects;
     107             : }
     108             : 
     109     1496547 : BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
     110     1496547 :   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
     111     1496547 :   BitVector BV(TRI->getNumRegs());
     112             : 
     113             :   // Before CSI is calculated, no registers are considered pristine. They can be
     114             :   // freely used and PEI will make sure they are saved.
     115     1496547 :   if (!isCalleeSavedInfoValid())
     116             :     return BV;
     117             : 
     118      559486 :   const MachineRegisterInfo &MRI = MF.getRegInfo();
     119    10455941 :   for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
     120             :        ++CSR)
     121    19792910 :     BV.set(*CSR);
     122             : 
     123             :   // Saved CSRs are not pristine.
     124     2743533 :   for (auto &I : getCalleeSavedInfo())
     125     2271486 :     for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
     126     3780924 :       BV.reset(*S);
     127             : 
     128             :   return BV;
     129             : }
     130             : 
     131       44617 : unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
     132       44617 :   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
     133       44617 :   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
     134       44617 :   unsigned MaxAlign = getMaxAlignment();
     135       44617 :   int Offset = 0;
     136             : 
     137             :   // This code is very, very similar to PEI::calculateFrameObjectOffsets().
     138             :   // It really should be refactored to share code. Until then, changes
     139             :   // should keep in mind that there's tight coupling between the two.
     140             : 
     141       97644 :   for (int i = getObjectIndexBegin(); i != 0; ++i) {
     142        8410 :     int FixedOff = -getObjectOffset(i);
     143        8410 :     if (FixedOff > Offset) Offset = FixedOff;
     144             :   }
     145      127510 :   for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
     146       76552 :     if (isDeadObjectIndex(i))
     147         277 :       continue;
     148       75998 :     Offset += getObjectSize(i);
     149       75998 :     unsigned Align = getObjectAlignment(i);
     150             :     // Adjust to alignment boundary
     151       37999 :     Offset = (Offset+Align-1)/Align*Align;
     152             : 
     153       37999 :     MaxAlign = std::max(Align, MaxAlign);
     154             :   }
     155             : 
     156       44617 :   if (adjustsStack() && TFI->hasReservedCallFrame(MF))
     157        6035 :     Offset += getMaxCallFrameSize();
     158             : 
     159             :   // Round up the size to a multiple of the alignment.  If the function has
     160             :   // any calls or alloca's, align to the target's StackAlignment value to
     161             :   // ensure that the callee's frame or the alloca data is suitably aligned;
     162             :   // otherwise, for leaf functions, align to the TransientStackAlignment
     163             :   // value.
     164             :   unsigned StackAlign;
     165       83056 :   if (adjustsStack() || hasVarSizedObjects() ||
     166       38566 :       (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
     167        6302 :     StackAlign = TFI->getStackAlignment();
     168             :   else
     169       38315 :     StackAlign = TFI->getTransientStackAlignment();
     170             : 
     171             :   // If the frame pointer is eliminated, all frame offsets will be relative to
     172             :   // SP not FP. Align to MaxAlign so this works.
     173       44617 :   StackAlign = std::max(StackAlign, MaxAlign);
     174       44617 :   unsigned AlignMask = StackAlign - 1;
     175       44617 :   Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
     176             : 
     177       44617 :   return (unsigned)Offset;
     178             : }
     179             : 
     180       12102 : void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
     181       12102 :   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
     182       12102 :   unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
     183       12102 :   unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
     184             :   assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
     185             :          "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
     186             : 
     187       12102 :   MaxCallFrameSize = 0;
     188       52864 :   for (const MachineBasicBlock &MBB : MF) {
     189      401338 :     for (const MachineInstr &MI : MBB) {
     190      167553 :       unsigned Opcode = MI.getOpcode();
     191      167553 :       if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
     192       26716 :         unsigned Size = TII.getFrameSize(MI);
     193       26716 :         MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
     194       13358 :         AdjustsStack = true;
     195      154195 :       } else if (MI.isInlineAsm()) {
     196             :         // Some inline asm's need a stack frame, as indicated by operand 1.
     197        3478 :         unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
     198        3478 :         if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
     199           1 :           AdjustsStack = true;
     200             :       }
     201             :     }
     202             :   }
     203       12102 : }
     204             : 
     205        1416 : void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
     206        2832 :   if (Objects.empty()) return;
     207             : 
     208         260 :   const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
     209         260 :   int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
     210             : 
     211         260 :   OS << "Frame Objects:\n";
     212             : 
     213        1067 :   for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
     214        1094 :     const StackObject &SO = Objects[i];
     215        1094 :     OS << "  fi#" << (int)(i-NumFixedObjects) << ": ";
     216             : 
     217         547 :     if (SO.StackID != 0)
     218           0 :       OS << "id=" << SO.StackID << ' ';
     219             : 
     220         548 :     if (SO.Size == ~0ULL) {
     221           1 :       OS << "dead\n";
     222           1 :       continue;
     223             :     }
     224         546 :     if (SO.Size == 0)
     225           1 :       OS << "variable sized";
     226             :     else
     227         545 :       OS << "size=" << SO.Size;
     228        1092 :     OS << ", align=" << SO.Alignment;
     229             : 
     230         546 :     if (i < NumFixedObjects)
     231          22 :       OS << ", fixed";
     232         546 :     if (i < NumFixedObjects || SO.SPOffset != -1) {
     233         546 :       int64_t Off = SO.SPOffset - ValOffset;
     234         546 :       OS << ", at location [SP";
     235         546 :       if (Off > 0)
     236          27 :         OS << "+" << Off;
     237         519 :       else if (Off < 0)
     238         495 :         OS << Off;
     239         546 :       OS << "]";
     240             :     }
     241         546 :     OS << "\n";
     242             :   }
     243             : }
     244             : 
     245             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     246             : LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
     247             :   print(MF, dbgs());
     248             : }
     249             : #endif

Generated by: LCOV version 1.13