Code Optimization
Unit- V
CHAMELI DEVI GROUP OF INSTITUTIONS,
INDORE
Prepared By:
Sumeet Kothari
Asst. Professor
Department of Computer Science & Engineering
Code Optimization
 A program transformation technique used for improving the
code by enabling it to consume fewer resources and deliver
high speed is known as Optimization.
 In optimization, high-level general programming constructs
are replaced by very efficient low-level programming codes.
 The code optimization in the synthesis phase is a program
transformation technique, which tries to improve the
intermediate code by making it consume fewer resources
(i.e. CPU, Memory) so that faster-running machine code will
result.
Objectives of Code Optimization
 Compiler optimizing process should meet the following
objectives :
 The optimization must be correct, it must not, in any way,
change the meaning of the program.
 Optimization should increase the speed and performance
of the program.
 The compilation time must be kept reasonable.
 The optimization process should not delay the overall
compiling process.
Types of Code Optimization
 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.
 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
may have absolute memory references rather than relative
references. Machine-dependent optimizers put efforts to take
maximum advantage of memory hierarchy.
Machine Independent Optimization
do
{
item = 10;
value = value + item;
}
while(value<100);
item = 10;
do
{
value = value + item;
}
while(value<100);
This code involves repeated assignment of the
identifier item
If we put this way it should not only save the
CPU cycles, but can be used on any
processor.
Basic Blocks
 A number of sequences are included in the source codes,
which are executed in sequence and are termed as the basic
blocks of the code.When the first instruction is executed, all
the instructions of the same basic block are executed in the
sequence of appearance by not losing the program flow
control.
 Various constructs can be included in a program as basic
blocks, like IF-THEN-ELSE, SWITCH-CASE conditional
statements and loops such as DO-WHILE, FOR, and
REPEAT-UNTIL, etc.
Basic Block Identification
 Search header statements of all the basic blocks from where a
basic block starts:
 First statement of a program.
 Statements that are target of any branch
(conditional/unconditional).
 Statements that follow any branch statement.
 Header statements and the statements following them form a
basic block.
 A basic block does not include any header statement of any
other basic block.
Basic blocks play an important role in identifying variables, which are
being used more than once in a single basic block. If any variable is
being used more than once, the register memory allocated to that
variable need not be emptied unless the block finishes execution.
Example
Control Flow Graph
 Basic blocks in a program can be represented by means of
control flow graphs. A control flow graph depicts how the
program control is being passed among the blocks. It is a
useful tool that helps in optimization by help locating any
unwanted loops in the program.
Optimization Techniques
 CompileTime Evaluation
 Constant Folding
 Constant Propagation
 Common sub-expression elimination
 Loop Optimization
 Code motion
 Induction-variable elimination
 Strength reduction
 Dead Code Elimination
 Partial Redundancy
Compile Time Evaluation
 Constant Folding- In this technique, as the name suggests, it
involves folding the constants.
 The expressions that contain the operands having constant values
at compile time are evaluated.
 Those expressions are then replaced with their respective results.
 Example-
Circumference of Circle = (22/7) x Diameter
Here,
 This technique evaluates the expression 22/7 at compile time.
 The expression is then replaced with its result 3.14.
Compile Time Evaluation
 Constant Propagation- In this technique, if some variable has been
assigned some constant value, then it replaces that variable with its
constant value in the further program during compilation.
 The condition is that the value of variable must not get alter in between.
 Example-
pi = 3.14
radius = 10
Area of circle = pi x radius x radius
Here,
 This technique substitutes the value of variables‘pi’ and‘radius’ at compile
time.
 It then evaluates the expression 3.14 x 10 x 10.
 The expression is then replaced with its result 314.
Common Sub-Expression Elimination
 The expression that has been already computed before and
appears again in the code for computation is called
as Common Sub-Expression.
 In this technique, As the name suggests, it involves
eliminating the common sub expressions.
 The redundant expressions are eliminated to avoid their re-
computation.
 The already computed result is used in the further program
when required.
Example
Code Before Optimization Code After Optimization
S1 = 4 * i
S2 = a[S1]
S3 = 4 * j
S4 = 4 * i //Redundant Expression
S5 = n
S6 = b[S4] + S5
S1 = 4 * i
S2 = a[S1]
S3 = 4 * j
S5 = n
S6 = b[S1] + S5
Loop Optimization
 Loop optimization is most valuable machine-independent
optimization because program's inner loop takes bulk to time
of a programmer.
 If we decrease the number of instructions in an inner loop
then the running time of a program may be improved even if
we increase the amount of code outside that loop.
 For loop optimization the following three techniques are
important:
 Code motion
 Induction-variable elimination
 Strength reduction
Code Motion
 Code motion is used to decrease the amount of code in loop.
This transformation takes a statement or expression which
can be moved outside the loop body without affecting the
semantics of the program.
 For example : In the while statement, the limit-2 equation is
a loop invariant equation.
while(i<=limit-2) /*statement does not change limit*/
After code motion the result is as follows:
a= limit-2;
while(i<=a) /*statement does not change limit or a*/
Induction-Variable Elimination
 Induction variable elimination is used to replace variable
from inner loop. It can reduce the number of additions in a
loop. It improves both code space and run time performance.
In this figure, we
can replace the
assignment t4:=4*j
by t4:=t4-4. The
only problem which
will be arose that t4
does not have a
value when we
enter block B2 for
the first time. So we
place a relation
t4=4*j on entry to
the block B2.
Reduction in Strength
 Strength reduction is used to replace the expensive operation
by the cheaper once on the target machine.
 Addition of a constant is cheaper than a multiplication. So we
can replace multiplication with an addition within the loop.
 Multiplication is cheaper than exponentiation. So we can
replace exponentiation with multiplication within the loop.
while (i<10)
{
j= 3 * i+1;
a[j]=a[j]-2;
i=i+2;
}
s= 3*i+1;
while (i<10)
{
j=s;
a[j]= a[j]-2;
i=i+2;
s=s+6;
}
After strength reduction
Dead-Code Elimination
 Dead code is one or more than one code statements, which are:
 Either never executed or unreachable,
 Or if executed, their output is never used.
 Thus, dead code plays no role in any program operation and
therefore it can simply be eliminated.
 Example:
c=a*b
x=a
till
d = a * b + 4
//After elimination :
c = a * b
till
d = a * b + 4
Example:
 The example depicts that the conditional statement is always
false, implying that the code, written in true case, will never
be executed, hence it can be removed.
a=1;
b=10;
if (a>b)
{
........;
}
else
{
..........;
}
Partially Dead Code
 There are some code statements whose computed values are
used only under certain circumstances, i.e., sometimes the
values are used and sometimes they are not. Such codes are
known as partially dead-code.
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.
[redundant expression] [partially redundant expression]
Example
 Loop-invariant code is partially redundant and can be
eliminated by using a code-motion technique. Another
example of a partially redundant code can be:
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 follows:
If (condition)
{
... tmp = y OP z;
a = tmp; ...
}
else
{
... tmp = y OP z;
}
c = tmp;
Data Flow Analysis
 It is the analysis of flow of data in control flow graph, i.e., the
analysis that determines the information regarding the
definition and use of data in program.
 With the help of this analysis optimization can be done. In
general, its process in which values are computed using data
flow analysis.
 The data flow property represents information which can be
used for optimization.
Basic Terminologies
 Definition Point: A point in a program containing some definition.
 Reference Point: A point in a program containing a reference to a
data item.
 Evaluation Point: A point in a program containing evaluation of
expression.
Data Flow Properties
 Available Expression – A expression is said to be available
at a program point x if and only if along paths its reaching to
x. A Expression is available at its evaluation point.
A expression a+b is said to be available if none of the
operands gets modified before their use.
 Example – (It is used to eliminate common sub expressions.)
 Reaching Definition – A definition D is reaches a point x
if there is path from D to x in which D is not killed, i.e., not
redefined.
 Example – (It is used in constant and variable propagation.)
 Live variable – A variable is said to be live at some point p
if from p to end the variable is used before it is redefined else
it becomes dead.
 Example- (It is useful for register allocation and dead code
elimination.)
 –
 Busy Expression-An expression is busy along a path if and
only if its evaluation exists along that path and none of its
operand definition exists before its evaluation along the path.
 Advantage –It is used for performing code movement
optimization.
Global Data Flow Analysis
 To efficiently optimize the code compiler collects all the
information about the program and distribute this
information to each block of the flow graph.This process is
known as data-flow graph analysis.
 Certain optimization can only be achieved by examining the
entire program. It can't be achieve by examining just a
portion of the program.
 For this kind of optimization user defined chaining is one
particular problem.
 Here using the value of the variable, we try to find out that
which definition of a variable is applicable in a statement.
 Based on the local information a compiler can perform some
optimizations. For example, consider the following code:
x = a + b;
x = 6 * 3
 In this code, the first assignment of x is useless. The value
computer for x is never used in the program. At compile time
the expression 6*3 will be computed, simplifying the second
assignment statement to x = 18;
 Some optimization needs more global information. For
example, consider the following code:
a = 1;
b = 2;
c = 3;
if (....) x = a + 5;
else x = b + 4;
c = x + 1;
 In this code, at line 3 the initial assignment is useless and x +1
expression can be simplified as 7.
 But it is less obvious that how a compiler can discover these
facts by looking only at one or two consecutive statements.A
more global analysis is required so that the compiler knows
the following things at each point in the program:
 Which variables are guaranteed to have constant values
 Which variables will be used before being redefined
 Data flow analysis is used to discover this kind of property.
The data flow analysis can be performed on the program's
control flow graph (CFG).
 The control flow graph of a program is used to determine
those parts of a program to which a particular value assigned
to a variable might propagate.
Code Improving Transformations
 It is an algorithms for performing the code improving
transformations rely on data-flow information.
 Here we consider common sub-expression elimination, copy
propagation and transformations for moving loop invariant
computations out of loops and for eliminating induction
variables.
 Global transformations are not substitute for local
transformations; both must be performed.
Algorithms for Performing the Code
Improving Transformations
 Elimination of global common sub expressions.
 Copy propagation
 Detection of loop-invariant computations
 Performing code motion
 Alternative code motion strategies
 Maintaining data-flow information after code motion
 Elimination of induction variable
Data-flow Analysis of Structured Programs
 Flow graphs for control flow constructs such as do-while
statements have a useful property: there is a single
beginning point at which control enters and a single end
point that control leaves from when execution of the
statement is over.
 We exploit this property when we talk of the definitions
reaching the beginning and the end of statements with the
following syntax.
S id: = E| S; S | if E then S else S | do S while E
E  id + id| id
S1 ; S2 IF E then S1 else S2 do S1 while E
 Expressions in this language are similar to those in the
intermediate code, but the flow graphs for statements have
restricted forms.
 We define a portion of a flow graph called a region to be a
set of nodes N that includes a header, which dominates all
other nodes in the region. All edges between nodes in
N are in the region, except for some that enter the header.
 We say that the beginning points of the dummy blocks at
the entry and exit of a statement’s region are the
beginning and end points, respectively, of the statement.
The equations are inductive, or syntax-directed,
definition of the sets in[S], out[S], gen[S], and kill[S]
for all statements S.
 gen[S] is the set of definitions “generated” by S while kill[S]
is the set of definitions that never reach the end of S.
 Consider the following data-flow equations for reaching
definitions :
d
gen [S] = { d }
kill [S] = Da – { d }
out [S] = gen [S] U ( in[S] – kill[S] )
 Observe the rules for a single assignment of variable a. Surely
that assignment is a definition of a, say d.Thus
Gen[S]={d}
 On the other hand, d “kil s” all other definitions of a, so we write
Kill[S] = Da – {d}
Where, Da is the set of all definitions in the program for variable a.
 Under what circumstances is definition d generated by S=S1; S2?
First of all, if it is generated by S2, then it is surely generated by
S. if d is generated by S1, it will reach the end of S provided it is
not killed by S2.Thus, we write
 gen[S]=gen[S2] U (gen[S1]-kill[S2])
 Similar reasoning applies to the killing of a definition, so we have
Kill[S] = kill[S2] U (kill[S1] – gen[S2])
gen[S]=gen[S2] U (gen[S1]-kill[S2])
Kill[S] = kill[S2] U (kill[S1] – gen[S2])
in [S1] = in [S] in [S2] = out [S1]
out [S] = out [S2]
THANK
YOU

complier design unit 5 for helping students

  • 1.
    Code Optimization Unit- V CHAMELIDEVI GROUP OF INSTITUTIONS, INDORE Prepared By: Sumeet Kothari Asst. Professor Department of Computer Science & Engineering
  • 2.
    Code Optimization  Aprogram transformation technique used for improving the code by enabling it to consume fewer resources and deliver high speed is known as Optimization.  In optimization, high-level general programming constructs are replaced by very efficient low-level programming codes.  The code optimization in the synthesis phase is a program transformation technique, which tries to improve the intermediate code by making it consume fewer resources (i.e. CPU, Memory) so that faster-running machine code will result.
  • 3.
    Objectives of CodeOptimization  Compiler optimizing process should meet the following objectives :  The optimization must be correct, it must not, in any way, change the meaning of the program.  Optimization should increase the speed and performance of the program.  The compilation time must be kept reasonable.  The optimization process should not delay the overall compiling process.
  • 4.
    Types of CodeOptimization  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.  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 may have absolute memory references rather than relative references. Machine-dependent optimizers put efforts to take maximum advantage of memory hierarchy.
  • 5.
    Machine Independent Optimization do { item= 10; value = value + item; } while(value<100); item = 10; do { value = value + item; } while(value<100); This code involves repeated assignment of the identifier item If we put this way it should not only save the CPU cycles, but can be used on any processor.
  • 6.
    Basic Blocks  Anumber of sequences are included in the source codes, which are executed in sequence and are termed as the basic blocks of the code.When the first instruction is executed, all the instructions of the same basic block are executed in the sequence of appearance by not losing the program flow control.  Various constructs can be included in a program as basic blocks, like IF-THEN-ELSE, SWITCH-CASE conditional statements and loops such as DO-WHILE, FOR, and REPEAT-UNTIL, etc.
  • 7.
    Basic Block Identification Search header statements of all the basic blocks from where a basic block starts:  First statement of a program.  Statements that are target of any branch (conditional/unconditional).  Statements that follow any branch statement.  Header statements and the statements following them form a basic block.  A basic block does not include any header statement of any other basic block.
  • 8.
    Basic blocks playan important role in identifying variables, which are being used more than once in a single basic block. If any variable is being used more than once, the register memory allocated to that variable need not be emptied unless the block finishes execution. Example
  • 9.
    Control Flow Graph Basic blocks in a program can be represented by means of control flow graphs. A control flow graph depicts how the program control is being passed among the blocks. It is a useful tool that helps in optimization by help locating any unwanted loops in the program.
  • 10.
    Optimization Techniques  CompileTimeEvaluation  Constant Folding  Constant Propagation  Common sub-expression elimination  Loop Optimization  Code motion  Induction-variable elimination  Strength reduction  Dead Code Elimination  Partial Redundancy
  • 11.
    Compile Time Evaluation Constant Folding- In this technique, as the name suggests, it involves folding the constants.  The expressions that contain the operands having constant values at compile time are evaluated.  Those expressions are then replaced with their respective results.  Example- Circumference of Circle = (22/7) x Diameter Here,  This technique evaluates the expression 22/7 at compile time.  The expression is then replaced with its result 3.14.
  • 12.
    Compile Time Evaluation Constant Propagation- In this technique, if some variable has been assigned some constant value, then it replaces that variable with its constant value in the further program during compilation.  The condition is that the value of variable must not get alter in between.  Example- pi = 3.14 radius = 10 Area of circle = pi x radius x radius Here,  This technique substitutes the value of variables‘pi’ and‘radius’ at compile time.  It then evaluates the expression 3.14 x 10 x 10.  The expression is then replaced with its result 314.
  • 13.
    Common Sub-Expression Elimination The expression that has been already computed before and appears again in the code for computation is called as Common Sub-Expression.  In this technique, As the name suggests, it involves eliminating the common sub expressions.  The redundant expressions are eliminated to avoid their re- computation.  The already computed result is used in the further program when required.
  • 14.
    Example Code Before OptimizationCode After Optimization S1 = 4 * i S2 = a[S1] S3 = 4 * j S4 = 4 * i //Redundant Expression S5 = n S6 = b[S4] + S5 S1 = 4 * i S2 = a[S1] S3 = 4 * j S5 = n S6 = b[S1] + S5
  • 15.
    Loop Optimization  Loopoptimization is most valuable machine-independent optimization because program's inner loop takes bulk to time of a programmer.  If we decrease the number of instructions in an inner loop then the running time of a program may be improved even if we increase the amount of code outside that loop.  For loop optimization the following three techniques are important:  Code motion  Induction-variable elimination  Strength reduction
  • 16.
    Code Motion  Codemotion is used to decrease the amount of code in loop. This transformation takes a statement or expression which can be moved outside the loop body without affecting the semantics of the program.  For example : In the while statement, the limit-2 equation is a loop invariant equation. while(i<=limit-2) /*statement does not change limit*/ After code motion the result is as follows: a= limit-2; while(i<=a) /*statement does not change limit or a*/
  • 17.
    Induction-Variable Elimination  Inductionvariable elimination is used to replace variable from inner loop. It can reduce the number of additions in a loop. It improves both code space and run time performance. In this figure, we can replace the assignment t4:=4*j by t4:=t4-4. The only problem which will be arose that t4 does not have a value when we enter block B2 for the first time. So we place a relation t4=4*j on entry to the block B2.
  • 18.
    Reduction in Strength Strength reduction is used to replace the expensive operation by the cheaper once on the target machine.  Addition of a constant is cheaper than a multiplication. So we can replace multiplication with an addition within the loop.  Multiplication is cheaper than exponentiation. So we can replace exponentiation with multiplication within the loop. while (i<10) { j= 3 * i+1; a[j]=a[j]-2; i=i+2; } s= 3*i+1; while (i<10) { j=s; a[j]= a[j]-2; i=i+2; s=s+6; } After strength reduction
  • 19.
    Dead-Code Elimination  Deadcode is one or more than one code statements, which are:  Either never executed or unreachable,  Or if executed, their output is never used.  Thus, dead code plays no role in any program operation and therefore it can simply be eliminated.  Example: c=a*b x=a till d = a * b + 4 //After elimination : c = a * b till d = a * b + 4
  • 20.
    Example:  The exampledepicts that the conditional statement is always false, implying that the code, written in true case, will never be executed, hence it can be removed. a=1; b=10; if (a>b) { ........; } else { ..........; }
  • 21.
    Partially Dead Code There are some code statements whose computed values are used only under certain circumstances, i.e., sometimes the values are used and sometimes they are not. Such codes are known as partially dead-code. 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.
  • 22.
    Partial Redundancy  Redundantexpressions 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. [redundant expression] [partially redundant expression]
  • 23.
    Example  Loop-invariant codeis partially redundant and can be eliminated by using a code-motion technique. Another example of a partially redundant code can be: 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 follows: If (condition) { ... tmp = y OP z; a = tmp; ... } else { ... tmp = y OP z; } c = tmp;
  • 24.
    Data Flow Analysis It is the analysis of flow of data in control flow graph, i.e., the analysis that determines the information regarding the definition and use of data in program.  With the help of this analysis optimization can be done. In general, its process in which values are computed using data flow analysis.  The data flow property represents information which can be used for optimization.
  • 25.
    Basic Terminologies  DefinitionPoint: A point in a program containing some definition.  Reference Point: A point in a program containing a reference to a data item.  Evaluation Point: A point in a program containing evaluation of expression.
  • 26.
    Data Flow Properties Available Expression – A expression is said to be available at a program point x if and only if along paths its reaching to x. A Expression is available at its evaluation point. A expression a+b is said to be available if none of the operands gets modified before their use.  Example – (It is used to eliminate common sub expressions.)
  • 27.
     Reaching Definition– A definition D is reaches a point x if there is path from D to x in which D is not killed, i.e., not redefined.  Example – (It is used in constant and variable propagation.)
  • 28.
     Live variable– A variable is said to be live at some point p if from p to end the variable is used before it is redefined else it becomes dead.  Example- (It is useful for register allocation and dead code elimination.)  –
  • 29.
     Busy Expression-Anexpression is busy along a path if and only if its evaluation exists along that path and none of its operand definition exists before its evaluation along the path.  Advantage –It is used for performing code movement optimization.
  • 30.
    Global Data FlowAnalysis  To efficiently optimize the code compiler collects all the information about the program and distribute this information to each block of the flow graph.This process is known as data-flow graph analysis.  Certain optimization can only be achieved by examining the entire program. It can't be achieve by examining just a portion of the program.  For this kind of optimization user defined chaining is one particular problem.  Here using the value of the variable, we try to find out that which definition of a variable is applicable in a statement.
  • 31.
     Based onthe local information a compiler can perform some optimizations. For example, consider the following code: x = a + b; x = 6 * 3  In this code, the first assignment of x is useless. The value computer for x is never used in the program. At compile time the expression 6*3 will be computed, simplifying the second assignment statement to x = 18;  Some optimization needs more global information. For example, consider the following code: a = 1; b = 2; c = 3; if (....) x = a + 5; else x = b + 4; c = x + 1;  In this code, at line 3 the initial assignment is useless and x +1 expression can be simplified as 7.
  • 32.
     But itis less obvious that how a compiler can discover these facts by looking only at one or two consecutive statements.A more global analysis is required so that the compiler knows the following things at each point in the program:  Which variables are guaranteed to have constant values  Which variables will be used before being redefined  Data flow analysis is used to discover this kind of property. The data flow analysis can be performed on the program's control flow graph (CFG).  The control flow graph of a program is used to determine those parts of a program to which a particular value assigned to a variable might propagate.
  • 33.
    Code Improving Transformations It is an algorithms for performing the code improving transformations rely on data-flow information.  Here we consider common sub-expression elimination, copy propagation and transformations for moving loop invariant computations out of loops and for eliminating induction variables.  Global transformations are not substitute for local transformations; both must be performed.
  • 34.
    Algorithms for Performingthe Code Improving Transformations  Elimination of global common sub expressions.  Copy propagation  Detection of loop-invariant computations  Performing code motion  Alternative code motion strategies  Maintaining data-flow information after code motion  Elimination of induction variable
  • 35.
    Data-flow Analysis ofStructured Programs  Flow graphs for control flow constructs such as do-while statements have a useful property: there is a single beginning point at which control enters and a single end point that control leaves from when execution of the statement is over.  We exploit this property when we talk of the definitions reaching the beginning and the end of statements with the following syntax. S id: = E| S; S | if E then S else S | do S while E E  id + id| id
  • 36.
    S1 ; S2IF E then S1 else S2 do S1 while E  Expressions in this language are similar to those in the intermediate code, but the flow graphs for statements have restricted forms.
  • 37.
     We definea portion of a flow graph called a region to be a set of nodes N that includes a header, which dominates all other nodes in the region. All edges between nodes in N are in the region, except for some that enter the header.  We say that the beginning points of the dummy blocks at the entry and exit of a statement’s region are the beginning and end points, respectively, of the statement. The equations are inductive, or syntax-directed, definition of the sets in[S], out[S], gen[S], and kill[S] for all statements S.  gen[S] is the set of definitions “generated” by S while kill[S] is the set of definitions that never reach the end of S.  Consider the following data-flow equations for reaching definitions :
  • 38.
    d gen [S] ={ d } kill [S] = Da – { d } out [S] = gen [S] U ( in[S] – kill[S] )  Observe the rules for a single assignment of variable a. Surely that assignment is a definition of a, say d.Thus Gen[S]={d}  On the other hand, d “kil s” all other definitions of a, so we write Kill[S] = Da – {d} Where, Da is the set of all definitions in the program for variable a.
  • 39.
     Under whatcircumstances is definition d generated by S=S1; S2? First of all, if it is generated by S2, then it is surely generated by S. if d is generated by S1, it will reach the end of S provided it is not killed by S2.Thus, we write  gen[S]=gen[S2] U (gen[S1]-kill[S2])  Similar reasoning applies to the killing of a definition, so we have Kill[S] = kill[S2] U (kill[S1] – gen[S2]) gen[S]=gen[S2] U (gen[S1]-kill[S2]) Kill[S] = kill[S2] U (kill[S1] – gen[S2]) in [S1] = in [S] in [S2] = out [S1] out [S] = out [S2]
  • 40.