Graphics & Design

Code generation strategies. Register Allocation. Register allocation. Main idea 2/27/ PDF

Description
Co gnrtion strtgis Rgistr Allotion Simpl: stk o ll vrils n usr-fin tmporris r llot on stk us rgistrs s tmporris whn vluting xprssions x86: n to rturn vlus from funtion lls Bttr strtgy: us rgistrs to ru
Published
of 12
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
Share
Transcript
Co gnrtion strtgis Rgistr Allotion Simpl: stk o ll vrils n usr-fin tmporris r llot on stk us rgistrs s tmporris whn vluting xprssions x86: n to rturn vlus from funtion lls Bttr strtgy: us rgistrs to ru los/stors lol vrils n usr-fin tmporris n llot to rgistrs if you hv nough rgistrs in our isussion, w will fous on usr-fin tmporris lso n rgistrs to rturn vlus from funtion lls, to prform som instrutions (g. MUL in x86) Approh: gnrt strt ssmly o in whih you ssum you hv n unoun numr of rgistrs prform rgistr llotion Rlity is littl mor omplx ut this is th high-lvl i Min i Wnt to rpl tmporry vrils with som fix st of rgistrs First: n to know whih vrils r liv ftr h instrution Two simultnously liv vrils nnot llot to th sm rgistr Rgistr llotion For vry no n in CFG, w hv out[n] St of tmporris liv out of n Two vrils intrfr if oth initilly liv (i: funtion rgs), or oth ppr in out[n] for ny n How to ssign rgistrs to vrils? 1 Intrfrn grph Nos of th grph = vrils Egs onnt vrils tht intrfr with on nothr Nos will ssign olor orrsponing to th rgistr ssign to th vril Two olors n t nxt to on nothr in th grph Instrutions = + 2 = * = + 1 rturn * Intrfrn grph Liv vrs Intrfrn grph Intrfrn grph Instrutions Liv vrs Instrutions Liv vrs = + 2 = + 2 = * = + 1 rturn *, = * = + 1 rturn *,, 2 Intrfrn grph Intrfrn grph Instrutions = + 2 = * = + 1 rturn * Liv vrs,,, Instrutions = + 2 = * = + 1 rturn * Liv vrs,,, Intrfrn grph Intrfrn grph olor rgistr olor rgistr Instrutions = + 2 = * = + 1 rturn * Liv vrs,,, x x Instrutions = + 2 = * = + 1 rturn * Liv vrs,,, x x 3 Grph oloring Qustions: Cn w ffiintly fin oloring of th grph whnvr possil? Cn w ffiintly fin n optiml oloring of th grph? How o w hoos rgistrs to voi mov instrutions? Wht o w o whn thr rn t nough olors (rgistrs) to olor th grph? Kmp s huristi Kmp s lgorithm [1879] for fining K-oloring of grph Stp 1 (simplify): fin no with t most K-1 gs n ut it out of th grph. rmmr this no on stk for ltr stgs. Intuition: on oloring is foun for th simplr grph, w n lwys olor th no w sv on th stk Stp 2 (olor): whn th simplifi grph hs n olor, k th no on th top of th stk n ssign it olor not tkn y on of th jnt nos olor rgistr olor rgistr x x x x stk: stk: 4 5 x x olor rgistr stk: x x olor rgistr stk: x x olor rgistr stk: x x olor rgistr stk: 6 x x olor rgistr stk: x x olor rgistr stk: x x olor rgistr stk: x x olor rgistr stk: olor rgistr x x stk: Filur If th grph nnot olor, it will vntully simplifi to grph in whih vry no hs t lst K nighors Somtims, th grph is still K-olorl! Fining K-oloring in ll situtions is n NP-omplt prolm W will hv to pproximt to mk rgistr llotors fst nough olor rgistr olor rgistr x x x x stk: stk: ll nos hv 2 nighours! 7 8 x x olor rgistr stk: x x olor rgistr stk: x x olor rgistr stk: x x olor rgistr stk: olor rgistr olor rgistr x x x x stk: stk: olor rgistr olor rgistr x x x x Som grphs n t olor in K olors: stk: stk: W got luky! 9 olor rgistr olor rgistr x x Som grphs n t olor in K olors: x x Som grphs n t olor in K olors: stk: stk: olor rgistr x x Som grphs n t olor in K olors: stk: Spilling Stp 3 (spilling): on ll nos hv K or mor nighors, pik no for spilling Storg on th stk Thr r mny huristis tht n us to pik no not in n innr loop no olors lft for! 10 Spilling o W n to gnrt xtr instrutions to lo vrils from stk n stor thm Ths instrutions us rgistrs thmslvs. Wht to o? Stupi pproh: lwys kp xtr rgistrs hny for shuffling t in n out Bttr pproh: rwrit o introuing nw tmporry; rrun livnss nlysis n rgistr llotion Intuition: you wr not l to ssign singl rgistr to th vril tht ws spill ut thr my fr rgistr vill t h spot whr you n to us th vlu of tht vril Rwriting o Consir: t1 t2 Suppos t2 is slt for spilling n ssign to stk lotion [p-24] Invnt nw tmporry t35 for just this instrution n rwrit: mov t35, [p 24]; t1, t35 Avntg: t35 hs vry short liv rng n is muh lss likly to intrfr. Rrun th lgorithm; fwr vrils will spill Prolor Nos Som vrils r pr-ssign to rgistrs Eg: mul on x86/pntium uss x; fins x, x Eg: ll on x86/pntium Dfins (trshs) llr-sv rgistrs x, x, x Trt ths rgistrs s spil tmporris; for ginning, thm to th grph with thir olors Prolor Nos Cn t simplify grph y rmoving prolor no Prolor nos r th strting point of th oloring pross On simplifi own to olor nos strt ing k th othr nos s for 11 Optimizing Movs Co gnrtion prous lot of xtr mov instrutions mov t1, t2 If w n ssign t1 n t2 to th sm rgistr, w o not hv to xut th mov I: if t1 n t2 r not onnt in th intrfrn grph, w ols into singl vril Colsing Prolm: olsing n inrs th numr of intrfrn gs n mk grph unolorl ols t1 t2 t1/t2 Solution 1 (Briggs): voi rtion of high-gr ( = K) nos Solution 2 (Gorg): n ols with if vry nighour t of : lry intrfrs with, or hs low-gr ( K) Simplify & Cols Ovrll Algorithm Stp 1 (simplify): simplify s muh s possil without rmoving nos tht r th sour or stintion of mov (mov-rlt nos) Stp 2 (ols): ols mov-rlt nos provi low-gr no rsults Stp 3 (frz): if nithr stps 1 or 2 pply, frz mov instrution: rgistrs involv r mrk not mov-rlt n try stp 1 gin Simplify, frz n ols Mrk possil spills Color & tt tul spills Livnss Rwrit o to implmnt tul spills 12
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks