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

What are the various LEADERS (leading statements) for the TAC given in the passage?

The correct answer is
100, 103, 104, 105, 106, 107, 111

Understanding Three Address Code Leaders

This solution explains how to identify the leaders (also known as leading statements) in a given sample of Three Address Code (TAC). Leaders are crucial for defining basic blocks, which are fundamental units in compiler design for control flow analysis and optimization.

What is Three Address Code (TAC)?

Three Address Code (TAC) is an intermediate representation (IR) form used in compilers. Each instruction in TAC generally has at most three operands (two source operands and one destination operand). It simplifies complex expressions into a sequence of simple statements.

What are Basic Blocks and Leaders?

A basic block is a straight-line sequence of code that has exactly one entry point and one exit point. Control flow can only enter the block at the beginning and leave at the end, without any jumps in or out of the middle of the block.

The leaders are the first statements of the basic blocks. They are identified based on the control flow of the program. Specifically, the leaders are:

  • The very first statement of the code.
  • Any statement that is the target of a jump instruction (conditional or unconditional).
  • Any statement that immediately follows a conditional jump instruction (and is not the target of that jump).

Identifying Leaders in the Given TAC Sample

Let's analyze the provided Three Address Code step by step to find the leaders:

Line No. TAC Instruction Reason for being a Leader
$100$ $t1 = y + 2$ First instruction of the entire code. Starts the first basic block.
$101$ $initial = x / t1$ Part of the first basic block. Not a leader.
$102$ $limit = 10$ Part of the first basic block. Not a leader.
$103$ $if i > j goto 105$ Target of jump from line 110. Starts a new basic block.
$104$ $goto 111$ Instruction immediately following a conditional jump (line 103). It's not the target ($105$) of the jump in line 103. Starts a new basic block.
$105$ $if num > b goto 107$ Target of jump from line 103. Starts a new basic block.
$106$ $goto 111$ Instruction immediately following a conditional jump (line 105). It's not the target ($107$) of the jump in line 105. Starts a new basic block.
$107$ $num = limit - 1$ Target of jump from line 105. Starts a new basic block.
$108$ $b = y + 2$ Part of the basic block starting at $107$. Not a leader.
$109$ $i = i - num$ Part of the basic block starting at $107$. Not a leader.
$110$ $goto 103$ This is an unconditional jump instruction. The block it belongs to ends here. It doesn't start a new block itself based on the leader rules.
$111$ $j = i - num$ Target of jumps from line 104 and line 106. Starts a new basic block.
$112$ $k = j + b$ Part of the basic block starting at $111$. This is the last instruction; it doesn't start a new block. Not a leader.

Conclusion: Identified Leaders

Based on the analysis of the control flow:

  • The first instruction is $100$.
  • Targets of jumps are $103$ (from line 110), $105$ (from line 103), $107$ (from line 105), and $111$ (from lines 104 and 106).
  • Instructions immediately following conditional jumps are $104$ (after line 103) and $106$ (after line 105).

Combining these identified statements, the set of leaders for the given Three Address Code is:

$100, 103, 104, 105, 106, 107, 111$

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. Which of the followings is the correct Flow graph for the TAC given in the passage?
  3. Which of the following is the Optimized version for the given TAC 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