Skip to content

Commit

Permalink
llvm-dg: preserve dbg intrinsic
Browse files Browse the repository at this point in the history
We usually want them in the sliced code.

Part of a fix for staticafi/symbiotic#202
  • Loading branch information
mchalupa committed Oct 14, 2021
1 parent bbcb2fa commit 34382e5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/dg/llvm/LLVMDependenceGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class LLVMDependenceGraph : public DependenceGraph<LLVMNode> {

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);
Expand Down
5 changes: 3 additions & 2 deletions include/dg/llvm/LLVMDependenceGraphBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct LLVMDependenceGraphOptions {

bool verifyGraph{true};
bool threads{false};
bool preserveDbg{true};

std::string entryFunction{"main"};

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -228,7 +229,7 @@ class LLVMDependenceGraphBuilder {

// data-dependence edges
_runDataDependenceAnalysis();
_dg->addDefUseEdges();
_dg->addDefUseEdges(_options.preserveDbg);

// fill-in control dependencies
_runControlDependenceAnalysis();
Expand Down
33 changes: 32 additions & 1 deletion lib/llvm/LLVMDependenceGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/InstIterator.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Value.h>
#include <llvm/Support/raw_ostream.h>
Expand Down Expand Up @@ -1250,9 +1251,39 @@ 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<Function>(it.first))) {
Value *val = nullptr;
if (auto *DI = dyn_cast<DbgDeclareInst>(&I))
val = DI->getAddress();
else if (auto *DI = dyn_cast<DbgValueInst>(&I))
val = DI->getValue();
#if LLVM_VERSION_MAJOR > 5
else if (auto *DI = dyn_cast<DbgAddrIntrinsic>(&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,
Expand Down
6 changes: 6 additions & 0 deletions tools/llvm-slicer-opts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> preserveDbg(
"preserve-dbg",
llvm::cl::desc("Preserve debugging information (default=true)."),
llvm::cl::init(true), llvm::cl::cat(SlicingOpts));

llvm::cl::opt<std::string> allocationFuns(
"allocation-funs",
llvm::cl::desc(
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 34382e5

Please sign in to comment.