How to get assembly line from *.ll file in LLVM - llvm

I want to get line of some instruction in *.ll file, i.e. a.ll:
0: define dso_local i32 #f(i32* %a) #0 {
1: entry:
2: %a.addr = alloca i32*, align 8
3: %c = alloca [10 x i32], align 16
4: %d = alloca [10 x [11 x i32]], align 16
5: %e = alloca [10 x [11 x [12 x i32]]], align 16
6: store i32* %a, i32** %a.addr, align 8
...
auto M = parseIRFile("a.ll");
const llvm::Instruction *I = M.someInstructionGetter("%a.addr = alloca i32*, align 8");
size_t l = someLineGetter(I);
assert(l == 2);
Some questions I searched are about source line ($ clang ... -g ... and llvm::DebugLoc), obviously that is not what I want.

Related

Find local variables in certain function llvm

Given a certain function in LLVM bit code, how can I identify its local variables?.
For example, the following snippet from GNU coreutils echo utility, I don't know how to find the variable do_v9 in the scope of the main IR code.
int main (int argc, char **argv)
{
bool display_return = true;
bool posixly_correct = getenv ("POSIXLY_CORRECT");
....
bool do_v9 = false;
}
I noticed LLVM creates a metadata for local variables, called DILocalVariable, where this variable will be replaced with a number starts with the letter i.
!686 = !DILocalVariable(name: "posixly_correct", scope: !678, file: !10, line: 114, type: !64)
!688 = !DILocalVariable(name: "do_v9", scope: !678, file: !10, line: 122, type: !64)
So the main IR code contains this neither the variable do_v9 nor its corresponding metadata !688, except for the value besides the definition of the main function. My analysis loops over the instructions in the main function, but I don't know how to find this local variable within my iteration. Where I'm using LLVM 6.0.
; Function Attrs: nounwind uwtable
define i32 #main(i32, i8**) #9 !dbg !678 {
%3 = alloca i32, align 4
%4 = alloca i32, align 4
%5 = alloca i8**, align 8
%6 = alloca i8, align 1
%7 = alloca i8, align 1
%8 = alloca i8, align 1
%9 = alloca i8, align 1
%10 = alloca i32
%11 = alloca i8*, align 8
%12 = alloca i64, align 8
%13 = alloca i8*, align 8
%14 = alloca i8, align 1
%15 = alloca i8, align 1
If you want to identify a local variable from your source code in llvm IR using the debug information emitted by the compiler, you can do this by looking at the calls to the #llvm.dbg.declare or #llvm.dbg.addr intrinsics in your source code. You will have either one or the other (but not both; the llvm.dbg.addr function replaces llvm.dbg.declare in newer versions of llvm) present once for each local variable in your function. For example, if you have the following:
%1 = alloca i32, align 4
call void #llvm.dbg.addr(metadata i32* %1, metadata !2, metadata ...), !dbg ...
!2 = !DILocalVariable(name: "i", ...)
This tells us that local variable i corresponds to the stack location allocated by the alloca whose address is %1.
Note that the ... above just represents stuff we don't care about in this context.

LLVM Comparing arguments of two functions

Suppose I have:
foo(int a, int b){
bar(a)
}
how would I check if argument a in bar is the same as argument a in foo in the llvm IR(*.ll file) I get something this:
test.ll:
18 ; Function Attrs: nounwind uwtable
19 define i32 #foo(i32 %a, i32 %b) #0 {
20 entry:
21 %a.addr = alloca i32, align 4
22 %b.addr = alloca i32, align 4
23 %f = alloca i32, align 4
24 store i32 %a, i32* %a.addr, align 4
25 store i32 %b, i32* %b.addr, align 4
26 %0 = load i32, i32* %a.addr, align 4
27 %call = call i32 #baz(i32 %0)
28 store i32 %call, i32* %f, align 4
29 %1 = load i32, i32* %f, align 4
30 ret i32 %1
31 }
32
I am assuming that you are only asking if the same variable is passed on to bar or not.
to check that while running on function
runOnFunction(Function &F)
{
Function::ArgumentListType &sArgs = F.getArgumentList();
Function::ArgumentListType::iterator psArg = sArgs.begin();
Function::ArgumentListType::iterator psEndArg = sArgs.end();
/* this will iterate through function arguments */
for ( ;psArg != psEndArg; ++psArg)
{
/* iterate argument uses */
Argument::user_iterator psUSe = psArg->user_begin();
Argument::user_iterator psUSeEnd = psArg->user_end();
for (;psUSe != psUSeEnd ; ++psUSe)
{
/* check if it is call to bar or not */
if (CallInst* psCall = dyn_cast<CallInst>(*psUSe))
//if (const CallInst* psInst = dyn_cast<CallInst>(*psUSe))
{
if(psCall->getCalledFunction()->getName().startswith("bar")
{ // hurray found it, do something
}
}
}
}
}
above code will check if function argument is diractly passed to another function call.

Create a LLVM function with a reference argument (e.g. double &x)

I want to create, from scratch, a new function in LLVM IR. The LLVM code should correspond to a C++ function with a reference argument, say
void foo(double &x){
x=0;
}
The tutorial such as http://llvm.org/releases/2.6/docs/tutorial/JITTutorial1.html is too old (llvm 2.6) and does not consider pass-by-reference function.
Any hint on how to do this? Thanks.
In LLVM, Reference types are typically implemented with pointer types. For the following C++ source code,
int foo(int & i) {
return i;
}
int bar(int *i) {
return *i;
}
void baz(int i) {
foo(i);
bar(&i);
}
The corresponding IR is:
; Function Attrs: nounwind
define i32 #_Z3fooRi(i32* dereferenceable(4) %i) #0 {
entry:
%i.addr = alloca i32*, align 8
store i32* %i, i32** %i.addr, align 8
%0 = load i32*, i32** %i.addr, align 8
%1 = load i32, i32* %0, align 4
ret i32 %1
}
; Function Attrs: nounwind
define i32 #_Z3barPi(i32* %i) #0 {
entry:
%i.addr = alloca i32*, align 8
store i32* %i, i32** %i.addr, align 8
%0 = load i32*, i32** %i.addr, align 8
%1 = load i32, i32* %0, align 4
ret i32 %1
}
; Function Attrs: nounwind
define void #_Z3bazi(i32 %i) #0 {
entry:
%i.addr = alloca i32, align 4
store i32 %i, i32* %i.addr, align 4
%call = call i32 #_Z3fooRi(i32* dereferenceable(4) %i.addr)
%call1 = call i32 #_Z3barPi(i32* %i.addr)
ret void
}
You can find that there is no essential difference for i between functions foo and bar: dereferenceable is just a parameter attribute that you can add yourself during the code generation from the frontend.

LLVM Can't find getelementptr instruction

I have this byte code fragment:
define void #setGlobal(i32 %a) #0 {
entry:
%a.addr = alloca i32, align 4
store i32 %a, i32* %a.addr, align 4
%0 = load i32* %a.addr, align 4
store i32 %0, i32* #Global, align 4
%1 = load i32* %a.addr, align 4
store i32 %1, i32* getelementptr inbounds ([5 x i32]* #GlobalVec, i32 0, i64 0), align 4
store i32 2, i32* getelementptr inbounds ([5 x i32]* #GlobalVec, i32 0, i64 2), align 4
ret void
}
I am using this code to find the getelementptr from "store i32 %1, i32* getelementptr inbounds ([5 x i32]* #GlobalVec, i32 0, i64 0), align 4":
for (Module::iterator F = p_Module.begin(), endF = p_Module.end(); F != endF; ++F) {
for (Function::iterator BB = F->begin(), endBB = F->end(); BB != endBB; ++BB) {
for (BasicBlock::iterator I = BB->begin(), endI = BB->end(); I
!= endI; ++I) {
if (StoreInst* SI = dyn_cast<StoreInst>(I)) {
if (Instruction *gep = dyn_cast<Instruction>(SI->getOperand(1)))
{
if (gep->getOpcode() == Instruction::GetElementPtr)
{
//do something
}
}
}
}
}
}
This code can't find the getelementptr. What am I doing wrong?
There are no getelementptr instructions in your bitcode snippet, which is why you can't find them.
The two cases that look like a getelementptr instructions are actually constant expressions - the telltale sign is that they appear as part of another instruction (store), which is not something you can do with regular instructions.
So if you want to search for that expression, you need to look for type GetElementPtrConstantExpr, not GetElementPtrInst.

How can you print instruction in llvm

From an llvm pass, I need to print an llvm instruction (Type llvm::Instruction) on the screen, just like as it appears in the llvm bitcode file. Actually my compilation is crashing, and does not reach the point where bitcode file is generated. So for debugging I want to print some instructions to know what is going wrong.
Assuming I is your instruction
I.print(errs());
By simply using the print method.
For a simple Hello World program, using C++'s range-based loops, you can do something like this:
for(auto& B: F){
for(auto& I: B){
errs() << I << "\n";
}
}
This gives the output:
%3 = alloca i32, align 4
%4 = alloca i8**, align 8
store i32 %0, i32* %3, align 4
store i8** %1, i8*** %4, align 8
%5 = call i32 (i8*, ...) #printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* #.str, i64 0, i64 0))
ret i32 0