[LLVMdev] How to Load a Value?

Óscar Fuentes ofv at wanadoo.es
Thu Apr 8 10:58:27 PDT 2010


Zheng Wang <jason.wangz at gmail.com> writes:

> I have a problem of generating a load instruction. The LLVM bytecode is:
>
> ------------------------
> entry:
>  ...
>  %2 = call i32 (...)* @atoi(i8*%1) nounwind
>  /*<- Insertpos*/
> ...
>
> --
> bb1:
>  ..
>  %5 = icmp sgt i32 %2, %i.0
>  ...
> -----------------
>
>
> Now I have
>
> pb: pointer to the Value object of *%2* of bb1.
>
> Here, I want to generate a load instruction and I did it as:
>
>  new LoadInst(pb, "load_2", InsertPos);
>
> where InsertPos points to the position immediately after "%2 = call
> i32 (...)* @atoi(i8*%1) nounwind".
>
>
> BUT I got a runtime error as:
>
> "
> include/llvm/Support/Casting.h:199: typename llvm::cast_retty<To,
> From>::ret_type llvm::cast(const Y&) [with X = llvm::PointerType, Y =
> const llvm::Type*]: Assertion `isa<X>(Val) && "cast<Ty>() argument of
> incompatible type!"' failed."
>
> This is because LoadInst is implemented as:
>
> LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
>   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
>                      Load, Ptr, InsertAE)
> {
>   ...
> }
>
> and pb->getType() is not a "PointerType"!!! WHY? Is it because it is a
> function call? In this situation, how can I load %2 assuming it has
> been load before.
>
> Can anybody gives me some hints?

You don't need to load the value. `pb' IS the value. The `load'
instruction is used for getting a value through a pointer, but your call
to `atoi' does not return a pointer, it returns an integer value, which
is what you want, so `load' makes no sense. Do not confuse LLVM
Assembler pointer types with C++ pointers to LLVM instructions.

When confused, use the online demo on

http://www.llvm.org/demo/

feed it with some C/C++ and look at both the generated LLVM assembler
and the equivalent C++ code. Remember that there is a one-to-one
relation among them.




More information about the llvm-dev mailing list