Ammar Naqvi via llvm-dev
2016-Apr-26 04:25 UTC
[llvm-dev] 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! :) Best Regards, Ammar Naqvi -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160425/0c48ca6a/attachment.html>
Mehdi Amini via llvm-dev
2016-Apr-26 04:47 UTC
[llvm-dev] Writing a pass to retrieve instruction operand value
> 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 respectivelyI'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>
Nema, Ashutosh via llvm-dev
2016-Apr-26 05:00 UTC
[llvm-dev] Writing a pass to retrieve instruction operand value
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 From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Mehdi Amini via llvm-dev Sent: Tuesday, April 26, 2016 10:17 AM To: Ammar Naqvi <ammarnaqvi92 at gmail.com> Cc: llvm-dev <llvm-dev at lists.llvm.org> Subject: Re: [llvm-dev] Writing a pass to retrieve instruction operand value On Apr 25, 2016, at 9:25 PM, Ammar Naqvi via llvm-dev <llvm-dev at lists.llvm.org<mailto: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<mailto: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/20160426/3fe8e1e6/attachment.html>