Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 1 | //===- Debugify.cpp - Attach synthetic debug info to everything -----------===// |
| 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 | /// \file This pass attaches synthetic debug info to everything. It can be used |
| 11 | /// to create targeted tests for debug info preservation. |
| 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Vedant Kumar | a4afd1f | 2018-07-24 00:41:28 +0000 | [diff] [blame] | 15 | #include "Debugify.h" |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/BitVector.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/IR/BasicBlock.h" |
| 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/DIBuilder.h" |
| 21 | #include "llvm/IR/DebugInfo.h" |
| 22 | #include "llvm/IR/Function.h" |
| 23 | #include "llvm/IR/GlobalVariable.h" |
| 24 | #include "llvm/IR/InstIterator.h" |
| 25 | #include "llvm/IR/Instruction.h" |
| 26 | #include "llvm/IR/Instructions.h" |
| 27 | #include "llvm/IR/IntrinsicInst.h" |
| 28 | #include "llvm/IR/Module.h" |
| 29 | #include "llvm/IR/Type.h" |
| 30 | #include "llvm/Pass.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | #include "llvm/Transforms/IPO.h" |
| 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | namespace { |
| 37 | |
Vedant Kumar | f0c081c | 2018-06-06 19:05:41 +0000 | [diff] [blame] | 38 | cl::opt<bool> Quiet("debugify-quiet", |
| 39 | cl::desc("Suppress verbose debugify output")); |
| 40 | |
| 41 | raw_ostream &dbg() { return Quiet ? nulls() : errs(); } |
| 42 | |
Vedant Kumar | acc9793 | 2018-06-26 22:46:41 +0000 | [diff] [blame] | 43 | uint64_t getAllocSizeInBits(Module &M, Type *Ty) { |
| 44 | return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0; |
| 45 | } |
| 46 | |
Vedant Kumar | da3c20c | 2018-02-15 21:28:38 +0000 | [diff] [blame] | 47 | bool isFunctionSkipped(Function &F) { |
| 48 | return F.isDeclaration() || !F.hasExactDefinition(); |
| 49 | } |
| 50 | |
Vedant Kumar | a959524 | 2018-06-06 19:05:42 +0000 | [diff] [blame] | 51 | /// Find the basic block's terminating instruction. |
Vedant Kumar | 0e9833d | 2018-06-05 00:56:07 +0000 | [diff] [blame] | 52 | /// |
Vedant Kumar | a959524 | 2018-06-06 19:05:42 +0000 | [diff] [blame] | 53 | /// Special care is needed to handle musttail and deopt calls, as these behave |
| 54 | /// like (but are in fact not) terminators. |
| 55 | Instruction *findTerminatingInstruction(BasicBlock &BB) { |
Vedant Kumar | 0e9833d | 2018-06-05 00:56:07 +0000 | [diff] [blame] | 56 | if (auto *I = BB.getTerminatingMustTailCall()) |
| 57 | return I; |
| 58 | if (auto *I = BB.getTerminatingDeoptimizeCall()) |
| 59 | return I; |
| 60 | return BB.getTerminator(); |
| 61 | } |
| 62 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 63 | bool applyDebugifyMetadata(Module &M, |
| 64 | iterator_range<Module::iterator> Functions, |
| 65 | StringRef Banner) { |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 66 | // Skip modules with debug info. |
| 67 | if (M.getNamedMetadata("llvm.dbg.cu")) { |
Vedant Kumar | f0c081c | 2018-06-06 19:05:41 +0000 | [diff] [blame] | 68 | dbg() << Banner << "Skipping module with debug info\n"; |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
| 72 | DIBuilder DIB(M); |
| 73 | LLVMContext &Ctx = M.getContext(); |
| 74 | |
| 75 | // Get a DIType which corresponds to Ty. |
| 76 | DenseMap<uint64_t, DIType *> TypeCache; |
| 77 | auto getCachedDIType = [&](Type *Ty) -> DIType * { |
Vedant Kumar | acc9793 | 2018-06-26 22:46:41 +0000 | [diff] [blame] | 78 | uint64_t Size = getAllocSizeInBits(M, Ty); |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 79 | DIType *&DTy = TypeCache[Size]; |
| 80 | if (!DTy) { |
| 81 | std::string Name = "ty" + utostr(Size); |
| 82 | DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned); |
| 83 | } |
| 84 | return DTy; |
| 85 | }; |
| 86 | |
| 87 | unsigned NextLine = 1; |
| 88 | unsigned NextVar = 1; |
| 89 | auto File = DIB.createFile(M.getName(), "/"); |
Vedant Kumar | 61b411c | 2018-06-05 00:56:07 +0000 | [diff] [blame] | 90 | auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "debugify", |
| 91 | /*isOptimized=*/true, "", 0); |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 92 | |
| 93 | // Visit each instruction. |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 94 | for (Function &F : Functions) { |
Vedant Kumar | da3c20c | 2018-02-15 21:28:38 +0000 | [diff] [blame] | 95 | if (isFunctionSkipped(F)) |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 96 | continue; |
| 97 | |
| 98 | auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None)); |
Paul Robinson | eaa7353 | 2018-11-19 18:29:28 +0000 | [diff] [blame] | 99 | DISubprogram::DISPFlags SPFlags = |
| 100 | DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized; |
| 101 | if (F.hasPrivateLinkage() || F.hasInternalLinkage()) |
| 102 | SPFlags |= DISubprogram::SPFlagLocalToUnit; |
| 103 | auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine, |
| 104 | SPType, NextLine, DINode::FlagZero, SPFlags); |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 105 | F.setSubprogram(SP); |
| 106 | for (BasicBlock &BB : F) { |
| 107 | // Attach debug locations. |
| 108 | for (Instruction &I : BB) |
| 109 | I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP)); |
| 110 | |
Vedant Kumar | 2373850 | 2018-06-03 22:50:22 +0000 | [diff] [blame] | 111 | // Inserting debug values into EH pads can break IR invariants. |
| 112 | if (BB.isEHPad()) |
| 113 | continue; |
| 114 | |
Vedant Kumar | a959524 | 2018-06-06 19:05:42 +0000 | [diff] [blame] | 115 | // Find the terminating instruction, after which no debug values are |
| 116 | // attached. |
| 117 | Instruction *LastInst = findTerminatingInstruction(BB); |
| 118 | assert(LastInst && "Expected basic block with a terminator"); |
| 119 | |
| 120 | // Maintain an insertion point which can't be invalidated when updates |
| 121 | // are made. |
| 122 | BasicBlock::iterator InsertPt = BB.getFirstInsertionPt(); |
| 123 | assert(InsertPt != BB.end() && "Expected to find an insertion point"); |
| 124 | Instruction *InsertBefore = &*InsertPt; |
Vedant Kumar | 4f55de6 | 2018-06-04 03:33:01 +0000 | [diff] [blame] | 125 | |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 126 | // Attach debug values. |
Vedant Kumar | a959524 | 2018-06-06 19:05:42 +0000 | [diff] [blame] | 127 | for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) { |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 128 | // Skip void-valued instructions. |
Vedant Kumar | a959524 | 2018-06-06 19:05:42 +0000 | [diff] [blame] | 129 | if (I->getType()->isVoidTy()) |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 130 | continue; |
| 131 | |
Vedant Kumar | a959524 | 2018-06-06 19:05:42 +0000 | [diff] [blame] | 132 | // Phis and EH pads must be grouped at the beginning of the block. |
| 133 | // Only advance the insertion point when we finish visiting these. |
| 134 | if (!isa<PHINode>(I) && !I->isEHPad()) |
| 135 | InsertBefore = I->getNextNode(); |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 136 | |
| 137 | std::string Name = utostr(NextVar++); |
Vedant Kumar | a959524 | 2018-06-06 19:05:42 +0000 | [diff] [blame] | 138 | const DILocation *Loc = I->getDebugLoc().get(); |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 139 | auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(), |
Vedant Kumar | a959524 | 2018-06-06 19:05:42 +0000 | [diff] [blame] | 140 | getCachedDIType(I->getType()), |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 141 | /*AlwaysPreserve=*/true); |
Vedant Kumar | a959524 | 2018-06-06 19:05:42 +0000 | [diff] [blame] | 142 | DIB.insertDbgValueIntrinsic(I, LocalVar, DIB.createExpression(), Loc, |
| 143 | InsertBefore); |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | DIB.finalizeSubprogram(SP); |
| 147 | } |
| 148 | DIB.finalize(); |
| 149 | |
| 150 | // Track the number of distinct lines and variables. |
| 151 | NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify"); |
| 152 | auto *IntTy = Type::getInt32Ty(Ctx); |
| 153 | auto addDebugifyOperand = [&](unsigned N) { |
| 154 | NMD->addOperand(MDNode::get( |
| 155 | Ctx, ValueAsMetadata::getConstant(ConstantInt::get(IntTy, N)))); |
| 156 | }; |
| 157 | addDebugifyOperand(NextLine - 1); // Original number of lines. |
| 158 | addDebugifyOperand(NextVar - 1); // Original number of variables. |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 159 | assert(NMD->getNumOperands() == 2 && |
| 160 | "llvm.debugify should have exactly 2 operands!"); |
Vedant Kumar | 646a736 | 2018-05-24 23:00:23 +0000 | [diff] [blame] | 161 | |
| 162 | // Claim that this synthetic debug info is valid. |
| 163 | StringRef DIVersionKey = "Debug Info Version"; |
| 164 | if (!M.getModuleFlag(DIVersionKey)) |
| 165 | M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION); |
| 166 | |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | |
Vedant Kumar | acc9793 | 2018-06-26 22:46:41 +0000 | [diff] [blame] | 170 | /// Return true if a mis-sized diagnostic is issued for \p DVI. |
| 171 | bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) { |
| 172 | // The size of a dbg.value's value operand should match the size of the |
| 173 | // variable it corresponds to. |
| 174 | // |
| 175 | // TODO: This, along with a check for non-null value operands, should be |
| 176 | // promoted to verifier failures. |
| 177 | Value *V = DVI->getValue(); |
| 178 | if (!V) |
| 179 | return false; |
| 180 | |
| 181 | // For now, don't try to interpret anything more complicated than an empty |
| 182 | // DIExpression. Eventually we should try to handle OP_deref and fragments. |
| 183 | if (DVI->getExpression()->getNumElements()) |
| 184 | return false; |
| 185 | |
| 186 | Type *Ty = V->getType(); |
| 187 | uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty); |
Vedant Kumar | 3dbfeaf | 2018-06-27 00:47:52 +0000 | [diff] [blame] | 188 | Optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits(); |
| 189 | if (!ValueOperandSize || !DbgVarSize) |
| 190 | return false; |
| 191 | |
Vedant Kumar | d364349 | 2018-07-06 17:32:40 +0000 | [diff] [blame] | 192 | bool HasBadSize = false; |
| 193 | if (Ty->isIntegerTy()) { |
| 194 | auto Signedness = DVI->getVariable()->getSignedness(); |
| 195 | if (Signedness && *Signedness == DIBasicType::Signedness::Signed) |
| 196 | HasBadSize = ValueOperandSize < *DbgVarSize; |
| 197 | } else { |
| 198 | HasBadSize = ValueOperandSize != *DbgVarSize; |
| 199 | } |
| 200 | |
Vedant Kumar | acc9793 | 2018-06-26 22:46:41 +0000 | [diff] [blame] | 201 | if (HasBadSize) { |
| 202 | dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize |
Vedant Kumar | 3dbfeaf | 2018-06-27 00:47:52 +0000 | [diff] [blame] | 203 | << ", but its variable has size " << *DbgVarSize << ": "; |
Vedant Kumar | acc9793 | 2018-06-26 22:46:41 +0000 | [diff] [blame] | 204 | DVI->print(dbg()); |
| 205 | dbg() << "\n"; |
| 206 | } |
| 207 | return HasBadSize; |
| 208 | } |
| 209 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 210 | bool checkDebugifyMetadata(Module &M, |
| 211 | iterator_range<Module::iterator> Functions, |
Vedant Kumar | 61b411c | 2018-06-05 00:56:07 +0000 | [diff] [blame] | 212 | StringRef NameOfWrappedPass, StringRef Banner, |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 213 | bool Strip, DebugifyStatsMap *StatsMap) { |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 214 | // Skip modules without debugify metadata. |
| 215 | NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify"); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 216 | if (!NMD) { |
Vedant Kumar | f0c081c | 2018-06-06 19:05:41 +0000 | [diff] [blame] | 217 | dbg() << Banner << "Skipping module without debugify metadata\n"; |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 218 | return false; |
| 219 | } |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 220 | |
| 221 | auto getDebugifyOperand = [&](unsigned Idx) -> unsigned { |
| 222 | return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0)) |
| 223 | ->getZExtValue(); |
| 224 | }; |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 225 | assert(NMD->getNumOperands() == 2 && |
| 226 | "llvm.debugify should have exactly 2 operands!"); |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 227 | unsigned OriginalNumLines = getDebugifyOperand(0); |
| 228 | unsigned OriginalNumVars = getDebugifyOperand(1); |
| 229 | bool HasErrors = false; |
| 230 | |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 231 | // Track debug info loss statistics if able. |
| 232 | DebugifyStatistics *Stats = nullptr; |
| 233 | if (StatsMap && !NameOfWrappedPass.empty()) |
| 234 | Stats = &StatsMap->operator[](NameOfWrappedPass); |
| 235 | |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 236 | BitVector MissingLines{OriginalNumLines, true}; |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 237 | BitVector MissingVars{OriginalNumVars, true}; |
| 238 | for (Function &F : Functions) { |
Vedant Kumar | da3c20c | 2018-02-15 21:28:38 +0000 | [diff] [blame] | 239 | if (isFunctionSkipped(F)) |
| 240 | continue; |
| 241 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 242 | // Find missing lines. |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 243 | for (Instruction &I : instructions(F)) { |
| 244 | if (isa<DbgValueInst>(&I)) |
| 245 | continue; |
| 246 | |
| 247 | auto DL = I.getDebugLoc(); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 248 | if (DL && DL.getLine() != 0) { |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 249 | MissingLines.reset(DL.getLine() - 1); |
| 250 | continue; |
| 251 | } |
| 252 | |
Vedant Kumar | 3d7cc9e | 2018-06-28 18:21:11 +0000 | [diff] [blame] | 253 | if (!DL) { |
| 254 | dbg() << "ERROR: Instruction with empty DebugLoc in function "; |
| 255 | dbg() << F.getName() << " --"; |
| 256 | I.print(dbg()); |
| 257 | dbg() << "\n"; |
| 258 | HasErrors = true; |
| 259 | } |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 260 | } |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 261 | |
Vedant Kumar | acc9793 | 2018-06-26 22:46:41 +0000 | [diff] [blame] | 262 | // Find missing variables and mis-sized debug values. |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 263 | for (Instruction &I : instructions(F)) { |
| 264 | auto *DVI = dyn_cast<DbgValueInst>(&I); |
| 265 | if (!DVI) |
| 266 | continue; |
| 267 | |
| 268 | unsigned Var = ~0U; |
| 269 | (void)to_integer(DVI->getVariable()->getName(), Var, 10); |
| 270 | assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable"); |
Vedant Kumar | acc9793 | 2018-06-26 22:46:41 +0000 | [diff] [blame] | 271 | bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI); |
| 272 | if (!HasBadSize) |
| 273 | MissingVars.reset(Var - 1); |
| 274 | HasErrors |= HasBadSize; |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 275 | } |
| 276 | } |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 277 | |
| 278 | // Print the results. |
| 279 | for (unsigned Idx : MissingLines.set_bits()) |
Vedant Kumar | f0c081c | 2018-06-06 19:05:41 +0000 | [diff] [blame] | 280 | dbg() << "WARNING: Missing line " << Idx + 1 << "\n"; |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 281 | |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 282 | for (unsigned Idx : MissingVars.set_bits()) |
Vedant Kumar | 9253369 | 2018-06-26 18:54:10 +0000 | [diff] [blame] | 283 | dbg() << "WARNING: Missing variable " << Idx + 1 << "\n"; |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 284 | |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 285 | // Update DI loss statistics. |
| 286 | if (Stats) { |
| 287 | Stats->NumDbgLocsExpected += OriginalNumLines; |
| 288 | Stats->NumDbgLocsMissing += MissingLines.count(); |
| 289 | Stats->NumDbgValuesExpected += OriginalNumVars; |
| 290 | Stats->NumDbgValuesMissing += MissingVars.count(); |
| 291 | } |
| 292 | |
Vedant Kumar | f0c081c | 2018-06-06 19:05:41 +0000 | [diff] [blame] | 293 | dbg() << Banner; |
Vedant Kumar | 951e605 | 2018-05-24 23:00:22 +0000 | [diff] [blame] | 294 | if (!NameOfWrappedPass.empty()) |
Vedant Kumar | f0c081c | 2018-06-06 19:05:41 +0000 | [diff] [blame] | 295 | dbg() << " [" << NameOfWrappedPass << "]"; |
| 296 | dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n'; |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 297 | |
| 298 | // Strip the Debugify Metadata if required. |
| 299 | if (Strip) { |
| 300 | StripDebugInfo(M); |
| 301 | M.eraseNamedMetadata(NMD); |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | return false; |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 308 | /// ModulePass for attaching synthetic debug info to everything, used with the |
| 309 | /// legacy module pass manager. |
| 310 | struct DebugifyModulePass : public ModulePass { |
| 311 | bool runOnModule(Module &M) override { |
| 312 | return applyDebugifyMetadata(M, M.functions(), "ModuleDebugify: "); |
| 313 | } |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 314 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 315 | DebugifyModulePass() : ModulePass(ID) {} |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 316 | |
| 317 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 318 | AU.setPreservesAll(); |
| 319 | } |
| 320 | |
| 321 | static char ID; // Pass identification. |
| 322 | }; |
| 323 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 324 | /// FunctionPass for attaching synthetic debug info to instructions within a |
| 325 | /// single function, used with the legacy module pass manager. |
| 326 | struct DebugifyFunctionPass : public FunctionPass { |
| 327 | bool runOnFunction(Function &F) override { |
| 328 | Module &M = *F.getParent(); |
| 329 | auto FuncIt = F.getIterator(); |
| 330 | return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)), |
Vedant Kumar | 61b411c | 2018-06-05 00:56:07 +0000 | [diff] [blame] | 331 | "FunctionDebugify: "); |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 334 | DebugifyFunctionPass() : FunctionPass(ID) {} |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 335 | |
| 336 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 337 | AU.setPreservesAll(); |
| 338 | } |
| 339 | |
| 340 | static char ID; // Pass identification. |
| 341 | }; |
| 342 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 343 | /// ModulePass for checking debug info inserted by -debugify, used with the |
| 344 | /// legacy module pass manager. |
| 345 | struct CheckDebugifyModulePass : public ModulePass { |
| 346 | bool runOnModule(Module &M) override { |
Anastasis Grammenos | d146023 | 2018-05-15 23:38:05 +0000 | [diff] [blame] | 347 | return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass, |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 348 | "CheckModuleDebugify", Strip, StatsMap); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 351 | CheckDebugifyModulePass(bool Strip = false, StringRef NameOfWrappedPass = "", |
| 352 | DebugifyStatsMap *StatsMap = nullptr) |
| 353 | : ModulePass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass), |
| 354 | StatsMap(StatsMap) {} |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 355 | |
Vedant Kumar | 8c3bbfc | 2018-06-04 21:43:28 +0000 | [diff] [blame] | 356 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 357 | AU.setPreservesAll(); |
| 358 | } |
| 359 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 360 | static char ID; // Pass identification. |
| 361 | |
| 362 | private: |
| 363 | bool Strip; |
Anastasis Grammenos | d146023 | 2018-05-15 23:38:05 +0000 | [diff] [blame] | 364 | StringRef NameOfWrappedPass; |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 365 | DebugifyStatsMap *StatsMap; |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | /// FunctionPass for checking debug info inserted by -debugify-function, used |
| 369 | /// with the legacy module pass manager. |
| 370 | struct CheckDebugifyFunctionPass : public FunctionPass { |
| 371 | bool runOnFunction(Function &F) override { |
| 372 | Module &M = *F.getParent(); |
| 373 | auto FuncIt = F.getIterator(); |
| 374 | return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)), |
Vedant Kumar | 646a736 | 2018-05-24 23:00:23 +0000 | [diff] [blame] | 375 | NameOfWrappedPass, "CheckFunctionDebugify", |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 376 | Strip, StatsMap); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Vedant Kumar | 646a736 | 2018-05-24 23:00:23 +0000 | [diff] [blame] | 379 | CheckDebugifyFunctionPass(bool Strip = false, |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 380 | StringRef NameOfWrappedPass = "", |
| 381 | DebugifyStatsMap *StatsMap = nullptr) |
| 382 | : FunctionPass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass), |
| 383 | StatsMap(StatsMap) {} |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 384 | |
| 385 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 386 | AU.setPreservesAll(); |
| 387 | } |
| 388 | |
| 389 | static char ID; // Pass identification. |
| 390 | |
| 391 | private: |
| 392 | bool Strip; |
Anastasis Grammenos | d146023 | 2018-05-15 23:38:05 +0000 | [diff] [blame] | 393 | StringRef NameOfWrappedPass; |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 394 | DebugifyStatsMap *StatsMap; |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 397 | } // end anonymous namespace |
| 398 | |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 399 | void exportDebugifyStats(llvm::StringRef Path, const DebugifyStatsMap &Map) { |
| 400 | std::error_code EC; |
| 401 | raw_fd_ostream OS{Path, EC}; |
| 402 | if (EC) { |
| 403 | errs() << "Could not open file: " << EC.message() << ", " << Path << '\n'; |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | OS << "Pass Name" << ',' << "# of missing debug values" << ',' |
| 408 | << "# of missing locations" << ',' << "Missing/Expected value ratio" << ',' |
| 409 | << "Missing/Expected location ratio" << '\n'; |
| 410 | for (const auto &Entry : Map) { |
| 411 | StringRef Pass = Entry.first; |
| 412 | DebugifyStatistics Stats = Entry.second; |
| 413 | |
| 414 | OS << Pass << ',' << Stats.NumDbgValuesMissing << ',' |
| 415 | << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ',' |
| 416 | << Stats.getEmptyLocationRatio() << '\n'; |
| 417 | } |
| 418 | } |
| 419 | |
Vedant Kumar | 61b411c | 2018-06-05 00:56:07 +0000 | [diff] [blame] | 420 | ModulePass *createDebugifyModulePass() { return new DebugifyModulePass(); } |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 421 | |
| 422 | FunctionPass *createDebugifyFunctionPass() { |
| 423 | return new DebugifyFunctionPass(); |
| 424 | } |
Vedant Kumar | 48c582c | 2018-01-23 20:43:50 +0000 | [diff] [blame] | 425 | |
Vedant Kumar | b8c067b | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 426 | PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) { |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 427 | applyDebugifyMetadata(M, M.functions(), "ModuleDebugify: "); |
Vedant Kumar | b8c067b | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 428 | return PreservedAnalyses::all(); |
| 429 | } |
| 430 | |
Vedant Kumar | 63a0fe2 | 2018-06-04 00:11:47 +0000 | [diff] [blame] | 431 | ModulePass *createCheckDebugifyModulePass(bool Strip, |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 432 | StringRef NameOfWrappedPass, |
| 433 | DebugifyStatsMap *StatsMap) { |
| 434 | return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Vedant Kumar | 63a0fe2 | 2018-06-04 00:11:47 +0000 | [diff] [blame] | 437 | FunctionPass *createCheckDebugifyFunctionPass(bool Strip, |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 438 | StringRef NameOfWrappedPass, |
| 439 | DebugifyStatsMap *StatsMap) { |
| 440 | return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 441 | } |
Vedant Kumar | 48c582c | 2018-01-23 20:43:50 +0000 | [diff] [blame] | 442 | |
Vedant Kumar | b8c067b | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 443 | PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M, |
| 444 | ModuleAnalysisManager &) { |
Vedant Kumar | 499e3df | 2018-07-24 00:41:29 +0000 | [diff] [blame] | 445 | checkDebugifyMetadata(M, M.functions(), "", "CheckModuleDebugify", false, |
| 446 | nullptr); |
Vedant Kumar | b8c067b | 2018-02-15 21:14:36 +0000 | [diff] [blame] | 447 | return PreservedAnalyses::all(); |
| 448 | } |
| 449 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 450 | char DebugifyModulePass::ID = 0; |
| 451 | static RegisterPass<DebugifyModulePass> DM("debugify", |
Vedant Kumar | 63a0fe2 | 2018-06-04 00:11:47 +0000 | [diff] [blame] | 452 | "Attach debug info to everything"); |
Vedant Kumar | 2cac108 | 2017-12-08 21:57:28 +0000 | [diff] [blame] | 453 | |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 454 | char CheckDebugifyModulePass::ID = 0; |
Vedant Kumar | 63a0fe2 | 2018-06-04 00:11:47 +0000 | [diff] [blame] | 455 | static RegisterPass<CheckDebugifyModulePass> |
| 456 | CDM("check-debugify", "Check debug info from -debugify"); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 457 | |
| 458 | char DebugifyFunctionPass::ID = 0; |
| 459 | static RegisterPass<DebugifyFunctionPass> DF("debugify-function", |
Vedant Kumar | 63a0fe2 | 2018-06-04 00:11:47 +0000 | [diff] [blame] | 460 | "Attach debug info to a function"); |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 461 | |
| 462 | char CheckDebugifyFunctionPass::ID = 0; |
Vedant Kumar | 63a0fe2 | 2018-06-04 00:11:47 +0000 | [diff] [blame] | 463 | static RegisterPass<CheckDebugifyFunctionPass> |
| 464 | CDF("check-debugify-function", "Check debug info from -debugify-function"); |