LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - DbgEntityHistoryCalculator.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 82 82 100.0 %
Date: 2018-10-20 13:21:21 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/CodeGen/AsmPrinter/DbgEntityHistoryCalculator.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             : #include "DbgEntityHistoryCalculator.h"
      11             : #include "llvm/ADT/BitVector.h"
      12             : #include "llvm/ADT/STLExtras.h"
      13             : #include "llvm/ADT/SmallVector.h"
      14             : #include "llvm/CodeGen/MachineBasicBlock.h"
      15             : #include "llvm/CodeGen/MachineFunction.h"
      16             : #include "llvm/CodeGen/MachineInstr.h"
      17             : #include "llvm/CodeGen/MachineOperand.h"
      18             : #include "llvm/CodeGen/TargetLowering.h"
      19             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      20             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      21             : #include "llvm/IR/DebugInfoMetadata.h"
      22             : #include "llvm/IR/DebugLoc.h"
      23             : #include "llvm/MC/MCRegisterInfo.h"
      24             : #include "llvm/Support/Debug.h"
      25             : #include "llvm/Support/raw_ostream.h"
      26             : #include <cassert>
      27             : #include <map>
      28             : #include <utility>
      29             : 
      30             : using namespace llvm;
      31             : 
      32             : #define DEBUG_TYPE "dwarfdebug"
      33             : 
      34             : // If @MI is a DBG_VALUE with debug value described by a
      35             : // defined register, returns the number of this register.
      36             : // In the other case, returns 0.
      37             : static unsigned isDescribedByReg(const MachineInstr &MI) {
      38             :   assert(MI.isDebugValue());
      39             :   assert(MI.getNumOperands() == 4);
      40             :   // If location of variable is described using a register (directly or
      41             :   // indirectly), this register is always a first operand.
      42      649000 :   return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0;
      43             : }
      44             : 
      45      300634 : void DbgValueHistoryMap::startInstrRange(InlinedEntity Var,
      46             :                                          const MachineInstr &MI) {
      47             :   // Instruction range should start with a DBG_VALUE instruction for the
      48             :   // variable.
      49             :   assert(MI.isDebugValue() && "not a DBG_VALUE");
      50      300634 :   auto &Ranges = VarInstrRanges[Var];
      51      348366 :   if (!Ranges.empty() && Ranges.back().second == nullptr &&
      52       47732 :       Ranges.back().first->isIdenticalTo(MI)) {
      53             :     LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
      54             :                       << "\t" << Ranges.back().first << "\t" << MI << "\n");
      55             :     return;
      56             :   }
      57      553978 :   Ranges.push_back(std::make_pair(&MI, nullptr));
      58             : }
      59             : 
      60      229173 : void DbgValueHistoryMap::endInstrRange(InlinedEntity Var,
      61             :                                        const MachineInstr &MI) {
      62      229173 :   auto &Ranges = VarInstrRanges[Var];
      63             :   // Verify that the current instruction range is not yet closed.
      64             :   assert(!Ranges.empty() && Ranges.back().second == nullptr);
      65             :   // For now, instruction ranges are not allowed to cross basic block
      66             :   // boundaries.
      67             :   assert(Ranges.back().first->getParent() == MI.getParent());
      68      229173 :   Ranges.back().second = &MI;
      69      229173 : }
      70             : 
      71      300634 : unsigned DbgValueHistoryMap::getRegisterForVar(InlinedEntity Var) const {
      72      300634 :   const auto &I = VarInstrRanges.find(Var);
      73             :   if (I == VarInstrRanges.end())
      74             :     return 0;
      75             :   const auto &Ranges = I->second;
      76      223490 :   if (Ranges.empty() || Ranges.back().second != nullptr)
      77             :     return 0;
      78       47732 :   return isDescribedByReg(*Ranges.back().first);
      79             : }
      80             : 
      81           6 : void DbgLabelInstrMap::addInstr(InlinedEntity Label, const MachineInstr &MI) {
      82             :   assert(MI.isDebugLabel() && "not a DBG_LABEL");
      83           6 :   LabelInstr[Label] = &MI;
      84           6 : }
      85             : 
      86             : namespace {
      87             : 
      88             : // Maps physreg numbers to the variables they describe.
      89             : using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
      90             : using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedEntity, 1>>;
      91             : 
      92             : } // end anonymous namespace
      93             : 
      94             : // Claim that @Var is not described by @RegNo anymore.
      95       40010 : static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
      96             :                                 InlinedEntity Var) {
      97             :   const auto &I = RegVars.find(RegNo);
      98             :   assert(RegNo != 0U && I != RegVars.end());
      99             :   auto &VarSet = I->second;
     100             :   const auto &VarPos = llvm::find(VarSet, Var);
     101             :   assert(VarPos != VarSet.end());
     102             :   VarSet.erase(VarPos);
     103             :   // Don't keep empty sets in a map to keep it as small as possible.
     104       40010 :   if (VarSet.empty())
     105             :     RegVars.erase(I);
     106       40010 : }
     107             : 
     108             : // Claim that @Var is now described by @RegNo.
     109             : static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
     110             :                                InlinedEntity Var) {
     111             :   assert(RegNo != 0U);
     112      275544 :   auto &VarSet = RegVars[RegNo];
     113             :   assert(!is_contained(VarSet, Var));
     114      275544 :   VarSet.push_back(Var);
     115             : }
     116             : 
     117             : // Terminate the location range for variables described by register at
     118             : // @I by inserting @ClobberingInstr to their history.
     119      125130 : static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
     120             :                                 RegDescribedVarsMap::iterator I,
     121             :                                 DbgValueHistoryMap &HistMap,
     122             :                                 const MachineInstr &ClobberingInstr) {
     123             :   // Iterate over all variables described by this register and add this
     124             :   // instruction to their history, clobbering it.
     125      354303 :   for (const auto &Var : I->second)
     126      229173 :     HistMap.endInstrRange(Var, ClobberingInstr);
     127             :   RegVars.erase(I);
     128      125130 : }
     129             : 
     130             : // Terminate the location range for variables described by register
     131             : // @RegNo by inserting @ClobberingInstr to their history.
     132    27993639 : static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
     133             :                                 DbgValueHistoryMap &HistMap,
     134             :                                 const MachineInstr &ClobberingInstr) {
     135             :   const auto &I = RegVars.find(RegNo);
     136    27993639 :   if (I == RegVars.end())
     137             :     return;
     138       15960 :   clobberRegisterUses(RegVars, I, HistMap, ClobberingInstr);
     139             : }
     140             : 
     141             : // Returns the first instruction in @MBB which corresponds to
     142             : // the function epilogue, or nullptr if @MBB doesn't contain an epilogue.
     143      231141 : static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
     144             :   auto LastMI = MBB.getLastNonDebugInstr();
     145      461442 :   if (LastMI == MBB.end() || !LastMI->isReturn())
     146      221222 :     return nullptr;
     147             :   // Assume that epilogue starts with instruction having the same debug location
     148             :   // as the return instruction.
     149             :   DebugLoc LastLoc = LastMI->getDebugLoc();
     150             :   auto Res = LastMI;
     151             :   for (MachineBasicBlock::const_reverse_iterator I = LastMI.getReverse(),
     152             :                                                  E = MBB.rend();
     153       84644 :        I != E; ++I) {
     154       82639 :     if (I->getDebugLoc() != LastLoc)
     155             :       return &*Res;
     156             :     Res = &*I;
     157             :   }
     158             :   // If all instructions have the same debug location, assume whole MBB is
     159             :   // an epilogue.
     160             :   return &*MBB.begin();
     161             : }
     162             : 
     163             : // Collect registers that are modified in the function body (their
     164             : // contents is changed outside of the prologue and epilogue).
     165        9611 : static void collectChangingRegs(const MachineFunction *MF,
     166             :                                 const TargetRegisterInfo *TRI,
     167             :                                 BitVector &Regs) {
     168      240752 :   for (const auto &MBB : *MF) {
     169      231141 :     auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
     170             : 
     171     2151470 :     for (const auto &MI : MBB) {
     172             :       // Avoid looking at prologue or epilogue instructions.
     173     1930248 :       if (&MI == FirstEpilogueInst)
     174             :         break;
     175     1920329 :       if (MI.getFlag(MachineInstr::FrameSetup))
     176             :         continue;
     177             : 
     178             :       // Look for register defs and register masks. Register masks are
     179             :       // typically on calls and they clobber everything not in the mask.
     180    10689527 :       for (const MachineOperand &MO : MI.operands()) {
     181             :         // Skip virtual registers since they are handled by the parent.
     182     8796683 :         if (MO.isReg() && MO.isDef() && MO.getReg() &&
     183             :             !TRI->isVirtualRegister(MO.getReg())) {
     184     8086299 :           for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
     185     6830031 :                ++AI)
     186             :             Regs.set(*AI);
     187     7540415 :         } else if (MO.isRegMask()) {
     188       86166 :           Regs.setBitsNotInMask(MO.getRegMask());
     189             :         }
     190             :       }
     191             :     }
     192             :   }
     193        9611 : }
     194             : 
     195        9611 : void llvm::calculateDbgEntityHistory(const MachineFunction *MF,
     196             :                                      const TargetRegisterInfo *TRI,
     197             :                                      DbgValueHistoryMap &DbgValues,
     198             :                                      DbgLabelInstrMap &DbgLabels) {
     199        9611 :   BitVector ChangingRegs(TRI->getNumRegs());
     200        9611 :   collectChangingRegs(MF, TRI, ChangingRegs);
     201             : 
     202        9611 :   const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
     203        9611 :   unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
     204             :   RegDescribedVarsMap RegVars;
     205      240752 :   for (const auto &MBB : *MF) {
     206     2226195 :     for (const auto &MI : MBB) {
     207             :       if (!MI.isDebugInstr()) {
     208             :         // Not a DBG_VALUE instruction. It may clobber registers which describe
     209             :         // some variables.
     210     9562641 :         for (const MachineOperand &MO : MI.operands()) {
     211     7868227 :           if (MO.isReg() && MO.isDef() && MO.getReg()) {
     212             :             // Ignore call instructions that claim to clobber SP. The AArch64
     213             :             // backend does this for aggregate function arguments.
     214     1352843 :             if (MI.isCall() && MO.getReg() == SP)
     215             :               continue;
     216             :             // If this is a virtual register, only clobber it since it doesn't
     217             :             // have aliases.
     218     2532250 :             if (TRI->isVirtualRegister(MO.getReg()))
     219         291 :               clobberRegisterUses(RegVars, MO.getReg(), DbgValues, MI);
     220             :             // If this is a register def operand, it may end a debug value
     221             :             // range.
     222             :             else {
     223     8038095 :               for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
     224     6772261 :                    ++AI)
     225     6772261 :                 if (ChangingRegs.test(*AI))
     226     6703558 :                   clobberRegisterUses(RegVars, *AI, DbgValues, MI);
     227             :             }
     228     6515384 :           } else if (MO.isRegMask()) {
     229             :             // If this is a register mask operand, clobber all debug values in
     230             :             // non-CSRs.
     231    23567973 :             for (unsigned I : ChangingRegs.set_bits()) {
     232             :               // Don't consider SP to be clobbered by register masks.
     233    23479500 :               if (unsigned(I) != SP && TRI->isPhysicalRegister(I) &&
     234             :                   MO.clobbersPhysReg(I)) {
     235    21289790 :                 clobberRegisterUses(RegVars, I, DbgValues, MI);
     236             :               }
     237             :             }
     238             :           }
     239             :         }
     240             :         continue;
     241             :       }
     242             : 
     243      300640 :       if (MI.isDebugValue()) {
     244             :         assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
     245             :         // Use the base variable (without any DW_OP_piece expressions)
     246             :         // as index into History. The full variables including the
     247             :         // piece expressions are attached to the MI.
     248      300634 :         const DILocalVariable *RawVar = MI.getDebugVariable();
     249             :         assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
     250             :                "Expected inlined-at fields to agree");
     251             :         InlinedEntity Var(RawVar, MI.getDebugLoc()->getInlinedAt());
     252             : 
     253      300634 :         if (unsigned PrevReg = DbgValues.getRegisterForVar(Var))
     254       40010 :           dropRegDescribedVar(RegVars, PrevReg, Var);
     255             : 
     256      300634 :         DbgValues.startInstrRange(Var, MI);
     257             : 
     258      292498 :         if (unsigned NewReg = isDescribedByReg(MI))
     259      275544 :           addRegDescribedVar(RegVars, NewReg, Var);
     260           6 :       } else if (MI.isDebugLabel()) {
     261             :         assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!");
     262           6 :         const DILabel *RawLabel = MI.getDebugLabel();
     263             :         assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
     264             :             "Expected inlined-at fields to agree");
     265             :         // When collecting debug information for labels, there is no MCSymbol
     266             :         // generated for it. So, we keep MachineInstr in DbgLabels in order
     267             :         // to query MCSymbol afterward.
     268             :         InlinedEntity L(RawLabel, MI.getDebugLoc()->getInlinedAt());
     269           6 :         DbgLabels.addInstr(L, MI);
     270             :       }
     271             :     }
     272             : 
     273             :     // Make sure locations for register-described variables are valid only
     274             :     // until the end of the basic block (unless it's the last basic block, in
     275             :     // which case let their liveness run off to the end of the function).
     276      231141 :     if (!MBB.empty() && &MBB != &MF->back()) {
     277      335648 :       for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) {
     278             :         auto CurElem = I++; // CurElem can be erased below.
     279      229160 :         if (TRI->isVirtualRegister(CurElem->first) ||
     280             :             ChangingRegs.test(CurElem->first))
     281      109170 :           clobberRegisterUses(RegVars, CurElem, DbgValues, MBB.back());
     282             :       }
     283             :     }
     284             :   }
     285        9611 : }
     286             : 
     287             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     288             : LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const {
     289             :   dbgs() << "DbgValueHistoryMap:\n";
     290             :   for (const auto &VarRangePair : *this) {
     291             :     const InlinedEntity &Var = VarRangePair.first;
     292             :     const InstrRanges &Ranges = VarRangePair.second;
     293             : 
     294             :     const DILocalVariable *LocalVar = cast<DILocalVariable>(Var.first);
     295             :     const DILocation *Location = Var.second;
     296             : 
     297             :     dbgs() << " - " << LocalVar->getName() << " at ";
     298             : 
     299             :     if (Location)
     300             :       dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
     301             :              << Location->getColumn();
     302             :     else
     303             :       dbgs() << "<unknown location>";
     304             : 
     305             :     dbgs() << " --\n";
     306             : 
     307             :     for (const InstrRange &Range : Ranges) {
     308             :       dbgs() << "   Begin: " << *Range.first;
     309             :       if (Range.second)
     310             :         dbgs() << "   End  : " << *Range.second;
     311             :       dbgs() << "\n";
     312             :     }
     313             :   }
     314             : }
     315             : #endif

Generated by: LCOV version 1.13