[cfe-dev] SSA form in Clang

David Chisnall David.Chisnall at cl.cam.ac.uk
Wed May 6 03:27:45 PDT 2015

On 5 May 2015, at 07:07, Hayden Livingston <halivingston at gmail.com> wrote:
>  I realized that after looking through all the
> code that goes into identifying loops in LLVM IR. We already know
> we're in a loop at the source code level

It’s not quite so clear cut.  For example, consider the following:

do { … } while (0);

Looks like a loop in the AST, but clearly isn’t in the IR.  This is a common idiom in code that lives in macros.

Or what about:

	if (fail) goto retry;

Doesn’t look like a loop in the AST, but clearly is in the IR.  Loop nests are also often quite different in structure after inlining.  Part of the point of doing this in the IR, rather than in a higher-level representation, is that it allows for better canonicalisation.  For example, you don’t need to special case all of the variations on:

do {} while(1)

You also can detect increment conditions much more accurately, whether or not the programmer has put them in the increment part of a for loop.

Programmers tend to be surprised if switching between a for loop and a while loop impacts code generation.  Doing the loop optimisations after canonicalisation makes this much less likely to happen.


More information about the cfe-dev mailing list