Writing a pass to retrieve instruction operand value

Hi Everyone,

I asked a question on the dev list related to the topic to which John Criswell and Jeremy Lakeman kindly provided some valuable insight.

I’m still stuck on the issue and i’m hoping i didn’t phrase the question well enough.

I have a foo.c file that is :

#include <stdio.h>
int foo(int a, int b){

return a+b;
}

int main() {
int x=foo(3,1);
printf("%d\n",x);
return 0;
}

Now, i obtain the foo.bc/foo.ll file,
the code i’m interested in looks like:

; Function Attrs: norecurse nounwind readnone uwtable
define i32 @addd(i32 %a, i32 %b) #0 {
entry:
%add = add nsw i32 %b, %a
ret i32 %add
}

running the file with lli foo.ll outputs

4

Now we know that the values of %a and %b are 3 and 1 respectively, is there any way i can retrieve these values by running foo.bc through a pass??

I know i->getOperand(0) would get me i32 %a
i->getOperand(1) would get me i32 %b

How do i retrieve 3 and 1, the integer values that these operands hold ?

Thanks in advance for any guidance and help! :slight_smile:

Best Regards,
Ammar Naqvi

Hi Everyone,

I asked a question on the dev list related to the topic to which John Criswell and Jeremy Lakeman kindly provided some valuable insight.

I’m still stuck on the issue and i’m hoping i didn’t phrase the question well enough.

Usual practice is to continue the discussion in the original thread if the question is not a new one.

I have a foo.c file that is :

#include <stdio.h>
int foo(int a, int b){

return a+b;
}

int main() {
int x=foo(3,1);
printf("%d\n",x);
return 0;
}

Now, i obtain the foo.bc/foo.ll file,
the code i’m interested in looks like:

; Function Attrs: norecurse nounwind readnone uwtable
define i32 @addd(i32 %a, i32 %b) #0 {
entry:
%add = add nsw i32 %b, %a
ret i32 %add
}

running the file with lli foo.ll outputs

4

Now we know that the values of %a and %b are 3 and 1 respectively

I’m not sure what you mean with this sentence. lli just interpreted the file, starting in main() and executing instruction one after each other.

, is there any way i can retrieve these values by running foo.bc through a pass??

I know i->getOperand(0) would get me i32 %a
i->getOperand(1) would get me i32 %b

How do i retrieve 3 and 1, the integer values that these operands hold ?

3 and 1 are arguments at the call site, while %a and %b are the actual parameter to the function foo(). You need to inspect the call site (there are potentially/usually multiple call-sites) if you have a pointer to the function foo().
The call sites will be amongst the user of the Function. The best is to look for example in the LLVM codebase, for instance:

/// AllCallersPassInValidPointerForArgument - Return true if we can prove that
/// all callees pass in a valid pointer for the specified function argument.
static bool AllCallersPassInValidPointerForArgument(Argument *Arg) {
Function *Callee = Arg->getParent();
const DataLayout &DL = Callee->getParent()->getDataLayout();

unsigned ArgNo = Arg->getArgNo();

// Look at all call sites of the function. At this pointer we know we only
// have direct callees.
for (User *U : Callee->users()) {
CallSite CS(U);
assert(CS && “Should only have direct calls!”);

if (!isDereferenceablePointer(CS.getArgument(ArgNo), DL))
return false;
}
return true;
}

Visible here as well: http://llvm.org/docs/doxygen/html/ArgumentPromotion_8cpp_source.html#l00344

Given an Argument (in your case %a or %b), it will first get a pointer to the Function in Callee (for you it is foo()), and then loop over the users of the function which it expects to be of type CallSite.

As Mehdi already mentioned you need to examine the CallSite.

You are looking for constant values, cast the actual parameters to ‘ConstantInt’.

If the cast goes successful then you can read the actual values.

http://llvm.org/docs/doxygen/html/classllvm_1_1ConstantInt.html

Hope this helps.

Regards,

Ashutosh