rgobj.pas 72 KB

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