rgobj.pas 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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) and (getsupreg(r)>=first_imaginary) 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. if assigned(reginfo[u].movelist) then
  922. begin
  923. {We have to weigh the cost of sorting the list against searching
  924. the cost of the unsorted part. I use factor of 8 here; if the
  925. number of items is less than 8 times the numer of unsorted items,
  926. we'll sort the list.}
  927. with reginfo[u].movelist^ do
  928. if count<8*(count-sorted_until) then
  929. sort_movelist(reginfo[u].movelist);
  930. if assigned(reginfo[v].movelist) then
  931. begin
  932. for n:=0 to reginfo[v].movelist^.count-1 do
  933. begin
  934. {Binary search the sorted part of the list.}
  935. searched:=reginfo[v].movelist^.data[n];
  936. p:=0;
  937. q:=reginfo[u].movelist^.sorted_until;
  938. i:=0;
  939. if q<>0 then
  940. repeat
  941. i:=(p+q) shr 1;
  942. if ptrint(searched)>ptrint(reginfo[u].movelist^.data[i]) then
  943. p:=i+1
  944. else
  945. q:=i;
  946. until p=q;
  947. with reginfo[u].movelist^ do
  948. if searched<>data[i] then
  949. begin
  950. {Linear search the unsorted part of the list.}
  951. for i:=sorted_until+1 to count-1 do
  952. if searched=data[i] then
  953. goto l1;
  954. {Not found -> add}
  955. add_to_movelist(u,searched);
  956. l1:
  957. end;
  958. end;
  959. end;
  960. end;
  961. enable_moves(v);
  962. adj:=reginfo[v].adjlist;
  963. if adj<>nil then
  964. for i:=1 to adj^.length do
  965. begin
  966. t:=adj^.buf^[i-1];
  967. with reginfo[t] do
  968. if not(ri_coalesced in flags) then
  969. begin
  970. {t has a connection to v. Since we are adding v to u, we
  971. need to connect t to u. However, beware if t was already
  972. connected to u...}
  973. if (ibitmap[t,u]) and not (ri_selected in flags) then
  974. {... because in that case, we are actually removing an edge
  975. and the degree of t decreases.}
  976. decrement_degree(t)
  977. else
  978. begin
  979. add_edge(t,u);
  980. {We have added an edge to t and u. So their degree increases.
  981. However, v is added to u. That means its neighbours will
  982. no longer point to v, but to u instead. Therefore, only the
  983. degree of u increases.}
  984. if (u>=first_imaginary) and not (ri_selected in flags) then
  985. inc(reginfo[u].degree);
  986. end;
  987. end;
  988. end;
  989. if (reginfo[u].degree>=usable_registers_cnt) and freezeworklist.delete(u) then
  990. spillworklist.add(u);
  991. end;
  992. procedure trgobj.coalesce;
  993. var m:Tmoveins;
  994. x,y,u,v:Tsuperregister;
  995. begin
  996. m:=Tmoveins(worklist_moves.getfirst);
  997. x:=get_alias(m.x);
  998. y:=get_alias(m.y);
  999. if (y<first_imaginary) then
  1000. begin
  1001. u:=y;
  1002. v:=x;
  1003. end
  1004. else
  1005. begin
  1006. u:=x;
  1007. v:=y;
  1008. end;
  1009. if (u=v) then
  1010. begin
  1011. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1012. coalesced_moves.insert(m);
  1013. add_worklist(u);
  1014. end
  1015. {Do u and v interfere? In that case the move is constrained. Two
  1016. precoloured nodes interfere allways. If v is precoloured, by the above
  1017. code u is precoloured, thus interference...}
  1018. else if (v<first_imaginary) or ibitmap[u,v] then
  1019. begin
  1020. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1021. constrained_moves.insert(m);
  1022. add_worklist(u);
  1023. add_worklist(v);
  1024. end
  1025. {Next test: is it possible and a good idea to coalesce??}
  1026. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  1027. ((u>=first_imaginary) and conservative(u,v)) then
  1028. begin
  1029. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1030. coalesced_moves.insert(m);
  1031. combine(u,v);
  1032. add_worklist(u);
  1033. end
  1034. else
  1035. begin
  1036. m.moveset:=ms_active_moves;
  1037. active_moves.insert(m);
  1038. end;
  1039. end;
  1040. procedure trgobj.freeze_moves(u:Tsuperregister);
  1041. var i:cardinal;
  1042. m:Tlinkedlistitem;
  1043. v,x,y:Tsuperregister;
  1044. begin
  1045. if reginfo[u].movelist<>nil then
  1046. for i:=0 to reginfo[u].movelist^.count-1 do
  1047. begin
  1048. m:=reginfo[u].movelist^.data[i];
  1049. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1050. begin
  1051. x:=Tmoveins(m).x;
  1052. y:=Tmoveins(m).y;
  1053. if get_alias(y)=get_alias(u) then
  1054. v:=get_alias(x)
  1055. else
  1056. v:=get_alias(y);
  1057. {Move m from active_moves/worklist_moves to frozen_moves.}
  1058. if Tmoveins(m).moveset=ms_active_moves then
  1059. active_moves.remove(m)
  1060. else
  1061. worklist_moves.remove(m);
  1062. Tmoveins(m).moveset:=ms_frozen_moves;
  1063. frozen_moves.insert(m);
  1064. if (v>=first_imaginary) and not(move_related(v)) and
  1065. (reginfo[v].degree<usable_registers_cnt) then
  1066. begin
  1067. freezeworklist.delete(v);
  1068. simplifyworklist.add(v);
  1069. end;
  1070. end;
  1071. end;
  1072. end;
  1073. procedure trgobj.freeze;
  1074. var n:Tsuperregister;
  1075. begin
  1076. { We need to take a random element out of the freezeworklist. We take
  1077. the last element. Dirty code! }
  1078. n:=freezeworklist.get;
  1079. {Add it to the simplifyworklist.}
  1080. simplifyworklist.add(n);
  1081. freeze_moves(n);
  1082. end;
  1083. procedure trgobj.select_spill;
  1084. var
  1085. n : tsuperregister;
  1086. adj : psuperregisterworklist;
  1087. max,p,i:word;
  1088. begin
  1089. { We must look for the element with the most interferences in the
  1090. spillworklist. This is required because those registers are creating
  1091. the most conflicts and keeping them in a register will not reduce the
  1092. complexity and even can cause the help registers for the spilling code
  1093. to get too much conflicts with the result that the spilling code
  1094. will never converge (PFV) }
  1095. max:=0;
  1096. p:=0;
  1097. with spillworklist do
  1098. begin
  1099. {Safe: This procedure is only called if length<>0}
  1100. for i:=0 to length-1 do
  1101. begin
  1102. adj:=reginfo[buf^[i]].adjlist;
  1103. if assigned(adj) and (adj^.length>max) then
  1104. begin
  1105. p:=i;
  1106. max:=adj^.length;
  1107. end;
  1108. end;
  1109. n:=buf^[p];
  1110. deleteidx(p);
  1111. end;
  1112. simplifyworklist.add(n);
  1113. freeze_moves(n);
  1114. end;
  1115. procedure trgobj.assign_colours;
  1116. {Assign_colours assigns the actual colours to the registers.}
  1117. var adj : Psuperregisterworklist;
  1118. i,j,k : word;
  1119. n,a,c : Tsuperregister;
  1120. adj_colours,
  1121. colourednodes : Tsuperregisterset;
  1122. found : boolean;
  1123. begin
  1124. spillednodes.clear;
  1125. {Reset colours}
  1126. for n:=0 to maxreg-1 do
  1127. reginfo[n].colour:=n;
  1128. {Colour the cpu registers...}
  1129. supregset_reset(colourednodes,false);
  1130. for n:=0 to first_imaginary-1 do
  1131. supregset_include(colourednodes,n);
  1132. {Now colour the imaginary registers on the select-stack.}
  1133. for i:=selectstack.length downto 1 do
  1134. begin
  1135. n:=selectstack.buf^[i-1];
  1136. {Create a list of colours that we cannot assign to n.}
  1137. supregset_reset(adj_colours,false);
  1138. adj:=reginfo[n].adjlist;
  1139. if adj<>nil then
  1140. for j:=0 to adj^.length-1 do
  1141. begin
  1142. a:=get_alias(adj^.buf^[j]);
  1143. if supregset_in(colourednodes,a) then
  1144. supregset_include(adj_colours,reginfo[a].colour);
  1145. end;
  1146. supregset_include(adj_colours,RS_STACK_POINTER_REG);
  1147. {Assume a spill by default...}
  1148. found:=false;
  1149. {Search for a colour not in this list.}
  1150. for k:=0 to usable_registers_cnt-1 do
  1151. begin
  1152. c:=usable_registers[k];
  1153. if not(supregset_in(adj_colours,c)) then
  1154. begin
  1155. reginfo[n].colour:=c;
  1156. found:=true;
  1157. supregset_include(colourednodes,n);
  1158. include(used_in_proc,c);
  1159. break;
  1160. end;
  1161. end;
  1162. if not found then
  1163. spillednodes.add(n);
  1164. end;
  1165. {Finally colour the nodes that were coalesced.}
  1166. for i:=1 to coalescednodes.length do
  1167. begin
  1168. n:=coalescednodes.buf^[i-1];
  1169. k:=get_alias(n);
  1170. reginfo[n].colour:=reginfo[k].colour;
  1171. if reginfo[k].colour<maxcpuregister then
  1172. include(used_in_proc,reginfo[k].colour);
  1173. end;
  1174. {$ifdef ra_debug}
  1175. if aktfilepos.line=179 then
  1176. begin
  1177. writeln('colourlist');
  1178. for i:=0 to maxreg-1 do
  1179. writeln(i:4,' ',reginfo[i].colour:4)
  1180. end;
  1181. {$endif ra_debug}
  1182. end;
  1183. procedure trgobj.colour_registers;
  1184. begin
  1185. repeat
  1186. if simplifyworklist.length<>0 then
  1187. simplify
  1188. else if not(worklist_moves.empty) then
  1189. coalesce
  1190. else if freezeworklist.length<>0 then
  1191. freeze
  1192. else if spillworklist.length<>0 then
  1193. select_spill;
  1194. until (simplifyworklist.length=0) and
  1195. worklist_moves.empty and
  1196. (freezeworklist.length=0) and
  1197. (spillworklist.length=0);
  1198. assign_colours;
  1199. end;
  1200. procedure trgobj.epilogue_colouring;
  1201. var
  1202. i : Tsuperregister;
  1203. begin
  1204. worklist_moves.clear;
  1205. active_moves.destroy;
  1206. active_moves:=nil;
  1207. frozen_moves.destroy;
  1208. frozen_moves:=nil;
  1209. coalesced_moves.destroy;
  1210. coalesced_moves:=nil;
  1211. constrained_moves.destroy;
  1212. constrained_moves:=nil;
  1213. for i:=0 to maxreg-1 do
  1214. with reginfo[i] do
  1215. if movelist<>nil then
  1216. begin
  1217. dispose(movelist);
  1218. movelist:=nil;
  1219. end;
  1220. end;
  1221. procedure trgobj.clear_interferences(u:Tsuperregister);
  1222. {Remove node u from the interference graph and remove all collected
  1223. move instructions it is associated with.}
  1224. var i : word;
  1225. v : Tsuperregister;
  1226. adj,adj2 : Psuperregisterworklist;
  1227. begin
  1228. adj:=reginfo[u].adjlist;
  1229. if adj<>nil then
  1230. begin
  1231. for i:=1 to adj^.length do
  1232. begin
  1233. v:=adj^.buf^[i-1];
  1234. {Remove (u,v) and (v,u) from bitmap.}
  1235. ibitmap[u,v]:=false;
  1236. ibitmap[v,u]:=false;
  1237. {Remove (v,u) from adjacency list.}
  1238. adj2:=reginfo[v].adjlist;
  1239. if adj2<>nil then
  1240. begin
  1241. adj2^.delete(u);
  1242. if adj2^.length=0 then
  1243. begin
  1244. dispose(adj2,done);
  1245. reginfo[v].adjlist:=nil;
  1246. end;
  1247. end;
  1248. end;
  1249. {Remove ( u,* ) from adjacency list.}
  1250. dispose(adj,done);
  1251. reginfo[u].adjlist:=nil;
  1252. end;
  1253. end;
  1254. procedure trgobj.getregisterinline(list:Taasmoutput;
  1255. position:Tai;subreg:Tsubregister;var result:Tregister);
  1256. var p:Tsuperregister;
  1257. r:Tregister;
  1258. begin
  1259. p:=getnewreg(subreg);
  1260. live_registers.add(p);
  1261. r:=newreg(regtype,p,subreg);
  1262. if position=nil then
  1263. list.insert(Tai_regalloc.alloc(r))
  1264. else
  1265. list.insertafter(Tai_regalloc.alloc(r),position);
  1266. add_edges_used(p);
  1267. add_constraints(r);
  1268. result:=r;
  1269. end;
  1270. procedure trgobj.ungetregisterinline(list:Taasmoutput;
  1271. position:Tai;r:Tregister);
  1272. var supreg:Tsuperregister;
  1273. begin
  1274. supreg:=getsupreg(r);
  1275. live_registers.delete(supreg);
  1276. if position=nil then
  1277. list.insert(Tai_regalloc.dealloc(r))
  1278. else
  1279. list.insertafter(Tai_regalloc.dealloc(r),position);
  1280. end;
  1281. procedure trgobj.insert_regalloc_info(list:Taasmoutput;headertai:tai);
  1282. var
  1283. supreg : tsuperregister;
  1284. p : tai;
  1285. r : tregister;
  1286. begin
  1287. { Insert regallocs for all imaginary registers }
  1288. for supreg:=first_imaginary to maxreg-1 do
  1289. with reginfo[supreg] do
  1290. begin
  1291. r:=newreg(regtype,supreg,subreg);
  1292. if assigned(live_start) then
  1293. begin
  1294. {$ifdef EXTDEBUG}
  1295. if live_start=live_end then
  1296. Comment(V_Warning,'Register '+std_regname(r)+' is only used once');
  1297. {$endif EXTDEBUG}
  1298. list.insertbefore(Tai_regalloc.alloc(r),live_start);
  1299. { Insert live end deallocation before reg allocations
  1300. to reduce conflicts }
  1301. p:=live_end;
  1302. while assigned(p) and
  1303. assigned(p.previous) and
  1304. (tai(p.previous).typ=ait_regalloc) and
  1305. tai_regalloc(p.previous).allocation and
  1306. (tai_regalloc(p.previous).reg<>r) do
  1307. p:=tai(p.previous);
  1308. list.insertbefore(Tai_regalloc.dealloc(r),p);
  1309. end
  1310. {$ifdef EXTDEBUG}
  1311. else
  1312. Comment(V_Warning,'Register '+std_regname(r)+' not used');
  1313. {$endif EXTDEBUG}
  1314. end;
  1315. end;
  1316. procedure trgobj.add_cpu_interferences(p : tai);
  1317. begin
  1318. end;
  1319. procedure trgobj.generate_interference_graph(list:Taasmoutput;headertai:tai);
  1320. var
  1321. p : tai;
  1322. i : integer;
  1323. supreg : tsuperregister;
  1324. begin
  1325. { All allocations are available. Now we can generate the
  1326. interference graph. Walk through all instructions, we can
  1327. start with the headertai, because before the header tai is
  1328. only symbols. }
  1329. live_registers.clear;
  1330. p:=headertai;
  1331. while assigned(p) do
  1332. begin
  1333. if p.typ=ait_regalloc then
  1334. with Tai_regalloc(p) do
  1335. begin
  1336. if (getregtype(reg)=regtype) then
  1337. begin
  1338. supreg:=getsupreg(reg);
  1339. if allocation then
  1340. live_registers.add(supreg)
  1341. else
  1342. live_registers.delete(supreg);
  1343. add_edges_used(supreg);
  1344. add_constraints(reg);
  1345. end;
  1346. end;
  1347. add_cpu_interferences(p);
  1348. p:=Tai(p.next);
  1349. end;
  1350. {$ifdef EXTDEBUG}
  1351. if live_registers.length>0 then
  1352. begin
  1353. for i:=0 to live_registers.length-1 do
  1354. begin
  1355. { Only report for imaginary registers }
  1356. if live_registers.buf^[i]>=first_imaginary then
  1357. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,live_registers.buf^[i],defaultsub))+' not released');
  1358. end;
  1359. end;
  1360. {$endif}
  1361. end;
  1362. procedure Trgobj.translate_registers(list:taasmoutput);
  1363. var
  1364. hp,p,q:Tai;
  1365. i:shortint;
  1366. {$ifdef arm}
  1367. so:pshifterop;
  1368. {$endif arm}
  1369. begin
  1370. { Leave when no imaginary registers are used }
  1371. if maxreg<=first_imaginary then
  1372. exit;
  1373. p:=Tai(list.first);
  1374. while assigned(p) do
  1375. begin
  1376. case p.typ of
  1377. ait_regalloc:
  1378. with Tai_regalloc(p) do
  1379. begin
  1380. if (getregtype(reg)=regtype) then
  1381. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1382. {
  1383. Remove sequences of release and
  1384. allocation of the same register like:
  1385. # Register X released
  1386. # Register X allocated
  1387. }
  1388. if assigned(previous) and
  1389. (Tai(previous).typ=ait_regalloc) and
  1390. (Tai_regalloc(previous).reg=reg) and
  1391. { allocation,deallocation or deallocation,allocation }
  1392. (Tai_regalloc(previous).allocation xor allocation) then
  1393. begin
  1394. q:=Tai(next);
  1395. hp:=tai(previous);
  1396. list.remove(hp);
  1397. hp.free;
  1398. list.remove(p);
  1399. p.free;
  1400. p:=q;
  1401. continue;
  1402. end;
  1403. end;
  1404. ait_instruction:
  1405. with Taicpu_abstract(p) do
  1406. begin
  1407. for i:=0 to ops-1 do
  1408. with oper[i]^ do
  1409. case typ of
  1410. Top_reg:
  1411. if (getregtype(reg)=regtype) then
  1412. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1413. Top_ref:
  1414. begin
  1415. if regtype=R_INTREGISTER then
  1416. with ref^ do
  1417. begin
  1418. if base<>NR_NO then
  1419. setsupreg(base,reginfo[getsupreg(base)].colour);
  1420. if index<>NR_NO then
  1421. setsupreg(index,reginfo[getsupreg(index)].colour);
  1422. end;
  1423. end;
  1424. {$ifdef arm}
  1425. Top_shifterop:
  1426. begin
  1427. so:=shifterop;
  1428. if so^.rs<>NR_NO then
  1429. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1430. end;
  1431. {$endif arm}
  1432. end;
  1433. { Maybe the operation can be removed when
  1434. it is a move and both arguments are the same }
  1435. if is_same_reg_move(regtype) then
  1436. begin
  1437. q:=Tai(p.next);
  1438. list.remove(p);
  1439. p.free;
  1440. p:=q;
  1441. continue;
  1442. end;
  1443. end;
  1444. end;
  1445. p:=Tai(p.next);
  1446. end;
  1447. end;
  1448. function trgobj.get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister):Tai;
  1449. var
  1450. back : Tsuperregisterworklist;
  1451. supreg : tsuperregister;
  1452. begin
  1453. back.copyfrom(live_registers);
  1454. result:=p;
  1455. while (p<>nil) and (p.typ=ait_regalloc) do
  1456. begin
  1457. supreg:=getsupreg(Tai_regalloc(p).reg);
  1458. {Rewind the register allocation.}
  1459. if Tai_regalloc(p).allocation then
  1460. live_registers.delete(supreg)
  1461. else
  1462. begin
  1463. live_registers.add(supreg);
  1464. if supreg=huntfor1 then
  1465. begin
  1466. get_insert_pos:=Tai(p.previous);
  1467. back.done;
  1468. back.copyfrom(live_registers);
  1469. end;
  1470. if supreg=huntfor2 then
  1471. begin
  1472. get_insert_pos:=Tai(p.previous);
  1473. back.done;
  1474. back.copyfrom(live_registers);
  1475. end;
  1476. if supreg=huntfor3 then
  1477. begin
  1478. get_insert_pos:=Tai(p.previous);
  1479. back.done;
  1480. back.copyfrom(live_registers);
  1481. end;
  1482. end;
  1483. p:=Tai(p.previous);
  1484. end;
  1485. live_registers.done;
  1486. live_registers:=back;
  1487. end;
  1488. procedure trgobj.forward_allocation(pfrom,pto:Tai);
  1489. var
  1490. p : tai;
  1491. begin
  1492. {Forward the register allocation again.}
  1493. p:=pfrom;
  1494. while (p<>pto) do
  1495. begin
  1496. if p.typ<>ait_regalloc then
  1497. internalerror(200305311);
  1498. if Tai_regalloc(p).allocation then
  1499. live_registers.add(getsupreg(Tai_regalloc(p).reg))
  1500. else
  1501. live_registers.delete(getsupreg(Tai_regalloc(p).reg));
  1502. p:=Tai(p.next);
  1503. end;
  1504. end;
  1505. function trgobj.spill_registers(list:Taasmoutput;headertai:tai):boolean;
  1506. { Returns true if any help registers have been used }
  1507. var
  1508. i : word;
  1509. t : tsuperregister;
  1510. p,q : Tai;
  1511. regs_to_spill_set:Tsuperregisterset;
  1512. spill_temps : ^Tspill_temp_list;
  1513. supreg : tsuperregister;
  1514. templist : taasmoutput;
  1515. begin
  1516. spill_registers:=false;
  1517. live_registers.clear;
  1518. for i:=first_imaginary to maxreg-1 do
  1519. exclude(reginfo[i].flags,ri_selected);
  1520. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1521. supregset_reset(regs_to_spill_set,false);
  1522. { Allocate temps and insert in front of the list }
  1523. templist:=taasmoutput.create;
  1524. {Safe: this procedure is only called if there are spilled nodes.}
  1525. with spillednodes do
  1526. for i:=0 to length-1 do
  1527. begin
  1528. t:=buf^[i];
  1529. {Alternative representation.}
  1530. supregset_include(regs_to_spill_set,t);
  1531. {Clear all interferences of the spilled register.}
  1532. clear_interferences(t);
  1533. {Get a temp for the spilled register}
  1534. tg.gettemp(templist,4,tt_noreuse,spill_temps^[t]);
  1535. end;
  1536. list.insertlistafter(headertai,templist);
  1537. templist.free;
  1538. { Walk through all instructions, we can start with the headertai,
  1539. because before the header tai is only symbols }
  1540. p:=headertai;
  1541. while assigned(p) do
  1542. begin
  1543. case p.typ of
  1544. ait_regalloc:
  1545. with Tai_regalloc(p) do
  1546. begin
  1547. if (getregtype(reg)=regtype) then
  1548. begin
  1549. {A register allocation of a spilled register can be removed.}
  1550. supreg:=getsupreg(reg);
  1551. if supregset_in(regs_to_spill_set,supreg) then
  1552. begin
  1553. q:=Tai(p.next);
  1554. list.remove(p);
  1555. p.free;
  1556. p:=q;
  1557. continue;
  1558. end
  1559. else
  1560. if allocation then
  1561. live_registers.add(supreg)
  1562. else
  1563. live_registers.delete(supreg);
  1564. end;
  1565. end;
  1566. ait_instruction:
  1567. with Taicpu_abstract(p) do
  1568. begin
  1569. aktfilepos:=fileinfo;
  1570. if instr_spill_register(list,Taicpu_abstract(p),regs_to_spill_set,spill_temps^) then
  1571. spill_registers:=true;
  1572. end;
  1573. end;
  1574. p:=Tai(p.next);
  1575. end;
  1576. aktfilepos:=current_procinfo.exitpos;
  1577. {Safe: this procedure is only called if there are spilled nodes.}
  1578. with spillednodes do
  1579. for i:=0 to length-1 do
  1580. tg.ungettemp(list,spill_temps^[buf^[i]]);
  1581. freemem(spill_temps);
  1582. end;
  1583. procedure Trgobj.do_spill_read(list:Taasmoutput;instr:Taicpu_abstract;
  1584. pos:Tai;regidx:word;
  1585. const spilltemplist:Tspill_temp_list;
  1586. const regs:Tspillregsinfo);
  1587. var helpins:Tai;
  1588. begin
  1589. with regs[regidx] do
  1590. begin
  1591. helpins:=instr.spilling_create_load(spilltemplist[orgreg],tempreg);
  1592. if pos=nil then
  1593. list.insertafter(helpins,list.first)
  1594. else
  1595. list.insertafter(helpins,pos.next);
  1596. ungetregisterinline(list,instr,tempreg);
  1597. forward_allocation(tai(helpins.next),instr);
  1598. end;
  1599. end;
  1600. procedure Trgobj.do_spill_written(list:Taasmoutput;instr:Taicpu_abstract;
  1601. pos:Tai;regidx:word;
  1602. const spilltemplist:Tspill_temp_list;
  1603. const regs:Tspillregsinfo);
  1604. var helpins:Tai;
  1605. begin
  1606. with regs[regidx] do
  1607. begin
  1608. helpins:=instr.spilling_create_store(tempreg,spilltemplist[orgreg]);
  1609. list.insertafter(helpins,instr);
  1610. ungetregisterinline(list,helpins,tempreg);
  1611. end;
  1612. end;
  1613. procedure Trgobj.do_spill_readwritten(list:Taasmoutput;instr:Taicpu_abstract;
  1614. pos:Tai;regidx:word;
  1615. const spilltemplist:Tspill_temp_list;
  1616. const regs:Tspillregsinfo);
  1617. var helpins1,helpins2:Tai;
  1618. begin
  1619. with regs[regidx] do
  1620. begin
  1621. helpins1:=instr.spilling_create_load(spilltemplist[orgreg],tempreg);
  1622. if pos=nil then
  1623. list.insertafter(helpins1,list.first)
  1624. else
  1625. list.insertafter(helpins1,pos.next);
  1626. helpins2:=instr.spilling_create_store(tempreg,spilltemplist[orgreg]);
  1627. list.insertafter(helpins2,instr);
  1628. ungetregisterinline(list,helpins2,tempreg);
  1629. forward_allocation(tai(helpins1.next),instr);
  1630. end;
  1631. end;
  1632. function trgobj.instr_spill_register(list:Taasmoutput;
  1633. instr:taicpu_abstract;
  1634. const r:Tsuperregisterset;
  1635. const spilltemplist:Tspill_temp_list): boolean;
  1636. var
  1637. counter, regindex: longint;
  1638. pos: tai;
  1639. regs: tspillregsinfo;
  1640. spilled: boolean;
  1641. procedure addreginfo(reg: tsuperregister; operation: topertype);
  1642. var
  1643. i, tmpindex: longint;
  1644. begin
  1645. tmpindex := regindex;
  1646. // did we already encounter this register?
  1647. for i := 0 to pred(regindex) do
  1648. if (regs[i].orgreg = reg) then
  1649. begin
  1650. tmpindex := i;
  1651. break;
  1652. end;
  1653. if tmpindex > high(regs) then
  1654. internalerror(2003120301);
  1655. regs[tmpindex].orgreg := reg;
  1656. if supregset_in(r,reg) then
  1657. begin
  1658. // add/update info on this register
  1659. regs[tmpindex].mustbespilled := true;
  1660. case operation of
  1661. operand_read:
  1662. regs[tmpindex].regread := true;
  1663. operand_write:
  1664. regs[tmpindex].regwritten := true;
  1665. operand_readwrite:
  1666. begin
  1667. regs[tmpindex].regread := true;
  1668. regs[tmpindex].regwritten := true;
  1669. end;
  1670. end;
  1671. spilled := true;
  1672. end;
  1673. inc(regindex,ord(regindex=tmpindex));
  1674. end;
  1675. procedure tryreplacereg(var reg: tregister);
  1676. var
  1677. i: longint;
  1678. supreg: tsuperregister;
  1679. begin
  1680. if (getregtype(reg) = R_INTREGISTER) then
  1681. begin
  1682. supreg := getsupreg(reg);
  1683. for i := 0 to pred(regindex) do
  1684. if (regs[i].mustbespilled) and
  1685. (regs[i].orgreg = supreg) then
  1686. begin
  1687. reg := regs[i].tempreg;
  1688. break;
  1689. end;
  1690. end;
  1691. end;
  1692. begin
  1693. result := false;
  1694. fillchar(regs,sizeof(regs),0);
  1695. for counter := low(regs) to high(regs) do
  1696. regs[counter].orgreg := RS_INVALID;
  1697. spilled := false;
  1698. regindex := 0;
  1699. { check whether and if so which and how (read/written) this instructions contains
  1700. registers that must be spilled }
  1701. for counter := 0 to instr.ops-1 do
  1702. with instr.oper[counter]^ do
  1703. begin
  1704. case typ of
  1705. top_reg:
  1706. begin
  1707. if (getregtype(reg) = regtype) then
  1708. addreginfo(getsupreg(reg),instr.spilling_get_operation_type(counter));
  1709. end;
  1710. top_ref:
  1711. begin
  1712. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1713. with ref^ do
  1714. begin
  1715. if (base <> NR_NO) then
  1716. addreginfo(getsupreg(base),operand_read);
  1717. if (index <> NR_NO) then
  1718. addreginfo(getsupreg(index),operand_read);
  1719. end;
  1720. end;
  1721. {$ifdef ARM}
  1722. top_shifterop:
  1723. begin
  1724. if shifterop^.rs<>NR_NO then
  1725. addreginfo(getsupreg(shifterop^.rs),operand_read);
  1726. end;
  1727. {$endif ARM}
  1728. end;
  1729. end;
  1730. { if no spilling for this instruction we can leave }
  1731. if not spilled then
  1732. exit;
  1733. { generate the spilling code }
  1734. result := true;
  1735. for counter := 0 to pred(regindex) do
  1736. with regs[counter] do
  1737. begin
  1738. if mustbespilled then
  1739. begin
  1740. pos:=get_insert_pos(Tai(instr.previous),regs[0].orgreg,regs[1].orgreg,regs[2].orgreg);
  1741. getregisterinline(list,pos,defaultsub,tempreg);
  1742. if regread then
  1743. if regwritten then
  1744. do_spill_readwritten(list,instr,pos,counter,spilltemplist,regs)
  1745. else
  1746. do_spill_read(list,instr,pos,counter,spilltemplist,regs)
  1747. else
  1748. do_spill_written(list,instr,pos,counter,spilltemplist,regs)
  1749. end;
  1750. end;
  1751. { substitute registers }
  1752. for counter := 0 to instr.ops-1 do
  1753. with instr.oper[counter]^ do
  1754. begin
  1755. case typ of
  1756. top_reg:
  1757. begin
  1758. tryreplacereg(reg);
  1759. end;
  1760. top_ref:
  1761. begin
  1762. tryreplacereg(ref^.base);
  1763. tryreplacereg(ref^.index);
  1764. end;
  1765. {$ifdef ARM}
  1766. top_shifterop:
  1767. begin
  1768. tryreplacereg(shifterop^.rs);
  1769. end;
  1770. {$endif ARM}
  1771. end;
  1772. end;
  1773. end;
  1774. end.
  1775. {
  1776. $Log$
  1777. Revision 1.123 2004-03-14 20:06:17 peter
  1778. * check if movelist is valid
  1779. Revision 1.122 2004/02/12 15:54:03 peter
  1780. * make extcycle is working again
  1781. Revision 1.121 2004/02/09 20:12:23 olle
  1782. + check that register allocation is not made at the wrong moment
  1783. Revision 1.120 2004/02/08 23:10:21 jonas
  1784. * taicpu.is_same_reg_move() now gets a regtype parameter so it only
  1785. removes moves of that particular register type. This is necessary so
  1786. we don't remove the live_start instruction of a register before it
  1787. has been processed
  1788. Revision 1.119 2004/02/08 14:26:28 daniel
  1789. * Register allocator speed boost
  1790. Revision 1.118 2004/02/07 23:28:34 daniel
  1791. * Take advantage of our new with statement optimization
  1792. Revision 1.117 2004/02/06 13:34:46 daniel
  1793. * Some changes to better accomodate very large movelists
  1794. * movelist resizing now exponential (avoids heap fragmentation, saves
  1795. 300 kb memory in make cycle)
  1796. * Trgobj.combine hand-optimized (still too slow)
  1797. Revision 1.116 2004/01/28 22:16:31 peter
  1798. * more record alignment fixes
  1799. Revision 1.115 2004/01/26 17:40:11 florian
  1800. * made DoSpill* overrideable
  1801. + add_cpu_interferences added
  1802. Revision 1.114 2004/01/26 16:12:28 daniel
  1803. * reginfo now also only allocated during register allocation
  1804. * third round of gdb cleanups: kick out most of concatstabto
  1805. Revision 1.112 2004/01/12 16:37:59 peter
  1806. * moved spilling code from taicpu to rg
  1807. Revision 1.109 2003/12/26 14:02:30 peter
  1808. * sparc updates
  1809. * use registertype in spill_register
  1810. Revision 1.108 2003/12/22 23:09:34 peter
  1811. * only report unreleased imaginary registers
  1812. Revision 1.107 2003/12/22 22:13:46 peter
  1813. * made decrease_degree working, but not really fixed
  1814. Revision 1.106 2003/12/18 17:06:21 florian
  1815. * arm compiler compilation fixed
  1816. Revision 1.105 2003/12/17 21:59:05 peter
  1817. * don't insert dealloc before alloc of the same register
  1818. Revision 1.104 2003/12/16 09:41:44 daniel
  1819. * Automatic conversion from integer constants to pointer constants is no
  1820. longer done except in Delphi mode
  1821. Revision 1.103 2003/12/15 21:25:49 peter
  1822. * reg allocations for imaginary register are now inserted just
  1823. before reg allocation
  1824. * tregister changed to enum to allow compile time check
  1825. * fixed several tregister-tsuperregister errors
  1826. Revision 1.102 2003/12/15 16:37:47 daniel
  1827. * More microoptimizations
  1828. Revision 1.101 2003/12/15 15:58:58 peter
  1829. * fix statedebug compile
  1830. Revision 1.100 2003/12/14 20:24:28 daniel
  1831. * Register allocator speed optimizations
  1832. - Worklist no longer a ringbuffer
  1833. - No find operations are left
  1834. - Simplify now done in constant time
  1835. - unusedregs is now a Tsuperregisterworklist
  1836. - Microoptimizations
  1837. Revision 1.99 2003/12/12 17:16:17 peter
  1838. * rg[tregistertype] added in tcg
  1839. Revision 1.98 2003/12/04 23:27:32 peter
  1840. * remove redundant calls to add_edge_used
  1841. Revision 1.97 2003/11/29 17:36:41 peter
  1842. * check for add_move_instruction
  1843. Revision 1.96 2003/11/24 15:17:37 florian
  1844. * changed some types to prevend range check errors
  1845. Revision 1.95 2003/11/10 19:05:50 peter
  1846. * fixed alias/colouring > 255
  1847. Revision 1.94 2003/11/07 15:58:32 florian
  1848. * Florian's culmutative nr. 1; contains:
  1849. - invalid calling conventions for a certain cpu are rejected
  1850. - arm softfloat calling conventions
  1851. - -Sp for cpu dependend code generation
  1852. - several arm fixes
  1853. - remaining code for value open array paras on heap
  1854. Revision 1.93 2003/10/30 16:22:40 peter
  1855. * call firstpass before allocation and codegeneration is started
  1856. * move leftover code from pass_2.generatecode() to psub
  1857. Revision 1.92 2003/10/29 21:29:14 jonas
  1858. * some ALLOWDUPREG improvements
  1859. Revision 1.91 2003/10/21 15:15:36 peter
  1860. * taicpu_abstract.oper[] changed to pointers
  1861. Revision 1.90 2003/10/19 12:36:36 florian
  1862. * improved speed; reduced memory usage of the interference bitmap
  1863. Revision 1.89 2003/10/19 01:34:30 florian
  1864. * some ppc stuff fixed
  1865. * memory leak fixed
  1866. Revision 1.88 2003/10/18 15:41:26 peter
  1867. * made worklists dynamic in size
  1868. Revision 1.87 2003/10/17 16:16:08 peter
  1869. * fixed last commit
  1870. Revision 1.86 2003/10/17 15:25:18 florian
  1871. * fixed more ppc stuff
  1872. Revision 1.85 2003/10/17 14:38:32 peter
  1873. * 64k registers supported
  1874. * fixed some memory leaks
  1875. Revision 1.84 2003/10/11 16:06:42 florian
  1876. * fixed some MMX<->SSE
  1877. * started to fix ppc, needs an overhaul
  1878. + stabs info improve for spilling, not sure if it works correctly/completly
  1879. - MMX_SUPPORT removed from Makefile.fpc
  1880. Revision 1.83 2003/10/10 17:48:14 peter
  1881. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1882. * tregisteralloctor renamed to trgobj
  1883. * removed rgobj from a lot of units
  1884. * moved location_* and reference_* to cgobj
  1885. * first things for mmx register allocation
  1886. Revision 1.82 2003/10/09 21:31:37 daniel
  1887. * Register allocator splitted, ans abstract now
  1888. Revision 1.81 2003/10/01 20:34:49 peter
  1889. * procinfo unit contains tprocinfo
  1890. * cginfo renamed to cgbase
  1891. * moved cgmessage to verbose
  1892. * fixed ppc and sparc compiles
  1893. Revision 1.80 2003/09/30 19:54:42 peter
  1894. * reuse registers with the least conflicts
  1895. Revision 1.79 2003/09/29 20:58:56 peter
  1896. * optimized releasing of registers
  1897. Revision 1.78 2003/09/28 13:41:12 peter
  1898. * return reg 255 when allowdupreg is defined
  1899. Revision 1.77 2003/09/25 16:19:32 peter
  1900. * fix filepositions
  1901. * insert spill temp allocations at the start of the proc
  1902. Revision 1.76 2003/09/16 16:17:01 peter
  1903. * varspez in calls to push_addr_param
  1904. Revision 1.75 2003/09/12 19:07:42 daniel
  1905. * Fixed fast spilling functionality by re-adding the code that initializes
  1906. precoloured nodes to degree 255. I would like to play hangman on the one
  1907. who removed that code.
  1908. Revision 1.74 2003/09/11 11:54:59 florian
  1909. * improved arm code generation
  1910. * move some protected and private field around
  1911. * the temp. register for register parameters/arguments are now released
  1912. before the move to the parameter register is done. This improves
  1913. the code in a lot of cases.
  1914. Revision 1.73 2003/09/09 20:59:27 daniel
  1915. * Adding register allocation order
  1916. Revision 1.72 2003/09/09 15:55:44 peter
  1917. * use register with least interferences in spillregister
  1918. Revision 1.71 2003/09/07 22:09:35 peter
  1919. * preparations for different default calling conventions
  1920. * various RA fixes
  1921. Revision 1.70 2003/09/03 21:06:45 peter
  1922. * fixes for FPU register allocation
  1923. Revision 1.69 2003/09/03 15:55:01 peter
  1924. * NEWRA branch merged
  1925. Revision 1.68 2003/09/03 11:18:37 florian
  1926. * fixed arm concatcopy
  1927. + arm support in the common compiler sources added
  1928. * moved some generic cg code around
  1929. + tfputype added
  1930. * ...
  1931. Revision 1.67.2.5 2003/08/31 20:44:07 peter
  1932. * fixed getexplicitregisterint tregister value
  1933. Revision 1.67.2.4 2003/08/31 20:40:50 daniel
  1934. * Fixed add_edges_used
  1935. Revision 1.67.2.3 2003/08/29 17:28:59 peter
  1936. * next batch of updates
  1937. Revision 1.67.2.2 2003/08/28 18:35:08 peter
  1938. * tregister changed to cardinal
  1939. Revision 1.67.2.1 2003/08/27 19:55:54 peter
  1940. * first tregister patch
  1941. Revision 1.67 2003/08/23 10:46:21 daniel
  1942. * Register allocator bugfix for h2pas
  1943. Revision 1.66 2003/08/17 16:59:20 jonas
  1944. * fixed regvars so they work with newra (at least for ppc)
  1945. * fixed some volatile register bugs
  1946. + -dnotranslation option for -dnewra, which causes the registers not to
  1947. be translated from virtual to normal registers. Requires support in
  1948. the assembler writer as well, which is only implemented in aggas/
  1949. agppcgas currently
  1950. Revision 1.65 2003/08/17 14:32:48 daniel
  1951. * Precoloured nodes now have an infinite degree approached with 255,
  1952. like they should.
  1953. Revision 1.64 2003/08/17 08:48:02 daniel
  1954. * Another register allocator bug fixed.
  1955. * usable_registers_cnt set to 6 for i386
  1956. Revision 1.63 2003/08/09 18:56:54 daniel
  1957. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1958. allocator
  1959. * Some preventive changes to i386 spillinh code
  1960. Revision 1.62 2003/08/03 14:09:50 daniel
  1961. * Fixed a register allocator bug
  1962. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1963. statements: changes in location_force. These moves are now no longer
  1964. constrained so they are optimized away.
  1965. Revision 1.61 2003/07/21 13:32:39 jonas
  1966. * add_edges_used() is now also called for registers allocated with
  1967. getexplicitregisterint()
  1968. * writing the intereference graph is now only done with -dradebug2 and
  1969. the created files are now called "igraph.<module_name>"
  1970. Revision 1.60 2003/07/06 15:31:21 daniel
  1971. * Fixed register allocator. *Lots* of fixes.
  1972. Revision 1.59 2003/07/06 15:00:47 jonas
  1973. * fixed my previous completely broken commit. It's not perfect though,
  1974. registers > last_int_supreg and < max_intreg may still be "translated"
  1975. Revision 1.58 2003/07/06 14:45:05 jonas
  1976. * support integer registers that are not managed by newra (ie. don't
  1977. translate register numbers that fall outside the range
  1978. first_int_supreg..last_int_supreg)
  1979. Revision 1.57 2003/07/02 22:18:04 peter
  1980. * paraloc splitted in callerparaloc,calleeparaloc
  1981. * sparc calling convention updates
  1982. Revision 1.56 2003/06/17 16:34:44 jonas
  1983. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1984. * renamed all_intregisters to volatile_intregisters and made it
  1985. processor dependent
  1986. Revision 1.55 2003/06/14 14:53:50 jonas
  1987. * fixed newra cycle for x86
  1988. * added constants for indicating source and destination operands of the
  1989. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1990. Revision 1.54 2003/06/13 21:19:31 peter
  1991. * current_procdef removed, use current_procinfo.procdef instead
  1992. Revision 1.53 2003/06/12 21:11:10 peter
  1993. * ungetregisterfpu gets size parameter
  1994. Revision 1.52 2003/06/12 16:43:07 peter
  1995. * newra compiles for sparc
  1996. Revision 1.51 2003/06/09 14:54:26 jonas
  1997. * (de)allocation of registers for parameters is now performed properly
  1998. (and checked on the ppc)
  1999. - removed obsolete allocation of all parameter registers at the start
  2000. of a procedure (and deallocation at the end)
  2001. Revision 1.50 2003/06/03 21:11:09 peter
  2002. * cg.a_load_* get a from and to size specifier
  2003. * makeregsize only accepts newregister
  2004. * i386 uses generic tcgnotnode,tcgunaryminus
  2005. Revision 1.49 2003/06/03 13:01:59 daniel
  2006. * Register allocator finished
  2007. Revision 1.48 2003/06/01 21:38:06 peter
  2008. * getregisterfpu size parameter added
  2009. * op_const_reg size parameter added
  2010. * sparc updates
  2011. Revision 1.47 2003/05/31 20:31:11 jonas
  2012. * set inital costs of assigning a variable to a register to 120 for
  2013. non-i386, because the used register must be store to memory at the
  2014. start and loaded again at the end
  2015. Revision 1.46 2003/05/30 18:55:21 jonas
  2016. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  2017. works for ppc
  2018. Revision 1.45 2003/05/30 12:36:13 jonas
  2019. * use as little different registers on the ppc until newra is released,
  2020. since every used register must be saved
  2021. Revision 1.44 2003/05/17 13:30:08 jonas
  2022. * changed tt_persistant to tt_persistent :)
  2023. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2024. temps, but a ttemptype, so you can also create ansistring temps etc
  2025. Revision 1.43 2003/05/16 14:33:31 peter
  2026. * regvar fixes
  2027. Revision 1.42 2003/04/26 20:03:49 daniel
  2028. * Bug fix in simplify
  2029. Revision 1.41 2003/04/25 20:59:35 peter
  2030. * removed funcretn,funcretsym, function result is now in varsym
  2031. and aliases for result and function name are added using absolutesym
  2032. * vs_hidden parameter for funcret passed in parameter
  2033. * vs_hidden fixes
  2034. * writenode changed to printnode and released from extdebug
  2035. * -vp option added to generate a tree.log with the nodetree
  2036. * nicer printnode for statements, callnode
  2037. Revision 1.40 2003/04/25 08:25:26 daniel
  2038. * Ifdefs around a lot of calls to cleartempgen
  2039. * Fixed registers that are allocated but not freed in several nodes
  2040. * Tweak to register allocator to cause less spills
  2041. * 8-bit registers now interfere with esi,edi and ebp
  2042. Compiler can now compile rtl successfully when using new register
  2043. allocator
  2044. Revision 1.39 2003/04/23 20:23:06 peter
  2045. * compile fix for no-newra
  2046. Revision 1.38 2003/04/23 14:42:07 daniel
  2047. * Further register allocator work. Compiler now smaller with new
  2048. allocator than without.
  2049. * Somebody forgot to adjust ppu version number
  2050. Revision 1.37 2003/04/22 23:50:23 peter
  2051. * firstpass uses expectloc
  2052. * checks if there are differences between the expectloc and
  2053. location.loc from secondpass in EXTDEBUG
  2054. Revision 1.36 2003/04/22 10:09:35 daniel
  2055. + Implemented the actual register allocator
  2056. + Scratch registers unavailable when new register allocator used
  2057. + maybe_save/maybe_restore unavailable when new register allocator used
  2058. Revision 1.35 2003/04/21 19:16:49 peter
  2059. * count address regs separate
  2060. Revision 1.34 2003/04/17 16:48:21 daniel
  2061. * Added some code to keep track of move instructions in register
  2062. allocator
  2063. Revision 1.33 2003/04/17 07:50:24 daniel
  2064. * Some work on interference graph construction
  2065. Revision 1.32 2003/03/28 19:16:57 peter
  2066. * generic constructor working for i386
  2067. * remove fixed self register
  2068. * esi added as address register for i386
  2069. Revision 1.31 2003/03/11 21:46:24 jonas
  2070. * lots of new regallocator fixes, both in generic and ppc-specific code
  2071. (ppc compiler still can't compile the linux system unit though)
  2072. Revision 1.30 2003/03/09 21:18:59 olle
  2073. + added cutils to the uses clause
  2074. Revision 1.29 2003/03/08 20:36:41 daniel
  2075. + Added newra version of Ti386shlshrnode
  2076. + Added interference graph construction code
  2077. Revision 1.28 2003/03/08 13:59:16 daniel
  2078. * Work to handle new register notation in ag386nsm
  2079. + Added newra version of Ti386moddivnode
  2080. Revision 1.27 2003/03/08 10:53:48 daniel
  2081. * Created newra version of secondmul in n386add.pas
  2082. Revision 1.26 2003/03/08 08:59:07 daniel
  2083. + $define newra will enable new register allocator
  2084. + getregisterint will return imaginary registers with $newra
  2085. + -sr switch added, will skip register allocation so you can see
  2086. the direct output of the code generator before register allocation
  2087. Revision 1.25 2003/02/26 20:50:45 daniel
  2088. * Fixed ungetreference
  2089. Revision 1.24 2003/02/19 22:39:56 daniel
  2090. * Fixed a few issues
  2091. Revision 1.23 2003/02/19 22:00:14 daniel
  2092. * Code generator converted to new register notation
  2093. - Horribily outdated todo.txt removed
  2094. Revision 1.22 2003/02/02 19:25:54 carl
  2095. * Several bugfixes for m68k target (register alloc., opcode emission)
  2096. + VIS target
  2097. + Generic add more complete (still not verified)
  2098. Revision 1.21 2003/01/08 18:43:57 daniel
  2099. * Tregister changed into a record
  2100. Revision 1.20 2002/10/05 12:43:28 carl
  2101. * fixes for Delphi 6 compilation
  2102. (warning : Some features do not work under Delphi)
  2103. Revision 1.19 2002/08/23 16:14:49 peter
  2104. * tempgen cleanup
  2105. * tt_noreuse temp type added that will be used in genentrycode
  2106. Revision 1.18 2002/08/17 22:09:47 florian
  2107. * result type handling in tcgcal.pass_2 overhauled
  2108. * better tnode.dowrite
  2109. * some ppc stuff fixed
  2110. Revision 1.17 2002/08/17 09:23:42 florian
  2111. * first part of procinfo rewrite
  2112. Revision 1.16 2002/08/06 20:55:23 florian
  2113. * first part of ppc calling conventions fix
  2114. Revision 1.15 2002/08/05 18:27:48 carl
  2115. + more more more documentation
  2116. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2117. Revision 1.14 2002/08/04 19:06:41 carl
  2118. + added generic exception support (still does not work!)
  2119. + more documentation
  2120. Revision 1.13 2002/07/07 09:52:32 florian
  2121. * powerpc target fixed, very simple units can be compiled
  2122. * some basic stuff for better callparanode handling, far from being finished
  2123. Revision 1.12 2002/07/01 18:46:26 peter
  2124. * internal linker
  2125. * reorganized aasm layer
  2126. Revision 1.11 2002/05/18 13:34:17 peter
  2127. * readded missing revisions
  2128. Revision 1.10 2002/05/16 19:46:44 carl
  2129. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2130. + try to fix temp allocation (still in ifdef)
  2131. + generic constructor calls
  2132. + start of tassembler / tmodulebase class cleanup
  2133. Revision 1.8 2002/04/21 15:23:03 carl
  2134. + makeregsize
  2135. + changeregsize is now a local routine
  2136. Revision 1.7 2002/04/20 21:32:25 carl
  2137. + generic FPC_CHECKPOINTER
  2138. + first parameter offset in stack now portable
  2139. * rename some constants
  2140. + move some cpu stuff to other units
  2141. - remove unused constents
  2142. * fix stacksize for some targets
  2143. * fix generic size problems which depend now on EXTEND_SIZE constant
  2144. Revision 1.6 2002/04/15 19:03:31 carl
  2145. + reg2str -> std_reg2str()
  2146. Revision 1.5 2002/04/06 18:13:01 jonas
  2147. * several powerpc-related additions and fixes
  2148. Revision 1.4 2002/04/04 19:06:04 peter
  2149. * removed unused units
  2150. * use tlocation.size in cg.a_*loc*() routines
  2151. Revision 1.3 2002/04/02 17:11:29 peter
  2152. * tlocation,treference update
  2153. * LOC_CONSTANT added for better constant handling
  2154. * secondadd splitted in multiple routines
  2155. * location_force_reg added for loading a location to a register
  2156. of a specified size
  2157. * secondassignment parses now first the right and then the left node
  2158. (this is compatible with Kylix). This saves a lot of push/pop especially
  2159. with string operations
  2160. * adapted some routines to use the new cg methods
  2161. Revision 1.2 2002/04/01 19:24:25 jonas
  2162. * fixed different parameter name in interface and implementation
  2163. declaration of a method (only 1.0.x detected this)
  2164. Revision 1.1 2002/03/31 20:26:36 jonas
  2165. + a_loadfpu_* and a_loadmm_* methods in tcg
  2166. * register allocation is now handled by a class and is mostly processor
  2167. independent (+rgobj.pas and i386/rgcpu.pas)
  2168. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2169. * some small improvements and fixes to the optimizer
  2170. * some register allocation fixes
  2171. * some fpuvaroffset fixes in the unary minus node
  2172. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2173. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2174. also better optimizable)
  2175. * fixed and optimized register saving/restoring for new/dispose nodes
  2176. * LOC_FPU locations now also require their "register" field to be set to
  2177. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2178. - list field removed of the tnode class because it's not used currently
  2179. and can cause hard-to-find bugs
  2180. }