Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
10 views
12 pages
CD Unit 5
Compiler design notes 5
Uploaded by
Vanam Niharika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save CD Unit 5 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
10 views
12 pages
CD Unit 5
Compiler design notes 5
Uploaded by
Vanam Niharika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save CD Unit 5 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save CD Unit 5 For Later
You are on page 1
/ 12
Search
Fullscreen
1a) Write about Data Flow Analysis of structural programs? Ans: 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 that 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, Lya=2 | Definition Point —-—__] 1 ———, Lo:b=a Reference Point ¥ ; Evaluation Point Lg: z=x*y | iv Data Flow Properties - * Available Expression — A expression is said to be available at a program point x paths its reaching to x. A Expression is available at its evaluation point An expression a+b is said to be available if none of the operands gets modified before their use. Example —y=x+d{lq] Bo Bs Expression 4 * i is available for block Bz B3 © Advantage — 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 ~ [ Dy:x=4 | By 1 Dj is reaching definition for By but not for Bg since it is killed by Dz + Advantage — 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 -ais live at block B, , Bz Bg but killed at Bs * Advantage — 1. Itis useful for register allocation. 2. It is used in dead code elimination. * Busy Expression ~ An expression is busy along a path 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. 2c) Explain in detail the procedure that eliminates global common sub expression. Ans: Common Sub-expression Elimination: The expression or sub-expression that has been appeared and computed before and appears again during the computation of the code is the common sub-expression. Elimination of that sub-expression is known as Common sub-expression elimination. The advantage of this elimination method is to make the computation faster and beter by avoiding the re-computation of the expression. In addition, it utilizes memory efficiently. ‘Types of common sub-expression elimination The two types of elimination methods in common sub-expression elimination are: 1. Local Common Sub-expression climination— It is used within a single basic block. Where a basic block is a simple code sequence that has no branches. 2. Global Common Sub-expression elimination It is used for an entire procedure of common sub-expression eliminationExample Before climination a= 10; baati*2 c=atl*2; ‘c' has common expression as ‘b ier a= 10, baatI*2 ation d=bta, Let's understand Example 1 with a diagram : 12 +10 =22 fig.: Example 1 As shown in the figure (fig.: Example 1), the result of ‘d’ would be similar with both expressions. So, we will eliminate one of the common subexpressions, as it helps in faster execution and efficient memory utilization. Example Before elimination x=ll yall * 24: Zax" 24; ‘2’ has common expression as 'y' as 'v’ can be evaluated directly ax done in 'y. After elimination — y=11*23¢)Explain how copy propagation can be done using data flow equation. th fot py statemen gation tani fc F ratement a := b. Let h oi P The flow graph should also con f B along every path and ther ah 1 ng that path, We also need dus chair efinition and used fini bedi utput h i a nsformat Method : F h state \ i pert 1 ; ichable f fi 3) tisfies the condition mentioned in step (2) then remove s and replace uses of x found in (1) |nd x is not altered alon 1 then eliminate the copy by t, and then climinate 4e) Explain about machine dependent code optimization. Ans: Machin lependent Opti 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. 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 programA 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. Basic block identification We may use the following algorithm to find the basic blocks in a program: + Search header statements of all the basic blocks from where a basic block starts: First statement of a program. o Stater ents 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 are important concepts from both code generation and optimization point of view. y= 2 att; att; } wax az; w=xta; Source Code Basic Blocks 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. 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,ENTER w= 0; y= 0; if( x > z) Bi = 7 \ as B2 83 s3 \ f EXIT Basic Blocks Flow Graph Loop Opt Most programs run as a loop in the system. It becomes necessary to optimize the loops in order to save CPU cycles and memory. Loops can be optimized by the following techniques: + 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. + Induction analysis ; A variable is called an induction variable if its value is altered within the loop by a loop-invariant value. + 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: ization * Either never executed or unreachable, © Orif executed, their output is never used. Thus, dead code plays no role in any program operation and therefore it can simply be eliminated. Partially dead codeThere 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. Likewise, the picture above 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. 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,[redundant expression] [partially redundant expression] 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=yOPz, } clse € ) c=yOPz; 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 (condi t p) tmp ~y OPz, a= tmp; 5 else { tmp =y OPz, } c=tmp, SnHere, whether the condition is true or false; y OP z should be computed only once 5C) Discuss about the following: i) Copy Propagation ii) Dead code Elimination and iii) Code motion, iv) Constant folding » Ans: Common Sub expressions elimination: + An occurrence of an expression E is called a common sub- expression if E was previously computed, and the values of variables in E have not changed since the previous computation. We can avoid recomputing the expression if we can use the previously computed value. For example The above code can be optimized using the common sub-expression elimination as The common sub expression t,:~4*i is eliminated as its computation is already in tr, And value of jis not been changed from tou > Copy Propagation: Assignments of the form : = g called copy statements, or copies for short. The idea behind the cony-propagation tansfommation is to use g for { whenever possible after the copy siaicment f= « Copy propagation means usc of onc variable insicad of another. This May not appear ta be an_improvement, but_as_we shall see it gives us_an opportunity to eliminate x. + For examplex=Pi ertry The optimization using copy propagation can be done as follows: A=Pi*r*r; Here the variable x is eliminated > Dead-Code Eliminations: + Avariable is live at a point in a program if its value can be used subsequently; otherwise, it is dead at that point. A related idea is dead or useless code, statements that compute values that never get used. While the programmer is unlikely to introduce any dead code intentionally, it may appear as the result of previous transformations. An optimization can be done by eliminating dead code. Example: Here, ‘if’ statement is dead code because this condition will never get satisfied > Constant folding: * We can eliminate both the test and printing from the object code. More generally, deducing at compile time that the value of an expression is a constant and using the constant instead is known as constant folding. * One advantage of copy_propagation_is_that_it_ofien tums the copy statement into dead code. v For example, a=3.14157/2 can be replaced by a=1.570 there by eliminating a division operation,
You might also like
CD Unit-V
PDF
No ratings yet
CD Unit-V
10 pages
Code Optimization
PDF
No ratings yet
Code Optimization
32 pages
Lecture Notes On Code Optimization
PDF
No ratings yet
Lecture Notes On Code Optimization
175 pages
0code Optimization
PDF
No ratings yet
0code Optimization
149 pages
Module 5 - Code Optimization
PDF
No ratings yet
Module 5 - Code Optimization
72 pages
Compiler Design-Code Optimization
PDF
No ratings yet
Compiler Design-Code Optimization
150 pages
Code Optimization
PDF
0% (1)
Code Optimization
90 pages
Unit5 0CodeOptimization
PDF
No ratings yet
Unit5 0CodeOptimization
90 pages
CODE Optimization
PDF
No ratings yet
CODE Optimization
50 pages
CD Unit 4
PDF
No ratings yet
CD Unit 4
152 pages
Code Optimization
PDF
0% (1)
Code Optimization
42 pages
CSE2002 Session38 Code Optimization3038
PDF
No ratings yet
CSE2002 Session38 Code Optimization3038
39 pages
CS602PC - Compiler - Design - Lecture Notes - Unit - 5
PDF
No ratings yet
CS602PC - Compiler - Design - Lecture Notes - Unit - 5
28 pages
Unit 5.1
PDF
No ratings yet
Unit 5.1
49 pages
Code Optimization
PDF
No ratings yet
Code Optimization
65 pages
ATC - Code Optimization and Code Generation
PDF
No ratings yet
ATC - Code Optimization and Code Generation
28 pages
Unit 4
PDF
No ratings yet
Unit 4
19 pages
CS3501 CD Qb-Unit 5
PDF
No ratings yet
CS3501 CD Qb-Unit 5
9 pages
CD Unit 4
PDF
No ratings yet
CD Unit 4
32 pages
CD Unit-5
PDF
No ratings yet
CD Unit-5
45 pages
Code Optimization Unit-4-II
PDF
No ratings yet
Code Optimization Unit-4-II
27 pages
Unit 5
PDF
No ratings yet
Unit 5
17 pages
Code Optimization
PDF
No ratings yet
Code Optimization
149 pages
CD Unit 5
PDF
No ratings yet
CD Unit 5
41 pages
Code Optimization
PDF
No ratings yet
Code Optimization
26 pages
Code Optimization
PDF
No ratings yet
Code Optimization
58 pages
Code Optimization - Compiler Design
PDF
No ratings yet
Code Optimization - Compiler Design
33 pages
Unit 5 Compiler PDF
PDF
No ratings yet
Unit 5 Compiler PDF
28 pages
Compiler Design Unit 5
PDF
No ratings yet
Compiler Design Unit 5
39 pages
Code Optimization
PDF
No ratings yet
Code Optimization
36 pages
Code Optmize
PDF
No ratings yet
Code Optmize
41 pages
@@code Optim
PDF
No ratings yet
@@code Optim
20 pages
Compiler Design
PDF
No ratings yet
Compiler Design
25 pages
Unit 5 Compiler Design
PDF
No ratings yet
Unit 5 Compiler Design
29 pages
Vision 2024 CD Chapter 5 Compiler Code Optimization 731689660928542
PDF
No ratings yet
Vision 2024 CD Chapter 5 Compiler Code Optimization 731689660928542
24 pages
Unit 4
PDF
No ratings yet
Unit 4
19 pages
Optimization PDF
PDF
No ratings yet
Optimization PDF
40 pages
CD Unit V
PDF
No ratings yet
CD Unit V
17 pages
Code Optimization
PDF
No ratings yet
Code Optimization
21 pages
Cds 1
PDF
No ratings yet
Cds 1
27 pages
CD Unit-5
PDF
No ratings yet
CD Unit-5
15 pages
UNIT IV CD (P)
PDF
No ratings yet
UNIT IV CD (P)
8 pages
Unit 5
PDF
No ratings yet
Unit 5
12 pages
Unit-V Control /data Flow Analysis
PDF
No ratings yet
Unit-V Control /data Flow Analysis
18 pages
Compiler Design UNIT V
PDF
No ratings yet
Compiler Design UNIT V
13 pages
UNIT 5 Notes CD
PDF
No ratings yet
UNIT 5 Notes CD
6 pages
Unit 6 Final
PDF
No ratings yet
Unit 6 Final
11 pages
Unit 4
PDF
No ratings yet
Unit 4
16 pages
Unit - Iv Run Time Storage Organization
PDF
No ratings yet
Unit - Iv Run Time Storage Organization
15 pages
CD Unit-Iv
PDF
No ratings yet
CD Unit-Iv
15 pages
CD Unit V
PDF
No ratings yet
CD Unit V
9 pages
Unit 5 Material
PDF
No ratings yet
Unit 5 Material
12 pages
AT&CD Unit 5
PDF
No ratings yet
AT&CD Unit 5
13 pages
CodeOptimization EnggNotes
PDF
No ratings yet
CodeOptimization EnggNotes
7 pages
Chapter 7 and 8
PDF
No ratings yet
Chapter 7 and 8
5 pages
Compiler Design - Code Optimization
PDF
No ratings yet
Compiler Design - Code Optimization
6 pages
Compiler Design Code Optimization
PDF
No ratings yet
Compiler Design Code Optimization
5 pages