Basically, when we define a local variable in LLVM it uses AllocaInst
. Here's the example that you have put up:
a=b+c
in C code:
int a;
int b=10;
int c=10;
a=b+c;
In LLVM IR compiled with -g
(debug mode):
%a = alloca i32, align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
call void @llvm.dbg.declare(metadata i32* %a, metadata !14, metadata !16),
... !dbg !17
call void @llvm.dbg.declare(metadata i32* %b, metadata !18, metadata !16),
... !dbg !19
store i32 10, i32* %b, align 4, !dbg !19
call void @llvm.dbg.declare(metadata i32* %c, metadata !20, metadata !16),
... !dbg !21
store i32 10, i32* %c, align 4, !dbg !21
%0 = load i32, i32* %b, align 4, !dbg !22
%1 = load i32, i32* %c, align 4, !dbg !23
%add = add nsw i32 %0, %1, !dbg !24
store i32 %add, i32* %a, align 4, !dbg !25
Let's see how to use the LLVM API to collect def-use chains. It is easy as LLVM has an in-house built-in API at function level for this one:
bool runOnFunction(Function &F){
errs() << "digraph " + F.getName() + "{\n";
errs() << "\n";
for (auto block = F.getBasicBlockList().begin(); block != F.getBasicBlockList().end(); block++) {
for (auto inst = block->begin(); inst != block->end(); inst++) {
for (Use &U:inst->operands()) {
Value *v = U.get();
if (dyn_cast<Instruction>(v)) {
errs() << "\"" << *dyn_cast<Instruction>(v) << "\"" << " -> " << "\"" << *inst << "\"" << ";\n";
}
if (v->getName() != "") {
errs() << "\"" << v->getName() << "\"" << " -> " << "\"" << *inst << "\"" << ";\n";
errs() << "\"" << v->getName() << "\"" << " [ color = red ]\n";
}
}
}
}
errs() << "\n}\n";
}
For def-use chains, simple tract alloca and user.
This will generate a PDG.
The code taken from https://github.com/DengMinghua/LLVM-Program-Dependency-Graph-Generator
Hope this helps.