Advanced Compiler Design and Implementation: Introduction To Advanced Topics
Advanced Compiler Design and Implementation: Introduction To Advanced Topics
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 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
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<
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
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
int a5 b5 c5 dE c J a M bE d J c M 1E
C code
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 &<
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
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
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+
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' #
14
d"x "x1' c) read " "xJ1' Y "x"x Mc # dd Mc Y d1' c) read " dJ1' #
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
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$