[LLVMdev] How to assign a constant to a register?

Duncan Sands
2010-10-19T07:56:49Z

Hi leledumbo,

> My bad... I should've started thinking in SSA way all the time. I got
it
> mixed with common assembly language. So I must hold the initial value until
> the first operation that deals with it has come, right?

I'm not sure exactly what the problem you are trying to solve is, but it
sounds
like it may be analogous to a problem dragonegg had to solve: it converts gimple
in SSA form to LLVM IR, with "SSA names" being converted to
"registers", but as
it doesn't visit basic blocks in dominator order it may visit uses of an SSA
name before encountering the SSA name's definition.  When this happens it
creates a fake "place holder" value which it later replaces with the
real value,
once it has it.  The trick here is that the fake value is an instruction with no
parent, so it can be easily distinguished from non-fake instructions, which
always have a parent.  Here's the code:

/// getSSAPlaceholder - A fake value associated with an SSA name when the name
/// is used before being defined (this can occur because basic blocks are not
/// output in dominator order).  Replaced with the correct value when the SSA
/// name's definition is encountered.
static Value *GetSSAPlaceholder(const Type *Ty) {
   // Cannot use a constant, since there is no way to distinguish a fake value
   // from a real value.  So use an instruction with no parent.  This needs to
   // be an instruction that can return a struct type, since the SSA name might
   // be a complex number.  It could be a PHINode, except that the GCC phi node
   // conversion logic also constructs phi nodes with no parent.  A SelectInst
   // would work, but a LoadInst seemed neater.
   return new LoadInst(UndefValue::get(Ty->getPointerTo()), NULL);
}

Ciao,

Duncan.