11

Consider the following piece of code:

int main() {
    int a = 0;
    int b = 1;

    for (int i = 0; i < 3; i++) {
        a = 2;
        int c = 1;
        int d = 3;
        d = a + c;
    }

   a = b+2;
}

In the piece of code above three variables have a lifespan contained in the body of the loop (i, c and d). I would like to be able to count the variables whose lifespan exists in the body of any given loop using LLVM (i.e. for this loop, my code should return 3).

I found the Live Variables Analysis, but I'm having trouble using it to find what I described above.

Frank C.
  • 7,758
  • 4
  • 35
  • 45
Farhad
  • 516
  • 3
  • 14
  • 3
    Note that the compiler may well optimise your code to `int main(){}`. So something analysing the compiled code might yield some surprises. – Bathsheba Dec 29 '17 at 07:47
  • @Bathsheba That's fine, I just want to identify whatever variables I can in the optimized IR. I don't care too much about the original C++ code. – Farhad Dec 29 '17 at 07:50
  • It's an interesting question (I upvoted it), but if I ever need to analyse what a compiler has done, I check the assembly. Perhaps I'm missing the point of your question? – Bathsheba Dec 29 '17 at 07:51
  • @Bathsheba I have a large set of for loops that I need to automatically extract various features from. Manually checking it myself is unfeasible unfortunately because of the number of loops. It's part of a larger research project. – Farhad Dec 29 '17 at 07:55
  • 2
    I'm not an llvm expert, but looking at the [generated AST](https://godbolt.org/g/tDD8CC) it should be easy to enumerate all VarDecl that are childs of ForStmt ... (well, it depends on what you exactly mean by "live variable", but you got the idea) – Massimiliano Janes Dec 29 '17 at 09:41
  • I believe Live Variables Analysis that you've found is meant to be used for register allocation, so it might be hard to adopt it for your problem. Besides as you can see it's a MachineFunctionPass, meaning it works in code generation phase, while you probably might want to do your analysis in llvm IR level. I found this paper useful while implementing liveness analysis [https://hal.inria.fr/inria-00558509v1/document] – Anahit Hayrapetyan Dec 29 '17 at 22:58

1 Answers1

1

Maybe this should just be a comment, but I couldn't express the code inline:

Only two variables have duration within the loop body. i is declared before the loop starts, and lasts until after the last execution of the loop body. In other words, c and d are constructed/destructed 3 times; after the third time they are destructed, then i is.

Thus, the for loop you wrote is equivalent to:

{
    int i = 0;
    while (i < 3)
    {
        a = 2;
        int c = 1;
        int d = 3;
        d = a + c;

    }
    i++;
}

The extra set of braces invokes block scoping; i goes out of scope and is destroyed outside of the for loop body, but before any subsequent code.

stands2reason
  • 672
  • 2
  • 7
  • 18