Spring 2010 Lecture 1: Introduction Intro. to Computer Language Engineering Course Administration info

advertisement
Spring 2010
Lecture 1: Introduction
Intro. to Computer Language Engineering
Course Administration info
info.
Outline
• Course Administration Information
• Introduction to computer language engineering
– Why do we need a compiler?
– What are compilers?
– Anatomy of a compiler
Saman Amarasinghe
2
6.035
©MIT Fall 1998
Course Administration
•
•
•
•
•
•
Staff
Optional Text
Course Outline
The Project
j
Project Groups
Grading
Saman Amarasinghe
3
6.035
©MIT Fall 1998
Reference Textbooks
Modern Compiler Implementation in Java (Tiger book)
A textbook tutorial on compiler
implementation, including
techniques for many language
features
•
Advanced Compiler Design and Implementation (Whale book)
Essentially a recipe book of
optimizations;
ti i ti
very complete
l t and
d
suited for industrial practitioners
and researchers.
•
Compilers: Principles,
Principles Techniques and Tools (Dragon book)
The classic
Th
l i compilers
il
textbook,
t tb k
although its front-end emphasis
reflects its age. New edition has
more optimization material.
•
Engineering a Compiler (Ark book)
A modern classroom textbook,
with increased emphasis on the
back-end and implementation
techniques
techniques.
•
Optimizing Compilers for Modern Architectures
A modern textbook that focuses
on optimizations including
parallelization and memory
hierarchy optimization
•
A.W. Appel
Cambridge University Press, 1998
ISBN 0-52158-388-8
Steven Muchnick
Morgan Kaufman Publishers, 1997
ISBN 1-55860-320-4
Aho, Lam, Sethi and Ullman
Addison-Wesley, 2006
ISBN 0321486811
Keith D. Cooper, Linda Torczon
Morgan Kaufman Publishers, 2003
ISBN 1-55860
55860-698
698-X
X
Randy Allen and Ken Kennedy
Morgan Kaufman Publishers, 2001
ISBN 1-55860-286-0
Saman Amarasinghe
5
6.035
©MIT Fall 1998
The Project: The Five Segments

Lexical and Syntax Analysis
Semantic Analysis

Code Generation

Data-flow
Data
flow Analysis

Optimizations

Saman Amarasinghe
6
6.035
©MIT Fall 1998
Each Segment...
• Segment Start
– Project Description
• Lectures
– 2 to 5 lectures
• Project Time
– (Design Document)
– (Project Checkpoint)
• Project Due
Saman Amarasinghe
7
6.035
©MIT Fall 1998
Project Groups
• 1st project is an individual project
• Projects 2 to 5 are group projects consists of
3 to 4 students
• Grading
– All group members
b
(mostly)
(
tl ) gett the
th same grade
d
Saman Amarasinghe
8
6.035
©MIT Fall 1998
Grades
• Compiler project
70%
• In-class Quizzes
30% (10% each)
• In-class mini-quizzes
10% (0.5% each)
Saman Amarasinghe
10
6.035
©MIT Fall 1998
Grades for the Project
–
–
–
–
–
Scanner/Parser
S
Semantic
ti Checking
Ch ki
Code Generation
D t fl
Data-flow
Analysis
A l i
Optimizations
Saman Amarasinghe
5%
7 5%
7.5%
10%
7 5%
7.5%
30%
60%
11
6.035
©MIT Fall 1998
Optimization Segment
• Making programs run fast
–
–
–
–
We provide a test set of applications
Figure-out what will make them run fast
Prioritize and implement the optimizations
Compiler derby at the end
• A “similar” application to the test set is provided the day before
• The compiler that produced the fastest code is the winner
• Do any optimizations you choose
– Including parallelization for multicores
• Grade is divided into:
– Documentation
• Justifyy yyour optimizations
p
and the selection p
process
6%
– Optimization Implementation
12%
– Derby performance
12%
• Producing correct code
30%
Saman Amarasinghe
12
6.035
©MIT Fall 1998
The Quiz
• Three Quizzes
• In-Class Quiz
– 50 Minutes (be on time!)
– Open book, open notes
Saman Amarasinghe
13
6.035
©MIT Fall 1998
Mini Quizzes
• You already got one.
one
• Given at the beginning of the class; Collected at
th end
the
d
• Collaboration is OK
• This is in lieu of time consuming problem sets
Saman Amarasinghe
14
6.035
©MIT Fall 1998
Outline
• Course Administration Information
• Introduction to computer language engineering
– What are compilers?
– Why should we learn about them?
– Anatomy of a compiler
Saman Amarasinghe
15
6.035
©MIT Fall 1998
Why Study Compilers?
• Compilers enable programming at a high level
language instead of machine instructions.
– Malleability
Malleability, Portability,
Portability Modularity,
Modularity Simplicity,
Simplicity
Programmer Productivity
– Also Efficiency and Performance
Saman Amarasinghe
16
6.035
©MIT Fall 1998
Compilers Construction touches
many topics
t i in
i Computer
C
t Science
S i
• Theory
– Finite
Fi it State
St t A
Automata,
t
t G
Grammars and
dP
Parsing,
i
d
data-flow
t fl
• Algorithms
– Graph manipulation, dynamic programming
• Data structures
– Symbol tables, abstract syntax trees
• Systems
– Allocation and naming, multi-pass systems, compiler construction
• Computer Architecture
– Memory
M
hierarchy,
hi
h instruction
i t ti selection,
l ti
interlocks
i t l k and
d latencies,
l t
i
parallelism
ll li
• Security
– Detection of and Protection against vulnerabilities
• Software Engineering
– Software development environments, debugging
• Artificial Intelligence
– Heuristic based search for best optimizations
Saman Amarasinghe
17
6.035
©MIT Fall 1998
Power of a Language
• Can use to describe any action
– Not tied to a “context”
• Many
M
ways to
t d
describe
ib the
th same action
ti
– Flexible
Saman Amarasinghe
18
6.035
©MIT Fall 1998
How to instruct a computer
• How about natural languages?
–
–
–
–
English??
“Open the pod bay doors, Hal.”
“I am sorryDDave, I am afraid
f id I cannott do
d that”
th t”
We are not there yet!!
• Natural Languages:
– Powerful, but…
– Ambiguous
• Same expression describes many possible actions
Saman Amarasinghe
19
6.035
©MIT Fall 1998
Programming Languages
• Properties
–
–
–
–
need
need
need
need
Saman Amarasinghe
to
to
to
to
be
be
be
be
precise
concise
expressive
at a high-level (lot of abstractions)
20
6.035
©MIT Fall 1998
High-level Abstract Description
t Low-level
to
L
l
l Implementation
I
l
t ti
Details
D t il
My poll ratings are low,
lets invade a small nation
President
General
Cross the river and take
defensive positions
Sergeant
Forward march,
march turn left
Stop!, Shoot
Foot Soldier
Saman Amarasinghe
21
Figure by MIT OpenCourseWare.
6.035
©MIT Fall 1998
1. How to instruct the computer
• Write a program using a programming language
– High-level Abstract Description
• Microprocessors talk in assembly language
– Low-level Implementation Details
Program
written
in a
Programming
Languages
Saman Amarasinghe
Compiler
p
22
Assembly
Language
Translation
6.035
©MIT Fall 1998
1. How to instruct the computer
• Input: High-level
High level programming language
• Output: Low-level assembly instructions
• Compiler
p
does the translation:
–
–
–
–
Read and understand the program
Preciselyy determine what actions it require
q
Figure-out how to faithfully carry-out those actions
Instruct the computer to carry out those actions
Saman Amarasinghe
23
6.035
©MIT Fall 1998
Input to the Compiler
• Standard imperative language (Java,
(Java C,
C C++)
– State
• Variables,
Variables
• Structures,
y
• Arrays
– Computation
•
•
•
•
Expressions (arithmetic, logical, etc.)
Assignment statements
Control flow (conditionals, loops)
P
Procedures
d
Saman Amarasinghe
24
6.035
©MIT Fall 1998
Output of the Compiler
• State
– Registers
– Memory with Flat Address S
Space
pace
• Machine code – load/store architecture
– Load, store instructions
– Arithmetic, logical operations on registers
– Branch instructions
Saman Amarasinghe
25
6.035
©MIT Fall 1998
Example (input program)
int sumcalc(int a, int b, int N)
{
int i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
}
Saman Amarasinghe
26
6.035
©MIT Fall 1998
Example (Output assembly code)
sumcalc:
.L2:
.L3:
Saman Amarasinghe
.size
pushq
movq
movl
movl
movl
movl
movl
movl
movl
cmpl
l
jg
movl
leal
leaq
movq
movl
movq
cltd
idivl
movl
movl
u
imull
movl
incl
imull
addl
leaq
addl
movl
movl
imull
leaq
addl
leaq
i l
incl
jmp
movl
leave
ret
sumcalc, .-sumcalc
.section
.Lframe1:
.long
.LECIE1-.LSCIE1
.LSCIE1:.long
0x0
.byte
0x1
.string ""
.uleb128 0x1
.sleb128 -8
.byte
0x10
.byte
b t
0
0xc
.uleb128 0x7
.uleb128 0x8
.byte
0x90
.uleb128 0x1
.align
8
.LECIE1:.long
LECIE1: long
.LEFDE1
LEFDE1-.LASFDE1
LASFDE1
.long
.LASFDE1-.Lframe1
.quad
.LFB2
.quad
.LFE2-.LFB2
.byte
0x4
.long
.LCFI0-.LFB2
.byte
0 e
0xe
.uleb128 0x10
.byte
0x86
.uleb128 0x2
.byte
0x4
.long
.LCFI1-.LCFI0
.byte
0xd
.uleb128 0x6
.align
8
%rbp
%rsp, %rbp
%edi, -4(%rbp)
%esi, -8(%rbp)
%edx, -12(%rbp)
$0, -20(%rbp)
$0, -24(%rbp)
$0, -16(%rbp)
-16(%rbp), %eax
-12(%rbp),
12(% b ) %
%eax
.L3
-4(%rbp), %eax
0(,%rax,4), %edx
-8(%rbp), %rax
%rax, -40(%rbp)
%edx %eax
%edx,
-40(%rbp), %rcx
(%rcx)
%eax, -28(%rbp)
-28(%rbp), %edx
-16(%rbp),
6(% bp), %ed
%edx
-16(%rbp), %eax
%eax
%eax, %eax
%eax, %edx
-20(%rbp), %rax
%edx, (%rax)
-8(%rbp), %eax
%eax, %edx
-24(%rbp), %edx
-20(%rbp), %rax
%edx, (%rax)
-16(%rbp), %rax
(%
(%rax)
)
.L2
-20(%rbp), %eax
27
6.035
©MIT Fall 1998
Mapping Time Continuum
C
Compilation
il ti
to
t Interpretation
I t
t ti
• Compile time
– Ex: C compiler
• Link
Li k time
ti
– Ex: Binary layout optimizer
• Load time
– Ex: JIT compiler
• Run time
– Ex: Java Interpreter
Saman Amarasinghe
28
6.035
©MIT Fall 1998
Anatomy of a Computer
Program
written
in a
Programming
Languages
Saman Amarasinghe
p
Compiler
29
Assembly
Language
Translation
6.035
©MIT Fall 1998
Anatomy of a Computer
Program (character stream)
L i l Analyzer
Lexical
A l
(Scanner)
(S
)
Token Stream
Saman Amarasinghe
30
6.035
©MIT Fall 1998
Lexical Analyzer (Scanner)
2 3 4
*
( 1 1
+ - 2 2 )
Num(234) mul_op lpar_op Num(11) add_op Num(-22) rpar_op
Saman Amarasinghe
31
6.035
©MIT Fall 1998
Lexical Analyzer (Scanner)
2 3 4
*
( 1 1
+ - 2 2 )
Num(234) mul_op lpar_op Num(11) add_op Num(-22) rpar_op
18..23 + val#ue
V i bl names cannott hhave ‘#’ character
Variable
h
t
Not a number
Saman Amarasinghe
32
6.035
©MIT Fall 1998
Anatomy of a Computer
Program (character stream)
L i lA
Lexical
Analyzer
l
(Scanner)
(S
)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Saman Amarasinghe
33
6.035
©MIT Fall 1998
Syntax Analyzer (Parser)
num ‘*’ ‘(‘ num ‘‘+’’ num ‘)’
<expr>
<expr>
num
<op>
<expr>
*
<expr>
(
<expr> <op>
num
Saman Amarasinghe
34
+
)
<expr>
num
6.035
©MIT Fall 1998
Syntax Analyzer (Parser)
int * foo(i, j, k))
int i;
int j;
Extra parentheses
{
for(i=0; i j) {
fi(i>j)
Missing increment
return j;
Not an expression
}
Not a keyword
Saman Amarasinghe
35
6.035
©MIT Fall 1998
Anatomy of a Computer
Program (character stream)
L i lA
Lexical
Analyzer
l
(Scanner)
(S
)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Semantic Analyzer
Intermediate Representation
Saman Amarasinghe
36
6.035
©MIT Fall 1998
Semantic Analyzer
int * foo(i, j, k)
int i;
int j;
{
int x;
x = x + j + N;
return j;
}
Saman Amarasinghe
37
Type not declared
Mismatched return type
Uninitialized variable used
Undeclared variable
6.035
©MIT Fall 1998
Anatomy of a Computer
Program (character stream)
L i lA
Lexical
Analyzer
l
(Scanner)
(S
)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Semantic Analyzer
Intermediate Representation
Code Optimizer
Optimized Intermediate Representation
Saman Amarasinghe
38
6.035
©MIT Fall 1998
Optimizer
int sumcalc(int a, int b, int N)
{
int i;
int x, t, u, v;
x = 0;
u = ((a<<2)/b);
v = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + v + t*t;
v = v + u;
}
return x;
}
int sumcalc(int a, int b, int N)
{
int i;
int x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x+4*a/b*i+(i+1)*(i+1);
x = x + b*y;
}
return x;
}
Saman Amarasinghe
39
6.035
©MIT Fall 1998
Anatomy of a Computer
Program (character stream)
L i lA
Lexical
Analyzer
l
(Scanner)
(S
)
Token Stream
Syntax Analyzer (Parser)
Parse Tree
Semantic Analyzer
Intermediate Representation
Code Optimizer
Optimized Intermediate Representation
Code Generator
Assembly code
Saman Amarasinghe
40
6.035
©MIT Fall 1998
Code Generator
int sumcalc(int a, int b, int N)
{
int i;
int x, t, u, v;
x = 0;
u = ((a<<2)/b);
(( <<2)/b)
v = 0;
for(i = 0; i <= N; i++) {
t = i+1;
;
x = x + v + t*t;
v = v + u;
}
return
t
x;
}
Saman Amarasinghe
41
sumcalc:
xorl
xorl
movl
cmpl
jg
sall
.L5:
movl
cltd
idivl
leal
movl
imull
movl
imull
leal
movl
addl
cmpl
jle
.L7:
movl
ret
%r8d, %r8d
%ecx
%ecx, %ecx
%edx, %r9d
%edx, %r8d
.L7
$2, %edi
, %eax
%edi,
%esi
1(%rcx), %edx
%eax, %r10d
%ecx, %r10d
%edx, %ecx
%edx, %ecx
(%r10,%rcx), %eax
%edx, %ecx
%eax, %r8d
% 9
%r9d,
%
%edx
.L5
%r8d, %eax
6.035
©MIT Fall 1998
Program Translation
• Correct
– The actions requested by the program has to be
faithfully executed
• Efficient
– Intelligently and efficiently use the available resources to
carry out the requests
– (the word optimization is used loosely in the compiler
community – Optimizing compilers are never optimal)
Saman Amarasinghe
42
6.035
©MIT Fall 1998
Efficient Execution
Cross the river and take
defensive positions
General
Sergeant
Foot Soldier
Figure by MIT OpenCourseWare.
Saman Amarasinghe
43
6.035
©MIT Fall 1998
Efficient Execution
Cross the river and take
defensive positions
General
Where to cross the river? Use the
bridge upstream or surprise the enemy
by crossing downstream?
How do I minimize the casualties??
Sergeant
Foot Soldier
Saman Amarasinghe
44
Figure by MIT OpenCourseWare.
6.035
©MIT Fall 1998
Efficient Execution
My poll ratings are low,
lets invade a small nation
President
Russia or Bermuda?
Or just stall for his poll
numbers to go up?
General
Figure by MIT OpenCourseWare.
Saman Amarasinghe
45
6.035
©MIT Fall 1998
Efficient Execution
• Mapping from High to Low
– Simple mapping of a program to assembly language
produces inefficient execution
– Higher the level of abstraction  more inefficiency
• If not efficient
– High-level abstractions are useless
• Need
d to:
– provide a high level abstraction
– with performance of giving low-level instructions
Saman Amarasinghe
46
6.035
©MIT Fall 1998
Efficient Execution help
i
increase
the
th llevell off abstraction
b t
ti
• Programming languages
– From C to OO-languages
with garbage collection
– Even more abstract
definitions
• Microprocessor
– From simple CISC to RISC to
VLIW to ….
Saman Amarasinghe
47
6.035
©MIT Fall 1998
The Multicore Dilemma
• Superscalars
• Multicores
High
g Level Language
g g
Simple von Neumann Machine
Multiple exposed cores
Harrd
warre
H
Hard
ware
w
Saman Amarasinghe
Co
omp
ile
er??
Com
pile
er
High Level Language
48
6.035
©MIT Fall 1998
The Multicore Dilemma
• Superscalars
• Multicores
Com
pile
er
High
g Level Language
g g
Simple von Neumann Machine
Multiple exposed cores
Harrd
warre
H
Hard
ware
w
Saman Amarasinghe
Com
m
pilerr
Parallel Language
49
6.035
©MIT Fall 1998
Optimization Example
int sumcalc(int a, int b, int N)
{
int i;
int x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
}
Saman Amarasinghe
50
6.035
©MIT Fall 1998
pushq
%rbp
movq
%rsp, %rbp
movl
%edi, -4(%rbp)
movl
%esi, -8(%rbp)
movl
l
%
%edx,
d
-12(%rbp)
12(% b )
movl
$0, -20(%rbp)
movl
$0, -24(%rbp)
movl
$0, -16(%rbp)
.L2:
movl
-16(%rbp), %eax
cmpl
12(%rbp) %eax
12(%rbp),
jg
.L3
movl - -4(%rbp), %eax
leal
0(,%rax,4), %edx
leaq
-8(%rbp), %rax
movq
%rax
%rax, -40(%rbp)
40(%rbp)
movl
%edx, %eax
movq
-40(%rbp), %rcx
cltd
idivl
(%rcx)
movl
%eax,
, -28(%rbp)
(
p)
movl
-28(%rbp), %edx
imull
-16(%rbp), %edx
movl
-16(%rbp), %eax
incl
%eax
imull
%eax, %eax
addl
%eax, %edx
leaq
-20(%rbp), %rax
addl
%edx, (%rax)
movl
-8(%rbp), %eax
movl
%eax, %edx
i ll
imull
24(% b ) %
24(%rbp),
%edx
d
leaq - -20(%rbp), %rax
addl
%edx, (%rax)
leaq
-16(%rbp), %rax
incl
(%rax)
jmp
L2
.L3:
movl
-20(%rbp), %eax
leave
.
ret
Saman Amarasinghe
45
51
6.035
©MIT Fall 1998
Lets Optimize...
int sumcalc(int a, int b, int N)
{
int i, x, y;
x = 0;
y = 0;
o ( = 0
0;
; i <= N;
; i++)
) {
for(i
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
}
Saman Amarasinghe
52
6.035
©MIT Fall 1998
Constant Propagation
int i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
Saman Amarasinghe
53
6.035
©MIT Fall 1998
Constant Propagation
int i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*y;
}
return x;
Saman Amarasinghe
54
6.035
©MIT Fall 1998
Constant Propagation
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*0;
}
return x;
Saman Amarasinghe
55
6.035
©MIT Fall 1998
Algebraic Simplification
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*0;
}
return x;
Saman Amarasinghe
56
6.035
©MIT Fall 1998
Algebraic Simplification
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x + b*0;
}
return x;
Saman Amarasinghe
57
6.035
©MIT Fall 1998
Algebraic Simplification
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x;
}
return x;
Saman Amarasinghe
58
6.035
©MIT Fall 1998
Copy Propagation
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x;
}
return x;
Saman Amarasinghe
59
6.035
©MIT Fall 1998
Copy Propagation
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
x = x;
}
return x;
Saman Amarasinghe
60
6.035
©MIT Fall 1998
Copy Propagation
int
i
t i
i, x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
}
return x;
Saman Amarasinghe
61
6.035
©MIT Fall 1998
Common Subexpression Elimination
int i
i, x
x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
}
return x;
Saman Amarasinghe
62
6.035
©MIT Fall 1998
Common Subexpression Elimination
int i
i, x
x, y;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
x = x + (4*a/b)*i + (i+1)*(i+1);
}
return x;
Saman Amarasinghe
63
6.035
©MIT Fall 1998
Common Subexpression Elimination
int
i
t i
i, x, y, t;
t
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
64
6.035
©MIT Fall 1998
Dead Code Elimination
int
i
t i
i, x, y, t
t;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
65
6.035
©MIT Fall 1998
Dead Code Elimination
int
i
t i
i, x, y, t
t;
x = 0;
y = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
66
6.035
©MIT Fall 1998
Dead Code Elimination
int
i
t i
i, x, t;
t
x = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
67
6.035
©MIT Fall 1998
Loop Invariant Removal
int
i
t i
i, x, t;
t
x = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
68
6.035
©MIT Fall 1998
Loop Invariant Removal
int
i
t i
i, x, t;
t
x = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + (4*a/b)*i + t*t;
}
return x;
Saman Amarasinghe
69
6.035
©MIT Fall 1998
Loop Invariant Removal
int
i
t i
i, x, t,
t u;
x = 0;
u = (4*a/b);
/
for(i = 0; i <= N; i++) {
t = i+1;
x = x + u*i + t*t;
}
return x;
Saman Amarasinghe
70
6.035
©MIT Fall 1998
Strength Reduction
int
i
t i
i, x, t,
t u;
x = 0;
u = (4*a/b);
/
for(i = 0; i <= N; i++) {
t = i+1;
x = x + u*i + t*t;
}
return x;
Saman Amarasinghe
71
6.035
©MIT Fall 1998
Strength Reduction
int
i
t i
i, x, t,
t u;
x = 0;
u = (4*a/b);
/
for(i = 0; i <= N; i++) {
t = i+1;
x = x + u*i + t*t;
}
return x;
Saman Amarasinghe
72
6.035
©MIT Fall 1998
Strength Reduction
int i
i, x
x, t
t, u
u, v;
x = 0;
u = ((a<<2)/b);
v = 0;
for(i = 0; i <=
< N; i++) {
t = i+1;
x = x + v + t*t;
v = v + u;
}
return x;
Saman Amarasinghe
73
6.035
©MIT Fall 1998
Register Allocation
fp
Local variable X
Local variable Y
Local variable I
Saman Amarasinghe
74
6.035
©MIT Fall 1998
Register Allocation
fp
Local variable X
Local variable Y
Local variable I
$r8d
$r9d
$r10d
$ebx
$ecx
=
=
=
=
=
X
t
u
v
i
Saman Amarasinghe
75
6.035
©MIT Fall 1998
Optimized Example
int sumcalc(int a, int b, int N)
{
int i, x, t, u, v;
x = 0;
u = ((a<<2)/b);
v = 0;
for(i = 0; i <= N; i++) {
t = i+1;
x = x + v + t*t;
v = v + u;
}
return x;
}
Saman Amarasinghe
76
6.035
©MIT Fall 1998
Unoptimized Code
pushq
movq
movl
movl
movl
movl
movl
movl
.L2:
cmpl
movl
jg
movl
leal
leaq
movq
movl
movq
cltd
idi l
idivl
movl
movl
imull
movl
incl
imull
addl
leaq
addl
movl
movl
imull
leaq
addl
q
leaq
incl
jmp
.L3:
Optimized Code
xorl
xorl
movl
cmpl
jg
sall
%rbp
%rsp, %rbp
%edi, -4(%rbp)
4(%rbp)
%esi, -8(%rbp)
%edx, -12(%rbp)
$0, -20(%rbp)
$0, -24(%rbp)
$0, -16(%rbp)
-16(%rbp), %eax
-12(%rbp), %eax
.L3
L3
-4(%rbp), %eax
0(,%rax,4), %edx
-8(%rbp), %rax
%rax, -40(%rbp)
%edx, %eax
-40(%rbp), %rcx
.L5:
cltd
movl
idivl
leal
movl
imull
movl
imull
leal
movl
addl
cmpl
jl
jle
(%
(%rcx)
)
%eax, -28(%rbp)
-28(%rbp), %edx
-16(%rbp), %edx
-16(%rbp), %eax
%eax
%eax, %eax
%eax, %edx
-20(%rbp), %rax
%edx, (%rax)
-8(%rbp), %eax
%eax, %edx
-24(%rbp), %edx
-20(%rbp), %rax
%edx, (%rax)
-16(%rbp),
(
p), %rax
(%rax)
.L2
-20(%rbp), %eax
.L7:
%r8d, %r8d
%ecx, %ecx
%edx, %r9d
%edx, %r8d
.L7
$2, %edi
%edi, %eax
%esi
1(%rcx), %edx
%eax
%eax, %r10d
%ecx, %r10d
%edx, %ecx
%edx, %ecx
(%r10,%rcx), %eax
%edx, %ecx
%eax, %r8d
%r9d, %edx
.L5
L5
%r8d, %eax
ret
movl
leave
movl
ret
Inner Loop:
10*mov + 5*lea + 5*add/inc
+ 4*div/mul + 5*cmp/br/jmp
= 29 iinstructions
i
Execution time = 43 sec
Saman Amarasinghe
4*mov + 2*lea + 1*add/inc+
3*div/mul + 2*cmp/br/jmp
= 12 iinstructions
i
Execution time = 17 sec
77
6.035
©MIT Fall 1998
Compilers Optimize Programs for…
•
•
•
•
•
•
Performance/Speed
Code Size
Power Consumption
Fast/Efficient
/
Compilation
p
Security/Reliability
Debugging
Saman Amarasinghe
78
6.035
©MIT Fall 1998
MIT OpenCourseWare
http://ocw.mit.edu
6.035 Computer Language Engineering
Spring 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Download