chapter

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

    Solve 9+ Basic Blocks, Data-flow Analysis and Code Optimization previous year questions for GATE CS with answers and detailed solutions. Free sample questions

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the control flow graph given below.
    ENTRY B1 a = b + c B2 d = a + e B3 e = a + f B4 g = d + e EXIT Which one of the following options is the set of live variables at the exit point of each basic block?
    Question 2
    2026 Slot Set1 PYQ
    Level 3: Exam Standard
    Consider the control flow graph shown in the figure.

    ENTRYa = b + iB1a = g * kf = d - fa = c * 4B2t = g * kb = c + mB3x = g * ky = b + iB4z = c + mB5EXIT

    Which one of the following options correctly lists the set of redundant expressions (common subexpressions) in the basic blocks B4 and B5?

    Note: All the variables are integers.
    Question 3
    2025 Slot Set1 PYQ
    Level 3: Exam Standard

    Which ONE of the following techniques used in compiler code optimization uses live variable analysis?

    Question 4
    2025 Slot Set1 PYQ
    Level 3: Exam Standard
    Refer to the given 3-address code sequence. This code sequence is split into basic blocks. The number of basic blocks is ________. (Answer in integer)

    1001: i = 1
    1002: j = 1
    1003: t1 = 10*i
    1004: t2 = t1+j
    1005: t3 = 8*t2
    1006: t4 = t3-88
    1007: a[t4] = 0.0
    1008: j = j+1
    1009: if j <= 10 goto 1003
    1010: i = i+1
    1011: if i <= 10 goto 1002
    1012: i = 1
    1013: t5 = i-1
    1014: t6 = 88*t5
    1015: a[t6] = 1.0
    1016: i = i+1
    1017: if i <= 10 goto 1013
    Question 5
    2024 Slot Set1 PYQ
    Level 3: Exam Standard
    Consider the following pseudo-code.

















    Which one of the following options CORRECTLY specifies the number of basic blocks and the number of instructions in the largest basic block, respectively ?
    Question 6
    2023 PYQ
    Level 3: Exam Standard
    Consider the control flow graph shown.

    ENTRYB1i = m − 1j = na = 10B2i = i + 1j = j − 1B3a = 20B4i = a + 1EXIT

    Which one of the following choices correctly lists the set of live variables at the exit point of each basic block?
    Question 7
    2021 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following ANSI C code segment:

    z = x + 3 + y->f1 + y->f2;
    for (i = 0; i < 200; i = i + 2){
        if (z > i) {
            p = p + x + 3;
            q = q + y->f1;
        } else {
            p = p + y->f2;
            q = q + x + 3;
        }
    }

    Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2, and the local variables x, y, z, p, q, and i are allotted registers. Common sub-expression elimination (CSE) optimization is applied on the code. The number of addition and dereference operations (of the form y->f1 or y->f2) in the optimized code, respectively, are:
    Question 8
    2021 Slot Set2 PYQ
    Level 3: Exam Standard
    For a statement in a program, in the context of liveness analysis, the following sets are defined:

        :   the set of variables used in
           :   the set of variables that are live at the entry of
        :   the set of variables that are live at the exit of

    Consider a basic block that consists of two statements, followed by . Which one of the following statements is correct?
    Question 9
    2021 Slot Set1 PYQ
    Level 3: Exam Standard
    Consider the following C code segment:

    a = b + c;
    e = a + 1;
    d = b + c;
    f = d + 1;
    g = e + f;

    In a compiler, this code segment is represented internally as a directed acyclic graph (DAG). The number of nodes in the DAG is __________.
    Free preview ends here

    Login to view the complete previous-year questions and solutions

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

    Solve 9+ Basic Blocks, Data-flow Analysis and Code Optimization previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.

    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.

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

    Question 1 · Compiler Design · 2026_Set2 MCQ
    Consider the control flow graph given below.
    ENTRY B1 a = b + c B2 d = a + e B3 e = a + f B4 g = d + e EXIT Which one of the following options is the set of live variables at the exit point of each basic block?
    1. A.

      B1:{a, b, c, e, f}, B2:{d, e}, B3:{b, c, e, f}, B4:

    2. B.

      B1:, B2:{d, e}, B3:{a, c, f}, B4:

    3. C.

      B1:{a, b, c, e, f}, B2:{d, e}, B3:{c, e, f}, B4:

    4. D.

      B1:, B2:{d, e, f}, B3:{a, b, c, e, f}, B4:

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: Global liveness analysis in the presence of loops requires synthesizing backward data-flow equations with fixed-point iteration to resolve back-edge dependencies.

    Exam route: Compute IN/OUT for each block backwards. OUT(B4) = \emptyset implies IN(B4) = \{d, e\}. Backtrack to B2, B3, and B1, testing the given options for consistency.

    Learning route:

    1. This is a global data-flow question, recognizable by the CFG with a back edge and a request for live variables at block exits.
    2. The trigger is the loop (back edge B3 to B1), which requires solving a system of backward equations to a fixed point.
    3. Step 1: EXIT: OUT(B4) = \emptyset.
    4. Step 2: B4: g = d + e. USE=\{d, e\}, DEF=\{g\}. IN(B4) = (OUT(B4) - \{g\}) \cup \{d, e\} = \{d, e\}.
    5. Step 3: B2: d = a + e. USE=\{a, e\}, DEF=\{d\}. Successor is B4. OUT(B2) = IN(B4) = \{d, e\}. IN(B2) = (OUT(B2) - \{d\}) \cup \{a, e\} = \{e\} \cup \{a, e\} = \{a, e\}.
    6. Step 4: B3: e = a + f. USE=\{a, f\}, DEF=\{e\}. Successor is B1 (back edge). OUT(B3) = IN(B1). IN(B3) = (OUT(B3) - \{e\}) \cup \{a, f\}.
    7. Step 5: B1: a = b + c. USE=\{b, c\}, DEF=\{a\}. Successors are B2 and B3. OUT(B1) = IN(B2) \cup IN(B3) = \{a, e\} \cup IN(B3).
    8. Step 6: Solve fixed point. Assume IN(B1) initially \emptyset. Then OUT(B3) = \emptyset, IN(B3) = \{a, f\}. OUT(B1) = \{a, e\} \cup \{a, f\} = \{a, e, f\}. IN(B1) = (OUT(B1) - \{a\}) \cup \{b, c\} = \{e, f\} \cup \{b, c\} = \{b, c, e, f\}.
    9. Step 7: Iterate again with IN(B1) = \{b, c, e, f\}. OUT(B3) = \{b, c, e, f\}. IN(B3) = (\{b, c, e, f\} - \{e\}) \cup \{a, f\} = \{a, b, c, f\}. OUT(B1) = \{a, e\} \cup \{a, b, c, f\} = \{a, b, c, e, f\}. IN(B1) = (\{a, b, c, e, f\} - \{a\}) \cup \{b, c\} = \{b, c, e, f\}. Fixed point reached.
    10. Step 8: Final OUT sets: OUT(B1) = \{a, b, c, e, f\}, OUT(B2) = \{d, e\}, OUT(B3) = \{b, c, e, f\}, OUT(B4) = \emptyset. This matches Option A. My verified answer matches the current answer.
    Question 2 · Compiler Design · 2026_Set1 MCQ
    Consider the control flow graph shown in the figure.

    ENTRYa = b + iB1a = g * kf = d - fa = c * 4B2t = g * kb = c + mB3x = g * ky = b + iB4z = c + mB5EXIT

    Which one of the following options correctly lists the set of redundant expressions (common subexpressions) in the basic blocks B4 and B5?

    Note: All the variables are integers.
    1. A. B4:
      B5:
    2. B. B4:
      B5:
    3. C. B4:
      B5:
    4. D. B4:
      B5:
    Correct Answer:

    D

    Step-by-Step Solution

    Insight: Available expressions analysis synthesizes control-flow path tracing with data-flow kill/gen sets to identify globally redundant computations for CSE.

    Exam route: Trace available expressions from ENTRY to B4 and B5. Check if the expression is computed on every path and not killed by any redefinition.

    Learning route:

    1. This is an available expressions question, recognizable by "redundant expressions" and "common subexpressions" in a CFG.
    2. The trigger is the CFG with multiple paths merging at B4. Availability requires the expression to be computed on ALL paths to the merge point without being killed.
    3. Step 1: Analyze paths to B4. There are two paths: ENTRY B1 B2 B4 and ENTRY B1 B3 B4.
    4. Step 2: Check g k in B4. It is computed in B2 (a = g k) and in B3 (t = g k). Neither g nor k is redefined before B4. Thus, g k is available on all paths to B4. It is redundant in B4.
    5. Step 3: Check b + i in B4. It is computed in B1. However, on the path through B3, b is redefined (b = c + m). Thus, b + i is killed on this path and is NOT available at B4.
    6. Step 4: Analyze paths to B5. The only path to B5 is through B4. For an expression to be available at B5, it must be available at the exit of B4.
    7. Step 5: Check c + m in B5. It is computed in B3. However, on the path B1 B2 B4 B5, c + m is never computed. Since it is not available on ALL paths to B4 (and thus B5), it is not available at B5.
    8. Conclusion: Redundant in B4 is , and in B5 is . My verified answer matches the current answer.
    Question 3 · Compiler Design · 2025_Set1 MCQ

    Which ONE of the following techniques used in compiler code optimization uses live variable analysis?

    1. A.

      Run-time function call management

    2. B.

      Register assignment to variables

    3. C.

      Strength reduction

    4. D.

      Constant folding

    Correct Answer:

    B

    Step-by-Step Solution

    Insight: Live variable analysis identifies variables whose values may be needed in the future, which is the exact information required to safely reuse registers.

    Exam route: Match the optimization technique to its primary data-flow dependency. Register allocation directly queries liveness to decide when a register can be overwritten.

    Learning route:

    1. This is a conceptual mapping question, recognizable by the request to link a specific data-flow analysis (live variable analysis) to its primary compiler optimization application.
    2. Step 1: Recall the purpose of live variable analysis. It determines if a variable's current value will be read before it is overwritten.
    3. Step 2: Evaluate the options. Run-time function call management relies on calling conventions, not liveness. Strength reduction replaces expensive operations with cheaper ones (e.g., multiplication with addition). Constant folding evaluates constant expressions at compile time.
    4. Step 3: Register assignment (allocation) requires knowing when a variable is "dead" so its register can be reassigned to another variable without overwriting a value that is still needed.
    5. Verification: Liveness analysis is the foundational data-flow problem for graph-coloring register allocators. My verified answer matches Option B.
    Question 4 · Compiler Design · 2025_Set1 NAT
    Refer to the given 3-address code sequence. This code sequence is split into basic blocks. The number of basic blocks is ________. (Answer in integer)

    1001: i = 1
    1002: j = 1
    1003: t1 = 10*i
    1004: t2 = t1+j
    1005: t3 = 8*t2
    1006: t4 = t3-88
    1007: a[t4] = 0.0
    1008: j = j+1
    1009: if j <= 10 goto 1003
    1010: i = i+1
    1011: if i <= 10 goto 1002
    1012: i = 1
    1013: t5 = i-1
    1014: t6 = 88*t5
    1015: a[t6] = 1.0
    1016: i = i+1
    1017: if i <= 10 goto 1013
    Correct Answer:

    6.00

    Step-by-Step Solution

    Insight: Leaders are strictly defined by three rules: the first instruction, any jump target, and the instruction immediately following a jump.

    Exam route: Scan the 3-address code, mark leaders using the 3 rules, then count the blocks formed between consecutive leaders.

    Learning route:

    1. Rule 1 (First instruction): 1001 is a leader.
    2. Rule 2 (Jump targets):
    • 1009 jumps to 1003 1003 is a leader.
    • 1011 jumps to 1002 1002 is a leader.
    • 1017 jumps to 1013 1013 is a leader.
    1. Rule 3 (After a jump):
    • 1009 is a conditional jump, so the next instruction, 1010, is a leader.
    • 1011 is a conditional jump, so the next instruction, 1012, is a leader.
    • 1017 is the last instruction, so nothing follows it.
    1. Complete list of leaders: 1001, 1002, 1003, 1010, 1012, 1013.
    2. Total leaders = 6. Therefore, there are 6 basic blocks.
    Question 5 · Compiler Design · 2024_Set1 MCQ
    Consider the following pseudo-code.

















    Which one of the following options CORRECTLY specifies the number of basic blocks and the number of instructions in the largest basic block, respectively ?
    1. A.

      6 and 6

    2. B.

      6 and 7

    3. C.

      7 and 7

    4. D.

      7 and 6

    Correct Answer:

    D

    Step-by-Step Solution

    Insight: Basic block partitioning relies strictly on the three leader rules: first instruction, jump targets, and instructions immediately following jumps.

    Exam route: List all leaders first using the three rules, then group instructions between leaders to form blocks, and finally count the blocks and find the maximum size.

    Learning route:

    1. This is a basic block partitioning question, recognizable by the request to count blocks and find the largest block size from three-address code.
    2. Step 1: Identify all leaders using the three rules.
    • Rule 1 (First): L1 is a leader.
    • Rule 2 (Jump targets): L9 targets L11 (L11 is a leader). L12 targets L4 (L4 is a leader). L14 targets L3 (L3 is a leader).
    • Rule 3 (Follows a jump): L9 is a conditional jump, so L10 is a leader. L12 is a conditional jump, so L13 is a leader. L14 is a conditional jump, so L15 is a leader.
    1. Step 2: The complete set of leaders is {L1, L3, L4, L10, L11, L13, L15}. Total = 7 leaders, meaning 7 basic blocks.
    2. Step 3: Group instructions into blocks:
    • B1: L1, L2 (size 2)
    • B2: L3 (size 1)
    • B3: L4, L5, L6, L7, L8, L9 (size 6)
    • B4: L10 (size 1)
    • B5: L11, L12 (size 2)
    • B6: L13, L14 (size 2)
    • B7: L15 (size 1)
    1. Step 4: The largest basic block is B3 with 6 instructions.
    2. Verification: Every block has exactly one entry (the leader) and one exit (the last instruction or a jump). The partition is valid. My verified answer matches Option D.
    Question 6 · Compiler Design · 2023 MCQ
    Consider the control flow graph shown.

    ENTRYB1i = m − 1j = na = 10B2i = i + 1j = j − 1B3a = 20B4i = a + 1EXIT

    Which one of the following choices correctly lists the set of live variables at the exit point of each basic block?
    1. A.

      B1: , B2: , B3: , B4:

    2. B.

      B1: , B2: , B3: , B4:

    3. C.

      B1: , B2: , B3: , B4:

    4. D.

      B1: , B2: , B3: , B4:

    Correct Answer:

    D

    Step-by-Step Solution

    Insight: Global liveness analysis in cyclic CFGs requires backward fixed-point iteration, propagating USE/DEF sets from EXIT through all paths, including back-edges.

    Exam route: Initialize IN sets to empty, iterate backward using and , until sets stabilize.

    Learning route:

    1. This is a global data-flow question, recognizable by a CFG with a cycle and a request for live variables at block exits.
    2. Step 1: Identify USE and DEF for each block (focusing on variables \{a, i, j\}):
    • B1: USE=\{\}, DEF=\{a, i, j\}
    • B2: USE=\{i, j\}, DEF=\{i, j\}
    • B3: USE=\{\}, DEF=\{a\}
    • B4: USE=\{a\}, DEF=\{i\}
    1. Step 2: Identify successors: B1B2; B2B3, B4; B3B4; B4B2, EXIT.
    2. Step 3: Iterate backward from EXIT (IN(EXIT)=):
    • Iter 1: IN(B4)=\{a\}, IN(B3)=, OUT(B2)=\{a\}, IN(B2)=\{a, i, j\}, IN(B1)=.
    • Iter 2: IN(B4)=\{a\} (\{a, i, j\} - \{i\}) = \{a, j\}. IN(B3)=\{a, j\} - \{a\} = \{j\}. OUT(B2)=\{j\} \{a, j\} = \{a, j\}. IN(B2)=\{i, j\} (\{a, j\} - \{i, j\}) = \{a, i, j\}.
    • Iter 3: Sets stabilize. IN(B1)=, IN(B2)=\{a, i, j\}, IN(B3)=\{j\}, IN(B4)=\{a, j\}.
    1. Step 4: Compute OUT sets:
    • OUT(B1) = IN(B2) = \{a, i, j\}
    • OUT(B2) = IN(B3) IN(B4) = \{j\} \{a, j\} = \{a, j\}
    • OUT(B3) = IN(B4) = \{a, j\}
    • OUT(B4) = IN(B2) IN(EXIT) = \{a, i, j\} = \{a, i, j\}
    1. Verification: The computed OUT sets perfectly match Option D. My verified answer matches Option D.
    Question 7 · Compiler Design · 2021_Set2 MCQ
    Consider the following ANSI C code segment:

    z = x + 3 + y->f1 + y->f2;
    for (i = 0; i < 200; i = i + 2){
        if (z > i) {
            p = p + x + 3;
            q = q + y->f1;
        } else {
            p = p + y->f2;
            q = q + x + 3;
        }
    }

    Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2, and the local variables x, y, z, p, q, and i are allotted registers. Common sub-expression elimination (CSE) optimization is applied on the code. The number of addition and dereference operations (of the form y->f1 or y->f2) in the optimized code, respectively, are:
    1. A.

      403 and 102

    2. B.

      203 and 2

    3. C.

      303 and 102

    4. D.

      303 and 2

    Correct Answer:

    D

    Step-by-Step Solution

    Insight: Optimizing loop performance requires synthesizing Common Subexpression Elimination (CSE) with Loop-Invariant Code Motion, treating pointer dereferences as hoistable expressions when aliasing is absent.

    Exam route: Count operations before the loop. Then, count operations per iteration assuming CSE caches x+3, y->f1, and y->f2. Multiply by 100 iterations.

    Learning route:

    1. This is a CSE optimization question, recognizable by "Common sub-expression elimination" and counting operations in optimized loops.
    2. The trigger is the repeated use of x + 3 and pointer dereferences inside a loop with branches, which CSE will hoist and cache.
    3. Step 1: Initial code: z = x + 3 + y->f1 + y->f2. This requires computing t1 = x + 3 (1 add), t2 = y->f1 (1 deref), t3 = y->f2 (1 deref), and combining them (2 adds). Total initial: 3 additions, 2 dereferences.
    4. Step 2: Loop runs for i = 0, 2, ..., 198, which is exactly 100 iterations.
    5. Step 3: Inside the loop, per iteration:
    • Loop counter: i = i + 2 (1 addition).
    • The expressions x + 3, y->f1, and y->f2 are loop-invariant and common. CSE evaluates them once (cached as t1, t2, t3), so they cost 0 additions/dereferences inside the loop.
    • If branch: p = p + t1 (1 add), q = q + t2 (1 add). Total: 2 additions, 0 derefs.
    • Else branch: p = p + t3 (1 add), q = q + t1 (1 add). Total: 2 additions, 0 derefs.
    1. Step 4: Total additions inside loop = 100 \times (1 + 2) = 300.
    2. Step 5: Total additions overall = 3 (initial) + 300 (loop) = 303. Total dereferences = 2 (initial) + 0 (loop) = 2.
    3. Verification: The optimized code hoists all invariant expressions, leaving only the loop counter increment and the two additions per branch, perfectly matching 303 additions and 2 dereferences. My verified answer matches the current answer.
    Question 8 · Compiler Design · 2021_Set2 MCQ
    For a statement in a program, in the context of liveness analysis, the following sets are defined:

        :   the set of variables used in
           :   the set of variables that are live at the entry of
        :   the set of variables that are live at the exit of

    Consider a basic block that consists of two statements, followed by . Which one of the following statements is correct?
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: Liveness analysis equations require strict adherence to backward data-flow propagation across basic block boundaries.

    Exam route: By definition of sequential control flow without branching, the liveness OUT set of S1 must identically match the IN set of the immediately following statement S2.

    Learning route:

    1. This is a data-flow equation question, recognizable by the definitions of IN, OUT, and USE sets for sequential statements.
    2. The trigger is "basic block that consists of two statements, S1 followed by S2", which implies strict sequential control flow with no branches between them.
    3. Step 1: Liveness analysis is a backward data-flow problem. Variables live at the exit of a statement are those that will be used before being redefined.
    4. Step 2: For S1 followed immediately by S2, any variable live at the exit of S1 is precisely a variable that is live at the entry of S2.
    5. Step 3: Therefore, the fundamental boundary equation for sequential statements is OUT(S1) = IN(S2).

    Verification: If a variable is live at the exit of S1, it must be needed by S2 or something after S2. Thus, it is live at the entry of S2. The sets are identical. My verified answer matches the current answer.

    Question 9 · Compiler Design · 2021_Set1 NAT
    Consider the following C code segment:

    a = b + c;
    e = a + 1;
    d = b + c;
    f = d + 1;
    g = e + f;

    In a compiler, this code segment is represented internally as a directed acyclic graph (DAG). The number of nodes in the DAG is __________.
    Correct Answer:

    6.00

    Step-by-Step Solution

    Insight: DAG construction maps unique computations and operands to exactly one node, reusing nodes for identical subexpressions to eliminate redundancy.

    Exam route: Identify unique leaves first, then build internal operation nodes bottom-up, reusing nodes for identical computations to get the final count.

    Learning route:

    1. This is a DAG construction question, recognizable by the request to count nodes in the internal representation of a code segment.
    2. Step 1: Identify all unique leaves. The unique operands are b, c, and 1. This gives 3 leaf nodes.
    3. Step 2: Process a = b + c. This creates a new + node with children b and c. (Node 4, total 4)
    4. Step 3: Process e = a + 1. This creates a new + node with children Node 4 and 1. (Node 5, total 5)
    5. Step 4: Process d = b + c. Since b + c was already computed as Node 4, we reuse it. (0 new nodes)
    6. Step 5: Process f = d + 1. Since d points to Node 4, d + 1 is identical to a + 1 (Node 5). We reuse Node 5. (0 new nodes)
    7. Step 6: Process g = e + f. Both e and f point to Node 5. This creates a new + node with children Node 5 and Node 5. (Node 6, total 6)
    8. Total nodes = 3 (leaves) + 3 (internal operations) = 6.

    Verification: Plugging the nodes back, we have leaves {b, c, 1}, ops {+(b,c), +(+(b,c),1), +(node5,node5)}, totaling exactly 6 unique nodes. My verified answer matches the current answer.

    More previous year questions (pyqs) in this unit