rgobj.pas 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  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] 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 : byte;
  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:=255;
  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. add_edges_used(supreg);
  554. add_constraints(r);
  555. end;
  556. end;
  557. procedure trgobj.getexplicitregister(list:Taasmoutput;r:Tregister);
  558. var supreg:Tsuperregister;
  559. begin
  560. supreg:=getsupreg(r);
  561. if supregset_in(unusedregs,supreg) then
  562. begin
  563. supregset_exclude(unusedregs,supreg);
  564. if supreg<first_imaginary then
  565. include(used_in_proc,supreg);
  566. list.concat(Tai_regalloc.alloc(r));
  567. add_edges_used(supreg);
  568. add_constraints(r);
  569. end
  570. else
  571. {$ifndef ALLOWDUPREG}
  572. internalerror(200301103)
  573. {$else ALLOWDUPREG}
  574. list.concat(Tai_regalloc.alloc(r));
  575. {$endif ALLOWDUPREG}
  576. ;
  577. end;
  578. procedure trgobj.allocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  579. var reg:Tregister;
  580. i:Tsuperregister;
  581. begin
  582. if unusedregs[0]*r=r then
  583. begin
  584. unusedregs[0]:=unusedregs[0]-r;
  585. used_in_proc:=used_in_proc+r;
  586. for i:=0 to first_imaginary-1 do
  587. if i in r then
  588. begin
  589. add_edges_used(i);
  590. reg:=newreg(regtype,i,R_SUBWHOLE);
  591. list.concat(Tai_regalloc.alloc(reg));
  592. end;
  593. end
  594. else
  595. {$ifndef ALLOWDUPREG}
  596. internalerror(200305061)
  597. {$else ALLOWDUPREG}
  598. list.concat(Tai_regalloc.alloc(reg));
  599. {$endif ALLOWDUPREG}
  600. ;
  601. end;
  602. procedure trgobj.deallocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  603. var reg:Tregister;
  604. i:Tsuperregister;
  605. begin
  606. if unusedregs[0]*r=[] then
  607. begin
  608. unusedregs[0]:=unusedregs[0]+r;
  609. for i:=first_imaginary-1 downto 0 do
  610. if i in r then
  611. begin
  612. reg:=newreg(regtype,i,R_SUBWHOLE);
  613. list.concat(Tai_regalloc.dealloc(reg));
  614. end;
  615. end
  616. else
  617. {$ifndef ALLOWDUPREG}
  618. internalerror(200305061);
  619. {$else ALLOWDUPREG}
  620. list.concat(Tai_regalloc.dealloc(reg));
  621. {$endif ALLOWDUPREG}
  622. end;
  623. procedure trgobj.do_register_allocation(list:Taasmoutput;headertai:tai);
  624. var
  625. spillingcounter:byte;
  626. endspill:boolean;
  627. begin
  628. {Do register allocation.}
  629. spillingcounter:=0;
  630. repeat
  631. prepare_colouring;
  632. colour_registers;
  633. epilogue_colouring;
  634. endspill:=true;
  635. if spillednodes.length<>0 then
  636. begin
  637. inc(spillingcounter);
  638. if spillingcounter>maxspillingcounter then
  639. internalerror(200309041);
  640. endspill:=not spill_registers(list,headertai);
  641. end;
  642. until endspill;
  643. end;
  644. procedure trgobj.add_constraints(reg:Tregister);
  645. begin
  646. end;
  647. procedure trgobj.add_edge(u,v:Tsuperregister);
  648. {This procedure will add an edge to the virtual interference graph.}
  649. procedure addadj(u,v:Tsuperregister);
  650. begin
  651. if reginfo[u].adjlist=nil then
  652. new(reginfo[u].adjlist,init);
  653. reginfo[u].adjlist^.add(v);
  654. end;
  655. begin
  656. if (u<>v) and not(ibitmap[v,u]) then
  657. begin
  658. ibitmap[v,u]:=true;
  659. ibitmap[u,v]:=true;
  660. {Precoloured nodes are not stored in the interference graph.}
  661. if (u>=first_imaginary) then
  662. begin
  663. addadj(u,v);
  664. inc(reginfo[u].degree);
  665. end;
  666. if (v>=first_imaginary) then
  667. begin
  668. addadj(v,u);
  669. inc(reginfo[v].degree);
  670. end;
  671. end;
  672. end;
  673. procedure trgobj.add_edges_used(u:Tsuperregister);
  674. var i:Tsuperregister;
  675. begin
  676. for i:=0 to maxreg-1 do
  677. if not(supregset_in(unusedregs,i)) then
  678. add_edge(u,i);
  679. end;
  680. {$ifdef EXTDEBUG}
  681. procedure trgobj.writegraph(loopidx:longint);
  682. {This procedure writes out the current interference graph in the
  683. register allocator.}
  684. var f:text;
  685. i,j:Tsuperregister;
  686. begin
  687. assign(f,'igraph'+tostr(loopidx));
  688. rewrite(f);
  689. writeln(f,'Interference graph');
  690. writeln(f);
  691. write(f,' ');
  692. for i:=0 to 15 do
  693. for j:=0 to 15 do
  694. write(f,hexstr(i,1));
  695. writeln(f);
  696. write(f,' ');
  697. for i:=0 to 15 do
  698. write(f,'0123456789ABCDEF');
  699. writeln(f);
  700. for i:=0 to maxreg-1 do
  701. begin
  702. write(f,hexstr(i,2):4);
  703. for j:=0 to maxreg-1 do
  704. if ibitmap[i,j] then
  705. write(f,'*')
  706. else
  707. write(f,'-');
  708. writeln(f);
  709. end;
  710. close(f);
  711. end;
  712. {$endif EXTDEBUG}
  713. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  714. begin
  715. if reginfo[u].movelist=nil then
  716. begin
  717. getmem(reginfo[u].movelist,64);
  718. reginfo[u].movelist^.count:=0;
  719. end
  720. else if (reginfo[u].movelist^.count and 15)=15 then
  721. reallocmem(reginfo[u].movelist,(reginfo[u].movelist^.count+1)*4+64);
  722. reginfo[u].movelist^.data[reginfo[u].movelist^.count]:=data;
  723. inc(reginfo[u].movelist^.count);
  724. end;
  725. procedure trgobj.add_move_instruction(instr:Taicpu);
  726. {This procedure notifies a certain as a move instruction so the
  727. register allocator can try to eliminate it.}
  728. var i:Tmoveins;
  729. ssupreg,dsupreg:Tsuperregister;
  730. begin
  731. i:=Tmoveins.create;
  732. i.moveset:=ms_worklist_moves;
  733. i.instruction:=instr;
  734. worklist_moves.insert(i);
  735. ssupreg:=getsupreg(instr.oper[O_MOV_SOURCE]^.reg);
  736. add_to_movelist(ssupreg,i);
  737. dsupreg:=getsupreg(instr.oper[O_MOV_DEST]^.reg);
  738. if ssupreg<>dsupreg then
  739. {Avoid adding the same move instruction twice to a single register.}
  740. add_to_movelist(dsupreg,i);
  741. i.x:=ssupreg;
  742. i.y:=dsupreg;
  743. end;
  744. function trgobj.move_related(n:Tsuperregister):boolean;
  745. var i:cardinal;
  746. begin
  747. move_related:=false;
  748. if reginfo[n].movelist<>nil then
  749. begin
  750. for i:=0 to reginfo[n].movelist^.count-1 do
  751. if Tmoveins(reginfo[n].movelist^.data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  752. begin
  753. move_related:=true;
  754. break;
  755. end;
  756. end;
  757. end;
  758. procedure trgobj.make_work_list;
  759. var n:Tsuperregister;
  760. begin
  761. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  762. assign it to any of the registers, thus it is significant.}
  763. for n:=first_imaginary to maxreg-1 do
  764. if reginfo[n].degree>=usable_registers_cnt then
  765. spillworklist.add(n)
  766. else if move_related(n) then
  767. freezeworklist.add(n)
  768. else
  769. simplifyworklist.add(n);
  770. end;
  771. procedure trgobj.prepare_colouring;
  772. var
  773. i : integer;
  774. begin
  775. make_work_list;
  776. active_moves:=Tlinkedlist.create;
  777. frozen_moves:=Tlinkedlist.create;
  778. coalesced_moves:=Tlinkedlist.create;
  779. constrained_moves:=Tlinkedlist.create;
  780. for i:=0 to maxreg-1 do
  781. reginfo[i].alias:=RS_INVALID;
  782. coalescednodes.clear;
  783. selectstack.clear;
  784. end;
  785. procedure trgobj.enable_moves(n:Tsuperregister);
  786. var m:Tlinkedlistitem;
  787. i:cardinal;
  788. begin
  789. if reginfo[n].movelist<>nil then
  790. for i:=0 to reginfo[n].movelist^.count-1 do
  791. begin
  792. m:=reginfo[n].movelist^.data[i];
  793. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  794. begin
  795. if Tmoveins(m).moveset=ms_active_moves then
  796. begin
  797. {Move m from the set active_moves to the set worklist_moves.}
  798. active_moves.remove(m);
  799. Tmoveins(m).moveset:=ms_worklist_moves;
  800. worklist_moves.concat(m);
  801. end;
  802. end;
  803. end;
  804. end;
  805. procedure trgobj.decrement_degree(m:Tsuperregister);
  806. var adj : Psuperregisterworklist;
  807. d : byte;
  808. 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. min : byte;
  844. p,n : Tsuperregister;
  845. i : integer;
  846. begin
  847. {We the element with the least interferences out of the
  848. simplifyworklist.}
  849. min:=$ff;
  850. p:=0;
  851. n:=0;
  852. i:=simplifyworklist.head;
  853. while (i<>simplifyworklist.tail) do
  854. begin
  855. adj:=reginfo[simplifyworklist.buf[i]].adjlist;
  856. if adj=nil then
  857. begin
  858. p:=i;
  859. min:=0;
  860. break; {We won't find smaller ones.}
  861. end
  862. else
  863. if adj^.length<min then
  864. begin
  865. p:=i;
  866. min:=adj^.length;
  867. if min=0 then
  868. break; {We won't find smaller ones.}
  869. end;
  870. simplifyworklist.next(i);
  871. end;
  872. n:=simplifyworklist.getidx(p);
  873. simplifyworklist.deleteidx(p);
  874. {Push it on the selectstack.}
  875. selectstack.add(n);
  876. adj:=reginfo[n].adjlist;
  877. if adj<>nil then
  878. begin
  879. i:=adj^.head;
  880. while (i<>adj^.tail) do
  881. begin
  882. n:=adj^.buf[i];
  883. if (n>first_imaginary) and
  884. not(selectstack.find(n) or
  885. coalescednodes.find(n)) then
  886. decrement_degree(n);
  887. adj^.next(i);
  888. end;
  889. end;
  890. end;
  891. function trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  892. begin
  893. while coalescednodes.find(n) do
  894. n:=reginfo[n].alias;
  895. get_alias:=n;
  896. end;
  897. procedure trgobj.add_worklist(u:Tsuperregister);
  898. begin
  899. if (u>=first_imaginary) and
  900. not move_related(u) and
  901. (reginfo[u].degree<usable_registers_cnt) then
  902. begin
  903. if not freezeworklist.delete(u) then
  904. internalerror(200308161); {must be found}
  905. simplifyworklist.add(u);
  906. end;
  907. end;
  908. function trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  909. {Check wether u and v should be coalesced. u is precoloured.}
  910. function ok(t,r:Tsuperregister):boolean;
  911. begin
  912. ok:=(reginfo[t].degree<usable_registers_cnt) or
  913. (t<first_imaginary) or
  914. ibitmap[r,t];
  915. end;
  916. var adj : Psuperregisterworklist;
  917. i : integer;
  918. n : tsuperregister;
  919. begin
  920. adjacent_ok:=true;
  921. adj:=reginfo[v].adjlist;
  922. if adj<>nil then
  923. begin
  924. i:=adj^.head;
  925. while (i<>adj^.tail) do
  926. begin
  927. n:=adj^.buf[i];
  928. if not(selectstack.find(n) or
  929. coalescednodes.find(n)) and
  930. not ok(n,u) then
  931. begin
  932. adjacent_ok:=false;
  933. break;
  934. end;
  935. adj^.next(i);
  936. end;
  937. end;
  938. end;
  939. function trgobj.conservative(u,v:Tsuperregister):boolean;
  940. var adj : Psuperregisterworklist;
  941. done : Tsuperregisterset; {To prevent that we count nodes twice.}
  942. i,k : integer;
  943. n : tsuperregister;
  944. begin
  945. k:=0;
  946. supregset_reset(done,false);
  947. adj:=reginfo[u].adjlist;
  948. if adj<>nil then
  949. begin
  950. i:=adj^.head;
  951. while (i<>adj^.tail) do
  952. begin
  953. n:=adj^.buf[i];
  954. if not(selectstack.find(n) or
  955. coalescednodes.find(n)) then
  956. begin
  957. supregset_include(done,n);
  958. if reginfo[n].degree>=usable_registers_cnt then
  959. inc(k);
  960. end;
  961. adj^.next(i);
  962. end;
  963. end;
  964. adj:=reginfo[v].adjlist;
  965. if adj<>nil then
  966. begin
  967. i:=adj^.head;
  968. while (i<>adj^.tail) do
  969. begin
  970. n:=adj^.buf[i];
  971. if not supregset_in(done,n) and
  972. (reginfo[n].degree>=usable_registers_cnt) and
  973. not(selectstack.find(n) or
  974. coalescednodes.find(n)) then
  975. inc(k);
  976. adj^.next(i);
  977. end;
  978. end;
  979. conservative:=(k<usable_registers_cnt);
  980. end;
  981. procedure trgobj.combine(u,v:Tsuperregister);
  982. var add : boolean;
  983. adj : Psuperregisterworklist;
  984. i : integer;
  985. t : tsuperregister;
  986. n,o : cardinal;
  987. decrement : boolean;
  988. begin
  989. if not freezeworklist.delete(v) then
  990. spillworklist.delete(v);
  991. coalescednodes.add(v);
  992. reginfo[v].alias:=u;
  993. {Combine both movelists. Since the movelists are sets, only add
  994. elements that are not already present.}
  995. if assigned(reginfo[v].movelist) then
  996. begin
  997. for n:=0 to reginfo[v].movelist^.count-1 do
  998. begin
  999. add:=true;
  1000. for o:=0 to reginfo[u].movelist^.count-1 do
  1001. if reginfo[u].movelist^.data[o]=reginfo[v].movelist^.data[n] then
  1002. begin
  1003. add:=false;
  1004. break;
  1005. end;
  1006. if add then
  1007. add_to_movelist(u,reginfo[v].movelist^.data[n]);
  1008. end;
  1009. enable_moves(v);
  1010. end;
  1011. adj:=reginfo[v].adjlist;
  1012. if adj<>nil then
  1013. begin
  1014. i:=adj^.head;
  1015. while (i<>adj^.tail) do
  1016. begin
  1017. t:=adj^.buf[i];
  1018. if not(selectstack.find(t) or
  1019. coalescednodes.find(t)) then
  1020. begin
  1021. decrement:=(t<>u) and not(ibitmap[u,t]);
  1022. add_edge(t,u);
  1023. { Do not call decrement_degree because it might move nodes between
  1024. lists while the degree does not change (add_edge will increase it).
  1025. Instead, we will decrement manually. (Only if the degree has been
  1026. increased.) }
  1027. if decrement and
  1028. (t>=first_imaginary) and
  1029. (reginfo[t].degree>0) then
  1030. dec(reginfo[t].degree);
  1031. end;
  1032. adj^.next(i);
  1033. end;
  1034. end;
  1035. if (reginfo[u].degree>=usable_registers_cnt) and
  1036. freezeworklist.delete(u) then
  1037. spillworklist.add(u);
  1038. end;
  1039. procedure trgobj.coalesce;
  1040. var m:Tmoveins;
  1041. x,y,u,v:Tsuperregister;
  1042. begin
  1043. m:=Tmoveins(worklist_moves.getfirst);
  1044. x:=get_alias(getsupreg(m.instruction.oper[0]^.reg));
  1045. y:=get_alias(getsupreg(m.instruction.oper[1]^.reg));
  1046. if (y<first_imaginary) then
  1047. begin
  1048. u:=y;
  1049. v:=x;
  1050. end
  1051. else
  1052. begin
  1053. u:=x;
  1054. v:=y;
  1055. end;
  1056. if (u=v) then
  1057. begin
  1058. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1059. coalesced_moves.insert(m);
  1060. add_worklist(u);
  1061. end
  1062. {Do u and v interfere? In that case the move is constrained. Two
  1063. precoloured nodes interfere allways. If v is precoloured, by the above
  1064. code u is precoloured, thus interference...}
  1065. else if (v<first_imaginary) or ibitmap[u,v] then
  1066. begin
  1067. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1068. constrained_moves.insert(m);
  1069. add_worklist(u);
  1070. add_worklist(v);
  1071. end
  1072. {Next test: is it possible and a good idea to coalesce??}
  1073. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  1074. ((u>=first_imaginary) and conservative(u,v)) then
  1075. begin
  1076. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1077. coalesced_moves.insert(m);
  1078. combine(u,v);
  1079. add_worklist(u);
  1080. end
  1081. else
  1082. begin
  1083. m.moveset:=ms_active_moves;
  1084. active_moves.insert(m);
  1085. end;
  1086. end;
  1087. procedure trgobj.freeze_moves(u:Tsuperregister);
  1088. var i:cardinal;
  1089. m:Tlinkedlistitem;
  1090. v,x,y:Tsuperregister;
  1091. begin
  1092. if reginfo[u].movelist<>nil then
  1093. for i:=0 to reginfo[u].movelist^.count-1 do
  1094. begin
  1095. m:=reginfo[u].movelist^.data[i];
  1096. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1097. begin
  1098. x:=getsupreg(Tmoveins(m).instruction.oper[0]^.reg);
  1099. y:=getsupreg(Tmoveins(m).instruction.oper[1]^.reg);
  1100. if get_alias(y)=get_alias(u) then
  1101. v:=get_alias(x)
  1102. else
  1103. v:=get_alias(y);
  1104. {Move m from active_moves/worklist_moves to frozen_moves.}
  1105. if Tmoveins(m).moveset=ms_active_moves then
  1106. active_moves.remove(m)
  1107. else
  1108. worklist_moves.remove(m);
  1109. Tmoveins(m).moveset:=ms_frozen_moves;
  1110. frozen_moves.insert(m);
  1111. if (v>=first_imaginary) and
  1112. not(move_related(v)) and
  1113. (reginfo[v].degree<usable_registers_cnt) then
  1114. begin
  1115. freezeworklist.delete(v);
  1116. simplifyworklist.add(v);
  1117. end;
  1118. end;
  1119. end;
  1120. end;
  1121. procedure trgobj.freeze;
  1122. var n:Tsuperregister;
  1123. begin
  1124. { We need to take a random element out of the freezeworklist. We take
  1125. the last element. Dirty code! }
  1126. n:=freezeworklist.get;
  1127. {Add it to the simplifyworklist.}
  1128. simplifyworklist.add(n);
  1129. freeze_moves(n);
  1130. end;
  1131. procedure trgobj.select_spill;
  1132. var
  1133. n : tsuperregister;
  1134. adj : psuperregisterworklist;
  1135. max,p,i : integer;
  1136. begin
  1137. { We must look for the element with the most interferences in the
  1138. spillworklist. This is required because those registers are creating
  1139. the most conflicts and keeping them in a register will not reduce the
  1140. complexity and even can cause the help registers for the spilling code
  1141. to get too much conflicts with the result that the spilling code
  1142. will never converge (PFV) }
  1143. max:=0;
  1144. p:=0;
  1145. i:=spillworklist.head;
  1146. while (i<>spillworklist.tail) do
  1147. begin
  1148. adj:=reginfo[spillworklist.buf[i]].adjlist;
  1149. if assigned(adj) and
  1150. (adj^.length>max) then
  1151. begin
  1152. p:=i;
  1153. max:=adj^.length;
  1154. end;
  1155. spillworklist.next(i);
  1156. end;
  1157. n:=spillworklist.getidx(p);
  1158. spillworklist.deleteidx(p);
  1159. simplifyworklist.add(n);
  1160. freeze_moves(n);
  1161. end;
  1162. procedure trgobj.assign_colours;
  1163. {Assign_colours assigns the actual colours to the registers.}
  1164. var adj : Psuperregisterworklist;
  1165. i,j,k : integer;
  1166. n,a,c : Tsuperregister;
  1167. adj_colours,
  1168. colourednodes : Tsuperregisterset;
  1169. found : boolean;
  1170. begin
  1171. spillednodes.clear;
  1172. {Reset colours}
  1173. for n:=0 to maxreg-1 do
  1174. reginfo[n].colour:=n;
  1175. {Colour the cpu registers...}
  1176. supregset_reset(colourednodes,false);
  1177. for n:=0 to first_imaginary-1 do
  1178. supregset_include(colourednodes,n);
  1179. {Now colour the imaginary registers on the select-stack.}
  1180. while (selectstack.length>0) do
  1181. begin
  1182. n:=selectstack.getlast;
  1183. {Create a list of colours that we cannot assign to n.}
  1184. supregset_reset(adj_colours,false);
  1185. adj:=reginfo[n].adjlist;
  1186. if adj<>nil then
  1187. begin
  1188. j:=adj^.head;
  1189. while (j<>adj^.tail) do
  1190. begin
  1191. a:=get_alias(adj^.buf[j]);
  1192. if supregset_in(colourednodes,a) then
  1193. supregset_include(adj_colours,reginfo[a].colour);
  1194. adj^.next(j);
  1195. end;
  1196. supregset_include(adj_colours,RS_STACK_POINTER_REG);
  1197. end;
  1198. {Assume a spill by default...}
  1199. found:=false;
  1200. {Search for a colour not in this list.}
  1201. for k:=0 to usable_registers_cnt-1 do
  1202. begin
  1203. c:=usable_registers[k];
  1204. if not(supregset_in(adj_colours,c)) then
  1205. begin
  1206. reginfo[n].colour:=c;
  1207. found:=true;
  1208. supregset_include(colourednodes,n);
  1209. include(used_in_proc,c);
  1210. break;
  1211. end;
  1212. end;
  1213. if not found then
  1214. spillednodes.add(n);
  1215. end;
  1216. {Finally colour the nodes that were coalesced.}
  1217. i:=coalescednodes.head;
  1218. while (i<>coalescednodes.tail) do
  1219. begin
  1220. n:=coalescednodes.buf[i];
  1221. k:=get_alias(n);
  1222. reginfo[n].colour:=reginfo[k].colour;
  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. add_edges_used(supreg);
  1413. add_constraints(r);
  1414. end;
  1415. function trgobj.spill_registers(list:Taasmoutput;headertai:tai):boolean;
  1416. {Returns true if any help registers have been used.}
  1417. var i : integer;
  1418. t : tsuperregister;
  1419. p,q : Tai;
  1420. regs_to_spill_set : Tsuperregisterset;
  1421. spill_temps : ^Tspill_temp_list;
  1422. supreg : tsuperregister;
  1423. templist : taasmoutput;
  1424. begin
  1425. spill_registers:=false;
  1426. supregset_reset(unusedregs,true);
  1427. {Precoloured nodes should have an infinite degree, which we can approach
  1428. by 255.}
  1429. for i:=0 to first_imaginary-1 do
  1430. reginfo[i].degree:=255;
  1431. for i:=first_imaginary to maxreg-1 do
  1432. reginfo[i].degree:=0;
  1433. { exclude(unusedregs,RS_STACK_POINTER_REG);}
  1434. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1435. {Make sure the register allocator won't allocate registers into ebp.}
  1436. supregset_exclude(unusedregs,RS_FRAME_POINTER_REG);
  1437. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1438. supregset_reset(regs_to_spill_set,false);
  1439. { Allocate temps and insert in front of the list }
  1440. templist:=taasmoutput.create;
  1441. i:=spillednodes.head;
  1442. while (i<>spillednodes.tail) do
  1443. begin
  1444. t:=spillednodes.buf[i];
  1445. {Alternative representation.}
  1446. supregset_include(regs_to_spill_set,t);
  1447. {Clear all interferences of the spilled register.}
  1448. clear_interferences(t);
  1449. {Get a temp for the spilled register}
  1450. tg.gettemp(templist,4,tt_noreuse,spill_temps^[t]);
  1451. spillednodes.next(i);
  1452. end;
  1453. list.insertlistafter(headertai,templist);
  1454. templist.free;
  1455. { Walk through all instructions, we can start with the headertai,
  1456. because before the header tai is only symbols }
  1457. p:=headertai;
  1458. while assigned(p) do
  1459. begin
  1460. case p.typ of
  1461. ait_regalloc:
  1462. begin
  1463. if (getregtype(Tai_regalloc(p).reg)=regtype) then
  1464. begin
  1465. {A register allocation of a spilled register can be removed.}
  1466. supreg:=getsupreg(Tai_regalloc(p).reg);
  1467. if supregset_in(regs_to_spill_set,supreg) then
  1468. begin
  1469. q:=Tai(p.next);
  1470. list.remove(p);
  1471. p.free;
  1472. p:=q;
  1473. continue;
  1474. end
  1475. else
  1476. if Tai_regalloc(p).allocation then
  1477. supregset_exclude(unusedregs,supreg)
  1478. else
  1479. supregset_include(unusedregs,supreg);
  1480. end;
  1481. end;
  1482. ait_instruction:
  1483. begin
  1484. aktfilepos:=Taicpu_abstract(p).fileinfo;
  1485. if Taicpu_abstract(p).spill_registers(list,
  1486. @getregisterinline,
  1487. @ungetregisterinline,
  1488. regs_to_spill_set,
  1489. unusedregs,
  1490. spill_temps^) then
  1491. spill_registers:=true;
  1492. if Taicpu_abstract(p).is_move then
  1493. add_move_instruction(Taicpu(p));
  1494. end;
  1495. end;
  1496. p:=Tai(p.next);
  1497. end;
  1498. aktfilepos:=current_procinfo.exitpos;
  1499. i:=spillednodes.head;
  1500. while (i<>spillednodes.tail) do
  1501. begin
  1502. tg.ungettemp(list,spill_temps^[spillednodes.buf[i]]);
  1503. spillednodes.next(i);
  1504. end;
  1505. freemem(spill_temps);
  1506. end;
  1507. procedure Trgobj.translate_registers(list:taasmoutput);
  1508. var hp,p,q:Tai;
  1509. i:shortint;
  1510. r:Preference;
  1511. {$ifdef arm}
  1512. so:pshifterop;
  1513. {$endif arm}
  1514. begin
  1515. { Leave when no imaginary registers are used }
  1516. if maxreg<=first_imaginary then
  1517. exit;
  1518. p:=Tai(list.first);
  1519. while assigned(p) do
  1520. begin
  1521. case p.typ of
  1522. ait_regalloc:
  1523. begin
  1524. if (getregtype(Tai_regalloc(p).reg)=regtype) then
  1525. setsupreg(Tai_regalloc(p).reg,reginfo[getsupreg(Tai_regalloc(p).reg)].colour);
  1526. {
  1527. Remove sequences of release and
  1528. allocation of the same register like:
  1529. # Register X released
  1530. # Register X allocated
  1531. }
  1532. if assigned(p.previous) and
  1533. (Tai(p.previous).typ=ait_regalloc) and
  1534. (Tai_regalloc(p.previous).reg=Tai_regalloc(p).reg) and
  1535. { allocation,deallocation or deallocation,allocation }
  1536. (Tai_regalloc(p.previous).allocation xor Tai_regalloc(p).allocation) then
  1537. begin
  1538. q:=Tai(p.next);
  1539. hp:=tai(p.previous);
  1540. list.remove(hp);
  1541. hp.free;
  1542. list.remove(p);
  1543. p.free;
  1544. p:=q;
  1545. continue;
  1546. end;
  1547. end;
  1548. ait_instruction:
  1549. begin
  1550. for i:=0 to Taicpu_abstract(p).ops-1 do
  1551. case Taicpu_abstract(p).oper[i]^.typ of
  1552. Top_reg:
  1553. if (getregtype(Taicpu_abstract(p).oper[i]^.reg)=regtype) then
  1554. setsupreg(Taicpu_abstract(p).oper[i]^.reg,reginfo[getsupreg(Taicpu_abstract(p).oper[i]^.reg)].colour);
  1555. Top_ref:
  1556. begin
  1557. if regtype=R_INTREGISTER then
  1558. begin
  1559. r:=Taicpu_abstract(p).oper[i]^.ref;
  1560. if r^.base<>NR_NO then
  1561. setsupreg(r^.base,reginfo[getsupreg(r^.base)].colour);
  1562. if r^.index<>NR_NO then
  1563. setsupreg(r^.index,reginfo[getsupreg(r^.index)].colour);
  1564. end;
  1565. end;
  1566. {$ifdef arm}
  1567. Top_shifterop:
  1568. begin
  1569. so:=Taicpu_abstract(p).oper[i]^.shifterop;
  1570. if so^.rs<>NR_NO then
  1571. setsupreg(so^.rs,table[getsupreg(so^.rs)]);
  1572. end;
  1573. {$endif arm}
  1574. end;
  1575. { Maybe the operation can be removed when
  1576. it is a move and both arguments are the same }
  1577. if Taicpu_abstract(p).is_nop then
  1578. begin
  1579. q:=Tai(p.next);
  1580. list.remove(p);
  1581. p.free;
  1582. p:=q;
  1583. continue;
  1584. end;
  1585. end;
  1586. end;
  1587. p:=Tai(p.next);
  1588. end;
  1589. end;
  1590. procedure Trgobj.check_unreleasedregs;
  1591. {$ifdef EXTDEBUG}
  1592. var
  1593. sr : tsuperregister;
  1594. {$endif EXTDEBUG}
  1595. begin
  1596. {$ifdef EXTDEBUG}
  1597. for sr:=first_imaginary to maxreg-1 do
  1598. if not(supregset_in(unusedregs,sr)) then
  1599. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,sr,R_SUBNONE))+' not released');
  1600. {$endif EXTDEBUG}
  1601. end;
  1602. end.
  1603. {
  1604. $Log$
  1605. Revision 1.93 2003-10-30 16:22:40 peter
  1606. * call firstpass before allocation and codegeneration is started
  1607. * move leftover code from pass_2.generatecode() to psub
  1608. Revision 1.92 2003/10/29 21:29:14 jonas
  1609. * some ALLOWDUPREG improvements
  1610. Revision 1.91 2003/10/21 15:15:36 peter
  1611. * taicpu_abstract.oper[] changed to pointers
  1612. Revision 1.90 2003/10/19 12:36:36 florian
  1613. * improved speed; reduced memory usage of the interference bitmap
  1614. Revision 1.89 2003/10/19 01:34:30 florian
  1615. * some ppc stuff fixed
  1616. * memory leak fixed
  1617. Revision 1.88 2003/10/18 15:41:26 peter
  1618. * made worklists dynamic in size
  1619. Revision 1.87 2003/10/17 16:16:08 peter
  1620. * fixed last commit
  1621. Revision 1.86 2003/10/17 15:25:18 florian
  1622. * fixed more ppc stuff
  1623. Revision 1.85 2003/10/17 14:38:32 peter
  1624. * 64k registers supported
  1625. * fixed some memory leaks
  1626. Revision 1.84 2003/10/11 16:06:42 florian
  1627. * fixed some MMX<->SSE
  1628. * started to fix ppc, needs an overhaul
  1629. + stabs info improve for spilling, not sure if it works correctly/completly
  1630. - MMX_SUPPORT removed from Makefile.fpc
  1631. Revision 1.83 2003/10/10 17:48:14 peter
  1632. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1633. * tregisteralloctor renamed to trgobj
  1634. * removed rgobj from a lot of units
  1635. * moved location_* and reference_* to cgobj
  1636. * first things for mmx register allocation
  1637. Revision 1.82 2003/10/09 21:31:37 daniel
  1638. * Register allocator splitted, ans abstract now
  1639. Revision 1.81 2003/10/01 20:34:49 peter
  1640. * procinfo unit contains tprocinfo
  1641. * cginfo renamed to cgbase
  1642. * moved cgmessage to verbose
  1643. * fixed ppc and sparc compiles
  1644. Revision 1.80 2003/09/30 19:54:42 peter
  1645. * reuse registers with the least conflicts
  1646. Revision 1.79 2003/09/29 20:58:56 peter
  1647. * optimized releasing of registers
  1648. Revision 1.78 2003/09/28 13:41:12 peter
  1649. * return reg 255 when allowdupreg is defined
  1650. Revision 1.77 2003/09/25 16:19:32 peter
  1651. * fix filepositions
  1652. * insert spill temp allocations at the start of the proc
  1653. Revision 1.76 2003/09/16 16:17:01 peter
  1654. * varspez in calls to push_addr_param
  1655. Revision 1.75 2003/09/12 19:07:42 daniel
  1656. * Fixed fast spilling functionality by re-adding the code that initializes
  1657. precoloured nodes to degree 255. I would like to play hangman on the one
  1658. who removed that code.
  1659. Revision 1.74 2003/09/11 11:54:59 florian
  1660. * improved arm code generation
  1661. * move some protected and private field around
  1662. * the temp. register for register parameters/arguments are now released
  1663. before the move to the parameter register is done. This improves
  1664. the code in a lot of cases.
  1665. Revision 1.73 2003/09/09 20:59:27 daniel
  1666. * Adding register allocation order
  1667. Revision 1.72 2003/09/09 15:55:44 peter
  1668. * use register with least interferences in spillregister
  1669. Revision 1.71 2003/09/07 22:09:35 peter
  1670. * preparations for different default calling conventions
  1671. * various RA fixes
  1672. Revision 1.70 2003/09/03 21:06:45 peter
  1673. * fixes for FPU register allocation
  1674. Revision 1.69 2003/09/03 15:55:01 peter
  1675. * NEWRA branch merged
  1676. Revision 1.68 2003/09/03 11:18:37 florian
  1677. * fixed arm concatcopy
  1678. + arm support in the common compiler sources added
  1679. * moved some generic cg code around
  1680. + tfputype added
  1681. * ...
  1682. Revision 1.67.2.5 2003/08/31 20:44:07 peter
  1683. * fixed getexplicitregisterint tregister value
  1684. Revision 1.67.2.4 2003/08/31 20:40:50 daniel
  1685. * Fixed add_edges_used
  1686. Revision 1.67.2.3 2003/08/29 17:28:59 peter
  1687. * next batch of updates
  1688. Revision 1.67.2.2 2003/08/28 18:35:08 peter
  1689. * tregister changed to cardinal
  1690. Revision 1.67.2.1 2003/08/27 19:55:54 peter
  1691. * first tregister patch
  1692. Revision 1.67 2003/08/23 10:46:21 daniel
  1693. * Register allocator bugfix for h2pas
  1694. Revision 1.66 2003/08/17 16:59:20 jonas
  1695. * fixed regvars so they work with newra (at least for ppc)
  1696. * fixed some volatile register bugs
  1697. + -dnotranslation option for -dnewra, which causes the registers not to
  1698. be translated from virtual to normal registers. Requires support in
  1699. the assembler writer as well, which is only implemented in aggas/
  1700. agppcgas currently
  1701. Revision 1.65 2003/08/17 14:32:48 daniel
  1702. * Precoloured nodes now have an infinite degree approached with 255,
  1703. like they should.
  1704. Revision 1.64 2003/08/17 08:48:02 daniel
  1705. * Another register allocator bug fixed.
  1706. * usable_registers_cnt set to 6 for i386
  1707. Revision 1.63 2003/08/09 18:56:54 daniel
  1708. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1709. allocator
  1710. * Some preventive changes to i386 spillinh code
  1711. Revision 1.62 2003/08/03 14:09:50 daniel
  1712. * Fixed a register allocator bug
  1713. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1714. statements: changes in location_force. These moves are now no longer
  1715. constrained so they are optimized away.
  1716. Revision 1.61 2003/07/21 13:32:39 jonas
  1717. * add_edges_used() is now also called for registers allocated with
  1718. getexplicitregisterint()
  1719. * writing the intereference graph is now only done with -dradebug2 and
  1720. the created files are now called "igraph.<module_name>"
  1721. Revision 1.60 2003/07/06 15:31:21 daniel
  1722. * Fixed register allocator. *Lots* of fixes.
  1723. Revision 1.59 2003/07/06 15:00:47 jonas
  1724. * fixed my previous completely broken commit. It's not perfect though,
  1725. registers > last_int_supreg and < max_intreg may still be "translated"
  1726. Revision 1.58 2003/07/06 14:45:05 jonas
  1727. * support integer registers that are not managed by newra (ie. don't
  1728. translate register numbers that fall outside the range
  1729. first_int_supreg..last_int_supreg)
  1730. Revision 1.57 2003/07/02 22:18:04 peter
  1731. * paraloc splitted in callerparaloc,calleeparaloc
  1732. * sparc calling convention updates
  1733. Revision 1.56 2003/06/17 16:34:44 jonas
  1734. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1735. * renamed all_intregisters to volatile_intregisters and made it
  1736. processor dependent
  1737. Revision 1.55 2003/06/14 14:53:50 jonas
  1738. * fixed newra cycle for x86
  1739. * added constants for indicating source and destination operands of the
  1740. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1741. Revision 1.54 2003/06/13 21:19:31 peter
  1742. * current_procdef removed, use current_procinfo.procdef instead
  1743. Revision 1.53 2003/06/12 21:11:10 peter
  1744. * ungetregisterfpu gets size parameter
  1745. Revision 1.52 2003/06/12 16:43:07 peter
  1746. * newra compiles for sparc
  1747. Revision 1.51 2003/06/09 14:54:26 jonas
  1748. * (de)allocation of registers for parameters is now performed properly
  1749. (and checked on the ppc)
  1750. - removed obsolete allocation of all parameter registers at the start
  1751. of a procedure (and deallocation at the end)
  1752. Revision 1.50 2003/06/03 21:11:09 peter
  1753. * cg.a_load_* get a from and to size specifier
  1754. * makeregsize only accepts newregister
  1755. * i386 uses generic tcgnotnode,tcgunaryminus
  1756. Revision 1.49 2003/06/03 13:01:59 daniel
  1757. * Register allocator finished
  1758. Revision 1.48 2003/06/01 21:38:06 peter
  1759. * getregisterfpu size parameter added
  1760. * op_const_reg size parameter added
  1761. * sparc updates
  1762. Revision 1.47 2003/05/31 20:31:11 jonas
  1763. * set inital costs of assigning a variable to a register to 120 for
  1764. non-i386, because the used register must be store to memory at the
  1765. start and loaded again at the end
  1766. Revision 1.46 2003/05/30 18:55:21 jonas
  1767. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  1768. works for ppc
  1769. Revision 1.45 2003/05/30 12:36:13 jonas
  1770. * use as little different registers on the ppc until newra is released,
  1771. since every used register must be saved
  1772. Revision 1.44 2003/05/17 13:30:08 jonas
  1773. * changed tt_persistant to tt_persistent :)
  1774. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1775. temps, but a ttemptype, so you can also create ansistring temps etc
  1776. Revision 1.43 2003/05/16 14:33:31 peter
  1777. * regvar fixes
  1778. Revision 1.42 2003/04/26 20:03:49 daniel
  1779. * Bug fix in simplify
  1780. Revision 1.41 2003/04/25 20:59:35 peter
  1781. * removed funcretn,funcretsym, function result is now in varsym
  1782. and aliases for result and function name are added using absolutesym
  1783. * vs_hidden parameter for funcret passed in parameter
  1784. * vs_hidden fixes
  1785. * writenode changed to printnode and released from extdebug
  1786. * -vp option added to generate a tree.log with the nodetree
  1787. * nicer printnode for statements, callnode
  1788. Revision 1.40 2003/04/25 08:25:26 daniel
  1789. * Ifdefs around a lot of calls to cleartempgen
  1790. * Fixed registers that are allocated but not freed in several nodes
  1791. * Tweak to register allocator to cause less spills
  1792. * 8-bit registers now interfere with esi,edi and ebp
  1793. Compiler can now compile rtl successfully when using new register
  1794. allocator
  1795. Revision 1.39 2003/04/23 20:23:06 peter
  1796. * compile fix for no-newra
  1797. Revision 1.38 2003/04/23 14:42:07 daniel
  1798. * Further register allocator work. Compiler now smaller with new
  1799. allocator than without.
  1800. * Somebody forgot to adjust ppu version number
  1801. Revision 1.37 2003/04/22 23:50:23 peter
  1802. * firstpass uses expectloc
  1803. * checks if there are differences between the expectloc and
  1804. location.loc from secondpass in EXTDEBUG
  1805. Revision 1.36 2003/04/22 10:09:35 daniel
  1806. + Implemented the actual register allocator
  1807. + Scratch registers unavailable when new register allocator used
  1808. + maybe_save/maybe_restore unavailable when new register allocator used
  1809. Revision 1.35 2003/04/21 19:16:49 peter
  1810. * count address regs separate
  1811. Revision 1.34 2003/04/17 16:48:21 daniel
  1812. * Added some code to keep track of move instructions in register
  1813. allocator
  1814. Revision 1.33 2003/04/17 07:50:24 daniel
  1815. * Some work on interference graph construction
  1816. Revision 1.32 2003/03/28 19:16:57 peter
  1817. * generic constructor working for i386
  1818. * remove fixed self register
  1819. * esi added as address register for i386
  1820. Revision 1.31 2003/03/11 21:46:24 jonas
  1821. * lots of new regallocator fixes, both in generic and ppc-specific code
  1822. (ppc compiler still can't compile the linux system unit though)
  1823. Revision 1.30 2003/03/09 21:18:59 olle
  1824. + added cutils to the uses clause
  1825. Revision 1.29 2003/03/08 20:36:41 daniel
  1826. + Added newra version of Ti386shlshrnode
  1827. + Added interference graph construction code
  1828. Revision 1.28 2003/03/08 13:59:16 daniel
  1829. * Work to handle new register notation in ag386nsm
  1830. + Added newra version of Ti386moddivnode
  1831. Revision 1.27 2003/03/08 10:53:48 daniel
  1832. * Created newra version of secondmul in n386add.pas
  1833. Revision 1.26 2003/03/08 08:59:07 daniel
  1834. + $define newra will enable new register allocator
  1835. + getregisterint will return imaginary registers with $newra
  1836. + -sr switch added, will skip register allocation so you can see
  1837. the direct output of the code generator before register allocation
  1838. Revision 1.25 2003/02/26 20:50:45 daniel
  1839. * Fixed ungetreference
  1840. Revision 1.24 2003/02/19 22:39:56 daniel
  1841. * Fixed a few issues
  1842. Revision 1.23 2003/02/19 22:00:14 daniel
  1843. * Code generator converted to new register notation
  1844. - Horribily outdated todo.txt removed
  1845. Revision 1.22 2003/02/02 19:25:54 carl
  1846. * Several bugfixes for m68k target (register alloc., opcode emission)
  1847. + VIS target
  1848. + Generic add more complete (still not verified)
  1849. Revision 1.21 2003/01/08 18:43:57 daniel
  1850. * Tregister changed into a record
  1851. Revision 1.20 2002/10/05 12:43:28 carl
  1852. * fixes for Delphi 6 compilation
  1853. (warning : Some features do not work under Delphi)
  1854. Revision 1.19 2002/08/23 16:14:49 peter
  1855. * tempgen cleanup
  1856. * tt_noreuse temp type added that will be used in genentrycode
  1857. Revision 1.18 2002/08/17 22:09:47 florian
  1858. * result type handling in tcgcal.pass_2 overhauled
  1859. * better tnode.dowrite
  1860. * some ppc stuff fixed
  1861. Revision 1.17 2002/08/17 09:23:42 florian
  1862. * first part of procinfo rewrite
  1863. Revision 1.16 2002/08/06 20:55:23 florian
  1864. * first part of ppc calling conventions fix
  1865. Revision 1.15 2002/08/05 18:27:48 carl
  1866. + more more more documentation
  1867. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1868. Revision 1.14 2002/08/04 19:06:41 carl
  1869. + added generic exception support (still does not work!)
  1870. + more documentation
  1871. Revision 1.13 2002/07/07 09:52:32 florian
  1872. * powerpc target fixed, very simple units can be compiled
  1873. * some basic stuff for better callparanode handling, far from being finished
  1874. Revision 1.12 2002/07/01 18:46:26 peter
  1875. * internal linker
  1876. * reorganized aasm layer
  1877. Revision 1.11 2002/05/18 13:34:17 peter
  1878. * readded missing revisions
  1879. Revision 1.10 2002/05/16 19:46:44 carl
  1880. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1881. + try to fix temp allocation (still in ifdef)
  1882. + generic constructor calls
  1883. + start of tassembler / tmodulebase class cleanup
  1884. Revision 1.8 2002/04/21 15:23:03 carl
  1885. + makeregsize
  1886. + changeregsize is now a local routine
  1887. Revision 1.7 2002/04/20 21:32:25 carl
  1888. + generic FPC_CHECKPOINTER
  1889. + first parameter offset in stack now portable
  1890. * rename some constants
  1891. + move some cpu stuff to other units
  1892. - remove unused constents
  1893. * fix stacksize for some targets
  1894. * fix generic size problems which depend now on EXTEND_SIZE constant
  1895. Revision 1.6 2002/04/15 19:03:31 carl
  1896. + reg2str -> std_reg2str()
  1897. Revision 1.5 2002/04/06 18:13:01 jonas
  1898. * several powerpc-related additions and fixes
  1899. Revision 1.4 2002/04/04 19:06:04 peter
  1900. * removed unused units
  1901. * use tlocation.size in cg.a_*loc*() routines
  1902. Revision 1.3 2002/04/02 17:11:29 peter
  1903. * tlocation,treference update
  1904. * LOC_CONSTANT added for better constant handling
  1905. * secondadd splitted in multiple routines
  1906. * location_force_reg added for loading a location to a register
  1907. of a specified size
  1908. * secondassignment parses now first the right and then the left node
  1909. (this is compatible with Kylix). This saves a lot of push/pop especially
  1910. with string operations
  1911. * adapted some routines to use the new cg methods
  1912. Revision 1.2 2002/04/01 19:24:25 jonas
  1913. * fixed different parameter name in interface and implementation
  1914. declaration of a method (only 1.0.x detected this)
  1915. Revision 1.1 2002/03/31 20:26:36 jonas
  1916. + a_loadfpu_* and a_loadmm_* methods in tcg
  1917. * register allocation is now handled by a class and is mostly processor
  1918. independent (+rgobj.pas and i386/rgcpu.pas)
  1919. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1920. * some small improvements and fixes to the optimizer
  1921. * some register allocation fixes
  1922. * some fpuvaroffset fixes in the unary minus node
  1923. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1924. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1925. also better optimizable)
  1926. * fixed and optimized register saving/restoring for new/dispose nodes
  1927. * LOC_FPU locations now also require their "register" field to be set to
  1928. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1929. - list field removed of the tnode class because it's not used currently
  1930. and can cause hard-to-find bugs
  1931. }