I did a Compilers course and now I’m practicing for my exam. One of the topics covered was variable liveness.
Now, outside of a loop, if we have
1. int a = 1;
2. a++;
3. int b = 2;
4. a = b;
then variable “a” is live in 1-2 and 4, right?
Inside of a loop, if we have
1. for (int i = 1; i < 3; i++)
and then no other operations using the value of “i” inside the loop, such as checking its value and so on, then “i” is only live at the point of assignment/increment, right?
Basically the question is, for a loop such as the one mentioned above, is the control variable live within the loop or just when incrementing? Unless of course, that variable is used inside the loop.
Yes, that is correct. Or, since we usually talk about a variable being live after or before (not during) a given step, I should say:
ais live after step 1, before step 2 and after step 4 (assumingawill be used some time after those instructions).You have to think about when each step of the loop happens. The execution of a loop will look something like this:
So each time after the body executes the condition is checked by reading the value of i that was set before the body was executed. This means that i has to be live during the entire execution of the body.