[LLVMdev] Create the Value object for StoreInst

Nick Lewycky nicholas at mxc.ca
Sun Aug 15 18:46:23 PDT 2010


θΏͺ wrote:
> Hello all,
>
> I would like to create a new StoreInst into a basic block.

Given a StoreInst *SI and BasicBlock *BB:

   SI->insertBefore(BB->getTerminator());

but you should consider using:

   IRBuilder<> builder(BB);
   builder.CreateStore( ... );

instead.

   I found that
> I could use methods like ConstantInt::get() to provide the Value* for
> the first argument of the constructor of the StoreInst, but I don't know
> how to provide the second one.  For example, if I would to do x = 1, I
> would like to create the "Value" object of "x" for the second argument.

Use an alloca.

   IRBuilder<> builder(BB);
   Value *X = builder.CreateAlloca(type, 0, "X");
   builder.CreateStore(ConstantInt::get(...), X);

> I tried to create the Value object directly to the StoreInst and it
> compiled, but it segfaulted when executing.  Also for this approach, I
> couldn't provide the variable name (i.e. the "x") I want.  I've been
> searching for the solution for a long time but haven't found anything to
> it.  Is there any way to achieve this?  Thank you very much!

If you haven't already, please read the tutorial:

   http://llvm.org/docs/tutorial

in particular chapter 3.

Nick



More information about the llvm-dev mailing list