code generation
play

Code Generation Chapter 9 1 Compiler Construction Code Generation - PowerPoint PPT Presentation

Code Generation Chapter 9 1 Compiler Construction Code Generation Issues in Code Generation Target language Relocatable machine code Commercial compilers produce this Absolute machine code OS code must start at a particular memory


  1. Code Generation Chapter 9 1 Compiler Construction Code Generation

  2. Issues in Code Generation Target language • Relocatable machine code Commercial compilers produce this • Absolute machine code OS code must start at a particular memory address • Assembly language – Easier, assembler does the linking – Commerical compilers don’t do this normally • High-level language E.g., cfront 2 Compiler Construction Code Generation

  3. Issues in Code Generation • Instruction selection LDR R0, a ADD R0, 1 STR R0, a a = a + 1; INC a (If a memory increment instruction exists) • Registers – Maximize register hit ratios – NP-complete problem, heuristics are helpful 3 Compiler Construction Code Generation

  4. Issues in Code Generation Code optimization • By analyzing various aspects of the code it may be possible to transform the program into an equivalent but more efficient form • Example, code motion: c = 10 * d; while ( a < b ) { Code motion c = 10 * d; a = a + c; } 4 Compiler Construction Code Generation

  5. Basic Blocks • A sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without any branching • A basic block always ends at a branch or at end of code a = 0 b = c + b M: if (c < a ) goto L c = c + b a = a * 2 goto M L: print c 5 Compiler Construction Code Generation

  6. Dividing Code into Basic Blocks a = 0 1 b = c + b M: if (c < a ) goto L 2 c = c + b a = a * 2 3 goto M 4 L: print c 6 Compiler Construction Code Generation

  7. Variables and Basic Blocks • Variable definition A statement that assigns a value to a variable • Variable use A statement that accesses the value of a variable Example: a = b + c • a is defined • b is used 7 Compiler Construction Code Generation

  8. Flow graph B 1 • A directed graph that shows the flow of control among basic blocks B 2 • There will be a directed edge from B 1 to B 2 if either of the following two conditions are satisfied: B 3 1. there is a conditional or unconditional jump from B 1 to B 2 B 4 2. B 2 immediately follows B 1 and B 1 does not have an unconditional jump 8 Compiler Construction Code Generation

  9. Basic Blocks and Flow Graphs B 1 • Within a basic block we can compute uses of a = 0 1 B 2 b = c + b variables M: if (c < a ) goto L 2 c = c + b a = a * 2 3 goto M • Basic blocks allow 4 L: print c B 3 us to do local optimizations B 4 • Flow graphs allow us to global optimizations 9 Compiler Construction Code Generation

  10. Partitioning Algorithm Program − → basic blocks • Input: A sequence of 3-address statements • Output: A list of basic blocks 10 Compiler Construction Code Generation

  11. Partitioning Algorithm 1. Determine the set of leaders, the first statement in a basic block • The first statement in a program is a leader • Any statement that is the target of a conditional or unconditional jump is a leader (can’t branch into the middle of a basic block) • Any statement that immediately follows a conditional or unconditional jump is a leader 2. For each leader, its basic block consists of the leader and all statements up to, but not including, the next leader or the end of the program 11 Compiler Construction Code Generation

  12. After Partitioning into Basic Blocks Now we can • construct flow graphs for global optimization • do local optimizations on basic blocks Here local and global mean relative to basic blocks 12 Compiler Construction Code Generation

  13. Potential Optimizations on Basic Blocks Potential optimizations include • Common subexpression elimination • Dead code elimination • Algebraic optimizations 13 Compiler Construction Code Generation

  14. Common Subexpression Elimination a = b + c b = a - d c = b + c d = a - d 14 Compiler Construction Code Generation

  15. Dead Code Elimination a = b + c b = d + e e = b + f a = e + x 15 Compiler Construction Code Generation

  16. Algebraic Optimizations Use algebraic identities to eliminate code x = x + 0; y = 1 * y; Can happen more often than you think /* In C/C++ */ #define OFFSET 0 #define FACTOR 1 . . . x = x + OFFSET; y = FACTOR * y; 16 Compiler Construction Code Generation

  17. Next Use Information Allows us to determine if a variable is “live” • A live variable is used subsequently in the basic block • Applies only locally • Need to perform data flow analysis on the flow graph for global optimization 17 Compiler Construction Code Generation

  18. Next Use Information Let the statements within a basic block be numbered sequentially from 1 to n ; for ( i ← n ; i > 0 ; i ← i − 1 ) { Scan backwards if ( Statement i has the form x = y op z ) { Attach to Statement i the information found in the symbol table about the next use and liveness of x , y , and z ; In the symbol table, set x to “not live” and “no next use” In the symbol table, set y and z to “live” and their next use to i ; } } 18 Compiler Construction Code Generation

  19. Example 1 . t 1 = a ∗ b 2 . t 2 = a ∗ a 3 . t 3 = 2 ∗ t 2 4 . t 4 = t 1 + t 3 5 . t 5 = b ∗ b 6 . t 6 = t 4 + t 5 19 Compiler Construction Code Generation

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend