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

          Line data    Source code
       1             : //===-- llvm/lib/CodeGen/AsmPrinter/DebugHandlerBase.cpp -------*- C++ -*--===//
       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             : // Common functionality for different debug information format backends.
      11             : // LLVM currently supports DWARF and CodeView.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "DebugHandlerBase.h"
      16             : #include "llvm/ADT/Optional.h"
      17             : #include "llvm/ADT/Twine.h"
      18             : #include "llvm/CodeGen/AsmPrinter.h"
      19             : #include "llvm/CodeGen/MachineFunction.h"
      20             : #include "llvm/CodeGen/MachineInstr.h"
      21             : #include "llvm/CodeGen/MachineModuleInfo.h"
      22             : #include "llvm/IR/DebugInfo.h"
      23             : #include "llvm/MC/MCStreamer.h"
      24             : #include "llvm/Target/TargetSubtargetInfo.h"
      25             : 
      26             : using namespace llvm;
      27             : 
      28             : Optional<DbgVariableLocation>
      29          72 : DbgVariableLocation::extractFromMachineInstruction(
      30             :     const MachineInstr &Instruction) {
      31         144 :   DbgVariableLocation Location;
      32          72 :   if (!Instruction.isDebugValue())
      33             :     return None;
      34         144 :   if (!Instruction.getOperand(0).isReg())
      35             :     return None;
      36          64 :   Location.Register = Instruction.getOperand(0).getReg();
      37          64 :   Location.FragmentInfo.reset();
      38             :   // We only handle expressions generated by DIExpression::appendOffset,
      39             :   // which doesn't require a full stack machine.
      40          64 :   int64_t Offset = 0;
      41          64 :   const DIExpression *DIExpr = Instruction.getDebugExpression();
      42          64 :   auto Op = DIExpr->expr_op_begin();
      43         198 :   while (Op != DIExpr->expr_op_end()) {
      44          70 :     switch (Op->getOp()) {
      45           2 :     case dwarf::DW_OP_constu: {
      46           4 :       int Value = Op->getArg(0);
      47           2 :       ++Op;
      48           4 :       if (Op != DIExpr->expr_op_end()) {
      49           4 :         switch (Op->getOp()) {
      50           2 :         case dwarf::DW_OP_minus:
      51           2 :           Offset -= Value;
      52           2 :           break;
      53           0 :         case dwarf::DW_OP_plus:
      54           0 :           Offset += Value;
      55           0 :           break;
      56           0 :         default:
      57           0 :           continue;
      58             :         }
      59             :       }
      60             :     } break;
      61           6 :     case dwarf::DW_OP_plus_uconst:
      62          12 :       Offset += Op->getArg(0);
      63           6 :       break;
      64          16 :     case dwarf::DW_OP_LLVM_fragment:
      65          48 :       Location.FragmentInfo = {Op->getArg(1), Op->getArg(0)};
      66             :       break;
      67          11 :     case dwarf::DW_OP_deref:
      68          11 :       Location.LoadChain.push_back(Offset);
      69          11 :       Offset = 0;
      70          11 :       break;
      71           0 :     default:
      72             :       return None;
      73             :     }
      74             :     ++Op;
      75             :   }
      76             : 
      77             :   // Do one final implicit DW_OP_deref if this was an indirect DBG_VALUE
      78             :   // instruction.
      79             :   // FIXME: Replace these with DIExpression.
      80          14 :   if (Instruction.isIndirectDebugValue())
      81          14 :     Location.LoadChain.push_back(Offset);
      82             : 
      83             :   return Location;
      84             : }
      85             : 
      86      115976 : DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
      87             : 
      88             : // Each LexicalScope has first instruction and last instruction to mark
      89             : // beginning and end of a scope respectively. Create an inverse map that list
      90             : // scopes starts (and ends) with an instruction. One instruction may start (or
      91             : // end) multiple scopes. Ignore scopes that are not reachable.
      92        6188 : void DebugHandlerBase::identifyScopeMarkers() {
      93       12376 :   SmallVector<LexicalScope *, 4> WorkList;
      94        6188 :   WorkList.push_back(LScopes.getCurrentFunctionScope());
      95      104937 :   while (!WorkList.empty()) {
      96       98749 :     LexicalScope *S = WorkList.pop_back_val();
      97             : 
      98       98749 :     const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
      99       98749 :     if (!Children.empty())
     100       50738 :       WorkList.append(Children.begin(), Children.end());
     101             : 
     102       98749 :     if (S->isAbstractScope())
     103           0 :       continue;
     104             : 
     105      511883 :     for (const InsnRange &R : S->getRanges()) {
     106             :       assert(R.first && "InsnRange does not have first instruction!");
     107             :       assert(R.second && "InsnRange does not have second instruction!");
     108      431272 :       requestLabelBeforeInsn(R.first);
     109      431272 :       requestLabelAfterInsn(R.second);
     110             :     }
     111             :   }
     112        6188 : }
     113             : 
     114             : // Return Label preceding the instruction.
     115      239098 : MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
     116      478196 :   MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
     117             :   assert(Label && "Didn't insert label before instruction");
     118      239098 :   return Label;
     119             : }
     120             : 
     121             : // Return Label immediately following the instruction.
     122      238834 : MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
     123      477668 :   return LabelsAfterInsn.lookup(MI);
     124             : }
     125             : 
     126         178 : int DebugHandlerBase::fragmentCmp(const DIExpression *P1,
     127             :                                   const DIExpression *P2) {
     128         534 :   auto Fragment1 = *P1->getFragmentInfo();
     129         534 :   auto Fragment2 = *P2->getFragmentInfo();
     130         178 :   unsigned l1 = Fragment1.OffsetInBits;
     131         178 :   unsigned l2 = Fragment2.OffsetInBits;
     132         178 :   unsigned r1 = l1 + Fragment1.SizeInBits;
     133         178 :   unsigned r2 = l2 + Fragment2.SizeInBits;
     134         178 :   if (r1 <= l2)
     135             :     return -1;
     136         118 :   else if (r2 <= l1)
     137             :     return 1;
     138             :   else
     139          75 :     return 0;
     140             : }
     141             : 
     142         150 : bool DebugHandlerBase::fragmentsOverlap(const DIExpression *P1,
     143             :                                         const DIExpression *P2) {
     144         297 :   if (!P1->isFragment() || !P2->isFragment())
     145             :     return true;
     146         147 :   return fragmentCmp(P1, P2) == 0;
     147             : }
     148             : 
     149             : /// If this type is derived from a base type then return base type size.
     150        7021 : uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
     151        7021 :   DIType *Ty = TyRef.resolve();
     152             :   assert(Ty);
     153        5093 :   DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
     154             :   if (!DDTy)
     155        1928 :     return Ty->getSizeInBits();
     156             : 
     157       10186 :   unsigned Tag = DDTy->getTag();
     158             : 
     159        5093 :   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
     160        1335 :       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
     161        1328 :       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
     162        1328 :     return DDTy->getSizeInBits();
     163             : 
     164        7530 :   DIType *BaseType = DDTy->getBaseType().resolve();
     165             : 
     166             :   assert(BaseType && "Unexpected invalid base type");
     167             : 
     168             :   // If this is a derived type, go ahead and get the base type, unless it's a
     169             :   // reference then it's just the size of the field. Pointer types have no need
     170             :   // of this since they're a different type of qualification on the type.
     171       11253 :   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
     172        3723 :       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
     173          42 :     return Ty->getSizeInBits();
     174             : 
     175        3723 :   return getBaseTypeSize(BaseType);
     176             : }
     177             : 
     178      281926 : static bool hasDebugInfo(const MachineModuleInfo *MMI,
     179             :                          const MachineFunction *MF) {
     180      281926 :   if (!MMI->hasDebugInfo())
     181             :     return false;
     182       14590 :   auto *SP = MF->getFunction()->getSubprogram();
     183       14590 :   if (!SP)
     184             :     return false;
     185             :   assert(SP->getUnit());
     186       12450 :   auto EK = SP->getUnit()->getEmissionKind();
     187       12450 :   if (EK == DICompileUnit::NoDebug)
     188             :     return false;
     189       12444 :   return true;
     190             : }
     191             : 
     192      140964 : void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
     193      140964 :   PrevInstBB = nullptr;
     194             : 
     195      140964 :   if (!Asm || !hasDebugInfo(MMI, MF)) {
     196      134743 :     skippedNonDebugFunction();
     197      134741 :     return;
     198             :   }
     199             : 
     200             :   // Grab the lexical scopes for the function, if we don't have any of those
     201             :   // then we're not going to be able to do anything.
     202        6222 :   LScopes.initialize(*MF);
     203        6222 :   if (LScopes.empty()) {
     204          34 :     beginFunctionImpl(MF);
     205          34 :     return;
     206             :   }
     207             : 
     208             :   // Make sure that each lexical scope will have a begin/end label.
     209        6188 :   identifyScopeMarkers();
     210             : 
     211             :   // Calculate history for local variables.
     212             :   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
     213        6188 :   calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
     214             :                            DbgValues);
     215             : 
     216             :   // Request labels for the full history.
     217       35319 :   for (const auto &I : DbgValues) {
     218       10567 :     const auto &Ranges = I.second;
     219       10567 :     if (Ranges.empty())
     220           0 :       continue;
     221             : 
     222             :     // The first mention of a function argument gets the CurrentFnBegin
     223             :     // label, so arguments are visible when breaking at function entry.
     224       21134 :     const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
     225       19505 :     if (DIVar->isParameter() &&
     226        8938 :         getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
     227        2784 :       LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
     228        2784 :       if (Ranges.front().first->getDebugExpression()->isFragment()) {
     229             :         // Mark all non-overlapping initial fragments.
     230          80 :         for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
     231          26 :           const DIExpression *Fragment = I->first->getDebugExpression();
     232          78 :           if (std::all_of(Ranges.begin(), I,
     233          22 :                           [&](DbgValueHistoryMap::InstrRange Pred) {
     234          44 :                             return !fragmentsOverlap(
     235          44 :                                 Fragment, Pred.first->getDebugExpression());
     236          22 :                           }))
     237          44 :             LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
     238             :           else
     239             :             break;
     240             :         }
     241             :       }
     242             :     }
     243             : 
     244       69024 :     for (const auto &Range : Ranges) {
     245       74646 :       requestLabelBeforeInsn(Range.first);
     246       37323 :       if (Range.second)
     247       34177 :         requestLabelAfterInsn(Range.second);
     248             :     }
     249             :   }
     250             : 
     251       24752 :   PrevInstLoc = DebugLoc();
     252        6188 :   PrevLabel = Asm->getFunctionBegin();
     253        6188 :   beginFunctionImpl(MF);
     254             : }
     255             : 
     256     1739567 : void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
     257     1739567 :   if (!MMI->hasDebugInfo())
     258     1535773 :     return;
     259             : 
     260             :   assert(CurMI == nullptr);
     261     1739567 :   CurMI = MI;
     262             : 
     263             :   // Insert labels where requested.
     264             :   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
     265     1739567 :       LabelsBeforeInsn.find(MI);
     266             : 
     267             :   // No label needed.
     268     5218701 :   if (I == LabelsBeforeInsn.end())
     269             :     return;
     270             : 
     271             :   // Label already assigned.
     272      204732 :   if (I->second)
     273             :     return;
     274             : 
     275      203794 :   if (!PrevLabel) {
     276      241500 :     PrevLabel = MMI->getContext().createTempSymbol();
     277      241500 :     Asm->OutStreamer->EmitLabel(PrevLabel);
     278             :   }
     279      203794 :   I->second = PrevLabel;
     280             : }
     281             : 
     282     1739567 : void DebugHandlerBase::endInstruction() {
     283     1739567 :   if (!MMI->hasDebugInfo())
     284     1585344 :     return;
     285             : 
     286             :   assert(CurMI != nullptr);
     287             :   // Don't create a new label after DBG_VALUE and other instructions that don't
     288             :   // generate code.
     289     1739567 :   if (!CurMI->isMetaInstruction()) {
     290     1448954 :     PrevLabel = nullptr;
     291     1448954 :     PrevInstBB = CurMI->getParent();
     292             :   }
     293             : 
     294             :   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
     295     1739567 :       LabelsAfterInsn.find(CurMI);
     296     1739567 :   CurMI = nullptr;
     297             : 
     298             :   // No label needed.
     299     5218701 :   if (I == LabelsAfterInsn.end())
     300             :     return;
     301             : 
     302             :   // Label already assigned.
     303      154223 :   if (I->second)
     304             :     return;
     305             : 
     306             :   // We need a label after this instruction.
     307      154223 :   if (!PrevLabel) {
     308      307820 :     PrevLabel = MMI->getContext().createTempSymbol();
     309      307820 :     Asm->OutStreamer->EmitLabel(PrevLabel);
     310             :   }
     311      154223 :   I->second = PrevLabel;
     312             : }
     313             : 
     314      140963 : void DebugHandlerBase::endFunction(const MachineFunction *MF) {
     315      140963 :   if (hasDebugInfo(MMI, MF))
     316        6222 :     endFunctionImpl(MF);
     317      281927 :   DbgValues.clear();
     318      140964 :   LabelsBeforeInsn.clear();
     319      140964 :   LabelsAfterInsn.clear();
     320      140963 : }

Generated by: LCOV version 1.13