get basic blocks inside a loop

Hi all,

I have a question about llvm::LoopBase getBlocks() method. Does it return the basic blocks inside the loop in random order? I need to order the blocks in some valid topological ordering of the AST. If getBlocks() does not do that, what can I do to find the ordering?

Thanks,
Naznin

Whenever I was tying to do that (in version 2.8), it didn't give them
to me in a topological order. What I did, as a hack-ish temporary
measure, was rely on Function's ordering, and just iterated over all
the blocks in the function, checking to see if the loop contains that
block. Not at all ideal, and what I was writing later evolved to not
need the topological ordering constraint, so I never revisited the
problem. In hindsight, I don't even know if Function guarantees that
they are presented in a topological order, but it always happened to
be the case for me.

Thanks Michael. Can you please explain you way a bit more? Did you run a function pass then a loop pass?

naive way:
    for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
      if (L.contains(BB))
         ... do stuff ...

Where F is your Function and L is your Loop (from LoopInfo.h). If you
want an O(n) approach, you can just create a pointer-set containing
all of the blocks in the loop, then query against that, since queries
for SmallPtrSets[1] are constant time (after growing beyond the
"small" size, I think). And of course there are miscellaneous other
improvements like keeping a counter and checking for the loop's size,
start the Function iterator (somehow) at the loop's header, etc.

Again, this was just my quick approach, and I'm not sure that my
assumption that Function always presents a topological-order is true
in all cases.

[1] http://llvm.org/docs/ProgrammersManual.html#dss_smallptrset