[LLVMdev] PHI and Allocas

Kenneth Uildriks kennethuil at gmail.com
Thu Oct 1 15:55:19 PDT 2009


On Thu, Oct 1, 2009 at 4:48 PM, Renato Golin <rengolin at systemcall.org> wrote:
> 2009/10/1 Kenneth Uildriks <kennethuil at gmail.com>:
>> I hope so, because I've been doing the same thing.  My local variables
>> are always bound to alloca's, and my test makefile just calls "opt
>> -std-compile-opts" after my compiler runs; so far it's been giving me
>> pretty good code as a result.
>
> Same here, but I'm not compiling any really complex code, so I thought
> it was just beginner's luck.
>
> Looks like it's another score for the "LLVM magic"! ;)
>
> It might create problems for a potential debugger, but I'm not
> planning to create one too soon.

There's a lot more magic where that came from.

Case in point:  my language has an "exit" statement, where any
statement you put after it gets run at the end of the scope it appears
in.  You can also put return statements in any scope you like, and
every containing scope will have its exit functions run on the way
out.  To make that work, I alloca'd a "destination scope" variable,
then when I finish a scope, I go to the exit statements, and the exit
statements are followed by a conditional jump based on the value of
the destination scope... I can go to the code right after the scope,
or to the containing scope's exit code.  A return statement just
compiles down to:

returnval = whatever
destScope = -1
goto scopeExit

If execution just goes to the end of the scope, it does:

destScope--;
goto scopeExit

Anyway, the code that my front-end outputs is full of statements
fiddling with scope destinations and conditional jumps, but when you
run it through "opt -std-compile-opts", all that goes away and, when
control unconditionally leaves a scope normally, the conditional
branches are gone too.




More information about the llvm-dev mailing list