rgobj.pas 72 KB

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