rgobj.pas 69 KB

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