Basic Blocks, Data-flow Analysis and Code Optimization Short Notes for Gate Cs
Basic Blocks, Data-flow Analysis and Code Optimization short notes for GATE CS: 9 study cards covering concepts, formulas, shortcuts and exam traps, plus solv
Basic Blocks Data Flow Analysis and Code Optimization short notes
Checklist: leader identification and block counting
Leader Identification and Block Counting
Can you list all six steps of the partitioning checklist from memory?
Mark the first instruction as a leader.
Mark every jump target as a leader.
Mark every instruction immediately following a conditional or unconditional jump as a leader.
Draw block boundaries starting at each leader and ending just before the next leader.
For each block, draw an edge to the jump target if the last instruction is a jump.
Draw a fall-through edge if the last instruction is not an unconditional jump and the next block immediately follows.
Trap Recall. Students often equate the number of goto statements with the number of leaders or blocks. This feels right because each jump seems to create a new block, which is almost true but misses fall-through leaders. This costs marks by undercounting the blocks. Check. List leaders first by the three-rule definition, then count, rather than counting jumps.
Mental check for doubt
If you ever doubt a boundary, ask two questions. Can control enter this instruction from outside the block? If yes, it needs a boundary before it. Can control leave this instruction to somewhere other than the next instruction? If yes, it needs a boundary after it. This mental check catches every edge case.
Structural insight
This checklist is not just a mnemonic; it is the mechanical execution of the single-entry, single-exit invariant. Every rule corresponds to a specific way control flow can violate the invariant. Mastering this checklist means you no longer think about individual instructions, but about the structural boundaries of the program.
Formula Matrix
USE[B]
Variables read before any write in B.
DEF[B]
Variables assigned a new value in B.
OUT[B]=⋃S∈succ(B)IN[S]
Backward MAY analysis; union over all successors.
IN[B]=USE[B]∪(OUT[B]−DEF[B])
Backward flow; kill effect of local definitions.
IN[Exit]=∅
Standard boundary; no variables live post-termination.
OUT(S1)=IN(S2)
S1,S2 are sequential in the same block.
If You See This, Do This
"sequential statements S1 then S2"
Set OUT(S1)=IN(S2).
"min passes to converge in N-block loop"
Answer is N (backward propagation = 1 pass/block).
"variable used and defined in same block"
Include in BOTH USE[B] and DEF[B].
"simultaneously live at any point"
Apply to Register Allocation (Interference Graph).
"statement eliminated as dead code"
Check if defined variable ∈/OUT(S).
"liveness analysis classification"
MAY analysis, Union, ∅ init, max fixed point.
6 more cards in this chapter
Try a question
Answer it here to see how it works. Nothing is recorded until you sign in.
Question 1
Level 1: Warm-up
A basic block is defined by having exactly E entry points and X exit points to ensure safe boundaries for optimization algorithms. What is the value of E+X?
Question 2
Level 1: Warm-up
Consider the following three-address code:
1: i = 0
2: if i < 10 goto 4
3: goto 6
4: i = i + 1
5: goto 2
6: return i
Which of the following statements about the basic blocks is true?
Question 3
Level 1: Warm-up
A basic block is defined by having exactly E entry points and X exit points to ensure safe boundaries for optimization algorithms. A student incorrectly assumes that a conditional branch at the end of a block creates two exit points, leading to the formula E+X=3. For a valid basic block, what is the actual value of E2+X2?
Question 4
Level 1: Warm-up
When constructing a Directed Acyclic Graph (DAG) for a basic block, under what strict condition can two identical expressions x = a + b and y = a + b share the same internal operator node?
Question 5
Level 1: Warm-up
Consider the following basic block:
t1 = a + b
t2 = c * d
t3 = a + b
t4 = c * d
If the compiler constructs a Directed Acyclic Graph (DAG) for this block to perform local common subexpression elimination, what is the total number of internal (operator) nodes created?
Question 6
Level 1: Warm-up
Consider the following Assertion and Reason regarding global common subexpression elimination:
<b>Assertion (A):</b> If an expression is computed in block B1, and B1 has two successors B2 and B3, the expression is only available at the entry of a successor block B4 (where B2 and B3 merge) if it is available at the exit of both B2 and B3.
<b>Reason (R):</b> Available expressions analysis is a MAY analysis, which requires the union of the OUT sets of all predecessors.
Question 7
Level 1: Warm-up
Consider the following basic block consisting of three assignment statements:
u = x + y;
v = x - y;
w = u * v;
When the compiler constructs a Directed Acyclic Graph (DAG) for this block, what is the total number of nodes (both leaf and internal operator nodes) created, assuming x and y are not redefined and u, v, w are not read subsequently?
Question 8
Level 1: Warm-up
Which of the following statements is universally TRUE regarding the data-flow equations for Available Expressions?
Question 9
Level 1: Warm-up
Which of the following properties are TRUE about a basic block in compiler design?
Question 10
Level 1: Warm-up
In a three-address code sequence, there are exactly 4 conditional branches and 1 unconditional jump. Assuming no jump targets coincide with the instruction immediately following any jump, how many leaders are identified strictly by the rule "an instruction immediately follows a conditional or unconditional jump"?
Free preview ends here
Login to view the complete short notes
Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.
Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.
Built around you, not around a syllabus PDF
Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.
Revision that hits your weak spots
We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.
Questions calibrated to the real exam
Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.
Notes written for recall, not for volume
Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.
One place for everything
Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.
Honest progress
No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.
Unlock the whole course
Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.
Basic Blocks, Data-flow Analysis and Code Optimization Short Notes for GATE CS
Basic Blocks, Data-flow Analysis and Code Optimization short notes for GATE CS: 9 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.
Checklist: leader identification and block counting
Leader Identification and Block Counting
Can you list all six steps of the partitioning checklist from memory?
Mark the first instruction as a leader.
Mark every jump target as a leader.
Mark every instruction immediately following a conditional or unconditional jump as a leader.
Draw block boundaries starting at each leader and ending just before the next leader.
For each block, draw an edge to the jump target if the last instruction is a jump.
Draw a fall-through edge if the last instruction is not an unconditional jump and the next block immediately follows.
Trap Recall. Students often equate the number of goto statements with the number of leaders or blocks. This feels right because each jump seems to create a new block, which is almost true but misses fall-through leaders. This costs marks by undercounting the blocks. Check. List leaders first by the three-rule definition, then count, rather than counting jumps.
Mental check for doubt
If you ever doubt a boundary, ask two questions. Can control enter this instruction from outside the block? If yes, it needs a boundary before it. Can control leave this instruction to somewhere other than the next instruction? If yes, it needs a boundary after it. This mental check catches every edge case.
Structural insight
This checklist is not just a mnemonic; it is the mechanical execution of the single-entry, single-exit invariant. Every rule corresponds to a specific way control flow can violate the invariant. Mastering this checklist means you no longer think about individual instructions, but about the structural boundaries of the program.
Formula Matrix
USE[B]
Variables read before any write in B.
DEF[B]
Variables assigned a new value in B.
OUT[B]=⋃S∈succ(B)IN[S]
Backward MAY analysis; union over all successors.
IN[B]=USE[B]∪(OUT[B]−DEF[B])
Backward flow; kill effect of local definitions.
IN[Exit]=∅
Standard boundary; no variables live post-termination.
OUT(S1)=IN(S2)
S1,S2 are sequential in the same block.
If You See This, Do This
"sequential statements S1 then S2"
Set OUT(S1)=IN(S2).
"min passes to converge in N-block loop"
Answer is N (backward propagation = 1 pass/block).
"variable used and defined in same block"
Include in BOTH USE[B] and DEF[B].
"simultaneously live at any point"
Apply to Register Allocation (Interference Graph).
Check: Liveness is BACKWARD. OUT depends on successor IN.
Assuming v∈DEF[B]⟹v∈/IN[B].
Check: If v∈USE[B] (read before write), v∈IN[B] regardless of DEF[B].
Using intersection (⋂) for OUT[B] calculation.
Check: Liveness is a MAY analysis; always use union (⋃).
Basic Blocks, Data-flow Analysis and Code Optimization: Solved Questions with Step-by-Step Explanations (10 Problems)
Question 1 · Compiler DesignMCQ
A basic block is defined by having exactly E entry points and X exit points to ensure safe boundaries for optimization algorithms. What is the value of E+X?
A.
0
B.
1
C.
2
D.
3
Correct Answer:
C
Step-by-Step Solution
Key idea: A basic block is an atomic unit of code with a strict single-entry, single-exit invariant.
Step 1: Recall the definition of a basic block. It is a maximal sequence of instructions where control flow can only enter at the first instruction and leave at the last instruction.
Step 2: This means there is exactly E=1 entry point and exactly X=1 exit point.
Step 3: Calculate the sum: E+X=1+1=2.
Answer: 2
Question 2 · Compiler DesignMCQ
Consider the following three-address code:
1: i = 0
2: if i < 10 goto 4
3: goto 6
4: i = i + 1
5: goto 2
6: return i
Which of the following statements about the basic blocks is true?
A.
The basic block starting at instruction 2 contains instructions 2 and 3.
B.
Instruction 3 is a leader because it immediately follows a conditional jump.
C.
The total number of basic blocks in this code is 4.
D.
Instruction 5 is a leader because it is the target of a jump.
Correct Answer:
B
Step-by-Step Solution
Key idea: Apply the three leader rules systematically to identify all block boundaries.
Step 1: Rule 1: Instruction 1 is a leader (first instruction).
Step 2: Rule 2: Targets of jumps are leaders. Jump at 2 targets 4. Jump at 3 targets 6. Jump at 5 targets 2. Leaders added: 2, 4, 6.
Step 3: Rule 3: Instructions following jumps are leaders. Jump at 2 is conditional, so 3 is a leader. Jump at 3 is unconditional, so 4 is a leader (already counted). Jump at 5 is unconditional, so 6 is a leader (already counted).
Option A: Block starting at 2 is just {2}, because 3 is a leader. False.
Option B: Instruction 3 is a leader because it follows the conditional jump at 2. True.
Option C: Total blocks is 5, not 4. False.
Option D: Instruction 5 is not a target of any jump. False.
Answer: Instruction 3 is a leader because it immediately follows a conditional jump.
Question 3 · Compiler DesignMCQ
A basic block is defined by having exactly E entry points and X exit points to ensure safe boundaries for optimization algorithms. A student incorrectly assumes that a conditional branch at the end of a block creates two exit points, leading to the formula E+X=3. For a valid basic block, what is the actual value of E2+X2?
A.
1
B.
2
C.
3
D.
5
Correct Answer:
B
Step-by-Step Solution
Key idea: A basic block is an atomic unit of code with a strict single-entry, single-exit invariant, regardless of the type of branch at its end.
Step 1: Recall the definition of a basic block. It is a maximal sequence of instructions where control flow can only enter at the first instruction and leave at the last instruction.
Step 2: This means there is exactly E=1 entry point and exactly X=1 exit point. The fact that the last instruction might be a conditional branch does not create a second exit point; the branch instruction itself is the single exit point.
Step 3: Calculate the required value using the correct invariant: E2+X2=12+12=1+1=2.
Answer: 2
Question 4 · Compiler DesignMCQ
When constructing a Directed Acyclic Graph (DAG) for a basic block, under what strict condition can two identical expressions x = a + b and y = a + b share the same internal operator node?
A.
They appear in the same basic block, regardless of any intervening assignments.
B.
They appear in different basic blocks but have the exact same operands.
C.
They appear in the same basic block, and neither a nor b is redefined between the two statements.
D.
They are separated by at most one assignment statement that does not modify a or b.
Correct Answer:
C
Step-by-Step Solution
Key idea: DAG construction for local common subexpression elimination requires that operands remain unchanged between computations.
Step 1: Identify the goal. We want to reuse the node for a + b to avoid redundant computation.
Step 2: Check the scope. Local CSE only applies within a single basic block. This eliminates options involving different blocks.
Step 3: Check the condition for reuse. The values of a and b must be the same. If any statement between the two expressions reassigns a or b, the second expression uses new values and cannot reuse the old node.
Step 4: Therefore, the strict condition is that they are in the same block and neither operand is redefined in between.
Answer: C
Question 5 · Compiler DesignMCQ
Consider the following basic block:
t1 = a + b
t2 = c * d
t3 = a + b
t4 = c * d
If the compiler constructs a Directed Acyclic Graph (DAG) for this block to perform local common subexpression elimination, what is the total number of internal (operator) nodes created?
A.
2
B.
1
C.
3
D.
4
Correct Answer:
A
Step-by-Step Solution
Key idea: DAG construction reuses existing operator nodes if the exact same operation with the exact same operands is requested, provided neither operand has been redefined.
Step 1: Process t1 = a + b. Create leaf nodes for a and b, and an internal + node. (1 internal node).
Step 2: Process t2 = c d. Create leaf nodes for c and d, and an internal node. (2 internal nodes).
Step 3: Process t3 = a + b. The compiler checks for an existing + node with children a and b. It finds the one from t1. It attaches t3 to this existing node. No new internal node is created.
Step 4: Process t4 = c d. The compiler finds the existing node from t2 and attaches t4 to it. No new internal node is created.
Step 5: The total number of internal nodes is 2.
Answer: 2
Question 6 · Compiler DesignMCQ
Consider the following Assertion and Reason regarding global common subexpression elimination:
<b>Assertion (A):</b> If an expression is computed in block B1, and B1 has two successors B2 and B3, the expression is only available at the entry of a successor block B4 (where B2 and B3 merge) if it is available at the exit of both B2 and B3.
<b>Reason (R):</b> Available expressions analysis is a MAY analysis, which requires the union of the OUT sets of all predecessors.
A.
Both A and R are true, and R is the correct explanation of A.
B.
Both A and R are true, but R is NOT the correct explanation of A.
C.
A is true, but R is false.
D.
A is false, but R is true.
Correct Answer:
C
Step-by-Step Solution
Key idea: Available expressions is a MUST analysis, meaning an expression must be valid on all paths to be considered available.
Step 1: Evaluate Assertion (A). For an expression to be available at the entry of B4, it must have been computed and not killed on every single path leading to B4. Thus, it must be available at the exit of both B2 and B3. (A) is true.
Step 2: Evaluate Reason (R). Available expressions is a MUST analysis, not a MAY analysis. It uses the intersection (∩) operator, not the union (∪) operator. (R) is false.
Step 3: Since (A) is true and (R) is false, the correct option is C.
Answer: C
Question 7 · Compiler DesignMCQ
Consider the following basic block consisting of three assignment statements:
u = x + y;
v = x - y;
w = u * v;
When the compiler constructs a Directed Acyclic Graph (DAG) for this block, what is the total number of nodes (both leaf and internal operator nodes) created, assuming x and y are not redefined and u, v, w are not read subsequently?
A.
3
B.
5
C.
6
D.
8
Correct Answer:
B
Step-by-Step Solution
Key idea: A DAG consists of leaf nodes for initial values and internal nodes for operations. Left-hand side variables are just labels attached to nodes, not new nodes themselves.
Step 1: Identify leaf nodes. The initial variables read are x and y. This gives 2 leaf nodes.
Step 2: Identify internal nodes. The operations are +, -, and *. Since no operands are redefined and no operations are identical, each creates a distinct internal node. This gives 3 internal nodes.
Step 3: The variables u, v, and w are simply labels attached to the +, -, and * nodes respectively. They do not create new leaf nodes because they are not read subsequently.
Which of the following statements is universally TRUE regarding the data-flow equations for Available Expressions?
A.
IN(B)=⋃P∈pred(B)OUT(P)
B.
OUT(B)=Gen(B)∩(IN(B)∖Kill(B))
C.
Gen(B)⊆OUT(B) for any basic block B
D.
Kill(B)⊆IN(B) for any basic block B
Correct Answer:
C
Step-by-Step Solution
Key idea: Available Expressions is a forward, MUST analysis that uses intersection at join points and union for the transfer function.
Step 1: Evaluate Option A. Available Expressions uses intersection (∩), not union (∪), because an expression must be available on ALL paths. (False).
Step 2: Evaluate Option B. The transfer function uses union (∪) to combine generated expressions with surviving input expressions: OUT(B)=Gen(B)∪(IN(B)∖Kill(B)). (False).
Step 3: Evaluate Option C. By the definition of the transfer function OUT(B)=Gen(B)∪…, the set Gen(B) is always a subset of OUT(B). (True).
Step 4: Evaluate Option D. The Kill set depends only on the statements within the block, not on what flows in from predecessors. It is not necessarily a subset of IN(B). (False).
Answer: C
Question 9 · Compiler DesignMSQ
Which of the following properties are TRUE about a basic block in compiler design?
A.
A basic block has exactly one entry point
B.
A basic block has exactly one exit point
C.
Control can enter a basic block at any instruction within it
D.
Control can leave a basic block from any instruction within it
Correct Answer:
["A","B"]
Step-by-Step Solution
Key idea: This is a direct recall question about the defining invariant of a basic block. The key insight is the single-entry, single-exit property.
Step 1: Recall the definition of a basic block
A basic block is a maximal sequence of instructions with exactly one entry point and one exit point
This is the fundamental invariant that makes optimization safe
Step 2: Evaluate each option
Option A: "exactly one entry point" - TRUE, this is the single-entry invariant
Option B: "exactly one exit point" - TRUE, this is the single-exit invariant
Option C: "control can enter at any instruction" - FALSE, this violates single-entry
Option D: "control can leave from any instruction" - FALSE, this violates single-exit
Common trap: Students might think C or D are true because they confuse basic blocks with arbitrary code sequences. This is a unit mismatch error - not understanding that the atomic property is what makes basic blocks special.
Answer: A, B
Question 10 · Compiler DesignMCQ
In a three-address code sequence, there are exactly 4 conditional branches and 1 unconditional jump. Assuming no jump targets coincide with the instruction immediately following any jump, how many leaders are identified strictly by the rule "an instruction immediately follows a conditional or unconditional jump"?
A.
1
B.
4
C.
5
D.
6
Correct Answer:
C
Step-by-Step Solution
Key idea: The third leader rule states that any instruction immediately following a jump (conditional or unconditional) is a leader.
Step 1: Count the total number of jump statements in the code. There are 4 conditional branches and 1 unconditional jump, making a total of 5 jumps.
Step 2: Apply the rule. Each jump statement causes the instruction immediately following it to be marked as a leader.
Step 3: Since no targets coincide with these fall-through instructions, each of the 5 jumps produces exactly 1 unique leader by this specific rule.
Step 4: Total leaders identified by this rule = 5.