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