Operand, instruction

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

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

Nipun Arora wrote:

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
  

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.

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.

John Criswell wrote:

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.

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

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.

Devang Patel-2 wrote:

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

Duncan Sands wrote:

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.

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.

Duncan Sands wrote:

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

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.

Duncan Sands wrote:

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.

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.

John Criswell-2 wrote:

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 wrote:

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.

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.

John Criswell-2 wrote:

Thanks for the reply.

Nick Lewycky wrote:

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:

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