Introduction to Optimization1
Kristoffer H. [email protected]
Compiler ConstructionCSCI-GA.2130-001/Spring 2014
NYU Courant Institute
April 28, 2014
1ALSU §9.1K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 1 / 13
Sixth compilation phase
source program
Lexical Analysis--
Syntax AnalysisTokens
--
Semantic AnalysisTree
--
Intermediate Representation GeneratorTree
--
SymbolTable
OptimizerIR--
Code GeneratorIR--
Machine-Dependent Code OptimizerAsm
--
target machine code��
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 2 / 13
Sources of Redundancy
I Programmer “cut-n-paste”I Compiler templates.I Insufficient state transfer.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 3 / 13
Sources of Redundancy
I Programmer “cut-n-paste”I Compiler templates.I Insufficient state transfer.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 3 / 13
Sources of Redundancy
I Programmer “cut-n-paste”I Compiler templates.I Insufficient state transfer.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 3 / 13
Example
void quicksort(int a[], int m, int n){int i, j, v, x; if (n <= m) return;
i = m-1; j = n; v = a[n];while (1) {do i = i+1; while (a[i] < v);do j = j-1; while (a[j] > v);if (i >= j) break;x = a[i]; a[i] = a[j]; a[j] = x;
}x = a[i]; a[i] = a[n]; a[n] = x;
quicksort(a,m,j); quicksort(a,i+1,n);}
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 4 / 13
Basic Blocks
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = a[t6]t7 = 4*it8 = 4*jt9 = a[t8]a[t7] = t9t10 = 4*ja[t10] = xgoto B2
B5:ww
��t11 = 4*ix = a[t11]t13 = 4*nt14 = a[t13]a[t11] = t14t15 = 4*na[t15] = x
B6:%%
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 5 / 13
(Local) Common Subexpression Elimination
t6 = 4*ix = a[t6]t7 = 4*it8 = 4*jt9 = a[t8]a[t7] = t9t10 = 4*ja[t10] = xgoto B2
B5:
⇒
t6 = 4*ix = a[t6]t8 = 4*jt9 = a[t8]a[t6] = t9a[t8] = xgoto B2
B5:
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 6 / 13
(Local) Common Subexpression Elimination
t6 = 4*ix = a[t6]t7 = 4*it8 = 4*jt9 = a[t8]a[t7] = t9t10 = 4*ja[t10] = xgoto B2
B5:
⇒
t6 = 4*ix = a[t6]t8 = 4*jt9 = a[t8]a[t6] = t9a[t8] = xgoto B2
B5:
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 6 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = a[t6]t8 = 4*jt9 = a[t8]a[t6] = t9a[t8] = xgoto B2
B5:yy��
t11 = 4*ix = a[t11]t13 = 4*nt14 = a[t1]a[t11] = t14a[t13] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = a[t6]t8 = 4*jt9 = a[t8]a[t6] = t9a[t8] = xgoto B2
B5:yy��
t11 = 4*ix = a[t11]t13 = 4*nt14 = a[t1]a[t11] = t14a[t13] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = a[t2]t8 = 4*jt9 = a[t8]a[t2] = t9a[t8] = xgoto B2
B5:yy��
t11 = 4*ix = a[t11]t13 = 4*nt14 = a[t1]a[t11] = t14a[t13] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = t3t8 = 4*jt9 = a[t8]a[t2] = t9a[t8] = xgoto B2
B5:yy��
t11 = 4*ix = a[t11]t13 = 4*nt14 = a[t1]a[t11] = t14a[t13] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = t3t8 = 4*jt9 = a[t8]a[t2] = t9a[t8] = xgoto B2
B5:yy��
t11 = 4*ix = a[t11]t13 = 4*nt14 = a[t1]a[t11] = t14a[t13] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = t3
a[t2] = t5a[t4] = xgoto B2
B5:yy��
t11 = 4*ix = a[t11]t13 = 4*nt14 = a[t1]a[t11] = t14a[t13] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = t3
a[t2] = t5a[t4] = xgoto B2
B5:yy��
t11 = 4*ix = a[t11]t13 = 4*nt14 = a[t1]a[t11] = t14a[t13] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = t3
a[t2] = t5a[t4] = xgoto B2
B5:yy��
t11 = 4*ix = t3t13 = 4*nt14 = a[t1]a[t2] = t14a[t1] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = t3
a[t2] = t5a[t4] = xgoto B2
B5:yy��
t11 = 4*ix = t3t13 = 4*nt14 = a[t1]a[t2] = t14a[t1] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Global Common Subexpression Elimination
i = m-1j = nt1 = 4*nv = a[t1]
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
t6 = 4*ix = t3
a[t2] = t5a[t4] = xgoto B2
B5:yy��
t11 = 4*ix = t3t13 = 4*nt14 = a[t1]a[t2] = t14a[t1] = x
B6:##
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 7 / 13
Copy Propagation
t = d + ea = d + e
B1: t = d + eb = d + e
B2:
c = d + eB3:$$ zz
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 8 / 13
Copy Propagation
t = d + ea = t
B1: t = d + eb = t
B2:
c = tB3:$$ zz
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 8 / 13
Code Motion
while (i <= limit -2) /*not changing limit*/
becomes
t = limit -2;while (i <= t) /*not changing limit or t*/
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 9 / 13
Code Motion
while (i <= limit -2) /*not changing limit*/
becomes
t = limit -2;while (i <= t) /*not changing limit or t*/
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 9 / 13
Induction Invariants and Reduction in Strength
i = m-1j = nt1 = 4*nv = a[t1]t2 = 4*it4 = 4*j
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
x = t3a[t2] = t5a[t4] = xgoto B2
B5:}}
��
x = t3t14 = a[t1]a[t2] = t14a[t1] = x
B6:!!
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 10 / 13
Induction Invariants and Reduction in Strength
i = m-1j = nt1 = 4*nv = a[t1]t2 = 4*it4 = 4*j
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = 4*jt5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
x = t3a[t2] = t5a[t4] = xgoto B2
B5:}}
��
x = t3t14 = a[t1]a[t2] = t14a[t1] = x
B6:!!
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 10 / 13
Induction Invariants and Reduction in Strength
i = m-1j = nt1 = 4*nv = a[t1]t2 = 4*it4 = 4*j
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = t4-4t5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
x = t3a[t2] = t5a[t4] = xgoto B2
B5:}}
��
x = t3t14 = a[t1]a[t2] = t14a[t1] = x
B6:!!
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 10 / 13
Induction Invariants and Reduction in Strength
i = m-1j = nt1 = 4*nv = a[t1]t2 = 4*it4 = 4*j
B1:
i = i+1t2 = 4*it3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = t4-4t5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
x = t3a[t2] = t5a[t4] = xgoto B2
B5:}}
��
x = t3t14 = a[t1]a[t2] = t14a[t1] = x
B6:!!
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 10 / 13
Induction Invariants and Reduction in Strength
i = m-1j = nt1 = 4*nv = a[t1]t2 = 4*it4 = 4*j
B1:
i = i+1t2 = t2+4t3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = t4-4t5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
x = t3a[t2] = t5a[t4] = xgoto B2
B5:}}
��
x = t3t14 = a[t1]a[t2] = t14a[t1] = x
B6:!!
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 10 / 13
Induction Invariants and Reduction in Strength
i = m-1j = nt1 = 4*nv = a[t1]t2 = 4*it4 = 4*j
B1:
i = i+1t2 = t2+4t3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = t4-4t5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
x = t3a[t2] = t5a[t4] = xgoto B2
B5:}}
��
x = t3t14 = a[t1]a[t2] = t14a[t1] = x
B6:!!
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 10 / 13
Induction Invariants and Reduction in Strength
i = m-1j = nt1 = 4*nv = a[t1]t2 = 4*it4 = 4*j
B1:
i = i+1t2 = t2+4t3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = t4-4t5 = a[t4]if t5>v goto B3
B3:�� ��
if i>=j goto B6B4:##
x = t3a[t2] = t5a[t4] = xgoto B2
B5:}}
��
x = t3t14 = a[t1]a[t2] = t14a[t1] = x
B6:!!
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 10 / 13
Induction Invariants and Reduction in Strength
i = m-1j = nt1 = 4*nv = a[t1]t2 = 4*it4 = 4*j
B1:
i = i+1t2 = t2+4t3 = a[t2]if t3<v goto B2
B2:�� ��
j = j-1t4 = t4-4t5 = a[t4]if t5>v goto B3
B3:�� ��
if t2>=t4 goto B6B4:##
x = t3a[t2] = t5a[t4] = xgoto B2
B5:}}
��
x = t3t14 = a[t1]a[t2] = t14a[t1] = x
B6:!!
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 10 / 13
i = m-1t1 = 4*nv = a[t1]t2 = 4*it4 = t1
B1:
t2 = t2+4t3 = a[t2]if t3<v goto B2
B2:�� ��
t4 = t4-4t5 = a[t4]if t5>v goto B3
B3:�� ��
if t2>=t4 goto B6B4:!!
a[t2] = t5a[t4] = t3goto B2
B5:~~
��
t14 = a[t1]a[t2] = t14a[t1] = t3
B6:
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 11 / 13
Summary
Redundancy but preserve semantics!I Global Common Subexpressions.I Copy Propagation.I Dead-code Elimination.I Code Motion.I Induction Variables/Strength Reduction.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 12 / 13
Summary
Redundancy but preserve semantics!I Global Common Subexpressions.I Copy Propagation.I Dead-code Elimination.I Code Motion.I Induction Variables/Strength Reduction.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 12 / 13
Summary
Redundancy but preserve semantics!I Global Common Subexpressions.I Copy Propagation.I Dead-code Elimination.I Code Motion.I Induction Variables/Strength Reduction.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 12 / 13
Summary
Redundancy but preserve semantics!I Global Common Subexpressions.I Copy Propagation.I Dead-code Elimination.I Code Motion.I Induction Variables/Strength Reduction.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 12 / 13
Summary
Redundancy but preserve semantics!I Global Common Subexpressions.I Copy Propagation.I Dead-code Elimination.I Code Motion.I Induction Variables/Strength Reduction.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 12 / 13
Summary
Redundancy but preserve semantics!I Global Common Subexpressions.I Copy Propagation.I Dead-code Elimination.I Code Motion.I Induction Variables/Strength Reduction.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 12 / 13
Summary
Redundancy but preserve semantics!I Global Common Subexpressions.I Copy Propagation.I Dead-code Elimination.I Code Motion.I Induction Variables/Strength Reduction.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 12 / 13
Questions?
I HACS will recover this week, and pr2 will be restarted.I Because of GSAS conflict we swap final and pr3:
I Final is May 12.I pr3 will be due week of May 19.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 13 / 13
Questions?
I HACS will recover this week, and pr2 will be restarted.I Because of GSAS conflict we swap final and pr3:
I Final is May 12.I pr3 will be due week of May 19.
K.H.Rose CSCI-GA.2130-001/Spring 2014/Optimization April 28, 2014 13 / 13