From b905b4697ef2864e7a6a03db61b0fe3b0ae867fa Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 14 Oct 2021 15:57:47 +0200 Subject: [PATCH] llvm-dg: preserve dbg intrinsic We usually want them in the sliced code. Part of a fix for https://github.com/staticafi/symbiotic/issues/202 --- include/dg/llvm/LLVMDependenceGraph.h | 2 +- include/dg/llvm/LLVMDependenceGraphBuilder.h | 5 +-- lib/llvm/LLVMDependenceGraph.cpp | 34 +++++++++++++++++++- tools/llvm-slicer-opts.cpp | 6 ++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/dg/llvm/LLVMDependenceGraph.h b/include/dg/llvm/LLVMDependenceGraph.h index 1aa877ad9..11fb59dea 100644 --- a/include/dg/llvm/LLVMDependenceGraph.h +++ b/include/dg/llvm/LLVMDependenceGraph.h @@ -139,7 +139,7 @@ class LLVMDependenceGraph : public DependenceGraph { LLVMNode *findNode(llvm::Value *value) const; - void addDefUseEdges(); + void addDefUseEdges(bool preserveDbg = true); void computeInterferenceDependentEdges(ControlFlowGraph *controlFlowGraph); static void computeForkJoinDependencies(ControlFlowGraph *controlFlowGraph); static void computeCriticalSections(ControlFlowGraph *controlFlowGraph); diff --git a/include/dg/llvm/LLVMDependenceGraphBuilder.h b/include/dg/llvm/LLVMDependenceGraphBuilder.h index ed978cbf4..688de6b89 100644 --- a/include/dg/llvm/LLVMDependenceGraphBuilder.h +++ b/include/dg/llvm/LLVMDependenceGraphBuilder.h @@ -40,6 +40,7 @@ struct LLVMDependenceGraphOptions { bool verifyGraph{true}; bool threads{false}; + bool preserveDbg{true}; std::string entryFunction{"main"}; @@ -164,7 +165,7 @@ class LLVMDependenceGraphBuilder { _dg->build(_M, _PTA.get(), _DDA.get(), _entryFunction); // insert the data dependencies edges - _dg->addDefUseEdges(); + _dg->addDefUseEdges(_options.preserveDbg); // compute and fill-in control dependencies _runControlDependenceAnalysis(); @@ -228,7 +229,7 @@ class LLVMDependenceGraphBuilder { // data-dependence edges _runDataDependenceAnalysis(); - _dg->addDefUseEdges(); + _dg->addDefUseEdges(_options.preserveDbg); // fill-in control dependencies _runControlDependenceAnalysis(); diff --git a/lib/llvm/LLVMDependenceGraph.cpp b/lib/llvm/LLVMDependenceGraph.cpp index 1b46c3fab..74e7f48cd 100644 --- a/lib/llvm/LLVMDependenceGraph.cpp +++ b/lib/llvm/LLVMDependenceGraph.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -1241,9 +1242,40 @@ void LLVMDependenceGraph::addNoreturnDependencies( } } -void LLVMDependenceGraph::addDefUseEdges() { +void LLVMDependenceGraph::addDefUseEdges(bool preserveDbg) { LLVMDefUseAnalysis DUA(this, DDA, PTA); DUA.run(); + + if (preserveDbg) { + using namespace llvm; + + for (const auto &it : getConstructedFunctions()) { + LLVMDependenceGraph *dg = it.second; + for (auto &I : instructions(cast(it.first))) { + Value *val = nullptr; + if (auto *DI = dyn_cast(&I)) + val = DI->getAddress(); + else if (auto *DI = dyn_cast(&I)) + val = DI->getValue(); +#if LLVM_VERSION_MAJOR > 5 + else if (auto *DI = dyn_cast(&I)) + val = DI->getAddress(); +#endif + + if (val) { + auto *nd = dg->getNode(&I); + auto *ndop = dg->getNode(val); + assert(nd && "Do not have a node for a dbg intrinsic"); + assert(ndop && "Do not have a node for an operand of a dbg " + "intrinsic"); + // add a use edge such that we preserve + // the debugging intrinsic when we preserve + // the value it is talking about + nd->addUseDependence(ndop); + } + } + } + } } LLVMNode *findInstruction(llvm::Instruction *instruction, diff --git a/tools/llvm-slicer-opts.cpp b/tools/llvm-slicer-opts.cpp index 966e9d04d..e4a108b8b 100644 --- a/tools/llvm-slicer-opts.cpp +++ b/tools/llvm-slicer-opts.cpp @@ -209,6 +209,11 @@ SlicerOptions parseSlicerOptions(int argc, char *argv[], bool requireCrit, "Consider threads are in input file (default=false)."), llvm::cl::init(false), llvm::cl::cat(SlicingOpts)); + llvm::cl::opt preserveDbg( + "preserve-dbg", + llvm::cl::desc("Preserve debugging information (default=true)."), + llvm::cl::init(true), llvm::cl::cat(SlicingOpts)); + llvm::cl::opt allocationFuns( "allocation-funs", llvm::cl::desc( @@ -380,6 +385,7 @@ SlicerOptions parseSlicerOptions(int argc, char *argv[], bool requireCrit, auto &CDAOptions = dgOptions.CDAOptions; dgOptions.entryFunction = entryFunction; + dgOptions.preserveDbg = preserveDbg; dgOptions.threads = threads; CDAOptions.algorithm = cdAlgorithm;