rgobj.pas 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  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. unit rgobj;
  22. interface
  23. uses
  24. cutils, cpubase,
  25. aasmbase,aasmtai,aasmcpu,
  26. cclasses,globtype,cgbase,node,
  27. {$ifdef delphi}
  28. dmisc,
  29. {$endif}
  30. cpuinfo
  31. ;
  32. type
  33. {
  34. regvarother_longintarray = array[tregisterindex] of longint;
  35. regvarother_booleanarray = array[tregisterindex] of boolean;
  36. regvarint_longintarray = array[first_int_supreg..last_int_supreg] of longint;
  37. regvarint_ptreearray = array[first_int_supreg..last_int_supreg] of tnode;
  38. }
  39. {
  40. The interference bitmap contains of 2 layers:
  41. layer 1 - 256*256 blocks with pointers to layer 2 blocks
  42. layer 2 - blocks of 32*256 (32 bytes = 256 bits)
  43. }
  44. Tinterferencebitmap2 = array[byte] of set of byte;
  45. Pinterferencebitmap2 = ^Tinterferencebitmap2;
  46. Tinterferencebitmap1 = array[byte] of Pinterferencebitmap2;
  47. pinterferencebitmap1 = ^tinterferencebitmap1;
  48. Tinterferencebitmap=class
  49. private
  50. maxx1,
  51. maxy1 : byte;
  52. fbitmap : pinterferencebitmap1;
  53. function getbitmap(x,y:tsuperregister):boolean;
  54. procedure setbitmap(x,y:tsuperregister;b:boolean);
  55. public
  56. constructor create;
  57. destructor destroy;override;
  58. property bitmap[x,y:tsuperregister]:boolean read getbitmap write setbitmap;default;
  59. end;
  60. Tmovelistheader=record
  61. count,
  62. maxcount,
  63. sorted_until : cardinal;
  64. end;
  65. Tmovelist=record
  66. header : Tmovelistheader;
  67. data : array[tsuperregister] of Tlinkedlistitem;
  68. end;
  69. Pmovelist=^Tmovelist;
  70. {In the register allocator we keep track of move instructions.
  71. These instructions are moved between five linked lists. There
  72. is also a linked list per register to keep track about the moves
  73. it is associated with. Because we need to determine quickly in
  74. which of the five lists it is we add anu enumeradtion to each
  75. move instruction.}
  76. Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
  77. ms_worklist_moves,ms_active_moves);
  78. Tmoveins=class(Tlinkedlistitem)
  79. moveset:Tmoveset;
  80. x,y:Tsuperregister;
  81. end;
  82. Treginfoflag=(ri_coalesced,ri_selected);
  83. Treginfoflagset=set of Treginfoflag;
  84. Treginfo=record
  85. live_start,
  86. live_end : Tai;
  87. subreg : tsubregister;
  88. alias : Tsuperregister;
  89. { The register allocator assigns each register a colour }
  90. colour : Tsuperregister;
  91. movelist : Pmovelist;
  92. adjlist : Psuperregisterworklist;
  93. degree : TSuperregister;
  94. flags : Treginfoflagset;
  95. end;
  96. Preginfo=^TReginfo;
  97. tspillreginfo = record
  98. spillreg : tregister;
  99. orgreg : tsuperregister;
  100. tempreg : tregister;
  101. regread,regwritten, mustbespilled: boolean;
  102. end;
  103. tspillregsinfo = array[0..2] of tspillreginfo;
  104. {#------------------------------------------------------------------
  105. This class implements the default register allocator. It is used by the
  106. code generator to allocate and free registers which might be valid
  107. across nodes. It also contains utility routines related to registers.
  108. Some of the methods in this class should be overriden
  109. by cpu-specific implementations.
  110. --------------------------------------------------------------------}
  111. trgobj=class
  112. preserved_by_proc : tcpuregisterset;
  113. used_in_proc : tcpuregisterset;
  114. constructor create(Aregtype:Tregistertype;
  115. Adefaultsub:Tsubregister;
  116. const Ausable:array of tsuperregister;
  117. Afirst_imaginary:Tsuperregister;
  118. Apreserved_by_proc:Tcpuregisterset);
  119. destructor destroy;override;
  120. {# Allocate a register. An internalerror will be generated if there is
  121. no more free registers which can be allocated.}
  122. function getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;virtual;
  123. {# Get the register specified.}
  124. procedure getcpuregister(list:Taasmoutput;r:Tregister);virtual;
  125. procedure ungetcpuregister(list:Taasmoutput;r:Tregister);virtual;
  126. {# Get multiple registers specified.}
  127. procedure alloccpuregisters(list:Taasmoutput;r:Tcpuregisterset);virtual;
  128. {# Free multiple registers specified.}
  129. procedure dealloccpuregisters(list:Taasmoutput;r:Tcpuregisterset);virtual;
  130. function uses_registers:boolean;virtual;
  131. procedure add_reg_instruction(instr:Tai;r:tregister);
  132. procedure add_move_instruction(instr:Taicpu);
  133. {# Do the register allocation.}
  134. procedure do_register_allocation(list:Taasmoutput;headertai:tai);virtual;
  135. { Adds an interference edge.
  136. don't move this to the protected section, the arm cg requires to access this (FK) }
  137. procedure add_edge(u,v:Tsuperregister);
  138. protected
  139. regtype : Tregistertype;
  140. { default subregister used }
  141. defaultsub : tsubregister;
  142. live_registers:Tsuperregisterworklist;
  143. { can be overriden to add cpu specific interferences }
  144. procedure add_cpu_interferences(p : tai);virtual;
  145. procedure add_constraints(reg:Tregister);virtual;
  146. procedure getregisterinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  147. procedure ungetregisterinline(list:Taasmoutput;position:Tai;r:Tregister);
  148. function get_spill_subreg(r : tregister) : tsubregister;virtual;
  149. procedure do_spill_read(list:Taasmoutput;instr:taicpu;const spilltemp:treference;const tempreg:tregister);virtual;
  150. procedure do_spill_written(list:Taasmoutput;instr:taicpu;const spilltemp:treference;const tempreg:tregister);virtual;
  151. function instr_spill_register(list:Taasmoutput;
  152. instr:taicpu;
  153. const r:Tsuperregisterset;
  154. const spilltemplist:Tspill_temp_list): boolean;virtual;
  155. private
  156. {# First imaginary register.}
  157. first_imaginary : Tsuperregister;
  158. {# Highest register allocated until now.}
  159. reginfo : PReginfo;
  160. maxreginfo,
  161. maxreginfoinc,
  162. maxreg : Tsuperregister;
  163. usable_registers_cnt : word;
  164. usable_registers : array[0..maxcpuregister-1] of tsuperregister;
  165. ibitmap : Tinterferencebitmap;
  166. spillednodes,
  167. simplifyworklist,
  168. freezeworklist,
  169. spillworklist,
  170. coalescednodes,
  171. selectstack : tsuperregisterworklist;
  172. worklist_moves,
  173. active_moves,
  174. frozen_moves,
  175. coalesced_moves,
  176. constrained_moves : Tlinkedlist;
  177. {$ifdef EXTDEBUG}
  178. procedure writegraph(loopidx:longint);
  179. {$endif EXTDEBUG}
  180. {# Disposes of the reginfo array.}
  181. procedure dispose_reginfo;
  182. {# Prepare the register colouring.}
  183. procedure prepare_colouring;
  184. {# Clean up after register colouring.}
  185. procedure epilogue_colouring;
  186. {# Colour the registers; that is do the register allocation.}
  187. procedure colour_registers;
  188. {# Spills certain registers in the specified assembler list.}
  189. procedure insert_regalloc_info(list:Taasmoutput;headertai:tai);
  190. procedure generate_interference_graph(list:Taasmoutput;headertai:tai);
  191. procedure translate_registers(list:Taasmoutput);
  192. function spill_registers(list:Taasmoutput;headertai:tai):boolean;virtual;
  193. function getnewreg(subreg:tsubregister):tsuperregister;
  194. procedure add_edges_used(u:Tsuperregister);
  195. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  196. function move_related(n:Tsuperregister):boolean;
  197. procedure make_work_list;
  198. procedure sort_simplify_worklist;
  199. procedure enable_moves(n:Tsuperregister);
  200. procedure decrement_degree(m:Tsuperregister);
  201. procedure simplify;
  202. function get_alias(n:Tsuperregister):Tsuperregister;
  203. procedure add_worklist(u:Tsuperregister);
  204. function adjacent_ok(u,v:Tsuperregister):boolean;
  205. function conservative(u,v:Tsuperregister):boolean;
  206. procedure combine(u,v:Tsuperregister);
  207. procedure coalesce;
  208. procedure freeze_moves(u:Tsuperregister);
  209. procedure freeze;
  210. procedure select_spill;
  211. procedure assign_colours;
  212. procedure clear_interferences(u:Tsuperregister);
  213. end;
  214. const
  215. first_reg = 0;
  216. last_reg = high(tsuperregister)-1;
  217. maxspillingcounter = 20;
  218. implementation
  219. uses
  220. systems,
  221. globals,verbose,tgobj,procinfo;
  222. procedure sort_movelist(ml:Pmovelist);
  223. {Ok, sorting pointers is silly, but it does the job to make Trgobj.combine
  224. faster.}
  225. var h,i,p:word;
  226. t:Tlinkedlistitem;
  227. begin
  228. with ml^ do
  229. begin
  230. if header.count<2 then
  231. exit;
  232. p:=1;
  233. while 2*p<header.count do
  234. p:=2*p;
  235. while p<>0 do
  236. begin
  237. for h:=p to header.count-1 do
  238. begin
  239. i:=h;
  240. t:=data[i];
  241. repeat
  242. if ptrint(data[i-p])<=ptrint(t) then
  243. break;
  244. data[i]:=data[i-p];
  245. dec(i,p);
  246. until i<p;
  247. data[i]:=t;
  248. end;
  249. p:=p shr 1;
  250. end;
  251. header.sorted_until:=header.count-1;
  252. end;
  253. end;
  254. {******************************************************************************
  255. tinterferencebitmap
  256. ******************************************************************************}
  257. constructor tinterferencebitmap.create;
  258. begin
  259. inherited create;
  260. maxx1:=1;
  261. getmem(fbitmap,sizeof(tinterferencebitmap1)*2);
  262. fillchar(fbitmap^,sizeof(tinterferencebitmap1)*2,0);
  263. end;
  264. destructor tinterferencebitmap.destroy;
  265. var i,j:byte;
  266. begin
  267. for i:=0 to maxx1 do
  268. for j:=0 to maxy1 do
  269. if assigned(fbitmap[i,j]) then
  270. dispose(fbitmap[i,j]);
  271. freemem(fbitmap);
  272. end;
  273. function tinterferencebitmap.getbitmap(x,y:tsuperregister):boolean;
  274. var
  275. page : pinterferencebitmap2;
  276. begin
  277. result:=false;
  278. if (x shr 8>maxx1) then
  279. exit;
  280. page:=fbitmap[x shr 8,y shr 8];
  281. result:=assigned(page) and
  282. ((x and $ff) in page^[y and $ff]);
  283. end;
  284. procedure tinterferencebitmap.setbitmap(x,y:tsuperregister;b:boolean);
  285. var
  286. x1,y1 : byte;
  287. begin
  288. x1:=x shr 8;
  289. y1:=y shr 8;
  290. if x1>maxx1 then
  291. begin
  292. reallocmem(fbitmap,sizeof(tinterferencebitmap1)*(x1+1));
  293. fillchar(fbitmap[maxx1+1],sizeof(tinterferencebitmap1)*(x1-maxx1),0);
  294. maxx1:=x1;
  295. end;
  296. if not assigned(fbitmap[x1,y1]) then
  297. begin
  298. if y1>maxy1 then
  299. maxy1:=y1;
  300. new(fbitmap[x1,y1]);
  301. fillchar(fbitmap[x1,y1]^,sizeof(tinterferencebitmap2),0);
  302. end;
  303. if b then
  304. include(fbitmap[x1,y1]^[y and $ff],(x and $ff))
  305. else
  306. exclude(fbitmap[x1,y1]^[y and $ff],(x and $ff));
  307. end;
  308. {******************************************************************************
  309. trgobj
  310. ******************************************************************************}
  311. constructor trgobj.create(Aregtype:Tregistertype;
  312. Adefaultsub:Tsubregister;
  313. const Ausable:array of tsuperregister;
  314. Afirst_imaginary:Tsuperregister;
  315. Apreserved_by_proc:Tcpuregisterset);
  316. var
  317. i : Tsuperregister;
  318. begin
  319. { empty super register sets can cause very strange problems }
  320. if high(Ausable)=0 then
  321. internalerror(200210181);
  322. first_imaginary:=Afirst_imaginary;
  323. maxreg:=Afirst_imaginary;
  324. regtype:=Aregtype;
  325. defaultsub:=Adefaultsub;
  326. preserved_by_proc:=Apreserved_by_proc;
  327. used_in_proc:=[];
  328. live_registers.init;
  329. { Get reginfo for CPU registers }
  330. maxreginfo:=first_imaginary;
  331. maxreginfoinc:=16;
  332. worklist_moves:=Tlinkedlist.create;
  333. reginfo:=allocmem(first_imaginary*sizeof(treginfo));
  334. for i:=0 to first_imaginary-1 do
  335. begin
  336. reginfo[i].degree:=high(tsuperregister);
  337. reginfo[i].alias:=RS_INVALID;
  338. end;
  339. { Usable registers }
  340. fillchar(usable_registers,sizeof(usable_registers),0);
  341. for i:=low(Ausable) to high(Ausable) do
  342. usable_registers[i]:=Ausable[i];
  343. usable_registers_cnt:=high(Ausable)+1;
  344. { Initialize Worklists }
  345. spillednodes.init;
  346. simplifyworklist.init;
  347. freezeworklist.init;
  348. spillworklist.init;
  349. coalescednodes.init;
  350. selectstack.init;
  351. end;
  352. destructor trgobj.destroy;
  353. begin
  354. spillednodes.done;
  355. simplifyworklist.done;
  356. freezeworklist.done;
  357. spillworklist.done;
  358. coalescednodes.done;
  359. selectstack.done;
  360. live_registers.done;
  361. worklist_moves.free;
  362. dispose_reginfo;
  363. end;
  364. procedure Trgobj.dispose_reginfo;
  365. var i:Tsuperregister;
  366. begin
  367. if reginfo<>nil then
  368. begin
  369. for i:=0 to maxreg-1 do
  370. with reginfo[i] do
  371. begin
  372. if adjlist<>nil then
  373. dispose(adjlist,done);
  374. if movelist<>nil then
  375. dispose(movelist);
  376. end;
  377. freemem(reginfo);
  378. reginfo:=nil;
  379. end;
  380. end;
  381. function trgobj.getnewreg(subreg:tsubregister):tsuperregister;
  382. var
  383. oldmaxreginfo : tsuperregister;
  384. begin
  385. result:=maxreg;
  386. inc(maxreg);
  387. if maxreg>=last_reg then
  388. internalerror(200310146);
  389. if maxreg>=maxreginfo then
  390. begin
  391. oldmaxreginfo:=maxreginfo;
  392. inc(maxreginfo,maxreginfoinc);
  393. if maxreginfoinc<256 then
  394. maxreginfoinc:=maxreginfoinc*2;
  395. reallocmem(reginfo,maxreginfo*sizeof(treginfo));
  396. { Do we really need it to clear it ? At least for 1.0.x (PFV) }
  397. fillchar(reginfo[oldmaxreginfo],(maxreginfo-oldmaxreginfo)*sizeof(treginfo),0);
  398. end;
  399. reginfo[result].subreg:=subreg;
  400. end;
  401. function trgobj.getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;
  402. begin
  403. {$ifdef EXTDEBUG}
  404. if reginfo=nil then
  405. InternalError(2004020901);
  406. {$endif EXTDEBUG}
  407. if defaultsub=R_SUBNONE then
  408. result:=newreg(regtype,getnewreg(R_SUBNONE),R_SUBNONE)
  409. else
  410. result:=newreg(regtype,getnewreg(subreg),subreg);
  411. end;
  412. function trgobj.uses_registers:boolean;
  413. begin
  414. result:=(maxreg>first_imaginary);
  415. end;
  416. procedure trgobj.ungetcpuregister(list:Taasmoutput;r:Tregister);
  417. begin
  418. if (getsupreg(r)>=first_imaginary) then
  419. InternalError(2004020901);
  420. list.concat(Tai_regalloc.dealloc(r));
  421. end;
  422. procedure trgobj.getcpuregister(list:Taasmoutput;r:Tregister);
  423. var
  424. supreg:Tsuperregister;
  425. begin
  426. supreg:=getsupreg(r);
  427. if supreg>=first_imaginary then
  428. internalerror(2003121503);
  429. include(used_in_proc,supreg);
  430. list.concat(Tai_regalloc.alloc(r));
  431. end;
  432. procedure trgobj.alloccpuregisters(list:Taasmoutput;r:Tcpuregisterset);
  433. var i:Tsuperregister;
  434. begin
  435. for i:=0 to first_imaginary-1 do
  436. if i in r then
  437. getcpuregister(list,newreg(regtype,i,defaultsub));
  438. end;
  439. procedure trgobj.dealloccpuregisters(list:Taasmoutput;r:Tcpuregisterset);
  440. var i:Tsuperregister;
  441. begin
  442. for i:=0 to first_imaginary-1 do
  443. if i in r then
  444. ungetcpuregister(list,newreg(regtype,i,defaultsub));
  445. end;
  446. procedure trgobj.do_register_allocation(list:Taasmoutput;headertai:tai);
  447. var
  448. spillingcounter:byte;
  449. endspill:boolean;
  450. i:Tsuperregister;
  451. begin
  452. { Insert regalloc info for imaginary registers }
  453. insert_regalloc_info(list,headertai);
  454. ibitmap:=tinterferencebitmap.create;
  455. generate_interference_graph(list,headertai);
  456. { Don't do the real allocation when -sr is passed }
  457. if (cs_no_regalloc in aktglobalswitches) then
  458. exit;
  459. {Do register allocation.}
  460. spillingcounter:=0;
  461. repeat
  462. prepare_colouring;
  463. colour_registers;
  464. epilogue_colouring;
  465. endspill:=true;
  466. if spillednodes.length<>0 then
  467. begin
  468. inc(spillingcounter);
  469. if spillingcounter>maxspillingcounter then
  470. internalerror(200309041);
  471. endspill:=not spill_registers(list,headertai);
  472. end;
  473. until endspill;
  474. ibitmap.free;
  475. translate_registers(list);
  476. dispose_reginfo;
  477. end;
  478. procedure trgobj.add_constraints(reg:Tregister);
  479. begin
  480. end;
  481. procedure trgobj.add_edge(u,v:Tsuperregister);
  482. {This procedure will add an edge to the virtual interference graph.}
  483. procedure addadj(u,v:Tsuperregister);
  484. begin
  485. with reginfo[u] do
  486. begin
  487. if adjlist=nil then
  488. new(adjlist,init);
  489. adjlist^.add(v);
  490. end;
  491. end;
  492. begin
  493. if (u<>v) and not(ibitmap[v,u]) then
  494. begin
  495. ibitmap[v,u]:=true;
  496. ibitmap[u,v]:=true;
  497. {Precoloured nodes are not stored in the interference graph.}
  498. if (u>=first_imaginary) then
  499. addadj(u,v);
  500. if (v>=first_imaginary) then
  501. addadj(v,u);
  502. end;
  503. end;
  504. procedure trgobj.add_edges_used(u:Tsuperregister);
  505. var i:word;
  506. begin
  507. with live_registers do
  508. if length>0 then
  509. for i:=0 to length-1 do
  510. add_edge(u,get_alias(buf^[i]));
  511. end;
  512. {$ifdef EXTDEBUG}
  513. procedure trgobj.writegraph(loopidx:longint);
  514. {This procedure writes out the current interference graph in the
  515. register allocator.}
  516. var f:text;
  517. i,j:Tsuperregister;
  518. begin
  519. assign(f,'igraph'+tostr(loopidx));
  520. rewrite(f);
  521. writeln(f,'Interference graph');
  522. writeln(f);
  523. write(f,' ');
  524. for i:=0 to 15 do
  525. for j:=0 to 15 do
  526. write(f,hexstr(i,1));
  527. writeln(f);
  528. write(f,' ');
  529. for i:=0 to 15 do
  530. write(f,'0123456789ABCDEF');
  531. writeln(f);
  532. for i:=0 to maxreg-1 do
  533. begin
  534. write(f,hexstr(i,2):4);
  535. for j:=0 to maxreg-1 do
  536. if ibitmap[i,j] then
  537. write(f,'*')
  538. else
  539. write(f,'-');
  540. writeln(f);
  541. end;
  542. close(f);
  543. end;
  544. {$endif EXTDEBUG}
  545. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  546. begin
  547. with reginfo[u] do
  548. begin
  549. if movelist=nil then
  550. begin
  551. getmem(movelist,sizeof(tmovelistheader)+60*sizeof(pointer));
  552. movelist^.header.maxcount:=60;
  553. movelist^.header.count:=0;
  554. movelist^.header.sorted_until:=0;
  555. end
  556. else
  557. begin
  558. if movelist^.header.count>=movelist^.header.maxcount then
  559. begin
  560. movelist^.header.maxcount:=movelist^.header.maxcount*2;
  561. reallocmem(movelist,sizeof(tmovelistheader)+movelist^.header.maxcount*sizeof(pointer));
  562. end;
  563. end;
  564. movelist^.data[movelist^.header.count]:=data;
  565. inc(movelist^.header.count);
  566. end;
  567. end;
  568. procedure trgobj.add_reg_instruction(instr:Tai;r:tregister);
  569. var
  570. supreg : tsuperregister;
  571. begin
  572. supreg:=getsupreg(r);
  573. if supreg>=first_imaginary then
  574. with reginfo[supreg] do
  575. begin
  576. if not assigned(live_start) then
  577. live_start:=instr;
  578. live_end:=instr;
  579. end;
  580. end;
  581. procedure trgobj.add_move_instruction(instr:Taicpu);
  582. {This procedure notifies a certain as a move instruction so the
  583. register allocator can try to eliminate it.}
  584. var i:Tmoveins;
  585. ssupreg,dsupreg:Tsuperregister;
  586. begin
  587. {$ifdef extdebug}
  588. if (instr.oper[O_MOV_SOURCE]^.typ<>top_reg) or
  589. (instr.oper[O_MOV_DEST]^.typ<>top_reg) then
  590. internalerror(200311291);
  591. {$endif}
  592. i:=Tmoveins.create;
  593. i.moveset:=ms_worklist_moves;
  594. worklist_moves.insert(i);
  595. ssupreg:=getsupreg(instr.oper[O_MOV_SOURCE]^.reg);
  596. add_to_movelist(ssupreg,i);
  597. dsupreg:=getsupreg(instr.oper[O_MOV_DEST]^.reg);
  598. if ssupreg<>dsupreg then
  599. {Avoid adding the same move instruction twice to a single register.}
  600. add_to_movelist(dsupreg,i);
  601. i.x:=ssupreg;
  602. i.y:=dsupreg;
  603. end;
  604. function trgobj.move_related(n:Tsuperregister):boolean;
  605. var i:cardinal;
  606. begin
  607. move_related:=false;
  608. with reginfo[n] do
  609. if movelist<>nil then
  610. with movelist^ do
  611. for i:=0 to header.count-1 do
  612. if Tmoveins(data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  613. begin
  614. move_related:=true;
  615. break;
  616. end;
  617. end;
  618. procedure Trgobj.sort_simplify_worklist;
  619. {Sorts the simplifyworklist by the number of interferences the
  620. registers in it cause. This allows simplify to execute in
  621. constant time.}
  622. var p,h,i,leni,lent:word;
  623. t:Tsuperregister;
  624. adji,adjt:Psuperregisterworklist;
  625. begin
  626. with simplifyworklist do
  627. begin
  628. if length<2 then
  629. exit;
  630. p:=1;
  631. while 2*p<length do
  632. p:=2*p;
  633. while p<>0 do
  634. begin
  635. for h:=p to length-1 do
  636. begin
  637. i:=h;
  638. t:=buf^[i];
  639. adjt:=reginfo[buf^[i]].adjlist;
  640. lent:=0;
  641. if adjt<>nil then
  642. lent:=adjt^.length;
  643. repeat
  644. adji:=reginfo[buf^[i-p]].adjlist;
  645. leni:=0;
  646. if adji<>nil then
  647. leni:=adji^.length;
  648. if leni<=lent then
  649. break;
  650. buf^[i]:=buf^[i-p];
  651. dec(i,p)
  652. until i<p;
  653. buf^[i]:=t;
  654. end;
  655. p:=p shr 1;
  656. end;
  657. end;
  658. end;
  659. procedure trgobj.make_work_list;
  660. var n:Tsuperregister;
  661. begin
  662. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  663. assign it to any of the registers, thus it is significant.}
  664. for n:=first_imaginary to maxreg-1 do
  665. with reginfo[n] do
  666. begin
  667. if adjlist=nil then
  668. degree:=0
  669. else
  670. degree:=adjlist^.length;
  671. if degree>=usable_registers_cnt then
  672. spillworklist.add(n)
  673. else if move_related(n) then
  674. freezeworklist.add(n)
  675. else
  676. simplifyworklist.add(n);
  677. end;
  678. sort_simplify_worklist;
  679. end;
  680. procedure trgobj.prepare_colouring;
  681. var i:word;
  682. begin
  683. make_work_list;
  684. active_moves:=Tlinkedlist.create;
  685. frozen_moves:=Tlinkedlist.create;
  686. coalesced_moves:=Tlinkedlist.create;
  687. constrained_moves:=Tlinkedlist.create;
  688. selectstack.clear;
  689. end;
  690. procedure trgobj.enable_moves(n:Tsuperregister);
  691. var m:Tlinkedlistitem;
  692. i:cardinal;
  693. begin
  694. with reginfo[n] do
  695. if movelist<>nil then
  696. for i:=0 to movelist^.header.count-1 do
  697. begin
  698. m:=movelist^.data[i];
  699. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  700. if Tmoveins(m).moveset=ms_active_moves then
  701. begin
  702. {Move m from the set active_moves to the set worklist_moves.}
  703. active_moves.remove(m);
  704. Tmoveins(m).moveset:=ms_worklist_moves;
  705. worklist_moves.concat(m);
  706. end;
  707. end;
  708. end;
  709. procedure Trgobj.decrement_degree(m:Tsuperregister);
  710. var adj : Psuperregisterworklist;
  711. n : tsuperregister;
  712. d,i : word;
  713. begin
  714. with reginfo[m] do
  715. begin
  716. d:=degree;
  717. if d=0 then
  718. internalerror(200312151);
  719. dec(degree);
  720. if d=usable_registers_cnt then
  721. begin
  722. {Enable moves for m.}
  723. enable_moves(m);
  724. {Enable moves for adjacent.}
  725. adj:=adjlist;
  726. if adj<>nil then
  727. for i:=1 to adj^.length do
  728. begin
  729. n:=adj^.buf^[i-1];
  730. if reginfo[n].flags*[ri_selected,ri_coalesced]<>[] then
  731. enable_moves(n);
  732. end;
  733. {Remove the node from the spillworklist.}
  734. if not spillworklist.delete(m) then
  735. internalerror(200310145);
  736. if move_related(m) then
  737. freezeworklist.add(m)
  738. else
  739. simplifyworklist.add(m);
  740. end;
  741. end;
  742. end;
  743. procedure trgobj.simplify;
  744. var adj : Psuperregisterworklist;
  745. m,n : Tsuperregister;
  746. i : word;
  747. begin
  748. {We take the element with the least interferences out of the
  749. simplifyworklist. Since the simplifyworklist is now sorted, we
  750. no longer need to search, but we can simply take the first element.}
  751. m:=simplifyworklist.get;
  752. {Push it on the selectstack.}
  753. selectstack.add(m);
  754. with reginfo[m] do
  755. begin
  756. include(flags,ri_selected);
  757. adj:=adjlist;
  758. end;
  759. if adj<>nil then
  760. for i:=1 to adj^.length do
  761. begin
  762. n:=adj^.buf^[i-1];
  763. if (n>=first_imaginary) and
  764. (reginfo[n].flags*[ri_selected,ri_coalesced]=[]) then
  765. decrement_degree(n);
  766. end;
  767. end;
  768. function trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  769. begin
  770. while ri_coalesced in reginfo[n].flags do
  771. n:=reginfo[n].alias;
  772. get_alias:=n;
  773. end;
  774. procedure trgobj.add_worklist(u:Tsuperregister);
  775. begin
  776. if (u>=first_imaginary) and
  777. (not move_related(u)) and
  778. (reginfo[u].degree<usable_registers_cnt) then
  779. begin
  780. if not freezeworklist.delete(u) then
  781. internalerror(200308161); {must be found}
  782. simplifyworklist.add(u);
  783. end;
  784. end;
  785. function trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  786. {Check wether u and v should be coalesced. u is precoloured.}
  787. function ok(t,r:Tsuperregister):boolean;
  788. begin
  789. ok:=(t<first_imaginary) or
  790. (reginfo[t].degree<usable_registers_cnt) or
  791. ibitmap[r,t];
  792. end;
  793. var adj : Psuperregisterworklist;
  794. i : word;
  795. n : tsuperregister;
  796. begin
  797. with reginfo[v] do
  798. begin
  799. adjacent_ok:=true;
  800. adj:=adjlist;
  801. if adj<>nil then
  802. for i:=1 to adj^.length do
  803. begin
  804. n:=adj^.buf^[i-1];
  805. if (flags*[ri_coalesced,ri_selected]=[]) and not ok(n,u) then
  806. begin
  807. adjacent_ok:=false;
  808. break;
  809. end;
  810. end;
  811. end;
  812. end;
  813. function trgobj.conservative(u,v:Tsuperregister):boolean;
  814. var adj : Psuperregisterworklist;
  815. done : Tsuperregisterset; {To prevent that we count nodes twice.}
  816. i,k:word;
  817. n : tsuperregister;
  818. begin
  819. k:=0;
  820. supregset_reset(done,false,maxreg);
  821. with reginfo[u] do
  822. begin
  823. adj:=adjlist;
  824. if adj<>nil then
  825. for i:=1 to adj^.length do
  826. begin
  827. n:=adj^.buf^[i-1];
  828. if flags*[ri_coalesced,ri_selected]=[] then
  829. begin
  830. supregset_include(done,n);
  831. if reginfo[n].degree>=usable_registers_cnt then
  832. inc(k);
  833. end;
  834. end;
  835. end;
  836. adj:=reginfo[v].adjlist;
  837. if adj<>nil then
  838. for i:=1 to adj^.length do
  839. begin
  840. n:=adj^.buf^[i-1];
  841. if not supregset_in(done,n) and
  842. (reginfo[n].degree>=usable_registers_cnt) and
  843. (reginfo[u].flags*[ri_coalesced,ri_selected]=[]) then
  844. inc(k);
  845. end;
  846. conservative:=(k<usable_registers_cnt);
  847. end;
  848. procedure trgobj.combine(u,v:Tsuperregister);
  849. var adj : Psuperregisterworklist;
  850. i,n,p,q:cardinal;
  851. t : tsuperregister;
  852. searched:Tlinkedlistitem;
  853. label l1;
  854. begin
  855. if not freezeworklist.delete(v) then
  856. spillworklist.delete(v);
  857. coalescednodes.add(v);
  858. include(reginfo[v].flags,ri_coalesced);
  859. reginfo[v].alias:=u;
  860. {Combine both movelists. Since the movelists are sets, only add
  861. elements that are not already present. The movelists cannot be
  862. empty by definition; nodes are only coalesced if there is a move
  863. between them. To prevent quadratic time blowup (movelists of
  864. especially machine registers can get very large because of moves
  865. generated during calls) we need to go into disgusting complexity.
  866. (See webtbs/tw2242 for an example that stresses this.)
  867. We want to sort the movelist to be able to search logarithmically.
  868. Unfortunately, sorting the movelist every time before searching
  869. is counter-productive, since the movelist usually grows with a few
  870. items at a time. Therefore, we split the movelist into a sorted
  871. and an unsorted part and search through both. If the unsorted part
  872. becomes too large, we sort.}
  873. if assigned(reginfo[u].movelist) then
  874. begin
  875. {We have to weigh the cost of sorting the list against searching
  876. the cost of the unsorted part. I use factor of 8 here; if the
  877. number of items is less than 8 times the numer of unsorted items,
  878. we'll sort the list.}
  879. with reginfo[u].movelist^ do
  880. if header.count<8*(header.count-header.sorted_until) then
  881. sort_movelist(reginfo[u].movelist);
  882. if assigned(reginfo[v].movelist) then
  883. begin
  884. for n:=0 to reginfo[v].movelist^.header.count-1 do
  885. begin
  886. {Binary search the sorted part of the list.}
  887. searched:=reginfo[v].movelist^.data[n];
  888. p:=0;
  889. q:=reginfo[u].movelist^.header.sorted_until;
  890. i:=0;
  891. if q<>0 then
  892. repeat
  893. i:=(p+q) shr 1;
  894. if ptrint(searched)>ptrint(reginfo[u].movelist^.data[i]) then
  895. p:=i+1
  896. else
  897. q:=i;
  898. until p=q;
  899. with reginfo[u].movelist^ do
  900. if searched<>data[i] then
  901. begin
  902. {Linear search the unsorted part of the list.}
  903. for i:=header.sorted_until+1 to header.count-1 do
  904. if searched=data[i] then
  905. goto l1;
  906. {Not found -> add}
  907. add_to_movelist(u,searched);
  908. l1:
  909. end;
  910. end;
  911. end;
  912. end;
  913. enable_moves(v);
  914. adj:=reginfo[v].adjlist;
  915. if adj<>nil then
  916. for i:=1 to adj^.length do
  917. begin
  918. t:=adj^.buf^[i-1];
  919. with reginfo[t] do
  920. if not(ri_coalesced in flags) then
  921. begin
  922. {t has a connection to v. Since we are adding v to u, we
  923. need to connect t to u. However, beware if t was already
  924. connected to u...}
  925. if (ibitmap[t,u]) and not (ri_selected in flags) then
  926. {... because in that case, we are actually removing an edge
  927. and the degree of t decreases.}
  928. decrement_degree(t)
  929. else
  930. begin
  931. add_edge(t,u);
  932. {We have added an edge to t and u. So their degree increases.
  933. However, v is added to u. That means its neighbours will
  934. no longer point to v, but to u instead. Therefore, only the
  935. degree of u increases.}
  936. if (u>=first_imaginary) and not (ri_selected in flags) then
  937. inc(reginfo[u].degree);
  938. end;
  939. end;
  940. end;
  941. if (reginfo[u].degree>=usable_registers_cnt) and freezeworklist.delete(u) then
  942. spillworklist.add(u);
  943. end;
  944. procedure trgobj.coalesce;
  945. var m:Tmoveins;
  946. x,y,u,v:Tsuperregister;
  947. begin
  948. m:=Tmoveins(worklist_moves.getfirst);
  949. x:=get_alias(m.x);
  950. y:=get_alias(m.y);
  951. if (y<first_imaginary) then
  952. begin
  953. u:=y;
  954. v:=x;
  955. end
  956. else
  957. begin
  958. u:=x;
  959. v:=y;
  960. end;
  961. if (u=v) then
  962. begin
  963. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  964. coalesced_moves.insert(m);
  965. add_worklist(u);
  966. end
  967. {Do u and v interfere? In that case the move is constrained. Two
  968. precoloured nodes interfere allways. If v is precoloured, by the above
  969. code u is precoloured, thus interference...}
  970. else if (v<first_imaginary) or ibitmap[u,v] then
  971. begin
  972. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  973. constrained_moves.insert(m);
  974. add_worklist(u);
  975. add_worklist(v);
  976. end
  977. {Next test: is it possible and a good idea to coalesce??}
  978. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  979. ((u>=first_imaginary) and conservative(u,v)) then
  980. begin
  981. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  982. coalesced_moves.insert(m);
  983. combine(u,v);
  984. add_worklist(u);
  985. end
  986. else
  987. begin
  988. m.moveset:=ms_active_moves;
  989. active_moves.insert(m);
  990. end;
  991. end;
  992. procedure trgobj.freeze_moves(u:Tsuperregister);
  993. var i:cardinal;
  994. m:Tlinkedlistitem;
  995. v,x,y:Tsuperregister;
  996. begin
  997. if reginfo[u].movelist<>nil then
  998. for i:=0 to reginfo[u].movelist^.header.count-1 do
  999. begin
  1000. m:=reginfo[u].movelist^.data[i];
  1001. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1002. begin
  1003. x:=Tmoveins(m).x;
  1004. y:=Tmoveins(m).y;
  1005. if get_alias(y)=get_alias(u) then
  1006. v:=get_alias(x)
  1007. else
  1008. v:=get_alias(y);
  1009. {Move m from active_moves/worklist_moves to frozen_moves.}
  1010. if Tmoveins(m).moveset=ms_active_moves then
  1011. active_moves.remove(m)
  1012. else
  1013. worklist_moves.remove(m);
  1014. Tmoveins(m).moveset:=ms_frozen_moves;
  1015. frozen_moves.insert(m);
  1016. if (v>=first_imaginary) and not(move_related(v)) and
  1017. (reginfo[v].degree<usable_registers_cnt) then
  1018. begin
  1019. freezeworklist.delete(v);
  1020. simplifyworklist.add(v);
  1021. end;
  1022. end;
  1023. end;
  1024. end;
  1025. procedure trgobj.freeze;
  1026. var n:Tsuperregister;
  1027. begin
  1028. { We need to take a random element out of the freezeworklist. We take
  1029. the last element. Dirty code! }
  1030. n:=freezeworklist.get;
  1031. {Add it to the simplifyworklist.}
  1032. simplifyworklist.add(n);
  1033. freeze_moves(n);
  1034. end;
  1035. procedure trgobj.select_spill;
  1036. var
  1037. n : tsuperregister;
  1038. adj : psuperregisterworklist;
  1039. max,p,i:word;
  1040. begin
  1041. { We must look for the element with the most interferences in the
  1042. spillworklist. This is required because those registers are creating
  1043. the most conflicts and keeping them in a register will not reduce the
  1044. complexity and even can cause the help registers for the spilling code
  1045. to get too much conflicts with the result that the spilling code
  1046. will never converge (PFV) }
  1047. max:=0;
  1048. p:=0;
  1049. with spillworklist do
  1050. begin
  1051. {Safe: This procedure is only called if length<>0}
  1052. for i:=0 to length-1 do
  1053. begin
  1054. adj:=reginfo[buf^[i]].adjlist;
  1055. if assigned(adj) and (adj^.length>max) then
  1056. begin
  1057. p:=i;
  1058. max:=adj^.length;
  1059. end;
  1060. end;
  1061. n:=buf^[p];
  1062. deleteidx(p);
  1063. end;
  1064. simplifyworklist.add(n);
  1065. freeze_moves(n);
  1066. end;
  1067. procedure trgobj.assign_colours;
  1068. {Assign_colours assigns the actual colours to the registers.}
  1069. var adj : Psuperregisterworklist;
  1070. i,j,k : word;
  1071. n,a,c : Tsuperregister;
  1072. colourednodes : Tsuperregisterset;
  1073. adj_colours:set of 0..255;
  1074. found : boolean;
  1075. begin
  1076. spillednodes.clear;
  1077. {Reset colours}
  1078. for n:=0 to maxreg-1 do
  1079. reginfo[n].colour:=n;
  1080. {Colour the cpu registers...}
  1081. supregset_reset(colourednodes,false,maxreg);
  1082. for n:=0 to first_imaginary-1 do
  1083. supregset_include(colourednodes,n);
  1084. {Now colour the imaginary registers on the select-stack.}
  1085. for i:=selectstack.length downto 1 do
  1086. begin
  1087. n:=selectstack.buf^[i-1];
  1088. {Create a list of colours that we cannot assign to n.}
  1089. adj_colours:=[];
  1090. adj:=reginfo[n].adjlist;
  1091. if adj<>nil then
  1092. for j:=0 to adj^.length-1 do
  1093. begin
  1094. a:=get_alias(adj^.buf^[j]);
  1095. if supregset_in(colourednodes,a) and (reginfo[a].colour<=255) then
  1096. include(adj_colours,reginfo[a].colour);
  1097. end;
  1098. include(adj_colours,RS_STACK_POINTER_REG);
  1099. {Assume a spill by default...}
  1100. found:=false;
  1101. {Search for a colour not in this list.}
  1102. for k:=0 to usable_registers_cnt-1 do
  1103. begin
  1104. c:=usable_registers[k];
  1105. if not(c in adj_colours) then
  1106. begin
  1107. reginfo[n].colour:=c;
  1108. found:=true;
  1109. supregset_include(colourednodes,n);
  1110. include(used_in_proc,c);
  1111. break;
  1112. end;
  1113. end;
  1114. if not found then
  1115. spillednodes.add(n);
  1116. end;
  1117. {Finally colour the nodes that were coalesced.}
  1118. for i:=1 to coalescednodes.length do
  1119. begin
  1120. n:=coalescednodes.buf^[i-1];
  1121. k:=get_alias(n);
  1122. reginfo[n].colour:=reginfo[k].colour;
  1123. if reginfo[k].colour<maxcpuregister then
  1124. include(used_in_proc,reginfo[k].colour);
  1125. end;
  1126. end;
  1127. procedure trgobj.colour_registers;
  1128. begin
  1129. repeat
  1130. if simplifyworklist.length<>0 then
  1131. simplify
  1132. else if not(worklist_moves.empty) then
  1133. coalesce
  1134. else if freezeworklist.length<>0 then
  1135. freeze
  1136. else if spillworklist.length<>0 then
  1137. select_spill;
  1138. until (simplifyworklist.length=0) and
  1139. worklist_moves.empty and
  1140. (freezeworklist.length=0) and
  1141. (spillworklist.length=0);
  1142. assign_colours;
  1143. end;
  1144. procedure trgobj.epilogue_colouring;
  1145. var
  1146. i : Tsuperregister;
  1147. begin
  1148. worklist_moves.clear;
  1149. active_moves.destroy;
  1150. active_moves:=nil;
  1151. frozen_moves.destroy;
  1152. frozen_moves:=nil;
  1153. coalesced_moves.destroy;
  1154. coalesced_moves:=nil;
  1155. constrained_moves.destroy;
  1156. constrained_moves:=nil;
  1157. for i:=0 to maxreg-1 do
  1158. with reginfo[i] do
  1159. if movelist<>nil then
  1160. begin
  1161. dispose(movelist);
  1162. movelist:=nil;
  1163. end;
  1164. end;
  1165. procedure trgobj.clear_interferences(u:Tsuperregister);
  1166. {Remove node u from the interference graph and remove all collected
  1167. move instructions it is associated with.}
  1168. var i : word;
  1169. v : Tsuperregister;
  1170. adj,adj2 : Psuperregisterworklist;
  1171. begin
  1172. adj:=reginfo[u].adjlist;
  1173. if adj<>nil then
  1174. begin
  1175. for i:=1 to adj^.length do
  1176. begin
  1177. v:=adj^.buf^[i-1];
  1178. {Remove (u,v) and (v,u) from bitmap.}
  1179. ibitmap[u,v]:=false;
  1180. ibitmap[v,u]:=false;
  1181. {Remove (v,u) from adjacency list.}
  1182. adj2:=reginfo[v].adjlist;
  1183. if adj2<>nil then
  1184. begin
  1185. adj2^.delete(u);
  1186. if adj2^.length=0 then
  1187. begin
  1188. dispose(adj2,done);
  1189. reginfo[v].adjlist:=nil;
  1190. end;
  1191. end;
  1192. end;
  1193. {Remove ( u,* ) from adjacency list.}
  1194. dispose(adj,done);
  1195. reginfo[u].adjlist:=nil;
  1196. end;
  1197. end;
  1198. procedure trgobj.getregisterinline(list:Taasmoutput;
  1199. position:Tai;subreg:Tsubregister;var result:Tregister);
  1200. var p:Tsuperregister;
  1201. r:Tregister;
  1202. begin
  1203. p:=getnewreg(subreg);
  1204. live_registers.add(p);
  1205. r:=newreg(regtype,p,subreg);
  1206. if position=nil then
  1207. list.insert(Tai_regalloc.alloc(r))
  1208. else
  1209. list.insertafter(Tai_regalloc.alloc(r),position);
  1210. add_edges_used(p);
  1211. add_constraints(r);
  1212. result:=r;
  1213. end;
  1214. procedure trgobj.ungetregisterinline(list:Taasmoutput;
  1215. position:Tai;r:Tregister);
  1216. var supreg:Tsuperregister;
  1217. begin
  1218. supreg:=getsupreg(r);
  1219. live_registers.delete(supreg);
  1220. if position=nil then
  1221. list.insert(Tai_regalloc.dealloc(r))
  1222. else
  1223. list.insertafter(Tai_regalloc.dealloc(r),position);
  1224. end;
  1225. procedure trgobj.insert_regalloc_info(list:Taasmoutput;headertai:tai);
  1226. var
  1227. supreg : tsuperregister;
  1228. p : tai;
  1229. r : tregister;
  1230. begin
  1231. { Insert regallocs for all imaginary registers }
  1232. for supreg:=first_imaginary to maxreg-1 do
  1233. with reginfo[supreg] do
  1234. begin
  1235. r:=newreg(regtype,supreg,subreg);
  1236. if assigned(live_start) then
  1237. begin
  1238. {$ifdef EXTDEBUG}
  1239. if live_start=live_end then
  1240. Comment(V_Warning,'Register '+std_regname(r)+' is only used once');
  1241. {$endif EXTDEBUG}
  1242. list.insertbefore(Tai_regalloc.alloc(r),live_start);
  1243. { Insert live end deallocation before reg allocations
  1244. to reduce conflicts }
  1245. p:=live_end;
  1246. while assigned(p) and
  1247. assigned(p.previous) and
  1248. (tai(p.previous).typ=ait_regalloc) and
  1249. (tai_regalloc(p.previous).ratype=ra_alloc) and
  1250. (tai_regalloc(p.previous).reg<>r) do
  1251. p:=tai(p.previous);
  1252. { , but add release after sync }
  1253. if assigned(p) and
  1254. (p.typ=ait_regalloc) and
  1255. (tai_regalloc(p).ratype=ra_sync) then
  1256. p:=tai(p.next);
  1257. if assigned(p) then
  1258. list.insertbefore(Tai_regalloc.dealloc(r),p)
  1259. else
  1260. list.concat(Tai_regalloc.dealloc(r));
  1261. end
  1262. {$ifdef EXTDEBUG}
  1263. else
  1264. Comment(V_Warning,'Register '+std_regname(r)+' not used');
  1265. {$endif EXTDEBUG}
  1266. end;
  1267. end;
  1268. procedure trgobj.add_cpu_interferences(p : tai);
  1269. begin
  1270. end;
  1271. procedure trgobj.generate_interference_graph(list:Taasmoutput;headertai:tai);
  1272. var
  1273. p : tai;
  1274. i : integer;
  1275. supreg : tsuperregister;
  1276. begin
  1277. { All allocations are available. Now we can generate the
  1278. interference graph. Walk through all instructions, we can
  1279. start with the headertai, because before the header tai is
  1280. only symbols. }
  1281. live_registers.clear;
  1282. p:=headertai;
  1283. while assigned(p) do
  1284. begin
  1285. if p.typ=ait_regalloc then
  1286. with Tai_regalloc(p) do
  1287. begin
  1288. if (getregtype(reg)=regtype) then
  1289. begin
  1290. supreg:=getsupreg(reg);
  1291. case ratype of
  1292. ra_alloc :
  1293. begin
  1294. live_registers.add(supreg);
  1295. add_edges_used(supreg);
  1296. end;
  1297. ra_dealloc :
  1298. begin
  1299. live_registers.delete(supreg);
  1300. add_edges_used(supreg);
  1301. end;
  1302. end;
  1303. { constraints needs always to be updated }
  1304. add_constraints(reg);
  1305. end;
  1306. end;
  1307. add_cpu_interferences(p);
  1308. p:=Tai(p.next);
  1309. end;
  1310. {$ifdef EXTDEBUG}
  1311. if live_registers.length>0 then
  1312. begin
  1313. for i:=0 to live_registers.length-1 do
  1314. begin
  1315. { Only report for imaginary registers }
  1316. if live_registers.buf^[i]>=first_imaginary then
  1317. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,live_registers.buf^[i],defaultsub))+' not released');
  1318. end;
  1319. end;
  1320. {$endif}
  1321. end;
  1322. procedure Trgobj.translate_registers(list:taasmoutput);
  1323. var
  1324. hp,p,q:Tai;
  1325. i:shortint;
  1326. {$ifdef arm}
  1327. so:pshifterop;
  1328. {$endif arm}
  1329. begin
  1330. { Leave when no imaginary registers are used }
  1331. if maxreg<=first_imaginary then
  1332. exit;
  1333. p:=Tai(list.first);
  1334. while assigned(p) do
  1335. begin
  1336. case p.typ of
  1337. ait_regalloc:
  1338. with Tai_regalloc(p) do
  1339. begin
  1340. { Only alloc/dealloc is needed for the optimizer, remove
  1341. other regalloc }
  1342. if not(ratype in [ra_alloc,ra_dealloc]) then
  1343. begin
  1344. q:=Tai(next);
  1345. list.remove(p);
  1346. p.free;
  1347. p:=q;
  1348. continue;
  1349. end
  1350. else
  1351. begin
  1352. if (getregtype(reg)=regtype) then
  1353. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1354. {
  1355. Remove sequences of release and
  1356. allocation of the same register like. Other combinations
  1357. of release/allocate need to stay in the list.
  1358. # Register X released
  1359. # Register X allocated
  1360. }
  1361. if assigned(previous) and
  1362. (ratype=ra_alloc) and
  1363. (Tai(previous).typ=ait_regalloc) and
  1364. (Tai_regalloc(previous).reg=reg) and
  1365. (Tai_regalloc(previous).ratype=ra_dealloc) then
  1366. begin
  1367. q:=Tai(next);
  1368. hp:=tai(previous);
  1369. list.remove(hp);
  1370. hp.free;
  1371. list.remove(p);
  1372. p.free;
  1373. p:=q;
  1374. continue;
  1375. end;
  1376. end;
  1377. end;
  1378. ait_instruction:
  1379. with Taicpu(p) do
  1380. begin
  1381. for i:=0 to ops-1 do
  1382. with oper[i]^ do
  1383. case typ of
  1384. Top_reg:
  1385. if (getregtype(reg)=regtype) then
  1386. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1387. Top_ref:
  1388. begin
  1389. if regtype=R_INTREGISTER then
  1390. with ref^ do
  1391. begin
  1392. if base<>NR_NO then
  1393. setsupreg(base,reginfo[getsupreg(base)].colour);
  1394. if index<>NR_NO then
  1395. setsupreg(index,reginfo[getsupreg(index)].colour);
  1396. end;
  1397. end;
  1398. {$ifdef arm}
  1399. Top_shifterop:
  1400. begin
  1401. so:=shifterop;
  1402. if so^.rs<>NR_NO then
  1403. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1404. end;
  1405. {$endif arm}
  1406. end;
  1407. { Maybe the operation can be removed when
  1408. it is a move and both arguments are the same }
  1409. if is_same_reg_move(regtype) then
  1410. begin
  1411. q:=Tai(p.next);
  1412. list.remove(p);
  1413. p.free;
  1414. p:=q;
  1415. continue;
  1416. end;
  1417. end;
  1418. end;
  1419. p:=Tai(p.next);
  1420. end;
  1421. end;
  1422. function trgobj.spill_registers(list:Taasmoutput;headertai:tai):boolean;
  1423. { Returns true if any help registers have been used }
  1424. var
  1425. i : word;
  1426. t : tsuperregister;
  1427. p,q : Tai;
  1428. regs_to_spill_set:Tsuperregisterset;
  1429. spill_temps : ^Tspill_temp_list;
  1430. supreg : tsuperregister;
  1431. templist : taasmoutput;
  1432. begin
  1433. spill_registers:=false;
  1434. live_registers.clear;
  1435. for i:=first_imaginary to maxreg-1 do
  1436. exclude(reginfo[i].flags,ri_selected);
  1437. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1438. supregset_reset(regs_to_spill_set,false,$ffff);
  1439. { Allocate temps and insert in front of the list }
  1440. templist:=taasmoutput.create;
  1441. {Safe: this procedure is only called if there are spilled nodes.}
  1442. with spillednodes do
  1443. for i:=0 to length-1 do
  1444. begin
  1445. t:=buf^[i];
  1446. {Alternative representation.}
  1447. supregset_include(regs_to_spill_set,t);
  1448. {Clear all interferences of the spilled register.}
  1449. clear_interferences(t);
  1450. {Get a temp for the spilled register, the size must at least equal a complete register,
  1451. take also care of the fact that subreg can be larger than a single register like doubles
  1452. that occupy 2 registers }
  1453. tg.gettemp(templist,
  1454. max(tcgsize2size[reg_cgsize(newreg(regtype,t,R_SUBWHOLE))],
  1455. tcgsize2size[reg_cgsize(newreg(regtype,t,reginfo[t].subreg))]),
  1456. tt_noreuse,spill_temps^[t]);
  1457. end;
  1458. list.insertlistafter(headertai,templist);
  1459. templist.free;
  1460. { Walk through all instructions, we can start with the headertai,
  1461. because before the header tai is only symbols }
  1462. p:=headertai;
  1463. while assigned(p) do
  1464. begin
  1465. case p.typ of
  1466. ait_regalloc:
  1467. with Tai_regalloc(p) do
  1468. begin
  1469. if (getregtype(reg)=regtype) then
  1470. begin
  1471. {A register allocation of a spilled register can be removed.}
  1472. supreg:=getsupreg(reg);
  1473. if supregset_in(regs_to_spill_set,supreg) then
  1474. begin
  1475. q:=Tai(p.next);
  1476. list.remove(p);
  1477. p.free;
  1478. p:=q;
  1479. continue;
  1480. end
  1481. else
  1482. begin
  1483. case ratype of
  1484. ra_alloc :
  1485. live_registers.add(supreg);
  1486. ra_dealloc :
  1487. live_registers.delete(supreg);
  1488. end;
  1489. end;
  1490. end;
  1491. end;
  1492. ait_instruction:
  1493. with Taicpu(p) do
  1494. begin
  1495. aktfilepos:=fileinfo;
  1496. if instr_spill_register(list,taicpu(p),regs_to_spill_set,spill_temps^) then
  1497. spill_registers:=true;
  1498. end;
  1499. end;
  1500. p:=Tai(p.next);
  1501. end;
  1502. aktfilepos:=current_procinfo.exitpos;
  1503. {Safe: this procedure is only called if there are spilled nodes.}
  1504. with spillednodes do
  1505. for i:=0 to length-1 do
  1506. tg.ungettemp(list,spill_temps^[buf^[i]]);
  1507. freemem(spill_temps);
  1508. end;
  1509. procedure Trgobj.do_spill_read(list:Taasmoutput;instr:taicpu;const spilltemp:treference;const tempreg:tregister);
  1510. var
  1511. helpins : Tai;
  1512. begin
  1513. helpins:=spilling_create_load(spilltemp,tempreg);
  1514. list.insertbefore(helpins,instr);
  1515. end;
  1516. procedure Trgobj.do_spill_written(list:Taasmoutput;instr:taicpu;const spilltemp:treference;const tempreg:tregister);
  1517. var
  1518. helpins : Tai;
  1519. begin
  1520. helpins:=spilling_create_store(tempreg,spilltemp);
  1521. list.insertafter(helpins,instr);
  1522. end;
  1523. function trgobj.get_spill_subreg(r : tregister) : tsubregister;
  1524. begin
  1525. result:=defaultsub;
  1526. end;
  1527. function trgobj.instr_spill_register(list:Taasmoutput;
  1528. instr:taicpu;
  1529. const r:Tsuperregisterset;
  1530. const spilltemplist:Tspill_temp_list): boolean;
  1531. var
  1532. counter, regindex: longint;
  1533. regs: tspillregsinfo;
  1534. spilled: boolean;
  1535. procedure addreginfo(reg: tregister; operation: topertype);
  1536. var
  1537. i, tmpindex: longint;
  1538. supreg : tsuperregister;
  1539. begin
  1540. tmpindex := regindex;
  1541. supreg:=getsupreg(reg);
  1542. { did we already encounter this register? }
  1543. for i := 0 to pred(regindex) do
  1544. if (regs[i].orgreg = supreg) then
  1545. begin
  1546. tmpindex := i;
  1547. break;
  1548. end;
  1549. if tmpindex > high(regs) then
  1550. internalerror(2003120301);
  1551. regs[tmpindex].orgreg := supreg;
  1552. regs[tmpindex].spillreg:=reg;
  1553. if supregset_in(r,supreg) then
  1554. begin
  1555. { add/update info on this register }
  1556. regs[tmpindex].mustbespilled := true;
  1557. case operation of
  1558. operand_read:
  1559. regs[tmpindex].regread := true;
  1560. operand_write:
  1561. regs[tmpindex].regwritten := true;
  1562. operand_readwrite:
  1563. begin
  1564. regs[tmpindex].regread := true;
  1565. regs[tmpindex].regwritten := true;
  1566. end;
  1567. end;
  1568. spilled := true;
  1569. end;
  1570. inc(regindex,ord(regindex=tmpindex));
  1571. end;
  1572. procedure tryreplacereg(var reg: tregister);
  1573. var
  1574. i: longint;
  1575. supreg: tsuperregister;
  1576. begin
  1577. supreg:=getsupreg(reg);
  1578. for i:=0 to pred(regindex) do
  1579. if (regs[i].mustbespilled) and
  1580. (regs[i].orgreg=supreg) then
  1581. begin
  1582. { Only replace supreg }
  1583. setsupreg(reg,getsupreg(regs[i].tempreg));
  1584. break;
  1585. end;
  1586. end;
  1587. var
  1588. counter2 : longint;
  1589. oldlive_registers : tsuperregisterworklist;
  1590. begin
  1591. result := false;
  1592. fillchar(regs,sizeof(regs),0);
  1593. for counter := low(regs) to high(regs) do
  1594. regs[counter].orgreg := RS_INVALID;
  1595. spilled := false;
  1596. regindex := 0;
  1597. { check whether and if so which and how (read/written) this instructions contains
  1598. registers that must be spilled }
  1599. for counter := 0 to instr.ops-1 do
  1600. with instr.oper[counter]^ do
  1601. begin
  1602. case typ of
  1603. top_reg:
  1604. begin
  1605. if (getregtype(reg) = regtype) then
  1606. addreginfo(reg,instr.spilling_get_operation_type(counter));
  1607. end;
  1608. top_ref:
  1609. begin
  1610. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1611. with ref^ do
  1612. begin
  1613. if (base <> NR_NO) then
  1614. addreginfo(base,operand_read);
  1615. if (index <> NR_NO) then
  1616. addreginfo(index,operand_read);
  1617. end;
  1618. end;
  1619. {$ifdef ARM}
  1620. top_shifterop:
  1621. begin
  1622. if shifterop^.rs<>NR_NO then
  1623. addreginfo(shifterop^.rs,operand_read);
  1624. end;
  1625. {$endif ARM}
  1626. end;
  1627. end;
  1628. { if no spilling for this instruction we can leave }
  1629. if not spilled then
  1630. exit;
  1631. { Add conflicts with all non-spilled registers }
  1632. oldlive_registers.copyfrom(live_registers);
  1633. for counter2 := 0 to pred(regindex) do
  1634. begin
  1635. if (not regs[counter2].mustbespilled) then
  1636. live_registers.add(get_alias(regs[counter2].orgreg));
  1637. end;
  1638. { generate the spilling code }
  1639. result := true;
  1640. for counter := 0 to pred(regindex) do
  1641. with regs[counter] do
  1642. begin
  1643. if mustbespilled then
  1644. begin
  1645. getregisterinline(list,tai(instr.previous),get_spill_subreg(regs[counter].spillreg),tempreg);
  1646. if regread then
  1647. do_spill_read(list,instr,spilltemplist[orgreg],tempreg);
  1648. if regwritten then
  1649. do_spill_written(list,instr,spilltemplist[orgreg],tempreg);
  1650. end;
  1651. end;
  1652. { Release temp registers after all registers for the instruction are spilled }
  1653. for counter := 0 to pred(regindex) do
  1654. with regs[counter] do
  1655. begin
  1656. if mustbespilled then
  1657. ungetregisterinline(list,instr,tempreg);
  1658. end;
  1659. { restore live registers }
  1660. live_registers.done;
  1661. live_registers:=oldlive_registers;
  1662. { substitute registers }
  1663. for counter:=0 to instr.ops-1 do
  1664. with instr.oper[counter]^ do
  1665. begin
  1666. case typ of
  1667. top_reg:
  1668. begin
  1669. tryreplacereg(reg);
  1670. end;
  1671. top_ref:
  1672. begin
  1673. tryreplacereg(ref^.base);
  1674. tryreplacereg(ref^.index);
  1675. end;
  1676. {$ifdef ARM}
  1677. top_shifterop:
  1678. begin
  1679. tryreplacereg(shifterop^.rs);
  1680. end;
  1681. {$endif ARM}
  1682. end;
  1683. end;
  1684. end;
  1685. end.
  1686. {
  1687. $Log$
  1688. Revision 1.138 2004-10-04 20:46:22 peter
  1689. * spilling code rewritten for x86. It now used the generic
  1690. spilling routines. Special x86 optimization still needs
  1691. to be added.
  1692. * Spilling fixed when both operands needed to be spilled
  1693. * Cleanup of spilling routine, do_spill_readwritten removed
  1694. Revision 1.137 2004/09/26 17:45:30 peter
  1695. * simple regvar support, not yet finished
  1696. Revision 1.136 2004/09/25 14:23:54 peter
  1697. * ungetregister is now only used for cpuregisters, renamed to
  1698. ungetcpuregister
  1699. * renamed (get|unget)explicitregister(s) to ..cpuregister
  1700. * removed location-release/reference_release
  1701. Revision 1.135 2004/09/21 17:25:12 peter
  1702. * paraloc branch merged
  1703. Revision 1.134.4.2 2004/09/21 17:03:26 peter
  1704. * Include aliases of coalesce registers when adding conflicts
  1705. Revision 1.134.4.1 2004/09/12 13:36:40 peter
  1706. * fixed alignment issues
  1707. Revision 1.134 2004/08/24 21:02:32 florian
  1708. * fixed longbool(<int64>) on sparc
  1709. Revision 1.133 2004/07/09 21:38:30 daniel
  1710. * Add check <= 255 when adding to adj_colours
  1711. Revision 1.132 2004/07/08 09:57:55 daniel
  1712. * Use a normal pascal set in assign_colours, since it only will contain
  1713. real registers
  1714. Revision 1.131 2004/07/07 17:35:26 daniel
  1715. * supregset_reset clears 8kb of memory. However, it is being called in
  1716. inner loops, see for example colour_registers. According to profile data
  1717. this causes fillchar to be the most time consuming procedure.
  1718. Some modifications done to make it clear less than 8kb of memory each
  1719. call. Divides time spent in fillchar by two, but it still is the no.1
  1720. procedure.
  1721. Revision 1.130 2004/06/22 18:24:18 florian
  1722. * fixed arm compilation
  1723. Revision 1.129 2004/06/20 08:55:30 florian
  1724. * logs truncated
  1725. Revision 1.128 2004/06/20 08:47:33 florian
  1726. * spilling of doubles on sparc fixed
  1727. Revision 1.127 2004/06/16 20:07:09 florian
  1728. * dwarf branch merged
  1729. Revision 1.126 2004/05/22 23:34:28 peter
  1730. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  1731. Revision 1.125 2004/04/26 19:57:50 jonas
  1732. * do not remove "allocation,deallocation" pairs, as those are important
  1733. for the optimizer
  1734. Revision 1.124.2.3 2004/06/13 10:51:16 florian
  1735. * fixed several register allocator problems (sparc/arm)
  1736. }