redefenition of variables in LLVM

Hi,

%_t4 = add int 23 , 0
%_t5 = add int %_t4 , 1
%_t4 = add int %_t5 , 0

the above line of code give an error when i try to convert to
bytecode. The error is that I am trying to redefine %_t4. Is there a
way to reassign a variable with another value. Also is there a way to
assign a value to a variable. I couldnt do that so I added 0 to it !
:slight_smile:

Thanks

Jai

No, this is Static Single Assignment form, which means there can only be one assignment to a value. If you want multiple assignment, use memory (such as a stack location). E.g.:

%t4 = alloca int
%_t4 = add int 23 , 0
store int %_t4, int* %t4
%_t5 = add int %_t4 , 1
%_t4 = add int %_t5 , 0
store int %_t4, int* %t4

Given (ugly) code like this, the LLVM -mem2reg pass will construct SSA for you and discard the unneeded allocas.

-Chris

*** Sorry, of course this won't work either. If you replace the last two lines with:

%_t4.2 = add int %_t5 , 0
store int %_t4.2, int* %t4

It should work.

-Chris

Jai,

Just to add a bit of context to Chris's reply ...

As he pointed out, LLVM registers are in SSA form but memory is not. You do not need to generate SSA form when generating LLVM in your front-end, because it is not straightforward to do so. Instead, you can "alloca" all variables on the stack, and use loads and stores to access their values. mem2reg will promote most such alloca variables back to registers in SSA form.

If you want to generate more compact code, you can put local temporaries (from within a simple source statement) in SSA registers since they are not used outside the basic block where they are generated. For example, you could put %_t5 in a register below easily, but leave _t4 on the stack. This is not particularly important for small programs.