<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Menlo;
panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">As Mehdi already mentioned you need to examine the CallSite.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">You are looking for constant values, cast the actual parameters to ‘ConstantInt’.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">If the cast goes successful then you can read the actual values.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><a href="http://llvm.org/docs/doxygen/html/classllvm_1_1ConstantInt.html">http://llvm.org/docs/doxygen/html/classllvm_1_1ConstantInt.html</a><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Hope this helps.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Regards,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Ashutosh<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<b>On Behalf Of </b>Mehdi Amini via llvm-dev<br>
<b>Sent:</b> Tuesday, April 26, 2016 10:17 AM<br>
<b>To:</b> Ammar Naqvi <ammarnaqvi92@gmail.com><br>
<b>Cc:</b> llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> Re: [llvm-dev] Writing a pass to retrieve instruction operand value<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">On Apr 25, 2016, at 9:25 PM, Ammar Naqvi via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">Hi Everyone,<br>
<br>
I asked a question on the dev list related to the topic to which John Criswell and Jeremy Lakeman kindly provided some valuable insight.<br>
<br>
I'm still stuck on the issue and i'm hoping i didn't phrase the question well enough.<o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Usual practice is to continue the discussion in the original thread if the question is not a new one.<o:p></o:p></p>
</div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal"><br>
I have a <b>foo.c</b> file that is :<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><b>#include <stdio.h></b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b>int foo(int a, int b){</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b> </b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b> return a+b;</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b>}</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><b>int main() {</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b>int x=foo(3,1); </b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b>printf("%d\n",x);</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b>return 0;</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b>}</b><o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Now, i obtain the foo.bc/foo.ll file,<br>
the code i'm interested in looks like:<o:p></o:p></p>
<div>
<p class="MsoNormal"><b>; Function Attrs: norecurse nounwind readnone uwtable</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b>define i32 @addd(i32 %a, i32 %b) #0 {</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b>entry:</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b> %add = add nsw i32 %b, %a</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b> ret i32 %add</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><b>}</b><o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">running the file with <b>lli</b> <b>foo.ll outputs </b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><b>4</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Now we know that the values of %a and %b are 3 and 1 respectively<o:p></o:p></p>
</div>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<div>
<p class="MsoNormal">, is there any way i can retrieve these values by running foo.bc through a pass??<br>
<br>
I know i->getOperand(0) would get me <b>i32 %a </b> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">i->getOperand(1) would get me <b>i32 %b</b><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><b>How do i retrieve 3 and 1, the integer values that these operands hold ?</b><o:p></o:p></p>
</div>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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()`.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">The call sites will be amongst the user of the Function. The best is to look for example in the LLVM codebase, for instance:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#008F00">/// AllCallersPassInValidPointerForArgument - Return true if we can prove that<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#008F00">/// all callees pass in a valid pointer for the specified function argument.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#0433FF">static</span><span style="font-size:8.5pt;font-family:"Menlo",serif">
<span style="color:#0433FF">bool</span> AllCallersPassInValidPointerForArgument(<span style="color:#3495AF">Argument</span> *Arg) {<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"> <span style="color:#3495AF">
Function</span> *Callee = Arg-><span style="color:#3495AF">getParent</span>();<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"> <span style="color:#0433FF">
const</span> <span style="color:#3495AF">DataLayout</span> &DL = Callee->getParent()->getDataLayout();<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"> <span style="color:#0433FF">
unsigned</span> ArgNo = Arg-><span style="color:#3495AF">getArgNo</span>();<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">
</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#008F00">// Look at all call sites of the function. At this pointer we know we only<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">
</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#008F00">// have direct callees.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"> <span style="color:#0433FF">
for</span> (User *U : Callee->users()) {<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"> CallSite CS(U);<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">
</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#0433FF">assert</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">(CS &&
</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#B4261A">"Should only have direct calls!"</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">);</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#B4261A"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"> <span style="color:#0433FF">
if</span> (!isDereferenceablePointer(CS.getArgument(ArgNo), DL))<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">
</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#0433FF">return</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">
</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#0433FF">false</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">;</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#0433FF"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif"> }<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">
</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#0433FF">return</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">
</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#0433FF">true</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:black">;</span><span style="font-size:8.5pt;font-family:"Menlo",serif;color:#0433FF"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Menlo",serif">}<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Visible here as well: <a href="http://llvm.org/docs/doxygen/html/ArgumentPromotion_8cpp_source.html#l00344">http://llvm.org/docs/doxygen/html/ArgumentPromotion_8cpp_source.html#l00344</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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`.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">-- <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Mehdi<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thanks in advance for any guidance and help! :)<br>
<br>
Best Regards,<br>
Ammar Naqvi<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal">_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><o:p></o:p></p>
</div>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>