<div dir="ltr">Hi John,<br><br>Thank you so much for the valuable answer! :)<div><br></div><div>However, i have one tough issue that i need to solve, in my pass :<br><br>let's say here </div><br style="font-size:12.8px"><span style="font-size:12.8px">%s = add %a, %b<br><br>I need to check if the value of %a or %b is 2 or 4 and then apply a transformation to the other operand accordingly,<br><br>for example if the value of %a = 2 the i'll multiply %b by 2 in my transform pass.<br><br>I'm having a hard time getting the integer value of variables since it is so simple in regular languages, it can't be possible that LLVM does not have a way for that, I've also tried casting the operand to a ConstantInt which wasn't successful.<br><br>I'm a beginner with LLVM and i do apologize for my naivety.<br><br>Best Regards,<br>Ammar </span></div><div class="gmail_extra"><br><div class="gmail_quote">On 24 April 2016 at 10:51, John Criswell <span dir="ltr"><<a href="mailto:jtcriswel@gmail.com" target="_blank">jtcriswel@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"><span class="">
<div>On 4/24/16 1:27 PM, Ammar Naqvi wrote:<br>
</div>
<blockquote type="cite">
<p dir="ltr">hey john, </p>
<p dir="ltr">yes indeed, that's what I'm trying, retreiving the
values of %a and %b in an LLVM pass, sorry about the confusion.
</p>
</blockquote>
<br></span>
If you want to add an instruction that uses the values %a and %b,
then your solution is easy: in the LLVM IR, a value and the
instruction that creates it are one and the same, and they are both
represented by the same object. For example, in the following
instruction:<br>
<br>
%s = add %a, %b<br>
<br>
The "%s" and the "add %a, %b" are the same thing (because SSA only
allows one assignment to a virtual register). Therefore, in memory,
there is one object (a sub-class of the Value class) that has name
"%s" and represents the instruction "add %a, %b."<br>
<br>
So, let's say you have the following code to get an operand from an
object of class Instruction:<br>
<br>
Instruction * i = <whatever>;<br>
Value * Va = i->getOperand(0);<br>
<br>
The variable Va points to the object representing the first operand
of the instruction (in your case, %a). You can then create new
instructions that use this value as an operand:<br>
<br>
BinaryOperator * Sub = BinaryOperator::CreateNeg (Va, "name",
InsertPt);<br>
<br>
The above would create the following instruction (which is an
integer negation instruction):<br>
<br>
%name = sub 0, %a<br>
<br>
Regards,<br>
<br>
John Criswell<div><div class="h5"><br>
<br>
<blockquote type="cite">
<div class="gmail_quote">On Apr 24, 2016 7:18 AM, "John Criswell"
<<a href="mailto:jtcriswel@gmail.com" target="_blank">jtcriswel@gmail.com</a>>
wrote:<br type="attribution">
<blockquote class="gmail_quote">
<div>
<div>Dear Ammar,<br>
<br>
It is not clear what you are asking. %a and %b in your
code below are not constants; there is no way, at compile
time, to determine what numeric values they will hold at
run-time.<br>
<br>
Are you asking how to write an LLVM pass that will add
code to summ() to print out its result?<br>
<br>
Regards,<br>
<br>
John Criswell <br>
<br>
On 4/24/16 2:27 AM, Ammar Naqvi via llvm-dev wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">Hello Everyone,<br>
<br>
I need some help in retrieving the numeric value of an
instruction operand from LLVM IR.
<div><br>
</div>
<div>this is what the IR looks like for a simple add
function that adds two i32 integers</div>
<div>
<div><br>
</div>
<div>define i32 @summ(i32 %a, i32 %b) #0 {</div>
<div>entry:</div>
<div> %add = add nsw i32 %b, %a</div>
<div> ret i32 %add</div>
<div>}</div>
</div>
<div><br>
</div>
<div>i would like to know the integer value of %a and
%b.<br>
<br>
I've tried </div>
<div><br>
</div>
<div>-i->getOpcodeName() which gives me the string
add</div>
<div><br>
</div>
<div>-i->getOperand(0) which gives me the string i32
%b<br>
</div>
<div><br>
</div>
-i->getOperand(0)->getName() which gives me the
string a
<div><br>
</div>
<div>what method exists to get the actual integer value
of the operands?</div>
<div>for example we called summ(1,2), how to retrieve
the values 1 and 2 held in the operands?<br>
<br>
Any help and guidance is greatly appreciated! :)<br>
<br>
Best,<br>
Ammar</div>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
LLVM Developers mailing list
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
</blockquote>
<br>
<br>
<pre cols="72">--
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
<a href="http://www.cs.rochester.edu/u/criswell" target="_blank">http://www.cs.rochester.edu/u/criswell</a></pre>
</div>
</blockquote>
</div>
</blockquote>
<br>
<br>
<pre cols="72">--
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
<a href="http://www.cs.rochester.edu/u/criswell" target="_blank">http://www.cs.rochester.edu/u/criswell</a></pre>
</div></div></div>
</blockquote></div><br></div>