SlideShare a Scribd company logo
1 of 40
CH10.2
CSE
4100
OverviewOverview
 Code Level OptimizationCode Level Optimization
 Common Sub-expression elimination
 Copy Propagation
 Dead-code elimination
 Peephole optimizationPeephole optimization
 Load/Store elimination
 Unreachable code
 Flow of Control Optimization
 Algebraic simplification
 Strength Reduction
CH10.4
CSE
4100
Where can Optimation Occur?Where can Optimation Occur?
 Software Engineer can:Software Engineer can:
 Profile Program
 Change Algorithm Data
 Transform/Improve Loops
Front End
LA, Parse,
Int. Code
Code
Generator
Int. Code Target
Program
Source
Program
 Compiler Can:Compiler Can:
 Improve Loops/Proc
Calls
 Calculate Addresses
 Use Registers
 Selected Instructions
 Perform Peephole Opt.
 All are OptimizationsAll are Optimizations
 1st
is User Controlled and Defined
 At Intermediate Code Level by Compiler
 At Assembly Level for Target Architecture (to take
advantage of different machine features)
CH10.6
CSE
4100
First Look at OptimizationFirst Look at Optimization
 Optimization Applied to 3 Address Coding (3AC)Optimization Applied to 3 Address Coding (3AC)
Version of Source Program - Examples:Version of Source Program - Examples:
CH10.7
CSE
4100
First Look at OptimizationFirst Look at Optimization
 Identify each Basic Block which
Represents a set of Three Address
Statements where
Execution Enters at Top and Leaves
at Bottom
No Branches within Code
 Represent the Control Flow
Dependencies Among and Between
Basic Blocks
Defines what is Termed a “Flow
Graph”
CH10.8
CSE
4100
First Look at OptimizationFirst Look at Optimization
 Steps 1 to 12 from two Slides Back Represented as:Steps 1 to 12 from two Slides Back Represented as:
 Optimization Works with Basic Blocks and FlowOptimization Works with Basic Blocks and Flow
Graph to Perform Transformations that:Graph to Perform Transformations that:
CH10.9
CSE
4100
First Look at OptimizationFirst Look at Optimization
 Optimization will PerformOptimization will Perform
Transformations on BasicTransformations on Basic
Blocks/Flow GraphBlocks/Flow Graph
 Resulting Graph(s) PassedResulting Graph(s) Passed
Through to Final Code GenerationThrough to Final Code Generation
to Obtain More Optimal Codeto Obtain More Optimal Code
 Two Fold Goal of OptimizationTwo Fold Goal of Optimization
Reduce Time
Reduce Space
CH10.10
CSE
4100
First Look at OptimizationFirst Look at Optimization
 Two Types of TransformationsTwo Types of Transformations
 Structure Preserving
Inherent Structure and Implicit
Functionality of Basic Blocks is
Unchanged
 Algebraic
Elimination of Useless Expressions
x = x + 0 or y = y * 1
Replace Expensive Operators
Change x = y ** 2 to x = y * y
CH10.11
CSE
4100
Structure Preserving TransformationsStructure Preserving Transformations
 Common Sub-Expression EliminationCommon Sub-Expression Elimination
 How can Following Code be Improved?
a = b + c
b = a – d
c = b + c
d = a – d
 What Must Make Sure Doesn’t happen?
 Dead-Code EliminationDead-Code Elimination
 If x is not Used in Block, Can it be
Removed?
x = y + z
d = b
CH10.12
CSE
4100
Structure Preserving TransformationsStructure Preserving Transformations
 Renaming Temporary VariablesRenaming Temporary Variables
 Consider the code t = b + c
 Can be Changed to u = b + c
 May Reduce the Number of temporaries
 Make Change from all t’s to all u’s
 Interchange of StatementsInterchange of Statements
 Consider and Change to:
t1 = b + c t2 = x + y
t2 = x + y t1 = b + c
 This can Occur as Long as:
 x and y not t1
 b and c not t2
CH10.13
CSE
4100
Requirements for OptimizationRequirements for Optimization
CH10.14
CSE
4100
The Overall Optimization ProcessThe Overall Optimization Process
 AdvantagesAdvantages
 Intermediate Code has Explicit Operations and Their
Identification Promotes Optimization
 Intermediate Code is Relatively Machine Independent
 Therefore, Optimization Doesn’t Impact Final Code
Generation
CH10.15
CSE
4100
Example Source CodeExample Source Code
CH10.16
CSE
4100
Generated Three Address CodingGenerated Three Address Coding
CH10.17
CSE
4100
Flow Graph of Basic BlocksFlow Graph of Basic Blocks
CH10.18
CSE
4100
Indepth Examination of OptimizationIndepth Examination of Optimization
 Code-Transformation Techniques:Code-Transformation Techniques:
 Local – within a “Basic Block”
 Global – between “Basic Blocks”
 Data Flow Dependencies Determined byData Flow Dependencies Determined by
InspectionInspection
what do i, a, and v refer to?what do i, a, and v refer to?
 Dependent in Another Basic BlockDependent in Another Basic Block
 Scoping is Very CriticalScoping is Very Critical
CH10.19
CSE
4100
Indepth Examination of OptimizationIndepth Examination of Optimization
 Function Preserving TransformationsFunction Preserving Transformations
 Common Subexpressions
 Copy Propagation
 Deal Code Elimination
 Constant Folding
 Loop OptimizationsLoop Optimizations
 Code Motion
 Induction Variables
 Strength Reduction
CH10.20
CSE
4100
Common Sub-ExpressionsCommon Sub-Expressions
 E is a Common Sub-Expression ifE is a Common Sub-Expression if
 E as Previously Computed
 Value of E Unchanged since Previous
Coamputation
 What Can be Saved in B5?What Can be Saved in B5?
 t6 and t7 same computation
 t8 and t10 same computation
 Save:
 Remove 2 temp variables
 Remove 2 multiplications
 Remove 4 variable accesses
 Remove 2 assignments
t6 := 4 * i
x := a[t6]
t7 := 4 * i
t8 := 4 * j
t9 := a[t8]
a[t7] := t9
t10 := 4 * j
a[t10]:= x
Goto B2
CH10.21
CSE
4100
Common Sub-ExpressionsCommon Sub-Expressions
 What about B6?What about B6?
 t11 and t12
 t13 and t15
 Similar Savings as in B5Similar Savings as in B5
t11 := 4 * i
x := a[t11]
t12 := 4 * i
t13 := 4 * n
t14 := a[t13]
a[t12]:= t14
t15 := 4 * n
a[t15]:= x
t11 := 4 * i
x := a[t11]
t13 := 4 * n
t14 := a[t13]
a[t11]:= t14
a[t13]:= x
CH10.22
CSE
4100
Common Sub-ExpressionsCommon Sub-Expressions
 What else Can be Accomplished?What else Can be Accomplished?
 Where is Variable j Determined?Where is Variable j Determined?
 In B3 – and when drop
through B3 to B4 and into B5,
no change occurs to j!
 What Does B5 Become?What Does B5 Become?
 t9 same as t5!t9 same as t5!
 Again savings in access,Again savings in access,
variables, operations, etc.variables, operations, etc.
t6 := 4 * i
x := a[t6]
t8 := 4 * j
t9 := a[t8]
a[t6] := t9
a[t8]:= x
Goto B2
j := j - 1
t4 := 4 * j
t5 := a[t4]
if t5>4 goto B3
B4
t6 := 4 * i
x := a[t6]
t9 := a[t4]
a[t6] := t9
a[t4]:= x
Goto B2
t6 := 4 * i
x := a[t6]
a[t6] := t5
a[t4]:= x
Goto B2
CH10.24
CSE
4100
Common Sub-ExpressionsCommon Sub-Expressions
 B6 is Similarly Changed ….B6 is Similarly Changed ….
t11 := 4 * i
x := a[t11]
t13 := 4 * n
t14 := a[t13]
a[t11]:= t14
a[t13]:= x
x := t3
t14 := a[t1]
a[t2]:= t14
a[t1]:= x
CH10.25
CSE
4100
Resulting Flow DiagramResulting Flow Diagram
CH10.26
CSE
4100
Copy PropagationCopy Propagation
 Introduce a Common Copy Statement toIntroduce a Common Copy Statement to
Replace an Arithmetic Calculation withReplace an Arithmetic Calculation with
AssignmentAssignment
 Regardless of the Path Chosen, the use ofRegardless of the Path Chosen, the use of
an Assignment Saves Time and Spacean Assignment Saves Time and Space
a:= d + e
a:= t
a:= d + e b:= d + e
c:= d + e
b:= d + e
a:= t
c:= t
CH10.27
CSE
4100
Copy PropagationCopy Propagation
 In our Example for B5 and B6 Below:In our Example for B5 and B6 Below:
 Since x is t3, we can replace the use of x on right handSince x is t3, we can replace the use of x on right hand
side as below:side as below:
x := t3
t14 := a[t1]
a[t2]:= t14
a[t1]:= x
x := t3
a[t2] := t5
a[t4]:= x
Goto B2
x := t3
t14 := a[t1]
a[t2] := t14
a[t1] := t3
x := t3
a[t2] := t5
a[t4] := t3
Goto B2
CH10.28
CSE
4100
Dead Code EliminationDead Code Elimination
 Variable is “Dead” if its Value will never be UtilizedVariable is “Dead” if its Value will never be Utilized
Again SubsequentlyAgain Subsequently
 Otherwise, Variable is “Live”Otherwise, Variable is “Live”
 What’s True about B5 and B6?What’s True about B5 and B6?
 Can Any Statements be Eliminated? Which Ones?Can Any Statements be Eliminated? Which Ones?
Why?Why?
 B5 and B6 are Now Optimized withB5 and B6 are Now Optimized with
 B5 has 9 Statements Reduced to 3
 B56 has 8 Statements Reduced to 3
x := t3
t14 := a[t1]
a[t2] := t14
a[t1] := t3
x := t3
a[t2] := t5
a[t4] := t3
Goto B2
CH10.29
CSE
4100
Loop OptimizationsLoop Optimizations
 Three Types: Code Motion, Induction Variables, andThree Types: Code Motion, Induction Variables, and
Strength ReductionStrength Reduction
 Code MotionCode Motion
 Remove Invariant Operations from Loop
while (limit * 2 > i) do
 Replaced by:
t = limit * 2
while (t > i) do
 Induction VariablesInduction Variables
 Identify Which Variables are Interdependent or in
Step
j = j – 1
t4 = 4 * j
 Replaced by below with an initialization of t4
t4 = t4 - 4
CH10.30
CSE
4100
Loop OptimizationsLoop Optimizations
 Strength ReductionStrength Reduction
 Replace an Expensive Operation (Such as
Multiply) with a Cheaper Operation (Such as Add)
 In B4, I and j can be replaced with t2 and t4
 This Eliminates the need for Variables i and j
CH10.31
CSE
4100
Final Optimized Flow GraphFinal Optimized Flow Graph
CH10.43
CSE
4100
Peephole OptimizationPeephole Optimization
 Simple IdeaSimple Idea
 Slide a window over the code
 Optimize code in the window only.
 Optimizations areOptimizations are
 Local
 Semantic preserving
 Cheap to implement
 UsuallyUsually
 One can repeat the peephole several times!
 Each pass can create new opportunities for
more
CH10.44
CSE
4100
Peephole OptimizerPeephole Optimizer
block_3:
mov [esp-4],ebp
mov ebp,esp
mov [ebp-8],esp
sub esp,28
mov eax,[ebp+8]
cmp eax,0
mov eax,0
sete ah
cmp eax,0
jz block_5
block_4:
mov eax,1
jmp block_6
block_5:
mov eax,[ebp+8]
sub eax,1
push eax
mov eax,[ebp+4]
push eax
mov eax,[eax]
mov eax,[eax]
call eax
add esp,8
mov ebx,[ebp+8]
imul ebx,eax
block_3:
mov [esp-4],ebp
mov ebp,esp
mov [ebp-8],esp
sub esp,28
mov eax,[ebp+8]
cmp eax,0
mov eax,0
sete ah
cmp eax,0
jz block_5
block_4:
mov eax,1
jmp block_6
block_5:
mov eax,[ebp+8]
sub eax,1
push eax
mov eax,[ebp+4]
push eax
mov eax,[eax]
mov eax,[eax]
call eax
add esp,8
mov ebx,[ebp+8]
imul ebx,eax
CH10.45
CSE
4100
Peephole OptimizationsPeephole Optimizations
 Load/Store elimination
 Get rid of redundant operations
 Unreachable code
 Get rid of code guaranteed to never execute
 Flow of Control Optimization
 Simply jump sequences.
 Algebraic simplification
 Use rules of algebra to rewrite some basic operation
 Strength Reduction
 Replace expensive instructions by equivalent ones (yet
cheaper)
 Machine Idioms
 Replace expensive instructions by equivalent ones (for a
given machine)
CH10.46
CSE
4100
Load / Store SequencesLoad / Store Sequences
 Imagine the following sequenceImagine the following sequence
 “a” is a label for a memory location
mov
a,eax
mov
eax,a
mov
a,eax
mov
eax,a
CH10.47
CSE
4100
Unreachable CodeUnreachable Code
 ExampleExample
#define debug 0
....
if (debug) {
printf(“This is a trace
messagen”);
}
....
#define debug 0
....
if (debug) {
printf(“This is a trace
messagen”);
}
....
CH10.48
CSE
4100
ExampleExample
 The Generated code looks like....The Generated code looks like....
 If we know that...If we know that...
 debug == 0
 Then
....
if (debug == 0) goto L2
printf(“This is a trace
messagen”);
L2: ....
....
if (debug == 0) goto L2
printf(“This is a trace
messagen”);
L2: ....
....
if (0 == 0) goto L2
printf(“This is a trace
messagen”);
L2: ....
....
if (0 == 0) goto L2
printf(“This is a trace
messagen”);
L2: ....
1
CH10.49
CSE
4100
ExampleExample
 Final transformationFinal transformation
 Given this codeGiven this code
 There is no way to branch “into” the blue block
 The last instruction (goto L2) jumps over the blue
block
 The blue block is never used. Get rid of it!
....
goto L2
printf(“This is a trace
messagen”);
L2: ....
....
goto L2
printf(“This is a trace
messagen”);
L2: ....
CH10.50
CSE
4100
Unreachable Code ExampleUnreachable Code Example
 Bottom LineBottom Line
 Now L2 is instruction after goto...Now L2 is instruction after goto...
 So get rid of goto altogether!
....
goto L2
L2: ....
....
goto L2
L2: ....
....
L2: ....
....
L2: ....
CH10.51
CSE
4100
Flow of Control OptimizationFlow of Control Optimization
 SituationSituation
 We can have chains of jumps
 Direct to conditional or vice-versa
 ObjectiveObjective
 Avoid extra jumps.
 ExampleExample
if (x relop y) goto L2
....
L2: goto L4
L3: ....
L4:
L4_BLOCK
if (x relop y) goto L2
....
L2: goto L4
L3: ....
L4:
L4_BLOCK
CH10.52
CSE
4100
Flow of ControlFlow of Control
 What can be doneWhat can be done
 Collapse the chain
if (x relop y) goto L4
....
L2: goto L4
L3: ....
L4:
L4_BLOCK
if (x relop y) goto L4
....
L2: goto L4
L3: ....
L4:
L4_BLOCK
CH10.53
CSE
4100
Algebraic SimplificationAlgebraic Simplification
 Simple IdeaSimple Idea
 Use algebraic rules to rewrite some code
 ExamplesExamples
x := y + 0x := y + 0
x := yx := y
x := y *
1
x := y *
1
x := yx := y
x := y *
0
x := y *
0
x := 0x := 0
CH10.54
CSE
4100
Strength ReductionStrength Reduction
 IdeaIdea
 Replace expensive operation
 By semantically equivalent cheaper ones.
 ExamplesExamples
 Multiplication by 2 is equivalent to a left shift
 Left shift is much faster
CH10.55
CSE
4100
Hardware IdiomHardware Idiom
 IdeaIdea
 Replace expensive instructions by...
 Equivalent instruction that are optimized for the
platform
 ExampleExample
add eax,1add eax,1
inc
eax
inc
eax

More Related Content

What's hot

Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow GraphsJenny Galino
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler designAnul Chaudhary
 
Advanced Operating System Lecture Notes
Advanced Operating System Lecture NotesAdvanced Operating System Lecture Notes
Advanced Operating System Lecture NotesAnirudhan Guru
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Cache memory principles
Cache memory principlesCache memory principles
Cache memory principlesbit allahabad
 
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
 
Translation of expression(copmiler construction)
Translation of expression(copmiler construction)Translation of expression(copmiler construction)
Translation of expression(copmiler construction)IrtazaAfzal3
 

What's hot (20)

Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Basic Blocks and Flow Graphs
Basic Blocks and Flow GraphsBasic Blocks and Flow Graphs
Basic Blocks and Flow Graphs
 
Compiler Design Unit 5
Compiler Design Unit 5Compiler Design Unit 5
Compiler Design Unit 5
 
Graphs
GraphsGraphs
Graphs
 
Code optimization
Code optimizationCode optimization
Code optimization
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Dataflow Analysis
Dataflow AnalysisDataflow Analysis
Dataflow Analysis
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Advanced Operating System Lecture Notes
Advanced Operating System Lecture NotesAdvanced Operating System Lecture Notes
Advanced Operating System Lecture Notes
 
Code generator
Code generatorCode generator
Code generator
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Cache memory principles
Cache memory principlesCache memory principles
Cache memory principles
 
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
 
Translation of expression(copmiler construction)
Translation of expression(copmiler construction)Translation of expression(copmiler construction)
Translation of expression(copmiler construction)
 

Similar to Code optimisation presnted

CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptssuser0be977
 
Cost theory analysis
Cost theory analysisCost theory analysis
Cost theory analysisNasful Prince
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languagesAnkit Pandey
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimizationZongYing Lyu
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdfSemsemSameer1
 
Bis 311 final examination answers
Bis 311 final examination answersBis 311 final examination answers
Bis 311 final examination answersRandalHoffman
 
Assembly Language Compiler Implementation
Assembly Language Compiler ImplementationAssembly Language Compiler Implementation
Assembly Language Compiler ImplementationRAVI TEJA KOMMA
 
Compiler presention
Compiler presentionCompiler presention
Compiler presentionFaria Priya
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler designhmnasim15
 
2.overview of c#
2.overview of c#2.overview of c#
2.overview of c#Raghu nath
 
Instruction_Set.pdf
Instruction_Set.pdfInstruction_Set.pdf
Instruction_Set.pdfboukomra
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
Code Tuning
Code TuningCode Tuning
Code Tuningbgtraghu
 
decoder and encoder
 decoder and encoder decoder and encoder
decoder and encoderUnsa Shakir
 
Unit 2 c programming_basics
Unit 2 c programming_basicsUnit 2 c programming_basics
Unit 2 c programming_basicskirthika jeyenth
 

Similar to Code optimisation presnted (20)

Code optimization
Code optimizationCode optimization
Code optimization
 
Code Optimization.ppt
Code Optimization.pptCode Optimization.ppt
Code Optimization.ppt
 
CS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.pptCS540-2-lecture11 - Copy.ppt
CS540-2-lecture11 - Copy.ppt
 
Cost theory analysis
Cost theory analysisCost theory analysis
Cost theory analysis
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Three Band EQ Design Report
Three Band EQ Design ReportThree Band EQ Design Report
Three Band EQ Design Report
 
On using BS to improve the
On using BS to improve theOn using BS to improve the
On using BS to improve the
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
 
Bis 311 final examination answers
Bis 311 final examination answersBis 311 final examination answers
Bis 311 final examination answers
 
Assembly Language Compiler Implementation
Assembly Language Compiler ImplementationAssembly Language Compiler Implementation
Assembly Language Compiler Implementation
 
Compiler presention
Compiler presentionCompiler presention
Compiler presention
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
2.overview of c#
2.overview of c#2.overview of c#
2.overview of c#
 
Instruction_Set.pdf
Instruction_Set.pdfInstruction_Set.pdf
Instruction_Set.pdf
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
decoder and encoder
 decoder and encoder decoder and encoder
decoder and encoder
 
Cpu unit
Cpu unitCpu unit
Cpu unit
 
Unit 2 c programming_basics
Unit 2 c programming_basicsUnit 2 c programming_basics
Unit 2 c programming_basics
 

More from bhavanatmithun

More from bhavanatmithun (13)

computer networks
computer networkscomputer networks
computer networks
 
Deadlock
DeadlockDeadlock
Deadlock
 
krisibhavan site
krisibhavan sitekrisibhavan site
krisibhavan site
 
User server interaction
User server interactionUser server interaction
User server interaction
 
Types of cn, protocols and standards
Types of cn, protocols and standardsTypes of cn, protocols and standards
Types of cn, protocols and standards
 
Snmp
SnmpSnmp
Snmp
 
Iso osi and tcp-ip reference models
Iso osi and tcp-ip reference modelsIso osi and tcp-ip reference models
Iso osi and tcp-ip reference models
 
HTTP
HTTPHTTP
HTTP
 
FTP
FTPFTP
FTP
 
DNS
DNSDNS
DNS
 
application layer protocols
application layer protocolsapplication layer protocols
application layer protocols
 
Group communication
Group communicationGroup communication
Group communication
 
Group communication
Group communicationGroup communication
Group communication
 

Recently uploaded

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 

Recently uploaded (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 

Code optimisation presnted

  • 1. CH10.2 CSE 4100 OverviewOverview  Code Level OptimizationCode Level Optimization  Common Sub-expression elimination  Copy Propagation  Dead-code elimination  Peephole optimizationPeephole optimization  Load/Store elimination  Unreachable code  Flow of Control Optimization  Algebraic simplification  Strength Reduction
  • 2. CH10.4 CSE 4100 Where can Optimation Occur?Where can Optimation Occur?  Software Engineer can:Software Engineer can:  Profile Program  Change Algorithm Data  Transform/Improve Loops Front End LA, Parse, Int. Code Code Generator Int. Code Target Program Source Program  Compiler Can:Compiler Can:  Improve Loops/Proc Calls  Calculate Addresses  Use Registers  Selected Instructions  Perform Peephole Opt.  All are OptimizationsAll are Optimizations  1st is User Controlled and Defined  At Intermediate Code Level by Compiler  At Assembly Level for Target Architecture (to take advantage of different machine features)
  • 3. CH10.6 CSE 4100 First Look at OptimizationFirst Look at Optimization  Optimization Applied to 3 Address Coding (3AC)Optimization Applied to 3 Address Coding (3AC) Version of Source Program - Examples:Version of Source Program - Examples:
  • 4. CH10.7 CSE 4100 First Look at OptimizationFirst Look at Optimization  Identify each Basic Block which Represents a set of Three Address Statements where Execution Enters at Top and Leaves at Bottom No Branches within Code  Represent the Control Flow Dependencies Among and Between Basic Blocks Defines what is Termed a “Flow Graph”
  • 5. CH10.8 CSE 4100 First Look at OptimizationFirst Look at Optimization  Steps 1 to 12 from two Slides Back Represented as:Steps 1 to 12 from two Slides Back Represented as:  Optimization Works with Basic Blocks and FlowOptimization Works with Basic Blocks and Flow Graph to Perform Transformations that:Graph to Perform Transformations that:
  • 6. CH10.9 CSE 4100 First Look at OptimizationFirst Look at Optimization  Optimization will PerformOptimization will Perform Transformations on BasicTransformations on Basic Blocks/Flow GraphBlocks/Flow Graph  Resulting Graph(s) PassedResulting Graph(s) Passed Through to Final Code GenerationThrough to Final Code Generation to Obtain More Optimal Codeto Obtain More Optimal Code  Two Fold Goal of OptimizationTwo Fold Goal of Optimization Reduce Time Reduce Space
  • 7. CH10.10 CSE 4100 First Look at OptimizationFirst Look at Optimization  Two Types of TransformationsTwo Types of Transformations  Structure Preserving Inherent Structure and Implicit Functionality of Basic Blocks is Unchanged  Algebraic Elimination of Useless Expressions x = x + 0 or y = y * 1 Replace Expensive Operators Change x = y ** 2 to x = y * y
  • 8. CH10.11 CSE 4100 Structure Preserving TransformationsStructure Preserving Transformations  Common Sub-Expression EliminationCommon Sub-Expression Elimination  How can Following Code be Improved? a = b + c b = a – d c = b + c d = a – d  What Must Make Sure Doesn’t happen?  Dead-Code EliminationDead-Code Elimination  If x is not Used in Block, Can it be Removed? x = y + z d = b
  • 9. CH10.12 CSE 4100 Structure Preserving TransformationsStructure Preserving Transformations  Renaming Temporary VariablesRenaming Temporary Variables  Consider the code t = b + c  Can be Changed to u = b + c  May Reduce the Number of temporaries  Make Change from all t’s to all u’s  Interchange of StatementsInterchange of Statements  Consider and Change to: t1 = b + c t2 = x + y t2 = x + y t1 = b + c  This can Occur as Long as:  x and y not t1  b and c not t2
  • 11. CH10.14 CSE 4100 The Overall Optimization ProcessThe Overall Optimization Process  AdvantagesAdvantages  Intermediate Code has Explicit Operations and Their Identification Promotes Optimization  Intermediate Code is Relatively Machine Independent  Therefore, Optimization Doesn’t Impact Final Code Generation
  • 13. CH10.16 CSE 4100 Generated Three Address CodingGenerated Three Address Coding
  • 14. CH10.17 CSE 4100 Flow Graph of Basic BlocksFlow Graph of Basic Blocks
  • 15. CH10.18 CSE 4100 Indepth Examination of OptimizationIndepth Examination of Optimization  Code-Transformation Techniques:Code-Transformation Techniques:  Local – within a “Basic Block”  Global – between “Basic Blocks”  Data Flow Dependencies Determined byData Flow Dependencies Determined by InspectionInspection what do i, a, and v refer to?what do i, a, and v refer to?  Dependent in Another Basic BlockDependent in Another Basic Block  Scoping is Very CriticalScoping is Very Critical
  • 16. CH10.19 CSE 4100 Indepth Examination of OptimizationIndepth Examination of Optimization  Function Preserving TransformationsFunction Preserving Transformations  Common Subexpressions  Copy Propagation  Deal Code Elimination  Constant Folding  Loop OptimizationsLoop Optimizations  Code Motion  Induction Variables  Strength Reduction
  • 17. CH10.20 CSE 4100 Common Sub-ExpressionsCommon Sub-Expressions  E is a Common Sub-Expression ifE is a Common Sub-Expression if  E as Previously Computed  Value of E Unchanged since Previous Coamputation  What Can be Saved in B5?What Can be Saved in B5?  t6 and t7 same computation  t8 and t10 same computation  Save:  Remove 2 temp variables  Remove 2 multiplications  Remove 4 variable accesses  Remove 2 assignments t6 := 4 * i x := a[t6] t7 := 4 * i t8 := 4 * j t9 := a[t8] a[t7] := t9 t10 := 4 * j a[t10]:= x Goto B2
  • 18. CH10.21 CSE 4100 Common Sub-ExpressionsCommon Sub-Expressions  What about B6?What about B6?  t11 and t12  t13 and t15  Similar Savings as in B5Similar Savings as in B5 t11 := 4 * i x := a[t11] t12 := 4 * i t13 := 4 * n t14 := a[t13] a[t12]:= t14 t15 := 4 * n a[t15]:= x t11 := 4 * i x := a[t11] t13 := 4 * n t14 := a[t13] a[t11]:= t14 a[t13]:= x
  • 19. CH10.22 CSE 4100 Common Sub-ExpressionsCommon Sub-Expressions  What else Can be Accomplished?What else Can be Accomplished?  Where is Variable j Determined?Where is Variable j Determined?  In B3 – and when drop through B3 to B4 and into B5, no change occurs to j!  What Does B5 Become?What Does B5 Become?  t9 same as t5!t9 same as t5!  Again savings in access,Again savings in access, variables, operations, etc.variables, operations, etc. t6 := 4 * i x := a[t6] t8 := 4 * j t9 := a[t8] a[t6] := t9 a[t8]:= x Goto B2 j := j - 1 t4 := 4 * j t5 := a[t4] if t5>4 goto B3 B4 t6 := 4 * i x := a[t6] t9 := a[t4] a[t6] := t9 a[t4]:= x Goto B2 t6 := 4 * i x := a[t6] a[t6] := t5 a[t4]:= x Goto B2
  • 20. CH10.24 CSE 4100 Common Sub-ExpressionsCommon Sub-Expressions  B6 is Similarly Changed ….B6 is Similarly Changed …. t11 := 4 * i x := a[t11] t13 := 4 * n t14 := a[t13] a[t11]:= t14 a[t13]:= x x := t3 t14 := a[t1] a[t2]:= t14 a[t1]:= x
  • 22. CH10.26 CSE 4100 Copy PropagationCopy Propagation  Introduce a Common Copy Statement toIntroduce a Common Copy Statement to Replace an Arithmetic Calculation withReplace an Arithmetic Calculation with AssignmentAssignment  Regardless of the Path Chosen, the use ofRegardless of the Path Chosen, the use of an Assignment Saves Time and Spacean Assignment Saves Time and Space a:= d + e a:= t a:= d + e b:= d + e c:= d + e b:= d + e a:= t c:= t
  • 23. CH10.27 CSE 4100 Copy PropagationCopy Propagation  In our Example for B5 and B6 Below:In our Example for B5 and B6 Below:  Since x is t3, we can replace the use of x on right handSince x is t3, we can replace the use of x on right hand side as below:side as below: x := t3 t14 := a[t1] a[t2]:= t14 a[t1]:= x x := t3 a[t2] := t5 a[t4]:= x Goto B2 x := t3 t14 := a[t1] a[t2] := t14 a[t1] := t3 x := t3 a[t2] := t5 a[t4] := t3 Goto B2
  • 24. CH10.28 CSE 4100 Dead Code EliminationDead Code Elimination  Variable is “Dead” if its Value will never be UtilizedVariable is “Dead” if its Value will never be Utilized Again SubsequentlyAgain Subsequently  Otherwise, Variable is “Live”Otherwise, Variable is “Live”  What’s True about B5 and B6?What’s True about B5 and B6?  Can Any Statements be Eliminated? Which Ones?Can Any Statements be Eliminated? Which Ones? Why?Why?  B5 and B6 are Now Optimized withB5 and B6 are Now Optimized with  B5 has 9 Statements Reduced to 3  B56 has 8 Statements Reduced to 3 x := t3 t14 := a[t1] a[t2] := t14 a[t1] := t3 x := t3 a[t2] := t5 a[t4] := t3 Goto B2
  • 25. CH10.29 CSE 4100 Loop OptimizationsLoop Optimizations  Three Types: Code Motion, Induction Variables, andThree Types: Code Motion, Induction Variables, and Strength ReductionStrength Reduction  Code MotionCode Motion  Remove Invariant Operations from Loop while (limit * 2 > i) do  Replaced by: t = limit * 2 while (t > i) do  Induction VariablesInduction Variables  Identify Which Variables are Interdependent or in Step j = j – 1 t4 = 4 * j  Replaced by below with an initialization of t4 t4 = t4 - 4
  • 26. CH10.30 CSE 4100 Loop OptimizationsLoop Optimizations  Strength ReductionStrength Reduction  Replace an Expensive Operation (Such as Multiply) with a Cheaper Operation (Such as Add)  In B4, I and j can be replaced with t2 and t4  This Eliminates the need for Variables i and j
  • 27. CH10.31 CSE 4100 Final Optimized Flow GraphFinal Optimized Flow Graph
  • 28. CH10.43 CSE 4100 Peephole OptimizationPeephole Optimization  Simple IdeaSimple Idea  Slide a window over the code  Optimize code in the window only.  Optimizations areOptimizations are  Local  Semantic preserving  Cheap to implement  UsuallyUsually  One can repeat the peephole several times!  Each pass can create new opportunities for more
  • 29. CH10.44 CSE 4100 Peephole OptimizerPeephole Optimizer block_3: mov [esp-4],ebp mov ebp,esp mov [ebp-8],esp sub esp,28 mov eax,[ebp+8] cmp eax,0 mov eax,0 sete ah cmp eax,0 jz block_5 block_4: mov eax,1 jmp block_6 block_5: mov eax,[ebp+8] sub eax,1 push eax mov eax,[ebp+4] push eax mov eax,[eax] mov eax,[eax] call eax add esp,8 mov ebx,[ebp+8] imul ebx,eax block_3: mov [esp-4],ebp mov ebp,esp mov [ebp-8],esp sub esp,28 mov eax,[ebp+8] cmp eax,0 mov eax,0 sete ah cmp eax,0 jz block_5 block_4: mov eax,1 jmp block_6 block_5: mov eax,[ebp+8] sub eax,1 push eax mov eax,[ebp+4] push eax mov eax,[eax] mov eax,[eax] call eax add esp,8 mov ebx,[ebp+8] imul ebx,eax
  • 30. CH10.45 CSE 4100 Peephole OptimizationsPeephole Optimizations  Load/Store elimination  Get rid of redundant operations  Unreachable code  Get rid of code guaranteed to never execute  Flow of Control Optimization  Simply jump sequences.  Algebraic simplification  Use rules of algebra to rewrite some basic operation  Strength Reduction  Replace expensive instructions by equivalent ones (yet cheaper)  Machine Idioms  Replace expensive instructions by equivalent ones (for a given machine)
  • 31. CH10.46 CSE 4100 Load / Store SequencesLoad / Store Sequences  Imagine the following sequenceImagine the following sequence  “a” is a label for a memory location mov a,eax mov eax,a mov a,eax mov eax,a
  • 32. CH10.47 CSE 4100 Unreachable CodeUnreachable Code  ExampleExample #define debug 0 .... if (debug) { printf(“This is a trace messagen”); } .... #define debug 0 .... if (debug) { printf(“This is a trace messagen”); } ....
  • 33. CH10.48 CSE 4100 ExampleExample  The Generated code looks like....The Generated code looks like....  If we know that...If we know that...  debug == 0  Then .... if (debug == 0) goto L2 printf(“This is a trace messagen”); L2: .... .... if (debug == 0) goto L2 printf(“This is a trace messagen”); L2: .... .... if (0 == 0) goto L2 printf(“This is a trace messagen”); L2: .... .... if (0 == 0) goto L2 printf(“This is a trace messagen”); L2: .... 1
  • 34. CH10.49 CSE 4100 ExampleExample  Final transformationFinal transformation  Given this codeGiven this code  There is no way to branch “into” the blue block  The last instruction (goto L2) jumps over the blue block  The blue block is never used. Get rid of it! .... goto L2 printf(“This is a trace messagen”); L2: .... .... goto L2 printf(“This is a trace messagen”); L2: ....
  • 35. CH10.50 CSE 4100 Unreachable Code ExampleUnreachable Code Example  Bottom LineBottom Line  Now L2 is instruction after goto...Now L2 is instruction after goto...  So get rid of goto altogether! .... goto L2 L2: .... .... goto L2 L2: .... .... L2: .... .... L2: ....
  • 36. CH10.51 CSE 4100 Flow of Control OptimizationFlow of Control Optimization  SituationSituation  We can have chains of jumps  Direct to conditional or vice-versa  ObjectiveObjective  Avoid extra jumps.  ExampleExample if (x relop y) goto L2 .... L2: goto L4 L3: .... L4: L4_BLOCK if (x relop y) goto L2 .... L2: goto L4 L3: .... L4: L4_BLOCK
  • 37. CH10.52 CSE 4100 Flow of ControlFlow of Control  What can be doneWhat can be done  Collapse the chain if (x relop y) goto L4 .... L2: goto L4 L3: .... L4: L4_BLOCK if (x relop y) goto L4 .... L2: goto L4 L3: .... L4: L4_BLOCK
  • 38. CH10.53 CSE 4100 Algebraic SimplificationAlgebraic Simplification  Simple IdeaSimple Idea  Use algebraic rules to rewrite some code  ExamplesExamples x := y + 0x := y + 0 x := yx := y x := y * 1 x := y * 1 x := yx := y x := y * 0 x := y * 0 x := 0x := 0
  • 39. CH10.54 CSE 4100 Strength ReductionStrength Reduction  IdeaIdea  Replace expensive operation  By semantically equivalent cheaper ones.  ExamplesExamples  Multiplication by 2 is equivalent to a left shift  Left shift is much faster
  • 40. CH10.55 CSE 4100 Hardware IdiomHardware Idiom  IdeaIdea  Replace expensive instructions by...  Equivalent instruction that are optimized for the platform  ExampleExample add eax,1add eax,1 inc eax inc eax