SlideShare a Scribd company logo
1 of 15
Code Optimization
• Optimization is the process of transforming a piece of code to make more efficient 
(either in terms of time or space) without changing its output or side-effects. The only 
difference visible to the code’s user should be that it runs faster and/or consumes less 
memory. It is really a misnomer that the name implies you are finding an "optimal" 
solution— in truth, optimization aims to improve, not perfect, the result. Optimization is 
the field where most compiler research is done today. The tasks of the front-end 
(scanning, parsing, semantic analysis) are well understood and unoptimized code 
generation is relatively straightforward. 
• Optimization, on the other hand, still retains a sizable measure of mysticism. High-quality 
optimization is more of an art than a science. Compilers for mature languages aren’t 
judged by how well they parse or analyze the code—you just expect it to do it right with 
a minimum of hassle—but instead by the quality of the object code they produce. Many 
optimization problems are NP-complete and thus most optimization algorithms rely on 
heuristics and approximations. It may be possible to come up with a case where a 
particular algorithm fails to produce better code or perhaps even makes it worse. 
• However, the algorithms tend to do rather well overall. It’s worth reiterating here that 
efficient code starts with intelligent decisions by the programmer. No one expects a 
compiler to replace BubbleSort with Quicksort. If a programmer uses a lousy algorithm, 
no amount of optimization can make it snappy. In terms of big-O, a compiler can only 
make improvements to constant factors. But, all else being equal, you want an algorithm 
with low constant factors.
When and Where To Optimize 
• There are a variety of tactics for attacking optimization. Some techniques 
are applied to the intermediate code, to streamline, rearrange, compress, 
etc. in an effort to reduce the size of the abstract syntax tree or shrink the 
number of TAC instructions. Others are applied as part of final code 
generation—choosing which instructions to emit, how to allocate registers 
and when/what to spill, and the like. And still other optimizations may 
occur after final code generation, attempting to re-work the assembly code 
itself into something more efficient. 
• Optimization can be very complex and time-consuming; it often involves 
multiple subphases, some of which are applied more than once. Most 
compilers allow optimization to be turned off to speed up compilation (gcc 
even has specific flags to turn on and off individual optimizations.)
Constant Folding 
• Constant folding refers to the evaluation at compile-time of 
expressions whose 
• operands are known to be constant. In its simplest form, it involves 
determining that all of the operands in an expression are constant-valued, 
performing the evaluation of the expression at compile-time, 
and then replacing the expression by its value. If an expression such 
as 10 + 2 * 3 is encountered, the compiler can compute the result at 
compile-time (16) and emit code as if the input contained the result 
rather than the original expression. Similarly, constant conditions, 
such as a conditional branch if a < b goto L1 else goto L2 where a and 
b are constant can be replaced by a Goto L1 or Goto L2 depending on 
the truth of the expression evaluated at compile-time.
Copy Propagation 
• This optimization is similar to constant propagation, but generalized 
to non-constant values. If we have an assignment a = b in our 
instruction stream, we can replace later occurrences of a with b 
(assuming there are no changes to either variable in-between). Given 
the way we generate TAC code, this is a particularly valuable 
optimization sinceit is able to eliminate a large number of instructions 
that only serve to copy values from one variable to another.
Common Subexpression Elimination 
• Two operations are common if they produce the same result. In such 
a case, it is likely more efficient to compute the result once and 
reference it the second time rather than re-evaluate it. An expression 
is alive if the operands used to compute the expression have not been 
changed. An expression that is no longer alive is dead.
PROGRAM FOR CONSTANT FOLDING 
• #include<stdio.h> 
• #include<conio.h> 
• int fib(int base) 
• {int result,tmp0,tmp1,tmp2,temp[12],f0,f1,i; 
• temp[0]=1; 
• temp[4]=0; 
• temp[1]=base < temp[0]; 
• temp[2]=base==temp[0]; 
• temp[3]=temp[1]||temp[2];
• if(temp[3]==temp[4]) 
• goto L0; 
• result=base; 
• goto L1; 
• L0: 
• temp[4]=0; 
• f0=temp[4]; 
• tmp0=f0; 
• temp[5]=1; 
• f1=temp[5]; 
• tmp1=f1; 
• printf("%d %d ",f0,f1);
• temp[6]=2; 
• i=temp[6]; 
• tmp2=i; 
• L2: 
• temp[7]=i<base; 
• temp[8]=i==base; 
• temp[9]=temp[7]||temp[8]; 
• if(temp[4]==temp[9]) 
• goto L3;
• temp[10]=tmp0+tmp1; 
• result=temp[10]; 
• printf("%d ",result); 
• tmp0=tmp1; 
• tmp1=result; 
• temp[11]=1; 
• temp[12]=tmp2+temp[11]; 
• tmp2=temp[12]; 
• goto L2; 
• L3: 
• L1: 
• return result; 
• } 
• void main() 
• { 
• int n; 
• clrscr(); 
• printf("nEnter a number:"); 
• scanf("%d",&n) ; 
• printf("nnfibonacci is %d",fib(n)); 
• getch(); 
• }
PROGRAM FOR COPY PROPOGATION 
• #include<stdio.h> 
• #include<conio.h> 
• int fib(int base) 
• { 
• int result,tmp0,tmp1,tmp2; 
• if (base <= 1) 
• { 
• result = base; 
• } 
• else 
• {
• int i; 
• int f0; 
• int f1; 
• f0 = 0; 
• tmp0=f0; 
• f1 = 1; 
• tmp1=f1; 
• i = 2; 
• tmp2=i; 
• printf("nn%dt%d",tmp0,tmp1); 
• while (tmp2 <= base) 
• { 
• result = tmp0 + tmp1; 
• printf("t%d",result); 
• tmp0 = tmp1; 
• tmp1 = result; 
• tmp2 = tmp2 + 1; 
• } 
• } 
• return result; 
• }
• void main() 
• { 
• int n; 
• clrscr(); 
• printf("nnEnter number:"); 
• scanf("%d",&n); 
• printf("nnFibonacci is: %dn",fib(n)); 
• getch(); 
• }
PROGRAM FOR COMMON SUBEXPRESSION 
ELIMINATION 
• #include<stdio.h> 
• #include<conio.h> 
• void main() 
• { 
• int a,b,c,d,e,f; 
• clrscr(); 
• printf("n Enter the values of a,b,c"); 
• scanf("n %d %d %d",&a,&b,&c); 
• d=a+b; 
• e=d*b;
• f=e+d+a; 
• printf("n D=%d",d); 
• printf("n E=%d",e); 
• printf("n F=%d",f); 
• getch(); 
• }

More Related Content

What's hot

02 intel v_tune_session_02
02 intel v_tune_session_0202 intel v_tune_session_02
02 intel v_tune_session_02
Vivek chan
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
liu_ming50
 
Compiler Optimization-Space Exploration
Compiler Optimization-Space ExplorationCompiler Optimization-Space Exploration
Compiler Optimization-Space Exploration
tmusabbir
 
Finding Bugs Faster with Assertion Based Verification (ABV)
Finding Bugs Faster with Assertion Based Verification (ABV)Finding Bugs Faster with Assertion Based Verification (ABV)
Finding Bugs Faster with Assertion Based Verification (ABV)
DVClub
 

What's hot (20)

Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Unit 3 part2
Unit 3 part2Unit 3 part2
Unit 3 part2
 
Peephole optimization techniques
Peephole optimization techniquesPeephole optimization techniques
Peephole optimization techniques
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
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...
 
Plc part 3
Plc  part 3Plc  part 3
Plc part 3
 
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
 
02 intel v_tune_session_02
02 intel v_tune_session_0202 intel v_tune_session_02
02 intel v_tune_session_02
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow Analysis
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
Arm developement
Arm developementArm developement
Arm developement
 
Compiler Optimization-Space Exploration
Compiler Optimization-Space ExplorationCompiler Optimization-Space Exploration
Compiler Optimization-Space Exploration
 
Finding Bugs Faster with Assertion Based Verification (ABV)
Finding Bugs Faster with Assertion Based Verification (ABV)Finding Bugs Faster with Assertion Based Verification (ABV)
Finding Bugs Faster with Assertion Based Verification (ABV)
 
Parallel concepts1
Parallel concepts1Parallel concepts1
Parallel concepts1
 
Problem solving (C++ Programming)
Problem solving (C++ Programming)Problem solving (C++ Programming)
Problem solving (C++ Programming)
 
C- language Lecture 4
C- language Lecture 4C- language Lecture 4
C- language Lecture 4
 
Recursion
RecursionRecursion
Recursion
 
09 implementing+subprograms
09 implementing+subprograms09 implementing+subprograms
09 implementing+subprograms
 

Viewers also liked

Viewers also liked (12)

States machine
States machineStates machine
States machine
 
Keyword Presentation
Keyword PresentationKeyword Presentation
Keyword Presentation
 
Types and roles
Types and rolesTypes and roles
Types and roles
 
Uml Common Mechanism
Uml Common MechanismUml Common Mechanism
Uml Common Mechanism
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Compilers
CompilersCompilers
Compilers
 
What is symbol table?
What is symbol table?What is symbol table?
What is symbol table?
 
Single Pass Assembler
Single Pass AssemblerSingle Pass Assembler
Single Pass Assembler
 
Linux programming - Getting self started
Linux programming - Getting self started Linux programming - Getting self started
Linux programming - Getting self started
 
Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0Linux Internals - Interview essentials 4.0
Linux Internals - Interview essentials 4.0
 
Operating Systems: Processor Management
Operating Systems: Processor ManagementOperating Systems: Processor Management
Operating Systems: Processor Management
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
 

Similar to sCode optimization

CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
kapib57390
 
FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)
Yogesh Deshpande
 
Effective cplusplus
Effective cplusplusEffective cplusplus
Effective cplusplus
Mark Veltzer
 

Similar to sCode optimization (20)

Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankar
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
CODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtryCODE TUNINGtertertertrtryryryryrtytrytrtry
CODE TUNINGtertertertrtryryryryrtytrytrtry
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Cs 568 Spring 10 Lecture 5 Estimation
Cs 568 Spring 10  Lecture 5 EstimationCs 568 Spring 10  Lecture 5 Estimation
Cs 568 Spring 10 Lecture 5 Estimation
 
Survelaine murillo ppt
Survelaine murillo pptSurvelaine murillo ppt
Survelaine murillo ppt
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Logic programming in python
Logic programming in pythonLogic programming in python
Logic programming in python
 
FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Effective cplusplus
Effective cplusplusEffective cplusplus
Effective cplusplus
 
pm1
pm1pm1
pm1
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
DAA Unit 1.pdf
DAA Unit 1.pdfDAA Unit 1.pdf
DAA Unit 1.pdf
 
What’s eating python performance
What’s eating python performanceWhat’s eating python performance
What’s eating python performance
 

More from Satyamevjayte Haxor (9)

Patterns
PatternsPatterns
Patterns
 
Uml class Diagram
Uml class DiagramUml class Diagram
Uml class Diagram
 
Lexical
LexicalLexical
Lexical
 
Linker
LinkerLinker
Linker
 
Nested micro
Nested microNested micro
Nested micro
 
Multiplier control unit
Multiplier control unitMultiplier control unit
Multiplier control unit
 
Control unit design
Control unit designControl unit design
Control unit design
 
Direct linking loaders
Direct linking loadersDirect linking loaders
Direct linking loaders
 
Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 

sCode optimization

  • 2. • Optimization is the process of transforming a piece of code to make more efficient (either in terms of time or space) without changing its output or side-effects. The only difference visible to the code’s user should be that it runs faster and/or consumes less memory. It is really a misnomer that the name implies you are finding an "optimal" solution— in truth, optimization aims to improve, not perfect, the result. Optimization is the field where most compiler research is done today. The tasks of the front-end (scanning, parsing, semantic analysis) are well understood and unoptimized code generation is relatively straightforward. • Optimization, on the other hand, still retains a sizable measure of mysticism. High-quality optimization is more of an art than a science. Compilers for mature languages aren’t judged by how well they parse or analyze the code—you just expect it to do it right with a minimum of hassle—but instead by the quality of the object code they produce. Many optimization problems are NP-complete and thus most optimization algorithms rely on heuristics and approximations. It may be possible to come up with a case where a particular algorithm fails to produce better code or perhaps even makes it worse. • However, the algorithms tend to do rather well overall. It’s worth reiterating here that efficient code starts with intelligent decisions by the programmer. No one expects a compiler to replace BubbleSort with Quicksort. If a programmer uses a lousy algorithm, no amount of optimization can make it snappy. In terms of big-O, a compiler can only make improvements to constant factors. But, all else being equal, you want an algorithm with low constant factors.
  • 3. When and Where To Optimize • There are a variety of tactics for attacking optimization. Some techniques are applied to the intermediate code, to streamline, rearrange, compress, etc. in an effort to reduce the size of the abstract syntax tree or shrink the number of TAC instructions. Others are applied as part of final code generation—choosing which instructions to emit, how to allocate registers and when/what to spill, and the like. And still other optimizations may occur after final code generation, attempting to re-work the assembly code itself into something more efficient. • Optimization can be very complex and time-consuming; it often involves multiple subphases, some of which are applied more than once. Most compilers allow optimization to be turned off to speed up compilation (gcc even has specific flags to turn on and off individual optimizations.)
  • 4. Constant Folding • Constant folding refers to the evaluation at compile-time of expressions whose • operands are known to be constant. In its simplest form, it involves determining that all of the operands in an expression are constant-valued, performing the evaluation of the expression at compile-time, and then replacing the expression by its value. If an expression such as 10 + 2 * 3 is encountered, the compiler can compute the result at compile-time (16) and emit code as if the input contained the result rather than the original expression. Similarly, constant conditions, such as a conditional branch if a < b goto L1 else goto L2 where a and b are constant can be replaced by a Goto L1 or Goto L2 depending on the truth of the expression evaluated at compile-time.
  • 5. Copy Propagation • This optimization is similar to constant propagation, but generalized to non-constant values. If we have an assignment a = b in our instruction stream, we can replace later occurrences of a with b (assuming there are no changes to either variable in-between). Given the way we generate TAC code, this is a particularly valuable optimization sinceit is able to eliminate a large number of instructions that only serve to copy values from one variable to another.
  • 6. Common Subexpression Elimination • Two operations are common if they produce the same result. In such a case, it is likely more efficient to compute the result once and reference it the second time rather than re-evaluate it. An expression is alive if the operands used to compute the expression have not been changed. An expression that is no longer alive is dead.
  • 7. PROGRAM FOR CONSTANT FOLDING • #include<stdio.h> • #include<conio.h> • int fib(int base) • {int result,tmp0,tmp1,tmp2,temp[12],f0,f1,i; • temp[0]=1; • temp[4]=0; • temp[1]=base < temp[0]; • temp[2]=base==temp[0]; • temp[3]=temp[1]||temp[2];
  • 8. • if(temp[3]==temp[4]) • goto L0; • result=base; • goto L1; • L0: • temp[4]=0; • f0=temp[4]; • tmp0=f0; • temp[5]=1; • f1=temp[5]; • tmp1=f1; • printf("%d %d ",f0,f1);
  • 9. • temp[6]=2; • i=temp[6]; • tmp2=i; • L2: • temp[7]=i<base; • temp[8]=i==base; • temp[9]=temp[7]||temp[8]; • if(temp[4]==temp[9]) • goto L3;
  • 10. • temp[10]=tmp0+tmp1; • result=temp[10]; • printf("%d ",result); • tmp0=tmp1; • tmp1=result; • temp[11]=1; • temp[12]=tmp2+temp[11]; • tmp2=temp[12]; • goto L2; • L3: • L1: • return result; • } • void main() • { • int n; • clrscr(); • printf("nEnter a number:"); • scanf("%d",&n) ; • printf("nnfibonacci is %d",fib(n)); • getch(); • }
  • 11. PROGRAM FOR COPY PROPOGATION • #include<stdio.h> • #include<conio.h> • int fib(int base) • { • int result,tmp0,tmp1,tmp2; • if (base <= 1) • { • result = base; • } • else • {
  • 12. • int i; • int f0; • int f1; • f0 = 0; • tmp0=f0; • f1 = 1; • tmp1=f1; • i = 2; • tmp2=i; • printf("nn%dt%d",tmp0,tmp1); • while (tmp2 <= base) • { • result = tmp0 + tmp1; • printf("t%d",result); • tmp0 = tmp1; • tmp1 = result; • tmp2 = tmp2 + 1; • } • } • return result; • }
  • 13. • void main() • { • int n; • clrscr(); • printf("nnEnter number:"); • scanf("%d",&n); • printf("nnFibonacci is: %dn",fib(n)); • getch(); • }
  • 14. PROGRAM FOR COMMON SUBEXPRESSION ELIMINATION • #include<stdio.h> • #include<conio.h> • void main() • { • int a,b,c,d,e,f; • clrscr(); • printf("n Enter the values of a,b,c"); • scanf("n %d %d %d",&a,&b,&c); • d=a+b; • e=d*b;
  • 15. • f=e+d+a; • printf("n D=%d",d); • printf("n E=%d",e); • printf("n F=%d",f); • getch(); • }