rgobj.pas 68 KB

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