0

I was reading: https://llvm.org/docs/LangRef.html#getelementptr-instruction

From what I can understand LLVM has infinite registers which raises the question, why we need store and load commands at all and when should we use them.

Why can't we keep everything in registers?

To make things clear, can you kindly refer to which commands are used in every line:

void example_func(int x) {
  int y;
  int z = 3;
  int a = x;
  a = 3;
  x = 2;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
David
  • 53
  • 4
  • 2
    Presumably you need loads to access memory, like if you want to implement `void foo(int *p) { *p = 123; }`. In your example, you don't need anything at all, the function might as well be empty unless you make a debug build. See [Why does clang produce inefficient asm with -O0 (for this simple floating point sum)?](https://stackoverflow.com/q/53366394) , and for making functions that compile to something interesting to look at, see [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) (take args and return a value, e.g. `return (x + z)*2 - 3*a;`) – Peter Cordes Jul 04 '23 at 00:26
  • 5
    Simple local variables do not need loads & stores. Most data structures (e.g. arrays, trees) are memory based. Manipulation of them requires loads & stores; complex local variables may require loads & stores. – Erik Eidt Jul 04 '23 at 00:38
  • For future readers: **this is asking about loads and stores in the LLVM-IR (I hope), not in the resulting asm**. LLVM-IR is an SSA form where assignments are to virtual "registers", aka local temporary values. It's an IR, and the number of "registers" you can use is unbounded, to support functions that make an unbounded number of mutations to an unbounded number of local variables. It's up to the compiler to figure out which things need to get spilled to memory when. https://llvm.org/docs/LangRef.html#abstract – Peter Cordes Jul 06 '23 at 00:29

1 Answers1

0

You asked why we need loads and stores and I will give you one reason why. Say your goal is to compile a program that has 32 registers but you want to have more than 32 variables live at a time. Well now you can see there is a need to load and store with memory.

Consider the following scenario:

int f(int a) {
  return a == 0 ? 0 : f(a-1);
  return f(a-1);
}
f(50);

If you consider the call tree, you will realize that there is an a for every call to f, and there are more than 32 calls to f, hence the need for loads and stores.

yasgur99
  • 756
  • 2
  • 11
  • 32
  • LLVM-IR is an SSA form where assignments are to virtual "registers". These are the registers the OP is talking about (I hope). It's an IR, so yes, the number of registers you can use is unbounded, to support functions that make an unbounded number of mutations to an unbounded number of local variables. It's up to the compiler to figure out which things need to get spilled to memory when. – Peter Cordes Jul 05 '23 at 21:33
  • See https://llvm.org/docs/LangRef.html#abstract re: LLVM-IR calling itself and SSA assembly language, and specifically https://llvm.org/docs/LangRef.html#identifiers - "*Local identifiers (register names, types) begin with the '%' character*" – Peter Cordes Jul 05 '23 at 21:34