How to distinguish global memory objects from none-global ones in LLVM-IR?

“None-global memory objects must be accessed via load/store instruction with pointers points to them; whereas global memory objects can be accessed directly.” Is that right?

Functions and global variables both are global objects, functions can have pointers points to them, can global variables have pointers points to them too?

Global variables at usage site are always pointers to the type you declared them as. So eg. if you have

@i = global i32 0

then you have to access its value using a load and store a value into it using a store.

define i32 @foo() {
  store i32 5, i32* @i
  %1 = load i32, i32* @i
  ret i32 %1
}

as you can see even though you defined the global as i32, the type of @i is actually i32*, so a pointer.
Generally speaking this is the case for all global objects, so functions as well. That is why functions always are of type pointer to function type. A pure function type does not exist in LLVM, or rather, not in material form.

So to summarize your first quote: No, that is incorrect. load and store instructions are used to access any kind of memory. Creating a global allocates memory that is valid for the lifetime of the program. Using alloca allocates memory that is valid for the lifetime of that function calls frame. The third kind of memory would be heap memory. One uses load and store instructions for all of those.

1 Like

The reason LLVM alloca exists and globals behave this way is because it elimiantes the need for an “address of” instruction. This makes it much simpler to reason about pointer aliasing, among other benefits.