chapter

    Basic Blocks, Data-flow Analysis and Code Optimization Notes for Gate Cs

    Basic Blocks, Data-flow Analysis and Code Optimization notes for GATE CS: 45 study cards covering concepts, formulas, shortcuts and exam traps, plus solved pr

    Basic Blocks Data Flow Analysis and Code Optimization notes

    Why basic blocks are the atomic unit of optimization

    Why Basic Blocks Are the Atomic Unit of Optimization

    A maximal sequence with exactly one entry and one exit.

    Optimization algorithms cannot safely rewrite instructions if control flow can enter or leave the sequence at arbitrary points. A basic block is a maximal (largest possible) sequence of instructions with exactly one entry point and one exit point. Consider a sequence where instruction 4 adds two variables and instruction 5 stores the result. If no jump can land between 4 and 5, and no jump can leave between 4 and 5, the compiler can treat them as a single unit. This atomic property is what allows subsequent analyses, like liveness or available expressions, to operate on blocks rather than individual instructions.

    Explain this more simply

    Imagine a train carriage. Passengers enter only at the front door and leave only at the back door. You can rearrange the seats inside without worrying about someone stepping on or off at a window. A basic block is exactly this carriage for instructions.

    Go one level deeper

    The single-entry, single-exit invariant reduces the complexity of data-flow equations from exponential in the number of instructions to linear in the number of blocks. Recognizing this invariant is the first step in seeing why compiler optimizations are tractable.

    The three leader rules, stated precisely

    The Three Rules

    1. First. It is the very first instruction in the program.
    2. Second. It is the target of a conditional or unconditional jump.
    3. Third. It immediately follows a conditional or unconditional jump.

    An instruction is a leader if it satisfies any of three conditions. Consider a program where instruction 10 is a jump to instruction 20. Instruction 20 is a leader by the second rule. Instruction 11 is a leader by the third rule. Instruction 1 is a leader by the first rule. These three rules are necessary and sufficient to partition the code.

    Common Trap. Students often count only jump targets as leaders and forget that the instruction immediately after a conditional branch is itself a leader. This feels right because the focus is on where control goes, not where it falls through to. This slip costs the entire question.
    Check. For every conditional branch, explicitly mark the next instruction as a leader before counting.
    Explain this more simply

    Think of leaders as the starting lines of a race. The first runner starts at the beginning of the track. Any runner starting where a previous runner was teleported to is also a starting line. Any runner starting immediately after a runner who just teleported away is also a starting line. Every instruction belongs to the block of the most recent leader.

    Go one level deeper

    The third rule exists because a conditional branch has two successors, the target and the fall-through. If the fall-through instruction were not a leader, it would be merged into the branch's block, violating the single-exit invariant. The rules mechanically enforce the structural definition of a basic block.

    Single entry, single exit: the defining invariant of a basic block

    A basic block is defined by the single-entry, single-exit invariant. Single entry means no instruction inside the block can be the target of a jump from outside the block, except for the first instruction. Single exit means control cannot leave the block from any instruction except the last one. Consider a block containing instructions 4, 5, and 6. If instruction 7 jumps to instruction 5, the single-entry property is broken, and the block must be split at 5. If instruction 5 contains a jump to instruction 10, the single-exit property is broken, and the block must end at 5.

    Explain this more simply

    Picture a hallway with doors. Single entry means there is only one door into the hallway. Single exit means there is only one door out. If someone can enter through a window in the middle, or leave through a side door, the hallway is not a basic block. The walls must be solid except for the designated ends.

    Go one level deeper

    This invariant is what makes the block a node in the control-flow graph. Without it, the node would have internal branching, requiring the graph to model intra-block control flow. The partition algorithm simply enforces this invariant by cutting the code at every leader.

    42 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 entry points and exit points to ensure safe boundaries for optimization algorithms. What is the value of ?

    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 entry points and 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 . For a valid basic block, what is the actual value of ?

    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 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.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    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 Notes for GATE CS

    Basic Blocks, Data-flow Analysis and Code Optimization notes for GATE CS: 45 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Why basic blocks are the atomic unit of optimization

    Why Basic Blocks Are the Atomic Unit of Optimization

    A maximal sequence with exactly one entry and one exit.

    Optimization algorithms cannot safely rewrite instructions if control flow can enter or leave the sequence at arbitrary points. A basic block is a maximal (largest possible) sequence of instructions with exactly one entry point and one exit point. Consider a sequence where instruction 4 adds two variables and instruction 5 stores the result. If no jump can land between 4 and 5, and no jump can leave between 4 and 5, the compiler can treat them as a single unit. This atomic property is what allows subsequent analyses, like liveness or available expressions, to operate on blocks rather than individual instructions.

    Explain this more simply

    Imagine a train carriage. Passengers enter only at the front door and leave only at the back door. You can rearrange the seats inside without worrying about someone stepping on or off at a window. A basic block is exactly this carriage for instructions.

    Go one level deeper

    The single-entry, single-exit invariant reduces the complexity of data-flow equations from exponential in the number of instructions to linear in the number of blocks. Recognizing this invariant is the first step in seeing why compiler optimizations are tractable.

    The three leader rules, stated precisely

    The Three Rules

    1. First. It is the very first instruction in the program.
    2. Second. It is the target of a conditional or unconditional jump.
    3. Third. It immediately follows a conditional or unconditional jump.

    An instruction is a leader if it satisfies any of three conditions. Consider a program where instruction 10 is a jump to instruction 20. Instruction 20 is a leader by the second rule. Instruction 11 is a leader by the third rule. Instruction 1 is a leader by the first rule. These three rules are necessary and sufficient to partition the code.

    Common Trap. Students often count only jump targets as leaders and forget that the instruction immediately after a conditional branch is itself a leader. This feels right because the focus is on where control goes, not where it falls through to. This slip costs the entire question.
    Check. For every conditional branch, explicitly mark the next instruction as a leader before counting.
    Explain this more simply

    Think of leaders as the starting lines of a race. The first runner starts at the beginning of the track. Any runner starting where a previous runner was teleported to is also a starting line. Any runner starting immediately after a runner who just teleported away is also a starting line. Every instruction belongs to the block of the most recent leader.

    Go one level deeper

    The third rule exists because a conditional branch has two successors, the target and the fall-through. If the fall-through instruction were not a leader, it would be merged into the branch's block, violating the single-exit invariant. The rules mechanically enforce the structural definition of a basic block.

    Single entry, single exit: the defining invariant of a basic block

    A basic block is defined by the single-entry, single-exit invariant. Single entry means no instruction inside the block can be the target of a jump from outside the block, except for the first instruction. Single exit means control cannot leave the block from any instruction except the last one. Consider a block containing instructions 4, 5, and 6. If instruction 7 jumps to instruction 5, the single-entry property is broken, and the block must be split at 5. If instruction 5 contains a jump to instruction 10, the single-exit property is broken, and the block must end at 5.

    Explain this more simply

    Picture a hallway with doors. Single entry means there is only one door into the hallway. Single exit means there is only one door out. If someone can enter through a window in the middle, or leave through a side door, the hallway is not a basic block. The walls must be solid except for the designated ends.

    Go one level deeper

    This invariant is what makes the block a node in the control-flow graph. Without it, the node would have internal branching, requiring the graph to model intra-block control flow. The partition algorithm simply enforces this invariant by cutting the code at every leader.

    Algorithm: from three-address code to a list of basic blocks

    Phase 1 — Identify Leaders

    Apply the three rules to mark every instruction that can be entered from outside its immediate predecessor.

    Phase 2 — Group Instructions

    A block starts with a leader and includes all subsequent instructions up to, but not including, the next leader or the end of the program.

    Consider leaders at 1, 4, and 8. The first block contains 1, 2, and 3. The second block contains 4, 5, 6, and 7. The third block contains 8 and beyond. This procedure is deterministic and always yields the correct partition.

    Trap. Students sometimes 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.
    Explain this more simply

    Take a piece of text and underline every word that starts a new paragraph. The first word of the text is underlined. Any word after a period is underlined. Now, group the words. The first paragraph runs from the first underlined word to the word just before the second underlined word. The second paragraph runs from the second underlined word to the third, and so on. Leaders are the underlined words; blocks are the paragraphs.

    Go one level deeper

    The algorithm runs in linear time, requiring only a single pass to mark leaders and a second pass to emit blocks. The correctness relies on the fact that leaders are exactly the instructions that can be reached from outside their immediate predecessor, which is the formal definition of a block boundary.

    Basic Blocks, Data-flow Analysis and Code Optimization: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Compiler Design MCQ

    A basic block is defined by having exactly entry points and exit points to ensure safe boundaries for optimization algorithms. What is the value of ?

    1. A.

      0

    2. B.

      1

    3. C.

      2

    4. 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 entry point and exactly exit point.

    Step 3: Calculate the sum: .

    Answer: 2

    Question 2 · Compiler Design MCQ

    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?

    1. A.

      The basic block starting at instruction 2 contains instructions 2 and 3.

    2. B.

      Instruction 3 is a leader because it immediately follows a conditional jump.

    3. C.

      The total number of basic blocks in this code is 4.

    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).

    Step 4: Complete leader set: 1, 2, 3, 4, 6. Total 5 blocks.

    Step 5: Evaluate options.

    • 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 Design MCQ

    A basic block is defined by having exactly entry points and 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 . For a valid basic block, what is the actual value of ?

    1. A.

      1

    2. B.

      2

    3. C.

      3

    4. 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 entry point and exactly 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: .

    Answer: 2

    Question 4 · Compiler Design MCQ

    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?

    1. A.

      They appear in the same basic block, regardless of any intervening assignments.

    2. B.

      They appear in different basic blocks but have the exact same operands.

    3. C.

      They appear in the same basic block, and neither a nor b is redefined between the two statements.

    4. 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 Design MCQ

    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?

    1. A.

      2

    2. B.

      1

    3. C.

      3

    4. 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 Design MCQ

    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.

    1. A.

      Both A and R are true, and R is the correct explanation of A.

    2. B.

      Both A and R are true, but R is NOT the correct explanation of A.

    3. C.

      A is true, but R is false.

    4. 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 Design MCQ

    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?

    1. A.

      3

    2. B.

      5

    3. C.

      6

    4. 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.

    Step 4: Total nodes = 2 (leaves) + 3 (internals) = 5.

    Answer: 5

    Question 8 · Compiler Design MCQ

    Which of the following statements is universally TRUE regarding the data-flow equations for Available Expressions?

    1. A.

    2. B.

    3. C.

      for any basic block

    4. D.

      for any basic block

    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: . (False).

    Step 3: Evaluate Option C. By the definition of the transfer function , the set is always a subset of . (True).

    Step 4: Evaluate Option D. The set depends only on the statements within the block, not on what flows in from predecessors. It is not necessarily a subset of . (False).

    Answer: C

    Question 9 · Compiler Design MSQ

    Which of the following properties are TRUE about a basic block in compiler design?

    1. A.

      A basic block has exactly one entry point

    2. B.

      A basic block has exactly one exit point

    3. C.

      Control can enter a basic block at any instruction within it

    4. 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 Design MCQ

    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"?

    1. A.

      1

    2. B.

      4

    3. C.

      5

    4. 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.

    Answer: 5

    More notes in this unit