ira.c 171 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602
  1. /* Integrated Register Allocator (IRA) entry point.
  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. /* The integrated register allocator (IRA) is a
  17. regional register allocator performing graph coloring on a top-down
  18. traversal of nested regions. Graph coloring in a region is based
  19. on Chaitin-Briggs algorithm. It is called integrated because
  20. register coalescing, register live range splitting, and choosing a
  21. better hard register are done on-the-fly during coloring. Register
  22. coalescing and choosing a cheaper hard register is done by hard
  23. register preferencing during hard register assigning. The live
  24. range splitting is a byproduct of the regional register allocation.
  25. Major IRA notions are:
  26. o *Region* is a part of CFG where graph coloring based on
  27. Chaitin-Briggs algorithm is done. IRA can work on any set of
  28. nested CFG regions forming a tree. Currently the regions are
  29. the entire function for the root region and natural loops for
  30. the other regions. Therefore data structure representing a
  31. region is called loop_tree_node.
  32. o *Allocno class* is a register class used for allocation of
  33. given allocno. It means that only hard register of given
  34. register class can be assigned to given allocno. In reality,
  35. even smaller subset of (*profitable*) hard registers can be
  36. assigned. In rare cases, the subset can be even smaller
  37. because our modification of Chaitin-Briggs algorithm requires
  38. that sets of hard registers can be assigned to allocnos forms a
  39. forest, i.e. the sets can be ordered in a way where any
  40. previous set is not intersected with given set or is a superset
  41. of given set.
  42. o *Pressure class* is a register class belonging to a set of
  43. register classes containing all of the hard-registers available
  44. for register allocation. The set of all pressure classes for a
  45. target is defined in the corresponding machine-description file
  46. according some criteria. Register pressure is calculated only
  47. for pressure classes and it affects some IRA decisions as
  48. forming allocation regions.
  49. o *Allocno* represents the live range of a pseudo-register in a
  50. region. Besides the obvious attributes like the corresponding
  51. pseudo-register number, allocno class, conflicting allocnos and
  52. conflicting hard-registers, there are a few allocno attributes
  53. which are important for understanding the allocation algorithm:
  54. - *Live ranges*. This is a list of ranges of *program points*
  55. where the allocno lives. Program points represent places
  56. where a pseudo can be born or become dead (there are
  57. approximately two times more program points than the insns)
  58. and they are represented by integers starting with 0. The
  59. live ranges are used to find conflicts between allocnos.
  60. They also play very important role for the transformation of
  61. the IRA internal representation of several regions into a one
  62. region representation. The later is used during the reload
  63. pass work because each allocno represents all of the
  64. corresponding pseudo-registers.
  65. - *Hard-register costs*. This is a vector of size equal to the
  66. number of available hard-registers of the allocno class. The
  67. cost of a callee-clobbered hard-register for an allocno is
  68. increased by the cost of save/restore code around the calls
  69. through the given allocno's life. If the allocno is a move
  70. instruction operand and another operand is a hard-register of
  71. the allocno class, the cost of the hard-register is decreased
  72. by the move cost.
  73. When an allocno is assigned, the hard-register with minimal
  74. full cost is used. Initially, a hard-register's full cost is
  75. the corresponding value from the hard-register's cost vector.
  76. If the allocno is connected by a *copy* (see below) to
  77. another allocno which has just received a hard-register, the
  78. cost of the hard-register is decreased. Before choosing a
  79. hard-register for an allocno, the allocno's current costs of
  80. the hard-registers are modified by the conflict hard-register
  81. costs of all of the conflicting allocnos which are not
  82. assigned yet.
  83. - *Conflict hard-register costs*. This is a vector of the same
  84. size as the hard-register costs vector. To permit an
  85. unassigned allocno to get a better hard-register, IRA uses
  86. this vector to calculate the final full cost of the
  87. available hard-registers. Conflict hard-register costs of an
  88. unassigned allocno are also changed with a change of the
  89. hard-register cost of the allocno when a copy involving the
  90. allocno is processed as described above. This is done to
  91. show other unassigned allocnos that a given allocno prefers
  92. some hard-registers in order to remove the move instruction
  93. corresponding to the copy.
  94. o *Cap*. If a pseudo-register does not live in a region but
  95. lives in a nested region, IRA creates a special allocno called
  96. a cap in the outer region. A region cap is also created for a
  97. subregion cap.
  98. o *Copy*. Allocnos can be connected by copies. Copies are used
  99. to modify hard-register costs for allocnos during coloring.
  100. Such modifications reflects a preference to use the same
  101. hard-register for the allocnos connected by copies. Usually
  102. copies are created for move insns (in this case it results in
  103. register coalescing). But IRA also creates copies for operands
  104. of an insn which should be assigned to the same hard-register
  105. due to constraints in the machine description (it usually
  106. results in removing a move generated in reload to satisfy
  107. the constraints) and copies referring to the allocno which is
  108. the output operand of an instruction and the allocno which is
  109. an input operand dying in the instruction (creation of such
  110. copies results in less register shuffling). IRA *does not*
  111. create copies between the same register allocnos from different
  112. regions because we use another technique for propagating
  113. hard-register preference on the borders of regions.
  114. Allocnos (including caps) for the upper region in the region tree
  115. *accumulate* information important for coloring from allocnos with
  116. the same pseudo-register from nested regions. This includes
  117. hard-register and memory costs, conflicts with hard-registers,
  118. allocno conflicts, allocno copies and more. *Thus, attributes for
  119. allocnos in a region have the same values as if the region had no
  120. subregions*. It means that attributes for allocnos in the
  121. outermost region corresponding to the function have the same values
  122. as though the allocation used only one region which is the entire
  123. function. It also means that we can look at IRA work as if the
  124. first IRA did allocation for all function then it improved the
  125. allocation for loops then their subloops and so on.
  126. IRA major passes are:
  127. o Building IRA internal representation which consists of the
  128. following subpasses:
  129. * First, IRA builds regions and creates allocnos (file
  130. ira-build.c) and initializes most of their attributes.
  131. * Then IRA finds an allocno class for each allocno and
  132. calculates its initial (non-accumulated) cost of memory and
  133. each hard-register of its allocno class (file ira-cost.c).
  134. * IRA creates live ranges of each allocno, calculates register
  135. pressure for each pressure class in each region, sets up
  136. conflict hard registers for each allocno and info about calls
  137. the allocno lives through (file ira-lives.c).
  138. * IRA removes low register pressure loops from the regions
  139. mostly to speed IRA up (file ira-build.c).
  140. * IRA propagates accumulated allocno info from lower region
  141. allocnos to corresponding upper region allocnos (file
  142. ira-build.c).
  143. * IRA creates all caps (file ira-build.c).
  144. * Having live-ranges of allocnos and their classes, IRA creates
  145. conflicting allocnos for each allocno. Conflicting allocnos
  146. are stored as a bit vector or array of pointers to the
  147. conflicting allocnos whatever is more profitable (file
  148. ira-conflicts.c). At this point IRA creates allocno copies.
  149. o Coloring. Now IRA has all necessary info to start graph coloring
  150. process. It is done in each region on top-down traverse of the
  151. region tree (file ira-color.c). There are following subpasses:
  152. * Finding profitable hard registers of corresponding allocno
  153. class for each allocno. For example, only callee-saved hard
  154. registers are frequently profitable for allocnos living
  155. through colors. If the profitable hard register set of
  156. allocno does not form a tree based on subset relation, we use
  157. some approximation to form the tree. This approximation is
  158. used to figure out trivial colorability of allocnos. The
  159. approximation is a pretty rare case.
  160. * Putting allocnos onto the coloring stack. IRA uses Briggs
  161. optimistic coloring which is a major improvement over
  162. Chaitin's coloring. Therefore IRA does not spill allocnos at
  163. this point. There is some freedom in the order of putting
  164. allocnos on the stack which can affect the final result of
  165. the allocation. IRA uses some heuristics to improve the
  166. order. The major one is to form *threads* from colorable
  167. allocnos and push them on the stack by threads. Thread is a
  168. set of non-conflicting colorable allocnos connected by
  169. copies. The thread contains allocnos from the colorable
  170. bucket or colorable allocnos already pushed onto the coloring
  171. stack. Pushing thread allocnos one after another onto the
  172. stack increases chances of removing copies when the allocnos
  173. get the same hard reg.
  174. We also use a modification of Chaitin-Briggs algorithm which
  175. works for intersected register classes of allocnos. To
  176. figure out trivial colorability of allocnos, the mentioned
  177. above tree of hard register sets is used. To get an idea how
  178. the algorithm works in i386 example, let us consider an
  179. allocno to which any general hard register can be assigned.
  180. If the allocno conflicts with eight allocnos to which only
  181. EAX register can be assigned, given allocno is still
  182. trivially colorable because all conflicting allocnos might be
  183. assigned only to EAX and all other general hard registers are
  184. still free.
  185. To get an idea of the used trivial colorability criterion, it
  186. is also useful to read article "Graph-Coloring Register
  187. Allocation for Irregular Architectures" by Michael D. Smith
  188. and Glen Holloway. Major difference between the article
  189. approach and approach used in IRA is that Smith's approach
  190. takes register classes only from machine description and IRA
  191. calculate register classes from intermediate code too
  192. (e.g. an explicit usage of hard registers in RTL code for
  193. parameter passing can result in creation of additional
  194. register classes which contain or exclude the hard
  195. registers). That makes IRA approach useful for improving
  196. coloring even for architectures with regular register files
  197. and in fact some benchmarking shows the improvement for
  198. regular class architectures is even bigger than for irregular
  199. ones. Another difference is that Smith's approach chooses
  200. intersection of classes of all insn operands in which a given
  201. pseudo occurs. IRA can use bigger classes if it is still
  202. more profitable than memory usage.
  203. * Popping the allocnos from the stack and assigning them hard
  204. registers. If IRA can not assign a hard register to an
  205. allocno and the allocno is coalesced, IRA undoes the
  206. coalescing and puts the uncoalesced allocnos onto the stack in
  207. the hope that some such allocnos will get a hard register
  208. separately. If IRA fails to assign hard register or memory
  209. is more profitable for it, IRA spills the allocno. IRA
  210. assigns the allocno the hard-register with minimal full
  211. allocation cost which reflects the cost of usage of the
  212. hard-register for the allocno and cost of usage of the
  213. hard-register for allocnos conflicting with given allocno.
  214. * Chaitin-Briggs coloring assigns as many pseudos as possible
  215. to hard registers. After coloring we try to improve
  216. allocation with cost point of view. We improve the
  217. allocation by spilling some allocnos and assigning the freed
  218. hard registers to other allocnos if it decreases the overall
  219. allocation cost.
  220. * After allocno assigning in the region, IRA modifies the hard
  221. register and memory costs for the corresponding allocnos in
  222. the subregions to reflect the cost of possible loads, stores,
  223. or moves on the border of the region and its subregions.
  224. When default regional allocation algorithm is used
  225. (-fira-algorithm=mixed), IRA just propagates the assignment
  226. for allocnos if the register pressure in the region for the
  227. corresponding pressure class is less than number of available
  228. hard registers for given pressure class.
  229. o Spill/restore code moving. When IRA performs an allocation
  230. by traversing regions in top-down order, it does not know what
  231. happens below in the region tree. Therefore, sometimes IRA
  232. misses opportunities to perform a better allocation. A simple
  233. optimization tries to improve allocation in a region having
  234. subregions and containing in another region. If the
  235. corresponding allocnos in the subregion are spilled, it spills
  236. the region allocno if it is profitable. The optimization
  237. implements a simple iterative algorithm performing profitable
  238. transformations while they are still possible. It is fast in
  239. practice, so there is no real need for a better time complexity
  240. algorithm.
  241. o Code change. After coloring, two allocnos representing the
  242. same pseudo-register outside and inside a region respectively
  243. may be assigned to different locations (hard-registers or
  244. memory). In this case IRA creates and uses a new
  245. pseudo-register inside the region and adds code to move allocno
  246. values on the region's borders. This is done during top-down
  247. traversal of the regions (file ira-emit.c). In some
  248. complicated cases IRA can create a new allocno to move allocno
  249. values (e.g. when a swap of values stored in two hard-registers
  250. is needed). At this stage, the new allocno is marked as
  251. spilled. IRA still creates the pseudo-register and the moves
  252. on the region borders even when both allocnos were assigned to
  253. the same hard-register. If the reload pass spills a
  254. pseudo-register for some reason, the effect will be smaller
  255. because another allocno will still be in the hard-register. In
  256. most cases, this is better then spilling both allocnos. If
  257. reload does not change the allocation for the two
  258. pseudo-registers, the trivial move will be removed by
  259. post-reload optimizations. IRA does not generate moves for
  260. allocnos assigned to the same hard register when the default
  261. regional allocation algorithm is used and the register pressure
  262. in the region for the corresponding pressure class is less than
  263. number of available hard registers for given pressure class.
  264. IRA also does some optimizations to remove redundant stores and
  265. to reduce code duplication on the region borders.
  266. o Flattening internal representation. After changing code, IRA
  267. transforms its internal representation for several regions into
  268. one region representation (file ira-build.c). This process is
  269. called IR flattening. Such process is more complicated than IR
  270. rebuilding would be, but is much faster.
  271. o After IR flattening, IRA tries to assign hard registers to all
  272. spilled allocnos. This is implemented by a simple and fast
  273. priority coloring algorithm (see function
  274. ira_reassign_conflict_allocnos::ira-color.c). Here new allocnos
  275. created during the code change pass can be assigned to hard
  276. registers.
  277. o At the end IRA calls the reload pass. The reload pass
  278. communicates with IRA through several functions in file
  279. ira-color.c to improve its decisions in
  280. * sharing stack slots for the spilled pseudos based on IRA info
  281. about pseudo-register conflicts.
  282. * reassigning hard-registers to all spilled pseudos at the end
  283. of each reload iteration.
  284. * choosing a better hard-register to spill based on IRA info
  285. about pseudo-register live ranges and the register pressure
  286. in places where the pseudo-register lives.
  287. IRA uses a lot of data representing the target processors. These
  288. data are initialized in file ira.c.
  289. If function has no loops (or the loops are ignored when
  290. -fira-algorithm=CB is used), we have classic Chaitin-Briggs
  291. coloring (only instead of separate pass of coalescing, we use hard
  292. register preferencing). In such case, IRA works much faster
  293. because many things are not made (like IR flattening, the
  294. spill/restore optimization, and the code change).
  295. Literature is worth to read for better understanding the code:
  296. o Preston Briggs, Keith D. Cooper, Linda Torczon. Improvements to
  297. Graph Coloring Register Allocation.
  298. o David Callahan, Brian Koblenz. Register allocation via
  299. hierarchical graph coloring.
  300. o Keith Cooper, Anshuman Dasgupta, Jason Eckhardt. Revisiting Graph
  301. Coloring Register Allocation: A Study of the Chaitin-Briggs and
  302. Callahan-Koblenz Algorithms.
  303. o Guei-Yuan Lueh, Thomas Gross, and Ali-Reza Adl-Tabatabai. Global
  304. Register Allocation Based on Graph Fusion.
  305. o Michael D. Smith and Glenn Holloway. Graph-Coloring Register
  306. Allocation for Irregular Architectures
  307. o Vladimir Makarov. The Integrated Register Allocator for GCC.
  308. o Vladimir Makarov. The top-down register allocator for irregular
  309. register file architectures.
  310. */
  311. #include "config.h"
  312. #include "system.h"
  313. #include "coretypes.h"
  314. #include "tm.h"
  315. #include "regs.h"
  316. #include "hash-set.h"
  317. #include "machmode.h"
  318. #include "vec.h"
  319. #include "double-int.h"
  320. #include "input.h"
  321. #include "alias.h"
  322. #include "symtab.h"
  323. #include "wide-int.h"
  324. #include "inchash.h"
  325. #include "tree.h"
  326. #include "rtl.h"
  327. #include "tm_p.h"
  328. #include "target.h"
  329. #include "flags.h"
  330. #include "obstack.h"
  331. #include "bitmap.h"
  332. #include "hard-reg-set.h"
  333. #include "predict.h"
  334. #include "function.h"
  335. #include "dominance.h"
  336. #include "cfg.h"
  337. #include "cfgrtl.h"
  338. #include "cfgbuild.h"
  339. #include "cfgcleanup.h"
  340. #include "basic-block.h"
  341. #include "df.h"
  342. #include "hashtab.h"
  343. #include "statistics.h"
  344. #include "real.h"
  345. #include "fixed-value.h"
  346. #include "insn-config.h"
  347. #include "expmed.h"
  348. #include "dojump.h"
  349. #include "explow.h"
  350. #include "calls.h"
  351. #include "emit-rtl.h"
  352. #include "varasm.h"
  353. #include "stmt.h"
  354. #include "expr.h"
  355. #include "recog.h"
  356. #include "params.h"
  357. #include "tree-pass.h"
  358. #include "output.h"
  359. #include "except.h"
  360. #include "reload.h"
  361. #include "diagnostic-core.h"
  362. #include "ggc.h"
  363. #include "ira-int.h"
  364. #include "lra.h"
  365. #include "dce.h"
  366. #include "dbgcnt.h"
  367. #include "rtl-iter.h"
  368. #include "shrink-wrap.h"
  369. struct target_ira default_target_ira;
  370. struct target_ira_int default_target_ira_int;
  371. #if SWITCHABLE_TARGET
  372. struct target_ira *this_target_ira = &default_target_ira;
  373. struct target_ira_int *this_target_ira_int = &default_target_ira_int;
  374. #endif
  375. /* A modified value of flag `-fira-verbose' used internally. */
  376. int internal_flag_ira_verbose;
  377. /* Dump file of the allocator if it is not NULL. */
  378. FILE *ira_dump_file;
  379. /* The number of elements in the following array. */
  380. int ira_spilled_reg_stack_slots_num;
  381. /* The following array contains info about spilled pseudo-registers
  382. stack slots used in current function so far. */
  383. struct ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
  384. /* Correspondingly overall cost of the allocation, overall cost before
  385. reload, cost of the allocnos assigned to hard-registers, cost of
  386. the allocnos assigned to memory, cost of loads, stores and register
  387. move insns generated for pseudo-register live range splitting (see
  388. ira-emit.c). */
  389. int64_t ira_overall_cost, overall_cost_before;
  390. int64_t ira_reg_cost, ira_mem_cost;
  391. int64_t ira_load_cost, ira_store_cost, ira_shuffle_cost;
  392. int ira_move_loops_num, ira_additional_jumps_num;
  393. /* All registers that can be eliminated. */
  394. HARD_REG_SET eliminable_regset;
  395. /* Value of max_reg_num () before IRA work start. This value helps
  396. us to recognize a situation when new pseudos were created during
  397. IRA work. */
  398. static int max_regno_before_ira;
  399. /* Temporary hard reg set used for a different calculation. */
  400. static HARD_REG_SET temp_hard_regset;
  401. #define last_mode_for_init_move_cost \
  402. (this_target_ira_int->x_last_mode_for_init_move_cost)
  403. /* The function sets up the map IRA_REG_MODE_HARD_REGSET. */
  404. static void
  405. setup_reg_mode_hard_regset (void)
  406. {
  407. int i, m, hard_regno;
  408. for (m = 0; m < NUM_MACHINE_MODES; m++)
  409. for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
  410. {
  411. CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
  412. for (i = hard_regno_nregs[hard_regno][m] - 1; i >= 0; i--)
  413. if (hard_regno + i < FIRST_PSEUDO_REGISTER)
  414. SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
  415. hard_regno + i);
  416. }
  417. }
  418. #define no_unit_alloc_regs \
  419. (this_target_ira_int->x_no_unit_alloc_regs)
  420. /* The function sets up the three arrays declared above. */
  421. static void
  422. setup_class_hard_regs (void)
  423. {
  424. int cl, i, hard_regno, n;
  425. HARD_REG_SET processed_hard_reg_set;
  426. ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
  427. for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
  428. {
  429. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
  430. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  431. CLEAR_HARD_REG_SET (processed_hard_reg_set);
  432. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  433. {
  434. ira_non_ordered_class_hard_regs[cl][i] = -1;
  435. ira_class_hard_reg_index[cl][i] = -1;
  436. }
  437. for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  438. {
  439. #ifdef REG_ALLOC_ORDER
  440. hard_regno = reg_alloc_order[i];
  441. #else
  442. hard_regno = i;
  443. #endif
  444. if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
  445. continue;
  446. SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
  447. if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
  448. ira_class_hard_reg_index[cl][hard_regno] = -1;
  449. else
  450. {
  451. ira_class_hard_reg_index[cl][hard_regno] = n;
  452. ira_class_hard_regs[cl][n++] = hard_regno;
  453. }
  454. }
  455. ira_class_hard_regs_num[cl] = n;
  456. for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  457. if (TEST_HARD_REG_BIT (temp_hard_regset, i))
  458. ira_non_ordered_class_hard_regs[cl][n++] = i;
  459. ira_assert (ira_class_hard_regs_num[cl] == n);
  460. }
  461. }
  462. /* Set up global variables defining info about hard registers for the
  463. allocation. These depend on USE_HARD_FRAME_P whose TRUE value means
  464. that we can use the hard frame pointer for the allocation. */
  465. static void
  466. setup_alloc_regs (bool use_hard_frame_p)
  467. {
  468. #ifdef ADJUST_REG_ALLOC_ORDER
  469. ADJUST_REG_ALLOC_ORDER;
  470. #endif
  471. COPY_HARD_REG_SET (no_unit_alloc_regs, fixed_reg_set);
  472. if (! use_hard_frame_p)
  473. SET_HARD_REG_BIT (no_unit_alloc_regs, HARD_FRAME_POINTER_REGNUM);
  474. setup_class_hard_regs ();
  475. }
  476. #define alloc_reg_class_subclasses \
  477. (this_target_ira_int->x_alloc_reg_class_subclasses)
  478. /* Initialize the table of subclasses of each reg class. */
  479. static void
  480. setup_reg_subclasses (void)
  481. {
  482. int i, j;
  483. HARD_REG_SET temp_hard_regset2;
  484. for (i = 0; i < N_REG_CLASSES; i++)
  485. for (j = 0; j < N_REG_CLASSES; j++)
  486. alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
  487. for (i = 0; i < N_REG_CLASSES; i++)
  488. {
  489. if (i == (int) NO_REGS)
  490. continue;
  491. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
  492. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  493. if (hard_reg_set_empty_p (temp_hard_regset))
  494. continue;
  495. for (j = 0; j < N_REG_CLASSES; j++)
  496. if (i != j)
  497. {
  498. enum reg_class *p;
  499. COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
  500. AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
  501. if (! hard_reg_set_subset_p (temp_hard_regset,
  502. temp_hard_regset2))
  503. continue;
  504. p = &alloc_reg_class_subclasses[j][0];
  505. while (*p != LIM_REG_CLASSES) p++;
  506. *p = (enum reg_class) i;
  507. }
  508. }
  509. }
  510. /* Set up IRA_MEMORY_MOVE_COST and IRA_MAX_MEMORY_MOVE_COST. */
  511. static void
  512. setup_class_subset_and_memory_move_costs (void)
  513. {
  514. int cl, cl2, mode, cost;
  515. HARD_REG_SET temp_hard_regset2;
  516. for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
  517. ira_memory_move_cost[mode][NO_REGS][0]
  518. = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
  519. for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
  520. {
  521. if (cl != (int) NO_REGS)
  522. for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
  523. {
  524. ira_max_memory_move_cost[mode][cl][0]
  525. = ira_memory_move_cost[mode][cl][0]
  526. = memory_move_cost ((machine_mode) mode,
  527. (reg_class_t) cl, false);
  528. ira_max_memory_move_cost[mode][cl][1]
  529. = ira_memory_move_cost[mode][cl][1]
  530. = memory_move_cost ((machine_mode) mode,
  531. (reg_class_t) cl, true);
  532. /* Costs for NO_REGS are used in cost calculation on the
  533. 1st pass when the preferred register classes are not
  534. known yet. In this case we take the best scenario. */
  535. if (ira_memory_move_cost[mode][NO_REGS][0]
  536. > ira_memory_move_cost[mode][cl][0])
  537. ira_max_memory_move_cost[mode][NO_REGS][0]
  538. = ira_memory_move_cost[mode][NO_REGS][0]
  539. = ira_memory_move_cost[mode][cl][0];
  540. if (ira_memory_move_cost[mode][NO_REGS][1]
  541. > ira_memory_move_cost[mode][cl][1])
  542. ira_max_memory_move_cost[mode][NO_REGS][1]
  543. = ira_memory_move_cost[mode][NO_REGS][1]
  544. = ira_memory_move_cost[mode][cl][1];
  545. }
  546. }
  547. for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
  548. for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
  549. {
  550. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
  551. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  552. COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
  553. AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
  554. ira_class_subset_p[cl][cl2]
  555. = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
  556. if (! hard_reg_set_empty_p (temp_hard_regset2)
  557. && hard_reg_set_subset_p (reg_class_contents[cl2],
  558. reg_class_contents[cl]))
  559. for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
  560. {
  561. cost = ira_memory_move_cost[mode][cl2][0];
  562. if (cost > ira_max_memory_move_cost[mode][cl][0])
  563. ira_max_memory_move_cost[mode][cl][0] = cost;
  564. cost = ira_memory_move_cost[mode][cl2][1];
  565. if (cost > ira_max_memory_move_cost[mode][cl][1])
  566. ira_max_memory_move_cost[mode][cl][1] = cost;
  567. }
  568. }
  569. for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
  570. for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
  571. {
  572. ira_memory_move_cost[mode][cl][0]
  573. = ira_max_memory_move_cost[mode][cl][0];
  574. ira_memory_move_cost[mode][cl][1]
  575. = ira_max_memory_move_cost[mode][cl][1];
  576. }
  577. setup_reg_subclasses ();
  578. }
  579. /* Define the following macro if allocation through malloc if
  580. preferable. */
  581. #define IRA_NO_OBSTACK
  582. #ifndef IRA_NO_OBSTACK
  583. /* Obstack used for storing all dynamic data (except bitmaps) of the
  584. IRA. */
  585. static struct obstack ira_obstack;
  586. #endif
  587. /* Obstack used for storing all bitmaps of the IRA. */
  588. static struct bitmap_obstack ira_bitmap_obstack;
  589. /* Allocate memory of size LEN for IRA data. */
  590. void *
  591. ira_allocate (size_t len)
  592. {
  593. void *res;
  594. #ifndef IRA_NO_OBSTACK
  595. res = obstack_alloc (&ira_obstack, len);
  596. #else
  597. res = xmalloc (len);
  598. #endif
  599. return res;
  600. }
  601. /* Free memory ADDR allocated for IRA data. */
  602. void
  603. ira_free (void *addr ATTRIBUTE_UNUSED)
  604. {
  605. #ifndef IRA_NO_OBSTACK
  606. /* do nothing */
  607. #else
  608. free (addr);
  609. #endif
  610. }
  611. /* Allocate and returns bitmap for IRA. */
  612. bitmap
  613. ira_allocate_bitmap (void)
  614. {
  615. return BITMAP_ALLOC (&ira_bitmap_obstack);
  616. }
  617. /* Free bitmap B allocated for IRA. */
  618. void
  619. ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
  620. {
  621. /* do nothing */
  622. }
  623. /* Output information about allocation of all allocnos (except for
  624. caps) into file F. */
  625. void
  626. ira_print_disposition (FILE *f)
  627. {
  628. int i, n, max_regno;
  629. ira_allocno_t a;
  630. basic_block bb;
  631. fprintf (f, "Disposition:");
  632. max_regno = max_reg_num ();
  633. for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  634. for (a = ira_regno_allocno_map[i];
  635. a != NULL;
  636. a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
  637. {
  638. if (n % 4 == 0)
  639. fprintf (f, "\n");
  640. n++;
  641. fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
  642. if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
  643. fprintf (f, "b%-3d", bb->index);
  644. else
  645. fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
  646. if (ALLOCNO_HARD_REGNO (a) >= 0)
  647. fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
  648. else
  649. fprintf (f, " mem");
  650. }
  651. fprintf (f, "\n");
  652. }
  653. /* Outputs information about allocation of all allocnos into
  654. stderr. */
  655. void
  656. ira_debug_disposition (void)
  657. {
  658. ira_print_disposition (stderr);
  659. }
  660. /* Set up ira_stack_reg_pressure_class which is the biggest pressure
  661. register class containing stack registers or NO_REGS if there are
  662. no stack registers. To find this class, we iterate through all
  663. register pressure classes and choose the first register pressure
  664. class containing all the stack registers and having the biggest
  665. size. */
  666. static void
  667. setup_stack_reg_pressure_class (void)
  668. {
  669. ira_stack_reg_pressure_class = NO_REGS;
  670. #ifdef STACK_REGS
  671. {
  672. int i, best, size;
  673. enum reg_class cl;
  674. HARD_REG_SET temp_hard_regset2;
  675. CLEAR_HARD_REG_SET (temp_hard_regset);
  676. for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
  677. SET_HARD_REG_BIT (temp_hard_regset, i);
  678. best = 0;
  679. for (i = 0; i < ira_pressure_classes_num; i++)
  680. {
  681. cl = ira_pressure_classes[i];
  682. COPY_HARD_REG_SET (temp_hard_regset2, temp_hard_regset);
  683. AND_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
  684. size = hard_reg_set_size (temp_hard_regset2);
  685. if (best < size)
  686. {
  687. best = size;
  688. ira_stack_reg_pressure_class = cl;
  689. }
  690. }
  691. }
  692. #endif
  693. }
  694. /* Find pressure classes which are register classes for which we
  695. calculate register pressure in IRA, register pressure sensitive
  696. insn scheduling, and register pressure sensitive loop invariant
  697. motion.
  698. To make register pressure calculation easy, we always use
  699. non-intersected register pressure classes. A move of hard
  700. registers from one register pressure class is not more expensive
  701. than load and store of the hard registers. Most likely an allocno
  702. class will be a subset of a register pressure class and in many
  703. cases a register pressure class. That makes usage of register
  704. pressure classes a good approximation to find a high register
  705. pressure. */
  706. static void
  707. setup_pressure_classes (void)
  708. {
  709. int cost, i, n, curr;
  710. int cl, cl2;
  711. enum reg_class pressure_classes[N_REG_CLASSES];
  712. int m;
  713. HARD_REG_SET temp_hard_regset2;
  714. bool insert_p;
  715. n = 0;
  716. for (cl = 0; cl < N_REG_CLASSES; cl++)
  717. {
  718. if (ira_class_hard_regs_num[cl] == 0)
  719. continue;
  720. if (ira_class_hard_regs_num[cl] != 1
  721. /* A register class without subclasses may contain a few
  722. hard registers and movement between them is costly
  723. (e.g. SPARC FPCC registers). We still should consider it
  724. as a candidate for a pressure class. */
  725. && alloc_reg_class_subclasses[cl][0] < cl)
  726. {
  727. /* Check that the moves between any hard registers of the
  728. current class are not more expensive for a legal mode
  729. than load/store of the hard registers of the current
  730. class. Such class is a potential candidate to be a
  731. register pressure class. */
  732. for (m = 0; m < NUM_MACHINE_MODES; m++)
  733. {
  734. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
  735. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  736. AND_COMPL_HARD_REG_SET (temp_hard_regset,
  737. ira_prohibited_class_mode_regs[cl][m]);
  738. if (hard_reg_set_empty_p (temp_hard_regset))
  739. continue;
  740. ira_init_register_move_cost_if_necessary ((machine_mode) m);
  741. cost = ira_register_move_cost[m][cl][cl];
  742. if (cost <= ira_max_memory_move_cost[m][cl][1]
  743. || cost <= ira_max_memory_move_cost[m][cl][0])
  744. break;
  745. }
  746. if (m >= NUM_MACHINE_MODES)
  747. continue;
  748. }
  749. curr = 0;
  750. insert_p = true;
  751. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
  752. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  753. /* Remove so far added pressure classes which are subset of the
  754. current candidate class. Prefer GENERAL_REGS as a pressure
  755. register class to another class containing the same
  756. allocatable hard registers. We do this because machine
  757. dependent cost hooks might give wrong costs for the latter
  758. class but always give the right cost for the former class
  759. (GENERAL_REGS). */
  760. for (i = 0; i < n; i++)
  761. {
  762. cl2 = pressure_classes[i];
  763. COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
  764. AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
  765. if (hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2)
  766. && (! hard_reg_set_equal_p (temp_hard_regset, temp_hard_regset2)
  767. || cl2 == (int) GENERAL_REGS))
  768. {
  769. pressure_classes[curr++] = (enum reg_class) cl2;
  770. insert_p = false;
  771. continue;
  772. }
  773. if (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset)
  774. && (! hard_reg_set_equal_p (temp_hard_regset2, temp_hard_regset)
  775. || cl == (int) GENERAL_REGS))
  776. continue;
  777. if (hard_reg_set_equal_p (temp_hard_regset2, temp_hard_regset))
  778. insert_p = false;
  779. pressure_classes[curr++] = (enum reg_class) cl2;
  780. }
  781. /* If the current candidate is a subset of a so far added
  782. pressure class, don't add it to the list of the pressure
  783. classes. */
  784. if (insert_p)
  785. pressure_classes[curr++] = (enum reg_class) cl;
  786. n = curr;
  787. }
  788. #ifdef ENABLE_IRA_CHECKING
  789. {
  790. HARD_REG_SET ignore_hard_regs;
  791. /* Check pressure classes correctness: here we check that hard
  792. registers from all register pressure classes contains all hard
  793. registers available for the allocation. */
  794. CLEAR_HARD_REG_SET (temp_hard_regset);
  795. CLEAR_HARD_REG_SET (temp_hard_regset2);
  796. COPY_HARD_REG_SET (ignore_hard_regs, no_unit_alloc_regs);
  797. for (cl = 0; cl < LIM_REG_CLASSES; cl++)
  798. {
  799. /* For some targets (like MIPS with MD_REGS), there are some
  800. classes with hard registers available for allocation but
  801. not able to hold value of any mode. */
  802. for (m = 0; m < NUM_MACHINE_MODES; m++)
  803. if (contains_reg_of_mode[cl][m])
  804. break;
  805. if (m >= NUM_MACHINE_MODES)
  806. {
  807. IOR_HARD_REG_SET (ignore_hard_regs, reg_class_contents[cl]);
  808. continue;
  809. }
  810. for (i = 0; i < n; i++)
  811. if ((int) pressure_classes[i] == cl)
  812. break;
  813. IOR_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
  814. if (i < n)
  815. IOR_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
  816. }
  817. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  818. /* Some targets (like SPARC with ICC reg) have allocatable regs
  819. for which no reg class is defined. */
  820. if (REGNO_REG_CLASS (i) == NO_REGS)
  821. SET_HARD_REG_BIT (ignore_hard_regs, i);
  822. AND_COMPL_HARD_REG_SET (temp_hard_regset, ignore_hard_regs);
  823. AND_COMPL_HARD_REG_SET (temp_hard_regset2, ignore_hard_regs);
  824. ira_assert (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset));
  825. }
  826. #endif
  827. ira_pressure_classes_num = 0;
  828. for (i = 0; i < n; i++)
  829. {
  830. cl = (int) pressure_classes[i];
  831. ira_reg_pressure_class_p[cl] = true;
  832. ira_pressure_classes[ira_pressure_classes_num++] = (enum reg_class) cl;
  833. }
  834. setup_stack_reg_pressure_class ();
  835. }
  836. /* Set up IRA_UNIFORM_CLASS_P. Uniform class is a register class
  837. whose register move cost between any registers of the class is the
  838. same as for all its subclasses. We use the data to speed up the
  839. 2nd pass of calculations of allocno costs. */
  840. static void
  841. setup_uniform_class_p (void)
  842. {
  843. int i, cl, cl2, m;
  844. for (cl = 0; cl < N_REG_CLASSES; cl++)
  845. {
  846. ira_uniform_class_p[cl] = false;
  847. if (ira_class_hard_regs_num[cl] == 0)
  848. continue;
  849. /* We can not use alloc_reg_class_subclasses here because move
  850. cost hooks does not take into account that some registers are
  851. unavailable for the subtarget. E.g. for i686, INT_SSE_REGS
  852. is element of alloc_reg_class_subclasses for GENERAL_REGS
  853. because SSE regs are unavailable. */
  854. for (i = 0; (cl2 = reg_class_subclasses[cl][i]) != LIM_REG_CLASSES; i++)
  855. {
  856. if (ira_class_hard_regs_num[cl2] == 0)
  857. continue;
  858. for (m = 0; m < NUM_MACHINE_MODES; m++)
  859. if (contains_reg_of_mode[cl][m] && contains_reg_of_mode[cl2][m])
  860. {
  861. ira_init_register_move_cost_if_necessary ((machine_mode) m);
  862. if (ira_register_move_cost[m][cl][cl]
  863. != ira_register_move_cost[m][cl2][cl2])
  864. break;
  865. }
  866. if (m < NUM_MACHINE_MODES)
  867. break;
  868. }
  869. if (cl2 == LIM_REG_CLASSES)
  870. ira_uniform_class_p[cl] = true;
  871. }
  872. }
  873. /* Set up IRA_ALLOCNO_CLASSES, IRA_ALLOCNO_CLASSES_NUM,
  874. IRA_IMPORTANT_CLASSES, and IRA_IMPORTANT_CLASSES_NUM.
  875. Target may have many subtargets and not all target hard registers can
  876. be used for allocation, e.g. x86 port in 32-bit mode can not use
  877. hard registers introduced in x86-64 like r8-r15). Some classes
  878. might have the same allocatable hard registers, e.g. INDEX_REGS
  879. and GENERAL_REGS in x86 port in 32-bit mode. To decrease different
  880. calculations efforts we introduce allocno classes which contain
  881. unique non-empty sets of allocatable hard-registers.
  882. Pseudo class cost calculation in ira-costs.c is very expensive.
  883. Therefore we are trying to decrease number of classes involved in
  884. such calculation. Register classes used in the cost calculation
  885. are called important classes. They are allocno classes and other
  886. non-empty classes whose allocatable hard register sets are inside
  887. of an allocno class hard register set. From the first sight, it
  888. looks like that they are just allocno classes. It is not true. In
  889. example of x86-port in 32-bit mode, allocno classes will contain
  890. GENERAL_REGS but not LEGACY_REGS (because allocatable hard
  891. registers are the same for the both classes). The important
  892. classes will contain GENERAL_REGS and LEGACY_REGS. It is done
  893. because a machine description insn constraint may refers for
  894. LEGACY_REGS and code in ira-costs.c is mostly base on investigation
  895. of the insn constraints. */
  896. static void
  897. setup_allocno_and_important_classes (void)
  898. {
  899. int i, j, n, cl;
  900. bool set_p;
  901. HARD_REG_SET temp_hard_regset2;
  902. static enum reg_class classes[LIM_REG_CLASSES + 1];
  903. n = 0;
  904. /* Collect classes which contain unique sets of allocatable hard
  905. registers. Prefer GENERAL_REGS to other classes containing the
  906. same set of hard registers. */
  907. for (i = 0; i < LIM_REG_CLASSES; i++)
  908. {
  909. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
  910. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  911. for (j = 0; j < n; j++)
  912. {
  913. cl = classes[j];
  914. COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
  915. AND_COMPL_HARD_REG_SET (temp_hard_regset2,
  916. no_unit_alloc_regs);
  917. if (hard_reg_set_equal_p (temp_hard_regset,
  918. temp_hard_regset2))
  919. break;
  920. }
  921. if (j >= n)
  922. classes[n++] = (enum reg_class) i;
  923. else if (i == GENERAL_REGS)
  924. /* Prefer general regs. For i386 example, it means that
  925. we prefer GENERAL_REGS over INDEX_REGS or LEGACY_REGS
  926. (all of them consists of the same available hard
  927. registers). */
  928. classes[j] = (enum reg_class) i;
  929. }
  930. classes[n] = LIM_REG_CLASSES;
  931. /* Set up classes which can be used for allocnos as classes
  932. containing non-empty unique sets of allocatable hard
  933. registers. */
  934. ira_allocno_classes_num = 0;
  935. for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
  936. if (ira_class_hard_regs_num[cl] > 0)
  937. ira_allocno_classes[ira_allocno_classes_num++] = (enum reg_class) cl;
  938. ira_important_classes_num = 0;
  939. /* Add non-allocno classes containing to non-empty set of
  940. allocatable hard regs. */
  941. for (cl = 0; cl < N_REG_CLASSES; cl++)
  942. if (ira_class_hard_regs_num[cl] > 0)
  943. {
  944. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
  945. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  946. set_p = false;
  947. for (j = 0; j < ira_allocno_classes_num; j++)
  948. {
  949. COPY_HARD_REG_SET (temp_hard_regset2,
  950. reg_class_contents[ira_allocno_classes[j]]);
  951. AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
  952. if ((enum reg_class) cl == ira_allocno_classes[j])
  953. break;
  954. else if (hard_reg_set_subset_p (temp_hard_regset,
  955. temp_hard_regset2))
  956. set_p = true;
  957. }
  958. if (set_p && j >= ira_allocno_classes_num)
  959. ira_important_classes[ira_important_classes_num++]
  960. = (enum reg_class) cl;
  961. }
  962. /* Now add allocno classes to the important classes. */
  963. for (j = 0; j < ira_allocno_classes_num; j++)
  964. ira_important_classes[ira_important_classes_num++]
  965. = ira_allocno_classes[j];
  966. for (cl = 0; cl < N_REG_CLASSES; cl++)
  967. {
  968. ira_reg_allocno_class_p[cl] = false;
  969. ira_reg_pressure_class_p[cl] = false;
  970. }
  971. for (j = 0; j < ira_allocno_classes_num; j++)
  972. ira_reg_allocno_class_p[ira_allocno_classes[j]] = true;
  973. setup_pressure_classes ();
  974. setup_uniform_class_p ();
  975. }
  976. /* Setup translation in CLASS_TRANSLATE of all classes into a class
  977. given by array CLASSES of length CLASSES_NUM. The function is used
  978. make translation any reg class to an allocno class or to an
  979. pressure class. This translation is necessary for some
  980. calculations when we can use only allocno or pressure classes and
  981. such translation represents an approximate representation of all
  982. classes.
  983. The translation in case when allocatable hard register set of a
  984. given class is subset of allocatable hard register set of a class
  985. in CLASSES is pretty simple. We use smallest classes from CLASSES
  986. containing a given class. If allocatable hard register set of a
  987. given class is not a subset of any corresponding set of a class
  988. from CLASSES, we use the cheapest (with load/store point of view)
  989. class from CLASSES whose set intersects with given class set. */
  990. static void
  991. setup_class_translate_array (enum reg_class *class_translate,
  992. int classes_num, enum reg_class *classes)
  993. {
  994. int cl, mode;
  995. enum reg_class aclass, best_class, *cl_ptr;
  996. int i, cost, min_cost, best_cost;
  997. for (cl = 0; cl < N_REG_CLASSES; cl++)
  998. class_translate[cl] = NO_REGS;
  999. for (i = 0; i < classes_num; i++)
  1000. {
  1001. aclass = classes[i];
  1002. for (cl_ptr = &alloc_reg_class_subclasses[aclass][0];
  1003. (cl = *cl_ptr) != LIM_REG_CLASSES;
  1004. cl_ptr++)
  1005. if (class_translate[cl] == NO_REGS)
  1006. class_translate[cl] = aclass;
  1007. class_translate[aclass] = aclass;
  1008. }
  1009. /* For classes which are not fully covered by one of given classes
  1010. (in other words covered by more one given class), use the
  1011. cheapest class. */
  1012. for (cl = 0; cl < N_REG_CLASSES; cl++)
  1013. {
  1014. if (cl == NO_REGS || class_translate[cl] != NO_REGS)
  1015. continue;
  1016. best_class = NO_REGS;
  1017. best_cost = INT_MAX;
  1018. for (i = 0; i < classes_num; i++)
  1019. {
  1020. aclass = classes[i];
  1021. COPY_HARD_REG_SET (temp_hard_regset,
  1022. reg_class_contents[aclass]);
  1023. AND_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
  1024. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  1025. if (! hard_reg_set_empty_p (temp_hard_regset))
  1026. {
  1027. min_cost = INT_MAX;
  1028. for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
  1029. {
  1030. cost = (ira_memory_move_cost[mode][aclass][0]
  1031. + ira_memory_move_cost[mode][aclass][1]);
  1032. if (min_cost > cost)
  1033. min_cost = cost;
  1034. }
  1035. if (best_class == NO_REGS || best_cost > min_cost)
  1036. {
  1037. best_class = aclass;
  1038. best_cost = min_cost;
  1039. }
  1040. }
  1041. }
  1042. class_translate[cl] = best_class;
  1043. }
  1044. }
  1045. /* Set up array IRA_ALLOCNO_CLASS_TRANSLATE and
  1046. IRA_PRESSURE_CLASS_TRANSLATE. */
  1047. static void
  1048. setup_class_translate (void)
  1049. {
  1050. setup_class_translate_array (ira_allocno_class_translate,
  1051. ira_allocno_classes_num, ira_allocno_classes);
  1052. setup_class_translate_array (ira_pressure_class_translate,
  1053. ira_pressure_classes_num, ira_pressure_classes);
  1054. }
  1055. /* Order numbers of allocno classes in original target allocno class
  1056. array, -1 for non-allocno classes. */
  1057. static int allocno_class_order[N_REG_CLASSES];
  1058. /* The function used to sort the important classes. */
  1059. static int
  1060. comp_reg_classes_func (const void *v1p, const void *v2p)
  1061. {
  1062. enum reg_class cl1 = *(const enum reg_class *) v1p;
  1063. enum reg_class cl2 = *(const enum reg_class *) v2p;
  1064. enum reg_class tcl1, tcl2;
  1065. int diff;
  1066. tcl1 = ira_allocno_class_translate[cl1];
  1067. tcl2 = ira_allocno_class_translate[cl2];
  1068. if (tcl1 != NO_REGS && tcl2 != NO_REGS
  1069. && (diff = allocno_class_order[tcl1] - allocno_class_order[tcl2]) != 0)
  1070. return diff;
  1071. return (int) cl1 - (int) cl2;
  1072. }
  1073. /* For correct work of function setup_reg_class_relation we need to
  1074. reorder important classes according to the order of their allocno
  1075. classes. It places important classes containing the same
  1076. allocatable hard register set adjacent to each other and allocno
  1077. class with the allocatable hard register set right after the other
  1078. important classes with the same set.
  1079. In example from comments of function
  1080. setup_allocno_and_important_classes, it places LEGACY_REGS and
  1081. GENERAL_REGS close to each other and GENERAL_REGS is after
  1082. LEGACY_REGS. */
  1083. static void
  1084. reorder_important_classes (void)
  1085. {
  1086. int i;
  1087. for (i = 0; i < N_REG_CLASSES; i++)
  1088. allocno_class_order[i] = -1;
  1089. for (i = 0; i < ira_allocno_classes_num; i++)
  1090. allocno_class_order[ira_allocno_classes[i]] = i;
  1091. qsort (ira_important_classes, ira_important_classes_num,
  1092. sizeof (enum reg_class), comp_reg_classes_func);
  1093. for (i = 0; i < ira_important_classes_num; i++)
  1094. ira_important_class_nums[ira_important_classes[i]] = i;
  1095. }
  1096. /* Set up IRA_REG_CLASS_SUBUNION, IRA_REG_CLASS_SUPERUNION,
  1097. IRA_REG_CLASS_SUPER_CLASSES, IRA_REG_CLASSES_INTERSECT, and
  1098. IRA_REG_CLASSES_INTERSECT_P. For the meaning of the relations,
  1099. please see corresponding comments in ira-int.h. */
  1100. static void
  1101. setup_reg_class_relations (void)
  1102. {
  1103. int i, cl1, cl2, cl3;
  1104. HARD_REG_SET intersection_set, union_set, temp_set2;
  1105. bool important_class_p[N_REG_CLASSES];
  1106. memset (important_class_p, 0, sizeof (important_class_p));
  1107. for (i = 0; i < ira_important_classes_num; i++)
  1108. important_class_p[ira_important_classes[i]] = true;
  1109. for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
  1110. {
  1111. ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
  1112. for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
  1113. {
  1114. ira_reg_classes_intersect_p[cl1][cl2] = false;
  1115. ira_reg_class_intersect[cl1][cl2] = NO_REGS;
  1116. ira_reg_class_subset[cl1][cl2] = NO_REGS;
  1117. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
  1118. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  1119. COPY_HARD_REG_SET (temp_set2, reg_class_contents[cl2]);
  1120. AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
  1121. if (hard_reg_set_empty_p (temp_hard_regset)
  1122. && hard_reg_set_empty_p (temp_set2))
  1123. {
  1124. /* The both classes have no allocatable hard registers
  1125. -- take all class hard registers into account and use
  1126. reg_class_subunion and reg_class_superunion. */
  1127. for (i = 0;; i++)
  1128. {
  1129. cl3 = reg_class_subclasses[cl1][i];
  1130. if (cl3 == LIM_REG_CLASSES)
  1131. break;
  1132. if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
  1133. (enum reg_class) cl3))
  1134. ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
  1135. }
  1136. ira_reg_class_subunion[cl1][cl2] = reg_class_subunion[cl1][cl2];
  1137. ira_reg_class_superunion[cl1][cl2] = reg_class_superunion[cl1][cl2];
  1138. continue;
  1139. }
  1140. ira_reg_classes_intersect_p[cl1][cl2]
  1141. = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
  1142. if (important_class_p[cl1] && important_class_p[cl2]
  1143. && hard_reg_set_subset_p (temp_hard_regset, temp_set2))
  1144. {
  1145. /* CL1 and CL2 are important classes and CL1 allocatable
  1146. hard register set is inside of CL2 allocatable hard
  1147. registers -- make CL1 a superset of CL2. */
  1148. enum reg_class *p;
  1149. p = &ira_reg_class_super_classes[cl1][0];
  1150. while (*p != LIM_REG_CLASSES)
  1151. p++;
  1152. *p++ = (enum reg_class) cl2;
  1153. *p = LIM_REG_CLASSES;
  1154. }
  1155. ira_reg_class_subunion[cl1][cl2] = NO_REGS;
  1156. ira_reg_class_superunion[cl1][cl2] = NO_REGS;
  1157. COPY_HARD_REG_SET (intersection_set, reg_class_contents[cl1]);
  1158. AND_HARD_REG_SET (intersection_set, reg_class_contents[cl2]);
  1159. AND_COMPL_HARD_REG_SET (intersection_set, no_unit_alloc_regs);
  1160. COPY_HARD_REG_SET (union_set, reg_class_contents[cl1]);
  1161. IOR_HARD_REG_SET (union_set, reg_class_contents[cl2]);
  1162. AND_COMPL_HARD_REG_SET (union_set, no_unit_alloc_regs);
  1163. for (cl3 = 0; cl3 < N_REG_CLASSES; cl3++)
  1164. {
  1165. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl3]);
  1166. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  1167. if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
  1168. {
  1169. /* CL3 allocatable hard register set is inside of
  1170. intersection of allocatable hard register sets
  1171. of CL1 and CL2. */
  1172. if (important_class_p[cl3])
  1173. {
  1174. COPY_HARD_REG_SET
  1175. (temp_set2,
  1176. reg_class_contents
  1177. [(int) ira_reg_class_intersect[cl1][cl2]]);
  1178. AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
  1179. if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
  1180. /* If the allocatable hard register sets are
  1181. the same, prefer GENERAL_REGS or the
  1182. smallest class for debugging
  1183. purposes. */
  1184. || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
  1185. && (cl3 == GENERAL_REGS
  1186. || ((ira_reg_class_intersect[cl1][cl2]
  1187. != GENERAL_REGS)
  1188. && hard_reg_set_subset_p
  1189. (reg_class_contents[cl3],
  1190. reg_class_contents
  1191. [(int)
  1192. ira_reg_class_intersect[cl1][cl2]])))))
  1193. ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
  1194. }
  1195. COPY_HARD_REG_SET
  1196. (temp_set2,
  1197. reg_class_contents[(int) ira_reg_class_subset[cl1][cl2]]);
  1198. AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
  1199. if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
  1200. /* Ignore unavailable hard registers and prefer
  1201. smallest class for debugging purposes. */
  1202. || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
  1203. && hard_reg_set_subset_p
  1204. (reg_class_contents[cl3],
  1205. reg_class_contents
  1206. [(int) ira_reg_class_subset[cl1][cl2]])))
  1207. ira_reg_class_subset[cl1][cl2] = (enum reg_class) cl3;
  1208. }
  1209. if (important_class_p[cl3]
  1210. && hard_reg_set_subset_p (temp_hard_regset, union_set))
  1211. {
  1212. /* CL3 allocatable hard register set is inside of
  1213. union of allocatable hard register sets of CL1
  1214. and CL2. */
  1215. COPY_HARD_REG_SET
  1216. (temp_set2,
  1217. reg_class_contents[(int) ira_reg_class_subunion[cl1][cl2]]);
  1218. AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
  1219. if (ira_reg_class_subunion[cl1][cl2] == NO_REGS
  1220. || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
  1221. && (! hard_reg_set_equal_p (temp_set2,
  1222. temp_hard_regset)
  1223. || cl3 == GENERAL_REGS
  1224. /* If the allocatable hard register sets are the
  1225. same, prefer GENERAL_REGS or the smallest
  1226. class for debugging purposes. */
  1227. || (ira_reg_class_subunion[cl1][cl2] != GENERAL_REGS
  1228. && hard_reg_set_subset_p
  1229. (reg_class_contents[cl3],
  1230. reg_class_contents
  1231. [(int) ira_reg_class_subunion[cl1][cl2]])))))
  1232. ira_reg_class_subunion[cl1][cl2] = (enum reg_class) cl3;
  1233. }
  1234. if (hard_reg_set_subset_p (union_set, temp_hard_regset))
  1235. {
  1236. /* CL3 allocatable hard register set contains union
  1237. of allocatable hard register sets of CL1 and
  1238. CL2. */
  1239. COPY_HARD_REG_SET
  1240. (temp_set2,
  1241. reg_class_contents[(int) ira_reg_class_superunion[cl1][cl2]]);
  1242. AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
  1243. if (ira_reg_class_superunion[cl1][cl2] == NO_REGS
  1244. || (hard_reg_set_subset_p (temp_hard_regset, temp_set2)
  1245. && (! hard_reg_set_equal_p (temp_set2,
  1246. temp_hard_regset)
  1247. || cl3 == GENERAL_REGS
  1248. /* If the allocatable hard register sets are the
  1249. same, prefer GENERAL_REGS or the smallest
  1250. class for debugging purposes. */
  1251. || (ira_reg_class_superunion[cl1][cl2] != GENERAL_REGS
  1252. && hard_reg_set_subset_p
  1253. (reg_class_contents[cl3],
  1254. reg_class_contents
  1255. [(int) ira_reg_class_superunion[cl1][cl2]])))))
  1256. ira_reg_class_superunion[cl1][cl2] = (enum reg_class) cl3;
  1257. }
  1258. }
  1259. }
  1260. }
  1261. }
  1262. /* Output all uniform and important classes into file F. */
  1263. static void
  1264. print_unform_and_important_classes (FILE *f)
  1265. {
  1266. static const char *const reg_class_names[] = REG_CLASS_NAMES;
  1267. int i, cl;
  1268. fprintf (f, "Uniform classes:\n");
  1269. for (cl = 0; cl < N_REG_CLASSES; cl++)
  1270. if (ira_uniform_class_p[cl])
  1271. fprintf (f, " %s", reg_class_names[cl]);
  1272. fprintf (f, "\nImportant classes:\n");
  1273. for (i = 0; i < ira_important_classes_num; i++)
  1274. fprintf (f, " %s", reg_class_names[ira_important_classes[i]]);
  1275. fprintf (f, "\n");
  1276. }
  1277. /* Output all possible allocno or pressure classes and their
  1278. translation map into file F. */
  1279. static void
  1280. print_translated_classes (FILE *f, bool pressure_p)
  1281. {
  1282. int classes_num = (pressure_p
  1283. ? ira_pressure_classes_num : ira_allocno_classes_num);
  1284. enum reg_class *classes = (pressure_p
  1285. ? ira_pressure_classes : ira_allocno_classes);
  1286. enum reg_class *class_translate = (pressure_p
  1287. ? ira_pressure_class_translate
  1288. : ira_allocno_class_translate);
  1289. static const char *const reg_class_names[] = REG_CLASS_NAMES;
  1290. int i;
  1291. fprintf (f, "%s classes:\n", pressure_p ? "Pressure" : "Allocno");
  1292. for (i = 0; i < classes_num; i++)
  1293. fprintf (f, " %s", reg_class_names[classes[i]]);
  1294. fprintf (f, "\nClass translation:\n");
  1295. for (i = 0; i < N_REG_CLASSES; i++)
  1296. fprintf (f, " %s -> %s\n", reg_class_names[i],
  1297. reg_class_names[class_translate[i]]);
  1298. }
  1299. /* Output all possible allocno and translation classes and the
  1300. translation maps into stderr. */
  1301. void
  1302. ira_debug_allocno_classes (void)
  1303. {
  1304. print_unform_and_important_classes (stderr);
  1305. print_translated_classes (stderr, false);
  1306. print_translated_classes (stderr, true);
  1307. }
  1308. /* Set up different arrays concerning class subsets, allocno and
  1309. important classes. */
  1310. static void
  1311. find_reg_classes (void)
  1312. {
  1313. setup_allocno_and_important_classes ();
  1314. setup_class_translate ();
  1315. reorder_important_classes ();
  1316. setup_reg_class_relations ();
  1317. }
  1318. /* Set up the array above. */
  1319. static void
  1320. setup_hard_regno_aclass (void)
  1321. {
  1322. int i;
  1323. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1324. {
  1325. #if 1
  1326. ira_hard_regno_allocno_class[i]
  1327. = (TEST_HARD_REG_BIT (no_unit_alloc_regs, i)
  1328. ? NO_REGS
  1329. : ira_allocno_class_translate[REGNO_REG_CLASS (i)]);
  1330. #else
  1331. int j;
  1332. enum reg_class cl;
  1333. ira_hard_regno_allocno_class[i] = NO_REGS;
  1334. for (j = 0; j < ira_allocno_classes_num; j++)
  1335. {
  1336. cl = ira_allocno_classes[j];
  1337. if (ira_class_hard_reg_index[cl][i] >= 0)
  1338. {
  1339. ira_hard_regno_allocno_class[i] = cl;
  1340. break;
  1341. }
  1342. }
  1343. #endif
  1344. }
  1345. }
  1346. /* Form IRA_REG_CLASS_MAX_NREGS and IRA_REG_CLASS_MIN_NREGS maps. */
  1347. static void
  1348. setup_reg_class_nregs (void)
  1349. {
  1350. int i, cl, cl2, m;
  1351. for (m = 0; m < MAX_MACHINE_MODE; m++)
  1352. {
  1353. for (cl = 0; cl < N_REG_CLASSES; cl++)
  1354. ira_reg_class_max_nregs[cl][m]
  1355. = ira_reg_class_min_nregs[cl][m]
  1356. = targetm.class_max_nregs ((reg_class_t) cl, (machine_mode) m);
  1357. for (cl = 0; cl < N_REG_CLASSES; cl++)
  1358. for (i = 0;
  1359. (cl2 = alloc_reg_class_subclasses[cl][i]) != LIM_REG_CLASSES;
  1360. i++)
  1361. if (ira_reg_class_min_nregs[cl2][m]
  1362. < ira_reg_class_min_nregs[cl][m])
  1363. ira_reg_class_min_nregs[cl][m] = ira_reg_class_min_nregs[cl2][m];
  1364. }
  1365. }
  1366. /* Set up IRA_PROHIBITED_CLASS_MODE_REGS and IRA_CLASS_SINGLETON.
  1367. This function is called once IRA_CLASS_HARD_REGS has been initialized. */
  1368. static void
  1369. setup_prohibited_class_mode_regs (void)
  1370. {
  1371. int j, k, hard_regno, cl, last_hard_regno, count;
  1372. for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
  1373. {
  1374. COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
  1375. AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
  1376. for (j = 0; j < NUM_MACHINE_MODES; j++)
  1377. {
  1378. count = 0;
  1379. last_hard_regno = -1;
  1380. CLEAR_HARD_REG_SET (ira_prohibited_class_mode_regs[cl][j]);
  1381. for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
  1382. {
  1383. hard_regno = ira_class_hard_regs[cl][k];
  1384. if (! HARD_REGNO_MODE_OK (hard_regno, (machine_mode) j))
  1385. SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
  1386. hard_regno);
  1387. else if (in_hard_reg_set_p (temp_hard_regset,
  1388. (machine_mode) j, hard_regno))
  1389. {
  1390. last_hard_regno = hard_regno;
  1391. count++;
  1392. }
  1393. }
  1394. ira_class_singleton[cl][j] = (count == 1 ? last_hard_regno : -1);
  1395. }
  1396. }
  1397. }
  1398. /* Clarify IRA_PROHIBITED_CLASS_MODE_REGS by excluding hard registers
  1399. spanning from one register pressure class to another one. It is
  1400. called after defining the pressure classes. */
  1401. static void
  1402. clarify_prohibited_class_mode_regs (void)
  1403. {
  1404. int j, k, hard_regno, cl, pclass, nregs;
  1405. for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
  1406. for (j = 0; j < NUM_MACHINE_MODES; j++)
  1407. {
  1408. CLEAR_HARD_REG_SET (ira_useful_class_mode_regs[cl][j]);
  1409. for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
  1410. {
  1411. hard_regno = ira_class_hard_regs[cl][k];
  1412. if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j], hard_regno))
  1413. continue;
  1414. nregs = hard_regno_nregs[hard_regno][j];
  1415. if (hard_regno + nregs > FIRST_PSEUDO_REGISTER)
  1416. {
  1417. SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
  1418. hard_regno);
  1419. continue;
  1420. }
  1421. pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
  1422. for (nregs-- ;nregs >= 0; nregs--)
  1423. if (((enum reg_class) pclass
  1424. != ira_pressure_class_translate[REGNO_REG_CLASS
  1425. (hard_regno + nregs)]))
  1426. {
  1427. SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
  1428. hard_regno);
  1429. break;
  1430. }
  1431. if (!TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
  1432. hard_regno))
  1433. add_to_hard_reg_set (&ira_useful_class_mode_regs[cl][j],
  1434. (machine_mode) j, hard_regno);
  1435. }
  1436. }
  1437. }
  1438. /* Allocate and initialize IRA_REGISTER_MOVE_COST, IRA_MAY_MOVE_IN_COST
  1439. and IRA_MAY_MOVE_OUT_COST for MODE. */
  1440. void
  1441. ira_init_register_move_cost (machine_mode mode)
  1442. {
  1443. static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
  1444. bool all_match = true;
  1445. unsigned int cl1, cl2;
  1446. ira_assert (ira_register_move_cost[mode] == NULL
  1447. && ira_may_move_in_cost[mode] == NULL
  1448. && ira_may_move_out_cost[mode] == NULL);
  1449. ira_assert (have_regs_of_mode[mode]);
  1450. for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
  1451. for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
  1452. {
  1453. int cost;
  1454. if (!contains_reg_of_mode[cl1][mode]
  1455. || !contains_reg_of_mode[cl2][mode])
  1456. {
  1457. if ((ira_reg_class_max_nregs[cl1][mode]
  1458. > ira_class_hard_regs_num[cl1])
  1459. || (ira_reg_class_max_nregs[cl2][mode]
  1460. > ira_class_hard_regs_num[cl2]))
  1461. cost = 65535;
  1462. else
  1463. cost = (ira_memory_move_cost[mode][cl1][0]
  1464. + ira_memory_move_cost[mode][cl2][1]) * 2;
  1465. }
  1466. else
  1467. {
  1468. cost = register_move_cost (mode, (enum reg_class) cl1,
  1469. (enum reg_class) cl2);
  1470. ira_assert (cost < 65535);
  1471. }
  1472. all_match &= (last_move_cost[cl1][cl2] == cost);
  1473. last_move_cost[cl1][cl2] = cost;
  1474. }
  1475. if (all_match && last_mode_for_init_move_cost != -1)
  1476. {
  1477. ira_register_move_cost[mode]
  1478. = ira_register_move_cost[last_mode_for_init_move_cost];
  1479. ira_may_move_in_cost[mode]
  1480. = ira_may_move_in_cost[last_mode_for_init_move_cost];
  1481. ira_may_move_out_cost[mode]
  1482. = ira_may_move_out_cost[last_mode_for_init_move_cost];
  1483. return;
  1484. }
  1485. last_mode_for_init_move_cost = mode;
  1486. ira_register_move_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
  1487. ira_may_move_in_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
  1488. ira_may_move_out_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
  1489. for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
  1490. for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
  1491. {
  1492. int cost;
  1493. enum reg_class *p1, *p2;
  1494. if (last_move_cost[cl1][cl2] == 65535)
  1495. {
  1496. ira_register_move_cost[mode][cl1][cl2] = 65535;
  1497. ira_may_move_in_cost[mode][cl1][cl2] = 65535;
  1498. ira_may_move_out_cost[mode][cl1][cl2] = 65535;
  1499. }
  1500. else
  1501. {
  1502. cost = last_move_cost[cl1][cl2];
  1503. for (p2 = &reg_class_subclasses[cl2][0];
  1504. *p2 != LIM_REG_CLASSES; p2++)
  1505. if (ira_class_hard_regs_num[*p2] > 0
  1506. && (ira_reg_class_max_nregs[*p2][mode]
  1507. <= ira_class_hard_regs_num[*p2]))
  1508. cost = MAX (cost, ira_register_move_cost[mode][cl1][*p2]);
  1509. for (p1 = &reg_class_subclasses[cl1][0];
  1510. *p1 != LIM_REG_CLASSES; p1++)
  1511. if (ira_class_hard_regs_num[*p1] > 0
  1512. && (ira_reg_class_max_nregs[*p1][mode]
  1513. <= ira_class_hard_regs_num[*p1]))
  1514. cost = MAX (cost, ira_register_move_cost[mode][*p1][cl2]);
  1515. ira_assert (cost <= 65535);
  1516. ira_register_move_cost[mode][cl1][cl2] = cost;
  1517. if (ira_class_subset_p[cl1][cl2])
  1518. ira_may_move_in_cost[mode][cl1][cl2] = 0;
  1519. else
  1520. ira_may_move_in_cost[mode][cl1][cl2] = cost;
  1521. if (ira_class_subset_p[cl2][cl1])
  1522. ira_may_move_out_cost[mode][cl1][cl2] = 0;
  1523. else
  1524. ira_may_move_out_cost[mode][cl1][cl2] = cost;
  1525. }
  1526. }
  1527. }
  1528. /* This is called once during compiler work. It sets up
  1529. different arrays whose values don't depend on the compiled
  1530. function. */
  1531. void
  1532. ira_init_once (void)
  1533. {
  1534. ira_init_costs_once ();
  1535. lra_init_once ();
  1536. }
  1537. /* Free ira_max_register_move_cost, ira_may_move_in_cost and
  1538. ira_may_move_out_cost for each mode. */
  1539. void
  1540. target_ira_int::free_register_move_costs (void)
  1541. {
  1542. int mode, i;
  1543. /* Reset move_cost and friends, making sure we only free shared
  1544. table entries once. */
  1545. for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
  1546. if (x_ira_register_move_cost[mode])
  1547. {
  1548. for (i = 0;
  1549. i < mode && (x_ira_register_move_cost[i]
  1550. != x_ira_register_move_cost[mode]);
  1551. i++)
  1552. ;
  1553. if (i == mode)
  1554. {
  1555. free (x_ira_register_move_cost[mode]);
  1556. free (x_ira_may_move_in_cost[mode]);
  1557. free (x_ira_may_move_out_cost[mode]);
  1558. }
  1559. }
  1560. memset (x_ira_register_move_cost, 0, sizeof x_ira_register_move_cost);
  1561. memset (x_ira_may_move_in_cost, 0, sizeof x_ira_may_move_in_cost);
  1562. memset (x_ira_may_move_out_cost, 0, sizeof x_ira_may_move_out_cost);
  1563. last_mode_for_init_move_cost = -1;
  1564. }
  1565. target_ira_int::~target_ira_int ()
  1566. {
  1567. free_ira_costs ();
  1568. free_register_move_costs ();
  1569. }
  1570. /* This is called every time when register related information is
  1571. changed. */
  1572. void
  1573. ira_init (void)
  1574. {
  1575. this_target_ira_int->free_register_move_costs ();
  1576. setup_reg_mode_hard_regset ();
  1577. setup_alloc_regs (flag_omit_frame_pointer != 0);
  1578. setup_class_subset_and_memory_move_costs ();
  1579. setup_reg_class_nregs ();
  1580. setup_prohibited_class_mode_regs ();
  1581. find_reg_classes ();
  1582. clarify_prohibited_class_mode_regs ();
  1583. setup_hard_regno_aclass ();
  1584. ira_init_costs ();
  1585. }
  1586. #define ira_prohibited_mode_move_regs_initialized_p \
  1587. (this_target_ira_int->x_ira_prohibited_mode_move_regs_initialized_p)
  1588. /* Set up IRA_PROHIBITED_MODE_MOVE_REGS. */
  1589. static void
  1590. setup_prohibited_mode_move_regs (void)
  1591. {
  1592. int i, j;
  1593. rtx test_reg1, test_reg2, move_pat;
  1594. rtx_insn *move_insn;
  1595. if (ira_prohibited_mode_move_regs_initialized_p)
  1596. return;
  1597. ira_prohibited_mode_move_regs_initialized_p = true;
  1598. test_reg1 = gen_rtx_REG (VOIDmode, 0);
  1599. test_reg2 = gen_rtx_REG (VOIDmode, 0);
  1600. move_pat = gen_rtx_SET (VOIDmode, test_reg1, test_reg2);
  1601. move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, move_pat, 0, -1, 0);
  1602. for (i = 0; i < NUM_MACHINE_MODES; i++)
  1603. {
  1604. SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
  1605. for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
  1606. {
  1607. if (! HARD_REGNO_MODE_OK (j, (machine_mode) i))
  1608. continue;
  1609. SET_REGNO_RAW (test_reg1, j);
  1610. PUT_MODE (test_reg1, (machine_mode) i);
  1611. SET_REGNO_RAW (test_reg2, j);
  1612. PUT_MODE (test_reg2, (machine_mode) i);
  1613. INSN_CODE (move_insn) = -1;
  1614. recog_memoized (move_insn);
  1615. if (INSN_CODE (move_insn) < 0)
  1616. continue;
  1617. extract_insn (move_insn);
  1618. /* We don't know whether the move will be in code that is optimized
  1619. for size or speed, so consider all enabled alternatives. */
  1620. if (! constrain_operands (1, get_enabled_alternatives (move_insn)))
  1621. continue;
  1622. CLEAR_HARD_REG_BIT (ira_prohibited_mode_move_regs[i], j);
  1623. }
  1624. }
  1625. }
  1626. /* Setup possible alternatives in ALTS for INSN. */
  1627. void
  1628. ira_setup_alts (rtx_insn *insn, HARD_REG_SET &alts)
  1629. {
  1630. /* MAP nalt * nop -> start of constraints for given operand and
  1631. alternative. */
  1632. static vec<const char *> insn_constraints;
  1633. int nop, nalt;
  1634. bool curr_swapped;
  1635. const char *p;
  1636. rtx op;
  1637. int commutative = -1;
  1638. extract_insn (insn);
  1639. alternative_mask preferred = get_preferred_alternatives (insn);
  1640. CLEAR_HARD_REG_SET (alts);
  1641. insn_constraints.release ();
  1642. insn_constraints.safe_grow_cleared (recog_data.n_operands
  1643. * recog_data.n_alternatives + 1);
  1644. /* Check that the hard reg set is enough for holding all
  1645. alternatives. It is hard to imagine the situation when the
  1646. assertion is wrong. */
  1647. ira_assert (recog_data.n_alternatives
  1648. <= (int) MAX (sizeof (HARD_REG_ELT_TYPE) * CHAR_BIT,
  1649. FIRST_PSEUDO_REGISTER));
  1650. for (curr_swapped = false;; curr_swapped = true)
  1651. {
  1652. /* Calculate some data common for all alternatives to speed up the
  1653. function. */
  1654. for (nop = 0; nop < recog_data.n_operands; nop++)
  1655. {
  1656. for (nalt = 0, p = recog_data.constraints[nop];
  1657. nalt < recog_data.n_alternatives;
  1658. nalt++)
  1659. {
  1660. insn_constraints[nop * recog_data.n_alternatives + nalt] = p;
  1661. while (*p && *p != ',')
  1662. p++;
  1663. if (*p)
  1664. p++;
  1665. }
  1666. }
  1667. for (nalt = 0; nalt < recog_data.n_alternatives; nalt++)
  1668. {
  1669. if (!TEST_BIT (preferred, nalt)
  1670. || TEST_HARD_REG_BIT (alts, nalt))
  1671. continue;
  1672. for (nop = 0; nop < recog_data.n_operands; nop++)
  1673. {
  1674. int c, len;
  1675. op = recog_data.operand[nop];
  1676. p = insn_constraints[nop * recog_data.n_alternatives + nalt];
  1677. if (*p == 0 || *p == ',')
  1678. continue;
  1679. do
  1680. switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
  1681. {
  1682. case '#':
  1683. case ',':
  1684. c = '\0';
  1685. case '\0':
  1686. len = 0;
  1687. break;
  1688. case '%':
  1689. /* We only support one commutative marker, the
  1690. first one. We already set commutative
  1691. above. */
  1692. if (commutative < 0)
  1693. commutative = nop;
  1694. break;
  1695. case '0': case '1': case '2': case '3': case '4':
  1696. case '5': case '6': case '7': case '8': case '9':
  1697. goto op_success;
  1698. break;
  1699. case 'g':
  1700. goto op_success;
  1701. break;
  1702. default:
  1703. {
  1704. enum constraint_num cn = lookup_constraint (p);
  1705. switch (get_constraint_type (cn))
  1706. {
  1707. case CT_REGISTER:
  1708. if (reg_class_for_constraint (cn) != NO_REGS)
  1709. goto op_success;
  1710. break;
  1711. case CT_CONST_INT:
  1712. if (CONST_INT_P (op)
  1713. && (insn_const_int_ok_for_constraint
  1714. (INTVAL (op), cn)))
  1715. goto op_success;
  1716. break;
  1717. case CT_ADDRESS:
  1718. case CT_MEMORY:
  1719. goto op_success;
  1720. case CT_FIXED_FORM:
  1721. if (constraint_satisfied_p (op, cn))
  1722. goto op_success;
  1723. break;
  1724. }
  1725. break;
  1726. }
  1727. }
  1728. while (p += len, c);
  1729. break;
  1730. op_success:
  1731. ;
  1732. }
  1733. if (nop >= recog_data.n_operands)
  1734. SET_HARD_REG_BIT (alts, nalt);
  1735. }
  1736. if (commutative < 0)
  1737. break;
  1738. if (curr_swapped)
  1739. break;
  1740. op = recog_data.operand[commutative];
  1741. recog_data.operand[commutative] = recog_data.operand[commutative + 1];
  1742. recog_data.operand[commutative + 1] = op;
  1743. }
  1744. }
  1745. /* Return the number of the output non-early clobber operand which
  1746. should be the same in any case as operand with number OP_NUM (or
  1747. negative value if there is no such operand). The function takes
  1748. only really possible alternatives into consideration. */
  1749. int
  1750. ira_get_dup_out_num (int op_num, HARD_REG_SET &alts)
  1751. {
  1752. int curr_alt, c, original, dup;
  1753. bool ignore_p, use_commut_op_p;
  1754. const char *str;
  1755. if (op_num < 0 || recog_data.n_alternatives == 0)
  1756. return -1;
  1757. /* We should find duplications only for input operands. */
  1758. if (recog_data.operand_type[op_num] != OP_IN)
  1759. return -1;
  1760. str = recog_data.constraints[op_num];
  1761. use_commut_op_p = false;
  1762. for (;;)
  1763. {
  1764. rtx op = recog_data.operand[op_num];
  1765. for (curr_alt = 0, ignore_p = !TEST_HARD_REG_BIT (alts, curr_alt),
  1766. original = -1;;)
  1767. {
  1768. c = *str;
  1769. if (c == '\0')
  1770. break;
  1771. if (c == '#')
  1772. ignore_p = true;
  1773. else if (c == ',')
  1774. {
  1775. curr_alt++;
  1776. ignore_p = !TEST_HARD_REG_BIT (alts, curr_alt);
  1777. }
  1778. else if (! ignore_p)
  1779. switch (c)
  1780. {
  1781. case 'g':
  1782. goto fail;
  1783. default:
  1784. {
  1785. enum constraint_num cn = lookup_constraint (str);
  1786. enum reg_class cl = reg_class_for_constraint (cn);
  1787. if (cl != NO_REGS
  1788. && !targetm.class_likely_spilled_p (cl))
  1789. goto fail;
  1790. if (constraint_satisfied_p (op, cn))
  1791. goto fail;
  1792. break;
  1793. }
  1794. case '0': case '1': case '2': case '3': case '4':
  1795. case '5': case '6': case '7': case '8': case '9':
  1796. if (original != -1 && original != c)
  1797. goto fail;
  1798. original = c;
  1799. break;
  1800. }
  1801. str += CONSTRAINT_LEN (c, str);
  1802. }
  1803. if (original == -1)
  1804. goto fail;
  1805. dup = -1;
  1806. for (ignore_p = false, str = recog_data.constraints[original - '0'];
  1807. *str != 0;
  1808. str++)
  1809. if (ignore_p)
  1810. {
  1811. if (*str == ',')
  1812. ignore_p = false;
  1813. }
  1814. else if (*str == '#')
  1815. ignore_p = true;
  1816. else if (! ignore_p)
  1817. {
  1818. if (*str == '=')
  1819. dup = original - '0';
  1820. /* It is better ignore an alternative with early clobber. */
  1821. else if (*str == '&')
  1822. goto fail;
  1823. }
  1824. if (dup >= 0)
  1825. return dup;
  1826. fail:
  1827. if (use_commut_op_p)
  1828. break;
  1829. use_commut_op_p = true;
  1830. if (recog_data.constraints[op_num][0] == '%')
  1831. str = recog_data.constraints[op_num + 1];
  1832. else if (op_num > 0 && recog_data.constraints[op_num - 1][0] == '%')
  1833. str = recog_data.constraints[op_num - 1];
  1834. else
  1835. break;
  1836. }
  1837. return -1;
  1838. }
  1839. /* Search forward to see if the source register of a copy insn dies
  1840. before either it or the destination register is modified, but don't
  1841. scan past the end of the basic block. If so, we can replace the
  1842. source with the destination and let the source die in the copy
  1843. insn.
  1844. This will reduce the number of registers live in that range and may
  1845. enable the destination and the source coalescing, thus often saving
  1846. one register in addition to a register-register copy. */
  1847. static void
  1848. decrease_live_ranges_number (void)
  1849. {
  1850. basic_block bb;
  1851. rtx_insn *insn;
  1852. rtx set, src, dest, dest_death, q, note;
  1853. rtx_insn *p;
  1854. int sregno, dregno;
  1855. if (! flag_expensive_optimizations)
  1856. return;
  1857. if (ira_dump_file)
  1858. fprintf (ira_dump_file, "Starting decreasing number of live ranges...\n");
  1859. FOR_EACH_BB_FN (bb, cfun)
  1860. FOR_BB_INSNS (bb, insn)
  1861. {
  1862. set = single_set (insn);
  1863. if (! set)
  1864. continue;
  1865. src = SET_SRC (set);
  1866. dest = SET_DEST (set);
  1867. if (! REG_P (src) || ! REG_P (dest)
  1868. || find_reg_note (insn, REG_DEAD, src))
  1869. continue;
  1870. sregno = REGNO (src);
  1871. dregno = REGNO (dest);
  1872. /* We don't want to mess with hard regs if register classes
  1873. are small. */
  1874. if (sregno == dregno
  1875. || (targetm.small_register_classes_for_mode_p (GET_MODE (src))
  1876. && (sregno < FIRST_PSEUDO_REGISTER
  1877. || dregno < FIRST_PSEUDO_REGISTER))
  1878. /* We don't see all updates to SP if they are in an
  1879. auto-inc memory reference, so we must disallow this
  1880. optimization on them. */
  1881. || sregno == STACK_POINTER_REGNUM
  1882. || dregno == STACK_POINTER_REGNUM)
  1883. continue;
  1884. dest_death = NULL_RTX;
  1885. for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
  1886. {
  1887. if (! INSN_P (p))
  1888. continue;
  1889. if (BLOCK_FOR_INSN (p) != bb)
  1890. break;
  1891. if (reg_set_p (src, p) || reg_set_p (dest, p)
  1892. /* If SRC is an asm-declared register, it must not be
  1893. replaced in any asm. Unfortunately, the REG_EXPR
  1894. tree for the asm variable may be absent in the SRC
  1895. rtx, so we can't check the actual register
  1896. declaration easily (the asm operand will have it,
  1897. though). To avoid complicating the test for a rare
  1898. case, we just don't perform register replacement
  1899. for a hard reg mentioned in an asm. */
  1900. || (sregno < FIRST_PSEUDO_REGISTER
  1901. && asm_noperands (PATTERN (p)) >= 0
  1902. && reg_overlap_mentioned_p (src, PATTERN (p)))
  1903. /* Don't change hard registers used by a call. */
  1904. || (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
  1905. && find_reg_fusage (p, USE, src))
  1906. /* Don't change a USE of a register. */
  1907. || (GET_CODE (PATTERN (p)) == USE
  1908. && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
  1909. break;
  1910. /* See if all of SRC dies in P. This test is slightly
  1911. more conservative than it needs to be. */
  1912. if ((note = find_regno_note (p, REG_DEAD, sregno))
  1913. && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
  1914. {
  1915. int failed = 0;
  1916. /* We can do the optimization. Scan forward from INSN
  1917. again, replacing regs as we go. Set FAILED if a
  1918. replacement can't be done. In that case, we can't
  1919. move the death note for SRC. This should be
  1920. rare. */
  1921. /* Set to stop at next insn. */
  1922. for (q = next_real_insn (insn);
  1923. q != next_real_insn (p);
  1924. q = next_real_insn (q))
  1925. {
  1926. if (reg_overlap_mentioned_p (src, PATTERN (q)))
  1927. {
  1928. /* If SRC is a hard register, we might miss
  1929. some overlapping registers with
  1930. validate_replace_rtx, so we would have to
  1931. undo it. We can't if DEST is present in
  1932. the insn, so fail in that combination of
  1933. cases. */
  1934. if (sregno < FIRST_PSEUDO_REGISTER
  1935. && reg_mentioned_p (dest, PATTERN (q)))
  1936. failed = 1;
  1937. /* Attempt to replace all uses. */
  1938. else if (!validate_replace_rtx (src, dest, q))
  1939. failed = 1;
  1940. /* If this succeeded, but some part of the
  1941. register is still present, undo the
  1942. replacement. */
  1943. else if (sregno < FIRST_PSEUDO_REGISTER
  1944. && reg_overlap_mentioned_p (src, PATTERN (q)))
  1945. {
  1946. validate_replace_rtx (dest, src, q);
  1947. failed = 1;
  1948. }
  1949. }
  1950. /* If DEST dies here, remove the death note and
  1951. save it for later. Make sure ALL of DEST dies
  1952. here; again, this is overly conservative. */
  1953. if (! dest_death
  1954. && (dest_death = find_regno_note (q, REG_DEAD, dregno)))
  1955. {
  1956. if (GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
  1957. remove_note (q, dest_death);
  1958. else
  1959. {
  1960. failed = 1;
  1961. dest_death = 0;
  1962. }
  1963. }
  1964. }
  1965. if (! failed)
  1966. {
  1967. /* Move death note of SRC from P to INSN. */
  1968. remove_note (p, note);
  1969. XEXP (note, 1) = REG_NOTES (insn);
  1970. REG_NOTES (insn) = note;
  1971. }
  1972. /* DEST is also dead if INSN has a REG_UNUSED note for
  1973. DEST. */
  1974. if (! dest_death
  1975. && (dest_death
  1976. = find_regno_note (insn, REG_UNUSED, dregno)))
  1977. {
  1978. PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
  1979. remove_note (insn, dest_death);
  1980. }
  1981. /* Put death note of DEST on P if we saw it die. */
  1982. if (dest_death)
  1983. {
  1984. XEXP (dest_death, 1) = REG_NOTES (p);
  1985. REG_NOTES (p) = dest_death;
  1986. }
  1987. break;
  1988. }
  1989. /* If SRC is a hard register which is set or killed in
  1990. some other way, we can't do this optimization. */
  1991. else if (sregno < FIRST_PSEUDO_REGISTER && dead_or_set_p (p, src))
  1992. break;
  1993. }
  1994. }
  1995. }
  1996. /* Return nonzero if REGNO is a particularly bad choice for reloading X. */
  1997. static bool
  1998. ira_bad_reload_regno_1 (int regno, rtx x)
  1999. {
  2000. int x_regno, n, i;
  2001. ira_allocno_t a;
  2002. enum reg_class pref;
  2003. /* We only deal with pseudo regs. */
  2004. if (! x || GET_CODE (x) != REG)
  2005. return false;
  2006. x_regno = REGNO (x);
  2007. if (x_regno < FIRST_PSEUDO_REGISTER)
  2008. return false;
  2009. /* If the pseudo prefers REGNO explicitly, then do not consider
  2010. REGNO a bad spill choice. */
  2011. pref = reg_preferred_class (x_regno);
  2012. if (reg_class_size[pref] == 1)
  2013. return !TEST_HARD_REG_BIT (reg_class_contents[pref], regno);
  2014. /* If the pseudo conflicts with REGNO, then we consider REGNO a
  2015. poor choice for a reload regno. */
  2016. a = ira_regno_allocno_map[x_regno];
  2017. n = ALLOCNO_NUM_OBJECTS (a);
  2018. for (i = 0; i < n; i++)
  2019. {
  2020. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  2021. if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno))
  2022. return true;
  2023. }
  2024. return false;
  2025. }
  2026. /* Return nonzero if REGNO is a particularly bad choice for reloading
  2027. IN or OUT. */
  2028. bool
  2029. ira_bad_reload_regno (int regno, rtx in, rtx out)
  2030. {
  2031. return (ira_bad_reload_regno_1 (regno, in)
  2032. || ira_bad_reload_regno_1 (regno, out));
  2033. }
  2034. /* Add register clobbers from asm statements. */
  2035. static void
  2036. compute_regs_asm_clobbered (void)
  2037. {
  2038. basic_block bb;
  2039. FOR_EACH_BB_FN (bb, cfun)
  2040. {
  2041. rtx_insn *insn;
  2042. FOR_BB_INSNS_REVERSE (bb, insn)
  2043. {
  2044. df_ref def;
  2045. if (NONDEBUG_INSN_P (insn) && extract_asm_operands (PATTERN (insn)))
  2046. FOR_EACH_INSN_DEF (def, insn)
  2047. {
  2048. unsigned int dregno = DF_REF_REGNO (def);
  2049. if (HARD_REGISTER_NUM_P (dregno))
  2050. add_to_hard_reg_set (&crtl->asm_clobbers,
  2051. GET_MODE (DF_REF_REAL_REG (def)),
  2052. dregno);
  2053. }
  2054. }
  2055. }
  2056. }
  2057. /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and
  2058. REGS_EVER_LIVE. */
  2059. void
  2060. ira_setup_eliminable_regset (void)
  2061. {
  2062. #ifdef ELIMINABLE_REGS
  2063. int i;
  2064. static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
  2065. #endif
  2066. /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
  2067. sp for alloca. So we can't eliminate the frame pointer in that
  2068. case. At some point, we should improve this by emitting the
  2069. sp-adjusting insns for this case. */
  2070. frame_pointer_needed
  2071. = (! flag_omit_frame_pointer
  2072. || (cfun->calls_alloca && EXIT_IGNORE_STACK)
  2073. /* We need the frame pointer to catch stack overflow exceptions
  2074. if the stack pointer is moving. */
  2075. || (flag_stack_check && STACK_CHECK_MOVING_SP)
  2076. || crtl->accesses_prior_frames
  2077. || (SUPPORTS_STACK_ALIGNMENT && crtl->stack_realign_needed)
  2078. /* We need a frame pointer for all Cilk Plus functions that use
  2079. Cilk keywords. */
  2080. || (flag_cilkplus && cfun->is_cilk_function)
  2081. || targetm.frame_pointer_required ());
  2082. /* The chance that FRAME_POINTER_NEEDED is changed from inspecting
  2083. RTL is very small. So if we use frame pointer for RA and RTL
  2084. actually prevents this, we will spill pseudos assigned to the
  2085. frame pointer in LRA. */
  2086. if (frame_pointer_needed)
  2087. df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
  2088. COPY_HARD_REG_SET (ira_no_alloc_regs, no_unit_alloc_regs);
  2089. CLEAR_HARD_REG_SET (eliminable_regset);
  2090. compute_regs_asm_clobbered ();
  2091. /* Build the regset of all eliminable registers and show we can't
  2092. use those that we already know won't be eliminated. */
  2093. #ifdef ELIMINABLE_REGS
  2094. for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
  2095. {
  2096. bool cannot_elim
  2097. = (! targetm.can_eliminate (eliminables[i].from, eliminables[i].to)
  2098. || (eliminables[i].to == STACK_POINTER_REGNUM && frame_pointer_needed));
  2099. if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, eliminables[i].from))
  2100. {
  2101. SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
  2102. if (cannot_elim)
  2103. SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
  2104. }
  2105. else if (cannot_elim)
  2106. error ("%s cannot be used in asm here",
  2107. reg_names[eliminables[i].from]);
  2108. else
  2109. df_set_regs_ever_live (eliminables[i].from, true);
  2110. }
  2111. #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
  2112. if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
  2113. {
  2114. SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
  2115. if (frame_pointer_needed)
  2116. SET_HARD_REG_BIT (ira_no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
  2117. }
  2118. else if (frame_pointer_needed)
  2119. error ("%s cannot be used in asm here",
  2120. reg_names[HARD_FRAME_POINTER_REGNUM]);
  2121. else
  2122. df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
  2123. #endif
  2124. #else
  2125. if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
  2126. {
  2127. SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
  2128. if (frame_pointer_needed)
  2129. SET_HARD_REG_BIT (ira_no_alloc_regs, FRAME_POINTER_REGNUM);
  2130. }
  2131. else if (frame_pointer_needed)
  2132. error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
  2133. else
  2134. df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
  2135. #endif
  2136. }
  2137. /* Vector of substitutions of register numbers,
  2138. used to map pseudo regs into hardware regs.
  2139. This is set up as a result of register allocation.
  2140. Element N is the hard reg assigned to pseudo reg N,
  2141. or is -1 if no hard reg was assigned.
  2142. If N is a hard reg number, element N is N. */
  2143. short *reg_renumber;
  2144. /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
  2145. the allocation found by IRA. */
  2146. static void
  2147. setup_reg_renumber (void)
  2148. {
  2149. int regno, hard_regno;
  2150. ira_allocno_t a;
  2151. ira_allocno_iterator ai;
  2152. caller_save_needed = 0;
  2153. FOR_EACH_ALLOCNO (a, ai)
  2154. {
  2155. if (ira_use_lra_p && ALLOCNO_CAP_MEMBER (a) != NULL)
  2156. continue;
  2157. /* There are no caps at this point. */
  2158. ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
  2159. if (! ALLOCNO_ASSIGNED_P (a))
  2160. /* It can happen if A is not referenced but partially anticipated
  2161. somewhere in a region. */
  2162. ALLOCNO_ASSIGNED_P (a) = true;
  2163. ira_free_allocno_updated_costs (a);
  2164. hard_regno = ALLOCNO_HARD_REGNO (a);
  2165. regno = ALLOCNO_REGNO (a);
  2166. reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
  2167. if (hard_regno >= 0)
  2168. {
  2169. int i, nwords;
  2170. enum reg_class pclass;
  2171. ira_object_t obj;
  2172. pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
  2173. nwords = ALLOCNO_NUM_OBJECTS (a);
  2174. for (i = 0; i < nwords; i++)
  2175. {
  2176. obj = ALLOCNO_OBJECT (a, i);
  2177. IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
  2178. reg_class_contents[pclass]);
  2179. }
  2180. if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
  2181. && ira_hard_reg_set_intersection_p (hard_regno, ALLOCNO_MODE (a),
  2182. call_used_reg_set))
  2183. {
  2184. ira_assert (!optimize || flag_caller_saves
  2185. || (ALLOCNO_CALLS_CROSSED_NUM (a)
  2186. == ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a))
  2187. || regno >= ira_reg_equiv_len
  2188. || ira_equiv_no_lvalue_p (regno));
  2189. caller_save_needed = 1;
  2190. }
  2191. }
  2192. }
  2193. }
  2194. /* Set up allocno assignment flags for further allocation
  2195. improvements. */
  2196. static void
  2197. setup_allocno_assignment_flags (void)
  2198. {
  2199. int hard_regno;
  2200. ira_allocno_t a;
  2201. ira_allocno_iterator ai;
  2202. FOR_EACH_ALLOCNO (a, ai)
  2203. {
  2204. if (! ALLOCNO_ASSIGNED_P (a))
  2205. /* It can happen if A is not referenced but partially anticipated
  2206. somewhere in a region. */
  2207. ira_free_allocno_updated_costs (a);
  2208. hard_regno = ALLOCNO_HARD_REGNO (a);
  2209. /* Don't assign hard registers to allocnos which are destination
  2210. of removed store at the end of loop. It has no sense to keep
  2211. the same value in different hard registers. It is also
  2212. impossible to assign hard registers correctly to such
  2213. allocnos because the cost info and info about intersected
  2214. calls are incorrect for them. */
  2215. ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
  2216. || ALLOCNO_EMIT_DATA (a)->mem_optimized_dest_p
  2217. || (ALLOCNO_MEMORY_COST (a)
  2218. - ALLOCNO_CLASS_COST (a)) < 0);
  2219. ira_assert
  2220. (hard_regno < 0
  2221. || ira_hard_reg_in_set_p (hard_regno, ALLOCNO_MODE (a),
  2222. reg_class_contents[ALLOCNO_CLASS (a)]));
  2223. }
  2224. }
  2225. /* Evaluate overall allocation cost and the costs for using hard
  2226. registers and memory for allocnos. */
  2227. static void
  2228. calculate_allocation_cost (void)
  2229. {
  2230. int hard_regno, cost;
  2231. ira_allocno_t a;
  2232. ira_allocno_iterator ai;
  2233. ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
  2234. FOR_EACH_ALLOCNO (a, ai)
  2235. {
  2236. hard_regno = ALLOCNO_HARD_REGNO (a);
  2237. ira_assert (hard_regno < 0
  2238. || (ira_hard_reg_in_set_p
  2239. (hard_regno, ALLOCNO_MODE (a),
  2240. reg_class_contents[ALLOCNO_CLASS (a)])));
  2241. if (hard_regno < 0)
  2242. {
  2243. cost = ALLOCNO_MEMORY_COST (a);
  2244. ira_mem_cost += cost;
  2245. }
  2246. else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
  2247. {
  2248. cost = (ALLOCNO_HARD_REG_COSTS (a)
  2249. [ira_class_hard_reg_index
  2250. [ALLOCNO_CLASS (a)][hard_regno]]);
  2251. ira_reg_cost += cost;
  2252. }
  2253. else
  2254. {
  2255. cost = ALLOCNO_CLASS_COST (a);
  2256. ira_reg_cost += cost;
  2257. }
  2258. ira_overall_cost += cost;
  2259. }
  2260. if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
  2261. {
  2262. fprintf (ira_dump_file,
  2263. "+++Costs: overall %"PRId64
  2264. ", reg %"PRId64
  2265. ", mem %"PRId64
  2266. ", ld %"PRId64
  2267. ", st %"PRId64
  2268. ", move %"PRId64,
  2269. ira_overall_cost, ira_reg_cost, ira_mem_cost,
  2270. ira_load_cost, ira_store_cost, ira_shuffle_cost);
  2271. fprintf (ira_dump_file, "\n+++ move loops %d, new jumps %d\n",
  2272. ira_move_loops_num, ira_additional_jumps_num);
  2273. }
  2274. }
  2275. #ifdef ENABLE_IRA_CHECKING
  2276. /* Check the correctness of the allocation. We do need this because
  2277. of complicated code to transform more one region internal
  2278. representation into one region representation. */
  2279. static void
  2280. check_allocation (void)
  2281. {
  2282. ira_allocno_t a;
  2283. int hard_regno, nregs, conflict_nregs;
  2284. ira_allocno_iterator ai;
  2285. FOR_EACH_ALLOCNO (a, ai)
  2286. {
  2287. int n = ALLOCNO_NUM_OBJECTS (a);
  2288. int i;
  2289. if (ALLOCNO_CAP_MEMBER (a) != NULL
  2290. || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
  2291. continue;
  2292. nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
  2293. if (nregs == 1)
  2294. /* We allocated a single hard register. */
  2295. n = 1;
  2296. else if (n > 1)
  2297. /* We allocated multiple hard registers, and we will test
  2298. conflicts in a granularity of single hard regs. */
  2299. nregs = 1;
  2300. for (i = 0; i < n; i++)
  2301. {
  2302. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  2303. ira_object_t conflict_obj;
  2304. ira_object_conflict_iterator oci;
  2305. int this_regno = hard_regno;
  2306. if (n > 1)
  2307. {
  2308. if (REG_WORDS_BIG_ENDIAN)
  2309. this_regno += n - i - 1;
  2310. else
  2311. this_regno += i;
  2312. }
  2313. FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
  2314. {
  2315. ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
  2316. int conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
  2317. if (conflict_hard_regno < 0)
  2318. continue;
  2319. conflict_nregs
  2320. = (hard_regno_nregs
  2321. [conflict_hard_regno][ALLOCNO_MODE (conflict_a)]);
  2322. if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1
  2323. && conflict_nregs == ALLOCNO_NUM_OBJECTS (conflict_a))
  2324. {
  2325. if (REG_WORDS_BIG_ENDIAN)
  2326. conflict_hard_regno += (ALLOCNO_NUM_OBJECTS (conflict_a)
  2327. - OBJECT_SUBWORD (conflict_obj) - 1);
  2328. else
  2329. conflict_hard_regno += OBJECT_SUBWORD (conflict_obj);
  2330. conflict_nregs = 1;
  2331. }
  2332. if ((conflict_hard_regno <= this_regno
  2333. && this_regno < conflict_hard_regno + conflict_nregs)
  2334. || (this_regno <= conflict_hard_regno
  2335. && conflict_hard_regno < this_regno + nregs))
  2336. {
  2337. fprintf (stderr, "bad allocation for %d and %d\n",
  2338. ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
  2339. gcc_unreachable ();
  2340. }
  2341. }
  2342. }
  2343. }
  2344. }
  2345. #endif
  2346. /* Allocate REG_EQUIV_INIT. Set up it from IRA_REG_EQUIV which should
  2347. be already calculated. */
  2348. static void
  2349. setup_reg_equiv_init (void)
  2350. {
  2351. int i;
  2352. int max_regno = max_reg_num ();
  2353. for (i = 0; i < max_regno; i++)
  2354. reg_equiv_init (i) = ira_reg_equiv[i].init_insns;
  2355. }
  2356. /* Update equiv regno from movement of FROM_REGNO to TO_REGNO. INSNS
  2357. are insns which were generated for such movement. It is assumed
  2358. that FROM_REGNO and TO_REGNO always have the same value at the
  2359. point of any move containing such registers. This function is used
  2360. to update equiv info for register shuffles on the region borders
  2361. and for caller save/restore insns. */
  2362. void
  2363. ira_update_equiv_info_by_shuffle_insn (int to_regno, int from_regno, rtx_insn *insns)
  2364. {
  2365. rtx_insn *insn;
  2366. rtx x, note;
  2367. if (! ira_reg_equiv[from_regno].defined_p
  2368. && (! ira_reg_equiv[to_regno].defined_p
  2369. || ((x = ira_reg_equiv[to_regno].memory) != NULL_RTX
  2370. && ! MEM_READONLY_P (x))))
  2371. return;
  2372. insn = insns;
  2373. if (NEXT_INSN (insn) != NULL_RTX)
  2374. {
  2375. if (! ira_reg_equiv[to_regno].defined_p)
  2376. {
  2377. ira_assert (ira_reg_equiv[to_regno].init_insns == NULL_RTX);
  2378. return;
  2379. }
  2380. ira_reg_equiv[to_regno].defined_p = false;
  2381. ira_reg_equiv[to_regno].memory
  2382. = ira_reg_equiv[to_regno].constant
  2383. = ira_reg_equiv[to_regno].invariant
  2384. = ira_reg_equiv[to_regno].init_insns = NULL;
  2385. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2386. fprintf (ira_dump_file,
  2387. " Invalidating equiv info for reg %d\n", to_regno);
  2388. return;
  2389. }
  2390. /* It is possible that FROM_REGNO still has no equivalence because
  2391. in shuffles to_regno<-from_regno and from_regno<-to_regno the 2nd
  2392. insn was not processed yet. */
  2393. if (ira_reg_equiv[from_regno].defined_p)
  2394. {
  2395. ira_reg_equiv[to_regno].defined_p = true;
  2396. if ((x = ira_reg_equiv[from_regno].memory) != NULL_RTX)
  2397. {
  2398. ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX
  2399. && ira_reg_equiv[from_regno].constant == NULL_RTX);
  2400. ira_assert (ira_reg_equiv[to_regno].memory == NULL_RTX
  2401. || rtx_equal_p (ira_reg_equiv[to_regno].memory, x));
  2402. ira_reg_equiv[to_regno].memory = x;
  2403. if (! MEM_READONLY_P (x))
  2404. /* We don't add the insn to insn init list because memory
  2405. equivalence is just to say what memory is better to use
  2406. when the pseudo is spilled. */
  2407. return;
  2408. }
  2409. else if ((x = ira_reg_equiv[from_regno].constant) != NULL_RTX)
  2410. {
  2411. ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX);
  2412. ira_assert (ira_reg_equiv[to_regno].constant == NULL_RTX
  2413. || rtx_equal_p (ira_reg_equiv[to_regno].constant, x));
  2414. ira_reg_equiv[to_regno].constant = x;
  2415. }
  2416. else
  2417. {
  2418. x = ira_reg_equiv[from_regno].invariant;
  2419. ira_assert (x != NULL_RTX);
  2420. ira_assert (ira_reg_equiv[to_regno].invariant == NULL_RTX
  2421. || rtx_equal_p (ira_reg_equiv[to_regno].invariant, x));
  2422. ira_reg_equiv[to_regno].invariant = x;
  2423. }
  2424. if (find_reg_note (insn, REG_EQUIV, x) == NULL_RTX)
  2425. {
  2426. note = set_unique_reg_note (insn, REG_EQUIV, x);
  2427. gcc_assert (note != NULL_RTX);
  2428. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2429. {
  2430. fprintf (ira_dump_file,
  2431. " Adding equiv note to insn %u for reg %d ",
  2432. INSN_UID (insn), to_regno);
  2433. dump_value_slim (ira_dump_file, x, 1);
  2434. fprintf (ira_dump_file, "\n");
  2435. }
  2436. }
  2437. }
  2438. ira_reg_equiv[to_regno].init_insns
  2439. = gen_rtx_INSN_LIST (VOIDmode, insn,
  2440. ira_reg_equiv[to_regno].init_insns);
  2441. if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
  2442. fprintf (ira_dump_file,
  2443. " Adding equiv init move insn %u to reg %d\n",
  2444. INSN_UID (insn), to_regno);
  2445. }
  2446. /* Fix values of array REG_EQUIV_INIT after live range splitting done
  2447. by IRA. */
  2448. static void
  2449. fix_reg_equiv_init (void)
  2450. {
  2451. int max_regno = max_reg_num ();
  2452. int i, new_regno, max;
  2453. rtx x, prev, next, insn, set;
  2454. if (max_regno_before_ira < max_regno)
  2455. {
  2456. max = vec_safe_length (reg_equivs);
  2457. grow_reg_equivs ();
  2458. for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
  2459. for (prev = NULL_RTX, x = reg_equiv_init (i);
  2460. x != NULL_RTX;
  2461. x = next)
  2462. {
  2463. next = XEXP (x, 1);
  2464. insn = XEXP (x, 0);
  2465. set = single_set (as_a <rtx_insn *> (insn));
  2466. ira_assert (set != NULL_RTX
  2467. && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
  2468. if (REG_P (SET_DEST (set))
  2469. && ((int) REGNO (SET_DEST (set)) == i
  2470. || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
  2471. new_regno = REGNO (SET_DEST (set));
  2472. else if (REG_P (SET_SRC (set))
  2473. && ((int) REGNO (SET_SRC (set)) == i
  2474. || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
  2475. new_regno = REGNO (SET_SRC (set));
  2476. else
  2477. gcc_unreachable ();
  2478. if (new_regno == i)
  2479. prev = x;
  2480. else
  2481. {
  2482. /* Remove the wrong list element. */
  2483. if (prev == NULL_RTX)
  2484. reg_equiv_init (i) = next;
  2485. else
  2486. XEXP (prev, 1) = next;
  2487. XEXP (x, 1) = reg_equiv_init (new_regno);
  2488. reg_equiv_init (new_regno) = x;
  2489. }
  2490. }
  2491. }
  2492. }
  2493. #ifdef ENABLE_IRA_CHECKING
  2494. /* Print redundant memory-memory copies. */
  2495. static void
  2496. print_redundant_copies (void)
  2497. {
  2498. int hard_regno;
  2499. ira_allocno_t a;
  2500. ira_copy_t cp, next_cp;
  2501. ira_allocno_iterator ai;
  2502. FOR_EACH_ALLOCNO (a, ai)
  2503. {
  2504. if (ALLOCNO_CAP_MEMBER (a) != NULL)
  2505. /* It is a cap. */
  2506. continue;
  2507. hard_regno = ALLOCNO_HARD_REGNO (a);
  2508. if (hard_regno >= 0)
  2509. continue;
  2510. for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
  2511. if (cp->first == a)
  2512. next_cp = cp->next_first_allocno_copy;
  2513. else
  2514. {
  2515. next_cp = cp->next_second_allocno_copy;
  2516. if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
  2517. && cp->insn != NULL_RTX
  2518. && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
  2519. fprintf (ira_dump_file,
  2520. " Redundant move from %d(freq %d):%d\n",
  2521. INSN_UID (cp->insn), cp->freq, hard_regno);
  2522. }
  2523. }
  2524. }
  2525. #endif
  2526. /* Setup preferred and alternative classes for new pseudo-registers
  2527. created by IRA starting with START. */
  2528. static void
  2529. setup_preferred_alternate_classes_for_new_pseudos (int start)
  2530. {
  2531. int i, old_regno;
  2532. int max_regno = max_reg_num ();
  2533. for (i = start; i < max_regno; i++)
  2534. {
  2535. old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
  2536. ira_assert (i != old_regno);
  2537. setup_reg_classes (i, reg_preferred_class (old_regno),
  2538. reg_alternate_class (old_regno),
  2539. reg_allocno_class (old_regno));
  2540. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  2541. fprintf (ira_dump_file,
  2542. " New r%d: setting preferred %s, alternative %s\n",
  2543. i, reg_class_names[reg_preferred_class (old_regno)],
  2544. reg_class_names[reg_alternate_class (old_regno)]);
  2545. }
  2546. }
  2547. /* The number of entries allocated in reg_info. */
  2548. static int allocated_reg_info_size;
  2549. /* Regional allocation can create new pseudo-registers. This function
  2550. expands some arrays for pseudo-registers. */
  2551. static void
  2552. expand_reg_info (void)
  2553. {
  2554. int i;
  2555. int size = max_reg_num ();
  2556. resize_reg_info ();
  2557. for (i = allocated_reg_info_size; i < size; i++)
  2558. setup_reg_classes (i, GENERAL_REGS, ALL_REGS, GENERAL_REGS);
  2559. setup_preferred_alternate_classes_for_new_pseudos (allocated_reg_info_size);
  2560. allocated_reg_info_size = size;
  2561. }
  2562. /* Return TRUE if there is too high register pressure in the function.
  2563. It is used to decide when stack slot sharing is worth to do. */
  2564. static bool
  2565. too_high_register_pressure_p (void)
  2566. {
  2567. int i;
  2568. enum reg_class pclass;
  2569. for (i = 0; i < ira_pressure_classes_num; i++)
  2570. {
  2571. pclass = ira_pressure_classes[i];
  2572. if (ira_loop_tree_root->reg_pressure[pclass] > 10000)
  2573. return true;
  2574. }
  2575. return false;
  2576. }
  2577. /* Indicate that hard register number FROM was eliminated and replaced with
  2578. an offset from hard register number TO. The status of hard registers live
  2579. at the start of a basic block is updated by replacing a use of FROM with
  2580. a use of TO. */
  2581. void
  2582. mark_elimination (int from, int to)
  2583. {
  2584. basic_block bb;
  2585. bitmap r;
  2586. FOR_EACH_BB_FN (bb, cfun)
  2587. {
  2588. r = DF_LR_IN (bb);
  2589. if (bitmap_bit_p (r, from))
  2590. {
  2591. bitmap_clear_bit (r, from);
  2592. bitmap_set_bit (r, to);
  2593. }
  2594. if (! df_live)
  2595. continue;
  2596. r = DF_LIVE_IN (bb);
  2597. if (bitmap_bit_p (r, from))
  2598. {
  2599. bitmap_clear_bit (r, from);
  2600. bitmap_set_bit (r, to);
  2601. }
  2602. }
  2603. }
  2604. /* The length of the following array. */
  2605. int ira_reg_equiv_len;
  2606. /* Info about equiv. info for each register. */
  2607. struct ira_reg_equiv_s *ira_reg_equiv;
  2608. /* Expand ira_reg_equiv if necessary. */
  2609. void
  2610. ira_expand_reg_equiv (void)
  2611. {
  2612. int old = ira_reg_equiv_len;
  2613. if (ira_reg_equiv_len > max_reg_num ())
  2614. return;
  2615. ira_reg_equiv_len = max_reg_num () * 3 / 2 + 1;
  2616. ira_reg_equiv
  2617. = (struct ira_reg_equiv_s *) xrealloc (ira_reg_equiv,
  2618. ira_reg_equiv_len
  2619. * sizeof (struct ira_reg_equiv_s));
  2620. gcc_assert (old < ira_reg_equiv_len);
  2621. memset (ira_reg_equiv + old, 0,
  2622. sizeof (struct ira_reg_equiv_s) * (ira_reg_equiv_len - old));
  2623. }
  2624. static void
  2625. init_reg_equiv (void)
  2626. {
  2627. ira_reg_equiv_len = 0;
  2628. ira_reg_equiv = NULL;
  2629. ira_expand_reg_equiv ();
  2630. }
  2631. static void
  2632. finish_reg_equiv (void)
  2633. {
  2634. free (ira_reg_equiv);
  2635. }
  2636. struct equivalence
  2637. {
  2638. /* Set when a REG_EQUIV note is found or created. Use to
  2639. keep track of what memory accesses might be created later,
  2640. e.g. by reload. */
  2641. rtx replacement;
  2642. rtx *src_p;
  2643. /* The list of each instruction which initializes this register.
  2644. NULL indicates we know nothing about this register's equivalence
  2645. properties.
  2646. An INSN_LIST with a NULL insn indicates this pseudo is already
  2647. known to not have a valid equivalence. */
  2648. rtx_insn_list *init_insns;
  2649. /* Loop depth is used to recognize equivalences which appear
  2650. to be present within the same loop (or in an inner loop). */
  2651. short loop_depth;
  2652. /* Nonzero if this had a preexisting REG_EQUIV note. */
  2653. unsigned char is_arg_equivalence : 1;
  2654. /* Set when an attempt should be made to replace a register
  2655. with the associated src_p entry. */
  2656. unsigned char replace : 1;
  2657. /* Set if this register has no known equivalence. */
  2658. unsigned char no_equiv : 1;
  2659. };
  2660. /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
  2661. structure for that register. */
  2662. static struct equivalence *reg_equiv;
  2663. /* Used for communication between the following two functions: contains
  2664. a MEM that we wish to ensure remains unchanged. */
  2665. static rtx equiv_mem;
  2666. /* Set nonzero if EQUIV_MEM is modified. */
  2667. static int equiv_mem_modified;
  2668. /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
  2669. Called via note_stores. */
  2670. static void
  2671. validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED,
  2672. void *data ATTRIBUTE_UNUSED)
  2673. {
  2674. if ((REG_P (dest)
  2675. && reg_overlap_mentioned_p (dest, equiv_mem))
  2676. || (MEM_P (dest)
  2677. && anti_dependence (equiv_mem, dest)))
  2678. equiv_mem_modified = 1;
  2679. }
  2680. /* Verify that no store between START and the death of REG invalidates
  2681. MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
  2682. by storing into an overlapping memory location, or with a non-const
  2683. CALL_INSN.
  2684. Return 1 if MEMREF remains valid. */
  2685. static int
  2686. validate_equiv_mem (rtx_insn *start, rtx reg, rtx memref)
  2687. {
  2688. rtx_insn *insn;
  2689. rtx note;
  2690. equiv_mem = memref;
  2691. equiv_mem_modified = 0;
  2692. /* If the memory reference has side effects or is volatile, it isn't a
  2693. valid equivalence. */
  2694. if (side_effects_p (memref))
  2695. return 0;
  2696. for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
  2697. {
  2698. if (! INSN_P (insn))
  2699. continue;
  2700. if (find_reg_note (insn, REG_DEAD, reg))
  2701. return 1;
  2702. /* This used to ignore readonly memory and const/pure calls. The problem
  2703. is the equivalent form may reference a pseudo which gets assigned a
  2704. call clobbered hard reg. When we later replace REG with its
  2705. equivalent form, the value in the call-clobbered reg has been
  2706. changed and all hell breaks loose. */
  2707. if (CALL_P (insn))
  2708. return 0;
  2709. note_stores (PATTERN (insn), validate_equiv_mem_from_store, NULL);
  2710. /* If a register mentioned in MEMREF is modified via an
  2711. auto-increment, we lose the equivalence. Do the same if one
  2712. dies; although we could extend the life, it doesn't seem worth
  2713. the trouble. */
  2714. for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
  2715. if ((REG_NOTE_KIND (note) == REG_INC
  2716. || REG_NOTE_KIND (note) == REG_DEAD)
  2717. && REG_P (XEXP (note, 0))
  2718. && reg_overlap_mentioned_p (XEXP (note, 0), memref))
  2719. return 0;
  2720. }
  2721. return 0;
  2722. }
  2723. /* Returns zero if X is known to be invariant. */
  2724. static int
  2725. equiv_init_varies_p (rtx x)
  2726. {
  2727. RTX_CODE code = GET_CODE (x);
  2728. int i;
  2729. const char *fmt;
  2730. switch (code)
  2731. {
  2732. case MEM:
  2733. return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
  2734. case CONST:
  2735. CASE_CONST_ANY:
  2736. case SYMBOL_REF:
  2737. case LABEL_REF:
  2738. return 0;
  2739. case REG:
  2740. return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
  2741. case ASM_OPERANDS:
  2742. if (MEM_VOLATILE_P (x))
  2743. return 1;
  2744. /* Fall through. */
  2745. default:
  2746. break;
  2747. }
  2748. fmt = GET_RTX_FORMAT (code);
  2749. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2750. if (fmt[i] == 'e')
  2751. {
  2752. if (equiv_init_varies_p (XEXP (x, i)))
  2753. return 1;
  2754. }
  2755. else if (fmt[i] == 'E')
  2756. {
  2757. int j;
  2758. for (j = 0; j < XVECLEN (x, i); j++)
  2759. if (equiv_init_varies_p (XVECEXP (x, i, j)))
  2760. return 1;
  2761. }
  2762. return 0;
  2763. }
  2764. /* Returns nonzero if X (used to initialize register REGNO) is movable.
  2765. X is only movable if the registers it uses have equivalent initializations
  2766. which appear to be within the same loop (or in an inner loop) and movable
  2767. or if they are not candidates for local_alloc and don't vary. */
  2768. static int
  2769. equiv_init_movable_p (rtx x, int regno)
  2770. {
  2771. int i, j;
  2772. const char *fmt;
  2773. enum rtx_code code = GET_CODE (x);
  2774. switch (code)
  2775. {
  2776. case SET:
  2777. return equiv_init_movable_p (SET_SRC (x), regno);
  2778. case CC0:
  2779. case CLOBBER:
  2780. return 0;
  2781. case PRE_INC:
  2782. case PRE_DEC:
  2783. case POST_INC:
  2784. case POST_DEC:
  2785. case PRE_MODIFY:
  2786. case POST_MODIFY:
  2787. return 0;
  2788. case REG:
  2789. return ((reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
  2790. && reg_equiv[REGNO (x)].replace)
  2791. || (REG_BASIC_BLOCK (REGNO (x)) < NUM_FIXED_BLOCKS
  2792. && ! rtx_varies_p (x, 0)));
  2793. case UNSPEC_VOLATILE:
  2794. return 0;
  2795. case ASM_OPERANDS:
  2796. if (MEM_VOLATILE_P (x))
  2797. return 0;
  2798. /* Fall through. */
  2799. default:
  2800. break;
  2801. }
  2802. fmt = GET_RTX_FORMAT (code);
  2803. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2804. switch (fmt[i])
  2805. {
  2806. case 'e':
  2807. if (! equiv_init_movable_p (XEXP (x, i), regno))
  2808. return 0;
  2809. break;
  2810. case 'E':
  2811. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2812. if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
  2813. return 0;
  2814. break;
  2815. }
  2816. return 1;
  2817. }
  2818. /* TRUE if X uses any registers for which reg_equiv[REGNO].replace is
  2819. true. */
  2820. static int
  2821. contains_replace_regs (rtx x)
  2822. {
  2823. int i, j;
  2824. const char *fmt;
  2825. enum rtx_code code = GET_CODE (x);
  2826. switch (code)
  2827. {
  2828. case CONST:
  2829. case LABEL_REF:
  2830. case SYMBOL_REF:
  2831. CASE_CONST_ANY:
  2832. case PC:
  2833. case CC0:
  2834. case HIGH:
  2835. return 0;
  2836. case REG:
  2837. return reg_equiv[REGNO (x)].replace;
  2838. default:
  2839. break;
  2840. }
  2841. fmt = GET_RTX_FORMAT (code);
  2842. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2843. switch (fmt[i])
  2844. {
  2845. case 'e':
  2846. if (contains_replace_regs (XEXP (x, i)))
  2847. return 1;
  2848. break;
  2849. case 'E':
  2850. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2851. if (contains_replace_regs (XVECEXP (x, i, j)))
  2852. return 1;
  2853. break;
  2854. }
  2855. return 0;
  2856. }
  2857. /* TRUE if X references a memory location that would be affected by a store
  2858. to MEMREF. */
  2859. static int
  2860. memref_referenced_p (rtx memref, rtx x)
  2861. {
  2862. int i, j;
  2863. const char *fmt;
  2864. enum rtx_code code = GET_CODE (x);
  2865. switch (code)
  2866. {
  2867. case CONST:
  2868. case LABEL_REF:
  2869. case SYMBOL_REF:
  2870. CASE_CONST_ANY:
  2871. case PC:
  2872. case CC0:
  2873. case HIGH:
  2874. case LO_SUM:
  2875. return 0;
  2876. case REG:
  2877. return (reg_equiv[REGNO (x)].replacement
  2878. && memref_referenced_p (memref,
  2879. reg_equiv[REGNO (x)].replacement));
  2880. case MEM:
  2881. if (true_dependence (memref, VOIDmode, x))
  2882. return 1;
  2883. break;
  2884. case SET:
  2885. /* If we are setting a MEM, it doesn't count (its address does), but any
  2886. other SET_DEST that has a MEM in it is referencing the MEM. */
  2887. if (MEM_P (SET_DEST (x)))
  2888. {
  2889. if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
  2890. return 1;
  2891. }
  2892. else if (memref_referenced_p (memref, SET_DEST (x)))
  2893. return 1;
  2894. return memref_referenced_p (memref, SET_SRC (x));
  2895. default:
  2896. break;
  2897. }
  2898. fmt = GET_RTX_FORMAT (code);
  2899. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2900. switch (fmt[i])
  2901. {
  2902. case 'e':
  2903. if (memref_referenced_p (memref, XEXP (x, i)))
  2904. return 1;
  2905. break;
  2906. case 'E':
  2907. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2908. if (memref_referenced_p (memref, XVECEXP (x, i, j)))
  2909. return 1;
  2910. break;
  2911. }
  2912. return 0;
  2913. }
  2914. /* TRUE if some insn in the range (START, END] references a memory location
  2915. that would be affected by a store to MEMREF. */
  2916. static int
  2917. memref_used_between_p (rtx memref, rtx_insn *start, rtx_insn *end)
  2918. {
  2919. rtx_insn *insn;
  2920. for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
  2921. insn = NEXT_INSN (insn))
  2922. {
  2923. if (!NONDEBUG_INSN_P (insn))
  2924. continue;
  2925. if (memref_referenced_p (memref, PATTERN (insn)))
  2926. return 1;
  2927. /* Nonconst functions may access memory. */
  2928. if (CALL_P (insn) && (! RTL_CONST_CALL_P (insn)))
  2929. return 1;
  2930. }
  2931. return 0;
  2932. }
  2933. /* Mark REG as having no known equivalence.
  2934. Some instructions might have been processed before and furnished
  2935. with REG_EQUIV notes for this register; these notes will have to be
  2936. removed.
  2937. STORE is the piece of RTL that does the non-constant / conflicting
  2938. assignment - a SET, CLOBBER or REG_INC note. It is currently not used,
  2939. but needs to be there because this function is called from note_stores. */
  2940. static void
  2941. no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED,
  2942. void *data ATTRIBUTE_UNUSED)
  2943. {
  2944. int regno;
  2945. rtx_insn_list *list;
  2946. if (!REG_P (reg))
  2947. return;
  2948. regno = REGNO (reg);
  2949. reg_equiv[regno].no_equiv = 1;
  2950. list = reg_equiv[regno].init_insns;
  2951. if (list && list->insn () == NULL)
  2952. return;
  2953. reg_equiv[regno].init_insns = gen_rtx_INSN_LIST (VOIDmode, NULL_RTX, NULL);
  2954. reg_equiv[regno].replacement = NULL_RTX;
  2955. /* This doesn't matter for equivalences made for argument registers, we
  2956. should keep their initialization insns. */
  2957. if (reg_equiv[regno].is_arg_equivalence)
  2958. return;
  2959. ira_reg_equiv[regno].defined_p = false;
  2960. ira_reg_equiv[regno].init_insns = NULL;
  2961. for (; list; list = list->next ())
  2962. {
  2963. rtx_insn *insn = list->insn ();
  2964. remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
  2965. }
  2966. }
  2967. /* Check whether the SUBREG is a paradoxical subreg and set the result
  2968. in PDX_SUBREGS. */
  2969. static void
  2970. set_paradoxical_subreg (rtx_insn *insn, bool *pdx_subregs)
  2971. {
  2972. subrtx_iterator::array_type array;
  2973. FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
  2974. {
  2975. const_rtx subreg = *iter;
  2976. if (GET_CODE (subreg) == SUBREG)
  2977. {
  2978. const_rtx reg = SUBREG_REG (subreg);
  2979. if (REG_P (reg) && paradoxical_subreg_p (subreg))
  2980. pdx_subregs[REGNO (reg)] = true;
  2981. }
  2982. }
  2983. }
  2984. /* In DEBUG_INSN location adjust REGs from CLEARED_REGS bitmap to the
  2985. equivalent replacement. */
  2986. static rtx
  2987. adjust_cleared_regs (rtx loc, const_rtx old_rtx ATTRIBUTE_UNUSED, void *data)
  2988. {
  2989. if (REG_P (loc))
  2990. {
  2991. bitmap cleared_regs = (bitmap) data;
  2992. if (bitmap_bit_p (cleared_regs, REGNO (loc)))
  2993. return simplify_replace_fn_rtx (copy_rtx (*reg_equiv[REGNO (loc)].src_p),
  2994. NULL_RTX, adjust_cleared_regs, data);
  2995. }
  2996. return NULL_RTX;
  2997. }
  2998. /* Nonzero if we recorded an equivalence for a LABEL_REF. */
  2999. static int recorded_label_ref;
  3000. /* Find registers that are equivalent to a single value throughout the
  3001. compilation (either because they can be referenced in memory or are
  3002. set once from a single constant). Lower their priority for a
  3003. register.
  3004. If such a register is only referenced once, try substituting its
  3005. value into the using insn. If it succeeds, we can eliminate the
  3006. register completely.
  3007. Initialize init_insns in ira_reg_equiv array.
  3008. Return non-zero if jump label rebuilding should be done. */
  3009. static int
  3010. update_equiv_regs (void)
  3011. {
  3012. rtx_insn *insn;
  3013. basic_block bb;
  3014. int loop_depth;
  3015. bitmap cleared_regs;
  3016. bool *pdx_subregs;
  3017. /* We need to keep track of whether or not we recorded a LABEL_REF so
  3018. that we know if the jump optimizer needs to be rerun. */
  3019. recorded_label_ref = 0;
  3020. /* Use pdx_subregs to show whether a reg is used in a paradoxical
  3021. subreg. */
  3022. pdx_subregs = XCNEWVEC (bool, max_regno);
  3023. reg_equiv = XCNEWVEC (struct equivalence, max_regno);
  3024. grow_reg_equivs ();
  3025. init_alias_analysis ();
  3026. /* Scan insns and set pdx_subregs[regno] if the reg is used in a
  3027. paradoxical subreg. Don't set such reg equivalent to a mem,
  3028. because lra will not substitute such equiv memory in order to
  3029. prevent access beyond allocated memory for paradoxical memory subreg. */
  3030. FOR_EACH_BB_FN (bb, cfun)
  3031. FOR_BB_INSNS (bb, insn)
  3032. if (NONDEBUG_INSN_P (insn))
  3033. set_paradoxical_subreg (insn, pdx_subregs);
  3034. /* Scan the insns and find which registers have equivalences. Do this
  3035. in a separate scan of the insns because (due to -fcse-follow-jumps)
  3036. a register can be set below its use. */
  3037. FOR_EACH_BB_FN (bb, cfun)
  3038. {
  3039. loop_depth = bb_loop_depth (bb);
  3040. for (insn = BB_HEAD (bb);
  3041. insn != NEXT_INSN (BB_END (bb));
  3042. insn = NEXT_INSN (insn))
  3043. {
  3044. rtx note;
  3045. rtx set;
  3046. rtx dest, src;
  3047. int regno;
  3048. if (! INSN_P (insn))
  3049. continue;
  3050. for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
  3051. if (REG_NOTE_KIND (note) == REG_INC)
  3052. no_equiv (XEXP (note, 0), note, NULL);
  3053. set = single_set (insn);
  3054. /* If this insn contains more (or less) than a single SET,
  3055. only mark all destinations as having no known equivalence. */
  3056. if (set == NULL_RTX)
  3057. {
  3058. note_stores (PATTERN (insn), no_equiv, NULL);
  3059. continue;
  3060. }
  3061. else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  3062. {
  3063. int i;
  3064. for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  3065. {
  3066. rtx part = XVECEXP (PATTERN (insn), 0, i);
  3067. if (part != set)
  3068. note_stores (part, no_equiv, NULL);
  3069. }
  3070. }
  3071. dest = SET_DEST (set);
  3072. src = SET_SRC (set);
  3073. /* See if this is setting up the equivalence between an argument
  3074. register and its stack slot. */
  3075. note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
  3076. if (note)
  3077. {
  3078. gcc_assert (REG_P (dest));
  3079. regno = REGNO (dest);
  3080. /* Note that we don't want to clear init_insns in
  3081. ira_reg_equiv even if there are multiple sets of this
  3082. register. */
  3083. reg_equiv[regno].is_arg_equivalence = 1;
  3084. /* The insn result can have equivalence memory although
  3085. the equivalence is not set up by the insn. We add
  3086. this insn to init insns as it is a flag for now that
  3087. regno has an equivalence. We will remove the insn
  3088. from init insn list later. */
  3089. if (rtx_equal_p (src, XEXP (note, 0)) || MEM_P (XEXP (note, 0)))
  3090. ira_reg_equiv[regno].init_insns
  3091. = gen_rtx_INSN_LIST (VOIDmode, insn,
  3092. ira_reg_equiv[regno].init_insns);
  3093. /* Continue normally in case this is a candidate for
  3094. replacements. */
  3095. }
  3096. if (!optimize)
  3097. continue;
  3098. /* We only handle the case of a pseudo register being set
  3099. once, or always to the same value. */
  3100. /* ??? The mn10200 port breaks if we add equivalences for
  3101. values that need an ADDRESS_REGS register and set them equivalent
  3102. to a MEM of a pseudo. The actual problem is in the over-conservative
  3103. handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
  3104. calculate_needs, but we traditionally work around this problem
  3105. here by rejecting equivalences when the destination is in a register
  3106. that's likely spilled. This is fragile, of course, since the
  3107. preferred class of a pseudo depends on all instructions that set
  3108. or use it. */
  3109. if (!REG_P (dest)
  3110. || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
  3111. || (reg_equiv[regno].init_insns
  3112. && reg_equiv[regno].init_insns->insn () == NULL)
  3113. || (targetm.class_likely_spilled_p (reg_preferred_class (regno))
  3114. && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
  3115. {
  3116. /* This might be setting a SUBREG of a pseudo, a pseudo that is
  3117. also set somewhere else to a constant. */
  3118. note_stores (set, no_equiv, NULL);
  3119. continue;
  3120. }
  3121. /* Don't set reg (if pdx_subregs[regno] == true) equivalent to a mem. */
  3122. if (MEM_P (src) && pdx_subregs[regno])
  3123. {
  3124. note_stores (set, no_equiv, NULL);
  3125. continue;
  3126. }
  3127. note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
  3128. /* cse sometimes generates function invariants, but doesn't put a
  3129. REG_EQUAL note on the insn. Since this note would be redundant,
  3130. there's no point creating it earlier than here. */
  3131. if (! note && ! rtx_varies_p (src, 0))
  3132. note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
  3133. /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
  3134. since it represents a function call. */
  3135. if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
  3136. note = NULL_RTX;
  3137. if (DF_REG_DEF_COUNT (regno) != 1)
  3138. {
  3139. bool equal_p = true;
  3140. rtx_insn_list *list;
  3141. /* If we have already processed this pseudo and determined it
  3142. can not have an equivalence, then honor that decision. */
  3143. if (reg_equiv[regno].no_equiv)
  3144. continue;
  3145. if (! note
  3146. || rtx_varies_p (XEXP (note, 0), 0)
  3147. || (reg_equiv[regno].replacement
  3148. && ! rtx_equal_p (XEXP (note, 0),
  3149. reg_equiv[regno].replacement)))
  3150. {
  3151. no_equiv (dest, set, NULL);
  3152. continue;
  3153. }
  3154. list = reg_equiv[regno].init_insns;
  3155. for (; list; list = list->next ())
  3156. {
  3157. rtx note_tmp;
  3158. rtx_insn *insn_tmp;
  3159. insn_tmp = list->insn ();
  3160. note_tmp = find_reg_note (insn_tmp, REG_EQUAL, NULL_RTX);
  3161. gcc_assert (note_tmp);
  3162. if (! rtx_equal_p (XEXP (note, 0), XEXP (note_tmp, 0)))
  3163. {
  3164. equal_p = false;
  3165. break;
  3166. }
  3167. }
  3168. if (! equal_p)
  3169. {
  3170. no_equiv (dest, set, NULL);
  3171. continue;
  3172. }
  3173. }
  3174. /* Record this insn as initializing this register. */
  3175. reg_equiv[regno].init_insns
  3176. = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
  3177. /* If this register is known to be equal to a constant, record that
  3178. it is always equivalent to the constant. */
  3179. if (DF_REG_DEF_COUNT (regno) == 1
  3180. && note && ! rtx_varies_p (XEXP (note, 0), 0))
  3181. {
  3182. rtx note_value = XEXP (note, 0);
  3183. remove_note (insn, note);
  3184. set_unique_reg_note (insn, REG_EQUIV, note_value);
  3185. }
  3186. /* If this insn introduces a "constant" register, decrease the priority
  3187. of that register. Record this insn if the register is only used once
  3188. more and the equivalence value is the same as our source.
  3189. The latter condition is checked for two reasons: First, it is an
  3190. indication that it may be more efficient to actually emit the insn
  3191. as written (if no registers are available, reload will substitute
  3192. the equivalence). Secondly, it avoids problems with any registers
  3193. dying in this insn whose death notes would be missed.
  3194. If we don't have a REG_EQUIV note, see if this insn is loading
  3195. a register used only in one basic block from a MEM. If so, and the
  3196. MEM remains unchanged for the life of the register, add a REG_EQUIV
  3197. note. */
  3198. note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
  3199. if (note == NULL_RTX && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
  3200. && MEM_P (SET_SRC (set))
  3201. && validate_equiv_mem (insn, dest, SET_SRC (set)))
  3202. note = set_unique_reg_note (insn, REG_EQUIV, copy_rtx (SET_SRC (set)));
  3203. if (note)
  3204. {
  3205. int regno = REGNO (dest);
  3206. rtx x = XEXP (note, 0);
  3207. /* If we haven't done so, record for reload that this is an
  3208. equivalencing insn. */
  3209. if (!reg_equiv[regno].is_arg_equivalence)
  3210. ira_reg_equiv[regno].init_insns
  3211. = gen_rtx_INSN_LIST (VOIDmode, insn,
  3212. ira_reg_equiv[regno].init_insns);
  3213. /* Record whether or not we created a REG_EQUIV note for a LABEL_REF.
  3214. We might end up substituting the LABEL_REF for uses of the
  3215. pseudo here or later. That kind of transformation may turn an
  3216. indirect jump into a direct jump, in which case we must rerun the
  3217. jump optimizer to ensure that the JUMP_LABEL fields are valid. */
  3218. if (GET_CODE (x) == LABEL_REF
  3219. || (GET_CODE (x) == CONST
  3220. && GET_CODE (XEXP (x, 0)) == PLUS
  3221. && (GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)))
  3222. recorded_label_ref = 1;
  3223. reg_equiv[regno].replacement = x;
  3224. reg_equiv[regno].src_p = &SET_SRC (set);
  3225. reg_equiv[regno].loop_depth = (short) loop_depth;
  3226. /* Don't mess with things live during setjmp. */
  3227. if (REG_LIVE_LENGTH (regno) >= 0 && optimize)
  3228. {
  3229. /* Note that the statement below does not affect the priority
  3230. in local-alloc! */
  3231. REG_LIVE_LENGTH (regno) *= 2;
  3232. /* If the register is referenced exactly twice, meaning it is
  3233. set once and used once, indicate that the reference may be
  3234. replaced by the equivalence we computed above. Do this
  3235. even if the register is only used in one block so that
  3236. dependencies can be handled where the last register is
  3237. used in a different block (i.e. HIGH / LO_SUM sequences)
  3238. and to reduce the number of registers alive across
  3239. calls. */
  3240. if (REG_N_REFS (regno) == 2
  3241. && (rtx_equal_p (x, src)
  3242. || ! equiv_init_varies_p (src))
  3243. && NONJUMP_INSN_P (insn)
  3244. && equiv_init_movable_p (PATTERN (insn), regno))
  3245. reg_equiv[regno].replace = 1;
  3246. }
  3247. }
  3248. }
  3249. }
  3250. if (!optimize)
  3251. goto out;
  3252. /* A second pass, to gather additional equivalences with memory. This needs
  3253. to be done after we know which registers we are going to replace. */
  3254. for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
  3255. {
  3256. rtx set, src, dest;
  3257. unsigned regno;
  3258. if (! INSN_P (insn))
  3259. continue;
  3260. set = single_set (insn);
  3261. if (! set)
  3262. continue;
  3263. dest = SET_DEST (set);
  3264. src = SET_SRC (set);
  3265. /* If this sets a MEM to the contents of a REG that is only used
  3266. in a single basic block, see if the register is always equivalent
  3267. to that memory location and if moving the store from INSN to the
  3268. insn that set REG is safe. If so, put a REG_EQUIV note on the
  3269. initializing insn.
  3270. Don't add a REG_EQUIV note if the insn already has one. The existing
  3271. REG_EQUIV is likely more useful than the one we are adding.
  3272. If one of the regs in the address has reg_equiv[REGNO].replace set,
  3273. then we can't add this REG_EQUIV note. The reg_equiv[REGNO].replace
  3274. optimization may move the set of this register immediately before
  3275. insn, which puts it after reg_equiv[REGNO].init_insns, and hence
  3276. the mention in the REG_EQUIV note would be to an uninitialized
  3277. pseudo. */
  3278. if (MEM_P (dest) && REG_P (src)
  3279. && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
  3280. && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
  3281. && DF_REG_DEF_COUNT (regno) == 1
  3282. && reg_equiv[regno].init_insns != NULL
  3283. && reg_equiv[regno].init_insns->insn () != NULL
  3284. && ! find_reg_note (XEXP (reg_equiv[regno].init_insns, 0),
  3285. REG_EQUIV, NULL_RTX)
  3286. && ! contains_replace_regs (XEXP (dest, 0))
  3287. && ! pdx_subregs[regno])
  3288. {
  3289. rtx_insn *init_insn =
  3290. as_a <rtx_insn *> (XEXP (reg_equiv[regno].init_insns, 0));
  3291. if (validate_equiv_mem (init_insn, src, dest)
  3292. && ! memref_used_between_p (dest, init_insn, insn)
  3293. /* Attaching a REG_EQUIV note will fail if INIT_INSN has
  3294. multiple sets. */
  3295. && set_unique_reg_note (init_insn, REG_EQUIV, copy_rtx (dest)))
  3296. {
  3297. /* This insn makes the equivalence, not the one initializing
  3298. the register. */
  3299. ira_reg_equiv[regno].init_insns
  3300. = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
  3301. df_notes_rescan (init_insn);
  3302. }
  3303. }
  3304. }
  3305. cleared_regs = BITMAP_ALLOC (NULL);
  3306. /* Now scan all regs killed in an insn to see if any of them are
  3307. registers only used that once. If so, see if we can replace the
  3308. reference with the equivalent form. If we can, delete the
  3309. initializing reference and this register will go away. If we
  3310. can't replace the reference, and the initializing reference is
  3311. within the same loop (or in an inner loop), then move the register
  3312. initialization just before the use, so that they are in the same
  3313. basic block. */
  3314. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  3315. {
  3316. loop_depth = bb_loop_depth (bb);
  3317. for (insn = BB_END (bb);
  3318. insn != PREV_INSN (BB_HEAD (bb));
  3319. insn = PREV_INSN (insn))
  3320. {
  3321. rtx link;
  3322. if (! INSN_P (insn))
  3323. continue;
  3324. /* Don't substitute into a non-local goto, this confuses CFG. */
  3325. if (JUMP_P (insn)
  3326. && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
  3327. continue;
  3328. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  3329. {
  3330. if (REG_NOTE_KIND (link) == REG_DEAD
  3331. /* Make sure this insn still refers to the register. */
  3332. && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
  3333. {
  3334. int regno = REGNO (XEXP (link, 0));
  3335. rtx equiv_insn;
  3336. if (! reg_equiv[regno].replace
  3337. || reg_equiv[regno].loop_depth < (short) loop_depth
  3338. /* There is no sense to move insns if live range
  3339. shrinkage or register pressure-sensitive
  3340. scheduling were done because it will not
  3341. improve allocation but worsen insn schedule
  3342. with a big probability. */
  3343. || flag_live_range_shrinkage
  3344. || (flag_sched_pressure && flag_schedule_insns))
  3345. continue;
  3346. /* reg_equiv[REGNO].replace gets set only when
  3347. REG_N_REFS[REGNO] is 2, i.e. the register is set
  3348. once and used once. (If it were only set, but
  3349. not used, flow would have deleted the setting
  3350. insns.) Hence there can only be one insn in
  3351. reg_equiv[REGNO].init_insns. */
  3352. gcc_assert (reg_equiv[regno].init_insns
  3353. && !XEXP (reg_equiv[regno].init_insns, 1));
  3354. equiv_insn = XEXP (reg_equiv[regno].init_insns, 0);
  3355. /* We may not move instructions that can throw, since
  3356. that changes basic block boundaries and we are not
  3357. prepared to adjust the CFG to match. */
  3358. if (can_throw_internal (equiv_insn))
  3359. continue;
  3360. if (asm_noperands (PATTERN (equiv_insn)) < 0
  3361. && validate_replace_rtx (regno_reg_rtx[regno],
  3362. *(reg_equiv[regno].src_p), insn))
  3363. {
  3364. rtx equiv_link;
  3365. rtx last_link;
  3366. rtx note;
  3367. /* Find the last note. */
  3368. for (last_link = link; XEXP (last_link, 1);
  3369. last_link = XEXP (last_link, 1))
  3370. ;
  3371. /* Append the REG_DEAD notes from equiv_insn. */
  3372. equiv_link = REG_NOTES (equiv_insn);
  3373. while (equiv_link)
  3374. {
  3375. note = equiv_link;
  3376. equiv_link = XEXP (equiv_link, 1);
  3377. if (REG_NOTE_KIND (note) == REG_DEAD)
  3378. {
  3379. remove_note (equiv_insn, note);
  3380. XEXP (last_link, 1) = note;
  3381. XEXP (note, 1) = NULL_RTX;
  3382. last_link = note;
  3383. }
  3384. }
  3385. remove_death (regno, insn);
  3386. SET_REG_N_REFS (regno, 0);
  3387. REG_FREQ (regno) = 0;
  3388. delete_insn (equiv_insn);
  3389. reg_equiv[regno].init_insns
  3390. = reg_equiv[regno].init_insns->next ();
  3391. ira_reg_equiv[regno].init_insns = NULL;
  3392. bitmap_set_bit (cleared_regs, regno);
  3393. }
  3394. /* Move the initialization of the register to just before
  3395. INSN. Update the flow information. */
  3396. else if (prev_nondebug_insn (insn) != equiv_insn)
  3397. {
  3398. rtx_insn *new_insn;
  3399. new_insn = emit_insn_before (PATTERN (equiv_insn), insn);
  3400. REG_NOTES (new_insn) = REG_NOTES (equiv_insn);
  3401. REG_NOTES (equiv_insn) = 0;
  3402. /* Rescan it to process the notes. */
  3403. df_insn_rescan (new_insn);
  3404. /* Make sure this insn is recognized before
  3405. reload begins, otherwise
  3406. eliminate_regs_in_insn will die. */
  3407. INSN_CODE (new_insn) = INSN_CODE (equiv_insn);
  3408. delete_insn (equiv_insn);
  3409. XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
  3410. REG_BASIC_BLOCK (regno) = bb->index;
  3411. REG_N_CALLS_CROSSED (regno) = 0;
  3412. REG_FREQ_CALLS_CROSSED (regno) = 0;
  3413. REG_N_THROWING_CALLS_CROSSED (regno) = 0;
  3414. REG_LIVE_LENGTH (regno) = 2;
  3415. if (insn == BB_HEAD (bb))
  3416. BB_HEAD (bb) = PREV_INSN (insn);
  3417. ira_reg_equiv[regno].init_insns
  3418. = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
  3419. bitmap_set_bit (cleared_regs, regno);
  3420. }
  3421. }
  3422. }
  3423. }
  3424. }
  3425. if (!bitmap_empty_p (cleared_regs))
  3426. {
  3427. FOR_EACH_BB_FN (bb, cfun)
  3428. {
  3429. bitmap_and_compl_into (DF_LR_IN (bb), cleared_regs);
  3430. bitmap_and_compl_into (DF_LR_OUT (bb), cleared_regs);
  3431. if (! df_live)
  3432. continue;
  3433. bitmap_and_compl_into (DF_LIVE_IN (bb), cleared_regs);
  3434. bitmap_and_compl_into (DF_LIVE_OUT (bb), cleared_regs);
  3435. }
  3436. /* Last pass - adjust debug insns referencing cleared regs. */
  3437. if (MAY_HAVE_DEBUG_INSNS)
  3438. for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
  3439. if (DEBUG_INSN_P (insn))
  3440. {
  3441. rtx old_loc = INSN_VAR_LOCATION_LOC (insn);
  3442. INSN_VAR_LOCATION_LOC (insn)
  3443. = simplify_replace_fn_rtx (old_loc, NULL_RTX,
  3444. adjust_cleared_regs,
  3445. (void *) cleared_regs);
  3446. if (old_loc != INSN_VAR_LOCATION_LOC (insn))
  3447. df_insn_rescan (insn);
  3448. }
  3449. }
  3450. BITMAP_FREE (cleared_regs);
  3451. out:
  3452. /* Clean up. */
  3453. end_alias_analysis ();
  3454. free (reg_equiv);
  3455. free (pdx_subregs);
  3456. return recorded_label_ref;
  3457. }
  3458. /* Set up fields memory, constant, and invariant from init_insns in
  3459. the structures of array ira_reg_equiv. */
  3460. static void
  3461. setup_reg_equiv (void)
  3462. {
  3463. int i;
  3464. rtx_insn_list *elem, *prev_elem, *next_elem;
  3465. rtx_insn *insn;
  3466. rtx set, x;
  3467. for (i = FIRST_PSEUDO_REGISTER; i < ira_reg_equiv_len; i++)
  3468. for (prev_elem = NULL, elem = ira_reg_equiv[i].init_insns;
  3469. elem;
  3470. prev_elem = elem, elem = next_elem)
  3471. {
  3472. next_elem = elem->next ();
  3473. insn = elem->insn ();
  3474. set = single_set (insn);
  3475. /* Init insns can set up equivalence when the reg is a destination or
  3476. a source (in this case the destination is memory). */
  3477. if (set != 0 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))))
  3478. {
  3479. if ((x = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL)
  3480. {
  3481. x = XEXP (x, 0);
  3482. if (REG_P (SET_DEST (set))
  3483. && REGNO (SET_DEST (set)) == (unsigned int) i
  3484. && ! rtx_equal_p (SET_SRC (set), x) && MEM_P (x))
  3485. {
  3486. /* This insn reporting the equivalence but
  3487. actually not setting it. Remove it from the
  3488. list. */
  3489. if (prev_elem == NULL)
  3490. ira_reg_equiv[i].init_insns = next_elem;
  3491. else
  3492. XEXP (prev_elem, 1) = next_elem;
  3493. elem = prev_elem;
  3494. }
  3495. }
  3496. else if (REG_P (SET_DEST (set))
  3497. && REGNO (SET_DEST (set)) == (unsigned int) i)
  3498. x = SET_SRC (set);
  3499. else
  3500. {
  3501. gcc_assert (REG_P (SET_SRC (set))
  3502. && REGNO (SET_SRC (set)) == (unsigned int) i);
  3503. x = SET_DEST (set);
  3504. }
  3505. if (! function_invariant_p (x)
  3506. || ! flag_pic
  3507. /* A function invariant is often CONSTANT_P but may
  3508. include a register. We promise to only pass
  3509. CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
  3510. || (CONSTANT_P (x) && LEGITIMATE_PIC_OPERAND_P (x)))
  3511. {
  3512. /* It can happen that a REG_EQUIV note contains a MEM
  3513. that is not a legitimate memory operand. As later
  3514. stages of reload assume that all addresses found in
  3515. the lra_regno_equiv_* arrays were originally
  3516. legitimate, we ignore such REG_EQUIV notes. */
  3517. if (memory_operand (x, VOIDmode))
  3518. {
  3519. ira_reg_equiv[i].defined_p = true;
  3520. ira_reg_equiv[i].memory = x;
  3521. continue;
  3522. }
  3523. else if (function_invariant_p (x))
  3524. {
  3525. machine_mode mode;
  3526. mode = GET_MODE (SET_DEST (set));
  3527. if (GET_CODE (x) == PLUS
  3528. || x == frame_pointer_rtx || x == arg_pointer_rtx)
  3529. /* This is PLUS of frame pointer and a constant,
  3530. or fp, or argp. */
  3531. ira_reg_equiv[i].invariant = x;
  3532. else if (targetm.legitimate_constant_p (mode, x))
  3533. ira_reg_equiv[i].constant = x;
  3534. else
  3535. {
  3536. ira_reg_equiv[i].memory = force_const_mem (mode, x);
  3537. if (ira_reg_equiv[i].memory == NULL_RTX)
  3538. {
  3539. ira_reg_equiv[i].defined_p = false;
  3540. ira_reg_equiv[i].init_insns = NULL;
  3541. break;
  3542. }
  3543. }
  3544. ira_reg_equiv[i].defined_p = true;
  3545. continue;
  3546. }
  3547. }
  3548. }
  3549. ira_reg_equiv[i].defined_p = false;
  3550. ira_reg_equiv[i].init_insns = NULL;
  3551. break;
  3552. }
  3553. }
  3554. /* Print chain C to FILE. */
  3555. static void
  3556. print_insn_chain (FILE *file, struct insn_chain *c)
  3557. {
  3558. fprintf (file, "insn=%d, ", INSN_UID (c->insn));
  3559. bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
  3560. bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
  3561. }
  3562. /* Print all reload_insn_chains to FILE. */
  3563. static void
  3564. print_insn_chains (FILE *file)
  3565. {
  3566. struct insn_chain *c;
  3567. for (c = reload_insn_chain; c ; c = c->next)
  3568. print_insn_chain (file, c);
  3569. }
  3570. /* Return true if pseudo REGNO should be added to set live_throughout
  3571. or dead_or_set of the insn chains for reload consideration. */
  3572. static bool
  3573. pseudo_for_reload_consideration_p (int regno)
  3574. {
  3575. /* Consider spilled pseudos too for IRA because they still have a
  3576. chance to get hard-registers in the reload when IRA is used. */
  3577. return (reg_renumber[regno] >= 0 || ira_conflicts_p);
  3578. }
  3579. /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] using
  3580. REG to the number of nregs, and INIT_VALUE to get the
  3581. initialization. ALLOCNUM need not be the regno of REG. */
  3582. static void
  3583. init_live_subregs (bool init_value, sbitmap *live_subregs,
  3584. bitmap live_subregs_used, int allocnum, rtx reg)
  3585. {
  3586. unsigned int regno = REGNO (SUBREG_REG (reg));
  3587. int size = GET_MODE_SIZE (GET_MODE (regno_reg_rtx[regno]));
  3588. gcc_assert (size > 0);
  3589. /* Been there, done that. */
  3590. if (bitmap_bit_p (live_subregs_used, allocnum))
  3591. return;
  3592. /* Create a new one. */
  3593. if (live_subregs[allocnum] == NULL)
  3594. live_subregs[allocnum] = sbitmap_alloc (size);
  3595. /* If the entire reg was live before blasting into subregs, we need
  3596. to init all of the subregs to ones else init to 0. */
  3597. if (init_value)
  3598. bitmap_ones (live_subregs[allocnum]);
  3599. else
  3600. bitmap_clear (live_subregs[allocnum]);
  3601. bitmap_set_bit (live_subregs_used, allocnum);
  3602. }
  3603. /* Walk the insns of the current function and build reload_insn_chain,
  3604. and record register life information. */
  3605. static void
  3606. build_insn_chain (void)
  3607. {
  3608. unsigned int i;
  3609. struct insn_chain **p = &reload_insn_chain;
  3610. basic_block bb;
  3611. struct insn_chain *c = NULL;
  3612. struct insn_chain *next = NULL;
  3613. bitmap live_relevant_regs = BITMAP_ALLOC (NULL);
  3614. bitmap elim_regset = BITMAP_ALLOC (NULL);
  3615. /* live_subregs is a vector used to keep accurate information about
  3616. which hardregs are live in multiword pseudos. live_subregs and
  3617. live_subregs_used are indexed by pseudo number. The live_subreg
  3618. entry for a particular pseudo is only used if the corresponding
  3619. element is non zero in live_subregs_used. The sbitmap size of
  3620. live_subreg[allocno] is number of bytes that the pseudo can
  3621. occupy. */
  3622. sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
  3623. bitmap live_subregs_used = BITMAP_ALLOC (NULL);
  3624. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  3625. if (TEST_HARD_REG_BIT (eliminable_regset, i))
  3626. bitmap_set_bit (elim_regset, i);
  3627. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  3628. {
  3629. bitmap_iterator bi;
  3630. rtx_insn *insn;
  3631. CLEAR_REG_SET (live_relevant_regs);
  3632. bitmap_clear (live_subregs_used);
  3633. EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), 0, i, bi)
  3634. {
  3635. if (i >= FIRST_PSEUDO_REGISTER)
  3636. break;
  3637. bitmap_set_bit (live_relevant_regs, i);
  3638. }
  3639. EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb),
  3640. FIRST_PSEUDO_REGISTER, i, bi)
  3641. {
  3642. if (pseudo_for_reload_consideration_p (i))
  3643. bitmap_set_bit (live_relevant_regs, i);
  3644. }
  3645. FOR_BB_INSNS_REVERSE (bb, insn)
  3646. {
  3647. if (!NOTE_P (insn) && !BARRIER_P (insn))
  3648. {
  3649. struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  3650. df_ref def, use;
  3651. c = new_insn_chain ();
  3652. c->next = next;
  3653. next = c;
  3654. *p = c;
  3655. p = &c->prev;
  3656. c->insn = insn;
  3657. c->block = bb->index;
  3658. if (NONDEBUG_INSN_P (insn))
  3659. FOR_EACH_INSN_INFO_DEF (def, insn_info)
  3660. {
  3661. unsigned int regno = DF_REF_REGNO (def);
  3662. /* Ignore may clobbers because these are generated
  3663. from calls. However, every other kind of def is
  3664. added to dead_or_set. */
  3665. if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
  3666. {
  3667. if (regno < FIRST_PSEUDO_REGISTER)
  3668. {
  3669. if (!fixed_regs[regno])
  3670. bitmap_set_bit (&c->dead_or_set, regno);
  3671. }
  3672. else if (pseudo_for_reload_consideration_p (regno))
  3673. bitmap_set_bit (&c->dead_or_set, regno);
  3674. }
  3675. if ((regno < FIRST_PSEUDO_REGISTER
  3676. || reg_renumber[regno] >= 0
  3677. || ira_conflicts_p)
  3678. && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
  3679. {
  3680. rtx reg = DF_REF_REG (def);
  3681. /* We can model subregs, but not if they are
  3682. wrapped in ZERO_EXTRACTS. */
  3683. if (GET_CODE (reg) == SUBREG
  3684. && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT))
  3685. {
  3686. unsigned int start = SUBREG_BYTE (reg);
  3687. unsigned int last = start
  3688. + GET_MODE_SIZE (GET_MODE (reg));
  3689. init_live_subregs
  3690. (bitmap_bit_p (live_relevant_regs, regno),
  3691. live_subregs, live_subregs_used, regno, reg);
  3692. if (!DF_REF_FLAGS_IS_SET
  3693. (def, DF_REF_STRICT_LOW_PART))
  3694. {
  3695. /* Expand the range to cover entire words.
  3696. Bytes added here are "don't care". */
  3697. start
  3698. = start / UNITS_PER_WORD * UNITS_PER_WORD;
  3699. last = ((last + UNITS_PER_WORD - 1)
  3700. / UNITS_PER_WORD * UNITS_PER_WORD);
  3701. }
  3702. /* Ignore the paradoxical bits. */
  3703. if (last > SBITMAP_SIZE (live_subregs[regno]))
  3704. last = SBITMAP_SIZE (live_subregs[regno]);
  3705. while (start < last)
  3706. {
  3707. bitmap_clear_bit (live_subregs[regno], start);
  3708. start++;
  3709. }
  3710. if (bitmap_empty_p (live_subregs[regno]))
  3711. {
  3712. bitmap_clear_bit (live_subregs_used, regno);
  3713. bitmap_clear_bit (live_relevant_regs, regno);
  3714. }
  3715. else
  3716. /* Set live_relevant_regs here because
  3717. that bit has to be true to get us to
  3718. look at the live_subregs fields. */
  3719. bitmap_set_bit (live_relevant_regs, regno);
  3720. }
  3721. else
  3722. {
  3723. /* DF_REF_PARTIAL is generated for
  3724. subregs, STRICT_LOW_PART, and
  3725. ZERO_EXTRACT. We handle the subreg
  3726. case above so here we have to keep from
  3727. modeling the def as a killing def. */
  3728. if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
  3729. {
  3730. bitmap_clear_bit (live_subregs_used, regno);
  3731. bitmap_clear_bit (live_relevant_regs, regno);
  3732. }
  3733. }
  3734. }
  3735. }
  3736. bitmap_and_compl_into (live_relevant_regs, elim_regset);
  3737. bitmap_copy (&c->live_throughout, live_relevant_regs);
  3738. if (NONDEBUG_INSN_P (insn))
  3739. FOR_EACH_INSN_INFO_USE (use, insn_info)
  3740. {
  3741. unsigned int regno = DF_REF_REGNO (use);
  3742. rtx reg = DF_REF_REG (use);
  3743. /* DF_REF_READ_WRITE on a use means that this use
  3744. is fabricated from a def that is a partial set
  3745. to a multiword reg. Here, we only model the
  3746. subreg case that is not wrapped in ZERO_EXTRACT
  3747. precisely so we do not need to look at the
  3748. fabricated use. */
  3749. if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE)
  3750. && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT)
  3751. && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
  3752. continue;
  3753. /* Add the last use of each var to dead_or_set. */
  3754. if (!bitmap_bit_p (live_relevant_regs, regno))
  3755. {
  3756. if (regno < FIRST_PSEUDO_REGISTER)
  3757. {
  3758. if (!fixed_regs[regno])
  3759. bitmap_set_bit (&c->dead_or_set, regno);
  3760. }
  3761. else if (pseudo_for_reload_consideration_p (regno))
  3762. bitmap_set_bit (&c->dead_or_set, regno);
  3763. }
  3764. if (regno < FIRST_PSEUDO_REGISTER
  3765. || pseudo_for_reload_consideration_p (regno))
  3766. {
  3767. if (GET_CODE (reg) == SUBREG
  3768. && !DF_REF_FLAGS_IS_SET (use,
  3769. DF_REF_SIGN_EXTRACT
  3770. | DF_REF_ZERO_EXTRACT))
  3771. {
  3772. unsigned int start = SUBREG_BYTE (reg);
  3773. unsigned int last = start
  3774. + GET_MODE_SIZE (GET_MODE (reg));
  3775. init_live_subregs
  3776. (bitmap_bit_p (live_relevant_regs, regno),
  3777. live_subregs, live_subregs_used, regno, reg);
  3778. /* Ignore the paradoxical bits. */
  3779. if (last > SBITMAP_SIZE (live_subregs[regno]))
  3780. last = SBITMAP_SIZE (live_subregs[regno]);
  3781. while (start < last)
  3782. {
  3783. bitmap_set_bit (live_subregs[regno], start);
  3784. start++;
  3785. }
  3786. }
  3787. else
  3788. /* Resetting the live_subregs_used is
  3789. effectively saying do not use the subregs
  3790. because we are reading the whole
  3791. pseudo. */
  3792. bitmap_clear_bit (live_subregs_used, regno);
  3793. bitmap_set_bit (live_relevant_regs, regno);
  3794. }
  3795. }
  3796. }
  3797. }
  3798. /* FIXME!! The following code is a disaster. Reload needs to see the
  3799. labels and jump tables that are just hanging out in between
  3800. the basic blocks. See pr33676. */
  3801. insn = BB_HEAD (bb);
  3802. /* Skip over the barriers and cruft. */
  3803. while (insn && (BARRIER_P (insn) || NOTE_P (insn)
  3804. || BLOCK_FOR_INSN (insn) == bb))
  3805. insn = PREV_INSN (insn);
  3806. /* While we add anything except barriers and notes, the focus is
  3807. to get the labels and jump tables into the
  3808. reload_insn_chain. */
  3809. while (insn)
  3810. {
  3811. if (!NOTE_P (insn) && !BARRIER_P (insn))
  3812. {
  3813. if (BLOCK_FOR_INSN (insn))
  3814. break;
  3815. c = new_insn_chain ();
  3816. c->next = next;
  3817. next = c;
  3818. *p = c;
  3819. p = &c->prev;
  3820. /* The block makes no sense here, but it is what the old
  3821. code did. */
  3822. c->block = bb->index;
  3823. c->insn = insn;
  3824. bitmap_copy (&c->live_throughout, live_relevant_regs);
  3825. }
  3826. insn = PREV_INSN (insn);
  3827. }
  3828. }
  3829. reload_insn_chain = c;
  3830. *p = NULL;
  3831. for (i = 0; i < (unsigned int) max_regno; i++)
  3832. if (live_subregs[i] != NULL)
  3833. sbitmap_free (live_subregs[i]);
  3834. free (live_subregs);
  3835. BITMAP_FREE (live_subregs_used);
  3836. BITMAP_FREE (live_relevant_regs);
  3837. BITMAP_FREE (elim_regset);
  3838. if (dump_file)
  3839. print_insn_chains (dump_file);
  3840. }
  3841. /* Examine the rtx found in *LOC, which is read or written to as determined
  3842. by TYPE. Return false if we find a reason why an insn containing this
  3843. rtx should not be moved (such as accesses to non-constant memory), true
  3844. otherwise. */
  3845. static bool
  3846. rtx_moveable_p (rtx *loc, enum op_type type)
  3847. {
  3848. const char *fmt;
  3849. rtx x = *loc;
  3850. enum rtx_code code = GET_CODE (x);
  3851. int i, j;
  3852. code = GET_CODE (x);
  3853. switch (code)
  3854. {
  3855. case CONST:
  3856. CASE_CONST_ANY:
  3857. case SYMBOL_REF:
  3858. case LABEL_REF:
  3859. return true;
  3860. case PC:
  3861. return type == OP_IN;
  3862. case CC0:
  3863. return false;
  3864. case REG:
  3865. if (x == frame_pointer_rtx)
  3866. return true;
  3867. if (HARD_REGISTER_P (x))
  3868. return false;
  3869. return true;
  3870. case MEM:
  3871. if (type == OP_IN && MEM_READONLY_P (x))
  3872. return rtx_moveable_p (&XEXP (x, 0), OP_IN);
  3873. return false;
  3874. case SET:
  3875. return (rtx_moveable_p (&SET_SRC (x), OP_IN)
  3876. && rtx_moveable_p (&SET_DEST (x), OP_OUT));
  3877. case STRICT_LOW_PART:
  3878. return rtx_moveable_p (&XEXP (x, 0), OP_OUT);
  3879. case ZERO_EXTRACT:
  3880. case SIGN_EXTRACT:
  3881. return (rtx_moveable_p (&XEXP (x, 0), type)
  3882. && rtx_moveable_p (&XEXP (x, 1), OP_IN)
  3883. && rtx_moveable_p (&XEXP (x, 2), OP_IN));
  3884. case CLOBBER:
  3885. return rtx_moveable_p (&SET_DEST (x), OP_OUT);
  3886. case UNSPEC_VOLATILE:
  3887. /* It is a bad idea to consider insns with with such rtl
  3888. as moveable ones. The insn scheduler also considers them as barrier
  3889. for a reason. */
  3890. return false;
  3891. default:
  3892. break;
  3893. }
  3894. fmt = GET_RTX_FORMAT (code);
  3895. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  3896. {
  3897. if (fmt[i] == 'e')
  3898. {
  3899. if (!rtx_moveable_p (&XEXP (x, i), type))
  3900. return false;
  3901. }
  3902. else if (fmt[i] == 'E')
  3903. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  3904. {
  3905. if (!rtx_moveable_p (&XVECEXP (x, i, j), type))
  3906. return false;
  3907. }
  3908. }
  3909. return true;
  3910. }
  3911. /* A wrapper around dominated_by_p, which uses the information in UID_LUID
  3912. to give dominance relationships between two insns I1 and I2. */
  3913. static bool
  3914. insn_dominated_by_p (rtx i1, rtx i2, int *uid_luid)
  3915. {
  3916. basic_block bb1 = BLOCK_FOR_INSN (i1);
  3917. basic_block bb2 = BLOCK_FOR_INSN (i2);
  3918. if (bb1 == bb2)
  3919. return uid_luid[INSN_UID (i2)] < uid_luid[INSN_UID (i1)];
  3920. return dominated_by_p (CDI_DOMINATORS, bb1, bb2);
  3921. }
  3922. /* Record the range of register numbers added by find_moveable_pseudos. */
  3923. int first_moveable_pseudo, last_moveable_pseudo;
  3924. /* These two vectors hold data for every register added by
  3925. find_movable_pseudos, with index 0 holding data for the
  3926. first_moveable_pseudo. */
  3927. /* The original home register. */
  3928. static vec<rtx> pseudo_replaced_reg;
  3929. /* Look for instances where we have an instruction that is known to increase
  3930. register pressure, and whose result is not used immediately. If it is
  3931. possible to move the instruction downwards to just before its first use,
  3932. split its lifetime into two ranges. We create a new pseudo to compute the
  3933. value, and emit a move instruction just before the first use. If, after
  3934. register allocation, the new pseudo remains unallocated, the function
  3935. move_unallocated_pseudos then deletes the move instruction and places
  3936. the computation just before the first use.
  3937. Such a move is safe and profitable if all the input registers remain live
  3938. and unchanged between the original computation and its first use. In such
  3939. a situation, the computation is known to increase register pressure, and
  3940. moving it is known to at least not worsen it.
  3941. We restrict moves to only those cases where a register remains unallocated,
  3942. in order to avoid interfering too much with the instruction schedule. As
  3943. an exception, we may move insns which only modify their input register
  3944. (typically induction variables), as this increases the freedom for our
  3945. intended transformation, and does not limit the second instruction
  3946. scheduler pass. */
  3947. static void
  3948. find_moveable_pseudos (void)
  3949. {
  3950. unsigned i;
  3951. int max_regs = max_reg_num ();
  3952. int max_uid = get_max_uid ();
  3953. basic_block bb;
  3954. int *uid_luid = XNEWVEC (int, max_uid);
  3955. rtx_insn **closest_uses = XNEWVEC (rtx_insn *, max_regs);
  3956. /* A set of registers which are live but not modified throughout a block. */
  3957. bitmap_head *bb_transp_live = XNEWVEC (bitmap_head,
  3958. last_basic_block_for_fn (cfun));
  3959. /* A set of registers which only exist in a given basic block. */
  3960. bitmap_head *bb_local = XNEWVEC (bitmap_head,
  3961. last_basic_block_for_fn (cfun));
  3962. /* A set of registers which are set once, in an instruction that can be
  3963. moved freely downwards, but are otherwise transparent to a block. */
  3964. bitmap_head *bb_moveable_reg_sets = XNEWVEC (bitmap_head,
  3965. last_basic_block_for_fn (cfun));
  3966. bitmap_head live, used, set, interesting, unusable_as_input;
  3967. bitmap_iterator bi;
  3968. bitmap_initialize (&interesting, 0);
  3969. first_moveable_pseudo = max_regs;
  3970. pseudo_replaced_reg.release ();
  3971. pseudo_replaced_reg.safe_grow_cleared (max_regs);
  3972. df_analyze ();
  3973. calculate_dominance_info (CDI_DOMINATORS);
  3974. i = 0;
  3975. bitmap_initialize (&live, 0);
  3976. bitmap_initialize (&used, 0);
  3977. bitmap_initialize (&set, 0);
  3978. bitmap_initialize (&unusable_as_input, 0);
  3979. FOR_EACH_BB_FN (bb, cfun)
  3980. {
  3981. rtx_insn *insn;
  3982. bitmap transp = bb_transp_live + bb->index;
  3983. bitmap moveable = bb_moveable_reg_sets + bb->index;
  3984. bitmap local = bb_local + bb->index;
  3985. bitmap_initialize (local, 0);
  3986. bitmap_initialize (transp, 0);
  3987. bitmap_initialize (moveable, 0);
  3988. bitmap_copy (&live, df_get_live_out (bb));
  3989. bitmap_and_into (&live, df_get_live_in (bb));
  3990. bitmap_copy (transp, &live);
  3991. bitmap_clear (moveable);
  3992. bitmap_clear (&live);
  3993. bitmap_clear (&used);
  3994. bitmap_clear (&set);
  3995. FOR_BB_INSNS (bb, insn)
  3996. if (NONDEBUG_INSN_P (insn))
  3997. {
  3998. df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  3999. df_ref def, use;
  4000. uid_luid[INSN_UID (insn)] = i++;
  4001. def = df_single_def (insn_info);
  4002. use = df_single_use (insn_info);
  4003. if (use
  4004. && def
  4005. && DF_REF_REGNO (use) == DF_REF_REGNO (def)
  4006. && !bitmap_bit_p (&set, DF_REF_REGNO (use))
  4007. && rtx_moveable_p (&PATTERN (insn), OP_IN))
  4008. {
  4009. unsigned regno = DF_REF_REGNO (use);
  4010. bitmap_set_bit (moveable, regno);
  4011. bitmap_set_bit (&set, regno);
  4012. bitmap_set_bit (&used, regno);
  4013. bitmap_clear_bit (transp, regno);
  4014. continue;
  4015. }
  4016. FOR_EACH_INSN_INFO_USE (use, insn_info)
  4017. {
  4018. unsigned regno = DF_REF_REGNO (use);
  4019. bitmap_set_bit (&used, regno);
  4020. if (bitmap_clear_bit (moveable, regno))
  4021. bitmap_clear_bit (transp, regno);
  4022. }
  4023. FOR_EACH_INSN_INFO_DEF (def, insn_info)
  4024. {
  4025. unsigned regno = DF_REF_REGNO (def);
  4026. bitmap_set_bit (&set, regno);
  4027. bitmap_clear_bit (transp, regno);
  4028. bitmap_clear_bit (moveable, regno);
  4029. }
  4030. }
  4031. }
  4032. bitmap_clear (&live);
  4033. bitmap_clear (&used);
  4034. bitmap_clear (&set);
  4035. FOR_EACH_BB_FN (bb, cfun)
  4036. {
  4037. bitmap local = bb_local + bb->index;
  4038. rtx_insn *insn;
  4039. FOR_BB_INSNS (bb, insn)
  4040. if (NONDEBUG_INSN_P (insn))
  4041. {
  4042. df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  4043. rtx_insn *def_insn;
  4044. rtx closest_use, note;
  4045. df_ref def, use;
  4046. unsigned regno;
  4047. bool all_dominated, all_local;
  4048. machine_mode mode;
  4049. def = df_single_def (insn_info);
  4050. /* There must be exactly one def in this insn. */
  4051. if (!def || !single_set (insn))
  4052. continue;
  4053. /* This must be the only definition of the reg. We also limit
  4054. which modes we deal with so that we can assume we can generate
  4055. move instructions. */
  4056. regno = DF_REF_REGNO (def);
  4057. mode = GET_MODE (DF_REF_REG (def));
  4058. if (DF_REG_DEF_COUNT (regno) != 1
  4059. || !DF_REF_INSN_INFO (def)
  4060. || HARD_REGISTER_NUM_P (regno)
  4061. || DF_REG_EQ_USE_COUNT (regno) > 0
  4062. || (!INTEGRAL_MODE_P (mode) && !FLOAT_MODE_P (mode)))
  4063. continue;
  4064. def_insn = DF_REF_INSN (def);
  4065. for (note = REG_NOTES (def_insn); note; note = XEXP (note, 1))
  4066. if (REG_NOTE_KIND (note) == REG_EQUIV && MEM_P (XEXP (note, 0)))
  4067. break;
  4068. if (note)
  4069. {
  4070. if (dump_file)
  4071. fprintf (dump_file, "Ignoring reg %d, has equiv memory\n",
  4072. regno);
  4073. bitmap_set_bit (&unusable_as_input, regno);
  4074. continue;
  4075. }
  4076. use = DF_REG_USE_CHAIN (regno);
  4077. all_dominated = true;
  4078. all_local = true;
  4079. closest_use = NULL_RTX;
  4080. for (; use; use = DF_REF_NEXT_REG (use))
  4081. {
  4082. rtx_insn *insn;
  4083. if (!DF_REF_INSN_INFO (use))
  4084. {
  4085. all_dominated = false;
  4086. all_local = false;
  4087. break;
  4088. }
  4089. insn = DF_REF_INSN (use);
  4090. if (DEBUG_INSN_P (insn))
  4091. continue;
  4092. if (BLOCK_FOR_INSN (insn) != BLOCK_FOR_INSN (def_insn))
  4093. all_local = false;
  4094. if (!insn_dominated_by_p (insn, def_insn, uid_luid))
  4095. all_dominated = false;
  4096. if (closest_use != insn && closest_use != const0_rtx)
  4097. {
  4098. if (closest_use == NULL_RTX)
  4099. closest_use = insn;
  4100. else if (insn_dominated_by_p (closest_use, insn, uid_luid))
  4101. closest_use = insn;
  4102. else if (!insn_dominated_by_p (insn, closest_use, uid_luid))
  4103. closest_use = const0_rtx;
  4104. }
  4105. }
  4106. if (!all_dominated)
  4107. {
  4108. if (dump_file)
  4109. fprintf (dump_file, "Reg %d not all uses dominated by set\n",
  4110. regno);
  4111. continue;
  4112. }
  4113. if (all_local)
  4114. bitmap_set_bit (local, regno);
  4115. if (closest_use == const0_rtx || closest_use == NULL
  4116. || next_nonnote_nondebug_insn (def_insn) == closest_use)
  4117. {
  4118. if (dump_file)
  4119. fprintf (dump_file, "Reg %d uninteresting%s\n", regno,
  4120. closest_use == const0_rtx || closest_use == NULL
  4121. ? " (no unique first use)" : "");
  4122. continue;
  4123. }
  4124. #ifdef HAVE_cc0
  4125. if (reg_referenced_p (cc0_rtx, PATTERN (closest_use)))
  4126. {
  4127. if (dump_file)
  4128. fprintf (dump_file, "Reg %d: closest user uses cc0\n",
  4129. regno);
  4130. continue;
  4131. }
  4132. #endif
  4133. bitmap_set_bit (&interesting, regno);
  4134. /* If we get here, we know closest_use is a non-NULL insn
  4135. (as opposed to const_0_rtx). */
  4136. closest_uses[regno] = as_a <rtx_insn *> (closest_use);
  4137. if (dump_file && (all_local || all_dominated))
  4138. {
  4139. fprintf (dump_file, "Reg %u:", regno);
  4140. if (all_local)
  4141. fprintf (dump_file, " local to bb %d", bb->index);
  4142. if (all_dominated)
  4143. fprintf (dump_file, " def dominates all uses");
  4144. if (closest_use != const0_rtx)
  4145. fprintf (dump_file, " has unique first use");
  4146. fputs ("\n", dump_file);
  4147. }
  4148. }
  4149. }
  4150. EXECUTE_IF_SET_IN_BITMAP (&interesting, 0, i, bi)
  4151. {
  4152. df_ref def = DF_REG_DEF_CHAIN (i);
  4153. rtx_insn *def_insn = DF_REF_INSN (def);
  4154. basic_block def_block = BLOCK_FOR_INSN (def_insn);
  4155. bitmap def_bb_local = bb_local + def_block->index;
  4156. bitmap def_bb_moveable = bb_moveable_reg_sets + def_block->index;
  4157. bitmap def_bb_transp = bb_transp_live + def_block->index;
  4158. bool local_to_bb_p = bitmap_bit_p (def_bb_local, i);
  4159. rtx_insn *use_insn = closest_uses[i];
  4160. df_ref use;
  4161. bool all_ok = true;
  4162. bool all_transp = true;
  4163. if (!REG_P (DF_REF_REG (def)))
  4164. continue;
  4165. if (!local_to_bb_p)
  4166. {
  4167. if (dump_file)
  4168. fprintf (dump_file, "Reg %u not local to one basic block\n",
  4169. i);
  4170. continue;
  4171. }
  4172. if (reg_equiv_init (i) != NULL_RTX)
  4173. {
  4174. if (dump_file)
  4175. fprintf (dump_file, "Ignoring reg %u with equiv init insn\n",
  4176. i);
  4177. continue;
  4178. }
  4179. if (!rtx_moveable_p (&PATTERN (def_insn), OP_IN))
  4180. {
  4181. if (dump_file)
  4182. fprintf (dump_file, "Found def insn %d for %d to be not moveable\n",
  4183. INSN_UID (def_insn), i);
  4184. continue;
  4185. }
  4186. if (dump_file)
  4187. fprintf (dump_file, "Examining insn %d, def for %d\n",
  4188. INSN_UID (def_insn), i);
  4189. FOR_EACH_INSN_USE (use, def_insn)
  4190. {
  4191. unsigned regno = DF_REF_REGNO (use);
  4192. if (bitmap_bit_p (&unusable_as_input, regno))
  4193. {
  4194. all_ok = false;
  4195. if (dump_file)
  4196. fprintf (dump_file, " found unusable input reg %u.\n", regno);
  4197. break;
  4198. }
  4199. if (!bitmap_bit_p (def_bb_transp, regno))
  4200. {
  4201. if (bitmap_bit_p (def_bb_moveable, regno)
  4202. && !control_flow_insn_p (use_insn)
  4203. #ifdef HAVE_cc0
  4204. && !sets_cc0_p (use_insn)
  4205. #endif
  4206. )
  4207. {
  4208. if (modified_between_p (DF_REF_REG (use), def_insn, use_insn))
  4209. {
  4210. rtx_insn *x = NEXT_INSN (def_insn);
  4211. while (!modified_in_p (DF_REF_REG (use), x))
  4212. {
  4213. gcc_assert (x != use_insn);
  4214. x = NEXT_INSN (x);
  4215. }
  4216. if (dump_file)
  4217. fprintf (dump_file, " input reg %u modified but insn %d moveable\n",
  4218. regno, INSN_UID (x));
  4219. emit_insn_after (PATTERN (x), use_insn);
  4220. set_insn_deleted (x);
  4221. }
  4222. else
  4223. {
  4224. if (dump_file)
  4225. fprintf (dump_file, " input reg %u modified between def and use\n",
  4226. regno);
  4227. all_transp = false;
  4228. }
  4229. }
  4230. else
  4231. all_transp = false;
  4232. }
  4233. }
  4234. if (!all_ok)
  4235. continue;
  4236. if (!dbg_cnt (ira_move))
  4237. break;
  4238. if (dump_file)
  4239. fprintf (dump_file, " all ok%s\n", all_transp ? " and transp" : "");
  4240. if (all_transp)
  4241. {
  4242. rtx def_reg = DF_REF_REG (def);
  4243. rtx newreg = ira_create_new_reg (def_reg);
  4244. if (validate_change (def_insn, DF_REF_REAL_LOC (def), newreg, 0))
  4245. {
  4246. unsigned nregno = REGNO (newreg);
  4247. emit_insn_before (gen_move_insn (def_reg, newreg), use_insn);
  4248. nregno -= max_regs;
  4249. pseudo_replaced_reg[nregno] = def_reg;
  4250. }
  4251. }
  4252. }
  4253. FOR_EACH_BB_FN (bb, cfun)
  4254. {
  4255. bitmap_clear (bb_local + bb->index);
  4256. bitmap_clear (bb_transp_live + bb->index);
  4257. bitmap_clear (bb_moveable_reg_sets + bb->index);
  4258. }
  4259. bitmap_clear (&interesting);
  4260. bitmap_clear (&unusable_as_input);
  4261. free (uid_luid);
  4262. free (closest_uses);
  4263. free (bb_local);
  4264. free (bb_transp_live);
  4265. free (bb_moveable_reg_sets);
  4266. last_moveable_pseudo = max_reg_num ();
  4267. fix_reg_equiv_init ();
  4268. expand_reg_info ();
  4269. regstat_free_n_sets_and_refs ();
  4270. regstat_free_ri ();
  4271. regstat_init_n_sets_and_refs ();
  4272. regstat_compute_ri ();
  4273. free_dominance_info (CDI_DOMINATORS);
  4274. }
  4275. /* If SET pattern SET is an assignment from a hard register to a pseudo which
  4276. is live at CALL_DOM (if non-NULL, otherwise this check is omitted), return
  4277. the destination. Otherwise return NULL. */
  4278. static rtx
  4279. interesting_dest_for_shprep_1 (rtx set, basic_block call_dom)
  4280. {
  4281. rtx src = SET_SRC (set);
  4282. rtx dest = SET_DEST (set);
  4283. if (!REG_P (src) || !HARD_REGISTER_P (src)
  4284. || !REG_P (dest) || HARD_REGISTER_P (dest)
  4285. || (call_dom && !bitmap_bit_p (df_get_live_in (call_dom), REGNO (dest))))
  4286. return NULL;
  4287. return dest;
  4288. }
  4289. /* If insn is interesting for parameter range-splitting shrink-wrapping
  4290. preparation, i.e. it is a single set from a hard register to a pseudo, which
  4291. is live at CALL_DOM (if non-NULL, otherwise this check is omitted), or a
  4292. parallel statement with only one such statement, return the destination.
  4293. Otherwise return NULL. */
  4294. static rtx
  4295. interesting_dest_for_shprep (rtx_insn *insn, basic_block call_dom)
  4296. {
  4297. if (!INSN_P (insn))
  4298. return NULL;
  4299. rtx pat = PATTERN (insn);
  4300. if (GET_CODE (pat) == SET)
  4301. return interesting_dest_for_shprep_1 (pat, call_dom);
  4302. if (GET_CODE (pat) != PARALLEL)
  4303. return NULL;
  4304. rtx ret = NULL;
  4305. for (int i = 0; i < XVECLEN (pat, 0); i++)
  4306. {
  4307. rtx sub = XVECEXP (pat, 0, i);
  4308. if (GET_CODE (sub) == USE || GET_CODE (sub) == CLOBBER)
  4309. continue;
  4310. if (GET_CODE (sub) != SET
  4311. || side_effects_p (sub))
  4312. return NULL;
  4313. rtx dest = interesting_dest_for_shprep_1 (sub, call_dom);
  4314. if (dest && ret)
  4315. return NULL;
  4316. if (dest)
  4317. ret = dest;
  4318. }
  4319. return ret;
  4320. }
  4321. /* Split live ranges of pseudos that are loaded from hard registers in the
  4322. first BB in a BB that dominates all non-sibling call if such a BB can be
  4323. found and is not in a loop. Return true if the function has made any
  4324. changes. */
  4325. static bool
  4326. split_live_ranges_for_shrink_wrap (void)
  4327. {
  4328. basic_block bb, call_dom = NULL;
  4329. basic_block first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
  4330. rtx_insn *insn, *last_interesting_insn = NULL;
  4331. bitmap_head need_new, reachable;
  4332. vec<basic_block> queue;
  4333. if (!SHRINK_WRAPPING_ENABLED)
  4334. return false;
  4335. bitmap_initialize (&need_new, 0);
  4336. bitmap_initialize (&reachable, 0);
  4337. queue.create (n_basic_blocks_for_fn (cfun));
  4338. FOR_EACH_BB_FN (bb, cfun)
  4339. FOR_BB_INSNS (bb, insn)
  4340. if (CALL_P (insn) && !SIBLING_CALL_P (insn))
  4341. {
  4342. if (bb == first)
  4343. {
  4344. bitmap_clear (&need_new);
  4345. bitmap_clear (&reachable);
  4346. queue.release ();
  4347. return false;
  4348. }
  4349. bitmap_set_bit (&need_new, bb->index);
  4350. bitmap_set_bit (&reachable, bb->index);
  4351. queue.quick_push (bb);
  4352. break;
  4353. }
  4354. if (queue.is_empty ())
  4355. {
  4356. bitmap_clear (&need_new);
  4357. bitmap_clear (&reachable);
  4358. queue.release ();
  4359. return false;
  4360. }
  4361. while (!queue.is_empty ())
  4362. {
  4363. edge e;
  4364. edge_iterator ei;
  4365. bb = queue.pop ();
  4366. FOR_EACH_EDGE (e, ei, bb->succs)
  4367. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  4368. && bitmap_set_bit (&reachable, e->dest->index))
  4369. queue.quick_push (e->dest);
  4370. }
  4371. queue.release ();
  4372. FOR_BB_INSNS (first, insn)
  4373. {
  4374. rtx dest = interesting_dest_for_shprep (insn, NULL);
  4375. if (!dest)
  4376. continue;
  4377. if (DF_REG_DEF_COUNT (REGNO (dest)) > 1)
  4378. {
  4379. bitmap_clear (&need_new);
  4380. bitmap_clear (&reachable);
  4381. return false;
  4382. }
  4383. for (df_ref use = DF_REG_USE_CHAIN (REGNO(dest));
  4384. use;
  4385. use = DF_REF_NEXT_REG (use))
  4386. {
  4387. int ubbi = DF_REF_BB (use)->index;
  4388. if (bitmap_bit_p (&reachable, ubbi))
  4389. bitmap_set_bit (&need_new, ubbi);
  4390. }
  4391. last_interesting_insn = insn;
  4392. }
  4393. bitmap_clear (&reachable);
  4394. if (!last_interesting_insn)
  4395. {
  4396. bitmap_clear (&need_new);
  4397. return false;
  4398. }
  4399. call_dom = nearest_common_dominator_for_set (CDI_DOMINATORS, &need_new);
  4400. bitmap_clear (&need_new);
  4401. if (call_dom == first)
  4402. return false;
  4403. loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
  4404. while (bb_loop_depth (call_dom) > 0)
  4405. call_dom = get_immediate_dominator (CDI_DOMINATORS, call_dom);
  4406. loop_optimizer_finalize ();
  4407. if (call_dom == first)
  4408. return false;
  4409. calculate_dominance_info (CDI_POST_DOMINATORS);
  4410. if (dominated_by_p (CDI_POST_DOMINATORS, first, call_dom))
  4411. {
  4412. free_dominance_info (CDI_POST_DOMINATORS);
  4413. return false;
  4414. }
  4415. free_dominance_info (CDI_POST_DOMINATORS);
  4416. if (dump_file)
  4417. fprintf (dump_file, "Will split live ranges of parameters at BB %i\n",
  4418. call_dom->index);
  4419. bool ret = false;
  4420. FOR_BB_INSNS (first, insn)
  4421. {
  4422. rtx dest = interesting_dest_for_shprep (insn, call_dom);
  4423. if (!dest || dest == pic_offset_table_rtx)
  4424. continue;
  4425. rtx newreg = NULL_RTX;
  4426. df_ref use, next;
  4427. for (use = DF_REG_USE_CHAIN (REGNO (dest)); use; use = next)
  4428. {
  4429. rtx_insn *uin = DF_REF_INSN (use);
  4430. next = DF_REF_NEXT_REG (use);
  4431. basic_block ubb = BLOCK_FOR_INSN (uin);
  4432. if (ubb == call_dom
  4433. || dominated_by_p (CDI_DOMINATORS, ubb, call_dom))
  4434. {
  4435. if (!newreg)
  4436. newreg = ira_create_new_reg (dest);
  4437. validate_change (uin, DF_REF_REAL_LOC (use), newreg, true);
  4438. }
  4439. }
  4440. if (newreg)
  4441. {
  4442. rtx new_move = gen_move_insn (newreg, dest);
  4443. emit_insn_after (new_move, bb_note (call_dom));
  4444. if (dump_file)
  4445. {
  4446. fprintf (dump_file, "Split live-range of register ");
  4447. print_rtl_single (dump_file, dest);
  4448. }
  4449. ret = true;
  4450. }
  4451. if (insn == last_interesting_insn)
  4452. break;
  4453. }
  4454. apply_change_group ();
  4455. return ret;
  4456. }
  4457. /* Perform the second half of the transformation started in
  4458. find_moveable_pseudos. We look for instances where the newly introduced
  4459. pseudo remains unallocated, and remove it by moving the definition to
  4460. just before its use, replacing the move instruction generated by
  4461. find_moveable_pseudos. */
  4462. static void
  4463. move_unallocated_pseudos (void)
  4464. {
  4465. int i;
  4466. for (i = first_moveable_pseudo; i < last_moveable_pseudo; i++)
  4467. if (reg_renumber[i] < 0)
  4468. {
  4469. int idx = i - first_moveable_pseudo;
  4470. rtx other_reg = pseudo_replaced_reg[idx];
  4471. rtx_insn *def_insn = DF_REF_INSN (DF_REG_DEF_CHAIN (i));
  4472. /* The use must follow all definitions of OTHER_REG, so we can
  4473. insert the new definition immediately after any of them. */
  4474. df_ref other_def = DF_REG_DEF_CHAIN (REGNO (other_reg));
  4475. rtx_insn *move_insn = DF_REF_INSN (other_def);
  4476. rtx_insn *newinsn = emit_insn_after (PATTERN (def_insn), move_insn);
  4477. rtx set;
  4478. int success;
  4479. if (dump_file)
  4480. fprintf (dump_file, "moving def of %d (insn %d now) ",
  4481. REGNO (other_reg), INSN_UID (def_insn));
  4482. delete_insn (move_insn);
  4483. while ((other_def = DF_REG_DEF_CHAIN (REGNO (other_reg))))
  4484. delete_insn (DF_REF_INSN (other_def));
  4485. delete_insn (def_insn);
  4486. set = single_set (newinsn);
  4487. success = validate_change (newinsn, &SET_DEST (set), other_reg, 0);
  4488. gcc_assert (success);
  4489. if (dump_file)
  4490. fprintf (dump_file, " %d) rather than keep unallocated replacement %d\n",
  4491. INSN_UID (newinsn), i);
  4492. SET_REG_N_REFS (i, 0);
  4493. }
  4494. }
  4495. /* If the backend knows where to allocate pseudos for hard
  4496. register initial values, register these allocations now. */
  4497. static void
  4498. allocate_initial_values (void)
  4499. {
  4500. if (targetm.allocate_initial_value)
  4501. {
  4502. rtx hreg, preg, x;
  4503. int i, regno;
  4504. for (i = 0; HARD_REGISTER_NUM_P (i); i++)
  4505. {
  4506. if (! initial_value_entry (i, &hreg, &preg))
  4507. break;
  4508. x = targetm.allocate_initial_value (hreg);
  4509. regno = REGNO (preg);
  4510. if (x && REG_N_SETS (regno) <= 1)
  4511. {
  4512. if (MEM_P (x))
  4513. reg_equiv_memory_loc (regno) = x;
  4514. else
  4515. {
  4516. basic_block bb;
  4517. int new_regno;
  4518. gcc_assert (REG_P (x));
  4519. new_regno = REGNO (x);
  4520. reg_renumber[regno] = new_regno;
  4521. /* Poke the regno right into regno_reg_rtx so that even
  4522. fixed regs are accepted. */
  4523. SET_REGNO (preg, new_regno);
  4524. /* Update global register liveness information. */
  4525. FOR_EACH_BB_FN (bb, cfun)
  4526. {
  4527. if (REGNO_REG_SET_P (df_get_live_in (bb), regno))
  4528. SET_REGNO_REG_SET (df_get_live_in (bb), new_regno);
  4529. if (REGNO_REG_SET_P (df_get_live_out (bb), regno))
  4530. SET_REGNO_REG_SET (df_get_live_out (bb), new_regno);
  4531. }
  4532. }
  4533. }
  4534. }
  4535. gcc_checking_assert (! initial_value_entry (FIRST_PSEUDO_REGISTER,
  4536. &hreg, &preg));
  4537. }
  4538. }
  4539. /* True when we use LRA instead of reload pass for the current
  4540. function. */
  4541. bool ira_use_lra_p;
  4542. /* True if we have allocno conflicts. It is false for non-optimized
  4543. mode or when the conflict table is too big. */
  4544. bool ira_conflicts_p;
  4545. /* Saved between IRA and reload. */
  4546. static int saved_flag_ira_share_spill_slots;
  4547. /* This is the main entry of IRA. */
  4548. static void
  4549. ira (FILE *f)
  4550. {
  4551. bool loops_p;
  4552. int ira_max_point_before_emit;
  4553. int rebuild_p;
  4554. bool saved_flag_caller_saves = flag_caller_saves;
  4555. enum ira_region saved_flag_ira_region = flag_ira_region;
  4556. /* Perform target specific PIC register initialization. */
  4557. targetm.init_pic_reg ();
  4558. ira_conflicts_p = optimize > 0;
  4559. ira_use_lra_p = targetm.lra_p ();
  4560. /* If there are too many pseudos and/or basic blocks (e.g. 10K
  4561. pseudos and 10K blocks or 100K pseudos and 1K blocks), we will
  4562. use simplified and faster algorithms in LRA. */
  4563. lra_simple_p
  4564. = (ira_use_lra_p
  4565. && max_reg_num () >= (1 << 26) / last_basic_block_for_fn (cfun));
  4566. if (lra_simple_p)
  4567. {
  4568. /* It permits to skip live range splitting in LRA. */
  4569. flag_caller_saves = false;
  4570. /* There is no sense to do regional allocation when we use
  4571. simplified LRA. */
  4572. flag_ira_region = IRA_REGION_ONE;
  4573. ira_conflicts_p = false;
  4574. }
  4575. #ifndef IRA_NO_OBSTACK
  4576. gcc_obstack_init (&ira_obstack);
  4577. #endif
  4578. bitmap_obstack_initialize (&ira_bitmap_obstack);
  4579. /* LRA uses its own infrastructure to handle caller save registers. */
  4580. if (flag_caller_saves && !ira_use_lra_p)
  4581. init_caller_save ();
  4582. if (flag_ira_verbose < 10)
  4583. {
  4584. internal_flag_ira_verbose = flag_ira_verbose;
  4585. ira_dump_file = f;
  4586. }
  4587. else
  4588. {
  4589. internal_flag_ira_verbose = flag_ira_verbose - 10;
  4590. ira_dump_file = stderr;
  4591. }
  4592. setup_prohibited_mode_move_regs ();
  4593. decrease_live_ranges_number ();
  4594. df_note_add_problem ();
  4595. /* DF_LIVE can't be used in the register allocator, too many other
  4596. parts of the compiler depend on using the "classic" liveness
  4597. interpretation of the DF_LR problem. See PR38711.
  4598. Remove the problem, so that we don't spend time updating it in
  4599. any of the df_analyze() calls during IRA/LRA. */
  4600. if (optimize > 1)
  4601. df_remove_problem (df_live);
  4602. gcc_checking_assert (df_live == NULL);
  4603. #ifdef ENABLE_CHECKING
  4604. df->changeable_flags |= DF_VERIFY_SCHEDULED;
  4605. #endif
  4606. df_analyze ();
  4607. init_reg_equiv ();
  4608. if (ira_conflicts_p)
  4609. {
  4610. calculate_dominance_info (CDI_DOMINATORS);
  4611. if (split_live_ranges_for_shrink_wrap ())
  4612. df_analyze ();
  4613. free_dominance_info (CDI_DOMINATORS);
  4614. }
  4615. df_clear_flags (DF_NO_INSN_RESCAN);
  4616. regstat_init_n_sets_and_refs ();
  4617. regstat_compute_ri ();
  4618. /* If we are not optimizing, then this is the only place before
  4619. register allocation where dataflow is done. And that is needed
  4620. to generate these warnings. */
  4621. if (warn_clobbered)
  4622. generate_setjmp_warnings ();
  4623. /* Determine if the current function is a leaf before running IRA
  4624. since this can impact optimizations done by the prologue and
  4625. epilogue thus changing register elimination offsets. */
  4626. crtl->is_leaf = leaf_function_p ();
  4627. if (resize_reg_info () && flag_ira_loop_pressure)
  4628. ira_set_pseudo_classes (true, ira_dump_file);
  4629. rebuild_p = update_equiv_regs ();
  4630. setup_reg_equiv ();
  4631. setup_reg_equiv_init ();
  4632. if (optimize && rebuild_p)
  4633. {
  4634. timevar_push (TV_JUMP);
  4635. rebuild_jump_labels (get_insns ());
  4636. if (purge_all_dead_edges ())
  4637. delete_unreachable_blocks ();
  4638. timevar_pop (TV_JUMP);
  4639. }
  4640. allocated_reg_info_size = max_reg_num ();
  4641. if (delete_trivially_dead_insns (get_insns (), max_reg_num ()))
  4642. df_analyze ();
  4643. /* It is not worth to do such improvement when we use a simple
  4644. allocation because of -O0 usage or because the function is too
  4645. big. */
  4646. if (ira_conflicts_p)
  4647. find_moveable_pseudos ();
  4648. max_regno_before_ira = max_reg_num ();
  4649. ira_setup_eliminable_regset ();
  4650. ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
  4651. ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
  4652. ira_move_loops_num = ira_additional_jumps_num = 0;
  4653. ira_assert (current_loops == NULL);
  4654. if (flag_ira_region == IRA_REGION_ALL || flag_ira_region == IRA_REGION_MIXED)
  4655. loop_optimizer_init (AVOID_CFG_MODIFICATIONS | LOOPS_HAVE_RECORDED_EXITS);
  4656. if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
  4657. fprintf (ira_dump_file, "Building IRA IR\n");
  4658. loops_p = ira_build ();
  4659. ira_assert (ira_conflicts_p || !loops_p);
  4660. saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
  4661. if (too_high_register_pressure_p () || cfun->calls_setjmp)
  4662. /* It is just wasting compiler's time to pack spilled pseudos into
  4663. stack slots in this case -- prohibit it. We also do this if
  4664. there is setjmp call because a variable not modified between
  4665. setjmp and longjmp the compiler is required to preserve its
  4666. value and sharing slots does not guarantee it. */
  4667. flag_ira_share_spill_slots = FALSE;
  4668. ira_color ();
  4669. ira_max_point_before_emit = ira_max_point;
  4670. ira_initiate_emit_data ();
  4671. ira_emit (loops_p);
  4672. max_regno = max_reg_num ();
  4673. if (ira_conflicts_p)
  4674. {
  4675. if (! loops_p)
  4676. {
  4677. if (! ira_use_lra_p)
  4678. ira_initiate_assign ();
  4679. }
  4680. else
  4681. {
  4682. expand_reg_info ();
  4683. if (ira_use_lra_p)
  4684. {
  4685. ira_allocno_t a;
  4686. ira_allocno_iterator ai;
  4687. FOR_EACH_ALLOCNO (a, ai)
  4688. {
  4689. int old_regno = ALLOCNO_REGNO (a);
  4690. int new_regno = REGNO (ALLOCNO_EMIT_DATA (a)->reg);
  4691. ALLOCNO_REGNO (a) = new_regno;
  4692. if (old_regno != new_regno)
  4693. setup_reg_classes (new_regno, reg_preferred_class (old_regno),
  4694. reg_alternate_class (old_regno),
  4695. reg_allocno_class (old_regno));
  4696. }
  4697. }
  4698. else
  4699. {
  4700. if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
  4701. fprintf (ira_dump_file, "Flattening IR\n");
  4702. ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
  4703. }
  4704. /* New insns were generated: add notes and recalculate live
  4705. info. */
  4706. df_analyze ();
  4707. /* ??? Rebuild the loop tree, but why? Does the loop tree
  4708. change if new insns were generated? Can that be handled
  4709. by updating the loop tree incrementally? */
  4710. loop_optimizer_finalize ();
  4711. free_dominance_info (CDI_DOMINATORS);
  4712. loop_optimizer_init (AVOID_CFG_MODIFICATIONS
  4713. | LOOPS_HAVE_RECORDED_EXITS);
  4714. if (! ira_use_lra_p)
  4715. {
  4716. setup_allocno_assignment_flags ();
  4717. ira_initiate_assign ();
  4718. ira_reassign_conflict_allocnos (max_regno);
  4719. }
  4720. }
  4721. }
  4722. ira_finish_emit_data ();
  4723. setup_reg_renumber ();
  4724. calculate_allocation_cost ();
  4725. #ifdef ENABLE_IRA_CHECKING
  4726. if (ira_conflicts_p)
  4727. check_allocation ();
  4728. #endif
  4729. if (max_regno != max_regno_before_ira)
  4730. {
  4731. regstat_free_n_sets_and_refs ();
  4732. regstat_free_ri ();
  4733. regstat_init_n_sets_and_refs ();
  4734. regstat_compute_ri ();
  4735. }
  4736. overall_cost_before = ira_overall_cost;
  4737. if (! ira_conflicts_p)
  4738. grow_reg_equivs ();
  4739. else
  4740. {
  4741. fix_reg_equiv_init ();
  4742. #ifdef ENABLE_IRA_CHECKING
  4743. print_redundant_copies ();
  4744. #endif
  4745. if (! ira_use_lra_p)
  4746. {
  4747. ira_spilled_reg_stack_slots_num = 0;
  4748. ira_spilled_reg_stack_slots
  4749. = ((struct ira_spilled_reg_stack_slot *)
  4750. ira_allocate (max_regno
  4751. * sizeof (struct ira_spilled_reg_stack_slot)));
  4752. memset (ira_spilled_reg_stack_slots, 0,
  4753. max_regno * sizeof (struct ira_spilled_reg_stack_slot));
  4754. }
  4755. }
  4756. allocate_initial_values ();
  4757. /* See comment for find_moveable_pseudos call. */
  4758. if (ira_conflicts_p)
  4759. move_unallocated_pseudos ();
  4760. /* Restore original values. */
  4761. if (lra_simple_p)
  4762. {
  4763. flag_caller_saves = saved_flag_caller_saves;
  4764. flag_ira_region = saved_flag_ira_region;
  4765. }
  4766. }
  4767. static void
  4768. do_reload (void)
  4769. {
  4770. basic_block bb;
  4771. bool need_dce;
  4772. unsigned pic_offset_table_regno = INVALID_REGNUM;
  4773. if (flag_ira_verbose < 10)
  4774. ira_dump_file = dump_file;
  4775. /* If pic_offset_table_rtx is a pseudo register, then keep it so
  4776. after reload to avoid possible wrong usages of hard reg assigned
  4777. to it. */
  4778. if (pic_offset_table_rtx
  4779. && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
  4780. pic_offset_table_regno = REGNO (pic_offset_table_rtx);
  4781. timevar_push (TV_RELOAD);
  4782. if (ira_use_lra_p)
  4783. {
  4784. if (current_loops != NULL)
  4785. {
  4786. loop_optimizer_finalize ();
  4787. free_dominance_info (CDI_DOMINATORS);
  4788. }
  4789. FOR_ALL_BB_FN (bb, cfun)
  4790. bb->loop_father = NULL;
  4791. current_loops = NULL;
  4792. ira_destroy ();
  4793. lra (ira_dump_file);
  4794. /* ???!!! Move it before lra () when we use ira_reg_equiv in
  4795. LRA. */
  4796. vec_free (reg_equivs);
  4797. reg_equivs = NULL;
  4798. need_dce = false;
  4799. }
  4800. else
  4801. {
  4802. df_set_flags (DF_NO_INSN_RESCAN);
  4803. build_insn_chain ();
  4804. need_dce = reload (get_insns (), ira_conflicts_p);
  4805. }
  4806. timevar_pop (TV_RELOAD);
  4807. timevar_push (TV_IRA);
  4808. if (ira_conflicts_p && ! ira_use_lra_p)
  4809. {
  4810. ira_free (ira_spilled_reg_stack_slots);
  4811. ira_finish_assign ();
  4812. }
  4813. if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
  4814. && overall_cost_before != ira_overall_cost)
  4815. fprintf (ira_dump_file, "+++Overall after reload %"PRId64 "\n",
  4816. ira_overall_cost);
  4817. flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
  4818. if (! ira_use_lra_p)
  4819. {
  4820. ira_destroy ();
  4821. if (current_loops != NULL)
  4822. {
  4823. loop_optimizer_finalize ();
  4824. free_dominance_info (CDI_DOMINATORS);
  4825. }
  4826. FOR_ALL_BB_FN (bb, cfun)
  4827. bb->loop_father = NULL;
  4828. current_loops = NULL;
  4829. regstat_free_ri ();
  4830. regstat_free_n_sets_and_refs ();
  4831. }
  4832. if (optimize)
  4833. cleanup_cfg (CLEANUP_EXPENSIVE);
  4834. finish_reg_equiv ();
  4835. bitmap_obstack_release (&ira_bitmap_obstack);
  4836. #ifndef IRA_NO_OBSTACK
  4837. obstack_free (&ira_obstack, NULL);
  4838. #endif
  4839. /* The code after the reload has changed so much that at this point
  4840. we might as well just rescan everything. Note that
  4841. df_rescan_all_insns is not going to help here because it does not
  4842. touch the artificial uses and defs. */
  4843. df_finish_pass (true);
  4844. df_scan_alloc (NULL);
  4845. df_scan_blocks ();
  4846. if (optimize > 1)
  4847. {
  4848. df_live_add_problem ();
  4849. df_live_set_all_dirty ();
  4850. }
  4851. if (optimize)
  4852. df_analyze ();
  4853. if (need_dce && optimize)
  4854. run_fast_dce ();
  4855. /* Diagnose uses of the hard frame pointer when it is used as a global
  4856. register. Often we can get away with letting the user appropriate
  4857. the frame pointer, but we should let them know when code generation
  4858. makes that impossible. */
  4859. if (global_regs[HARD_FRAME_POINTER_REGNUM] && frame_pointer_needed)
  4860. {
  4861. tree decl = global_regs_decl[HARD_FRAME_POINTER_REGNUM];
  4862. error_at (DECL_SOURCE_LOCATION (current_function_decl),
  4863. "frame pointer required, but reserved");
  4864. inform (DECL_SOURCE_LOCATION (decl), "for %qD", decl);
  4865. }
  4866. if (pic_offset_table_regno != INVALID_REGNUM)
  4867. pic_offset_table_rtx = gen_rtx_REG (Pmode, pic_offset_table_regno);
  4868. timevar_pop (TV_IRA);
  4869. }
  4870. /* Run the integrated register allocator. */
  4871. namespace {
  4872. const pass_data pass_data_ira =
  4873. {
  4874. RTL_PASS, /* type */
  4875. "ira", /* name */
  4876. OPTGROUP_NONE, /* optinfo_flags */
  4877. TV_IRA, /* tv_id */
  4878. 0, /* properties_required */
  4879. 0, /* properties_provided */
  4880. 0, /* properties_destroyed */
  4881. 0, /* todo_flags_start */
  4882. TODO_do_not_ggc_collect, /* todo_flags_finish */
  4883. };
  4884. class pass_ira : public rtl_opt_pass
  4885. {
  4886. public:
  4887. pass_ira (gcc::context *ctxt)
  4888. : rtl_opt_pass (pass_data_ira, ctxt)
  4889. {}
  4890. /* opt_pass methods: */
  4891. virtual bool gate (function *)
  4892. {
  4893. return !targetm.no_register_allocation;
  4894. }
  4895. virtual unsigned int execute (function *)
  4896. {
  4897. ira (dump_file);
  4898. return 0;
  4899. }
  4900. }; // class pass_ira
  4901. } // anon namespace
  4902. rtl_opt_pass *
  4903. make_pass_ira (gcc::context *ctxt)
  4904. {
  4905. return new pass_ira (ctxt);
  4906. }
  4907. namespace {
  4908. const pass_data pass_data_reload =
  4909. {
  4910. RTL_PASS, /* type */
  4911. "reload", /* name */
  4912. OPTGROUP_NONE, /* optinfo_flags */
  4913. TV_RELOAD, /* tv_id */
  4914. 0, /* properties_required */
  4915. 0, /* properties_provided */
  4916. 0, /* properties_destroyed */
  4917. 0, /* todo_flags_start */
  4918. 0, /* todo_flags_finish */
  4919. };
  4920. class pass_reload : public rtl_opt_pass
  4921. {
  4922. public:
  4923. pass_reload (gcc::context *ctxt)
  4924. : rtl_opt_pass (pass_data_reload, ctxt)
  4925. {}
  4926. /* opt_pass methods: */
  4927. virtual bool gate (function *)
  4928. {
  4929. return !targetm.no_register_allocation;
  4930. }
  4931. virtual unsigned int execute (function *)
  4932. {
  4933. do_reload ();
  4934. return 0;
  4935. }
  4936. }; // class pass_reload
  4937. } // anon namespace
  4938. rtl_opt_pass *
  4939. make_pass_reload (gcc::context *ctxt)
  4940. {
  4941. return new pass_reload (ctxt);
  4942. }