rgobj.pas 67 KB

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