Question
A variable is said to be live at a statement in a program if the following three conditions hold simultaneously:
i. There exists a statement that uses
ii. There is a path from to in the flow graph corresponding to the program
iii. The path has no intervening assignment to including at and
The variables which are live both at the statement in basic block 2 and at the statement in basic block 3 of the above control flow graph are
Official answer and solution
- Official answer
- C
- From
- Official GATE 2015 answer key
Solution
Trace variable uses and definitions downstream from basic blocks 2 and 3 using the given definition of live variables.
-
Definition of live variable at statement :
- A variable must be used downstream at some .
- There must exist a path from to without any intervening assignment to (including at and ).
-
Check definitions at blocks 2 and 3:
- Basic block 2 assigns :
v = r + u. By condition (iii), is assigned at this statement, so is NOT live at block 2. - Basic block 3 assigns :
q = s * u. By condition (iii), is assigned at this statement, so is NOT live at block 3.
Hence, neither nor can be live at both blocks.
- Basic block 2 assigns :
-
Check variable :
- In block 2,
v = r + uuses . Is live before this use? The question asks about variables live at the statement in block 2 and block 3. At block 2, is read on the right-hand side, defined in block 1 with no intervening assignment. Similarly, at block 3,q = s * ureads . Hence, is live at both statements.
- In block 2,
-
Check variable :
- Variable is read in block 2 (
v = r + u). - From block 3, control flows to block 4 (
q = v + r), which reads . Since is never assigned anywhere in blocks 1, 2, 3, or 4, reaches its use in block 4 without any intervening definition.
Hence, is live at both statements.
- Variable is read in block 2 (
-
Check variable :
- In block 3,
q = s * uuses . - However, from block 2, control flows through block 4 to block 1, where is redefined (
s = p + q) before any subsequent use of . Therefore, is NOT live at block 2.
- In block 3,
Thus, the variables that are live both at the statement in block 2 and block 3 are and (option C).
C
Including variable s, which is overwritten in block 1 before any subsequent use along the path from block 2.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2015 Computer Science and Information Technology (CS) Session 1
- Question
- 60 of 65, printed as CS Q.50
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.