rgobj.pas 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  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 : TSuperregister;
  145. end;
  146. Preginfo=^TReginfo;
  147. {#------------------------------------------------------------------
  148. This class implements the abstract register allocator. It is used by the
  149. code generator to allocate and free registers which might be valid across
  150. nodes. It also contains utility routines related to registers.
  151. Some of the methods in this class should be overriden
  152. by cpu-specific implementations.
  153. --------------------------------------------------------------------}
  154. trgobj=class
  155. preserved_by_proc : tcpuregisterset;
  156. used_in_proc : tcpuregisterset;
  157. // is_reg_var : Tsuperregisterset; {old regvars}
  158. // reg_var_loaded:Tsuperregisterset; {old regvars}
  159. constructor create(Aregtype:Tregistertype;
  160. Adefaultsub:Tsubregister;
  161. const Ausable:array of tsuperregister;
  162. Afirst_imaginary:Tsuperregister;
  163. Apreserved_by_proc:Tcpuregisterset);
  164. destructor destroy;override;
  165. {# Allocate a register. An internalerror will be generated if there is
  166. no more free registers which can be allocated.}
  167. function getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;
  168. procedure add_constraints(reg:Tregister);virtual;
  169. {# Get the register specified.}
  170. procedure getexplicitregister(list:Taasmoutput;r:Tregister);
  171. {# Get multiple registers specified.}
  172. procedure allocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  173. {# Free multiple registers specified.}
  174. procedure deallocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  175. function uses_registers:boolean;
  176. {# Deallocate any kind of register }
  177. procedure ungetregister(list:Taasmoutput;r:Tregister);virtual;
  178. {# Do the register allocation.}
  179. procedure do_register_allocation(list:Taasmoutput;headertai:tai);
  180. { procedure resetusableregisters;virtual;}
  181. { procedure makeregvar(reg:Tsuperregister);}
  182. {$ifdef EXTDEBUG}
  183. procedure writegraph(loopidx:longint);
  184. {$endif EXTDEBUG}
  185. procedure add_move_instruction(instr:Taicpu);
  186. {# Prepare the register colouring.}
  187. procedure prepare_colouring;
  188. {# Clean up after register colouring.}
  189. procedure epilogue_colouring;
  190. {# Colour the registers; that is do the register allocation.}
  191. procedure colour_registers;
  192. {# Spills certain registers in the specified assembler list.}
  193. function spill_registers(list:Taasmoutput;headertai:tai):boolean;
  194. procedure translate_registers(list:Taasmoutput);
  195. {# Adds an interference edge.}
  196. procedure add_edge(u,v:Tsuperregister);
  197. procedure check_unreleasedregs;
  198. unusedregs : Tsuperregisterset;
  199. protected
  200. regtype : Tregistertype;
  201. { default subregister used }
  202. defaultsub : tsubregister;
  203. {# First imaginary register.}
  204. first_imaginary : Tsuperregister;
  205. {# Highest register allocated until now.}
  206. reginfo : PReginfo;
  207. maxreginfo,
  208. maxreginfoinc,
  209. maxreg : Tsuperregister;
  210. usable_registers_cnt : integer;
  211. usable_registers : array[0..maxcpuregister-1] of tsuperregister;
  212. ibitmap : Tinterferencebitmap;
  213. spillednodes,
  214. simplifyworklist,
  215. freezeworklist,
  216. spillworklist,
  217. coalescednodes,
  218. selectstack : tsuperregisterworklist;
  219. worklist_moves,
  220. active_moves,
  221. frozen_moves,
  222. coalesced_moves,
  223. constrained_moves : Tlinkedlist;
  224. function getnewreg:tsuperregister;
  225. procedure getregisterinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  226. procedure ungetregisterinline(list:Taasmoutput;position:Tai;r:Tregister);
  227. procedure add_edges_used(u:Tsuperregister);
  228. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  229. function move_related(n:Tsuperregister):boolean;
  230. procedure make_work_list;
  231. procedure enable_moves(n:Tsuperregister);
  232. procedure decrement_degree(m:Tsuperregister);
  233. procedure simplify;
  234. function get_alias(n:Tsuperregister):Tsuperregister;
  235. procedure add_worklist(u:Tsuperregister);
  236. function adjacent_ok(u,v:Tsuperregister):boolean;
  237. function conservative(u,v:Tsuperregister):boolean;
  238. procedure combine(u,v:Tsuperregister);
  239. procedure coalesce;
  240. procedure freeze_moves(u:Tsuperregister);
  241. procedure freeze;
  242. procedure select_spill;
  243. procedure assign_colours;
  244. procedure clear_interferences(u:Tsuperregister);
  245. end;
  246. const
  247. first_reg = 0;
  248. last_reg = high(tsuperregister)-1;
  249. maxspillingcounter = 20;
  250. implementation
  251. uses
  252. systems,
  253. globals,verbose,tgobj,procinfo;
  254. {******************************************************************************
  255. tsuperregisterworklist
  256. ******************************************************************************}
  257. constructor tsuperregisterworklist.init;
  258. begin
  259. length:=0;
  260. buflength:=0;
  261. buflengthinc:=16;
  262. head:=0;
  263. tail:=0;
  264. buf:=nil;
  265. end;
  266. destructor tsuperregisterworklist.done;
  267. begin
  268. if assigned(buf) then
  269. freemem(buf);
  270. end;
  271. procedure tsuperregisterworklist.add(s:tsuperregister);
  272. var
  273. oldbuflength : integer;
  274. newbuf : ^tsuperregister;
  275. begin
  276. inc(length);
  277. { Need to increase buffer length? }
  278. if length>=buflength then
  279. begin
  280. oldbuflength:=buflength;
  281. inc(buflength,buflengthinc);
  282. buflengthinc:=buflengthinc*2;
  283. if buflengthinc>256 then
  284. buflengthinc:=256;
  285. { We need to allocate a new block and move data around when the
  286. tail is wrapped around }
  287. if tail<head then
  288. begin
  289. Getmem(newbuf,buflength*sizeof(tsuperregister));
  290. move(buf[0],newbuf[oldbuflength-head],tail*sizeof(tsuperregister));
  291. move(buf[head],newbuf[0],(oldbuflength-head)*sizeof(tsuperregister));
  292. Freemem(buf);
  293. buf:=newbuf;
  294. head:=0;
  295. tail:=oldbuflength-1;
  296. end
  297. else
  298. Reallocmem(buf,buflength*sizeof(tsuperregister));
  299. end;
  300. buf[tail]:=s;
  301. inc(tail);
  302. if tail>=buflength then
  303. tail:=0;
  304. end;
  305. procedure tsuperregisterworklist.clear;
  306. begin
  307. length:=0;
  308. tail:=0;
  309. head:=0;
  310. end;
  311. procedure tsuperregisterworklist.next(var i:integer);
  312. begin
  313. inc(i);
  314. if i>=buflength then
  315. i:=0;
  316. end;
  317. function tsuperregisterworklist.getidx(i:integer):tsuperregister;
  318. begin
  319. result:=buf[i];
  320. end;
  321. procedure tsuperregisterworklist.deleteidx(i:integer);
  322. begin
  323. if length=0 then
  324. internalerror(200310144);
  325. buf[i]:=buf[head];
  326. inc(head);
  327. if head>=buflength then
  328. head:=0;
  329. dec(length);
  330. end;
  331. function tsuperregisterworklist.get:tsuperregister;
  332. begin
  333. if length=0 then
  334. internalerror(200310142);
  335. result:=buf[head];
  336. inc(head);
  337. if head>=buflength then
  338. head:=0;
  339. dec(length);
  340. end;
  341. function tsuperregisterworklist.getlast:tsuperregister;
  342. begin
  343. if length=0 then
  344. internalerror(200310143);
  345. dec(tail);
  346. if tail<0 then
  347. tail:=buflength-1;
  348. result:=buf[tail];
  349. dec(length);
  350. end;
  351. function tsuperregisterworklist.delete(s:tsuperregister):boolean;
  352. var
  353. i : integer;
  354. begin
  355. result:=false;
  356. i:=head;
  357. while (i<>tail) do
  358. begin
  359. if buf[i]=s then
  360. begin
  361. deleteidx(i);
  362. result:=true;
  363. exit;
  364. end;
  365. inc(i);
  366. if i>=buflength then
  367. i:=0;
  368. end;
  369. end;
  370. function tsuperregisterworklist.find(s:tsuperregister):boolean;
  371. var
  372. i : integer;
  373. begin
  374. result:=false;
  375. i:=head;
  376. while (i<>tail) do
  377. begin
  378. if buf[i]=s then
  379. begin
  380. result:=true;
  381. exit;
  382. end;
  383. inc(i);
  384. if i>=buflength then
  385. i:=0;
  386. end;
  387. end;
  388. {******************************************************************************
  389. tinterferencebitmap
  390. ******************************************************************************}
  391. constructor tinterferencebitmap.create;
  392. begin
  393. inherited create;
  394. maxx1:=1;
  395. getmem(fbitmap,sizeof(tinterferencebitmap1)*2);
  396. fillchar(fbitmap^,sizeof(tinterferencebitmap1)*2,0);
  397. end;
  398. destructor tinterferencebitmap.destroy;
  399. var
  400. i,j : byte;
  401. begin
  402. if assigned(fbitmap) then
  403. begin
  404. for i:=0 to maxx1 do
  405. for j:=0 to maxy1 do
  406. if assigned(fbitmap[i,j]) then
  407. dispose(fbitmap[i,j]);
  408. freemem(fbitmap);
  409. end;
  410. end;
  411. function tinterferencebitmap.getbitmap(x,y:tsuperregister):boolean;
  412. var
  413. page : pinterferencebitmap2;
  414. begin
  415. result:=false;
  416. if (x shr 8>maxx1) then
  417. exit;
  418. page:=fbitmap[x shr 8,y shr 8];
  419. result:=assigned(page) and
  420. ((x and $ff) in page^[y and $ff]);
  421. end;
  422. procedure tinterferencebitmap.setbitmap(x,y:tsuperregister;b:boolean);
  423. var
  424. x1,y1 : byte;
  425. begin
  426. x1:=x shr 8;
  427. y1:=y shr 8;
  428. if x1>maxx1 then
  429. begin
  430. reallocmem(fbitmap,sizeof(tinterferencebitmap1)*(x1+1));
  431. fillchar(fbitmap[maxx1+1],sizeof(tinterferencebitmap1)*(x1-maxx1),0);
  432. maxx1:=x1;
  433. end;
  434. if not assigned(fbitmap[x1,y1]) then
  435. begin
  436. if y1>maxy1 then
  437. maxy1:=y1;
  438. new(fbitmap[x1,y1]);
  439. fillchar(fbitmap[x1,y1]^,sizeof(tinterferencebitmap2),0);
  440. end;
  441. if b then
  442. include(fbitmap[x1,y1]^[y and $ff],(x and $ff))
  443. else
  444. exclude(fbitmap[x1,y1]^[y and $ff],(x and $ff));
  445. end;
  446. {******************************************************************************
  447. trgobj
  448. ******************************************************************************}
  449. constructor trgobj.create(Aregtype:Tregistertype;
  450. Adefaultsub:Tsubregister;
  451. const Ausable:array of tsuperregister;
  452. Afirst_imaginary:Tsuperregister;
  453. Apreserved_by_proc:Tcpuregisterset);
  454. var
  455. i : Tsuperregister;
  456. begin
  457. { empty super register sets can cause very strange problems }
  458. if high(Ausable)=0 then
  459. internalerror(200210181);
  460. first_imaginary:=Afirst_imaginary;
  461. maxreg:=Afirst_imaginary;
  462. regtype:=Aregtype;
  463. defaultsub:=Adefaultsub;
  464. preserved_by_proc:=Apreserved_by_proc;
  465. used_in_proc:=[];
  466. supregset_reset(unusedregs,true);
  467. { RS_INVALID can't be used }
  468. supregset_exclude(unusedregs,RS_INVALID);
  469. ibitmap:=tinterferencebitmap.create;
  470. { Get reginfo for CPU registers }
  471. reginfo:=allocmem(first_imaginary*sizeof(treginfo));
  472. maxreginfo:=first_imaginary;
  473. maxreginfoinc:=16;
  474. for i:=0 to first_imaginary-1 do
  475. reginfo[i].degree:=high(tsuperregister);
  476. worklist_moves:=Tlinkedlist.create;
  477. { Usable registers }
  478. fillchar(usable_registers,sizeof(usable_registers),0);
  479. for i:=low(Ausable) to high(Ausable) do
  480. usable_registers[i]:=Ausable[i];
  481. usable_registers_cnt:=high(Ausable)+1;
  482. { Initialize Worklists }
  483. spillednodes.init;
  484. simplifyworklist.init;
  485. freezeworklist.init;
  486. spillworklist.init;
  487. coalescednodes.init;
  488. selectstack.init;
  489. end;
  490. destructor trgobj.destroy;
  491. var i:Tsuperregister;
  492. begin
  493. spillednodes.done;
  494. simplifyworklist.done;
  495. freezeworklist.done;
  496. spillworklist.done;
  497. coalescednodes.done;
  498. selectstack.done;
  499. for i:=0 to maxreg-1 do
  500. begin
  501. if reginfo[i].adjlist<>nil then
  502. dispose(reginfo[i].adjlist,done);
  503. if reginfo[i].movelist<>nil then
  504. dispose(reginfo[i].movelist);
  505. end;
  506. freemem(reginfo);
  507. worklist_moves.free;
  508. ibitmap.free;
  509. end;
  510. function trgobj.getnewreg:tsuperregister;
  511. var
  512. oldmaxreginfo : tsuperregister;
  513. begin
  514. result:=maxreg;
  515. inc(maxreg);
  516. if maxreg>=last_reg then
  517. internalerror(200310146);
  518. if maxreg>=maxreginfo then
  519. begin
  520. oldmaxreginfo:=maxreginfo;
  521. inc(maxreginfo,maxreginfoinc);
  522. if maxreginfoinc<256 then
  523. maxreginfoinc:=maxreginfoinc*2;
  524. reallocmem(reginfo,maxreginfo*sizeof(treginfo));
  525. { Do we really need it to clear it ? At least for 1.0.x (PFV) }
  526. fillchar(reginfo[oldmaxreginfo],(maxreginfo-oldmaxreginfo)*sizeof(treginfo),0);
  527. end;
  528. end;
  529. function trgobj.getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;
  530. var p:Tsuperregister;
  531. r:Tregister;
  532. begin
  533. p:=getnewreg;
  534. supregset_exclude(unusedregs,p);
  535. r:=newreg(regtype,p,subreg);
  536. list.concat(Tai_regalloc.alloc(r));
  537. add_edges_used(p);
  538. add_constraints(r);
  539. result:=r;
  540. end;
  541. function trgobj.uses_registers:boolean;
  542. begin
  543. result:=(maxreg>first_imaginary);
  544. end;
  545. procedure trgobj.ungetregister(list:Taasmoutput;r:Tregister);
  546. var supreg:Tsuperregister;
  547. begin
  548. supreg:=getsupreg(r);
  549. if not supregset_in(unusedregs,supreg) then
  550. begin
  551. supregset_include(unusedregs,supreg);
  552. list.concat(Tai_regalloc.dealloc(r));
  553. add_edges_used(supreg);
  554. add_constraints(r);
  555. end;
  556. end;
  557. procedure trgobj.getexplicitregister(list:Taasmoutput;r:Tregister);
  558. var supreg:Tsuperregister;
  559. begin
  560. supreg:=getsupreg(r);
  561. if supregset_in(unusedregs,supreg) then
  562. begin
  563. supregset_exclude(unusedregs,supreg);
  564. if supreg<first_imaginary then
  565. include(used_in_proc,supreg);
  566. list.concat(Tai_regalloc.alloc(r));
  567. add_edges_used(supreg);
  568. add_constraints(r);
  569. end
  570. else
  571. {$ifndef ALLOWDUPREG}
  572. internalerror(200301103)
  573. {$else ALLOWDUPREG}
  574. list.concat(Tai_regalloc.alloc(r));
  575. {$endif ALLOWDUPREG}
  576. ;
  577. end;
  578. procedure trgobj.allocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  579. var reg:Tregister;
  580. i:Tsuperregister;
  581. begin
  582. if unusedregs[0]*r=r then
  583. begin
  584. unusedregs[0]:=unusedregs[0]-r;
  585. used_in_proc:=used_in_proc+r;
  586. for i:=0 to first_imaginary-1 do
  587. if i in r then
  588. begin
  589. add_edges_used(i);
  590. reg:=newreg(regtype,i,R_SUBWHOLE);
  591. list.concat(Tai_regalloc.alloc(reg));
  592. end;
  593. end
  594. else
  595. {$ifndef ALLOWDUPREG}
  596. internalerror(200305061)
  597. {$else ALLOWDUPREG}
  598. list.concat(Tai_regalloc.alloc(reg));
  599. {$endif ALLOWDUPREG}
  600. ;
  601. end;
  602. procedure trgobj.deallocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  603. var reg:Tregister;
  604. i:Tsuperregister;
  605. begin
  606. if unusedregs[0]*r=[] then
  607. begin
  608. unusedregs[0]:=unusedregs[0]+r;
  609. for i:=first_imaginary-1 downto 0 do
  610. if i in r then
  611. begin
  612. reg:=newreg(regtype,i,R_SUBWHOLE);
  613. list.concat(Tai_regalloc.dealloc(reg));
  614. end;
  615. end
  616. else
  617. {$ifndef ALLOWDUPREG}
  618. internalerror(200305061);
  619. {$else ALLOWDUPREG}
  620. list.concat(Tai_regalloc.dealloc(reg));
  621. {$endif ALLOWDUPREG}
  622. end;
  623. procedure trgobj.do_register_allocation(list:Taasmoutput;headertai:tai);
  624. var
  625. spillingcounter:byte;
  626. endspill:boolean;
  627. begin
  628. {Do register allocation.}
  629. spillingcounter:=0;
  630. repeat
  631. prepare_colouring;
  632. colour_registers;
  633. epilogue_colouring;
  634. endspill:=true;
  635. if spillednodes.length<>0 then
  636. begin
  637. inc(spillingcounter);
  638. if spillingcounter>maxspillingcounter then
  639. internalerror(200309041);
  640. endspill:=not spill_registers(list,headertai);
  641. end;
  642. until endspill;
  643. end;
  644. procedure trgobj.add_constraints(reg:Tregister);
  645. begin
  646. end;
  647. procedure trgobj.add_edge(u,v:Tsuperregister);
  648. {This procedure will add an edge to the virtual interference graph.}
  649. procedure addadj(u,v:Tsuperregister);
  650. begin
  651. if reginfo[u].adjlist=nil then
  652. new(reginfo[u].adjlist,init);
  653. reginfo[u].adjlist^.add(v);
  654. end;
  655. begin
  656. if (u<>v) and not(ibitmap[v,u]) then
  657. begin
  658. ibitmap[v,u]:=true;
  659. ibitmap[u,v]:=true;
  660. {Precoloured nodes are not stored in the interference graph.}
  661. if (u>=first_imaginary) then
  662. begin
  663. addadj(u,v);
  664. inc(reginfo[u].degree);
  665. end;
  666. if (v>=first_imaginary) then
  667. begin
  668. addadj(v,u);
  669. inc(reginfo[v].degree);
  670. end;
  671. end;
  672. end;
  673. procedure trgobj.add_edges_used(u:Tsuperregister);
  674. var i:Tsuperregister;
  675. begin
  676. for i:=0 to maxreg-1 do
  677. if not(supregset_in(unusedregs,i)) then
  678. add_edge(u,i);
  679. end;
  680. {$ifdef EXTDEBUG}
  681. procedure trgobj.writegraph(loopidx:longint);
  682. {This procedure writes out the current interference graph in the
  683. register allocator.}
  684. var f:text;
  685. i,j:Tsuperregister;
  686. begin
  687. assign(f,'igraph'+tostr(loopidx));
  688. rewrite(f);
  689. writeln(f,'Interference graph');
  690. writeln(f);
  691. write(f,' ');
  692. for i:=0 to 15 do
  693. for j:=0 to 15 do
  694. write(f,hexstr(i,1));
  695. writeln(f);
  696. write(f,' ');
  697. for i:=0 to 15 do
  698. write(f,'0123456789ABCDEF');
  699. writeln(f);
  700. for i:=0 to maxreg-1 do
  701. begin
  702. write(f,hexstr(i,2):4);
  703. for j:=0 to maxreg-1 do
  704. if ibitmap[i,j] then
  705. write(f,'*')
  706. else
  707. write(f,'-');
  708. writeln(f);
  709. end;
  710. close(f);
  711. end;
  712. {$endif EXTDEBUG}
  713. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  714. begin
  715. if reginfo[u].movelist=nil then
  716. begin
  717. getmem(reginfo[u].movelist,64);
  718. reginfo[u].movelist^.count:=0;
  719. end
  720. else if (reginfo[u].movelist^.count and 15)=15 then
  721. reallocmem(reginfo[u].movelist,(reginfo[u].movelist^.count+1)*4+64);
  722. reginfo[u].movelist^.data[reginfo[u].movelist^.count]:=data;
  723. inc(reginfo[u].movelist^.count);
  724. end;
  725. procedure trgobj.add_move_instruction(instr:Taicpu);
  726. {This procedure notifies a certain as a move instruction so the
  727. register allocator can try to eliminate it.}
  728. var i:Tmoveins;
  729. ssupreg,dsupreg:Tsuperregister;
  730. begin
  731. i:=Tmoveins.create;
  732. i.moveset:=ms_worklist_moves;
  733. i.instruction:=instr;
  734. worklist_moves.insert(i);
  735. ssupreg:=getsupreg(instr.oper[O_MOV_SOURCE]^.reg);
  736. add_to_movelist(ssupreg,i);
  737. dsupreg:=getsupreg(instr.oper[O_MOV_DEST]^.reg);
  738. if ssupreg<>dsupreg then
  739. {Avoid adding the same move instruction twice to a single register.}
  740. add_to_movelist(dsupreg,i);
  741. i.x:=ssupreg;
  742. i.y:=dsupreg;
  743. end;
  744. function trgobj.move_related(n:Tsuperregister):boolean;
  745. var i:cardinal;
  746. begin
  747. move_related:=false;
  748. if reginfo[n].movelist<>nil then
  749. begin
  750. for i:=0 to reginfo[n].movelist^.count-1 do
  751. if Tmoveins(reginfo[n].movelist^.data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  752. begin
  753. move_related:=true;
  754. break;
  755. end;
  756. end;
  757. end;
  758. procedure trgobj.make_work_list;
  759. var n:Tsuperregister;
  760. begin
  761. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  762. assign it to any of the registers, thus it is significant.}
  763. for n:=first_imaginary to maxreg-1 do
  764. if reginfo[n].degree>=usable_registers_cnt then
  765. spillworklist.add(n)
  766. else if move_related(n) then
  767. freezeworklist.add(n)
  768. else
  769. simplifyworklist.add(n);
  770. end;
  771. procedure trgobj.prepare_colouring;
  772. var
  773. i : integer;
  774. begin
  775. make_work_list;
  776. active_moves:=Tlinkedlist.create;
  777. frozen_moves:=Tlinkedlist.create;
  778. coalesced_moves:=Tlinkedlist.create;
  779. constrained_moves:=Tlinkedlist.create;
  780. for i:=0 to maxreg-1 do
  781. reginfo[i].alias:=RS_INVALID;
  782. coalescednodes.clear;
  783. selectstack.clear;
  784. end;
  785. procedure trgobj.enable_moves(n:Tsuperregister);
  786. var m:Tlinkedlistitem;
  787. i:cardinal;
  788. begin
  789. if reginfo[n].movelist<>nil then
  790. for i:=0 to reginfo[n].movelist^.count-1 do
  791. begin
  792. m:=reginfo[n].movelist^.data[i];
  793. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  794. begin
  795. if Tmoveins(m).moveset=ms_active_moves then
  796. begin
  797. {Move m from the set active_moves to the set worklist_moves.}
  798. active_moves.remove(m);
  799. Tmoveins(m).moveset:=ms_worklist_moves;
  800. worklist_moves.concat(m);
  801. end;
  802. end;
  803. end;
  804. end;
  805. procedure trgobj.decrement_degree(m:Tsuperregister);
  806. var adj : Psuperregisterworklist;
  807. d,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. p,n : Tsuperregister;
  843. min,i : integer;
  844. begin
  845. {We the element with the least interferences out of the
  846. simplifyworklist.}
  847. min:=high(integer);
  848. p:=0;
  849. n:=0;
  850. i:=simplifyworklist.head;
  851. while (i<>simplifyworklist.tail) do
  852. begin
  853. adj:=reginfo[simplifyworklist.buf[i]].adjlist;
  854. if adj=nil then
  855. begin
  856. p:=i;
  857. min:=0;
  858. break; {We won't find smaller ones.}
  859. end
  860. else
  861. if adj^.length<min then
  862. begin
  863. p:=i;
  864. min:=adj^.length;
  865. if min=0 then
  866. break; {We won't find smaller ones.}
  867. end;
  868. simplifyworklist.next(i);
  869. end;
  870. n:=simplifyworklist.getidx(p);
  871. simplifyworklist.deleteidx(p);
  872. {Push it on the selectstack.}
  873. selectstack.add(n);
  874. adj:=reginfo[n].adjlist;
  875. if adj<>nil then
  876. begin
  877. i:=adj^.head;
  878. while (i<>adj^.tail) do
  879. begin
  880. n:=adj^.buf[i];
  881. if (n>first_imaginary) and
  882. not(selectstack.find(n) or
  883. coalescednodes.find(n)) then
  884. decrement_degree(n);
  885. adj^.next(i);
  886. end;
  887. end;
  888. end;
  889. function trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  890. begin
  891. while coalescednodes.find(n) do
  892. n:=reginfo[n].alias;
  893. get_alias:=n;
  894. end;
  895. procedure trgobj.add_worklist(u:Tsuperregister);
  896. begin
  897. if (u>=first_imaginary) and
  898. not move_related(u) and
  899. (reginfo[u].degree<usable_registers_cnt) then
  900. begin
  901. if not freezeworklist.delete(u) then
  902. internalerror(200308161); {must be found}
  903. simplifyworklist.add(u);
  904. end;
  905. end;
  906. function trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  907. {Check wether u and v should be coalesced. u is precoloured.}
  908. function ok(t,r:Tsuperregister):boolean;
  909. begin
  910. ok:=(reginfo[t].degree<usable_registers_cnt) or
  911. (t<first_imaginary) or
  912. ibitmap[r,t];
  913. end;
  914. var adj : Psuperregisterworklist;
  915. i : integer;
  916. n : tsuperregister;
  917. begin
  918. adjacent_ok:=true;
  919. adj:=reginfo[v].adjlist;
  920. if adj<>nil then
  921. begin
  922. i:=adj^.head;
  923. while (i<>adj^.tail) do
  924. begin
  925. n:=adj^.buf[i];
  926. if not(selectstack.find(n) or
  927. coalescednodes.find(n)) and
  928. not ok(n,u) then
  929. begin
  930. adjacent_ok:=false;
  931. break;
  932. end;
  933. adj^.next(i);
  934. end;
  935. end;
  936. end;
  937. function trgobj.conservative(u,v:Tsuperregister):boolean;
  938. var adj : Psuperregisterworklist;
  939. done : Tsuperregisterset; {To prevent that we count nodes twice.}
  940. i,k : integer;
  941. n : tsuperregister;
  942. begin
  943. k:=0;
  944. supregset_reset(done,false);
  945. adj:=reginfo[u].adjlist;
  946. if adj<>nil then
  947. begin
  948. i:=adj^.head;
  949. while (i<>adj^.tail) do
  950. begin
  951. n:=adj^.buf[i];
  952. if not(selectstack.find(n) or
  953. coalescednodes.find(n)) then
  954. begin
  955. supregset_include(done,n);
  956. if reginfo[n].degree>=usable_registers_cnt then
  957. inc(k);
  958. end;
  959. adj^.next(i);
  960. end;
  961. end;
  962. adj:=reginfo[v].adjlist;
  963. if adj<>nil then
  964. begin
  965. i:=adj^.head;
  966. while (i<>adj^.tail) do
  967. begin
  968. n:=adj^.buf[i];
  969. if not supregset_in(done,n) and
  970. (reginfo[n].degree>=usable_registers_cnt) and
  971. not(selectstack.find(n) or
  972. coalescednodes.find(n)) then
  973. inc(k);
  974. adj^.next(i);
  975. end;
  976. end;
  977. conservative:=(k<usable_registers_cnt);
  978. end;
  979. procedure trgobj.combine(u,v:Tsuperregister);
  980. var add : boolean;
  981. adj : Psuperregisterworklist;
  982. i : integer;
  983. t : tsuperregister;
  984. n,o : cardinal;
  985. decrement : boolean;
  986. begin
  987. if not freezeworklist.delete(v) then
  988. spillworklist.delete(v);
  989. coalescednodes.add(v);
  990. reginfo[v].alias:=u;
  991. {Combine both movelists. Since the movelists are sets, only add
  992. elements that are not already present.}
  993. if assigned(reginfo[v].movelist) then
  994. begin
  995. for n:=0 to reginfo[v].movelist^.count-1 do
  996. begin
  997. add:=true;
  998. for o:=0 to reginfo[u].movelist^.count-1 do
  999. if reginfo[u].movelist^.data[o]=reginfo[v].movelist^.data[n] then
  1000. begin
  1001. add:=false;
  1002. break;
  1003. end;
  1004. if add then
  1005. add_to_movelist(u,reginfo[v].movelist^.data[n]);
  1006. end;
  1007. enable_moves(v);
  1008. end;
  1009. adj:=reginfo[v].adjlist;
  1010. if adj<>nil then
  1011. begin
  1012. i:=adj^.head;
  1013. while (i<>adj^.tail) do
  1014. begin
  1015. t:=adj^.buf[i];
  1016. if not(selectstack.find(t) or
  1017. coalescednodes.find(t)) then
  1018. begin
  1019. decrement:=(t<>u) and not(ibitmap[u,t]);
  1020. add_edge(t,u);
  1021. { Do not call decrement_degree because it might move nodes between
  1022. lists while the degree does not change (add_edge will increase it).
  1023. Instead, we will decrement manually. (Only if the degree has been
  1024. increased.) }
  1025. if decrement and
  1026. (t>=first_imaginary) and
  1027. (reginfo[t].degree>0) then
  1028. dec(reginfo[t].degree);
  1029. end;
  1030. adj^.next(i);
  1031. end;
  1032. end;
  1033. if (reginfo[u].degree>=usable_registers_cnt) and
  1034. freezeworklist.delete(u) then
  1035. spillworklist.add(u);
  1036. end;
  1037. procedure trgobj.coalesce;
  1038. var m:Tmoveins;
  1039. x,y,u,v:Tsuperregister;
  1040. begin
  1041. m:=Tmoveins(worklist_moves.getfirst);
  1042. x:=get_alias(getsupreg(m.instruction.oper[0]^.reg));
  1043. y:=get_alias(getsupreg(m.instruction.oper[1]^.reg));
  1044. if (y<first_imaginary) then
  1045. begin
  1046. u:=y;
  1047. v:=x;
  1048. end
  1049. else
  1050. begin
  1051. u:=x;
  1052. v:=y;
  1053. end;
  1054. if (u=v) then
  1055. begin
  1056. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1057. coalesced_moves.insert(m);
  1058. add_worklist(u);
  1059. end
  1060. {Do u and v interfere? In that case the move is constrained. Two
  1061. precoloured nodes interfere allways. If v is precoloured, by the above
  1062. code u is precoloured, thus interference...}
  1063. else if (v<first_imaginary) or ibitmap[u,v] then
  1064. begin
  1065. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1066. constrained_moves.insert(m);
  1067. add_worklist(u);
  1068. add_worklist(v);
  1069. end
  1070. {Next test: is it possible and a good idea to coalesce??}
  1071. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  1072. ((u>=first_imaginary) and conservative(u,v)) then
  1073. begin
  1074. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1075. coalesced_moves.insert(m);
  1076. combine(u,v);
  1077. add_worklist(u);
  1078. end
  1079. else
  1080. begin
  1081. m.moveset:=ms_active_moves;
  1082. active_moves.insert(m);
  1083. end;
  1084. end;
  1085. procedure trgobj.freeze_moves(u:Tsuperregister);
  1086. var i:cardinal;
  1087. m:Tlinkedlistitem;
  1088. v,x,y:Tsuperregister;
  1089. begin
  1090. if reginfo[u].movelist<>nil then
  1091. for i:=0 to reginfo[u].movelist^.count-1 do
  1092. begin
  1093. m:=reginfo[u].movelist^.data[i];
  1094. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1095. begin
  1096. x:=getsupreg(Tmoveins(m).instruction.oper[0]^.reg);
  1097. y:=getsupreg(Tmoveins(m).instruction.oper[1]^.reg);
  1098. if get_alias(y)=get_alias(u) then
  1099. v:=get_alias(x)
  1100. else
  1101. v:=get_alias(y);
  1102. {Move m from active_moves/worklist_moves to frozen_moves.}
  1103. if Tmoveins(m).moveset=ms_active_moves then
  1104. active_moves.remove(m)
  1105. else
  1106. worklist_moves.remove(m);
  1107. Tmoveins(m).moveset:=ms_frozen_moves;
  1108. frozen_moves.insert(m);
  1109. if (v>=first_imaginary) and
  1110. not(move_related(v)) and
  1111. (reginfo[v].degree<usable_registers_cnt) then
  1112. begin
  1113. freezeworklist.delete(v);
  1114. simplifyworklist.add(v);
  1115. end;
  1116. end;
  1117. end;
  1118. end;
  1119. procedure trgobj.freeze;
  1120. var n:Tsuperregister;
  1121. begin
  1122. { We need to take a random element out of the freezeworklist. We take
  1123. the last element. Dirty code! }
  1124. n:=freezeworklist.get;
  1125. {Add it to the simplifyworklist.}
  1126. simplifyworklist.add(n);
  1127. freeze_moves(n);
  1128. end;
  1129. procedure trgobj.select_spill;
  1130. var
  1131. n : tsuperregister;
  1132. adj : psuperregisterworklist;
  1133. max,p,i : integer;
  1134. begin
  1135. { We must look for the element with the most interferences in the
  1136. spillworklist. This is required because those registers are creating
  1137. the most conflicts and keeping them in a register will not reduce the
  1138. complexity and even can cause the help registers for the spilling code
  1139. to get too much conflicts with the result that the spilling code
  1140. will never converge (PFV) }
  1141. max:=0;
  1142. p:=0;
  1143. i:=spillworklist.head;
  1144. while (i<>spillworklist.tail) do
  1145. begin
  1146. adj:=reginfo[spillworklist.buf[i]].adjlist;
  1147. if assigned(adj) and
  1148. (adj^.length>max) then
  1149. begin
  1150. p:=i;
  1151. max:=adj^.length;
  1152. end;
  1153. spillworklist.next(i);
  1154. end;
  1155. n:=spillworklist.getidx(p);
  1156. spillworklist.deleteidx(p);
  1157. simplifyworklist.add(n);
  1158. freeze_moves(n);
  1159. end;
  1160. procedure trgobj.assign_colours;
  1161. {Assign_colours assigns the actual colours to the registers.}
  1162. var adj : Psuperregisterworklist;
  1163. i,j,k : integer;
  1164. n,a,c : Tsuperregister;
  1165. adj_colours,
  1166. colourednodes : Tsuperregisterset;
  1167. found : boolean;
  1168. begin
  1169. spillednodes.clear;
  1170. {Reset colours}
  1171. for n:=0 to maxreg-1 do
  1172. reginfo[n].colour:=n;
  1173. {Colour the cpu registers...}
  1174. supregset_reset(colourednodes,false);
  1175. for n:=0 to first_imaginary-1 do
  1176. supregset_include(colourednodes,n);
  1177. {Now colour the imaginary registers on the select-stack.}
  1178. while (selectstack.length>0) do
  1179. begin
  1180. n:=selectstack.getlast;
  1181. {Create a list of colours that we cannot assign to n.}
  1182. supregset_reset(adj_colours,false);
  1183. adj:=reginfo[n].adjlist;
  1184. if adj<>nil then
  1185. begin
  1186. j:=adj^.head;
  1187. while (j<>adj^.tail) do
  1188. begin
  1189. a:=get_alias(adj^.buf[j]);
  1190. if supregset_in(colourednodes,a) then
  1191. supregset_include(adj_colours,reginfo[a].colour);
  1192. adj^.next(j);
  1193. end;
  1194. supregset_include(adj_colours,RS_STACK_POINTER_REG);
  1195. end;
  1196. {Assume a spill by default...}
  1197. found:=false;
  1198. {Search for a colour not in this list.}
  1199. for k:=0 to usable_registers_cnt-1 do
  1200. begin
  1201. c:=usable_registers[k];
  1202. if not(supregset_in(adj_colours,c)) then
  1203. begin
  1204. reginfo[n].colour:=c;
  1205. found:=true;
  1206. supregset_include(colourednodes,n);
  1207. include(used_in_proc,c);
  1208. break;
  1209. end;
  1210. end;
  1211. if not found then
  1212. spillednodes.add(n);
  1213. end;
  1214. {Finally colour the nodes that were coalesced.}
  1215. i:=coalescednodes.head;
  1216. while (i<>coalescednodes.tail) do
  1217. begin
  1218. n:=coalescednodes.buf[i];
  1219. k:=get_alias(n);
  1220. reginfo[n].colour:=reginfo[k].colour;
  1221. if reginfo[k].colour<maxcpuregister then
  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:=high(tsuperregister);
  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,reginfo[getsupreg(so^.rs)].colour);
  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. procedure Trgobj.check_unreleasedregs;
  1590. {$ifdef EXTDEBUG}
  1591. var
  1592. sr : tsuperregister;
  1593. {$endif EXTDEBUG}
  1594. begin
  1595. {$ifdef EXTDEBUG}
  1596. for sr:=first_imaginary to maxreg-1 do
  1597. if not(supregset_in(unusedregs,sr)) then
  1598. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,sr,R_SUBNONE))+' not released');
  1599. {$endif EXTDEBUG}
  1600. end;
  1601. end.
  1602. {
  1603. $Log$
  1604. Revision 1.95 2003-11-10 19:05:50 peter
  1605. * fixed alias/colouring > 255
  1606. Revision 1.94 2003/11/07 15:58:32 florian
  1607. * Florian's culmutative nr. 1; contains:
  1608. - invalid calling conventions for a certain cpu are rejected
  1609. - arm softfloat calling conventions
  1610. - -Sp for cpu dependend code generation
  1611. - several arm fixes
  1612. - remaining code for value open array paras on heap
  1613. Revision 1.93 2003/10/30 16:22:40 peter
  1614. * call firstpass before allocation and codegeneration is started
  1615. * move leftover code from pass_2.generatecode() to psub
  1616. Revision 1.92 2003/10/29 21:29:14 jonas
  1617. * some ALLOWDUPREG improvements
  1618. Revision 1.91 2003/10/21 15:15:36 peter
  1619. * taicpu_abstract.oper[] changed to pointers
  1620. Revision 1.90 2003/10/19 12:36:36 florian
  1621. * improved speed; reduced memory usage of the interference bitmap
  1622. Revision 1.89 2003/10/19 01:34:30 florian
  1623. * some ppc stuff fixed
  1624. * memory leak fixed
  1625. Revision 1.88 2003/10/18 15:41:26 peter
  1626. * made worklists dynamic in size
  1627. Revision 1.87 2003/10/17 16:16:08 peter
  1628. * fixed last commit
  1629. Revision 1.86 2003/10/17 15:25:18 florian
  1630. * fixed more ppc stuff
  1631. Revision 1.85 2003/10/17 14:38:32 peter
  1632. * 64k registers supported
  1633. * fixed some memory leaks
  1634. Revision 1.84 2003/10/11 16:06:42 florian
  1635. * fixed some MMX<->SSE
  1636. * started to fix ppc, needs an overhaul
  1637. + stabs info improve for spilling, not sure if it works correctly/completly
  1638. - MMX_SUPPORT removed from Makefile.fpc
  1639. Revision 1.83 2003/10/10 17:48:14 peter
  1640. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1641. * tregisteralloctor renamed to trgobj
  1642. * removed rgobj from a lot of units
  1643. * moved location_* and reference_* to cgobj
  1644. * first things for mmx register allocation
  1645. Revision 1.82 2003/10/09 21:31:37 daniel
  1646. * Register allocator splitted, ans abstract now
  1647. Revision 1.81 2003/10/01 20:34:49 peter
  1648. * procinfo unit contains tprocinfo
  1649. * cginfo renamed to cgbase
  1650. * moved cgmessage to verbose
  1651. * fixed ppc and sparc compiles
  1652. Revision 1.80 2003/09/30 19:54:42 peter
  1653. * reuse registers with the least conflicts
  1654. Revision 1.79 2003/09/29 20:58:56 peter
  1655. * optimized releasing of registers
  1656. Revision 1.78 2003/09/28 13:41:12 peter
  1657. * return reg 255 when allowdupreg is defined
  1658. Revision 1.77 2003/09/25 16:19:32 peter
  1659. * fix filepositions
  1660. * insert spill temp allocations at the start of the proc
  1661. Revision 1.76 2003/09/16 16:17:01 peter
  1662. * varspez in calls to push_addr_param
  1663. Revision 1.75 2003/09/12 19:07:42 daniel
  1664. * Fixed fast spilling functionality by re-adding the code that initializes
  1665. precoloured nodes to degree 255. I would like to play hangman on the one
  1666. who removed that code.
  1667. Revision 1.74 2003/09/11 11:54:59 florian
  1668. * improved arm code generation
  1669. * move some protected and private field around
  1670. * the temp. register for register parameters/arguments are now released
  1671. before the move to the parameter register is done. This improves
  1672. the code in a lot of cases.
  1673. Revision 1.73 2003/09/09 20:59:27 daniel
  1674. * Adding register allocation order
  1675. Revision 1.72 2003/09/09 15:55:44 peter
  1676. * use register with least interferences in spillregister
  1677. Revision 1.71 2003/09/07 22:09:35 peter
  1678. * preparations for different default calling conventions
  1679. * various RA fixes
  1680. Revision 1.70 2003/09/03 21:06:45 peter
  1681. * fixes for FPU register allocation
  1682. Revision 1.69 2003/09/03 15:55:01 peter
  1683. * NEWRA branch merged
  1684. Revision 1.68 2003/09/03 11:18:37 florian
  1685. * fixed arm concatcopy
  1686. + arm support in the common compiler sources added
  1687. * moved some generic cg code around
  1688. + tfputype added
  1689. * ...
  1690. Revision 1.67.2.5 2003/08/31 20:44:07 peter
  1691. * fixed getexplicitregisterint tregister value
  1692. Revision 1.67.2.4 2003/08/31 20:40:50 daniel
  1693. * Fixed add_edges_used
  1694. Revision 1.67.2.3 2003/08/29 17:28:59 peter
  1695. * next batch of updates
  1696. Revision 1.67.2.2 2003/08/28 18:35:08 peter
  1697. * tregister changed to cardinal
  1698. Revision 1.67.2.1 2003/08/27 19:55:54 peter
  1699. * first tregister patch
  1700. Revision 1.67 2003/08/23 10:46:21 daniel
  1701. * Register allocator bugfix for h2pas
  1702. Revision 1.66 2003/08/17 16:59:20 jonas
  1703. * fixed regvars so they work with newra (at least for ppc)
  1704. * fixed some volatile register bugs
  1705. + -dnotranslation option for -dnewra, which causes the registers not to
  1706. be translated from virtual to normal registers. Requires support in
  1707. the assembler writer as well, which is only implemented in aggas/
  1708. agppcgas currently
  1709. Revision 1.65 2003/08/17 14:32:48 daniel
  1710. * Precoloured nodes now have an infinite degree approached with 255,
  1711. like they should.
  1712. Revision 1.64 2003/08/17 08:48:02 daniel
  1713. * Another register allocator bug fixed.
  1714. * usable_registers_cnt set to 6 for i386
  1715. Revision 1.63 2003/08/09 18:56:54 daniel
  1716. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1717. allocator
  1718. * Some preventive changes to i386 spillinh code
  1719. Revision 1.62 2003/08/03 14:09:50 daniel
  1720. * Fixed a register allocator bug
  1721. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1722. statements: changes in location_force. These moves are now no longer
  1723. constrained so they are optimized away.
  1724. Revision 1.61 2003/07/21 13:32:39 jonas
  1725. * add_edges_used() is now also called for registers allocated with
  1726. getexplicitregisterint()
  1727. * writing the intereference graph is now only done with -dradebug2 and
  1728. the created files are now called "igraph.<module_name>"
  1729. Revision 1.60 2003/07/06 15:31:21 daniel
  1730. * Fixed register allocator. *Lots* of fixes.
  1731. Revision 1.59 2003/07/06 15:00:47 jonas
  1732. * fixed my previous completely broken commit. It's not perfect though,
  1733. registers > last_int_supreg and < max_intreg may still be "translated"
  1734. Revision 1.58 2003/07/06 14:45:05 jonas
  1735. * support integer registers that are not managed by newra (ie. don't
  1736. translate register numbers that fall outside the range
  1737. first_int_supreg..last_int_supreg)
  1738. Revision 1.57 2003/07/02 22:18:04 peter
  1739. * paraloc splitted in callerparaloc,calleeparaloc
  1740. * sparc calling convention updates
  1741. Revision 1.56 2003/06/17 16:34:44 jonas
  1742. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1743. * renamed all_intregisters to volatile_intregisters and made it
  1744. processor dependent
  1745. Revision 1.55 2003/06/14 14:53:50 jonas
  1746. * fixed newra cycle for x86
  1747. * added constants for indicating source and destination operands of the
  1748. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1749. Revision 1.54 2003/06/13 21:19:31 peter
  1750. * current_procdef removed, use current_procinfo.procdef instead
  1751. Revision 1.53 2003/06/12 21:11:10 peter
  1752. * ungetregisterfpu gets size parameter
  1753. Revision 1.52 2003/06/12 16:43:07 peter
  1754. * newra compiles for sparc
  1755. Revision 1.51 2003/06/09 14:54:26 jonas
  1756. * (de)allocation of registers for parameters is now performed properly
  1757. (and checked on the ppc)
  1758. - removed obsolete allocation of all parameter registers at the start
  1759. of a procedure (and deallocation at the end)
  1760. Revision 1.50 2003/06/03 21:11:09 peter
  1761. * cg.a_load_* get a from and to size specifier
  1762. * makeregsize only accepts newregister
  1763. * i386 uses generic tcgnotnode,tcgunaryminus
  1764. Revision 1.49 2003/06/03 13:01:59 daniel
  1765. * Register allocator finished
  1766. Revision 1.48 2003/06/01 21:38:06 peter
  1767. * getregisterfpu size parameter added
  1768. * op_const_reg size parameter added
  1769. * sparc updates
  1770. Revision 1.47 2003/05/31 20:31:11 jonas
  1771. * set inital costs of assigning a variable to a register to 120 for
  1772. non-i386, because the used register must be store to memory at the
  1773. start and loaded again at the end
  1774. Revision 1.46 2003/05/30 18:55:21 jonas
  1775. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  1776. works for ppc
  1777. Revision 1.45 2003/05/30 12:36:13 jonas
  1778. * use as little different registers on the ppc until newra is released,
  1779. since every used register must be saved
  1780. Revision 1.44 2003/05/17 13:30:08 jonas
  1781. * changed tt_persistant to tt_persistent :)
  1782. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1783. temps, but a ttemptype, so you can also create ansistring temps etc
  1784. Revision 1.43 2003/05/16 14:33:31 peter
  1785. * regvar fixes
  1786. Revision 1.42 2003/04/26 20:03:49 daniel
  1787. * Bug fix in simplify
  1788. Revision 1.41 2003/04/25 20:59:35 peter
  1789. * removed funcretn,funcretsym, function result is now in varsym
  1790. and aliases for result and function name are added using absolutesym
  1791. * vs_hidden parameter for funcret passed in parameter
  1792. * vs_hidden fixes
  1793. * writenode changed to printnode and released from extdebug
  1794. * -vp option added to generate a tree.log with the nodetree
  1795. * nicer printnode for statements, callnode
  1796. Revision 1.40 2003/04/25 08:25:26 daniel
  1797. * Ifdefs around a lot of calls to cleartempgen
  1798. * Fixed registers that are allocated but not freed in several nodes
  1799. * Tweak to register allocator to cause less spills
  1800. * 8-bit registers now interfere with esi,edi and ebp
  1801. Compiler can now compile rtl successfully when using new register
  1802. allocator
  1803. Revision 1.39 2003/04/23 20:23:06 peter
  1804. * compile fix for no-newra
  1805. Revision 1.38 2003/04/23 14:42:07 daniel
  1806. * Further register allocator work. Compiler now smaller with new
  1807. allocator than without.
  1808. * Somebody forgot to adjust ppu version number
  1809. Revision 1.37 2003/04/22 23:50:23 peter
  1810. * firstpass uses expectloc
  1811. * checks if there are differences between the expectloc and
  1812. location.loc from secondpass in EXTDEBUG
  1813. Revision 1.36 2003/04/22 10:09:35 daniel
  1814. + Implemented the actual register allocator
  1815. + Scratch registers unavailable when new register allocator used
  1816. + maybe_save/maybe_restore unavailable when new register allocator used
  1817. Revision 1.35 2003/04/21 19:16:49 peter
  1818. * count address regs separate
  1819. Revision 1.34 2003/04/17 16:48:21 daniel
  1820. * Added some code to keep track of move instructions in register
  1821. allocator
  1822. Revision 1.33 2003/04/17 07:50:24 daniel
  1823. * Some work on interference graph construction
  1824. Revision 1.32 2003/03/28 19:16:57 peter
  1825. * generic constructor working for i386
  1826. * remove fixed self register
  1827. * esi added as address register for i386
  1828. Revision 1.31 2003/03/11 21:46:24 jonas
  1829. * lots of new regallocator fixes, both in generic and ppc-specific code
  1830. (ppc compiler still can't compile the linux system unit though)
  1831. Revision 1.30 2003/03/09 21:18:59 olle
  1832. + added cutils to the uses clause
  1833. Revision 1.29 2003/03/08 20:36:41 daniel
  1834. + Added newra version of Ti386shlshrnode
  1835. + Added interference graph construction code
  1836. Revision 1.28 2003/03/08 13:59:16 daniel
  1837. * Work to handle new register notation in ag386nsm
  1838. + Added newra version of Ti386moddivnode
  1839. Revision 1.27 2003/03/08 10:53:48 daniel
  1840. * Created newra version of secondmul in n386add.pas
  1841. Revision 1.26 2003/03/08 08:59:07 daniel
  1842. + $define newra will enable new register allocator
  1843. + getregisterint will return imaginary registers with $newra
  1844. + -sr switch added, will skip register allocation so you can see
  1845. the direct output of the code generator before register allocation
  1846. Revision 1.25 2003/02/26 20:50:45 daniel
  1847. * Fixed ungetreference
  1848. Revision 1.24 2003/02/19 22:39:56 daniel
  1849. * Fixed a few issues
  1850. Revision 1.23 2003/02/19 22:00:14 daniel
  1851. * Code generator converted to new register notation
  1852. - Horribily outdated todo.txt removed
  1853. Revision 1.22 2003/02/02 19:25:54 carl
  1854. * Several bugfixes for m68k target (register alloc., opcode emission)
  1855. + VIS target
  1856. + Generic add more complete (still not verified)
  1857. Revision 1.21 2003/01/08 18:43:57 daniel
  1858. * Tregister changed into a record
  1859. Revision 1.20 2002/10/05 12:43:28 carl
  1860. * fixes for Delphi 6 compilation
  1861. (warning : Some features do not work under Delphi)
  1862. Revision 1.19 2002/08/23 16:14:49 peter
  1863. * tempgen cleanup
  1864. * tt_noreuse temp type added that will be used in genentrycode
  1865. Revision 1.18 2002/08/17 22:09:47 florian
  1866. * result type handling in tcgcal.pass_2 overhauled
  1867. * better tnode.dowrite
  1868. * some ppc stuff fixed
  1869. Revision 1.17 2002/08/17 09:23:42 florian
  1870. * first part of procinfo rewrite
  1871. Revision 1.16 2002/08/06 20:55:23 florian
  1872. * first part of ppc calling conventions fix
  1873. Revision 1.15 2002/08/05 18:27:48 carl
  1874. + more more more documentation
  1875. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1876. Revision 1.14 2002/08/04 19:06:41 carl
  1877. + added generic exception support (still does not work!)
  1878. + more documentation
  1879. Revision 1.13 2002/07/07 09:52:32 florian
  1880. * powerpc target fixed, very simple units can be compiled
  1881. * some basic stuff for better callparanode handling, far from being finished
  1882. Revision 1.12 2002/07/01 18:46:26 peter
  1883. * internal linker
  1884. * reorganized aasm layer
  1885. Revision 1.11 2002/05/18 13:34:17 peter
  1886. * readded missing revisions
  1887. Revision 1.10 2002/05/16 19:46:44 carl
  1888. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1889. + try to fix temp allocation (still in ifdef)
  1890. + generic constructor calls
  1891. + start of tassembler / tmodulebase class cleanup
  1892. Revision 1.8 2002/04/21 15:23:03 carl
  1893. + makeregsize
  1894. + changeregsize is now a local routine
  1895. Revision 1.7 2002/04/20 21:32:25 carl
  1896. + generic FPC_CHECKPOINTER
  1897. + first parameter offset in stack now portable
  1898. * rename some constants
  1899. + move some cpu stuff to other units
  1900. - remove unused constents
  1901. * fix stacksize for some targets
  1902. * fix generic size problems which depend now on EXTEND_SIZE constant
  1903. Revision 1.6 2002/04/15 19:03:31 carl
  1904. + reg2str -> std_reg2str()
  1905. Revision 1.5 2002/04/06 18:13:01 jonas
  1906. * several powerpc-related additions and fixes
  1907. Revision 1.4 2002/04/04 19:06:04 peter
  1908. * removed unused units
  1909. * use tlocation.size in cg.a_*loc*() routines
  1910. Revision 1.3 2002/04/02 17:11:29 peter
  1911. * tlocation,treference update
  1912. * LOC_CONSTANT added for better constant handling
  1913. * secondadd splitted in multiple routines
  1914. * location_force_reg added for loading a location to a register
  1915. of a specified size
  1916. * secondassignment parses now first the right and then the left node
  1917. (this is compatible with Kylix). This saves a lot of push/pop especially
  1918. with string operations
  1919. * adapted some routines to use the new cg methods
  1920. Revision 1.2 2002/04/01 19:24:25 jonas
  1921. * fixed different parameter name in interface and implementation
  1922. declaration of a method (only 1.0.x detected this)
  1923. Revision 1.1 2002/03/31 20:26:36 jonas
  1924. + a_loadfpu_* and a_loadmm_* methods in tcg
  1925. * register allocation is now handled by a class and is mostly processor
  1926. independent (+rgobj.pas and i386/rgcpu.pas)
  1927. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1928. * some small improvements and fixes to the optimizer
  1929. * some register allocation fixes
  1930. * some fpuvaroffset fixes in the unary minus node
  1931. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1932. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1933. also better optimizable)
  1934. * fixed and optimized register saving/restoring for new/dispose nodes
  1935. * LOC_FPU locations now also require their "register" field to be set to
  1936. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1937. - list field removed of the tnode class because it's not used currently
  1938. and can cause hard-to-find bugs
  1939. }