Reference
Reference
Syntax Analysis: Role of the parser - Context free grammars - Top-down parsing: shift reduce
- predictive parsing; Bottom-up parsing: Operator precedence, LR parsers (SLR, Canonical
LR,LALR) - Parser generators-Design aspects of Parser.
SYNTAX ANALYSIS
Syntax analysis is the second phase of the compiler. It gets the input from the tokens and
generates a syntax tree or parse tree.
The parser or syntactic analyzer obtains a string of tokens from the lexical analyzer and verifies
that the string can be generated by the grammar for the source language. It reports any syntax
errors in the program. It also recovers from commonly occurring errors so that it can continue
processing its input.
Issues :
It should recover from each error quickly enough to be able to detect subsequent errors.
This is the easiest way of error-recovery and also, it prevents the parser from developing
infinite loops.
Statement mode:
When a parser encounters an error, it tries to take corrective measures so that the rest of
inputs of statement allow the parser to parse ahead.
Error productions:
Some common errors are known to the compiler designers that may occur in the code.
In addition, the designers can create augmented grammar to be used, as productions that
generate erroneous constructs when these errors are encountered.
Global correction:
The parser considers the program in hand as a whole and tries to figure out what the
program is intended to do and tries to find out a closest match for it, which is error-free.
When an erroneous input (statement) X is fed, it creates a parse tree for some closest
error-free statement Y.
This may allow the parser to make minimal changes in the source code, but due to the
complexity (time and space) of this strategy, it has not been implemented in practice yet.
A context-free grammar (CFG) consisting of a finite set of grammar rules for deriving
(or) generating strings (or sentences) is a quadruple (V, T, P, S) where
Example 1:
Construct the CFG for the language having any number of a’s.
Solution:
SaS
S
aS
aaS
aaaS
aaaaS
aaaaaS
aaaaaaS
aaaaaaaS
Example 2:
Solution:
S
aSb
aaSbb
aaaSbbb
aaabbb
Example 3:
Find aab by SAB
AaaA
A
BBb
B
Solution:
S AB
aaAB
aa B
aaB
aaBb
aa b
aab
Example 4:
Solution:
S 0S1
00S11
000111
Example 5:
Let, G be the grammar
S aB | bA
Aa | aS | bAA
Bb | bs | aBB
For the string baaabbabba. Find LMD, RMD and Parse tree
Solution:
The way the production rules are implemented (derivation) divides parsing into two
types : top-down parsing and bottom-up parsing.
Top-down Parsing:
When the parser starts constructing the parse tree from the start symbol and then tries to
transform the start symbol to the input, it is called top-down parsing.
Bottom-up Parsing:
As the name suggests, bottom-up parsing starts with the input symbols and tries to
construct the parse tree up to the start symbol.
Example:
Input string : a + b * c
Production rules:
S→E
E→E+T
E→E*T
a+b*c
Read the input and check if any production matches with the input:
a+b*c
T+b*c
E+b*c
E+T*c
E*c
E*T
E
S
We have learnt in the last chapter that the top-down parsing technique parses the input,
and starts constructing a parse tree from the root node gradually moving down to the leaf
nodes.
This parsing technique recursively parses the input to make a parse tree, which may or
may not require back-tracking.
But the grammar associated with it (if not left factored) cannot avoid back-tracking.
A form of recursive-descent parsing that does not require any back-tracking is known
as predictive parsing.
Back-tracking:
Top- down parsers start from the root node (start symbol) and match the input string
against the production rules to replace them (if matched).
S → rXd | rZd
X → oa | ea
Z → ai
For an input string: read, a top-down parser, will behave like this:
It will start with S from the production rules and will match its yield to the left-most letter of the
input, i.e. ‘r’. The very production of S (S → rXd) matches with it. So the top-down parser
advances to the next input letter (i.e. ‘e’). The parser tries to expand non-terminal ‘X’ and
checks its production from the left (X → oa). It does not match with the next input symbol. So
the top-down parser backtracks to obtain the next production rule of X, (X → ea).
Now the parser matches all the input letters in an ordered manner. The string is accepted.
To accomplish its tasks, the predictive parser uses a look-ahead pointer, which points to
the next input symbols.
To make the parser back-tracking free, the predictive parser puts some constraints on the
grammar and accepts only a class of grammar known as LL(k) grammar.
Both the stack and the input contains an end symbol $to denote that the stack is empty
and the input is consumed.
The parser refers to the parsing table to take any decision on the input and stack element
combination.
In recursive descent parsing, the parser may have more than one production to choose
from for a single instance of input, whereas in predictive parser, each step has at most
one production to choose.
There might be instances where there is no production matching the input string, making
the parsing procedure to fail.
LL grammar is a subset of context-free grammar but with some restrictions to get the
simplified version, in order to achieve easy implementation.
LL parser is denoted as LL(k). The first L in LL(k) is parsing the input from left to right, the
second L in LL(k) stands for left-most derivation and k itself represents the number of look
aheads. Generally k = 1, so LL(k) may also be written as LL(1).
LL Parsing Algorithm:
We may stick to deterministic LL(1) for parser explanation, as the size of table grows
exponentially with the value of k.
Secondly, if a given grammar is not LL(1), then usually, it is not LL(k), for any given k.
Input:
string ω
parsing table M for grammar G
Output:
If ω is in L(G) then left-most derivation of ω,
error otherwise.
repeat
let X be the top stack symbol and a the symbol pointed by ip.
if X∈ Vt or $
if X = a
POP X and advance ip.
else
error()
endif
else /* X is non-terminal */
if M[X,a] = X → Y1, Y2,... Yk
POP X
PUSH Yk, Yk-1,... Y1 /* Y1 on top */
Output the production X → Y1, Y2,... Yk
else
error()
endif
endif
until X = $ /* empty stack */
if β → t, then α does not derive any string beginning with a terminal in FOLLOW(A).
Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction till
it reaches the root node.
Here, we start from a sentence and then apply production rules in reverse manner in
order to reach the start symbol. The image given below depicts the bottom-up parsers
available.
Shift-Reduce Parsing:
Shift-reduce parsing uses two unique steps for bottom-up parsing.
Shift step: The shift step refers to the advancement of the input pointer to the next input
symbol, which is called the shifted symbol. This symbol is pushed onto the stack. The
shifted symbol is treated as a single node of the parse tree.
Reduce step : When the parser finds a complete grammar rule (RHS) and replaces it to
(LHS), it is known as reduce-step. This occurs when the top of the stack contains a
handle. To reduce, a POP function is performed on the stack which pops off the handle
and replaces it with LHS non-terminal symbol.
It uses a wide class of context-free grammar which makes it the most efficient syntax
analysis technique.
LR parsers are also known as LR(k) parsers, where L stands for left-to-right scanning of
the input stream; R stands for the construction of right-most derivation in reverse, and k
denotes the number of lookahead symbols to make decisions.
There are three widely used algorithms available for constructing an LR parser:
LR(1) – LR Parser:
token = next_token()
repeat forever
s = top of stack
else
error()
Starts with the root nonterminal on the Ends with the root nonterminal on the stack.
stack.
Uses the stack for designating what is still Uses the stack for designating what is already
to be expected. seen.
Builds the parse tree top-down. Builds the parse tree bottom-up.
Continuously pops a nonterminal off the Tries to recognize a right hand side on the
stack, and pushes the corresponding right stack, pops it, and pushes the corresponding
hand side. nonterminal.
Reads the terminals when it pops one off Reads the terminals while it pushes them on
the stack. the stack.
Pre-order traversal of the parse tree. Post-order traversal of the parse tree.
SEMANTIC ANALYSIS
Semantic Analysis computes additional information related to the meaning of the
program once the syntactic structure is known.
In typed languages as C, semantic analysis involves adding information to the symbol
table and performing type checking.
The information to be computed is beyond the capabilities of standard parsing
techniques, therefore it is not regarded as syntax.
As for Lexical and Syntax analysis, also for Semantic Analysis we need both a
Representation Formalism and an Implementation Mechanism.
As representation formalism this lecture illustrates what are called Syntax Directed
Translations.
SYNTAX DIRECTED TRANSLATION
The Principle of Syntax Directed Translation states that the meaning of an input
sentence is related to its syntactic structure, i.e., to its Parse-Tree.
By Syntax Directed Translations we indicate those formalisms for specifying
translations for programming language constructs guided by context-free grammars.
o We associate Attributes to the grammar symbols representing the language
constructs.
S-attributed SDT
If an SDT uses only synthesized attributes, it is called as S-attributed SDT.
These attributes are evaluated using S-attributed SDTs that have their semantic actions
written after the production (right hand side).
L-attributed SDT
This form of SDT uses both synthesized and inherited attributes with restriction of not
taking values from right siblings.
In L-attributed SDTs, a non-terminal can get values from its parent, child, and sibling
nodes. As in the following production.
S can take values from A, B, and C (synthesized). A can take values from S only. B can
take values from S and A. C can get values from S, A, and B. No non-terminal can get
values from the sibling to its right.
Top-Down Translation
TYPE CHECKING
A compiler must check that the source program follows both syntactic and
semantic conventions of the source language.
This checking, called static checking, detects and reports programming errors.
Type checks –
A compiler should report an error if an operator is applied to an incompatible
operand. Example: If an array variable and function variable are added together.
Flow-of-control checks
Statements that cause flow of control to leave a construct must have some place to which
to transfer the flow of control. Example: An error occurs when an enclosing statement,
such as break, does not exist in switch statement.
A type checker verifies that the type of a construct matches that expected by its
context. For example : arithmetic operator mod in Pascal requires integer operands, so a
type checker verifies that the operands of mod have type integer.
Type information gathered by a type checker may be needed when code is generated.
TYPE SYSTEMS
The design of a type checker for a language is based on information about the syntactic
constructs in the language, the notion of types, and the rules for assigning types to language
.
constructs.
For example : “ if both operands of the arithmetic operators of +,- and * are of type
integer, then the result is of type integer ”
Run-Time Environment:
A program as a source code is merely a collection of text (code, statements etc.) and to
make it alive, it requires actions to be performed on the target machine.
Runtime support system is a package, mostly generated with the executable program
itself and facilitates the process communication between the process and the runtime
environment.
It takes care of memory allocation and de-allocation while the program is being
executed.
Activation Trees
A program is a sequence of instructions combined into a number of procedures.
Instructions in a procedure are executed sequentially.
A procedure has a start and an end delimiter and everything inside it is called the body of
the procedure.
The procedure identifier and the sequence of finite instructions inside it make up the
body of the procedure.
The execution of a procedure is called its activation. An activation record contains all the
necessary information required to call a procedure.
An activation record may contain the following units (depending upon the source
language used).
Machine Status Stores machine status such as Registers, Program Counter etc., before
the procedure is called.
Control Link Stores the address of activation record of the caller procedure.
Access Link Stores the information of data which is outside the local scope.
Actual Parameters Stores actual parameters, i.e., parameters which are used to send input
to the called procedure.
Whenever a procedure is executed, its activation record is stored on the stack, also
known as control stack.
When a procedure calls another procedure, the execution of the caller is suspended until
the called procedure finishes execution.
At this time, the activation record of the called procedure is stored on the stack.
We assume that the program control flows in a sequential manner and when a procedure
is called, its control is transferred to the called procedure. When a called procedure is
executed, it returns the control back to the caller. This type of control flow makes it
easier to represent a series of activations in the form of a tree, known as the activation
tree.
...
printf(“Enter Your Name: “);
Dr. Neeraj Dahiya, Asst. Prof., CSE Page 6
scanf(“%s”, username);
show_data(username);
printf(“Press any key to continue…”);
...
int show_data(char *user)
{
printf(“Your name is %s”, username);
return 0;
}
...
Type systems
type system a collection of rules for assigning type expressions to the various
-directed manner.
he
Checkingdone by a compiler is said to be static, while checking done when the target
program runs is termed dynamic.
Any check can be done dynamically, if the target code carries the type of an element
along with the value of that element.
Sound type system
A sound type system eliminates the need for dynamic checking for type errors because it
allows us to determine statically that these errors cannot occur when the target program
runs.
That is, if a sound type system assigns a type other than type_error to a program part,
then type errors cannot occur when the target code for the program part is run.
Error Recovery
Since type checking has the potential for catching errors in program, it is desirable for
type checker to recover from errors, so it can check the rest of the input.
Error handling has to be designed into the type system right from the start; the type
checking rules must be prepared to cope with errors.
SPECIFICATION OF A SIMPLE TYPE CHECKER:
Dr. Neeraj Dahiya, Asst. Prof., CSE Page 8
Here, we specify a type checker for a simple language in which the type of each identifier
must be declared before the identifier is used.
The type checker is. a translation scheme that synthesizes the type of each expression
from the types of its sub expressions.
The type checker can handle arrays, pointers, statements and functions.
A Simple Language
P→D;E
D → D ; D | id : T
T → char | integer | array [ num ] of T | ↑ T
E → literal | num | id | E mod E | E [ E ] | E ↑
Translation scheme:
P→D;E
D→D;D
D → id : T { addtype (id.entry , T.type)}
T → char { T.type : = char }
T → integer { T.type : = integer }
T → ↑ T1 { T.type : = pointer(T1.type) }
T → array [ num ] of T1 { T.type : = array ( 1… num.val , T1.type) }
Statements do not have values; hence the basic type void can be assigned to them. If an error is
1. Assignment statement:
S → id : = E { S.type : = if id.type = E.type then void else
type_error }
2. Conditional statement:
S → if E then S1 { S.type : = if E.type = boolean then S1.type
else type_error }
3. While statement:
S → while E do S1 { S.type : = if E.type = boolean then S1.type
Procedures:
A procedure definition is a declaration that associates an identifier with a statement. The
identifier is the procedure name, and the statement is the procedure body.
tk/
For example, the following is the definition of procedure named readarray :
procedure readarray; .
Var i : integer;
begin
for i : = 1 to 9 do
read(a[i]) end;
When a procedure name appears within an executable statement, the procedure is said to
be called at that point.
Storage Allocation
Runtime environment manages runtime memory requirements for the following entities:
Code : It is known as the text part of a program that does not change at runtime. Its
memory requirements are known at the compile time.
Variables : Variables are known at the runtime only, unless they are global or constant.
Heap memory allocation scheme is used for managing allocation and de-allocation of
memory for variables in runtime.
Static Allocation
In this allocation scheme, the compilation data is bound to a fixed location in the
memory and it does not change when the program executes.
As the memory requirement and storage locations are known in advance, runtime support
package for memory allocation and de-allocation is not required.
Stack Allocation
Procedure calls and their activations are managed by means of stack memory allocation.
It works in last-in-first-out (LIFO) method and this allocation strategy is very useful for
recursive procedure calls.
Heap Allocation
Variables local to a procedure are allocated and de-allocated only at runtime.
Heap allocation is used to dynamically allocate memory to the variables and claim it
back when the variables are no more required.
Except statically allocated memory area, both stack and heap memory can grow and
shrink dynamically and unexpectedly.
Therefore, they cannot be provided with a fixed amount of memory in the system.
Stack and heap memory are arranged at the extremes of total memory allocated to the
program. Both shrink and grow against each other.
Before moving ahead, first go through some basic terminologies pertaining to the values
in a program.
r-value
The value of an expression is called its r-value. The value contained in a single variable
also becomes an r-value if it appears on the right-hand side of the assignment operator.
l-value
The location of memory (address) where an expression is stored is known as the l-value
of that expression.
For example:
day = 1;
week = day * 7;
month = 1;
year = month * 12;
From this example, we understand that constant values like 1, 7, 12, and variables like
day, week, month and year, all have r-values.
Only variables have l-values as they also represent the memory location assigned to
them.
For example:
7 = x + y;
is an l-value error, as the constant 7 does not represent any memory location.
Actual Parameters
Variables whose values or addresses are being passed to the called procedure are called
actual parameters.
Example:
fun_one()
{
int actual_parameter = 10;
call fun_two(int actual_parameter);
}
fun_two(int formal_parameter)
{
print formal_parameter;
}
Formal parameters hold the information of the actual parameter, depending upon the
parameter passing technique used.
Pass by Value
In pass by value mechanism, the calling procedure passes the r-value of actual
parameters and the compiler puts that into the called procedure’s activation record.
Formal parameters then hold the values passed by the calling procedure.
Pass by Reference
In pass by reference mechanism, the l-value of the actual parameter is copied to the
activation record of the called procedure.
This way, the called procedure now has the address (memory location) of the actual
parameter and the formal parameter refers to the same memory location.
Therefore, if the value pointed by the formal parameter is changed, the impact should be
seen on the actual parameter as they should also point to the same value.
Pass by Copy-restore
This parameter passing mechanism works similar to ‘pass-by-reference’ except that the
changes to actual parameters are made when the called procedure ends.
Upon function call, the values of actual parameters are copied in the activation record of
the called procedure.
Example:
int y;
calling_procedure()
{
y = 10;
copy_restore(y); //l-value of y is passed
printf y; //prints 99
}
copy_restore(int x)
{
x = 99; // y still has value 10 (unaffected)
Dr. Neeraj Dahiya, Asst. Prof., CSE Page 15
y = 0; // y is now 0
}
When this function ends, the l-value of formal parameter x is copied to the actual
parameter y.
Even if the value of y is changed before the procedure ends, the l-value of x is copied to
the l-value of y making it behave like call by reference.
Pass by Name
Languages like Algol provide a new kind of parameter passing mechanism that works
like preprocessor in C language.
In pass by name mechanism, the name of the procedure being called is replaced by its
actual body.
Pass-by-name textually substitutes the argument expressions in a procedure call for the
corresponding parameters in the body of the procedure so that it can now work on actual
parameters, much like pass-by-reference.
Symbol Table:
Symbol table is an important data structure created and maintained by compilers in order
to store information about the occurrence of various entities such as variable names,
function names, objects, classes, interfaces, etc. Symbol table is used by both the
analysis and the synthesis parts of a compiler.
A symbol table may serve the following purposes depending upon the language in hand:
For example, if a symbol table has to store information about the following variable declaration:
Implementation
If a compiler is to handle a small amount of data, then the symbol table can be
implemented as an unordered list, which is easy to code, but it is only suitable for small
tables only. A symbol table can be implemented in one of the following ways:
Operations
A symbol table, either linear or hash, should provide the following operations.
insert()
This operation is more frequently used by analysis phase, i.e., the first half of the
compiler where tokens are identified and names are stored in the table.
This operation is used to add information in the symbol table about unique names
occurring in the source code. The format or structure in which the names are stored
depends upon the compiler in hand.
Dr. Neeraj Dahiya, Asst. Prof., CSE Page 17
An attribute for a symbol in the source code is the information associated with that
symbol.
This information contains the value, state, scope, and type about the symbol.
The insert() function takes the symbol and its attributes as arguments and stores the
information in the symbol table.
For example:
int a;
insert(a, int);
lookup()
lookup() operation is used to search a name in the symbol table to determine:
lookup(symbol)
This method returns 0 (zero) if the symbol does not exist in the symbol table. If the
symbol exists in the symbol table, it returns its attributes stored in the table.
Scope Management
A compiler maintains two types of symbol tables: a global symbol table which can be
accessed by all the procedures and scope symbol tables that are created for each scope
in the program.
The global symbol table contains names for one global variable (int value) and two
procedure names, which should be available to all the child nodes shown above.
The names mentioned in the pro_one symbol table (and all its child tables) are not
available for pro_two symbols and its child tables.
This symbol table data structure hierarchy is stored in the semantic analyzer and
whenever a name needs to be searched in a symbol table, it is searched using the
following algorithm:
first a symbol will be searched in the current scope, i.e. current symbol table.
if a name is found, then search is completed, else it will be searched in the parent symbol
table until,
either the name is found or global symbol table has been searched for the name.
CODE GENERATION:
Code generation can be considered as the final phase of compilation. Through post code
generation, optimization process can be applied on the code, but that can be seen as a part of
code generation phase itself. The code generated by the compiler is an object code of some
lower-level programming language, for example, assembly language. We have seen that the
source code written in a higher-level language is transformed into a lower-level language that
results in a lower-level object code, which should have the following minimum properties:
Interior nodes also represent the results of expressions or the identifiers/name where the
values are to be stored or assigned.
Example:
t0 = a + b
t1 = t0 + c
d = t0 + t1
[t0 = a + b]
[t1 = t0 + c]
[d = t0 + t1]
Peephole Optimization
This optimization technique works locally on the source code to transform it into an optimized
code. By locally, we mean a small portion of the code block at hand. These methods can be
applied on intermediate codes as well as on target codes. A bunch of statements is analyzed and
are checked for the following possible optimization:
At compilation level, the compiler searches for instructions redundant in nature. Multiple
loading and storing of instructions may carry the same meaning even if some of them are
removed. For example:
MOV x, R0
MOV R0, R1
We can delete the first instruction and re-write the sentence as:
MOV x, R1
Unreachable code
Unreachable code is a part of the program code that is never accessed because of programming
constructs. Programmers may have accidently written a piece of code that can never be reached.
Example:
void add_ten(int x)
{
return x + 10;
printf(“value of x is %d”, x);
}
In this code segment, the printf statement will never be executed as the program control returns
back before it can execute, hence printf can be removed.
...
In this code,label L1 can be removed as it passes the control to L2. So instead of jumping to L1
and then to L2, the control can directly reach L2, as shown below:
...
MOV R1, R2
GOTO L2
...
L2 : INC R1
Strength reduction
There are operations that consume more time and space. Their ‘strength’ can be reduced by
replacing them with other operations that consume less time and space, but produce the same
result.
For example, x * 2 can be replaced by x << 1, which involves only one left shift. Though the
output of a * a and a2 is same, a2 is much more efficient to implement.
Target language : The code generator has to be aware of the nature of the target
language for which the code is to be transformed. That language may facilitate some
machine-specific instructions to help the compiler generate the code in a more
convenient way. The target machine can have either CISC or RISC processor
architecture.
Ordering of instructions : At last, the code generator decides the order in which the
instruction will be executed. It creates schedules for instructions to execute them.
Descriptors
The code generator has to track both the registers (for availability) and addresses (location of
values) while generating the code. For both of them, the following two descriptors are used:
Register descriptor : Register descriptor is used to inform the code generator about the
availability of registers. Register descriptor keeps track of values stored in each register.
Whenever a new register is required during code generation, this descriptor is consulted
for register availability.
Address descriptor : Values of the names (identifiers) used in the program might be
stored at different locations while in execution. Address descriptors are used to keep
track of memory locations where the values of identifiers are stored. These locations
Code generator keeps both the descriptor updated in real-time. For a load statement, LD R1, x,
the code generator:
Code Generation
Basic blocks comprise of a sequence of three-address instructions. Code generator takes
these sequence of instructions as input.
Note : If the value of a name is found at more than one place (register, cache, or memory), the
register’s value will be preferred over the cache and main memory. Likewise cache’s value will
be preferred over the main memory. Main memory is barely given any preference.
getReg : Code generator uses getReg function to determine the status of available registers and
the location of name values. getReg works as follows:
Else if both the above options are not possible, it chooses a register that requires minimal
number of load and store instructions.
For an instruction x = y OP z, the code generator may perform the following actions. Let us
assume that L is the location (preferably register) where the output of y OP z is to be saved:
MOV y’, L
OP z’, L
If y and z has no further use, they can be given back to the system.
Other code constructs like loops and conditional statements are transformed into assembly
language in general assembly way.
CODE OPTIMIZATION:
Optimization is a program transformation technique, which tries to improve the code by making
it consume less resources (i.e. CPU, Memory) and deliver high speed.
In optimization, high-level general programming constructs are replaced by very efficient low-
level programming codes. A code optimizing process must follow the three rules given below:
The output code must not, in any way, change the meaning of the program.
Optimization should increase the speed of the program and if possible, the program
should demand less number of resources.
Optimization should itself be fast and should not delay the overall compiling process.
Efforts for an optimized code can be made at various levels of compiling the process.
At the beginning, users can change/rearrange the code or use better algorithms to write
the code.
While producing the target machine code, the compiler can make use of memory
hierarchy and CPU registers.
Machine-independent Optimization
In this optimization, the compiler takes in the intermediate code and transforms a part of the
code that does not involve any CPU registers and/or absolute memory locations. For example:
do
{
item = 10;
value = value + item;
} while(value<100);
This code involves repeated assignment of the identifier item, which if we put this way:
Item = 10;
do
{
value = value + item;
} while(value<100);
should not only save the CPU cycles, but can be used on any processor.
Machine-dependent Optimization
Machine-dependent optimization is done after the target code has been generated and when the
code is transformed according to the target machine architecture. It involves CPU registers and
Basic Blocks
Source codes generally have a number of instructions, which are always executed in sequence
and are considered as the basic blocks of the code. These basic blocks do not have any jump
statements among them, i.e., when the first instruction is executed, all the instructions in the
same basic block will be executed in their sequence of appearance without losing the flow
control of the program.
A program can have various constructs as basic blocks, like IF-THEN-ELSE, SWITCH-CASE
conditional statements and loops such as DO-WHILE, FOR, and REPEAT-UNTIL, etc.
Search header statements of all the basic blocks from where a basic block starts:
A basic block does not include any header statement of any other basic block.
Basic blocks are important concepts from both code generation and optimization point of view.
Invariant code : A fragment of code that resides in the loop and computes the same
value at each iteration is called a loop-invariant code. This code can be moved out of the
loop by saving it to be computed only once, rather than with each iteration.
Strength reduction : There are expressions that consume more CPU cycles, time, and
memory. These expressions should be replaced with cheaper expressions without
compromising the output of expression. For example, multiplication (x * 2) is expensive
in terms of CPU cycles than (x << 1) and yields the same result.
Dead-code Elimination
Dead code is one or more than one code statements, which are:
The above control flow graph depicts a chunk of program where variable ‘a’ is used to assign
the output of expression ‘x * y’. Let us assume that the value assigned to ‘a’ is never used inside
the loop.Immediately after the control leaves the loop, ‘a’ is assigned the value of variable ‘z’,
which would be used later in the program. We conclude here that the assignment code of ‘a’ is
never used anywhere, therefore it is eligible to be eliminated.
Partial Redundancy
Redundant expressions are computed more than once in parallel path, without any change in
operands.whereas partial-redundant expressions are computed more than once in a path, without
any change in operands. For example,
If (condition)
{
a = y OP z;
}
else
{
...
}
c = y OP z;
We assume that the values of operands (y and z) are not changed from assignment of
variable a to variable c. Here, if the condition statement is true, then y OP z is computed twice,
otherwise once. Code motion can be used to eliminate this redundancy, as shown below:
If (condition)
{
...
tmp = y OP z;
a = tmp;
...
}
else
{
...
Dr. Neeraj Dahiya, Asst. Prof., CSE Page 14
tmp = y OP z;
}
c = tmp;
Here, whether the condition is true or false; y OP z should be computed only once.