Basic Blocks in Compiler Design - GeeksforGeeks

16 Feb, 2022
Basic Block is a straight line code sequence that has no branches in and out branches except to the entry and at the end respectively. Basic Block is a set of statements that always executes one after other, in a sequence. 
The first task is to partition a sequence of three-address code into basic blocks. A new basic block is begun with the first instruction and instructions are added until a jump or a label is met. In the absence of a jump, control moves further consecutively from one instruction to another. The idea is standardized in the algorithm below: 
Algorithm: Partitioning three-address code into basic blocks. 
Input: A sequence of three address instructions. 
Process: Instructions from intermediate code which are leaders are determined. Following are the rules used for finding a leader:  
The first three-address instruction of the intermediate code is a leader.  Instructions that are targets of unconditional or conditional jump/goto statements are leaders.  Instructions that immediately follow unconditional or conditional jump/goto statements are considered leaders.  
The first three-address instruction of the intermediate code is a leader.  
Instructions that are targets of unconditional or conditional jump/goto statements are leaders.  
Instructions that immediately follow unconditional or conditional jump/goto statements are considered leaders.  
For each leader thus determined its basic block contains itself and all instructions up to excluding the next leader. 
Example: Intermediate code to set a 10*10 matrix to an identity matrix: 
 
1)  i=1        //Leader 1 (First statement)
2)  j=1             //Leader 2 (Target of 11th statement)
3)  t1 = 10 * i     //Leader 3 (Target of 9th statement) 
4)  t2 = t1 + j
5)  t3 = 8 * t2
6)  t4 = t3 - 88
7)  a[t4] = 0.0
8)  j = j + 1
9)  if j <= goto (3)       
10) i = i + 1                    //Leader 4 (Immediately following Conditional goto statement)
11) if i <= 10 goto (2)
12) i = 1                        //Leader 5 (Immediately following Conditional goto statement)
13) t5 = i - 1                   //Leader 6 (Target of 17th statement) 
14) t6 = 88 * t5
15) a[t6] = 1.0
16) i = i + 1
17) if i <= 10 goto (13) 
The given algorithm is used to convert a matrix into identity matrix i.e. a matrix with all diagonal elements 1 and all other elements as 0. 
Steps (3)-(6) are used to make elements 0, step (14) is used to make an element 1. These steps are used recursively by goto statements. 
There are 6 Basic Blocks in the above code : B1) Statement 1 B2) Statement 2 B3) Statement 3-9 B4) Statement 10-11 B5) Statement 12 B6) Statement 13-17 
jatin7599
rajkumaar23
singhdrishti1001
mohansaitejat
Compiler Design
GATE CS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Directed Acyclic graph in Compiler Design (with examples)
Difference between Top down parsing and Bottom up parsing
Loop Optimization in Compiler Design
S - attributed and L - attributed SDTs in Syntax directed translation
Compiler construction tools
Layers of OSI Model
ACID Properties in DBMS
TCP/IP Model
Page Replacement Algorithms in Operating Systems
Types of Operating Systems