rgobj.pas 78 KB

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