rgobj.pas 69 KB

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