Skip to content

Commit c4b12c2

Browse files
committed
llvm-dg: preserve dbg intrinsic
We usually want them in the sliced code. Part of a fix for staticafi/symbiotic#202
1 parent bbcb2fa commit c4b12c2

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

include/dg/llvm/LLVMDependenceGraph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class LLVMDependenceGraph : public DependenceGraph<LLVMNode> {
147147

148148
LLVMNode *findNode(llvm::Value *value) const;
149149

150-
void addDefUseEdges();
150+
void addDefUseEdges(bool preserveDbg = true);
151151
void computeInterferenceDependentEdges(ControlFlowGraph *controlFlowGraph);
152152
static void computeForkJoinDependencies(ControlFlowGraph *controlFlowGraph);
153153
static void computeCriticalSections(ControlFlowGraph *controlFlowGraph);

include/dg/llvm/LLVMDependenceGraphBuilder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct LLVMDependenceGraphOptions {
4040

4141
bool verifyGraph{true};
4242
bool threads{false};
43+
bool preserveDbg{true};
4344

4445
std::string entryFunction{"main"};
4546

@@ -164,7 +165,7 @@ class LLVMDependenceGraphBuilder {
164165
_dg->build(_M, _PTA.get(), _DDA.get(), _entryFunction);
165166

166167
// insert the data dependencies edges
167-
_dg->addDefUseEdges();
168+
_dg->addDefUseEdges(_options.preserveDbg);
168169

169170
// compute and fill-in control dependencies
170171
_runControlDependenceAnalysis();
@@ -228,7 +229,7 @@ class LLVMDependenceGraphBuilder {
228229

229230
// data-dependence edges
230231
_runDataDependenceAnalysis();
231-
_dg->addDefUseEdges();
232+
_dg->addDefUseEdges(_options.preserveDbg);
232233

233234
// fill-in control dependencies
234235
_runControlDependenceAnalysis();

lib/llvm/LLVMDependenceGraph.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <llvm/IR/DataLayout.h>
2222
#include <llvm/IR/Function.h>
2323
#include <llvm/IR/Instructions.h>
24+
#include <llvm/IR/InstIterator.h>
2425
#include <llvm/IR/Module.h>
2526
#include <llvm/IR/Value.h>
2627
#include <llvm/Support/raw_ostream.h>
@@ -1250,9 +1251,37 @@ void LLVMDependenceGraph::addNoreturnDependencies(
12501251
}
12511252
}
12521253

1253-
void LLVMDependenceGraph::addDefUseEdges() {
1254+
void LLVMDependenceGraph::addDefUseEdges(bool preserveDbg) {
12541255
LLVMDefUseAnalysis DUA(this, DDA, PTA);
12551256
DUA.run();
1257+
1258+
if (preserveDbg) {
1259+
using namespace llvm;
1260+
1261+
for (const auto &it : getConstructedFunctions()) {
1262+
LLVMDependenceGraph *dg = it.second;
1263+
for (auto &I : instructions(cast<Function>(it.first))) {
1264+
Value *val = nullptr;
1265+
if (auto *DI = dyn_cast<DbgDeclareInst>(&I))
1266+
val = DI->getAddress();
1267+
else if (auto *DI = dyn_cast<DbgValueInst>(&I))
1268+
val = DI->getValue();
1269+
else if (auto *DI = dyn_cast<DbgAddrIntrinsic>(&I))
1270+
val = DI->getAddress();
1271+
1272+
if (val) {
1273+
auto *nd = dg->getNode(&I);
1274+
auto *ndop = dg->getNode(val);
1275+
assert(nd && "Do not have a node for a dbg intrinsic");
1276+
assert(ndop && "Do not have a node for an operand of a dbg intrinsic");
1277+
// add a use edge such that we preserve
1278+
// the debugging intrinsic when we preserve
1279+
// the value it is talking about
1280+
nd->addUseDependence(ndop);
1281+
}
1282+
}
1283+
}
1284+
}
12561285
}
12571286

12581287
LLVMNode *findInstruction(llvm::Instruction *instruction,

tools/llvm-slicer-opts.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ SlicerOptions parseSlicerOptions(int argc, char *argv[], bool requireCrit,
209209
"Consider threads are in input file (default=false)."),
210210
llvm::cl::init(false), llvm::cl::cat(SlicingOpts));
211211

212+
llvm::cl::opt<bool> preserveDbg(
213+
"preserve-dbg",
214+
llvm::cl::desc("Preserve debugging information (default=true)."),
215+
llvm::cl::init(true), llvm::cl::cat(SlicingOpts));
216+
212217
llvm::cl::opt<std::string> allocationFuns(
213218
"allocation-funs",
214219
llvm::cl::desc(
@@ -380,6 +385,7 @@ SlicerOptions parseSlicerOptions(int argc, char *argv[], bool requireCrit,
380385
auto &CDAOptions = dgOptions.CDAOptions;
381386

382387
dgOptions.entryFunction = entryFunction;
388+
dgOptions.preserveDbg = preserveDbg;
383389
dgOptions.threads = threads;
384390

385391
CDAOptions.algorithm = cdAlgorithm;

0 commit comments

Comments
 (0)