LLVM中load、store、alloca指令的使用场景及必要性咨询
Hey there! Let's break down your questions about LLVM's alloca, load, and store instructions clearly, since you're diving into LLVM IR and aren't super familiar with C yet.
alloca, load, and store 1. alloca Instruction
alloca is LLVM's way of allocating memory on the stack for local variables. You'll see it whenever:
- You declare a non-static local variable in C (like
int x;orchar buf[10];). LLVM needs to reserve stack space for these variables before you can read/write to them. - The compiler needs temporary stack storage for intermediate values (though this is often optimized away in later passes).
2. load and store Instructions
LLVM IR uses a Static Single Assignment (SSA) form, meaning all values used in computations live in virtual registers. Since memory (stack, heap, global variables) isn't a register, we need these two instructions to bridge the gap:
store: Writes a value from a register into a memory address. Use this whenever you assign a value to a variable (local, global, or pointer-targeted).load: Reads a value from a memory address into a register. Use this whenever you need to use the value of a variable in a computation.
These two are almost always paired with alloca for local variables, but they also work with global variables, heap-allocated memory (from malloc in C), and function parameters that are pointers.
load/store Required in LLVM IR? Yes, they're essential for any operation that involves accessing memory. Since LLVM's core computation model relies on register values, you can't directly use a memory address as an operand in arithmetic or other instructions. Any time you need to move data between memory and registers, you must use load or store.
The only exception is if your code never touches memory at all (e.g., a function that just returns an immediate value like int foo() { return 42; }), but that's an edge case.
Here are straightforward C snippets that will produce the LLVM IR you're looking for. You can compile these with clang -S -emit-llvm filename.c to see the corresponding IR:
Example 1: alloca Only
void basic_alloca() { int local_var; // No assignment, just stack allocation }
The LLVM IR will include an alloca i32 line to reserve stack space for local_var.
Example 2: alloca + store
void store_example() { int x = 10; // Allocate stack space, then store 10 into it }
Corresponding IR will have:
%x = alloca i32to allocate stack spacestore i32 10, i32* %xto write the value 10 into that space
Example 3: alloca + store + load
void load_store_example() { int x = 5; int y = x; // Load x's value from memory, then store it into y's stack space }
IR steps:
allocaforxandystore5 intoxloadthe value fromxinto a temporary registerstorethat temporary value intoy
Example 4: load/store with Global Variables
int global_num; // Global variable lives in static memory void global_example() { global_num = 20; // Store to global memory address int z = global_num; // Load from global memory address }
IR will reference the global @global_num and use store/load to interact with it, no alloca needed here (since global variables aren't on the stack).
Example 5: load/store with Pointers
void pointer_example(int *ptr) { *ptr = 100; // Store 100 into the memory address ptr points to int val = *ptr; // Load the value from ptr's target address }
IR will use store i32 100, i32* %ptr and load i32, i32* %ptr to interact with the pointer's target memory.
内容的提问来源于stack exchange,提问作者Lance Pollard

