[LLVMdev] not to break 'for' statement into basic blocks

Anton Korobeynikov asl at math.spbu.ru
Sat Jul 14 15:23:27 PDT 2007


Hello, Seung J. Lee

> LLVM optimization and other tools are really fantastic. 
> However I don't want LLVM breaks my 'for' statement in C code into
> basic blocks during compiling. 
> I'm sure this sounds really strange but there is a reason for me.
LLVM is 'low-level'. It doesn't contain any special instruction for
loops at all.

> Furthermore, this assembly does not have the notion of
> 'br'instruction, phi-node and so on. Therefore, I have no choice
> without making 'for' in the assembly. 
Maybe you can look, how code operates on phi-nodes in C & MSIL backends.

> 1) First, I tried to re-unite basic blocks which llvm-gcc spits out to make 'for' again. 
> But this is quite tricky. Generalizing it for the optimzed llvm bytecode is not easy.
I'd say 'is not possible at all'.

In general, loop contain induction variable, which is being assigned
each iteration of loop. As LLVM IR is in SSA mode, there are only two
possibilities to operate on induction variable so:

1. Use phi node
2. Use memory (memory is never in SSA form)

So, if you don't want to deal with phi-node, you have to lower
everything to memory. 'reg2mem' pass is your friend in this case. This
will eliminate all phi nodes, but you have to resolve all branches.

However, I really don't see, how you can transform C code (in general)
to your target, because C *has* explicit goto statement. Maybe you can
split the whole CFG into "cycles" and "paths" and try to "lower" them.
But this can be pretty complicated.

-- 
With best regards, Anton Korobeynikov.

Faculty of Mathematics & Mechanics, Saint Petersburg State University.





More information about the llvm-dev mailing list