[LLVMdev] A problem creating operands for a new IR instruction to the mailing list

Jeffrey Yasskin jyasskin at google.com
Tue May 5 21:18:05 PDT 2009

2009/5/5 seventh moon <suigintou_ at hotmail.com>:
> Thank you for your answer.  But there is still a problem.
> You seem correct about how to define a constant operand.  So I added it into
> my code, like so:
>        Builder.SetInsertPoint(LLVMBB, I);
>        Constant *C = ConstantInt::get(APInt(32, 5, false));
>         Instruction *newI=Builder.Create! Load(C,"");
> It compiles. But it still aborts at runtime with a complaint that the type
> is not right.
> This leads me to think that immediate loads are not generated with the
> CreateLoad
> instruction?

Correct. Immediates are already Value*s in LLVM IR; you don't have to
load them. You may want to read http://llvm.org/docs/LangRef.html for
descriptions of the instructions and the arguments they take. And
http://llvm.org/docs/ProgrammersManual.html#Value for a description of
the Value class.

> Another person's reply to my question indicated that a way to avoid
> inserting the
> immediate load entirely; but that is also not what I want, because my
> explicit purpose
> in inserting this load is to claim a register.

I'd bet that's not actually your purpose. Why do you think you need to
claim a register? Note that LLVM pseudo-registers (the things
Instructions define) are not at all the same as machine registers.

If you really want an Instruction, you can bitcast the constant to its
original type. So:

  new BitCastInst(ConstantInt::get(Type::Int32Ty, 5), Type::Int32Ty, I);

Don't use the IRBuilder to create the BitCast because it'll
constant-fold it into a ConstantExpr, which isn't an Instruction.


More information about the llvm-dev mailing list