SlideShare a Scribd company logo
1 of 31
Download to read offline
Code Optimization
Prepared By:
Prabhat Shukla
Basic Blocks
Introduction:
■ A basic block is a sequence of consecutive
instructions which are always executed in sequence
without halt or possibilty of branching.
■ The basic block does not have any jump statements
among them.
Basic Blocks(Contd..)
example
Wrong
1. if A<B goto(4)
2. t1=0
3. goto(5)
4. t1=1
Right
1. t1=b + c
2. t2=t1+d
3. a=t2
Algorithms
1. Determine the set of leaders,
a. The first statement is the leader.
b. Any statement that is the target of
a conditional or goto is a leader.
c. Any statement that immediately
follows conditional or goto is a
leader.
2. For each leader, its basic block
consist of the leader and all
statements up to but not including
the next leader or the end of the
program
Consider the following three address
code statements .Compute the basic
blocks.
1) PROD = 0
2) I = 1
3) T2=addr(A)-4
4) T4 =addr(B)-4
5) T1 =4*I
6) T3= T2[T1]
7) T5 = T4[T1]
8) T6 =T3*T5
9) PROD =PROD + T6
10) I =I+1
11) IF I<= 20 GOTO (5)
Example on Basic Blocks
1. a=0;
2. if(a<10) goto(4)
3. goto(9)
4. t1=x+1
5. x=t1
6. t2=a+1
7. a=t2
8. goto (2)
9. exit
1. Determine the set of leaders,
a. The first statement is the leader.
b. Any statement that is the target of
a conditional or goto is a leader.
c. Any statement that immediately
follows conditional or goto is a
leader.
2. For each leader, its basic block
consist of the leader and all
statements up to but not including
the next leader or the end of the
program
Example on Basic Blocks
Flow Graph
Introduction:
■ A flow graph is a graphical representation of a sequence
of instructions with control flow edges.
■ A flow graph can be defined at the intermediate code
level or target code level.
■ The nodes of flow graphs are the basic blocks and flow-
of-control to immediately follow node connected by
directed arrow.
Rules
■ The basic blocks are the nodes to the flow
graph .
■ The block whose leader is the first
statement is called initial block.
■ There is a directed edge from block B1 to B2
if B2 immediately follows B1 in the given
sequence
■ Then we say that B1 is the predecessor of
B2.
Directed Acyclic Graph(DAG)
Definition:-
In compiler design, a directed acyclic graph is
an abstract syntax tree with a unique node for
each value
Use of DAG for optimizing basic blocks
 DAG is a useful data structure for implementing
transformations on basic blocks.
 A basic block can be optimized by the
construction of DAG.
 A DAG can be constructed for a block and certain
transformations such as common subexpression
elimination and dead code elimination can be
applied for performing the local optimization
Application of DAG
 Determining common subexpression
 Determining which name are used in the block
and computed outside the block
 Determining which statements of the block
could have their computed value outside the
block
Rules for construction of DAG
1. In a DAG
a. Leaf nodes represent identifiers, name or constants.
b. Interior nodes represent operators
2. while constructing DAG, there is a check made to find if
there is an existing node with same children. A new node
is created only when such a node does not exist. This
action allows us to detect common sub-expression .
2. The assignment of the form x=y must not be performed
until and unless it is a must.
Construct DAG for the given expression
(a+b)*(a+b+c)
Result
Construct DAG for the given expression
(((a+a)+(a+a))+((a+a)+(a+a)))
Result
Construct DAG for the following basic blocks
a=b*c
d=b
e=d*c
b=e
f=b+c
g=f+d
Draw the DAG representation of
following code
1. s1=4*I
2. s2=addr(A)-4
3. s3=s2[s1]
4. s4=4*I
5. s5=addr(B)-4
6. s6=s5[s4]
7. s7=s3*s6
8. s8= PROD+s7
9. PROD=s8
10. s9=I+1
11. I=s9
12. if I<=20 goto(1)
Code optimization lecture

More Related Content

What's hot

Optimization of basic blocks
Optimization of basic blocksOptimization of basic blocks
Optimization of basic blocksishwarya516
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONAnil Pokhrel
 
Sem 1 mca summer 2014 solved assignments
Sem 1 mca summer 2014  solved assignmentsSem 1 mca summer 2014  solved assignments
Sem 1 mca summer 2014 solved assignmentssmumbahelp
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler designRajkumar R
 
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...Jinho Choi
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow AnalysisEdgar Barbosa
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
17429 computer network
17429 computer network17429 computer network
17429 computer networksoni_nits
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Principles of object oriented programing
Principles of object oriented programingPrinciples of object oriented programing
Principles of object oriented programingAhammed Alamin
 
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...Jinho Choi
 
Bca1040 digital logic
Bca1040  digital logicBca1040  digital logic
Bca1040 digital logicsmumbahelp
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generationIffat Anjum
 

What's hot (20)

Optimization of basic blocks
Optimization of basic blocksOptimization of basic blocks
Optimization of basic blocks
 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTION
 
Sem 1 mca summer 2014 solved assignments
Sem 1 mca summer 2014  solved assignmentsSem 1 mca summer 2014  solved assignments
Sem 1 mca summer 2014 solved assignments
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow Analysis
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
17429 computer network
17429 computer network17429 computer network
17429 computer network
 
Code generator
Code generatorCode generator
Code generator
 
Slide1
Slide1Slide1
Slide1
 
Principles of object oriented programing
Principles of object oriented programingPrinciples of object oriented programing
Principles of object oriented programing
 
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
 
Bca1040 digital logic
Bca1040  digital logicBca1040  digital logic
Bca1040 digital logic
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 

Similar to Code optimization lecture

Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design LogsAk
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphsdaimk2020
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksMohammad Vaseem Akaram
 
vlsi design using verilog presentaion 1
vlsi design using verilog   presentaion 1vlsi design using verilog   presentaion 1
vlsi design using verilog presentaion 1MANDHASAIGOUD1
 
Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Janki Shah
 
Structure of c
Structure of cStructure of c
Structure of cdevauro
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarPRAVIN GHOLKAR
 
VerilogTutorial-101702.ppt
VerilogTutorial-101702.pptVerilogTutorial-101702.ppt
VerilogTutorial-101702.pptPavanBhandari6
 
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptxLecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptxwafawafa52
 
C interview questions
C interview questionsC interview questions
C interview questionsSoba Arjun
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsPraveen Penumathsa
 

Similar to Code optimization lecture (20)

Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design Principal Sources of Optimization in compiler design
Principal Sources of Optimization in compiler design
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocks
 
Bp150520
Bp150520Bp150520
Bp150520
 
ERTS UNIT 3.ppt
ERTS UNIT 3.pptERTS UNIT 3.ppt
ERTS UNIT 3.ppt
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
 
vlsi design using verilog presentaion 1
vlsi design using verilog   presentaion 1vlsi design using verilog   presentaion 1
vlsi design using verilog presentaion 1
 
Lecture03
Lecture03Lecture03
Lecture03
 
Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...Compiler in System Programming/Code Optimization techniques in System Program...
Compiler in System Programming/Code Optimization techniques in System Program...
 
Structure of c
Structure of cStructure of c
Structure of c
 
1.ppt
1.ppt1.ppt
1.ppt
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. Gholkar
 
VerilogTutorial-101702.ppt
VerilogTutorial-101702.pptVerilogTutorial-101702.ppt
VerilogTutorial-101702.ppt
 
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptxLecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
Lecture 16 RC Architecture Types & FPGA Interns Lecturer.pptx
 
C interview questions
C interview questionsC interview questions
C interview questions
 
Slicing of Object-Oriented Programs
Slicing of Object-Oriented ProgramsSlicing of Object-Oriented Programs
Slicing of Object-Oriented Programs
 

Recently uploaded

VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130Suhani Kapoor
 
The Art of Batik, template ppt aesthetic
The Art of Batik, template ppt aestheticThe Art of Batik, template ppt aesthetic
The Art of Batik, template ppt aestheticTiaFebriani
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Servicearoranaina404
 
VVIP Pune Call Girls Dange Chowk (8250192130) Pune Escorts Nearby with Comple...
VVIP Pune Call Girls Dange Chowk (8250192130) Pune Escorts Nearby with Comple...VVIP Pune Call Girls Dange Chowk (8250192130) Pune Escorts Nearby with Comple...
VVIP Pune Call Girls Dange Chowk (8250192130) Pune Escorts Nearby with Comple...Call Girls in Nagpur High Profile
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentationamedia6
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdftbatkhuu1
 
Delhi Call Girls Paharganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Paharganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Paharganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Paharganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...Call Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...Call Girls in Nagpur High Profile
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsCharles Obaleagbon
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Delhi Call girls
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneLukeKholes
 

Recently uploaded (20)

VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
 
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
 
The Art of Batik, template ppt aesthetic
The Art of Batik, template ppt aestheticThe Art of Batik, template ppt aesthetic
The Art of Batik, template ppt aesthetic
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
 
VVIP Pune Call Girls Dange Chowk (8250192130) Pune Escorts Nearby with Comple...
VVIP Pune Call Girls Dange Chowk (8250192130) Pune Escorts Nearby with Comple...VVIP Pune Call Girls Dange Chowk (8250192130) Pune Escorts Nearby with Comple...
VVIP Pune Call Girls Dange Chowk (8250192130) Pune Escorts Nearby with Comple...
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentation
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
 
Delhi Call Girls Paharganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Paharganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Paharganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Paharganj 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past Questions
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, Pune
 

Code optimization lecture

  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11. Basic Blocks Introduction: ■ A basic block is a sequence of consecutive instructions which are always executed in sequence without halt or possibilty of branching. ■ The basic block does not have any jump statements among them.
  • 12. Basic Blocks(Contd..) example Wrong 1. if A<B goto(4) 2. t1=0 3. goto(5) 4. t1=1 Right 1. t1=b + c 2. t2=t1+d 3. a=t2
  • 13. Algorithms 1. Determine the set of leaders, a. The first statement is the leader. b. Any statement that is the target of a conditional or goto is a leader. c. Any statement that immediately follows conditional or goto is a leader. 2. For each leader, its basic block consist of the leader and all statements up to but not including the next leader or the end of the program Consider the following three address code statements .Compute the basic blocks. 1) PROD = 0 2) I = 1 3) T2=addr(A)-4 4) T4 =addr(B)-4 5) T1 =4*I 6) T3= T2[T1] 7) T5 = T4[T1] 8) T6 =T3*T5 9) PROD =PROD + T6 10) I =I+1 11) IF I<= 20 GOTO (5)
  • 14.
  • 15. Example on Basic Blocks 1. a=0; 2. if(a<10) goto(4) 3. goto(9) 4. t1=x+1 5. x=t1 6. t2=a+1 7. a=t2 8. goto (2) 9. exit 1. Determine the set of leaders, a. The first statement is the leader. b. Any statement that is the target of a conditional or goto is a leader. c. Any statement that immediately follows conditional or goto is a leader. 2. For each leader, its basic block consist of the leader and all statements up to but not including the next leader or the end of the program
  • 17. Flow Graph Introduction: ■ A flow graph is a graphical representation of a sequence of instructions with control flow edges. ■ A flow graph can be defined at the intermediate code level or target code level. ■ The nodes of flow graphs are the basic blocks and flow- of-control to immediately follow node connected by directed arrow.
  • 18. Rules ■ The basic blocks are the nodes to the flow graph . ■ The block whose leader is the first statement is called initial block. ■ There is a directed edge from block B1 to B2 if B2 immediately follows B1 in the given sequence ■ Then we say that B1 is the predecessor of B2.
  • 19.
  • 20. Directed Acyclic Graph(DAG) Definition:- In compiler design, a directed acyclic graph is an abstract syntax tree with a unique node for each value
  • 21. Use of DAG for optimizing basic blocks  DAG is a useful data structure for implementing transformations on basic blocks.  A basic block can be optimized by the construction of DAG.  A DAG can be constructed for a block and certain transformations such as common subexpression elimination and dead code elimination can be applied for performing the local optimization
  • 22. Application of DAG  Determining common subexpression  Determining which name are used in the block and computed outside the block  Determining which statements of the block could have their computed value outside the block
  • 23. Rules for construction of DAG 1. In a DAG a. Leaf nodes represent identifiers, name or constants. b. Interior nodes represent operators 2. while constructing DAG, there is a check made to find if there is an existing node with same children. A new node is created only when such a node does not exist. This action allows us to detect common sub-expression . 2. The assignment of the form x=y must not be performed until and unless it is a must.
  • 24. Construct DAG for the given expression (a+b)*(a+b+c)
  • 26. Construct DAG for the given expression (((a+a)+(a+a))+((a+a)+(a+a)))
  • 28. Construct DAG for the following basic blocks a=b*c d=b e=d*c b=e f=b+c g=f+d
  • 29.
  • 30. Draw the DAG representation of following code 1. s1=4*I 2. s2=addr(A)-4 3. s3=s2[s1] 4. s4=4*I 5. s5=addr(B)-4 6. s6=s5[s4] 7. s7=s3*s6 8. s8= PROD+s7 9. PROD=s8 10. s9=I+1 11. I=s9 12. if I<=20 goto(1)