rgobj.pas 82 KB

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