SlideShare a Scribd company logo
1 of 16
Compiler Optimization
Speaker :呂宗螢
Adviser :梁文耀 老師
Date : 2006/10/11
Embedded and Parallel Systems Lab2
The Structure of Recent
Front end per
language
High-level
optimizations
Global optimizer
Code generator
Intermediate
representation
Dependencies Function
Language Dependent machine
independent
Transform language to
common intermediate form
Largely machine independent For example, procedure
inlineing (also called
procedure intergration)
Small language dependencies
machine dependencies slight
(e.g., register count/types)
Including global and local
optimizations + register
allocation
Highly machine dependent
Language independent
Detailed instruction selection
and machine-dependent
optimizations
May include or be followed
by assembler
Embedded and Parallel Systems Lab3
Major types of optimizations
Embedded and Parallel Systems Lab4
Procedure integration
 Replace procedure call by procedure body
Int a;
void up(){
a=a+1;
}
Void main(){
a=10;
up();
b=a+5;
}
Int a;
Void main(){
a=10;
a=a+1;
b=a+5;
}
Embedded and Parallel Systems Lab5
Common subexpression elimination
 Replace two instances of same
computation by single copy
a = b * c + g;
d = b * c * d;
tmp = b * c;
a = tmp + g;
d = tmp * d;
Embedded and Parallel Systems Lab6
Constant propagation
 Replace all instances of a variable that is
assigned a constant with the constant
int x = 14;
int y = 7 - x / 2;
return y * (28 / x + 2);
int x = 14;
int y = 7 - 14 / 2;
return y * (28 / 14 +
2);
int x = 14;
int y = 0;
return y * 4;
Embedded and Parallel Systems Lab7
Stack height reduction
 Rearrange expression tree to minimize resources needed for
expression evaluation
ADD R6,R2,R3
ADD R7,R6,R4
ADD R8,R7,R5
ADD R6,R2,R3
ADD R7,R4,R5
ADD R8,R7,R6
I1
I2
I3
I1 I2
I3
R8=((R2+R3)+R4)+R5 R8=(R2+R3)+(R4+R5)
Embedded and Parallel Systems Lab8
Copy propagation
 Replace all instances of a variable A that
has been assigned X (i.e., A = X) with X
y = x ;
z = 3 + y;
z = 3 + x
Embedded and Parallel Systems Lab9
Code motion
 Remove code from a loop that computes
same value each iteration of the loop
 Loop-invariant code
while (j < maximum - 1) {
x=1;
j = j + 4 * a;
}
int maxval = maximum - 1;
int calcval = 4 * a;
x=1;
while (j < maxval) {
j = j + calcval;
}
Embedded and Parallel Systems Lab10
Induction variable elimination
 Simplify / eliminate array addressing
calculations within loops
Int i=0;
while( i<10){
i=i+1;
p = 4*i ;
do some things;
}
Int p=0;
while( p<40){
p=p+4;
do some things;
}
Embedded and Parallel Systems Lab11
Strength reduction
 Such as , replace multiply by constant
with adds and shifts
for (i=0 ; i<n ; i++){
z = i * x;
do some things;
}
for (i=0 ; i<n ; i++){
z = z + x;
do some things;
}
Embedded and Parallel Systems Lab12
Branch offset optimization
 Choose the shortest branch displacement
that reaches target
if( a ){
statement 1
}
else {
goto L1;
}
statement 2
L1:
statement 3
if( !a ){
goto L1;
}
statement 1
statement 2
L1:
statement 3
Embedded and Parallel Systems Lab13
dead (unreachable) code elimination
 Remove instructions that will not affect the
behavior of the program.
Int func( int a){
int b, c;
b=a*2;
c=a*3;
return b;
}
Int func( int a){
int b, c;
b=a*2;
return b;
}
Embedded and Parallel Systems Lab14
Boolean Expression
If( a=0 || b=0 || c=0) {
statement 1;
}
If( a=0) {
statement 1;
}else if( b=0){
statement 1;
}else if( c=0){
statement 1;
}
Embedded and Parallel Systems Lab15
Loop unrolling
int i;
for (i = 0; i < 5; i++){
function(i);
}
function(0);
function(1);
function(2);
function(3);
function(4);
 Duplicates the body of the loop multiple times, in order to decrease
the number of times the loop condition is tested and the number of
jumps, which hurt performance by impairing the instruction pipeline
Embedded and Parallel Systems Lab16
 Thanks

More Related Content

What's hot

What's hot (20)

Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Lecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithmLecture 17 Iterative Deepening a star algorithm
Lecture 17 Iterative Deepening a star algorithm
 
Basic MIPS implementation
Basic MIPS implementationBasic MIPS implementation
Basic MIPS implementation
 
Semaphores
SemaphoresSemaphores
Semaphores
 
Python basic syntax
Python basic syntaxPython basic syntax
Python basic syntax
 
Intel Pentium Pro
Intel Pentium ProIntel Pentium Pro
Intel Pentium Pro
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Embedded C - Day 1
Embedded C - Day 1Embedded C - Day 1
Embedded C - Day 1
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
 
Memory & I/O interfacing
Memory & I/O  interfacingMemory & I/O  interfacing
Memory & I/O interfacing
 
Status register
Status registerStatus register
Status register
 
LPC 2148 ARM MICROCONTROLLER
LPC 2148 ARM MICROCONTROLLERLPC 2148 ARM MICROCONTROLLER
LPC 2148 ARM MICROCONTROLLER
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Loops in flow
Loops in flowLoops in flow
Loops in flow
 
Introduction to embedded systems
Introduction  to embedded systemsIntroduction  to embedded systems
Introduction to embedded systems
 
CISC & RISC Architecture
CISC & RISC Architecture CISC & RISC Architecture
CISC & RISC Architecture
 
Little o and little omega
Little o and little omegaLittle o and little omega
Little o and little omega
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set Architecture
 
Interrupts of microprocessor 8085
Interrupts of microprocessor  8085Interrupts of microprocessor  8085
Interrupts of microprocessor 8085
 

Viewers also liked

Fiqih icha
Fiqih ichaFiqih icha
Fiqih icha
ichaa17
 
Tik allisya smpit rpi
Tik allisya smpit rpiTik allisya smpit rpi
Tik allisya smpit rpi
ichaa17
 
tentang menu toolbar pada microsoft word
tentang menu toolbar pada microsoft wordtentang menu toolbar pada microsoft word
tentang menu toolbar pada microsoft word
ichaa17
 
An Opinion Without Support Is Not An Appraisal
An Opinion Without Support Is Not An AppraisalAn Opinion Without Support Is Not An Appraisal
An Opinion Without Support Is Not An Appraisal
James Regnere
 

Viewers also liked (18)

Engranajes fotos
Engranajes fotosEngranajes fotos
Engranajes fotos
 
Fiqih icha
Fiqih ichaFiqih icha
Fiqih icha
 
Tik allisya smpit rpi
Tik allisya smpit rpiTik allisya smpit rpi
Tik allisya smpit rpi
 
Creative & Digital Business Briefing - October 2016
Creative & Digital Business Briefing - October 2016Creative & Digital Business Briefing - October 2016
Creative & Digital Business Briefing - October 2016
 
Creative & Digital Business Briefing - November 2016
Creative & Digital Business Briefing - November 2016Creative & Digital Business Briefing - November 2016
Creative & Digital Business Briefing - November 2016
 
tentang menu toolbar pada microsoft word
tentang menu toolbar pada microsoft wordtentang menu toolbar pada microsoft word
tentang menu toolbar pada microsoft word
 
Programme on Ms Excel For Managerial Computing
Programme on Ms Excel For Managerial ComputingProgramme on Ms Excel For Managerial Computing
Programme on Ms Excel For Managerial Computing
 
An Opinion Without Support Is Not An Appraisal
An Opinion Without Support Is Not An AppraisalAn Opinion Without Support Is Not An Appraisal
An Opinion Without Support Is Not An Appraisal
 
Digital business briefing January 2015
Digital business briefing January 2015Digital business briefing January 2015
Digital business briefing January 2015
 
Health supervision policy for the workplace
Health supervision policy for the workplaceHealth supervision policy for the workplace
Health supervision policy for the workplace
 
Ramya mmwt
Ramya mmwtRamya mmwt
Ramya mmwt
 
English research report
English research reportEnglish research report
English research report
 
Three Post - Media Production Capabilities
Three Post - Media Production CapabilitiesThree Post - Media Production Capabilities
Three Post - Media Production Capabilities
 
Hukum newton gravitasi
Hukum newton gravitasiHukum newton gravitasi
Hukum newton gravitasi
 
Psy 1
Psy 1Psy 1
Psy 1
 
Fit notes and work
Fit notes and workFit notes and work
Fit notes and work
 
Epc slides (part1)
Epc slides (part1)Epc slides (part1)
Epc slides (part1)
 
Life insurance after retirement
Life insurance after retirementLife insurance after retirement
Life insurance after retirement
 

Similar to Compiler optimization

20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris
imec.archive
 
PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12
Andrew Dunstan
 

Similar to Compiler optimization (20)

Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
Compiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flatteningCompiler optimizations based on call-graph flattening
Compiler optimizations based on call-graph flattening
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris20081114 Friday Food iLabt Bart Joris
20081114 Friday Food iLabt Bart Joris
 
PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12
 
control statements
control statementscontrol statements
control statements
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Matopt
MatoptMatopt
Matopt
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
LLVM
LLVMLLVM
LLVM
 
KScope14 Jython Scripting
KScope14 Jython ScriptingKScope14 Jython Scripting
KScope14 Jython Scripting
 
Thinking in Functions
Thinking in FunctionsThinking in Functions
Thinking in Functions
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 

More from ZongYing Lyu

More from ZongYing Lyu (16)

Vue.js
Vue.jsVue.js
Vue.js
 
Performance improvement techniques for software distributed shared memory
Performance improvement techniques for software distributed shared memoryPerformance improvement techniques for software distributed shared memory
Performance improvement techniques for software distributed shared memory
 
Architecture of the oasis mobile shared virtual memory system
Architecture of the oasis mobile shared virtual memory systemArchitecture of the oasis mobile shared virtual memory system
Architecture of the oasis mobile shared virtual memory system
 
A deep dive into energy efficient multi core processor
A deep dive into energy efficient multi core processorA deep dive into energy efficient multi core processor
A deep dive into energy efficient multi core processor
 
Libckpt transparent checkpointing under unix
Libckpt transparent checkpointing under unixLibckpt transparent checkpointing under unix
Libckpt transparent checkpointing under unix
 
Device Driver - Chapter 6字元驅動程式的進階作業
Device Driver - Chapter 6字元驅動程式的進階作業Device Driver - Chapter 6字元驅動程式的進階作業
Device Driver - Chapter 6字元驅動程式的進階作業
 
Device Driver - Chapter 3字元驅動程式
Device Driver - Chapter 3字元驅動程式Device Driver - Chapter 3字元驅動程式
Device Driver - Chapter 3字元驅動程式
 
Web coding principle
Web coding principleWeb coding principle
Web coding principle
 
提高 Code 品質心得
提高 Code 品質心得提高 Code 品質心得
提高 Code 品質心得
 
SCRUM
SCRUMSCRUM
SCRUM
 
Consistency protocols
Consistency protocolsConsistency protocols
Consistency protocols
 
MPI use c language
MPI use c languageMPI use c language
MPI use c language
 
Cvs
CvsCvs
Cvs
 
Parallel program design
Parallel program designParallel program design
Parallel program design
 
MPI
MPIMPI
MPI
 
OpenMP
OpenMPOpenMP
OpenMP
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Compiler optimization

  • 1. Compiler Optimization Speaker :呂宗螢 Adviser :梁文耀 老師 Date : 2006/10/11
  • 2. Embedded and Parallel Systems Lab2 The Structure of Recent Front end per language High-level optimizations Global optimizer Code generator Intermediate representation Dependencies Function Language Dependent machine independent Transform language to common intermediate form Largely machine independent For example, procedure inlineing (also called procedure intergration) Small language dependencies machine dependencies slight (e.g., register count/types) Including global and local optimizations + register allocation Highly machine dependent Language independent Detailed instruction selection and machine-dependent optimizations May include or be followed by assembler
  • 3. Embedded and Parallel Systems Lab3 Major types of optimizations
  • 4. Embedded and Parallel Systems Lab4 Procedure integration  Replace procedure call by procedure body Int a; void up(){ a=a+1; } Void main(){ a=10; up(); b=a+5; } Int a; Void main(){ a=10; a=a+1; b=a+5; }
  • 5. Embedded and Parallel Systems Lab5 Common subexpression elimination  Replace two instances of same computation by single copy a = b * c + g; d = b * c * d; tmp = b * c; a = tmp + g; d = tmp * d;
  • 6. Embedded and Parallel Systems Lab6 Constant propagation  Replace all instances of a variable that is assigned a constant with the constant int x = 14; int y = 7 - x / 2; return y * (28 / x + 2); int x = 14; int y = 7 - 14 / 2; return y * (28 / 14 + 2); int x = 14; int y = 0; return y * 4;
  • 7. Embedded and Parallel Systems Lab7 Stack height reduction  Rearrange expression tree to minimize resources needed for expression evaluation ADD R6,R2,R3 ADD R7,R6,R4 ADD R8,R7,R5 ADD R6,R2,R3 ADD R7,R4,R5 ADD R8,R7,R6 I1 I2 I3 I1 I2 I3 R8=((R2+R3)+R4)+R5 R8=(R2+R3)+(R4+R5)
  • 8. Embedded and Parallel Systems Lab8 Copy propagation  Replace all instances of a variable A that has been assigned X (i.e., A = X) with X y = x ; z = 3 + y; z = 3 + x
  • 9. Embedded and Parallel Systems Lab9 Code motion  Remove code from a loop that computes same value each iteration of the loop  Loop-invariant code while (j < maximum - 1) { x=1; j = j + 4 * a; } int maxval = maximum - 1; int calcval = 4 * a; x=1; while (j < maxval) { j = j + calcval; }
  • 10. Embedded and Parallel Systems Lab10 Induction variable elimination  Simplify / eliminate array addressing calculations within loops Int i=0; while( i<10){ i=i+1; p = 4*i ; do some things; } Int p=0; while( p<40){ p=p+4; do some things; }
  • 11. Embedded and Parallel Systems Lab11 Strength reduction  Such as , replace multiply by constant with adds and shifts for (i=0 ; i<n ; i++){ z = i * x; do some things; } for (i=0 ; i<n ; i++){ z = z + x; do some things; }
  • 12. Embedded and Parallel Systems Lab12 Branch offset optimization  Choose the shortest branch displacement that reaches target if( a ){ statement 1 } else { goto L1; } statement 2 L1: statement 3 if( !a ){ goto L1; } statement 1 statement 2 L1: statement 3
  • 13. Embedded and Parallel Systems Lab13 dead (unreachable) code elimination  Remove instructions that will not affect the behavior of the program. Int func( int a){ int b, c; b=a*2; c=a*3; return b; } Int func( int a){ int b, c; b=a*2; return b; }
  • 14. Embedded and Parallel Systems Lab14 Boolean Expression If( a=0 || b=0 || c=0) { statement 1; } If( a=0) { statement 1; }else if( b=0){ statement 1; }else if( c=0){ statement 1; }
  • 15. Embedded and Parallel Systems Lab15 Loop unrolling int i; for (i = 0; i < 5; i++){ function(i); } function(0); function(1); function(2); function(3); function(4);  Duplicates the body of the loop multiple times, in order to decrease the number of times the loop condition is tested and the number of jumps, which hurt performance by impairing the instruction pipeline
  • 16. Embedded and Parallel Systems Lab16  Thanks