Download presentation
Presentation is loading. Please wait.
1
Compilation 2007 Optimization Michael I. Schwartzbach BRICS, University of Aarhus
2
2 Optimization Optimization The optimizer aims at: reducing the runtime reducing the code size These goals often conflict, since a larger program may in fact be faster The best optimizations achive both goals An optimizer may also have more esoteric aims: reducing energy consumption reducing chip area
3
3 Optimization Optimizations for Space Were historically important, because memory was small and expensive When memory became large and cheap, optimizing compilers traded space for time Java compilers do not optimize much, but JVM bytecodes are designed to be small When Java is targeted at mobile devices, space optimizations are again important
4
4 Optimization Optimizations for Speed Were historically important to gain acceptance for the introduction of high-level languages Are still important, since the software always strains the limits of the hardware Are challenged by ever higher abstractions in programming languages and must constantly adapt to changing microprocessor architecures Java compilers do not optimize much, since the JVM kicks in with the JIT compiler
5
5 Optimization Opportunities for Optimization At the source code level At an intermediate low level At the binary machine code level At runtime (JIT compilers) At the hardware level An aggressive optimization requires many small contributions from all levels
6
6 Optimization Optimizers Must Undo Abstractions Variables abstract away from registers, so the optimizer must find an efficient mapping Control structures abstract away from gotos, so the optimizer must simplify a goto graph Data structures abstract away from memory, so the optimizer must find an efficient layout... Method invocations abstract away from procedure calls, so the optimizer must efficiently determine the intended implementation
7
7 Optimization Difficult Compromises A high abstraction level makes the development time cheaper, but the runtime more expensive An optimizing compiler makes runtime more efficient, but compile time less efficient Optimizations for speed and size may conflict Different applications may require different choices at different times
8
8 Optimization Examples of Optimizations Strength reduction Loop unrolling Common subexpression elimination Loop invariant code motion Inline expansion These may take place either at the source level or at the bytecode level Most require information from static analyses
9
9 Optimization Strength Reduction Replace expensive operations with cheap ones: for (i = 0; i < a.length; i++) a[i] = a[i] + i/4; for (i = 0; i < a.length; i++) a[i] += (i >> 2);
10
10 Optimization Loop Unrolling Unfold a loop to save condition tests: for (i = 0; i < 100; i++) g(i); for (i = 0; i < 100; i += 2) { g(i); g(i+1); }
11
11 Optimization Common Subexpression Elimination Avoid redundant computations: double d = a * Math.sqrt(c); double e = b * Math.sqrt(c); double tmp = Math.sqrt(c); double d = a * tmp; double e = b * tmp;
12
12 Optimization Loop Invariant Code Motion Move constant valued expressions outside loops: for (i = 0; i < a.length; i++) b[i] = a[i] + c * d; int tmp1 = a.length; int tmp2 = c * d; for (i = 0; i < tmp1; i++) b[i] = a[i] + tmp2;
13
13 Optimization Inline Expansion Replace method invocations with copies: int pred(int x) { if (x == 0) return x; else return x-1; } int f(int y) { return pred(y) + pred(0) + pred(y+1); } int f(int y) { int tmp = 0; if (y == 0) tmp += 0; else tmp += y-1; if (0 == 0) tmp += 0; else tmp += 0-1; if (y+1 ==0 ) tmp += 0; else tmp += (y+1)-1; return tmp; }
14
14 Optimization Collaborating Optimizations Optimizations may enable other optimizations: int f(int y) { int tmp = 0; if (y == 0) tmp += 0; else tmp += y-1; if (0 == 0) tmp += 0; else tmp += 0-1; if (y+1 == 0) tmp += 0; else tmp += (y+1)-1; return tmp; } int f(int y) { if (y == 0) return 0; else if (y == -1) return -2; else return y+y-1; }
15
15 Optimization Optimization in Joos public int foo(int a, int b, int c) { c = a*b+c; if (c<a) a = a+b*113; while (b>0) { a = a*c; b = b-1; } return a; } iload_1 iload_2 imul iload_3 iadd dup istore_3 pop iload_3 iload_1 if_icmplt true1 iconst_0 goto end2 true1: iconst_1 end2: ifeq false0 iload_1 iload_2 imul iload_3 iadd istore_3 iload_3 iload_1 if_icmpge cond4 iload_1 iload_2 bipush 113 imul iadd istore_1 goto cond4 loop3: iload_1 iload_3 imul istore_1 iinc 2 -1 cond4: iload_2 ifgt loop3 iload_1 ireturn iload_1 iload_2 bipush 113 imul iadd dup istore_1 pop false0: goto cond4 loop3: iload_1 iload_3 imul dup istore_1 pop iload_2 iconst_1 isub dup istore_2 pop cond4: iload_2 iconst_0 if_icmpgt true5 iconst_0 goto end6 true5: iconst_1 end6: ifne loop3 iload_1 ireturn 52 bytecodes 27 bytecodes
16
16 Optimization Peephole Optimizations Make local improvements in bytecode sequences The optimizers considers only finite windows of the sequence When the pattern "clicks", the optimizer rewrites a part of the code using a template: dup istore 3 istore 3 pop
17
17 Optimization Peephole Transitions Let P be a collection of peephole patterns It defines a transition relation on sequences of bytecodes: B 1 B 2 meaning that p P clicked at some position in the sequence B 1 and produced the sequence B 2 p
18
18 Optimization Termination A collection of peephole patterns must terminate This means that for the collection P, there must not exist an infinite sequence: B 0 B 1 B 2 B 3... for any B 0 and p i P p1p1 p2p2 p3p3 p4p4
19
19 Optimization Soundness (1/2) Every peephole pattern must preserve semantics Assume the pattern p transforms a bytecode sequence B 1 into the sequence B 2 Consider now any bytecode context C If C[B 1 ] emits the verifiable code E 1, then C[B 2 ] must emit some verifiable code E 2 with the same semantics
20
20 Optimization Soundness (2/2) C B 1 :C B 1 B 2 E 1 E 2 p emit
21
21 Optimization A Peephole Pattern Language Joos has a domain-specific language for specifying peephole patterns The Joos compiler contains an interpreter for this peephole language It is invoked with the option -O patternfile It will try all patterns in an unspecified order until no pattern clicks anywhere
22
22 Optimization Pattern Syntax pattern → pattern name var : exp -> intconst templates The exp determines whether the pattern clicks The intconst tells how many bytecodes to replace The template specifies the new bytecodes The evaluation of exp produces a set of bindings that may be used inside the templates and later in the expression
23
23 Optimization Expression Types The following types are possible results: int label type-signature field-signature method-signature string condition bytecodes boolean The notation inst(σ 1,..., σ k ) means that the given instruction has these arguments in the JVM specification
24
24 Optimization exp intop exp | exp intcomp exp | exp comp exp | exp ~ peepholes | ! exp | exp && exp | exp || exp | intconst | condconst Pattern Expressions exp →var | degree var | target var | formals var | returns var | negate exp | commute exp |
25
25 Optimization Peepholes and Templates peepholes →peephole* peephole →instruction | instruction ( vars ) | * |(any single instruction) var : (label binder) template →template* template →instruction | instruction ( exps ) condconst → eq | ne | lt | le | gt | ge | aeq | ane intop → + | - | * | / | % intcomp → | >= comp → == | !=
26
26 Optimization Peephole Judgements The judgement: |- E: σ[ → '] means that the expression E: evaluates to a result of type σ consumes the bindings produces the bindings ' The judgement: |- X: [ → '] similarly describes peepholes, templates, and patterns
27
27 Optimization Expression Well-Formedness (1/5) (x) = σ |- x: σ[ → ] (x) = label |- degree x: int[ → ] (x) = label |- target x: bytecodes[ → ] (x) = method-signature |- formals x: int[ → ]
28
28 Optimization Expression Well-Formedness (2/5) |- E: condition[ → '] |- negate E: condition[ → '] (x)= method-signature |- returns x: int[ → ] |- E: condition[ → '] |- commute E: condition[ → ']
29
29 Optimization Expression Well-Formedness (3/5) |- E 1 : int[ → '] |- E 2 : int[ ' → ''] |- E 1 intop E 2 : int[ → ''] |- E 1 : int[ → '] |- E 2 : int[ '→ ''] |- E 1 intcomp E 2 : boolean[ → ''] |- E 1 : σ[ → '] |- E 2 : σ[ '→ ''] |- E 1 comp E 2 : boolean[ → '']
30
30 Optimization Expression Well-Formedness (4/5) |- E: bytecodes[ → '] |- P[ '→ ''] |- E ~ P: boolean[ → ''] |- E: boolean[ → '] |- ! E: boolean[ → ] |- E 1 : boolean[ → '] |- E 2 : boolean[ '→ ''] |- E 1 && E 2 : boolean[ → '']
31
31 Optimization Expression Well-Formedness (5/5) |- E 1 : boolean[ → '] |- E 2 : boolean[ → ''] x: '(x)= ' ''(x)= '' ' = '' |- E 1 || E 2 : boolean[ → ' ''] |- k: int[ → ] |- cond: condition[ → ]
32
32 Optimization Peephole Well-Formedness (1/2) |- P i [ i → i+1 ] |- P 1 P 2...P k [ 1 → k+1 ] |- inst: [ → ] x i ≠ x j x i inst(σ 1,..., σ k ) |- inst(x 1,...,x k )[ → [x i →σ i ]]
33
33 Optimization Peephole Well-Formedness (2/2) |- * : [ → ] |- x : : [ → [x→label]] |- label (x) : [ → [x→label]]
34
34 Optimization Template Well-Formedness |- T i : [ i → i+1 ] |- T 1 T 2...T k : [ 1 → k+1 ] |- inst: [ → ] |- E i : σ i [ i → i+1 ] inst(σ 1,..., σ k ) |- inst(E 1,...,E k )[ 1 → k+1 ] |- E: label [ 1 → 2 ] |- E:inst: [ 1 → 2 ]
35
35 Optimization Pattern Well-Formedness |- E: boolean[[x→bytecodes] → ] |- T[ → '] |- pattern n x : E -> k T: [[]→ ']
36
36 Optimization Pattern Examples (1/4) pattern dup_istore_pop x: x ~ dup istore (i0) pop -> 3 istore (i0) This pattern is relevant for code like: x = a*b;
37
37 Optimization Pattern Examples (2/4) pattern goto_label x: x ~ goto (l1) label (l2) && l1 == l2 -> 1 This pattern arises during optimization of nested control structures
38
38 Optimization Pattern Examples (3/4) pattern constant_iadd_residue x: x ~ ldc_int (i0) iadd ldc_int (i1) iadd -> 4 ldc_int (i0+i1) iadd This pattern is relevant for code like: a+5+7
39
39 Optimization Pattern Examples (4/4) pattern goto_goto x: x ~ goto (l0) && target l0 ~ goto (l1) && ! (target l1 ~ goto (l2)) && ! (target l1 ~ label (l3)) -> 1 goto (l1) This pattern arises during optimization of nested control structures
40
40 Optimization Proving Termination We want to avoid infinite sequences like: B 0 B 1 B 2 B 3... Define an integer valued function such that: B: (B) 0 p P: B 1 B 2 (B 2 ) < (B 1 ) p1p1 p2p2 p3p3 p4p4 p
41
41 Optimization Termination Function Example For our 4 example patterns we define: (B) = # dup + # goto + # iadd + ??? What gets smaller in the goto_goto pattern?
42
42 Optimization Termination Function Example For our 4 example patterns we define: (B) = # dup + # goto + # iadd + ??? What gets smaller in the goto_goto pattern? label (l 1 ) B l 1 → l 2 → l 3 →... → l k l i ≠ l j goto k
43
43 Optimization A Non-Terminating Pattern pattern bad_goto_goto x: x ~ goto (l0) && target l0 ~ goto (l1) -> 1 goto (l1) foo: goto bar bar: goto foo
44
44 Optimization Proving Soundness A formal proof of soundness for a collection of patterns requires a full formal semantics of: bytecode sequences peephole patterns bytecode contexts code emission the complete JVM The pitfall is usually the universal quantification of contexts: does this really always work?
45
45 Optimization An Unsound Pattern pattern idiv_pop x: x ~ idiv pop -> 1 pop This pattern may actually click And the resulting bytecode will always verify But the semantics is not preserved, since it may remove a java.lang.ArithmeticException
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.