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;
}
3
  • 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)? , and for making functions that compile to something interesting to look at, see How to remove "noise" from GCC/clang assembly output? (take args and return a value, e.g. return (x + z)*2 - 3*a;) Commented Jul 4, 2023 at 0: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
    Commented Jul 4, 2023 at 0: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. llvm.org/docs/LangRef.html#abstract Commented Jul 6, 2023 at 0:29

1 Answer 1

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.

2
  • 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. Commented Jul 5, 2023 at 21:33
  • See llvm.org/docs/LangRef.html#abstract re: LLVM-IR calling itself and SSA assembly language, and specifically llvm.org/docs/LangRef.html#identifiers - "Local identifiers (register names, types) begin with the '%' character" Commented Jul 5, 2023 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.