LCOV - code coverage report
Current view: top level - lib/CodeGen - MachineFrameInfo.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 112 113 99.1 %
Date: 2018-10-20 13:21:21 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/CodeGen/TargetFrameLowering.h"
      20             : #include "llvm/CodeGen/TargetInstrInfo.h"
      21             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      22             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      23             : #include "llvm/Config/llvm-config.h"
      24             : #include "llvm/Support/Debug.h"
      25             : #include "llvm/Support/raw_ostream.h"
      26             : #include <cassert>
      27             : 
      28             : #define DEBUG_TYPE "codegen"
      29             : 
      30             : using namespace llvm;
      31             : 
      32     9436119 : void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
      33             :   if (!StackRealignable)
      34             :     assert(Align <= StackAlignment &&
      35             :            "For targets without stack realignment, Align is out of limit!");
      36     9436119 :   if (MaxAlignment < Align) MaxAlignment = Align;
      37     9436119 : }
      38             : 
      39             : /// Clamp the alignment if requested and emit a warning.
      40             : static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
      41             :                                            unsigned StackAlign) {
      42     9435948 :   if (!ShouldClamp || Align <= StackAlign)
      43             :     return Align;
      44             :   LLVM_DEBUG(dbgs() << "Warning: requested alignment " << Align
      45             :                     << " exceeds the stack alignment " << StackAlign
      46             :                     << " when stack realignment is off" << '\n');
      47             :   return StackAlign;
      48             : }
      49             : 
      50     7749613 : int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
      51             :                                         bool IsSpillSlot,
      52             :                                         const AllocaInst *Alloca,
      53             :                                         uint8_t StackID) {
      54             :   assert(Size != 0 && "Cannot allocate zero size stack objects!");
      55     7749613 :   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
      56    15499226 :   Objects.push_back(StackObject(Size, Alignment, 0, false, IsSpillSlot, Alloca,
      57     7749613 :                                 !IsSpillSlot, StackID));
      58     7749613 :   int Index = (int)Objects.size() - NumFixedObjects - 1;
      59             :   assert(Index >= 0 && "Bad frame index!");
      60     7749613 :   ensureMaxAlignment(Alignment);
      61     7749613 :   return Index;
      62             : }
      63             : 
      64     1404487 : int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
      65             :                                              unsigned Alignment) {
      66     1404487 :   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
      67     1404487 :   CreateStackObject(Size, Alignment, true);
      68     1404487 :   int Index = (int)Objects.size() - NumFixedObjects - 1;
      69     1404487 :   ensureMaxAlignment(Alignment);
      70     1404487 :   return Index;
      71             : }
      72             : 
      73         576 : int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
      74             :                                                 const AllocaInst *Alloca) {
      75         576 :   HasVarSizedObjects = true;
      76         576 :   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
      77         576 :   Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
      78         576 :   ensureMaxAlignment(Alignment);
      79        1152 :   return (int)Objects.size()-NumFixedObjects-1;
      80             : }
      81             : 
      82       43172 : int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
      83             :                                         bool IsImmutable, bool IsAliased) {
      84             :   assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
      85             :   // The alignment of the frame index can be determined from its offset from
      86             :   // the incoming frame position.  If the frame object is at offset 32 and
      87             :   // the stack is guaranteed to be 16-byte aligned, then we know that the
      88             :   // object is 16-byte aligned. Note that unlike the non-fixed case, if the
      89             :   // stack needs realignment, we can't assume that the stack will in fact be
      90             :   // aligned.
      91       43172 :   unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
      92       43172 :   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
      93             :   Objects.insert(Objects.begin(),
      94       43172 :                  StackObject(Size, Alignment, SPOffset, IsImmutable,
      95             :                              /*isSpillSlot=*/false, /*Alloca=*/nullptr,
      96       43172 :                              IsAliased));
      97       43172 :   return -++NumFixedObjects;
      98             : }
      99             : 
     100      238100 : int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
     101             :                                                   int64_t SPOffset,
     102             :                                                   bool IsImmutable) {
     103      238100 :   unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
     104      238100 :   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
     105             :   Objects.insert(Objects.begin(),
     106      238100 :                  StackObject(Size, Alignment, SPOffset, IsImmutable,
     107             :                              /*IsSpillSlot=*/true, /*Alloca=*/nullptr,
     108      238100 :                              /*IsAliased=*/false));
     109      238100 :   return -++NumFixedObjects;
     110             : }
     111             : 
     112     2188462 : BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
     113     2188462 :   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
     114     2188462 :   BitVector BV(TRI->getNumRegs());
     115             : 
     116             :   // Before CSI is calculated, no registers are considered pristine. They can be
     117             :   // freely used and PEI will make sure they are saved.
     118     2188462 :   if (!isCalleeSavedInfoValid())
     119             :     return BV;
     120             : 
     121      795702 :   const MachineRegisterInfo &MRI = MF.getRegInfo();
     122    18272367 :   for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
     123             :        ++CSR)
     124    17476665 :     BV.set(*CSR);
     125             : 
     126             :   // Saved CSRs are not pristine.
     127     1461449 :   for (auto &I : getCalleeSavedInfo())
     128     3062029 :     for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
     129             :       BV.reset(*S);
     130             : 
     131             :   return BV;
     132             : }
     133             : 
     134       71586 : unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
     135       71586 :   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
     136       71586 :   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
     137       71586 :   unsigned MaxAlign = getMaxAlignment();
     138             :   int Offset = 0;
     139             : 
     140             :   // This code is very, very similar to PEI::calculateFrameObjectOffsets().
     141             :   // It really should be refactored to share code. Until then, changes
     142             :   // should keep in mind that there's tight coupling between the two.
     143             : 
     144       83170 :   for (int i = getObjectIndexBegin(); i != 0; ++i) {
     145       11584 :     int FixedOff = -getObjectOffset(i);
     146       11584 :     if (FixedOff > Offset) Offset = FixedOff;
     147             :   }
     148      125333 :   for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
     149       53747 :     if (isDeadObjectIndex(i))
     150         307 :       continue;
     151       53440 :     Offset += getObjectSize(i);
     152       53440 :     unsigned Align = getObjectAlignment(i);
     153             :     // Adjust to alignment boundary
     154       53440 :     Offset = (Offset+Align-1)/Align*Align;
     155             : 
     156       53440 :     MaxAlign = std::max(Align, MaxAlign);
     157             :   }
     158             : 
     159       71586 :   if (adjustsStack() && TFI->hasReservedCallFrame(MF))
     160        8809 :     Offset += getMaxCallFrameSize();
     161             : 
     162             :   // Round up the size to a multiple of the alignment.  If the function has
     163             :   // any calls or alloca's, align to the target's StackAlignment value to
     164             :   // ensure that the callee's frame or the alloca data is suitably aligned;
     165             :   // otherwise, for leaf functions, align to the TransientStackAlignment
     166             :   // value.
     167             :   unsigned StackAlign;
     168      134145 :   if (adjustsStack() || hasVarSizedObjects() ||
     169       63178 :       (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
     170        9587 :     StackAlign = TFI->getStackAlignment();
     171             :   else
     172       61999 :     StackAlign = TFI->getTransientStackAlignment();
     173             : 
     174             :   // If the frame pointer is eliminated, all frame offsets will be relative to
     175             :   // SP not FP. Align to MaxAlign so this works.
     176       71586 :   StackAlign = std::max(StackAlign, MaxAlign);
     177       71586 :   unsigned AlignMask = StackAlign - 1;
     178       71586 :   Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
     179             : 
     180       71586 :   return (unsigned)Offset;
     181             : }
     182             : 
     183       30816 : void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
     184       30816 :   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
     185       30816 :   unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
     186       30816 :   unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
     187             :   assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
     188             :          "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
     189             : 
     190       30816 :   MaxCallFrameSize = 0;
     191       69362 :   for (const MachineBasicBlock &MBB : MF) {
     192      368916 :     for (const MachineInstr &MI : MBB) {
     193      330370 :       unsigned Opcode = MI.getOpcode();
     194      330370 :       if (Opcode == FrameSetupOpcode || Opcode == FrameDestroyOpcode) {
     195       19086 :         unsigned Size = TII.getFrameSize(MI);
     196       19086 :         MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
     197       19086 :         AdjustsStack = true;
     198      311284 :       } else if (MI.isInlineAsm()) {
     199             :         // Some inline asm's need a stack frame, as indicated by operand 1.
     200        3760 :         unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
     201        3760 :         if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
     202           1 :           AdjustsStack = true;
     203             :       }
     204             :     }
     205             :   }
     206       30816 : }
     207             : 
     208        1889 : void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
     209        1889 :   if (Objects.empty()) return;
     210             : 
     211         394 :   const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
     212         394 :   int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
     213             : 
     214         394 :   OS << "Frame Objects:\n";
     215             : 
     216        1603 :   for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
     217         815 :     const StackObject &SO = Objects[i];
     218         815 :     OS << "  fi#" << (int)(i-NumFixedObjects) << ": ";
     219             : 
     220         815 :     if (SO.StackID != 0)
     221           0 :       OS << "id=" << static_cast<unsigned>(SO.StackID) << ' ';
     222             : 
     223         815 :     if (SO.Size == ~0ULL) {
     224           1 :       OS << "dead\n";
     225           1 :       continue;
     226             :     }
     227         814 :     if (SO.Size == 0)
     228           1 :       OS << "variable sized";
     229             :     else
     230         813 :       OS << "size=" << SO.Size;
     231         814 :     OS << ", align=" << SO.Alignment;
     232             : 
     233         814 :     if (i < NumFixedObjects)
     234          21 :       OS << ", fixed";
     235         814 :     if (i < NumFixedObjects || SO.SPOffset != -1) {
     236         814 :       int64_t Off = SO.SPOffset - ValOffset;
     237         814 :       OS << ", at location [SP";
     238         814 :       if (Off > 0)
     239          27 :         OS << "+" << Off;
     240         787 :       else if (Off < 0)
     241         679 :         OS << Off;
     242         814 :       OS << "]";
     243             :     }
     244         814 :     OS << "\n";
     245             :   }
     246             : }
     247             : 
     248             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     249             : LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
     250             :   print(MF, dbgs());
     251             : }
     252             : #endif

Generated by: LCOV version 1.13