rgobj.pas 69 KB

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