Code Optimization: A Project Report ON
Code Optimization: A Project Report ON
Code Optimization: A Project Report ON
CODE OPTIMIZATION
Submitted By,
Akshat Mishra Anuj M Khasgiwala Arpit Jain Narendra Shakya Team Number- A10
Guided By,
Mr. Ranjeet Jaiswal Lecturer, CSE.
Oriental Institute of Science & Technology, Bhopal Department of Computer Science & Engineering
1|Page
Table of Contents
1. Introduction
1.1 About the project 1.2 Objective
3
4 4
5 10
4. System Study
4.1 System Development Life Cycle 12 4.2 Software Model 13
12
5. Requirement Specification
5.1 Feasibility Study 15 5.2 Hardware Requirement 16 5.3 Software Requirement 16
15
6. Design
6.1 Input Requirement 17
17
2|Page
6.2 Data Flow Diagram 17 6.3 Control Flow Diagram 18 6.4 System Flow Diagram 19
20 20
Compiler optimization is the process of tuning the output of a compiler to minimize or maximize some attributes of an executable computer program. The most common requirement is to minimize the time taken to execute a program; a less common one is to minimize the amount of memory occupied. In computing, optimization is the process of modifying a system to make some aspect of it work more efficiently or use fewer resources. There are broadly two types of optimizations 1. Memory optimization 2. Speed optimization Memory: The program created need to be compact in order to use less memory space.Code optimiser aims at minimising the code length and removing unnecessary codes or variables. Speed: There are two types of programs those that need to be fast and those that don't. With ever increasing speed of processors and peripherals more applications fall into the latter category. Of those where speed is critical there are several possible bottlenecks:
Hard disk & File system Network Operating system kernel Languages standard library Users 3|Page
Memory Processor
Code optimization can be also broadly categorized as 1. Platform dependent 2. Platform independent techniques While the latter ones are effective on most or all platforms, platform dependent techniques use specific properties of one platform, or rely on parameters depending on the single platform or even on the single processor; writing or producing different versions of the same code for different processors might be thus needed.
1.2 OBJECTIVES
The main goal or objective behind preparing for this project is to optimize the code which makes it more efficient in reference to space usage and time complexity. It is a technique which is mainly used to optimize the code in order to get the best output as early as possible. Code optimizer aims to optimizeExecution time Not important if the program execution time is very short Important in High Performance Computing where execution times may be very long (days, weeks or months). Also very important in many embedded systems, where there may be 4|Page
strict requirements on the execution time Memory usage This is part of normal algorithm design The memory requirements of an algorithm are fairly easy to understand Corresponds directly to the data structures allocated in the program
2. CLASSIFICATION OF OPTIMIZATION
There are so many Optimization technique it is worthwhile to reduce the complexity of their study. Two useful classifications are: The time during compilation process when an optimization can be applied. The area of the program over which the optimization applies. Optimization can be performed at practically every stage of compilation. Example: the constant folding can be performed as early as during parsing. Some optimization can be delayed until after target code has been generated.-target code is examined and rewritten to reflect optimization. The classification scheme for optimization that we consider is by area of the program over which the optimization applies. It is divided into fellow category: Local optimization. Global optimization. Inter-Procedural optimization.
Local Optimization
Major local optimization strategies are: 5|Page
1. Dead code Elimination: A variable is live at a point in a program If it can be used subsequently. Similarly the statements that never get execute or never compute values are called the dead code. Example: Void main () { int a; a=5; cout<<a; return; for(a=0;a<=32760;a++) { cout<<a; }} AFTER OPTIMIZATION: void main() { int a; a=5; cout<<a; return; }
2. Common Sub-Expression Elimination: An occurrence of an expression E is called common sub-expression if E was previously computed and the values of variables in E have not changed since the previous computation. So we can avoid the recomputation if we can use the previous value. Example: void main() { int a,b,c; cout<<"enter a:"; cin>>a; cout<<"enter b:"; cin>>b; c=a+b; 6|Page
AFTER OPTIMIZAIOTN: void main() { int a,b,c; cout<<"enter a:"; cin>>a; cout<<"enter b:"; cin>>b; c=a+b; cout<<"value of c ="<<c; cout<<"now the value of c="<<c; }
3. Loop Optimization: Loop is a building block of any program and usually digests most of the program time loop optimize is a set of optimization techniques used for improving the loop. There are different loop optimization techniques: Loop-Invariant code motion. Reeducation in strength.
Loop-Invariant Code Motion: Loop-Invariant Code Motion analyzes the body of a loop and move test portion of the loop and all the code that doesn't change as the loop executes.
Strength Reduction: A code generator should not only look for unnecessary operation but should take advantage of opportunities to reduce the cost of the operation. Those are necessary but can be implemented in cheaper ways than the source code or a simple implementation might indicate. 7|Page
A typical example of this is the replacement of arithmetic operation by cheaper operation. Such as multiplication by 2 can be implemented as a shift operation and as small integer power such as x3 can be implemented as a multiplication such as x*x*x. This optimization Is called Reduction in strength. 4. Constant Folding: In compiler theory, constant folding and constant propagation are related optimization techniques used by many modern compilers. A more advanced form of constant propagation known as sparse conditional constant propagation may be utilized to simultaneously remove dead code and more accurately propagate constants. Constant folding is the process of simplifying constant expressions at compile time. Terms in constant expressions are typically simple literals, such as the integer 2, but can also be variables whose values are never modified, or variables explicitly marked as constant. Consider the statement:i = 320 * 200 * 32; Most modern compilers would not actually generate two multiply instructions and a store for this statement. Instead, they identify constructs such as these, and substitute the computed values at compile time (in this case, 2,048,000), usually in the intermediate representation (IR) tree. In some compilers, constant folding is done early so that statements such as C's array initializers can accept simple arithmetic expressions. However, it is also common to include further constant folding rounds in later stages in the compiler, as well. Constant folding can be done in a compiler's front end on the IR tree that represents the high-level source language, before it is translated into three-address code, or in the back end, as an adjunct to constant propagation.
Global Optimization
Optimization that extend beyond basic blocks but are confined to an individual procedure. Global optimization is an extend form of the local optimization it is more difficult to perform. 8|Page
They are generally required a techniques called data flow analysis which attempts to collect information across jump boundaries. Global optimization uses the same techniques that are used in the local optimization. But instead of considering the basic block global optimization consider the complete procedure as a processed block.
Global Sub-Expression Elimination: An occurrence of an expression E is called Global sub=expression if E was previously computed and the values of variables in E have not changed since the previous computation. So we can avoid the recomputation if we can use the previous value.
Inter-Procedural Optimization
Optimizations that extend beyond the boundaries to the entire program are called Interprocedural Optimization. It is even more difficult since it involves possible several different parameters passing mechanisms the possibility of non-local variables access and the need to compute simultaneous information on all procedure that might call each other. An additional compilation is the possibility that many procedures may be compiles separately and only linked together at a later point. The complier then cannot perform any inter-procedural optimization at all without the involvement of a specialized form of linker that carries out optimization based on information that the compiler has gathered.
9|Page
3. PROBLEM DEFINITION
In the execution of any program, user may face several problems. Our project will eliminate these problems, as follows Dead code Elimination - Dead Code Elimination is an optimization technique to eliminate some variables not used at all. The variables which are useless in a program will detect and eliminate by the optimizer. Common Sub-Expression Elimination - Common sub expression elimination is a compiler optimization that searches for instances of identical expressions (i.e., they all evaluate to the same value), and analyses whether it is worthwhile replacing them with a single variable holding the computed value.
Loop Optimization - Loop optimization plays an important role in improving cache performance, making effective use of parallel processing capabilities, and reducing overheads associated with executing loops. Most execution time of a scientific program is spent on loops. Thus a lot of compiler analysis and compiler optimization techniques have been developed to make the execution of loops faster. Strength Reduction - Strength reduction is a compiler optimization where expensive operations are replaced with equivalent but less expensive operations. The classic example of strength reduction converts "strong" multiplications inside a loop into "weaker" additions something that frequently occurs in array addressing. 10 | P a g e
Constant Folding - Constant folding and constant propagation are related compiler optimizations used by many modern compilers. An advanced form of constant propagation known as sparse conditional constant propagation can more accurately propagate constants and simultaneously remove dead code. Elimination of useless instruction - Some instructions that do not modify any memory storage can be detect and removed by optimizer.
A Simple but effective technique for locally improving the target code is peephole optimization. A method for trying to improve the performance of the target program. By examining a short sequence of target instructions and replacing these instructions by a shorter or faster sequence whenever possible.
4. SYSTEM STUDY
The system study phase involves the initial investigation of the structure of the System, which is currently in use, with the objective of identifying the problem and difficulties with the existing system. The major steps involved in this phase included defining the user requirements and studying the present system to verify the problem. The performance expected by the new system was also defined in this phase in order to meet the user requirements. The information gathered from various documents were analyzed and evaluated and the findings reviewed in order to establish specific system objectives.
12 | P a g e
Systems Development Life Cycle (SDLC) is any logical process used by a systems analyst to develop an information system, including requirements, validation, training, and user ownership. A SDLC should result in a high quality system that meets or exceeds customer expectations, reaches completion within time and cost estimates, works effectively and efficiently in the current and planned Information Technology infrastructure, and is inexpensive to maintain and cost-effective to enhance. Below we have shown the software development life cycle of our project Code Optimizer.
13 | P a g e
The basic idea behind iterative enhancement is to develop a software system incrementally, allowing the developer to take advantage of what was being learned during the development of earlier, incremental, deliverable versions of the system. Learning comes from both the development and use of the system, where possible. Key steps in the process were to start with a simple implementation of a subset of the software requirements and iteratively enhance the evolving sequence of versions until the full system is implemented. At each iteration, design modifications are made and new functional capabilities are added. The Procedure itself consists of the Initialization step, the Iteration step, and the Project Control List. The initialization step creates a base version of the system. The goal for this initial implementation is to create a product to which the user can react. It should offer a sampling of the key aspects of the problem and provide a solution that is simple enough to understand and implement easily. To guide the iteration process, a project control list is created that contains a record of all tasks that need to be performed. It includes such items as new features to be implemented and areas of redesign of the existing solution. The control list is constantly being revised as a result of the analysis phase. The iteration involves the redesign and implementation of a task from the project control list, and the analysis of the current version of the system. The goal for the design and implementation of any iteration is to be simple, straightforward, and modular, supporting redesign at that stage or as a task added to the project control list. The level of design detail is not dictated by the interactive approach. In a light-weight iterative project the code may represent the major source of documentation of the system; however, in a mission-critical iterative project a formal Software Design Document may be used. The analysis of iteration is based upon user feedback, and the program analysis facilities available. It involves analysis of the structure, modularity, usability, reliability, efficiency, & achievement of goals. The project control list is modified in light of the analysis results.
14 | P a g e
5. REQUIREMENT SPECIFICATION
The primary goal of the system analyst is to improve the efficiency of the existing system. For that the study of specification of the requirements is very essential. For the development of the new system, a preliminary survey of the existing system will be conducted. Investigation done whether the up gradation of the system into an application program could solve the problems and eradicate the inefficiency of the existing system.
15 | P a g e
The requirements of the system are specified with a set of constraints such as system objectives and the description of the out puts. It is then duty of the analyst to evaluate the feasibility of the proposed system to generate the above results. Three key factors are to be considered during the feasibility study.
Operation Feasibility:
An estimate should be made to determine how much effort and care will go into the developing of the system including the training to be given to the user. Usually, people are reluctant to changes that come in their progression. The computer initialization will certainly affected the turn over, transfer and employee job status. Hence an additional effort is to be made to train and educate the users on the new way of the system.
Technical Feasibility:
The main consideration is to be given to the study of available resources of the organization where the software is to be implemented. Here the system analyst evaluates the technical merits of the system giving emphasis on the performance, reliability, maintainability. By taking the consideration before developing the proposed system, the resources availability of the organization was studied. The organization was immense computer facilities equipped with sophisticated machines and the software hence this technically feasible.
Economic Feasibility:
Economic feasibility is the most important and frequently used method for evaluating the effectiveness of the proposed system. It is very essential because the main goal of the proposed system is to have economically better result along with increased efficiency. Cost benefit analysis is usually performed for this purpose. It is the comparative study of the cost verses the benefit and savings that are expected from the proposed system. Since the organization is well equipped with the required hard ware, the project was found to be economically.
16 | P a g e
6. DESIGN
6.1 INPUT REQUIREMENT
In our project, we have to input a program written in C programming language.This can be done in two ways: 1. User can provide the input by specifying filename i.e. C format. 2. User can provide the input by writing a C program on Code Window.
17 | P a g e
LEVEL 1:
USER
Constant Folding
Optimized Output
Strength Reduction
Code Motion
18 | P a g e
Exit Start/Stop
User
Input Code
Loop Optimization
Elimination Process
Exit Start/Stop
Optimized Code
19 | P a g e
Input Choice
Operation
Code
Application Subsystem
Input Interface
Output Interface
Diagnostics Subsystem
20 | P a g e
7. CONCLUSION
The Code Optimization is developed using Java and fully meets the objectives of the system for which it has been developed. The system has reached a steady state where all errors have been eliminated. The system is operated at a high level of efficiency and all the user associated with the system understands its advantage. The system solves the problem. It was intended to solve as requirement specification.
8. REFERENCE
21 | P a g e