rgobj.pas 70 KB

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