[llvm-dev] Writing a pass to retrieve instruction operand value

Mehdi Amini via llvm-dev llvm-dev at lists.llvm.org
Mon Apr 25 21:47:20 PDT 2016


> On Apr 25, 2016, at 9:25 PM, Ammar Naqvi via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> 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`.

-- 
Mehdi





> 
> Thanks in advance for any guidance and help! :)
> 
> Best Regards,
> Ammar Naqvi
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160425/8d8325da/attachment.html>


More information about the llvm-dev mailing list