rgobj.pas 71 KB

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