Local Optimization
Local Optimization – Compiler Design (MCQs 1–100 with Solutions)
1.
Local optimization works on which part of the program?
A) Whole program
B) Function level
C) Basic block level
D) Module level
✅ Answer: C) Basic block level
Solution:
Local optimization is applied within basic blocks—code segments with single entry and exit points.
2.
Which of the following is an example of a local optimization?
A) Constant folding
B) Loop unrolling
C) Code motion
D) Function inlining
✅ Answer: A) Constant folding
Solution:
Constant folding evaluates constant expressions at compile time—within a single block.
3.
In local optimization, redundant instructions are removed from:
A) Data section
B) Symbol table
C) Basic block
D) Entire program
✅ Answer: C) Basic block
Solution:
Redundant computations within the same block can be safely removed.
4.
If x = 5 * 8; y = x + 2; appears in a block, the compiler can replace 5 * 8 with 40. This is an example of:
A) Constant folding
B) Strength reduction
C) Dead code elimination
D) Copy propagation
✅ Answer: A) Constant folding
Solution:
The constant expression 5 * 8 is computed at compile time and replaced with 40.
5.
In local optimization, dead code elimination removes:
A) Code that affects output
B) Unreachable code
C) Code that does not affect program output
D) Initialization code
✅ Answer: C) Code that does not affect program output
Solution:
If a computed value is never used, that computation is removed.
6.
Which of the following transformations is not a local optimization?
A) Algebraic simplification
B) Loop-invariant code motion
C) Constant folding
D) Common subexpression elimination
✅ Answer: B) Loop-invariant code motion
Solution:
This is a global optimization because it spans multiple basic blocks.
7.
Algebraic simplification converts expressions like x * 1 into:
A) x * 0
B) x
C) x + 1
D) 1
✅ Answer: B) x
Solution:
Multiplying by 1 has no effect; compiler simplifies it to x.
8.
Which local optimization improves both execution time and memory use?
A) Constant folding
B) Dead code elimination
C) Strength reduction
D) Copy propagation
✅ Answer: B) Dead code elimination
Solution:
It removes unnecessary computations and data storage.
9.
Expression t1 = a + b; t2 = a + b; can be optimized to a single computation using:
A) Strength reduction
B) Common subexpression elimination
C) Copy propagation
D) Peephole optimization
✅ Answer: B) Common subexpression elimination
Solution:
Identifies identical subexpressions within the same block and removes redundancy.
10.
Replacing x = x * 2 with x = x + x is an example of:
A) Constant folding
B) Dead code elimination
C) Strength reduction
D) Copy propagation
✅ Answer: C) Strength reduction
Solution:
A costly multiplication is replaced with a cheaper addition.
11.
Which of the following optimizations can be done using local information only?
A) Loop optimization
B) Dead code elimination
C) Register allocation
D) Code scheduling
✅ Answer: B) Dead code elimination
Solution:
It uses data flow information within a block to remove unnecessary computations.
12.
“Peephole optimization” refers to:
A) Local optimization over small instruction sequences
B) Global optimization across loops
C) Register allocation
D) Linking optimization
✅ Answer: A) Local optimization over small instruction sequences
Solution:
Peephole optimization analyzes a “small window” of machine instructions.
13.
If a variable is assigned twice before being used, the first assignment can be:
A) Propagated
B) Eliminated
C) Reordered
D) Recomputed
✅ Answer: B) Eliminated
Solution:
The first assignment’s result is overwritten before use—hence dead code.
14.a = b; c = a + 5; can be optimized as:
A) a = b; c = b + 5;
B) a = b + 5;
C) c = a;
D) a = c;
✅ Answer: A) a = b; c = b + 5;
Solution:
Copy propagation replaces a with b since both hold same value.
15.
Which local optimization aims to replace expensive operations with cheaper ones?
A) Constant folding
B) Strength reduction
C) Dead code elimination
D) Peephole optimization
✅ Answer: B) Strength reduction
Solution:
It replaces expensive operations (like multiply/divide) with simpler arithmetic.
16.
In x = y + 0, compiler can simplify it to:
A) x = y
B) x = 0
C) x = y * 0
D) No simplification
✅ Answer: A) x = y
Solution:
Adding zero doesn’t change the value, so the operation is redundant.
17.
Which is an example of algebraic simplification?
A) Removing dead statements
B) Changing x * 0 to 0
C) Removing unused variables
D) Combining loops
✅ Answer: B) Changing x * 0 to 0
Solution:
Arithmetic identities simplify expressions.
18.
In peephole optimization, instructions are analyzed:
A) Across modules
B) Across functions
C) In small windows
D) Across loops
✅ Answer: C) In small windows
Solution:
Peephole optimization examines few consecutive instructions for simplification.
19.
Which of these cannot be done locally?
A) Constant propagation
B) Copy propagation
C) Loop unrolling
D) Constant folding
✅ Answer: C) Loop unrolling
Solution:
Loop unrolling involves multiple blocks—global optimization.
20.
If instruction x = 10; x = 20; y = x + 5; appears, then removing the first x = 10 is:
A) Strength reduction
B) Dead code elimination
C) Code motion
D) Copy propagation
✅ Answer: B) Dead code elimination
Solution:
The first assignment’s result is overwritten before use.
21.
Which optimization checks for common subexpressions?
A) Constant folding
B) Algebraic simplification
C) Common subexpression elimination
D) Peephole optimization
✅ Answer: C) Common subexpression elimination
Solution:
Detects and eliminates recomputation of identical expressions.
22.
What does local value numbering detect?
A) Loop invariants
B) Common subexpressions
C) Dead variables
D) Copy propagation
✅ Answer: B) Common subexpressions
Solution:
Local value numbering maps expressions to numbers to identify redundancy.
23.
Replacing instruction pair load x; load x; with one load is:
A) Common subexpression elimination
B) Peephole optimization
C) Constant folding
D) Strength reduction
✅ Answer: B) Peephole optimization
Solution:
Redundant instruction removal at machine level.
24.MOV R1, R2; MOV R2, R1; can be eliminated using:
A) Strength reduction
B) Copy propagation
C) Peephole optimization
D) Dead code elimination
✅ Answer: C) Peephole optimization
Solution:
The two instructions cancel each other—detected by peephole techniques.
25.
In t1 = a + b; t2 = t1 + 0;, what can be eliminated?
A) Both statements
B) Second statement simplified
C) First statement removed
D) No optimization
✅ Answer: B) Second statement simplified
Solution:t2 = t1 + 0 → t2 = t1.
26.
Local optimization removes redundancies within:
A) Single function
B) Basic block
C) Whole loop
D) Program file
✅ Answer: B) Basic block
Solution:
Local optimization scope is limited to single-entry, single-exit blocks.
27.
If a = b + c; d = b + c; occurs in a block, we can replace second computation using:
A) Strength reduction
B) Common subexpression elimination
C) Peephole optimization
D) Copy propagation
✅ Answer: B) Common subexpression elimination
Solution:
Avoids recalculating identical expressions.
28.
The optimization replacing (x << 1) instead of (x * 2) is:
A) Copy propagation
B) Strength reduction
C) Constant folding
D) Peephole optimization
✅ Answer: B) Strength reduction
Solution:
Bit shift replaces multiplication by 2—cheaper operation.
29.
In peephole optimization, which instruction pattern can be replaced by “no operation”?
A) INC X; DEC X
B) MOV X, Y; ADD Y, X
C) MOV A, A
D) Both A and C
✅ Answer: D) Both A and C
Solution:
Both patterns are redundant—no effect on program state.
30.
Dead code elimination requires which analysis?
A) Control flow
B) Data flow
C) Syntax
D) Type checking
✅ Answer: B) Data flow
Solution:
Identifies variables whose values are never used.
31.
Which local optimization improves instruction scheduling?
A) Peephole optimization
B) Strength reduction
C) Constant propagation
D) Code motion
✅ Answer: A) Peephole optimization
Solution:
Small instruction rearrangements can improve efficiency.
32.
If both operands are constants, computation is performed at:
A) Runtime
B) Compile time
C) Link time
D) Load time
✅ Answer: B) Compile time
Solution:
This is constant folding—reduces runtime cost.
33.
For statement x = y + 0, removing + 0 is:
A) Dead code elimination
B) Strength reduction
C) Algebraic simplification
D) Constant propagation
✅ Answer: C) Algebraic simplification
Solution:
Applies mathematical identity y + 0 = y.
34.
Which optimization removes unnecessary temporaries?
A) Copy propagation
B) Constant folding
C) Dead code elimination
D) Loop unrolling
✅ Answer: A) Copy propagation
Solution:
Replaces variable references with original expressions.
35.
“Local” in local optimization refers to:
A) Function scope
B) Basic block
C) Program scope
D) File scope
✅ Answer: B) Basic block
Solution:
Each optimization applies within one straight-line block.
36.
Which of the following may increase code size but improve performance locally?
A) Strength reduction
B) Code duplication
C) Constant folding
D) Peephole optimization
✅ Answer: D) Peephole optimization
Solution:
Small rearrangements can increase size but improve pipeline efficiency.
37.
Detecting MOV R1,R1 and deleting it is:
A) Dead code elimination
B) Peephole optimization
C) Strength reduction
D) Algebraic simplification
✅ Answer: B) Peephole optimization
Solution:
Redundant self-assignments have no effect.
38.
Which optimization is most frequently done by pattern matching?
A) Peephole optimization
B) Constant folding
C) Strength reduction
D) Copy propagation
✅ Answer: A) Peephole optimization
Solution:
Uses pattern templates to identify replaceable instruction sequences.
39.
Replacing a = b + c; d = a; with d = b + c; is:
A) Copy propagation
B) Constant folding
C) Common subexpression elimination
D) Strength reduction
✅ Answer: A) Copy propagation
Solution:
Propagates value of a directly to use site.
40.
Which optimization removes statements whose results are never used?
A) Common subexpression elimination
B) Dead code elimination
C) Peephole optimization
D) Constant folding
✅ Answer: B) Dead code elimination
Solution:
Eliminates computations not contributing to final output.
41.MOV R1, 5; ADD R1, 0; → can be simplified to:
A) MOV R1, 0
B) MOV R1, 5
C) ADD R1, 5
D) NOP
✅ Answer: B) MOV R1, 5
Solution:
Adding zero changes nothing; redundant instruction removed.
42.
Local optimization is performed after:
A) Parsing
B) Code generation
C) Intermediate code generation
D) Lexical analysis
✅ Answer: C) Intermediate code generation
Solution:
Optimizations use intermediate representation before final code emission.
43.
Which local optimization uses algebraic identities?
A) Strength reduction
B) Algebraic simplification
C) Copy propagation
D) Loop unrolling
✅ Answer: B) Algebraic simplification
Solution:
Simplifies expressions like x * 1 = x.
44.
Peephole optimization primarily targets:
A) Machine code
B) Source code
C) Intermediate code
D) Symbol table
✅ Answer: A) Machine code
Solution:
Operates on short sequences of generated assembly instructions.
45.
Which optimization reduces number of arithmetic operations within block?
A) Common subexpression elimination
B) Constant propagation
C) Dead code elimination
D) Code motion
✅ Answer: A) Common subexpression elimination
Solution:
Removes repeated computations.
46.
Which local optimization ensures no redundant calculations in same block?
A) Copy propagation
B) Dead code elimination
C) Common subexpression elimination
D) Strength reduction
✅ Answer: C) Common subexpression elimination
Solution:
Reuses previously computed identical expressions.
47.
The simplest form of local optimization is:
A) Peephole optimization
B) Strength reduction
C) Constant folding
D) Loop optimization
✅ Answer: C) Constant folding
Solution:
Evaluates constants at compile time—most basic transformation.
48.
The main goal of local optimization is to:
A) Reduce memory size
B) Improve data structure design
C) Reduce instruction count
D) Simplify parsing
✅ Answer: C) Reduce instruction count
Solution:
Removes redundancies within a block to make execution faster.
49.
When applied repeatedly, local optimization must preserve:
A) Symbol names
B) Semantic equivalence
C) Syntax structure
D) Source order
✅ Answer: B) Semantic equivalence
Solution:
Optimized code must behave identically to original.
50.
Replacing (x * 4) by (x << 2) is an example of:
A) Strength reduction
B) Peephole optimization
C) Copy propagation
D) Constant folding
✅ Answer: A) Strength reduction
Solution:
Shift operation replaces costly multiplication.
Local Optimization — Compiler Design
51.
Which of the following is a peephole optimization?
A) Removing redundant MOV instructions
B) Loop-invariant code motion
C) Function inlining
D) Constant propagation across functions
✅ Answer: A) Removing redundant MOV instructions
Solution:
Peephole optimization identifies local redundant assembly instructions like consecutive MOVs.
52.
The purpose of local optimization is to:
A) Improve portability
B) Improve code within a basic block
C) Reduce linking overhead
D) Modify object code
✅ Answer: B) Improve code within a basic block
Solution:
Local optimizations restrict transformations to linear code sequences inside one block.
53.
In the code t1 = a + b; t2 = a + b;, local optimization will:
A) Leave it as is
B) Replace t2 with t1
C) Replace t1 with t2
D) Delete both
✅ Answer: B) Replace t2 with t1
Solution:
The second identical computation can reuse the previous result using common subexpression elimination.
54.
Copy propagation is typically performed:
A) Before constant folding
B) After code generation
C) After dead code elimination
D) Alongside algebraic simplification
✅ Answer: D) Alongside algebraic simplification
Solution:
These local optimizations complement each other and are usually done together in a basic block.
55.
Which of the following would be removed by dead code elimination?
A) x = 10; y = x + 5; x = 20;
B) x = y + 10; print(x);
C) x = 1; if (x) print(x);
D) x = 2; y = x + 3;
✅ Answer: A) x = 10; y = x + 5; x = 20;
Solution:
The assignment x = 10 is dead because its result is overwritten before use.
56.
Algebraic simplification can change x = x * 0 to:
A) x = 1
B) x = 0
C) x = x
D) None
✅ Answer: B) x = 0
Solution:
Multiplying any number by 0 yields 0, hence simplification applies.
57.
In t1 = a * 2; t2 = t1 * 2;, strength reduction could convert this to:
A) Shifts by 1
B) Two additions
C) A single multiplication by 4
D) A loop invariant
✅ Answer: C) A single multiplication by 4
Solution:
Combining operations: a * 2 * 2 = a * 4.
58.
Which local optimization helps to minimize repeated memory fetches?
A) Common subexpression elimination
B) Constant folding
C) Copy propagation
D) Peephole optimization
✅ Answer: A) Common subexpression elimination
Solution:
By storing previously computed expressions, redundant memory access is reduced.
59.
The primary target of local optimization is:
A) Execution time
B) Compile time
C) Storage layout
D) Syntax tree construction
✅ Answer: A) Execution time
Solution:
Local optimizations improve runtime performance by simplifying computation sequences.
60.
Which optimization might replace x = y * 8 with x = y << 3?
A) Constant propagation
B) Peephole optimization
C) Strength reduction
D) Common subexpression elimination
✅ Answer: C) Strength reduction
Solution:
Multiplication by powers of 2 can be replaced by left shifts for faster execution.
61.
After applying copy propagation, some statements may become:
A) Redundant
B) Constants
C) Dead
D) Dynamic
✅ Answer: C) Dead
Solution:
Copy propagation often exposes dead assignments, which can then be removed.
62.
Peephole optimization can remove:
A) Jump to jump sequences
B) Loops
C) Functions
D) Blocks
✅ Answer: A) Jump to jump sequences
Solution:
Eliminates unnecessary branch chains like JMP L1; L1: JMP L2.
63.
Which of the following is true about local optimization?
A) Always guarantees optimal global code
B) May miss cross-block redundancies
C) Requires loop analysis
D) Needs inlining first
✅ Answer: B) May miss cross-block redundancies
Solution:
Local optimization doesn’t analyze control flow between blocks.
64.
Which optimization detects a*b + a*b in the same block?
A) Constant folding
B) Common subexpression elimination
C) Dead code elimination
D) Peephole optimization
✅ Answer: B) Common subexpression elimination
Solution:
Detects repeated computations and reuses the stored result.
65.
Which of the following optimizations deals with assembly instruction-level changes?
A) Peephole optimization
B) Constant folding
C) Loop unrolling
D) Common subexpression elimination
✅ Answer: A) Peephole optimization
Solution:
It operates on the machine code level for local improvement.
66.
Dead code elimination improves:
A) Execution time and code size
B) Compilation speed
C) Stack usage only
D) None of these
✅ Answer: A) Execution time and code size
Solution:
By removing unnecessary code, the program becomes smaller and faster.
67.
A code segment where control always enters at the beginning and leaves at the end is called:
A) Function
B) Basic block
C) Loop body
D) Region
✅ Answer: B) Basic block
Solution:
Basic blocks are the core units of local optimization.
68.
Algebraic simplification can optimize x = (y * 2) / 2 to:
A) x = y
B) x = 2 * y
C) x = y * 4
D) None
✅ Answer: A) x = y
Solution:
Multiplying and dividing by 2 cancel out.
69.
Copy propagation helps improve which optimization’s effectiveness?
A) Dead code elimination
B) Loop unrolling
C) Code motion
D) Register allocation
✅ Answer: A) Dead code elimination
Solution:
After copy propagation, dead assignments become easier to detect.
70.
In the code a = b; b = c; c = a;, which variable copy becomes useless?
A) a = b
B) b = c
C) c = a
D) All of these
✅ Answer: A) a = b
Solution:a is overwritten indirectly in the next steps, making the first assignment dead.
71.
Constant folding reduces:
A) Number of computations at runtime
B) Number of loops
C) Number of functions
D) Size of symbol table
✅ Answer: A) Number of computations at runtime
Solution:
Pre-computes constants during compilation.
72.
Which is the simplest and fastest local optimization?
A) Peephole optimization
B) Constant folding
C) Common subexpression elimination
D) Dead code elimination
✅ Answer: B) Constant folding
Solution:
It’s easy and requires no data flow analysis.
73.
In peephole optimization, instruction reordering is allowed only if:
A) Semantics don’t change
B) Register count increases
C) New labels are introduced
D) Code size doubles
✅ Answer: A) Semantics don’t change
Solution:
Semantic preservation is mandatory for all optimizations.
74.
The optimization ADD R1, 0 → NOP is:
A) Constant folding
B) Strength reduction
C) Peephole optimization
D) Common subexpression elimination
✅ Answer: C) Peephole optimization
Solution:
Redundant arithmetic instructions are removed at the assembly level.
75.
Which of the following requires identifying expression values across lines?
A) Common subexpression elimination
B) Peephole optimization
C) Strength reduction
D) Algebraic simplification
✅ Answer: A) Common subexpression elimination
Solution:
Requires tracking computed expressions to reuse previous results.
76.x = x + 0 can be replaced by x = x using:
A) Constant propagation
B) Algebraic simplification
C) Code motion
D) Strength reduction
✅ Answer: B) Algebraic simplification
Solution:
Adds identity simplification to reduce redundancy.
77.
Which of these is a machine-independent local optimization?
A) Common subexpression elimination
B) Peephole optimization
C) Instruction combination
D) Register reassignment
✅ Answer: A) Common subexpression elimination
Solution:
Operates on intermediate code, not machine-specific instructions.
78.
Constant propagation is useful when:
A) Constant value is known at compile-time
B) Constant changes at runtime
C) Code has loops
D) Function calls are recursive
✅ Answer: A) Constant value is known at compile-time
Solution:
Substitutes known constants directly into expressions.
79.
Local optimization cannot eliminate redundancy:
A) Across basic blocks
B) Within a single block
C) In loops
D) In sequential statements
✅ Answer: A) Across basic blocks
Solution:
Global optimization is required for inter-block analysis.
80.
Which optimization is least likely to affect control flow?
A) Dead code elimination
B) Constant propagation
C) Peephole optimization
D) Algebraic simplification
✅ Answer: D) Algebraic simplification
Solution:
It affects only expressions, not control structures.
81.
Peephole optimization improves:
A) Register allocation
B) Instruction sequence efficiency
C) Data structure layout
D) Parsing
✅ Answer: B) Instruction sequence efficiency
Solution:
It reduces redundant or slow instruction patterns.
82.
Which optimization aims to improve instruction scheduling locally?
A) Peephole optimization
B) Loop optimization
C) Inlining
D) Dead code elimination
✅ Answer: A) Peephole optimization
Solution:
Optimizes small instruction windows for better execution.
83.
Local value numbering identifies:
A) Equivalent expressions
B) Constant literals
C) Register aliases
D) Function boundaries
✅ Answer: A) Equivalent expressions
Solution:
Assigns unique identifiers to identical computations to detect redundancy.
84.
Which optimization simplifies address computations like a[i*4]?
A) Strength reduction
B) Peephole optimization
C) Constant folding
D) Copy propagation
✅ Answer: A) Strength reduction
Solution:
Converts multiplication to efficient bit-shift operations.
85.
Dead code can occur due to:
A) Unused assignments
B) Constant propagation
C) Copy propagation
D) All of these
✅ Answer: D) All of these
Solution:
These transformations may reveal redundant code that produces no visible output.
86.
Which optimization ensures reuse of previously computed values?
A) Common subexpression elimination
B) Dead code elimination
C) Peephole optimization
D) Constant folding
✅ Answer: A) Common subexpression elimination
Solution:
Avoids recomputing the same expression multiple times.
87.
Local optimizations are often applied:
A) After IR generation
B) Before lexical analysis
C) During parsing
D) After linking
✅ Answer: A) After IR generation
Solution:
Intermediate representation provides an ideal format for applying local transformations.
88.
The key difference between peephole and algebraic optimization is:
A) Input domain (machine vs. intermediate code)
B) Target architecture
C) Symbol table usage
D) Control flow
✅ Answer: A) Input domain (machine vs. intermediate code)
Solution:
Peephole works on assembly, algebraic works on high-level IR.
89.
Which optimization can change t = x * 2 + y * 2 → t = (x + y) * 2?
A) Algebraic simplification
B) Strength reduction
C) Common subexpression elimination
D) Peephole optimization
✅ Answer: A) Algebraic simplification
Solution:
Uses distributive property to simplify expressions.
90.
Local optimizations are best applied:
A) On small, linear code regions
B) On recursive function graphs
C) Across modules
D) After linking
✅ Answer: A) On small, linear code regions
Solution:
They are restricted to single basic blocks.
91.x = y; z = x; can be reduced to:
A) z = y;
B) x = z;
C) y = z;
D) No change
✅ Answer: A) z = y;
Solution:
Copy propagation allows replacing x with y.
92.
Eliminating the redundant jump sequence JMP L1; L1: is:
A) Peephole optimization
B) Common subexpression elimination
C) Code motion
D) Loop optimization
✅ Answer: A) Peephole optimization
Solution:
Removes unnecessary branch instructions.
93.
Local optimization that converts x = x * 1 → x = x is:
A) Algebraic simplification
B) Dead code elimination
C) Constant folding
D) Peephole optimization
✅ Answer: A) Algebraic simplification
Solution:
Removes identity operations.
94.
Local optimization reduces:
A) Register usage
B) Code size
C) Execution time
D) Both B and C
✅ Answer: D) Both B and C
Solution:
Simplified code runs faster and is shorter.
95.
In local optimization, redundancy is detected using:
A) Data flow within block
B) Symbol table
C) Parse tree
D) Machine code
✅ Answer: A) Data flow within block
Solution:
Uses local data dependencies to detect reuse opportunities.
96.
The simplest form of optimization is:
A) Constant folding
B) Dead code elimination
C) Loop unrolling
D) Register allocation
✅ Answer: A) Constant folding
Solution:
Requires only simple expression evaluation.
97.
Copy propagation may increase opportunities for:
A) Dead code elimination
B) Loop fusion
C) Inlining
D) Constant folding
✅ Answer: A) Dead code elimination
Solution:
Exposes useless assignments after copies are propagated.
98.
Local optimizations generally:
A) Do not affect program semantics
B) Modify program meaning
C) Create more loops
D) Add redundant computations
✅ Answer: A) Do not affect program semantics
Solution:
They preserve correctness while improving efficiency.
99.
Which of the following is not usually part of local optimization?
A) Code motion
B) Algebraic simplification
C) Peephole optimization
D) Constant folding
✅ Answer: A) Code motion
Solution:
Code motion requires control-flow analysis across blocks, making it global.
100.
Local optimization techniques are mainly applied:
A) Within basic blocks to improve efficiency
B) Across procedures
C) On high-level syntax
D) At linking time
✅ Answer: A) Within basic blocks to improve efficiency
Solution:
They operate locally within blocks to simplify computations and eliminate redundancy.