How to Load a Value?


I have a problem of generating a load instruction. The LLVM bytecode is:

Dear Zheng,

You may want to review the LLVM Language Reference Manual (

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:

%2 is an i32, so you don't need to load it before using it. If you
were writing in C, the load instruction you're trying to add would be

int two = atoi(...);
... loaded_two = *two;

which doesn't make any sense.

Zheng Wang <> writes:

I have a problem of generating a load instruction. The LLVM bytecode is:

%2 = call i32 (...)* @atoi(i8*%1) nounwind
/*<- Insertpos*/

%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,
>::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

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.

Thanks guys!!

I got it!

I was confuse with the global variables, which require a explicit load!

Something like load i32* @variable.