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" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
Adrian Prantl | b141683 | 2014-08-01 22:11:58 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DIBuilder.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DerivedTypes.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GVMaterializer.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Instructions.h" |
| 24 | #include "llvm/IR/IntrinsicInst.h" |
| 25 | #include "llvm/IR/Intrinsics.h" |
| 26 | #include "llvm/IR/Module.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 27 | #include "llvm/IR/ValueHandle.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/Dwarf.h" |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | using namespace llvm; |
| 32 | using namespace llvm::dwarf; |
| 33 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 34 | DISubprogram *llvm::getDISubprogram(const MDNode *Scope) { |
| 35 | if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope)) |
Duncan P. N. Exon Smith | dd77af8 | 2015-03-31 02:06:28 +0000 | [diff] [blame] | 36 | return LocalScope->getSubprogram(); |
| 37 | return nullptr; |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // DebugInfoFinder implementations. |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | void DebugInfoFinder::reset() { |
| 45 | CUs.clear(); |
| 46 | SPs.clear(); |
| 47 | GVs.clear(); |
| 48 | TYs.clear(); |
| 49 | Scopes.clear(); |
| 50 | NodesSeen.clear(); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 53 | void DebugInfoFinder::processModule(const Module &M) { |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 54 | for (auto *CU : M.debug_compile_units()) { |
| 55 | addCompileUnit(CU); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 56 | for (auto DIG : CU->getGlobalVariables()) { |
| 57 | if (!addGlobalVariable(DIG)) |
| 58 | continue; |
| 59 | auto *GV = DIG->getVariable(); |
| 60 | processScope(GV->getScope()); |
| 61 | processType(GV->getType().resolve()); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 62 | } |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 63 | for (auto *ET : CU->getEnumTypes()) |
| 64 | processType(ET); |
| 65 | for (auto *RT : CU->getRetainedTypes()) |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 66 | if (auto *T = dyn_cast<DIType>(RT)) |
| 67 | processType(T); |
| 68 | else |
| 69 | processSubprogram(cast<DISubprogram>(RT)); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 70 | for (auto *Import : CU->getImportedEntities()) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 71 | auto *Entity = Import->getEntity().resolve(); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 72 | if (auto *T = dyn_cast<DIType>(Entity)) |
| 73 | processType(T); |
| 74 | else if (auto *SP = dyn_cast<DISubprogram>(Entity)) |
Duncan P. N. Exon Smith | 000fa2c | 2015-04-07 04:14:33 +0000 | [diff] [blame] | 75 | processSubprogram(SP); |
Adrian Prantl | 5992a72 | 2016-04-08 22:43:03 +0000 | [diff] [blame] | 76 | else if (auto *NS = dyn_cast<DINamespace>(Entity)) |
| 77 | processScope(NS->getScope()); |
| 78 | else if (auto *M = dyn_cast<DIModule>(Entity)) |
| 79 | processScope(M->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Adrian Prantl | 75819ae | 2016-04-15 15:57:41 +0000 | [diff] [blame] | 82 | for (auto &F : M.functions()) |
| 83 | if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram())) |
| 84 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 87 | void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 88 | if (!Loc) |
| 89 | return; |
Duncan P. N. Exon Smith | b7e221b | 2015-04-14 01:35:55 +0000 | [diff] [blame] | 90 | processScope(Loc->getScope()); |
| 91 | processLocation(M, Loc->getInlinedAt()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 94 | void DebugInfoFinder::processType(DIType *DT) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 95 | if (!addType(DT)) |
| 96 | return; |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 97 | processScope(DT->getScope().resolve()); |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 98 | if (auto *ST = dyn_cast<DISubroutineType>(DT)) { |
| 99 | for (DITypeRef Ref : ST->getTypeArray()) |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 100 | processType(Ref.resolve()); |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | if (auto *DCT = dyn_cast<DICompositeType>(DT)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 104 | processType(DCT->getBaseType().resolve()); |
Anders Waldenborg | 1433fd4 | 2015-04-14 09:18:17 +0000 | [diff] [blame] | 105 | for (Metadata *D : DCT->getElements()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 106 | if (auto *T = dyn_cast<DIType>(D)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 107 | processType(T); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 108 | else if (auto *SP = dyn_cast<DISubprogram>(D)) |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 109 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 110 | } |
Duncan P. N. Exon Smith | 260fa8a | 2015-07-24 20:56:10 +0000 | [diff] [blame] | 111 | return; |
| 112 | } |
| 113 | if (auto *DDT = dyn_cast<DIDerivedType>(DT)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 114 | processType(DDT->getBaseType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 118 | void DebugInfoFinder::processScope(DIScope *Scope) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 119 | if (!Scope) |
| 120 | return; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 121 | if (auto *Ty = dyn_cast<DIType>(Scope)) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 122 | processType(Ty); |
| 123 | return; |
| 124 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 125 | if (auto *CU = dyn_cast<DICompileUnit>(Scope)) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 126 | addCompileUnit(CU); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 127 | return; |
| 128 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 129 | if (auto *SP = dyn_cast<DISubprogram>(Scope)) { |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 130 | processSubprogram(SP); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | if (!addScope(Scope)) |
| 134 | return; |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 135 | if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) { |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 136 | processScope(LB->getScope()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 137 | } else if (auto *NS = dyn_cast<DINamespace>(Scope)) { |
Duncan P. N. Exon Smith | 20caafb | 2015-04-14 03:01:27 +0000 | [diff] [blame] | 138 | processScope(NS->getScope()); |
Adrian Prantl | ab1243f | 2015-06-29 23:03:47 +0000 | [diff] [blame] | 139 | } else if (auto *M = dyn_cast<DIModule>(Scope)) { |
| 140 | processScope(M->getScope()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 144 | void DebugInfoFinder::processSubprogram(DISubprogram *SP) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 145 | if (!addSubprogram(SP)) |
| 146 | return; |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 147 | processScope(SP->getScope().resolve()); |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 148 | processType(SP->getType()); |
| 149 | for (auto *Element : SP->getTemplateParams()) { |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 150 | if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 151 | processType(TType->getType().resolve()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 152 | } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) { |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 153 | processType(TVal->getType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 158 | void DebugInfoFinder::processDeclare(const Module &M, |
| 159 | const DbgDeclareInst *DDI) { |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 160 | auto *N = dyn_cast<MDNode>(DDI->getVariable()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 161 | if (!N) |
| 162 | return; |
| 163 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 164 | auto *DV = dyn_cast<DILocalVariable>(N); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 165 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 166 | return; |
| 167 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 168 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 169 | return; |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 170 | processScope(DV->getScope()); |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 171 | processType(DV->getType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Manman Ren | 2085ccc | 2013-11-17 18:42:37 +0000 | [diff] [blame] | 174 | void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) { |
Duncan P. N. Exon Smith | ed557b5 | 2015-04-17 23:20:10 +0000 | [diff] [blame] | 175 | auto *N = dyn_cast<MDNode>(DVI->getVariable()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 176 | if (!N) |
| 177 | return; |
| 178 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 179 | auto *DV = dyn_cast<DILocalVariable>(N); |
Duncan P. N. Exon Smith | 9d1cf4c | 2015-04-06 23:18:49 +0000 | [diff] [blame] | 180 | if (!DV) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 181 | return; |
| 182 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 183 | if (!NodesSeen.insert(DV).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 184 | return; |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 185 | processScope(DV->getScope()); |
Duncan P. N. Exon Smith | a59d3e5 | 2016-04-23 21:08:00 +0000 | [diff] [blame] | 186 | processType(DV->getType().resolve()); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 189 | bool DebugInfoFinder::addType(DIType *DT) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 190 | if (!DT) |
| 191 | return false; |
| 192 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 193 | if (!NodesSeen.insert(DT).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 194 | return false; |
| 195 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 196 | TYs.push_back(const_cast<DIType *>(DT)); |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 200 | bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 201 | if (!CU) |
| 202 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 203 | if (!NodesSeen.insert(CU).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 204 | return false; |
| 205 | |
| 206 | CUs.push_back(CU); |
| 207 | return true; |
| 208 | } |
| 209 | |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 210 | bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 211 | if (!NodesSeen.insert(DIG).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 212 | return false; |
| 213 | |
| 214 | GVs.push_back(DIG); |
| 215 | return true; |
| 216 | } |
| 217 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 218 | bool DebugInfoFinder::addSubprogram(DISubprogram *SP) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 219 | if (!SP) |
| 220 | return false; |
| 221 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 222 | if (!NodesSeen.insert(SP).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 223 | return false; |
| 224 | |
| 225 | SPs.push_back(SP); |
| 226 | return true; |
| 227 | } |
| 228 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 229 | bool DebugInfoFinder::addScope(DIScope *Scope) { |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 230 | if (!Scope) |
| 231 | return false; |
| 232 | // FIXME: Ocaml binding generates a scope with no content, we treat it |
| 233 | // as null for now. |
| 234 | if (Scope->getNumOperands() == 0) |
| 235 | return false; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 236 | if (!NodesSeen.insert(Scope).second) |
Bill Wendling | 523bea8 | 2013-11-08 08:13:15 +0000 | [diff] [blame] | 237 | return false; |
| 238 | Scopes.push_back(Scope); |
| 239 | return true; |
| 240 | } |
| 241 | |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 242 | static llvm::MDNode *stripDebugLocFromLoopID(llvm::MDNode *N) { |
| 243 | assert(N->op_begin() != N->op_end() && "Missing self reference?"); |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 244 | |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame^] | 245 | // if there is no debug location, we do not have to rewrite this MDNode. |
| 246 | if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { |
| 247 | return isa<DILocation>(Op.get()); |
| 248 | })) |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 249 | return N; |
| 250 | |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame^] | 251 | // If there is only the debug location without any actual loop metadata, we |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 252 | // can remove the metadata. |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame^] | 253 | if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) { |
| 254 | return !isa<DILocation>(Op.get()); |
| 255 | })) |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 256 | return nullptr; |
| 257 | |
| 258 | SmallVector<Metadata *, 4> Args; |
| 259 | // Reserve operand 0 for loop id self reference. |
| 260 | auto TempNode = MDNode::getTemporary(N->getContext(), None); |
| 261 | Args.push_back(TempNode.get()); |
Teresa Johnson | 9b4b8c8 | 2017-03-19 13:54:57 +0000 | [diff] [blame^] | 262 | // Add all non-debug location operands back. |
| 263 | for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) { |
| 264 | if (!isa<DILocation>(*Op)) |
| 265 | Args.push_back(*Op); |
| 266 | } |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 267 | |
| 268 | // Set the first operand to itself. |
| 269 | MDNode *LoopID = MDNode::get(N->getContext(), Args); |
| 270 | LoopID->replaceOperandWith(0, LoopID); |
| 271 | return LoopID; |
| 272 | } |
| 273 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 274 | bool llvm::stripDebugInfo(Function &F) { |
| 275 | bool Changed = false; |
Peter Collingbourne | d4bff30 | 2015-11-05 22:03:56 +0000 | [diff] [blame] | 276 | if (F.getSubprogram()) { |
| 277 | Changed = true; |
| 278 | F.setSubprogram(nullptr); |
| 279 | } |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 280 | |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 281 | llvm::DenseMap<llvm::MDNode*, llvm::MDNode*> LoopIDsMap; |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 282 | for (BasicBlock &BB : F) { |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 283 | for (auto II = BB.begin(), End = BB.end(); II != End;) { |
| 284 | Instruction &I = *II++; // We may delete the instruction, increment now. |
Mehdi Amini | db8dd55 | 2016-05-14 04:58:35 +0000 | [diff] [blame] | 285 | if (isa<DbgInfoIntrinsic>(&I)) { |
| 286 | I.eraseFromParent(); |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 287 | Changed = true; |
Mehdi Amini | bbedb14 | 2016-05-07 05:07:47 +0000 | [diff] [blame] | 288 | continue; |
Mehdi Amini | 581f0e1 | 2016-05-07 04:10:52 +0000 | [diff] [blame] | 289 | } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 290 | if (I.getDebugLoc()) { |
| 291 | Changed = true; |
| 292 | I.setDebugLoc(DebugLoc()); |
| 293 | } |
| 294 | } |
Daniel Sanders | b96a945 | 2017-01-28 11:22:05 +0000 | [diff] [blame] | 295 | |
| 296 | auto *TermInst = BB.getTerminator(); |
| 297 | if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) { |
| 298 | auto *NewLoopID = LoopIDsMap.lookup(LoopID); |
| 299 | if (!NewLoopID) |
| 300 | NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID); |
| 301 | if (NewLoopID != LoopID) |
| 302 | TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID); |
| 303 | } |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 304 | } |
| 305 | return Changed; |
| 306 | } |
| 307 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 308 | bool llvm::StripDebugInfo(Module &M) { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 309 | bool Changed = false; |
| 310 | |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 311 | for (Module::named_metadata_iterator NMI = M.named_metadata_begin(), |
| 312 | NME = M.named_metadata_end(); NMI != NME;) { |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 313 | NamedMDNode *NMD = &*NMI; |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 314 | ++NMI; |
Davide Italiano | 84bd58e | 2016-10-17 20:05:35 +0000 | [diff] [blame] | 315 | |
| 316 | // We're stripping debug info, and without them, coverage information |
| 317 | // doesn't quite make sense. |
| 318 | if (NMD->getName().startswith("llvm.dbg.") || |
| 319 | NMD->getName() == "llvm.gcov") { |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 320 | NMD->eraseFromParent(); |
| 321 | Changed = true; |
| 322 | } |
| 323 | } |
| 324 | |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 325 | for (Function &F : M) |
| 326 | Changed |= stripDebugInfo(F); |
| 327 | |
Adrian Prantl | 3bfe109 | 2016-10-10 17:53:33 +0000 | [diff] [blame] | 328 | for (auto &GV : M.globals()) { |
| 329 | SmallVector<MDNode *, 1> MDs; |
| 330 | GV.getMetadata(LLVMContext::MD_dbg, MDs); |
| 331 | if (!MDs.empty()) { |
| 332 | GV.eraseMetadata(LLVMContext::MD_dbg); |
| 333 | Changed = true; |
| 334 | } |
| 335 | } |
| 336 | |
Rafael Espindola | 468b868 | 2015-04-01 14:44:59 +0000 | [diff] [blame] | 337 | if (GVMaterializer *Materializer = M.getMaterializer()) |
Rafael Espindola | 0d68b4c | 2015-03-30 21:36:43 +0000 | [diff] [blame] | 338 | Materializer->setStripDebugInfo(); |
Manman Ren | cb14bbc | 2013-11-22 22:06:31 +0000 | [diff] [blame] | 339 | |
| 340 | return Changed; |
| 341 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 342 | |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 343 | namespace { |
| 344 | |
| 345 | /// Helper class to downgrade -g metadata to -gline-tables-only metadata. |
| 346 | class DebugTypeInfoRemoval { |
| 347 | DenseMap<Metadata *, Metadata *> Replacements; |
| 348 | |
| 349 | public: |
| 350 | /// The (void)() type. |
| 351 | MDNode *EmptySubroutineType; |
| 352 | |
| 353 | private: |
| 354 | /// Remember what linkage name we originally had before stripping. If we end |
| 355 | /// up making two subprograms identical who originally had different linkage |
| 356 | /// names, then we need to make one of them distinct, to avoid them getting |
| 357 | /// uniqued. Maps the new node to the old linkage name. |
| 358 | DenseMap<DISubprogram *, StringRef> NewToLinkageName; |
| 359 | |
| 360 | // TODO: Remember the distinct subprogram we created for a given linkage name, |
| 361 | // so that we can continue to unique whenever possible. Map <newly created |
| 362 | // node, old linkage name> to the first (possibly distinct) mdsubprogram |
| 363 | // created for that combination. This is not strictly needed for correctness, |
| 364 | // but can cut down on the number of MDNodes and let us diff cleanly with the |
| 365 | // output of -gline-tables-only. |
| 366 | |
| 367 | public: |
| 368 | DebugTypeInfoRemoval(LLVMContext &C) |
| 369 | : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0, |
| 370 | MDNode::get(C, {}))) {} |
| 371 | |
| 372 | Metadata *map(Metadata *M) { |
| 373 | if (!M) |
| 374 | return nullptr; |
| 375 | auto Replacement = Replacements.find(M); |
| 376 | if (Replacement != Replacements.end()) |
| 377 | return Replacement->second; |
| 378 | |
| 379 | return M; |
| 380 | } |
| 381 | MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); } |
| 382 | |
| 383 | /// Recursively remap N and all its referenced children. Does a DF post-order |
| 384 | /// traversal, so as to remap bottoms up. |
| 385 | void traverseAndRemap(MDNode *N) { traverse(N); } |
| 386 | |
| 387 | private: |
| 388 | // Create a new DISubprogram, to replace the one given. |
| 389 | DISubprogram *getReplacementSubprogram(DISubprogram *MDS) { |
| 390 | auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile())); |
| 391 | StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : ""; |
| 392 | DISubprogram *Declaration = nullptr; |
| 393 | auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType())); |
| 394 | DITypeRef ContainingType(map(MDS->getContainingType())); |
| 395 | auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit())); |
| 396 | auto Variables = nullptr; |
| 397 | auto TemplateParams = nullptr; |
| 398 | |
| 399 | // Make a distinct DISubprogram, for situations that warrent it. |
| 400 | auto distinctMDSubprogram = [&]() { |
| 401 | return DISubprogram::getDistinct( |
| 402 | MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, |
| 403 | FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(), |
| 404 | MDS->isDefinition(), MDS->getScopeLine(), ContainingType, |
| 405 | MDS->getVirtuality(), MDS->getVirtualIndex(), |
| 406 | MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit, |
| 407 | TemplateParams, Declaration, Variables); |
| 408 | }; |
| 409 | |
| 410 | if (MDS->isDistinct()) |
| 411 | return distinctMDSubprogram(); |
| 412 | |
| 413 | auto *NewMDS = DISubprogram::get( |
| 414 | MDS->getContext(), FileAndScope, MDS->getName(), LinkageName, |
| 415 | FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(), |
| 416 | MDS->isDefinition(), MDS->getScopeLine(), ContainingType, |
| 417 | MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(), |
| 418 | MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration, |
| 419 | Variables); |
| 420 | |
| 421 | StringRef OldLinkageName = MDS->getLinkageName(); |
| 422 | |
| 423 | // See if we need to make a distinct one. |
| 424 | auto OrigLinkage = NewToLinkageName.find(NewMDS); |
| 425 | if (OrigLinkage != NewToLinkageName.end()) { |
| 426 | if (OrigLinkage->second == OldLinkageName) |
| 427 | // We're good. |
| 428 | return NewMDS; |
| 429 | |
| 430 | // Otherwise, need to make a distinct one. |
| 431 | // TODO: Query the map to see if we already have one. |
| 432 | return distinctMDSubprogram(); |
| 433 | } |
| 434 | |
| 435 | NewToLinkageName.insert({NewMDS, MDS->getLinkageName()}); |
| 436 | return NewMDS; |
| 437 | } |
| 438 | |
| 439 | /// Create a new compile unit, to replace the one given |
| 440 | DICompileUnit *getReplacementCU(DICompileUnit *CU) { |
| 441 | // Drop skeleton CUs. |
| 442 | if (CU->getDWOId()) |
| 443 | return nullptr; |
| 444 | |
| 445 | auto *File = cast_or_null<DIFile>(map(CU->getFile())); |
| 446 | MDTuple *EnumTypes = nullptr; |
| 447 | MDTuple *RetainedTypes = nullptr; |
| 448 | MDTuple *GlobalVariables = nullptr; |
| 449 | MDTuple *ImportedEntities = nullptr; |
| 450 | return DICompileUnit::getDistinct( |
| 451 | CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(), |
| 452 | CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(), |
| 453 | CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes, |
| 454 | RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(), |
Dehao Chen | 0944a8c | 2017-02-01 22:45:09 +0000 | [diff] [blame] | 455 | CU->getDWOId(), CU->getSplitDebugInlining(), |
| 456 | CU->getDebugInfoForProfiling()); |
Michael Ilseman | e542804 | 2016-10-25 18:44:13 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | DILocation *getReplacementMDLocation(DILocation *MLD) { |
| 460 | auto *Scope = map(MLD->getScope()); |
| 461 | auto *InlinedAt = map(MLD->getInlinedAt()); |
| 462 | if (MLD->isDistinct()) |
| 463 | return DILocation::getDistinct(MLD->getContext(), MLD->getLine(), |
| 464 | MLD->getColumn(), Scope, InlinedAt); |
| 465 | return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(), |
| 466 | Scope, InlinedAt); |
| 467 | } |
| 468 | |
| 469 | /// Create a new generic MDNode, to replace the one given |
| 470 | MDNode *getReplacementMDNode(MDNode *N) { |
| 471 | SmallVector<Metadata *, 8> Ops; |
| 472 | Ops.reserve(N->getNumOperands()); |
| 473 | for (auto &I : N->operands()) |
| 474 | if (I) |
| 475 | Ops.push_back(map(I)); |
| 476 | auto *Ret = MDNode::get(N->getContext(), Ops); |
| 477 | return Ret; |
| 478 | } |
| 479 | |
| 480 | /// Attempt to re-map N to a newly created node. |
| 481 | void remap(MDNode *N) { |
| 482 | if (Replacements.count(N)) |
| 483 | return; |
| 484 | |
| 485 | auto doRemap = [&](MDNode *N) -> MDNode * { |
| 486 | if (!N) |
| 487 | return nullptr; |
| 488 | if (auto *MDSub = dyn_cast<DISubprogram>(N)) { |
| 489 | remap(MDSub->getUnit()); |
| 490 | return getReplacementSubprogram(MDSub); |
| 491 | } |
| 492 | if (isa<DISubroutineType>(N)) |
| 493 | return EmptySubroutineType; |
| 494 | if (auto *CU = dyn_cast<DICompileUnit>(N)) |
| 495 | return getReplacementCU(CU); |
| 496 | if (isa<DIFile>(N)) |
| 497 | return N; |
| 498 | if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N)) |
| 499 | // Remap to our referenced scope (recursively). |
| 500 | return mapNode(MDLB->getScope()); |
| 501 | if (auto *MLD = dyn_cast<DILocation>(N)) |
| 502 | return getReplacementMDLocation(MLD); |
| 503 | |
| 504 | // Otherwise, if we see these, just drop them now. Not strictly necessary, |
| 505 | // but this speeds things up a little. |
| 506 | if (isa<DINode>(N)) |
| 507 | return nullptr; |
| 508 | |
| 509 | return getReplacementMDNode(N); |
| 510 | }; |
| 511 | Replacements[N] = doRemap(N); |
| 512 | } |
| 513 | |
| 514 | /// Do the remapping traversal. |
| 515 | void traverse(MDNode *); |
| 516 | }; |
| 517 | |
| 518 | } // Anonymous namespace. |
| 519 | |
| 520 | void DebugTypeInfoRemoval::traverse(MDNode *N) { |
| 521 | if (!N || Replacements.count(N)) |
| 522 | return; |
| 523 | |
| 524 | // To avoid cycles, as well as for efficiency sake, we will sometimes prune |
| 525 | // parts of the graph. |
| 526 | auto prune = [](MDNode *Parent, MDNode *Child) { |
| 527 | if (auto *MDS = dyn_cast<DISubprogram>(Parent)) |
| 528 | return Child == MDS->getVariables().get(); |
| 529 | return false; |
| 530 | }; |
| 531 | |
| 532 | SmallVector<MDNode *, 16> ToVisit; |
| 533 | DenseSet<MDNode *> Opened; |
| 534 | |
| 535 | // Visit each node starting at N in post order, and map them. |
| 536 | ToVisit.push_back(N); |
| 537 | while (!ToVisit.empty()) { |
| 538 | auto *N = ToVisit.back(); |
| 539 | if (!Opened.insert(N).second) { |
| 540 | // Close it. |
| 541 | remap(N); |
| 542 | ToVisit.pop_back(); |
| 543 | continue; |
| 544 | } |
| 545 | for (auto &I : N->operands()) |
| 546 | if (auto *MDN = dyn_cast_or_null<MDNode>(I)) |
| 547 | if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) && |
| 548 | !isa<DICompileUnit>(MDN)) |
| 549 | ToVisit.push_back(MDN); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | bool llvm::stripNonLineTableDebugInfo(Module &M) { |
| 554 | bool Changed = false; |
| 555 | |
| 556 | // First off, delete the debug intrinsics. |
| 557 | auto RemoveUses = [&](StringRef Name) { |
| 558 | if (auto *DbgVal = M.getFunction(Name)) { |
| 559 | while (!DbgVal->use_empty()) |
| 560 | cast<Instruction>(DbgVal->user_back())->eraseFromParent(); |
| 561 | DbgVal->eraseFromParent(); |
| 562 | Changed = true; |
| 563 | } |
| 564 | }; |
| 565 | RemoveUses("llvm.dbg.declare"); |
| 566 | RemoveUses("llvm.dbg.value"); |
| 567 | |
| 568 | // Delete non-CU debug info named metadata nodes. |
| 569 | for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end(); |
| 570 | NMI != NME;) { |
| 571 | NamedMDNode *NMD = &*NMI; |
| 572 | ++NMI; |
| 573 | // Specifically keep dbg.cu around. |
| 574 | if (NMD->getName() == "llvm.dbg.cu") |
| 575 | continue; |
| 576 | } |
| 577 | |
| 578 | // Drop all dbg attachments from global variables. |
| 579 | for (auto &GV : M.globals()) |
| 580 | GV.eraseMetadata(LLVMContext::MD_dbg); |
| 581 | |
| 582 | DebugTypeInfoRemoval Mapper(M.getContext()); |
| 583 | auto remap = [&](llvm::MDNode *Node) -> llvm::MDNode * { |
| 584 | if (!Node) |
| 585 | return nullptr; |
| 586 | Mapper.traverseAndRemap(Node); |
| 587 | auto *NewNode = Mapper.mapNode(Node); |
| 588 | Changed |= Node != NewNode; |
| 589 | Node = NewNode; |
| 590 | return NewNode; |
| 591 | }; |
| 592 | |
| 593 | // Rewrite the DebugLocs to be equivalent to what |
| 594 | // -gline-tables-only would have created. |
| 595 | for (auto &F : M) { |
| 596 | if (auto *SP = F.getSubprogram()) { |
| 597 | Mapper.traverseAndRemap(SP); |
| 598 | auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP)); |
| 599 | Changed |= SP != NewSP; |
| 600 | F.setSubprogram(NewSP); |
| 601 | } |
| 602 | for (auto &BB : F) { |
| 603 | for (auto &I : BB) { |
| 604 | if (I.getDebugLoc() == DebugLoc()) |
| 605 | continue; |
| 606 | |
| 607 | // Make a replacement. |
| 608 | auto &DL = I.getDebugLoc(); |
| 609 | auto *Scope = DL.getScope(); |
| 610 | MDNode *InlinedAt = DL.getInlinedAt(); |
| 611 | Scope = remap(Scope); |
| 612 | InlinedAt = remap(InlinedAt); |
| 613 | I.setDebugLoc( |
| 614 | DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt)); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | // Create a new llvm.dbg.cu, which is equivalent to the one |
| 620 | // -gline-tables-only would have created. |
| 621 | for (auto &NMD : M.getNamedMDList()) { |
| 622 | SmallVector<MDNode *, 8> Ops; |
| 623 | for (MDNode *Op : NMD.operands()) |
| 624 | Ops.push_back(remap(Op)); |
| 625 | |
| 626 | if (!Changed) |
| 627 | continue; |
| 628 | |
| 629 | NMD.clearOperands(); |
| 630 | for (auto *Op : Ops) |
| 631 | if (Op) |
| 632 | NMD.addOperand(Op); |
| 633 | } |
| 634 | return Changed; |
| 635 | } |
| 636 | |
Manman Ren | bd4daf8 | 2013-12-03 00:12:14 +0000 | [diff] [blame] | 637 | unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) { |
David Majnemer | e7a9cdb | 2015-02-16 06:04:53 +0000 | [diff] [blame] | 638 | if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>( |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 639 | M.getModuleFlag("Debug Info Version"))) |
| 640 | return Val->getZExtValue(); |
| 641 | return 0; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 642 | } |