How to assign a constant to a register?

If x is a local variable, it will be stored on the stack. So you need an
alloca for it:

Aha, I remember this in Kaleidoscope tutorial. So, after the alloca %x can
be modified freely (e.g. without adding suffix)?

Hi,

After the alloca, %x is of type i8* (i.e. a pointer to whatever type x
is in your HLL). It can be mutated using the 'store' instruction.

Note that if you want to read its value, you will have to perform a
'load' to a temporary - for example to increment 'x' by one:

Load i8 %x.tmp1, i8* %x
Add i8 %x.tmp2, i8 %x.tmp1, i8 1
Store i8* %x, %x.tmp2

Think of the LLVM architecture as like a load/store RISC machine, apart
from it has infinite registers and each register can only be written to
once.

Cheers,

James

From: llvmdev-bounces@cs.uiuc.edu [mailto:llvmdev-bounces@cs.uiuc.edu]
On Behalf Of leledumbo
Sent: 21 October 2010 07:20
To: llvmdev@cs.uiuc.edu
Subject: Re: [LLVMdev] Re : How to assign a constant to a register?

> If x is a local variable, it will be stored on the stack. So you

need

Yes, because an alloca allocates stack memory for %x, so this means that %x is a
pointer, not a register in SSA form.
So you need to use "load" and "store" to access the contents of the variable and
to modify it respectively (as shown in the "Memory in LLVM" section:
http://llvm.org/docs/tutorial/LangImpl7.html#memory )
Using local registers is less verbose (because you do not have all those
load/store operations), and is not too hard to generate if you already have a
representation in SSA form.
On the other hand, using loads and stores does not require the SSA form, and the
code will use registers anyway once the mem2reg pass is run.

Matthieu

----- Message d'origine ----