[LLVMdev] get basic blocks inside a loop

Michael Ilseman michael at lunarg.com
Mon May 9 16:00:29 PDT 2011


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

On Mon, May 9, 2011 at 4:37 PM, Naznin Fauzia <laboni14 at gmail.com> wrote:
> Thanks Michael. Can you please explain you way a bit more? Did you run a
> function pass then a loop pass?
>
>
> On Mon, May 9, 2011 at 12:56 PM, Michael Ilseman <michael at lunarg.com> wrote:
>>
>> 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.
>>
>>
>> On Mon, May 9, 2011 at 10:45 AM, Naznin Fauzia <laboni14 at gmail.com> wrote:
>> > 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
>> >
>> > _______________________________________________
>> > LLVM Developers mailing list
>> > LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
>> > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>> >
>> >
>
>




More information about the llvm-dev mailing list