constructing 'for' statement from LLVM bitcode

Hello, guys.
I am trying to construct higher-level 'for' from the low-level LLVM bitcode(ver 1.9).
It's partly successful thanks to David A. Greene's advice suggested to use Control Dependence Graph(CDG).
I could find which BB contributes to form which loop with CDG.
For example, for this simple function:

I strongly suggest looking at the Muchnick book: http://www.amazon.com/Advanced-Compiler-Design-Implementation-Muchnick/dp/1558603204

It has a section on "structural analysis" that you will find useful.

Why do you want "for statements"?

-Chris

Hi,

Seung Jae Lee wrote:

Hello, guys.
I am trying to construct higher-level 'for' from the low-level LLVM bitcode(ver 1.9).
It's partly successful thanks to David A. Greene's advice suggested to use Control Dependence Graph(CDG).
I could find which BB contributes to form which loop with CDG.

I know my post comes late... Are you still interested in this subject?
No? Just ignore it;)

I have been recently dealing with a task which is somehow similar to
yours. I was trying to reconstruct simple 'for' loops and check if it's
possible to automatically parallelize their execution.

For example, for this simple function:
-----------------------------------------------------------
void bsloop(int n, int pM, Params* ps) {
   int i, sim;
   for (sim=0; sim < pM; sim++) {
      for (i = 0; i < n; i++) {
         Params* p = ps + i;
         double eps = norm(0,1);
      }
   }
}

I think it would be possible to reconstruct loops like this. However, it
might be very difficult (impossible in general?) if they contained
'break', 'continue' or 'goto' statements.

Have you tried the use LoopInfo pass for your aim? If you compile the
example to bitcode without optimization and then choose optimizer passes
manually:
opt -mem2reg -instcombine -indvars -o bsloop-opt.bc bsloop.bc
you will get the following code for bsloop() function (llvm and
llvm-gcc-4.2 from SVN):