[llvm-dev] Retrieving numeric value of instruction operand

John Criswell via llvm-dev llvm-dev at lists.llvm.org
Sun Apr 24 10:51:34 PDT 2016


On 4/24/16 1:27 PM, Ammar Naqvi wrote:
>
> hey john,
>
> yes indeed, that's what I'm trying, retreiving the values of %a and %b 
> in an LLVM pass,  sorry about the confusion.
>

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:

%s = add %a, %b

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."

So, let's say you have the following code to get an operand from an 
object of class Instruction:

Instruction * i = <whatever>;
Value * Va = i->getOperand(0);

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:

BinaryOperator * Sub = BinaryOperator::CreateNeg (Va, "name", InsertPt);

The above would create the following instruction (which is an integer 
negation instruction):

%name = sub 0, %a

Regards,

John Criswell

> On Apr 24, 2016 7:18 AM, "John Criswell" <jtcriswel at gmail.com 
> <mailto:jtcriswel at gmail.com>> wrote:
>
>     Dear Ammar,
>
>     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.
>
>     Are you asking how to write an LLVM pass that will add code to
>     summ() to print out its result?
>
>     Regards,
>
>     John Criswell
>
>     On 4/24/16 2:27 AM, Ammar Naqvi via llvm-dev wrote:
>>     Hello Everyone,
>>
>>     I need some help in retrieving the numeric value of an
>>     instruction operand from LLVM IR.
>>
>>     this is what the IR looks like for a simple add function that
>>     adds two i32 integers
>>
>>     define i32 @summ(i32 %a, i32 %b) #0 {
>>     entry:
>>       %add = add nsw i32 %b, %a
>>       ret i32 %add
>>     }
>>
>>     i would like to know the integer value of %a and %b.
>>
>>     I've tried
>>
>>     -i->getOpcodeName() which gives me the string add
>>
>>     -i->getOperand(0) which gives me the string i32 %b
>>
>>     -i->getOperand(0)->getName() which gives me the string a
>>
>>     what method exists to get the actual integer value of the operands?
>>     for example we called summ(1,2), how to retrieve the values 1 and
>>     2 held in the operands?
>>
>>     Any help and guidance is greatly appreciated! :)
>>
>>     Best,
>>     Ammar
>>
>>
>>     _______________________________________________
>>     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
>
>
>     -- 
>     John Criswell
>     Assistant Professor
>     Department of Computer Science, University of Rochester
>     http://www.cs.rochester.edu/u/criswell
>


-- 
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
http://www.cs.rochester.edu/u/criswell

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160424/52bc8bfc/attachment-0001.html>


More information about the llvm-dev mailing list