[LLVMdev] PHI and Allocas

Renato Golin rengolin at systemcall.org
Thu Oct 1 14:35:51 PDT 2009


Hi,

I'm incrementing my toy compiler to account for variable mutations
around if/else blocks.

Reading the Kaleidoscope tutorial, I've learnt that I can substitute
the PHI node creation by allocas and retrieving the value later, if
needed. The Kaleidoscope example seems to account for one value only,
the result of:

Value *ThenV = Then->Codegen();
(...)
Value *ElseV = Else->Codegen();
(...)
PN->addIncoming(ThenV, ThenBB);
PN->addIncoming(ElseV, ElseBB);

But both Then and Else are expressions, so the Value returned is in
the form of a single variable.

ExprAST *Then = ParseExpression();
(...)
ExprAST *Else = ParseExpression();

In my toy language, I accept any number of statements inside a block.

Ignoring nested if statements, should I keep track of *each* variable
mutation inside the block, or is there some LLVM magic (as I'm getting
used to)?

A simple C example would be:

int a, b, c, d;
(...)
if (a > 10) {
  b = c = d = 10 * a;
} else {
  b = c = d = 10 / a;
}

As I'm always using alloca, the mem2reg pass seems to do the trick,
but I'm not sure it would in all cases. The tutorial states that it
works on all scalar values (I have no structs or arrays), so I believe
I'm safe.

Am I?

cheers,
--renato



More information about the llvm-dev mailing list