diff options
author | Nico Weber <thakis@chromium.org> | 2020-12-14 22:33:29 -0500 |
---|---|---|
committer | Nico Weber <thakis@chromium.org> | 2020-12-14 22:34:23 -0500 |
commit | a852ee199c73237d89932019e8a74cf38842f2bd (patch) | |
tree | c8f82bae1780772c3393763515e052b0a31e2cc2 /llvm/lib/Transforms/Utils/Debugify.cpp | |
parent | 04701698ebaff8d0c108e590a7e55c3c5bcd4d9e (diff) | |
download | llvm-a852ee199c73237d89932019e8a74cf38842f2bd.zip llvm-a852ee199c73237d89932019e8a74cf38842f2bd.tar.gz llvm-a852ee199c73237d89932019e8a74cf38842f2bd.tar.bz2 |
Reland "[MachineDebugify] Insert synthetic DBG_VALUE instructions"
This reverts commit 841f9c937f6e593c926a26aedf054436eb807fe6.
The change landed many months ago; something else broke those tests.
Diffstat (limited to 'llvm/lib/Transforms/Utils/Debugify.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/Debugify.cpp | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/llvm/lib/Transforms/Utils/Debugify.cpp b/llvm/lib/Transforms/Utils/Debugify.cpp index f5fdeac..cb6985f 100644 --- a/llvm/lib/Transforms/Utils/Debugify.cpp +++ b/llvm/lib/Transforms/Utils/Debugify.cpp @@ -76,6 +76,7 @@ bool llvm::applyDebugifyMetadata( DIBuilder DIB(M); LLVMContext &Ctx = M.getContext(); + auto *Int32Ty = Type::getInt32Ty(Ctx); // Get a DIType which corresponds to Ty. DenseMap<uint64_t, DIType *> TypeCache; @@ -100,6 +101,7 @@ bool llvm::applyDebugifyMetadata( if (isFunctionSkipped(F)) continue; + bool InsertedDbgVal = false; auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None)); DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized; @@ -108,6 +110,23 @@ bool llvm::applyDebugifyMetadata( auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine, SPType, NextLine, DINode::FlagZero, SPFlags); F.setSubprogram(SP); + + // Helper that inserts a dbg.value before \p InsertBefore, copying the + // location (and possibly the type, if it's non-void) from \p TemplateInst. + auto insertDbgVal = [&](Instruction &TemplateInst, + Instruction *InsertBefore) { + std::string Name = utostr(NextVar++); + Value *V = &TemplateInst; + if (TemplateInst.getType()->isVoidTy()) + V = ConstantInt::get(Int32Ty, 0); + const DILocation *Loc = TemplateInst.getDebugLoc().get(); + auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(), + getCachedDIType(V->getType()), + /*AlwaysPreserve=*/true); + DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc, + InsertBefore); + }; + for (BasicBlock &BB : F) { // Attach debug locations. for (Instruction &I : BB) @@ -142,15 +161,19 @@ bool llvm::applyDebugifyMetadata( if (!isa<PHINode>(I) && !I->isEHPad()) InsertBefore = I->getNextNode(); - std::string Name = utostr(NextVar++); - const DILocation *Loc = I->getDebugLoc().get(); - auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(), - getCachedDIType(I->getType()), - /*AlwaysPreserve=*/true); - DIB.insertDbgValueIntrinsic(I, LocalVar, DIB.createExpression(), Loc, - InsertBefore); + insertDbgVal(*I, InsertBefore); + InsertedDbgVal = true; } } + // Make sure we emit at least one dbg.value, otherwise MachineDebugify may + // not have anything to work with as it goes about inserting DBG_VALUEs. + // (It's common for MIR tests to be written containing skeletal IR with + // empty functions -- we're still interested in debugifying the MIR within + // those tests, and this helps with that.) + if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) { + auto *Term = findTerminatingInstruction(F.getEntryBlock()); + insertDbgVal(*Term, Term); + } if (ApplyToMF) ApplyToMF(DIB, F); DIB.finalizeSubprogram(SP); @@ -159,10 +182,9 @@ bool llvm::applyDebugifyMetadata( // Track the number of distinct lines and variables. NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify"); - auto *IntTy = Type::getInt32Ty(Ctx); auto addDebugifyOperand = [&](unsigned N) { NMD->addOperand(MDNode::get( - Ctx, ValueAsMetadata::getConstant(ConstantInt::get(IntTy, N)))); + Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N)))); }; addDebugifyOperand(NextLine - 1); // Original number of lines. addDebugifyOperand(NextVar - 1); // Original number of variables. |