mirror of
https://github.com/revng/revng
synced 2026-06-21 14:07:57 +00:00
f68b7866b3
This commit introduces `BasicBlockID` as the unique identifier for a `efa::BasicBlock` into the CFG. A `BasicBlockID` is defined by a `MetaAddress` plus an incremental integer. This enables us to have multiple instances of the same block in a single function, which is particularly useful when inlining multiple times the same function. Apart from this, the commit also does the following: * It drops representing `MetaAddress`es a `structs` in the IR. This created several issues related to ABI. We now represent them as strings. * It defines more functions in `support.h`, instead of defining prototypes by hand in `CodeGenerator.cpp` and the like. Specifically, `unknownPC` and `raise_exception_helper`. We also introduce a C "constructor" for `PlainMetaAddress`. * It significantly reduces the API of `GeneratedCodeBasicInfo`, which was supposed to be put on a diet since a long time. Specifically, many jump target related methods have been moved to free functions in `IRHelpers.h`. Also `GCBI::getSuccessors` has been pushed into its only user, `PruneRetSuccessors`, to prevent further usage of a deprecated API. In the future, it would be nice to drop it entirely. * It introduces `efa::BasicBlock::InlinedFrom`. * Introduce an enum to represent named argument indices for `newpc`. This enables us to more effectively manipulate its argument list. * It improves the verification and error reporting for `efa::FunctionMetadata`. * Update tests. This commit is preliminary to another piece of work to improve the generality of inlining beyond the simple "fake function" scenario, for which the feature was originally conceived.
148 lines
6.0 KiB
C++
148 lines
6.0 KiB
C++
/// \file AttachDebugInfo.cpp
|
|
/// \brief A simple pass to attach debug metadata.
|
|
|
|
//
|
|
// This file is distributed under the MIT License. See LICENSE.md for details.
|
|
//
|
|
|
|
#include "llvm/IR/DIBuilder.h"
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
|
#include "llvm/IR/Function.h"
|
|
#include "llvm/IR/Instructions.h"
|
|
#include "llvm/IR/Metadata.h"
|
|
|
|
#include "revng/EarlyFunctionAnalysis/AttachDebugInfo.h"
|
|
#include "revng/Pipeline/Location.h"
|
|
#include "revng/Pipes/Ranks.h"
|
|
#include "revng/Support/FunctionTags.h"
|
|
|
|
using namespace llvm;
|
|
|
|
char AttachDebugInfo::ID = 0;
|
|
|
|
using Register = RegisterPass<AttachDebugInfo>;
|
|
static Register X("attach-debug-info", "Attach debug info metadata.");
|
|
static Logger<> Log("attach-debug-info");
|
|
|
|
static void handleBasicBlock(DIBuilder &DIB,
|
|
llvm::BasicBlock &BB,
|
|
DISubprogram *RootSubprogram,
|
|
efa::FunctionMetadata &FM,
|
|
GeneratedCodeBasicInfo &GCBI) {
|
|
namespace ranks = revng::ranks;
|
|
|
|
if (!isJumpTarget(&BB)) {
|
|
revng_log(Log, " Not a jump target " << BB.getName());
|
|
return;
|
|
}
|
|
|
|
const efa::BasicBlock *Block = FM.findBlock(GCBI, &BB);
|
|
if (!Block) {
|
|
revng_log(Log, " No metadata for " << BB.getName());
|
|
return;
|
|
}
|
|
|
|
revng_log(Log, " " << BB.getName() << ": " << Block->ID().toString());
|
|
|
|
llvm::Module &M = *BB.getParent()->getParent();
|
|
DILocation *CurrentDebugLocation = nullptr;
|
|
for (auto &I : BB) {
|
|
if (auto *Call = getCallTo(&I, "newpc")) {
|
|
MetaAddress NewPC = blockIDFromNewPC(Call).start();
|
|
// We cloned `line:` only, but we need full MetaAddress that we keep
|
|
// as `DISubprogram`.
|
|
auto SPFlags = DISubprogram::toSPFlags(false, /* isLocalToUnit */
|
|
true, /* isDefinition*/
|
|
false /* isOptimized */);
|
|
auto Type = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
|
|
// Let's make the debug location that points back to the binary.
|
|
std::string NewDebugLocation = serializedLocation(ranks::Instruction,
|
|
FM.Entry(),
|
|
Block->ID(),
|
|
NewPC);
|
|
auto Subprogram = DIB.createFunction(RootSubprogram->getFile(), // Scope
|
|
NewDebugLocation, // Name
|
|
StringRef(), // LinkageName
|
|
RootSubprogram->getFile(), // File
|
|
1, // LineNo
|
|
Type, // Ty (subroutine type)
|
|
1, // ScopeLine
|
|
DINode::FlagPrototyped, // Flags
|
|
SPFlags);
|
|
DIB.finalizeSubprogram(Subprogram);
|
|
auto InlineLocationForMetaAddress = DILocation::get(M.getContext(),
|
|
NewPC.address(),
|
|
0,
|
|
RootSubprogram,
|
|
nullptr);
|
|
// Represent debug info for all the isolated functions as if they were
|
|
// inlined in the root.
|
|
auto InlineLoc = DILocation::get(M.getContext(),
|
|
NewPC.address(),
|
|
0,
|
|
Subprogram,
|
|
InlineLocationForMetaAddress);
|
|
CurrentDebugLocation = InlineLoc;
|
|
I.setDebugLoc(CurrentDebugLocation);
|
|
} else {
|
|
I.setDebugLoc(CurrentDebugLocation);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool AttachDebugInfo::runOnModule(llvm::Module &M) {
|
|
DIBuilder DIB(M);
|
|
FunctionMetadataCache *Cache = &getAnalysis<FunctionMetadataCachePass>()
|
|
.get();
|
|
auto &GCBI = getAnalysis<GeneratedCodeBasicInfoWrapperPass>().getGCBI();
|
|
|
|
// This will be used for attaching the !dbg to instructions.
|
|
// TODO: Document how are we going to abuse DILocation fields.
|
|
DIFile *File = DIB.createFile(M.getSourceFileName(), "./");
|
|
// Also add dummy CU.
|
|
DICompileUnit *CU = DIB.createCompileUnit(dwarf::DW_LANG_C,
|
|
File,
|
|
"revng", // Producer
|
|
true, // isOptimized
|
|
"", // Flags
|
|
0 // RV
|
|
);
|
|
// Create debug info for the 'root' function.
|
|
auto SPFlags = DISubprogram::toSPFlags(false, // isLocalToUnit
|
|
true, // isDefinition
|
|
false // isOptimized
|
|
);
|
|
auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
|
|
DISubprogram
|
|
*RootSubprogram = DIB.createFunction(CU->getFile(), // Scope
|
|
"root", // Name
|
|
StringRef(), // LinkageName
|
|
CU->getFile(), // File
|
|
1, // LineNo
|
|
SPType, // Ty (subroutine type)
|
|
1, // ScopeLine
|
|
DINode::FlagPrototyped, // Flags
|
|
SPFlags);
|
|
DIB.finalizeSubprogram(RootSubprogram);
|
|
|
|
for (auto &F : M) {
|
|
// Skip non-isolated functions (e.g. helpers from QEMU).
|
|
if (not FunctionTags::Isolated.isTagOf(&F))
|
|
continue;
|
|
|
|
// Skip functions with debug-info.
|
|
if (F.getSubprogram())
|
|
continue;
|
|
|
|
auto FM = Cache->getFunctionMetadata(&F);
|
|
revng_log(Log,
|
|
"Metadata for Function " << F.getName() << ":"
|
|
<< FM.Entry().toString());
|
|
|
|
for (auto &BB : F)
|
|
handleBasicBlock(DIB, BB, RootSubprogram, FM, GCBI);
|
|
}
|
|
|
|
return true;
|
|
}
|