rgobj.pas 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the base class for the register allocator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. { Allow duplicate allocations, can be used to get the .s file written }
  20. { $define ALLOWDUPREG}
  21. {#******************************************************************************
  22. @abstract(Abstract register allocator unit)
  23. Register allocator introduction.
  24. Free Pascal uses a Chaitin style register allocator. We use a variant similair
  25. to the one described in the book "Modern compiler implementation in C" by
  26. Andrew W. Appel., published by Cambridge University Press.
  27. The register allocator that is described by Appel uses a much improved way
  28. of register coalescing, called "iterated register coalescing". Instead
  29. of doing coalescing as a prepass to the register allocation, the coalescing
  30. is done inside the register allocator. This has the advantage that the
  31. register allocator can coalesce very aggresively without introducing spills.
  32. Reading this book is recommended for a complete understanding. Here is a small
  33. introduction.
  34. The code generator thinks it has an infinite amount of registers. Our processor
  35. has a limited amount of registers. Therefore we must reduce the amount of
  36. registers until there are less enough to fit into the processors registers.
  37. Registers can interfere or not interfere. If two imaginary registers interfere
  38. they cannot be placed into the same psysical register. Reduction of registers
  39. is done by:
  40. - "coalescing" Two registers that do not interfere are combined
  41. into one register.
  42. - "spilling" A register is changed into a memory location and the generated
  43. code is modified to use the memory location instead of the register.
  44. Register allocation is a graph colouring problem. Each register is a colour, and
  45. if two registers interfere there is a connection between them in the graph.
  46. In addition to the imaginary registers in the code generator, the psysical
  47. CPU registers are also present in this graph. This allows us to make
  48. interferences between imaginary registers and cpu registers. This is very
  49. usefull for describing architectural constraints, like for example that
  50. the div instruction modifies edx, so variables that are in use at that time
  51. cannot be stored into edx. This can be modelled by making edx interfere
  52. with those variables.
  53. Graph colouring is an NP complete problem. Therefore we use an approximation
  54. that pushes registers to colour on to a stack. This is done in the "simplify"
  55. procedure.
  56. The register allocator first checks which registers are a candidate for
  57. coalescing.
  58. *******************************************************************************}
  59. unit rgobj;
  60. interface
  61. uses
  62. cutils, cpubase,
  63. aasmbase,aasmtai,aasmcpu,
  64. cclasses,globtype,cgbase,node,
  65. {$ifdef delphi}
  66. dmisc,
  67. {$endif}
  68. cpuinfo
  69. ;
  70. type
  71. {
  72. regvarother_longintarray = array[tregisterindex] of longint;
  73. regvarother_booleanarray = array[tregisterindex] of boolean;
  74. regvarint_longintarray = array[first_int_supreg..last_int_supreg] of longint;
  75. regvarint_ptreearray = array[first_int_supreg..last_int_supreg] of tnode;
  76. }
  77. {
  78. The interference bitmap contains of 2 layers:
  79. layer 1 - 256*256 blocks with pointers to layer 2 blocks
  80. layer 2 - blocks of 32*256 (32 bytes = 256 bits)
  81. }
  82. Tinterferencebitmap2 = array[byte] of set of byte;
  83. Pinterferencebitmap2 = ^Tinterferencebitmap2;
  84. Tinterferencebitmap1 = array[byte] of Pinterferencebitmap2;
  85. pinterferencebitmap1 = ^tinterferencebitmap1;
  86. Tinterferencebitmap=class
  87. private
  88. maxx1,
  89. maxy1 : byte;
  90. fbitmap : pinterferencebitmap1;
  91. function getbitmap(x,y:tsuperregister):boolean;
  92. procedure setbitmap(x,y:tsuperregister;b:boolean);
  93. public
  94. constructor create;
  95. destructor destroy;override;
  96. property bitmap[x,y:tsuperregister]:boolean read getbitmap write setbitmap;default;
  97. end;
  98. Tmovelistheader=record
  99. count,
  100. maxcount,
  101. sorted_until : cardinal;
  102. end;
  103. Tmovelist=record
  104. header : Tmovelistheader;
  105. data : array[tsuperregister] of Tlinkedlistitem;
  106. end;
  107. Pmovelist=^Tmovelist;
  108. {In the register allocator we keep track of move instructions.
  109. These instructions are moved between five linked lists. There
  110. is also a linked list per register to keep track about the moves
  111. it is associated with. Because we need to determine quickly in
  112. which of the five lists it is we add anu enumeradtion to each
  113. move instruction.}
  114. Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
  115. ms_worklist_moves,ms_active_moves);
  116. Tmoveins=class(Tlinkedlistitem)
  117. moveset:Tmoveset;
  118. x,y:Tsuperregister;
  119. end;
  120. Treginfoflag=(ri_coalesced,ri_selected);
  121. Treginfoflagset=set of Treginfoflag;
  122. Treginfo=record
  123. live_start,
  124. live_end : Tai;
  125. subreg : tsubregister;
  126. alias : Tsuperregister;
  127. { The register allocator assigns each register a colour }
  128. colour : Tsuperregister;
  129. movelist : Pmovelist;
  130. adjlist : Psuperregisterworklist;
  131. degree : TSuperregister;
  132. flags : Treginfoflagset;
  133. end;
  134. Preginfo=^TReginfo;
  135. tspillreginfo = record
  136. orgreg : tsuperregister;
  137. tempreg : tregister;
  138. regread,regwritten, mustbespilled: boolean;
  139. end;
  140. tspillregsinfo = array[0..2] of tspillreginfo;
  141. {#------------------------------------------------------------------
  142. This class implements the default register allocator. It is used by the
  143. code generator to allocate and free registers which might be valid
  144. across nodes. It also contains utility routines related to registers.
  145. Some of the methods in this class should be overriden
  146. by cpu-specific implementations.
  147. --------------------------------------------------------------------}
  148. trgobj=class
  149. preserved_by_proc : tcpuregisterset;
  150. used_in_proc : tcpuregisterset;
  151. // is_reg_var : Tsuperregisterset; {old regvars}
  152. // reg_var_loaded:Tsuperregisterset; {old regvars}
  153. constructor create(Aregtype:Tregistertype;
  154. Adefaultsub:Tsubregister;
  155. const Ausable:array of tsuperregister;
  156. Afirst_imaginary:Tsuperregister;
  157. Apreserved_by_proc:Tcpuregisterset);
  158. destructor destroy;override;
  159. {# Allocate a register. An internalerror will be generated if there is
  160. no more free registers which can be allocated.}
  161. function getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;virtual;
  162. {# Get the register specified.}
  163. procedure getexplicitregister(list:Taasmoutput;r:Tregister);virtual;
  164. {# Get multiple registers specified.}
  165. procedure allocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);virtual;
  166. {# Free multiple registers specified.}
  167. procedure deallocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);virtual;
  168. function uses_registers:boolean;virtual;
  169. {# Deallocate any kind of register }
  170. procedure ungetregister(list:Taasmoutput;r:Tregister);virtual;
  171. procedure add_reg_instruction(instr:Tai;r:tregister);
  172. procedure add_move_instruction(instr:Taicpu);
  173. {# Do the register allocation.}
  174. procedure do_register_allocation(list:Taasmoutput;headertai:tai);virtual;
  175. { Adds an interference edge.
  176. don't move this to the protected section, the arm cg requires to access this (FK) }
  177. procedure add_edge(u,v:Tsuperregister);
  178. protected
  179. regtype : Tregistertype;
  180. { default subregister used }
  181. defaultsub : tsubregister;
  182. live_registers:Tsuperregisterworklist;
  183. { can be overriden to add cpu specific interferences }
  184. procedure add_cpu_interferences(p : tai);virtual;
  185. function get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister):Tai;
  186. procedure forward_allocation(pfrom,pto:Tai);
  187. procedure getregisterinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  188. procedure ungetregisterinline(list:Taasmoutput;position:Tai;r:Tregister);
  189. procedure add_constraints(reg:Tregister);virtual;
  190. procedure do_spill_read(list:Taasmoutput;instr:Taicpu_abstract;
  191. pos:Tai;regidx:word;
  192. const spilltemplist:Tspill_temp_list;
  193. const regs:Tspillregsinfo);virtual;
  194. procedure do_spill_written(list:Taasmoutput;instr:Taicpu_abstract;
  195. pos:Tai;regidx:word;
  196. const spilltemplist:Tspill_temp_list;
  197. const regs:Tspillregsinfo);virtual;
  198. procedure do_spill_readwritten(list:Taasmoutput;instr:Taicpu_abstract;
  199. pos:Tai;regidx:word;
  200. const spilltemplist:Tspill_temp_list;
  201. const regs:Tspillregsinfo);virtual;
  202. function instr_spill_register(list:Taasmoutput;
  203. instr:taicpu_abstract;
  204. const r:Tsuperregisterset;
  205. const spilltemplist:Tspill_temp_list): boolean;virtual;
  206. private
  207. {# First imaginary register.}
  208. first_imaginary : Tsuperregister;
  209. {# Highest register allocated until now.}
  210. reginfo : PReginfo;
  211. maxreginfo,
  212. maxreginfoinc,
  213. maxreg : Tsuperregister;
  214. usable_registers_cnt : word;
  215. usable_registers : array[0..maxcpuregister-1] of tsuperregister;
  216. ibitmap : Tinterferencebitmap;
  217. spillednodes,
  218. simplifyworklist,
  219. freezeworklist,
  220. spillworklist,
  221. coalescednodes,
  222. selectstack : tsuperregisterworklist;
  223. worklist_moves,
  224. active_moves,
  225. frozen_moves,
  226. coalesced_moves,
  227. constrained_moves : Tlinkedlist;
  228. {$ifdef EXTDEBUG}
  229. procedure writegraph(loopidx:longint);
  230. {$endif EXTDEBUG}
  231. {# Disposes of the reginfo array.}
  232. procedure dispose_reginfo;
  233. {# Prepare the register colouring.}
  234. procedure prepare_colouring;
  235. {# Clean up after register colouring.}
  236. procedure epilogue_colouring;
  237. {# Colour the registers; that is do the register allocation.}
  238. procedure colour_registers;
  239. {# Spills certain registers in the specified assembler list.}
  240. procedure insert_regalloc_info(list:Taasmoutput;headertai:tai);
  241. procedure generate_interference_graph(list:Taasmoutput;headertai:tai);
  242. procedure translate_registers(list:Taasmoutput);
  243. function spill_registers(list:Taasmoutput;headertai:tai):boolean;virtual;
  244. function getnewreg(subreg:tsubregister):tsuperregister;
  245. procedure add_edges_used(u:Tsuperregister);
  246. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  247. function move_related(n:Tsuperregister):boolean;
  248. procedure make_work_list;
  249. procedure sort_simplify_worklist;
  250. procedure enable_moves(n:Tsuperregister);
  251. procedure decrement_degree(m:Tsuperregister);
  252. procedure simplify;
  253. function get_alias(n:Tsuperregister):Tsuperregister;
  254. procedure add_worklist(u:Tsuperregister);
  255. function adjacent_ok(u,v:Tsuperregister):boolean;
  256. function conservative(u,v:Tsuperregister):boolean;
  257. procedure combine(u,v:Tsuperregister);
  258. procedure coalesce;
  259. procedure freeze_moves(u:Tsuperregister);
  260. procedure freeze;
  261. procedure select_spill;
  262. procedure assign_colours;
  263. procedure clear_interferences(u:Tsuperregister);
  264. end;
  265. const
  266. first_reg = 0;
  267. last_reg = high(tsuperregister)-1;
  268. maxspillingcounter = 20;
  269. implementation
  270. uses
  271. systems,
  272. globals,verbose,tgobj,procinfo;
  273. procedure sort_movelist(ml:Pmovelist);
  274. {Ok, sorting pointers is silly, but it does the job to make Trgobj.combine
  275. faster.}
  276. var h,i,p:word;
  277. t:Tlinkedlistitem;
  278. begin
  279. with ml^ do
  280. begin
  281. if header.count<2 then
  282. exit;
  283. p:=1;
  284. while 2*p<header.count do
  285. p:=2*p;
  286. while p<>0 do
  287. begin
  288. for h:=p to header.count-1 do
  289. begin
  290. i:=h;
  291. t:=data[i];
  292. repeat
  293. if ptrint(data[i-p])<=ptrint(t) then
  294. break;
  295. data[i]:=data[i-p];
  296. dec(i,p);
  297. until i<p;
  298. data[i]:=t;
  299. end;
  300. p:=p shr 1;
  301. end;
  302. header.sorted_until:=header.count-1;
  303. end;
  304. end;
  305. {******************************************************************************
  306. tinterferencebitmap
  307. ******************************************************************************}
  308. constructor tinterferencebitmap.create;
  309. begin
  310. inherited create;
  311. maxx1:=1;
  312. getmem(fbitmap,sizeof(tinterferencebitmap1)*2);
  313. fillchar(fbitmap^,sizeof(tinterferencebitmap1)*2,0);
  314. end;
  315. destructor tinterferencebitmap.destroy;
  316. var i,j:byte;
  317. begin
  318. for i:=0 to maxx1 do
  319. for j:=0 to maxy1 do
  320. if assigned(fbitmap[i,j]) then
  321. dispose(fbitmap[i,j]);
  322. freemem(fbitmap);
  323. end;
  324. function tinterferencebitmap.getbitmap(x,y:tsuperregister):boolean;
  325. var
  326. page : pinterferencebitmap2;
  327. begin
  328. result:=false;
  329. if (x shr 8>maxx1) then
  330. exit;
  331. page:=fbitmap[x shr 8,y shr 8];
  332. result:=assigned(page) and
  333. ((x and $ff) in page^[y and $ff]);
  334. end;
  335. procedure tinterferencebitmap.setbitmap(x,y:tsuperregister;b:boolean);
  336. var
  337. x1,y1 : byte;
  338. begin
  339. x1:=x shr 8;
  340. y1:=y shr 8;
  341. if x1>maxx1 then
  342. begin
  343. reallocmem(fbitmap,sizeof(tinterferencebitmap1)*(x1+1));
  344. fillchar(fbitmap[maxx1+1],sizeof(tinterferencebitmap1)*(x1-maxx1),0);
  345. maxx1:=x1;
  346. end;
  347. if not assigned(fbitmap[x1,y1]) then
  348. begin
  349. if y1>maxy1 then
  350. maxy1:=y1;
  351. new(fbitmap[x1,y1]);
  352. fillchar(fbitmap[x1,y1]^,sizeof(tinterferencebitmap2),0);
  353. end;
  354. if b then
  355. include(fbitmap[x1,y1]^[y and $ff],(x and $ff))
  356. else
  357. exclude(fbitmap[x1,y1]^[y and $ff],(x and $ff));
  358. end;
  359. {******************************************************************************
  360. trgobj
  361. ******************************************************************************}
  362. constructor trgobj.create(Aregtype:Tregistertype;
  363. Adefaultsub:Tsubregister;
  364. const Ausable:array of tsuperregister;
  365. Afirst_imaginary:Tsuperregister;
  366. Apreserved_by_proc:Tcpuregisterset);
  367. var
  368. i : Tsuperregister;
  369. begin
  370. { empty super register sets can cause very strange problems }
  371. if high(Ausable)=0 then
  372. internalerror(200210181);
  373. first_imaginary:=Afirst_imaginary;
  374. maxreg:=Afirst_imaginary;
  375. regtype:=Aregtype;
  376. defaultsub:=Adefaultsub;
  377. preserved_by_proc:=Apreserved_by_proc;
  378. used_in_proc:=[];
  379. live_registers.init;
  380. { Get reginfo for CPU registers }
  381. maxreginfo:=first_imaginary;
  382. maxreginfoinc:=16;
  383. worklist_moves:=Tlinkedlist.create;
  384. reginfo:=allocmem(first_imaginary*sizeof(treginfo));
  385. for i:=0 to first_imaginary-1 do
  386. begin
  387. reginfo[i].degree:=high(tsuperregister);
  388. reginfo[i].alias:=RS_INVALID;
  389. end;
  390. { Usable registers }
  391. fillchar(usable_registers,sizeof(usable_registers),0);
  392. for i:=low(Ausable) to high(Ausable) do
  393. usable_registers[i]:=Ausable[i];
  394. usable_registers_cnt:=high(Ausable)+1;
  395. { Initialize Worklists }
  396. spillednodes.init;
  397. simplifyworklist.init;
  398. freezeworklist.init;
  399. spillworklist.init;
  400. coalescednodes.init;
  401. selectstack.init;
  402. end;
  403. destructor trgobj.destroy;
  404. begin
  405. spillednodes.done;
  406. simplifyworklist.done;
  407. freezeworklist.done;
  408. spillworklist.done;
  409. coalescednodes.done;
  410. selectstack.done;
  411. live_registers.done;
  412. worklist_moves.free;
  413. dispose_reginfo;
  414. end;
  415. procedure Trgobj.dispose_reginfo;
  416. var i:Tsuperregister;
  417. begin
  418. if reginfo<>nil then
  419. begin
  420. for i:=0 to maxreg-1 do
  421. with reginfo[i] do
  422. begin
  423. if adjlist<>nil then
  424. dispose(adjlist,done);
  425. if movelist<>nil then
  426. dispose(movelist);
  427. end;
  428. freemem(reginfo);
  429. reginfo:=nil;
  430. end;
  431. end;
  432. function trgobj.getnewreg(subreg:tsubregister):tsuperregister;
  433. var
  434. oldmaxreginfo : tsuperregister;
  435. begin
  436. result:=maxreg;
  437. inc(maxreg);
  438. if maxreg>=last_reg then
  439. internalerror(200310146);
  440. if maxreg>=maxreginfo then
  441. begin
  442. oldmaxreginfo:=maxreginfo;
  443. inc(maxreginfo,maxreginfoinc);
  444. if maxreginfoinc<256 then
  445. maxreginfoinc:=maxreginfoinc*2;
  446. reallocmem(reginfo,maxreginfo*sizeof(treginfo));
  447. { Do we really need it to clear it ? At least for 1.0.x (PFV) }
  448. fillchar(reginfo[oldmaxreginfo],(maxreginfo-oldmaxreginfo)*sizeof(treginfo),0);
  449. end;
  450. reginfo[result].subreg:=subreg;
  451. end;
  452. function trgobj.getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;
  453. begin
  454. {$ifdef EXTDEBUG}
  455. if reginfo=nil then
  456. InternalError(2004020901);
  457. {$endif EXTDEBUG}
  458. if defaultsub=R_SUBNONE then
  459. result:=newreg(regtype,getnewreg(R_SUBNONE),R_SUBNONE)
  460. else
  461. result:=newreg(regtype,getnewreg(subreg),subreg);
  462. end;
  463. function trgobj.uses_registers:boolean;
  464. begin
  465. result:=(maxreg>first_imaginary);
  466. end;
  467. procedure trgobj.ungetregister(list:Taasmoutput;r:Tregister);
  468. begin
  469. {$ifdef EXTDEBUG}
  470. if (reginfo=nil) and (getsupreg(r)>=first_imaginary) then
  471. InternalError(2004020901);
  472. {$endif EXTDEBUG}
  473. { Only explicit allocs insert regalloc info }
  474. if getsupreg(r)<first_imaginary then
  475. list.concat(Tai_regalloc.dealloc(r));
  476. end;
  477. procedure trgobj.getexplicitregister(list:Taasmoutput;r:Tregister);
  478. var
  479. supreg:Tsuperregister;
  480. begin
  481. supreg:=getsupreg(r);
  482. if supreg>=first_imaginary then
  483. internalerror(2003121503);
  484. include(used_in_proc,supreg);
  485. list.concat(Tai_regalloc.alloc(r));
  486. end;
  487. procedure trgobj.allocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  488. var i:Tsuperregister;
  489. begin
  490. for i:=0 to first_imaginary-1 do
  491. if i in r then
  492. getexplicitregister(list,newreg(regtype,i,defaultsub));
  493. end;
  494. procedure trgobj.deallocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  495. var i:Tsuperregister;
  496. begin
  497. for i:=0 to first_imaginary-1 do
  498. if i in r then
  499. ungetregister(list,newreg(regtype,i,defaultsub));
  500. end;
  501. procedure trgobj.do_register_allocation(list:Taasmoutput;headertai:tai);
  502. var
  503. spillingcounter:byte;
  504. endspill:boolean;
  505. i:Tsuperregister;
  506. begin
  507. { Insert regalloc info for imaginary registers }
  508. insert_regalloc_info(list,headertai);
  509. ibitmap:=tinterferencebitmap.create;
  510. generate_interference_graph(list,headertai);
  511. { Don't do the real allocation when -sr is passed }
  512. if (cs_no_regalloc in aktglobalswitches) then
  513. exit;
  514. {Do register allocation.}
  515. spillingcounter:=0;
  516. repeat
  517. prepare_colouring;
  518. colour_registers;
  519. epilogue_colouring;
  520. endspill:=true;
  521. if spillednodes.length<>0 then
  522. begin
  523. inc(spillingcounter);
  524. if spillingcounter>maxspillingcounter then
  525. internalerror(200309041);
  526. endspill:=not spill_registers(list,headertai);
  527. end;
  528. until endspill;
  529. ibitmap.free;
  530. translate_registers(list);
  531. dispose_reginfo;
  532. end;
  533. procedure trgobj.add_constraints(reg:Tregister);
  534. begin
  535. end;
  536. procedure trgobj.add_edge(u,v:Tsuperregister);
  537. {This procedure will add an edge to the virtual interference graph.}
  538. procedure addadj(u,v:Tsuperregister);
  539. begin
  540. with reginfo[u] do
  541. begin
  542. if adjlist=nil then
  543. new(adjlist,init);
  544. adjlist^.add(v);
  545. end;
  546. end;
  547. begin
  548. if (u<>v) and not(ibitmap[v,u]) then
  549. begin
  550. ibitmap[v,u]:=true;
  551. ibitmap[u,v]:=true;
  552. {Precoloured nodes are not stored in the interference graph.}
  553. if (u>=first_imaginary) then
  554. addadj(u,v);
  555. if (v>=first_imaginary) then
  556. addadj(v,u);
  557. end;
  558. end;
  559. procedure trgobj.add_edges_used(u:Tsuperregister);
  560. var i:word;
  561. begin
  562. with live_registers do
  563. if length>0 then
  564. for i:=0 to length-1 do
  565. add_edge(u,buf^[i]);
  566. end;
  567. {$ifdef EXTDEBUG}
  568. procedure trgobj.writegraph(loopidx:longint);
  569. {This procedure writes out the current interference graph in the
  570. register allocator.}
  571. var f:text;
  572. i,j:Tsuperregister;
  573. begin
  574. assign(f,'igraph'+tostr(loopidx));
  575. rewrite(f);
  576. writeln(f,'Interference graph');
  577. writeln(f);
  578. write(f,' ');
  579. for i:=0 to 15 do
  580. for j:=0 to 15 do
  581. write(f,hexstr(i,1));
  582. writeln(f);
  583. write(f,' ');
  584. for i:=0 to 15 do
  585. write(f,'0123456789ABCDEF');
  586. writeln(f);
  587. for i:=0 to maxreg-1 do
  588. begin
  589. write(f,hexstr(i,2):4);
  590. for j:=0 to maxreg-1 do
  591. if ibitmap[i,j] then
  592. write(f,'*')
  593. else
  594. write(f,'-');
  595. writeln(f);
  596. end;
  597. close(f);
  598. end;
  599. {$endif EXTDEBUG}
  600. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  601. begin
  602. with reginfo[u] do
  603. begin
  604. if movelist=nil then
  605. begin
  606. getmem(movelist,sizeof(tmovelistheader)+60*sizeof(pointer));
  607. movelist^.header.maxcount:=60;
  608. movelist^.header.count:=0;
  609. movelist^.header.sorted_until:=0;
  610. end
  611. else
  612. begin
  613. if movelist^.header.count>=movelist^.header.maxcount then
  614. begin
  615. movelist^.header.maxcount:=movelist^.header.maxcount*2;
  616. reallocmem(movelist,sizeof(tmovelistheader)+movelist^.header.maxcount*sizeof(pointer));
  617. end;
  618. end;
  619. movelist^.data[movelist^.header.count]:=data;
  620. inc(movelist^.header.count);
  621. end;
  622. end;
  623. procedure trgobj.add_reg_instruction(instr:Tai;r:tregister);
  624. var
  625. supreg : tsuperregister;
  626. begin
  627. supreg:=getsupreg(r);
  628. if supreg>=first_imaginary then
  629. with reginfo[supreg] do
  630. begin
  631. if not assigned(live_start) then
  632. live_start:=instr;
  633. live_end:=instr;
  634. end;
  635. end;
  636. procedure trgobj.add_move_instruction(instr:Taicpu);
  637. {This procedure notifies a certain as a move instruction so the
  638. register allocator can try to eliminate it.}
  639. var i:Tmoveins;
  640. ssupreg,dsupreg:Tsuperregister;
  641. begin
  642. {$ifdef extdebug}
  643. if (instr.oper[O_MOV_SOURCE]^.typ<>top_reg) or
  644. (instr.oper[O_MOV_DEST]^.typ<>top_reg) then
  645. internalerror(200311291);
  646. {$endif}
  647. i:=Tmoveins.create;
  648. i.moveset:=ms_worklist_moves;
  649. worklist_moves.insert(i);
  650. ssupreg:=getsupreg(instr.oper[O_MOV_SOURCE]^.reg);
  651. add_to_movelist(ssupreg,i);
  652. dsupreg:=getsupreg(instr.oper[O_MOV_DEST]^.reg);
  653. if ssupreg<>dsupreg then
  654. {Avoid adding the same move instruction twice to a single register.}
  655. add_to_movelist(dsupreg,i);
  656. i.x:=ssupreg;
  657. i.y:=dsupreg;
  658. end;
  659. function trgobj.move_related(n:Tsuperregister):boolean;
  660. var i:cardinal;
  661. begin
  662. move_related:=false;
  663. with reginfo[n] do
  664. if movelist<>nil then
  665. with movelist^ do
  666. for i:=0 to header.count-1 do
  667. if Tmoveins(data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  668. begin
  669. move_related:=true;
  670. break;
  671. end;
  672. end;
  673. procedure Trgobj.sort_simplify_worklist;
  674. {Sorts the simplifyworklist by the number of interferences the
  675. registers in it cause. This allows simplify to execute in
  676. constant time.}
  677. var p,h,i,leni,lent:word;
  678. t:Tsuperregister;
  679. adji,adjt:Psuperregisterworklist;
  680. begin
  681. with simplifyworklist do
  682. begin
  683. if length<2 then
  684. exit;
  685. p:=1;
  686. while 2*p<length do
  687. p:=2*p;
  688. while p<>0 do
  689. begin
  690. for h:=p to length-1 do
  691. begin
  692. i:=h;
  693. t:=buf^[i];
  694. adjt:=reginfo[buf^[i]].adjlist;
  695. lent:=0;
  696. if adjt<>nil then
  697. lent:=adjt^.length;
  698. repeat
  699. adji:=reginfo[buf^[i-p]].adjlist;
  700. leni:=0;
  701. if adji<>nil then
  702. leni:=adji^.length;
  703. if leni<=lent then
  704. break;
  705. buf^[i]:=buf^[i-p];
  706. dec(i,p)
  707. until i<p;
  708. buf^[i]:=t;
  709. end;
  710. p:=p shr 1;
  711. end;
  712. end;
  713. end;
  714. procedure trgobj.make_work_list;
  715. var n:Tsuperregister;
  716. begin
  717. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  718. assign it to any of the registers, thus it is significant.}
  719. for n:=first_imaginary to maxreg-1 do
  720. with reginfo[n] do
  721. begin
  722. if adjlist=nil then
  723. degree:=0
  724. else
  725. degree:=adjlist^.length;
  726. if degree>=usable_registers_cnt then
  727. spillworklist.add(n)
  728. else if move_related(n) then
  729. freezeworklist.add(n)
  730. else
  731. simplifyworklist.add(n);
  732. end;
  733. sort_simplify_worklist;
  734. end;
  735. procedure trgobj.prepare_colouring;
  736. var i:word;
  737. begin
  738. make_work_list;
  739. active_moves:=Tlinkedlist.create;
  740. frozen_moves:=Tlinkedlist.create;
  741. coalesced_moves:=Tlinkedlist.create;
  742. constrained_moves:=Tlinkedlist.create;
  743. selectstack.clear;
  744. end;
  745. procedure trgobj.enable_moves(n:Tsuperregister);
  746. var m:Tlinkedlistitem;
  747. i:cardinal;
  748. begin
  749. with reginfo[n] do
  750. if movelist<>nil then
  751. for i:=0 to movelist^.header.count-1 do
  752. begin
  753. m:=movelist^.data[i];
  754. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  755. if Tmoveins(m).moveset=ms_active_moves then
  756. begin
  757. {Move m from the set active_moves to the set worklist_moves.}
  758. active_moves.remove(m);
  759. Tmoveins(m).moveset:=ms_worklist_moves;
  760. worklist_moves.concat(m);
  761. end;
  762. end;
  763. end;
  764. procedure Trgobj.decrement_degree(m:Tsuperregister);
  765. var adj : Psuperregisterworklist;
  766. n : tsuperregister;
  767. d,i : word;
  768. begin
  769. with reginfo[m] do
  770. begin
  771. d:=degree;
  772. if d=0 then
  773. internalerror(200312151);
  774. dec(degree);
  775. if d=usable_registers_cnt then
  776. begin
  777. {Enable moves for m.}
  778. enable_moves(m);
  779. {Enable moves for adjacent.}
  780. adj:=adjlist;
  781. if adj<>nil then
  782. for i:=1 to adj^.length do
  783. begin
  784. n:=adj^.buf^[i-1];
  785. if reginfo[n].flags*[ri_selected,ri_coalesced]<>[] then
  786. enable_moves(n);
  787. end;
  788. {Remove the node from the spillworklist.}
  789. if not spillworklist.delete(m) then
  790. internalerror(200310145);
  791. if move_related(m) then
  792. freezeworklist.add(m)
  793. else
  794. simplifyworklist.add(m);
  795. end;
  796. end;
  797. end;
  798. procedure trgobj.simplify;
  799. var adj : Psuperregisterworklist;
  800. m,n : Tsuperregister;
  801. i : word;
  802. begin
  803. {We take the element with the least interferences out of the
  804. simplifyworklist. Since the simplifyworklist is now sorted, we
  805. no longer need to search, but we can simply take the first element.}
  806. m:=simplifyworklist.get;
  807. {Push it on the selectstack.}
  808. selectstack.add(m);
  809. with reginfo[m] do
  810. begin
  811. include(flags,ri_selected);
  812. adj:=adjlist;
  813. end;
  814. if adj<>nil then
  815. for i:=1 to adj^.length do
  816. begin
  817. n:=adj^.buf^[i-1];
  818. if (n>=first_imaginary) and
  819. (reginfo[n].flags*[ri_selected,ri_coalesced]=[]) then
  820. decrement_degree(n);
  821. end;
  822. end;
  823. function trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  824. begin
  825. while ri_coalesced in reginfo[n].flags do
  826. n:=reginfo[n].alias;
  827. get_alias:=n;
  828. end;
  829. procedure trgobj.add_worklist(u:Tsuperregister);
  830. begin
  831. if (u>=first_imaginary) and
  832. (not move_related(u)) and
  833. (reginfo[u].degree<usable_registers_cnt) then
  834. begin
  835. if not freezeworklist.delete(u) then
  836. internalerror(200308161); {must be found}
  837. simplifyworklist.add(u);
  838. end;
  839. end;
  840. function trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  841. {Check wether u and v should be coalesced. u is precoloured.}
  842. function ok(t,r:Tsuperregister):boolean;
  843. begin
  844. ok:=(t<first_imaginary) or
  845. (reginfo[t].degree<usable_registers_cnt) or
  846. ibitmap[r,t];
  847. end;
  848. var adj : Psuperregisterworklist;
  849. i : word;
  850. n : tsuperregister;
  851. begin
  852. with reginfo[v] do
  853. begin
  854. adjacent_ok:=true;
  855. adj:=adjlist;
  856. if adj<>nil then
  857. for i:=1 to adj^.length do
  858. begin
  859. n:=adj^.buf^[i-1];
  860. if (flags*[ri_coalesced,ri_selected]=[]) and not ok(n,u) then
  861. begin
  862. adjacent_ok:=false;
  863. break;
  864. end;
  865. end;
  866. end;
  867. end;
  868. function trgobj.conservative(u,v:Tsuperregister):boolean;
  869. var adj : Psuperregisterworklist;
  870. done : Tsuperregisterset; {To prevent that we count nodes twice.}
  871. i,k:word;
  872. n : tsuperregister;
  873. begin
  874. k:=0;
  875. supregset_reset(done,false);
  876. with reginfo[u] do
  877. begin
  878. adj:=adjlist;
  879. if adj<>nil then
  880. for i:=1 to adj^.length do
  881. begin
  882. n:=adj^.buf^[i-1];
  883. if flags*[ri_coalesced,ri_selected]=[] then
  884. begin
  885. supregset_include(done,n);
  886. if reginfo[n].degree>=usable_registers_cnt then
  887. inc(k);
  888. end;
  889. end;
  890. end;
  891. adj:=reginfo[v].adjlist;
  892. if adj<>nil then
  893. for i:=1 to adj^.length do
  894. begin
  895. n:=adj^.buf^[i-1];
  896. if not supregset_in(done,n) and
  897. (reginfo[n].degree>=usable_registers_cnt) and
  898. (reginfo[u].flags*[ri_coalesced,ri_selected]=[]) then
  899. inc(k);
  900. end;
  901. conservative:=(k<usable_registers_cnt);
  902. end;
  903. procedure trgobj.combine(u,v:Tsuperregister);
  904. var adj : Psuperregisterworklist;
  905. i,n,p,q:cardinal;
  906. t : tsuperregister;
  907. searched:Tlinkedlistitem;
  908. label l1;
  909. begin
  910. if not freezeworklist.delete(v) then
  911. spillworklist.delete(v);
  912. coalescednodes.add(v);
  913. include(reginfo[v].flags,ri_coalesced);
  914. reginfo[v].alias:=u;
  915. {Combine both movelists. Since the movelists are sets, only add
  916. elements that are not already present. The movelists cannot be
  917. empty by definition; nodes are only coalesced if there is a move
  918. between them. To prevent quadratic time blowup (movelists of
  919. especially machine registers can get very large because of moves
  920. generated during calls) we need to go into disgusting complexity.
  921. (See webtbs/tw2242 for an example that stresses this.)
  922. We want to sort the movelist to be able to search logarithmically.
  923. Unfortunately, sorting the movelist every time before searching
  924. is counter-productive, since the movelist usually grows with a few
  925. items at a time. Therefore, we split the movelist into a sorted
  926. and an unsorted part and search through both. If the unsorted part
  927. becomes too large, we sort.}
  928. if assigned(reginfo[u].movelist) then
  929. begin
  930. {We have to weigh the cost of sorting the list against searching
  931. the cost of the unsorted part. I use factor of 8 here; if the
  932. number of items is less than 8 times the numer of unsorted items,
  933. we'll sort the list.}
  934. with reginfo[u].movelist^ do
  935. if header.count<8*(header.count-header.sorted_until) then
  936. sort_movelist(reginfo[u].movelist);
  937. if assigned(reginfo[v].movelist) then
  938. begin
  939. for n:=0 to reginfo[v].movelist^.header.count-1 do
  940. begin
  941. {Binary search the sorted part of the list.}
  942. searched:=reginfo[v].movelist^.data[n];
  943. p:=0;
  944. q:=reginfo[u].movelist^.header.sorted_until;
  945. i:=0;
  946. if q<>0 then
  947. repeat
  948. i:=(p+q) shr 1;
  949. if ptrint(searched)>ptrint(reginfo[u].movelist^.data[i]) then
  950. p:=i+1
  951. else
  952. q:=i;
  953. until p=q;
  954. with reginfo[u].movelist^ do
  955. if searched<>data[i] then
  956. begin
  957. {Linear search the unsorted part of the list.}
  958. for i:=header.sorted_until+1 to header.count-1 do
  959. if searched=data[i] then
  960. goto l1;
  961. {Not found -> add}
  962. add_to_movelist(u,searched);
  963. l1:
  964. end;
  965. end;
  966. end;
  967. end;
  968. enable_moves(v);
  969. adj:=reginfo[v].adjlist;
  970. if adj<>nil then
  971. for i:=1 to adj^.length do
  972. begin
  973. t:=adj^.buf^[i-1];
  974. with reginfo[t] do
  975. if not(ri_coalesced in flags) then
  976. begin
  977. {t has a connection to v. Since we are adding v to u, we
  978. need to connect t to u. However, beware if t was already
  979. connected to u...}
  980. if (ibitmap[t,u]) and not (ri_selected in flags) then
  981. {... because in that case, we are actually removing an edge
  982. and the degree of t decreases.}
  983. decrement_degree(t)
  984. else
  985. begin
  986. add_edge(t,u);
  987. {We have added an edge to t and u. So their degree increases.
  988. However, v is added to u. That means its neighbours will
  989. no longer point to v, but to u instead. Therefore, only the
  990. degree of u increases.}
  991. if (u>=first_imaginary) and not (ri_selected in flags) then
  992. inc(reginfo[u].degree);
  993. end;
  994. end;
  995. end;
  996. if (reginfo[u].degree>=usable_registers_cnt) and freezeworklist.delete(u) then
  997. spillworklist.add(u);
  998. end;
  999. procedure trgobj.coalesce;
  1000. var m:Tmoveins;
  1001. x,y,u,v:Tsuperregister;
  1002. begin
  1003. m:=Tmoveins(worklist_moves.getfirst);
  1004. x:=get_alias(m.x);
  1005. y:=get_alias(m.y);
  1006. if (y<first_imaginary) then
  1007. begin
  1008. u:=y;
  1009. v:=x;
  1010. end
  1011. else
  1012. begin
  1013. u:=x;
  1014. v:=y;
  1015. end;
  1016. if (u=v) then
  1017. begin
  1018. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1019. coalesced_moves.insert(m);
  1020. add_worklist(u);
  1021. end
  1022. {Do u and v interfere? In that case the move is constrained. Two
  1023. precoloured nodes interfere allways. If v is precoloured, by the above
  1024. code u is precoloured, thus interference...}
  1025. else if (v<first_imaginary) or ibitmap[u,v] then
  1026. begin
  1027. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1028. constrained_moves.insert(m);
  1029. add_worklist(u);
  1030. add_worklist(v);
  1031. end
  1032. {Next test: is it possible and a good idea to coalesce??}
  1033. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  1034. ((u>=first_imaginary) and conservative(u,v)) then
  1035. begin
  1036. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1037. coalesced_moves.insert(m);
  1038. combine(u,v);
  1039. add_worklist(u);
  1040. end
  1041. else
  1042. begin
  1043. m.moveset:=ms_active_moves;
  1044. active_moves.insert(m);
  1045. end;
  1046. end;
  1047. procedure trgobj.freeze_moves(u:Tsuperregister);
  1048. var i:cardinal;
  1049. m:Tlinkedlistitem;
  1050. v,x,y:Tsuperregister;
  1051. begin
  1052. if reginfo[u].movelist<>nil then
  1053. for i:=0 to reginfo[u].movelist^.header.count-1 do
  1054. begin
  1055. m:=reginfo[u].movelist^.data[i];
  1056. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1057. begin
  1058. x:=Tmoveins(m).x;
  1059. y:=Tmoveins(m).y;
  1060. if get_alias(y)=get_alias(u) then
  1061. v:=get_alias(x)
  1062. else
  1063. v:=get_alias(y);
  1064. {Move m from active_moves/worklist_moves to frozen_moves.}
  1065. if Tmoveins(m).moveset=ms_active_moves then
  1066. active_moves.remove(m)
  1067. else
  1068. worklist_moves.remove(m);
  1069. Tmoveins(m).moveset:=ms_frozen_moves;
  1070. frozen_moves.insert(m);
  1071. if (v>=first_imaginary) and not(move_related(v)) and
  1072. (reginfo[v].degree<usable_registers_cnt) then
  1073. begin
  1074. freezeworklist.delete(v);
  1075. simplifyworklist.add(v);
  1076. end;
  1077. end;
  1078. end;
  1079. end;
  1080. procedure trgobj.freeze;
  1081. var n:Tsuperregister;
  1082. begin
  1083. { We need to take a random element out of the freezeworklist. We take
  1084. the last element. Dirty code! }
  1085. n:=freezeworklist.get;
  1086. {Add it to the simplifyworklist.}
  1087. simplifyworklist.add(n);
  1088. freeze_moves(n);
  1089. end;
  1090. procedure trgobj.select_spill;
  1091. var
  1092. n : tsuperregister;
  1093. adj : psuperregisterworklist;
  1094. max,p,i:word;
  1095. begin
  1096. { We must look for the element with the most interferences in the
  1097. spillworklist. This is required because those registers are creating
  1098. the most conflicts and keeping them in a register will not reduce the
  1099. complexity and even can cause the help registers for the spilling code
  1100. to get too much conflicts with the result that the spilling code
  1101. will never converge (PFV) }
  1102. max:=0;
  1103. p:=0;
  1104. with spillworklist do
  1105. begin
  1106. {Safe: This procedure is only called if length<>0}
  1107. for i:=0 to length-1 do
  1108. begin
  1109. adj:=reginfo[buf^[i]].adjlist;
  1110. if assigned(adj) and (adj^.length>max) then
  1111. begin
  1112. p:=i;
  1113. max:=adj^.length;
  1114. end;
  1115. end;
  1116. n:=buf^[p];
  1117. deleteidx(p);
  1118. end;
  1119. simplifyworklist.add(n);
  1120. freeze_moves(n);
  1121. end;
  1122. procedure trgobj.assign_colours;
  1123. {Assign_colours assigns the actual colours to the registers.}
  1124. var adj : Psuperregisterworklist;
  1125. i,j,k : word;
  1126. n,a,c : Tsuperregister;
  1127. adj_colours,
  1128. colourednodes : Tsuperregisterset;
  1129. found : boolean;
  1130. begin
  1131. spillednodes.clear;
  1132. {Reset colours}
  1133. for n:=0 to maxreg-1 do
  1134. reginfo[n].colour:=n;
  1135. {Colour the cpu registers...}
  1136. supregset_reset(colourednodes,false);
  1137. for n:=0 to first_imaginary-1 do
  1138. supregset_include(colourednodes,n);
  1139. {Now colour the imaginary registers on the select-stack.}
  1140. for i:=selectstack.length downto 1 do
  1141. begin
  1142. n:=selectstack.buf^[i-1];
  1143. {Create a list of colours that we cannot assign to n.}
  1144. supregset_reset(adj_colours,false);
  1145. adj:=reginfo[n].adjlist;
  1146. if adj<>nil then
  1147. for j:=0 to adj^.length-1 do
  1148. begin
  1149. a:=get_alias(adj^.buf^[j]);
  1150. if supregset_in(colourednodes,a) then
  1151. supregset_include(adj_colours,reginfo[a].colour);
  1152. end;
  1153. supregset_include(adj_colours,RS_STACK_POINTER_REG);
  1154. {Assume a spill by default...}
  1155. found:=false;
  1156. {Search for a colour not in this list.}
  1157. for k:=0 to usable_registers_cnt-1 do
  1158. begin
  1159. c:=usable_registers[k];
  1160. if not(supregset_in(adj_colours,c)) then
  1161. begin
  1162. reginfo[n].colour:=c;
  1163. found:=true;
  1164. supregset_include(colourednodes,n);
  1165. include(used_in_proc,c);
  1166. break;
  1167. end;
  1168. end;
  1169. if not found then
  1170. spillednodes.add(n);
  1171. end;
  1172. {Finally colour the nodes that were coalesced.}
  1173. for i:=1 to coalescednodes.length do
  1174. begin
  1175. n:=coalescednodes.buf^[i-1];
  1176. k:=get_alias(n);
  1177. reginfo[n].colour:=reginfo[k].colour;
  1178. if reginfo[k].colour<maxcpuregister then
  1179. include(used_in_proc,reginfo[k].colour);
  1180. end;
  1181. {$ifdef ra_debug}
  1182. if aktfilepos.line=179 then
  1183. begin
  1184. writeln('colourlist');
  1185. for i:=0 to maxreg-1 do
  1186. writeln(i:4,' ',reginfo[i].colour:4)
  1187. end;
  1188. {$endif ra_debug}
  1189. end;
  1190. procedure trgobj.colour_registers;
  1191. begin
  1192. repeat
  1193. if simplifyworklist.length<>0 then
  1194. simplify
  1195. else if not(worklist_moves.empty) then
  1196. coalesce
  1197. else if freezeworklist.length<>0 then
  1198. freeze
  1199. else if spillworklist.length<>0 then
  1200. select_spill;
  1201. until (simplifyworklist.length=0) and
  1202. worklist_moves.empty and
  1203. (freezeworklist.length=0) and
  1204. (spillworklist.length=0);
  1205. assign_colours;
  1206. end;
  1207. procedure trgobj.epilogue_colouring;
  1208. var
  1209. i : Tsuperregister;
  1210. begin
  1211. worklist_moves.clear;
  1212. active_moves.destroy;
  1213. active_moves:=nil;
  1214. frozen_moves.destroy;
  1215. frozen_moves:=nil;
  1216. coalesced_moves.destroy;
  1217. coalesced_moves:=nil;
  1218. constrained_moves.destroy;
  1219. constrained_moves:=nil;
  1220. for i:=0 to maxreg-1 do
  1221. with reginfo[i] do
  1222. if movelist<>nil then
  1223. begin
  1224. dispose(movelist);
  1225. movelist:=nil;
  1226. end;
  1227. end;
  1228. procedure trgobj.clear_interferences(u:Tsuperregister);
  1229. {Remove node u from the interference graph and remove all collected
  1230. move instructions it is associated with.}
  1231. var i : word;
  1232. v : Tsuperregister;
  1233. adj,adj2 : Psuperregisterworklist;
  1234. begin
  1235. adj:=reginfo[u].adjlist;
  1236. if adj<>nil then
  1237. begin
  1238. for i:=1 to adj^.length do
  1239. begin
  1240. v:=adj^.buf^[i-1];
  1241. {Remove (u,v) and (v,u) from bitmap.}
  1242. ibitmap[u,v]:=false;
  1243. ibitmap[v,u]:=false;
  1244. {Remove (v,u) from adjacency list.}
  1245. adj2:=reginfo[v].adjlist;
  1246. if adj2<>nil then
  1247. begin
  1248. adj2^.delete(u);
  1249. if adj2^.length=0 then
  1250. begin
  1251. dispose(adj2,done);
  1252. reginfo[v].adjlist:=nil;
  1253. end;
  1254. end;
  1255. end;
  1256. {Remove ( u,* ) from adjacency list.}
  1257. dispose(adj,done);
  1258. reginfo[u].adjlist:=nil;
  1259. end;
  1260. end;
  1261. procedure trgobj.getregisterinline(list:Taasmoutput;
  1262. position:Tai;subreg:Tsubregister;var result:Tregister);
  1263. var p:Tsuperregister;
  1264. r:Tregister;
  1265. begin
  1266. p:=getnewreg(subreg);
  1267. live_registers.add(p);
  1268. r:=newreg(regtype,p,subreg);
  1269. if position=nil then
  1270. list.insert(Tai_regalloc.alloc(r))
  1271. else
  1272. list.insertafter(Tai_regalloc.alloc(r),position);
  1273. add_edges_used(p);
  1274. add_constraints(r);
  1275. result:=r;
  1276. end;
  1277. procedure trgobj.ungetregisterinline(list:Taasmoutput;
  1278. position:Tai;r:Tregister);
  1279. var supreg:Tsuperregister;
  1280. begin
  1281. supreg:=getsupreg(r);
  1282. live_registers.delete(supreg);
  1283. if position=nil then
  1284. list.insert(Tai_regalloc.dealloc(r))
  1285. else
  1286. list.insertafter(Tai_regalloc.dealloc(r),position);
  1287. end;
  1288. procedure trgobj.insert_regalloc_info(list:Taasmoutput;headertai:tai);
  1289. var
  1290. supreg : tsuperregister;
  1291. p : tai;
  1292. r : tregister;
  1293. begin
  1294. { Insert regallocs for all imaginary registers }
  1295. for supreg:=first_imaginary to maxreg-1 do
  1296. with reginfo[supreg] do
  1297. begin
  1298. r:=newreg(regtype,supreg,subreg);
  1299. if assigned(live_start) then
  1300. begin
  1301. {$ifdef EXTDEBUG}
  1302. if live_start=live_end then
  1303. Comment(V_Warning,'Register '+std_regname(r)+' is only used once');
  1304. {$endif EXTDEBUG}
  1305. list.insertbefore(Tai_regalloc.alloc(r),live_start);
  1306. { Insert live end deallocation before reg allocations
  1307. to reduce conflicts }
  1308. p:=live_end;
  1309. while assigned(p) and
  1310. assigned(p.previous) and
  1311. (tai(p.previous).typ=ait_regalloc) and
  1312. (tai_regalloc(p.previous).ratype=ra_alloc) and
  1313. (tai_regalloc(p.previous).reg<>r) do
  1314. p:=tai(p.previous);
  1315. list.insertbefore(Tai_regalloc.dealloc(r),p);
  1316. end
  1317. {$ifdef EXTDEBUG}
  1318. else
  1319. Comment(V_Warning,'Register '+std_regname(r)+' not used');
  1320. {$endif EXTDEBUG}
  1321. end;
  1322. end;
  1323. procedure trgobj.add_cpu_interferences(p : tai);
  1324. begin
  1325. end;
  1326. procedure trgobj.generate_interference_graph(list:Taasmoutput;headertai:tai);
  1327. var
  1328. p : tai;
  1329. i : integer;
  1330. supreg : tsuperregister;
  1331. begin
  1332. { All allocations are available. Now we can generate the
  1333. interference graph. Walk through all instructions, we can
  1334. start with the headertai, because before the header tai is
  1335. only symbols. }
  1336. live_registers.clear;
  1337. p:=headertai;
  1338. while assigned(p) do
  1339. begin
  1340. if p.typ=ait_regalloc then
  1341. with Tai_regalloc(p) do
  1342. begin
  1343. if (getregtype(reg)=regtype) then
  1344. begin
  1345. supreg:=getsupreg(reg);
  1346. case ratype of
  1347. ra_alloc :
  1348. begin
  1349. live_registers.add(supreg);
  1350. add_edges_used(supreg);
  1351. end;
  1352. ra_dealloc :
  1353. begin
  1354. live_registers.delete(supreg);
  1355. add_edges_used(supreg);
  1356. end;
  1357. end;
  1358. { constraints needs always to be updated }
  1359. add_constraints(reg);
  1360. end;
  1361. end;
  1362. add_cpu_interferences(p);
  1363. p:=Tai(p.next);
  1364. end;
  1365. {$ifdef EXTDEBUG}
  1366. if live_registers.length>0 then
  1367. begin
  1368. for i:=0 to live_registers.length-1 do
  1369. begin
  1370. { Only report for imaginary registers }
  1371. if live_registers.buf^[i]>=first_imaginary then
  1372. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,live_registers.buf^[i],defaultsub))+' not released');
  1373. end;
  1374. end;
  1375. {$endif}
  1376. end;
  1377. procedure Trgobj.translate_registers(list:taasmoutput);
  1378. var
  1379. hp,p,q:Tai;
  1380. i:shortint;
  1381. {$ifdef arm}
  1382. so:pshifterop;
  1383. {$endif arm}
  1384. begin
  1385. { Leave when no imaginary registers are used }
  1386. if maxreg<=first_imaginary then
  1387. exit;
  1388. p:=Tai(list.first);
  1389. while assigned(p) do
  1390. begin
  1391. case p.typ of
  1392. ait_regalloc:
  1393. with Tai_regalloc(p) do
  1394. begin
  1395. if (getregtype(reg)=regtype) then
  1396. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1397. {
  1398. Remove sequences of release and
  1399. allocation of the same register like:
  1400. # Register X released
  1401. # Register X allocated
  1402. }
  1403. if assigned(previous) and
  1404. (Tai(previous).typ=ait_regalloc) and
  1405. (Tai_regalloc(previous).reg=reg) and
  1406. { deallocation,allocation }
  1407. { note: do not remove allocation,deallocation, those }
  1408. { do have a real meaning }
  1409. (not(Tai_regalloc(previous).ratype=ra_alloc) and (ratype=ra_alloc)) then
  1410. begin
  1411. q:=Tai(next);
  1412. hp:=tai(previous);
  1413. list.remove(hp);
  1414. hp.free;
  1415. list.remove(p);
  1416. p.free;
  1417. p:=q;
  1418. continue;
  1419. end;
  1420. end;
  1421. ait_instruction:
  1422. with Taicpu_abstract(p) do
  1423. begin
  1424. for i:=0 to ops-1 do
  1425. with oper[i]^ do
  1426. case typ of
  1427. Top_reg:
  1428. if (getregtype(reg)=regtype) then
  1429. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1430. Top_ref:
  1431. begin
  1432. if regtype=R_INTREGISTER then
  1433. with ref^ do
  1434. begin
  1435. if base<>NR_NO then
  1436. setsupreg(base,reginfo[getsupreg(base)].colour);
  1437. if index<>NR_NO then
  1438. setsupreg(index,reginfo[getsupreg(index)].colour);
  1439. end;
  1440. end;
  1441. {$ifdef arm}
  1442. Top_shifterop:
  1443. begin
  1444. so:=shifterop;
  1445. if so^.rs<>NR_NO then
  1446. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1447. end;
  1448. {$endif arm}
  1449. end;
  1450. { Maybe the operation can be removed when
  1451. it is a move and both arguments are the same }
  1452. if is_same_reg_move(regtype) then
  1453. begin
  1454. q:=Tai(p.next);
  1455. list.remove(p);
  1456. p.free;
  1457. p:=q;
  1458. continue;
  1459. end;
  1460. end;
  1461. end;
  1462. p:=Tai(p.next);
  1463. end;
  1464. end;
  1465. function trgobj.get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister):Tai;
  1466. var
  1467. back : Tsuperregisterworklist;
  1468. supreg : tsuperregister;
  1469. begin
  1470. back.copyfrom(live_registers);
  1471. result:=p;
  1472. while (p<>nil) and (p.typ=ait_regalloc) do
  1473. begin
  1474. supreg:=getsupreg(Tai_regalloc(p).reg);
  1475. {Rewind the register allocation.}
  1476. if (Tai_regalloc(p).ratype=ra_alloc) then
  1477. live_registers.delete(supreg)
  1478. else
  1479. begin
  1480. live_registers.add(supreg);
  1481. if supreg=huntfor1 then
  1482. begin
  1483. get_insert_pos:=Tai(p.previous);
  1484. back.done;
  1485. back.copyfrom(live_registers);
  1486. end;
  1487. if supreg=huntfor2 then
  1488. begin
  1489. get_insert_pos:=Tai(p.previous);
  1490. back.done;
  1491. back.copyfrom(live_registers);
  1492. end;
  1493. if supreg=huntfor3 then
  1494. begin
  1495. get_insert_pos:=Tai(p.previous);
  1496. back.done;
  1497. back.copyfrom(live_registers);
  1498. end;
  1499. end;
  1500. p:=Tai(p.previous);
  1501. end;
  1502. live_registers.done;
  1503. live_registers:=back;
  1504. end;
  1505. procedure trgobj.forward_allocation(pfrom,pto:Tai);
  1506. var
  1507. p : tai;
  1508. begin
  1509. {Forward the register allocation again.}
  1510. p:=pfrom;
  1511. while (p<>pto) do
  1512. begin
  1513. if p.typ<>ait_regalloc then
  1514. internalerror(200305311);
  1515. case Tai_regalloc(p).ratype of
  1516. ra_alloc :
  1517. live_registers.add(getsupreg(Tai_regalloc(p).reg));
  1518. ra_dealloc :
  1519. live_registers.delete(getsupreg(Tai_regalloc(p).reg));
  1520. end;
  1521. p:=Tai(p.next);
  1522. end;
  1523. end;
  1524. function trgobj.spill_registers(list:Taasmoutput;headertai:tai):boolean;
  1525. { Returns true if any help registers have been used }
  1526. var
  1527. i : word;
  1528. t : tsuperregister;
  1529. p,q : Tai;
  1530. regs_to_spill_set:Tsuperregisterset;
  1531. spill_temps : ^Tspill_temp_list;
  1532. supreg : tsuperregister;
  1533. templist : taasmoutput;
  1534. begin
  1535. spill_registers:=false;
  1536. live_registers.clear;
  1537. for i:=first_imaginary to maxreg-1 do
  1538. exclude(reginfo[i].flags,ri_selected);
  1539. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1540. supregset_reset(regs_to_spill_set,false);
  1541. { Allocate temps and insert in front of the list }
  1542. templist:=taasmoutput.create;
  1543. {Safe: this procedure is only called if there are spilled nodes.}
  1544. with spillednodes do
  1545. for i:=0 to length-1 do
  1546. begin
  1547. t:=buf^[i];
  1548. {Alternative representation.}
  1549. supregset_include(regs_to_spill_set,t);
  1550. {Clear all interferences of the spilled register.}
  1551. clear_interferences(t);
  1552. {Get a temp for the spilled register}
  1553. tg.gettemp(templist,4,tt_noreuse,spill_temps^[t]);
  1554. end;
  1555. list.insertlistafter(headertai,templist);
  1556. templist.free;
  1557. { Walk through all instructions, we can start with the headertai,
  1558. because before the header tai is only symbols }
  1559. p:=headertai;
  1560. while assigned(p) do
  1561. begin
  1562. case p.typ of
  1563. ait_regalloc:
  1564. with Tai_regalloc(p) do
  1565. begin
  1566. if (getregtype(reg)=regtype) then
  1567. begin
  1568. {A register allocation of a spilled register can be removed.}
  1569. supreg:=getsupreg(reg);
  1570. if supregset_in(regs_to_spill_set,supreg) then
  1571. begin
  1572. q:=Tai(p.next);
  1573. list.remove(p);
  1574. p.free;
  1575. p:=q;
  1576. continue;
  1577. end
  1578. else
  1579. begin
  1580. case ratype of
  1581. ra_alloc :
  1582. live_registers.add(supreg);
  1583. ra_dealloc :
  1584. live_registers.delete(supreg);
  1585. end;
  1586. end;
  1587. end;
  1588. end;
  1589. ait_instruction:
  1590. with Taicpu_abstract(p) do
  1591. begin
  1592. aktfilepos:=fileinfo;
  1593. if instr_spill_register(list,Taicpu_abstract(p),regs_to_spill_set,spill_temps^) then
  1594. spill_registers:=true;
  1595. end;
  1596. end;
  1597. p:=Tai(p.next);
  1598. end;
  1599. aktfilepos:=current_procinfo.exitpos;
  1600. {Safe: this procedure is only called if there are spilled nodes.}
  1601. with spillednodes do
  1602. for i:=0 to length-1 do
  1603. tg.ungettemp(list,spill_temps^[buf^[i]]);
  1604. freemem(spill_temps);
  1605. end;
  1606. procedure Trgobj.do_spill_read(list:Taasmoutput;instr:Taicpu_abstract;
  1607. pos:Tai;regidx:word;
  1608. const spilltemplist:Tspill_temp_list;
  1609. const regs:Tspillregsinfo);
  1610. var helpins:Tai;
  1611. begin
  1612. with regs[regidx] do
  1613. begin
  1614. helpins:=instr.spilling_create_load(spilltemplist[orgreg],tempreg);
  1615. if pos=nil then
  1616. list.insertafter(helpins,list.first)
  1617. else
  1618. list.insertafter(helpins,pos.next);
  1619. ungetregisterinline(list,instr,tempreg);
  1620. forward_allocation(tai(helpins.next),instr);
  1621. end;
  1622. end;
  1623. procedure Trgobj.do_spill_written(list:Taasmoutput;instr:Taicpu_abstract;
  1624. pos:Tai;regidx:word;
  1625. const spilltemplist:Tspill_temp_list;
  1626. const regs:Tspillregsinfo);
  1627. var helpins:Tai;
  1628. begin
  1629. with regs[regidx] do
  1630. begin
  1631. helpins:=instr.spilling_create_store(tempreg,spilltemplist[orgreg]);
  1632. list.insertafter(helpins,instr);
  1633. ungetregisterinline(list,helpins,tempreg);
  1634. end;
  1635. end;
  1636. procedure Trgobj.do_spill_readwritten(list:Taasmoutput;instr:Taicpu_abstract;
  1637. pos:Tai;regidx:word;
  1638. const spilltemplist:Tspill_temp_list;
  1639. const regs:Tspillregsinfo);
  1640. var helpins1,helpins2:Tai;
  1641. begin
  1642. with regs[regidx] do
  1643. begin
  1644. helpins1:=instr.spilling_create_load(spilltemplist[orgreg],tempreg);
  1645. if pos=nil then
  1646. list.insertafter(helpins1,list.first)
  1647. else
  1648. list.insertafter(helpins1,pos.next);
  1649. helpins2:=instr.spilling_create_store(tempreg,spilltemplist[orgreg]);
  1650. list.insertafter(helpins2,instr);
  1651. ungetregisterinline(list,helpins2,tempreg);
  1652. forward_allocation(tai(helpins1.next),instr);
  1653. end;
  1654. end;
  1655. function trgobj.instr_spill_register(list:Taasmoutput;
  1656. instr:taicpu_abstract;
  1657. const r:Tsuperregisterset;
  1658. const spilltemplist:Tspill_temp_list): boolean;
  1659. var
  1660. counter, regindex: longint;
  1661. pos: tai;
  1662. regs: tspillregsinfo;
  1663. spilled: boolean;
  1664. procedure addreginfo(reg: tsuperregister; operation: topertype);
  1665. var
  1666. i, tmpindex: longint;
  1667. begin
  1668. tmpindex := regindex;
  1669. // did we already encounter this register?
  1670. for i := 0 to pred(regindex) do
  1671. if (regs[i].orgreg = reg) then
  1672. begin
  1673. tmpindex := i;
  1674. break;
  1675. end;
  1676. if tmpindex > high(regs) then
  1677. internalerror(2003120301);
  1678. regs[tmpindex].orgreg := reg;
  1679. if supregset_in(r,reg) then
  1680. begin
  1681. // add/update info on this register
  1682. regs[tmpindex].mustbespilled := true;
  1683. case operation of
  1684. operand_read:
  1685. regs[tmpindex].regread := true;
  1686. operand_write:
  1687. regs[tmpindex].regwritten := true;
  1688. operand_readwrite:
  1689. begin
  1690. regs[tmpindex].regread := true;
  1691. regs[tmpindex].regwritten := true;
  1692. end;
  1693. end;
  1694. spilled := true;
  1695. end;
  1696. inc(regindex,ord(regindex=tmpindex));
  1697. end;
  1698. procedure tryreplacereg(var reg: tregister);
  1699. var
  1700. i: longint;
  1701. supreg: tsuperregister;
  1702. begin
  1703. if (getregtype(reg) = R_INTREGISTER) then
  1704. begin
  1705. supreg := getsupreg(reg);
  1706. for i := 0 to pred(regindex) do
  1707. if (regs[i].mustbespilled) and
  1708. (regs[i].orgreg = supreg) then
  1709. begin
  1710. reg := regs[i].tempreg;
  1711. break;
  1712. end;
  1713. end;
  1714. end;
  1715. begin
  1716. result := false;
  1717. fillchar(regs,sizeof(regs),0);
  1718. for counter := low(regs) to high(regs) do
  1719. regs[counter].orgreg := RS_INVALID;
  1720. spilled := false;
  1721. regindex := 0;
  1722. { check whether and if so which and how (read/written) this instructions contains
  1723. registers that must be spilled }
  1724. for counter := 0 to instr.ops-1 do
  1725. with instr.oper[counter]^ do
  1726. begin
  1727. case typ of
  1728. top_reg:
  1729. begin
  1730. if (getregtype(reg) = regtype) then
  1731. addreginfo(getsupreg(reg),instr.spilling_get_operation_type(counter));
  1732. end;
  1733. top_ref:
  1734. begin
  1735. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1736. with ref^ do
  1737. begin
  1738. if (base <> NR_NO) then
  1739. addreginfo(getsupreg(base),operand_read);
  1740. if (index <> NR_NO) then
  1741. addreginfo(getsupreg(index),operand_read);
  1742. end;
  1743. end;
  1744. {$ifdef ARM}
  1745. top_shifterop:
  1746. begin
  1747. if shifterop^.rs<>NR_NO then
  1748. addreginfo(getsupreg(shifterop^.rs),operand_read);
  1749. end;
  1750. {$endif ARM}
  1751. end;
  1752. end;
  1753. { if no spilling for this instruction we can leave }
  1754. if not spilled then
  1755. exit;
  1756. { generate the spilling code }
  1757. result := true;
  1758. for counter := 0 to pred(regindex) do
  1759. with regs[counter] do
  1760. begin
  1761. if mustbespilled then
  1762. begin
  1763. pos:=get_insert_pos(Tai(instr.previous),regs[0].orgreg,regs[1].orgreg,regs[2].orgreg);
  1764. getregisterinline(list,pos,defaultsub,tempreg);
  1765. if regread then
  1766. if regwritten then
  1767. do_spill_readwritten(list,instr,pos,counter,spilltemplist,regs)
  1768. else
  1769. do_spill_read(list,instr,pos,counter,spilltemplist,regs)
  1770. else
  1771. do_spill_written(list,instr,pos,counter,spilltemplist,regs)
  1772. end;
  1773. end;
  1774. { substitute registers }
  1775. for counter := 0 to instr.ops-1 do
  1776. with instr.oper[counter]^ do
  1777. begin
  1778. case typ of
  1779. top_reg:
  1780. begin
  1781. tryreplacereg(reg);
  1782. end;
  1783. top_ref:
  1784. begin
  1785. tryreplacereg(ref^.base);
  1786. tryreplacereg(ref^.index);
  1787. end;
  1788. {$ifdef ARM}
  1789. top_shifterop:
  1790. begin
  1791. tryreplacereg(shifterop^.rs);
  1792. end;
  1793. {$endif ARM}
  1794. end;
  1795. end;
  1796. end;
  1797. end.
  1798. {
  1799. $Log$
  1800. Revision 1.126 2004-05-22 23:34:28 peter
  1801. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  1802. Revision 1.125 2004/04/26 19:57:50 jonas
  1803. * do not remove "allocation,deallocation" pairs, as those are important
  1804. for the optimizer
  1805. Revision 1.124 2004/03/14 22:50:04 peter
  1806. * rewrote add_to_movelist, it now uses a field to store the number
  1807. of allocated entries. Also made it using less hardcoded values
  1808. Revision 1.123 2004/03/14 20:06:17 peter
  1809. * check if movelist is valid
  1810. Revision 1.122 2004/02/12 15:54:03 peter
  1811. * make extcycle is working again
  1812. Revision 1.121 2004/02/09 20:12:23 olle
  1813. + check that register allocation is not made at the wrong moment
  1814. Revision 1.120 2004/02/08 23:10:21 jonas
  1815. * taicpu.is_same_reg_move() now gets a regtype parameter so it only
  1816. removes moves of that particular register type. This is necessary so
  1817. we don't remove the live_start instruction of a register before it
  1818. has been processed
  1819. Revision 1.119 2004/02/08 14:26:28 daniel
  1820. * Register allocator speed boost
  1821. Revision 1.118 2004/02/07 23:28:34 daniel
  1822. * Take advantage of our new with statement optimization
  1823. Revision 1.117 2004/02/06 13:34:46 daniel
  1824. * Some changes to better accomodate very large movelists
  1825. * movelist resizing now exponential (avoids heap fragmentation, saves
  1826. 300 kb memory in make cycle)
  1827. * Trgobj.combine hand-optimized (still too slow)
  1828. Revision 1.116 2004/01/28 22:16:31 peter
  1829. * more record alignment fixes
  1830. Revision 1.115 2004/01/26 17:40:11 florian
  1831. * made DoSpill* overrideable
  1832. + add_cpu_interferences added
  1833. Revision 1.114 2004/01/26 16:12:28 daniel
  1834. * reginfo now also only allocated during register allocation
  1835. * third round of gdb cleanups: kick out most of concatstabto
  1836. Revision 1.112 2004/01/12 16:37:59 peter
  1837. * moved spilling code from taicpu to rg
  1838. Revision 1.109 2003/12/26 14:02:30 peter
  1839. * sparc updates
  1840. * use registertype in spill_register
  1841. Revision 1.108 2003/12/22 23:09:34 peter
  1842. * only report unreleased imaginary registers
  1843. Revision 1.107 2003/12/22 22:13:46 peter
  1844. * made decrease_degree working, but not really fixed
  1845. Revision 1.106 2003/12/18 17:06:21 florian
  1846. * arm compiler compilation fixed
  1847. Revision 1.105 2003/12/17 21:59:05 peter
  1848. * don't insert dealloc before alloc of the same register
  1849. Revision 1.104 2003/12/16 09:41:44 daniel
  1850. * Automatic conversion from integer constants to pointer constants is no
  1851. longer done except in Delphi mode
  1852. Revision 1.103 2003/12/15 21:25:49 peter
  1853. * reg allocations for imaginary register are now inserted just
  1854. before reg allocation
  1855. * tregister changed to enum to allow compile time check
  1856. * fixed several tregister-tsuperregister errors
  1857. Revision 1.102 2003/12/15 16:37:47 daniel
  1858. * More microoptimizations
  1859. Revision 1.101 2003/12/15 15:58:58 peter
  1860. * fix statedebug compile
  1861. Revision 1.100 2003/12/14 20:24:28 daniel
  1862. * Register allocator speed optimizations
  1863. - Worklist no longer a ringbuffer
  1864. - No find operations are left
  1865. - Simplify now done in constant time
  1866. - unusedregs is now a Tsuperregisterworklist
  1867. - Microoptimizations
  1868. Revision 1.99 2003/12/12 17:16:17 peter
  1869. * rg[tregistertype] added in tcg
  1870. Revision 1.98 2003/12/04 23:27:32 peter
  1871. * remove redundant calls to add_edge_used
  1872. Revision 1.97 2003/11/29 17:36:41 peter
  1873. * check for add_move_instruction
  1874. Revision 1.96 2003/11/24 15:17:37 florian
  1875. * changed some types to prevend range check errors
  1876. Revision 1.95 2003/11/10 19:05:50 peter
  1877. * fixed alias/colouring > 255
  1878. Revision 1.94 2003/11/07 15:58:32 florian
  1879. * Florian's culmutative nr. 1; contains:
  1880. - invalid calling conventions for a certain cpu are rejected
  1881. - arm softfloat calling conventions
  1882. - -Sp for cpu dependend code generation
  1883. - several arm fixes
  1884. - remaining code for value open array paras on heap
  1885. Revision 1.93 2003/10/30 16:22:40 peter
  1886. * call firstpass before allocation and codegeneration is started
  1887. * move leftover code from pass_2.generatecode() to psub
  1888. Revision 1.92 2003/10/29 21:29:14 jonas
  1889. * some ALLOWDUPREG improvements
  1890. Revision 1.91 2003/10/21 15:15:36 peter
  1891. * taicpu_abstract.oper[] changed to pointers
  1892. Revision 1.90 2003/10/19 12:36:36 florian
  1893. * improved speed; reduced memory usage of the interference bitmap
  1894. Revision 1.89 2003/10/19 01:34:30 florian
  1895. * some ppc stuff fixed
  1896. * memory leak fixed
  1897. Revision 1.88 2003/10/18 15:41:26 peter
  1898. * made worklists dynamic in size
  1899. Revision 1.87 2003/10/17 16:16:08 peter
  1900. * fixed last commit
  1901. Revision 1.86 2003/10/17 15:25:18 florian
  1902. * fixed more ppc stuff
  1903. Revision 1.85 2003/10/17 14:38:32 peter
  1904. * 64k registers supported
  1905. * fixed some memory leaks
  1906. Revision 1.84 2003/10/11 16:06:42 florian
  1907. * fixed some MMX<->SSE
  1908. * started to fix ppc, needs an overhaul
  1909. + stabs info improve for spilling, not sure if it works correctly/completly
  1910. - MMX_SUPPORT removed from Makefile.fpc
  1911. Revision 1.83 2003/10/10 17:48:14 peter
  1912. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1913. * tregisteralloctor renamed to trgobj
  1914. * removed rgobj from a lot of units
  1915. * moved location_* and reference_* to cgobj
  1916. * first things for mmx register allocation
  1917. Revision 1.82 2003/10/09 21:31:37 daniel
  1918. * Register allocator splitted, ans abstract now
  1919. Revision 1.81 2003/10/01 20:34:49 peter
  1920. * procinfo unit contains tprocinfo
  1921. * cginfo renamed to cgbase
  1922. * moved cgmessage to verbose
  1923. * fixed ppc and sparc compiles
  1924. Revision 1.80 2003/09/30 19:54:42 peter
  1925. * reuse registers with the least conflicts
  1926. Revision 1.79 2003/09/29 20:58:56 peter
  1927. * optimized releasing of registers
  1928. Revision 1.78 2003/09/28 13:41:12 peter
  1929. * return reg 255 when allowdupreg is defined
  1930. Revision 1.77 2003/09/25 16:19:32 peter
  1931. * fix filepositions
  1932. * insert spill temp allocations at the start of the proc
  1933. Revision 1.76 2003/09/16 16:17:01 peter
  1934. * varspez in calls to push_addr_param
  1935. Revision 1.75 2003/09/12 19:07:42 daniel
  1936. * Fixed fast spilling functionality by re-adding the code that initializes
  1937. precoloured nodes to degree 255. I would like to play hangman on the one
  1938. who removed that code.
  1939. Revision 1.74 2003/09/11 11:54:59 florian
  1940. * improved arm code generation
  1941. * move some protected and private field around
  1942. * the temp. register for register parameters/arguments are now released
  1943. before the move to the parameter register is done. This improves
  1944. the code in a lot of cases.
  1945. Revision 1.73 2003/09/09 20:59:27 daniel
  1946. * Adding register allocation order
  1947. Revision 1.72 2003/09/09 15:55:44 peter
  1948. * use register with least interferences in spillregister
  1949. Revision 1.71 2003/09/07 22:09:35 peter
  1950. * preparations for different default calling conventions
  1951. * various RA fixes
  1952. Revision 1.70 2003/09/03 21:06:45 peter
  1953. * fixes for FPU register allocation
  1954. Revision 1.69 2003/09/03 15:55:01 peter
  1955. * NEWRA branch merged
  1956. Revision 1.68 2003/09/03 11:18:37 florian
  1957. * fixed arm concatcopy
  1958. + arm support in the common compiler sources added
  1959. * moved some generic cg code around
  1960. + tfputype added
  1961. * ...
  1962. Revision 1.67.2.5 2003/08/31 20:44:07 peter
  1963. * fixed getexplicitregisterint tregister value
  1964. Revision 1.67.2.4 2003/08/31 20:40:50 daniel
  1965. * Fixed add_edges_used
  1966. Revision 1.67.2.3 2003/08/29 17:28:59 peter
  1967. * next batch of updates
  1968. Revision 1.67.2.2 2003/08/28 18:35:08 peter
  1969. * tregister changed to cardinal
  1970. Revision 1.67.2.1 2003/08/27 19:55:54 peter
  1971. * first tregister patch
  1972. Revision 1.67 2003/08/23 10:46:21 daniel
  1973. * Register allocator bugfix for h2pas
  1974. Revision 1.66 2003/08/17 16:59:20 jonas
  1975. * fixed regvars so they work with newra (at least for ppc)
  1976. * fixed some volatile register bugs
  1977. + -dnotranslation option for -dnewra, which causes the registers not to
  1978. be translated from virtual to normal registers. Requires support in
  1979. the assembler writer as well, which is only implemented in aggas/
  1980. agppcgas currently
  1981. Revision 1.65 2003/08/17 14:32:48 daniel
  1982. * Precoloured nodes now have an infinite degree approached with 255,
  1983. like they should.
  1984. Revision 1.64 2003/08/17 08:48:02 daniel
  1985. * Another register allocator bug fixed.
  1986. * usable_registers_cnt set to 6 for i386
  1987. Revision 1.63 2003/08/09 18:56:54 daniel
  1988. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1989. allocator
  1990. * Some preventive changes to i386 spillinh code
  1991. Revision 1.62 2003/08/03 14:09:50 daniel
  1992. * Fixed a register allocator bug
  1993. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1994. statements: changes in location_force. These moves are now no longer
  1995. constrained so they are optimized away.
  1996. Revision 1.61 2003/07/21 13:32:39 jonas
  1997. * add_edges_used() is now also called for registers allocated with
  1998. getexplicitregisterint()
  1999. * writing the intereference graph is now only done with -dradebug2 and
  2000. the created files are now called "igraph.<module_name>"
  2001. Revision 1.60 2003/07/06 15:31:21 daniel
  2002. * Fixed register allocator. *Lots* of fixes.
  2003. Revision 1.59 2003/07/06 15:00:47 jonas
  2004. * fixed my previous completely broken commit. It's not perfect though,
  2005. registers > last_int_supreg and < max_intreg may still be "translated"
  2006. Revision 1.58 2003/07/06 14:45:05 jonas
  2007. * support integer registers that are not managed by newra (ie. don't
  2008. translate register numbers that fall outside the range
  2009. first_int_supreg..last_int_supreg)
  2010. Revision 1.57 2003/07/02 22:18:04 peter
  2011. * paraloc splitted in callerparaloc,calleeparaloc
  2012. * sparc calling convention updates
  2013. Revision 1.56 2003/06/17 16:34:44 jonas
  2014. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  2015. * renamed all_intregisters to volatile_intregisters and made it
  2016. processor dependent
  2017. Revision 1.55 2003/06/14 14:53:50 jonas
  2018. * fixed newra cycle for x86
  2019. * added constants for indicating source and destination operands of the
  2020. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  2021. Revision 1.54 2003/06/13 21:19:31 peter
  2022. * current_procdef removed, use current_procinfo.procdef instead
  2023. Revision 1.53 2003/06/12 21:11:10 peter
  2024. * ungetregisterfpu gets size parameter
  2025. Revision 1.52 2003/06/12 16:43:07 peter
  2026. * newra compiles for sparc
  2027. Revision 1.51 2003/06/09 14:54:26 jonas
  2028. * (de)allocation of registers for parameters is now performed properly
  2029. (and checked on the ppc)
  2030. - removed obsolete allocation of all parameter registers at the start
  2031. of a procedure (and deallocation at the end)
  2032. Revision 1.50 2003/06/03 21:11:09 peter
  2033. * cg.a_load_* get a from and to size specifier
  2034. * makeregsize only accepts newregister
  2035. * i386 uses generic tcgnotnode,tcgunaryminus
  2036. Revision 1.49 2003/06/03 13:01:59 daniel
  2037. * Register allocator finished
  2038. Revision 1.48 2003/06/01 21:38:06 peter
  2039. * getregisterfpu size parameter added
  2040. * op_const_reg size parameter added
  2041. * sparc updates
  2042. Revision 1.47 2003/05/31 20:31:11 jonas
  2043. * set inital costs of assigning a variable to a register to 120 for
  2044. non-i386, because the used register must be store to memory at the
  2045. start and loaded again at the end
  2046. Revision 1.46 2003/05/30 18:55:21 jonas
  2047. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  2048. works for ppc
  2049. Revision 1.45 2003/05/30 12:36:13 jonas
  2050. * use as little different registers on the ppc until newra is released,
  2051. since every used register must be saved
  2052. Revision 1.44 2003/05/17 13:30:08 jonas
  2053. * changed tt_persistant to tt_persistent :)
  2054. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2055. temps, but a ttemptype, so you can also create ansistring temps etc
  2056. Revision 1.43 2003/05/16 14:33:31 peter
  2057. * regvar fixes
  2058. Revision 1.42 2003/04/26 20:03:49 daniel
  2059. * Bug fix in simplify
  2060. Revision 1.41 2003/04/25 20:59:35 peter
  2061. * removed funcretn,funcretsym, function result is now in varsym
  2062. and aliases for result and function name are added using absolutesym
  2063. * vs_hidden parameter for funcret passed in parameter
  2064. * vs_hidden fixes
  2065. * writenode changed to printnode and released from extdebug
  2066. * -vp option added to generate a tree.log with the nodetree
  2067. * nicer printnode for statements, callnode
  2068. Revision 1.40 2003/04/25 08:25:26 daniel
  2069. * Ifdefs around a lot of calls to cleartempgen
  2070. * Fixed registers that are allocated but not freed in several nodes
  2071. * Tweak to register allocator to cause less spills
  2072. * 8-bit registers now interfere with esi,edi and ebp
  2073. Compiler can now compile rtl successfully when using new register
  2074. allocator
  2075. Revision 1.39 2003/04/23 20:23:06 peter
  2076. * compile fix for no-newra
  2077. Revision 1.38 2003/04/23 14:42:07 daniel
  2078. * Further register allocator work. Compiler now smaller with new
  2079. allocator than without.
  2080. * Somebody forgot to adjust ppu version number
  2081. Revision 1.37 2003/04/22 23:50:23 peter
  2082. * firstpass uses expectloc
  2083. * checks if there are differences between the expectloc and
  2084. location.loc from secondpass in EXTDEBUG
  2085. Revision 1.36 2003/04/22 10:09:35 daniel
  2086. + Implemented the actual register allocator
  2087. + Scratch registers unavailable when new register allocator used
  2088. + maybe_save/maybe_restore unavailable when new register allocator used
  2089. Revision 1.35 2003/04/21 19:16:49 peter
  2090. * count address regs separate
  2091. Revision 1.34 2003/04/17 16:48:21 daniel
  2092. * Added some code to keep track of move instructions in register
  2093. allocator
  2094. Revision 1.33 2003/04/17 07:50:24 daniel
  2095. * Some work on interference graph construction
  2096. Revision 1.32 2003/03/28 19:16:57 peter
  2097. * generic constructor working for i386
  2098. * remove fixed self register
  2099. * esi added as address register for i386
  2100. Revision 1.31 2003/03/11 21:46:24 jonas
  2101. * lots of new regallocator fixes, both in generic and ppc-specific code
  2102. (ppc compiler still can't compile the linux system unit though)
  2103. Revision 1.30 2003/03/09 21:18:59 olle
  2104. + added cutils to the uses clause
  2105. Revision 1.29 2003/03/08 20:36:41 daniel
  2106. + Added newra version of Ti386shlshrnode
  2107. + Added interference graph construction code
  2108. Revision 1.28 2003/03/08 13:59:16 daniel
  2109. * Work to handle new register notation in ag386nsm
  2110. + Added newra version of Ti386moddivnode
  2111. Revision 1.27 2003/03/08 10:53:48 daniel
  2112. * Created newra version of secondmul in n386add.pas
  2113. Revision 1.26 2003/03/08 08:59:07 daniel
  2114. + $define newra will enable new register allocator
  2115. + getregisterint will return imaginary registers with $newra
  2116. + -sr switch added, will skip register allocation so you can see
  2117. the direct output of the code generator before register allocation
  2118. Revision 1.25 2003/02/26 20:50:45 daniel
  2119. * Fixed ungetreference
  2120. Revision 1.24 2003/02/19 22:39:56 daniel
  2121. * Fixed a few issues
  2122. Revision 1.23 2003/02/19 22:00:14 daniel
  2123. * Code generator converted to new register notation
  2124. - Horribily outdated todo.txt removed
  2125. Revision 1.22 2003/02/02 19:25:54 carl
  2126. * Several bugfixes for m68k target (register alloc., opcode emission)
  2127. + VIS target
  2128. + Generic add more complete (still not verified)
  2129. Revision 1.21 2003/01/08 18:43:57 daniel
  2130. * Tregister changed into a record
  2131. Revision 1.20 2002/10/05 12:43:28 carl
  2132. * fixes for Delphi 6 compilation
  2133. (warning : Some features do not work under Delphi)
  2134. Revision 1.19 2002/08/23 16:14:49 peter
  2135. * tempgen cleanup
  2136. * tt_noreuse temp type added that will be used in genentrycode
  2137. Revision 1.18 2002/08/17 22:09:47 florian
  2138. * result type handling in tcgcal.pass_2 overhauled
  2139. * better tnode.dowrite
  2140. * some ppc stuff fixed
  2141. Revision 1.17 2002/08/17 09:23:42 florian
  2142. * first part of procinfo rewrite
  2143. Revision 1.16 2002/08/06 20:55:23 florian
  2144. * first part of ppc calling conventions fix
  2145. Revision 1.15 2002/08/05 18:27:48 carl
  2146. + more more more documentation
  2147. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2148. Revision 1.14 2002/08/04 19:06:41 carl
  2149. + added generic exception support (still does not work!)
  2150. + more documentation
  2151. Revision 1.13 2002/07/07 09:52:32 florian
  2152. * powerpc target fixed, very simple units can be compiled
  2153. * some basic stuff for better callparanode handling, far from being finished
  2154. Revision 1.12 2002/07/01 18:46:26 peter
  2155. * internal linker
  2156. * reorganized aasm layer
  2157. Revision 1.11 2002/05/18 13:34:17 peter
  2158. * readded missing revisions
  2159. Revision 1.10 2002/05/16 19:46:44 carl
  2160. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2161. + try to fix temp allocation (still in ifdef)
  2162. + generic constructor calls
  2163. + start of tassembler / tmodulebase class cleanup
  2164. Revision 1.8 2002/04/21 15:23:03 carl
  2165. + makeregsize
  2166. + changeregsize is now a local routine
  2167. Revision 1.7 2002/04/20 21:32:25 carl
  2168. + generic FPC_CHECKPOINTER
  2169. + first parameter offset in stack now portable
  2170. * rename some constants
  2171. + move some cpu stuff to other units
  2172. - remove unused constents
  2173. * fix stacksize for some targets
  2174. * fix generic size problems which depend now on EXTEND_SIZE constant
  2175. Revision 1.6 2002/04/15 19:03:31 carl
  2176. + reg2str -> std_reg2str()
  2177. Revision 1.5 2002/04/06 18:13:01 jonas
  2178. * several powerpc-related additions and fixes
  2179. Revision 1.4 2002/04/04 19:06:04 peter
  2180. * removed unused units
  2181. * use tlocation.size in cg.a_*loc*() routines
  2182. Revision 1.3 2002/04/02 17:11:29 peter
  2183. * tlocation,treference update
  2184. * LOC_CONSTANT added for better constant handling
  2185. * secondadd splitted in multiple routines
  2186. * location_force_reg added for loading a location to a register
  2187. of a specified size
  2188. * secondassignment parses now first the right and then the left node
  2189. (this is compatible with Kylix). This saves a lot of push/pop especially
  2190. with string operations
  2191. * adapted some routines to use the new cg methods
  2192. Revision 1.2 2002/04/01 19:24:25 jonas
  2193. * fixed different parameter name in interface and implementation
  2194. declaration of a method (only 1.0.x detected this)
  2195. Revision 1.1 2002/03/31 20:26:36 jonas
  2196. + a_loadfpu_* and a_loadmm_* methods in tcg
  2197. * register allocation is now handled by a class and is mostly processor
  2198. independent (+rgobj.pas and i386/rgcpu.pas)
  2199. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2200. * some small improvements and fixes to the optimizer
  2201. * some register allocation fixes
  2202. * some fpuvaroffset fixes in the unary minus node
  2203. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2204. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2205. also better optimizable)
  2206. * fixed and optimized register saving/restoring for new/dispose nodes
  2207. * LOC_FPU locations now also require their "register" field to be set to
  2208. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2209. - list field removed of the tnode class because it's not used currently
  2210. and can cause hard-to-find bugs
  2211. }