rgobj.pas 68 KB

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