[LLVMdev] How to Load a Value?

John Criswell criswell at uiuc.edu
Thu Apr 8 10:52:59 PDT 2010


Dear Zheng,

You may want to review the LLVM Language Reference Manual 
(http://llvm.org/docs/LangRef.html).

The SSA value %2 is an i32 (32-bit integer).  It is an SSA virtual 
register; it is defined once, never written, and can be read multiple 
times.  To read the value, you just use it; no load instruction is 
required.  For example, to pass it to a function, you would write:

CallInst::Create (Function, pb, "", InsertPt)

... where Function is an SSA value containing the Function to call and 
InsertPt is a pointer to an Instruction before which the call should be 
inserted.

Memory, in LLVM IR, is anything that is not in SSA form.  This is 
essentially global variables, stack allocated objects (the result of an 
LLVM alloca instruction), and heap memory (pointers to which are 
returned from an allocator function like malloc()).  You use load and 
store instructions to read and write this memory; load reads the memory 
and places the result in a new SSA virtual register, and store places 
the value in an SSA virtual register into a memory location.

-- John T.


Zheng Wang wrote:
> Hello,
>
> 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?
>
> Cheers,
> Zheng
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>   




More information about the llvm-dev mailing list