13#include "llvm/Analysis/ValueTracking.h"
14#include "llvm/IR/AssemblyAnnotationWriter.h"
15#include "llvm/IR/DebugInfo.h"
16#include "llvm/IR/DebugInfoMetadata.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/InstIterator.h"
19#include "llvm/IR/Instructions.h"
20#include "llvm/IR/IntrinsicInst.h"
21#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/Module.h"
23#include "llvm/Linker/Linker.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/FormattedStream.h"
26#include "llvm/Support/Path.h"
27#include "llvm/Support/raw_ostream.h"
38 llvm::formatted_raw_ostream &os) {
40 os << reinterpret_cast<std::uintptr_t>(i);
44 llvm::formatted_raw_ostream &os) {
46 os << reinterpret_cast<std::uintptr_t>(f);
50static std::map<uintptr_t, uint64_t>
53 std::map<uintptr_t, uint64_t> mapping;
57 llvm::raw_string_ostream os(str);
64 for (s=str.c_str(); *s; s++) {
69 if (s[1] !=
'%' || s[2] !=
'%' || s[3] !=
'%')
74 uint64_t value = strtoull(s, &end, 10);
76 mapping.insert(std::make_pair(value,
line));
92 std::vector<std::unique_ptr<std::string>> &_internedStrings,
93 const llvm::Module &_module)
100 [&s](
const std::unique_ptr<std::string> &item) {
101 return *item.get() == s;
104 return *found->get();
106 auto newItem = std::unique_ptr<std::string>(
new std::string(s));
107 auto result = newItem.get();
114 auto asmLine =
lineTable.at(
reinterpret_cast<std::uintptr_t
>(&Func));
115 auto dsub = Func.getSubprogram();
117 if (dsub !=
nullptr) {
118 auto path = dsub->getFilename();
124 return std::make_unique<FunctionInfo>(
128 std::unique_ptr<InstructionInfo>
130 auto asmLine =
lineTable.at(
reinterpret_cast<std::uintptr_t
>(&Inst));
133 auto dl = Inst.getDebugLoc();
136 if (dl.get() !=
nullptr) {
137 auto full_path = dl.get()->getFilename();
138 auto line = dl.getLine();
139 auto column = dl.getCol();
144 if (
auto LexicalBlock =
145 llvm::dyn_cast<llvm::DILexicalBlock>(dl.getScope())) {
146 line = LexicalBlock->getLine();
147 column = LexicalBlock->getColumn();
156 return std::make_unique<InstructionInfo>(
159 return std::make_unique<InstructionInfo>(
164InstructionInfoTable::InstructionInfoTable(
const llvm::Module &m) {
167 for (
const auto &Func : m) {
172 for (
auto it = llvm::inst_begin(Func), ie = llvm::inst_end(Func); it != ie;
180 size_t idCounter = 0;
181 for (
auto &item :
infos)
182 item.second->id = idCounter++;
184 item.second->id = idCounter++;
193 auto it =
infos.find(&inst);
194 if (it ==
infos.end())
195 llvm::report_fatal_error(
"invalid instruction, not present in "
197 return *it->second.get();
204 llvm::report_fatal_error(
"invalid instruction, not present in "
207 return *found->second.get();
static std::map< uintptr_t, uint64_t > buildInstructionToLineMap(const llvm::Module &m)
void emitFunctionAnnot(const llvm::Function *f, llvm::formatted_raw_ostream &os)
void emitInstructionAnnot(const llvm::Instruction *i, llvm::formatted_raw_ostream &os)
std::unordered_map< const llvm::Instruction *, std::unique_ptr< InstructionInfo > > infos
const FunctionInfo & getFunctionInfo(const llvm::Function &) const
const InstructionInfo & getInfo(const llvm::Instruction &) const
std::unordered_map< const llvm::Function *, std::unique_ptr< FunctionInfo > > functionInfos
std::vector< std::unique_ptr< std::string > > internedStrings
unsigned getMaxID() const
FunctionInfo stores debug information for a KFunction.
unsigned line
Line number in source file.
const std::string & file
Source file name.
InstructionInfo stores debug information for a KInstruction.