CH06
CH06
Outline
Introduction
Intermediate-Code Generation
Machine-Independent Optimizations
6.1 Introduction: Structure of a Compiler
6.2 Intermediate Code Generation
C SPARC
Pascal HP PA
FORTRAN x86
C SPARC
Pascal HP PA
IR
FORTRAN x86
10
Methods of Intermediate Code (IC) Generation
* *
C
+ * + *
A
* * *
A
B C B C A
B C
DAG Syntax tree
A syntax tree depicts the natural hierarchical structure of a
source program. A DAG gives the same information but in
compact way because common expressions are identified
Postfix Notation: PN
• Quadruples
Four fields: op, arg1, arg2, result
Array of struct {op, *arg1, *arg2, *result}
x:=y op z is represented as op y, z, x
arg1, arg2 and result are usually pointers to symbol table
entries.
May need to use many temporary names.
Many assembly instructions are like quadruple, but arg1,
arg2, and result are real registers.
• Triples
Three fields: op, arg1, and arg2. Result become implicit.
arg1 and arg2 can be pointers to the symbol table.
5/31/2015 \course\cpeg621-10F\Topic-1a.ppt 11
Types of Three-Address Statements
Assignment statements:
x := y op z, where op is a binary operator add a,b,c
x := op z, where op is a unary operator not a, ,c or intoreal a, ,c
Copy statements
x := y mov a, ,c
The unconditional jumps:
goto L jump , ,L1
Conditional jumps:
if x relop y goto L jmprelop y,z,L or if y relop z goto L
param x and call p, n and return y relating to procedure calls
Eg: f(x+1,y) add x,1,t1
param t1, ,
param y, ,
call f,2,
Indexed assignments:
x := y[i]
x[i] := y
Address and pointer assignments:
x := &y, x := *y, and *x = y
6.3 Code Optimization:
Summary of Front End
Front
Abstract Syntax Tree w/Attributes End
Intermediate-code Generator
• Peephole
• Local
• Global
• Loop
• Inter-procedural, whole-program or link-time
• Machine code
• ….
Basic Blocks
of B to the beginning of C.
2. C immediately follows B in the original order of the three-
address instructions, and B does not end in an
unconditional jump.
B is a predecessor of C, and C is a successor of B.
Flow Graphs: Example
Flow Graph Example of program in Example(1).
The block led by first statement of the program is the
start, or entry node.
Entry
Exit
B1: i = 1
B6: t3 = i – 1
B2: j = 1 if i <= 10 goto (10)
B3: t1 = 10 * i B5: i = 1
t2 = t1 + j
j=j+1 B4: i = i + 1
if j <= 10 goto (3) if i <= 10 goto (2)
22
Representation of Basic Blocks
23
Peephole Optimization
• Improve the performance of the target program by
examining and transforming a short sequence of
target instructions
• Depends on the window size
• May need repeated passes over the code
Examples Redundant loads and stores
MOV R0, a
MOV a, Ro
• Algebraic Simplification
x := x + 0
x := x * 1
• Constant folding
x := 2 + 3 x := 5
y := x + 3 y := 8
Local Optimizations
Analysis and transformation performed within a basic block
No control flow information is considered
Examples of local optimizations:
Local common sub expression elimination
analysis: same expression evaluated more than once.
transformation: replace with single calculation
Local constant folding or elimination
analysis: expression can be evaluated at compile time
transformation: replace by constant, compile-time value
Dead code elimination
25
Global Optimizations:
Intraprocedural
Global versions of local optimizations
Global common sub-expression elimination
Global constant propagation
Dead code elimination
Loop optimizations
Reduce code to be executed in each iteration
26
Examples
• Unreachable code
#define debug 0
if (debug) (print debugging information)
if 0 <> 1 goto L1
print debugging
information L1:
if 1 goto L1
print debugging information
L1:
27
Examples
• Flow-of-control optimization
goto L1 goto L2
… …
L1: goto L2 L2: …
28