rgobj.pas 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  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. unusedregs : Tsuperregisterset;
  198. protected
  199. regtype : Tregistertype;
  200. { default subregister used }
  201. defaultsub : tsubregister;
  202. {# First imaginary register.}
  203. first_imaginary : Tsuperregister;
  204. {# Highest register allocated until now.}
  205. reginfo : PReginfo;
  206. maxreginfo,
  207. maxreginfoinc,
  208. maxreg : Tsuperregister;
  209. usable_registers_cnt : integer;
  210. usable_registers : array[0..maxcpuregister-1] of tsuperregister;
  211. ibitmap : Tinterferencebitmap;
  212. spillednodes,
  213. simplifyworklist,
  214. freezeworklist,
  215. spillworklist,
  216. coalescednodes,
  217. selectstack : tsuperregisterworklist;
  218. worklist_moves,
  219. active_moves,
  220. frozen_moves,
  221. coalesced_moves,
  222. constrained_moves : Tlinkedlist;
  223. function getnewreg:tsuperregister;
  224. procedure getregisterinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  225. procedure ungetregisterinline(list:Taasmoutput;position:Tai;r:Tregister);
  226. procedure add_edges_used(u:Tsuperregister);
  227. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  228. function move_related(n:Tsuperregister):boolean;
  229. procedure make_work_list;
  230. procedure enable_moves(n:Tsuperregister);
  231. procedure decrement_degree(m:Tsuperregister);
  232. procedure simplify;
  233. function get_alias(n:Tsuperregister):Tsuperregister;
  234. procedure add_worklist(u:Tsuperregister);
  235. function adjacent_ok(u,v:Tsuperregister):boolean;
  236. function conservative(u,v:Tsuperregister):boolean;
  237. procedure combine(u,v:Tsuperregister);
  238. procedure coalesce;
  239. procedure freeze_moves(u:Tsuperregister);
  240. procedure freeze;
  241. procedure select_spill;
  242. procedure assign_colours;
  243. procedure clear_interferences(u:Tsuperregister);
  244. end;
  245. const
  246. first_reg = 0;
  247. last_reg = high(tsuperregister)-1;
  248. maxspillingcounter = 20;
  249. implementation
  250. uses
  251. systems,
  252. globals,verbose,tgobj,procinfo;
  253. {******************************************************************************
  254. tsuperregisterworklist
  255. ******************************************************************************}
  256. constructor tsuperregisterworklist.init;
  257. begin
  258. length:=0;
  259. buflength:=0;
  260. buflengthinc:=16;
  261. head:=0;
  262. tail:=0;
  263. buf:=nil;
  264. end;
  265. destructor tsuperregisterworklist.done;
  266. begin
  267. if assigned(buf) then
  268. freemem(buf);
  269. end;
  270. procedure tsuperregisterworklist.add(s:tsuperregister);
  271. var
  272. oldbuflength : integer;
  273. newbuf : ^tsuperregister;
  274. begin
  275. inc(length);
  276. { Need to increase buffer length? }
  277. if length>=buflength then
  278. begin
  279. oldbuflength:=buflength;
  280. inc(buflength,buflengthinc);
  281. buflengthinc:=buflengthinc*2;
  282. if buflengthinc>256 then
  283. buflengthinc:=256;
  284. { We need to allocate a new block and move data around when the
  285. tail is wrapped around }
  286. if tail<head then
  287. begin
  288. Getmem(newbuf,buflength*sizeof(tsuperregister));
  289. move(buf[0],newbuf[oldbuflength-head],tail*sizeof(tsuperregister));
  290. move(buf[head],newbuf[0],(oldbuflength-head)*sizeof(tsuperregister));
  291. Freemem(buf);
  292. buf:=newbuf;
  293. head:=0;
  294. tail:=oldbuflength-1;
  295. end
  296. else
  297. Reallocmem(buf,buflength*sizeof(tsuperregister));
  298. end;
  299. buf[tail]:=s;
  300. inc(tail);
  301. if tail>=buflength then
  302. tail:=0;
  303. end;
  304. procedure tsuperregisterworklist.clear;
  305. begin
  306. length:=0;
  307. tail:=0;
  308. head:=0;
  309. end;
  310. procedure tsuperregisterworklist.next(var i:integer);
  311. begin
  312. inc(i);
  313. if i>=buflength then
  314. i:=0;
  315. end;
  316. function tsuperregisterworklist.getidx(i:integer):tsuperregister;
  317. begin
  318. result:=buf[i];
  319. end;
  320. procedure tsuperregisterworklist.deleteidx(i:integer);
  321. begin
  322. if length=0 then
  323. internalerror(200310144);
  324. buf[i]:=buf[head];
  325. inc(head);
  326. if head>=buflength then
  327. head:=0;
  328. dec(length);
  329. end;
  330. function tsuperregisterworklist.get:tsuperregister;
  331. begin
  332. if length=0 then
  333. internalerror(200310142);
  334. result:=buf[head];
  335. inc(head);
  336. if head>=buflength then
  337. head:=0;
  338. dec(length);
  339. end;
  340. function tsuperregisterworklist.getlast:tsuperregister;
  341. begin
  342. if length=0 then
  343. internalerror(200310143);
  344. dec(tail);
  345. if tail<0 then
  346. tail:=buflength-1;
  347. result:=buf[tail];
  348. dec(length);
  349. end;
  350. function tsuperregisterworklist.delete(s:tsuperregister):boolean;
  351. var
  352. i : integer;
  353. begin
  354. result:=false;
  355. i:=head;
  356. while (i<>tail) do
  357. begin
  358. if buf[i]=s then
  359. begin
  360. deleteidx(i);
  361. result:=true;
  362. exit;
  363. end;
  364. inc(i);
  365. if i>=buflength then
  366. i:=0;
  367. end;
  368. end;
  369. function tsuperregisterworklist.find(s:tsuperregister):boolean;
  370. var
  371. i : integer;
  372. begin
  373. result:=false;
  374. i:=head;
  375. while (i<>tail) do
  376. begin
  377. if buf[i]=s then
  378. begin
  379. result:=true;
  380. exit;
  381. end;
  382. inc(i);
  383. if i>=buflength then
  384. i:=0;
  385. end;
  386. end;
  387. {******************************************************************************
  388. tinterferencebitmap
  389. ******************************************************************************}
  390. constructor tinterferencebitmap.create;
  391. begin
  392. inherited create;
  393. maxx1:=1;
  394. getmem(fbitmap,sizeof(tinterferencebitmap1)*2);
  395. fillchar(fbitmap^,sizeof(tinterferencebitmap1)*2,0);
  396. end;
  397. destructor tinterferencebitmap.destroy;
  398. var
  399. i,j : byte;
  400. begin
  401. if assigned(fbitmap) then
  402. begin
  403. for i:=0 to maxx1 do
  404. for j:=0 to maxy1 do
  405. if assigned(fbitmap[i,j]) then
  406. dispose(fbitmap[i,j]);
  407. freemem(fbitmap);
  408. end;
  409. end;
  410. function tinterferencebitmap.getbitmap(x,y:tsuperregister):boolean;
  411. var
  412. page : pinterferencebitmap2;
  413. begin
  414. result:=false;
  415. if (x shr 8>maxx1) then
  416. exit;
  417. page:=fbitmap[x shr 8,y shr 8];
  418. result:=assigned(page) and
  419. ((x and $ff) in page^[y and $ff]);
  420. end;
  421. procedure tinterferencebitmap.setbitmap(x,y:tsuperregister;b:boolean);
  422. var
  423. x1,y1 : byte;
  424. begin
  425. x1:=x shr 8;
  426. y1:=y shr 8;
  427. if x1>maxx1 then
  428. begin
  429. reallocmem(fbitmap,sizeof(tinterferencebitmap1)*(x1+1));
  430. fillchar(fbitmap[maxx1+1],sizeof(tinterferencebitmap1)*(x1-maxx1),0);
  431. maxx1:=x1;
  432. end;
  433. if not assigned(fbitmap[x1,y1]) then
  434. begin
  435. if y1>maxy1 then
  436. maxy1:=y1;
  437. new(fbitmap[x1,y1]);
  438. fillchar(fbitmap[x1,y1]^,sizeof(tinterferencebitmap2),0);
  439. end;
  440. if b then
  441. include(fbitmap[x1,y1]^[y and $ff],(x and $ff))
  442. else
  443. exclude(fbitmap[x1,y1]^[y and $ff],(x and $ff));
  444. end;
  445. {******************************************************************************
  446. trgobj
  447. ******************************************************************************}
  448. constructor trgobj.create(Aregtype:Tregistertype;
  449. Adefaultsub:Tsubregister;
  450. const Ausable:array of tsuperregister;
  451. Afirst_imaginary:Tsuperregister;
  452. Apreserved_by_proc:Tcpuregisterset);
  453. var
  454. i : Tsuperregister;
  455. begin
  456. { empty super register sets can cause very strange problems }
  457. if high(Ausable)=0 then
  458. internalerror(200210181);
  459. first_imaginary:=Afirst_imaginary;
  460. maxreg:=Afirst_imaginary;
  461. regtype:=Aregtype;
  462. defaultsub:=Adefaultsub;
  463. preserved_by_proc:=Apreserved_by_proc;
  464. used_in_proc:=[];
  465. supregset_reset(unusedregs,true);
  466. { RS_INVALID can't be used }
  467. supregset_exclude(unusedregs,RS_INVALID);
  468. ibitmap:=tinterferencebitmap.create;
  469. { Get reginfo for CPU registers }
  470. reginfo:=allocmem(first_imaginary*sizeof(treginfo));
  471. maxreginfo:=first_imaginary;
  472. maxreginfoinc:=16;
  473. for i:=0 to first_imaginary-1 do
  474. reginfo[i].degree:=255;
  475. worklist_moves:=Tlinkedlist.create;
  476. { Usable registers }
  477. fillchar(usable_registers,sizeof(usable_registers),0);
  478. for i:=low(Ausable) to high(Ausable) do
  479. usable_registers[i]:=Ausable[i];
  480. usable_registers_cnt:=high(Ausable)+1;
  481. { Initialize Worklists }
  482. spillednodes.init;
  483. simplifyworklist.init;
  484. freezeworklist.init;
  485. spillworklist.init;
  486. coalescednodes.init;
  487. selectstack.init;
  488. end;
  489. destructor trgobj.destroy;
  490. var i:Tsuperregister;
  491. begin
  492. spillednodes.done;
  493. simplifyworklist.done;
  494. freezeworklist.done;
  495. spillworklist.done;
  496. coalescednodes.done;
  497. selectstack.done;
  498. for i:=0 to maxreg-1 do
  499. begin
  500. if reginfo[i].adjlist<>nil then
  501. dispose(reginfo[i].adjlist,done);
  502. if reginfo[i].movelist<>nil then
  503. dispose(reginfo[i].movelist);
  504. end;
  505. freemem(reginfo);
  506. worklist_moves.free;
  507. ibitmap.free;
  508. end;
  509. function trgobj.getnewreg:tsuperregister;
  510. var
  511. oldmaxreginfo : tsuperregister;
  512. begin
  513. result:=maxreg;
  514. inc(maxreg);
  515. if maxreg>=last_reg then
  516. internalerror(200310146);
  517. if maxreg>=maxreginfo then
  518. begin
  519. oldmaxreginfo:=maxreginfo;
  520. inc(maxreginfo,maxreginfoinc);
  521. if maxreginfoinc<256 then
  522. maxreginfoinc:=maxreginfoinc*2;
  523. reallocmem(reginfo,maxreginfo*sizeof(treginfo));
  524. { Do we really need it to clear it ? At least for 1.0.x (PFV) }
  525. fillchar(reginfo[oldmaxreginfo],(maxreginfo-oldmaxreginfo)*sizeof(treginfo),0);
  526. end;
  527. end;
  528. function trgobj.getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;
  529. var p:Tsuperregister;
  530. r:Tregister;
  531. begin
  532. p:=getnewreg;
  533. supregset_exclude(unusedregs,p);
  534. r:=newreg(regtype,p,subreg);
  535. list.concat(Tai_regalloc.alloc(r));
  536. add_edges_used(p);
  537. add_constraints(r);
  538. result:=r;
  539. end;
  540. function trgobj.uses_registers:boolean;
  541. begin
  542. result:=(maxreg>first_imaginary);
  543. end;
  544. procedure trgobj.ungetregister(list:Taasmoutput;r:Tregister);
  545. var supreg:Tsuperregister;
  546. begin
  547. supreg:=getsupreg(r);
  548. if not supregset_in(unusedregs,supreg) then
  549. begin
  550. supregset_include(unusedregs,supreg);
  551. list.concat(Tai_regalloc.dealloc(r));
  552. add_edges_used(supreg);
  553. add_constraints(r);
  554. end;
  555. end;
  556. procedure trgobj.getexplicitregister(list:Taasmoutput;r:Tregister);
  557. var supreg:Tsuperregister;
  558. begin
  559. supreg:=getsupreg(r);
  560. if supregset_in(unusedregs,supreg) then
  561. begin
  562. supregset_exclude(unusedregs,supreg);
  563. if supreg<first_imaginary then
  564. include(used_in_proc,supreg);
  565. list.concat(Tai_regalloc.alloc(r));
  566. add_edges_used(supreg);
  567. add_constraints(r);
  568. end
  569. else
  570. {$ifndef ALLOWDUPREG}
  571. internalerror(200301103)
  572. {$else ALLOWDUPREG}
  573. list.concat(Tai_regalloc.alloc(r));
  574. {$endif ALLOWDUPREG}
  575. ;
  576. end;
  577. procedure trgobj.allocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  578. var reg:Tregister;
  579. i:Tsuperregister;
  580. begin
  581. if unusedregs[0]*r=r then
  582. begin
  583. unusedregs[0]:=unusedregs[0]-r;
  584. used_in_proc:=used_in_proc+r;
  585. for i:=0 to first_imaginary-1 do
  586. if i in r then
  587. begin
  588. add_edges_used(i);
  589. reg:=newreg(regtype,i,R_SUBWHOLE);
  590. list.concat(Tai_regalloc.alloc(reg));
  591. end;
  592. end
  593. else
  594. {$ifndef ALLOWDUPREG}
  595. internalerror(200305061)
  596. {$else ALLOWDUPREG}
  597. list.concat(Tai_regalloc.alloc(reg));
  598. {$endif ALLOWDUPREG}
  599. ;
  600. end;
  601. procedure trgobj.deallocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  602. var reg:Tregister;
  603. i:Tsuperregister;
  604. begin
  605. if unusedregs[0]*r=[] then
  606. begin
  607. unusedregs[0]:=unusedregs[0]+r;
  608. for i:=first_imaginary-1 downto 0 do
  609. if i in r then
  610. begin
  611. reg:=newreg(regtype,i,R_SUBWHOLE);
  612. list.concat(Tai_regalloc.dealloc(reg));
  613. end;
  614. end
  615. else
  616. {$ifndef ALLOWDUPREG}
  617. internalerror(200305061);
  618. {$else ALLOWDUPREG}
  619. list.concat(Tai_regalloc.dealloc(reg));
  620. {$endif ALLOWDUPREG}
  621. end;
  622. procedure trgobj.do_register_allocation(list:Taasmoutput;headertai:tai);
  623. var
  624. spillingcounter:byte;
  625. endspill:boolean;
  626. begin
  627. {Do register allocation.}
  628. spillingcounter:=0;
  629. repeat
  630. prepare_colouring;
  631. colour_registers;
  632. epilogue_colouring;
  633. endspill:=true;
  634. if spillednodes.length<>0 then
  635. begin
  636. inc(spillingcounter);
  637. if spillingcounter>maxspillingcounter then
  638. internalerror(200309041);
  639. endspill:=not spill_registers(list,headertai);
  640. end;
  641. until endspill;
  642. end;
  643. procedure trgobj.add_constraints(reg:Tregister);
  644. begin
  645. end;
  646. procedure trgobj.add_edge(u,v:Tsuperregister);
  647. {This procedure will add an edge to the virtual interference graph.}
  648. procedure addadj(u,v:Tsuperregister);
  649. begin
  650. if reginfo[u].adjlist=nil then
  651. new(reginfo[u].adjlist,init);
  652. reginfo[u].adjlist^.add(v);
  653. end;
  654. begin
  655. if (u<>v) and not(ibitmap[v,u]) then
  656. begin
  657. ibitmap[v,u]:=true;
  658. ibitmap[u,v]:=true;
  659. {Precoloured nodes are not stored in the interference graph.}
  660. if (u>=first_imaginary) then
  661. begin
  662. addadj(u,v);
  663. inc(reginfo[u].degree);
  664. end;
  665. if (v>=first_imaginary) then
  666. begin
  667. addadj(v,u);
  668. inc(reginfo[v].degree);
  669. end;
  670. end;
  671. end;
  672. procedure trgobj.add_edges_used(u:Tsuperregister);
  673. var i:Tsuperregister;
  674. begin
  675. for i:=0 to maxreg-1 do
  676. if not(supregset_in(unusedregs,i)) then
  677. add_edge(u,i);
  678. end;
  679. {$ifdef EXTDEBUG}
  680. procedure trgobj.writegraph(loopidx:longint);
  681. {This procedure writes out the current interference graph in the
  682. register allocator.}
  683. var f:text;
  684. i,j:Tsuperregister;
  685. begin
  686. assign(f,'igraph'+tostr(loopidx));
  687. rewrite(f);
  688. writeln(f,'Interference graph');
  689. writeln(f);
  690. write(f,' ');
  691. for i:=0 to 15 do
  692. for j:=0 to 15 do
  693. write(f,hexstr(i,1));
  694. writeln(f);
  695. write(f,' ');
  696. for i:=0 to 15 do
  697. write(f,'0123456789ABCDEF');
  698. writeln(f);
  699. for i:=0 to maxreg-1 do
  700. begin
  701. write(f,hexstr(i,2):4);
  702. for j:=0 to maxreg-1 do
  703. if ibitmap[i,j] then
  704. write(f,'*')
  705. else
  706. write(f,'-');
  707. writeln(f);
  708. end;
  709. close(f);
  710. end;
  711. {$endif EXTDEBUG}
  712. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  713. begin
  714. if reginfo[u].movelist=nil then
  715. begin
  716. getmem(reginfo[u].movelist,64);
  717. reginfo[u].movelist^.count:=0;
  718. end
  719. else if (reginfo[u].movelist^.count and 15)=15 then
  720. reallocmem(reginfo[u].movelist,(reginfo[u].movelist^.count+1)*4+64);
  721. reginfo[u].movelist^.data[reginfo[u].movelist^.count]:=data;
  722. inc(reginfo[u].movelist^.count);
  723. end;
  724. procedure trgobj.add_move_instruction(instr:Taicpu);
  725. {This procedure notifies a certain as a move instruction so the
  726. register allocator can try to eliminate it.}
  727. var i:Tmoveins;
  728. ssupreg,dsupreg:Tsuperregister;
  729. begin
  730. i:=Tmoveins.create;
  731. i.moveset:=ms_worklist_moves;
  732. i.instruction:=instr;
  733. worklist_moves.insert(i);
  734. ssupreg:=getsupreg(instr.oper[O_MOV_SOURCE]^.reg);
  735. add_to_movelist(ssupreg,i);
  736. dsupreg:=getsupreg(instr.oper[O_MOV_DEST]^.reg);
  737. if ssupreg<>dsupreg then
  738. {Avoid adding the same move instruction twice to a single register.}
  739. add_to_movelist(dsupreg,i);
  740. i.x:=ssupreg;
  741. i.y:=dsupreg;
  742. end;
  743. function trgobj.move_related(n:Tsuperregister):boolean;
  744. var i:cardinal;
  745. begin
  746. move_related:=false;
  747. if reginfo[n].movelist<>nil then
  748. begin
  749. for i:=0 to reginfo[n].movelist^.count-1 do
  750. if Tmoveins(reginfo[n].movelist^.data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  751. begin
  752. move_related:=true;
  753. break;
  754. end;
  755. end;
  756. end;
  757. procedure trgobj.make_work_list;
  758. var n:Tsuperregister;
  759. begin
  760. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  761. assign it to any of the registers, thus it is significant.}
  762. for n:=first_imaginary to maxreg-1 do
  763. if reginfo[n].degree>=usable_registers_cnt then
  764. spillworklist.add(n)
  765. else if move_related(n) then
  766. freezeworklist.add(n)
  767. else
  768. simplifyworklist.add(n);
  769. end;
  770. procedure trgobj.prepare_colouring;
  771. var
  772. i : integer;
  773. begin
  774. make_work_list;
  775. active_moves:=Tlinkedlist.create;
  776. frozen_moves:=Tlinkedlist.create;
  777. coalesced_moves:=Tlinkedlist.create;
  778. constrained_moves:=Tlinkedlist.create;
  779. for i:=0 to maxreg-1 do
  780. reginfo[i].alias:=RS_INVALID;
  781. coalescednodes.clear;
  782. selectstack.clear;
  783. end;
  784. procedure trgobj.enable_moves(n:Tsuperregister);
  785. var m:Tlinkedlistitem;
  786. i:cardinal;
  787. begin
  788. if reginfo[n].movelist<>nil then
  789. for i:=0 to reginfo[n].movelist^.count-1 do
  790. begin
  791. m:=reginfo[n].movelist^.data[i];
  792. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  793. begin
  794. if Tmoveins(m).moveset=ms_active_moves then
  795. begin
  796. {Move m from the set active_moves to the set worklist_moves.}
  797. active_moves.remove(m);
  798. Tmoveins(m).moveset:=ms_worklist_moves;
  799. worklist_moves.concat(m);
  800. end;
  801. end;
  802. end;
  803. end;
  804. procedure trgobj.decrement_degree(m:Tsuperregister);
  805. var adj : Psuperregisterworklist;
  806. d : byte;
  807. n : tsuperregister;
  808. i : integer;
  809. begin
  810. d:=reginfo[m].degree;
  811. if reginfo[m].degree>0 then
  812. dec(reginfo[m].degree);
  813. if d=usable_registers_cnt then
  814. begin
  815. {Enable moves for m.}
  816. enable_moves(m);
  817. {Enable moves for adjacent.}
  818. adj:=reginfo[m].adjlist;
  819. if adj<>nil then
  820. begin
  821. i:=adj^.head;
  822. while (i<>adj^.tail) do
  823. begin
  824. n:=adj^.buf[i];
  825. if selectstack.find(n) or
  826. coalescednodes.find(n) then
  827. enable_moves(n);
  828. adj^.next(i);
  829. end;
  830. end;
  831. {Remove the node from the spillworklist.}
  832. if not spillworklist.delete(m) then
  833. internalerror(200310145);
  834. if move_related(m) then
  835. freezeworklist.add(m)
  836. else
  837. simplifyworklist.add(m);
  838. end;
  839. end;
  840. procedure trgobj.simplify;
  841. var adj : Psuperregisterworklist;
  842. min : byte;
  843. p,n : Tsuperregister;
  844. i : integer;
  845. begin
  846. {We the element with the least interferences out of the
  847. simplifyworklist.}
  848. min:=$ff;
  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. include(used_in_proc,reginfo[k].colour);
  1223. coalescednodes.next(i);
  1224. end;
  1225. {$ifdef ra_debug}
  1226. if aktfilepos.line=51 then
  1227. begin
  1228. writeln('colourlist');
  1229. for i:=0 to maxreg-1 do
  1230. writeln(i:4,' ',reginfo[i].colour:4)
  1231. end;
  1232. {$endif ra_debug}
  1233. end;
  1234. procedure trgobj.colour_registers;
  1235. begin
  1236. repeat
  1237. if simplifyworklist.length<>0 then
  1238. simplify
  1239. else if not(worklist_moves.empty) then
  1240. coalesce
  1241. else if freezeworklist.length<>0 then
  1242. freeze
  1243. else if spillworklist.length<>0 then
  1244. select_spill;
  1245. until (simplifyworklist.length=0) and
  1246. worklist_moves.empty and
  1247. (freezeworklist.length=0) and
  1248. (spillworklist.length=0);
  1249. assign_colours;
  1250. end;
  1251. procedure trgobj.epilogue_colouring;
  1252. {
  1253. procedure move_to_worklist_moves(list:Tlinkedlist);
  1254. var p:Tlinkedlistitem;
  1255. begin
  1256. p:=list.first;
  1257. while p<>nil do
  1258. begin
  1259. Tmoveins(p).moveset:=ms_worklist_moves;
  1260. p:=p.next;
  1261. end;
  1262. worklist_moves.concatlist(list);
  1263. end;
  1264. }
  1265. var i:Tsuperregister;
  1266. begin
  1267. worklist_moves.clear;
  1268. {$ifdef Principle_wrong_by_definition}
  1269. {Move everything back to worklist_moves.}
  1270. move_to_worklist_moves(active_moves);
  1271. move_to_worklist_moves(frozen_moves);
  1272. move_to_worklist_moves(coalesced_moves);
  1273. move_to_worklist_moves(constrained_moves);
  1274. {$endif Principle_wrong_by_definition}
  1275. active_moves.destroy;
  1276. active_moves:=nil;
  1277. frozen_moves.destroy;
  1278. frozen_moves:=nil;
  1279. coalesced_moves.destroy;
  1280. coalesced_moves:=nil;
  1281. constrained_moves.destroy;
  1282. constrained_moves:=nil;
  1283. for i:=0 to maxreg-1 do
  1284. if reginfo[i].movelist<>nil then
  1285. begin
  1286. dispose(reginfo[i].movelist);
  1287. reginfo[i].movelist:=0;
  1288. end;
  1289. end;
  1290. procedure trgobj.clear_interferences(u:Tsuperregister);
  1291. {Remove node u from the interference graph and remove all collected
  1292. move instructions it is associated with.}
  1293. var i : integer;
  1294. v : Tsuperregister;
  1295. adj,adj2 : Psuperregisterworklist;
  1296. {$ifdef Principle_wrong_by_definition}
  1297. k,j,count : cardinal;
  1298. m,n : Tmoveins;
  1299. {$endif Principle_wrong_by_definition}
  1300. begin
  1301. adj:=reginfo[u].adjlist;
  1302. if adj<>nil then
  1303. begin
  1304. i:=adj^.head;
  1305. while (i<>adj^.tail) do
  1306. begin
  1307. v:=adj^.buf[i];
  1308. {Remove (u,v) and (v,u) from bitmap.}
  1309. ibitmap[u,v]:=false;
  1310. ibitmap[v,u]:=false;
  1311. {Remove (v,u) from adjacency list.}
  1312. adj2:=reginfo[v].adjlist;
  1313. if adj2<>nil then
  1314. begin
  1315. adj2^.delete(v);
  1316. if adj2^.length=0 then
  1317. begin
  1318. dispose(adj2,done);
  1319. reginfo[v].adjlist:=nil;
  1320. end;
  1321. end;
  1322. adj^.next(i);
  1323. end;
  1324. {Remove ( u,* ) from adjacency list.}
  1325. dispose(adj,done);
  1326. reginfo[u].adjlist:=nil;
  1327. end;
  1328. {$ifdef Principle_wrong_by_definition}
  1329. {Now remove the moves.}
  1330. if movelist[u]<>nil then
  1331. begin
  1332. for j:=0 to movelist[u]^.count-1 do
  1333. begin
  1334. m:=Tmoveins(movelist[u]^.data[j]);
  1335. {Get the other register of the move instruction.}
  1336. v:=m.instruction.oper[0]^.reg.number shr 8;
  1337. if v=u then
  1338. v:=m.instruction.oper[1]^.reg.number shr 8;
  1339. repeat
  1340. repeat
  1341. if (u<>v) and (movelist[v]<>nil) then
  1342. begin
  1343. {Remove the move from it's movelist.}
  1344. count:=movelist[v]^.count-1;
  1345. for k:=0 to count do
  1346. if m=movelist[v]^.data[k] then
  1347. begin
  1348. if k<>count then
  1349. movelist[v]^.data[k]:=movelist[v]^.data[count];
  1350. dec(movelist[v]^.count);
  1351. if count=0 then
  1352. begin
  1353. dispose(movelist[v]);
  1354. movelist[v]:=nil;
  1355. end;
  1356. break;
  1357. end;
  1358. end;
  1359. {The complexity is enourmous: the register might have been
  1360. coalesced. In that case it's movelists have been added to
  1361. it's coalescing alias. (DM)}
  1362. v:=alias[v];
  1363. until v=0;
  1364. {And also register u might have been coalesced.}
  1365. u:=alias[u];
  1366. until u=0;
  1367. case m.moveset of
  1368. ms_coalesced_moves:
  1369. coalesced_moves.remove(m);
  1370. ms_constrained_moves:
  1371. constrained_moves.remove(m);
  1372. ms_frozen_moves:
  1373. frozen_moves.remove(m);
  1374. ms_worklist_moves:
  1375. worklist_moves.remove(m);
  1376. ms_active_moves:
  1377. active_moves.remove(m);
  1378. end;
  1379. end;
  1380. dispose(movelist[u]);
  1381. movelist[u]:=nil;
  1382. end;
  1383. {$endif Principle_wrong_by_definition}
  1384. end;
  1385. procedure trgobj.getregisterinline(list:Taasmoutput;
  1386. position:Tai;subreg:Tsubregister;var result:Tregister);
  1387. var p:Tsuperregister;
  1388. r:Tregister;
  1389. begin
  1390. p:=getnewreg;
  1391. supregset_exclude(unusedregs,p);
  1392. r:=newreg(regtype,p,subreg);
  1393. if position=nil then
  1394. list.insert(Tai_regalloc.alloc(r))
  1395. else
  1396. list.insertafter(Tai_regalloc.alloc(r),position);
  1397. add_edges_used(p);
  1398. add_constraints(r);
  1399. result:=r;
  1400. end;
  1401. procedure trgobj.ungetregisterinline(list:Taasmoutput;
  1402. position:Tai;r:Tregister);
  1403. var supreg:Tsuperregister;
  1404. begin
  1405. supreg:=getsupreg(r);
  1406. supregset_include(unusedregs,supreg);
  1407. if position=nil then
  1408. list.insert(Tai_regalloc.dealloc(r))
  1409. else
  1410. list.insertafter(Tai_regalloc.dealloc(r),position);
  1411. add_edges_used(supreg);
  1412. add_constraints(r);
  1413. end;
  1414. function trgobj.spill_registers(list:Taasmoutput;headertai:tai):boolean;
  1415. {Returns true if any help registers have been used.}
  1416. var i : integer;
  1417. t : tsuperregister;
  1418. p,q : Tai;
  1419. regs_to_spill_set : Tsuperregisterset;
  1420. spill_temps : ^Tspill_temp_list;
  1421. supreg : tsuperregister;
  1422. templist : taasmoutput;
  1423. begin
  1424. spill_registers:=false;
  1425. supregset_reset(unusedregs,true);
  1426. {Precoloured nodes should have an infinite degree, which we can approach
  1427. by 255.}
  1428. for i:=0 to first_imaginary-1 do
  1429. reginfo[i].degree:=255;
  1430. for i:=first_imaginary to maxreg-1 do
  1431. reginfo[i].degree:=0;
  1432. { exclude(unusedregs,RS_STACK_POINTER_REG);}
  1433. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1434. {Make sure the register allocator won't allocate registers into ebp.}
  1435. supregset_exclude(unusedregs,RS_FRAME_POINTER_REG);
  1436. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1437. supregset_reset(regs_to_spill_set,false);
  1438. { Allocate temps and insert in front of the list }
  1439. templist:=taasmoutput.create;
  1440. i:=spillednodes.head;
  1441. while (i<>spillednodes.tail) do
  1442. begin
  1443. t:=spillednodes.buf[i];
  1444. {Alternative representation.}
  1445. supregset_include(regs_to_spill_set,t);
  1446. {Clear all interferences of the spilled register.}
  1447. clear_interferences(t);
  1448. {Get a temp for the spilled register}
  1449. tg.gettemp(templist,4,tt_noreuse,spill_temps^[t]);
  1450. spillednodes.next(i);
  1451. end;
  1452. list.insertlistafter(headertai,templist);
  1453. templist.free;
  1454. { Walk through all instructions, we can start with the headertai,
  1455. because before the header tai is only symbols }
  1456. p:=headertai;
  1457. while assigned(p) do
  1458. begin
  1459. case p.typ of
  1460. ait_regalloc:
  1461. begin
  1462. if (getregtype(Tai_regalloc(p).reg)=regtype) then
  1463. begin
  1464. {A register allocation of a spilled register can be removed.}
  1465. supreg:=getsupreg(Tai_regalloc(p).reg);
  1466. if supregset_in(regs_to_spill_set,supreg) then
  1467. begin
  1468. q:=Tai(p.next);
  1469. list.remove(p);
  1470. p.free;
  1471. p:=q;
  1472. continue;
  1473. end
  1474. else
  1475. if Tai_regalloc(p).allocation then
  1476. supregset_exclude(unusedregs,supreg)
  1477. else
  1478. supregset_include(unusedregs,supreg);
  1479. end;
  1480. end;
  1481. ait_instruction:
  1482. begin
  1483. aktfilepos:=Taicpu_abstract(p).fileinfo;
  1484. if Taicpu_abstract(p).spill_registers(list,
  1485. @getregisterinline,
  1486. @ungetregisterinline,
  1487. regs_to_spill_set,
  1488. unusedregs,
  1489. spill_temps^) then
  1490. spill_registers:=true;
  1491. if Taicpu_abstract(p).is_move then
  1492. add_move_instruction(Taicpu(p));
  1493. end;
  1494. end;
  1495. p:=Tai(p.next);
  1496. end;
  1497. aktfilepos:=current_procinfo.exitpos;
  1498. i:=spillednodes.head;
  1499. while (i<>spillednodes.tail) do
  1500. begin
  1501. tg.ungettemp(list,spill_temps^[spillednodes.buf[i]]);
  1502. spillednodes.next(i);
  1503. end;
  1504. freemem(spill_temps);
  1505. end;
  1506. procedure Trgobj.translate_registers(list:taasmoutput);
  1507. var hp,p,q:Tai;
  1508. i:shortint;
  1509. r:Preference;
  1510. {$ifdef arm}
  1511. so:pshifterop;
  1512. {$endif arm}
  1513. begin
  1514. { Leave when no imaginary registers are used }
  1515. if maxreg<=first_imaginary then
  1516. exit;
  1517. p:=Tai(list.first);
  1518. while assigned(p) do
  1519. begin
  1520. case p.typ of
  1521. ait_regalloc:
  1522. begin
  1523. if (getregtype(Tai_regalloc(p).reg)=regtype) then
  1524. setsupreg(Tai_regalloc(p).reg,reginfo[getsupreg(Tai_regalloc(p).reg)].colour);
  1525. {
  1526. Remove sequences of release and
  1527. allocation of the same register like:
  1528. # Register X released
  1529. # Register X allocated
  1530. }
  1531. if assigned(p.previous) and
  1532. (Tai(p.previous).typ=ait_regalloc) and
  1533. (Tai_regalloc(p.previous).reg=Tai_regalloc(p).reg) and
  1534. { allocation,deallocation or deallocation,allocation }
  1535. (Tai_regalloc(p.previous).allocation xor Tai_regalloc(p).allocation) then
  1536. begin
  1537. q:=Tai(p.next);
  1538. hp:=tai(p.previous);
  1539. list.remove(hp);
  1540. hp.free;
  1541. list.remove(p);
  1542. p.free;
  1543. p:=q;
  1544. continue;
  1545. end;
  1546. end;
  1547. ait_instruction:
  1548. begin
  1549. for i:=0 to Taicpu_abstract(p).ops-1 do
  1550. case Taicpu_abstract(p).oper[i]^.typ of
  1551. Top_reg:
  1552. if (getregtype(Taicpu_abstract(p).oper[i]^.reg)=regtype) then
  1553. setsupreg(Taicpu_abstract(p).oper[i]^.reg,reginfo[getsupreg(Taicpu_abstract(p).oper[i]^.reg)].colour);
  1554. Top_ref:
  1555. begin
  1556. if regtype=R_INTREGISTER then
  1557. begin
  1558. r:=Taicpu_abstract(p).oper[i]^.ref;
  1559. if r^.base<>NR_NO then
  1560. setsupreg(r^.base,reginfo[getsupreg(r^.base)].colour);
  1561. if r^.index<>NR_NO then
  1562. setsupreg(r^.index,reginfo[getsupreg(r^.index)].colour);
  1563. end;
  1564. end;
  1565. {$ifdef arm}
  1566. Top_shifterop:
  1567. begin
  1568. so:=Taicpu_abstract(p).oper[i]^.shifterop;
  1569. if so^.rs<>NR_NO then
  1570. setsupreg(so^.rs,table[getsupreg(so^.rs)]);
  1571. end;
  1572. {$endif arm}
  1573. end;
  1574. { Maybe the operation can be removed when
  1575. it is a move and both arguments are the same }
  1576. if Taicpu_abstract(p).is_nop then
  1577. begin
  1578. q:=Tai(p.next);
  1579. list.remove(p);
  1580. p.free;
  1581. p:=q;
  1582. continue;
  1583. end;
  1584. end;
  1585. end;
  1586. p:=Tai(p.next);
  1587. end;
  1588. end;
  1589. end.
  1590. {
  1591. $Log$
  1592. Revision 1.92 2003-10-29 21:29:14 jonas
  1593. * some ALLOWDUPREG improvements
  1594. Revision 1.91 2003/10/21 15:15:36 peter
  1595. * taicpu_abstract.oper[] changed to pointers
  1596. Revision 1.90 2003/10/19 12:36:36 florian
  1597. * improved speed; reduced memory usage of the interference bitmap
  1598. Revision 1.89 2003/10/19 01:34:30 florian
  1599. * some ppc stuff fixed
  1600. * memory leak fixed
  1601. Revision 1.88 2003/10/18 15:41:26 peter
  1602. * made worklists dynamic in size
  1603. Revision 1.87 2003/10/17 16:16:08 peter
  1604. * fixed last commit
  1605. Revision 1.86 2003/10/17 15:25:18 florian
  1606. * fixed more ppc stuff
  1607. Revision 1.85 2003/10/17 14:38:32 peter
  1608. * 64k registers supported
  1609. * fixed some memory leaks
  1610. Revision 1.84 2003/10/11 16:06:42 florian
  1611. * fixed some MMX<->SSE
  1612. * started to fix ppc, needs an overhaul
  1613. + stabs info improve for spilling, not sure if it works correctly/completly
  1614. - MMX_SUPPORT removed from Makefile.fpc
  1615. Revision 1.83 2003/10/10 17:48:14 peter
  1616. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1617. * tregisteralloctor renamed to trgobj
  1618. * removed rgobj from a lot of units
  1619. * moved location_* and reference_* to cgobj
  1620. * first things for mmx register allocation
  1621. Revision 1.82 2003/10/09 21:31:37 daniel
  1622. * Register allocator splitted, ans abstract now
  1623. Revision 1.81 2003/10/01 20:34:49 peter
  1624. * procinfo unit contains tprocinfo
  1625. * cginfo renamed to cgbase
  1626. * moved cgmessage to verbose
  1627. * fixed ppc and sparc compiles
  1628. Revision 1.80 2003/09/30 19:54:42 peter
  1629. * reuse registers with the least conflicts
  1630. Revision 1.79 2003/09/29 20:58:56 peter
  1631. * optimized releasing of registers
  1632. Revision 1.78 2003/09/28 13:41:12 peter
  1633. * return reg 255 when allowdupreg is defined
  1634. Revision 1.77 2003/09/25 16:19:32 peter
  1635. * fix filepositions
  1636. * insert spill temp allocations at the start of the proc
  1637. Revision 1.76 2003/09/16 16:17:01 peter
  1638. * varspez in calls to push_addr_param
  1639. Revision 1.75 2003/09/12 19:07:42 daniel
  1640. * Fixed fast spilling functionality by re-adding the code that initializes
  1641. precoloured nodes to degree 255. I would like to play hangman on the one
  1642. who removed that code.
  1643. Revision 1.74 2003/09/11 11:54:59 florian
  1644. * improved arm code generation
  1645. * move some protected and private field around
  1646. * the temp. register for register parameters/arguments are now released
  1647. before the move to the parameter register is done. This improves
  1648. the code in a lot of cases.
  1649. Revision 1.73 2003/09/09 20:59:27 daniel
  1650. * Adding register allocation order
  1651. Revision 1.72 2003/09/09 15:55:44 peter
  1652. * use register with least interferences in spillregister
  1653. Revision 1.71 2003/09/07 22:09:35 peter
  1654. * preparations for different default calling conventions
  1655. * various RA fixes
  1656. Revision 1.70 2003/09/03 21:06:45 peter
  1657. * fixes for FPU register allocation
  1658. Revision 1.69 2003/09/03 15:55:01 peter
  1659. * NEWRA branch merged
  1660. Revision 1.68 2003/09/03 11:18:37 florian
  1661. * fixed arm concatcopy
  1662. + arm support in the common compiler sources added
  1663. * moved some generic cg code around
  1664. + tfputype added
  1665. * ...
  1666. Revision 1.67.2.5 2003/08/31 20:44:07 peter
  1667. * fixed getexplicitregisterint tregister value
  1668. Revision 1.67.2.4 2003/08/31 20:40:50 daniel
  1669. * Fixed add_edges_used
  1670. Revision 1.67.2.3 2003/08/29 17:28:59 peter
  1671. * next batch of updates
  1672. Revision 1.67.2.2 2003/08/28 18:35:08 peter
  1673. * tregister changed to cardinal
  1674. Revision 1.67.2.1 2003/08/27 19:55:54 peter
  1675. * first tregister patch
  1676. Revision 1.67 2003/08/23 10:46:21 daniel
  1677. * Register allocator bugfix for h2pas
  1678. Revision 1.66 2003/08/17 16:59:20 jonas
  1679. * fixed regvars so they work with newra (at least for ppc)
  1680. * fixed some volatile register bugs
  1681. + -dnotranslation option for -dnewra, which causes the registers not to
  1682. be translated from virtual to normal registers. Requires support in
  1683. the assembler writer as well, which is only implemented in aggas/
  1684. agppcgas currently
  1685. Revision 1.65 2003/08/17 14:32:48 daniel
  1686. * Precoloured nodes now have an infinite degree approached with 255,
  1687. like they should.
  1688. Revision 1.64 2003/08/17 08:48:02 daniel
  1689. * Another register allocator bug fixed.
  1690. * usable_registers_cnt set to 6 for i386
  1691. Revision 1.63 2003/08/09 18:56:54 daniel
  1692. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1693. allocator
  1694. * Some preventive changes to i386 spillinh code
  1695. Revision 1.62 2003/08/03 14:09:50 daniel
  1696. * Fixed a register allocator bug
  1697. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1698. statements: changes in location_force. These moves are now no longer
  1699. constrained so they are optimized away.
  1700. Revision 1.61 2003/07/21 13:32:39 jonas
  1701. * add_edges_used() is now also called for registers allocated with
  1702. getexplicitregisterint()
  1703. * writing the intereference graph is now only done with -dradebug2 and
  1704. the created files are now called "igraph.<module_name>"
  1705. Revision 1.60 2003/07/06 15:31:21 daniel
  1706. * Fixed register allocator. *Lots* of fixes.
  1707. Revision 1.59 2003/07/06 15:00:47 jonas
  1708. * fixed my previous completely broken commit. It's not perfect though,
  1709. registers > last_int_supreg and < max_intreg may still be "translated"
  1710. Revision 1.58 2003/07/06 14:45:05 jonas
  1711. * support integer registers that are not managed by newra (ie. don't
  1712. translate register numbers that fall outside the range
  1713. first_int_supreg..last_int_supreg)
  1714. Revision 1.57 2003/07/02 22:18:04 peter
  1715. * paraloc splitted in callerparaloc,calleeparaloc
  1716. * sparc calling convention updates
  1717. Revision 1.56 2003/06/17 16:34:44 jonas
  1718. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1719. * renamed all_intregisters to volatile_intregisters and made it
  1720. processor dependent
  1721. Revision 1.55 2003/06/14 14:53:50 jonas
  1722. * fixed newra cycle for x86
  1723. * added constants for indicating source and destination operands of the
  1724. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1725. Revision 1.54 2003/06/13 21:19:31 peter
  1726. * current_procdef removed, use current_procinfo.procdef instead
  1727. Revision 1.53 2003/06/12 21:11:10 peter
  1728. * ungetregisterfpu gets size parameter
  1729. Revision 1.52 2003/06/12 16:43:07 peter
  1730. * newra compiles for sparc
  1731. Revision 1.51 2003/06/09 14:54:26 jonas
  1732. * (de)allocation of registers for parameters is now performed properly
  1733. (and checked on the ppc)
  1734. - removed obsolete allocation of all parameter registers at the start
  1735. of a procedure (and deallocation at the end)
  1736. Revision 1.50 2003/06/03 21:11:09 peter
  1737. * cg.a_load_* get a from and to size specifier
  1738. * makeregsize only accepts newregister
  1739. * i386 uses generic tcgnotnode,tcgunaryminus
  1740. Revision 1.49 2003/06/03 13:01:59 daniel
  1741. * Register allocator finished
  1742. Revision 1.48 2003/06/01 21:38:06 peter
  1743. * getregisterfpu size parameter added
  1744. * op_const_reg size parameter added
  1745. * sparc updates
  1746. Revision 1.47 2003/05/31 20:31:11 jonas
  1747. * set inital costs of assigning a variable to a register to 120 for
  1748. non-i386, because the used register must be store to memory at the
  1749. start and loaded again at the end
  1750. Revision 1.46 2003/05/30 18:55:21 jonas
  1751. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  1752. works for ppc
  1753. Revision 1.45 2003/05/30 12:36:13 jonas
  1754. * use as little different registers on the ppc until newra is released,
  1755. since every used register must be saved
  1756. Revision 1.44 2003/05/17 13:30:08 jonas
  1757. * changed tt_persistant to tt_persistent :)
  1758. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1759. temps, but a ttemptype, so you can also create ansistring temps etc
  1760. Revision 1.43 2003/05/16 14:33:31 peter
  1761. * regvar fixes
  1762. Revision 1.42 2003/04/26 20:03:49 daniel
  1763. * Bug fix in simplify
  1764. Revision 1.41 2003/04/25 20:59:35 peter
  1765. * removed funcretn,funcretsym, function result is now in varsym
  1766. and aliases for result and function name are added using absolutesym
  1767. * vs_hidden parameter for funcret passed in parameter
  1768. * vs_hidden fixes
  1769. * writenode changed to printnode and released from extdebug
  1770. * -vp option added to generate a tree.log with the nodetree
  1771. * nicer printnode for statements, callnode
  1772. Revision 1.40 2003/04/25 08:25:26 daniel
  1773. * Ifdefs around a lot of calls to cleartempgen
  1774. * Fixed registers that are allocated but not freed in several nodes
  1775. * Tweak to register allocator to cause less spills
  1776. * 8-bit registers now interfere with esi,edi and ebp
  1777. Compiler can now compile rtl successfully when using new register
  1778. allocator
  1779. Revision 1.39 2003/04/23 20:23:06 peter
  1780. * compile fix for no-newra
  1781. Revision 1.38 2003/04/23 14:42:07 daniel
  1782. * Further register allocator work. Compiler now smaller with new
  1783. allocator than without.
  1784. * Somebody forgot to adjust ppu version number
  1785. Revision 1.37 2003/04/22 23:50:23 peter
  1786. * firstpass uses expectloc
  1787. * checks if there are differences between the expectloc and
  1788. location.loc from secondpass in EXTDEBUG
  1789. Revision 1.36 2003/04/22 10:09:35 daniel
  1790. + Implemented the actual register allocator
  1791. + Scratch registers unavailable when new register allocator used
  1792. + maybe_save/maybe_restore unavailable when new register allocator used
  1793. Revision 1.35 2003/04/21 19:16:49 peter
  1794. * count address regs separate
  1795. Revision 1.34 2003/04/17 16:48:21 daniel
  1796. * Added some code to keep track of move instructions in register
  1797. allocator
  1798. Revision 1.33 2003/04/17 07:50:24 daniel
  1799. * Some work on interference graph construction
  1800. Revision 1.32 2003/03/28 19:16:57 peter
  1801. * generic constructor working for i386
  1802. * remove fixed self register
  1803. * esi added as address register for i386
  1804. Revision 1.31 2003/03/11 21:46:24 jonas
  1805. * lots of new regallocator fixes, both in generic and ppc-specific code
  1806. (ppc compiler still can't compile the linux system unit though)
  1807. Revision 1.30 2003/03/09 21:18:59 olle
  1808. + added cutils to the uses clause
  1809. Revision 1.29 2003/03/08 20:36:41 daniel
  1810. + Added newra version of Ti386shlshrnode
  1811. + Added interference graph construction code
  1812. Revision 1.28 2003/03/08 13:59:16 daniel
  1813. * Work to handle new register notation in ag386nsm
  1814. + Added newra version of Ti386moddivnode
  1815. Revision 1.27 2003/03/08 10:53:48 daniel
  1816. * Created newra version of secondmul in n386add.pas
  1817. Revision 1.26 2003/03/08 08:59:07 daniel
  1818. + $define newra will enable new register allocator
  1819. + getregisterint will return imaginary registers with $newra
  1820. + -sr switch added, will skip register allocation so you can see
  1821. the direct output of the code generator before register allocation
  1822. Revision 1.25 2003/02/26 20:50:45 daniel
  1823. * Fixed ungetreference
  1824. Revision 1.24 2003/02/19 22:39:56 daniel
  1825. * Fixed a few issues
  1826. Revision 1.23 2003/02/19 22:00:14 daniel
  1827. * Code generator converted to new register notation
  1828. - Horribily outdated todo.txt removed
  1829. Revision 1.22 2003/02/02 19:25:54 carl
  1830. * Several bugfixes for m68k target (register alloc., opcode emission)
  1831. + VIS target
  1832. + Generic add more complete (still not verified)
  1833. Revision 1.21 2003/01/08 18:43:57 daniel
  1834. * Tregister changed into a record
  1835. Revision 1.20 2002/10/05 12:43:28 carl
  1836. * fixes for Delphi 6 compilation
  1837. (warning : Some features do not work under Delphi)
  1838. Revision 1.19 2002/08/23 16:14:49 peter
  1839. * tempgen cleanup
  1840. * tt_noreuse temp type added that will be used in genentrycode
  1841. Revision 1.18 2002/08/17 22:09:47 florian
  1842. * result type handling in tcgcal.pass_2 overhauled
  1843. * better tnode.dowrite
  1844. * some ppc stuff fixed
  1845. Revision 1.17 2002/08/17 09:23:42 florian
  1846. * first part of procinfo rewrite
  1847. Revision 1.16 2002/08/06 20:55:23 florian
  1848. * first part of ppc calling conventions fix
  1849. Revision 1.15 2002/08/05 18:27:48 carl
  1850. + more more more documentation
  1851. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1852. Revision 1.14 2002/08/04 19:06:41 carl
  1853. + added generic exception support (still does not work!)
  1854. + more documentation
  1855. Revision 1.13 2002/07/07 09:52:32 florian
  1856. * powerpc target fixed, very simple units can be compiled
  1857. * some basic stuff for better callparanode handling, far from being finished
  1858. Revision 1.12 2002/07/01 18:46:26 peter
  1859. * internal linker
  1860. * reorganized aasm layer
  1861. Revision 1.11 2002/05/18 13:34:17 peter
  1862. * readded missing revisions
  1863. Revision 1.10 2002/05/16 19:46:44 carl
  1864. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1865. + try to fix temp allocation (still in ifdef)
  1866. + generic constructor calls
  1867. + start of tassembler / tmodulebase class cleanup
  1868. Revision 1.8 2002/04/21 15:23:03 carl
  1869. + makeregsize
  1870. + changeregsize is now a local routine
  1871. Revision 1.7 2002/04/20 21:32:25 carl
  1872. + generic FPC_CHECKPOINTER
  1873. + first parameter offset in stack now portable
  1874. * rename some constants
  1875. + move some cpu stuff to other units
  1876. - remove unused constents
  1877. * fix stacksize for some targets
  1878. * fix generic size problems which depend now on EXTEND_SIZE constant
  1879. Revision 1.6 2002/04/15 19:03:31 carl
  1880. + reg2str -> std_reg2str()
  1881. Revision 1.5 2002/04/06 18:13:01 jonas
  1882. * several powerpc-related additions and fixes
  1883. Revision 1.4 2002/04/04 19:06:04 peter
  1884. * removed unused units
  1885. * use tlocation.size in cg.a_*loc*() routines
  1886. Revision 1.3 2002/04/02 17:11:29 peter
  1887. * tlocation,treference update
  1888. * LOC_CONSTANT added for better constant handling
  1889. * secondadd splitted in multiple routines
  1890. * location_force_reg added for loading a location to a register
  1891. of a specified size
  1892. * secondassignment parses now first the right and then the left node
  1893. (this is compatible with Kylix). This saves a lot of push/pop especially
  1894. with string operations
  1895. * adapted some routines to use the new cg methods
  1896. Revision 1.2 2002/04/01 19:24:25 jonas
  1897. * fixed different parameter name in interface and implementation
  1898. declaration of a method (only 1.0.x detected this)
  1899. Revision 1.1 2002/03/31 20:26:36 jonas
  1900. + a_loadfpu_* and a_loadmm_* methods in tcg
  1901. * register allocation is now handled by a class and is mostly processor
  1902. independent (+rgobj.pas and i386/rgcpu.pas)
  1903. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1904. * some small improvements and fixes to the optimizer
  1905. * some register allocation fixes
  1906. * some fpuvaroffset fixes in the unary minus node
  1907. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1908. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1909. also better optimizable)
  1910. * fixed and optimized register saving/restoring for new/dispose nodes
  1911. * LOC_FPU locations now also require their "register" field to be set to
  1912. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1913. - list field removed of the tnode class because it's not used currently
  1914. and can cause hard-to-find bugs
  1915. }