LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - DebugHandlerBase.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 131 138 94.9 %
Date: 2018-02-23 15:42:53 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/CodeGen/TargetSubtargetInfo.h"
      23             : #include "llvm/IR/DebugInfo.h"
      24             : #include "llvm/MC/MCStreamer.h"
      25             : 
      26             : using namespace llvm;
      27             : 
      28             : Optional<DbgVariableLocation>
      29         137 : DbgVariableLocation::extractFromMachineInstruction(
      30             :     const MachineInstr &Instruction) {
      31             :   DbgVariableLocation Location;
      32         137 :   if (!Instruction.isDebugValue())
      33             :     return None;
      34         274 :   if (!Instruction.getOperand(0).isReg())
      35             :     return None;
      36         128 :   Location.Register = Instruction.getOperand(0).getReg();
      37             :   Location.FragmentInfo.reset();
      38             :   // We only handle expressions generated by DIExpression::appendOffset,
      39             :   // which doesn't require a full stack machine.
      40         128 :   int64_t Offset = 0;
      41         128 :   const DIExpression *DIExpr = Instruction.getDebugExpression();
      42         128 :   auto Op = DIExpr->expr_op_begin();
      43         178 :   while (Op != DIExpr->expr_op_end()) {
      44          50 :     switch (Op->getOp()) {
      45             :     case dwarf::DW_OP_constu: {
      46           2 :       int Value = Op->getArg(0);
      47             :       ++Op;
      48           2 :       if (Op != DIExpr->expr_op_end()) {
      49           2 :         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             :     case dwarf::DW_OP_plus_uconst:
      62          21 :       Offset += Op->getArg(0);
      63          21 :       break;
      64             :     case dwarf::DW_OP_LLVM_fragment:
      65             :       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             :     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             :   if (Instruction.isIndirectDebugValue())
      81          31 :     Location.LoadChain.push_back(Offset);
      82             : 
      83             :   return Location;
      84             : }
      85             : 
      86      110298 : 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        6537 : void DebugHandlerBase::identifyScopeMarkers() {
      93             :   SmallVector<LexicalScope *, 4> WorkList;
      94        6537 :   WorkList.push_back(LScopes.getCurrentFunctionScope());
      95      113142 :   while (!WorkList.empty()) {
      96             :     LexicalScope *S = WorkList.pop_back_val();
      97             : 
      98             :     const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
      99      106605 :     if (!Children.empty())
     100       55037 :       WorkList.append(Children.begin(), Children.end());
     101             : 
     102      106605 :     if (S->isAbstractScope())
     103           0 :       continue;
     104             : 
     105      560979 :     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      227187 :       requestLabelBeforeInsn(R.first);
     109      227187 :       requestLabelAfterInsn(R.second);
     110             :     }
     111             :   }
     112        6537 : }
     113             : 
     114             : // Return Label preceding the instruction.
     115      265478 : MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) {
     116      265478 :   MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
     117             :   assert(Label && "Didn't insert label before instruction");
     118      265478 :   return Label;
     119             : }
     120             : 
     121             : // Return Label immediately following the instruction.
     122      258820 : MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) {
     123      517640 :   return LabelsAfterInsn.lookup(MI);
     124             : }
     125             : 
     126         321 : int DebugHandlerBase::fragmentCmp(const DIExpression *P1,
     127             :                                   const DIExpression *P2) {
     128         321 :   auto Fragment1 = *P1->getFragmentInfo();
     129         321 :   auto Fragment2 = *P2->getFragmentInfo();
     130         321 :   unsigned l1 = Fragment1.OffsetInBits;
     131         321 :   unsigned l2 = Fragment2.OffsetInBits;
     132         321 :   unsigned r1 = l1 + Fragment1.SizeInBits;
     133         321 :   unsigned r2 = l2 + Fragment2.SizeInBits;
     134         321 :   if (r1 <= l2)
     135             :     return -1;
     136         266 :   else if (r2 <= l1)
     137             :     return 1;
     138             :   else
     139         213 :     return 0;
     140             : }
     141             : 
     142         298 : bool DebugHandlerBase::fragmentsOverlap(const DIExpression *P1,
     143             :                                         const DIExpression *P2) {
     144         589 :   if (!P1->isFragment() || !P2->isFragment())
     145             :     return true;
     146         291 :   return fragmentCmp(P1, P2) == 0;
     147             : }
     148             : 
     149             : /// If this type is derived from a base type then return base type size.
     150        8566 : uint64_t DebugHandlerBase::getBaseTypeSize(const DITypeRef TyRef) {
     151             :   DIType *Ty = TyRef.resolve();
     152             :   assert(Ty);
     153             :   DIDerivedType *DDTy = dyn_cast<DIDerivedType>(Ty);
     154             :   if (!DDTy)
     155        2152 :     return Ty->getSizeInBits();
     156             : 
     157        6414 :   unsigned Tag = DDTy->getTag();
     158             : 
     159        6414 :   if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef &&
     160        1593 :       Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
     161        1584 :       Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_atomic_type)
     162        1584 :     return DDTy->getSizeInBits();
     163             : 
     164             :   DIType *BaseType = DDTy->getBaseType().resolve();
     165             : 
     166             :   if (!BaseType)
     167             :     return 0;
     168             : 
     169             :   // If this is a derived type, go ahead and get the base type, unless it's a
     170             :   // reference then it's just the size of the field. Pointer types have no need
     171             :   // of this since they're a different type of qualification on the type.
     172        9658 :   if (BaseType->getTag() == dwarf::DW_TAG_reference_type ||
     173             :       BaseType->getTag() == dwarf::DW_TAG_rvalue_reference_type)
     174          52 :     return Ty->getSizeInBits();
     175             : 
     176        4777 :   return getBaseTypeSize(BaseType);
     177             : }
     178             : 
     179      330778 : static bool hasDebugInfo(const MachineModuleInfo *MMI,
     180             :                          const MachineFunction *MF) {
     181      330778 :   if (!MMI->hasDebugInfo())
     182             :     return false;
     183       15376 :   auto *SP = MF->getFunction().getSubprogram();
     184       15376 :   if (!SP)
     185             :     return false;
     186             :   assert(SP->getUnit());
     187       13184 :   auto EK = SP->getUnit()->getEmissionKind();
     188       13184 :   if (EK == DICompileUnit::NoDebug)
     189             :     return false;
     190       13178 :   return true;
     191             : }
     192             : 
     193      165390 : void DebugHandlerBase::beginFunction(const MachineFunction *MF) {
     194      165390 :   PrevInstBB = nullptr;
     195             : 
     196      165390 :   if (!Asm || !hasDebugInfo(MMI, MF)) {
     197      158800 :     skippedNonDebugFunction();
     198      158801 :     return;
     199             :   }
     200             : 
     201             :   // Grab the lexical scopes for the function, if we don't have any of those
     202             :   // then we're not going to be able to do anything.
     203        6589 :   LScopes.initialize(*MF);
     204        6589 :   if (LScopes.empty()) {
     205          52 :     beginFunctionImpl(MF);
     206          52 :     return;
     207             :   }
     208             : 
     209             :   // Make sure that each lexical scope will have a begin/end label.
     210        6537 :   identifyScopeMarkers();
     211             : 
     212             :   // Calculate history for local variables.
     213             :   assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
     214        6537 :   calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
     215             :                            DbgValues);
     216             : 
     217             :   // Request labels for the full history.
     218       20272 :   for (const auto &I : DbgValues) {
     219             :     const auto &Ranges = I.second;
     220       13735 :     if (Ranges.empty())
     221           0 :       continue;
     222             : 
     223             :     // The first mention of a function argument gets the CurrentFnBegin
     224             :     // label, so arguments are visible when breaking at function entry.
     225       13735 :     const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
     226       25626 :     if (DIVar->isParameter() &&
     227       11891 :         getDISubprogram(DIVar->getScope())->describes(&MF->getFunction())) {
     228        2036 :       LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
     229        2036 :       if (Ranges.front().first->getDebugExpression()->isFragment()) {
     230             :         // Mark all non-overlapping initial fragments.
     231          56 :         for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
     232          26 :           const DIExpression *Fragment = I->first->getDebugExpression();
     233          26 :           if (std::all_of(Ranges.begin(), I,
     234          22 :                           [&](DbgValueHistoryMap::InstrRange Pred) {
     235          44 :                             return !fragmentsOverlap(
     236          44 :                                 Fragment, Pred.first->getDebugExpression());
     237          22 :                           }))
     238          44 :             LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
     239             :           else
     240             :             break;
     241             :         }
     242             :       }
     243             :     }
     244             : 
     245      117851 :     for (const auto &Range : Ranges) {
     246       52058 :       requestLabelBeforeInsn(Range.first);
     247       52058 :       if (Range.second)
     248             :         requestLabelAfterInsn(Range.second);
     249             :     }
     250             :   }
     251             : 
     252       13074 :   PrevInstLoc = DebugLoc();
     253        6537 :   PrevLabel = Asm->getFunctionBegin();
     254        6537 :   beginFunctionImpl(MF);
     255             : }
     256             : 
     257     1825057 : void DebugHandlerBase::beginInstruction(const MachineInstr *MI) {
     258     1825057 :   if (!MMI->hasDebugInfo())
     259     1599966 :     return;
     260             : 
     261             :   assert(CurMI == nullptr);
     262     1825057 :   CurMI = MI;
     263             : 
     264             :   // Insert labels where requested.
     265             :   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
     266     1825057 :       LabelsBeforeInsn.find(MI);
     267             : 
     268             :   // No label needed.
     269     1825057 :   if (I == LabelsBeforeInsn.end())
     270             :     return;
     271             : 
     272             :   // Label already assigned.
     273      226119 :   if (I->second)
     274             :     return;
     275             : 
     276      225091 :   if (!PrevLabel) {
     277      249932 :     PrevLabel = MMI->getContext().createTempSymbol();
     278      249932 :     Asm->OutStreamer->EmitLabel(PrevLabel);
     279             :   }
     280      225091 :   I->second = PrevLabel;
     281             : }
     282             : 
     283     1825057 : void DebugHandlerBase::endInstruction() {
     284     1825057 :   if (!MMI->hasDebugInfo())
     285     1662906 :     return;
     286             : 
     287             :   assert(CurMI != nullptr);
     288             :   // Don't create a new label after DBG_VALUE and other instructions that don't
     289             :   // generate code.
     290     1825057 :   if (!CurMI->isMetaInstruction()) {
     291     1507695 :     PrevLabel = nullptr;
     292     1507695 :     PrevInstBB = CurMI->getParent();
     293             :   }
     294             : 
     295             :   DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
     296     1825057 :       LabelsAfterInsn.find(CurMI);
     297     1825057 :   CurMI = nullptr;
     298             : 
     299             :   // No label needed.
     300     1825057 :   if (I == LabelsAfterInsn.end())
     301             :     return;
     302             : 
     303             :   // Label already assigned.
     304      162151 :   if (I->second)
     305             :     return;
     306             : 
     307             :   // We need a label after this instruction.
     308      162151 :   if (!PrevLabel) {
     309      323384 :     PrevLabel = MMI->getContext().createTempSymbol();
     310      323384 :     Asm->OutStreamer->EmitLabel(PrevLabel);
     311             :   }
     312      162151 :   I->second = PrevLabel;
     313             : }
     314             : 
     315      165390 : void DebugHandlerBase::endFunction(const MachineFunction *MF) {
     316      165390 :   if (hasDebugInfo(MMI, MF))
     317        6589 :     endFunctionImpl(MF);
     318             :   DbgValues.clear();
     319      165390 :   LabelsBeforeInsn.clear();
     320      165390 :   LabelsAfterInsn.clear();
     321      165390 : }

Generated by: LCOV version 1.13