[LLVMdev] constructing 'for' statement from LLVM bitcode

Seung Jae Lee lee225 at uiuc.edu
Fri Aug 24 22:07:18 PDT 2007


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:
-----------------------------------------------------------
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); 
      }
   }
}
-----------------------------------------------------------
LLVM bitcode is emitted like this:
-----------------------------------------------------------
void %bsloop(int %n, int %pM, %struct.Params* %ps) {
entry:
        %pM = cast int %pM to uint              ; <uint> [#uses=1]
        %tmp1822 = setgt int %pM, 0             ; <bool> [#uses=1]
        br bool %tmp1822, label %bb9.outer, label %return

bb9.outer:              ; preds = %bb12, %entry
        %indvar23 = phi uint [ 0, %entry ], [ %indvar.next24, %bb12 ]           ; <uint> [#uses=1]
        br label %bb9

bb1:            ; preds = %bb9
        %tmp = tail call double %mtrandres53( sbyte 1 )         ; <double> [#uses=0]
        %indvar.next = add uint %indvar, 1              ; <uint> [#uses=1]
        br label %bb9

bb9:            ; preds = %bb1, %bb9.outer
        %indvar = phi uint [ 0, %bb9.outer ], [ %indvar.next, %bb1 ]            ; <uint> [#uses=2]
        %i.1 = cast uint %indvar to int         ; <int> [#uses=1] 
        %tmp = setlt int %i.1, %n               ; <bool> [#uses=1]
        br bool %tmp, label %bb1, label %bb12

bb12:           ; preds = %bb9
        %indvar.next24 = add uint %indvar23, 1          ; <uint> [#uses=2]
        %exitcond = seteq uint %indvar.next24, %pM              ; <bool> [#uses=1]
        br bool %exitcond, label %return, label %bb9.outer

return:         ; preds = %bb12, %entry
        ret void
}
-----------------------------------------------------------
This code above offers me info of Control Flow Graph(CFG).
(If I could number each BB above like this:)
-----------------------------------------------------------
entry -> (0)
bb9.outer -> (1)
bb1 -> (2)
bb9 -> (3)
bb12 -> (4)
return -> (5)
-----------------------------------------------------------
With the CFG info, I could make the dominator tree, dominance frontier and finally Control Depedence Graph(CDG) shown as follows:
-----------------------------------------------------------
(x) | CDG of (x)
(0) | (1),(3),(4)
(1) | - 
(2) | -
(3) | (2),(3)
(4) | (1),(3),(4)
(5) | -
-----------------------------------------------------------
Now that (3) is a member of CDG of (3) and (4) is a member of CDG of (4), they are found as loops and the loop which is composed of BB (2) and (3) is inside of the loop comprises BB (1)~(4).
I can manage to re-construct 'for' with this CDG info but generally it might be much complex if 'for' is nested several times and mixed with 'if' and so on in the high-level code so it's not sure if I can succeed in those cases. 
Do you have any idea on how I can construct 'for' more systemically with this CDG info from LLVM bitcode?

Thanks,
SJL



More information about the llvm-dev mailing list