common sub expression elimination in Compiler Design
Common Subexpression Elimination(Data flow analyses) MCQs
1.
Common subexpression elimination (CSE) aims to:
A) Remove duplicate computations
B) Remove dead code
C) Inline functions
D) Allocate registers
✅ Answer: A) Remove duplicate computations
Solution:
CSE identifies expressions that are computed multiple times with the same operands and reuses the previously computed value.
2.
Which of the following is an example of a common subexpression?
t1 = a + b
t2 = a + b
A) a + b
B) t1
C) t2
D) b + a
✅ Answer: A) a + b
Solution:
The expression a + b appears multiple times; it can be computed once and reused.
3.
CSE is primarily a:
A) Forward data flow analysis
B) Backward data flow analysis
C) Constant propagation
D) Loop optimization
✅ Answer: A) Forward data flow analysis
Solution:
CSE propagates computed values forward to eliminate repeated evaluations.
4.
Which condition must hold to safely perform CSE?
A) Operands have not changed since last computation
B) Operands are constants
C) The expression is in a loop
D) Expression contains only global variables
✅ Answer: A) Operands have not changed since last computation
Solution:
Reusing a value is safe only if operands’ values remain unchanged.
5.
CSE can help reduce:
A) Runtime computations
B) Memory usage
C) Syntax tree size
D) Number of function calls
✅ Answer: A) Runtime computations
Solution:
By avoiding repeated evaluation, execution time is reduced.
6.
In the following code:
x = a + b
y = a + b
z = x + 5
What can be eliminated using CSE?
A) y = a + b
B) z = x + 5
C) Both
D) None
✅ Answer: A) y = a + b
Solution:a + b was already computed in x = a + b; y can reuse x.
7.
Which type of analysis is required to implement CSE in loops?
A) Available expressions
B) Liveness analysis
C) Constant propagation
D) Dead code analysis
✅ Answer: A) Available expressions
Solution:
CSE relies on available expressions analysis to know which expressions have been computed previously and are safe to reuse.
8.
An expression is available at a point if:
A) It has been computed along all paths and operands not modified
B) It has been computed along some paths
C) It is a constant
D) It is used once
✅ Answer: A) It has been computed along all paths and operands not modified
Solution:
To safely reuse a previous computation, it must be computed along all paths reaching that point without operand modification.
9.
Which of the following is not safe for CSE?
x = a + b
a = 5
y = a + b
A) Replacing y with x
B) Recomputing y
C) Assigning y = x after a=5
D) Both A and C
✅ Answer: D) Both A and C
Solution:a was modified; previous computation x is no longer valid.
10.
CSE reduces:
A) Number of redundant computations
B) Number of loops
C) Parsing time
D) Stack size
✅ Answer: A) Number of redundant computations
Solution:
Primary goal is runtime optimization by reusing previously computed expressions.
11.
In data flow analysis for CSE, the main set computed is:
A) Available expressions
B) Live variables
C) Constant expressions
D) Reaching definitions
✅ Answer: A) Available expressions
Solution:
Determines expressions that have been computed and can safely be reused.
12.
If a + b is available at a point, it means:
A) No assignments to a or b occurred after last computation
B) Only a is unchanged
C) Only b is unchanged
D) The expression is a constant
✅ Answer: A) No assignments to a or b occurred after last computation
Solution:
Operand modification invalidates previous computation.
13.
CSE is most effective when:
A) There are repeated computations
B) Variables are constants
C) Loops are small
D) No function calls
✅ Answer: A) There are repeated computations
Solution:
Redundant expressions are eliminated, giving runtime benefit.
14.
Which analysis is forward and uses intersection at predecessors?
A) Available expressions
B) Liveness
C) Reaching definitions
D) Constant propagation
✅ Answer: A) Available expressions
Solution:
Only expressions available on all paths from predecessors can be reused.
15.
If an expression is killed along one path, it is:
A) Not available at the merge point
B) Available
C) Constant
D) Dead
✅ Answer: A) Not available at the merge point
Solution:
All paths must preserve expression availability for safe reuse.
16.
Which optimization technique complements CSE?
A) Copy propagation
B) Loop unrolling
C) Dead code elimination
D) Inline expansion
✅ Answer: A) Copy propagation
Solution:
After CSE, copy propagation can replace redundant temporary variables with original expressions.
17.
In loops, CSE may require:
A) Hoisting invariant expressions outside loops
B) Recomputing expressions every iteration
C) Ignoring availability
D) Constant folding only
✅ Answer: A) Hoisting invariant expressions outside loops
Solution:
Loop-invariant code motion removes redundant computation within loops.
18.
Which of the following can kill an available expression?
A) Assignment to one of its operands
B) Assignment to unrelated variable
C) Use in print statement
D) Constant folding
✅ Answer: A) Assignment to one of its operands
Solution:
Operand change invalidates previously computed expression.
19.
CSE does not typically eliminate:
A) Expressions with side effects
B) Pure arithmetic expressions
C) Temporaries
D) Reused expressions
✅ Answer: A) Expressions with side effects
Solution:
Cannot safely eliminate calls or expressions that produce side effects.
20.
For the code:
x = a + b
y = a + b
z = y + c
After CSE, y = a + b becomes:
A) y = x
B) Recomputed a + b
C) Dead code
D) Constant
✅ Answer: A) y = x
Solution:
Previous computation stored in x is reused.
21.
Which of the following data flow sets are computed for CSE?
A) avail_in and avail_out for blocks
B) live_in and live_out
C) def and use sets only
D) Constant sets only
✅ Answer: A) avail_in and avail_out for blocks
Solution:
Forward propagation uses available expressions at block entry and exit.
22.
Intersection of avail_out of predecessors is used to compute:
A) avail_in of current block
B) avail_out
C) def set
D) Live variables
✅ Answer: A) avail_in of current block
Solution:
Expression must be available along all paths to be safe for reuse.
23.
CSE is most beneficial in:
A) Expression-intensive code
B) Minimal arithmetic code
C) Parsing phase
D) Code with no loops
✅ Answer: A) Expression-intensive code
Solution:
Redundant computation occurs often in expression-heavy code, giving optimization benefit.
24.
Which expressions cannot be eliminated by CSE?
A) Function calls with side effects
B) Arithmetic with only variables
C) Temporaries
D) Loop-invariant expressions
✅ Answer: A) Function calls with side effects
Solution:
Evaluation may affect program behavior; not safe to remove.
25.
Available expressions analysis is:
A) Forward, must analysis
B) Forward, may analysis
C) Backward, must analysis
D) Backward, may analysis
✅ Answer: A) Forward, must analysis
Solution:
Expression must be computed along all paths; information flows forward in CFG.
26.
In CSE, if x = a + b and later y = a + b * c, can we reuse x?
A) No
B) Yes
C) Only if c = 1
D) Only inside loops
✅ Answer: A) No
Solution:
Expressions must match exactly; a + b * c is not identical to a + b.
27.
An expression is killed if:
A) One of its operands is reassigned
B) The expression is used
C) Expression is loop-invariant
D) It appears multiple times
✅ Answer: A) One of its operands is reassigned
Solution:
Modification invalidates previous computation for reuse.
28.
Which data flow equation is used for available expressions?
A) avail_in[B] = ⋂ avail_out[P]
B) avail_out[B] = ⋃ avail_in[S]
C) IN[B] = USE[B] ∪ (OUT[B] – DEF[B])
D) OUT[B] = DEF[B] ∪ USE[B]
✅ Answer: A) avail_in[B] = ⋂ avail_out[P]
Solution:
Intersection ensures expression is available along all predecessor paths.
29.
CSE is more effective in:
A) Loops with repeated expressions
B) Single-statement functions
C) Programs without arithmetic
D) Programs with no variables
✅ Answer: A) Loops with repeated expressions
Solution:
Repeated evaluation inside loops provides optimization opportunities.
30.
Which of the following is true for CSE?
A) Forward must analysis
B) Backward may analysis
C) Backward must analysis
D) Forward may analysis
✅ Answer: A) Forward must analysis
Solution:
Expression must be available along all paths (must) and flows forward.
31.
Expression a + b in:
x = a + b
y = a + b
z = x + 1
After CSE, y is:
A) y = x
B) y = a + b
C) Dead code
D) y = z
✅ Answer: A) y = x
Solution:
Previous computation stored in x can be reused.
32.
Which expressions cannot be eliminated by CSE?
A) Expressions with side effects
B) a + b
C) x * y
D) t1 + t2
✅ Answer: A) Expressions with side effects
Solution:
Function calls or operations affecting program state cannot be safely removed.
33.
CSE may increase:
A) Temporary variables
B) Loop iterations
C) Parsing time
D) Memory leaks
✅ Answer: A) Temporary variables
Solution:
Extra temporaries may be introduced to store reused values.
34.
Which of the following statements is true?
A) CSE uses intersection at block entry
B) CSE uses union at block entry
C) CSE uses difference at block entry
D) CSE ignores block entry
✅ Answer: A) CSE uses intersection at block entry
Solution:
Expression must be computed along all paths to be safely available.
35.
CSE is particularly beneficial for:
A) Repeated arithmetic expressions
B) Single-use expressions
C) Variable declarations
D) Function prototypes
✅ Answer: A) Repeated arithmetic expressions
Solution:
Redundant computations provide optimization opportunities.
36.
In loops, CSE combined with loop-invariant code motion can:
A) Hoist repeated expressions outside loops
B) Reduce syntax tree size
C) Increase parsing time
D) Remove variables
✅ Answer: A) Hoist repeated expressions outside loops
Solution:
Expressions that don’t change in the loop can be computed once before the loop.
37.
Expression availability is killed by:
A) Operand reassignment
B) Expression use
C) Loop exit
D) Function return
✅ Answer: A) Operand reassignment
Solution:
Change of operand value invalidates previous computation.
38.
Which analysis is used to compute available expressions?
A) Forward data flow analysis
B) Backward data flow analysis
C) Liveness analysis
D) Constant propagation
✅ Answer: A) Forward data flow analysis
Solution:
Available expressions flow forward along CFG.
39.
At CFG merge point, expressions are available if:
A) Available along all paths
B) Available along any path
C) Constant
D) Used once
✅ Answer: A) Available along all paths
Solution:
Intersection ensures safety for reuse.
40.
CSE is unsafe for:
A) Expressions with side effects like function calls
B) Pure arithmetic expressions
C) Temporaries
D) Loop-invariant expressions
✅ Answer: A) Expressions with side effects like function calls
Solution:
Removing or reusing side-effect expressions can alter program behavior.
41.
Which optimization is usually applied after CSE?
A) Copy propagation
B) Dead code elimination
C) Loop unrolling
D) Constant folding
✅ Answer: A) Copy propagation
Solution:
Replaces temporary variables introduced by CSE with original expressions.
42.
In CSE, avail_out[B] = ?
A) avail_gen[B] ∪ (avail_in[B] – avail_kill[B])
B) avail_in[B] ∪ DEF[B]
C) USE[B] ∪ DEF[B]
D) avail_in[B] ∩ avail_kill[B]
✅ Answer: A) avail_gen[B] ∪ (avail_in[B] – avail_kill[B])
Solution:
Standard data flow equation for available expressions.
43.
Which of the following expressions can be eliminated?
t1 = x * y
t2 = x * y
A) t2
B) t1
C) x * y in t1
D) None
✅ Answer: A) t2
Solution:
t2 is a repeated computation; can reuse t1.
44.
CSE is more effective in:
A) Expression-heavy code
B) Minimal arithmetic code
C) Parser optimization
D) Function calls only
✅ Answer: A) Expression-heavy code
Solution:
Redundant computations are abundant in such code.
45.
Which of the following kills availability?
A) Assignment to operand
B) Expression reuse
C) Constant folding
D) Loop-invariant code motion
✅ Answer: A) Assignment to operand
Solution:
Operand change invalidates previous expression.
46.
Expression availability analysis is:
A) Forward must analysis
B) Backward must analysis
C) Forward may analysis
D) Backward may analysis
✅ Answer: A) Forward must analysis
Solution:
Expression must be computed along all paths to be safely available.
47.
Which of the following is NOT eliminated by CSE?
A) Expressions with side effects
B) a + b repeated
C) x * y repeated
D) Loop-invariant expressions
✅ Answer: A) Expressions with side effects
Solution:
Removing side-effect expressions can change program behavior.
48.
CSE can be safely applied if:
A) Operands unchanged since last computation
B) Expression used only once
C) Expression in a dead block
D) Operand is constant
✅ Answer: A) Operands unchanged since last computation
Solution:
Safety condition for reusing previously computed expression.
49.
Which set is computed for each basic block in CSE?
A) avail_in and avail_out
B) live_in and live_out
C) use and def only
D) Constants
✅ Answer: A) avail_in and avail_out
Solution:
Used to track available expressions at block entry and exit.
50.
Which operation is used at CFG merge for CSE?
A) Intersection
B) Union
C) Difference
D) Symmetric difference
✅ Answer: A) Intersection
Solution:
Expression must be available along all paths to be safely reused.
51.
CSE is particularly useful in:
A) Loops with repeated expressions
B) Single-statement blocks
C) Function prototypes
D) Empty loops
✅ Answer: A) Loops with repeated expressions
Solution:
Redundant computations in loops are high; elimination improves runtime.
52.
Expression is available if:
A) Computed along all paths, operands not modified
B) Computed along some paths
C) Operands constant only
D) Used once
✅ Answer: A) Computed along all paths, operands not modified
Solution:
Requirement for safe reuse.
53.
Which of the following may require additional temporaries in CSE?
A) Replacing expression with previously computed value
B) Using expression inline
C) Loop unrolling
D) Dead code elimination
✅ Answer: A) Replacing expression with previously computed value
Solution:
New temporaries store reused expressions.
54.
CSE is unsafe in:
A) Expressions with side effects
B) Arithmetic-only expressions
C) Repeated temporaries
D) Loop-invariant expressions
✅ Answer: A) Expressions with side effects
Solution:
Evaluation cannot be removed safely.
55.
Which type of data flow analysis is used for CSE?
A) Forward, must analysis
B) Backward, may analysis
C) Backward, must analysis
D) Forward, may analysis
✅ Answer: A) Forward, must analysis
Solution:
Expression must be available along all paths; information flows forward.
56.
CSE can optimize code by:
A) Reducing repeated computation
B) Increasing memory usage
C) Eliminating loops
D) Modifying function signatures
✅ Answer: A) Reducing repeated computation
Solution:
Primary benefit is runtime performance improvement.
57.
Expression a + b can be reused if:
A) a and b not modified since last computation
B) Only a unchanged
C) Only b unchanged
D) Expression used only once
✅ Answer: A) a and b not modified since last computation
Solution:
Operands must remain unchanged for safe reuse.
58.
Which analysis is needed before applying CSE?
A) Available expressions analysis
B) Liveness analysis
C) Constant propagation
D) Dead code elimination
✅ Answer: A) Available expressions analysis
Solution:
Determines which expressions are safe to reuse.
59.
At CFG merge point, expression is available if:
A) Available along all paths
B) Available along some paths
C) Constant
D) Loop-invariant
✅ Answer: A) Available along all paths
Solution:
Intersection ensures safe reuse.
60.
CSE can introduce:
A) Temporaries
B) Loops
C) Parsing errors
D) Stack overflow
✅ Answer: A) Temporaries
Solution:
Intermediate variables store previously computed expressions.
61.
Which of the following expressions can be safely eliminated by CSE?
t1 = x + y
t2 = x + y
A) t2
B) t1
C) x + y in t1
D) None
✅ Answer: A) t2
Solution:t2 is a repeated computation; reuse t1 instead.
62.
CSE may fail if:
A) One of the operands changes between occurrences
B) Expression is a pure arithmetic operation
C) Expression is repeated exactly
D) Expression uses temporaries
✅ Answer: A) One of the operands changes between occurrences
Solution:
Previous computation is invalid if operands are modified.
63.
Which operation is used to combine available expressions from multiple predecessors?
A) Intersection
B) Union
C) Difference
D) Symmetric difference
✅ Answer: A) Intersection
Solution:
Expression must be available along all paths to be safely reused.
64.
CSE is unsafe in the presence of:
A) Function calls with side effects
B) Arithmetic expressions
C) Temporaries
D) Loop-invariant expressions
✅ Answer: A) Function calls with side effects
Solution:
Removing such expressions can change program behavior.
65.
CSE is often combined with:
A) Copy propagation
B) Loop unrolling
C) Dead code elimination
D) Inline expansion
✅ Answer: A) Copy propagation
Solution:
Copy propagation replaces temporary variables introduced by CSE.
66.
Available expressions analysis is:
A) Forward must analysis
B) Backward may analysis
C) Backward must analysis
D) Forward may analysis
✅ Answer: A) Forward must analysis
Solution:
Expressions must be available along all paths; analysis flows forward.
67.
CSE can reduce:
A) Number of redundant computations
B) Number of variables
C) Stack usage
D) Function calls
✅ Answer: A) Number of redundant computations
Solution:
Primary goal is runtime optimization.
68.
An expression is killed if:
A) One of its operands is reassigned
B) Expression is used
C) Expression is constant
D) Expression is loop-invariant
✅ Answer: A) One of its operands is reassigned
Solution:
Modification invalidates previous computation.
69.
Which of the following is true about CSE in loops?
A) Loop-invariant expressions can be hoisted
B) All expressions must be recomputed each iteration
C) CSE is not applicable in loops
D) Temporaries are never needed
✅ Answer: A) Loop-invariant expressions can be hoisted
Solution:
Hoisting moves repeated computation outside the loop for efficiency.
70.
CSE cannot eliminate:
A) Expressions with side effects
B) Arithmetic-only expressions
C) Repeated temporaries
D) Loop-invariant expressions
✅ Answer: A) Expressions with side effects
Solution:
Evaluation of such expressions cannot be skipped safely.
71.
Which set is used to track available expressions at block entry?
A) avail_in
B) avail_out
C) use
D) def
✅ Answer: A) avail_in
Solution:
Represents expressions available at the start of a block.
72.avail_out[B] is computed as:
A) avail_gen[B] ∪ (avail_in[B] – avail_kill[B])
B) avail_in[B] ∪ DEF[B]
C) USE[B] ∪ DEF[B]
D) avail_in[B] ∩ avail_kill[B]
✅ Answer: A) avail_gen[B] ∪ (avail_in[B] – avail_kill[B])
Solution:
Standard forward data flow equation for available expressions.
73.
Which type of analysis is CSE?
A) Forward must
B) Forward may
C) Backward must
D) Backward may
✅ Answer: A) Forward must
Solution:
Expression must be available along all paths for safe reuse.
74.
CSE is most effective in:
A) Expression-heavy code
B) Minimal arithmetic code
C) Function prototypes
D) Empty loops
✅ Answer: A) Expression-heavy code
Solution:
Redundant computations are frequent in such code.
75.
Which of the following kills an available expression?
A) Assignment to one of its operands
B) Reusing the expression
C) Constant folding
D) Loop-invariant code motion
✅ Answer: A) Assignment to one of its operands
Solution:
Operand change invalidates previous computation.
76.
CSE may introduce:
A) Temporary variables
B) Loops
C) Parsing errors
D) Stack overflow
✅ Answer: A) Temporary variables
Solution:
Extra temporaries store previously computed expressions for reuse.
77.
Which type of data flow analysis is used for CSE?
A) Forward, must
B) Backward, may
C) Forward, may
D) Backward, must
✅ Answer: A) Forward, must
Solution:
Expression must be available along all paths; information flows forward.
78.
Intersection at CFG merge ensures:
A) Expression available along all paths
B) Expression available along some paths
C) Expression constant
D) Expression dead
✅ Answer: A) Expression available along all paths
Solution:
Safety requirement for reuse.
79.
CSE is unsafe for:
A) Function calls with side effects
B) Pure arithmetic
C) Temporaries
D) Loop-invariant expressions
✅ Answer: A) Function calls with side effects
Solution:
Removing side-effect expressions changes program behavior.
80.
CSE is usually applied:
A) After parsing, during intermediate code optimization
B) During lexical analysis
C) During syntax tree construction
D) During code generation
✅ Answer: A) After parsing, during intermediate code optimization
Solution:
CSE operates on intermediate representation to optimize runtime.
81.
Expression a + b can be reused if:
A) Both operands unchanged since last computation
B) Only a unchanged
C) Only b unchanged
D) Expression used only once
✅ Answer: A) Both operands unchanged since last computation
Solution:
Operands must remain the same for safe reuse.
82.
Which of the following is NOT affected by CSE?
A) Expressions with side effects
B) Arithmetic expressions
C) Temporaries
D) Loop-invariant expressions
✅ Answer: A) Expressions with side effects
Solution:
Cannot remove or reuse safely.
83.
CSE is most effective when:
A) Repeated computations occur
B) Variables are constants
C) Code has no arithmetic
D) Loops absent
✅ Answer: A) Repeated computations occur
Solution:
Redundancy provides optimization opportunities.
84.
Which optimization complements CSE?
A) Copy propagation
B) Loop unrolling
C) Dead code elimination
D) Inline expansion
✅ Answer: A) Copy propagation
Solution:
Eliminates unnecessary temporary variables after CSE.
85.
An expression is killed by:
A) Operand modification
B) Expression reuse
C) Constant folding
D) Loop-invariant code motion
✅ Answer: A) Operand modification
Solution:
Operands’ values must remain unchanged for safe reuse.
86.
CSE is forward must analysis because:
A) Expression must be available along all paths
B) Expression must be available along some paths
C) Expression flows backward
D) Expression is always constant
✅ Answer: A) Expression must be available along all paths
Solution:
Safety condition ensures correct reuse.
87.
Which of the following may increase due to CSE?
A) Temporaries
B) Loops
C) Parsing time
D) Stack usage
✅ Answer: A) Temporaries
Solution:
Extra variables store reused computations.
88.
CSE combined with loop-invariant code motion can:
A) Hoist repeated expressions outside loops
B) Increase runtime
C) Break code semantics
D) Replace function calls
✅ Answer: A) Hoist repeated expressions outside loops
Solution:
Improves runtime efficiency.
89.
Expression availability sets for blocks are:
A) avail_in and avail_out
B) live_in and live_out
C) use and def
D) Constant sets
✅ Answer: A) avail_in and avail_out
Solution:
Used to track expression availability across CFG.
90.
CSE is unsafe if:
A) Operands modified
B) Expression repeated
C) Expression uses temporaries
D) Expression in loop
✅ Answer: A) Operands modified
Solution:
Invalidates previous computation.
91.
Which operator is used at CFG merge for CSE?
A) Intersection
B) Union
C) Difference
D) Symmetric difference
✅ Answer: A) Intersection
Solution:
Expression must be available along all paths.
92.
Loop-invariant expressions can be:
A) Hoisted outside loop
B) Recomputed each iteration
C) Ignored
D) Eliminated only if constant
✅ Answer: A) Hoisted outside loop
Solution:
Removes redundant computations from inside loop.
93.
CSE is ineffective if:
A) Expressions are computed only once
B) Expressions repeated
C) Loops exist
D) Temporaries used
✅ Answer: A) Expressions are computed only once
Solution:
No redundancy → no optimization possible.
94.
Which analysis is prerequisite for CSE?
A) Available expressions analysis
B) Liveness analysis
C) Constant propagation
D) Dead code elimination
✅ Answer: A) Available expressions analysis
Solution:
Determines which expressions are safe to reuse.
95.
CSE improves:
A) Runtime performance
B) Parsing speed
C) Function inlining
D) Stack usage
✅ Answer: A) Runtime performance
Solution:
Avoids redundant computation.
96.
Expression is available at CFG merge if:
A) Available along all paths
B) Available along some paths
C) Operand constant
D) Expression unused
✅ Answer: A) Available along all paths
Solution:
Intersection ensures safe reuse.
97.
Which expressions cannot be removed by CSE?
A) Expressions with side effects
B) Repeated arithmetic expressions
C) Temporaries
D) Loop-invariant expressions
✅ Answer: A) Expressions with side effects
Solution:
Cannot safely remove function calls or assignments with side effects.
98.
CSE may introduce:
A) Temporaries
B) Loops
C) Parsing errors
D) Stack overflow
✅ Answer: A) Temporaries
Solution:
Intermediate variables store reused computations.
99.
Which type of expressions is CSE safe
for?
A) Pure arithmetic expressions
B) Function calls with side effects
C) Expressions modifying global state
D) I/O expressions
✅ Answer: A) Pure arithmetic expressions
Solution:
No side effects → safe to remove/reuse.
100.
CSE combined with copy propagation:
A) Reduces temporaries
B) Removes loops
C) Increases syntax tree size
D) Introduces dead code
✅ Answer: A) Reduces temporaries
Solution:
Copy propagation replaces temporary variables with original expressions, optimizing the code.