Optimization of Basic Blocks in Compiler Design

Optimization of Basic Blocks

We can apply the optimization process on a basic block. While optimization, there is no need to change the set of expressions computed by the block.

The basic block optimization can be done in two ways:

  • Structure-Preserving Transformations
  • Algebraic Transformations

Structure-Preserving Transformations

The primary Structure-Preserving Transformation on basic blocks are as follows:

  • Common sub-expression elimination
  • Dead code elimination
  • Renaming of temporary variables
  • Interchange of two adjacent independent statements.

Common sub-expression elimination:

Suppose we have two expressions that compute the same values. In that case, we need to eliminate one of the expressions. This method is known as the common sub-expression elimination method.

Example:

 p = a + b
 q = c – d
 r = x+ y
 s = c - d 

This example shows that the second and fourth statements compute the same expression: c - d. So the basic block can be transformed as follows:

 p = a + b
 q = c - d
 r = x + y
 s = q 

Dead code elimination:

Whenever a programmer writes a program, there is a possibility that the program may have some dead code. These dead codes are the result of some expression in the program. Generally, a programmer doesn't introduce dead code intentionally. The dead code may be a variable or the result of some expression computed by the programmer that may not have any further uses. By eliminating these useless things from a code, the code will get optimized.

Example:

A statement p = q + r appears in a block, and p is a dead symbol. It means that it will never be used subsequently so that we can eliminate this statement. This elimination does not have any impact on the values of the basic block.

Renaming of temporary variables:

Consider the statement x = a + b

The given statement's value is stored in the temporary variable x that can be changed to another temporary variable y and changes all uses of x to y.

This kind of transformation is also known as a normal-form block.

Interchange of two independent adjacent statements:

Suppose we have two statements as follows:

                          p = a + b
                          q = x + y                 

The given statement can be interchanged without affecting the value of the block when the value of p does not affect the value of q.

Algebraic Transformations:

We can also optimized the basic blocks using algebraic identities. For example, we may apply arithmetic identities, such as

 x + 0 = 0 + x = x                  x , 0= x
  x * 1=1 * x = x                    x= 1 = x                     

Local reduction in strength is also another kind of algebraic transformation. In this optimization, a more expensive operator is replaced by a cheaper one.

EXPENSIVE                  CHEAPER

x^2                                        x * x

2 * x                                       x + x  

x/2                                           x * 0.5

The third class of optimizations is constant folding. In constant folding, the value of the constant expression is evaluated at compile-time, and their values replace constant expression. So, the expression 3 * 3 will be replaced by 9.

Sometimes unexpected common subexpression arose due to some relational operators like <, >, and =.           

The relational operators such as < and = sometimes generate unexpected common subexpressions.

Sometimes associative rule may be applied to expose common sub expression. If the source code has the assignments:

a = b + c;

e = c + d+ b;

Then, the intermediate code may be generated as the following:

a = b + c

t = c + d

e = t + b


Related Topics

LALR 1 Parsing | Compiler Design

LALR (1) Parsing The LALR parsing refers to the "lookahead LR" that has many lesser steps than typical parsers based on LR(1) items. For constructing the LALR(1) parsing table, the canonical...

6 minutes read.

Storage Allocation

Storage Allocation The storage allocation represents memory management. The allocation of memory can be done in the following ways: Static AllocationStack AllocationHeap Management Static Allocation: It is a procedure used for the allocation of...

1 minute read.

Machine-Independent Optimizations in Compiler Design

Machine-Independent Optimizations The main aim of machine-independent optimization is to improve the generated intermediate code so that compiler can get better target code. Eliminating unwanted code from the object code or replacing...

8 minutes read.

Basic Blocks and Flow Graphs in Compiler Design

Basic Blocks and Flow Graphs In this section, we are going to learn how to work with basic block and flow graphs in compiler design. Basic Block The basic block is a set...

3 minutes read.

YACC in Compiler Design

YACC  YACC is known as Yet Another Compiler Compiler. It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. The input...

5 minutes read.

Switch Case Statement Compiler Design

Case Statement The “case” or “switch” statement is available in various languages. The following is the syntax for the case statement: switch (E)  {             case V1: S1             case V2: S2 ...

1 minute read.

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

6 minutes read.

Compiler Design Tutorial

Our compiler design tutorial will provide all the information about compiler from basic to advanced level. This compiler tutorial will help the student for their semester as well as for...

3 minutes read.

Syntax-Directed Translation

Syntax-Directed Translation A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can...

3 minutes read.

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

Evolution of Programming Languages in Compiler Design

The Evolution of Programming Languages The first computer came in the 1940s and was programmed in a binary language that told the computer what operations are to be performed and in...

4 minutes read.

S-attributed and L-attributed SDTs

S-attributed and L-attributed SDTs STD stands for Syntax Directed Translation. When we associate some informal notations called semantic rules and the grammar, they are known as STD. So we can say...

2 minutes read.

Parsing in Compiler Design

Parsing is the term used to describe the process of converting data between different formats. The parser can carry out this operation. The parser is a part of the translator...

4 minutes read.

LEX

LEX Lex is a tool/computer program that generates a Lexical analyzer. Lex is developed by Vern Paxson in C around 1987. Lex works together with the YACC parser generator. It allows...

3 minutes read.

Symbol Table Compiler Design

Symbol Table It is an important data structure used by the compiler. It stores information about various entities such as object, class, variable names, functions name, interfaces, procedure, literals, string etc.  The...

3 minutes read.

Errors in Compiler Design

Introduction Errors in compiler design refer to mistakes or issues that arise during the process of execution of the program. A compiler is a program that translates source code written in...

4 minutes read.

Shift Reduce Parsing in Compiler Design

Like the bottom-up parsing, the shift reduce parser also builds the parse tree from the leaves (bottom) to the root (up). The LR parser is a more versatile variation of...

4 minutes read.

Intermediate-Code Generator Compiler Design

Intermediate-Code Generator The process of translating a source language into machine code for a given target machine is done by intermediate-code. It lies between the high-level language and the machine language....

2 minutes read.

Three-Address Code

Three-Address Code If there is at most one operator on the right side of the instruction, then the instruction will be the three-address code so that no arithmetic expressions are permitted....

2 minutes read.

Ambiguity Elimination Compiler Design

Ambiguity Elimination Ambiguity elimination makes the sentence clear and readable. A sentence is grammatically ambiguous if it can produce more than one parse tree for a particular grammar. In this article,...

3 minutes read.