Download as pdf
Download as pdf
You are on page 1of 8
es PKBRTHLE Drsub props, Ib “ MVSRec Compiler Construction Unit-4 (Part-3) ‘Machine-Independent Optimizations In this optimization, the compiler tokes in the intermediate code and transforms a part of the ode that does not involve any CPU registers and/or absolute memory locations. For example: do € item= 10; value = value + item: }while(valuect00)- ‘This code involves repeated assignment of the identifier item, which if we put this way: I Item = 10; do ul ‘ value = value + item: } while(valuectoo); should not only save the CPU cycles, but can be used on any processor. ‘The Principal Sources of Optimization ‘A-compiler optimization must preserve the semantics of the original program. Except in very special circumstances, once a programmer chooses and implements a particular algorithm, the compiler ‘cannot understand enough about the program to replace it with a substantially different and more efficient clgorithm, A compiler knows only how to apply relatively low-level semantic Fronsformations, using general facts such os algebraic identities like i + O = i or program Semantics ‘such as the fact that performing the same operation on the same values yields the same result, pe haha Scanned with CamScanner a Redundant operations are. = at the source level ~ aside effect of having written the program in a high-level language ~ Each of high-level data-structure accesses expands into a number of low-level cerithmetic operations 17% Programmers are not aware of these low-level operations end comat eliminate the redundancies themselves. <7 By having a compiler eliminate the redundancies ‘The programs are both efficient and easy to maintain ‘4 Bunning Example: Quick sort void quicksort(int m, int n) /* recursively sorts alm] through afn) */ { int i, j; int ¥, x} if (n <= m) return; /* fragment begins here +/ emt; j= a3 v= alnl; while (1) { do i= itl; while (ali) < v); do j = j-1; while (a(j] > v); if (i >= j) break; sali]; ali) = alj); aljl =x; /* svap ali, alj) #/ > el anes x = ali); ali) = alm); alo) = x; /* swap alil, aln) */ \ /* fragment ends here +/ quicksort(n,j); quicksort (i#1,n); } ‘Above given code is'C' code for quick sort Co) msl portal Genonrear of Sem of tp el ' logat ou u ooow uw aCwerd 0 1 Jo Pts Agorithm the Omumptin 40 trek Mobegee vist ocompy y hg a: Scanned with CamScanner Yo bebo code, reali] ko trembled bos WaT uc alte} 2 Three Ades Cle fr theme srencadets CFateewe dA Me, SOAS tim W deemed 4 w'} aljdex © 6 i ale berownes, @ AF tev goto Om 4 : i 2 Ho-ykd ay 4002 (2) Af. ts > v goto (9) ol ae te a's 3 Goto a HW) ty det =) x r= altg] Evory i ACE TA We original Prag eran, Hrontlakes tok Rain of Steps , wontasni Maks Yan avesday bob cewitig Oporction I Flow graph for quick sort code is given below Go, Shorr Spnognam —= , et ho been eupanded ia lacge 1-0ddy AN cordtbinal uno ditional jumps +o bem. 10 ohove Interwedtate Code have been replated with Jrraps « a loops oe. pratnt = ~ B>, A ave lope =e 7 2 — BRR Be = Bowie bagettey Pim a topp. B pow. : is = : = Scanned with CamScanner a a : sider as 2 Ha 4 tmiration of Common (ub-ene. nts Trot + Arnumber of ways in which a compiler can improve a program without changing the function it computes ~ Common-sub expression elimination s\, = Copy propagation = Dead-code elimination = Constant folding > Gammon Sub xp or. caer oS Chi nabion - ~ Previously eomputed ~The values of the variables not changed [met] oa, : ey leo = ateay | jateey = eo . a(ts] = x ' [sey cays : Bey oo ate Consider . Porm gang In previous prge: ' oe bs Specified Here ave & Ivops in the gry from Hewlr graghy above diagram ghowt 2h from Bs* i tqawont to £6 WEF | bey Wtlo one Some fede ae ae fvceeed ad oe qub-expnevions eS 0 Yat kWAL- Cbelore elivrinedion) sub -empreasors We we a phror _diieioag common me fo ple of +. 7 4g le plate of bebe Ca Scanned with CamScanner ; a - bbe performing LOLOL co mora Gab-emp. ebrivinedion , POs - Por ae, TE SHI Uralaates Globak. Sub -¢ a et eae = aD YAY an frown» TS 44. : " _ fnam 81 86, We Com See Har: ree He = 4a bes tb storties tLe He} ~ ear oCer) £ erste} * Eucté = Horus} peer fo Above eye, ophimigaction ean] cox) be seoplercented ony block? - we Any value of Hale Portioday vartable used as nok changeo before vairg. a Jo me gnash +9 con be ered » Copy Propagation arets Ve copies) era] [oa] [evan] [eww irs alt woe, El Ge el Ee Lele avin [exae eed ee a alee gab ayes so ry a BS in ‘ahove Ciagnaw Com ‘be Further improved bp ebro sone lovemal ab exp, Cz Ate ae vow fo bold vale of die. consider Figdlorsy bel wh te yt y 2 ‘ j oy ‘ bo. Con! shone, te figuy J Je ae perch te aber asdee fi bedee so ih nol eioaik tdea fo place “thet «Regen tden of tory Prof, : arr =e hk wt ov tw where evr possi Ble « . Scanned with CamScanner => A variable is lve ata point ina program if its value can be used subsequently: Otherwise, it is dead at that point. wld yaere + constant folding 1s teeta i S< ~ _Deducing at compile tie thatthe vlue ofan expressions constant end using the. Comptved | constant instead + econ veces If ip oye az ls) b FE(a==0 Go ile y | Bert lontlant fotde f : he a rey ct 2 yoo) a yw @ vale of Eo ale tel nate"), ~ compile Hime “Goal * feinpsrtot niin that decreses te amaint fede intaop by ving ode op-lnurantcomoxtatia : : tuk “bf foop An expression that yields the sane result independent ofthe number of times loop isexeaited ‘Code Motion takes loop-invariant computation before its loop 4p We mbove Loop, wo if Whale jp be, Ves of Wik: will noir Unanges ko" flr tow madsFred’ ost— temp 2 yi wile Liz eters) Scanned with CamScanner > Induction Variables and Reduction in Strength + Induction variable = For-an induction variable 2, there is positive or negative constant c-such that each tiie xis assigned, its value increases by.¢.+ + Induction variables can be computed with a single increment (etion ractin per loop iteration + Strength reduction = The transformation of replacing an expensive operation, such as multiplication, by a cheaper one, such as addition + : So xr do BY, Tere = = eliminate computation awe trducton 1 vit ables « —H* When Hee owt move = . ~ Hom 2 toduthbn * — 99 BB Jay ! Wsinbies 50 boop rt induchtoo ik ors VAERbICS + Recoure, postibe b i By thth re vine alive nare ne {ie ih, of “F derrensea by 4, From vole of , Rue 1 (te 4 Aseweores byw a we have Eo loner le, = Exownple. for So we [i b=5 soon] we Gear meIOONe- NEALE? f- J gee * iT becom . for to15 t 4 sto,iny by > nned @ be : “ Count = £ wiory +O 83 A J ie BM. 1 : SEM FL 40; ale : a 7 Wie woof HO eleni Above code tom be $056 £icjos | wodsbred nk, Uewrges ale y . da ey , Mable ei tan bates ‘i de 30 bee ww (Pay fect idey nt ty ee ed Bowie Slocks- wi cook 242 2 a | nelot i ay tis Sette Scanned with CamScanner sa —— diately following d to ‘A definition d reaches a point pif there is path from the pint immediately F i ee 17° such that dis not "killed along that path. -Q mihere olong the “4 ‘A definition ofa variable xi killed if there i any other definition of x anywhere 22M? a 2 — ifwedonot loon whetherasttenentsisssigingavahe oxnemuctessime SD ‘that it may assign to it, fore oURiy POE * i, Je vate op Geta De estcaantt mat lee ge ro al | eS Scanned with CamScanner

You might also like