17338067 Code Optimization Code Generation

download 17338067 Code Optimization Code Generation

of 10

Transcript of 17338067 Code Optimization Code Generation

  • 8/13/2019 17338067 Code Optimization Code Generation

    1/10

    UNIT 8Code Optimization and Generation

    Code Generation Issues

    Instruction Selection Can be a simple translation of three address instruction to targetcodex = y + z becomes lw $t! ylw $t"! zadd $t#! $t! $t"sw $t#! x Concepts of code generation! register allocation andoptimization are introduced lgorithm for generating code for arithmetic expressions usinga minimal number of registers

    Target Code Addresses %our areas of memory& Code! Static! 'eap and Stac( Can use one static base location for Code and Static )ariable area procedures ha)e a location *offset of the code in this area global )ariables allocated in the static area also gi)en offsets here ,ther program )ariables! local )ariables and formalparameters! are gi)en offset locations with regard to a stac(acti)ation record pointer

    Basic Blocks and Flow Graphs -epresentation of intermediate code as a graph nodes of the graph are basic bloc(s! where the flow of control canonly enter at the first instruction and lea)e through the last edges indicate which bloc(s can follow other bloc(s! representingthe .umps in the code /seful for discussing code generation 0efining basic bloc(s separate se1uence of 2C *three address code into basic bloc(s byidentifying the first instruction as a leader& )ery first instruction is a leader any instruction that is the target of a .ump is a leader any instruction following a .ump is a leader

    !ample o" Basic #lockspsuedo code to initialize a " by "array to be the identity matrix!for i from " to " dofor . from " to " do

  • 8/13/2019 17338067 Code Optimization Code Generation

    2/10

    a3i! .4 = 5for i from " to " doa3i! .4 = "52hree address code! assuminga is the starting address of the

    array in row6ma.or form andthat each element ta(es 7 byteseach&"5 i = " leader#5 . = " leader85 t" = " 9 i leader:5 t# = t" + .;5 t8 = 7 9 t#

  • 8/13/2019 17338067 Code Optimization Code Generation

    3/10

    Ne!t Use and %i&eness

    /se of a )ariable&

    if instruction i assigns a )alue to x! instruction . has x as an operand!and control can flow along a path from i to . with no inter)eningassignments to x! then . uses i @i)e )ariables& for each instruction x = y + z! determine for x! y and z whichinstruction next uses that )ariable 2hese properties can be determined by ma(ing a bac(wardpass o)er a basic bloc( and recording the information in thesymbol table

    'imple code generation

    Aenerates code considering .ust one basic bloc(! butintroduces the ideas of register allocation assume that we (eep information about registers register descriptor (eeps trac( of which )ariable names ha)e acurrent )alue in that register address descriptor for each program )ariable (eeps trac( of where

  • 8/13/2019 17338067 Code Optimization Code Generation

    4/10

    the current )alue of the )ariable can be found Aet-egister function gets an appropriate register for anyoperand of the 2C %or the instruction x = y + z call Aet-egister for each of the operands

    if y is not already in its register& lw -y! y if z is not already in its register& lw -z! z gi)e the instruction add -x! -y! -z

    'imple generation updates descriptors

    t end of basic bloc(! ignore 2C temporaries *not li)e for each program )ariable x! if current )alue of x is not in memory!issue a sw instruction /pdating descriptors& %or the instruction lw -y! y! change register descr5 for -y to onlyhold y add -y to address descriptor of y as a location for the instruction sw x! -x! change the address descr of x to includememory address of x for each operation add -x! -y! -z register descr -x holds only x address descr of x has only -x *not any memory location remo)e -x from address descr of any other )ariable

    Get$egister "unction

    Bic( a register -y for any )ariable y that is an operand if y is already in a register! pic( it *no load instruction needed if y is not in a register! but one is free! pic( that register *and load it if y is not in a register but there is not register free! considercandidateregisters -

    any register with a )ariable )! whose descriptor says its current)alue is in memory already any register with the )ariable x! the result of the instruction! and xis not also one of the operands *x will be rewritten anyway any register with a )ariable ) that is not used later otherwise! generate a store instruction sw -! ) to spillD ) repeat these steps for other )ariables in the register! and pic( aregister with the fewest number of stores

  • 8/13/2019 17338067 Code Optimization Code Generation

    5/10

    Bic( a register -x for the )ariable x that is the result in addition to abo)e& any register holding only x if y is not used later! use -y to hold the result -x *e5g5 x = y

    (eephole Optimization

    nother strategy is to generate naE)e code and then impro)ethe 1uality of the target code by simple optimizations sliding window of instructions *peephole to examine -edundant loads and stores Fliminating unreachable code example& eliminate .umps o)er .umps %low of control optimizations analyze .ump se1uences lgebraic simplification and reduction in strength

    $egister Allocation and Assignment

    Alobal register allocation tries to (eep fre1uently used)ariables in registers across basic bloc( boundaries assign registers to most acti)e )alues of inner loops /sage counts approximate formula for the benefit to beobtained from allocating a register x within loop @ is&S*bloc(s G in @ use*x! G + # 9 li)e*x!G also assumes that loops iterations are a large number and that the

    difference in number of times a bloc( is executed is negligible choose register allocations that maximize benefits o)er the )ariableset

    after allocating inner loops! go on to similar counts for outerloops5

    Optimization Concepts

    ,ptimization see(s to impro)e the time or space used by the generated code without changing the beha)ior of the program

    Compiler area where most research is currently being conducted Hany compiler optimization problems are B approximations and heuristics used instead soupD approach (eep adding functionalities Hany modern compilers ha)e extensi)e optimization which may be 1uite time and space6consuming themsel)es options to turn off optimization for better compiling

  • 8/13/2019 17338067 Code Optimization Code Generation

    6/10

    Glo#al Optimization Framework

    Fxtend the ideas of the program as a 0A of basic bloc(s to doglobal data6flow analysis pre)iously discussed optimizations within basic bloc(s as part

    of code generation loo( at examples in Stanford optimization handout constant folding! constand and copy propagation! %unction preser)ing transformations can impro)e the program

    without changing what it computes

    Fliminating common subexpressions Copy propogation 0ead6code elimination @oop optimizations Code motion for loop6in)ariant code Induction )ariables and reduction in strength Brimarily at procedure le)el! cross6procedure data6flow is more

    difficult

    )ata "or data*"low anal+sis

    2he compiler needs to collect information about the program as a whole! primarily about )ariable definitions 0ata6flow e1uations are set up5 typical e1uation may ha)e the form& out3S4 = gen3S4 + *in3S4 (ill3S4 the information at the end of statement S is either generated within S or enters at the beginning and is not (illed during SD for different data6flow problems may sol)e in the direction of control flow to find out3S4 in terms of in3S4! or the problem may re1uire to bac(wards to find in3S4 in terms of out3S4

    )e"initions "or Flow Graphs point is between two statement or basic bloc(s path connects points node d is a dominator node o)er a node n if e)ery path in the

    flow graph to n also goes through d

  • 8/13/2019 17338067 Code Optimization Code Generation

    7/10

    loop is a set of nodes with a single entry node! called theloop header! that dominates all other nodes in the loop

    the loop must also ha)e a path to iterate the loop! i5e5 go bac(to the header

    $eaching )e"initions

    definition of a )ariable x is a statement that assigns! or may assign!a )alue to x = expr and -ead*x are unambiguous assignments ambiguous assignments include passing x as a reference parameterto a procedure or assigning to a pointer that could refer to x definition d reaches a point p if there is a path from the definition dsuch that d is not (illedD along the path definition d is (illed if its )ariable x has another definition

    ,uations "or reaching de"initions

    Gased on the structure of statements! we can define the e1uationsfor reaching definitions Fxample& S is an assignment statement *def d& d& a = b + cgen3S4 = JdK! (ill3S4 = 0ef*a JdK!out3S4 = gen3S4 + *in3S4 (ill3S4 Fxample& S is # statement se1uence& S"L S#gen3S4 = gen3S#4 + *gen3S"4 (ill3S#4(ill3S4 = (ill3S#4 + *(ill3S"4 gen3S#4in3S"4 = in3S4! in3S#4 = out3S"4! out3S4 = out3S#4

    Fxample& S has the body of a loop& while M J S" Kgen3S4 = gen3S"4(ill3S4 = (ill3S"4in3S"4 = in3S4 + gen3S"4out3S4 = out3S"4

    Use*)e"inition Chains

    -eaching definition information is often stored as use definitionschains! also (nown as ud6chains5 for each use of a )ariable! there is a list of all the definitions of the)ariable that reach that use

    lgorithms to sol)e the data6flow analysis may be difficult if there arebrea( and continue statements allowed

    approaches include iterati)e algorithms& start with sets at emtpy and iterate *eitherforward or bac(ward adding definitions until con)ergence inter)al analysis Similar techni1ues are used to define a)ailable expressionsD

  • 8/13/2019 17338067 Code Optimization Code Generation

    8/10

    an expression x + yD is a)ailable at point p if e)ery path fromthe initial node to p e)aluates x + y! and after the last suche)aluation! there are no subse1uent assignments to x or y

    %i&e -aria#le anal+sis

    %or a )ariable x and a point p in the program! we want to (now if xcan be used along some path starting from p5 If so! x is li)eD at pL otherwise x is deadD at p can be used inregister allocation 0efine in3G4 to be the set of )ariables li)e at the point immediatelybefore bloc( G and out3G4 to be the same at the end of the bloc(5 lsodef3G4 is the set of )ariables defined in G prior to any use of that)ariable and use3G4 is the set of )ariables whose )alues may be used inG prior to any definitionin3G4 = use3G4 + *out3G4 def3G4

    out3G4 = union o)er all successors S& in3S4

    )e"inition*Use Chains

    calculation done in the same manner as li)e6)ariable analysis isdefinition6use chaining! *du6chains5 )ariable is used in a statement if its )alue is re1uired5 b and c *but not a are used in both a = b+ c and a3b4 = c5 2he du6chain computes for a point p! the set of uses s of a

    )ariable x! such that there is a point from p to s that does notredefine x

    limination o" Glo#al Common 'u#e!pressions Ai)en a flow graph with a)ailable expression information!computes a re)ised flow graph eliminating common subexpressions %or e)ery statement of the form x = y + z such that y + z isa)ailable at the beginning of the bloc(! search to find thee)aluations of y+z in pre)ious bloc( *say w = y + z and replacewith

    u = y + zw = u2hen replace all x = y + z in this bloc( with x = u Conser)ati)e algorithm doesnNt find all possible commonsubexpressions It doesnNt always impro)e code! but later optimizations may

    Cop+ (ropogation

  • 8/13/2019 17338067 Code Optimization Code Generation

    9/10

    Oarious optimizations produce copy statements of the form s& x =y 0etermine all the places where this definition of x is used andsubstitute y for x in all those places

    e)ery use of x must meet conditions statement s must be the only definition of x reaching u i5e5 theud6chain for use u consists only of s on e)ery path from s to u! there are no assignments to y thisre1uires setting up an additional data6flow problem computing theuse of copy statements

    )etection o" %oop*In&ariant Computations

    /se ud6chains to detect computations whose )alue does notchange as long as control stays within the loop Consider an assignment x = y + z in a loop where all possibledefinitions of y and z are outside the loop! then y + z is loop6in)ariant Suppose there is another statement ) = x + w! where w is onlydefined outside the loop! the this statement is also loopin)ariant the du6chain for x = y + z will tell us there the )alue of x can beused! so we need only chec( those uses of x for operands w that arealso defined outside the loop5

    Code .otion

    ,nce we ha)e found in)ariant statements! then we want toperform code motion to mo)e such statements outside the loop! ifpossible Gefore the header bloc( of the loop! we introduce a preheaderDbloc( with loop6in)ariant assignments Conditions for mo)ing statement s& x = y + z bloc( containing s dominates all exit nodes of the loop there are no other statements in the loop that assign to x no use of x in the loop is reached by any definition of x other thans 2his is conser)ati)e! there are other code motion strategies

    limination o" induction &aria#les

  • 8/13/2019 17338067 Code Optimization Code Generation

    10/10

    )ariable x is called an induction )ariable of a loop @ if e)ery timethat the )ariable changes )alues! it is incremented or decrementedby the same constant each time around the loop e5g5 in a for loop! the index )ariable I Pe first loo( for basic induction )ariables! those whose only

    assignments are *+ or 6 a constant re1uires reaching definition information and loop6in)ariantinformation Pe next loo( for additional induction )ariables . that are definedonly in @ and are a linear function of a basic induction )ariable Hodify the computations of the additional induction )ariables touse + or constants! instead of multiplications

    )i""iculties "or Code Optimization

    0ealing with aliases& two or more expressions denote thesame memory address liases can be introduced both by pointers and by procedurecalls analysis of pointer assignments data6flow analysis of changedD )ariables across procedure calls