rgobj.pas 71 KB

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