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