rgobj.pas 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109
  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*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 : 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,ptruint(@movelist^.data)-ptruint(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,ptruint(@movelist^.data)-ptruint(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. found : boolean;
  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. found:=false;
  966. for i:=header.sorted_until+1 to header.count-1 do
  967. if searched=data[i] then
  968. begin
  969. found:=true;
  970. break;
  971. end;
  972. if not found then
  973. add_to_movelist(u,searched);
  974. end;
  975. end;
  976. end;
  977. end;
  978. enable_moves(v);
  979. adj:=reginfo[v].adjlist;
  980. if adj<>nil then
  981. for i:=1 to adj^.length do
  982. begin
  983. t:=adj^.buf^[i-1];
  984. with reginfo[t] do
  985. if not(ri_coalesced in flags) then
  986. begin
  987. {t has a connection to v. Since we are adding v to u, we
  988. need to connect t to u. However, beware if t was already
  989. connected to u...}
  990. if (ibitmap[t,u]) and not (ri_selected in flags) then
  991. {... because in that case, we are actually removing an edge
  992. and the degree of t decreases.}
  993. decrement_degree(t)
  994. else
  995. begin
  996. add_edge(t,u);
  997. {We have added an edge to t and u. So their degree increases.
  998. However, v is added to u. That means its neighbours will
  999. no longer point to v, but to u instead. Therefore, only the
  1000. degree of u increases.}
  1001. if (u>=first_imaginary) and not (ri_selected in flags) then
  1002. inc(reginfo[u].degree);
  1003. end;
  1004. end;
  1005. end;
  1006. if (reginfo[u].degree>=usable_registers_cnt) and freezeworklist.delete(u) then
  1007. spillworklist.add(u);
  1008. end;
  1009. procedure trgobj.coalesce;
  1010. var m:Tmoveins;
  1011. x,y,u,v:Tsuperregister;
  1012. begin
  1013. m:=Tmoveins(worklist_moves.getfirst);
  1014. x:=get_alias(m.x);
  1015. y:=get_alias(m.y);
  1016. if (y<first_imaginary) then
  1017. begin
  1018. u:=y;
  1019. v:=x;
  1020. end
  1021. else
  1022. begin
  1023. u:=x;
  1024. v:=y;
  1025. end;
  1026. if (u=v) then
  1027. begin
  1028. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1029. coalesced_moves.insert(m);
  1030. add_worklist(u);
  1031. end
  1032. {Do u and v interfere? In that case the move is constrained. Two
  1033. precoloured nodes interfere allways. If v is precoloured, by the above
  1034. code u is precoloured, thus interference...}
  1035. else if (v<first_imaginary) or ibitmap[u,v] then
  1036. begin
  1037. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1038. constrained_moves.insert(m);
  1039. add_worklist(u);
  1040. add_worklist(v);
  1041. end
  1042. {Next test: is it possible and a good idea to coalesce??}
  1043. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  1044. ((u>=first_imaginary) and conservative(u,v)) then
  1045. begin
  1046. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1047. coalesced_moves.insert(m);
  1048. combine(u,v);
  1049. add_worklist(u);
  1050. end
  1051. else
  1052. begin
  1053. m.moveset:=ms_active_moves;
  1054. active_moves.insert(m);
  1055. end;
  1056. end;
  1057. procedure trgobj.freeze_moves(u:Tsuperregister);
  1058. var i:cardinal;
  1059. m:Tlinkedlistitem;
  1060. v,x,y:Tsuperregister;
  1061. begin
  1062. if reginfo[u].movelist<>nil then
  1063. for i:=0 to reginfo[u].movelist^.header.count-1 do
  1064. begin
  1065. m:=reginfo[u].movelist^.data[i];
  1066. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1067. begin
  1068. x:=Tmoveins(m).x;
  1069. y:=Tmoveins(m).y;
  1070. if get_alias(y)=get_alias(u) then
  1071. v:=get_alias(x)
  1072. else
  1073. v:=get_alias(y);
  1074. {Move m from active_moves/worklist_moves to frozen_moves.}
  1075. if Tmoveins(m).moveset=ms_active_moves then
  1076. active_moves.remove(m)
  1077. else
  1078. worklist_moves.remove(m);
  1079. Tmoveins(m).moveset:=ms_frozen_moves;
  1080. frozen_moves.insert(m);
  1081. if (v>=first_imaginary) and not(move_related(v)) and
  1082. (reginfo[v].degree<usable_registers_cnt) then
  1083. begin
  1084. freezeworklist.delete(v);
  1085. simplifyworklist.add(v);
  1086. end;
  1087. end;
  1088. end;
  1089. end;
  1090. procedure trgobj.freeze;
  1091. var n:Tsuperregister;
  1092. begin
  1093. { We need to take a random element out of the freezeworklist. We take
  1094. the last element. Dirty code! }
  1095. n:=freezeworklist.get;
  1096. {Add it to the simplifyworklist.}
  1097. simplifyworklist.add(n);
  1098. freeze_moves(n);
  1099. end;
  1100. procedure trgobj.select_spill;
  1101. var
  1102. n : tsuperregister;
  1103. adj : psuperregisterworklist;
  1104. max,p,i:word;
  1105. minweight: longint;
  1106. begin
  1107. { We must look for the element with the most interferences in the
  1108. spillworklist. This is required because those registers are creating
  1109. the most conflicts and keeping them in a register will not reduce the
  1110. complexity and even can cause the help registers for the spilling code
  1111. to get too much conflicts with the result that the spilling code
  1112. will never converge (PFV) }
  1113. max:=0;
  1114. minweight:=high(longint);
  1115. p:=0;
  1116. with spillworklist do
  1117. begin
  1118. {Safe: This procedure is only called if length<>0}
  1119. for i:=0 to length-1 do
  1120. begin
  1121. adj:=reginfo[buf^[i]].adjlist;
  1122. if assigned(adj) and
  1123. (
  1124. (adj^.length>max) or
  1125. ((adj^.length=max) and (reginfo[buf^[i]].weight<minweight))
  1126. ) then
  1127. begin
  1128. p:=i;
  1129. max:=adj^.length;
  1130. minweight:=reginfo[buf^[i]].weight;
  1131. end;
  1132. end;
  1133. n:=buf^[p];
  1134. deleteidx(p);
  1135. end;
  1136. simplifyworklist.add(n);
  1137. freeze_moves(n);
  1138. end;
  1139. procedure trgobj.assign_colours;
  1140. {Assign_colours assigns the actual colours to the registers.}
  1141. var adj : Psuperregisterworklist;
  1142. i,j,k : word;
  1143. n,a,c : Tsuperregister;
  1144. colourednodes : Tsuperregisterset;
  1145. adj_colours:set of 0..255;
  1146. found : boolean;
  1147. begin
  1148. spillednodes.clear;
  1149. {Reset colours}
  1150. for n:=0 to maxreg-1 do
  1151. reginfo[n].colour:=n;
  1152. {Colour the cpu registers...}
  1153. supregset_reset(colourednodes,false,maxreg);
  1154. for n:=0 to first_imaginary-1 do
  1155. supregset_include(colourednodes,n);
  1156. {Now colour the imaginary registers on the select-stack.}
  1157. for i:=selectstack.length downto 1 do
  1158. begin
  1159. n:=selectstack.buf^[i-1];
  1160. {Create a list of colours that we cannot assign to n.}
  1161. adj_colours:=[];
  1162. adj:=reginfo[n].adjlist;
  1163. if adj<>nil then
  1164. for j:=0 to adj^.length-1 do
  1165. begin
  1166. a:=get_alias(adj^.buf^[j]);
  1167. if supregset_in(colourednodes,a) and (reginfo[a].colour<=255) then
  1168. include(adj_colours,reginfo[a].colour);
  1169. end;
  1170. if regtype=R_INTREGISTER then
  1171. include(adj_colours,RS_STACK_POINTER_REG);
  1172. {Assume a spill by default...}
  1173. found:=false;
  1174. {Search for a colour not in this list.}
  1175. for k:=0 to usable_registers_cnt-1 do
  1176. begin
  1177. c:=usable_registers[k];
  1178. if not(c in adj_colours) then
  1179. begin
  1180. reginfo[n].colour:=c;
  1181. found:=true;
  1182. supregset_include(colourednodes,n);
  1183. include(used_in_proc,c);
  1184. break;
  1185. end;
  1186. end;
  1187. if not found then
  1188. spillednodes.add(n);
  1189. end;
  1190. {Finally colour the nodes that were coalesced.}
  1191. for i:=1 to coalescednodes.length do
  1192. begin
  1193. n:=coalescednodes.buf^[i-1];
  1194. k:=get_alias(n);
  1195. reginfo[n].colour:=reginfo[k].colour;
  1196. if reginfo[k].colour<maxcpuregister then
  1197. include(used_in_proc,reginfo[k].colour);
  1198. end;
  1199. end;
  1200. procedure trgobj.colour_registers;
  1201. begin
  1202. repeat
  1203. if simplifyworklist.length<>0 then
  1204. simplify
  1205. else if not(worklist_moves.empty) then
  1206. coalesce
  1207. else if freezeworklist.length<>0 then
  1208. freeze
  1209. else if spillworklist.length<>0 then
  1210. select_spill;
  1211. until (simplifyworklist.length=0) and
  1212. worklist_moves.empty and
  1213. (freezeworklist.length=0) and
  1214. (spillworklist.length=0);
  1215. assign_colours;
  1216. end;
  1217. procedure trgobj.epilogue_colouring;
  1218. var
  1219. i : Tsuperregister;
  1220. begin
  1221. worklist_moves.clear;
  1222. active_moves.destroy;
  1223. active_moves:=nil;
  1224. frozen_moves.destroy;
  1225. frozen_moves:=nil;
  1226. coalesced_moves.destroy;
  1227. coalesced_moves:=nil;
  1228. constrained_moves.destroy;
  1229. constrained_moves:=nil;
  1230. for i:=0 to maxreg-1 do
  1231. with reginfo[i] do
  1232. if movelist<>nil then
  1233. begin
  1234. dispose(movelist);
  1235. movelist:=nil;
  1236. end;
  1237. end;
  1238. procedure trgobj.clear_interferences(u:Tsuperregister);
  1239. {Remove node u from the interference graph and remove all collected
  1240. move instructions it is associated with.}
  1241. var i : word;
  1242. v : Tsuperregister;
  1243. adj,adj2 : Psuperregisterworklist;
  1244. begin
  1245. adj:=reginfo[u].adjlist;
  1246. if adj<>nil then
  1247. begin
  1248. for i:=1 to adj^.length do
  1249. begin
  1250. v:=adj^.buf^[i-1];
  1251. {Remove (u,v) and (v,u) from bitmap.}
  1252. ibitmap[u,v]:=false;
  1253. ibitmap[v,u]:=false;
  1254. {Remove (v,u) from adjacency list.}
  1255. adj2:=reginfo[v].adjlist;
  1256. if adj2<>nil then
  1257. begin
  1258. adj2^.delete(u);
  1259. if adj2^.length=0 then
  1260. begin
  1261. dispose(adj2,done);
  1262. reginfo[v].adjlist:=nil;
  1263. end;
  1264. end;
  1265. end;
  1266. {Remove ( u,* ) from adjacency list.}
  1267. dispose(adj,done);
  1268. reginfo[u].adjlist:=nil;
  1269. end;
  1270. end;
  1271. function trgobj.getregisterinline(list:TAsmList;subreg:Tsubregister):Tregister;
  1272. var
  1273. p : Tsuperregister;
  1274. begin
  1275. p:=getnewreg(subreg);
  1276. live_registers.add(p);
  1277. result:=newreg(regtype,p,subreg);
  1278. add_edges_used(p);
  1279. add_constraints(result);
  1280. end;
  1281. procedure trgobj.ungetregisterinline(list:TAsmList;r:Tregister);
  1282. var
  1283. supreg:Tsuperregister;
  1284. begin
  1285. supreg:=getsupreg(r);
  1286. live_registers.delete(supreg);
  1287. insert_regalloc_info(list,supreg);
  1288. end;
  1289. procedure trgobj.insert_regalloc_info(list:TAsmList;u:tsuperregister);
  1290. var
  1291. p : tai;
  1292. r : tregister;
  1293. palloc,
  1294. pdealloc : tai_regalloc;
  1295. begin
  1296. { Insert regallocs for all imaginary registers }
  1297. with reginfo[u] do
  1298. begin
  1299. r:=newreg(regtype,u,subreg);
  1300. if assigned(live_start) then
  1301. begin
  1302. { Generate regalloc and bind it to an instruction, this
  1303. is needed to find all live registers belonging to an
  1304. instruction during the spilling }
  1305. if live_start.typ=ait_instruction then
  1306. palloc:=tai_regalloc.alloc(r,live_start)
  1307. else
  1308. palloc:=tai_regalloc.alloc(r,nil);
  1309. if live_end.typ=ait_instruction then
  1310. pdealloc:=tai_regalloc.dealloc(r,live_end)
  1311. else
  1312. pdealloc:=tai_regalloc.dealloc(r,nil);
  1313. { Insert live start allocation before the instruction/reg_a_sync }
  1314. list.insertbefore(palloc,live_start);
  1315. { Insert live end deallocation before reg allocations
  1316. to reduce conflicts }
  1317. p:=live_end;
  1318. while assigned(p) and
  1319. assigned(p.previous) and
  1320. (tai(p.previous).typ=ait_regalloc) and
  1321. (tai_regalloc(p.previous).ratype=ra_alloc) and
  1322. (tai_regalloc(p.previous).reg<>r) do
  1323. p:=tai(p.previous);
  1324. { , but add release after a reg_a_sync }
  1325. if assigned(p) and
  1326. (p.typ=ait_regalloc) and
  1327. (tai_regalloc(p).ratype=ra_sync) then
  1328. p:=tai(p.next);
  1329. if assigned(p) then
  1330. list.insertbefore(pdealloc,p)
  1331. else
  1332. list.concat(pdealloc);
  1333. end;
  1334. end;
  1335. end;
  1336. procedure trgobj.insert_regalloc_info_all(list:TAsmList);
  1337. var
  1338. supreg : tsuperregister;
  1339. begin
  1340. { Insert regallocs for all imaginary registers }
  1341. for supreg:=first_imaginary to maxreg-1 do
  1342. insert_regalloc_info(list,supreg);
  1343. end;
  1344. procedure trgobj.add_cpu_interferences(p : tai);
  1345. begin
  1346. end;
  1347. procedure trgobj.generate_interference_graph(list:TAsmList;headertai:tai);
  1348. var
  1349. p : tai;
  1350. {$ifdef EXTDEBUG}
  1351. i : integer;
  1352. {$endif EXTDEBUG}
  1353. supreg : tsuperregister;
  1354. begin
  1355. { All allocations are available. Now we can generate the
  1356. interference graph. Walk through all instructions, we can
  1357. start with the headertai, because before the header tai is
  1358. only symbols. }
  1359. live_registers.clear;
  1360. p:=headertai;
  1361. while assigned(p) do
  1362. begin
  1363. if p.typ=ait_regalloc then
  1364. with Tai_regalloc(p) do
  1365. begin
  1366. if (getregtype(reg)=regtype) then
  1367. begin
  1368. supreg:=getsupreg(reg);
  1369. case ratype of
  1370. ra_alloc :
  1371. begin
  1372. live_registers.add(supreg);
  1373. add_edges_used(supreg);
  1374. end;
  1375. ra_dealloc :
  1376. begin
  1377. live_registers.delete(supreg);
  1378. add_edges_used(supreg);
  1379. end;
  1380. end;
  1381. { constraints needs always to be updated }
  1382. add_constraints(reg);
  1383. end;
  1384. end;
  1385. add_cpu_interferences(p);
  1386. p:=Tai(p.next);
  1387. end;
  1388. {$ifdef EXTDEBUG}
  1389. if live_registers.length>0 then
  1390. begin
  1391. for i:=0 to live_registers.length-1 do
  1392. begin
  1393. { Only report for imaginary registers }
  1394. if live_registers.buf^[i]>=first_imaginary then
  1395. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,live_registers.buf^[i],defaultsub))+' not released');
  1396. end;
  1397. end;
  1398. {$endif}
  1399. end;
  1400. procedure trgobj.translate_register(var reg : tregister);
  1401. begin
  1402. if (getregtype(reg)=regtype) then
  1403. setsupreg(reg,reginfo[getsupreg(reg)].colour)
  1404. else
  1405. internalerror(200602021);
  1406. end;
  1407. procedure Trgobj.translate_registers(list:TAsmList);
  1408. var
  1409. hp,p,q:Tai;
  1410. i:shortint;
  1411. {$ifdef arm}
  1412. so:pshifterop;
  1413. {$endif arm}
  1414. begin
  1415. { Leave when no imaginary registers are used }
  1416. if maxreg<=first_imaginary then
  1417. exit;
  1418. p:=Tai(list.first);
  1419. while assigned(p) do
  1420. begin
  1421. case p.typ of
  1422. ait_regalloc:
  1423. with Tai_regalloc(p) do
  1424. begin
  1425. if (getregtype(reg)=regtype) then
  1426. begin
  1427. { Only alloc/dealloc is needed for the optimizer, remove
  1428. other regalloc }
  1429. if not(ratype in [ra_alloc,ra_dealloc]) then
  1430. begin
  1431. q:=Tai(next);
  1432. list.remove(p);
  1433. p.free;
  1434. p:=q;
  1435. continue;
  1436. end
  1437. else
  1438. begin
  1439. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1440. {
  1441. Remove sequences of release and
  1442. allocation of the same register like. Other combinations
  1443. of release/allocate need to stay in the list.
  1444. # Register X released
  1445. # Register X allocated
  1446. }
  1447. if assigned(previous) and
  1448. (ratype=ra_alloc) and
  1449. (Tai(previous).typ=ait_regalloc) and
  1450. (Tai_regalloc(previous).reg=reg) and
  1451. (Tai_regalloc(previous).ratype=ra_dealloc) then
  1452. begin
  1453. q:=Tai(next);
  1454. hp:=tai(previous);
  1455. list.remove(hp);
  1456. hp.free;
  1457. list.remove(p);
  1458. p.free;
  1459. p:=q;
  1460. continue;
  1461. end;
  1462. end;
  1463. end;
  1464. end;
  1465. ait_instruction:
  1466. with Taicpu(p) do
  1467. begin
  1468. current_filepos:=fileinfo;
  1469. for i:=0 to ops-1 do
  1470. with oper[i]^ do
  1471. case typ of
  1472. Top_reg:
  1473. if (getregtype(reg)=regtype) then
  1474. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1475. Top_ref:
  1476. begin
  1477. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1478. with ref^ do
  1479. begin
  1480. if (base<>NR_NO) and
  1481. (getregtype(base)=regtype) then
  1482. setsupreg(base,reginfo[getsupreg(base)].colour);
  1483. if (index<>NR_NO) and
  1484. (getregtype(index)=regtype) then
  1485. setsupreg(index,reginfo[getsupreg(index)].colour);
  1486. end;
  1487. end;
  1488. {$ifdef arm}
  1489. Top_shifterop:
  1490. begin
  1491. if regtype=R_INTREGISTER then
  1492. begin
  1493. so:=shifterop;
  1494. if (so^.rs<>NR_NO) and
  1495. (getregtype(so^.rs)=regtype) then
  1496. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1497. end;
  1498. end;
  1499. {$endif arm}
  1500. end;
  1501. { Maybe the operation can be removed when
  1502. it is a move and both arguments are the same }
  1503. if is_same_reg_move(regtype) then
  1504. begin
  1505. q:=Tai(p.next);
  1506. list.remove(p);
  1507. p.free;
  1508. p:=q;
  1509. continue;
  1510. end;
  1511. end;
  1512. end;
  1513. p:=Tai(p.next);
  1514. end;
  1515. current_filepos:=current_procinfo.exitpos;
  1516. end;
  1517. function trgobj.spill_registers(list:TAsmList;headertai:tai):boolean;
  1518. { Returns true if any help registers have been used }
  1519. var
  1520. i : word;
  1521. t : tsuperregister;
  1522. p,q : Tai;
  1523. regs_to_spill_set:Tsuperregisterset;
  1524. spill_temps : ^Tspill_temp_list;
  1525. supreg : tsuperregister;
  1526. templist : TAsmList;
  1527. size: ptrint;
  1528. begin
  1529. spill_registers:=false;
  1530. live_registers.clear;
  1531. for i:=first_imaginary to maxreg-1 do
  1532. exclude(reginfo[i].flags,ri_selected);
  1533. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1534. supregset_reset(regs_to_spill_set,false,$ffff);
  1535. { Allocate temps and insert in front of the list }
  1536. templist:=TAsmList.create;
  1537. {Safe: this procedure is only called if there are spilled nodes.}
  1538. with spillednodes do
  1539. for i:=0 to length-1 do
  1540. begin
  1541. t:=buf^[i];
  1542. {Alternative representation.}
  1543. supregset_include(regs_to_spill_set,t);
  1544. {Clear all interferences of the spilled register.}
  1545. clear_interferences(t);
  1546. {Get a temp for the spilled register, the size must at least equal a complete register,
  1547. take also care of the fact that subreg can be larger than a single register like doubles
  1548. that occupy 2 registers }
  1549. size:=max(tcgsize2size[reg_cgsize(newreg(regtype,t,R_SUBWHOLE))],
  1550. tcgsize2size[reg_cgsize(newreg(regtype,t,reginfo[t].subreg))]);
  1551. tg.gettemp(templist,
  1552. size,size,
  1553. tt_noreuse,spill_temps^[t]);
  1554. end;
  1555. list.insertlistafter(headertai,templist);
  1556. templist.free;
  1557. { Walk through all instructions, we can start with the headertai,
  1558. because before the header tai is only symbols }
  1559. p:=headertai;
  1560. while assigned(p) do
  1561. begin
  1562. case p.typ of
  1563. ait_regalloc:
  1564. with Tai_regalloc(p) do
  1565. begin
  1566. if (getregtype(reg)=regtype) then
  1567. begin
  1568. {A register allocation of a spilled register can be removed.}
  1569. supreg:=getsupreg(reg);
  1570. if supregset_in(regs_to_spill_set,supreg) then
  1571. begin
  1572. q:=Tai(p.next);
  1573. list.remove(p);
  1574. p.free;
  1575. p:=q;
  1576. continue;
  1577. end
  1578. else
  1579. begin
  1580. case ratype of
  1581. ra_alloc :
  1582. live_registers.add(supreg);
  1583. ra_dealloc :
  1584. live_registers.delete(supreg);
  1585. end;
  1586. end;
  1587. end;
  1588. end;
  1589. ait_instruction:
  1590. with Taicpu(p) do
  1591. begin
  1592. current_filepos:=fileinfo;
  1593. if instr_spill_register(list,taicpu(p),regs_to_spill_set,spill_temps^) then
  1594. spill_registers:=true;
  1595. end;
  1596. end;
  1597. p:=Tai(p.next);
  1598. end;
  1599. current_filepos:=current_procinfo.exitpos;
  1600. {Safe: this procedure is only called if there are spilled nodes.}
  1601. with spillednodes do
  1602. for i:=0 to length-1 do
  1603. tg.ungettemp(list,spill_temps^[buf^[i]]);
  1604. freemem(spill_temps);
  1605. end;
  1606. function trgobj.do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;
  1607. begin
  1608. result:=false;
  1609. end;
  1610. procedure Trgobj.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  1611. var ins:Taicpu;
  1612. begin
  1613. ins:=spilling_create_load(spilltemp,tempreg);
  1614. add_cpu_interferences(ins);
  1615. list.insertafter(ins,pos);
  1616. end;
  1617. procedure Trgobj.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  1618. var ins:Taicpu;
  1619. begin
  1620. ins:=spilling_create_store(tempreg,spilltemp);
  1621. add_cpu_interferences(ins);
  1622. list.insertafter(ins,pos);
  1623. end;
  1624. function trgobj.get_spill_subreg(r : tregister) : tsubregister;
  1625. begin
  1626. result:=defaultsub;
  1627. end;
  1628. function trgobj.instr_spill_register(list:TAsmList;
  1629. instr:taicpu;
  1630. const r:Tsuperregisterset;
  1631. const spilltemplist:Tspill_temp_list): boolean;
  1632. var
  1633. counter, regindex: longint;
  1634. regs: tspillregsinfo;
  1635. spilled: boolean;
  1636. procedure addreginfo(reg: tregister; operation: topertype);
  1637. var
  1638. i, tmpindex: longint;
  1639. supreg : tsuperregister;
  1640. begin
  1641. tmpindex := regindex;
  1642. supreg:=get_alias(getsupreg(reg));
  1643. { did we already encounter this register? }
  1644. for i := 0 to pred(regindex) do
  1645. if (regs[i].orgreg = supreg) then
  1646. begin
  1647. tmpindex := i;
  1648. break;
  1649. end;
  1650. if tmpindex > high(regs) then
  1651. internalerror(2003120301);
  1652. regs[tmpindex].orgreg := supreg;
  1653. regs[tmpindex].spillreg:=reg;
  1654. if supregset_in(r,supreg) then
  1655. begin
  1656. { add/update info on this register }
  1657. regs[tmpindex].mustbespilled := true;
  1658. case operation of
  1659. operand_read:
  1660. regs[tmpindex].regread := true;
  1661. operand_write:
  1662. regs[tmpindex].regwritten := true;
  1663. operand_readwrite:
  1664. begin
  1665. regs[tmpindex].regread := true;
  1666. regs[tmpindex].regwritten := true;
  1667. end;
  1668. end;
  1669. spilled := true;
  1670. end;
  1671. inc(regindex,ord(regindex=tmpindex));
  1672. end;
  1673. procedure tryreplacereg(var reg: tregister);
  1674. var
  1675. i: longint;
  1676. supreg: tsuperregister;
  1677. begin
  1678. supreg:=get_alias(getsupreg(reg));
  1679. for i:=0 to pred(regindex) do
  1680. if (regs[i].mustbespilled) and
  1681. (regs[i].orgreg=supreg) then
  1682. begin
  1683. { Only replace supreg }
  1684. setsupreg(reg,getsupreg(regs[i].tempreg));
  1685. break;
  1686. end;
  1687. end;
  1688. var
  1689. loadpos,
  1690. storepos : tai;
  1691. oldlive_registers : tsuperregisterworklist;
  1692. begin
  1693. result := false;
  1694. fillchar(regs,sizeof(regs),0);
  1695. for counter := low(regs) to high(regs) do
  1696. regs[counter].orgreg := RS_INVALID;
  1697. spilled := false;
  1698. regindex := 0;
  1699. { check whether and if so which and how (read/written) this instructions contains
  1700. registers that must be spilled }
  1701. for counter := 0 to instr.ops-1 do
  1702. with instr.oper[counter]^ do
  1703. begin
  1704. case typ of
  1705. top_reg:
  1706. begin
  1707. if (getregtype(reg) = regtype) then
  1708. addreginfo(reg,instr.spilling_get_operation_type(counter));
  1709. end;
  1710. top_ref:
  1711. begin
  1712. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1713. with ref^ do
  1714. begin
  1715. if (base <> NR_NO) then
  1716. addreginfo(base,instr.spilling_get_operation_type_ref(counter,base));
  1717. if (index <> NR_NO) then
  1718. addreginfo(index,instr.spilling_get_operation_type_ref(counter,index));
  1719. end;
  1720. end;
  1721. {$ifdef ARM}
  1722. top_shifterop:
  1723. begin
  1724. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1725. if shifterop^.rs<>NR_NO then
  1726. addreginfo(shifterop^.rs,operand_read);
  1727. end;
  1728. {$endif ARM}
  1729. end;
  1730. end;
  1731. { if no spilling for this instruction we can leave }
  1732. if not spilled then
  1733. exit;
  1734. {$ifdef x86}
  1735. { Try replacing the register with the spilltemp. This is usefull only
  1736. for the i386,x86_64 that support memory locations for several instructions }
  1737. for counter := 0 to pred(regindex) do
  1738. with regs[counter] do
  1739. begin
  1740. if mustbespilled then
  1741. begin
  1742. if do_spill_replace(list,instr,orgreg,spilltemplist[orgreg]) then
  1743. mustbespilled:=false;
  1744. end;
  1745. end;
  1746. {$endif x86}
  1747. {
  1748. There are registers that need are spilled. We generate the
  1749. following code for it. The used positions where code need
  1750. to be inserted are marked using #. Note that code is always inserted
  1751. before the positions using pos.previous. This way the position is always
  1752. the same since pos doesn't change, but pos.previous is modified everytime
  1753. new code is inserted.
  1754. [
  1755. - reg_allocs load spills
  1756. - load spills
  1757. ]
  1758. [#loadpos
  1759. - reg_deallocs
  1760. - reg_allocs
  1761. ]
  1762. [
  1763. - reg_deallocs for load-only spills
  1764. - reg_allocs for store-only spills
  1765. ]
  1766. [#instr
  1767. - original instruction
  1768. ]
  1769. [
  1770. - store spills
  1771. - reg_deallocs store spills
  1772. ]
  1773. [#storepos
  1774. ]
  1775. }
  1776. result := true;
  1777. oldlive_registers.copyfrom(live_registers);
  1778. { Process all tai_regallocs belonging to this instruction, ignore explicit
  1779. inserted regallocs. These can happend for example in i386:
  1780. mov ref,ireg26
  1781. <regdealloc ireg26, instr=taicpu of lea>
  1782. <regalloc edi, insrt=nil>
  1783. lea [ireg26+ireg17],edi
  1784. All released registers are also added to the live_registers because
  1785. they can't be used during the spilling }
  1786. loadpos:=tai(instr.previous);
  1787. while assigned(loadpos) and
  1788. (loadpos.typ=ait_regalloc) and
  1789. ((tai_regalloc(loadpos).instr=nil) or
  1790. (tai_regalloc(loadpos).instr=instr)) do
  1791. begin
  1792. { Only add deallocs belonging to the instruction. Explicit inserted deallocs
  1793. belong to the previous instruction and not the current instruction }
  1794. if (tai_regalloc(loadpos).instr=instr) and
  1795. (tai_regalloc(loadpos).ratype=ra_dealloc) then
  1796. live_registers.add(getsupreg(tai_regalloc(loadpos).reg));
  1797. loadpos:=tai(loadpos.previous);
  1798. end;
  1799. loadpos:=tai(loadpos.next);
  1800. { Load the spilled registers }
  1801. for counter := 0 to pred(regindex) do
  1802. with regs[counter] do
  1803. begin
  1804. if mustbespilled and regread then
  1805. begin
  1806. tempreg:=getregisterinline(list,get_spill_subreg(regs[counter].spillreg));
  1807. do_spill_read(list,tai(loadpos.previous),spilltemplist[orgreg],tempreg);
  1808. end;
  1809. end;
  1810. { Release temp registers of read-only registers, and add reference of the instruction
  1811. to the reginfo }
  1812. for counter := 0 to pred(regindex) do
  1813. with regs[counter] do
  1814. begin
  1815. if mustbespilled and regread and (not regwritten) then
  1816. begin
  1817. { The original instruction will be the next that uses this register }
  1818. add_reg_instruction(instr,tempreg,1);
  1819. ungetregisterinline(list,tempreg);
  1820. end;
  1821. end;
  1822. { Allocate temp registers of write-only registers, and add reference of the instruction
  1823. to the reginfo }
  1824. for counter := 0 to pred(regindex) do
  1825. with regs[counter] do
  1826. begin
  1827. if mustbespilled and regwritten then
  1828. begin
  1829. { When the register is also loaded there is already a register assigned }
  1830. if (not regread) then
  1831. tempreg:=getregisterinline(list,get_spill_subreg(regs[counter].spillreg));
  1832. { The original instruction will be the next that uses this register, this
  1833. also needs to be done for read-write registers }
  1834. add_reg_instruction(instr,tempreg,1);
  1835. end;
  1836. end;
  1837. { store the spilled registers }
  1838. storepos:=tai(instr.next);
  1839. for counter := 0 to pred(regindex) do
  1840. with regs[counter] do
  1841. begin
  1842. if mustbespilled and regwritten then
  1843. begin
  1844. do_spill_written(list,tai(storepos.previous),spilltemplist[orgreg],tempreg);
  1845. ungetregisterinline(list,tempreg);
  1846. end;
  1847. end;
  1848. { now all spilling code is generated we can restore the live registers. This
  1849. must be done after the store because the store can need an extra register
  1850. that also needs to conflict with the registers of the instruction }
  1851. live_registers.done;
  1852. live_registers:=oldlive_registers;
  1853. { substitute registers }
  1854. for counter:=0 to instr.ops-1 do
  1855. with instr.oper[counter]^ do
  1856. case typ of
  1857. top_reg:
  1858. begin
  1859. if (getregtype(reg) = regtype) then
  1860. tryreplacereg(reg);
  1861. end;
  1862. top_ref:
  1863. begin
  1864. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1865. begin
  1866. tryreplacereg(ref^.base);
  1867. tryreplacereg(ref^.index);
  1868. end;
  1869. end;
  1870. {$ifdef ARM}
  1871. top_shifterop:
  1872. begin
  1873. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1874. tryreplacereg(shifterop^.rs);
  1875. end;
  1876. {$endif ARM}
  1877. end;
  1878. {We have modified the instruction; perhaps the new instruction has
  1879. certain constraints regarding which imaginary registers interfere
  1880. with certain physical registers.}
  1881. add_cpu_interferences(instr);
  1882. end;
  1883. end.