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?

1 Like

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.

2 Likes

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.

Thank you.

Thank you for answering my question.
Now I have a new question:
In this “Data Layout String”:
target datalayout = “e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128”
What does the “m:e” mean?

It specifies how symbols are mangled (in this case, the ELF way: no prefix for global symbols and private ones start with .L). See LLVM Language Reference Manual — LLVM 16.0.0git documentation

1 Like

thank you