[LLVMdev] Operand, instruction

1,795 views
Skip to first unread message

Nipun Arora

unread,
Feb 11, 2009, 1:42:47 PM2/11/09
to LLVM Developers Mailing List
Hi,

How can one extract the operand of an instruction in an LLVM pass?
Like I can get the opcode bt I'd like to get the operands as well

Thanks
Nipun

Devang Patel

unread,
Feb 11, 2009, 1:49:12 PM2/11/09
to LLVM Developers Mailing List

getOperand(i). See Instruction.h and User.h

-
Devang
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

John Criswell

unread,
Feb 11, 2009, 3:44:21 PM2/11/09
to LLVM Developers Mailing List
Use the getOperand() method of class Instruction (which I think is
inherited from Value or User or some other LLVM class). It takes a
single parameter that is an index specifying which operand to return.
The return value is a llvm::Value *, IIRC.

If you haven't used it yet, I'd recommend using the LLVM doxygen
documentation (http://llvm.org/doxygen/hierarchy.html). I've found it
to be an invaluable resource for answering these sorts of questions. In
this case, just look up the llvm::Instruction class and see if it has a
method that does what you want. If it doesn't, check its parent class,
the grandparent class, etc. until you find the method you want.

-- John T.

> Thanks
> Nipun

help__me_please

unread,
Mar 26, 2010, 9:53:35 AM3/26/10
to llv...@cs.uiuc.edu

Can anyone tell how to get the result name or instruction name of all
instruction? For example if the instruction is "x=add y,z", here i need "x".
Using getName(), i am getting some instructions result name, but llvm
produces some instruction like "%0=add i32 tmp, 1", here getName() shows
empty string as result name.
So please help.

--
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28042767.html
Sent from the LLVM - Dev mailing list archive at Nabble.com.

Duncan Sands

unread,
Mar 27, 2010, 1:21:38 PM3/27/10
to llv...@cs.uiuc.edu
Hi,

> Can anyone tell how to get the result name or instruction name of all
> instruction? For example if the instruction is "x=add y,z", here i need "x".
> Using getName(), i am getting some instructions result name, but llvm
> produces some instruction like "%0=add i32 tmp, 1", here getName() shows
> empty string as result name.
> So please help.

as you have noticed, names are optional: instructions may not have names.
That said, you can give names to all instructions by running the instnamer
pass.

Ciao,

Duncan.

Devang Patel

unread,
Mar 28, 2010, 3:51:28 PM3/28/10
to help__me_please, llv...@cs.uiuc.edu
On Fri, Mar 26, 2010 at 6:53 AM, help__me_please
<krish...@cse.iitb.ac.in> wrote:
>
> Can anyone tell how to get the result name or instruction name of all
> instruction? For example if the instruction is "x=add y,z", here i need "x".
> Using getName(), i am getting some instructions result name, but llvm
> produces some instruction like "%0=add i32 tmp, 1", here getName() shows
> empty string as result name.
> So please help.
>

Instruction names are optional and not reliable. Why do you need a
name ? You can use use_iterator to find instruction's uses.
-
Devang

help__me_please

unread,
Mar 28, 2010, 11:13:03 PM3/28/10
to llv...@cs.uiuc.edu

Actually i have to implement strength reduction, for that i have to first
detect induction variables using ALLEN-COCKE-KENNEDY algorithm. To find out
induction variables, i need the name of the instruction.
Thanks for the reply.

--
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28064581.html


Sent from the LLVM - Dev mailing list archive at Nabble.com.

_______________________________________________

help__me_please

unread,
Mar 28, 2010, 11:19:17 PM3/28/10
to llv...@cs.uiuc.edu

Thanks for the reply.
My problem is solved.
Thanks once again.

--
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28064599.html


Sent from the LLVM - Dev mailing list archive at Nabble.com.

_______________________________________________

Duncan Sands

unread,
Mar 29, 2010, 8:53:34 AM3/29/10
to llv...@cs.uiuc.edu
Hi,

> Actually i have to implement strength reduction, for that i have to first
> detect induction variables using ALLEN-COCKE-KENNEDY algorithm. To find out
> induction variables, i need the name of the instruction.

I suspect you are confused. Surely you only need a way of referring to each
instruction. For this you can use a pointer to the instruction.

Ciao,

Duncan.

help__me_please

unread,
Mar 30, 2010, 3:50:12 PM3/30/10
to llv...@cs.uiuc.edu

Can you tell how to use vector globally? I mean, i am able to add entries to
a vector in a basicblock, but whenever a basicblock function call is
returned, the vector becomes empty. So how to use data structures globally?
And one thing, where is main ( or same kind of) function in llvm?
Thanks for you reply.

--
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28087559.html


Sent from the LLVM - Dev mailing list archive at Nabble.com.

_______________________________________________

Arvind Sudarsanam

unread,
Mar 30, 2010, 5:59:26 PM3/30/10
to help__me_please, llv...@cs.uiuc.edu
Hi,

Instead of a basic block function call....You might want to use a module
function call...then iterate through functions and then iterate through
basic blocks...
Hope this helped.

Best Regards
Arvind
------------------------------------------------------------------------
------------
Arvind Sudarsanam Phone: (435) 512-7769
CPU Technology, Inc Fax: (866) 848-5599
1500 Kansas Ave, Suite 3E
Longmont, CO 80501

help__me_please

unread,
Apr 13, 2010, 4:26:40 PM4/13/10
to llv...@cs.uiuc.edu

Can you please give an example of creating an instruction (for example add
instructions with two operand a and b)? I am trying instruction() for a
while, but no success yet.

--
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28235150.html


Sent from the LLVM - Dev mailing list archive at Nabble.com.

_______________________________________________

John Criswell

unread,
Apr 13, 2010, 4:31:39 PM4/13/10
to help__me_please, llv...@cs.uiuc.edu
help__me_please wrote:
> Can you please give an example of creating an instruction (for example add
> instructions with two operand a and b)? I am trying instruction() for a
> while, but no success yet.
>

You need to look for the appropriate subclass of llvm::Instruction and
find the method for creating a new instruction. The method is usually a
static method and takes arguments pointing to the values to use as
operands. Doxygen is your best resource for finding these methods.

For example, if you look at
http://llvm.org/doxygen/classllvm_1_1AllocaInst.html, you can see that
the AllocaInst class (which represents an alloca instruction) has a
standard constructor method that takes the type of object to allocate,
the name of the new alloca instruction, an instruction before which to
insert the alloca instruction, etc.

As another example, the CallInst class
(http://llvm.org/doxygen/classllvm_1_1CallInst.html) represents a call
instruction and has a static Create() method that you can use to create
a new call instruction.

-- John T.

help__me_please

unread,
Apr 14, 2010, 1:33:33 AM4/14/10
to llv...@cs.uiuc.edu

Thanks for reply.
I have used AllocaInst, it's working but i think it's only for allocating
some memory for new variable. And CallInst creates a call instruction. I am
looking for creating a add or sub instruction.

I used function instruction(), which gives me error "error: cannot allocate
an object of abstract type ‘llvm::Instruction’" and also "Instruction.h:28:
note: because the following virtual functions are pure within
‘llvm::Instruction’:", "Instruction.h:50: note: virtual llvm::Instruction*
llvm::Instruction::clone(llvm::LLVMContext&) const"

Also i have used BinaryOperator::create(), but it gives error that there is
no function called create().

Thanks again for your reply.

--
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28238512.html


Sent from the LLVM - Dev mailing list archive at Nabble.com.

John Criswell

unread,
Apr 14, 2010, 2:38:19 AM4/14/10
to help__me_please, llv...@cs.uiuc.edu
help__me_please wrote:
> Thanks for reply.
> I have used AllocaInst, it's working but i think it's only for allocating
> some memory for new variable. And CallInst creates a call instruction. I am
> looking for creating a add or sub instruction.
>
> I used function instruction(), which gives me error "error: cannot allocate
> an object of abstract type ‘llvm::Instruction’" and also "Instruction.h:28:
> note: because the following virtual functions are pure within
> ‘llvm::Instruction’:", "Instruction.h:50: note: virtual llvm::Instruction*
> llvm::Instruction::clone(llvm::LLVMContext&) const"
>

An add or sub should be a BinaryOperator, if memory serves me
correctly. Looking at mainline doxygen, there's a whole bunch of
CreateNSWAdd, CreateNSWMul, etc. methods. It's one of those that you want.

-- John T.

> Also i have used BinaryOperator::create(), but it gives error that there is

Nick Lewycky

unread,
Apr 14, 2010, 1:48:08 AM4/14/10
to help__me_please, llv...@cs.uiuc.edu
There's an entire tutorial at http://llvm.org/docs/tutorial which
includes covers the IRBuilder to generate all sorts of IR, including the
add instruction. You can skip ahead to chapter 3 if you just want to see
LLVM API, but I suggest you read the whole tutorial if you have the time.

Nick

help__me_please

unread,
Apr 14, 2010, 3:23:48 AM4/14/10
to llv...@cs.uiuc.edu

Thanks for the reply.
You are correct, sometimes ago i found myself this functions. I was
searching for just this functions. Still one problem is there, adding a
instruction in another basic block, error shows that basic block has no
terminator inst, but that's not true.
Thanks again for help.

--
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28239236.html


Sent from the LLVM - Dev mailing list archive at Nabble.com.

help__me_please

unread,
Apr 14, 2010, 3:25:10 AM4/14/10
to llv...@cs.uiuc.edu

Thanks for the reply.

--
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28239244.html


Sent from the LLVM - Dev mailing list archive at Nabble.com.

Ben Perry

unread,
Apr 22, 2010, 10:43:35 AM4/22/10
to llv...@cs.uiuc.edu, help__me_please
if (ConstantInt *c = (ConstantInt *) dyn_cast<ConstantInt>(index)){
errs() << "And it's a constant!!!!!!1111oneone!! (" <<c->getZExtValue()
<< ")\n";
}

--
"Believe you can, believe you can't; either way you're right."  -Henry Ford


-----Original Message-----
From: llvmdev...@cs.uiuc.edu [mailto:llvmdev...@cs.uiuc.edu] On
Behalf Of help__me_please
Sent: Thursday, April 22, 2010 8:38 AM
To: llv...@cs.uiuc.edu
Subject: Re: [LLVMdev] Operand, instruction


Hello, how to get the numerical value of an constant operand? For ex. %tmp =
mul i32 %indvar, 6 , in this instruction second operand is a numerical
constant with value 6. But i am only able to print it by using
getOperand(1), not able to store it in any integer variable. Using getName()
returns a empty string. Is there any way to solve this problem?
Thank you.
--
View this message in context:
http://old.nabble.com/Operand%2C-instruction-tp21961718p28329007.html
Sent from the LLVM - Dev mailing list archive at Nabble.com.

_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


--
Subscription settings: http://groups.google.com/group/llvm-dev/subscribe?hl=en

help__me_please

unread,
Apr 22, 2010, 9:37:57 AM4/22/10
to llv...@cs.uiuc.edu

Hello, how to get the numerical value of an constant operand? For ex. %tmp =
mul i32 %indvar, 6 , in this instruction second operand is a numerical
constant with value 6. But i am only able to print it by using
getOperand(1), not able to store it in any integer variable. Using getName()
returns a empty string. Is there any way to solve this problem?
Thank you.

John Criswell-2 wrote:
>
--
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28329007.html
Sent from the LLVM - Dev mailing list archive at Nabble.com.

_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


Duncan Sands

unread,
Apr 22, 2010, 10:13:11 AM4/22/10
to llv...@cs.uiuc.edu
Hi Ben,

> if (ConstantInt *c = (ConstantInt *) dyn_cast<ConstantInt>(index)){
> errs()<< "And it's a constant!!!!!!1111oneone!! ("<<c->getZExtValue()
> << ")\n";
> }

I don't think you need the "(ConstantInt *)" cast. The getZExtValue will fail
if the constant doesn't fit in 64 bits.

Ciao,

Duncan.

help__me_please

unread,
Apr 22, 2010, 12:54:54 PM4/22/10
to llv...@cs.uiuc.edu

Thanks for the quick reply.
I am able to get the numerical value. But i also need the numerical value to
be in value type, so that it can be used as operand of other instruction.
For that i am trying to create a new ConstantInt, but i am unable to find
create() function for ConstantInt.

Thanks again for the quick and helpful reply.
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28331688.html
Sent from the LLVM - Dev mailing list archive at Nabble.com.


_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


John Criswell

unread,
Apr 22, 2010, 1:00:18 PM4/22/10
to help__me_please, llv...@cs.uiuc.edu
help__me_please wrote:
> Thanks for the quick reply.
> I am able to get the numerical value. But i also need the numerical value to
> be in value type, so that it can be used as operand of other instruction.
> For that i am trying to create a new ConstantInt, but i am unable to find
> create() function for ConstantInt.
>

There is no need to do that. If you look at the inheritance hierarchy
in doxygen, ConstantInt inherits (indirectly) from Value. You can use a
ConstantInt in any place where a Value can be used.

-- John T.

_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


--
Subscription settings: http://groups.google.com/group/llvm-dev/subscribe?hl=en

help__me_please

unread,
Apr 22, 2010, 2:39:09 PM4/22/10
to llv...@cs.uiuc.edu

Thanks again for your reply.
You are right. But i am looking to change the value of that constant value
and use that as operand in other instruction. For example there is one
constant operand with value 6, i will multiply it with 2, and then use 12 as
operand in other instruction. So i need to convert 12 to value class and
then i will be able to set it as operand.
Hope i am clear now.
Thanks.
View this message in context: http://old.nabble.com/Operand%2C-instruction-tp21961718p28333005.html
Sent from the LLVM - Dev mailing list archive at Nabble.com.

Eli Friedman

unread,
Apr 22, 2010, 3:01:02 PM4/22/10
to help__me_please, llv...@cs.uiuc.edu
On Thu, Apr 22, 2010 at 11:39 AM, help__me_please
<krish...@cse.iitb.ac.in> wrote:
>
> Thanks again for your reply.
> You are right. But i am looking to change the value of that constant value
> and use that as operand in other instruction. For example there is one
> constant operand with value 6, i will multiply it with 2, and then use 12 as
> operand in other instruction. So i need to convert 12 to value class and
> then i will be able to set it as operand.
> Hope i am clear now.
> Thanks.

I think you're looking for ConstantInt::get; the naming is a bit
different from instructions because constants values are reused.

-Eli
Reply all
Reply to author
Forward
0 new messages