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 one set of code with another set of code, which makes the object code faster without changing the result of object code, is generally called code improvement or code optimization.

A high-level language can have run-time overhead. Writing a program in a high-level language can cause redundancy. Code optimization will allow us to remove these inefficiencies.

Optimized code will use less space and makes the program execution fast. The optimized code can be re-used.

Code optimization can be performed in the following ways:

Compile Time Evaluation

  1. a = 4*(19/0.5)*r

            Perform 4*(19/0.5)*r at compile time

      2) a = 5

           b = a/6

           Perform a/6 as 5/6 at compile time

Global Common Subexpressions

Any expression can be known as a common subexpression if the value of an expression is previously computed. And the value of the variable in expression has not changed since the previous computation.

Example

p = x * y

q = x

r = q * y + 9

After the optimization of code:

p = x * y

q = x

r = x * y + 9

Here, after variable propagation, x * y and q * y identified as common sub-expression.

Dead-Code Elimination

If any variable's value can be used anywhere in the program, then the variable will be called live at a point in a program; otherwise, it will be termed as dead code. A dead code is not added intentionally by the programmer in any program. It may be the result of the previous computation.

Example

Before the elimination of code:

p = x * y

q = x

r = q * y + 9

After the elimination of code:

p = x * y

r = q * y + 9

In this example, q = x is a dead code because there is no use of this code in the program.

Code Motion

Loops have a very important place for optimizations, mostly in the inner loops where programs tend to spend a lot of their time. A program's run time complexity may be improved by decreasing the amount of code in an inner loop, even if we increase the amount of code outside that loop.

Code motion reduces the number of code in a loop. This optimization computes a loop-invariant statement outside the loop.

Example

Evaluation of limit - 2 in the following while-statement:

while (i <= limit - 2)

This code will be further optimized as:

t = limit-2

while (i <= t)  

After the optimization, limit-2 is computed only one time before entering the loop.  Previously there would be n +1 calculations of limit-2 if we iterated the body of the loop n times.

Induction Variables and Reduction in Strength

Optimization of induction variables inside a loop is also an important optimization. Any variable of the form x = x + constant is induction variable.

Induction variables can be computed with a single increment per loop iteration.

Replacing an expensive operator with low strength is strength reduction.

Example

Before the reduction, code is:

i= 2;                                                                                                                          

while(i<10)                                                                                                               

{                                                                                                                                 

  y = i * 6                                                                                                                   

}                                                                                                                              

Code, after the reduction:

i=2                                                                                                                          

x = 6                                                                                                                        

{                                                                                                                             

  while(x<30)                                                                                                          

  y =x;                                                                                                                    

  x = x +4:                                                                                                               

}                                                                                                                             


Related Topics

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.

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.

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.

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.

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.

Syntax Analysis Compiler Design

Syntax Analysis This article will describe the parsing method used in the compiler. The grammatical rule of programming language can be constructed with the help of context-free grammars or BNF (Backus–Naur...

3 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.

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.

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.

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.

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.

LR Parser Compiler Design

LR Parser LR parsing is a type of bottom-up parsing that is used to parse the large class of grammars. Here "L" stands for left-to-right scanning of the input "R" stands...

6 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.

Boolean Expression in Compiler Design

Boolean Expression The translation of conditional statements such as if-else statements and while-do statements is associated with Boolean expression's translation. The main use of the Boolean expression is the following: Boolean expressions...

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.

LR Parser in Compiler Design

LR Parser The most popular type of bottom-up parsing is LR(K) parsing. The LR() parser scans the input from left – to – right, which is the actual abbreviation of L...

2 minutes read.

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 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.

Phases of Compiler

Phases of Compiler The compilation process of a compiler goes through various phases. Each phase of the compiler takes the output from the previous as an input. Here we will see all...

2 minutes read.