rgobj.pas 76 KB

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