Вы находитесь на странице: 1из 16

Advanced Compiler Design and Implementation

Introduction to Advanced Topics Chapter 1


Eran Yahav

Course textbook: Advanced compiler design and Implementation Steven S !uchnick IS"# 1$$%&'()'* T+o copies available in TA, librar.urchase -our o+n cop- / send e/mail to -ahave0math tau ac il

Outline
1evie+ o2 compiler structure Advanced issues in elementar- topics The importance o2 optimi3ations Structure o2 optimi3ing compilers .lacement o2 optimi3ations Tentative schedule Course re4uirements

Compiler Structure

Character string Scanner Scanner Tokens .arser .arser AST Semantic Semantic Anal-3er Anal-3er I1 Code Code >enerator >enerator =b?ect code Figure 1 - General structure of a non-optimizing one-pass compiler This section +ill give a short reminder o2 basic compiler structure and basic terminolog- I2 -ou are not 2amiliar +ith the terms5 -ou can 2ind more elaborate description in 617 or 6)7 8ets start b- a describing the structure o2 a non/optimi3ing one/pass compiler The structure o2 such a compiler is brought in details in 9igure 1 A simple non/optimi3ing one/pass compiler consists o2 the 2ollo+ing components:phases: 1 Scanner ;also kno+n as 8exical Anal-3er< / input 2or this phase is the stream o2 characters representing the source program During scanning5 the stream is read se4uentiall- and characters are grouped into meaning2ul tokens =utput o2 this phase is a token stream5 to be anal-3ed b- the parser ) .arser ;also kno+n as S-ntax Anal-3er< / input 2or this phase is a stream o2 tokens ;usuallproduced b- the lexical anal-3er< During parsing5 tokens are grouped into grammatical phrases The parsing phase produces abstract syntax tree ;AST< representation o2 the program ( Semantic Anal-3er / input 2or this phase is the AST5 produced b- the parser The semantic anal-sis phase veri2ies the program in terms o2 semantic errors5 and collects t-pe in2ormation later used b- the code/generator Semantic anal-sis usuall- produces some 2orm o2 intermediate representation ;I1< o2 the program * Code >enerator / input 2or this phase is the intermediate representation ;I1<5 produced b- the semantic anal-3er The code generator produces target code using the intermediate representation

S-mbol table and access routines

=S Inter2ace

The other t+o components in 9igure 1 / s-mbol table5 access routines and =S inter2ace are used b- most o2 compiler phases The s-mbol table and access routines record identi2iers used in the source program5 and collect in2ormation about various attributes o2 each identi2ier A s-mbol table is a data structure containing a record 2or each identi2ier +ith 2ields 2or the attributes o2 the identi2ier A naive implementation o2 a s-mbol table is 4uite trivial5 but5 as +e shall later see5 e22icient s-mbol table management is not at all simple #ote that the s-mbol table ;and access routines< is usuall- used b- all compilation phases5 2or example: the lexical anal-3er detects the existence o2 an identi2ier in the source program5 and creates the corresponding record in the s-mbol table @o+ever5 most o2 the identi2ier attributes +ill be onl- detected b- later phases The =S inter2ace is used b- the compiler to handle 2iles5 error handling at the =S level etc A more sophisticated compiler +ould probabl- contain additional components5 such as a code optimi3er

Advanced Issues in Elementary Topics


Although the compiler structure described in the previous section is 4uite simple5 there are some advanced issues to be investigated ;textbook chapter brought in parenthesis<: 1 S-mbol table management ;(< E22icienc- / the s-mbol table can be triviall- implemented as a hash table +ith identi2ier names used as a hashing index Is that the best implementationA Can +e use other data structure to achieve better per2ormanceA =verloading / ho+ do +e handle overloading ;the problem is demonstrated in the 2ollo+ing section< ) T-pe In2erence / all compilers use some degree o2 t-pe in2erence The problem o2 t-pe in2erence can be simpl- described as / determine the t-pe o2 an expression based on the t-pes o2 operands participating in the expression Although man- cases seem to straight2or+ard5 in ob?ect oriented languages5 t-pe in2erence can get ver- complicated ( Intermediate 8anguage Selection ;*< / selecting an intermediate representation can a22ect the 4ualit- o2 target code generated5 and 4ualit- o2 optimi3ations per2ormed * 1un/Time Support;$< / the data structures used b- the compiler to support the program at runtime5 representation o2 ob?ects during run/time etc $ .roducing Code >enerators ;&<

Symbol table management


procedure "AC@ is procedure put ;x: boolean< is begin nullE endE procedure put ;x: 2loat< is begin nullE endE procedure put ;x: integer< is begin nullE endE package x is t-pe boolean is ;2alse5 true<E function f return boolean; // ;D1< end xE package bod- x is 2unction 2 return boolean is begin nullE endE end xE function f return float is begin null; end; // ;D)< use xE begin put (f); // ;A1< A: declare f: integer; // ;D(< begin put (f); // ;A)< ": declare function f return integer is begin null; end; // ;D*< begin put (f); // ;A(< end "E end AE end "AC@E Figure 2 - sample program - is symbol table management trivial? S-mbol table management should take overloading into account 9igure ) sho+s a sample program in +hich a number o2 identi2iers BfB are de2ined5 In some program points f is a 2unction5 in other5 it is a simple integer variable @o+ever5 in each program point5 the compiler should resolve f to a single entr- in the s-mbol table according to the language semantics 9or example5 lets examine the call site denoted BA1B Chich f is invoked at BA1BA The 2unction declared at BD1B or that de2ined in BD)BA #ote that the put procedure is overloaded to handle t-pes boolean5 float and integer f6D17 is declared +ith a boolean return t-pe So is it f6D17 that +ill be invoked +ith a put(boolean) procedure A Taking a care2ul look at f6D17 reveals that the boolean t-pe returned b- f6D17 is a speci2ied t-pe de2ined inside that package x5 rather than the BglobalB boolean de2ined in Ada Since f6D17 does not return a BglobalB boolean t-pe5 there is no implementation o2 put that can handle it There2ore5 the site BA1B +ill invoke put(float) +ith f6D)7 Chat about BA)B A the site BD(B added -et another s-mbol f to the s-mbol table5 and no+ as an integer variable According to AdaDs semantics5 a locall- de2ined s-mbol BmasksB other identi2iers +ith identical names In short5 the site BA)B +ill invoke put(integer) on the integer variable f de2ined at BD(B 9inall-5 lets take a look at BA(B #o+ +e have 2our di22erent de2initions o2 f in our program Chich f +ill be used at BA(B A Again5 language de2inition comes to our aid5 and localit- o2 de2inition helps us to resolve f to f6D*7 and put to put(integer) As can be seen 2rom our simple example5 s-mbol table management could be 4uite trick- This sub?ect is covered in details in chapter ( o2 the textbook

Type Inference
T-pe in2erence / the problem o2 determining the t-pe o2 a language construct 2rom the +a- it is used This term is o2ten applied to the problem o2 in2erring the t-pe o2 an expression 2rom its arguments or the t-pe o2 a 2unction 2rom its bodt-pe link GcellE procedure mlist ;lptr :linkE procedure p<E begin +hile lptr HI nil do begin p;lptr<E lptr :J lptrG next end endE Figure - type inference example - procedure mlist !it" procedure parameter p (dragon book) 9igure ( is an example o2 t-pe/in2erence in .ascal5 the example +as taken 2rom 6)7 The procedure mlist takes as parameter another procedure p "- looking at mlistDs header5 +e do not kno+ the number or t-pe o2 parameters taken b- procedure p This kind o2 incomplete speci2ication o2 the t-pe o2 procedure p is allo+ed b- C and .ascal re2erence manual Since mlist invokes p5 +e can in2er the t-pe o2 p 2rom the use o2 p inside mlist p is used in the expression p;lptr<5 there2ore5 the t-pe o2 p must be link F void5 i e 5 the 2unction p maps values 2rom t-pe link to t-pe void ;p is a procedure and there2ore its return t-pe is void<

Intermediate Language Selection


8o+ Ks @igh level control 2lo+ structures 9lat Ks @ierarchical ;tree< !achine Ks @igh level o2 instructions ;S-mbolic< 1egisters Ks Stack #ormal 2orms ;SSA< Intermediate 2orms: Control 9lo+ >raph5 Call >raph5 .rogram Dependence >raph Issues: Engineering5 e22icienc-5 portabilit-5 optimi3ation level

IRs in the Boo


2or v v1 b- v) to v( do a6i7 :J) end2or v v1 t) v) t( v( 81: i2 v It( goto 8) t* addr a t$ *Li t& t*Mt$ Lt& ) v v M t) goto 81 8): !edium Intermediate 1epresentation ;!I1< s) s1 s* s( s& s$ 81: i2 s) Is& goto 8) sN addr a s% *LsO s1' sNMs% 6s1'7 ) s) s) M s* goto 81 8): 8o+ Intermediate 1epresentation ;8I1<

@igh Intermediate 1epresentation ;@I1<

Figure # - different levels of intermediate representation 9igure * sho+s an example o2 three di22erent levels o2 intermediate representation as described in the textbook The high/level intermediate representation ;@I1< uses a high level structure +ith loops and arra-s subscripting The medium level5 uses a high level structure +ith a lo+ level control 2lo+ #ote that the loop control structure is no longer available at the !I1 level5 and the arra- access is no+ expressed in terms o2 pointer o22sets The lo+ level uses a lo+er representation5 b- taking variables and assigning them to s-mbolic registers

Single Static Assignment !orm "SSA#


Single static assignment 2orm5 as its name ma- hint5 is a 2orm o2 representation in +hich ever- variable ;or s-mbolic register< is assigned a value onl- once This representation is eas- to create b- the compiler5 and makes is eas- to determine 2or each variable the site in +hich it +as assigned a value The ke- idea in this 2orm is to use the non/deterministic selection 2unction +here more than one value is available 2or assignment

s) s1 s* s( s& s$ 81: i2 s) Is& goto 8) sN addr a s% *LsO s1' sNMs% 6s1'7 ) s) s) M s* goto 81 8):

s)1 s1 s* s( s* s( s)) ;s)1 5 s)(< s))IJs& # sN addr a s% *LsO s1' sNMs% 6s1'7 ) s)( s)) M s* ;b< Y

;a<

Figure $ - %ingle static assignment form The above 2igure sho+s an example o2 SSA representation o2 a 8I1 program The SSA 2orm given in ;b< represents the 8I1 given in ;a< #ote the use o2 the non/deterministic selection 2unction to choose a value 2rom s)1 5 the value assigned at the loop header5 and s)(5 the value assigned in the loop bod-

Run$Time Support
Data representation and instructions 1egister usage Stack 2rames ;activation records< .arameter passing disciplines S-mbolic and pol-morphic language support

The Importance of Optimi%ations


,sing a non/optimi3ing one/pass compiler as described in 9igure 1 +ill usuall- result a non/e22icient code +hen compared to more sophisticated compilers 9igure & sho+s the code generated 2or the simple C program on an expression b- expression basis As can be easil- seen5 the non/optimi3ed S.A1C code uses N instructions5 $ o2 +hich are memor- access instructions The non/optimi3ed S.A1C code does not take advantage o2 pre/calculated values ;sub/ expression 2or c< or e22icient register allocation The optimi3ed code is much more e22icient and uses onl- ) instructions In terms o2 run/time improvement5 the non/optimi3ed code takes 1' c-cles and the optimi3ed code onl- ) c-clesP

int a5 b5 c5 dE c J a M bE d J c M 1E

ld+ ld+ add st+ ld+ add st+

a5 r1 b5 r) r15 r)5 r( r(5 c c5 r( r(5 15 r* r*5 d

add r15 r)5 r( add r(5 15 r*

C code

S.A1C code 1' C-cles

=ptimi3ed S.A1C code ) C-cles

Figure & - optimization benefits In some compilers ;or programming languages<5 it is common 2or the programmer to aid the compiler bgiving optimi3ation BhintsB Examples 2or such hints are the !it" clause used in .ascal5 or the 2act that loop index variables are not guaranteed to exist a2ter the loop ;this allo+s the compiler to allocate the index in a register that its value is not stored as the loop ends< =ptimi3ations based on programmer hints are no longer popular All modern programming st-les encourage the use o2 simple and readable code rather than an optimi3ed non/readable code +ith compiler hints or constructs chosen to aid the compiler 9urthermore5 as complexit- o2 programs is constantl- increasing5 it is not likel- 2or programmers to keep optimi3ing their programs manuall8etting the compiler +ork harder ;rather than the programmer< and appl- optimi3ations on a BreadableB program ma- open the +a- 2or simpler machines and more e22icient use o2 the target machine ;1ISC5 K8IC< !odularit- o2 modern compilers gives another motivation 2or using compiler optimi3ations !odern compilers o2ten generate non/optimi3ed code to allo+ reuse o2 compiler components I2 a certain component applies an optimi3ation5 it ma- omit data needed b- 2ollo+ing components 9urthermore5 since the compiler needs to be modular5 it consists o2 distinct modules5 each handling speci2ic tasks5 and possiblcausing code generation to be local ;similar to code generation sho+n in 9igure &<

Application &ependent Optimi%ations


Di22erent applications:programs ma- re4uire di22erent optimi3ations =2ten5 one +ants to develop an application dependent optimi3ation that is targeted to+ards a speci2ic class o2 applications 9or example5 2unctional programs can be optimi3ed b- replacement o2 ;expensive< recursion b- loops ;tail/ calls elimination< Another common optimi3ation 2or 2unctional programs is replacement o2 heap b- stack ;heap allocation and access is al+a-s more expensive than stack access< =b?ect/oriented programs per2ormance can be improved b- dead member elimination ;2or example see 6(7< and replacement o2 virtual b- static 2unction ;2or example see 6A7< In addition to the above5 speci2ic optimi3ations can be applied on speci2ic t-pes o2 applications such as numeric code or database access code

'i(ed vs) Lo* Level Optimi%ers


The location o2 the optimi3ation phase is another interesting issue Should +e place the optimi3er be2ore code generation5 a2ter code generation5 bothA There are t+o common approaches 2or placing optimi3ation phase;s<: 1 =ptimi3er can be placed a2ter code generation ;lo!-level) 8o+ level optimi3ation is used b- @./ .A/1ISC:I"!/.o+er .C 8o+ level optimi3ation is considered as -ielding more e22icient code5 and considered as conceptuall- simpler ;in 1ISC< Since optimi3ation is done at the lo+est level ;a2ter code generation<5 speci2ic programming language details do not exist and the optimi3ation can be used 2or multiple programming languages ) =ptimi3er can be divided to t+o phases5 one preceding code generation5 and the other 2ollo+ing the code generation phase ;see 9igure N< Such optimi3er per2orms both high/level ;usuall- architecture independent< and lo+/level ;architecture dependent< optimi3ation5 and thus called mixed optimizer !ixed optimi3ers are used b- Sun/S.A1C5 Dec Alpha5 S>I/!I.S5 IntelQs (%& !ixed optimi3ers are considered easier to port since at least part o2 the optimi3er is architecture independent Since part o2 the optimi3ation is per2ormed at high/level5 it can take advantage o2 high/level in2ormation to achieve more e22icient compilation !ixed optimi3ation supports CISC The approaches described above are sho+n in 9igure N In both approaches5 the optimi3er anal-ses the intermediate code and tries to take advantage o2 the speci2ic case introduced 9or example consider loop invariant / an expression inside a loop +ith a result that is independent o2 loop iteration Such an expression could be extracted 2rom the loop and evaluated onlonce In a loop +ith large number o2 iterations5 evaluating an- expression5 simple as it ma- be5 ma- have a considerable e22ect on per2ormance Elimination o2 such redundant recalculation is onl- one example o2 optimi3ations that are both simple and e22icient

Character string Scanner Scanner Tokens .arser .arser AST Semantic Semantic Anal-3er Anal-3er AST 8I1 8I1 >enerator >enerator 8I1

Character string Scanner Scanner Tokens .arser .arser AST Semantic Semantic Anal-3er Anal-3er AST I1 >enerator I1 >enerator !I1

=ptimi3er =ptimi3er 8I1 9inal 9inal AssemblAssembl=b?ect

=ptimi3er =ptimi3er !I1 Code Code >enerator >enerator 8I1

.ost .ost =ptimi3er =ptimi3er =b?ect

Figure ' - location of t"e optimization p"ase

10

Translation by +reprocessing
Source to source translation o2 one programming language to another can produce BcheapB compilation solutions b- using a translator and an existing compiler o2 the target language 9or example5 some programming languages are translated into C to be later compiled b- a standard C compiler This allo+s the language developer to +rite a translator rather than a +hole compiler package 1eal +orld examples are elimination o2 includes b- C preprocessor5 translation o2 CMM to C ;c2ront<5 @askel5 translation o2 9ortran into RvectorS 9ortran program etc C is ver- com2ortable as an Bintermediate languageB due to its support o2 some indirect source level debugging ;such as the Tline directive< Indirect source level debugging allo+s sho+ing the original source program5 rather than source o2 the C program ;produced b- translating the original source program< "- using compiler directives such as Tline5 +e maintain a relationship bet+een the translation ;C program< and the original source program 2iles This relationship can be later used to identi2- +hich source program statements are related to statements o2 the target ;C< program

&ata$Cache Optimi%ations ",-#


.rocessors are getting 2aster all the time !emor- is lagging behind Di22erences bet+een main memorand cache access time are dramatic "- causing data ;or instructions< to be read 2rom cache rather than 2rom main memor-5 one can signi2icantl- improve per2ormance Data/cache optimi3ations are usuall- most e22ective +hen applied to high/level intermediate code ;or source code< Character 9igure % sho+s the phases o2 an string optimi3ing compiler +here data/ cache optimi3ation is applied to Scanner high/level code The I"! .o+er.C Scanner compiler uses a de/compiler to translate a lo+/level intermediate Tokens representation to a higher/level intermediate representation5 then using the higher/level I1 2or data/ .arser .arser cache optimi3ation This allo+s I"! to place data/cache optimi3ation a2ter lo+/level I1 AST generator5 +ith the rest o2 compiler optimi3ations applied at that phase The I"! .o+er.C compiler is Semantic Semantic sketched in 9igure O Anal-3er Anal-3er @I1 Data/cache Data/cache =ptimi3er =ptimi3er !I1

U Figure ( - data cac"e optimization applied on "ig"-level )*

11

Character string Scanner Scanner Tokens .arser .arser AST Semantic Anal-3er Semantic Anal-3er AST 8I1 >enerator 8I1 >enerator 8I1 J VI8 =ptimi3er =ptimi3er 8I1 9inal Assembl9inal Assembl=b?ect 8o+ to high 8o+ to high @I1 J YI8 Data cache optimi3er Data cache optimi3er @I1 J YI8 @igh to lo+ @igh to lo+

Figure + - ),- .o!er./ compiler

12

+lacement of optimi%ations
It is clear that some optimi3ations are dependent o2 each other =ne optimi3ation can create optimi3ation opportunities 2or another5 but it might as +ell destro- optimi3ation opportunities Ce +ant to 2ind an ordering o2 the applied optimi3ations in +hich combined optimi3ations -ield better/ optimi3ed code5 and do not clash +ith each other

Scalar replacement o2 arra- re2erences Data/cache optimi3ations .rocedure integration Tail/call elimination Scalar replacement o2 aggregates Sparse constant propagation Interprocedural constant propagation .rocedure speciali3ation and cloning Sparse conditional constant propagation >lobal value numbering 8ocal and global cop- propagation Sparse conditional constant propagation Dead code elimination Common Subexpression Elimination 8oop invariant code motion ;or partial redundanc- elimination< Inline expansion 8ea2/routine optimi3ations Instruction Scheduling 1 1egister allocation Instruction Scheduling ) Intraprocedural I/cache optimi3ations Instruction pre2etching Data pre2ertching "ranch predication Interprocedural register allocation Interprocedural I/cache optimi3ation

@igh 8evel

"

8o+ 8evel

Figure 10 - placement of optimizations example 9igure 1' gives an example o2 optimi3ation placement in a compiler =ptimi3ations are applied starting +ith the high/level optimi3ations and going do+n to the lo+er level ones #ote that some optimi3ations are applied more than once ;such as sparse conditional constant propagation< =ptimi3ations in 9igure 1' are labeled according to the t-pe o2 code 2or +hich the- appl-: 13

A / high/level optimi3ations that are applied on high/level I1 or source code ,suall- applied during earl- compilation stages " / medium/level or lo+/level optimi3ations D / usuall- applied on lo+/level I1 E / per2ormed at link time Applied to ob?ect code 9igure 11 and 9igure 1) demonstrate the signi2icance o2 combining optimi3ations5 and signi2icance o2 optimi3ation order 9igure 11 ;a< sho+s a simple program a2ter constant propagation #ote that evaluation path ;marked +ith thick edges< is determined due to constant propagation / the constant a1 is propagated to the condition a11 @o+ever5 constant propagation ;usuall-< does not +ork on aggregate t-pes5 and the value ,2x120 is not propagated to the 2ollo+ing conditional ;,2x1120< .art ;b< o2 the 2igure sho+s the simple program a2ter scalar replacement ;a2ter constant propagation +as alread- applied< Scalar replacement replaces the term ,2x b- c Since c is no+ a scalar value5 it is sub?ect to the second pass o2 constant propagation5 and the condition c1120 is evaluated to true using the value c120 assigned in previous block c"x a1 aJ1 Y "x1' "-1' # read " x read " Y c1' "-1' a1 aJ1 # read " x read " -

"xJ1' Y # Y

cJ1' #

;a< A2ter constant propagation

;b< A2ter scalar replacement

Figure 11 - %calar replacement after constant propagation

14

d"x "x1' c) read " "xJ1' Y "x"x Mc # dd Mc Y d1' c) read " dJ1' #

;a< "e2ore optimi3ation

;b< A2ter scalar replacement and constant propagation

Figure 12 - %calar replacement before constant propagation 9igure 1) ;a< sho+s a simple program5 be2ore an- optimi3ation is applied .art ;b< o2 the 2igure sho+s application o2 scalar replacement and constant propagation ;in that order< 9irst5 the term ,2x is replaced ba scalar d Constant propagation is then applied and propagates the value 1 ' o2 d ;assigned b- d120< to the condition d11205 +hich then evaluates to true

Theoretically Open .uestions


.icking the RrightS order Combining optimi3ations .roving correctness

Tentative Schedule
(':( Introduction;1< 1(:* #o class ;read Chapter ) and solve @ome+ork T1< )':* Yom @a3ikaron )*:* Oam5 Intermediate 1epresentations;*< )N:* Control 9lo+ Anal-sis ;N< *:$ Data 9lo+ Anal-sis ;%< 11:$ Dependence Anal-sis and Dependence >raphs;O< 1%:$ Introduction to =ptimi3ation;11< )$:$ Earl- =ptimi3ations ;1)< 1:& 1edundanc- Elimination ;1(< %:& 8oop =ptimi3ations ;1*< N:& Interprocedural Anal-sis and =ptimi3ations ;1O< 1*:& =ptimi3ations 2or memor- hierarch- ;)'< )1:& Case Studies o2 Compilers and 9uture Trends ;)1<

15

/ncovered Chapters
S-mbol tables;(< 1un/time support;$< .roducing code generators automaticall- ;&< Alias anal-sis;1'< .rocedure optimi3ations ;1$< 1egister allocation ;1&< Code scheduling ;1N< Control/9lo+ and 8o+/8evel =ptimi3ations ;1%<

/ncovered Topics
Code pro2iling Source level debugging .aralleli3ation and vectori3ation Wust in time compilation

Course Re0uirements
.repare course notes 1'X Theoretical assignments ('X 9inal exam &'X

References1
1 ) ( S S !uchnick Advanced Compiler Design and Implementation A K Aho Compilers / .rinciples5 Techni4ues and Tools . 9 S+een- and 9 Tip A Stud- o2 Dead Data !embers in CMM Applications In .roceedings o2 the 1OO% Con2erence on .rogramming 8anguage Design and Implementation5 !ontreal5 Canada5 Wune 1OO% * > Aigner and , @ol3le Eliminating Kirtual 9unctions Call in CMM .rograms Technical 1eport T1CS O$/))5 Department o2 computer science5 ,niversit- o2 Cali2ornia5 Santa "arbara5 December 1OO$

1e2erences 2or 2irst lecture notes 16

Вам также может понравиться