Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 1 | //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===// |
| 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 | // This file implements the helper classes used to build and interpret debug |
| 11 | // information in LLVM IR form. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 15 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallPtrSet.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/Analysis/ValueTracking.h" |
| 21 | #include "llvm/IR/Constants.h" |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DIBuilder.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DerivedTypes.h" |
| 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/IntrinsicInst.h" |
| 26 | #include "llvm/IR/Intrinsics.h" |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 27 | #include "llvm/IR/GVMaterializer.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 29 | #include "llvm/IR/ValueHandle.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/Dwarf.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | using namespace llvm; |
| 34 | using namespace llvm::dwarf; |
| 35 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 36 | DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { |
| 37 | if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope)) |
Duncan P. N. Exon Smith | dd77af8 | 2015-03-31 02:06:28 +0000 | [diff] [blame] | 38 | return LocalScope->getSubprogram(); |
| 39 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 42 | DISubprogram *llvm::getDISubprogram(const Function *F) { |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 43 | // We look for the first instr that has a debug annotation leading back to F. |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 44 | for (auto &BB : *F) { |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 45 | auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) { |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 46 | return Inst.getDebugLoc(); |
David Majnemer | c758df4 | 2014-11-01 07:57:14 +0000 | [diff] [blame] | 47 | }); |
| 48 | if (Inst == BB.end()) |
| 49 | continue; |
| 50 | DebugLoc DLoc = Inst->getDebugLoc(); |
Duncan P. N. Exon Smith | ab659fb3 | 2015-03-30 19:40:05 +0000 | [diff] [blame] | 51 | const MDNode *Scope = DLoc.getInlinedAtScope(); |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 52 | auto *Subprogram = getDISubprogram(Scope); |
| 53 | return Subprogram->describes(F) ? Subprogram : nullptr; |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 56 | return nullptr; |
Timur Iskhodzhanov | eb229ca | 2014-10-23 23:46:28 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 59 | DITypeIdentifierMap |
| 60 | llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) { |
| 61 | DITypeIdentifierMap Map; |
| 62 | for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 63 | auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi)); |
| 64 | DINodeArray Retain = CU->getRetainedTypes(); |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 65 | for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 66 | if (!isa<DICompositeType>(Retain[Ti])) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 67 | continue; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 68 | auto *Ty = cast<DICompositeType>(Retain[Ti]); |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 69 | if (MDString *TypeId = Ty->getRawIdentifier()) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 70 | // Definition has priority over declaration. |
| 71 | // Try to insert (TypeId, Ty) to Map. |
| 72 | std::pair<DITypeIdentifierMap::iterator, bool> P = |
| 73 | Map.insert(std::make_pair(TypeId, Ty)); |
| 74 | // If TypeId already exists in Map and this is a definition, replace |
| 75 | // whatever we had (declaration or definition) with the definition. |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 76 | if (!P.second && !Ty->isForwardDecl()) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 77 | P.first->second = Ty; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return Map; |
| 82 | } |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | // DebugInfoFinder implementations. |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | |
| 88 | void DebugInfoFinder::reset() { |
| 89 | CUs.clear(); |
| 90 | SPs.clear(); |
| 91 | GVs.clear(); |
| 92 | TYs.clear(); |
| 93 | Scopes.clear(); |
| 94 | NodesSeen.clear(); |
| 95 | TypeIdentifierMap.clear(); |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 96 | TypeMapInitialized = false; |
| 97 | } |
| 98 | |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 99 | void DebugInfoFinder::InitializeTypeMap(const Module &M) { |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 100 | if (!TypeMapInitialized) |
| 101 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
| 102 | TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes); |
| 103 | TypeMapInitialized = true; |
| 104 | } |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 107 | void DebugInfoFinder::processModule(const Module &M) { |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 108 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 109 | if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 110 | for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 111 | auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 112 | addCompileUnit(CU); |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 113 | for (auto *DIG : CU->getGlobalVariables()) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 114 | if (addGlobalVariable(DIG)) { |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 115 | processScope(DIG->getScope()); |
| 116 | processType(DIG->getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 119 | for (auto *SP : CU->getSubprograms()) |
| 120 | processSubprogram(SP); |
| 121 | for (auto *ET : CU->getEnumTypes()) |
| 122 | processType(ET); |
| 123 | for (auto *RT : CU->getRetainedTypes()) |
| 124 | processType(RT); |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 125 | for (auto *Import : CU->getImportedEntities()) { |
Duncan P. N. Exon Smith | de8e427 | 2015-04-14 01:46:44 +0000 | [diff] [blame] | 126 | auto *Entity = Import->getEntity().resolve(TypeIdentifierMap); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 127 | if (auto *T = dyn_cast<DIType>(Entity)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 128 | processType(T); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 129 | else if (auto *SP = dyn_cast<DISubprogram>(Entity)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 130 | processSubprogram(SP); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 131 | else if (auto *NS = dyn_cast<DINamespace>(Entity)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 132 | processScope(NS->getScope()); |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 133 | else if (auto *M = dyn_cast<DIModule>(Entity)) |
| 134 | processScope(M->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 140 | void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 141 | if (!Loc) |
| 142 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 143 | InitializeTypeMap(M); |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 144 | processScope(Loc->getScope()); |
| 145 | processLocation(M, Loc->getInlinedAt()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 148 | void DebugInfoFinder::processType(DIType *DT) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 149 | if (!addType(DT)) |
| 150 | return; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 151 | processScope(DT->getScope().resolve(TypeIdentifierMap)); |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 152 | if (auto *ST = dyn_cast<DISubroutineType>(DT)) { |
| 153 | for (DITypeRef Ref : ST->getTypeArray()) |
| 154 | processType(Ref.resolve(TypeIdentifierMap)); |
| 155 | return; |
| 156 | } |
| 157 | if (auto *DCT = dyn_cast<DICompositeType>(DT)) { |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 158 | processType(DCT->getBaseType().resolve(TypeIdentifierMap)); |
Anders Waldenborg | 1433fd4 | 2015-04-14 09:18:17 +0000 | [diff] [blame] | 159 | for (Metadata *D : DCT->getElements()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 160 | if (auto *T = dyn_cast<DIType>(D)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 161 | processType(T); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 162 | else if (auto *SP = dyn_cast<DISubprogram>(D)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 163 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 164 | } |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | if (auto *DDT = dyn_cast<DIDerivedType>(DT)) { |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 168 | processType(DDT->getBaseType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 172 | void DebugInfoFinder::processScope(DIScope *Scope) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 173 | if (!Scope) |
| 174 | return; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 175 | if (auto *Ty = dyn_cast<DIType>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 176 | processType(Ty); |
| 177 | return; |
| 178 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 179 | if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 180 | addCompileUnit(CU); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 181 | return; |
| 182 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 183 | if (auto *SP = dyn_cast<DISubprogram>(Scope)) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 184 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | if (!addScope(Scope)) |
| 188 | return; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 189 | if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 190 | processScope(LB->getScope()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 191 | } else if (auto *NS = dyn_cast<DINamespace>(Scope)) { |
Duncan P. N. Exon Smith | 20caafb | 2015-04-14 03:01:27 +0000 | [diff] [blame] | 192 | processScope(NS->getScope()); |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 193 | } else if (auto *M = dyn_cast<DIModule>(Scope)) { |
| 194 | processScope(M->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 198 | void DebugInfoFinder::processSubprogram(DISubprogram *SP) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 199 | if (!addSubprogram(SP)) |
| 200 | return; |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 201 | processScope(SP->getScope().resolve(TypeIdentifierMap)); |
| 202 | processType(SP->getType()); |
| 203 | for (auto *Element : SP->getTemplateParams()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 204 | if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { |
Duncan P. N. Exon Smith | 20caafb | 2015-04-14 03:01:27 +0000 | [diff] [blame] | 205 | processType(TType->getType().resolve(TypeIdentifierMap)); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 206 | } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { |
Duncan P. N. Exon Smith | 20caafb | 2015-04-14 03:01:27 +0000 | [diff] [blame] | 207 | processType(TVal->getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 212 | void DebugInfoFinder::processDeclare(const Module &M, |
| 213 | const DbgDeclareInst *DDI) { |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 214 | auto *N = dyn_cast<MDNode>(DDI->getVariable()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 215 | if (!N) |
| 216 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 217 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 218 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 219 | auto *DV = dyn_cast<DILocalVariable>(N); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 220 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 221 | return; |
| 222 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 223 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 224 | return; |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 225 | processScope(DV->getScope()); |
| 226 | processType(DV->getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 229 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 230 | auto *N = dyn_cast<MDNode>(DVI->getVariable()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 231 | if (!N) |
| 232 | return; |
Manman Ren | b46e550 | 2013-11-17 19:35:03 +0000 | [diff] [blame] | 233 | InitializeTypeMap(M); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 234 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 235 | auto *DV = dyn_cast<DILocalVariable>(N); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 236 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 237 | return; |
| 238 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 239 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 240 | return; |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 241 | processScope(DV->getScope()); |
| 242 | processType(DV->getType().resolve(TypeIdentifierMap)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 245 | bool DebugInfoFinder::addType(DIType *DT) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 246 | if (!DT) |
| 247 | return false; |
| 248 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 249 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 250 | return false; |
| 251 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 252 | TYs.push_back(const_cast<DIType *>(DT)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 256 | bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 257 | if (!CU) |
| 258 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 259 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 260 | return false; |
| 261 | |
| 262 | CUs.push_back(CU); |
| 263 | return true; |
| 264 | } |
| 265 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 266 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 267 | if (!DIG) |
| 268 | return false; |
| 269 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 270 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 271 | return false; |
| 272 | |
| 273 | GVs.push_back(DIG); |
| 274 | return true; |
| 275 | } |
| 276 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 277 | bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 278 | if (!SP) |
| 279 | return false; |
| 280 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 281 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 282 | return false; |
| 283 | |
| 284 | SPs.push_back(SP); |
| 285 | return true; |
| 286 | } |
| 287 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 288 | bool DebugInfoFinder::addScope(DIScope *Scope) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 289 | if (!Scope) |
| 290 | return false; |
| 291 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 292 | // as null for now. |
| 293 | if (Scope->getNumOperands() == 0) |
| 294 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 295 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 296 | return false; |
| 297 | Scopes.push_back(Scope); |
| 298 | return true; |
| 299 | } |
| 300 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 301 | bool llvm::stripDebugInfo(Function &F) { |
| 302 | bool Changed = false; |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 303 | if (F.getSubprogram()) { |
| 304 | Changed = true; |
| 305 | F.setSubprogram(nullptr); |
| 306 | } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 307 | for (BasicBlock &BB : F) { |
| 308 | for (Instruction &I : BB) { |
| 309 | if (I.getDebugLoc()) { |
| 310 | Changed = true; |
| 311 | I.setDebugLoc(DebugLoc()); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | return Changed; |
| 316 | } |
| 317 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 318 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 319 | bool Changed = false; |
| 320 | |
| 321 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 322 | // the module. |
| 323 | if (Function *Declare = M.getFunction("llvm.dbg.declare")) { |
| 324 | while (!Declare->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 325 | CallInst *CI = cast<CallInst>(Declare->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 326 | CI->eraseFromParent(); |
| 327 | } |
| 328 | Declare->eraseFromParent(); |
| 329 | Changed = true; |
| 330 | } |
| 331 | |
| 332 | if (Function *DbgVal = M.getFunction("llvm.dbg.value")) { |
| 333 | while (!DbgVal->use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 334 | CallInst *CI = cast<CallInst>(DbgVal->user_back()); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 335 | CI->eraseFromParent(); |
| 336 | } |
| 337 | DbgVal->eraseFromParent(); |
| 338 | Changed = true; |
| 339 | } |
| 340 | |
| 341 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 342 | NME = M.named_metadata_end(); NMI != NME;) { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 343 | NamedMDNode *NMD = &*NMI; |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 344 | ++NMI; |
| 345 | if (NMD->getName().startswith("llvm.dbg.")) { |
| 346 | NMD->eraseFromParent(); |
| 347 | Changed = true; |
| 348 | } |
| 349 | } |
| 350 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 351 | for (Function &F : M) |
| 352 | Changed |= stripDebugInfo(F); |
| 353 | |
Rafael Espindola | 468b868 | 2015-04-01 14:44:59 +0000 | [diff] [blame] | 354 | if (GVMaterializer *Materializer = M.getMaterializer()) |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 355 | Materializer->setStripDebugInfo(); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 356 | |
| 357 | return Changed; |
| 358 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 359 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 360 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 361 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 362 | M.getModuleFlag("Debug Info Version"))) |
| 363 | return Val->getZExtValue(); |
| 364 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 365 | } |