You are on page 1of 38
Wat -& Code optimiation - Cnsiduokion -for code. optimizabion’- code oplimixation & vequived le produce an efficient target cede. here ave two important constdered while applying the te optimiaakion and those are (isthe Semailic equivalence mut not be ed (to The improvement ever the program e pen be achteved without changing te gentinn “Of | iesues that need te be} choique or code on the Source Penge must the Pprogaam Advantage code optimization + Ls ‘the intermediate code optimized te produce efficient object code i. She cede optimization allows faster execitian ef Sourge program b making mivvoy modigicalions elosséftcation of optimisation + Cade opliimization (Machine §n de pendent Machire Dependets Machine endependent =~ Machine independent optimizations ove lecal and. loop optimixabions % alobal optémizalons Machine dependent optimization + Machine dependent optimizations ae feep- hole optimization - Shese. ale using inskuctons Scope Of operon’ {Re optémizalion. technique Zan The Source based scope of the cperalzon * be applied on. ol, Local scope ® can ow Lihen @ local optémizalion has bo be perpor d a “ope ts Linited to certah Specifte Hock - | atements 02 Global spade? then a global optimization has to be perfouned its scope 18 troughouk the program ot within. some procedures or & performed using dala {loo analysts : zertdin Lode outiid a. codemotion * means mowing tre loop be Shength reduction = means replace meclteplication | by addition % diviston 4 subkiacdtion - Local optimization :- {- Compile: tine evaluation ?~ ME means shige of computations fiom. auntime te comptlakien time tp-folding an ths technique at compile time instead the computalion 4 conslal is done of guntine at Leng = aay xd of 2% & done ak diere the computation time - Compile time tnutead of execution ai) Constant Propagation’: In this technique tie value oF vartoble is replaced and competalior ob ig done ab the compilation time - eer pra ds ye5 5 Area = prr™ #5 Aleve at the comptlatier- heine the value FFE Belt and 6 then competion. y cyeplaced @ done during compilation . Bol ¥ 5 45 & Common Sub expression eltmination + “the common. sub expression 8 an expression appeartt repeatedly th the prgqnam which & com pute previously « Then ef Me operands hE this expyesstons fe used , instead of, necomputiry it each and every tune but by =4et eae ty ao[h) oe tg=a[t] te= 4¥ laminate -4ay aistaeyt aeceee, y= et sane dyer ee ee 4, - bltyt & Aer 3. Variable pee Variable propa akon means use Of One jwar Use of one variable instead Of another - exe x= PES = area = Pre? * avea= L*® ay Here the variable ‘%' ts eliminated. Here "Be fecear condition B hat & vawlable musk be assigned te another vartable % 4: Code movetnent im the are, two basie oals o code movement— lok Iwo reduce the size Of Ine tode. ve Space comyle aicty - lg. Jo reduce the freq % execudion of tode '¢: time complext iy Ext foo Gico} T2=l05 C4 +) YaVES5 K=O D-+508 t this cae be eptimesed as temp=y xz for (7-03 = 10s 144) ie k= temp+ sD5 The cede 47 (2 will be pe only one + And séenply the vest of the ompak alien te fused tlherever necessary «. B) Loop invavient computakion (ov cede Fs the loop invovient optimization (a be obtained by monty some amount ab cade alae’ fi a 7 | bw Zz the Loop and Plating aE funk before enter “ge e ne We Loop This metha} also called as code motion ér> while (Ke=Max -/) Sum= Sum+alty5 he above wal can be optimized removing the computation of maz-I cutside the loop 7 She offimiged tede can be nemax-! vohile (Tk=n> { Samm =Sarot als s-tkength reduchon + The shength of tertath operators is hegher gaits hanes than ‘4'-9n the higher Strength lower strength oper clos thon others fox insane shength veducton technique ys can be replaced rf opera lo t+) bxie for (720 91-80 34 AnH aounk a1 KES Sleve ue get Ime count values as ly 2) and_ |so on pls less than wo- Whé cede can be veplaced. by mie sttenalt- seduction as temp=+ 7 fos (i203 1¢=90 34+) { ‘caunke temp 4 temp= temp 445 5 G-Dead code €limination z- A variable @ods 1% said te be lve ho Program Gp, the value centaihed tnfo tt ie wsed_ subsequently « Lyon the otherhand the variable fs aid tp be dead ak a pone th a program a the value contained thle it & never been used athe code conkaining such. a variable supposed. to ede and an optimization. can be be @ dead |ferfowed by elininaling auch @ dead code | fet ey 5 oe K+4105 The optinica lion can be performed by clifatnateig Ine statement ty he assignment Matement called dead cede Mak tl; €xi2 424 #74 ty alti) Ly-qal ty= ei be yal ta= alt] ty = bbe] ahd. ty=blt] tea te * ty code th =k by te = prod + bs tee pred tte 1 Loep optimization - “The cede optiimtcalron can. be si ifveosly done tr loops oI (he program ° specially an ner loo ba place where program spends large amount tre » Here us No. & instcuclions are less inner loop then Aunnthg ‘one op the ‘on le ill gek improved wi Loop optimtxabion techniques ore 1% code motion | UN) crduclten vortable & steenglh reducttoa Ui) Loop invartent melhed tw) Loop unrolliAg v) Loop fusstn ° Civ) Loop oe 4p this meliods the nor Of Ik an be reduced by woxttery be Baa and tes % code two tines © ink Gs > éyt ‘tot Lels while (t=10D while ({4= 106.649 { { Sano a [TI = gam= sunt alt] tet Cres a(fl 5 Loop aston 4. Loop 4urctéon geveral lo ORS are meee inte one loop Ext foy t=1 ton do for fel tom do _, -for f=1 te nam do alin J= o$ Pivected aeyelte eae Applications o, DAGY the commen seb- ave used block, alt =lo% Ole Determining expae gston 5 tpside 'he op. Debermintr vohich names paved outetde {ne which chatemenls Ff the Hock lue outside le block com block \ininaling of Stonplefgig the Lisk of Quad 5 aples by € [the common, — Sutb- expression: and not f rene the askignment: of Me -form a useless and_ until ib % a must kur Consiuct DAG for the follow Hock ae de=b es =dk co b= & feabte jee 4rd (Step te hep at ee tep : a Gap step 2 @are | PP wed b c spur steps © aie | a a aierb ly © convert the elloving exepression into pag ath € (atbd+c+d tj s=atb tg+=atb sabi kte tye = tat & te oe atytd Steph @tte step a+ ta - Pr a »b enta Step ar a b a 4 Step ue AL +3 ty Brite es fe a b o ~b Peephole opteniixalion« the statement Stakement— = ik we apply tode ration Shaker then the enerabed_ code. contain wequandan- | ta poor: . ch a code certains feanshormalko =) Jo optimize se eq en the pe tode - need te be appli [> The branspomalcon uibeémete ely . Ure Signigicank improvement over lhe will result tr | thsiuekions + the qualty Of cath code & sear ] ounning tube (@) Space requirement of Une tage program. : Definition peephole optimiation is a Simple and e{pecive technique 40° Locally tinprouhg farger code Lythis techneque & added te tmpaove tite perqor" mance of me targel code 4 examining he Shot sequence of tor ok inshuckons and weplacing these ths buction aT therted (o)-faster Sequence chavacterstiea of peephole optimisation le Redundant inskuck _ elimin alto 92 flow of contol optimiacation- | 2. Aigeboaée sinpligfcation- | Yo Use of machine idioms * le Redundant instuetion elimination - special aedundant loads and stoves can be eliminabed- & this tyre of teanspormakeonm Ear Mov Re» we tan eliminate the wiea~ Mov %) ho chable tnsteuetion since xb ww Ro» Buk + q@ label stmrE alreas fou xe) Q-flow of cookol Optimization + using peephole. opttmixation oe “jeunes on qeers tan be eliminated bast goto test ‘a z. r° done tests golo done vee | test ‘ce done Aone: f eck 2 done 5 ‘Thus wee Feauae. One fer Paes tranfosmakon A os Ub ach ae test | fa ip aab oe doa. test + gote done - cae iz f test s qe den done + aeniees phlgebraie Simmpligpcation : dorst : peephole optimization % an. Sinilary or eq to node 5 there & another path {-3-6 vwohich 1 not LSNede. “5! dominates no node Natural Leopsi- can be denoted by nod —>leop in a -fioegra uch that “d dom neJhese edges ave called back edg4 and fox aloop there car be mose th edges « : 1) Sf there & ps4 then q, ts a head and p ts ata thyough Qe an one back and head dominates tall» - ext 4 = he loops tr tris pain can bel denoted qo te (dom y) Scvilarly ey fe, 4 deme]: back edge ned the natural teop b defied. by a of all ene the Such that there exisls @ coltection Nedes thak can veach tb ~n' without qe" Hoough ‘dt and at the same tine ‘dials cam be added te thie Zolleck“on ext Apo above waph. la Gal & O natural leo p becaue we can reauy to alt the remaining nodes 4x0" 6. 2. Dnnev loop? the innev loop & a Loop Wat contawis ne other loop * Here the tnner loop fs 4-8 that means edge Hea bey Q-3-Y “<— Ext— F 7 Consider the follow flow aa and 4nd @ Dominalor tow each basic block DB Detect all the Loops th the york : eed nate dominaler ils ely 5p, = tniffal node domin atzs all tre nodes a Bar By by bo Ses B41 BeiP9 Bio Bu > ba= 4 > by = Bo» BH PE : Bs = Bribe She = Dominates ibsety | i > By = Bs ba a 3 Ba= domirakes they ack edge. ¢ Be - Bo >> B= 8" Header +B» —> Bu = dominates no node lwop Back edge + 8, Fy tteader = 8Y Loop 3 % Back edge % 8g~- 8y Header Bq- Global optimiz akiont- foo a global oplimizaliion a prograss & represented. i the, foam of pepe -flow qe FP The et 4flovo qe wa eee ae presenr tation th which 2ach- node represents bade black and edges crepretent Ihe flow of conkol from One Hock te another Contiol flows analysis ie Global optémixatcon. Data flow analys sis * Cortiol flow analyst deter-| Aes the h-formattion otpardaig arrangement h nodes presence 4 loops, nesting of, Loops anol. sn ee Te especie node odes visited before exe culfon of a Speeipie cat the analgsts optimization can be performed 4% Betild the corileol flow en fox follaving eed Li as=kta Corio! - flow ep cde) ole B ti = d-b dz =bx2 ds: =atb q Seed. ‘ CB of i Bz te =it! ttab-d be dos Koa da és (ec LE b= a4pP ppane es =ak Et Aerly dead conkol flao 4 Sole [2 dn above 4lo0 aph. various expessions ave there thal Qve ath k42 )d-b bed) da) at ,etk) dtd cid-b & the dead code, bene Lyodn Block 81, be used tndusther blots the value oF ‘cannot sin Block Pgs 4esetk has no meanin there fs not Link -from %3+ further to ang dber block and Value q f' is not Used in ‘Bs! at all because— B3 Data -flow —- of floographe ” the dala flow qroretty represent Ihe certain | ti-fermakon ne rding wsepullress of the dala akems for The purpose of optimixakion Ihese dala flow properties ove \e Available. expression. a Reading definitions 3. hive variables Ww busy expressions —>Some baste terminologies thab will be used while Ford tie dala flow properties are Crh program point containihy the dleviuations % called deginelzon- potnt wh A program yoink at which a veference te a dala Yen f& made i called reference. point Lin A program potrak- ak which some evaluating i . i a expression is = fs called evaluation. pn auene a) definition jor EB ids 2 ages Reference pot Ayzicath evaluation pour 1. Ayoilable expression point (wo) if and only ay along all palhs are eae to Ww tb The expression. aiy & Scud te be available ak thr evaluation point” i | Li) The entpresstOn- aay te gatd to be avail abu no definition of an operand of expresstin follow] ils Last evaluation along the path: - by [exer Tul The express n- are is the available exepression. $7 Bg, Bz, By becatrse this expre gion te nok beer changed b any of the block befeve appearing -fdvankages* beThe Use Of available expressor és te al common sul expression - 9. Reachi de finitionsi- “A efinitéons ~D weaches at qoint ‘pif dherel from df P along which >! is iniunab— wa qalk An expression act & ayatlable aba propam bal SS not Killed by Bi | sys => 6 8a [davy: =Vvra Ls5§n fiqci> dy & Ihe ir init 18 fqd di & p veading definition ipl Bloc k fox Block Ba. fuk not Bs because at & killed a detinitfon t block Ba - LAdvantagesr 2. Live Vartable * ‘pt 4, there &@ en fiom pte eit, alo the value ¥ & used before it & nedeyined. » p sad to be dead Lthevwise Me variable oe c (wiz & Ga | 2=X+0 [By | BS BF : ; feet * Advantages’ 5 five yortables ave wseped th | _ register allocation - aye : by : ee ty ue Aepjnibion dit the readin definitio ae ts Used tn constant and \asiable peg ~The variable, *x' € Live at Some point woh’ ch at hel) eb Spduction Vortable t A vaviale “i & called an tnduckion. Variable t} Loop uw th every time the variable ‘f) changes Values Ce, j tune ‘V either qe theremented (00) decremented - but for a Aor loop txt to lo - The thduction variables comes th following fons avsieb y Qt=be oe , as=Ctb , at-btt” = fn the flow graph Bu and K in & are wo induc tier. vaviables because thew Values get changed ab each iteration - =) We will create New fem po vayiables “$Y og te whieh induction Novtables “t!and &' We assigned s tys =ye Kk ty: =a[ty}4to b fe=mt3 kean 42 spew Vico [th mts ORT vgs =4ek %: Fa-4 ty = 0D tea[ty] to rk introduced variab on Tee we get efhect oe & Not thar wow we ar value of 4#t Note that noo we ai Data flow equakion- Data flow equatéons are. the equations repre} senting Me expressions thak Qre eappecaririg in the flow raph —? These equations are useful 40 compl line varéablec, available expressions and mare definition © i = The dala flow equation fe th the form 5 aut [5] = goo ls] v (in [sy - ett CSD) “he tn-formabion ab the end of statement & etthes enerated within the statement Gy enters ab lhe begining and i not Killed as contol -flao “trough he detement Pa Dala flow analysis -for skuctised pe zamsi- ‘hel -floo consadct i thd ~> Ihe propery af Cori fag pein at which contre] eontzol leaves Hine B @ Single oo enters and a Single en point thak when the execertiin fF % datoment i over” from bys- Consider wonnmnay S-> Gi =e 6. S> 4 &€ thn S $-> do $ whik € - qels= fs set of de-fint bins gape by eae Kall [s] > te set of defiar Bons “Yetlled by "= Sek of defiritfons tat never yeach the end of S, even 4 the each , tne beqinuid St5 Sa a7 © then Sy else Sa do | while € enls)= 4d { => 4 Bae nea out (s] = gen[sJu[in UF tilts] { ic fs] an [sJol en [5)] - kill ts) : = yill fg) = Kal a sd- gor) tn (si =th ts] tn{s) = out O83 KOH 5 out[s} = onF {so] enerabed uy seo a seuely tt ie ge ated ys and not} Ss Gatements we end of gaat 4 &- an Killed by Sg ther 4t will’ veach at Ihe . we @ fence get? ee = ae [sa] VU 4 She kit deggnilcon % exac 4 Se cred Jill (sj = kell [sale ( kil (si]- gerd) Jha can be alco read as Kill of S amecis lal f So lox kilt of St We not genet aed by sq qos qn [sa] 2 qeobsd req e gee 5) Kill [s} = Kel! [s 0 EU (sj a w{sil = th [s] é infss] = tafe) pe out [s]= outta} vous] en (HJ Kell iD) thy aeverse te generated vgen [s] = gen {si] vgea{sal where. as definition 'd of voutal We? alo pa | an ” (s] age (s] b> ( Kl] = Kelsi] | ! wel sil= mls] ¥ gen] out [s| = ou [si] in{s] > & the set of definitions reodkng the “begining tt inte account Me contol 41> am , ticludin stectementr hich Sie nested ble must be Killed a 54 taka Abrougnowt the ente por outside of 'S' OD withih 0 puts] > ts the Sek ob definitions reacting the tend os | live Variable analysis 2 Before unddrstandén hoo te compile dale Low equaliens we will leare two vinpostent terms ay use fs] - fase f Variables used by s bn des Ls] - & a set of vartables defined by s Sv gmt uss some wt Of Variables and defh Some set voviable 4 Ext~ negra 5 use =i ard de oi ene and det = CHM DD Us Htere either of the branch. | See a defiriteo rn) ; let us fee an ovithm for computing dataflow equation s using live Vartable analysis: sor all 6 cn[t]ag and cut [i> g Vepeak unlél no nen os all f vc tle suce (7) tthe use [to (out ~ dep tnd = | end vepeat ber compute inl] and cut sox follwing flow pape wang use and det Bi [rose J aie B>| fn Xr Z tse =n" ran © lee ®3 [zest] pez uf rela) use = ase te g v g g g 6 g rhtows we wo Will Computs | uk tf]=0 tm [F] whee fv (owt £8 - 4b BD v inl = use [? - out [sJ= abu m= 2 -p= tn [a] = use [stu (out (al-aegled = #98 ae, ilk Successor node oh | outl2] =v Ls) = \F4 ‘z' can bel nfs], -use [3] (outst 448)” vers caper ints] = Soh ut [al = 465 out [a] = 4 —py-4 in{ul =use {uJ v(out [J -deb ty) le uy out[s}~ trleJ~% “yy EO én (s]= use [s}ulmt [s]-depleD = a “te re vif stefon.24 ourl al - talely [=P OF= 1 tals] - wse [6] v (out [6] -dejle = % & GB? =H out [l= Lal = fx} infa] = use [FY (cut [7] —Aep [a =X UCM) =ZOX infa} = fszt out [2] = nla] 4x} tale] a welel © lout Le} —def tel) = 4 u fam} ly neglectaig >) Cm, vemove pyar any After IS¥ pass we ies - wn owt | g g a {xy p 3 my g 4 4xy v 5 foo pg + ia + fae} dx} e \eyl 4x Now we will compute for pass a: out [i] = tnfal = Ay 1 euse [Jo (out [iJ-dep i) =¢ olt-d= 4x4 fe in (t = neglek —1] outfa) = tr[3] 0 én [4} = fro} v fx = fmn,x} ih [a] = uefa] (out [a] -def [2]) = cu fone) =f a out [a= wnfs] =hm iXy cn [2|= use 13) u (out [a}- def l2])=m Y (Goh =m U(m%) a 9) — Jai out (= 4 tn ly = we Loy (ot - degli) = x GD dx ame as wn pass te Hence Since in[y} and oub [] “ws and cut LYJ gan : Lwe will nok conidey inlt) out [j= iol] fx} i Lj— usels}u (ourls} -def[s])= fixolry) fe need 4) = fmt out (¢] = infaju fe [2] = joa 0 {xs = Joys inlé] - we [du (out Lg -dep (ei) = 2 ¥ (Gye IA | = foug: zy} out fa] = in) = fret | ntaye webs u(our (a) deh 4D 4 9 (4mh -) | = Zo) = {fw} out [g) = inla] = fray ne j= use [8] u (out [8)-depL2]) =4 v (mr —m) =4 ur ~dacg} / out fy AA a fro} fry ~Mtey pass 2 We qe in Soi Heth foot oe 6 foyay — foypet a Simixj Sry ee ee le CNlow we will perform computations fov pas 3 out {t] = trial = jar] in [t}euse LOS” (out (d-deb UT =g & (fry fro) 4nd : out (a) _ifsju wl] afrmxdo iy afm) in fey = ues) v (out (9)-Aept]) 2x0 (Jah -9) = fin} | in[a} and out [a] & similar be previdus computa ' ienre we can- neglect it oubte]= ante] - {rvs sata] - weet abv (out La} ded [>= Ue Jhis & also similar lo prevBut pous Compukakcon. Je, thus we also gek similar values for node 4,458 - outs] = tn [l= fuyey fn[st—use 5]u (out (5]-det (50) fring (gory DL mrnyu 4004 = fm} After pass 6 Ue get in Ou \ fx} Jimi g a F fmvnzy [xp reel - Pev-foum Computalcon- fox paws y ob pacwious pees out [3J= in ls]}= fro WK 4 tn [3] = use L3] 0 (out 3} -deb L3}) =mv ( {masz} -7) afmixy “Ater pass 4 we ot i oer 3 Sens} fay ; in a out 8 soe cow wnt, Ana volust Oh fae ir oul” gyda fry pel jay Aron {och g ove Ser} fogs Srey Jy Mast} ayy fmnd

You might also like