<div dir="ltr">Hey Jeremy,<br><br>thank you for the response,I see what you're saying,<br><br>The simplified version of the question is can i compare a Value to check if it is 1, is there a way to do <b>if(i32 %b==1) </b>in LLVM through a pass?</div><div class="gmail_extra"><br><div class="gmail_quote">On 24 April 2016 at 19:36, Jeremy Lakeman <span dir="ltr"><<a href="mailto:Jeremy.Lakeman@gmail.com" target="_blank">Jeremy.Lakeman@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Let me repeat the above;<br>
%a is a variable. It could have any value.<br>
There could be any number of other calls to @summ(a,b) in other<br>
translation units, or linked at runtime, which pass any value at all.<br>
<br>
However, if you have a call site which calls @summ with constant<br>
arguments, the method could be inlined at that call site...<br>
<br>
There is a very big difference between compiling a method, and<br>
interpreting a method.<br>
<br>
You can write an LLVM pass to modify the method and change what it<br>
does. For example you can add code to inspect the values of a and b at<br>
run time.<br>
<br>
It might help to think about what the final version of your function<br>
would look like if you wrote it in your source language. eg;<br>
<br>
int summ(int a, int b) {return a+b;} ==> int summ(int a, int b) {if<br>
(a==2) return b*2; else return a+b;}<br>
<br>
Then compile both versions to LLVM so you can start thinking about<br>
what LLVM operations need to be performed to turn one into the other.<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
On Mon, Apr 25, 2016 at 5:45 AM, Ammar Naqvi via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
> Hi John,<br>
><br>
> Thank you so much for the valuable answer! :)<br>
><br>
> However, i have one tough issue that i need to solve, in my pass :<br>
><br>
> let's say here<br>
><br>
> %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<br>
> 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<br>
> pass.<br>
><br>
> I'm having a hard time getting the integer value of variables since it is so<br>
> simple in regular languages, it can't be possible that LLVM does not have a<br>
> way for that, I've also tried casting the operand to a ConstantInt which<br>
> wasn't successful.<br>
><br>
> I'm a beginner with LLVM and i do apologize for my naivety.<br>
><br>
> Best Regards,<br>
> Ammar<br>
><br>
> On 24 April 2016 at 10:51, John Criswell <<a href="mailto:jtcriswel@gmail.com">jtcriswel@gmail.com</a>> wrote:<br>
>><br>
>> On 4/24/16 1:27 PM, Ammar Naqvi wrote:<br>
>><br>
>> hey john,<br>
>><br>
>> yes indeed, that's what I'm trying, retreiving the values of %a and %b in<br>
>> an LLVM pass, sorry about the confusion.<br>
>><br>
>><br>
>> If you want to add an instruction that uses the values %a and %b, then<br>
>> your solution is easy: in the LLVM IR, a value and the instruction that<br>
>> creates it are one and the same, and they are both represented by the same<br>
>> 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<br>
>> one assignment to a virtual register). Therefore, in memory, there is one<br>
>> object (a sub-class of the Value class) that has name "%s" and represents<br>
>> the instruction "add %a, %b."<br>
>><br>
>> So, let's say you have the following code to get an operand from an object<br>
>> 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<br>
>> instruction (in your case, %a). You can then create new instructions that<br>
>> 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<br>
>> negation instruction):<br>
>><br>
>> %name = sub 0, %a<br>
>><br>
>> Regards,<br>
>><br>
>> John Criswell<br>
>><br>
>><br>
>> On Apr 24, 2016 7:18 AM, "John Criswell" <<a href="mailto:jtcriswel@gmail.com">jtcriswel@gmail.com</a>> wrote:<br>
>>><br>
>>> Dear Ammar,<br>
>>><br>
>>> It is not clear what you are asking. %a and %b in your code below are<br>
>>> not constants; there is no way, at compile time, to determine what numeric<br>
>>> 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<br>
>>> 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>
>>><br>
>>> Hello Everyone,<br>
>>><br>
>>> I need some help in retrieving the numeric value of an instruction<br>
>>> operand from LLVM IR.<br>
>>><br>
>>> this is what the IR looks like for a simple add function that adds two<br>
>>> i32 integers<br>
>>><br>
>>> define i32 @summ(i32 %a, i32 %b) #0 {<br>
>>> entry:<br>
>>> %add = add nsw i32 %b, %a<br>
>>> ret i32 %add<br>
>>> }<br>
>>><br>
>>> i would like to know the integer value of %a and %b.<br>
>>><br>
>>> I've tried<br>
>>><br>
>>> -i->getOpcodeName() which gives me the string add<br>
>>><br>
>>> -i->getOperand(0) which gives me the string i32 %b<br>
>>><br>
>>> -i->getOperand(0)->getName() which gives me the string a<br>
>>><br>
>>> what method exists to get the actual integer value of the operands?<br>
>>> for example we called summ(1,2), how to retrieve the values 1 and 2 held<br>
>>> in the operands?<br>
>>><br>
>>> Any help and guidance is greatly appreciated! :)<br>
>>><br>
>>> Best,<br>
>>> Ammar<br>
>>><br>
>>><br>
>>> _______________________________________________<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" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
>>><br>
>>><br>
>>><br>
>>> --<br>
>>> John Criswell<br>
>>> Assistant Professor<br>
>>> Department of Computer Science, University of Rochester<br>
>>> <a href="http://www.cs.rochester.edu/u/criswell" rel="noreferrer" target="_blank">http://www.cs.rochester.edu/u/criswell</a><br>
>><br>
>><br>
>><br>
>> --<br>
>> John Criswell<br>
>> Assistant Professor<br>
>> Department of Computer Science, University of Rochester<br>
>> <a href="http://www.cs.rochester.edu/u/criswell" rel="noreferrer" target="_blank">http://www.cs.rochester.edu/u/criswell</a><br>
><br>
><br>
><br>
> _______________________________________________<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" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
><br>
</div></div></blockquote></div><br></div>