0% found this document useful (0 votes)
49 views6 pages

CD Unit 5 QB Answers

Uploaded by

Akhil Vinnakota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
49 views6 pages

CD Unit 5 QB Answers

Uploaded by

Akhil Vinnakota
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
Unit-V Q.No. LONG ANSWERS QUESTION hoc ‘a, | Explain about the Position of machine dependent code 5 "| optimizer? 1 ‘What are the issues considered at the time of code 5 B. | optimization? Explain them. ‘a, | Explain about the register allocation and assignment with 5 an example. 2 Discuss the difference between machine dependent and ¥ B. | machine independent code optimization. A. | Write about basic block with an example. 5 3 Discuss about control flow graph with an example. 5 A. | Explain about the Position of code generator? 5 4 Which issues are considered at the time of code ji B. | generation? Explain them. Write a Three address code for the following control ‘statements: a. |} Simple it 5 ~ ii) Ifelse iii) While 5 Apply the Structure-Preserving Transformations (or) Peephole optimization (or) Principle sources of optimization for the following C code. B. | fori from 1 to 10 do 5 for j from 1 to 10 do ali, j] = 0.0; for i from 1 to 10 do afi, ] = 1.0; Machine dependant optimizations: Machine dependant optimizations are based on register allocation and utilization of special machine-instruction sequences. The criteria for code improvement transformations: 1. Simply stated, the best program transformations are those that yield the most benefit for the least effort. 2. The transformation must preserve the meaning of programs. That is, the optimization must not change the output produced by a program for a given input, or cause an error such as division by zero, that was not present in the original source program. At all times we take the “safe” approach of missing an opportunity to apply a transformation rather than risk changing what the program does. 3. A transformation must, on the average, speed up programs by a measurable amount. We are also interested in reducing the size of the compiled code although the size of the code has less importance than it once had. Not every transformation succeeds in improving every program, occasionally an “optimization” may slow down a program slightly. 4, The transformation must be worth the effort. It does not make sense for a compiler writer to expend the intellectual effort to implement a code improving transformation and to have the ‘compiler expend the additional time compiling source programs if this effort is not repaid when the target programs are executed. “Peephole” transformations of this kind are simple enough and beneficial enough to be included in any compiler. target Program Fig: Position of code optimizer An Organization for an Optimizing Compiler: 1. Flow analysis is a fundamental prerequisite for many important types of code improvement. 2. Generally control flow analysis precedes data flow analysis. Blog: anilkumarprathipati. wordpress.com 1 UNIT-V cD 3. Control flow analysis (CFA) represents flow of control usually in form of graphs, CFA constructs such as a. control flow graph b. Call graph 4, Data flow analysis (DFA) is the process of ascerting and collecting information prior to program execution about the possible modification, preservation, and use of certain entities (such as values or attributes of variables) in a computer program. 2. CODE GENERATION ISSUES Code Generation Issues: ‘A code generator is expected to have an understanding of the target machine’s runtime environment and its instruction set. The code generator should take the following things (issues) into consideration to generate the code: ‘+ Target language: The code generator has to be aware of the nature of the target language for which the eode is to be transformed. That language may facilitate some machine-specific structions to help the compiler generate the code in a more convenient way. The target ‘machine can have either CISC or RISC processor architecture. ‘+ IR Type: Intermediate representation has various forms. It can be in Abstract Syntax Tree (AST) structure, Reverse Polish Notation, or 3-address code. ‘+ Selection of instruction: The code generator takes Intermediate Representation as input and ‘converts (maps) it into target machine’s instruction set. One representation can have many ways (instructions) to convert it, so it becomes the responsibility of the code generator to ‘choose the appropriate instructions wisely. ‘+ Register allocation: A program has a number of values to be maintained during the execution. The target machine’s architecture may not allow all of the values to be kept in the CPU memory or registers. Code generator decides what values to keep in the registers. Also, it decides the registers to be used to keep these values. + Ordering of instructions: At last, the code generator decides the order in which the instruction will be executed. It creates schedules for instructions to execute them. ‘The code generator has to track both the registers (for availability) and addresses (location of values) while generating the code. For both of them, the following two descriptors are used: «Register descriptor: Register descriptor is used to inform the code generator about the availability of registers. Register descriptor keeps track of values stored in each register. ‘Whenever a new register is required during code generation, this descriptor is consulted for register availability a Serer vile a a tli) yA dpa in bl at different locations while in execution. Address descriptors are used to keep track of ‘memory locations where the values of identifiers are stored. These locations may include CPU registers, heaps, stacks, memory or a combination of the mentioned locations. Code generator keeps both the descriptor updated in real-time, For a load statement, LD RI, x, the code generator: + Updates the Register Descriptor RI that has value of x and + Updates the Address Descriptor (x) to show that one instance of x is in RI. Example: dad := {a-b) + (a-c) + (a-c) Blog: anilkumarprathipati wordpress.com 2 UNIT-V cp Reaisre Generate | Desceirror registers empty RO contains t STATEMENTS tin RO RO contains & 1 contains ‘tin Ro win RY ge 598 88 Fig Register and Address descriptor What is the Difference Between Machine Dependent and Machine Independent Code Optimization? Machine dependent code optimization is applied to object code, applied to the intermediate code. ‘Machine dependent optimization ‘Machine independent code optimization does involves CPU registers and absolute, ‘not involve CPU registers or absolute memory references. memory references. Summary - Machine Dependent vs Machine Independent Code Optimization ‘Code optimization consists of two optimization techniques namely, machine dependent and machine independent code optimization. The difference between machine dependent and machine independent code optimization is, in machine dependent optimization it is applied to object code whereas, in machine independent code, the optimization is applied to intermediate code, Basic Block: Source codes generally have a number of instructions, which are always executed in sequence and are considered as the basic blocks of the code. These basic blocks do not have any jump statements among them, i.e., when the first instruction is executed, all the instructions in the same basic block will be executed in their sequence of appearance without losing the flow control of the program. ‘A program can have various constructs as basic blocks, like IF-THEN-ELSE, SWITCH- CASE conditional statements and loops such as DO- WHILE, FOR, and REPEAT-UNTIL, ete. Basic block identification ‘We may use the following algorithm to find the basic blocks in a program: ‘© Search header statements of all the basic blocks from where a basic block starts: ‘©. First statement of a program. (© Statements that are target of any branch (conditional/unconditional). © Statements that follow any branch statement. ‘© Header statements and the statements following them form a basic block. ‘A basic block does not include any header statement of any other basic block, Basic blocks are important concepts from both code generation and optimization point of view. Example Biog: anilkumarprathipati.wordpress.com 7 AE( => 2) ly =x) xt wext zy Source Code Canteal Flow Crank: Control Flow Graph: Basic blocks in a program can be represented by means of control flow graphs. A control flow graph depicts how the program control is being passed among the blocks. It is a useful tool that helps in optimization by help locating any unwanted loops in the program. Example: Bt ENTER: fame | y= 0; eee ra Be ~f eae ae Le 63 \ i iy a Ba | La tT B4 eee oF Basic Blocks Flow Graph ‘There are two types of basic block optimizations. They are: i, Structure-Preserving Transformations. ii, Algebraic Transformations i) Structure-Preserving Transformations (or) Principle Sources of optimization (or) Peephole optimization: The primary Structure-Preserving Transformation on basic blocks are: ‘a. Common sub-expression elimination . Copy propagation. ¢. Dead-code elimination. 4. Constant folding. €. Code motion. f. Induction-variable elimination. Ete. Blog: anilkumarprathipati.wordpress.com 8 unr ii) Algebraic Transformation Algebraic identities represent another important class of optimizations on basic blocks. This includes simplifying expressions or replacing expensive operation by cheaper ones i. reduction in strength, Another class of related optimizations is constant folding, Here we evaluate constant expressions at compile time and replace the constant expressions by their values. Thus the expression 2°3.14 would be replaced by 6.28. ‘The relational operators <=, >=, <, >, + and = sometimes generate unexpected common sub expressions. ‘Associative laws may also be applied to expose common sub expressions. Ifthe given example code is ‘void quicksort (m,n) int m,n; cp int js int vax; if (n<=m) return; /* fragment begins here */ fem 1; jams v=aln]; while(1) { do i=i+1; while ( afi] v): Lil=x3 (In|; afn|=x; —/* fragment ends here */ quicksort (m,i); quicksort (i+1,n); x=alil; alil=aljl

You might also like