ira-build.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547
  1. /* Building internal representation for IRA.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. Contributed by Vladimir Makarov <vmakarov@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "rtl.h"
  21. #include "tm_p.h"
  22. #include "target.h"
  23. #include "regs.h"
  24. #include "flags.h"
  25. #include "hard-reg-set.h"
  26. #include "predict.h"
  27. #include "vec.h"
  28. #include "hashtab.h"
  29. #include "hash-set.h"
  30. #include "machmode.h"
  31. #include "input.h"
  32. #include "function.h"
  33. #include "dominance.h"
  34. #include "cfg.h"
  35. #include "basic-block.h"
  36. #include "insn-config.h"
  37. #include "recog.h"
  38. #include "diagnostic-core.h"
  39. #include "params.h"
  40. #include "df.h"
  41. #include "reload.h"
  42. #include "sparseset.h"
  43. #include "ira-int.h"
  44. #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
  45. static ira_copy_t find_allocno_copy (ira_allocno_t, ira_allocno_t, rtx_insn *,
  46. ira_loop_tree_node_t);
  47. /* The root of the loop tree corresponding to the all function. */
  48. ira_loop_tree_node_t ira_loop_tree_root;
  49. /* Height of the loop tree. */
  50. int ira_loop_tree_height;
  51. /* All nodes representing basic blocks are referred through the
  52. following array. We can not use basic block member `aux' for this
  53. because it is used for insertion of insns on edges. */
  54. ira_loop_tree_node_t ira_bb_nodes;
  55. /* All nodes representing loops are referred through the following
  56. array. */
  57. ira_loop_tree_node_t ira_loop_nodes;
  58. /* And size of the ira_loop_nodes array. */
  59. unsigned int ira_loop_nodes_count;
  60. /* Map regno -> allocnos with given regno (see comments for
  61. allocno member `next_regno_allocno'). */
  62. ira_allocno_t *ira_regno_allocno_map;
  63. /* Array of references to all allocnos. The order number of the
  64. allocno corresponds to the index in the array. Removed allocnos
  65. have NULL element value. */
  66. ira_allocno_t *ira_allocnos;
  67. /* Sizes of the previous array. */
  68. int ira_allocnos_num;
  69. /* Count of conflict record structures we've created, used when creating
  70. a new conflict id. */
  71. int ira_objects_num;
  72. /* Map a conflict id to its conflict record. */
  73. ira_object_t *ira_object_id_map;
  74. /* Array of references to all allocno preferences. The order number
  75. of the preference corresponds to the index in the array. */
  76. ira_pref_t *ira_prefs;
  77. /* Size of the previous array. */
  78. int ira_prefs_num;
  79. /* Array of references to all copies. The order number of the copy
  80. corresponds to the index in the array. Removed copies have NULL
  81. element value. */
  82. ira_copy_t *ira_copies;
  83. /* Size of the previous array. */
  84. int ira_copies_num;
  85. /* LAST_BASIC_BLOCK before generating additional insns because of live
  86. range splitting. Emitting insns on a critical edge creates a new
  87. basic block. */
  88. static int last_basic_block_before_change;
  89. /* Initialize some members in loop tree node NODE. Use LOOP_NUM for
  90. the member loop_num. */
  91. static void
  92. init_loop_tree_node (struct ira_loop_tree_node *node, int loop_num)
  93. {
  94. int max_regno = max_reg_num ();
  95. node->regno_allocno_map
  96. = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t) * max_regno);
  97. memset (node->regno_allocno_map, 0, sizeof (ira_allocno_t) * max_regno);
  98. memset (node->reg_pressure, 0, sizeof (node->reg_pressure));
  99. node->all_allocnos = ira_allocate_bitmap ();
  100. node->modified_regnos = ira_allocate_bitmap ();
  101. node->border_allocnos = ira_allocate_bitmap ();
  102. node->local_copies = ira_allocate_bitmap ();
  103. node->loop_num = loop_num;
  104. node->children = NULL;
  105. node->subloops = NULL;
  106. }
  107. /* The following function allocates the loop tree nodes. If
  108. CURRENT_LOOPS is NULL, the nodes corresponding to the loops (except
  109. the root which corresponds the all function) will be not allocated
  110. but nodes will still be allocated for basic blocks. */
  111. static void
  112. create_loop_tree_nodes (void)
  113. {
  114. unsigned int i, j;
  115. bool skip_p;
  116. edge_iterator ei;
  117. edge e;
  118. vec<edge> edges;
  119. loop_p loop;
  120. ira_bb_nodes
  121. = ((struct ira_loop_tree_node *)
  122. ira_allocate (sizeof (struct ira_loop_tree_node)
  123. * last_basic_block_for_fn (cfun)));
  124. last_basic_block_before_change = last_basic_block_for_fn (cfun);
  125. for (i = 0; i < (unsigned int) last_basic_block_for_fn (cfun); i++)
  126. {
  127. ira_bb_nodes[i].regno_allocno_map = NULL;
  128. memset (ira_bb_nodes[i].reg_pressure, 0,
  129. sizeof (ira_bb_nodes[i].reg_pressure));
  130. ira_bb_nodes[i].all_allocnos = NULL;
  131. ira_bb_nodes[i].modified_regnos = NULL;
  132. ira_bb_nodes[i].border_allocnos = NULL;
  133. ira_bb_nodes[i].local_copies = NULL;
  134. }
  135. if (current_loops == NULL)
  136. {
  137. ira_loop_nodes_count = 1;
  138. ira_loop_nodes = ((struct ira_loop_tree_node *)
  139. ira_allocate (sizeof (struct ira_loop_tree_node)));
  140. init_loop_tree_node (ira_loop_nodes, 0);
  141. return;
  142. }
  143. ira_loop_nodes_count = number_of_loops (cfun);
  144. ira_loop_nodes = ((struct ira_loop_tree_node *)
  145. ira_allocate (sizeof (struct ira_loop_tree_node)
  146. * ira_loop_nodes_count));
  147. FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
  148. {
  149. if (loop_outer (loop) != NULL)
  150. {
  151. ira_loop_nodes[i].regno_allocno_map = NULL;
  152. skip_p = false;
  153. FOR_EACH_EDGE (e, ei, loop->header->preds)
  154. if (e->src != loop->latch
  155. && (e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
  156. {
  157. skip_p = true;
  158. break;
  159. }
  160. if (skip_p)
  161. continue;
  162. edges = get_loop_exit_edges (loop);
  163. FOR_EACH_VEC_ELT (edges, j, e)
  164. if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
  165. {
  166. skip_p = true;
  167. break;
  168. }
  169. edges.release ();
  170. if (skip_p)
  171. continue;
  172. }
  173. init_loop_tree_node (&ira_loop_nodes[i], loop->num);
  174. }
  175. }
  176. /* The function returns TRUE if there are more one allocation
  177. region. */
  178. static bool
  179. more_one_region_p (void)
  180. {
  181. unsigned int i;
  182. loop_p loop;
  183. if (current_loops != NULL)
  184. FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
  185. if (ira_loop_nodes[i].regno_allocno_map != NULL
  186. && ira_loop_tree_root != &ira_loop_nodes[i])
  187. return true;
  188. return false;
  189. }
  190. /* Free the loop tree node of a loop. */
  191. static void
  192. finish_loop_tree_node (ira_loop_tree_node_t loop)
  193. {
  194. if (loop->regno_allocno_map != NULL)
  195. {
  196. ira_assert (loop->bb == NULL);
  197. ira_free_bitmap (loop->local_copies);
  198. ira_free_bitmap (loop->border_allocnos);
  199. ira_free_bitmap (loop->modified_regnos);
  200. ira_free_bitmap (loop->all_allocnos);
  201. ira_free (loop->regno_allocno_map);
  202. loop->regno_allocno_map = NULL;
  203. }
  204. }
  205. /* Free the loop tree nodes. */
  206. static void
  207. finish_loop_tree_nodes (void)
  208. {
  209. unsigned int i;
  210. for (i = 0; i < ira_loop_nodes_count; i++)
  211. finish_loop_tree_node (&ira_loop_nodes[i]);
  212. ira_free (ira_loop_nodes);
  213. for (i = 0; i < (unsigned int) last_basic_block_before_change; i++)
  214. {
  215. if (ira_bb_nodes[i].local_copies != NULL)
  216. ira_free_bitmap (ira_bb_nodes[i].local_copies);
  217. if (ira_bb_nodes[i].border_allocnos != NULL)
  218. ira_free_bitmap (ira_bb_nodes[i].border_allocnos);
  219. if (ira_bb_nodes[i].modified_regnos != NULL)
  220. ira_free_bitmap (ira_bb_nodes[i].modified_regnos);
  221. if (ira_bb_nodes[i].all_allocnos != NULL)
  222. ira_free_bitmap (ira_bb_nodes[i].all_allocnos);
  223. if (ira_bb_nodes[i].regno_allocno_map != NULL)
  224. ira_free (ira_bb_nodes[i].regno_allocno_map);
  225. }
  226. ira_free (ira_bb_nodes);
  227. }
  228. /* The following recursive function adds LOOP to the loop tree
  229. hierarchy. LOOP is added only once. If LOOP is NULL we adding
  230. loop designating the whole function when CFG loops are not
  231. built. */
  232. static void
  233. add_loop_to_tree (struct loop *loop)
  234. {
  235. int loop_num;
  236. struct loop *parent;
  237. ira_loop_tree_node_t loop_node, parent_node;
  238. /* We can not use loop node access macros here because of potential
  239. checking and because the nodes are not initialized enough
  240. yet. */
  241. if (loop != NULL && loop_outer (loop) != NULL)
  242. add_loop_to_tree (loop_outer (loop));
  243. loop_num = loop != NULL ? loop->num : 0;
  244. if (ira_loop_nodes[loop_num].regno_allocno_map != NULL
  245. && ira_loop_nodes[loop_num].children == NULL)
  246. {
  247. /* We have not added loop node to the tree yet. */
  248. loop_node = &ira_loop_nodes[loop_num];
  249. loop_node->loop = loop;
  250. loop_node->bb = NULL;
  251. if (loop == NULL)
  252. parent = NULL;
  253. else
  254. {
  255. for (parent = loop_outer (loop);
  256. parent != NULL;
  257. parent = loop_outer (parent))
  258. if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
  259. break;
  260. }
  261. if (parent == NULL)
  262. {
  263. loop_node->next = NULL;
  264. loop_node->subloop_next = NULL;
  265. loop_node->parent = NULL;
  266. }
  267. else
  268. {
  269. parent_node = &ira_loop_nodes[parent->num];
  270. loop_node->next = parent_node->children;
  271. parent_node->children = loop_node;
  272. loop_node->subloop_next = parent_node->subloops;
  273. parent_node->subloops = loop_node;
  274. loop_node->parent = parent_node;
  275. }
  276. }
  277. }
  278. /* The following recursive function sets up levels of nodes of the
  279. tree given its root LOOP_NODE. The enumeration starts with LEVEL.
  280. The function returns maximal value of level in the tree + 1. */
  281. static int
  282. setup_loop_tree_level (ira_loop_tree_node_t loop_node, int level)
  283. {
  284. int height, max_height;
  285. ira_loop_tree_node_t subloop_node;
  286. ira_assert (loop_node->bb == NULL);
  287. loop_node->level = level;
  288. max_height = level + 1;
  289. for (subloop_node = loop_node->subloops;
  290. subloop_node != NULL;
  291. subloop_node = subloop_node->subloop_next)
  292. {
  293. ira_assert (subloop_node->bb == NULL);
  294. height = setup_loop_tree_level (subloop_node, level + 1);
  295. if (height > max_height)
  296. max_height = height;
  297. }
  298. return max_height;
  299. }
  300. /* Create the loop tree. The algorithm is designed to provide correct
  301. order of loops (they are ordered by their last loop BB) and basic
  302. blocks in the chain formed by member next. */
  303. static void
  304. form_loop_tree (void)
  305. {
  306. basic_block bb;
  307. struct loop *parent;
  308. ira_loop_tree_node_t bb_node, loop_node;
  309. /* We can not use loop/bb node access macros because of potential
  310. checking and because the nodes are not initialized enough
  311. yet. */
  312. FOR_EACH_BB_FN (bb, cfun)
  313. {
  314. bb_node = &ira_bb_nodes[bb->index];
  315. bb_node->bb = bb;
  316. bb_node->loop = NULL;
  317. bb_node->subloops = NULL;
  318. bb_node->children = NULL;
  319. bb_node->subloop_next = NULL;
  320. bb_node->next = NULL;
  321. if (current_loops == NULL)
  322. parent = NULL;
  323. else
  324. {
  325. for (parent = bb->loop_father;
  326. parent != NULL;
  327. parent = loop_outer (parent))
  328. if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
  329. break;
  330. }
  331. add_loop_to_tree (parent);
  332. loop_node = &ira_loop_nodes[parent == NULL ? 0 : parent->num];
  333. bb_node->next = loop_node->children;
  334. bb_node->parent = loop_node;
  335. loop_node->children = bb_node;
  336. }
  337. ira_loop_tree_root = IRA_LOOP_NODE_BY_INDEX (0);
  338. ira_loop_tree_height = setup_loop_tree_level (ira_loop_tree_root, 0);
  339. ira_assert (ira_loop_tree_root->regno_allocno_map != NULL);
  340. }
  341. /* Rebuild IRA_REGNO_ALLOCNO_MAP and REGNO_ALLOCNO_MAPs of the loop
  342. tree nodes. */
  343. static void
  344. rebuild_regno_allocno_maps (void)
  345. {
  346. unsigned int l;
  347. int max_regno, regno;
  348. ira_allocno_t a;
  349. ira_loop_tree_node_t loop_tree_node;
  350. loop_p loop;
  351. ira_allocno_iterator ai;
  352. ira_assert (current_loops != NULL);
  353. max_regno = max_reg_num ();
  354. FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), l, loop)
  355. if (ira_loop_nodes[l].regno_allocno_map != NULL)
  356. {
  357. ira_free (ira_loop_nodes[l].regno_allocno_map);
  358. ira_loop_nodes[l].regno_allocno_map
  359. = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
  360. * max_regno);
  361. memset (ira_loop_nodes[l].regno_allocno_map, 0,
  362. sizeof (ira_allocno_t) * max_regno);
  363. }
  364. ira_free (ira_regno_allocno_map);
  365. ira_regno_allocno_map
  366. = (ira_allocno_t *) ira_allocate (max_regno * sizeof (ira_allocno_t));
  367. memset (ira_regno_allocno_map, 0, max_regno * sizeof (ira_allocno_t));
  368. FOR_EACH_ALLOCNO (a, ai)
  369. {
  370. if (ALLOCNO_CAP_MEMBER (a) != NULL)
  371. /* Caps are not in the regno allocno maps. */
  372. continue;
  373. regno = ALLOCNO_REGNO (a);
  374. loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
  375. ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
  376. ira_regno_allocno_map[regno] = a;
  377. if (loop_tree_node->regno_allocno_map[regno] == NULL)
  378. /* Remember that we can create temporary allocnos to break
  379. cycles in register shuffle. */
  380. loop_tree_node->regno_allocno_map[regno] = a;
  381. }
  382. }
  383. /* Pools for allocnos, allocno live ranges and objects. */
  384. static alloc_pool allocno_pool, live_range_pool, object_pool;
  385. /* Vec containing references to all created allocnos. It is a
  386. container of array allocnos. */
  387. static vec<ira_allocno_t> allocno_vec;
  388. /* Vec containing references to all created ira_objects. It is a
  389. container of ira_object_id_map. */
  390. static vec<ira_object_t> ira_object_id_map_vec;
  391. /* Initialize data concerning allocnos. */
  392. static void
  393. initiate_allocnos (void)
  394. {
  395. live_range_pool
  396. = create_alloc_pool ("live ranges",
  397. sizeof (struct live_range), 100);
  398. allocno_pool
  399. = create_alloc_pool ("allocnos", sizeof (struct ira_allocno), 100);
  400. object_pool
  401. = create_alloc_pool ("objects", sizeof (struct ira_object), 100);
  402. allocno_vec.create (max_reg_num () * 2);
  403. ira_allocnos = NULL;
  404. ira_allocnos_num = 0;
  405. ira_objects_num = 0;
  406. ira_object_id_map_vec.create (max_reg_num () * 2);
  407. ira_object_id_map = NULL;
  408. ira_regno_allocno_map
  409. = (ira_allocno_t *) ira_allocate (max_reg_num ()
  410. * sizeof (ira_allocno_t));
  411. memset (ira_regno_allocno_map, 0, max_reg_num () * sizeof (ira_allocno_t));
  412. }
  413. /* Create and return an object corresponding to a new allocno A. */
  414. static ira_object_t
  415. ira_create_object (ira_allocno_t a, int subword)
  416. {
  417. enum reg_class aclass = ALLOCNO_CLASS (a);
  418. ira_object_t obj = (ira_object_t) pool_alloc (object_pool);
  419. OBJECT_ALLOCNO (obj) = a;
  420. OBJECT_SUBWORD (obj) = subword;
  421. OBJECT_CONFLICT_ID (obj) = ira_objects_num;
  422. OBJECT_CONFLICT_VEC_P (obj) = false;
  423. OBJECT_CONFLICT_ARRAY (obj) = NULL;
  424. OBJECT_NUM_CONFLICTS (obj) = 0;
  425. COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
  426. COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
  427. IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
  428. reg_class_contents[aclass]);
  429. IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
  430. reg_class_contents[aclass]);
  431. OBJECT_MIN (obj) = INT_MAX;
  432. OBJECT_MAX (obj) = -1;
  433. OBJECT_LIVE_RANGES (obj) = NULL;
  434. ira_object_id_map_vec.safe_push (obj);
  435. ira_object_id_map
  436. = ira_object_id_map_vec.address ();
  437. ira_objects_num = ira_object_id_map_vec.length ();
  438. return obj;
  439. }
  440. /* Create and return the allocno corresponding to REGNO in
  441. LOOP_TREE_NODE. Add the allocno to the list of allocnos with the
  442. same regno if CAP_P is FALSE. */
  443. ira_allocno_t
  444. ira_create_allocno (int regno, bool cap_p,
  445. ira_loop_tree_node_t loop_tree_node)
  446. {
  447. ira_allocno_t a;
  448. a = (ira_allocno_t) pool_alloc (allocno_pool);
  449. ALLOCNO_REGNO (a) = regno;
  450. ALLOCNO_LOOP_TREE_NODE (a) = loop_tree_node;
  451. if (! cap_p)
  452. {
  453. ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
  454. ira_regno_allocno_map[regno] = a;
  455. if (loop_tree_node->regno_allocno_map[regno] == NULL)
  456. /* Remember that we can create temporary allocnos to break
  457. cycles in register shuffle on region borders (see
  458. ira-emit.c). */
  459. loop_tree_node->regno_allocno_map[regno] = a;
  460. }
  461. ALLOCNO_CAP (a) = NULL;
  462. ALLOCNO_CAP_MEMBER (a) = NULL;
  463. ALLOCNO_NUM (a) = ira_allocnos_num;
  464. bitmap_set_bit (loop_tree_node->all_allocnos, ALLOCNO_NUM (a));
  465. ALLOCNO_NREFS (a) = 0;
  466. ALLOCNO_FREQ (a) = 0;
  467. ALLOCNO_HARD_REGNO (a) = -1;
  468. ALLOCNO_CALL_FREQ (a) = 0;
  469. ALLOCNO_CALLS_CROSSED_NUM (a) = 0;
  470. ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a) = 0;
  471. CLEAR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
  472. #ifdef STACK_REGS
  473. ALLOCNO_NO_STACK_REG_P (a) = false;
  474. ALLOCNO_TOTAL_NO_STACK_REG_P (a) = false;
  475. #endif
  476. ALLOCNO_DONT_REASSIGN_P (a) = false;
  477. ALLOCNO_BAD_SPILL_P (a) = false;
  478. ALLOCNO_ASSIGNED_P (a) = false;
  479. ALLOCNO_MODE (a) = (regno < 0 ? VOIDmode : PSEUDO_REGNO_MODE (regno));
  480. ALLOCNO_WMODE (a) = ALLOCNO_MODE (a);
  481. ALLOCNO_PREFS (a) = NULL;
  482. ALLOCNO_COPIES (a) = NULL;
  483. ALLOCNO_HARD_REG_COSTS (a) = NULL;
  484. ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
  485. ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
  486. ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
  487. ALLOCNO_CLASS (a) = NO_REGS;
  488. ALLOCNO_UPDATED_CLASS_COST (a) = 0;
  489. ALLOCNO_CLASS_COST (a) = 0;
  490. ALLOCNO_MEMORY_COST (a) = 0;
  491. ALLOCNO_UPDATED_MEMORY_COST (a) = 0;
  492. ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) = 0;
  493. ALLOCNO_NUM_OBJECTS (a) = 0;
  494. ALLOCNO_ADD_DATA (a) = NULL;
  495. allocno_vec.safe_push (a);
  496. ira_allocnos = allocno_vec.address ();
  497. ira_allocnos_num = allocno_vec.length ();
  498. return a;
  499. }
  500. /* Set up register class for A and update its conflict hard
  501. registers. */
  502. void
  503. ira_set_allocno_class (ira_allocno_t a, enum reg_class aclass)
  504. {
  505. ira_allocno_object_iterator oi;
  506. ira_object_t obj;
  507. ALLOCNO_CLASS (a) = aclass;
  508. FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
  509. {
  510. IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
  511. reg_class_contents[aclass]);
  512. IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
  513. reg_class_contents[aclass]);
  514. }
  515. }
  516. /* Determine the number of objects we should associate with allocno A
  517. and allocate them. */
  518. void
  519. ira_create_allocno_objects (ira_allocno_t a)
  520. {
  521. machine_mode mode = ALLOCNO_MODE (a);
  522. enum reg_class aclass = ALLOCNO_CLASS (a);
  523. int n = ira_reg_class_max_nregs[aclass][mode];
  524. int i;
  525. if (GET_MODE_SIZE (mode) != 2 * UNITS_PER_WORD || n != 2)
  526. n = 1;
  527. ALLOCNO_NUM_OBJECTS (a) = n;
  528. for (i = 0; i < n; i++)
  529. ALLOCNO_OBJECT (a, i) = ira_create_object (a, i);
  530. }
  531. /* For each allocno, set ALLOCNO_NUM_OBJECTS and create the
  532. ALLOCNO_OBJECT structures. This must be called after the allocno
  533. classes are known. */
  534. static void
  535. create_allocno_objects (void)
  536. {
  537. ira_allocno_t a;
  538. ira_allocno_iterator ai;
  539. FOR_EACH_ALLOCNO (a, ai)
  540. ira_create_allocno_objects (a);
  541. }
  542. /* Merge hard register conflict information for all objects associated with
  543. allocno TO into the corresponding objects associated with FROM.
  544. If TOTAL_ONLY is true, we only merge OBJECT_TOTAL_CONFLICT_HARD_REGS. */
  545. static void
  546. merge_hard_reg_conflicts (ira_allocno_t from, ira_allocno_t to,
  547. bool total_only)
  548. {
  549. int i;
  550. gcc_assert (ALLOCNO_NUM_OBJECTS (to) == ALLOCNO_NUM_OBJECTS (from));
  551. for (i = 0; i < ALLOCNO_NUM_OBJECTS (to); i++)
  552. {
  553. ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
  554. ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
  555. if (!total_only)
  556. IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (to_obj),
  557. OBJECT_CONFLICT_HARD_REGS (from_obj));
  558. IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (to_obj),
  559. OBJECT_TOTAL_CONFLICT_HARD_REGS (from_obj));
  560. }
  561. #ifdef STACK_REGS
  562. if (!total_only && ALLOCNO_NO_STACK_REG_P (from))
  563. ALLOCNO_NO_STACK_REG_P (to) = true;
  564. if (ALLOCNO_TOTAL_NO_STACK_REG_P (from))
  565. ALLOCNO_TOTAL_NO_STACK_REG_P (to) = true;
  566. #endif
  567. }
  568. /* Update hard register conflict information for all objects associated with
  569. A to include the regs in SET. */
  570. void
  571. ior_hard_reg_conflicts (ira_allocno_t a, HARD_REG_SET *set)
  572. {
  573. ira_allocno_object_iterator i;
  574. ira_object_t obj;
  575. FOR_EACH_ALLOCNO_OBJECT (a, obj, i)
  576. {
  577. IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), *set);
  578. IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), *set);
  579. }
  580. }
  581. /* Return TRUE if a conflict vector with NUM elements is more
  582. profitable than a conflict bit vector for OBJ. */
  583. bool
  584. ira_conflict_vector_profitable_p (ira_object_t obj, int num)
  585. {
  586. int nw;
  587. int max = OBJECT_MAX (obj);
  588. int min = OBJECT_MIN (obj);
  589. if (max < min)
  590. /* We prefer a bit vector in such case because it does not result
  591. in allocation. */
  592. return false;
  593. nw = (max - min + IRA_INT_BITS) / IRA_INT_BITS;
  594. return (2 * sizeof (ira_object_t) * (num + 1)
  595. < 3 * nw * sizeof (IRA_INT_TYPE));
  596. }
  597. /* Allocates and initialize the conflict vector of OBJ for NUM
  598. conflicting objects. */
  599. void
  600. ira_allocate_conflict_vec (ira_object_t obj, int num)
  601. {
  602. int size;
  603. ira_object_t *vec;
  604. ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
  605. num++; /* for NULL end marker */
  606. size = sizeof (ira_object_t) * num;
  607. OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
  608. vec = (ira_object_t *) OBJECT_CONFLICT_ARRAY (obj);
  609. vec[0] = NULL;
  610. OBJECT_NUM_CONFLICTS (obj) = 0;
  611. OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
  612. OBJECT_CONFLICT_VEC_P (obj) = true;
  613. }
  614. /* Allocate and initialize the conflict bit vector of OBJ. */
  615. static void
  616. allocate_conflict_bit_vec (ira_object_t obj)
  617. {
  618. unsigned int size;
  619. ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
  620. size = ((OBJECT_MAX (obj) - OBJECT_MIN (obj) + IRA_INT_BITS)
  621. / IRA_INT_BITS * sizeof (IRA_INT_TYPE));
  622. OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
  623. memset (OBJECT_CONFLICT_ARRAY (obj), 0, size);
  624. OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
  625. OBJECT_CONFLICT_VEC_P (obj) = false;
  626. }
  627. /* Allocate and initialize the conflict vector or conflict bit vector
  628. of OBJ for NUM conflicting allocnos whatever is more profitable. */
  629. void
  630. ira_allocate_object_conflicts (ira_object_t obj, int num)
  631. {
  632. if (ira_conflict_vector_profitable_p (obj, num))
  633. ira_allocate_conflict_vec (obj, num);
  634. else
  635. allocate_conflict_bit_vec (obj);
  636. }
  637. /* Add OBJ2 to the conflicts of OBJ1. */
  638. static void
  639. add_to_conflicts (ira_object_t obj1, ira_object_t obj2)
  640. {
  641. int num;
  642. unsigned int size;
  643. if (OBJECT_CONFLICT_VEC_P (obj1))
  644. {
  645. ira_object_t *vec = OBJECT_CONFLICT_VEC (obj1);
  646. int curr_num = OBJECT_NUM_CONFLICTS (obj1);
  647. num = curr_num + 2;
  648. if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < num * sizeof (ira_object_t))
  649. {
  650. ira_object_t *newvec;
  651. size = (3 * num / 2 + 1) * sizeof (ira_allocno_t);
  652. newvec = (ira_object_t *) ira_allocate (size);
  653. memcpy (newvec, vec, curr_num * sizeof (ira_object_t));
  654. ira_free (vec);
  655. vec = newvec;
  656. OBJECT_CONFLICT_ARRAY (obj1) = vec;
  657. OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
  658. }
  659. vec[num - 2] = obj2;
  660. vec[num - 1] = NULL;
  661. OBJECT_NUM_CONFLICTS (obj1)++;
  662. }
  663. else
  664. {
  665. int nw, added_head_nw, id;
  666. IRA_INT_TYPE *vec = OBJECT_CONFLICT_BITVEC (obj1);
  667. id = OBJECT_CONFLICT_ID (obj2);
  668. if (OBJECT_MIN (obj1) > id)
  669. {
  670. /* Expand head of the bit vector. */
  671. added_head_nw = (OBJECT_MIN (obj1) - id - 1) / IRA_INT_BITS + 1;
  672. nw = (OBJECT_MAX (obj1) - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
  673. size = (nw + added_head_nw) * sizeof (IRA_INT_TYPE);
  674. if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) >= size)
  675. {
  676. memmove ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
  677. vec, nw * sizeof (IRA_INT_TYPE));
  678. memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
  679. }
  680. else
  681. {
  682. size
  683. = (3 * (nw + added_head_nw) / 2 + 1) * sizeof (IRA_INT_TYPE);
  684. vec = (IRA_INT_TYPE *) ira_allocate (size);
  685. memcpy ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
  686. OBJECT_CONFLICT_ARRAY (obj1), nw * sizeof (IRA_INT_TYPE));
  687. memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
  688. memset ((char *) vec
  689. + (nw + added_head_nw) * sizeof (IRA_INT_TYPE),
  690. 0, size - (nw + added_head_nw) * sizeof (IRA_INT_TYPE));
  691. ira_free (OBJECT_CONFLICT_ARRAY (obj1));
  692. OBJECT_CONFLICT_ARRAY (obj1) = vec;
  693. OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
  694. }
  695. OBJECT_MIN (obj1) -= added_head_nw * IRA_INT_BITS;
  696. }
  697. else if (OBJECT_MAX (obj1) < id)
  698. {
  699. nw = (id - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
  700. size = nw * sizeof (IRA_INT_TYPE);
  701. if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < size)
  702. {
  703. /* Expand tail of the bit vector. */
  704. size = (3 * nw / 2 + 1) * sizeof (IRA_INT_TYPE);
  705. vec = (IRA_INT_TYPE *) ira_allocate (size);
  706. memcpy (vec, OBJECT_CONFLICT_ARRAY (obj1), OBJECT_CONFLICT_ARRAY_SIZE (obj1));
  707. memset ((char *) vec + OBJECT_CONFLICT_ARRAY_SIZE (obj1),
  708. 0, size - OBJECT_CONFLICT_ARRAY_SIZE (obj1));
  709. ira_free (OBJECT_CONFLICT_ARRAY (obj1));
  710. OBJECT_CONFLICT_ARRAY (obj1) = vec;
  711. OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
  712. }
  713. OBJECT_MAX (obj1) = id;
  714. }
  715. SET_MINMAX_SET_BIT (vec, id, OBJECT_MIN (obj1), OBJECT_MAX (obj1));
  716. }
  717. }
  718. /* Add OBJ1 to the conflicts of OBJ2 and vice versa. */
  719. static void
  720. ira_add_conflict (ira_object_t obj1, ira_object_t obj2)
  721. {
  722. add_to_conflicts (obj1, obj2);
  723. add_to_conflicts (obj2, obj1);
  724. }
  725. /* Clear all conflicts of OBJ. */
  726. static void
  727. clear_conflicts (ira_object_t obj)
  728. {
  729. if (OBJECT_CONFLICT_VEC_P (obj))
  730. {
  731. OBJECT_NUM_CONFLICTS (obj) = 0;
  732. OBJECT_CONFLICT_VEC (obj)[0] = NULL;
  733. }
  734. else if (OBJECT_CONFLICT_ARRAY_SIZE (obj) != 0)
  735. {
  736. int nw;
  737. nw = (OBJECT_MAX (obj) - OBJECT_MIN (obj)) / IRA_INT_BITS + 1;
  738. memset (OBJECT_CONFLICT_BITVEC (obj), 0, nw * sizeof (IRA_INT_TYPE));
  739. }
  740. }
  741. /* The array used to find duplications in conflict vectors of
  742. allocnos. */
  743. static int *conflict_check;
  744. /* The value used to mark allocation presence in conflict vector of
  745. the current allocno. */
  746. static int curr_conflict_check_tick;
  747. /* Remove duplications in conflict vector of OBJ. */
  748. static void
  749. compress_conflict_vec (ira_object_t obj)
  750. {
  751. ira_object_t *vec, conflict_obj;
  752. int i, j;
  753. ira_assert (OBJECT_CONFLICT_VEC_P (obj));
  754. vec = OBJECT_CONFLICT_VEC (obj);
  755. curr_conflict_check_tick++;
  756. for (i = j = 0; (conflict_obj = vec[i]) != NULL; i++)
  757. {
  758. int id = OBJECT_CONFLICT_ID (conflict_obj);
  759. if (conflict_check[id] != curr_conflict_check_tick)
  760. {
  761. conflict_check[id] = curr_conflict_check_tick;
  762. vec[j++] = conflict_obj;
  763. }
  764. }
  765. OBJECT_NUM_CONFLICTS (obj) = j;
  766. vec[j] = NULL;
  767. }
  768. /* Remove duplications in conflict vectors of all allocnos. */
  769. static void
  770. compress_conflict_vecs (void)
  771. {
  772. ira_object_t obj;
  773. ira_object_iterator oi;
  774. conflict_check = (int *) ira_allocate (sizeof (int) * ira_objects_num);
  775. memset (conflict_check, 0, sizeof (int) * ira_objects_num);
  776. curr_conflict_check_tick = 0;
  777. FOR_EACH_OBJECT (obj, oi)
  778. {
  779. if (OBJECT_CONFLICT_VEC_P (obj))
  780. compress_conflict_vec (obj);
  781. }
  782. ira_free (conflict_check);
  783. }
  784. /* This recursive function outputs allocno A and if it is a cap the
  785. function outputs its members. */
  786. void
  787. ira_print_expanded_allocno (ira_allocno_t a)
  788. {
  789. basic_block bb;
  790. fprintf (ira_dump_file, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
  791. if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
  792. fprintf (ira_dump_file, ",b%d", bb->index);
  793. else
  794. fprintf (ira_dump_file, ",l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
  795. if (ALLOCNO_CAP_MEMBER (a) != NULL)
  796. {
  797. fprintf (ira_dump_file, ":");
  798. ira_print_expanded_allocno (ALLOCNO_CAP_MEMBER (a));
  799. }
  800. fprintf (ira_dump_file, ")");
  801. }
  802. /* Create and return the cap representing allocno A in the
  803. parent loop. */
  804. static ira_allocno_t
  805. create_cap_allocno (ira_allocno_t a)
  806. {
  807. ira_allocno_t cap;
  808. ira_loop_tree_node_t parent;
  809. enum reg_class aclass;
  810. parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
  811. cap = ira_create_allocno (ALLOCNO_REGNO (a), true, parent);
  812. ALLOCNO_MODE (cap) = ALLOCNO_MODE (a);
  813. ALLOCNO_WMODE (cap) = ALLOCNO_WMODE (a);
  814. aclass = ALLOCNO_CLASS (a);
  815. ira_set_allocno_class (cap, aclass);
  816. ira_create_allocno_objects (cap);
  817. ALLOCNO_CAP_MEMBER (cap) = a;
  818. ALLOCNO_CAP (a) = cap;
  819. ALLOCNO_CLASS_COST (cap) = ALLOCNO_CLASS_COST (a);
  820. ALLOCNO_MEMORY_COST (cap) = ALLOCNO_MEMORY_COST (a);
  821. ira_allocate_and_copy_costs
  822. (&ALLOCNO_HARD_REG_COSTS (cap), aclass, ALLOCNO_HARD_REG_COSTS (a));
  823. ira_allocate_and_copy_costs
  824. (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap), aclass,
  825. ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
  826. ALLOCNO_BAD_SPILL_P (cap) = ALLOCNO_BAD_SPILL_P (a);
  827. ALLOCNO_NREFS (cap) = ALLOCNO_NREFS (a);
  828. ALLOCNO_FREQ (cap) = ALLOCNO_FREQ (a);
  829. ALLOCNO_CALL_FREQ (cap) = ALLOCNO_CALL_FREQ (a);
  830. merge_hard_reg_conflicts (a, cap, false);
  831. ALLOCNO_CALLS_CROSSED_NUM (cap) = ALLOCNO_CALLS_CROSSED_NUM (a);
  832. ALLOCNO_CHEAP_CALLS_CROSSED_NUM (cap) = ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
  833. IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (cap),
  834. ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
  835. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  836. {
  837. fprintf (ira_dump_file, " Creating cap ");
  838. ira_print_expanded_allocno (cap);
  839. fprintf (ira_dump_file, "\n");
  840. }
  841. return cap;
  842. }
  843. /* Create and return a live range for OBJECT with given attributes. */
  844. live_range_t
  845. ira_create_live_range (ira_object_t obj, int start, int finish,
  846. live_range_t next)
  847. {
  848. live_range_t p;
  849. p = (live_range_t) pool_alloc (live_range_pool);
  850. p->object = obj;
  851. p->start = start;
  852. p->finish = finish;
  853. p->next = next;
  854. return p;
  855. }
  856. /* Create a new live range for OBJECT and queue it at the head of its
  857. live range list. */
  858. void
  859. ira_add_live_range_to_object (ira_object_t object, int start, int finish)
  860. {
  861. live_range_t p;
  862. p = ira_create_live_range (object, start, finish,
  863. OBJECT_LIVE_RANGES (object));
  864. OBJECT_LIVE_RANGES (object) = p;
  865. }
  866. /* Copy allocno live range R and return the result. */
  867. static live_range_t
  868. copy_live_range (live_range_t r)
  869. {
  870. live_range_t p;
  871. p = (live_range_t) pool_alloc (live_range_pool);
  872. *p = *r;
  873. return p;
  874. }
  875. /* Copy allocno live range list given by its head R and return the
  876. result. */
  877. live_range_t
  878. ira_copy_live_range_list (live_range_t r)
  879. {
  880. live_range_t p, first, last;
  881. if (r == NULL)
  882. return NULL;
  883. for (first = last = NULL; r != NULL; r = r->next)
  884. {
  885. p = copy_live_range (r);
  886. if (first == NULL)
  887. first = p;
  888. else
  889. last->next = p;
  890. last = p;
  891. }
  892. return first;
  893. }
  894. /* Merge ranges R1 and R2 and returns the result. The function
  895. maintains the order of ranges and tries to minimize number of the
  896. result ranges. */
  897. live_range_t
  898. ira_merge_live_ranges (live_range_t r1, live_range_t r2)
  899. {
  900. live_range_t first, last, temp;
  901. if (r1 == NULL)
  902. return r2;
  903. if (r2 == NULL)
  904. return r1;
  905. for (first = last = NULL; r1 != NULL && r2 != NULL;)
  906. {
  907. if (r1->start < r2->start)
  908. {
  909. temp = r1;
  910. r1 = r2;
  911. r2 = temp;
  912. }
  913. if (r1->start <= r2->finish + 1)
  914. {
  915. /* Intersected ranges: merge r1 and r2 into r1. */
  916. r1->start = r2->start;
  917. if (r1->finish < r2->finish)
  918. r1->finish = r2->finish;
  919. temp = r2;
  920. r2 = r2->next;
  921. ira_finish_live_range (temp);
  922. if (r2 == NULL)
  923. {
  924. /* To try to merge with subsequent ranges in r1. */
  925. r2 = r1->next;
  926. r1->next = NULL;
  927. }
  928. }
  929. else
  930. {
  931. /* Add r1 to the result. */
  932. if (first == NULL)
  933. first = last = r1;
  934. else
  935. {
  936. last->next = r1;
  937. last = r1;
  938. }
  939. r1 = r1->next;
  940. if (r1 == NULL)
  941. {
  942. /* To try to merge with subsequent ranges in r2. */
  943. r1 = r2->next;
  944. r2->next = NULL;
  945. }
  946. }
  947. }
  948. if (r1 != NULL)
  949. {
  950. if (first == NULL)
  951. first = r1;
  952. else
  953. last->next = r1;
  954. ira_assert (r1->next == NULL);
  955. }
  956. else if (r2 != NULL)
  957. {
  958. if (first == NULL)
  959. first = r2;
  960. else
  961. last->next = r2;
  962. ira_assert (r2->next == NULL);
  963. }
  964. else
  965. {
  966. ira_assert (last->next == NULL);
  967. }
  968. return first;
  969. }
  970. /* Return TRUE if live ranges R1 and R2 intersect. */
  971. bool
  972. ira_live_ranges_intersect_p (live_range_t r1, live_range_t r2)
  973. {
  974. /* Remember the live ranges are always kept ordered. */
  975. while (r1 != NULL && r2 != NULL)
  976. {
  977. if (r1->start > r2->finish)
  978. r1 = r1->next;
  979. else if (r2->start > r1->finish)
  980. r2 = r2->next;
  981. else
  982. return true;
  983. }
  984. return false;
  985. }
  986. /* Free allocno live range R. */
  987. void
  988. ira_finish_live_range (live_range_t r)
  989. {
  990. pool_free (live_range_pool, r);
  991. }
  992. /* Free list of allocno live ranges starting with R. */
  993. void
  994. ira_finish_live_range_list (live_range_t r)
  995. {
  996. live_range_t next_r;
  997. for (; r != NULL; r = next_r)
  998. {
  999. next_r = r->next;
  1000. ira_finish_live_range (r);
  1001. }
  1002. }
  1003. /* Free updated register costs of allocno A. */
  1004. void
  1005. ira_free_allocno_updated_costs (ira_allocno_t a)
  1006. {
  1007. enum reg_class aclass;
  1008. aclass = ALLOCNO_CLASS (a);
  1009. if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
  1010. ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
  1011. ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
  1012. if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
  1013. ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
  1014. aclass);
  1015. ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
  1016. }
  1017. /* Free and nullify all cost vectors allocated earlier for allocno
  1018. A. */
  1019. static void
  1020. ira_free_allocno_costs (ira_allocno_t a)
  1021. {
  1022. enum reg_class aclass = ALLOCNO_CLASS (a);
  1023. ira_object_t obj;
  1024. ira_allocno_object_iterator oi;
  1025. FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
  1026. {
  1027. ira_finish_live_range_list (OBJECT_LIVE_RANGES (obj));
  1028. ira_object_id_map[OBJECT_CONFLICT_ID (obj)] = NULL;
  1029. if (OBJECT_CONFLICT_ARRAY (obj) != NULL)
  1030. ira_free (OBJECT_CONFLICT_ARRAY (obj));
  1031. pool_free (object_pool, obj);
  1032. }
  1033. ira_allocnos[ALLOCNO_NUM (a)] = NULL;
  1034. if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
  1035. ira_free_cost_vector (ALLOCNO_HARD_REG_COSTS (a), aclass);
  1036. if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL)
  1037. ira_free_cost_vector (ALLOCNO_CONFLICT_HARD_REG_COSTS (a), aclass);
  1038. if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
  1039. ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
  1040. if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
  1041. ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
  1042. aclass);
  1043. ALLOCNO_HARD_REG_COSTS (a) = NULL;
  1044. ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
  1045. ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
  1046. ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
  1047. }
  1048. /* Free the memory allocated for allocno A. */
  1049. static void
  1050. finish_allocno (ira_allocno_t a)
  1051. {
  1052. ira_free_allocno_costs (a);
  1053. pool_free (allocno_pool, a);
  1054. }
  1055. /* Free the memory allocated for all allocnos. */
  1056. static void
  1057. finish_allocnos (void)
  1058. {
  1059. ira_allocno_t a;
  1060. ira_allocno_iterator ai;
  1061. FOR_EACH_ALLOCNO (a, ai)
  1062. finish_allocno (a);
  1063. ira_free (ira_regno_allocno_map);
  1064. ira_object_id_map_vec.release ();
  1065. allocno_vec.release ();
  1066. free_alloc_pool (allocno_pool);
  1067. free_alloc_pool (object_pool);
  1068. free_alloc_pool (live_range_pool);
  1069. }
  1070. /* Pools for allocno preferences. */
  1071. static alloc_pool pref_pool;
  1072. /* Vec containing references to all created preferences. It is a
  1073. container of array ira_prefs. */
  1074. static vec<ira_pref_t> pref_vec;
  1075. /* The function initializes data concerning allocno prefs. */
  1076. static void
  1077. initiate_prefs (void)
  1078. {
  1079. pref_pool
  1080. = create_alloc_pool ("prefs", sizeof (struct ira_allocno_pref), 100);
  1081. pref_vec.create (get_max_uid ());
  1082. ira_prefs = NULL;
  1083. ira_prefs_num = 0;
  1084. }
  1085. /* Return pref for A and HARD_REGNO if any. */
  1086. static ira_pref_t
  1087. find_allocno_pref (ira_allocno_t a, int hard_regno)
  1088. {
  1089. ira_pref_t pref;
  1090. for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
  1091. if (pref->allocno == a && pref->hard_regno == hard_regno)
  1092. return pref;
  1093. return NULL;
  1094. }
  1095. /* Create and return pref with given attributes A, HARD_REGNO, and FREQ. */
  1096. ira_pref_t
  1097. ira_create_pref (ira_allocno_t a, int hard_regno, int freq)
  1098. {
  1099. ira_pref_t pref;
  1100. pref = (ira_pref_t) pool_alloc (pref_pool);
  1101. pref->num = ira_prefs_num;
  1102. pref->allocno = a;
  1103. pref->hard_regno = hard_regno;
  1104. pref->freq = freq;
  1105. pref_vec.safe_push (pref);
  1106. ira_prefs = pref_vec.address ();
  1107. ira_prefs_num = pref_vec.length ();
  1108. return pref;
  1109. }
  1110. /* Attach a pref PREF to the corresponding allocno. */
  1111. static void
  1112. add_allocno_pref_to_list (ira_pref_t pref)
  1113. {
  1114. ira_allocno_t a = pref->allocno;
  1115. pref->next_pref = ALLOCNO_PREFS (a);
  1116. ALLOCNO_PREFS (a) = pref;
  1117. }
  1118. /* Create (or update frequency if the pref already exists) the pref of
  1119. allocnos A preferring HARD_REGNO with frequency FREQ. */
  1120. void
  1121. ira_add_allocno_pref (ira_allocno_t a, int hard_regno, int freq)
  1122. {
  1123. ira_pref_t pref;
  1124. if (freq <= 0)
  1125. return;
  1126. if ((pref = find_allocno_pref (a, hard_regno)) != NULL)
  1127. {
  1128. pref->freq += freq;
  1129. return;
  1130. }
  1131. pref = ira_create_pref (a, hard_regno, freq);
  1132. ira_assert (a != NULL);
  1133. add_allocno_pref_to_list (pref);
  1134. }
  1135. /* Print info about PREF into file F. */
  1136. static void
  1137. print_pref (FILE *f, ira_pref_t pref)
  1138. {
  1139. fprintf (f, " pref%d:a%d(r%d)<-hr%d@%d\n", pref->num,
  1140. ALLOCNO_NUM (pref->allocno), ALLOCNO_REGNO (pref->allocno),
  1141. pref->hard_regno, pref->freq);
  1142. }
  1143. /* Print info about PREF into stderr. */
  1144. void
  1145. ira_debug_pref (ira_pref_t pref)
  1146. {
  1147. print_pref (stderr, pref);
  1148. }
  1149. /* Print info about all prefs into file F. */
  1150. static void
  1151. print_prefs (FILE *f)
  1152. {
  1153. ira_pref_t pref;
  1154. ira_pref_iterator pi;
  1155. FOR_EACH_PREF (pref, pi)
  1156. print_pref (f, pref);
  1157. }
  1158. /* Print info about all prefs into stderr. */
  1159. void
  1160. ira_debug_prefs (void)
  1161. {
  1162. print_prefs (stderr);
  1163. }
  1164. /* Print info about prefs involving allocno A into file F. */
  1165. static void
  1166. print_allocno_prefs (FILE *f, ira_allocno_t a)
  1167. {
  1168. ira_pref_t pref;
  1169. fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
  1170. for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
  1171. fprintf (f, " pref%d:hr%d@%d", pref->num, pref->hard_regno, pref->freq);
  1172. fprintf (f, "\n");
  1173. }
  1174. /* Print info about prefs involving allocno A into stderr. */
  1175. void
  1176. ira_debug_allocno_prefs (ira_allocno_t a)
  1177. {
  1178. print_allocno_prefs (stderr, a);
  1179. }
  1180. /* The function frees memory allocated for PREF. */
  1181. static void
  1182. finish_pref (ira_pref_t pref)
  1183. {
  1184. ira_prefs[pref->num] = NULL;
  1185. pool_free (pref_pool, pref);
  1186. }
  1187. /* Remove PREF from the list of allocno prefs and free memory for
  1188. it. */
  1189. void
  1190. ira_remove_pref (ira_pref_t pref)
  1191. {
  1192. ira_pref_t cpref, prev;
  1193. if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
  1194. fprintf (ira_dump_file, " Removing pref%d:hr%d@%d\n",
  1195. pref->num, pref->hard_regno, pref->freq);
  1196. for (prev = NULL, cpref = ALLOCNO_PREFS (pref->allocno);
  1197. cpref != NULL;
  1198. prev = cpref, cpref = cpref->next_pref)
  1199. if (cpref == pref)
  1200. break;
  1201. ira_assert (cpref != NULL);
  1202. if (prev == NULL)
  1203. ALLOCNO_PREFS (pref->allocno) = pref->next_pref;
  1204. else
  1205. prev->next_pref = pref->next_pref;
  1206. finish_pref (pref);
  1207. }
  1208. /* Remove all prefs of allocno A. */
  1209. void
  1210. ira_remove_allocno_prefs (ira_allocno_t a)
  1211. {
  1212. ira_pref_t pref, next_pref;
  1213. for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
  1214. {
  1215. next_pref = pref->next_pref;
  1216. finish_pref (pref);
  1217. }
  1218. ALLOCNO_PREFS (a) = NULL;
  1219. }
  1220. /* Free memory allocated for all prefs. */
  1221. static void
  1222. finish_prefs (void)
  1223. {
  1224. ira_pref_t pref;
  1225. ira_pref_iterator pi;
  1226. FOR_EACH_PREF (pref, pi)
  1227. finish_pref (pref);
  1228. pref_vec.release ();
  1229. free_alloc_pool (pref_pool);
  1230. }
  1231. /* Pools for copies. */
  1232. static alloc_pool copy_pool;
  1233. /* Vec containing references to all created copies. It is a
  1234. container of array ira_copies. */
  1235. static vec<ira_copy_t> copy_vec;
  1236. /* The function initializes data concerning allocno copies. */
  1237. static void
  1238. initiate_copies (void)
  1239. {
  1240. copy_pool
  1241. = create_alloc_pool ("copies", sizeof (struct ira_allocno_copy), 100);
  1242. copy_vec.create (get_max_uid ());
  1243. ira_copies = NULL;
  1244. ira_copies_num = 0;
  1245. }
  1246. /* Return copy connecting A1 and A2 and originated from INSN of
  1247. LOOP_TREE_NODE if any. */
  1248. static ira_copy_t
  1249. find_allocno_copy (ira_allocno_t a1, ira_allocno_t a2, rtx_insn *insn,
  1250. ira_loop_tree_node_t loop_tree_node)
  1251. {
  1252. ira_copy_t cp, next_cp;
  1253. ira_allocno_t another_a;
  1254. for (cp = ALLOCNO_COPIES (a1); cp != NULL; cp = next_cp)
  1255. {
  1256. if (cp->first == a1)
  1257. {
  1258. next_cp = cp->next_first_allocno_copy;
  1259. another_a = cp->second;
  1260. }
  1261. else if (cp->second == a1)
  1262. {
  1263. next_cp = cp->next_second_allocno_copy;
  1264. another_a = cp->first;
  1265. }
  1266. else
  1267. gcc_unreachable ();
  1268. if (another_a == a2 && cp->insn == insn
  1269. && cp->loop_tree_node == loop_tree_node)
  1270. return cp;
  1271. }
  1272. return NULL;
  1273. }
  1274. /* Create and return copy with given attributes LOOP_TREE_NODE, FIRST,
  1275. SECOND, FREQ, CONSTRAINT_P, and INSN. */
  1276. ira_copy_t
  1277. ira_create_copy (ira_allocno_t first, ira_allocno_t second, int freq,
  1278. bool constraint_p, rtx_insn *insn,
  1279. ira_loop_tree_node_t loop_tree_node)
  1280. {
  1281. ira_copy_t cp;
  1282. cp = (ira_copy_t) pool_alloc (copy_pool);
  1283. cp->num = ira_copies_num;
  1284. cp->first = first;
  1285. cp->second = second;
  1286. cp->freq = freq;
  1287. cp->constraint_p = constraint_p;
  1288. cp->insn = insn;
  1289. cp->loop_tree_node = loop_tree_node;
  1290. copy_vec.safe_push (cp);
  1291. ira_copies = copy_vec.address ();
  1292. ira_copies_num = copy_vec.length ();
  1293. return cp;
  1294. }
  1295. /* Attach a copy CP to allocnos involved into the copy. */
  1296. static void
  1297. add_allocno_copy_to_list (ira_copy_t cp)
  1298. {
  1299. ira_allocno_t first = cp->first, second = cp->second;
  1300. cp->prev_first_allocno_copy = NULL;
  1301. cp->prev_second_allocno_copy = NULL;
  1302. cp->next_first_allocno_copy = ALLOCNO_COPIES (first);
  1303. if (cp->next_first_allocno_copy != NULL)
  1304. {
  1305. if (cp->next_first_allocno_copy->first == first)
  1306. cp->next_first_allocno_copy->prev_first_allocno_copy = cp;
  1307. else
  1308. cp->next_first_allocno_copy->prev_second_allocno_copy = cp;
  1309. }
  1310. cp->next_second_allocno_copy = ALLOCNO_COPIES (second);
  1311. if (cp->next_second_allocno_copy != NULL)
  1312. {
  1313. if (cp->next_second_allocno_copy->second == second)
  1314. cp->next_second_allocno_copy->prev_second_allocno_copy = cp;
  1315. else
  1316. cp->next_second_allocno_copy->prev_first_allocno_copy = cp;
  1317. }
  1318. ALLOCNO_COPIES (first) = cp;
  1319. ALLOCNO_COPIES (second) = cp;
  1320. }
  1321. /* Make a copy CP a canonical copy where number of the
  1322. first allocno is less than the second one. */
  1323. static void
  1324. swap_allocno_copy_ends_if_necessary (ira_copy_t cp)
  1325. {
  1326. ira_allocno_t temp;
  1327. ira_copy_t temp_cp;
  1328. if (ALLOCNO_NUM (cp->first) <= ALLOCNO_NUM (cp->second))
  1329. return;
  1330. temp = cp->first;
  1331. cp->first = cp->second;
  1332. cp->second = temp;
  1333. temp_cp = cp->prev_first_allocno_copy;
  1334. cp->prev_first_allocno_copy = cp->prev_second_allocno_copy;
  1335. cp->prev_second_allocno_copy = temp_cp;
  1336. temp_cp = cp->next_first_allocno_copy;
  1337. cp->next_first_allocno_copy = cp->next_second_allocno_copy;
  1338. cp->next_second_allocno_copy = temp_cp;
  1339. }
  1340. /* Create (or update frequency if the copy already exists) and return
  1341. the copy of allocnos FIRST and SECOND with frequency FREQ
  1342. corresponding to move insn INSN (if any) and originated from
  1343. LOOP_TREE_NODE. */
  1344. ira_copy_t
  1345. ira_add_allocno_copy (ira_allocno_t first, ira_allocno_t second, int freq,
  1346. bool constraint_p, rtx_insn *insn,
  1347. ira_loop_tree_node_t loop_tree_node)
  1348. {
  1349. ira_copy_t cp;
  1350. if ((cp = find_allocno_copy (first, second, insn, loop_tree_node)) != NULL)
  1351. {
  1352. cp->freq += freq;
  1353. return cp;
  1354. }
  1355. cp = ira_create_copy (first, second, freq, constraint_p, insn,
  1356. loop_tree_node);
  1357. ira_assert (first != NULL && second != NULL);
  1358. add_allocno_copy_to_list (cp);
  1359. swap_allocno_copy_ends_if_necessary (cp);
  1360. return cp;
  1361. }
  1362. /* Print info about copy CP into file F. */
  1363. static void
  1364. print_copy (FILE *f, ira_copy_t cp)
  1365. {
  1366. fprintf (f, " cp%d:a%d(r%d)<->a%d(r%d)@%d:%s\n", cp->num,
  1367. ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
  1368. ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second), cp->freq,
  1369. cp->insn != NULL
  1370. ? "move" : cp->constraint_p ? "constraint" : "shuffle");
  1371. }
  1372. DEBUG_FUNCTION void
  1373. debug (ira_allocno_copy &ref)
  1374. {
  1375. print_copy (stderr, &ref);
  1376. }
  1377. DEBUG_FUNCTION void
  1378. debug (ira_allocno_copy *ptr)
  1379. {
  1380. if (ptr)
  1381. debug (*ptr);
  1382. else
  1383. fprintf (stderr, "<nil>\n");
  1384. }
  1385. /* Print info about copy CP into stderr. */
  1386. void
  1387. ira_debug_copy (ira_copy_t cp)
  1388. {
  1389. print_copy (stderr, cp);
  1390. }
  1391. /* Print info about all copies into file F. */
  1392. static void
  1393. print_copies (FILE *f)
  1394. {
  1395. ira_copy_t cp;
  1396. ira_copy_iterator ci;
  1397. FOR_EACH_COPY (cp, ci)
  1398. print_copy (f, cp);
  1399. }
  1400. /* Print info about all copies into stderr. */
  1401. void
  1402. ira_debug_copies (void)
  1403. {
  1404. print_copies (stderr);
  1405. }
  1406. /* Print info about copies involving allocno A into file F. */
  1407. static void
  1408. print_allocno_copies (FILE *f, ira_allocno_t a)
  1409. {
  1410. ira_allocno_t another_a;
  1411. ira_copy_t cp, next_cp;
  1412. fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
  1413. for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
  1414. {
  1415. if (cp->first == a)
  1416. {
  1417. next_cp = cp->next_first_allocno_copy;
  1418. another_a = cp->second;
  1419. }
  1420. else if (cp->second == a)
  1421. {
  1422. next_cp = cp->next_second_allocno_copy;
  1423. another_a = cp->first;
  1424. }
  1425. else
  1426. gcc_unreachable ();
  1427. fprintf (f, " cp%d:a%d(r%d)@%d", cp->num,
  1428. ALLOCNO_NUM (another_a), ALLOCNO_REGNO (another_a), cp->freq);
  1429. }
  1430. fprintf (f, "\n");
  1431. }
  1432. DEBUG_FUNCTION void
  1433. debug (ira_allocno &ref)
  1434. {
  1435. print_allocno_copies (stderr, &ref);
  1436. }
  1437. DEBUG_FUNCTION void
  1438. debug (ira_allocno *ptr)
  1439. {
  1440. if (ptr)
  1441. debug (*ptr);
  1442. else
  1443. fprintf (stderr, "<nil>\n");
  1444. }
  1445. /* Print info about copies involving allocno A into stderr. */
  1446. void
  1447. ira_debug_allocno_copies (ira_allocno_t a)
  1448. {
  1449. print_allocno_copies (stderr, a);
  1450. }
  1451. /* The function frees memory allocated for copy CP. */
  1452. static void
  1453. finish_copy (ira_copy_t cp)
  1454. {
  1455. pool_free (copy_pool, cp);
  1456. }
  1457. /* Free memory allocated for all copies. */
  1458. static void
  1459. finish_copies (void)
  1460. {
  1461. ira_copy_t cp;
  1462. ira_copy_iterator ci;
  1463. FOR_EACH_COPY (cp, ci)
  1464. finish_copy (cp);
  1465. copy_vec.release ();
  1466. free_alloc_pool (copy_pool);
  1467. }
  1468. /* Pools for cost vectors. It is defined only for allocno classes. */
  1469. static alloc_pool cost_vector_pool[N_REG_CLASSES];
  1470. /* The function initiates work with hard register cost vectors. It
  1471. creates allocation pool for each allocno class. */
  1472. static void
  1473. initiate_cost_vectors (void)
  1474. {
  1475. int i;
  1476. enum reg_class aclass;
  1477. for (i = 0; i < ira_allocno_classes_num; i++)
  1478. {
  1479. aclass = ira_allocno_classes[i];
  1480. cost_vector_pool[aclass]
  1481. = create_alloc_pool ("cost vectors",
  1482. sizeof (int) * ira_class_hard_regs_num[aclass],
  1483. 100);
  1484. }
  1485. }
  1486. /* Allocate and return a cost vector VEC for ACLASS. */
  1487. int *
  1488. ira_allocate_cost_vector (reg_class_t aclass)
  1489. {
  1490. return (int *) pool_alloc (cost_vector_pool[(int) aclass]);
  1491. }
  1492. /* Free a cost vector VEC for ACLASS. */
  1493. void
  1494. ira_free_cost_vector (int *vec, reg_class_t aclass)
  1495. {
  1496. ira_assert (vec != NULL);
  1497. pool_free (cost_vector_pool[(int) aclass], vec);
  1498. }
  1499. /* Finish work with hard register cost vectors. Release allocation
  1500. pool for each allocno class. */
  1501. static void
  1502. finish_cost_vectors (void)
  1503. {
  1504. int i;
  1505. enum reg_class aclass;
  1506. for (i = 0; i < ira_allocno_classes_num; i++)
  1507. {
  1508. aclass = ira_allocno_classes[i];
  1509. free_alloc_pool (cost_vector_pool[aclass]);
  1510. }
  1511. }
  1512. /* Compute a post-ordering of the reverse control flow of the loop body
  1513. designated by the children nodes of LOOP_NODE, whose body nodes in
  1514. pre-order are input as LOOP_PREORDER. Return a VEC with a post-order
  1515. of the reverse loop body.
  1516. For the post-order of the reverse CFG, we visit the basic blocks in
  1517. LOOP_PREORDER array in the reverse order of where they appear.
  1518. This is important: We do not just want to compute a post-order of
  1519. the reverse CFG, we want to make a best-guess for a visiting order that
  1520. minimizes the number of chain elements per allocno live range. If the
  1521. blocks would be visited in a different order, we would still compute a
  1522. correct post-ordering but it would be less likely that two nodes
  1523. connected by an edge in the CFG are neighbours in the topsort. */
  1524. static vec<ira_loop_tree_node_t>
  1525. ira_loop_tree_body_rev_postorder (ira_loop_tree_node_t loop_node ATTRIBUTE_UNUSED,
  1526. vec<ira_loop_tree_node_t> loop_preorder)
  1527. {
  1528. vec<ira_loop_tree_node_t> topsort_nodes = vNULL;
  1529. unsigned int n_loop_preorder;
  1530. n_loop_preorder = loop_preorder.length ();
  1531. if (n_loop_preorder != 0)
  1532. {
  1533. ira_loop_tree_node_t subloop_node;
  1534. unsigned int i;
  1535. auto_vec<ira_loop_tree_node_t> dfs_stack;
  1536. /* This is a bit of strange abuse of the BB_VISITED flag: We use
  1537. the flag to mark blocks we still have to visit to add them to
  1538. our post-order. Define an alias to avoid confusion. */
  1539. #define BB_TO_VISIT BB_VISITED
  1540. FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
  1541. {
  1542. gcc_checking_assert (! (subloop_node->bb->flags & BB_TO_VISIT));
  1543. subloop_node->bb->flags |= BB_TO_VISIT;
  1544. }
  1545. topsort_nodes.create (n_loop_preorder);
  1546. dfs_stack.create (n_loop_preorder);
  1547. FOR_EACH_VEC_ELT_REVERSE (loop_preorder, i, subloop_node)
  1548. {
  1549. if (! (subloop_node->bb->flags & BB_TO_VISIT))
  1550. continue;
  1551. subloop_node->bb->flags &= ~BB_TO_VISIT;
  1552. dfs_stack.quick_push (subloop_node);
  1553. while (! dfs_stack.is_empty ())
  1554. {
  1555. edge e;
  1556. edge_iterator ei;
  1557. ira_loop_tree_node_t n = dfs_stack.last ();
  1558. FOR_EACH_EDGE (e, ei, n->bb->preds)
  1559. {
  1560. ira_loop_tree_node_t pred_node;
  1561. basic_block pred_bb = e->src;
  1562. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1563. continue;
  1564. pred_node = IRA_BB_NODE_BY_INDEX (pred_bb->index);
  1565. if (pred_node != n
  1566. && (pred_node->bb->flags & BB_TO_VISIT))
  1567. {
  1568. pred_node->bb->flags &= ~BB_TO_VISIT;
  1569. dfs_stack.quick_push (pred_node);
  1570. }
  1571. }
  1572. if (n == dfs_stack.last ())
  1573. {
  1574. dfs_stack.pop ();
  1575. topsort_nodes.quick_push (n);
  1576. }
  1577. }
  1578. }
  1579. #undef BB_TO_VISIT
  1580. }
  1581. gcc_assert (topsort_nodes.length () == n_loop_preorder);
  1582. return topsort_nodes;
  1583. }
  1584. /* The current loop tree node and its regno allocno map. */
  1585. ira_loop_tree_node_t ira_curr_loop_tree_node;
  1586. ira_allocno_t *ira_curr_regno_allocno_map;
  1587. /* This recursive function traverses loop tree with root LOOP_NODE
  1588. calling non-null functions PREORDER_FUNC and POSTORDER_FUNC
  1589. correspondingly in preorder and postorder. The function sets up
  1590. IRA_CURR_LOOP_TREE_NODE and IRA_CURR_REGNO_ALLOCNO_MAP. If BB_P,
  1591. basic block nodes of LOOP_NODE is also processed (before its
  1592. subloop nodes).
  1593. If BB_P is set and POSTORDER_FUNC is given, the basic blocks in
  1594. the loop are passed in the *reverse* post-order of the *reverse*
  1595. CFG. This is only used by ira_create_allocno_live_ranges, which
  1596. wants to visit basic blocks in this order to minimize the number
  1597. of elements per live range chain.
  1598. Note that the loop tree nodes are still visited in the normal,
  1599. forward post-order of the loop tree. */
  1600. void
  1601. ira_traverse_loop_tree (bool bb_p, ira_loop_tree_node_t loop_node,
  1602. void (*preorder_func) (ira_loop_tree_node_t),
  1603. void (*postorder_func) (ira_loop_tree_node_t))
  1604. {
  1605. ira_loop_tree_node_t subloop_node;
  1606. ira_assert (loop_node->bb == NULL);
  1607. ira_curr_loop_tree_node = loop_node;
  1608. ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
  1609. if (preorder_func != NULL)
  1610. (*preorder_func) (loop_node);
  1611. if (bb_p)
  1612. {
  1613. auto_vec<ira_loop_tree_node_t> loop_preorder;
  1614. unsigned int i;
  1615. /* Add all nodes to the set of nodes to visit. The IRA loop tree
  1616. is set up such that nodes in the loop body appear in a pre-order
  1617. of their place in the CFG. */
  1618. for (subloop_node = loop_node->children;
  1619. subloop_node != NULL;
  1620. subloop_node = subloop_node->next)
  1621. if (subloop_node->bb != NULL)
  1622. loop_preorder.safe_push (subloop_node);
  1623. if (preorder_func != NULL)
  1624. FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
  1625. (*preorder_func) (subloop_node);
  1626. if (postorder_func != NULL)
  1627. {
  1628. vec<ira_loop_tree_node_t> loop_rev_postorder =
  1629. ira_loop_tree_body_rev_postorder (loop_node, loop_preorder);
  1630. FOR_EACH_VEC_ELT_REVERSE (loop_rev_postorder, i, subloop_node)
  1631. (*postorder_func) (subloop_node);
  1632. loop_rev_postorder.release ();
  1633. }
  1634. }
  1635. for (subloop_node = loop_node->subloops;
  1636. subloop_node != NULL;
  1637. subloop_node = subloop_node->subloop_next)
  1638. {
  1639. ira_assert (subloop_node->bb == NULL);
  1640. ira_traverse_loop_tree (bb_p, subloop_node,
  1641. preorder_func, postorder_func);
  1642. }
  1643. ira_curr_loop_tree_node = loop_node;
  1644. ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
  1645. if (postorder_func != NULL)
  1646. (*postorder_func) (loop_node);
  1647. }
  1648. /* The basic block currently being processed. */
  1649. static basic_block curr_bb;
  1650. /* This recursive function creates allocnos corresponding to
  1651. pseudo-registers containing in X. True OUTPUT_P means that X is
  1652. an lvalue. PARENT corresponds to the parent expression of X. */
  1653. static void
  1654. create_insn_allocnos (rtx x, rtx outer, bool output_p)
  1655. {
  1656. int i, j;
  1657. const char *fmt;
  1658. enum rtx_code code = GET_CODE (x);
  1659. if (code == REG)
  1660. {
  1661. int regno;
  1662. if ((regno = REGNO (x)) >= FIRST_PSEUDO_REGISTER)
  1663. {
  1664. ira_allocno_t a;
  1665. if ((a = ira_curr_regno_allocno_map[regno]) == NULL)
  1666. {
  1667. a = ira_create_allocno (regno, false, ira_curr_loop_tree_node);
  1668. if (outer != NULL && GET_CODE (outer) == SUBREG)
  1669. {
  1670. machine_mode wmode = GET_MODE (outer);
  1671. if (GET_MODE_SIZE (wmode) > GET_MODE_SIZE (ALLOCNO_WMODE (a)))
  1672. ALLOCNO_WMODE (a) = wmode;
  1673. }
  1674. }
  1675. ALLOCNO_NREFS (a)++;
  1676. ALLOCNO_FREQ (a) += REG_FREQ_FROM_BB (curr_bb);
  1677. if (output_p)
  1678. bitmap_set_bit (ira_curr_loop_tree_node->modified_regnos, regno);
  1679. }
  1680. return;
  1681. }
  1682. else if (code == SET)
  1683. {
  1684. create_insn_allocnos (SET_DEST (x), NULL, true);
  1685. create_insn_allocnos (SET_SRC (x), NULL, false);
  1686. return;
  1687. }
  1688. else if (code == CLOBBER)
  1689. {
  1690. create_insn_allocnos (XEXP (x, 0), NULL, true);
  1691. return;
  1692. }
  1693. else if (code == MEM)
  1694. {
  1695. create_insn_allocnos (XEXP (x, 0), NULL, false);
  1696. return;
  1697. }
  1698. else if (code == PRE_DEC || code == POST_DEC || code == PRE_INC ||
  1699. code == POST_INC || code == POST_MODIFY || code == PRE_MODIFY)
  1700. {
  1701. create_insn_allocnos (XEXP (x, 0), NULL, true);
  1702. create_insn_allocnos (XEXP (x, 0), NULL, false);
  1703. return;
  1704. }
  1705. fmt = GET_RTX_FORMAT (code);
  1706. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1707. {
  1708. if (fmt[i] == 'e')
  1709. create_insn_allocnos (XEXP (x, i), x, output_p);
  1710. else if (fmt[i] == 'E')
  1711. for (j = 0; j < XVECLEN (x, i); j++)
  1712. create_insn_allocnos (XVECEXP (x, i, j), x, output_p);
  1713. }
  1714. }
  1715. /* Create allocnos corresponding to pseudo-registers living in the
  1716. basic block represented by the corresponding loop tree node
  1717. BB_NODE. */
  1718. static void
  1719. create_bb_allocnos (ira_loop_tree_node_t bb_node)
  1720. {
  1721. basic_block bb;
  1722. rtx_insn *insn;
  1723. unsigned int i;
  1724. bitmap_iterator bi;
  1725. curr_bb = bb = bb_node->bb;
  1726. ira_assert (bb != NULL);
  1727. FOR_BB_INSNS_REVERSE (bb, insn)
  1728. if (NONDEBUG_INSN_P (insn))
  1729. create_insn_allocnos (PATTERN (insn), NULL, false);
  1730. /* It might be a allocno living through from one subloop to
  1731. another. */
  1732. EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, i, bi)
  1733. if (ira_curr_regno_allocno_map[i] == NULL)
  1734. ira_create_allocno (i, false, ira_curr_loop_tree_node);
  1735. }
  1736. /* Create allocnos corresponding to pseudo-registers living on edge E
  1737. (a loop entry or exit). Also mark the allocnos as living on the
  1738. loop border. */
  1739. static void
  1740. create_loop_allocnos (edge e)
  1741. {
  1742. unsigned int i;
  1743. bitmap live_in_regs, border_allocnos;
  1744. bitmap_iterator bi;
  1745. ira_loop_tree_node_t parent;
  1746. live_in_regs = df_get_live_in (e->dest);
  1747. border_allocnos = ira_curr_loop_tree_node->border_allocnos;
  1748. EXECUTE_IF_SET_IN_REG_SET (df_get_live_out (e->src),
  1749. FIRST_PSEUDO_REGISTER, i, bi)
  1750. if (bitmap_bit_p (live_in_regs, i))
  1751. {
  1752. if (ira_curr_regno_allocno_map[i] == NULL)
  1753. {
  1754. /* The order of creations is important for right
  1755. ira_regno_allocno_map. */
  1756. if ((parent = ira_curr_loop_tree_node->parent) != NULL
  1757. && parent->regno_allocno_map[i] == NULL)
  1758. ira_create_allocno (i, false, parent);
  1759. ira_create_allocno (i, false, ira_curr_loop_tree_node);
  1760. }
  1761. bitmap_set_bit (border_allocnos,
  1762. ALLOCNO_NUM (ira_curr_regno_allocno_map[i]));
  1763. }
  1764. }
  1765. /* Create allocnos corresponding to pseudo-registers living in loop
  1766. represented by the corresponding loop tree node LOOP_NODE. This
  1767. function is called by ira_traverse_loop_tree. */
  1768. static void
  1769. create_loop_tree_node_allocnos (ira_loop_tree_node_t loop_node)
  1770. {
  1771. if (loop_node->bb != NULL)
  1772. create_bb_allocnos (loop_node);
  1773. else if (loop_node != ira_loop_tree_root)
  1774. {
  1775. int i;
  1776. edge_iterator ei;
  1777. edge e;
  1778. vec<edge> edges;
  1779. ira_assert (current_loops != NULL);
  1780. FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
  1781. if (e->src != loop_node->loop->latch)
  1782. create_loop_allocnos (e);
  1783. edges = get_loop_exit_edges (loop_node->loop);
  1784. FOR_EACH_VEC_ELT (edges, i, e)
  1785. create_loop_allocnos (e);
  1786. edges.release ();
  1787. }
  1788. }
  1789. /* Propagate information about allocnos modified inside the loop given
  1790. by its LOOP_TREE_NODE to its parent. */
  1791. static void
  1792. propagate_modified_regnos (ira_loop_tree_node_t loop_tree_node)
  1793. {
  1794. if (loop_tree_node == ira_loop_tree_root)
  1795. return;
  1796. ira_assert (loop_tree_node->bb == NULL);
  1797. bitmap_ior_into (loop_tree_node->parent->modified_regnos,
  1798. loop_tree_node->modified_regnos);
  1799. }
  1800. /* Propagate new info about allocno A (see comments about accumulated
  1801. info in allocno definition) to the corresponding allocno on upper
  1802. loop tree level. So allocnos on upper levels accumulate
  1803. information about the corresponding allocnos in nested regions.
  1804. The new info means allocno info finally calculated in this
  1805. file. */
  1806. static void
  1807. propagate_allocno_info (void)
  1808. {
  1809. int i;
  1810. ira_allocno_t a, parent_a;
  1811. ira_loop_tree_node_t parent;
  1812. enum reg_class aclass;
  1813. if (flag_ira_region != IRA_REGION_ALL
  1814. && flag_ira_region != IRA_REGION_MIXED)
  1815. return;
  1816. for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
  1817. for (a = ira_regno_allocno_map[i];
  1818. a != NULL;
  1819. a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
  1820. if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
  1821. && (parent_a = parent->regno_allocno_map[i]) != NULL
  1822. /* There are no caps yet at this point. So use
  1823. border_allocnos to find allocnos for the propagation. */
  1824. && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE (a)->border_allocnos,
  1825. ALLOCNO_NUM (a)))
  1826. {
  1827. if (! ALLOCNO_BAD_SPILL_P (a))
  1828. ALLOCNO_BAD_SPILL_P (parent_a) = false;
  1829. ALLOCNO_NREFS (parent_a) += ALLOCNO_NREFS (a);
  1830. ALLOCNO_FREQ (parent_a) += ALLOCNO_FREQ (a);
  1831. ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
  1832. merge_hard_reg_conflicts (a, parent_a, true);
  1833. ALLOCNO_CALLS_CROSSED_NUM (parent_a)
  1834. += ALLOCNO_CALLS_CROSSED_NUM (a);
  1835. ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
  1836. += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
  1837. IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a),
  1838. ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
  1839. ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
  1840. += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
  1841. aclass = ALLOCNO_CLASS (a);
  1842. ira_assert (aclass == ALLOCNO_CLASS (parent_a));
  1843. ira_allocate_and_accumulate_costs
  1844. (&ALLOCNO_HARD_REG_COSTS (parent_a), aclass,
  1845. ALLOCNO_HARD_REG_COSTS (a));
  1846. ira_allocate_and_accumulate_costs
  1847. (&ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a),
  1848. aclass,
  1849. ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
  1850. ALLOCNO_CLASS_COST (parent_a)
  1851. += ALLOCNO_CLASS_COST (a);
  1852. ALLOCNO_MEMORY_COST (parent_a) += ALLOCNO_MEMORY_COST (a);
  1853. }
  1854. }
  1855. /* Create allocnos corresponding to pseudo-registers in the current
  1856. function. Traverse the loop tree for this. */
  1857. static void
  1858. create_allocnos (void)
  1859. {
  1860. /* We need to process BB first to correctly link allocnos by member
  1861. next_regno_allocno. */
  1862. ira_traverse_loop_tree (true, ira_loop_tree_root,
  1863. create_loop_tree_node_allocnos, NULL);
  1864. if (optimize)
  1865. ira_traverse_loop_tree (false, ira_loop_tree_root, NULL,
  1866. propagate_modified_regnos);
  1867. }
  1868. /* The page contains function to remove some regions from a separate
  1869. register allocation. We remove regions whose separate allocation
  1870. will hardly improve the result. As a result we speed up regional
  1871. register allocation. */
  1872. /* The function changes the object in range list given by R to OBJ. */
  1873. static void
  1874. change_object_in_range_list (live_range_t r, ira_object_t obj)
  1875. {
  1876. for (; r != NULL; r = r->next)
  1877. r->object = obj;
  1878. }
  1879. /* Move all live ranges associated with allocno FROM to allocno TO. */
  1880. static void
  1881. move_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
  1882. {
  1883. int i;
  1884. int n = ALLOCNO_NUM_OBJECTS (from);
  1885. gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
  1886. for (i = 0; i < n; i++)
  1887. {
  1888. ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
  1889. ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
  1890. live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
  1891. if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
  1892. {
  1893. fprintf (ira_dump_file,
  1894. " Moving ranges of a%dr%d to a%dr%d: ",
  1895. ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
  1896. ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
  1897. ira_print_live_range_list (ira_dump_file, lr);
  1898. }
  1899. change_object_in_range_list (lr, to_obj);
  1900. OBJECT_LIVE_RANGES (to_obj)
  1901. = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
  1902. OBJECT_LIVE_RANGES (from_obj) = NULL;
  1903. }
  1904. }
  1905. static void
  1906. copy_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
  1907. {
  1908. int i;
  1909. int n = ALLOCNO_NUM_OBJECTS (from);
  1910. gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
  1911. for (i = 0; i < n; i++)
  1912. {
  1913. ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
  1914. ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
  1915. live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
  1916. if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
  1917. {
  1918. fprintf (ira_dump_file, " Copying ranges of a%dr%d to a%dr%d: ",
  1919. ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
  1920. ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
  1921. ira_print_live_range_list (ira_dump_file, lr);
  1922. }
  1923. lr = ira_copy_live_range_list (lr);
  1924. change_object_in_range_list (lr, to_obj);
  1925. OBJECT_LIVE_RANGES (to_obj)
  1926. = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
  1927. }
  1928. }
  1929. /* Return TRUE if NODE represents a loop with low register
  1930. pressure. */
  1931. static bool
  1932. low_pressure_loop_node_p (ira_loop_tree_node_t node)
  1933. {
  1934. int i;
  1935. enum reg_class pclass;
  1936. if (node->bb != NULL)
  1937. return false;
  1938. for (i = 0; i < ira_pressure_classes_num; i++)
  1939. {
  1940. pclass = ira_pressure_classes[i];
  1941. if (node->reg_pressure[pclass] > ira_class_hard_regs_num[pclass]
  1942. && ira_class_hard_regs_num[pclass] > 1)
  1943. return false;
  1944. }
  1945. return true;
  1946. }
  1947. #ifdef STACK_REGS
  1948. /* Return TRUE if LOOP has a complex enter or exit edge. We don't
  1949. form a region from such loop if the target use stack register
  1950. because reg-stack.c can not deal with such edges. */
  1951. static bool
  1952. loop_with_complex_edge_p (struct loop *loop)
  1953. {
  1954. int i;
  1955. edge_iterator ei;
  1956. edge e;
  1957. vec<edge> edges;
  1958. bool res;
  1959. FOR_EACH_EDGE (e, ei, loop->header->preds)
  1960. if (e->flags & EDGE_EH)
  1961. return true;
  1962. edges = get_loop_exit_edges (loop);
  1963. res = false;
  1964. FOR_EACH_VEC_ELT (edges, i, e)
  1965. if (e->flags & EDGE_COMPLEX)
  1966. {
  1967. res = true;
  1968. break;
  1969. }
  1970. edges.release ();
  1971. return res;
  1972. }
  1973. #endif
  1974. /* Sort loops for marking them for removal. We put already marked
  1975. loops first, then less frequent loops next, and then outer loops
  1976. next. */
  1977. static int
  1978. loop_compare_func (const void *v1p, const void *v2p)
  1979. {
  1980. int diff;
  1981. ira_loop_tree_node_t l1 = *(const ira_loop_tree_node_t *) v1p;
  1982. ira_loop_tree_node_t l2 = *(const ira_loop_tree_node_t *) v2p;
  1983. ira_assert (l1->parent != NULL && l2->parent != NULL);
  1984. if (l1->to_remove_p && ! l2->to_remove_p)
  1985. return -1;
  1986. if (! l1->to_remove_p && l2->to_remove_p)
  1987. return 1;
  1988. if ((diff = l1->loop->header->frequency - l2->loop->header->frequency) != 0)
  1989. return diff;
  1990. if ((diff = (int) loop_depth (l1->loop) - (int) loop_depth (l2->loop)) != 0)
  1991. return diff;
  1992. /* Make sorting stable. */
  1993. return l1->loop_num - l2->loop_num;
  1994. }
  1995. /* Mark loops which should be removed from regional allocation. We
  1996. remove a loop with low register pressure inside another loop with
  1997. register pressure. In this case a separate allocation of the loop
  1998. hardly helps (for irregular register file architecture it could
  1999. help by choosing a better hard register in the loop but we prefer
  2000. faster allocation even in this case). We also remove cheap loops
  2001. if there are more than IRA_MAX_LOOPS_NUM of them. Loop with EH
  2002. exit or enter edges are removed too because the allocation might
  2003. require put pseudo moves on the EH edges (we could still do this
  2004. for pseudos with caller saved hard registers in some cases but it
  2005. is impossible to say here or during top-down allocation pass what
  2006. hard register the pseudos get finally). */
  2007. static void
  2008. mark_loops_for_removal (void)
  2009. {
  2010. int i, n;
  2011. ira_loop_tree_node_t *sorted_loops;
  2012. loop_p loop;
  2013. ira_assert (current_loops != NULL);
  2014. sorted_loops
  2015. = (ira_loop_tree_node_t *) ira_allocate (sizeof (ira_loop_tree_node_t)
  2016. * number_of_loops (cfun));
  2017. for (n = i = 0; vec_safe_iterate (get_loops (cfun), i, &loop); i++)
  2018. if (ira_loop_nodes[i].regno_allocno_map != NULL)
  2019. {
  2020. if (ira_loop_nodes[i].parent == NULL)
  2021. {
  2022. /* Don't remove the root. */
  2023. ira_loop_nodes[i].to_remove_p = false;
  2024. continue;
  2025. }
  2026. sorted_loops[n++] = &ira_loop_nodes[i];
  2027. ira_loop_nodes[i].to_remove_p
  2028. = ((low_pressure_loop_node_p (ira_loop_nodes[i].parent)
  2029. && low_pressure_loop_node_p (&ira_loop_nodes[i]))
  2030. #ifdef STACK_REGS
  2031. || loop_with_complex_edge_p (ira_loop_nodes[i].loop)
  2032. #endif
  2033. );
  2034. }
  2035. qsort (sorted_loops, n, sizeof (ira_loop_tree_node_t), loop_compare_func);
  2036. for (i = 0; n - i + 1 > IRA_MAX_LOOPS_NUM; i++)
  2037. {
  2038. sorted_loops[i]->to_remove_p = true;
  2039. if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
  2040. fprintf
  2041. (ira_dump_file,
  2042. " Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
  2043. sorted_loops[i]->loop_num, sorted_loops[i]->loop->header->index,
  2044. sorted_loops[i]->loop->header->frequency,
  2045. loop_depth (sorted_loops[i]->loop),
  2046. low_pressure_loop_node_p (sorted_loops[i]->parent)
  2047. && low_pressure_loop_node_p (sorted_loops[i])
  2048. ? "low pressure" : "cheap loop");
  2049. }
  2050. ira_free (sorted_loops);
  2051. }
  2052. /* Mark all loops but root for removing. */
  2053. static void
  2054. mark_all_loops_for_removal (void)
  2055. {
  2056. int i;
  2057. loop_p loop;
  2058. ira_assert (current_loops != NULL);
  2059. FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
  2060. if (ira_loop_nodes[i].regno_allocno_map != NULL)
  2061. {
  2062. if (ira_loop_nodes[i].parent == NULL)
  2063. {
  2064. /* Don't remove the root. */
  2065. ira_loop_nodes[i].to_remove_p = false;
  2066. continue;
  2067. }
  2068. ira_loop_nodes[i].to_remove_p = true;
  2069. if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
  2070. fprintf
  2071. (ira_dump_file,
  2072. " Mark loop %d (header %d, freq %d, depth %d) for removal\n",
  2073. ira_loop_nodes[i].loop_num,
  2074. ira_loop_nodes[i].loop->header->index,
  2075. ira_loop_nodes[i].loop->header->frequency,
  2076. loop_depth (ira_loop_nodes[i].loop));
  2077. }
  2078. }
  2079. /* Definition of vector of loop tree nodes. */
  2080. /* Vec containing references to all removed loop tree nodes. */
  2081. static vec<ira_loop_tree_node_t> removed_loop_vec;
  2082. /* Vec containing references to all children of loop tree nodes. */
  2083. static vec<ira_loop_tree_node_t> children_vec;
  2084. /* Remove subregions of NODE if their separate allocation will not
  2085. improve the result. */
  2086. static void
  2087. remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node)
  2088. {
  2089. unsigned int start;
  2090. bool remove_p;
  2091. ira_loop_tree_node_t subnode;
  2092. remove_p = node->to_remove_p;
  2093. if (! remove_p)
  2094. children_vec.safe_push (node);
  2095. start = children_vec.length ();
  2096. for (subnode = node->children; subnode != NULL; subnode = subnode->next)
  2097. if (subnode->bb == NULL)
  2098. remove_uneccesary_loop_nodes_from_loop_tree (subnode);
  2099. else
  2100. children_vec.safe_push (subnode);
  2101. node->children = node->subloops = NULL;
  2102. if (remove_p)
  2103. {
  2104. removed_loop_vec.safe_push (node);
  2105. return;
  2106. }
  2107. while (children_vec.length () > start)
  2108. {
  2109. subnode = children_vec.pop ();
  2110. subnode->parent = node;
  2111. subnode->next = node->children;
  2112. node->children = subnode;
  2113. if (subnode->bb == NULL)
  2114. {
  2115. subnode->subloop_next = node->subloops;
  2116. node->subloops = subnode;
  2117. }
  2118. }
  2119. }
  2120. /* Return TRUE if NODE is inside PARENT. */
  2121. static bool
  2122. loop_is_inside_p (ira_loop_tree_node_t node, ira_loop_tree_node_t parent)
  2123. {
  2124. for (node = node->parent; node != NULL; node = node->parent)
  2125. if (node == parent)
  2126. return true;
  2127. return false;
  2128. }
  2129. /* Sort allocnos according to their order in regno allocno list. */
  2130. static int
  2131. regno_allocno_order_compare_func (const void *v1p, const void *v2p)
  2132. {
  2133. ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
  2134. ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
  2135. ira_loop_tree_node_t n1 = ALLOCNO_LOOP_TREE_NODE (a1);
  2136. ira_loop_tree_node_t n2 = ALLOCNO_LOOP_TREE_NODE (a2);
  2137. if (loop_is_inside_p (n1, n2))
  2138. return -1;
  2139. else if (loop_is_inside_p (n2, n1))
  2140. return 1;
  2141. /* If allocnos are equally good, sort by allocno numbers, so that
  2142. the results of qsort leave nothing to chance. We put allocnos
  2143. with higher number first in the list because it is the original
  2144. order for allocnos from loops on the same levels. */
  2145. return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
  2146. }
  2147. /* This array is used to sort allocnos to restore allocno order in
  2148. the regno allocno list. */
  2149. static ira_allocno_t *regno_allocnos;
  2150. /* Restore allocno order for REGNO in the regno allocno list. */
  2151. static void
  2152. ira_rebuild_regno_allocno_list (int regno)
  2153. {
  2154. int i, n;
  2155. ira_allocno_t a;
  2156. for (n = 0, a = ira_regno_allocno_map[regno];
  2157. a != NULL;
  2158. a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
  2159. regno_allocnos[n++] = a;
  2160. ira_assert (n > 0);
  2161. qsort (regno_allocnos, n, sizeof (ira_allocno_t),
  2162. regno_allocno_order_compare_func);
  2163. for (i = 1; i < n; i++)
  2164. ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[i - 1]) = regno_allocnos[i];
  2165. ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[n - 1]) = NULL;
  2166. ira_regno_allocno_map[regno] = regno_allocnos[0];
  2167. if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
  2168. fprintf (ira_dump_file, " Rebuilding regno allocno list for %d\n", regno);
  2169. }
  2170. /* Propagate info from allocno FROM_A to allocno A. */
  2171. static void
  2172. propagate_some_info_from_allocno (ira_allocno_t a, ira_allocno_t from_a)
  2173. {
  2174. enum reg_class aclass;
  2175. merge_hard_reg_conflicts (from_a, a, false);
  2176. ALLOCNO_NREFS (a) += ALLOCNO_NREFS (from_a);
  2177. ALLOCNO_FREQ (a) += ALLOCNO_FREQ (from_a);
  2178. ALLOCNO_CALL_FREQ (a) += ALLOCNO_CALL_FREQ (from_a);
  2179. ALLOCNO_CALLS_CROSSED_NUM (a) += ALLOCNO_CALLS_CROSSED_NUM (from_a);
  2180. ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a)
  2181. += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (from_a);
  2182. IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a),
  2183. ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (from_a));
  2184. ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
  2185. += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a);
  2186. if (! ALLOCNO_BAD_SPILL_P (from_a))
  2187. ALLOCNO_BAD_SPILL_P (a) = false;
  2188. aclass = ALLOCNO_CLASS (from_a);
  2189. ira_assert (aclass == ALLOCNO_CLASS (a));
  2190. ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a), aclass,
  2191. ALLOCNO_HARD_REG_COSTS (from_a));
  2192. ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
  2193. aclass,
  2194. ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a));
  2195. ALLOCNO_CLASS_COST (a) += ALLOCNO_CLASS_COST (from_a);
  2196. ALLOCNO_MEMORY_COST (a) += ALLOCNO_MEMORY_COST (from_a);
  2197. }
  2198. /* Remove allocnos from loops removed from the allocation
  2199. consideration. */
  2200. static void
  2201. remove_unnecessary_allocnos (void)
  2202. {
  2203. int regno;
  2204. bool merged_p, rebuild_p;
  2205. ira_allocno_t a, prev_a, next_a, parent_a;
  2206. ira_loop_tree_node_t a_node, parent;
  2207. merged_p = false;
  2208. regno_allocnos = NULL;
  2209. for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
  2210. {
  2211. rebuild_p = false;
  2212. for (prev_a = NULL, a = ira_regno_allocno_map[regno];
  2213. a != NULL;
  2214. a = next_a)
  2215. {
  2216. next_a = ALLOCNO_NEXT_REGNO_ALLOCNO (a);
  2217. a_node = ALLOCNO_LOOP_TREE_NODE (a);
  2218. if (! a_node->to_remove_p)
  2219. prev_a = a;
  2220. else
  2221. {
  2222. for (parent = a_node->parent;
  2223. (parent_a = parent->regno_allocno_map[regno]) == NULL
  2224. && parent->to_remove_p;
  2225. parent = parent->parent)
  2226. ;
  2227. if (parent_a == NULL)
  2228. {
  2229. /* There are no allocnos with the same regno in
  2230. upper region -- just move the allocno to the
  2231. upper region. */
  2232. prev_a = a;
  2233. ALLOCNO_LOOP_TREE_NODE (a) = parent;
  2234. parent->regno_allocno_map[regno] = a;
  2235. bitmap_set_bit (parent->all_allocnos, ALLOCNO_NUM (a));
  2236. rebuild_p = true;
  2237. }
  2238. else
  2239. {
  2240. /* Remove the allocno and update info of allocno in
  2241. the upper region. */
  2242. if (prev_a == NULL)
  2243. ira_regno_allocno_map[regno] = next_a;
  2244. else
  2245. ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a) = next_a;
  2246. move_allocno_live_ranges (a, parent_a);
  2247. merged_p = true;
  2248. propagate_some_info_from_allocno (parent_a, a);
  2249. /* Remove it from the corresponding regno allocno
  2250. map to avoid info propagation of subsequent
  2251. allocno into this already removed allocno. */
  2252. a_node->regno_allocno_map[regno] = NULL;
  2253. ira_remove_allocno_prefs (a);
  2254. finish_allocno (a);
  2255. }
  2256. }
  2257. }
  2258. if (rebuild_p)
  2259. /* We need to restore the order in regno allocno list. */
  2260. {
  2261. if (regno_allocnos == NULL)
  2262. regno_allocnos
  2263. = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
  2264. * ira_allocnos_num);
  2265. ira_rebuild_regno_allocno_list (regno);
  2266. }
  2267. }
  2268. if (merged_p)
  2269. ira_rebuild_start_finish_chains ();
  2270. if (regno_allocnos != NULL)
  2271. ira_free (regno_allocnos);
  2272. }
  2273. /* Remove allocnos from all loops but the root. */
  2274. static void
  2275. remove_low_level_allocnos (void)
  2276. {
  2277. int regno;
  2278. bool merged_p, propagate_p;
  2279. ira_allocno_t a, top_a;
  2280. ira_loop_tree_node_t a_node, parent;
  2281. ira_allocno_iterator ai;
  2282. merged_p = false;
  2283. FOR_EACH_ALLOCNO (a, ai)
  2284. {
  2285. a_node = ALLOCNO_LOOP_TREE_NODE (a);
  2286. if (a_node == ira_loop_tree_root || ALLOCNO_CAP_MEMBER (a) != NULL)
  2287. continue;
  2288. regno = ALLOCNO_REGNO (a);
  2289. if ((top_a = ira_loop_tree_root->regno_allocno_map[regno]) == NULL)
  2290. {
  2291. ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
  2292. ira_loop_tree_root->regno_allocno_map[regno] = a;
  2293. continue;
  2294. }
  2295. propagate_p = a_node->parent->regno_allocno_map[regno] == NULL;
  2296. /* Remove the allocno and update info of allocno in the upper
  2297. region. */
  2298. move_allocno_live_ranges (a, top_a);
  2299. merged_p = true;
  2300. if (propagate_p)
  2301. propagate_some_info_from_allocno (top_a, a);
  2302. }
  2303. FOR_EACH_ALLOCNO (a, ai)
  2304. {
  2305. a_node = ALLOCNO_LOOP_TREE_NODE (a);
  2306. if (a_node == ira_loop_tree_root)
  2307. continue;
  2308. parent = a_node->parent;
  2309. regno = ALLOCNO_REGNO (a);
  2310. if (ALLOCNO_CAP_MEMBER (a) != NULL)
  2311. ira_assert (ALLOCNO_CAP (a) != NULL);
  2312. else if (ALLOCNO_CAP (a) == NULL)
  2313. ira_assert (parent->regno_allocno_map[regno] != NULL);
  2314. }
  2315. FOR_EACH_ALLOCNO (a, ai)
  2316. {
  2317. regno = ALLOCNO_REGNO (a);
  2318. if (ira_loop_tree_root->regno_allocno_map[regno] == a)
  2319. {
  2320. ira_object_t obj;
  2321. ira_allocno_object_iterator oi;
  2322. ira_regno_allocno_map[regno] = a;
  2323. ALLOCNO_NEXT_REGNO_ALLOCNO (a) = NULL;
  2324. ALLOCNO_CAP_MEMBER (a) = NULL;
  2325. FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
  2326. COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
  2327. OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
  2328. #ifdef STACK_REGS
  2329. if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
  2330. ALLOCNO_NO_STACK_REG_P (a) = true;
  2331. #endif
  2332. }
  2333. else
  2334. {
  2335. ira_remove_allocno_prefs (a);
  2336. finish_allocno (a);
  2337. }
  2338. }
  2339. if (merged_p)
  2340. ira_rebuild_start_finish_chains ();
  2341. }
  2342. /* Remove loops from consideration. We remove all loops except for
  2343. root if ALL_P or loops for which a separate allocation will not
  2344. improve the result. We have to do this after allocno creation and
  2345. their costs and allocno class evaluation because only after that
  2346. the register pressure can be known and is calculated. */
  2347. static void
  2348. remove_unnecessary_regions (bool all_p)
  2349. {
  2350. if (current_loops == NULL)
  2351. return;
  2352. if (all_p)
  2353. mark_all_loops_for_removal ();
  2354. else
  2355. mark_loops_for_removal ();
  2356. children_vec.create (last_basic_block_for_fn (cfun)
  2357. + number_of_loops (cfun));
  2358. removed_loop_vec.create (last_basic_block_for_fn (cfun)
  2359. + number_of_loops (cfun));
  2360. remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root);
  2361. children_vec.release ();
  2362. if (all_p)
  2363. remove_low_level_allocnos ();
  2364. else
  2365. remove_unnecessary_allocnos ();
  2366. while (removed_loop_vec.length () > 0)
  2367. finish_loop_tree_node (removed_loop_vec.pop ());
  2368. removed_loop_vec.release ();
  2369. }
  2370. /* At this point true value of allocno attribute bad_spill_p means
  2371. that there is an insn where allocno occurs and where the allocno
  2372. can not be used as memory. The function updates the attribute, now
  2373. it can be true only for allocnos which can not be used as memory in
  2374. an insn and in whose live ranges there is other allocno deaths.
  2375. Spilling allocnos with true value will not improve the code because
  2376. it will not make other allocnos colorable and additional reloads
  2377. for the corresponding pseudo will be generated in reload pass for
  2378. each insn it occurs.
  2379. This is a trick mentioned in one classic article of Chaitin etc
  2380. which is frequently omitted in other implementations of RA based on
  2381. graph coloring. */
  2382. static void
  2383. update_bad_spill_attribute (void)
  2384. {
  2385. int i;
  2386. ira_allocno_t a;
  2387. ira_allocno_iterator ai;
  2388. ira_allocno_object_iterator aoi;
  2389. ira_object_t obj;
  2390. live_range_t r;
  2391. enum reg_class aclass;
  2392. bitmap_head dead_points[N_REG_CLASSES];
  2393. for (i = 0; i < ira_allocno_classes_num; i++)
  2394. {
  2395. aclass = ira_allocno_classes[i];
  2396. bitmap_initialize (&dead_points[aclass], &reg_obstack);
  2397. }
  2398. FOR_EACH_ALLOCNO (a, ai)
  2399. {
  2400. aclass = ALLOCNO_CLASS (a);
  2401. if (aclass == NO_REGS)
  2402. continue;
  2403. FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
  2404. for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
  2405. bitmap_set_bit (&dead_points[aclass], r->finish);
  2406. }
  2407. FOR_EACH_ALLOCNO (a, ai)
  2408. {
  2409. aclass = ALLOCNO_CLASS (a);
  2410. if (aclass == NO_REGS)
  2411. continue;
  2412. if (! ALLOCNO_BAD_SPILL_P (a))
  2413. continue;
  2414. FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
  2415. {
  2416. for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
  2417. {
  2418. for (i = r->start + 1; i < r->finish; i++)
  2419. if (bitmap_bit_p (&dead_points[aclass], i))
  2420. break;
  2421. if (i < r->finish)
  2422. break;
  2423. }
  2424. if (r != NULL)
  2425. {
  2426. ALLOCNO_BAD_SPILL_P (a) = false;
  2427. break;
  2428. }
  2429. }
  2430. }
  2431. for (i = 0; i < ira_allocno_classes_num; i++)
  2432. {
  2433. aclass = ira_allocno_classes[i];
  2434. bitmap_clear (&dead_points[aclass]);
  2435. }
  2436. }
  2437. /* Set up minimal and maximal live range points for allocnos. */
  2438. static void
  2439. setup_min_max_allocno_live_range_point (void)
  2440. {
  2441. int i;
  2442. ira_allocno_t a, parent_a, cap;
  2443. ira_allocno_iterator ai;
  2444. #ifdef ENABLE_IRA_CHECKING
  2445. ira_object_iterator oi;
  2446. ira_object_t obj;
  2447. #endif
  2448. live_range_t r;
  2449. ira_loop_tree_node_t parent;
  2450. FOR_EACH_ALLOCNO (a, ai)
  2451. {
  2452. int n = ALLOCNO_NUM_OBJECTS (a);
  2453. for (i = 0; i < n; i++)
  2454. {
  2455. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  2456. r = OBJECT_LIVE_RANGES (obj);
  2457. if (r == NULL)
  2458. continue;
  2459. OBJECT_MAX (obj) = r->finish;
  2460. for (; r->next != NULL; r = r->next)
  2461. ;
  2462. OBJECT_MIN (obj) = r->start;
  2463. }
  2464. }
  2465. for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
  2466. for (a = ira_regno_allocno_map[i];
  2467. a != NULL;
  2468. a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
  2469. {
  2470. int j;
  2471. int n = ALLOCNO_NUM_OBJECTS (a);
  2472. for (j = 0; j < n; j++)
  2473. {
  2474. ira_object_t obj = ALLOCNO_OBJECT (a, j);
  2475. ira_object_t parent_obj;
  2476. if (OBJECT_MAX (obj) < 0)
  2477. continue;
  2478. ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
  2479. /* Accumulation of range info. */
  2480. if (ALLOCNO_CAP (a) != NULL)
  2481. {
  2482. for (cap = ALLOCNO_CAP (a); cap != NULL; cap = ALLOCNO_CAP (cap))
  2483. {
  2484. ira_object_t cap_obj = ALLOCNO_OBJECT (cap, j);
  2485. if (OBJECT_MAX (cap_obj) < OBJECT_MAX (obj))
  2486. OBJECT_MAX (cap_obj) = OBJECT_MAX (obj);
  2487. if (OBJECT_MIN (cap_obj) > OBJECT_MIN (obj))
  2488. OBJECT_MIN (cap_obj) = OBJECT_MIN (obj);
  2489. }
  2490. continue;
  2491. }
  2492. if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) == NULL)
  2493. continue;
  2494. parent_a = parent->regno_allocno_map[i];
  2495. parent_obj = ALLOCNO_OBJECT (parent_a, j);
  2496. if (OBJECT_MAX (parent_obj) < OBJECT_MAX (obj))
  2497. OBJECT_MAX (parent_obj) = OBJECT_MAX (obj);
  2498. if (OBJECT_MIN (parent_obj) > OBJECT_MIN (obj))
  2499. OBJECT_MIN (parent_obj) = OBJECT_MIN (obj);
  2500. }
  2501. }
  2502. #ifdef ENABLE_IRA_CHECKING
  2503. FOR_EACH_OBJECT (obj, oi)
  2504. {
  2505. if ((0 <= OBJECT_MIN (obj) && OBJECT_MIN (obj) <= ira_max_point)
  2506. && (0 <= OBJECT_MAX (obj) && OBJECT_MAX (obj) <= ira_max_point))
  2507. continue;
  2508. gcc_unreachable ();
  2509. }
  2510. #endif
  2511. }
  2512. /* Sort allocnos according to their live ranges. Allocnos with
  2513. smaller allocno class are put first unless we use priority
  2514. coloring. Allocnos with the same class are ordered according
  2515. their start (min). Allocnos with the same start are ordered
  2516. according their finish (max). */
  2517. static int
  2518. object_range_compare_func (const void *v1p, const void *v2p)
  2519. {
  2520. int diff;
  2521. ira_object_t obj1 = *(const ira_object_t *) v1p;
  2522. ira_object_t obj2 = *(const ira_object_t *) v2p;
  2523. ira_allocno_t a1 = OBJECT_ALLOCNO (obj1);
  2524. ira_allocno_t a2 = OBJECT_ALLOCNO (obj2);
  2525. if ((diff = OBJECT_MIN (obj1) - OBJECT_MIN (obj2)) != 0)
  2526. return diff;
  2527. if ((diff = OBJECT_MAX (obj1) - OBJECT_MAX (obj2)) != 0)
  2528. return diff;
  2529. return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
  2530. }
  2531. /* Sort ira_object_id_map and set up conflict id of allocnos. */
  2532. static void
  2533. sort_conflict_id_map (void)
  2534. {
  2535. int i, num;
  2536. ira_allocno_t a;
  2537. ira_allocno_iterator ai;
  2538. num = 0;
  2539. FOR_EACH_ALLOCNO (a, ai)
  2540. {
  2541. ira_allocno_object_iterator oi;
  2542. ira_object_t obj;
  2543. FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
  2544. ira_object_id_map[num++] = obj;
  2545. }
  2546. if (num > 1)
  2547. qsort (ira_object_id_map, num, sizeof (ira_object_t),
  2548. object_range_compare_func);
  2549. for (i = 0; i < num; i++)
  2550. {
  2551. ira_object_t obj = ira_object_id_map[i];
  2552. gcc_assert (obj != NULL);
  2553. OBJECT_CONFLICT_ID (obj) = i;
  2554. }
  2555. for (i = num; i < ira_objects_num; i++)
  2556. ira_object_id_map[i] = NULL;
  2557. }
  2558. /* Set up minimal and maximal conflict ids of allocnos with which
  2559. given allocno can conflict. */
  2560. static void
  2561. setup_min_max_conflict_allocno_ids (void)
  2562. {
  2563. int aclass;
  2564. int i, j, min, max, start, finish, first_not_finished, filled_area_start;
  2565. int *live_range_min, *last_lived;
  2566. int word0_min, word0_max;
  2567. ira_allocno_t a;
  2568. ira_allocno_iterator ai;
  2569. live_range_min = (int *) ira_allocate (sizeof (int) * ira_objects_num);
  2570. aclass = -1;
  2571. first_not_finished = -1;
  2572. for (i = 0; i < ira_objects_num; i++)
  2573. {
  2574. ira_object_t obj = ira_object_id_map[i];
  2575. if (obj == NULL)
  2576. continue;
  2577. a = OBJECT_ALLOCNO (obj);
  2578. if (aclass < 0)
  2579. {
  2580. aclass = ALLOCNO_CLASS (a);
  2581. min = i;
  2582. first_not_finished = i;
  2583. }
  2584. else
  2585. {
  2586. start = OBJECT_MIN (obj);
  2587. /* If we skip an allocno, the allocno with smaller ids will
  2588. be also skipped because of the secondary sorting the
  2589. range finishes (see function
  2590. object_range_compare_func). */
  2591. while (first_not_finished < i
  2592. && start > OBJECT_MAX (ira_object_id_map
  2593. [first_not_finished]))
  2594. first_not_finished++;
  2595. min = first_not_finished;
  2596. }
  2597. if (min == i)
  2598. /* We could increase min further in this case but it is good
  2599. enough. */
  2600. min++;
  2601. live_range_min[i] = OBJECT_MIN (obj);
  2602. OBJECT_MIN (obj) = min;
  2603. }
  2604. last_lived = (int *) ira_allocate (sizeof (int) * ira_max_point);
  2605. aclass = -1;
  2606. filled_area_start = -1;
  2607. for (i = ira_objects_num - 1; i >= 0; i--)
  2608. {
  2609. ira_object_t obj = ira_object_id_map[i];
  2610. if (obj == NULL)
  2611. continue;
  2612. a = OBJECT_ALLOCNO (obj);
  2613. if (aclass < 0)
  2614. {
  2615. aclass = ALLOCNO_CLASS (a);
  2616. for (j = 0; j < ira_max_point; j++)
  2617. last_lived[j] = -1;
  2618. filled_area_start = ira_max_point;
  2619. }
  2620. min = live_range_min[i];
  2621. finish = OBJECT_MAX (obj);
  2622. max = last_lived[finish];
  2623. if (max < 0)
  2624. /* We could decrease max further in this case but it is good
  2625. enough. */
  2626. max = OBJECT_CONFLICT_ID (obj) - 1;
  2627. OBJECT_MAX (obj) = max;
  2628. /* In filling, we can go further A range finish to recognize
  2629. intersection quickly because if the finish of subsequently
  2630. processed allocno (it has smaller conflict id) range is
  2631. further A range finish than they are definitely intersected
  2632. (the reason for this is the allocnos with bigger conflict id
  2633. have their range starts not smaller than allocnos with
  2634. smaller ids. */
  2635. for (j = min; j < filled_area_start; j++)
  2636. last_lived[j] = i;
  2637. filled_area_start = min;
  2638. }
  2639. ira_free (last_lived);
  2640. ira_free (live_range_min);
  2641. /* For allocnos with more than one object, we may later record extra conflicts in
  2642. subobject 0 that we cannot really know about here.
  2643. For now, simply widen the min/max range of these subobjects. */
  2644. word0_min = INT_MAX;
  2645. word0_max = INT_MIN;
  2646. FOR_EACH_ALLOCNO (a, ai)
  2647. {
  2648. int n = ALLOCNO_NUM_OBJECTS (a);
  2649. ira_object_t obj0;
  2650. if (n < 2)
  2651. continue;
  2652. obj0 = ALLOCNO_OBJECT (a, 0);
  2653. if (OBJECT_CONFLICT_ID (obj0) < word0_min)
  2654. word0_min = OBJECT_CONFLICT_ID (obj0);
  2655. if (OBJECT_CONFLICT_ID (obj0) > word0_max)
  2656. word0_max = OBJECT_CONFLICT_ID (obj0);
  2657. }
  2658. FOR_EACH_ALLOCNO (a, ai)
  2659. {
  2660. int n = ALLOCNO_NUM_OBJECTS (a);
  2661. ira_object_t obj0;
  2662. if (n < 2)
  2663. continue;
  2664. obj0 = ALLOCNO_OBJECT (a, 0);
  2665. if (OBJECT_MIN (obj0) > word0_min)
  2666. OBJECT_MIN (obj0) = word0_min;
  2667. if (OBJECT_MAX (obj0) < word0_max)
  2668. OBJECT_MAX (obj0) = word0_max;
  2669. }
  2670. }
  2671. static void
  2672. create_caps (void)
  2673. {
  2674. ira_allocno_t a;
  2675. ira_allocno_iterator ai;
  2676. ira_loop_tree_node_t loop_tree_node;
  2677. FOR_EACH_ALLOCNO (a, ai)
  2678. {
  2679. if (ALLOCNO_LOOP_TREE_NODE (a) == ira_loop_tree_root)
  2680. continue;
  2681. if (ALLOCNO_CAP_MEMBER (a) != NULL)
  2682. create_cap_allocno (a);
  2683. else if (ALLOCNO_CAP (a) == NULL)
  2684. {
  2685. loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
  2686. if (!bitmap_bit_p (loop_tree_node->border_allocnos, ALLOCNO_NUM (a)))
  2687. create_cap_allocno (a);
  2688. }
  2689. }
  2690. }
  2691. /* The page contains code transforming more one region internal
  2692. representation (IR) to one region IR which is necessary for reload.
  2693. This transformation is called IR flattening. We might just rebuild
  2694. the IR for one region but we don't do it because it takes a lot of
  2695. time. */
  2696. /* Map: regno -> allocnos which will finally represent the regno for
  2697. IR with one region. */
  2698. static ira_allocno_t *regno_top_level_allocno_map;
  2699. /* Find the allocno that corresponds to A at a level one higher up in the
  2700. loop tree. Returns NULL if A is a cap, or if it has no parent. */
  2701. ira_allocno_t
  2702. ira_parent_allocno (ira_allocno_t a)
  2703. {
  2704. ira_loop_tree_node_t parent;
  2705. if (ALLOCNO_CAP (a) != NULL)
  2706. return NULL;
  2707. parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
  2708. if (parent == NULL)
  2709. return NULL;
  2710. return parent->regno_allocno_map[ALLOCNO_REGNO (a)];
  2711. }
  2712. /* Find the allocno that corresponds to A at a level one higher up in the
  2713. loop tree. If ALLOCNO_CAP is set for A, return that. */
  2714. ira_allocno_t
  2715. ira_parent_or_cap_allocno (ira_allocno_t a)
  2716. {
  2717. if (ALLOCNO_CAP (a) != NULL)
  2718. return ALLOCNO_CAP (a);
  2719. return ira_parent_allocno (a);
  2720. }
  2721. /* Process all allocnos originated from pseudo REGNO and copy live
  2722. ranges, hard reg conflicts, and allocno stack reg attributes from
  2723. low level allocnos to final allocnos which are destinations of
  2724. removed stores at a loop exit. Return true if we copied live
  2725. ranges. */
  2726. static bool
  2727. copy_info_to_removed_store_destinations (int regno)
  2728. {
  2729. ira_allocno_t a;
  2730. ira_allocno_t parent_a = NULL;
  2731. ira_loop_tree_node_t parent;
  2732. bool merged_p;
  2733. merged_p = false;
  2734. for (a = ira_regno_allocno_map[regno];
  2735. a != NULL;
  2736. a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
  2737. {
  2738. if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))])
  2739. /* This allocno will be removed. */
  2740. continue;
  2741. /* Caps will be removed. */
  2742. ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
  2743. for (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
  2744. parent != NULL;
  2745. parent = parent->parent)
  2746. if ((parent_a = parent->regno_allocno_map[regno]) == NULL
  2747. || (parent_a
  2748. == regno_top_level_allocno_map[REGNO
  2749. (allocno_emit_reg (parent_a))]
  2750. && ALLOCNO_EMIT_DATA (parent_a)->mem_optimized_dest_p))
  2751. break;
  2752. if (parent == NULL || parent_a == NULL)
  2753. continue;
  2754. copy_allocno_live_ranges (a, parent_a);
  2755. merge_hard_reg_conflicts (a, parent_a, true);
  2756. ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
  2757. ALLOCNO_CALLS_CROSSED_NUM (parent_a)
  2758. += ALLOCNO_CALLS_CROSSED_NUM (a);
  2759. ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
  2760. += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
  2761. IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a),
  2762. ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
  2763. ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
  2764. += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
  2765. merged_p = true;
  2766. }
  2767. return merged_p;
  2768. }
  2769. /* Flatten the IR. In other words, this function transforms IR as if
  2770. it were built with one region (without loops). We could make it
  2771. much simpler by rebuilding IR with one region, but unfortunately it
  2772. takes a lot of time. MAX_REGNO_BEFORE_EMIT and
  2773. IRA_MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
  2774. IRA_MAX_POINT before emitting insns on the loop borders. */
  2775. void
  2776. ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
  2777. {
  2778. int i, j;
  2779. bool keep_p;
  2780. int hard_regs_num;
  2781. bool new_pseudos_p, merged_p, mem_dest_p;
  2782. unsigned int n;
  2783. enum reg_class aclass;
  2784. ira_allocno_t a, parent_a, first, second, node_first, node_second;
  2785. ira_copy_t cp;
  2786. ira_loop_tree_node_t node;
  2787. live_range_t r;
  2788. ira_allocno_iterator ai;
  2789. ira_copy_iterator ci;
  2790. regno_top_level_allocno_map
  2791. = (ira_allocno_t *) ira_allocate (max_reg_num ()
  2792. * sizeof (ira_allocno_t));
  2793. memset (regno_top_level_allocno_map, 0,
  2794. max_reg_num () * sizeof (ira_allocno_t));
  2795. new_pseudos_p = merged_p = false;
  2796. FOR_EACH_ALLOCNO (a, ai)
  2797. {
  2798. ira_allocno_object_iterator oi;
  2799. ira_object_t obj;
  2800. if (ALLOCNO_CAP_MEMBER (a) != NULL)
  2801. /* Caps are not in the regno allocno maps and they are never
  2802. will be transformed into allocnos existing after IR
  2803. flattening. */
  2804. continue;
  2805. FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
  2806. COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
  2807. OBJECT_CONFLICT_HARD_REGS (obj));
  2808. #ifdef STACK_REGS
  2809. ALLOCNO_TOTAL_NO_STACK_REG_P (a) = ALLOCNO_NO_STACK_REG_P (a);
  2810. #endif
  2811. }
  2812. /* Fix final allocno attributes. */
  2813. for (i = max_regno_before_emit - 1; i >= FIRST_PSEUDO_REGISTER; i--)
  2814. {
  2815. mem_dest_p = false;
  2816. for (a = ira_regno_allocno_map[i];
  2817. a != NULL;
  2818. a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
  2819. {
  2820. ira_emit_data_t parent_data, data = ALLOCNO_EMIT_DATA (a);
  2821. ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
  2822. if (data->somewhere_renamed_p)
  2823. new_pseudos_p = true;
  2824. parent_a = ira_parent_allocno (a);
  2825. if (parent_a == NULL)
  2826. {
  2827. ALLOCNO_COPIES (a) = NULL;
  2828. regno_top_level_allocno_map[REGNO (data->reg)] = a;
  2829. continue;
  2830. }
  2831. ira_assert (ALLOCNO_CAP_MEMBER (parent_a) == NULL);
  2832. if (data->mem_optimized_dest != NULL)
  2833. mem_dest_p = true;
  2834. parent_data = ALLOCNO_EMIT_DATA (parent_a);
  2835. if (REGNO (data->reg) == REGNO (parent_data->reg))
  2836. {
  2837. merge_hard_reg_conflicts (a, parent_a, true);
  2838. move_allocno_live_ranges (a, parent_a);
  2839. merged_p = true;
  2840. parent_data->mem_optimized_dest_p
  2841. = (parent_data->mem_optimized_dest_p
  2842. || data->mem_optimized_dest_p);
  2843. continue;
  2844. }
  2845. new_pseudos_p = true;
  2846. for (;;)
  2847. {
  2848. ALLOCNO_NREFS (parent_a) -= ALLOCNO_NREFS (a);
  2849. ALLOCNO_FREQ (parent_a) -= ALLOCNO_FREQ (a);
  2850. ALLOCNO_CALL_FREQ (parent_a) -= ALLOCNO_CALL_FREQ (a);
  2851. ALLOCNO_CALLS_CROSSED_NUM (parent_a)
  2852. -= ALLOCNO_CALLS_CROSSED_NUM (a);
  2853. ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
  2854. -= ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
  2855. ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
  2856. -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
  2857. ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a) >= 0
  2858. && ALLOCNO_NREFS (parent_a) >= 0
  2859. && ALLOCNO_FREQ (parent_a) >= 0);
  2860. aclass = ALLOCNO_CLASS (parent_a);
  2861. hard_regs_num = ira_class_hard_regs_num[aclass];
  2862. if (ALLOCNO_HARD_REG_COSTS (a) != NULL
  2863. && ALLOCNO_HARD_REG_COSTS (parent_a) != NULL)
  2864. for (j = 0; j < hard_regs_num; j++)
  2865. ALLOCNO_HARD_REG_COSTS (parent_a)[j]
  2866. -= ALLOCNO_HARD_REG_COSTS (a)[j];
  2867. if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL
  2868. && ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a) != NULL)
  2869. for (j = 0; j < hard_regs_num; j++)
  2870. ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a)[j]
  2871. -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[j];
  2872. ALLOCNO_CLASS_COST (parent_a)
  2873. -= ALLOCNO_CLASS_COST (a);
  2874. ALLOCNO_MEMORY_COST (parent_a) -= ALLOCNO_MEMORY_COST (a);
  2875. parent_a = ira_parent_allocno (parent_a);
  2876. if (parent_a == NULL)
  2877. break;
  2878. }
  2879. ALLOCNO_COPIES (a) = NULL;
  2880. regno_top_level_allocno_map[REGNO (data->reg)] = a;
  2881. }
  2882. if (mem_dest_p && copy_info_to_removed_store_destinations (i))
  2883. merged_p = true;
  2884. }
  2885. ira_assert (new_pseudos_p || ira_max_point_before_emit == ira_max_point);
  2886. if (merged_p || ira_max_point_before_emit != ira_max_point)
  2887. ira_rebuild_start_finish_chains ();
  2888. if (new_pseudos_p)
  2889. {
  2890. sparseset objects_live;
  2891. /* Rebuild conflicts. */
  2892. FOR_EACH_ALLOCNO (a, ai)
  2893. {
  2894. ira_allocno_object_iterator oi;
  2895. ira_object_t obj;
  2896. if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
  2897. || ALLOCNO_CAP_MEMBER (a) != NULL)
  2898. continue;
  2899. FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
  2900. {
  2901. for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
  2902. ira_assert (r->object == obj);
  2903. clear_conflicts (obj);
  2904. }
  2905. }
  2906. objects_live = sparseset_alloc (ira_objects_num);
  2907. for (i = 0; i < ira_max_point; i++)
  2908. {
  2909. for (r = ira_start_point_ranges[i]; r != NULL; r = r->start_next)
  2910. {
  2911. ira_object_t obj = r->object;
  2912. a = OBJECT_ALLOCNO (obj);
  2913. if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
  2914. || ALLOCNO_CAP_MEMBER (a) != NULL)
  2915. continue;
  2916. aclass = ALLOCNO_CLASS (a);
  2917. EXECUTE_IF_SET_IN_SPARSESET (objects_live, n)
  2918. {
  2919. ira_object_t live_obj = ira_object_id_map[n];
  2920. ira_allocno_t live_a = OBJECT_ALLOCNO (live_obj);
  2921. enum reg_class live_aclass = ALLOCNO_CLASS (live_a);
  2922. if (ira_reg_classes_intersect_p[aclass][live_aclass]
  2923. /* Don't set up conflict for the allocno with itself. */
  2924. && live_a != a)
  2925. ira_add_conflict (obj, live_obj);
  2926. }
  2927. sparseset_set_bit (objects_live, OBJECT_CONFLICT_ID (obj));
  2928. }
  2929. for (r = ira_finish_point_ranges[i]; r != NULL; r = r->finish_next)
  2930. sparseset_clear_bit (objects_live, OBJECT_CONFLICT_ID (r->object));
  2931. }
  2932. sparseset_free (objects_live);
  2933. compress_conflict_vecs ();
  2934. }
  2935. /* Mark some copies for removing and change allocnos in the rest
  2936. copies. */
  2937. FOR_EACH_COPY (cp, ci)
  2938. {
  2939. if (ALLOCNO_CAP_MEMBER (cp->first) != NULL
  2940. || ALLOCNO_CAP_MEMBER (cp->second) != NULL)
  2941. {
  2942. if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
  2943. fprintf
  2944. (ira_dump_file, " Remove cp%d:%c%dr%d-%c%dr%d\n",
  2945. cp->num, ALLOCNO_CAP_MEMBER (cp->first) != NULL ? 'c' : 'a',
  2946. ALLOCNO_NUM (cp->first),
  2947. REGNO (allocno_emit_reg (cp->first)),
  2948. ALLOCNO_CAP_MEMBER (cp->second) != NULL ? 'c' : 'a',
  2949. ALLOCNO_NUM (cp->second),
  2950. REGNO (allocno_emit_reg (cp->second)));
  2951. cp->loop_tree_node = NULL;
  2952. continue;
  2953. }
  2954. first
  2955. = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->first))];
  2956. second
  2957. = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->second))];
  2958. node = cp->loop_tree_node;
  2959. if (node == NULL)
  2960. keep_p = true; /* It copy generated in ira-emit.c. */
  2961. else
  2962. {
  2963. /* Check that the copy was not propagated from level on
  2964. which we will have different pseudos. */
  2965. node_first = node->regno_allocno_map[ALLOCNO_REGNO (cp->first)];
  2966. node_second = node->regno_allocno_map[ALLOCNO_REGNO (cp->second)];
  2967. keep_p = ((REGNO (allocno_emit_reg (first))
  2968. == REGNO (allocno_emit_reg (node_first)))
  2969. && (REGNO (allocno_emit_reg (second))
  2970. == REGNO (allocno_emit_reg (node_second))));
  2971. }
  2972. if (keep_p)
  2973. {
  2974. cp->loop_tree_node = ira_loop_tree_root;
  2975. cp->first = first;
  2976. cp->second = second;
  2977. }
  2978. else
  2979. {
  2980. cp->loop_tree_node = NULL;
  2981. if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
  2982. fprintf (ira_dump_file, " Remove cp%d:a%dr%d-a%dr%d\n",
  2983. cp->num, ALLOCNO_NUM (cp->first),
  2984. REGNO (allocno_emit_reg (cp->first)),
  2985. ALLOCNO_NUM (cp->second),
  2986. REGNO (allocno_emit_reg (cp->second)));
  2987. }
  2988. }
  2989. /* Remove unnecessary allocnos on lower levels of the loop tree. */
  2990. FOR_EACH_ALLOCNO (a, ai)
  2991. {
  2992. if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
  2993. || ALLOCNO_CAP_MEMBER (a) != NULL)
  2994. {
  2995. if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
  2996. fprintf (ira_dump_file, " Remove a%dr%d\n",
  2997. ALLOCNO_NUM (a), REGNO (allocno_emit_reg (a)));
  2998. ira_remove_allocno_prefs (a);
  2999. finish_allocno (a);
  3000. continue;
  3001. }
  3002. ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
  3003. ALLOCNO_REGNO (a) = REGNO (allocno_emit_reg (a));
  3004. ALLOCNO_CAP (a) = NULL;
  3005. /* Restore updated costs for assignments from reload. */
  3006. ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
  3007. ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
  3008. if (! ALLOCNO_ASSIGNED_P (a))
  3009. ira_free_allocno_updated_costs (a);
  3010. ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
  3011. ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
  3012. }
  3013. /* Remove unnecessary copies. */
  3014. FOR_EACH_COPY (cp, ci)
  3015. {
  3016. if (cp->loop_tree_node == NULL)
  3017. {
  3018. ira_copies[cp->num] = NULL;
  3019. finish_copy (cp);
  3020. continue;
  3021. }
  3022. ira_assert
  3023. (ALLOCNO_LOOP_TREE_NODE (cp->first) == ira_loop_tree_root
  3024. && ALLOCNO_LOOP_TREE_NODE (cp->second) == ira_loop_tree_root);
  3025. add_allocno_copy_to_list (cp);
  3026. swap_allocno_copy_ends_if_necessary (cp);
  3027. }
  3028. rebuild_regno_allocno_maps ();
  3029. if (ira_max_point != ira_max_point_before_emit)
  3030. ira_compress_allocno_live_ranges ();
  3031. ira_free (regno_top_level_allocno_map);
  3032. }
  3033. #ifdef ENABLE_IRA_CHECKING
  3034. /* Check creation of all allocnos. Allocnos on lower levels should
  3035. have allocnos or caps on all upper levels. */
  3036. static void
  3037. check_allocno_creation (void)
  3038. {
  3039. ira_allocno_t a;
  3040. ira_allocno_iterator ai;
  3041. ira_loop_tree_node_t loop_tree_node;
  3042. FOR_EACH_ALLOCNO (a, ai)
  3043. {
  3044. loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
  3045. ira_assert (bitmap_bit_p (loop_tree_node->all_allocnos,
  3046. ALLOCNO_NUM (a)));
  3047. if (loop_tree_node == ira_loop_tree_root)
  3048. continue;
  3049. if (ALLOCNO_CAP_MEMBER (a) != NULL)
  3050. ira_assert (ALLOCNO_CAP (a) != NULL);
  3051. else if (ALLOCNO_CAP (a) == NULL)
  3052. ira_assert (loop_tree_node->parent
  3053. ->regno_allocno_map[ALLOCNO_REGNO (a)] != NULL
  3054. && bitmap_bit_p (loop_tree_node->border_allocnos,
  3055. ALLOCNO_NUM (a)));
  3056. }
  3057. }
  3058. #endif
  3059. /* Identify allocnos which prefer a register class with a single hard register.
  3060. Adjust ALLOCNO_CONFLICT_HARD_REG_COSTS so that conflicting allocnos are
  3061. less likely to use the preferred singleton register. */
  3062. static void
  3063. update_conflict_hard_reg_costs (void)
  3064. {
  3065. ira_allocno_t a;
  3066. ira_allocno_iterator ai;
  3067. int i, index, min;
  3068. FOR_EACH_ALLOCNO (a, ai)
  3069. {
  3070. reg_class_t aclass = ALLOCNO_CLASS (a);
  3071. reg_class_t pref = reg_preferred_class (ALLOCNO_REGNO (a));
  3072. int singleton = ira_class_singleton[pref][ALLOCNO_MODE (a)];
  3073. if (singleton < 0)
  3074. continue;
  3075. index = ira_class_hard_reg_index[(int) aclass][singleton];
  3076. if (index < 0)
  3077. continue;
  3078. if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) == NULL
  3079. || ALLOCNO_HARD_REG_COSTS (a) == NULL)
  3080. continue;
  3081. min = INT_MAX;
  3082. for (i = ira_class_hard_regs_num[(int) aclass] - 1; i >= 0; i--)
  3083. if (ALLOCNO_HARD_REG_COSTS (a)[i] > ALLOCNO_CLASS_COST (a)
  3084. && min > ALLOCNO_HARD_REG_COSTS (a)[i])
  3085. min = ALLOCNO_HARD_REG_COSTS (a)[i];
  3086. if (min == INT_MAX)
  3087. continue;
  3088. ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
  3089. aclass, 0);
  3090. ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[index]
  3091. -= min - ALLOCNO_CLASS_COST (a);
  3092. }
  3093. }
  3094. /* Create a internal representation (IR) for IRA (allocnos, copies,
  3095. loop tree nodes). The function returns TRUE if we generate loop
  3096. structure (besides nodes representing all function and the basic
  3097. blocks) for regional allocation. A true return means that we
  3098. really need to flatten IR before the reload. */
  3099. bool
  3100. ira_build (void)
  3101. {
  3102. bool loops_p;
  3103. df_analyze ();
  3104. initiate_cost_vectors ();
  3105. initiate_allocnos ();
  3106. initiate_prefs ();
  3107. initiate_copies ();
  3108. create_loop_tree_nodes ();
  3109. form_loop_tree ();
  3110. create_allocnos ();
  3111. ira_costs ();
  3112. create_allocno_objects ();
  3113. ira_create_allocno_live_ranges ();
  3114. remove_unnecessary_regions (false);
  3115. ira_compress_allocno_live_ranges ();
  3116. update_bad_spill_attribute ();
  3117. loops_p = more_one_region_p ();
  3118. if (loops_p)
  3119. {
  3120. propagate_allocno_info ();
  3121. create_caps ();
  3122. }
  3123. ira_tune_allocno_costs ();
  3124. #ifdef ENABLE_IRA_CHECKING
  3125. check_allocno_creation ();
  3126. #endif
  3127. setup_min_max_allocno_live_range_point ();
  3128. sort_conflict_id_map ();
  3129. setup_min_max_conflict_allocno_ids ();
  3130. ira_build_conflicts ();
  3131. update_conflict_hard_reg_costs ();
  3132. if (! ira_conflicts_p)
  3133. {
  3134. ira_allocno_t a;
  3135. ira_allocno_iterator ai;
  3136. /* Remove all regions but root one. */
  3137. if (loops_p)
  3138. {
  3139. remove_unnecessary_regions (true);
  3140. loops_p = false;
  3141. }
  3142. /* We don't save hard registers around calls for fast allocation
  3143. -- add caller clobbered registers as conflicting ones to
  3144. allocno crossing calls. */
  3145. FOR_EACH_ALLOCNO (a, ai)
  3146. if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
  3147. ior_hard_reg_conflicts (a, &call_used_reg_set);
  3148. }
  3149. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  3150. print_copies (ira_dump_file);
  3151. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  3152. print_prefs (ira_dump_file);
  3153. if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
  3154. {
  3155. int n, nr, nr_big;
  3156. ira_allocno_t a;
  3157. live_range_t r;
  3158. ira_allocno_iterator ai;
  3159. n = 0;
  3160. nr = 0;
  3161. nr_big = 0;
  3162. FOR_EACH_ALLOCNO (a, ai)
  3163. {
  3164. int j, nobj = ALLOCNO_NUM_OBJECTS (a);
  3165. if (nobj > 1)
  3166. nr_big++;
  3167. for (j = 0; j < nobj; j++)
  3168. {
  3169. ira_object_t obj = ALLOCNO_OBJECT (a, j);
  3170. n += OBJECT_NUM_CONFLICTS (obj);
  3171. for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
  3172. nr++;
  3173. }
  3174. }
  3175. fprintf (ira_dump_file, " regions=%d, blocks=%d, points=%d\n",
  3176. current_loops == NULL ? 1 : number_of_loops (cfun),
  3177. n_basic_blocks_for_fn (cfun), ira_max_point);
  3178. fprintf (ira_dump_file,
  3179. " allocnos=%d (big %d), copies=%d, conflicts=%d, ranges=%d\n",
  3180. ira_allocnos_num, nr_big, ira_copies_num, n, nr);
  3181. }
  3182. return loops_p;
  3183. }
  3184. /* Release the data created by function ira_build. */
  3185. void
  3186. ira_destroy (void)
  3187. {
  3188. finish_loop_tree_nodes ();
  3189. finish_prefs ();
  3190. finish_copies ();
  3191. finish_allocnos ();
  3192. finish_cost_vectors ();
  3193. ira_finish_allocno_live_ranges ();
  3194. }