Clarification regarding the LLVM IR.

Hi,

Looking at the documents, it looks like all instructions, e.g. ADD, SUB work only on labels/registers and constants.

e.g.
%1 = add nsw i32 %value, 291

The only instructions that can work on the values on the stack or on the heap are LOAD and STORE.

So the following is not possible:
%local = alloca i32, align 4
%1 = add nsw i32* %local, 291

Is the above correct?

Kind Regards

James

Hi James,

The only instructions that can work on the values on the stack or on the
heap are LOAD and STORE.

That's more or less true. If your variable is in memory then you have
to load it before doing operations and store it back afterwards. There
are one or two other instructions that can perform loads and stores
though (atomicrmw, certain intrinsics, ...).

%local = alloca i32, align 4
%1 = add nsw i32* %local, 291

To add 291 to this local variable you'd write:
    %curVal = load i32* %local
    %newVal = add i32 %curVal, 291
    store i32 %newVal, i32* %local

Optimizations (one called mem2reg in the first instance) will normally
get rid of the implied inefficiency in loading and storing everything.

Tim.