Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does load, store, and alloca get used in LLVM

Tags:

c

llvm

I am looking at LLVM to see how they use load, store, and alloca. In the first slide below, there is no use of them. In the second, there is use of alloca.

I am not familiar with C so going to have to bring myself up to speed in order to run an example and figure this out myself, but wanted to ask if anyone knew already. Not sure the kind of example C code to write in order to determine the output that uses load, store, and alloca in LLVM.

The question is, when LLVM uses load, store, and alloca.

Wondering if load/store are necessary as well, or LLVM can do without it.

Figure 1 ↓

enter image description here

Figure 2 ↓

enter image description here

like image 656
Lance Avatar asked Mar 07 '23 03:03

Lance


1 Answers

Without optimizations, clang will produce LLVM code where there's one alloca for each local variable, one read for each use of that variable as an r-value and one store for each assignment to that variable (including its initialization).

With optimizations, clang will try to minimize the number of reads and store and will often eliminate the alloca completely if possible (using only registers).

One way to ensure that the variable is stored in memory, even with optimizations, would be to take its address (since registers don't have an address).

Wondering if load/store are necessary as well, or LLVM can do without it.

You need store / load whenever you write to memory locations. So the question becomes whether you can do without memory, storing everything in registers. Since LLVM (unlike real machines) supports an infinite amount of registers, that's a valid question.

However, as I mentioned, registers don't have addresses. So any code that takes the address of a variable, needs to use memory. So does any code that performs arithmetic on addresses, such as code that indexes arrays.

like image 158
sepp2k Avatar answered Mar 10 '23 12:03

sepp2k