ira-color.c 153 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852
  1. /* IRA allocation based on graph coloring.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. Contributed by Vladimir Makarov <vmakarov@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "rtl.h"
  21. #include "tm_p.h"
  22. #include "target.h"
  23. #include "regs.h"
  24. #include "flags.h"
  25. #include "sbitmap.h"
  26. #include "bitmap.h"
  27. #include "hash-table.h"
  28. #include "hard-reg-set.h"
  29. #include "predict.h"
  30. #include "vec.h"
  31. #include "hashtab.h"
  32. #include "hash-set.h"
  33. #include "machmode.h"
  34. #include "input.h"
  35. #include "function.h"
  36. #include "dominance.h"
  37. #include "cfg.h"
  38. #include "basic-block.h"
  39. #include "symtab.h"
  40. #include "statistics.h"
  41. #include "double-int.h"
  42. #include "real.h"
  43. #include "fixed-value.h"
  44. #include "alias.h"
  45. #include "wide-int.h"
  46. #include "inchash.h"
  47. #include "tree.h"
  48. #include "insn-config.h"
  49. #include "expmed.h"
  50. #include "dojump.h"
  51. #include "explow.h"
  52. #include "calls.h"
  53. #include "emit-rtl.h"
  54. #include "varasm.h"
  55. #include "stmt.h"
  56. #include "expr.h"
  57. #include "diagnostic-core.h"
  58. #include "reload.h"
  59. #include "params.h"
  60. #include "df.h"
  61. #include "ira-int.h"
  62. typedef struct allocno_hard_regs *allocno_hard_regs_t;
  63. /* The structure contains information about hard registers can be
  64. assigned to allocnos. Usually it is allocno profitable hard
  65. registers but in some cases this set can be a bit different. Major
  66. reason of the difference is a requirement to use hard register sets
  67. that form a tree or a forest (set of trees), i.e. hard register set
  68. of a node should contain hard register sets of its subnodes. */
  69. struct allocno_hard_regs
  70. {
  71. /* Hard registers can be assigned to an allocno. */
  72. HARD_REG_SET set;
  73. /* Overall (spilling) cost of all allocnos with given register
  74. set. */
  75. int64_t cost;
  76. };
  77. typedef struct allocno_hard_regs_node *allocno_hard_regs_node_t;
  78. /* A node representing allocno hard registers. Such nodes form a
  79. forest (set of trees). Each subnode of given node in the forest
  80. refers for hard register set (usually allocno profitable hard
  81. register set) which is a subset of one referred from given
  82. node. */
  83. struct allocno_hard_regs_node
  84. {
  85. /* Set up number of the node in preorder traversing of the forest. */
  86. int preorder_num;
  87. /* Used for different calculation like finding conflict size of an
  88. allocno. */
  89. int check;
  90. /* Used for calculation of conflict size of an allocno. The
  91. conflict size of the allocno is maximal number of given allocno
  92. hard registers needed for allocation of the conflicting allocnos.
  93. Given allocno is trivially colored if this number plus the number
  94. of hard registers needed for given allocno is not greater than
  95. the number of given allocno hard register set. */
  96. int conflict_size;
  97. /* The number of hard registers given by member hard_regs. */
  98. int hard_regs_num;
  99. /* The following member is used to form the final forest. */
  100. bool used_p;
  101. /* Pointer to the corresponding profitable hard registers. */
  102. allocno_hard_regs_t hard_regs;
  103. /* Parent, first subnode, previous and next node with the same
  104. parent in the forest. */
  105. allocno_hard_regs_node_t parent, first, prev, next;
  106. };
  107. /* Info about changing hard reg costs of an allocno. */
  108. struct update_cost_record
  109. {
  110. /* Hard regno for which we changed the cost. */
  111. int hard_regno;
  112. /* Divisor used when we changed the cost of HARD_REGNO. */
  113. int divisor;
  114. /* Next record for given allocno. */
  115. struct update_cost_record *next;
  116. };
  117. /* To decrease footprint of ira_allocno structure we store all data
  118. needed only for coloring in the following structure. */
  119. struct allocno_color_data
  120. {
  121. /* TRUE value means that the allocno was not removed yet from the
  122. conflicting graph during coloring. */
  123. unsigned int in_graph_p : 1;
  124. /* TRUE if it is put on the stack to make other allocnos
  125. colorable. */
  126. unsigned int may_be_spilled_p : 1;
  127. /* TRUE if the allocno is trivially colorable. */
  128. unsigned int colorable_p : 1;
  129. /* Number of hard registers of the allocno class really
  130. available for the allocno allocation. It is number of the
  131. profitable hard regs. */
  132. int available_regs_num;
  133. /* Allocnos in a bucket (used in coloring) chained by the following
  134. two members. */
  135. ira_allocno_t next_bucket_allocno;
  136. ira_allocno_t prev_bucket_allocno;
  137. /* Used for temporary purposes. */
  138. int temp;
  139. /* Used to exclude repeated processing. */
  140. int last_process;
  141. /* Profitable hard regs available for this pseudo allocation. It
  142. means that the set excludes unavailable hard regs and hard regs
  143. conflicting with given pseudo. They should be of the allocno
  144. class. */
  145. HARD_REG_SET profitable_hard_regs;
  146. /* The allocno hard registers node. */
  147. allocno_hard_regs_node_t hard_regs_node;
  148. /* Array of structures allocno_hard_regs_subnode representing
  149. given allocno hard registers node (the 1st element in the array)
  150. and all its subnodes in the tree (forest) of allocno hard
  151. register nodes (see comments above). */
  152. int hard_regs_subnodes_start;
  153. /* The length of the previous array. */
  154. int hard_regs_subnodes_num;
  155. /* Records about updating allocno hard reg costs from copies. If
  156. the allocno did not get expected hard register, these records are
  157. used to restore original hard reg costs of allocnos connected to
  158. this allocno by copies. */
  159. struct update_cost_record *update_cost_records;
  160. /* Threads. We collect allocnos connected by copies into threads
  161. and try to assign hard regs to allocnos by threads. */
  162. /* Allocno representing all thread. */
  163. ira_allocno_t first_thread_allocno;
  164. /* Allocnos in thread forms a cycle list through the following
  165. member. */
  166. ira_allocno_t next_thread_allocno;
  167. /* All thread frequency. Defined only for first thread allocno. */
  168. int thread_freq;
  169. };
  170. /* See above. */
  171. typedef struct allocno_color_data *allocno_color_data_t;
  172. /* Container for storing allocno data concerning coloring. */
  173. static allocno_color_data_t allocno_color_data;
  174. /* Macro to access the data concerning coloring. */
  175. #define ALLOCNO_COLOR_DATA(a) ((allocno_color_data_t) ALLOCNO_ADD_DATA (a))
  176. /* Used for finding allocno colorability to exclude repeated allocno
  177. processing and for updating preferencing to exclude repeated
  178. allocno processing during assignment. */
  179. static int curr_allocno_process;
  180. /* This file contains code for regional graph coloring, spill/restore
  181. code placement optimization, and code helping the reload pass to do
  182. a better job. */
  183. /* Bitmap of allocnos which should be colored. */
  184. static bitmap coloring_allocno_bitmap;
  185. /* Bitmap of allocnos which should be taken into account during
  186. coloring. In general case it contains allocnos from
  187. coloring_allocno_bitmap plus other already colored conflicting
  188. allocnos. */
  189. static bitmap consideration_allocno_bitmap;
  190. /* All allocnos sorted according their priorities. */
  191. static ira_allocno_t *sorted_allocnos;
  192. /* Vec representing the stack of allocnos used during coloring. */
  193. static vec<ira_allocno_t> allocno_stack_vec;
  194. /* Helper for qsort comparison callbacks - return a positive integer if
  195. X > Y, or a negative value otherwise. Use a conditional expression
  196. instead of a difference computation to insulate from possible overflow
  197. issues, e.g. X - Y < 0 for some X > 0 and Y < 0. */
  198. #define SORTGT(x,y) (((x) > (y)) ? 1 : -1)
  199. /* Definition of vector of allocno hard registers. */
  200. /* Vector of unique allocno hard registers. */
  201. static vec<allocno_hard_regs_t> allocno_hard_regs_vec;
  202. struct allocno_hard_regs_hasher : typed_noop_remove <allocno_hard_regs>
  203. {
  204. typedef allocno_hard_regs value_type;
  205. typedef allocno_hard_regs compare_type;
  206. static inline hashval_t hash (const value_type *);
  207. static inline bool equal (const value_type *, const compare_type *);
  208. };
  209. /* Returns hash value for allocno hard registers V. */
  210. inline hashval_t
  211. allocno_hard_regs_hasher::hash (const value_type *hv)
  212. {
  213. return iterative_hash (&hv->set, sizeof (HARD_REG_SET), 0);
  214. }
  215. /* Compares allocno hard registers V1 and V2. */
  216. inline bool
  217. allocno_hard_regs_hasher::equal (const value_type *hv1, const compare_type *hv2)
  218. {
  219. return hard_reg_set_equal_p (hv1->set, hv2->set);
  220. }
  221. /* Hash table of unique allocno hard registers. */
  222. static hash_table<allocno_hard_regs_hasher> *allocno_hard_regs_htab;
  223. /* Return allocno hard registers in the hash table equal to HV. */
  224. static allocno_hard_regs_t
  225. find_hard_regs (allocno_hard_regs_t hv)
  226. {
  227. return allocno_hard_regs_htab->find (hv);
  228. }
  229. /* Insert allocno hard registers HV in the hash table (if it is not
  230. there yet) and return the value which in the table. */
  231. static allocno_hard_regs_t
  232. insert_hard_regs (allocno_hard_regs_t hv)
  233. {
  234. allocno_hard_regs **slot = allocno_hard_regs_htab->find_slot (hv, INSERT);
  235. if (*slot == NULL)
  236. *slot = hv;
  237. return *slot;
  238. }
  239. /* Initialize data concerning allocno hard registers. */
  240. static void
  241. init_allocno_hard_regs (void)
  242. {
  243. allocno_hard_regs_vec.create (200);
  244. allocno_hard_regs_htab
  245. = new hash_table<allocno_hard_regs_hasher> (200);
  246. }
  247. /* Add (or update info about) allocno hard registers with SET and
  248. COST. */
  249. static allocno_hard_regs_t
  250. add_allocno_hard_regs (HARD_REG_SET set, int64_t cost)
  251. {
  252. struct allocno_hard_regs temp;
  253. allocno_hard_regs_t hv;
  254. gcc_assert (! hard_reg_set_empty_p (set));
  255. COPY_HARD_REG_SET (temp.set, set);
  256. if ((hv = find_hard_regs (&temp)) != NULL)
  257. hv->cost += cost;
  258. else
  259. {
  260. hv = ((struct allocno_hard_regs *)
  261. ira_allocate (sizeof (struct allocno_hard_regs)));
  262. COPY_HARD_REG_SET (hv->set, set);
  263. hv->cost = cost;
  264. allocno_hard_regs_vec.safe_push (hv);
  265. insert_hard_regs (hv);
  266. }
  267. return hv;
  268. }
  269. /* Finalize data concerning allocno hard registers. */
  270. static void
  271. finish_allocno_hard_regs (void)
  272. {
  273. int i;
  274. allocno_hard_regs_t hv;
  275. for (i = 0;
  276. allocno_hard_regs_vec.iterate (i, &hv);
  277. i++)
  278. ira_free (hv);
  279. delete allocno_hard_regs_htab;
  280. allocno_hard_regs_htab = NULL;
  281. allocno_hard_regs_vec.release ();
  282. }
  283. /* Sort hard regs according to their frequency of usage. */
  284. static int
  285. allocno_hard_regs_compare (const void *v1p, const void *v2p)
  286. {
  287. allocno_hard_regs_t hv1 = *(const allocno_hard_regs_t *) v1p;
  288. allocno_hard_regs_t hv2 = *(const allocno_hard_regs_t *) v2p;
  289. if (hv2->cost > hv1->cost)
  290. return 1;
  291. else if (hv2->cost < hv1->cost)
  292. return -1;
  293. else
  294. return 0;
  295. }
  296. /* Used for finding a common ancestor of two allocno hard registers
  297. nodes in the forest. We use the current value of
  298. 'node_check_tick' to mark all nodes from one node to the top and
  299. then walking up from another node until we find a marked node.
  300. It is also used to figure out allocno colorability as a mark that
  301. we already reset value of member 'conflict_size' for the forest
  302. node corresponding to the processed allocno. */
  303. static int node_check_tick;
  304. /* Roots of the forest containing hard register sets can be assigned
  305. to allocnos. */
  306. static allocno_hard_regs_node_t hard_regs_roots;
  307. /* Definition of vector of allocno hard register nodes. */
  308. /* Vector used to create the forest. */
  309. static vec<allocno_hard_regs_node_t> hard_regs_node_vec;
  310. /* Create and return allocno hard registers node containing allocno
  311. hard registers HV. */
  312. static allocno_hard_regs_node_t
  313. create_new_allocno_hard_regs_node (allocno_hard_regs_t hv)
  314. {
  315. allocno_hard_regs_node_t new_node;
  316. new_node = ((struct allocno_hard_regs_node *)
  317. ira_allocate (sizeof (struct allocno_hard_regs_node)));
  318. new_node->check = 0;
  319. new_node->hard_regs = hv;
  320. new_node->hard_regs_num = hard_reg_set_size (hv->set);
  321. new_node->first = NULL;
  322. new_node->used_p = false;
  323. return new_node;
  324. }
  325. /* Add allocno hard registers node NEW_NODE to the forest on its level
  326. given by ROOTS. */
  327. static void
  328. add_new_allocno_hard_regs_node_to_forest (allocno_hard_regs_node_t *roots,
  329. allocno_hard_regs_node_t new_node)
  330. {
  331. new_node->next = *roots;
  332. if (new_node->next != NULL)
  333. new_node->next->prev = new_node;
  334. new_node->prev = NULL;
  335. *roots = new_node;
  336. }
  337. /* Add allocno hard registers HV (or its best approximation if it is
  338. not possible) to the forest on its level given by ROOTS. */
  339. static void
  340. add_allocno_hard_regs_to_forest (allocno_hard_regs_node_t *roots,
  341. allocno_hard_regs_t hv)
  342. {
  343. unsigned int i, start;
  344. allocno_hard_regs_node_t node, prev, new_node;
  345. HARD_REG_SET temp_set;
  346. allocno_hard_regs_t hv2;
  347. start = hard_regs_node_vec.length ();
  348. for (node = *roots; node != NULL; node = node->next)
  349. {
  350. if (hard_reg_set_equal_p (hv->set, node->hard_regs->set))
  351. return;
  352. if (hard_reg_set_subset_p (hv->set, node->hard_regs->set))
  353. {
  354. add_allocno_hard_regs_to_forest (&node->first, hv);
  355. return;
  356. }
  357. if (hard_reg_set_subset_p (node->hard_regs->set, hv->set))
  358. hard_regs_node_vec.safe_push (node);
  359. else if (hard_reg_set_intersect_p (hv->set, node->hard_regs->set))
  360. {
  361. COPY_HARD_REG_SET (temp_set, hv->set);
  362. AND_HARD_REG_SET (temp_set, node->hard_regs->set);
  363. hv2 = add_allocno_hard_regs (temp_set, hv->cost);
  364. add_allocno_hard_regs_to_forest (&node->first, hv2);
  365. }
  366. }
  367. if (hard_regs_node_vec.length ()
  368. > start + 1)
  369. {
  370. /* Create a new node which contains nodes in hard_regs_node_vec. */
  371. CLEAR_HARD_REG_SET (temp_set);
  372. for (i = start;
  373. i < hard_regs_node_vec.length ();
  374. i++)
  375. {
  376. node = hard_regs_node_vec[i];
  377. IOR_HARD_REG_SET (temp_set, node->hard_regs->set);
  378. }
  379. hv = add_allocno_hard_regs (temp_set, hv->cost);
  380. new_node = create_new_allocno_hard_regs_node (hv);
  381. prev = NULL;
  382. for (i = start;
  383. i < hard_regs_node_vec.length ();
  384. i++)
  385. {
  386. node = hard_regs_node_vec[i];
  387. if (node->prev == NULL)
  388. *roots = node->next;
  389. else
  390. node->prev->next = node->next;
  391. if (node->next != NULL)
  392. node->next->prev = node->prev;
  393. if (prev == NULL)
  394. new_node->first = node;
  395. else
  396. prev->next = node;
  397. node->prev = prev;
  398. node->next = NULL;
  399. prev = node;
  400. }
  401. add_new_allocno_hard_regs_node_to_forest (roots, new_node);
  402. }
  403. hard_regs_node_vec.truncate (start);
  404. }
  405. /* Add allocno hard registers nodes starting with the forest level
  406. given by FIRST which contains biggest set inside SET. */
  407. static void
  408. collect_allocno_hard_regs_cover (allocno_hard_regs_node_t first,
  409. HARD_REG_SET set)
  410. {
  411. allocno_hard_regs_node_t node;
  412. ira_assert (first != NULL);
  413. for (node = first; node != NULL; node = node->next)
  414. if (hard_reg_set_subset_p (node->hard_regs->set, set))
  415. hard_regs_node_vec.safe_push (node);
  416. else if (hard_reg_set_intersect_p (set, node->hard_regs->set))
  417. collect_allocno_hard_regs_cover (node->first, set);
  418. }
  419. /* Set up field parent as PARENT in all allocno hard registers nodes
  420. in forest given by FIRST. */
  421. static void
  422. setup_allocno_hard_regs_nodes_parent (allocno_hard_regs_node_t first,
  423. allocno_hard_regs_node_t parent)
  424. {
  425. allocno_hard_regs_node_t node;
  426. for (node = first; node != NULL; node = node->next)
  427. {
  428. node->parent = parent;
  429. setup_allocno_hard_regs_nodes_parent (node->first, node);
  430. }
  431. }
  432. /* Return allocno hard registers node which is a first common ancestor
  433. node of FIRST and SECOND in the forest. */
  434. static allocno_hard_regs_node_t
  435. first_common_ancestor_node (allocno_hard_regs_node_t first,
  436. allocno_hard_regs_node_t second)
  437. {
  438. allocno_hard_regs_node_t node;
  439. node_check_tick++;
  440. for (node = first; node != NULL; node = node->parent)
  441. node->check = node_check_tick;
  442. for (node = second; node != NULL; node = node->parent)
  443. if (node->check == node_check_tick)
  444. return node;
  445. return first_common_ancestor_node (second, first);
  446. }
  447. /* Print hard reg set SET to F. */
  448. static void
  449. print_hard_reg_set (FILE *f, HARD_REG_SET set, bool new_line_p)
  450. {
  451. int i, start;
  452. for (start = -1, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  453. {
  454. if (TEST_HARD_REG_BIT (set, i))
  455. {
  456. if (i == 0 || ! TEST_HARD_REG_BIT (set, i - 1))
  457. start = i;
  458. }
  459. if (start >= 0
  460. && (i == FIRST_PSEUDO_REGISTER - 1 || ! TEST_HARD_REG_BIT (set, i)))
  461. {
  462. if (start == i - 1)
  463. fprintf (f, " %d", start);
  464. else if (start == i - 2)
  465. fprintf (f, " %d %d", start, start + 1);
  466. else
  467. fprintf (f, " %d-%d", start, i - 1);
  468. start = -1;
  469. }
  470. }
  471. if (new_line_p)
  472. fprintf (f, "\n");
  473. }
  474. /* Print allocno hard register subforest given by ROOTS and its LEVEL
  475. to F. */
  476. static void
  477. print_hard_regs_subforest (FILE *f, allocno_hard_regs_node_t roots,
  478. int level)
  479. {
  480. int i;
  481. allocno_hard_regs_node_t node;
  482. for (node = roots; node != NULL; node = node->next)
  483. {
  484. fprintf (f, " ");
  485. for (i = 0; i < level * 2; i++)
  486. fprintf (f, " ");
  487. fprintf (f, "%d:(", node->preorder_num);
  488. print_hard_reg_set (f, node->hard_regs->set, false);
  489. fprintf (f, ")@%"PRId64"\n", node->hard_regs->cost);
  490. print_hard_regs_subforest (f, node->first, level + 1);
  491. }
  492. }
  493. /* Print the allocno hard register forest to F. */
  494. static void
  495. print_hard_regs_forest (FILE *f)
  496. {
  497. fprintf (f, " Hard reg set forest:\n");
  498. print_hard_regs_subforest (f, hard_regs_roots, 1);
  499. }
  500. /* Print the allocno hard register forest to stderr. */
  501. void
  502. ira_debug_hard_regs_forest (void)
  503. {
  504. print_hard_regs_forest (stderr);
  505. }
  506. /* Remove unused allocno hard registers nodes from forest given by its
  507. *ROOTS. */
  508. static void
  509. remove_unused_allocno_hard_regs_nodes (allocno_hard_regs_node_t *roots)
  510. {
  511. allocno_hard_regs_node_t node, prev, next, last;
  512. for (prev = NULL, node = *roots; node != NULL; node = next)
  513. {
  514. next = node->next;
  515. if (node->used_p)
  516. {
  517. remove_unused_allocno_hard_regs_nodes (&node->first);
  518. prev = node;
  519. }
  520. else
  521. {
  522. for (last = node->first;
  523. last != NULL && last->next != NULL;
  524. last = last->next)
  525. ;
  526. if (last != NULL)
  527. {
  528. if (prev == NULL)
  529. *roots = node->first;
  530. else
  531. prev->next = node->first;
  532. if (next != NULL)
  533. next->prev = last;
  534. last->next = next;
  535. next = node->first;
  536. }
  537. else
  538. {
  539. if (prev == NULL)
  540. *roots = next;
  541. else
  542. prev->next = next;
  543. if (next != NULL)
  544. next->prev = prev;
  545. }
  546. ira_free (node);
  547. }
  548. }
  549. }
  550. /* Set up fields preorder_num starting with START_NUM in all allocno
  551. hard registers nodes in forest given by FIRST. Return biggest set
  552. PREORDER_NUM increased by 1. */
  553. static int
  554. enumerate_allocno_hard_regs_nodes (allocno_hard_regs_node_t first,
  555. allocno_hard_regs_node_t parent,
  556. int start_num)
  557. {
  558. allocno_hard_regs_node_t node;
  559. for (node = first; node != NULL; node = node->next)
  560. {
  561. node->preorder_num = start_num++;
  562. node->parent = parent;
  563. start_num = enumerate_allocno_hard_regs_nodes (node->first, node,
  564. start_num);
  565. }
  566. return start_num;
  567. }
  568. /* Number of allocno hard registers nodes in the forest. */
  569. static int allocno_hard_regs_nodes_num;
  570. /* Table preorder number of allocno hard registers node in the forest
  571. -> the allocno hard registers node. */
  572. static allocno_hard_regs_node_t *allocno_hard_regs_nodes;
  573. /* See below. */
  574. typedef struct allocno_hard_regs_subnode *allocno_hard_regs_subnode_t;
  575. /* The structure is used to describes all subnodes (not only immediate
  576. ones) in the mentioned above tree for given allocno hard register
  577. node. The usage of such data accelerates calculation of
  578. colorability of given allocno. */
  579. struct allocno_hard_regs_subnode
  580. {
  581. /* The conflict size of conflicting allocnos whose hard register
  582. sets are equal sets (plus supersets if given node is given
  583. allocno hard registers node) of one in the given node. */
  584. int left_conflict_size;
  585. /* The summary conflict size of conflicting allocnos whose hard
  586. register sets are strict subsets of one in the given node.
  587. Overall conflict size is
  588. left_conflict_subnodes_size
  589. + MIN (max_node_impact - left_conflict_subnodes_size,
  590. left_conflict_size)
  591. */
  592. short left_conflict_subnodes_size;
  593. short max_node_impact;
  594. };
  595. /* Container for hard regs subnodes of all allocnos. */
  596. static allocno_hard_regs_subnode_t allocno_hard_regs_subnodes;
  597. /* Table (preorder number of allocno hard registers node in the
  598. forest, preorder number of allocno hard registers subnode) -> index
  599. of the subnode relative to the node. -1 if it is not a
  600. subnode. */
  601. static int *allocno_hard_regs_subnode_index;
  602. /* Setup arrays ALLOCNO_HARD_REGS_NODES and
  603. ALLOCNO_HARD_REGS_SUBNODE_INDEX. */
  604. static void
  605. setup_allocno_hard_regs_subnode_index (allocno_hard_regs_node_t first)
  606. {
  607. allocno_hard_regs_node_t node, parent;
  608. int index;
  609. for (node = first; node != NULL; node = node->next)
  610. {
  611. allocno_hard_regs_nodes[node->preorder_num] = node;
  612. for (parent = node; parent != NULL; parent = parent->parent)
  613. {
  614. index = parent->preorder_num * allocno_hard_regs_nodes_num;
  615. allocno_hard_regs_subnode_index[index + node->preorder_num]
  616. = node->preorder_num - parent->preorder_num;
  617. }
  618. setup_allocno_hard_regs_subnode_index (node->first);
  619. }
  620. }
  621. /* Count all allocno hard registers nodes in tree ROOT. */
  622. static int
  623. get_allocno_hard_regs_subnodes_num (allocno_hard_regs_node_t root)
  624. {
  625. int len = 1;
  626. for (root = root->first; root != NULL; root = root->next)
  627. len += get_allocno_hard_regs_subnodes_num (root);
  628. return len;
  629. }
  630. /* Build the forest of allocno hard registers nodes and assign each
  631. allocno a node from the forest. */
  632. static void
  633. form_allocno_hard_regs_nodes_forest (void)
  634. {
  635. unsigned int i, j, size, len;
  636. int start;
  637. ira_allocno_t a;
  638. allocno_hard_regs_t hv;
  639. bitmap_iterator bi;
  640. HARD_REG_SET temp;
  641. allocno_hard_regs_node_t node, allocno_hard_regs_node;
  642. allocno_color_data_t allocno_data;
  643. node_check_tick = 0;
  644. init_allocno_hard_regs ();
  645. hard_regs_roots = NULL;
  646. hard_regs_node_vec.create (100);
  647. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  648. if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
  649. {
  650. CLEAR_HARD_REG_SET (temp);
  651. SET_HARD_REG_BIT (temp, i);
  652. hv = add_allocno_hard_regs (temp, 0);
  653. node = create_new_allocno_hard_regs_node (hv);
  654. add_new_allocno_hard_regs_node_to_forest (&hard_regs_roots, node);
  655. }
  656. start = allocno_hard_regs_vec.length ();
  657. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  658. {
  659. a = ira_allocnos[i];
  660. allocno_data = ALLOCNO_COLOR_DATA (a);
  661. if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
  662. continue;
  663. hv = (add_allocno_hard_regs
  664. (allocno_data->profitable_hard_regs,
  665. ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a)));
  666. }
  667. SET_HARD_REG_SET (temp);
  668. AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
  669. add_allocno_hard_regs (temp, 0);
  670. qsort (allocno_hard_regs_vec.address () + start,
  671. allocno_hard_regs_vec.length () - start,
  672. sizeof (allocno_hard_regs_t), allocno_hard_regs_compare);
  673. for (i = start;
  674. allocno_hard_regs_vec.iterate (i, &hv);
  675. i++)
  676. {
  677. add_allocno_hard_regs_to_forest (&hard_regs_roots, hv);
  678. ira_assert (hard_regs_node_vec.length () == 0);
  679. }
  680. /* We need to set up parent fields for right work of
  681. first_common_ancestor_node. */
  682. setup_allocno_hard_regs_nodes_parent (hard_regs_roots, NULL);
  683. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  684. {
  685. a = ira_allocnos[i];
  686. allocno_data = ALLOCNO_COLOR_DATA (a);
  687. if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
  688. continue;
  689. hard_regs_node_vec.truncate (0);
  690. collect_allocno_hard_regs_cover (hard_regs_roots,
  691. allocno_data->profitable_hard_regs);
  692. allocno_hard_regs_node = NULL;
  693. for (j = 0; hard_regs_node_vec.iterate (j, &node); j++)
  694. allocno_hard_regs_node
  695. = (j == 0
  696. ? node
  697. : first_common_ancestor_node (node, allocno_hard_regs_node));
  698. /* That is a temporary storage. */
  699. allocno_hard_regs_node->used_p = true;
  700. allocno_data->hard_regs_node = allocno_hard_regs_node;
  701. }
  702. ira_assert (hard_regs_roots->next == NULL);
  703. hard_regs_roots->used_p = true;
  704. remove_unused_allocno_hard_regs_nodes (&hard_regs_roots);
  705. allocno_hard_regs_nodes_num
  706. = enumerate_allocno_hard_regs_nodes (hard_regs_roots, NULL, 0);
  707. allocno_hard_regs_nodes
  708. = ((allocno_hard_regs_node_t *)
  709. ira_allocate (allocno_hard_regs_nodes_num
  710. * sizeof (allocno_hard_regs_node_t)));
  711. size = allocno_hard_regs_nodes_num * allocno_hard_regs_nodes_num;
  712. allocno_hard_regs_subnode_index
  713. = (int *) ira_allocate (size * sizeof (int));
  714. for (i = 0; i < size; i++)
  715. allocno_hard_regs_subnode_index[i] = -1;
  716. setup_allocno_hard_regs_subnode_index (hard_regs_roots);
  717. start = 0;
  718. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  719. {
  720. a = ira_allocnos[i];
  721. allocno_data = ALLOCNO_COLOR_DATA (a);
  722. if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
  723. continue;
  724. len = get_allocno_hard_regs_subnodes_num (allocno_data->hard_regs_node);
  725. allocno_data->hard_regs_subnodes_start = start;
  726. allocno_data->hard_regs_subnodes_num = len;
  727. start += len;
  728. }
  729. allocno_hard_regs_subnodes
  730. = ((allocno_hard_regs_subnode_t)
  731. ira_allocate (sizeof (struct allocno_hard_regs_subnode) * start));
  732. hard_regs_node_vec.release ();
  733. }
  734. /* Free tree of allocno hard registers nodes given by its ROOT. */
  735. static void
  736. finish_allocno_hard_regs_nodes_tree (allocno_hard_regs_node_t root)
  737. {
  738. allocno_hard_regs_node_t child, next;
  739. for (child = root->first; child != NULL; child = next)
  740. {
  741. next = child->next;
  742. finish_allocno_hard_regs_nodes_tree (child);
  743. }
  744. ira_free (root);
  745. }
  746. /* Finish work with the forest of allocno hard registers nodes. */
  747. static void
  748. finish_allocno_hard_regs_nodes_forest (void)
  749. {
  750. allocno_hard_regs_node_t node, next;
  751. ira_free (allocno_hard_regs_subnodes);
  752. for (node = hard_regs_roots; node != NULL; node = next)
  753. {
  754. next = node->next;
  755. finish_allocno_hard_regs_nodes_tree (node);
  756. }
  757. ira_free (allocno_hard_regs_nodes);
  758. ira_free (allocno_hard_regs_subnode_index);
  759. finish_allocno_hard_regs ();
  760. }
  761. /* Set up left conflict sizes and left conflict subnodes sizes of hard
  762. registers subnodes of allocno A. Return TRUE if allocno A is
  763. trivially colorable. */
  764. static bool
  765. setup_left_conflict_sizes_p (ira_allocno_t a)
  766. {
  767. int i, k, nobj, start;
  768. int conflict_size, left_conflict_subnodes_size, node_preorder_num;
  769. allocno_color_data_t data;
  770. HARD_REG_SET profitable_hard_regs;
  771. allocno_hard_regs_subnode_t subnodes;
  772. allocno_hard_regs_node_t node;
  773. HARD_REG_SET node_set;
  774. nobj = ALLOCNO_NUM_OBJECTS (a);
  775. data = ALLOCNO_COLOR_DATA (a);
  776. subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
  777. COPY_HARD_REG_SET (profitable_hard_regs, data->profitable_hard_regs);
  778. node = data->hard_regs_node;
  779. node_preorder_num = node->preorder_num;
  780. COPY_HARD_REG_SET (node_set, node->hard_regs->set);
  781. node_check_tick++;
  782. for (k = 0; k < nobj; k++)
  783. {
  784. ira_object_t obj = ALLOCNO_OBJECT (a, k);
  785. ira_object_t conflict_obj;
  786. ira_object_conflict_iterator oci;
  787. FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
  788. {
  789. int size;
  790. ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
  791. allocno_hard_regs_node_t conflict_node, temp_node;
  792. HARD_REG_SET conflict_node_set;
  793. allocno_color_data_t conflict_data;
  794. conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
  795. if (! ALLOCNO_COLOR_DATA (conflict_a)->in_graph_p
  796. || ! hard_reg_set_intersect_p (profitable_hard_regs,
  797. conflict_data
  798. ->profitable_hard_regs))
  799. continue;
  800. conflict_node = conflict_data->hard_regs_node;
  801. COPY_HARD_REG_SET (conflict_node_set, conflict_node->hard_regs->set);
  802. if (hard_reg_set_subset_p (node_set, conflict_node_set))
  803. temp_node = node;
  804. else
  805. {
  806. ira_assert (hard_reg_set_subset_p (conflict_node_set, node_set));
  807. temp_node = conflict_node;
  808. }
  809. if (temp_node->check != node_check_tick)
  810. {
  811. temp_node->check = node_check_tick;
  812. temp_node->conflict_size = 0;
  813. }
  814. size = (ira_reg_class_max_nregs
  815. [ALLOCNO_CLASS (conflict_a)][ALLOCNO_MODE (conflict_a)]);
  816. if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1)
  817. /* We will deal with the subwords individually. */
  818. size = 1;
  819. temp_node->conflict_size += size;
  820. }
  821. }
  822. for (i = 0; i < data->hard_regs_subnodes_num; i++)
  823. {
  824. allocno_hard_regs_node_t temp_node;
  825. temp_node = allocno_hard_regs_nodes[i + node_preorder_num];
  826. ira_assert (temp_node->preorder_num == i + node_preorder_num);
  827. subnodes[i].left_conflict_size = (temp_node->check != node_check_tick
  828. ? 0 : temp_node->conflict_size);
  829. if (hard_reg_set_subset_p (temp_node->hard_regs->set,
  830. profitable_hard_regs))
  831. subnodes[i].max_node_impact = temp_node->hard_regs_num;
  832. else
  833. {
  834. HARD_REG_SET temp_set;
  835. int j, n, hard_regno;
  836. enum reg_class aclass;
  837. COPY_HARD_REG_SET (temp_set, temp_node->hard_regs->set);
  838. AND_HARD_REG_SET (temp_set, profitable_hard_regs);
  839. aclass = ALLOCNO_CLASS (a);
  840. for (n = 0, j = ira_class_hard_regs_num[aclass] - 1; j >= 0; j--)
  841. {
  842. hard_regno = ira_class_hard_regs[aclass][j];
  843. if (TEST_HARD_REG_BIT (temp_set, hard_regno))
  844. n++;
  845. }
  846. subnodes[i].max_node_impact = n;
  847. }
  848. subnodes[i].left_conflict_subnodes_size = 0;
  849. }
  850. start = node_preorder_num * allocno_hard_regs_nodes_num;
  851. for (i = data->hard_regs_subnodes_num - 1; i >= 0; i--)
  852. {
  853. int size, parent_i;
  854. allocno_hard_regs_node_t parent;
  855. size = (subnodes[i].left_conflict_subnodes_size
  856. + MIN (subnodes[i].max_node_impact
  857. - subnodes[i].left_conflict_subnodes_size,
  858. subnodes[i].left_conflict_size));
  859. parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
  860. if (parent == NULL)
  861. continue;
  862. parent_i
  863. = allocno_hard_regs_subnode_index[start + parent->preorder_num];
  864. if (parent_i < 0)
  865. continue;
  866. subnodes[parent_i].left_conflict_subnodes_size += size;
  867. }
  868. left_conflict_subnodes_size = subnodes[0].left_conflict_subnodes_size;
  869. conflict_size
  870. = (left_conflict_subnodes_size
  871. + MIN (subnodes[0].max_node_impact - left_conflict_subnodes_size,
  872. subnodes[0].left_conflict_size));
  873. conflict_size += ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
  874. data->colorable_p = conflict_size <= data->available_regs_num;
  875. return data->colorable_p;
  876. }
  877. /* Update left conflict sizes of hard registers subnodes of allocno A
  878. after removing allocno REMOVED_A with SIZE from the conflict graph.
  879. Return TRUE if A is trivially colorable. */
  880. static bool
  881. update_left_conflict_sizes_p (ira_allocno_t a,
  882. ira_allocno_t removed_a, int size)
  883. {
  884. int i, conflict_size, before_conflict_size, diff, start;
  885. int node_preorder_num, parent_i;
  886. allocno_hard_regs_node_t node, removed_node, parent;
  887. allocno_hard_regs_subnode_t subnodes;
  888. allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
  889. ira_assert (! data->colorable_p);
  890. node = data->hard_regs_node;
  891. node_preorder_num = node->preorder_num;
  892. removed_node = ALLOCNO_COLOR_DATA (removed_a)->hard_regs_node;
  893. ira_assert (hard_reg_set_subset_p (removed_node->hard_regs->set,
  894. node->hard_regs->set)
  895. || hard_reg_set_subset_p (node->hard_regs->set,
  896. removed_node->hard_regs->set));
  897. start = node_preorder_num * allocno_hard_regs_nodes_num;
  898. i = allocno_hard_regs_subnode_index[start + removed_node->preorder_num];
  899. if (i < 0)
  900. i = 0;
  901. subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
  902. before_conflict_size
  903. = (subnodes[i].left_conflict_subnodes_size
  904. + MIN (subnodes[i].max_node_impact
  905. - subnodes[i].left_conflict_subnodes_size,
  906. subnodes[i].left_conflict_size));
  907. subnodes[i].left_conflict_size -= size;
  908. for (;;)
  909. {
  910. conflict_size
  911. = (subnodes[i].left_conflict_subnodes_size
  912. + MIN (subnodes[i].max_node_impact
  913. - subnodes[i].left_conflict_subnodes_size,
  914. subnodes[i].left_conflict_size));
  915. if ((diff = before_conflict_size - conflict_size) == 0)
  916. break;
  917. ira_assert (conflict_size < before_conflict_size);
  918. parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
  919. if (parent == NULL)
  920. break;
  921. parent_i
  922. = allocno_hard_regs_subnode_index[start + parent->preorder_num];
  923. if (parent_i < 0)
  924. break;
  925. i = parent_i;
  926. before_conflict_size
  927. = (subnodes[i].left_conflict_subnodes_size
  928. + MIN (subnodes[i].max_node_impact
  929. - subnodes[i].left_conflict_subnodes_size,
  930. subnodes[i].left_conflict_size));
  931. subnodes[i].left_conflict_subnodes_size -= diff;
  932. }
  933. if (i != 0
  934. || (conflict_size
  935. + ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
  936. > data->available_regs_num))
  937. return false;
  938. data->colorable_p = true;
  939. return true;
  940. }
  941. /* Return true if allocno A has empty profitable hard regs. */
  942. static bool
  943. empty_profitable_hard_regs (ira_allocno_t a)
  944. {
  945. allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
  946. return hard_reg_set_empty_p (data->profitable_hard_regs);
  947. }
  948. /* Set up profitable hard registers for each allocno being
  949. colored. */
  950. static void
  951. setup_profitable_hard_regs (void)
  952. {
  953. unsigned int i;
  954. int j, k, nobj, hard_regno, nregs, class_size;
  955. ira_allocno_t a;
  956. bitmap_iterator bi;
  957. enum reg_class aclass;
  958. machine_mode mode;
  959. allocno_color_data_t data;
  960. /* Initial set up from allocno classes and explicitly conflicting
  961. hard regs. */
  962. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  963. {
  964. a = ira_allocnos[i];
  965. if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS)
  966. continue;
  967. data = ALLOCNO_COLOR_DATA (a);
  968. if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL
  969. && ALLOCNO_CLASS_COST (a) > ALLOCNO_MEMORY_COST (a))
  970. CLEAR_HARD_REG_SET (data->profitable_hard_regs);
  971. else
  972. {
  973. mode = ALLOCNO_MODE (a);
  974. COPY_HARD_REG_SET (data->profitable_hard_regs,
  975. ira_useful_class_mode_regs[aclass][mode]);
  976. nobj = ALLOCNO_NUM_OBJECTS (a);
  977. for (k = 0; k < nobj; k++)
  978. {
  979. ira_object_t obj = ALLOCNO_OBJECT (a, k);
  980. AND_COMPL_HARD_REG_SET (data->profitable_hard_regs,
  981. OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
  982. }
  983. }
  984. }
  985. /* Exclude hard regs already assigned for conflicting objects. */
  986. EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, i, bi)
  987. {
  988. a = ira_allocnos[i];
  989. if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
  990. || ! ALLOCNO_ASSIGNED_P (a)
  991. || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
  992. continue;
  993. mode = ALLOCNO_MODE (a);
  994. nregs = hard_regno_nregs[hard_regno][mode];
  995. nobj = ALLOCNO_NUM_OBJECTS (a);
  996. for (k = 0; k < nobj; k++)
  997. {
  998. ira_object_t obj = ALLOCNO_OBJECT (a, k);
  999. ira_object_t conflict_obj;
  1000. ira_object_conflict_iterator oci;
  1001. FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
  1002. {
  1003. ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
  1004. /* We can process the conflict allocno repeatedly with
  1005. the same result. */
  1006. if (nregs == nobj && nregs > 1)
  1007. {
  1008. int num = OBJECT_SUBWORD (conflict_obj);
  1009. if (REG_WORDS_BIG_ENDIAN)
  1010. CLEAR_HARD_REG_BIT
  1011. (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
  1012. hard_regno + nobj - num - 1);
  1013. else
  1014. CLEAR_HARD_REG_BIT
  1015. (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
  1016. hard_regno + num);
  1017. }
  1018. else
  1019. AND_COMPL_HARD_REG_SET
  1020. (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
  1021. ira_reg_mode_hard_regset[hard_regno][mode]);
  1022. }
  1023. }
  1024. }
  1025. /* Exclude too costly hard regs. */
  1026. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  1027. {
  1028. int min_cost = INT_MAX;
  1029. int *costs;
  1030. a = ira_allocnos[i];
  1031. if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
  1032. || empty_profitable_hard_regs (a))
  1033. continue;
  1034. data = ALLOCNO_COLOR_DATA (a);
  1035. mode = ALLOCNO_MODE (a);
  1036. if ((costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a)) != NULL
  1037. || (costs = ALLOCNO_HARD_REG_COSTS (a)) != NULL)
  1038. {
  1039. class_size = ira_class_hard_regs_num[aclass];
  1040. for (j = 0; j < class_size; j++)
  1041. {
  1042. hard_regno = ira_class_hard_regs[aclass][j];
  1043. if (! TEST_HARD_REG_BIT (data->profitable_hard_regs,
  1044. hard_regno))
  1045. continue;
  1046. if (ALLOCNO_UPDATED_MEMORY_COST (a) < costs[j])
  1047. CLEAR_HARD_REG_BIT (data->profitable_hard_regs,
  1048. hard_regno);
  1049. else if (min_cost > costs[j])
  1050. min_cost = costs[j];
  1051. }
  1052. }
  1053. else if (ALLOCNO_UPDATED_MEMORY_COST (a)
  1054. < ALLOCNO_UPDATED_CLASS_COST (a))
  1055. CLEAR_HARD_REG_SET (data->profitable_hard_regs);
  1056. if (ALLOCNO_UPDATED_CLASS_COST (a) > min_cost)
  1057. ALLOCNO_UPDATED_CLASS_COST (a) = min_cost;
  1058. }
  1059. }
  1060. /* This page contains functions used to choose hard registers for
  1061. allocnos. */
  1062. /* Pool for update cost records. */
  1063. static alloc_pool update_cost_record_pool;
  1064. /* Initiate update cost records. */
  1065. static void
  1066. init_update_cost_records (void)
  1067. {
  1068. update_cost_record_pool
  1069. = create_alloc_pool ("update cost records",
  1070. sizeof (struct update_cost_record), 100);
  1071. }
  1072. /* Return new update cost record with given params. */
  1073. static struct update_cost_record *
  1074. get_update_cost_record (int hard_regno, int divisor,
  1075. struct update_cost_record *next)
  1076. {
  1077. struct update_cost_record *record;
  1078. record = (struct update_cost_record *) pool_alloc (update_cost_record_pool);
  1079. record->hard_regno = hard_regno;
  1080. record->divisor = divisor;
  1081. record->next = next;
  1082. return record;
  1083. }
  1084. /* Free memory for all records in LIST. */
  1085. static void
  1086. free_update_cost_record_list (struct update_cost_record *list)
  1087. {
  1088. struct update_cost_record *next;
  1089. while (list != NULL)
  1090. {
  1091. next = list->next;
  1092. pool_free (update_cost_record_pool, list);
  1093. list = next;
  1094. }
  1095. }
  1096. /* Free memory allocated for all update cost records. */
  1097. static void
  1098. finish_update_cost_records (void)
  1099. {
  1100. free_alloc_pool (update_cost_record_pool);
  1101. }
  1102. /* Array whose element value is TRUE if the corresponding hard
  1103. register was already allocated for an allocno. */
  1104. static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
  1105. /* Describes one element in a queue of allocnos whose costs need to be
  1106. updated. Each allocno in the queue is known to have an allocno
  1107. class. */
  1108. struct update_cost_queue_elem
  1109. {
  1110. /* This element is in the queue iff CHECK == update_cost_check. */
  1111. int check;
  1112. /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
  1113. connecting this allocno to the one being allocated. */
  1114. int divisor;
  1115. /* Allocno from which we are chaining costs of connected allocnos.
  1116. It is used not go back in graph of allocnos connected by
  1117. copies. */
  1118. ira_allocno_t from;
  1119. /* The next allocno in the queue, or null if this is the last element. */
  1120. ira_allocno_t next;
  1121. };
  1122. /* The first element in a queue of allocnos whose copy costs need to be
  1123. updated. Null if the queue is empty. */
  1124. static ira_allocno_t update_cost_queue;
  1125. /* The last element in the queue described by update_cost_queue.
  1126. Not valid if update_cost_queue is null. */
  1127. static struct update_cost_queue_elem *update_cost_queue_tail;
  1128. /* A pool of elements in the queue described by update_cost_queue.
  1129. Elements are indexed by ALLOCNO_NUM. */
  1130. static struct update_cost_queue_elem *update_cost_queue_elems;
  1131. /* The current value of update_costs_from_copies call count. */
  1132. static int update_cost_check;
  1133. /* Allocate and initialize data necessary for function
  1134. update_costs_from_copies. */
  1135. static void
  1136. initiate_cost_update (void)
  1137. {
  1138. size_t size;
  1139. size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
  1140. update_cost_queue_elems
  1141. = (struct update_cost_queue_elem *) ira_allocate (size);
  1142. memset (update_cost_queue_elems, 0, size);
  1143. update_cost_check = 0;
  1144. init_update_cost_records ();
  1145. }
  1146. /* Deallocate data used by function update_costs_from_copies. */
  1147. static void
  1148. finish_cost_update (void)
  1149. {
  1150. ira_free (update_cost_queue_elems);
  1151. finish_update_cost_records ();
  1152. }
  1153. /* When we traverse allocnos to update hard register costs, the cost
  1154. divisor will be multiplied by the following macro value for each
  1155. hop from given allocno to directly connected allocnos. */
  1156. #define COST_HOP_DIVISOR 4
  1157. /* Start a new cost-updating pass. */
  1158. static void
  1159. start_update_cost (void)
  1160. {
  1161. update_cost_check++;
  1162. update_cost_queue = NULL;
  1163. }
  1164. /* Add (ALLOCNO, FROM, DIVISOR) to the end of update_cost_queue, unless
  1165. ALLOCNO is already in the queue, or has NO_REGS class. */
  1166. static inline void
  1167. queue_update_cost (ira_allocno_t allocno, ira_allocno_t from, int divisor)
  1168. {
  1169. struct update_cost_queue_elem *elem;
  1170. elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
  1171. if (elem->check != update_cost_check
  1172. && ALLOCNO_CLASS (allocno) != NO_REGS)
  1173. {
  1174. elem->check = update_cost_check;
  1175. elem->from = from;
  1176. elem->divisor = divisor;
  1177. elem->next = NULL;
  1178. if (update_cost_queue == NULL)
  1179. update_cost_queue = allocno;
  1180. else
  1181. update_cost_queue_tail->next = allocno;
  1182. update_cost_queue_tail = elem;
  1183. }
  1184. }
  1185. /* Try to remove the first element from update_cost_queue. Return
  1186. false if the queue was empty, otherwise make (*ALLOCNO, *FROM,
  1187. *DIVISOR) describe the removed element. */
  1188. static inline bool
  1189. get_next_update_cost (ira_allocno_t *allocno, ira_allocno_t *from, int *divisor)
  1190. {
  1191. struct update_cost_queue_elem *elem;
  1192. if (update_cost_queue == NULL)
  1193. return false;
  1194. *allocno = update_cost_queue;
  1195. elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
  1196. *from = elem->from;
  1197. *divisor = elem->divisor;
  1198. update_cost_queue = elem->next;
  1199. return true;
  1200. }
  1201. /* Increase costs of HARD_REGNO by UPDATE_COST for ALLOCNO. Return
  1202. true if we really modified the cost. */
  1203. static bool
  1204. update_allocno_cost (ira_allocno_t allocno, int hard_regno, int update_cost)
  1205. {
  1206. int i;
  1207. enum reg_class aclass = ALLOCNO_CLASS (allocno);
  1208. i = ira_class_hard_reg_index[aclass][hard_regno];
  1209. if (i < 0)
  1210. return false;
  1211. ira_allocate_and_set_or_copy_costs
  1212. (&ALLOCNO_UPDATED_HARD_REG_COSTS (allocno), aclass,
  1213. ALLOCNO_UPDATED_CLASS_COST (allocno),
  1214. ALLOCNO_HARD_REG_COSTS (allocno));
  1215. ira_allocate_and_set_or_copy_costs
  1216. (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno),
  1217. aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (allocno));
  1218. ALLOCNO_UPDATED_HARD_REG_COSTS (allocno)[i] += update_cost;
  1219. ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno)[i] += update_cost;
  1220. return true;
  1221. }
  1222. /* Update (decrease if DECR_P) HARD_REGNO cost of allocnos connected
  1223. by copies to ALLOCNO to increase chances to remove some copies as
  1224. the result of subsequent assignment. Record cost updates if
  1225. RECORD_P is true. */
  1226. static void
  1227. update_costs_from_allocno (ira_allocno_t allocno, int hard_regno,
  1228. int divisor, bool decr_p, bool record_p)
  1229. {
  1230. int cost, update_cost;
  1231. machine_mode mode;
  1232. enum reg_class rclass, aclass;
  1233. ira_allocno_t another_allocno, from = NULL;
  1234. ira_copy_t cp, next_cp;
  1235. rclass = REGNO_REG_CLASS (hard_regno);
  1236. do
  1237. {
  1238. mode = ALLOCNO_MODE (allocno);
  1239. ira_init_register_move_cost_if_necessary (mode);
  1240. for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
  1241. {
  1242. if (cp->first == allocno)
  1243. {
  1244. next_cp = cp->next_first_allocno_copy;
  1245. another_allocno = cp->second;
  1246. }
  1247. else if (cp->second == allocno)
  1248. {
  1249. next_cp = cp->next_second_allocno_copy;
  1250. another_allocno = cp->first;
  1251. }
  1252. else
  1253. gcc_unreachable ();
  1254. if (another_allocno == from)
  1255. continue;
  1256. aclass = ALLOCNO_CLASS (another_allocno);
  1257. if (! TEST_HARD_REG_BIT (reg_class_contents[aclass],
  1258. hard_regno)
  1259. || ALLOCNO_ASSIGNED_P (another_allocno))
  1260. continue;
  1261. cost = (cp->second == allocno
  1262. ? ira_register_move_cost[mode][rclass][aclass]
  1263. : ira_register_move_cost[mode][aclass][rclass]);
  1264. if (decr_p)
  1265. cost = -cost;
  1266. update_cost = cp->freq * cost / divisor;
  1267. if (update_cost == 0)
  1268. continue;
  1269. if (! update_allocno_cost (another_allocno, hard_regno, update_cost))
  1270. continue;
  1271. queue_update_cost (another_allocno, allocno, divisor * COST_HOP_DIVISOR);
  1272. if (record_p && ALLOCNO_COLOR_DATA (another_allocno) != NULL)
  1273. ALLOCNO_COLOR_DATA (another_allocno)->update_cost_records
  1274. = get_update_cost_record (hard_regno, divisor,
  1275. ALLOCNO_COLOR_DATA (another_allocno)
  1276. ->update_cost_records);
  1277. }
  1278. }
  1279. while (get_next_update_cost (&allocno, &from, &divisor));
  1280. }
  1281. /* Decrease preferred ALLOCNO hard register costs and costs of
  1282. allocnos connected to ALLOCNO through copy. */
  1283. static void
  1284. update_costs_from_prefs (ira_allocno_t allocno)
  1285. {
  1286. ira_pref_t pref;
  1287. start_update_cost ();
  1288. for (pref = ALLOCNO_PREFS (allocno); pref != NULL; pref = pref->next_pref)
  1289. update_costs_from_allocno (allocno, pref->hard_regno,
  1290. COST_HOP_DIVISOR, true, true);
  1291. }
  1292. /* Update (decrease if DECR_P) the cost of allocnos connected to
  1293. ALLOCNO through copies to increase chances to remove some copies as
  1294. the result of subsequent assignment. ALLOCNO was just assigned to
  1295. a hard register. Record cost updates if RECORD_P is true. */
  1296. static void
  1297. update_costs_from_copies (ira_allocno_t allocno, bool decr_p, bool record_p)
  1298. {
  1299. int hard_regno;
  1300. hard_regno = ALLOCNO_HARD_REGNO (allocno);
  1301. ira_assert (hard_regno >= 0 && ALLOCNO_CLASS (allocno) != NO_REGS);
  1302. start_update_cost ();
  1303. update_costs_from_allocno (allocno, hard_regno, 1, decr_p, record_p);
  1304. }
  1305. /* Restore costs of allocnos connected to ALLOCNO by copies as it was
  1306. before updating costs of these allocnos from given allocno. This
  1307. is a wise thing to do as if given allocno did not get an expected
  1308. hard reg, using smaller cost of the hard reg for allocnos connected
  1309. by copies to given allocno becomes actually misleading. Free all
  1310. update cost records for ALLOCNO as we don't need them anymore. */
  1311. static void
  1312. restore_costs_from_copies (ira_allocno_t allocno)
  1313. {
  1314. struct update_cost_record *records, *curr;
  1315. if (ALLOCNO_COLOR_DATA (allocno) == NULL)
  1316. return;
  1317. records = ALLOCNO_COLOR_DATA (allocno)->update_cost_records;
  1318. start_update_cost ();
  1319. for (curr = records; curr != NULL; curr = curr->next)
  1320. update_costs_from_allocno (allocno, curr->hard_regno,
  1321. curr->divisor, true, false);
  1322. free_update_cost_record_list (records);
  1323. ALLOCNO_COLOR_DATA (allocno)->update_cost_records = NULL;
  1324. }
  1325. /* This function updates COSTS (decrease if DECR_P) for hard_registers
  1326. of ACLASS by conflict costs of the unassigned allocnos
  1327. connected by copies with allocnos in update_cost_queue. This
  1328. update increases chances to remove some copies. */
  1329. static void
  1330. update_conflict_hard_regno_costs (int *costs, enum reg_class aclass,
  1331. bool decr_p)
  1332. {
  1333. int i, cost, class_size, freq, mult, div, divisor;
  1334. int index, hard_regno;
  1335. int *conflict_costs;
  1336. bool cont_p;
  1337. enum reg_class another_aclass;
  1338. ira_allocno_t allocno, another_allocno, from;
  1339. ira_copy_t cp, next_cp;
  1340. while (get_next_update_cost (&allocno, &from, &divisor))
  1341. for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
  1342. {
  1343. if (cp->first == allocno)
  1344. {
  1345. next_cp = cp->next_first_allocno_copy;
  1346. another_allocno = cp->second;
  1347. }
  1348. else if (cp->second == allocno)
  1349. {
  1350. next_cp = cp->next_second_allocno_copy;
  1351. another_allocno = cp->first;
  1352. }
  1353. else
  1354. gcc_unreachable ();
  1355. if (another_allocno == from)
  1356. continue;
  1357. another_aclass = ALLOCNO_CLASS (another_allocno);
  1358. if (! ira_reg_classes_intersect_p[aclass][another_aclass]
  1359. || ALLOCNO_ASSIGNED_P (another_allocno)
  1360. || ALLOCNO_COLOR_DATA (another_allocno)->may_be_spilled_p)
  1361. continue;
  1362. class_size = ira_class_hard_regs_num[another_aclass];
  1363. ira_allocate_and_copy_costs
  1364. (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
  1365. another_aclass, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
  1366. conflict_costs
  1367. = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
  1368. if (conflict_costs == NULL)
  1369. cont_p = true;
  1370. else
  1371. {
  1372. mult = cp->freq;
  1373. freq = ALLOCNO_FREQ (another_allocno);
  1374. if (freq == 0)
  1375. freq = 1;
  1376. div = freq * divisor;
  1377. cont_p = false;
  1378. for (i = class_size - 1; i >= 0; i--)
  1379. {
  1380. hard_regno = ira_class_hard_regs[another_aclass][i];
  1381. ira_assert (hard_regno >= 0);
  1382. index = ira_class_hard_reg_index[aclass][hard_regno];
  1383. if (index < 0)
  1384. continue;
  1385. cost = (int) ((unsigned) conflict_costs [i] * mult) / div;
  1386. if (cost == 0)
  1387. continue;
  1388. cont_p = true;
  1389. if (decr_p)
  1390. cost = -cost;
  1391. costs[index] += cost;
  1392. }
  1393. }
  1394. /* Probably 5 hops will be enough. */
  1395. if (cont_p
  1396. && divisor <= (COST_HOP_DIVISOR
  1397. * COST_HOP_DIVISOR
  1398. * COST_HOP_DIVISOR
  1399. * COST_HOP_DIVISOR))
  1400. queue_update_cost (another_allocno, allocno, divisor * COST_HOP_DIVISOR);
  1401. }
  1402. }
  1403. /* Set up conflicting (through CONFLICT_REGS) for each object of
  1404. allocno A and the start allocno profitable regs (through
  1405. START_PROFITABLE_REGS). Remember that the start profitable regs
  1406. exclude hard regs which can not hold value of mode of allocno A.
  1407. This covers mostly cases when multi-register value should be
  1408. aligned. */
  1409. static inline void
  1410. get_conflict_and_start_profitable_regs (ira_allocno_t a, bool retry_p,
  1411. HARD_REG_SET *conflict_regs,
  1412. HARD_REG_SET *start_profitable_regs)
  1413. {
  1414. int i, nwords;
  1415. ira_object_t obj;
  1416. nwords = ALLOCNO_NUM_OBJECTS (a);
  1417. for (i = 0; i < nwords; i++)
  1418. {
  1419. obj = ALLOCNO_OBJECT (a, i);
  1420. COPY_HARD_REG_SET (conflict_regs[i],
  1421. OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
  1422. }
  1423. if (retry_p)
  1424. {
  1425. COPY_HARD_REG_SET (*start_profitable_regs,
  1426. reg_class_contents[ALLOCNO_CLASS (a)]);
  1427. AND_COMPL_HARD_REG_SET (*start_profitable_regs,
  1428. ira_prohibited_class_mode_regs
  1429. [ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
  1430. }
  1431. else
  1432. COPY_HARD_REG_SET (*start_profitable_regs,
  1433. ALLOCNO_COLOR_DATA (a)->profitable_hard_regs);
  1434. }
  1435. /* Return true if HARD_REGNO is ok for assigning to allocno A with
  1436. PROFITABLE_REGS and whose objects have CONFLICT_REGS. */
  1437. static inline bool
  1438. check_hard_reg_p (ira_allocno_t a, int hard_regno,
  1439. HARD_REG_SET *conflict_regs, HARD_REG_SET profitable_regs)
  1440. {
  1441. int j, nwords, nregs;
  1442. enum reg_class aclass;
  1443. machine_mode mode;
  1444. aclass = ALLOCNO_CLASS (a);
  1445. mode = ALLOCNO_MODE (a);
  1446. if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[aclass][mode],
  1447. hard_regno))
  1448. return false;
  1449. /* Checking only profitable hard regs. */
  1450. if (! TEST_HARD_REG_BIT (profitable_regs, hard_regno))
  1451. return false;
  1452. nregs = hard_regno_nregs[hard_regno][mode];
  1453. nwords = ALLOCNO_NUM_OBJECTS (a);
  1454. for (j = 0; j < nregs; j++)
  1455. {
  1456. int k;
  1457. int set_to_test_start = 0, set_to_test_end = nwords;
  1458. if (nregs == nwords)
  1459. {
  1460. if (REG_WORDS_BIG_ENDIAN)
  1461. set_to_test_start = nwords - j - 1;
  1462. else
  1463. set_to_test_start = j;
  1464. set_to_test_end = set_to_test_start + 1;
  1465. }
  1466. for (k = set_to_test_start; k < set_to_test_end; k++)
  1467. if (TEST_HARD_REG_BIT (conflict_regs[k], hard_regno + j))
  1468. break;
  1469. if (k != set_to_test_end)
  1470. break;
  1471. }
  1472. return j == nregs;
  1473. }
  1474. /* Return number of registers needed to be saved and restored at
  1475. function prologue/epilogue if we allocate HARD_REGNO to hold value
  1476. of MODE. */
  1477. static int
  1478. calculate_saved_nregs (int hard_regno, machine_mode mode)
  1479. {
  1480. int i;
  1481. int nregs = 0;
  1482. ira_assert (hard_regno >= 0);
  1483. for (i = hard_regno_nregs[hard_regno][mode] - 1; i >= 0; i--)
  1484. if (!allocated_hardreg_p[hard_regno + i]
  1485. && !TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + i)
  1486. && !LOCAL_REGNO (hard_regno + i))
  1487. nregs++;
  1488. return nregs;
  1489. }
  1490. /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
  1491. that the function called from function
  1492. `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
  1493. this case some allocno data are not defined or updated and we
  1494. should not touch these data. The function returns true if we
  1495. managed to assign a hard register to the allocno.
  1496. To assign a hard register, first of all we calculate all conflict
  1497. hard registers which can come from conflicting allocnos with
  1498. already assigned hard registers. After that we find first free
  1499. hard register with the minimal cost. During hard register cost
  1500. calculation we take conflict hard register costs into account to
  1501. give a chance for conflicting allocnos to get a better hard
  1502. register in the future.
  1503. If the best hard register cost is bigger than cost of memory usage
  1504. for the allocno, we don't assign a hard register to given allocno
  1505. at all.
  1506. If we assign a hard register to the allocno, we update costs of the
  1507. hard register for allocnos connected by copies to improve a chance
  1508. to coalesce insns represented by the copies when we assign hard
  1509. registers to the allocnos connected by the copies. */
  1510. static bool
  1511. assign_hard_reg (ira_allocno_t a, bool retry_p)
  1512. {
  1513. HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
  1514. int i, j, hard_regno, best_hard_regno, class_size;
  1515. int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords, word;
  1516. int *a_costs;
  1517. enum reg_class aclass;
  1518. machine_mode mode;
  1519. static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
  1520. int saved_nregs;
  1521. enum reg_class rclass;
  1522. int add_cost;
  1523. #ifdef STACK_REGS
  1524. bool no_stack_reg_p;
  1525. #endif
  1526. ira_assert (! ALLOCNO_ASSIGNED_P (a));
  1527. get_conflict_and_start_profitable_regs (a, retry_p,
  1528. conflicting_regs,
  1529. &profitable_hard_regs);
  1530. aclass = ALLOCNO_CLASS (a);
  1531. class_size = ira_class_hard_regs_num[aclass];
  1532. best_hard_regno = -1;
  1533. memset (full_costs, 0, sizeof (int) * class_size);
  1534. mem_cost = 0;
  1535. memset (costs, 0, sizeof (int) * class_size);
  1536. memset (full_costs, 0, sizeof (int) * class_size);
  1537. #ifdef STACK_REGS
  1538. no_stack_reg_p = false;
  1539. #endif
  1540. if (! retry_p)
  1541. start_update_cost ();
  1542. mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
  1543. ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
  1544. aclass, ALLOCNO_HARD_REG_COSTS (a));
  1545. a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
  1546. #ifdef STACK_REGS
  1547. no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
  1548. #endif
  1549. cost = ALLOCNO_UPDATED_CLASS_COST (a);
  1550. for (i = 0; i < class_size; i++)
  1551. if (a_costs != NULL)
  1552. {
  1553. costs[i] += a_costs[i];
  1554. full_costs[i] += a_costs[i];
  1555. }
  1556. else
  1557. {
  1558. costs[i] += cost;
  1559. full_costs[i] += cost;
  1560. }
  1561. nwords = ALLOCNO_NUM_OBJECTS (a);
  1562. curr_allocno_process++;
  1563. for (word = 0; word < nwords; word++)
  1564. {
  1565. ira_object_t conflict_obj;
  1566. ira_object_t obj = ALLOCNO_OBJECT (a, word);
  1567. ira_object_conflict_iterator oci;
  1568. /* Take preferences of conflicting allocnos into account. */
  1569. FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
  1570. {
  1571. ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
  1572. enum reg_class conflict_aclass;
  1573. allocno_color_data_t data = ALLOCNO_COLOR_DATA (conflict_a);
  1574. /* Reload can give another class so we need to check all
  1575. allocnos. */
  1576. if (!retry_p
  1577. && (!bitmap_bit_p (consideration_allocno_bitmap,
  1578. ALLOCNO_NUM (conflict_a))
  1579. || ((!ALLOCNO_ASSIGNED_P (conflict_a)
  1580. || ALLOCNO_HARD_REGNO (conflict_a) < 0)
  1581. && !(hard_reg_set_intersect_p
  1582. (profitable_hard_regs,
  1583. ALLOCNO_COLOR_DATA
  1584. (conflict_a)->profitable_hard_regs)))))
  1585. continue;
  1586. conflict_aclass = ALLOCNO_CLASS (conflict_a);
  1587. ira_assert (ira_reg_classes_intersect_p
  1588. [aclass][conflict_aclass]);
  1589. if (ALLOCNO_ASSIGNED_P (conflict_a))
  1590. {
  1591. hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
  1592. if (hard_regno >= 0
  1593. && (ira_hard_reg_set_intersection_p
  1594. (hard_regno, ALLOCNO_MODE (conflict_a),
  1595. reg_class_contents[aclass])))
  1596. {
  1597. int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
  1598. int conflict_nregs;
  1599. mode = ALLOCNO_MODE (conflict_a);
  1600. conflict_nregs = hard_regno_nregs[hard_regno][mode];
  1601. if (conflict_nregs == n_objects && conflict_nregs > 1)
  1602. {
  1603. int num = OBJECT_SUBWORD (conflict_obj);
  1604. if (REG_WORDS_BIG_ENDIAN)
  1605. SET_HARD_REG_BIT (conflicting_regs[word],
  1606. hard_regno + n_objects - num - 1);
  1607. else
  1608. SET_HARD_REG_BIT (conflicting_regs[word],
  1609. hard_regno + num);
  1610. }
  1611. else
  1612. IOR_HARD_REG_SET
  1613. (conflicting_regs[word],
  1614. ira_reg_mode_hard_regset[hard_regno][mode]);
  1615. if (hard_reg_set_subset_p (profitable_hard_regs,
  1616. conflicting_regs[word]))
  1617. goto fail;
  1618. }
  1619. }
  1620. else if (! retry_p
  1621. && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p
  1622. /* Don't process the conflict allocno twice. */
  1623. && (ALLOCNO_COLOR_DATA (conflict_a)->last_process
  1624. != curr_allocno_process))
  1625. {
  1626. int k, *conflict_costs;
  1627. ALLOCNO_COLOR_DATA (conflict_a)->last_process
  1628. = curr_allocno_process;
  1629. ira_allocate_and_copy_costs
  1630. (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
  1631. conflict_aclass,
  1632. ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
  1633. conflict_costs
  1634. = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
  1635. if (conflict_costs != NULL)
  1636. for (j = class_size - 1; j >= 0; j--)
  1637. {
  1638. hard_regno = ira_class_hard_regs[aclass][j];
  1639. ira_assert (hard_regno >= 0);
  1640. k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
  1641. if (k < 0
  1642. /* If HARD_REGNO is not available for CONFLICT_A,
  1643. the conflict would be ignored, since HARD_REGNO
  1644. will never be assigned to CONFLICT_A. */
  1645. || !TEST_HARD_REG_BIT (data->profitable_hard_regs,
  1646. hard_regno))
  1647. continue;
  1648. full_costs[j] -= conflict_costs[k];
  1649. }
  1650. queue_update_cost (conflict_a, NULL, COST_HOP_DIVISOR);
  1651. }
  1652. }
  1653. }
  1654. if (! retry_p)
  1655. /* Take into account preferences of allocnos connected by copies to
  1656. the conflict allocnos. */
  1657. update_conflict_hard_regno_costs (full_costs, aclass, true);
  1658. /* Take preferences of allocnos connected by copies into
  1659. account. */
  1660. if (! retry_p)
  1661. {
  1662. start_update_cost ();
  1663. queue_update_cost (a, NULL, COST_HOP_DIVISOR);
  1664. update_conflict_hard_regno_costs (full_costs, aclass, false);
  1665. }
  1666. min_cost = min_full_cost = INT_MAX;
  1667. /* We don't care about giving callee saved registers to allocnos no
  1668. living through calls because call clobbered registers are
  1669. allocated first (it is usual practice to put them first in
  1670. REG_ALLOC_ORDER). */
  1671. mode = ALLOCNO_MODE (a);
  1672. for (i = 0; i < class_size; i++)
  1673. {
  1674. hard_regno = ira_class_hard_regs[aclass][i];
  1675. #ifdef STACK_REGS
  1676. if (no_stack_reg_p
  1677. && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
  1678. continue;
  1679. #endif
  1680. if (! check_hard_reg_p (a, hard_regno,
  1681. conflicting_regs, profitable_hard_regs))
  1682. continue;
  1683. cost = costs[i];
  1684. full_cost = full_costs[i];
  1685. if (!HONOR_REG_ALLOC_ORDER)
  1686. {
  1687. if ((saved_nregs = calculate_saved_nregs (hard_regno, mode)) != 0)
  1688. /* We need to save/restore the hard register in
  1689. epilogue/prologue. Therefore we increase the cost. */
  1690. {
  1691. rclass = REGNO_REG_CLASS (hard_regno);
  1692. add_cost = ((ira_memory_move_cost[mode][rclass][0]
  1693. + ira_memory_move_cost[mode][rclass][1])
  1694. * saved_nregs / hard_regno_nregs[hard_regno][mode] - 1);
  1695. cost += add_cost;
  1696. full_cost += add_cost;
  1697. }
  1698. }
  1699. if (min_cost > cost)
  1700. min_cost = cost;
  1701. if (min_full_cost > full_cost)
  1702. {
  1703. min_full_cost = full_cost;
  1704. best_hard_regno = hard_regno;
  1705. ira_assert (hard_regno >= 0);
  1706. }
  1707. }
  1708. if (min_full_cost > mem_cost)
  1709. {
  1710. if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  1711. fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
  1712. mem_cost, min_full_cost);
  1713. best_hard_regno = -1;
  1714. }
  1715. fail:
  1716. if (best_hard_regno >= 0)
  1717. {
  1718. for (i = hard_regno_nregs[best_hard_regno][mode] - 1; i >= 0; i--)
  1719. allocated_hardreg_p[best_hard_regno + i] = true;
  1720. }
  1721. if (! retry_p)
  1722. restore_costs_from_copies (a);
  1723. ALLOCNO_HARD_REGNO (a) = best_hard_regno;
  1724. ALLOCNO_ASSIGNED_P (a) = true;
  1725. if (best_hard_regno >= 0)
  1726. update_costs_from_copies (a, true, ! retry_p);
  1727. ira_assert (ALLOCNO_CLASS (a) == aclass);
  1728. /* We don't need updated costs anymore. */
  1729. ira_free_allocno_updated_costs (a);
  1730. return best_hard_regno >= 0;
  1731. }
  1732. /* An array used to sort copies. */
  1733. static ira_copy_t *sorted_copies;
  1734. /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
  1735. used to find a conflict for new allocnos or allocnos with the
  1736. different allocno classes. */
  1737. static bool
  1738. allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
  1739. {
  1740. rtx reg1, reg2;
  1741. int i, j;
  1742. int n1 = ALLOCNO_NUM_OBJECTS (a1);
  1743. int n2 = ALLOCNO_NUM_OBJECTS (a2);
  1744. if (a1 == a2)
  1745. return false;
  1746. reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
  1747. reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
  1748. if (reg1 != NULL && reg2 != NULL
  1749. && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
  1750. return false;
  1751. for (i = 0; i < n1; i++)
  1752. {
  1753. ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
  1754. for (j = 0; j < n2; j++)
  1755. {
  1756. ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
  1757. if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
  1758. OBJECT_LIVE_RANGES (c2)))
  1759. return true;
  1760. }
  1761. }
  1762. return false;
  1763. }
  1764. /* The function is used to sort copies according to their execution
  1765. frequencies. */
  1766. static int
  1767. copy_freq_compare_func (const void *v1p, const void *v2p)
  1768. {
  1769. ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
  1770. int pri1, pri2;
  1771. pri1 = cp1->freq;
  1772. pri2 = cp2->freq;
  1773. if (pri2 - pri1)
  1774. return pri2 - pri1;
  1775. /* If frequencies are equal, sort by copies, so that the results of
  1776. qsort leave nothing to chance. */
  1777. return cp1->num - cp2->num;
  1778. }
  1779. /* Return true if any allocno from thread of A1 conflicts with any
  1780. allocno from thread A2. */
  1781. static bool
  1782. allocno_thread_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
  1783. {
  1784. ira_allocno_t a, conflict_a;
  1785. for (a = ALLOCNO_COLOR_DATA (a2)->next_thread_allocno;;
  1786. a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
  1787. {
  1788. for (conflict_a = ALLOCNO_COLOR_DATA (a1)->next_thread_allocno;;
  1789. conflict_a = ALLOCNO_COLOR_DATA (conflict_a)->next_thread_allocno)
  1790. {
  1791. if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
  1792. return true;
  1793. if (conflict_a == a1)
  1794. break;
  1795. }
  1796. if (a == a2)
  1797. break;
  1798. }
  1799. return false;
  1800. }
  1801. /* Merge two threads given correspondingly by their first allocnos T1
  1802. and T2 (more accurately merging T2 into T1). */
  1803. static void
  1804. merge_threads (ira_allocno_t t1, ira_allocno_t t2)
  1805. {
  1806. ira_allocno_t a, next, last;
  1807. gcc_assert (t1 != t2
  1808. && ALLOCNO_COLOR_DATA (t1)->first_thread_allocno == t1
  1809. && ALLOCNO_COLOR_DATA (t2)->first_thread_allocno == t2);
  1810. for (last = t2, a = ALLOCNO_COLOR_DATA (t2)->next_thread_allocno;;
  1811. a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
  1812. {
  1813. ALLOCNO_COLOR_DATA (a)->first_thread_allocno = t1;
  1814. if (a == t2)
  1815. break;
  1816. last = a;
  1817. }
  1818. next = ALLOCNO_COLOR_DATA (t1)->next_thread_allocno;
  1819. ALLOCNO_COLOR_DATA (t1)->next_thread_allocno = t2;
  1820. ALLOCNO_COLOR_DATA (last)->next_thread_allocno = next;
  1821. ALLOCNO_COLOR_DATA (t1)->thread_freq += ALLOCNO_COLOR_DATA (t2)->thread_freq;
  1822. }
  1823. /* Create threads by processing CP_NUM copies from sorted copies. We
  1824. process the most expensive copies first. */
  1825. static void
  1826. form_threads_from_copies (int cp_num)
  1827. {
  1828. ira_allocno_t a, thread1, thread2;
  1829. ira_copy_t cp;
  1830. int i, n;
  1831. qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
  1832. /* Form threads processing copies, most frequently executed
  1833. first. */
  1834. for (; cp_num != 0;)
  1835. {
  1836. for (i = 0; i < cp_num; i++)
  1837. {
  1838. cp = sorted_copies[i];
  1839. thread1 = ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno;
  1840. thread2 = ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno;
  1841. if (thread1 == thread2)
  1842. continue;
  1843. if (! allocno_thread_conflict_p (thread1, thread2))
  1844. {
  1845. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  1846. fprintf
  1847. (ira_dump_file,
  1848. " Forming thread by copy %d:a%dr%d-a%dr%d (freq=%d):\n",
  1849. cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
  1850. ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
  1851. cp->freq);
  1852. merge_threads (thread1, thread2);
  1853. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  1854. {
  1855. thread1 = ALLOCNO_COLOR_DATA (thread1)->first_thread_allocno;
  1856. fprintf (ira_dump_file, " Result (freq=%d): a%dr%d(%d)",
  1857. ALLOCNO_COLOR_DATA (thread1)->thread_freq,
  1858. ALLOCNO_NUM (thread1), ALLOCNO_REGNO (thread1),
  1859. ALLOCNO_FREQ (thread1));
  1860. for (a = ALLOCNO_COLOR_DATA (thread1)->next_thread_allocno;
  1861. a != thread1;
  1862. a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
  1863. fprintf (ira_dump_file, " a%dr%d(%d)",
  1864. ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
  1865. ALLOCNO_FREQ (a));
  1866. fprintf (ira_dump_file, "\n");
  1867. }
  1868. i++;
  1869. break;
  1870. }
  1871. }
  1872. /* Collect the rest of copies. */
  1873. for (n = 0; i < cp_num; i++)
  1874. {
  1875. cp = sorted_copies[i];
  1876. if (ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno
  1877. != ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno)
  1878. sorted_copies[n++] = cp;
  1879. }
  1880. cp_num = n;
  1881. }
  1882. }
  1883. /* Create threads by processing copies of all alocnos from BUCKET. We
  1884. process the most expensive copies first. */
  1885. static void
  1886. form_threads_from_bucket (ira_allocno_t bucket)
  1887. {
  1888. ira_allocno_t a;
  1889. ira_copy_t cp, next_cp;
  1890. int cp_num = 0;
  1891. for (a = bucket; a != NULL; a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
  1892. {
  1893. for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
  1894. {
  1895. if (cp->first == a)
  1896. {
  1897. next_cp = cp->next_first_allocno_copy;
  1898. sorted_copies[cp_num++] = cp;
  1899. }
  1900. else if (cp->second == a)
  1901. next_cp = cp->next_second_allocno_copy;
  1902. else
  1903. gcc_unreachable ();
  1904. }
  1905. }
  1906. form_threads_from_copies (cp_num);
  1907. }
  1908. /* Create threads by processing copies of colorable allocno A. We
  1909. process most expensive copies first. */
  1910. static void
  1911. form_threads_from_colorable_allocno (ira_allocno_t a)
  1912. {
  1913. ira_allocno_t another_a;
  1914. ira_copy_t cp, next_cp;
  1915. int cp_num = 0;
  1916. for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
  1917. {
  1918. if (cp->first == a)
  1919. {
  1920. next_cp = cp->next_first_allocno_copy;
  1921. another_a = cp->second;
  1922. }
  1923. else if (cp->second == a)
  1924. {
  1925. next_cp = cp->next_second_allocno_copy;
  1926. another_a = cp->first;
  1927. }
  1928. else
  1929. gcc_unreachable ();
  1930. if ((! ALLOCNO_COLOR_DATA (another_a)->in_graph_p
  1931. && !ALLOCNO_COLOR_DATA (another_a)->may_be_spilled_p)
  1932. || ALLOCNO_COLOR_DATA (another_a)->colorable_p)
  1933. sorted_copies[cp_num++] = cp;
  1934. }
  1935. form_threads_from_copies (cp_num);
  1936. }
  1937. /* Form initial threads which contain only one allocno. */
  1938. static void
  1939. init_allocno_threads (void)
  1940. {
  1941. ira_allocno_t a;
  1942. unsigned int j;
  1943. bitmap_iterator bi;
  1944. EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
  1945. {
  1946. a = ira_allocnos[j];
  1947. /* Set up initial thread data: */
  1948. ALLOCNO_COLOR_DATA (a)->first_thread_allocno
  1949. = ALLOCNO_COLOR_DATA (a)->next_thread_allocno = a;
  1950. ALLOCNO_COLOR_DATA (a)->thread_freq = ALLOCNO_FREQ (a);
  1951. }
  1952. }
  1953. /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
  1954. /* Bucket of allocnos that can colored currently without spilling. */
  1955. static ira_allocno_t colorable_allocno_bucket;
  1956. /* Bucket of allocnos that might be not colored currently without
  1957. spilling. */
  1958. static ira_allocno_t uncolorable_allocno_bucket;
  1959. /* The current number of allocnos in the uncolorable_bucket. */
  1960. static int uncolorable_allocnos_num;
  1961. /* Return the current spill priority of allocno A. The less the
  1962. number, the more preferable the allocno for spilling. */
  1963. static inline int
  1964. allocno_spill_priority (ira_allocno_t a)
  1965. {
  1966. allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
  1967. return (data->temp
  1968. / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
  1969. * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
  1970. + 1));
  1971. }
  1972. /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
  1973. before the call. */
  1974. static void
  1975. add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
  1976. {
  1977. ira_allocno_t first_a;
  1978. allocno_color_data_t data;
  1979. if (bucket_ptr == &uncolorable_allocno_bucket
  1980. && ALLOCNO_CLASS (a) != NO_REGS)
  1981. {
  1982. uncolorable_allocnos_num++;
  1983. ira_assert (uncolorable_allocnos_num > 0);
  1984. }
  1985. first_a = *bucket_ptr;
  1986. data = ALLOCNO_COLOR_DATA (a);
  1987. data->next_bucket_allocno = first_a;
  1988. data->prev_bucket_allocno = NULL;
  1989. if (first_a != NULL)
  1990. ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
  1991. *bucket_ptr = a;
  1992. }
  1993. /* Compare two allocnos to define which allocno should be pushed first
  1994. into the coloring stack. If the return is a negative number, the
  1995. allocno given by the first parameter will be pushed first. In this
  1996. case such allocno has less priority than the second one and the
  1997. hard register will be assigned to it after assignment to the second
  1998. one. As the result of such assignment order, the second allocno
  1999. has a better chance to get the best hard register. */
  2000. static int
  2001. bucket_allocno_compare_func (const void *v1p, const void *v2p)
  2002. {
  2003. ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
  2004. ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
  2005. int diff, freq1, freq2, a1_num, a2_num;
  2006. ira_allocno_t t1 = ALLOCNO_COLOR_DATA (a1)->first_thread_allocno;
  2007. ira_allocno_t t2 = ALLOCNO_COLOR_DATA (a2)->first_thread_allocno;
  2008. int cl1 = ALLOCNO_CLASS (a1), cl2 = ALLOCNO_CLASS (a2);
  2009. freq1 = ALLOCNO_COLOR_DATA (t1)->thread_freq;
  2010. freq2 = ALLOCNO_COLOR_DATA (t2)->thread_freq;
  2011. if ((diff = freq1 - freq2) != 0)
  2012. return diff;
  2013. if ((diff = ALLOCNO_NUM (t2) - ALLOCNO_NUM (t1)) != 0)
  2014. return diff;
  2015. /* Push pseudos requiring less hard registers first. It means that
  2016. we will assign pseudos requiring more hard registers first
  2017. avoiding creation small holes in free hard register file into
  2018. which the pseudos requiring more hard registers can not fit. */
  2019. if ((diff = (ira_reg_class_max_nregs[cl1][ALLOCNO_MODE (a1)]
  2020. - ira_reg_class_max_nregs[cl2][ALLOCNO_MODE (a2)])) != 0)
  2021. return diff;
  2022. freq1 = ALLOCNO_FREQ (a1);
  2023. freq2 = ALLOCNO_FREQ (a2);
  2024. if ((diff = freq1 - freq2) != 0)
  2025. return diff;
  2026. a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
  2027. a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
  2028. if ((diff = a2_num - a1_num) != 0)
  2029. return diff;
  2030. return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
  2031. }
  2032. /* Sort bucket *BUCKET_PTR and return the result through
  2033. BUCKET_PTR. */
  2034. static void
  2035. sort_bucket (ira_allocno_t *bucket_ptr,
  2036. int (*compare_func) (const void *, const void *))
  2037. {
  2038. ira_allocno_t a, head;
  2039. int n;
  2040. for (n = 0, a = *bucket_ptr;
  2041. a != NULL;
  2042. a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
  2043. sorted_allocnos[n++] = a;
  2044. if (n <= 1)
  2045. return;
  2046. qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
  2047. head = NULL;
  2048. for (n--; n >= 0; n--)
  2049. {
  2050. a = sorted_allocnos[n];
  2051. ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
  2052. ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
  2053. if (head != NULL)
  2054. ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
  2055. head = a;
  2056. }
  2057. *bucket_ptr = head;
  2058. }
  2059. /* Add ALLOCNO to colorable bucket maintaining the order according
  2060. their priority. ALLOCNO should be not in a bucket before the
  2061. call. */
  2062. static void
  2063. add_allocno_to_ordered_colorable_bucket (ira_allocno_t allocno)
  2064. {
  2065. ira_allocno_t before, after;
  2066. form_threads_from_colorable_allocno (allocno);
  2067. for (before = colorable_allocno_bucket, after = NULL;
  2068. before != NULL;
  2069. after = before,
  2070. before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
  2071. if (bucket_allocno_compare_func (&allocno, &before) < 0)
  2072. break;
  2073. ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
  2074. ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
  2075. if (after == NULL)
  2076. colorable_allocno_bucket = allocno;
  2077. else
  2078. ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
  2079. if (before != NULL)
  2080. ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
  2081. }
  2082. /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
  2083. the call. */
  2084. static void
  2085. delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
  2086. {
  2087. ira_allocno_t prev_allocno, next_allocno;
  2088. if (bucket_ptr == &uncolorable_allocno_bucket
  2089. && ALLOCNO_CLASS (allocno) != NO_REGS)
  2090. {
  2091. uncolorable_allocnos_num--;
  2092. ira_assert (uncolorable_allocnos_num >= 0);
  2093. }
  2094. prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
  2095. next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
  2096. if (prev_allocno != NULL)
  2097. ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
  2098. else
  2099. {
  2100. ira_assert (*bucket_ptr == allocno);
  2101. *bucket_ptr = next_allocno;
  2102. }
  2103. if (next_allocno != NULL)
  2104. ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
  2105. }
  2106. /* Put allocno A onto the coloring stack without removing it from its
  2107. bucket. Pushing allocno to the coloring stack can result in moving
  2108. conflicting allocnos from the uncolorable bucket to the colorable
  2109. one. */
  2110. static void
  2111. push_allocno_to_stack (ira_allocno_t a)
  2112. {
  2113. enum reg_class aclass;
  2114. allocno_color_data_t data, conflict_data;
  2115. int size, i, n = ALLOCNO_NUM_OBJECTS (a);
  2116. data = ALLOCNO_COLOR_DATA (a);
  2117. data->in_graph_p = false;
  2118. allocno_stack_vec.safe_push (a);
  2119. aclass = ALLOCNO_CLASS (a);
  2120. if (aclass == NO_REGS)
  2121. return;
  2122. size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
  2123. if (n > 1)
  2124. {
  2125. /* We will deal with the subwords individually. */
  2126. gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
  2127. size = 1;
  2128. }
  2129. for (i = 0; i < n; i++)
  2130. {
  2131. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  2132. ira_object_t conflict_obj;
  2133. ira_object_conflict_iterator oci;
  2134. FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
  2135. {
  2136. ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
  2137. conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
  2138. if (conflict_data->colorable_p
  2139. || ! conflict_data->in_graph_p
  2140. || ALLOCNO_ASSIGNED_P (conflict_a)
  2141. || !(hard_reg_set_intersect_p
  2142. (ALLOCNO_COLOR_DATA (a)->profitable_hard_regs,
  2143. conflict_data->profitable_hard_regs)))
  2144. continue;
  2145. ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
  2146. ALLOCNO_NUM (conflict_a)));
  2147. if (update_left_conflict_sizes_p (conflict_a, a, size))
  2148. {
  2149. delete_allocno_from_bucket
  2150. (conflict_a, &uncolorable_allocno_bucket);
  2151. add_allocno_to_ordered_colorable_bucket (conflict_a);
  2152. if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
  2153. {
  2154. fprintf (ira_dump_file, " Making");
  2155. ira_print_expanded_allocno (conflict_a);
  2156. fprintf (ira_dump_file, " colorable\n");
  2157. }
  2158. }
  2159. }
  2160. }
  2161. }
  2162. /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
  2163. The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
  2164. static void
  2165. remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
  2166. {
  2167. if (colorable_p)
  2168. delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
  2169. else
  2170. delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
  2171. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2172. {
  2173. fprintf (ira_dump_file, " Pushing");
  2174. ira_print_expanded_allocno (allocno);
  2175. if (colorable_p)
  2176. fprintf (ira_dump_file, "(cost %d)\n",
  2177. ALLOCNO_COLOR_DATA (allocno)->temp);
  2178. else
  2179. fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
  2180. ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
  2181. allocno_spill_priority (allocno),
  2182. ALLOCNO_COLOR_DATA (allocno)->temp);
  2183. }
  2184. if (! colorable_p)
  2185. ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
  2186. push_allocno_to_stack (allocno);
  2187. }
  2188. /* Put all allocnos from colorable bucket onto the coloring stack. */
  2189. static void
  2190. push_only_colorable (void)
  2191. {
  2192. form_threads_from_bucket (colorable_allocno_bucket);
  2193. sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
  2194. for (;colorable_allocno_bucket != NULL;)
  2195. remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
  2196. }
  2197. /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
  2198. loop given by its LOOP_NODE. */
  2199. int
  2200. ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
  2201. {
  2202. int freq, i;
  2203. edge_iterator ei;
  2204. edge e;
  2205. vec<edge> edges;
  2206. ira_assert (current_loops != NULL && loop_node->loop != NULL
  2207. && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
  2208. freq = 0;
  2209. if (! exit_p)
  2210. {
  2211. FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
  2212. if (e->src != loop_node->loop->latch
  2213. && (regno < 0
  2214. || (bitmap_bit_p (df_get_live_out (e->src), regno)
  2215. && bitmap_bit_p (df_get_live_in (e->dest), regno))))
  2216. freq += EDGE_FREQUENCY (e);
  2217. }
  2218. else
  2219. {
  2220. edges = get_loop_exit_edges (loop_node->loop);
  2221. FOR_EACH_VEC_ELT (edges, i, e)
  2222. if (regno < 0
  2223. || (bitmap_bit_p (df_get_live_out (e->src), regno)
  2224. && bitmap_bit_p (df_get_live_in (e->dest), regno)))
  2225. freq += EDGE_FREQUENCY (e);
  2226. edges.release ();
  2227. }
  2228. return REG_FREQ_FROM_EDGE_FREQ (freq);
  2229. }
  2230. /* Calculate and return the cost of putting allocno A into memory. */
  2231. static int
  2232. calculate_allocno_spill_cost (ira_allocno_t a)
  2233. {
  2234. int regno, cost;
  2235. machine_mode mode;
  2236. enum reg_class rclass;
  2237. ira_allocno_t parent_allocno;
  2238. ira_loop_tree_node_t parent_node, loop_node;
  2239. regno = ALLOCNO_REGNO (a);
  2240. cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
  2241. if (ALLOCNO_CAP (a) != NULL)
  2242. return cost;
  2243. loop_node = ALLOCNO_LOOP_TREE_NODE (a);
  2244. if ((parent_node = loop_node->parent) == NULL)
  2245. return cost;
  2246. if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
  2247. return cost;
  2248. mode = ALLOCNO_MODE (a);
  2249. rclass = ALLOCNO_CLASS (a);
  2250. if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
  2251. cost -= (ira_memory_move_cost[mode][rclass][0]
  2252. * ira_loop_edge_freq (loop_node, regno, true)
  2253. + ira_memory_move_cost[mode][rclass][1]
  2254. * ira_loop_edge_freq (loop_node, regno, false));
  2255. else
  2256. {
  2257. ira_init_register_move_cost_if_necessary (mode);
  2258. cost += ((ira_memory_move_cost[mode][rclass][1]
  2259. * ira_loop_edge_freq (loop_node, regno, true)
  2260. + ira_memory_move_cost[mode][rclass][0]
  2261. * ira_loop_edge_freq (loop_node, regno, false))
  2262. - (ira_register_move_cost[mode][rclass][rclass]
  2263. * (ira_loop_edge_freq (loop_node, regno, false)
  2264. + ira_loop_edge_freq (loop_node, regno, true))));
  2265. }
  2266. return cost;
  2267. }
  2268. /* Used for sorting allocnos for spilling. */
  2269. static inline int
  2270. allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
  2271. {
  2272. int pri1, pri2, diff;
  2273. if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
  2274. return 1;
  2275. if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
  2276. return -1;
  2277. pri1 = allocno_spill_priority (a1);
  2278. pri2 = allocno_spill_priority (a2);
  2279. if ((diff = pri1 - pri2) != 0)
  2280. return diff;
  2281. if ((diff
  2282. = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
  2283. return diff;
  2284. return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
  2285. }
  2286. /* Used for sorting allocnos for spilling. */
  2287. static int
  2288. allocno_spill_sort_compare (const void *v1p, const void *v2p)
  2289. {
  2290. ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
  2291. ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
  2292. return allocno_spill_priority_compare (p1, p2);
  2293. }
  2294. /* Push allocnos to the coloring stack. The order of allocnos in the
  2295. stack defines the order for the subsequent coloring. */
  2296. static void
  2297. push_allocnos_to_stack (void)
  2298. {
  2299. ira_allocno_t a;
  2300. int cost;
  2301. /* Calculate uncolorable allocno spill costs. */
  2302. for (a = uncolorable_allocno_bucket;
  2303. a != NULL;
  2304. a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
  2305. if (ALLOCNO_CLASS (a) != NO_REGS)
  2306. {
  2307. cost = calculate_allocno_spill_cost (a);
  2308. /* ??? Remove cost of copies between the coalesced
  2309. allocnos. */
  2310. ALLOCNO_COLOR_DATA (a)->temp = cost;
  2311. }
  2312. sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
  2313. for (;;)
  2314. {
  2315. push_only_colorable ();
  2316. a = uncolorable_allocno_bucket;
  2317. if (a == NULL)
  2318. break;
  2319. remove_allocno_from_bucket_and_push (a, false);
  2320. }
  2321. ira_assert (colorable_allocno_bucket == NULL
  2322. && uncolorable_allocno_bucket == NULL);
  2323. ira_assert (uncolorable_allocnos_num == 0);
  2324. }
  2325. /* Pop the coloring stack and assign hard registers to the popped
  2326. allocnos. */
  2327. static void
  2328. pop_allocnos_from_stack (void)
  2329. {
  2330. ira_allocno_t allocno;
  2331. enum reg_class aclass;
  2332. for (;allocno_stack_vec.length () != 0;)
  2333. {
  2334. allocno = allocno_stack_vec.pop ();
  2335. aclass = ALLOCNO_CLASS (allocno);
  2336. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2337. {
  2338. fprintf (ira_dump_file, " Popping");
  2339. ira_print_expanded_allocno (allocno);
  2340. fprintf (ira_dump_file, " -- ");
  2341. }
  2342. if (aclass == NO_REGS)
  2343. {
  2344. ALLOCNO_HARD_REGNO (allocno) = -1;
  2345. ALLOCNO_ASSIGNED_P (allocno) = true;
  2346. ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
  2347. ira_assert
  2348. (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
  2349. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2350. fprintf (ira_dump_file, "assign memory\n");
  2351. }
  2352. else if (assign_hard_reg (allocno, false))
  2353. {
  2354. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2355. fprintf (ira_dump_file, "assign reg %d\n",
  2356. ALLOCNO_HARD_REGNO (allocno));
  2357. }
  2358. else if (ALLOCNO_ASSIGNED_P (allocno))
  2359. {
  2360. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2361. fprintf (ira_dump_file, "spill%s\n",
  2362. ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p
  2363. ? "" : "!");
  2364. }
  2365. ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
  2366. }
  2367. }
  2368. /* Set up number of available hard registers for allocno A. */
  2369. static void
  2370. setup_allocno_available_regs_num (ira_allocno_t a)
  2371. {
  2372. int i, n, hard_regno, hard_regs_num, nwords;
  2373. enum reg_class aclass;
  2374. allocno_color_data_t data;
  2375. aclass = ALLOCNO_CLASS (a);
  2376. data = ALLOCNO_COLOR_DATA (a);
  2377. data->available_regs_num = 0;
  2378. if (aclass == NO_REGS)
  2379. return;
  2380. hard_regs_num = ira_class_hard_regs_num[aclass];
  2381. nwords = ALLOCNO_NUM_OBJECTS (a);
  2382. for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
  2383. {
  2384. hard_regno = ira_class_hard_regs[aclass][i];
  2385. /* Checking only profitable hard regs. */
  2386. if (TEST_HARD_REG_BIT (data->profitable_hard_regs, hard_regno))
  2387. n++;
  2388. }
  2389. data->available_regs_num = n;
  2390. if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
  2391. return;
  2392. fprintf
  2393. (ira_dump_file,
  2394. " Allocno a%dr%d of %s(%d) has %d avail. regs ",
  2395. ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
  2396. reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
  2397. print_hard_reg_set (ira_dump_file, data->profitable_hard_regs, false);
  2398. fprintf (ira_dump_file, ", %snode: ",
  2399. hard_reg_set_equal_p (data->profitable_hard_regs,
  2400. data->hard_regs_node->hard_regs->set)
  2401. ? "" : "^");
  2402. print_hard_reg_set (ira_dump_file,
  2403. data->hard_regs_node->hard_regs->set, false);
  2404. for (i = 0; i < nwords; i++)
  2405. {
  2406. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  2407. if (nwords != 1)
  2408. {
  2409. if (i != 0)
  2410. fprintf (ira_dump_file, ", ");
  2411. fprintf (ira_dump_file, " obj %d", i);
  2412. }
  2413. fprintf (ira_dump_file, " (confl regs = ");
  2414. print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
  2415. false);
  2416. fprintf (ira_dump_file, ")");
  2417. }
  2418. fprintf (ira_dump_file, "\n");
  2419. }
  2420. /* Put ALLOCNO in a bucket corresponding to its number and size of its
  2421. conflicting allocnos and hard registers. */
  2422. static void
  2423. put_allocno_into_bucket (ira_allocno_t allocno)
  2424. {
  2425. ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
  2426. setup_allocno_available_regs_num (allocno);
  2427. if (setup_left_conflict_sizes_p (allocno))
  2428. add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
  2429. else
  2430. add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
  2431. }
  2432. /* Map: allocno number -> allocno priority. */
  2433. static int *allocno_priorities;
  2434. /* Set up priorities for N allocnos in array
  2435. CONSIDERATION_ALLOCNOS. */
  2436. static void
  2437. setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
  2438. {
  2439. int i, length, nrefs, priority, max_priority, mult;
  2440. ira_allocno_t a;
  2441. max_priority = 0;
  2442. for (i = 0; i < n; i++)
  2443. {
  2444. a = consideration_allocnos[i];
  2445. nrefs = ALLOCNO_NREFS (a);
  2446. ira_assert (nrefs >= 0);
  2447. mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
  2448. ira_assert (mult >= 0);
  2449. allocno_priorities[ALLOCNO_NUM (a)]
  2450. = priority
  2451. = (mult
  2452. * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a))
  2453. * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
  2454. if (priority < 0)
  2455. priority = -priority;
  2456. if (max_priority < priority)
  2457. max_priority = priority;
  2458. }
  2459. mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
  2460. for (i = 0; i < n; i++)
  2461. {
  2462. a = consideration_allocnos[i];
  2463. length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
  2464. if (ALLOCNO_NUM_OBJECTS (a) > 1)
  2465. length /= ALLOCNO_NUM_OBJECTS (a);
  2466. if (length <= 0)
  2467. length = 1;
  2468. allocno_priorities[ALLOCNO_NUM (a)]
  2469. = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
  2470. }
  2471. }
  2472. /* Sort allocnos according to the profit of usage of a hard register
  2473. instead of memory for them. */
  2474. static int
  2475. allocno_cost_compare_func (const void *v1p, const void *v2p)
  2476. {
  2477. ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
  2478. ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
  2479. int c1, c2;
  2480. c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
  2481. c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
  2482. if (c1 - c2)
  2483. return c1 - c2;
  2484. /* If regs are equally good, sort by allocno numbers, so that the
  2485. results of qsort leave nothing to chance. */
  2486. return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
  2487. }
  2488. /* We used Chaitin-Briggs coloring to assign as many pseudos as
  2489. possible to hard registers. Let us try to improve allocation with
  2490. cost point of view. This function improves the allocation by
  2491. spilling some allocnos and assigning the freed hard registers to
  2492. other allocnos if it decreases the overall allocation cost. */
  2493. static void
  2494. improve_allocation (void)
  2495. {
  2496. unsigned int i;
  2497. int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
  2498. int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
  2499. bool try_p;
  2500. enum reg_class aclass;
  2501. machine_mode mode;
  2502. int *allocno_costs;
  2503. int costs[FIRST_PSEUDO_REGISTER];
  2504. HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
  2505. ira_allocno_t a;
  2506. bitmap_iterator bi;
  2507. /* Clear counts used to process conflicting allocnos only once for
  2508. each allocno. */
  2509. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  2510. ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
  2511. check = n = 0;
  2512. /* Process each allocno and try to assign a hard register to it by
  2513. spilling some its conflicting allocnos. */
  2514. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  2515. {
  2516. a = ira_allocnos[i];
  2517. ALLOCNO_COLOR_DATA (a)->temp = 0;
  2518. if (empty_profitable_hard_regs (a))
  2519. continue;
  2520. check++;
  2521. aclass = ALLOCNO_CLASS (a);
  2522. allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
  2523. if (allocno_costs == NULL)
  2524. allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
  2525. if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
  2526. base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
  2527. else if (allocno_costs == NULL)
  2528. /* It means that assigning a hard register is not profitable
  2529. (we don't waste memory for hard register costs in this
  2530. case). */
  2531. continue;
  2532. else
  2533. base_cost = allocno_costs[ira_class_hard_reg_index[aclass][hregno]];
  2534. try_p = false;
  2535. get_conflict_and_start_profitable_regs (a, false,
  2536. conflicting_regs,
  2537. &profitable_hard_regs);
  2538. class_size = ira_class_hard_regs_num[aclass];
  2539. /* Set up cost improvement for usage of each profitable hard
  2540. register for allocno A. */
  2541. for (j = 0; j < class_size; j++)
  2542. {
  2543. hregno = ira_class_hard_regs[aclass][j];
  2544. if (! check_hard_reg_p (a, hregno,
  2545. conflicting_regs, profitable_hard_regs))
  2546. continue;
  2547. ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
  2548. k = allocno_costs == NULL ? 0 : j;
  2549. costs[hregno] = (allocno_costs == NULL
  2550. ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
  2551. costs[hregno] -= base_cost;
  2552. if (costs[hregno] < 0)
  2553. try_p = true;
  2554. }
  2555. if (! try_p)
  2556. /* There is no chance to improve the allocation cost by
  2557. assigning hard register to allocno A even without spilling
  2558. conflicting allocnos. */
  2559. continue;
  2560. mode = ALLOCNO_MODE (a);
  2561. nwords = ALLOCNO_NUM_OBJECTS (a);
  2562. /* Process each allocno conflicting with A and update the cost
  2563. improvement for profitable hard registers of A. To use a
  2564. hard register for A we need to spill some conflicting
  2565. allocnos and that creates penalty for the cost
  2566. improvement. */
  2567. for (word = 0; word < nwords; word++)
  2568. {
  2569. ira_object_t conflict_obj;
  2570. ira_object_t obj = ALLOCNO_OBJECT (a, word);
  2571. ira_object_conflict_iterator oci;
  2572. FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
  2573. {
  2574. ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
  2575. if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
  2576. /* We already processed this conflicting allocno
  2577. because we processed earlier another object of the
  2578. conflicting allocno. */
  2579. continue;
  2580. ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
  2581. if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
  2582. continue;
  2583. spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
  2584. k = (ira_class_hard_reg_index
  2585. [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
  2586. ira_assert (k >= 0);
  2587. if ((allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (conflict_a))
  2588. != NULL)
  2589. spill_cost -= allocno_costs[k];
  2590. else if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
  2591. != NULL)
  2592. spill_cost -= allocno_costs[k];
  2593. else
  2594. spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
  2595. conflict_nregs
  2596. = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
  2597. for (r = conflict_hregno;
  2598. r >= 0 && r + hard_regno_nregs[r][mode] > conflict_hregno;
  2599. r--)
  2600. if (check_hard_reg_p (a, r,
  2601. conflicting_regs, profitable_hard_regs))
  2602. costs[r] += spill_cost;
  2603. for (r = conflict_hregno + 1;
  2604. r < conflict_hregno + conflict_nregs;
  2605. r++)
  2606. if (check_hard_reg_p (a, r,
  2607. conflicting_regs, profitable_hard_regs))
  2608. costs[r] += spill_cost;
  2609. }
  2610. }
  2611. min_cost = INT_MAX;
  2612. best = -1;
  2613. /* Now we choose hard register for A which results in highest
  2614. allocation cost improvement. */
  2615. for (j = 0; j < class_size; j++)
  2616. {
  2617. hregno = ira_class_hard_regs[aclass][j];
  2618. if (check_hard_reg_p (a, hregno,
  2619. conflicting_regs, profitable_hard_regs)
  2620. && min_cost > costs[hregno])
  2621. {
  2622. best = hregno;
  2623. min_cost = costs[hregno];
  2624. }
  2625. }
  2626. if (min_cost >= 0)
  2627. /* We are in a situation when assigning any hard register to A
  2628. by spilling some conflicting allocnos does not improve the
  2629. allocation cost. */
  2630. continue;
  2631. nregs = hard_regno_nregs[best][mode];
  2632. /* Now spill conflicting allocnos which contain a hard register
  2633. of A when we assign the best chosen hard register to it. */
  2634. for (word = 0; word < nwords; word++)
  2635. {
  2636. ira_object_t conflict_obj;
  2637. ira_object_t obj = ALLOCNO_OBJECT (a, word);
  2638. ira_object_conflict_iterator oci;
  2639. FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
  2640. {
  2641. ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
  2642. if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
  2643. continue;
  2644. conflict_nregs
  2645. = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
  2646. if (best + nregs <= conflict_hregno
  2647. || conflict_hregno + conflict_nregs <= best)
  2648. /* No intersection. */
  2649. continue;
  2650. ALLOCNO_HARD_REGNO (conflict_a) = -1;
  2651. sorted_allocnos[n++] = conflict_a;
  2652. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  2653. fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
  2654. ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
  2655. ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
  2656. }
  2657. }
  2658. /* Assign the best chosen hard register to A. */
  2659. ALLOCNO_HARD_REGNO (a) = best;
  2660. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  2661. fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
  2662. best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
  2663. }
  2664. if (n == 0)
  2665. return;
  2666. /* We spilled some allocnos to assign their hard registers to other
  2667. allocnos. The spilled allocnos are now in array
  2668. 'sorted_allocnos'. There is still a possibility that some of the
  2669. spilled allocnos can get hard registers. So let us try assign
  2670. them hard registers again (just a reminder -- function
  2671. 'assign_hard_reg' assigns hard registers only if it is possible
  2672. and profitable). We process the spilled allocnos with biggest
  2673. benefit to get hard register first -- see function
  2674. 'allocno_cost_compare_func'. */
  2675. qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
  2676. allocno_cost_compare_func);
  2677. for (j = 0; j < n; j++)
  2678. {
  2679. a = sorted_allocnos[j];
  2680. ALLOCNO_ASSIGNED_P (a) = false;
  2681. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2682. {
  2683. fprintf (ira_dump_file, " ");
  2684. ira_print_expanded_allocno (a);
  2685. fprintf (ira_dump_file, " -- ");
  2686. }
  2687. if (assign_hard_reg (a, false))
  2688. {
  2689. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2690. fprintf (ira_dump_file, "assign hard reg %d\n",
  2691. ALLOCNO_HARD_REGNO (a));
  2692. }
  2693. else
  2694. {
  2695. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2696. fprintf (ira_dump_file, "assign memory\n");
  2697. }
  2698. }
  2699. }
  2700. /* Sort allocnos according to their priorities. */
  2701. static int
  2702. allocno_priority_compare_func (const void *v1p, const void *v2p)
  2703. {
  2704. ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
  2705. ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
  2706. int pri1, pri2;
  2707. pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
  2708. pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
  2709. if (pri2 != pri1)
  2710. return SORTGT (pri2, pri1);
  2711. /* If regs are equally good, sort by allocnos, so that the results of
  2712. qsort leave nothing to chance. */
  2713. return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
  2714. }
  2715. /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
  2716. taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
  2717. static void
  2718. color_allocnos (void)
  2719. {
  2720. unsigned int i, n;
  2721. bitmap_iterator bi;
  2722. ira_allocno_t a;
  2723. setup_profitable_hard_regs ();
  2724. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  2725. {
  2726. int l, nr;
  2727. HARD_REG_SET conflict_hard_regs;
  2728. allocno_color_data_t data;
  2729. ira_pref_t pref, next_pref;
  2730. a = ira_allocnos[i];
  2731. nr = ALLOCNO_NUM_OBJECTS (a);
  2732. CLEAR_HARD_REG_SET (conflict_hard_regs);
  2733. for (l = 0; l < nr; l++)
  2734. {
  2735. ira_object_t obj = ALLOCNO_OBJECT (a, l);
  2736. IOR_HARD_REG_SET (conflict_hard_regs,
  2737. OBJECT_CONFLICT_HARD_REGS (obj));
  2738. }
  2739. data = ALLOCNO_COLOR_DATA (a);
  2740. for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
  2741. {
  2742. next_pref = pref->next_pref;
  2743. if (! ira_hard_reg_in_set_p (pref->hard_regno,
  2744. ALLOCNO_MODE (a),
  2745. data->profitable_hard_regs))
  2746. ira_remove_pref (pref);
  2747. }
  2748. }
  2749. if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
  2750. {
  2751. n = 0;
  2752. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  2753. {
  2754. a = ira_allocnos[i];
  2755. if (ALLOCNO_CLASS (a) == NO_REGS)
  2756. {
  2757. ALLOCNO_HARD_REGNO (a) = -1;
  2758. ALLOCNO_ASSIGNED_P (a) = true;
  2759. ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
  2760. ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
  2761. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2762. {
  2763. fprintf (ira_dump_file, " Spill");
  2764. ira_print_expanded_allocno (a);
  2765. fprintf (ira_dump_file, "\n");
  2766. }
  2767. continue;
  2768. }
  2769. sorted_allocnos[n++] = a;
  2770. }
  2771. if (n != 0)
  2772. {
  2773. setup_allocno_priorities (sorted_allocnos, n);
  2774. qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
  2775. allocno_priority_compare_func);
  2776. for (i = 0; i < n; i++)
  2777. {
  2778. a = sorted_allocnos[i];
  2779. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2780. {
  2781. fprintf (ira_dump_file, " ");
  2782. ira_print_expanded_allocno (a);
  2783. fprintf (ira_dump_file, " -- ");
  2784. }
  2785. if (assign_hard_reg (a, false))
  2786. {
  2787. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2788. fprintf (ira_dump_file, "assign hard reg %d\n",
  2789. ALLOCNO_HARD_REGNO (a));
  2790. }
  2791. else
  2792. {
  2793. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2794. fprintf (ira_dump_file, "assign memory\n");
  2795. }
  2796. }
  2797. }
  2798. }
  2799. else
  2800. {
  2801. form_allocno_hard_regs_nodes_forest ();
  2802. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  2803. print_hard_regs_forest (ira_dump_file);
  2804. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  2805. {
  2806. a = ira_allocnos[i];
  2807. if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
  2808. {
  2809. ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
  2810. update_costs_from_prefs (a);
  2811. }
  2812. else
  2813. {
  2814. ALLOCNO_HARD_REGNO (a) = -1;
  2815. ALLOCNO_ASSIGNED_P (a) = true;
  2816. /* We don't need updated costs anymore. */
  2817. ira_free_allocno_updated_costs (a);
  2818. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2819. {
  2820. fprintf (ira_dump_file, " Spill");
  2821. ira_print_expanded_allocno (a);
  2822. fprintf (ira_dump_file, "\n");
  2823. }
  2824. }
  2825. }
  2826. /* Put the allocnos into the corresponding buckets. */
  2827. colorable_allocno_bucket = NULL;
  2828. uncolorable_allocno_bucket = NULL;
  2829. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
  2830. {
  2831. a = ira_allocnos[i];
  2832. if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
  2833. put_allocno_into_bucket (a);
  2834. }
  2835. push_allocnos_to_stack ();
  2836. pop_allocnos_from_stack ();
  2837. finish_allocno_hard_regs_nodes_forest ();
  2838. }
  2839. improve_allocation ();
  2840. }
  2841. /* Output information about the loop given by its LOOP_TREE_NODE. */
  2842. static void
  2843. print_loop_title (ira_loop_tree_node_t loop_tree_node)
  2844. {
  2845. unsigned int j;
  2846. bitmap_iterator bi;
  2847. ira_loop_tree_node_t subloop_node, dest_loop_node;
  2848. edge e;
  2849. edge_iterator ei;
  2850. if (loop_tree_node->parent == NULL)
  2851. fprintf (ira_dump_file,
  2852. "\n Loop 0 (parent -1, header bb%d, depth 0)\n bbs:",
  2853. NUM_FIXED_BLOCKS);
  2854. else
  2855. {
  2856. ira_assert (current_loops != NULL && loop_tree_node->loop != NULL);
  2857. fprintf (ira_dump_file,
  2858. "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
  2859. loop_tree_node->loop_num, loop_tree_node->parent->loop_num,
  2860. loop_tree_node->loop->header->index,
  2861. loop_depth (loop_tree_node->loop));
  2862. }
  2863. for (subloop_node = loop_tree_node->children;
  2864. subloop_node != NULL;
  2865. subloop_node = subloop_node->next)
  2866. if (subloop_node->bb != NULL)
  2867. {
  2868. fprintf (ira_dump_file, " %d", subloop_node->bb->index);
  2869. FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
  2870. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  2871. && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
  2872. != loop_tree_node))
  2873. fprintf (ira_dump_file, "(->%d:l%d)",
  2874. e->dest->index, dest_loop_node->loop_num);
  2875. }
  2876. fprintf (ira_dump_file, "\n all:");
  2877. EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
  2878. fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
  2879. fprintf (ira_dump_file, "\n modified regnos:");
  2880. EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
  2881. fprintf (ira_dump_file, " %d", j);
  2882. fprintf (ira_dump_file, "\n border:");
  2883. EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
  2884. fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
  2885. fprintf (ira_dump_file, "\n Pressure:");
  2886. for (j = 0; (int) j < ira_pressure_classes_num; j++)
  2887. {
  2888. enum reg_class pclass;
  2889. pclass = ira_pressure_classes[j];
  2890. if (loop_tree_node->reg_pressure[pclass] == 0)
  2891. continue;
  2892. fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
  2893. loop_tree_node->reg_pressure[pclass]);
  2894. }
  2895. fprintf (ira_dump_file, "\n");
  2896. }
  2897. /* Color the allocnos inside loop (in the extreme case it can be all
  2898. of the function) given the corresponding LOOP_TREE_NODE. The
  2899. function is called for each loop during top-down traverse of the
  2900. loop tree. */
  2901. static void
  2902. color_pass (ira_loop_tree_node_t loop_tree_node)
  2903. {
  2904. int regno, hard_regno, index = -1, n;
  2905. int cost, exit_freq, enter_freq;
  2906. unsigned int j;
  2907. bitmap_iterator bi;
  2908. machine_mode mode;
  2909. enum reg_class rclass, aclass, pclass;
  2910. ira_allocno_t a, subloop_allocno;
  2911. ira_loop_tree_node_t subloop_node;
  2912. ira_assert (loop_tree_node->bb == NULL);
  2913. if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
  2914. print_loop_title (loop_tree_node);
  2915. bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
  2916. bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
  2917. n = 0;
  2918. EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
  2919. {
  2920. a = ira_allocnos[j];
  2921. n++;
  2922. if (! ALLOCNO_ASSIGNED_P (a))
  2923. continue;
  2924. bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
  2925. }
  2926. allocno_color_data
  2927. = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
  2928. * n);
  2929. memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
  2930. curr_allocno_process = 0;
  2931. n = 0;
  2932. EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
  2933. {
  2934. a = ira_allocnos[j];
  2935. ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
  2936. n++;
  2937. }
  2938. init_allocno_threads ();
  2939. /* Color all mentioned allocnos including transparent ones. */
  2940. color_allocnos ();
  2941. /* Process caps. They are processed just once. */
  2942. if (flag_ira_region == IRA_REGION_MIXED
  2943. || flag_ira_region == IRA_REGION_ALL)
  2944. EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
  2945. {
  2946. a = ira_allocnos[j];
  2947. if (ALLOCNO_CAP_MEMBER (a) == NULL)
  2948. continue;
  2949. /* Remove from processing in the next loop. */
  2950. bitmap_clear_bit (consideration_allocno_bitmap, j);
  2951. rclass = ALLOCNO_CLASS (a);
  2952. pclass = ira_pressure_class_translate[rclass];
  2953. if (flag_ira_region == IRA_REGION_MIXED
  2954. && (loop_tree_node->reg_pressure[pclass]
  2955. <= ira_class_hard_regs_num[pclass]))
  2956. {
  2957. mode = ALLOCNO_MODE (a);
  2958. hard_regno = ALLOCNO_HARD_REGNO (a);
  2959. if (hard_regno >= 0)
  2960. {
  2961. index = ira_class_hard_reg_index[rclass][hard_regno];
  2962. ira_assert (index >= 0);
  2963. }
  2964. regno = ALLOCNO_REGNO (a);
  2965. subloop_allocno = ALLOCNO_CAP_MEMBER (a);
  2966. subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
  2967. ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
  2968. ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
  2969. ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
  2970. if (hard_regno >= 0)
  2971. update_costs_from_copies (subloop_allocno, true, true);
  2972. /* We don't need updated costs anymore. */
  2973. ira_free_allocno_updated_costs (subloop_allocno);
  2974. }
  2975. }
  2976. /* Update costs of the corresponding allocnos (not caps) in the
  2977. subloops. */
  2978. for (subloop_node = loop_tree_node->subloops;
  2979. subloop_node != NULL;
  2980. subloop_node = subloop_node->subloop_next)
  2981. {
  2982. ira_assert (subloop_node->bb == NULL);
  2983. EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
  2984. {
  2985. a = ira_allocnos[j];
  2986. ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
  2987. mode = ALLOCNO_MODE (a);
  2988. rclass = ALLOCNO_CLASS (a);
  2989. pclass = ira_pressure_class_translate[rclass];
  2990. hard_regno = ALLOCNO_HARD_REGNO (a);
  2991. /* Use hard register class here. ??? */
  2992. if (hard_regno >= 0)
  2993. {
  2994. index = ira_class_hard_reg_index[rclass][hard_regno];
  2995. ira_assert (index >= 0);
  2996. }
  2997. regno = ALLOCNO_REGNO (a);
  2998. /* ??? conflict costs */
  2999. subloop_allocno = subloop_node->regno_allocno_map[regno];
  3000. if (subloop_allocno == NULL
  3001. || ALLOCNO_CAP (subloop_allocno) != NULL)
  3002. continue;
  3003. ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
  3004. ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
  3005. ALLOCNO_NUM (subloop_allocno)));
  3006. if ((flag_ira_region == IRA_REGION_MIXED
  3007. && (loop_tree_node->reg_pressure[pclass]
  3008. <= ira_class_hard_regs_num[pclass]))
  3009. || (pic_offset_table_rtx != NULL
  3010. && regno == (int) REGNO (pic_offset_table_rtx))
  3011. /* Avoid overlapped multi-registers. Moves between them
  3012. might result in wrong code generation. */
  3013. || (hard_regno >= 0
  3014. && ira_reg_class_max_nregs[pclass][mode] > 1))
  3015. {
  3016. if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
  3017. {
  3018. ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
  3019. ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
  3020. if (hard_regno >= 0)
  3021. update_costs_from_copies (subloop_allocno, true, true);
  3022. /* We don't need updated costs anymore. */
  3023. ira_free_allocno_updated_costs (subloop_allocno);
  3024. }
  3025. continue;
  3026. }
  3027. exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
  3028. enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
  3029. ira_assert (regno < ira_reg_equiv_len);
  3030. if (ira_equiv_no_lvalue_p (regno))
  3031. {
  3032. if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
  3033. {
  3034. ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
  3035. ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
  3036. if (hard_regno >= 0)
  3037. update_costs_from_copies (subloop_allocno, true, true);
  3038. /* We don't need updated costs anymore. */
  3039. ira_free_allocno_updated_costs (subloop_allocno);
  3040. }
  3041. }
  3042. else if (hard_regno < 0)
  3043. {
  3044. ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
  3045. -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
  3046. + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
  3047. }
  3048. else
  3049. {
  3050. aclass = ALLOCNO_CLASS (subloop_allocno);
  3051. ira_init_register_move_cost_if_necessary (mode);
  3052. cost = (ira_register_move_cost[mode][rclass][rclass]
  3053. * (exit_freq + enter_freq));
  3054. ira_allocate_and_set_or_copy_costs
  3055. (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
  3056. ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
  3057. ALLOCNO_HARD_REG_COSTS (subloop_allocno));
  3058. ira_allocate_and_set_or_copy_costs
  3059. (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
  3060. aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
  3061. ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
  3062. ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
  3063. -= cost;
  3064. if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
  3065. > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
  3066. ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
  3067. = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
  3068. ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
  3069. += (ira_memory_move_cost[mode][rclass][0] * enter_freq
  3070. + ira_memory_move_cost[mode][rclass][1] * exit_freq);
  3071. }
  3072. }
  3073. }
  3074. ira_free (allocno_color_data);
  3075. EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
  3076. {
  3077. a = ira_allocnos[j];
  3078. ALLOCNO_ADD_DATA (a) = NULL;
  3079. }
  3080. }
  3081. /* Initialize the common data for coloring and calls functions to do
  3082. Chaitin-Briggs and regional coloring. */
  3083. static void
  3084. do_coloring (void)
  3085. {
  3086. coloring_allocno_bitmap = ira_allocate_bitmap ();
  3087. if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
  3088. fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
  3089. ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
  3090. if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
  3091. ira_print_disposition (ira_dump_file);
  3092. ira_free_bitmap (coloring_allocno_bitmap);
  3093. }
  3094. /* Move spill/restore code, which are to be generated in ira-emit.c,
  3095. to less frequent points (if it is profitable) by reassigning some
  3096. allocnos (in loop with subloops containing in another loop) to
  3097. memory which results in longer live-range where the corresponding
  3098. pseudo-registers will be in memory. */
  3099. static void
  3100. move_spill_restore (void)
  3101. {
  3102. int cost, regno, hard_regno, hard_regno2, index;
  3103. bool changed_p;
  3104. int enter_freq, exit_freq;
  3105. machine_mode mode;
  3106. enum reg_class rclass;
  3107. ira_allocno_t a, parent_allocno, subloop_allocno;
  3108. ira_loop_tree_node_t parent, loop_node, subloop_node;
  3109. ira_allocno_iterator ai;
  3110. for (;;)
  3111. {
  3112. changed_p = false;
  3113. if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
  3114. fprintf (ira_dump_file, "New iteration of spill/restore move\n");
  3115. FOR_EACH_ALLOCNO (a, ai)
  3116. {
  3117. regno = ALLOCNO_REGNO (a);
  3118. loop_node = ALLOCNO_LOOP_TREE_NODE (a);
  3119. if (ALLOCNO_CAP_MEMBER (a) != NULL
  3120. || ALLOCNO_CAP (a) != NULL
  3121. || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
  3122. || loop_node->children == NULL
  3123. /* don't do the optimization because it can create
  3124. copies and the reload pass can spill the allocno set
  3125. by copy although the allocno will not get memory
  3126. slot. */
  3127. || ira_equiv_no_lvalue_p (regno)
  3128. || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a)))
  3129. continue;
  3130. mode = ALLOCNO_MODE (a);
  3131. rclass = ALLOCNO_CLASS (a);
  3132. index = ira_class_hard_reg_index[rclass][hard_regno];
  3133. ira_assert (index >= 0);
  3134. cost = (ALLOCNO_MEMORY_COST (a)
  3135. - (ALLOCNO_HARD_REG_COSTS (a) == NULL
  3136. ? ALLOCNO_CLASS_COST (a)
  3137. : ALLOCNO_HARD_REG_COSTS (a)[index]));
  3138. ira_init_register_move_cost_if_necessary (mode);
  3139. for (subloop_node = loop_node->subloops;
  3140. subloop_node != NULL;
  3141. subloop_node = subloop_node->subloop_next)
  3142. {
  3143. ira_assert (subloop_node->bb == NULL);
  3144. subloop_allocno = subloop_node->regno_allocno_map[regno];
  3145. if (subloop_allocno == NULL)
  3146. continue;
  3147. ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
  3148. /* We have accumulated cost. To get the real cost of
  3149. allocno usage in the loop we should subtract costs of
  3150. the subloop allocnos. */
  3151. cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
  3152. - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
  3153. ? ALLOCNO_CLASS_COST (subloop_allocno)
  3154. : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
  3155. exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
  3156. enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
  3157. if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
  3158. cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
  3159. + ira_memory_move_cost[mode][rclass][1] * enter_freq);
  3160. else
  3161. {
  3162. cost
  3163. += (ira_memory_move_cost[mode][rclass][0] * exit_freq
  3164. + ira_memory_move_cost[mode][rclass][1] * enter_freq);
  3165. if (hard_regno2 != hard_regno)
  3166. cost -= (ira_register_move_cost[mode][rclass][rclass]
  3167. * (exit_freq + enter_freq));
  3168. }
  3169. }
  3170. if ((parent = loop_node->parent) != NULL
  3171. && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
  3172. {
  3173. ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
  3174. exit_freq = ira_loop_edge_freq (loop_node, regno, true);
  3175. enter_freq = ira_loop_edge_freq (loop_node, regno, false);
  3176. if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
  3177. cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
  3178. + ira_memory_move_cost[mode][rclass][1] * enter_freq);
  3179. else
  3180. {
  3181. cost
  3182. += (ira_memory_move_cost[mode][rclass][1] * exit_freq
  3183. + ira_memory_move_cost[mode][rclass][0] * enter_freq);
  3184. if (hard_regno2 != hard_regno)
  3185. cost -= (ira_register_move_cost[mode][rclass][rclass]
  3186. * (exit_freq + enter_freq));
  3187. }
  3188. }
  3189. if (cost < 0)
  3190. {
  3191. ALLOCNO_HARD_REGNO (a) = -1;
  3192. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  3193. {
  3194. fprintf
  3195. (ira_dump_file,
  3196. " Moving spill/restore for a%dr%d up from loop %d",
  3197. ALLOCNO_NUM (a), regno, loop_node->loop_num);
  3198. fprintf (ira_dump_file, " - profit %d\n", -cost);
  3199. }
  3200. changed_p = true;
  3201. }
  3202. }
  3203. if (! changed_p)
  3204. break;
  3205. }
  3206. }
  3207. /* Update current hard reg costs and current conflict hard reg costs
  3208. for allocno A. It is done by processing its copies containing
  3209. other allocnos already assigned. */
  3210. static void
  3211. update_curr_costs (ira_allocno_t a)
  3212. {
  3213. int i, hard_regno, cost;
  3214. machine_mode mode;
  3215. enum reg_class aclass, rclass;
  3216. ira_allocno_t another_a;
  3217. ira_copy_t cp, next_cp;
  3218. ira_free_allocno_updated_costs (a);
  3219. ira_assert (! ALLOCNO_ASSIGNED_P (a));
  3220. aclass = ALLOCNO_CLASS (a);
  3221. if (aclass == NO_REGS)
  3222. return;
  3223. mode = ALLOCNO_MODE (a);
  3224. ira_init_register_move_cost_if_necessary (mode);
  3225. for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
  3226. {
  3227. if (cp->first == a)
  3228. {
  3229. next_cp = cp->next_first_allocno_copy;
  3230. another_a = cp->second;
  3231. }
  3232. else if (cp->second == a)
  3233. {
  3234. next_cp = cp->next_second_allocno_copy;
  3235. another_a = cp->first;
  3236. }
  3237. else
  3238. gcc_unreachable ();
  3239. if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
  3240. || ! ALLOCNO_ASSIGNED_P (another_a)
  3241. || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
  3242. continue;
  3243. rclass = REGNO_REG_CLASS (hard_regno);
  3244. i = ira_class_hard_reg_index[aclass][hard_regno];
  3245. if (i < 0)
  3246. continue;
  3247. cost = (cp->first == a
  3248. ? ira_register_move_cost[mode][rclass][aclass]
  3249. : ira_register_move_cost[mode][aclass][rclass]);
  3250. ira_allocate_and_set_or_copy_costs
  3251. (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
  3252. ALLOCNO_HARD_REG_COSTS (a));
  3253. ira_allocate_and_set_or_copy_costs
  3254. (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
  3255. aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
  3256. ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
  3257. ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
  3258. }
  3259. }
  3260. /* Try to assign hard registers to the unassigned allocnos and
  3261. allocnos conflicting with them or conflicting with allocnos whose
  3262. regno >= START_REGNO. The function is called after ira_flattening,
  3263. so more allocnos (including ones created in ira-emit.c) will have a
  3264. chance to get a hard register. We use simple assignment algorithm
  3265. based on priorities. */
  3266. void
  3267. ira_reassign_conflict_allocnos (int start_regno)
  3268. {
  3269. int i, allocnos_to_color_num;
  3270. ira_allocno_t a;
  3271. enum reg_class aclass;
  3272. bitmap allocnos_to_color;
  3273. ira_allocno_iterator ai;
  3274. allocnos_to_color = ira_allocate_bitmap ();
  3275. allocnos_to_color_num = 0;
  3276. FOR_EACH_ALLOCNO (a, ai)
  3277. {
  3278. int n = ALLOCNO_NUM_OBJECTS (a);
  3279. if (! ALLOCNO_ASSIGNED_P (a)
  3280. && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
  3281. {
  3282. if (ALLOCNO_CLASS (a) != NO_REGS)
  3283. sorted_allocnos[allocnos_to_color_num++] = a;
  3284. else
  3285. {
  3286. ALLOCNO_ASSIGNED_P (a) = true;
  3287. ALLOCNO_HARD_REGNO (a) = -1;
  3288. ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
  3289. ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
  3290. }
  3291. bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
  3292. }
  3293. if (ALLOCNO_REGNO (a) < start_regno
  3294. || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
  3295. continue;
  3296. for (i = 0; i < n; i++)
  3297. {
  3298. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  3299. ira_object_t conflict_obj;
  3300. ira_object_conflict_iterator oci;
  3301. FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
  3302. {
  3303. ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
  3304. ira_assert (ira_reg_classes_intersect_p
  3305. [aclass][ALLOCNO_CLASS (conflict_a)]);
  3306. if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
  3307. continue;
  3308. sorted_allocnos[allocnos_to_color_num++] = conflict_a;
  3309. }
  3310. }
  3311. }
  3312. ira_free_bitmap (allocnos_to_color);
  3313. if (allocnos_to_color_num > 1)
  3314. {
  3315. setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
  3316. qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
  3317. allocno_priority_compare_func);
  3318. }
  3319. for (i = 0; i < allocnos_to_color_num; i++)
  3320. {
  3321. a = sorted_allocnos[i];
  3322. ALLOCNO_ASSIGNED_P (a) = false;
  3323. update_curr_costs (a);
  3324. }
  3325. for (i = 0; i < allocnos_to_color_num; i++)
  3326. {
  3327. a = sorted_allocnos[i];
  3328. if (assign_hard_reg (a, true))
  3329. {
  3330. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  3331. fprintf
  3332. (ira_dump_file,
  3333. " Secondary allocation: assign hard reg %d to reg %d\n",
  3334. ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
  3335. }
  3336. }
  3337. }
  3338. /* This page contains functions used to find conflicts using allocno
  3339. live ranges. */
  3340. #ifdef ENABLE_IRA_CHECKING
  3341. /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
  3342. intersect. This should be used when there is only one region.
  3343. Currently this is used during reload. */
  3344. static bool
  3345. conflict_by_live_ranges_p (int regno1, int regno2)
  3346. {
  3347. ira_allocno_t a1, a2;
  3348. ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
  3349. && regno2 >= FIRST_PSEUDO_REGISTER);
  3350. /* Reg info calculated by dataflow infrastructure can be different
  3351. from one calculated by regclass. */
  3352. if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
  3353. || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
  3354. return false;
  3355. return allocnos_conflict_by_live_ranges_p (a1, a2);
  3356. }
  3357. #endif
  3358. /* This page contains code to coalesce memory stack slots used by
  3359. spilled allocnos. This results in smaller stack frame, better data
  3360. locality, and in smaller code for some architectures like
  3361. x86/x86_64 where insn size depends on address displacement value.
  3362. On the other hand, it can worsen insn scheduling after the RA but
  3363. in practice it is less important than smaller stack frames. */
  3364. /* TRUE if we coalesced some allocnos. In other words, if we got
  3365. loops formed by members first_coalesced_allocno and
  3366. next_coalesced_allocno containing more one allocno. */
  3367. static bool allocno_coalesced_p;
  3368. /* Bitmap used to prevent a repeated allocno processing because of
  3369. coalescing. */
  3370. static bitmap processed_coalesced_allocno_bitmap;
  3371. /* See below. */
  3372. typedef struct coalesce_data *coalesce_data_t;
  3373. /* To decrease footprint of ira_allocno structure we store all data
  3374. needed only for coalescing in the following structure. */
  3375. struct coalesce_data
  3376. {
  3377. /* Coalesced allocnos form a cyclic list. One allocno given by
  3378. FIRST represents all coalesced allocnos. The
  3379. list is chained by NEXT. */
  3380. ira_allocno_t first;
  3381. ira_allocno_t next;
  3382. int temp;
  3383. };
  3384. /* Container for storing allocno data concerning coalescing. */
  3385. static coalesce_data_t allocno_coalesce_data;
  3386. /* Macro to access the data concerning coalescing. */
  3387. #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
  3388. /* Merge two sets of coalesced allocnos given correspondingly by
  3389. allocnos A1 and A2 (more accurately merging A2 set into A1
  3390. set). */
  3391. static void
  3392. merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
  3393. {
  3394. ira_allocno_t a, first, last, next;
  3395. first = ALLOCNO_COALESCE_DATA (a1)->first;
  3396. a = ALLOCNO_COALESCE_DATA (a2)->first;
  3397. if (first == a)
  3398. return;
  3399. for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
  3400. a = ALLOCNO_COALESCE_DATA (a)->next)
  3401. {
  3402. ALLOCNO_COALESCE_DATA (a)->first = first;
  3403. if (a == a2)
  3404. break;
  3405. last = a;
  3406. }
  3407. next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
  3408. allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
  3409. allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
  3410. }
  3411. /* Return TRUE if there are conflicting allocnos from two sets of
  3412. coalesced allocnos given correspondingly by allocnos A1 and A2. We
  3413. use live ranges to find conflicts because conflicts are represented
  3414. only for allocnos of the same allocno class and during the reload
  3415. pass we coalesce allocnos for sharing stack memory slots. */
  3416. static bool
  3417. coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
  3418. {
  3419. ira_allocno_t a, conflict_a;
  3420. if (allocno_coalesced_p)
  3421. {
  3422. bitmap_clear (processed_coalesced_allocno_bitmap);
  3423. for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
  3424. a = ALLOCNO_COALESCE_DATA (a)->next)
  3425. {
  3426. bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
  3427. if (a == a1)
  3428. break;
  3429. }
  3430. }
  3431. for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
  3432. a = ALLOCNO_COALESCE_DATA (a)->next)
  3433. {
  3434. for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
  3435. conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
  3436. {
  3437. if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
  3438. return true;
  3439. if (conflict_a == a1)
  3440. break;
  3441. }
  3442. if (a == a2)
  3443. break;
  3444. }
  3445. return false;
  3446. }
  3447. /* The major function for aggressive allocno coalescing. We coalesce
  3448. only spilled allocnos. If some allocnos have been coalesced, we
  3449. set up flag allocno_coalesced_p. */
  3450. static void
  3451. coalesce_allocnos (void)
  3452. {
  3453. ira_allocno_t a;
  3454. ira_copy_t cp, next_cp;
  3455. unsigned int j;
  3456. int i, n, cp_num, regno;
  3457. bitmap_iterator bi;
  3458. cp_num = 0;
  3459. /* Collect copies. */
  3460. EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
  3461. {
  3462. a = ira_allocnos[j];
  3463. regno = ALLOCNO_REGNO (a);
  3464. if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
  3465. || ira_equiv_no_lvalue_p (regno))
  3466. continue;
  3467. for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
  3468. {
  3469. if (cp->first == a)
  3470. {
  3471. next_cp = cp->next_first_allocno_copy;
  3472. regno = ALLOCNO_REGNO (cp->second);
  3473. /* For priority coloring we coalesce allocnos only with
  3474. the same allocno class not with intersected allocno
  3475. classes as it were possible. It is done for
  3476. simplicity. */
  3477. if ((cp->insn != NULL || cp->constraint_p)
  3478. && ALLOCNO_ASSIGNED_P (cp->second)
  3479. && ALLOCNO_HARD_REGNO (cp->second) < 0
  3480. && ! ira_equiv_no_lvalue_p (regno))
  3481. sorted_copies[cp_num++] = cp;
  3482. }
  3483. else if (cp->second == a)
  3484. next_cp = cp->next_second_allocno_copy;
  3485. else
  3486. gcc_unreachable ();
  3487. }
  3488. }
  3489. qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
  3490. /* Coalesced copies, most frequently executed first. */
  3491. for (; cp_num != 0;)
  3492. {
  3493. for (i = 0; i < cp_num; i++)
  3494. {
  3495. cp = sorted_copies[i];
  3496. if (! coalesced_allocno_conflict_p (cp->first, cp->second))
  3497. {
  3498. allocno_coalesced_p = true;
  3499. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  3500. fprintf
  3501. (ira_dump_file,
  3502. " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
  3503. cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
  3504. ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
  3505. cp->freq);
  3506. merge_allocnos (cp->first, cp->second);
  3507. i++;
  3508. break;
  3509. }
  3510. }
  3511. /* Collect the rest of copies. */
  3512. for (n = 0; i < cp_num; i++)
  3513. {
  3514. cp = sorted_copies[i];
  3515. if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
  3516. != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
  3517. sorted_copies[n++] = cp;
  3518. }
  3519. cp_num = n;
  3520. }
  3521. }
  3522. /* Usage cost and order number of coalesced allocno set to which
  3523. given pseudo register belongs to. */
  3524. static int *regno_coalesced_allocno_cost;
  3525. static int *regno_coalesced_allocno_num;
  3526. /* Sort pseudos according frequencies of coalesced allocno sets they
  3527. belong to (putting most frequently ones first), and according to
  3528. coalesced allocno set order numbers. */
  3529. static int
  3530. coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
  3531. {
  3532. const int regno1 = *(const int *) v1p;
  3533. const int regno2 = *(const int *) v2p;
  3534. int diff;
  3535. if ((diff = (regno_coalesced_allocno_cost[regno2]
  3536. - regno_coalesced_allocno_cost[regno1])) != 0)
  3537. return diff;
  3538. if ((diff = (regno_coalesced_allocno_num[regno1]
  3539. - regno_coalesced_allocno_num[regno2])) != 0)
  3540. return diff;
  3541. return regno1 - regno2;
  3542. }
  3543. /* Widest width in which each pseudo reg is referred to (via subreg).
  3544. It is used for sorting pseudo registers. */
  3545. static unsigned int *regno_max_ref_width;
  3546. /* Redefine STACK_GROWS_DOWNWARD in terms of 0 or 1. */
  3547. #ifdef STACK_GROWS_DOWNWARD
  3548. # undef STACK_GROWS_DOWNWARD
  3549. # define STACK_GROWS_DOWNWARD 1
  3550. #else
  3551. # define STACK_GROWS_DOWNWARD 0
  3552. #endif
  3553. /* Sort pseudos according their slot numbers (putting ones with
  3554. smaller numbers first, or last when the frame pointer is not
  3555. needed). */
  3556. static int
  3557. coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
  3558. {
  3559. const int regno1 = *(const int *) v1p;
  3560. const int regno2 = *(const int *) v2p;
  3561. ira_allocno_t a1 = ira_regno_allocno_map[regno1];
  3562. ira_allocno_t a2 = ira_regno_allocno_map[regno2];
  3563. int diff, slot_num1, slot_num2;
  3564. int total_size1, total_size2;
  3565. if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
  3566. {
  3567. if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
  3568. return regno1 - regno2;
  3569. return 1;
  3570. }
  3571. else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
  3572. return -1;
  3573. slot_num1 = -ALLOCNO_HARD_REGNO (a1);
  3574. slot_num2 = -ALLOCNO_HARD_REGNO (a2);
  3575. if ((diff = slot_num1 - slot_num2) != 0)
  3576. return (frame_pointer_needed
  3577. || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
  3578. total_size1 = MAX (PSEUDO_REGNO_BYTES (regno1),
  3579. regno_max_ref_width[regno1]);
  3580. total_size2 = MAX (PSEUDO_REGNO_BYTES (regno2),
  3581. regno_max_ref_width[regno2]);
  3582. if ((diff = total_size2 - total_size1) != 0)
  3583. return diff;
  3584. return regno1 - regno2;
  3585. }
  3586. /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
  3587. for coalesced allocno sets containing allocnos with their regnos
  3588. given in array PSEUDO_REGNOS of length N. */
  3589. static void
  3590. setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
  3591. {
  3592. int i, num, regno, cost;
  3593. ira_allocno_t allocno, a;
  3594. for (num = i = 0; i < n; i++)
  3595. {
  3596. regno = pseudo_regnos[i];
  3597. allocno = ira_regno_allocno_map[regno];
  3598. if (allocno == NULL)
  3599. {
  3600. regno_coalesced_allocno_cost[regno] = 0;
  3601. regno_coalesced_allocno_num[regno] = ++num;
  3602. continue;
  3603. }
  3604. if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
  3605. continue;
  3606. num++;
  3607. for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
  3608. a = ALLOCNO_COALESCE_DATA (a)->next)
  3609. {
  3610. cost += ALLOCNO_FREQ (a);
  3611. if (a == allocno)
  3612. break;
  3613. }
  3614. for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
  3615. a = ALLOCNO_COALESCE_DATA (a)->next)
  3616. {
  3617. regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
  3618. regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
  3619. if (a == allocno)
  3620. break;
  3621. }
  3622. }
  3623. }
  3624. /* Collect spilled allocnos representing coalesced allocno sets (the
  3625. first coalesced allocno). The collected allocnos are returned
  3626. through array SPILLED_COALESCED_ALLOCNOS. The function returns the
  3627. number of the collected allocnos. The allocnos are given by their
  3628. regnos in array PSEUDO_REGNOS of length N. */
  3629. static int
  3630. collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
  3631. ira_allocno_t *spilled_coalesced_allocnos)
  3632. {
  3633. int i, num, regno;
  3634. ira_allocno_t allocno;
  3635. for (num = i = 0; i < n; i++)
  3636. {
  3637. regno = pseudo_regnos[i];
  3638. allocno = ira_regno_allocno_map[regno];
  3639. if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
  3640. || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
  3641. continue;
  3642. spilled_coalesced_allocnos[num++] = allocno;
  3643. }
  3644. return num;
  3645. }
  3646. /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
  3647. given slot contains live ranges of coalesced allocnos assigned to
  3648. given slot. */
  3649. static live_range_t *slot_coalesced_allocnos_live_ranges;
  3650. /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
  3651. ranges intersected with live ranges of coalesced allocnos assigned
  3652. to slot with number N. */
  3653. static bool
  3654. slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
  3655. {
  3656. ira_allocno_t a;
  3657. for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
  3658. a = ALLOCNO_COALESCE_DATA (a)->next)
  3659. {
  3660. int i;
  3661. int nr = ALLOCNO_NUM_OBJECTS (a);
  3662. for (i = 0; i < nr; i++)
  3663. {
  3664. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  3665. if (ira_live_ranges_intersect_p
  3666. (slot_coalesced_allocnos_live_ranges[n],
  3667. OBJECT_LIVE_RANGES (obj)))
  3668. return true;
  3669. }
  3670. if (a == allocno)
  3671. break;
  3672. }
  3673. return false;
  3674. }
  3675. /* Update live ranges of slot to which coalesced allocnos represented
  3676. by ALLOCNO were assigned. */
  3677. static void
  3678. setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
  3679. {
  3680. int i, n;
  3681. ira_allocno_t a;
  3682. live_range_t r;
  3683. n = ALLOCNO_COALESCE_DATA (allocno)->temp;
  3684. for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
  3685. a = ALLOCNO_COALESCE_DATA (a)->next)
  3686. {
  3687. int nr = ALLOCNO_NUM_OBJECTS (a);
  3688. for (i = 0; i < nr; i++)
  3689. {
  3690. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  3691. r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
  3692. slot_coalesced_allocnos_live_ranges[n]
  3693. = ira_merge_live_ranges
  3694. (slot_coalesced_allocnos_live_ranges[n], r);
  3695. }
  3696. if (a == allocno)
  3697. break;
  3698. }
  3699. }
  3700. /* We have coalesced allocnos involving in copies. Coalesce allocnos
  3701. further in order to share the same memory stack slot. Allocnos
  3702. representing sets of allocnos coalesced before the call are given
  3703. in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
  3704. some allocnos were coalesced in the function. */
  3705. static bool
  3706. coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
  3707. {
  3708. int i, j, n, last_coalesced_allocno_num;
  3709. ira_allocno_t allocno, a;
  3710. bool merged_p = false;
  3711. bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
  3712. slot_coalesced_allocnos_live_ranges
  3713. = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
  3714. memset (slot_coalesced_allocnos_live_ranges, 0,
  3715. sizeof (live_range_t) * ira_allocnos_num);
  3716. last_coalesced_allocno_num = 0;
  3717. /* Coalesce non-conflicting spilled allocnos preferring most
  3718. frequently used. */
  3719. for (i = 0; i < num; i++)
  3720. {
  3721. allocno = spilled_coalesced_allocnos[i];
  3722. if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
  3723. || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
  3724. || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
  3725. continue;
  3726. for (j = 0; j < i; j++)
  3727. {
  3728. a = spilled_coalesced_allocnos[j];
  3729. n = ALLOCNO_COALESCE_DATA (a)->temp;
  3730. if (ALLOCNO_COALESCE_DATA (a)->first == a
  3731. && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
  3732. && ! ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a))
  3733. && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
  3734. break;
  3735. }
  3736. if (j >= i)
  3737. {
  3738. /* No coalescing: set up number for coalesced allocnos
  3739. represented by ALLOCNO. */
  3740. ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
  3741. setup_slot_coalesced_allocno_live_ranges (allocno);
  3742. }
  3743. else
  3744. {
  3745. allocno_coalesced_p = true;
  3746. merged_p = true;
  3747. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  3748. fprintf (ira_dump_file,
  3749. " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
  3750. ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
  3751. ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
  3752. ALLOCNO_COALESCE_DATA (allocno)->temp
  3753. = ALLOCNO_COALESCE_DATA (a)->temp;
  3754. setup_slot_coalesced_allocno_live_ranges (allocno);
  3755. merge_allocnos (a, allocno);
  3756. ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
  3757. }
  3758. }
  3759. for (i = 0; i < ira_allocnos_num; i++)
  3760. ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
  3761. ira_free (slot_coalesced_allocnos_live_ranges);
  3762. return merged_p;
  3763. }
  3764. /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
  3765. subsequent assigning stack slots to them in the reload pass. To do
  3766. this we coalesce spilled allocnos first to decrease the number of
  3767. memory-memory move insns. This function is called by the
  3768. reload. */
  3769. void
  3770. ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
  3771. unsigned int *reg_max_ref_width)
  3772. {
  3773. int max_regno = max_reg_num ();
  3774. int i, regno, num, slot_num;
  3775. ira_allocno_t allocno, a;
  3776. ira_allocno_iterator ai;
  3777. ira_allocno_t *spilled_coalesced_allocnos;
  3778. ira_assert (! ira_use_lra_p);
  3779. /* Set up allocnos can be coalesced. */
  3780. coloring_allocno_bitmap = ira_allocate_bitmap ();
  3781. for (i = 0; i < n; i++)
  3782. {
  3783. regno = pseudo_regnos[i];
  3784. allocno = ira_regno_allocno_map[regno];
  3785. if (allocno != NULL)
  3786. bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
  3787. }
  3788. allocno_coalesced_p = false;
  3789. processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
  3790. allocno_coalesce_data
  3791. = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
  3792. * ira_allocnos_num);
  3793. /* Initialize coalesce data for allocnos. */
  3794. FOR_EACH_ALLOCNO (a, ai)
  3795. {
  3796. ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
  3797. ALLOCNO_COALESCE_DATA (a)->first = a;
  3798. ALLOCNO_COALESCE_DATA (a)->next = a;
  3799. }
  3800. coalesce_allocnos ();
  3801. ira_free_bitmap (coloring_allocno_bitmap);
  3802. regno_coalesced_allocno_cost
  3803. = (int *) ira_allocate (max_regno * sizeof (int));
  3804. regno_coalesced_allocno_num
  3805. = (int *) ira_allocate (max_regno * sizeof (int));
  3806. memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
  3807. setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
  3808. /* Sort regnos according frequencies of the corresponding coalesced
  3809. allocno sets. */
  3810. qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
  3811. spilled_coalesced_allocnos
  3812. = (ira_allocno_t *) ira_allocate (ira_allocnos_num
  3813. * sizeof (ira_allocno_t));
  3814. /* Collect allocnos representing the spilled coalesced allocno
  3815. sets. */
  3816. num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
  3817. spilled_coalesced_allocnos);
  3818. if (flag_ira_share_spill_slots
  3819. && coalesce_spill_slots (spilled_coalesced_allocnos, num))
  3820. {
  3821. setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
  3822. qsort (pseudo_regnos, n, sizeof (int),
  3823. coalesced_pseudo_reg_freq_compare);
  3824. num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
  3825. spilled_coalesced_allocnos);
  3826. }
  3827. ira_free_bitmap (processed_coalesced_allocno_bitmap);
  3828. allocno_coalesced_p = false;
  3829. /* Assign stack slot numbers to spilled allocno sets, use smaller
  3830. numbers for most frequently used coalesced allocnos. -1 is
  3831. reserved for dynamic search of stack slots for pseudos spilled by
  3832. the reload. */
  3833. slot_num = 1;
  3834. for (i = 0; i < num; i++)
  3835. {
  3836. allocno = spilled_coalesced_allocnos[i];
  3837. if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
  3838. || ALLOCNO_HARD_REGNO (allocno) >= 0
  3839. || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
  3840. continue;
  3841. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  3842. fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
  3843. slot_num++;
  3844. for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
  3845. a = ALLOCNO_COALESCE_DATA (a)->next)
  3846. {
  3847. ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
  3848. ALLOCNO_HARD_REGNO (a) = -slot_num;
  3849. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  3850. fprintf (ira_dump_file, " a%dr%d(%d,%d)",
  3851. ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a),
  3852. MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a)),
  3853. reg_max_ref_width[ALLOCNO_REGNO (a)]));
  3854. if (a == allocno)
  3855. break;
  3856. }
  3857. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  3858. fprintf (ira_dump_file, "\n");
  3859. }
  3860. ira_spilled_reg_stack_slots_num = slot_num - 1;
  3861. ira_free (spilled_coalesced_allocnos);
  3862. /* Sort regnos according the slot numbers. */
  3863. regno_max_ref_width = reg_max_ref_width;
  3864. qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
  3865. FOR_EACH_ALLOCNO (a, ai)
  3866. ALLOCNO_ADD_DATA (a) = NULL;
  3867. ira_free (allocno_coalesce_data);
  3868. ira_free (regno_coalesced_allocno_num);
  3869. ira_free (regno_coalesced_allocno_cost);
  3870. }
  3871. /* This page contains code used by the reload pass to improve the
  3872. final code. */
  3873. /* The function is called from reload to mark changes in the
  3874. allocation of REGNO made by the reload. Remember that reg_renumber
  3875. reflects the change result. */
  3876. void
  3877. ira_mark_allocation_change (int regno)
  3878. {
  3879. ira_allocno_t a = ira_regno_allocno_map[regno];
  3880. int old_hard_regno, hard_regno, cost;
  3881. enum reg_class aclass = ALLOCNO_CLASS (a);
  3882. ira_assert (a != NULL);
  3883. hard_regno = reg_renumber[regno];
  3884. if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
  3885. return;
  3886. if (old_hard_regno < 0)
  3887. cost = -ALLOCNO_MEMORY_COST (a);
  3888. else
  3889. {
  3890. ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
  3891. cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
  3892. ? ALLOCNO_CLASS_COST (a)
  3893. : ALLOCNO_HARD_REG_COSTS (a)
  3894. [ira_class_hard_reg_index[aclass][old_hard_regno]]);
  3895. update_costs_from_copies (a, false, false);
  3896. }
  3897. ira_overall_cost -= cost;
  3898. ALLOCNO_HARD_REGNO (a) = hard_regno;
  3899. if (hard_regno < 0)
  3900. {
  3901. ALLOCNO_HARD_REGNO (a) = -1;
  3902. cost += ALLOCNO_MEMORY_COST (a);
  3903. }
  3904. else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
  3905. {
  3906. cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
  3907. ? ALLOCNO_CLASS_COST (a)
  3908. : ALLOCNO_HARD_REG_COSTS (a)
  3909. [ira_class_hard_reg_index[aclass][hard_regno]]);
  3910. update_costs_from_copies (a, true, false);
  3911. }
  3912. else
  3913. /* Reload changed class of the allocno. */
  3914. cost = 0;
  3915. ira_overall_cost += cost;
  3916. }
  3917. /* This function is called when reload deletes memory-memory move. In
  3918. this case we marks that the allocation of the corresponding
  3919. allocnos should be not changed in future. Otherwise we risk to get
  3920. a wrong code. */
  3921. void
  3922. ira_mark_memory_move_deletion (int dst_regno, int src_regno)
  3923. {
  3924. ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
  3925. ira_allocno_t src = ira_regno_allocno_map[src_regno];
  3926. ira_assert (dst != NULL && src != NULL
  3927. && ALLOCNO_HARD_REGNO (dst) < 0
  3928. && ALLOCNO_HARD_REGNO (src) < 0);
  3929. ALLOCNO_DONT_REASSIGN_P (dst) = true;
  3930. ALLOCNO_DONT_REASSIGN_P (src) = true;
  3931. }
  3932. /* Try to assign a hard register (except for FORBIDDEN_REGS) to
  3933. allocno A and return TRUE in the case of success. */
  3934. static bool
  3935. allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
  3936. {
  3937. int hard_regno;
  3938. enum reg_class aclass;
  3939. int regno = ALLOCNO_REGNO (a);
  3940. HARD_REG_SET saved[2];
  3941. int i, n;
  3942. n = ALLOCNO_NUM_OBJECTS (a);
  3943. for (i = 0; i < n; i++)
  3944. {
  3945. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  3946. COPY_HARD_REG_SET (saved[i], OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
  3947. IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), forbidden_regs);
  3948. if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
  3949. IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
  3950. call_used_reg_set);
  3951. }
  3952. ALLOCNO_ASSIGNED_P (a) = false;
  3953. aclass = ALLOCNO_CLASS (a);
  3954. update_curr_costs (a);
  3955. assign_hard_reg (a, true);
  3956. hard_regno = ALLOCNO_HARD_REGNO (a);
  3957. reg_renumber[regno] = hard_regno;
  3958. if (hard_regno < 0)
  3959. ALLOCNO_HARD_REGNO (a) = -1;
  3960. else
  3961. {
  3962. ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
  3963. ira_overall_cost
  3964. -= (ALLOCNO_MEMORY_COST (a)
  3965. - (ALLOCNO_HARD_REG_COSTS (a) == NULL
  3966. ? ALLOCNO_CLASS_COST (a)
  3967. : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
  3968. [aclass][hard_regno]]));
  3969. if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
  3970. && ira_hard_reg_set_intersection_p (hard_regno, ALLOCNO_MODE (a),
  3971. call_used_reg_set))
  3972. {
  3973. ira_assert (flag_caller_saves);
  3974. caller_save_needed = 1;
  3975. }
  3976. }
  3977. /* If we found a hard register, modify the RTL for the pseudo
  3978. register to show the hard register, and mark the pseudo register
  3979. live. */
  3980. if (reg_renumber[regno] >= 0)
  3981. {
  3982. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  3983. fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
  3984. SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
  3985. mark_home_live (regno);
  3986. }
  3987. else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  3988. fprintf (ira_dump_file, "\n");
  3989. for (i = 0; i < n; i++)
  3990. {
  3991. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  3992. COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), saved[i]);
  3993. }
  3994. return reg_renumber[regno] >= 0;
  3995. }
  3996. /* Sort pseudos according their usage frequencies (putting most
  3997. frequently ones first). */
  3998. static int
  3999. pseudo_reg_compare (const void *v1p, const void *v2p)
  4000. {
  4001. int regno1 = *(const int *) v1p;
  4002. int regno2 = *(const int *) v2p;
  4003. int diff;
  4004. if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
  4005. return diff;
  4006. return regno1 - regno2;
  4007. }
  4008. /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
  4009. NUM of them) or spilled pseudos conflicting with pseudos in
  4010. SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
  4011. allocation has been changed. The function doesn't use
  4012. BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
  4013. PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
  4014. is called by the reload pass at the end of each reload
  4015. iteration. */
  4016. bool
  4017. ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
  4018. HARD_REG_SET bad_spill_regs,
  4019. HARD_REG_SET *pseudo_forbidden_regs,
  4020. HARD_REG_SET *pseudo_previous_regs,
  4021. bitmap spilled)
  4022. {
  4023. int i, n, regno;
  4024. bool changed_p;
  4025. ira_allocno_t a;
  4026. HARD_REG_SET forbidden_regs;
  4027. bitmap temp = BITMAP_ALLOC (NULL);
  4028. /* Add pseudos which conflict with pseudos already in
  4029. SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
  4030. to allocating in two steps as some of the conflicts might have
  4031. a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
  4032. for (i = 0; i < num; i++)
  4033. bitmap_set_bit (temp, spilled_pseudo_regs[i]);
  4034. for (i = 0, n = num; i < n; i++)
  4035. {
  4036. int nr, j;
  4037. int regno = spilled_pseudo_regs[i];
  4038. bitmap_set_bit (temp, regno);
  4039. a = ira_regno_allocno_map[regno];
  4040. nr = ALLOCNO_NUM_OBJECTS (a);
  4041. for (j = 0; j < nr; j++)
  4042. {
  4043. ira_object_t conflict_obj;
  4044. ira_object_t obj = ALLOCNO_OBJECT (a, j);
  4045. ira_object_conflict_iterator oci;
  4046. FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
  4047. {
  4048. ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
  4049. if (ALLOCNO_HARD_REGNO (conflict_a) < 0
  4050. && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
  4051. && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
  4052. {
  4053. spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
  4054. /* ?!? This seems wrong. */
  4055. bitmap_set_bit (consideration_allocno_bitmap,
  4056. ALLOCNO_NUM (conflict_a));
  4057. }
  4058. }
  4059. }
  4060. }
  4061. if (num > 1)
  4062. qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
  4063. changed_p = false;
  4064. /* Try to assign hard registers to pseudos from
  4065. SPILLED_PSEUDO_REGS. */
  4066. for (i = 0; i < num; i++)
  4067. {
  4068. regno = spilled_pseudo_regs[i];
  4069. COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
  4070. IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
  4071. IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
  4072. gcc_assert (reg_renumber[regno] < 0);
  4073. a = ira_regno_allocno_map[regno];
  4074. ira_mark_allocation_change (regno);
  4075. ira_assert (reg_renumber[regno] < 0);
  4076. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  4077. fprintf (ira_dump_file,
  4078. " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
  4079. ALLOCNO_MEMORY_COST (a)
  4080. - ALLOCNO_CLASS_COST (a));
  4081. allocno_reload_assign (a, forbidden_regs);
  4082. if (reg_renumber[regno] >= 0)
  4083. {
  4084. CLEAR_REGNO_REG_SET (spilled, regno);
  4085. changed_p = true;
  4086. }
  4087. }
  4088. BITMAP_FREE (temp);
  4089. return changed_p;
  4090. }
  4091. /* The function is called by reload and returns already allocated
  4092. stack slot (if any) for REGNO with given INHERENT_SIZE and
  4093. TOTAL_SIZE. In the case of failure to find a slot which can be
  4094. used for REGNO, the function returns NULL. */
  4095. rtx
  4096. ira_reuse_stack_slot (int regno, unsigned int inherent_size,
  4097. unsigned int total_size)
  4098. {
  4099. unsigned int i;
  4100. int slot_num, best_slot_num;
  4101. int cost, best_cost;
  4102. ira_copy_t cp, next_cp;
  4103. ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
  4104. rtx x;
  4105. bitmap_iterator bi;
  4106. struct ira_spilled_reg_stack_slot *slot = NULL;
  4107. ira_assert (! ira_use_lra_p);
  4108. ira_assert (inherent_size == PSEUDO_REGNO_BYTES (regno)
  4109. && inherent_size <= total_size
  4110. && ALLOCNO_HARD_REGNO (allocno) < 0);
  4111. if (! flag_ira_share_spill_slots)
  4112. return NULL_RTX;
  4113. slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
  4114. if (slot_num != -1)
  4115. {
  4116. slot = &ira_spilled_reg_stack_slots[slot_num];
  4117. x = slot->mem;
  4118. }
  4119. else
  4120. {
  4121. best_cost = best_slot_num = -1;
  4122. x = NULL_RTX;
  4123. /* It means that the pseudo was spilled in the reload pass, try
  4124. to reuse a slot. */
  4125. for (slot_num = 0;
  4126. slot_num < ira_spilled_reg_stack_slots_num;
  4127. slot_num++)
  4128. {
  4129. slot = &ira_spilled_reg_stack_slots[slot_num];
  4130. if (slot->mem == NULL_RTX)
  4131. continue;
  4132. if (slot->width < total_size
  4133. || GET_MODE_SIZE (GET_MODE (slot->mem)) < inherent_size)
  4134. continue;
  4135. EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
  4136. FIRST_PSEUDO_REGISTER, i, bi)
  4137. {
  4138. another_allocno = ira_regno_allocno_map[i];
  4139. if (allocnos_conflict_by_live_ranges_p (allocno,
  4140. another_allocno))
  4141. goto cont;
  4142. }
  4143. for (cost = 0, cp = ALLOCNO_COPIES (allocno);
  4144. cp != NULL;
  4145. cp = next_cp)
  4146. {
  4147. if (cp->first == allocno)
  4148. {
  4149. next_cp = cp->next_first_allocno_copy;
  4150. another_allocno = cp->second;
  4151. }
  4152. else if (cp->second == allocno)
  4153. {
  4154. next_cp = cp->next_second_allocno_copy;
  4155. another_allocno = cp->first;
  4156. }
  4157. else
  4158. gcc_unreachable ();
  4159. if (cp->insn == NULL_RTX)
  4160. continue;
  4161. if (bitmap_bit_p (&slot->spilled_regs,
  4162. ALLOCNO_REGNO (another_allocno)))
  4163. cost += cp->freq;
  4164. }
  4165. if (cost > best_cost)
  4166. {
  4167. best_cost = cost;
  4168. best_slot_num = slot_num;
  4169. }
  4170. cont:
  4171. ;
  4172. }
  4173. if (best_cost >= 0)
  4174. {
  4175. slot_num = best_slot_num;
  4176. slot = &ira_spilled_reg_stack_slots[slot_num];
  4177. SET_REGNO_REG_SET (&slot->spilled_regs, regno);
  4178. x = slot->mem;
  4179. ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
  4180. }
  4181. }
  4182. if (x != NULL_RTX)
  4183. {
  4184. ira_assert (slot->width >= total_size);
  4185. #ifdef ENABLE_IRA_CHECKING
  4186. EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
  4187. FIRST_PSEUDO_REGISTER, i, bi)
  4188. {
  4189. ira_assert (! conflict_by_live_ranges_p (regno, i));
  4190. }
  4191. #endif
  4192. SET_REGNO_REG_SET (&slot->spilled_regs, regno);
  4193. if (internal_flag_ira_verbose > 3 && ira_dump_file)
  4194. {
  4195. fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
  4196. regno, REG_FREQ (regno), slot_num);
  4197. EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
  4198. FIRST_PSEUDO_REGISTER, i, bi)
  4199. {
  4200. if ((unsigned) regno != i)
  4201. fprintf (ira_dump_file, " %d", i);
  4202. }
  4203. fprintf (ira_dump_file, "\n");
  4204. }
  4205. }
  4206. return x;
  4207. }
  4208. /* This is called by reload every time a new stack slot X with
  4209. TOTAL_SIZE was allocated for REGNO. We store this info for
  4210. subsequent ira_reuse_stack_slot calls. */
  4211. void
  4212. ira_mark_new_stack_slot (rtx x, int regno, unsigned int total_size)
  4213. {
  4214. struct ira_spilled_reg_stack_slot *slot;
  4215. int slot_num;
  4216. ira_allocno_t allocno;
  4217. ira_assert (! ira_use_lra_p);
  4218. ira_assert (PSEUDO_REGNO_BYTES (regno) <= total_size);
  4219. allocno = ira_regno_allocno_map[regno];
  4220. slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
  4221. if (slot_num == -1)
  4222. {
  4223. slot_num = ira_spilled_reg_stack_slots_num++;
  4224. ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
  4225. }
  4226. slot = &ira_spilled_reg_stack_slots[slot_num];
  4227. INIT_REG_SET (&slot->spilled_regs);
  4228. SET_REGNO_REG_SET (&slot->spilled_regs, regno);
  4229. slot->mem = x;
  4230. slot->width = total_size;
  4231. if (internal_flag_ira_verbose > 3 && ira_dump_file)
  4232. fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
  4233. regno, REG_FREQ (regno), slot_num);
  4234. }
  4235. /* Return spill cost for pseudo-registers whose numbers are in array
  4236. REGNOS (with a negative number as an end marker) for reload with
  4237. given IN and OUT for INSN. Return also number points (through
  4238. EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
  4239. the register pressure is high, number of references of the
  4240. pseudo-registers (through NREFS), number of callee-clobbered
  4241. hard-registers occupied by the pseudo-registers (through
  4242. CALL_USED_COUNT), and the first hard regno occupied by the
  4243. pseudo-registers (through FIRST_HARD_REGNO). */
  4244. static int
  4245. calculate_spill_cost (int *regnos, rtx in, rtx out, rtx insn,
  4246. int *excess_pressure_live_length,
  4247. int *nrefs, int *call_used_count, int *first_hard_regno)
  4248. {
  4249. int i, cost, regno, hard_regno, j, count, saved_cost, nregs;
  4250. bool in_p, out_p;
  4251. int length;
  4252. ira_allocno_t a;
  4253. *nrefs = 0;
  4254. for (length = count = cost = i = 0;; i++)
  4255. {
  4256. regno = regnos[i];
  4257. if (regno < 0)
  4258. break;
  4259. *nrefs += REG_N_REFS (regno);
  4260. hard_regno = reg_renumber[regno];
  4261. ira_assert (hard_regno >= 0);
  4262. a = ira_regno_allocno_map[regno];
  4263. length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
  4264. cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
  4265. nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
  4266. for (j = 0; j < nregs; j++)
  4267. if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j))
  4268. break;
  4269. if (j == nregs)
  4270. count++;
  4271. in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
  4272. out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
  4273. if ((in_p || out_p)
  4274. && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
  4275. {
  4276. saved_cost = 0;
  4277. if (in_p)
  4278. saved_cost += ira_memory_move_cost
  4279. [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
  4280. if (out_p)
  4281. saved_cost
  4282. += ira_memory_move_cost
  4283. [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
  4284. cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
  4285. }
  4286. }
  4287. *excess_pressure_live_length = length;
  4288. *call_used_count = count;
  4289. hard_regno = -1;
  4290. if (regnos[0] >= 0)
  4291. {
  4292. hard_regno = reg_renumber[regnos[0]];
  4293. }
  4294. *first_hard_regno = hard_regno;
  4295. return cost;
  4296. }
  4297. /* Return TRUE if spilling pseudo-registers whose numbers are in array
  4298. REGNOS is better than spilling pseudo-registers with numbers in
  4299. OTHER_REGNOS for reload with given IN and OUT for INSN. The
  4300. function used by the reload pass to make better register spilling
  4301. decisions. */
  4302. bool
  4303. ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
  4304. rtx in, rtx out, rtx insn)
  4305. {
  4306. int cost, other_cost;
  4307. int length, other_length;
  4308. int nrefs, other_nrefs;
  4309. int call_used_count, other_call_used_count;
  4310. int hard_regno, other_hard_regno;
  4311. cost = calculate_spill_cost (regnos, in, out, insn,
  4312. &length, &nrefs, &call_used_count, &hard_regno);
  4313. other_cost = calculate_spill_cost (other_regnos, in, out, insn,
  4314. &other_length, &other_nrefs,
  4315. &other_call_used_count,
  4316. &other_hard_regno);
  4317. if (nrefs == 0 && other_nrefs != 0)
  4318. return true;
  4319. if (nrefs != 0 && other_nrefs == 0)
  4320. return false;
  4321. if (cost != other_cost)
  4322. return cost < other_cost;
  4323. if (length != other_length)
  4324. return length > other_length;
  4325. #ifdef REG_ALLOC_ORDER
  4326. if (hard_regno >= 0 && other_hard_regno >= 0)
  4327. return (inv_reg_alloc_order[hard_regno]
  4328. < inv_reg_alloc_order[other_hard_regno]);
  4329. #else
  4330. if (call_used_count != other_call_used_count)
  4331. return call_used_count > other_call_used_count;
  4332. #endif
  4333. return false;
  4334. }
  4335. /* Allocate and initialize data necessary for assign_hard_reg. */
  4336. void
  4337. ira_initiate_assign (void)
  4338. {
  4339. sorted_allocnos
  4340. = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
  4341. * ira_allocnos_num);
  4342. consideration_allocno_bitmap = ira_allocate_bitmap ();
  4343. initiate_cost_update ();
  4344. allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
  4345. sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
  4346. * sizeof (ira_copy_t));
  4347. }
  4348. /* Deallocate data used by assign_hard_reg. */
  4349. void
  4350. ira_finish_assign (void)
  4351. {
  4352. ira_free (sorted_allocnos);
  4353. ira_free_bitmap (consideration_allocno_bitmap);
  4354. finish_cost_update ();
  4355. ira_free (allocno_priorities);
  4356. ira_free (sorted_copies);
  4357. }
  4358. /* Entry function doing color-based register allocation. */
  4359. static void
  4360. color (void)
  4361. {
  4362. allocno_stack_vec.create (ira_allocnos_num);
  4363. memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
  4364. ira_initiate_assign ();
  4365. do_coloring ();
  4366. ira_finish_assign ();
  4367. allocno_stack_vec.release ();
  4368. move_spill_restore ();
  4369. }
  4370. /* This page contains a simple register allocator without usage of
  4371. allocno conflicts. This is used for fast allocation for -O0. */
  4372. /* Do register allocation by not using allocno conflicts. It uses
  4373. only allocno live ranges. The algorithm is close to Chow's
  4374. priority coloring. */
  4375. static void
  4376. fast_allocation (void)
  4377. {
  4378. int i, j, k, num, class_size, hard_regno;
  4379. #ifdef STACK_REGS
  4380. bool no_stack_reg_p;
  4381. #endif
  4382. enum reg_class aclass;
  4383. machine_mode mode;
  4384. ira_allocno_t a;
  4385. ira_allocno_iterator ai;
  4386. live_range_t r;
  4387. HARD_REG_SET conflict_hard_regs, *used_hard_regs;
  4388. sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
  4389. * ira_allocnos_num);
  4390. num = 0;
  4391. FOR_EACH_ALLOCNO (a, ai)
  4392. sorted_allocnos[num++] = a;
  4393. allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
  4394. setup_allocno_priorities (sorted_allocnos, num);
  4395. used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
  4396. * ira_max_point);
  4397. for (i = 0; i < ira_max_point; i++)
  4398. CLEAR_HARD_REG_SET (used_hard_regs[i]);
  4399. qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
  4400. allocno_priority_compare_func);
  4401. for (i = 0; i < num; i++)
  4402. {
  4403. int nr, l;
  4404. a = sorted_allocnos[i];
  4405. nr = ALLOCNO_NUM_OBJECTS (a);
  4406. CLEAR_HARD_REG_SET (conflict_hard_regs);
  4407. for (l = 0; l < nr; l++)
  4408. {
  4409. ira_object_t obj = ALLOCNO_OBJECT (a, l);
  4410. IOR_HARD_REG_SET (conflict_hard_regs,
  4411. OBJECT_CONFLICT_HARD_REGS (obj));
  4412. for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
  4413. for (j = r->start; j <= r->finish; j++)
  4414. IOR_HARD_REG_SET (conflict_hard_regs, used_hard_regs[j]);
  4415. }
  4416. aclass = ALLOCNO_CLASS (a);
  4417. ALLOCNO_ASSIGNED_P (a) = true;
  4418. ALLOCNO_HARD_REGNO (a) = -1;
  4419. if (hard_reg_set_subset_p (reg_class_contents[aclass],
  4420. conflict_hard_regs))
  4421. continue;
  4422. mode = ALLOCNO_MODE (a);
  4423. #ifdef STACK_REGS
  4424. no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
  4425. #endif
  4426. class_size = ira_class_hard_regs_num[aclass];
  4427. for (j = 0; j < class_size; j++)
  4428. {
  4429. hard_regno = ira_class_hard_regs[aclass][j];
  4430. #ifdef STACK_REGS
  4431. if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
  4432. && hard_regno <= LAST_STACK_REG)
  4433. continue;
  4434. #endif
  4435. if (ira_hard_reg_set_intersection_p (hard_regno, mode, conflict_hard_regs)
  4436. || (TEST_HARD_REG_BIT
  4437. (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
  4438. continue;
  4439. ALLOCNO_HARD_REGNO (a) = hard_regno;
  4440. for (l = 0; l < nr; l++)
  4441. {
  4442. ira_object_t obj = ALLOCNO_OBJECT (a, l);
  4443. for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
  4444. for (k = r->start; k <= r->finish; k++)
  4445. IOR_HARD_REG_SET (used_hard_regs[k],
  4446. ira_reg_mode_hard_regset[hard_regno][mode]);
  4447. }
  4448. break;
  4449. }
  4450. }
  4451. ira_free (sorted_allocnos);
  4452. ira_free (used_hard_regs);
  4453. ira_free (allocno_priorities);
  4454. if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
  4455. ira_print_disposition (ira_dump_file);
  4456. }
  4457. /* Entry function doing coloring. */
  4458. void
  4459. ira_color (void)
  4460. {
  4461. ira_allocno_t a;
  4462. ira_allocno_iterator ai;
  4463. /* Setup updated costs. */
  4464. FOR_EACH_ALLOCNO (a, ai)
  4465. {
  4466. ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
  4467. ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
  4468. }
  4469. if (ira_conflicts_p)
  4470. color ();
  4471. else
  4472. fast_allocation ();
  4473. }