Prerequisite – Three address code in Compiler
Loop optimization is the phase after the Intermediate Code Generation. The main intention of this phase is to reduce the number of lines in a program. In any program majority of the time is spent by any program is actually inside the loop for an iterative program. In case of the recursive program a block will be there and the majority of the time will present inside the block.
Loop Optimization –
- To apply loop optimization we must first detect loops.
- For detecting loops we use Control Flow Analysis(CFA) using Program Flow Graph(PFG).
- To find program flow graph we need to find Basic Block
Basic Block – A basic block is a sequence of three address statements where control enters at the beginning and leaves only at the end without any jumps or halts.
Finding the Basic Block –
In order to find the basic blocks, we need to find the leaders in the program. Then a basic block will start from one leader to the next leader but not including the next leader. Which means if you find out that lines no 1 is a leader and line no 15 is the next leader, then the line from 1 to 14 is a basic block, but not including line no 15.
Identifying leader in a Basic Block –
- First statement is always a leader
- Statement that is target of conditional or un-conditional statement is a leader
- Statement that follows immediately a conditional or un-conditional statement is a leader
Three Address Code of the above C code:
- f = 1;
- i = 2;
- if (i > x) goto 9
- t1 = f * i;
- f = t1;
- t2 = i + 1;
- i = t2;
- goto calling program
Leader and Basic Block –
Control Flow Analysis –
If the control enter B1 there is no other option after B1, it has to enter B2. Now, if control enters B2, then depending on the condition control will flow, if the condition is true we are going to line no 9, which means 9 is nothing but B4. But if the condition is a false control goes to next block B3. After B3, there is no condition at all we are direct goes to 3rd statement B2. Above control flow graph have a cycle between B2 and B3 which is nothing but a loop.
- Three address code in Compiler
- Loop Optimization in Compiler Design
- Code Optimization in Compiler Design
- Introduction of Object Code in Compiler Design
- Intermediate Code Generation in Compiler Design
- Error detection and Recovery in Compiler
- Introduction of Compiler Design
- Bootstrapping in Compiler Design
- Why FIRST and FOLLOW in Compiler Design?
- Runtime Environments in Compiler Design
- Peephole Optimization in Compiler Design
- Error Handling in Compiler Design
- Types of Parsers in Compiler Design
- Semantic Analysis in Compiler Design
- Basic Blocks in Compiler Design
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.