DEPARTMENT OF INFORMATION TECHNOLOGY UNIT IV CODE OPTIMIZATION AND CODE GENERATION TWO MARK WITH ANSWERS 1.What is code optimization? The code optimization is required to produce an efficient target code. There are two important issues that need to be considered while applying the techniques for code optimization and those are: The semantic equivalence of the source program must not be changed. The improvement over the program efficiently must be achieved without changing the algorithm of the program. 2.Explain Machine dependent optimization. It has some special machine properties that can reduce the amount of code or execution time. It is dependent on the source language and target machine. Using immediate instruction sequences. Ex:Register allocation, Utilization of special machine instruction sequences. 3.Explain Machine Indepentent optimization. IT depends only on the arithmetic Operation in the language and not on the target machine. The program transformations improve the target code without taking into consideration any properties of the target machine. Elimination unreachable code from the source program. 4.Properties of Optimizing compiler. The source code should be such that it should produce minimum amount of target code. There should not be any unreachable code.
COMPILER DESIGN [Year]
Dead code should be completely removed from source
language. The Optimizing compilers should apply following code improving transformations on source language. Common sub expression Elimination Dead code Elimination Code movement Strength reduction 5.Transformation techniques that can be applied in code Optimization. Constant folding Constant propagation Common sub expression Variable propagation Code Movement Strength Reduction Dead code Elimination Loop Invariant computation Loop Optimization 6.What is constant Folding Intuition:Based on deducing at compile time, that the value of expression is a constant. Constant folding is the transformation that substitutesnan expression with a constant. Constant folding is useful to discover dead code Ex: if(x) goto L. x is always false. We can eliminate both the if test and jump to L. 7.Explain Constant Propagation. In This technique the value of variable is replaced and computation of expression is done at the compile time. Ex: Pi=3.14 r=5 A=Pi*r*r Pi is replaced by 3.14, r is 5 then computation of 3.14*5*5 is done during computation. 8.Explain Common Sub Expression. An occurance of an expression E is called Common sub Expression. If E was previous computed and values of
COMPILER DESIGN [Year]
variables in E have no changed,since the previous
computation. Assignments to temporary variables involving common sub expression can be eliminated. 9.Explain Dead code Elimination. Intiuition:A variable is live at a point in a program if it value can be used subsequently otherwise is dead. Deadcode:A piece of code is dead, if data computed is never used elsewhere and can be eliminated.. Useless code can better be eliminated. Dead code may appear as the result of previous transformation.Dead code works well together with copy propagation. Ex: After copy propagation we can see that x is never reused all over the code. 10.Define Code motion The loop invariant optimization can be obtained by moving some amount of code outside the loop and placing it just before entering in the loop. It moves code outside the loop. The transformation takes an expression that yields the same results independent of the number of times a loop is executed and place the expression before the loop. Ex: Consider the statement while(i<= limit-2) Code motion t:limit-2 while(i<=t) 11.Explain Induction variable elimination. A variable x is an Induction variable of a loop if every time the variable x changes values, it is incremented or decremented by some constant. In general when there are two or more Induction variables. It is possible to get rid of all but one. 12.Explain Reduction in strength. It is based on the replacement of a computation with a less expensive one.
COMPILER DESIGN [Year]
This replacement will speed up the object code if addition
takes less time than multiplication as is the case in many machines. 13.Define Loop unrolling. In this method the number of jumps and test can be reduced by writing the code two times. 14.Define Peephole Optimization. It is a method for trying to improve the performance of the target program by examining a short sequence of target instructions and replacing those instructions by a shorter or faster sequence wherever possible. The peephole is a small moving window on the target program. 15.State the role of a code generator. As we see that the final phase in any compiler is the code generator. In takes as input an intermediate representation of the source program and produces as output an equivalent target program, as shown in the figure. Optimization phase is optional as far as compilers correct working is considered. In order to have a good compiler following conditions should hold:
COMPILER DESIGN [Year]
16.Explain the various form of object code.(RE)
Absolute machine code: Producing an absolute machine language program as output has
the advantage that it can be placed in a fixed
location in memory and immediately executed.
Relocatable machine code: Producing a relocatable machine language program as output allows subprograms to be compiled separately. A set of relocatable object modules can be linked together and loaded for execution by a linking loader. If the target machine does not handle relocation automatically, the compiler must provide explicit relocation information to the loader, to link the separately compiled program segments. Assembly code: Producing an assembly language program as output makes the process of code generation some what easier.
17.Define Intermediate code generation?
Retargeting is facilitated. That is, a compiler for a different machine can be created by attaching a back end for the new machine to an existing front end. A machine-independent code optimizer can be applied to the intermediate representation.
COMPILER DESIGN [Year]
18.State the application of DAG.
DAGs are useful for: 1. Removing common local sub-expressions. 2. Renaming temporaries. 3. Finding names used inside the block but evaluated outside. 4. Finding statements in the block that could have their computed values used outside the block. 5.Statements that can be reordered (or executed in parallel). 19.fuction Getreg() The function getreg() when called upon to return where the computations specifications by the three address statements x=y op z should be performed returns a location l. 20.Register and address descriptors. The code generation algorithm uses descriptors to keep track of register contents and address for names. Register descriptors: A register descriptor is a pointer to a list containing information about the current contents of each register. Initially,all the registers can be empty. Address descriptors: an address descriptors keeps track of the location where the current values of the names can be found at runtime. This information can be stored in the symbol table.
Learn Python Programming for Beginners: Best Step-by-Step Guide for Coding with Python, Great for Kids and Adults. Includes Practical Exercises on Data Analysis, Machine Learning and More.
Microsoft 365 Guide to Success: 10 Books in 1 | Kick-start Your Career Learning the Key Information to Master Your Microsoft Office Files to Optimize Your Tasks & Surprise Your Colleagues | Access, Excel, OneDrive, Outlook, PowerPoint, Word, Teams, etc.
Microsoft PowerPoint Guide for Success: Learn in a Guided Way to Create, Edit & Format Your Presentations Documents to Visual Explain Your Projects & Surprise Your Bosses And Colleagues | Big Four Consulting Firms Method
Microsoft Excel Guide for Success: Transform Your Work with Microsoft Excel, Unleash Formulas, Functions, and Charts to Optimize Tasks and Surpass Expectations [II EDITION]
The Advanced Roblox Coding Book: An Unofficial Guide, Updated Edition: Learn How to Script Games, Code Objects and Settings, and Create Your Own World!
Modern Tkinter for Busy Python Developers: Quickly Learn to Create Great Looking User Interfaces for Windows, Mac and Linux Using Python's Standard GUI Toolkit