All Exams Test series for 1 year @ ₹349 only
Question

Consider the following Three Address code sample for solving the questions:
100: t1 = y+2
101: initial = x/t1
102: limit = 10
103: if i > j goto 105
104: goto 111
105: if num > b goto 107
106: goto 111
107: num = limit-1
108: b = y+2
109: i = i - num
110: goto 103
111: j = i - num
112: k= j+b

Which of the following is the Optimized version for the given TAC in the passage

The correct answer is

100: t1 = y+2
101: initial x/t1
102: limit = 10
103: if i > j goto 105
104: goto 111
105: if num < b goto 111
106: num = limit-1
107: b = t1
108: i = i-num
109: goto 103
110: k=i+b

TAC Optimization: Analysis and Explanation

Original Three Address Code Sample

The initial Three Address Code (TAC) provided is:

  • $100: t1 = y+2$
  • $101: initial = x/t1$
  • $102: limit = 10$
  • $103: if i > j goto 105$
  • $104: goto 111$
  • $105: if num > b goto 107$
  • $106: goto 111$
  • $107: num = limit-1$
  • $108: b = y+2$
  • $109: i = i - num$
  • $110: goto 103$
  • $111: j = i - num$
  • $112: k= j+b$

Optimization Techniques Identification

The goal of optimizing TAC is to improve code efficiency, making it run faster or use fewer resources. We analyze common techniques applied to the given code:

1. Constant Folding

This technique computes expressions involving only constant values during compile time, rather than at runtime.

  • In the original TAC, line 102 defines a constant: $limit = 10$.
  • Line 107 contains the calculation $num = limit-1$.
  • Optimization: Since $limit$ is known to be $10$, the expression $limit-1$ (which mathematically is $10 - 1$) can be calculated immediately by the compiler, resulting in $9$. The instruction can be simplified to $num = 9$. This avoids performing the subtraction during program execution.

2. Copy Propagation

This optimization technique replaces the use of a variable with its assigned value, especially after a direct copy instruction.

  • The original TAC calculates $t1 = y+2$ in line 100.
  • Subsequently, line 108 assigns $b = y+2$. This appears to be a redundant calculation, as the same value is computed twice.
  • Optimization: If the value of $t1$ (computed in line 100) is still valid and needed where $b$ is used later, the assignment $b = y+2$ can be replaced by $b = t1$. This leverages the previously computed value, potentially making the code more efficient by eliminating a redundant operation.

3. Syntax Correction and Control Flow Simplification

Optimizations can also involve correcting minor errors and restructuring the code's execution flow for better clarity or efficiency.

  • Syntax Fix: The TAC presented in line 101, $initial x/t1$, seems incomplete. Standard TAC format usually includes an assignment operator, suggesting the intended instruction was likely $initial = x/t1$.
  • Control Flow Restructuring: The sequence of conditional jumps and their targets can be analyzed and modified. Optimizations might involve removing unnecessary jumps, merging conditional blocks, or changing conditions slightly if the overall logic remains equivalent or is intentionally simplified, as seen in the provided options.

Analyzing the Optimized Version (Option 4)

Option 4 represents a version of the TAC that incorporates several optimizations:

  • $100: t1 = y+2$
  • $101: initial = x/t1$ (Syntax corrected from the likely original form)
  • $102: limit = 10$
  • $103: if i > j goto 105$
  • $104: goto 111$
  • $105: if num < b goto 111$ (Note: The condition $num < b$ differs from the original $num > b$, indicating a potential restructuring or correction of control flow logic.)
  • $106: num = limit-1$ (Implementation of Constant Folding; evaluates to $num = 9$)
  • $107: b = t1$ (Implementation of Copy Propagation, reusing value from line 100)
  • $108: i = i-num$
  • $109: goto 103$ (This maintains a loop structure similar to the original)
  • $110: k=i+b$ (This replaces the original two instructions $j = i - num; k = j + b$, suggesting a simplification or algorithmic change.)

Key optimizations evident in this version include:

  • Constant Folding: As applied in line 106, where $limit-1$ would be computed as $9$.
  • Copy Propagation: Demonstrated in line 107 ($b = t1$), avoiding recalculation.
  • Restructuring & Simplification: The code flow is reorganized, and the final calculation for $k$ is simplified compared to the original TAC.

These identified optimizations contribute to a potentially more efficient execution compared to the original TAC.

Was this answer helpful?

Important Questions from Code Optimization

  1. Which of the following is the correct High level code for the given TAC in the passage
  2. What are the various LEADERS (leading statements) for the TAC given in the passage?
  3. Which of the followings is the correct Flow graph for the TAC given in the passage?
  4. Consider the control flow graph shown in the figure.
     


    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.

  5. Consider the control flow graph given below.



    Which one of the following options is the set of live variables at the exit point of each basic block?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App