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/CodeGen/AsmPrinter.h" 17 #include "llvm/CodeGen/MachineFunction.h" 18 #include "llvm/CodeGen/MachineInstr.h" 19 #include "llvm/CodeGen/MachineModuleInfo.h" 20 #include "llvm/Target/TargetSubtargetInfo.h" 21 22 using namespace llvm; 23 24 DebugHandlerBase::DebugHandlerBase(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} 25 26 // Each LexicalScope has first instruction and last instruction to mark 27 // beginning and end of a scope respectively. Create an inverse map that list 28 // scopes starts (and ends) with an instruction. One instruction may start (or 29 // end) multiple scopes. Ignore scopes that are not reachable. 30 void DebugHandlerBase::identifyScopeMarkers() { 31 SmallVector<LexicalScope *, 4> WorkList; 32 WorkList.push_back(LScopes.getCurrentFunctionScope()); 33 while (!WorkList.empty()) { 34 LexicalScope *S = WorkList.pop_back_val(); 35 36 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren(); 37 if (!Children.empty()) 38 WorkList.append(Children.begin(), Children.end()); 39 40 if (S->isAbstractScope()) 41 continue; 42 43 for (const InsnRange &R : S->getRanges()) { 44 assert(R.first && "InsnRange does not have first instruction!"); 45 assert(R.second && "InsnRange does not have second instruction!"); 46 requestLabelBeforeInsn(R.first); 47 requestLabelAfterInsn(R.second); 48 } 49 } 50 } 51 52 // Return Label preceding the instruction. 53 MCSymbol *DebugHandlerBase::getLabelBeforeInsn(const MachineInstr *MI) { 54 MCSymbol *Label = LabelsBeforeInsn.lookup(MI); 55 assert(Label && "Didn't insert label before instruction"); 56 return Label; 57 } 58 59 // Return Label immediately following the instruction. 60 MCSymbol *DebugHandlerBase::getLabelAfterInsn(const MachineInstr *MI) { 61 return LabelsAfterInsn.lookup(MI); 62 } 63 64 // Determine the relative position of the pieces described by P1 and P2. 65 // Returns -1 if P1 is entirely before P2, 0 if P1 and P2 overlap, 66 // 1 if P1 is entirely after P2. 67 int DebugHandlerBase::pieceCmp(const DIExpression *P1, const DIExpression *P2) { 68 unsigned l1 = P1->getBitPieceOffset(); 69 unsigned l2 = P2->getBitPieceOffset(); 70 unsigned r1 = l1 + P1->getBitPieceSize(); 71 unsigned r2 = l2 + P2->getBitPieceSize(); 72 if (r1 <= l2) 73 return -1; 74 else if (r2 <= l1) 75 return 1; 76 else 77 return 0; 78 } 79 80 /// Determine whether two variable pieces overlap. 81 bool DebugHandlerBase::piecesOverlap(const DIExpression *P1, const DIExpression *P2) { 82 if (!P1->isBitPiece() || !P2->isBitPiece()) 83 return true; 84 return pieceCmp(P1, P2) == 0; 85 } 86 87 void DebugHandlerBase::beginFunction(const MachineFunction *MF) { 88 // Grab the lexical scopes for the function, if we don't have any of those 89 // then we're not going to be able to do anything. 90 LScopes.initialize(*MF); 91 if (LScopes.empty()) 92 return; 93 94 // Make sure that each lexical scope will have a begin/end label. 95 identifyScopeMarkers(); 96 97 // Calculate history for local variables. 98 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!"); 99 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(), 100 DbgValues); 101 102 // Request labels for the full history. 103 for (const auto &I : DbgValues) { 104 const auto &Ranges = I.second; 105 if (Ranges.empty()) 106 continue; 107 108 // The first mention of a function argument gets the CurrentFnBegin 109 // label, so arguments are visible when breaking at function entry. 110 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable(); 111 if (DIVar->isParameter() && 112 getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) { 113 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin(); 114 if (Ranges.front().first->getDebugExpression()->isBitPiece()) { 115 // Mark all non-overlapping initial pieces. 116 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) { 117 const DIExpression *Piece = I->first->getDebugExpression(); 118 if (std::all_of(Ranges.begin(), I, 119 [&](DbgValueHistoryMap::InstrRange Pred) { 120 return !piecesOverlap(Piece, Pred.first->getDebugExpression()); 121 })) 122 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin(); 123 else 124 break; 125 } 126 } 127 } 128 129 for (const auto &Range : Ranges) { 130 requestLabelBeforeInsn(Range.first); 131 if (Range.second) 132 requestLabelAfterInsn(Range.second); 133 } 134 } 135 136 PrevInstLoc = DebugLoc(); 137 PrevLabel = Asm->getFunctionBegin(); 138 } 139 140 void DebugHandlerBase::beginInstruction(const MachineInstr *MI) { 141 if (!MMI->hasDebugInfo()) 142 return; 143 144 assert(CurMI == nullptr); 145 CurMI = MI; 146 147 // Insert labels where requested. 148 DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 149 LabelsBeforeInsn.find(MI); 150 151 // No label needed. 152 if (I == LabelsBeforeInsn.end()) 153 return; 154 155 // Label already assigned. 156 if (I->second) 157 return; 158 159 if (!PrevLabel) { 160 PrevLabel = MMI->getContext().createTempSymbol(); 161 Asm->OutStreamer->EmitLabel(PrevLabel); 162 } 163 I->second = PrevLabel; 164 } 165 166 void DebugHandlerBase::endInstruction() { 167 if (!MMI->hasDebugInfo()) 168 return; 169 170 assert(CurMI != nullptr); 171 // Don't create a new label after DBG_VALUE instructions. 172 // They don't generate code. 173 if (!CurMI->isDebugValue()) 174 PrevLabel = nullptr; 175 176 DenseMap<const MachineInstr *, MCSymbol *>::iterator I = 177 LabelsAfterInsn.find(CurMI); 178 CurMI = nullptr; 179 180 // No label needed. 181 if (I == LabelsAfterInsn.end()) 182 return; 183 184 // Label already assigned. 185 if (I->second) 186 return; 187 188 // We need a label after this instruction. 189 if (!PrevLabel) { 190 PrevLabel = MMI->getContext().createTempSymbol(); 191 Asm->OutStreamer->EmitLabel(PrevLabel); 192 } 193 I->second = PrevLabel; 194 } 195 196 void DebugHandlerBase::endFunction(const MachineFunction *MF) { 197 DbgValues.clear(); 198 LabelsBeforeInsn.clear(); 199 LabelsAfterInsn.clear(); 200 } 201