2
0

rgobj.pas 68 KB

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