rgobj.pas 69 KB

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