rgobj.pas 70 KB

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