rgobj.pas 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725
  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. { $define DEBUG_REGALLOC}
  19. { $define DEBUG_SPILLCOALESCE}
  20. { $define DEBUG_REGISTERLIFE}
  21. { Allow duplicate allocations, can be used to get the .s file written }
  22. { $define ALLOWDUPREG}
  23. {$ifdef DEBUG_REGALLOC}
  24. {$define EXTDEBUG}
  25. {$endif DEBUG_REGALLOC}
  26. unit rgobj;
  27. interface
  28. uses
  29. cutils, cpubase,
  30. aasmtai,aasmdata,aasmsym,aasmcpu,
  31. cclasses,globtype,cgbase,cgutils;
  32. type
  33. {
  34. The interference bitmap contains of 2 layers:
  35. layer 1 - 256*256 blocks with pointers to layer 2 blocks
  36. layer 2 - blocks of 32*256 (32 bytes = 256 bits)
  37. }
  38. Tinterferencebitmap2 = array[byte] of set of byte;
  39. Pinterferencebitmap2 = ^Tinterferencebitmap2;
  40. Tinterferencebitmap1 = array[byte] of Pinterferencebitmap2;
  41. pinterferencebitmap1 = ^tinterferencebitmap1;
  42. Tinterferencebitmap=class
  43. private
  44. maxx1,
  45. maxy1 : byte;
  46. fbitmap : pinterferencebitmap1;
  47. function getbitmap(x,y:tsuperregister):boolean;
  48. procedure setbitmap(x,y:tsuperregister;b:boolean);
  49. public
  50. constructor create;
  51. destructor destroy;override;
  52. property bitmap[x,y:tsuperregister]:boolean read getbitmap write setbitmap;default;
  53. end;
  54. Tmovelistheader=record
  55. count,
  56. maxcount,
  57. sorted_until : cardinal;
  58. end;
  59. Tmovelist=record
  60. header : Tmovelistheader;
  61. data : array[tsuperregister] of Tlinkedlistitem;
  62. end;
  63. Pmovelist=^Tmovelist;
  64. {In the register allocator we keep track of move instructions.
  65. These instructions are moved between five linked lists. There
  66. is also a linked list per register to keep track about the moves
  67. it is associated with. Because we need to determine quickly in
  68. which of the five lists it is we add anu enumeradtion to each
  69. move instruction.}
  70. Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
  71. ms_worklist_moves,ms_active_moves);
  72. Tmoveins=class(Tlinkedlistitem)
  73. moveset:Tmoveset;
  74. x,y:Tsuperregister;
  75. end;
  76. Treginfoflag=(ri_coalesced,ri_selected);
  77. Treginfoflagset=set of Treginfoflag;
  78. Treginfo=record
  79. live_start,
  80. live_end : Tai;
  81. subreg : tsubregister;
  82. alias : Tsuperregister;
  83. { The register allocator assigns each register a colour }
  84. colour : Tsuperregister;
  85. movelist : Pmovelist;
  86. adjlist : Psuperregisterworklist;
  87. degree : TSuperregister;
  88. flags : Treginfoflagset;
  89. weight : longint;
  90. {$ifdef llvm}
  91. def : pointer;
  92. {$endif llvm}
  93. count_uses : longint;
  94. total_interferences : longint;
  95. end;
  96. Preginfo=^TReginfo;
  97. tspillreginfo = record
  98. { a single register may appear more than once in an instruction,
  99. but with different subregister types -> store all subregister types
  100. that occur, so we can add the necessary constraints for the inline
  101. register that will have to replace it }
  102. spillregconstraints : set of TSubRegister;
  103. orgreg : tsuperregister;
  104. loadreg,
  105. storereg: tregister;
  106. regread, regwritten, mustbespilled: boolean;
  107. end;
  108. tspillregsinfo = record
  109. reginfocount: longint;
  110. reginfo: array[0..3] of tspillreginfo;
  111. end;
  112. Pspill_temp_list=^Tspill_temp_list;
  113. Tspill_temp_list=array[tsuperregister] of Treference;
  114. { used to store where a register is spilled and what interferences it has at the point of being spilled }
  115. tspillinfo = record
  116. spilllocation : treference;
  117. spilled : boolean;
  118. interferences : Tinterferencebitmap;
  119. end;
  120. {#------------------------------------------------------------------
  121. This class implements the default register allocator. It is used by the
  122. code generator to allocate and free registers which might be valid
  123. across nodes. It also contains utility routines related to registers.
  124. Some of the methods in this class should be overridden
  125. by cpu-specific implementations.
  126. --------------------------------------------------------------------}
  127. trgobj=class
  128. preserved_by_proc : tcpuregisterset;
  129. used_in_proc : tcpuregisterset;
  130. { generate SSA code? }
  131. ssa_safe: boolean;
  132. constructor create(Aregtype:Tregistertype;
  133. Adefaultsub:Tsubregister;
  134. const Ausable:array of tsuperregister;
  135. Afirst_imaginary:Tsuperregister;
  136. Apreserved_by_proc:Tcpuregisterset);
  137. destructor destroy;override;
  138. { Allocate a register. An internalerror will be generated if there is
  139. no more free registers which can be allocated.}
  140. function getregister(list:TAsmList;subreg:Tsubregister):Tregister;virtual;
  141. { Get the register specified.}
  142. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  143. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  144. { Get multiple registers specified.}
  145. procedure alloccpuregisters(list:TAsmList;const r:Tcpuregisterset);virtual;
  146. { Free multiple registers specified.}
  147. procedure dealloccpuregisters(list:TAsmList;const r:Tcpuregisterset);virtual;
  148. function uses_registers:boolean;virtual;
  149. procedure add_reg_instruction(instr:Tai;r:tregister;aweight:longint);
  150. procedure add_move_instruction(instr:Taicpu);
  151. { Do the register allocation.}
  152. procedure do_register_allocation(list:TAsmList;headertai:tai);virtual;
  153. { Adds an interference edge.
  154. don't move this to the protected section, the arm cg requires to access this (FK) }
  155. procedure add_edge(u,v:Tsuperregister);
  156. { translates a single given imaginary register to it's real register }
  157. procedure translate_register(var reg : tregister);
  158. protected
  159. maxreginfo,
  160. maxreginfoinc,
  161. maxreg : Tsuperregister;
  162. regtype : Tregistertype;
  163. { default subregister used }
  164. defaultsub : tsubregister;
  165. live_registers:Tsuperregisterworklist;
  166. spillednodes: tsuperregisterworklist;
  167. { can be overridden to add cpu specific interferences }
  168. procedure add_cpu_interferences(p : tai);virtual;
  169. procedure add_constraints(reg:Tregister);virtual;
  170. function getregisterinline(list:TAsmList;const subregconstraints:Tsubregisterset):Tregister;
  171. procedure ungetregisterinline(list:TAsmList;r:Tregister);
  172. function get_spill_subreg(r : tregister) : tsubregister;virtual;
  173. function do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;virtual;
  174. { the orgrsupeg parameter is only here for the llvm target, so it can
  175. discover the def to use for the load }
  176. procedure do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister;orgsupreg:tsuperregister);virtual;
  177. procedure do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister;orgsupreg:tsuperregister);virtual;
  178. function addreginfo(var regs: tspillregsinfo; const r: tsuperregisterset; reg: tregister; operation: topertype): boolean;
  179. function instr_get_oper_spilling_info(var regs: tspillregsinfo; const r: tsuperregisterset; instr: tai_cpu_abstract_sym; opidx: longint): boolean; virtual;
  180. procedure substitute_spilled_registers(const regs: tspillregsinfo; instr: tai_cpu_abstract_sym; opidx: longint); virtual;
  181. procedure try_replace_reg(const regs: tspillregsinfo; var reg: tregister; useloadreg: boolean);
  182. function instr_spill_register(list:TAsmList;
  183. instr:tai_cpu_abstract_sym;
  184. const r:Tsuperregisterset;
  185. const spilltemplist:Tspill_temp_list): boolean;virtual;
  186. procedure insert_regalloc_info_all(list:TAsmList);
  187. procedure determine_spill_registers(list:TAsmList;headertail:tai); virtual;
  188. procedure get_spill_temp(list:TAsmlist;spill_temps: Pspill_temp_list; supreg: tsuperregister);virtual;
  189. strict protected
  190. { Highest register allocated until now.}
  191. reginfo : PReginfo;
  192. private
  193. int_live_range_direction: TRADirection;
  194. { First imaginary register.}
  195. first_imaginary : Tsuperregister;
  196. usable_registers_cnt : word;
  197. usable_registers : array[0..maxcpuregister] of tsuperregister;
  198. usable_register_set : tcpuregisterset;
  199. ibitmap : Tinterferencebitmap;
  200. simplifyworklist,
  201. freezeworklist,
  202. spillworklist,
  203. coalescednodes,
  204. selectstack : tsuperregisterworklist;
  205. worklist_moves,
  206. active_moves,
  207. frozen_moves,
  208. coalesced_moves,
  209. constrained_moves,
  210. { in this list we collect all moveins which should be disposed after register allocation finishes,
  211. we still need the moves for spill coalesce for the whole register allocation process, so they cannot be
  212. released as soon as they are frozen or whatever }
  213. move_garbage : Tlinkedlist;
  214. extended_backwards,
  215. backwards_was_first : tbitset;
  216. has_usedmarks: boolean;
  217. has_directalloc: boolean;
  218. spillinfo : array of tspillinfo;
  219. { Disposes of the reginfo array.}
  220. procedure dispose_reginfo;
  221. { Prepare the register colouring.}
  222. procedure prepare_colouring;
  223. { Clean up after register colouring.}
  224. procedure epilogue_colouring;
  225. { Colour the registers; that is do the register allocation.}
  226. procedure colour_registers;
  227. procedure insert_regalloc_info(list:TAsmList;u:tsuperregister);
  228. procedure generate_interference_graph(list:TAsmList;headertai:tai);
  229. { sort spilled nodes by increasing number of interferences }
  230. procedure sort_spillednodes;
  231. { translates the registers in the given assembler list }
  232. procedure translate_registers(list:TAsmList);
  233. function spill_registers(list:TAsmList;headertai:tai):boolean;virtual;
  234. function getnewreg(subreg:tsubregister):tsuperregister;
  235. procedure add_edges_used(u:Tsuperregister);
  236. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  237. function move_related(n:Tsuperregister):boolean;
  238. procedure make_work_list;
  239. procedure sort_simplify_worklist;
  240. procedure enable_moves(n:Tsuperregister);
  241. procedure decrement_degree(m:Tsuperregister);
  242. procedure simplify;
  243. procedure add_worklist(u:Tsuperregister);
  244. function adjacent_ok(u,v:Tsuperregister):boolean;
  245. function conservative(u,v:Tsuperregister):boolean;
  246. procedure coalesce;
  247. procedure freeze_moves(u:Tsuperregister);
  248. procedure freeze;
  249. procedure select_spill;
  250. procedure assign_colours;
  251. procedure clear_interferences(u:Tsuperregister);
  252. procedure set_live_range_direction(dir: TRADirection);
  253. procedure set_live_start(reg : tsuperregister;t : tai);
  254. function get_live_start(reg : tsuperregister) : tai;
  255. procedure set_live_end(reg : tsuperregister;t : tai);
  256. function get_live_end(reg : tsuperregister) : tai;
  257. public
  258. {$ifdef EXTDEBUG}
  259. procedure writegraph(loopidx:longint);
  260. {$endif EXTDEBUG}
  261. procedure combine(u,v:Tsuperregister);
  262. { set v as an alias for u }
  263. procedure set_alias(u,v:Tsuperregister);
  264. function get_alias(n:Tsuperregister):Tsuperregister;
  265. property live_range_direction: TRADirection read int_live_range_direction write set_live_range_direction;
  266. property live_start[reg : tsuperregister]: tai read get_live_start write set_live_start;
  267. property live_end[reg : tsuperregister]: tai read get_live_end write set_live_end;
  268. end;
  269. const
  270. first_reg = 0;
  271. last_reg = high(tsuperregister)-1;
  272. maxspillingcounter = 20;
  273. implementation
  274. uses
  275. sysutils,
  276. globals,
  277. verbose,tgobj,procinfo;
  278. procedure sort_movelist(ml:Pmovelist);
  279. {Ok, sorting pointers is silly, but it does the job to make Trgobj.combine
  280. faster.}
  281. var h,i,p:longword;
  282. t:Tlinkedlistitem;
  283. begin
  284. with ml^ do
  285. begin
  286. if header.count<2 then
  287. exit;
  288. p:=1;
  289. while 2*cardinal(p)<header.count do
  290. p:=2*p;
  291. while p<>0 do
  292. begin
  293. for h:=p to header.count-1 do
  294. begin
  295. i:=h;
  296. t:=data[i];
  297. repeat
  298. if ptruint(data[i-p])<=ptruint(t) then
  299. break;
  300. data[i]:=data[i-p];
  301. dec(i,p);
  302. until i<p;
  303. data[i]:=t;
  304. end;
  305. p:=p shr 1;
  306. end;
  307. header.sorted_until:=header.count-1;
  308. end;
  309. end;
  310. {******************************************************************************
  311. tinterferencebitmap
  312. ******************************************************************************}
  313. constructor tinterferencebitmap.create;
  314. begin
  315. inherited create;
  316. maxx1:=1;
  317. fbitmap:=AllocMem(sizeof(tinterferencebitmap1)*2);
  318. end;
  319. destructor tinterferencebitmap.destroy;
  320. var i,j:byte;
  321. begin
  322. for i:=0 to maxx1 do
  323. for j:=0 to maxy1 do
  324. if assigned(fbitmap[i,j]) then
  325. dispose(fbitmap[i,j]);
  326. freemem(fbitmap);
  327. end;
  328. function tinterferencebitmap.getbitmap(x,y:tsuperregister):boolean;
  329. var
  330. page : pinterferencebitmap2;
  331. begin
  332. result:=false;
  333. if (x shr 8>maxx1) then
  334. exit;
  335. page:=fbitmap[x shr 8,y shr 8];
  336. result:=assigned(page) and
  337. ((x and $ff) in page^[y and $ff]);
  338. end;
  339. procedure tinterferencebitmap.setbitmap(x,y:tsuperregister;b:boolean);
  340. var
  341. x1,y1 : byte;
  342. begin
  343. x1:=x shr 8;
  344. y1:=y shr 8;
  345. if x1>maxx1 then
  346. begin
  347. reallocmem(fbitmap,sizeof(tinterferencebitmap1)*(x1+1));
  348. fillchar(fbitmap[maxx1+1],sizeof(tinterferencebitmap1)*(x1-maxx1),0);
  349. maxx1:=x1;
  350. end;
  351. if not assigned(fbitmap[x1,y1]) then
  352. begin
  353. if y1>maxy1 then
  354. maxy1:=y1;
  355. new(fbitmap[x1,y1]);
  356. fillchar(fbitmap[x1,y1]^,sizeof(tinterferencebitmap2),0);
  357. end;
  358. if b then
  359. include(fbitmap[x1,y1]^[y and $ff],(x and $ff))
  360. else
  361. exclude(fbitmap[x1,y1]^[y and $ff],(x and $ff));
  362. end;
  363. {******************************************************************************
  364. trgobj
  365. ******************************************************************************}
  366. constructor trgobj.create(Aregtype:Tregistertype;
  367. Adefaultsub:Tsubregister;
  368. const Ausable:array of tsuperregister;
  369. Afirst_imaginary:Tsuperregister;
  370. Apreserved_by_proc:Tcpuregisterset);
  371. var
  372. i : cardinal;
  373. begin
  374. { empty super register sets can cause very strange problems }
  375. if high(Ausable)=-1 then
  376. internalerror(200210181);
  377. live_range_direction:=rad_forward;
  378. first_imaginary:=Afirst_imaginary;
  379. maxreg:=Afirst_imaginary;
  380. regtype:=Aregtype;
  381. defaultsub:=Adefaultsub;
  382. preserved_by_proc:=Apreserved_by_proc;
  383. // default values set by newinstance
  384. // used_in_proc:=[];
  385. // ssa_safe:=false;
  386. live_registers.init;
  387. { Get reginfo for CPU registers }
  388. maxreginfo:=first_imaginary;
  389. maxreginfoinc:=16;
  390. worklist_moves:=Tlinkedlist.create;
  391. move_garbage:=TLinkedList.Create;
  392. reginfo:=allocmem(first_imaginary*sizeof(treginfo));
  393. for i:=0 to first_imaginary-1 do
  394. begin
  395. reginfo[i].degree:=high(tsuperregister);
  396. reginfo[i].alias:=RS_INVALID;
  397. end;
  398. { Usable registers }
  399. // default value set by constructor
  400. // fillchar(usable_registers,sizeof(usable_registers),0);
  401. for i:=low(Ausable) to high(Ausable) do
  402. begin
  403. usable_registers[i]:=Ausable[i];
  404. include(usable_register_set,Ausable[i]);
  405. end;
  406. usable_registers_cnt:=high(Ausable)+1;
  407. { Initialize Worklists }
  408. spillednodes.init;
  409. simplifyworklist.init;
  410. freezeworklist.init;
  411. spillworklist.init;
  412. coalescednodes.init;
  413. selectstack.init;
  414. end;
  415. destructor trgobj.destroy;
  416. begin
  417. spillednodes.done;
  418. simplifyworklist.done;
  419. freezeworklist.done;
  420. spillworklist.done;
  421. coalescednodes.done;
  422. selectstack.done;
  423. live_registers.done;
  424. move_garbage.free;
  425. worklist_moves.free;
  426. dispose_reginfo;
  427. extended_backwards.free;
  428. backwards_was_first.free;
  429. end;
  430. procedure Trgobj.dispose_reginfo;
  431. var
  432. i : cardinal;
  433. begin
  434. if reginfo<>nil then
  435. begin
  436. for i:=0 to maxreg-1 do
  437. with reginfo[i] do
  438. begin
  439. if adjlist<>nil then
  440. dispose(adjlist,done);
  441. if movelist<>nil then
  442. dispose(movelist);
  443. end;
  444. freemem(reginfo);
  445. reginfo:=nil;
  446. end;
  447. end;
  448. function trgobj.getnewreg(subreg:tsubregister):tsuperregister;
  449. var
  450. oldmaxreginfo : tsuperregister;
  451. begin
  452. result:=maxreg;
  453. inc(maxreg);
  454. if maxreg>=last_reg then
  455. Message(parser_f_too_complex_proc);
  456. if maxreg>=maxreginfo then
  457. begin
  458. oldmaxreginfo:=maxreginfo;
  459. { Prevent overflow }
  460. if maxreginfoinc>last_reg-maxreginfo then
  461. maxreginfo:=last_reg
  462. else
  463. begin
  464. inc(maxreginfo,maxreginfoinc);
  465. if maxreginfoinc<256 then
  466. maxreginfoinc:=maxreginfoinc*2;
  467. end;
  468. reallocmem(reginfo,maxreginfo*sizeof(treginfo));
  469. { Do we really need it to clear it ? At least for 1.0.x (PFV) }
  470. fillchar(reginfo[oldmaxreginfo],(maxreginfo-oldmaxreginfo)*sizeof(treginfo),0);
  471. end;
  472. reginfo[result].subreg:=subreg;
  473. end;
  474. function trgobj.getregister(list:TAsmList;subreg:Tsubregister):Tregister;
  475. begin
  476. {$ifdef EXTDEBUG}
  477. if reginfo=nil then
  478. InternalError(2004020901);
  479. {$endif EXTDEBUG}
  480. if defaultsub=R_SUBNONE then
  481. result:=newreg(regtype,getnewreg(R_SUBNONE),R_SUBNONE)
  482. else
  483. result:=newreg(regtype,getnewreg(subreg),subreg);
  484. end;
  485. function trgobj.uses_registers:boolean;
  486. begin
  487. result:=(maxreg>first_imaginary) or has_usedmarks or has_directalloc;
  488. end;
  489. procedure trgobj.ungetcpuregister(list:TAsmList;r:Tregister);
  490. begin
  491. if (getsupreg(r)>=first_imaginary) then
  492. InternalError(2004020901);
  493. list.concat(Tai_regalloc.dealloc(r,nil));
  494. end;
  495. procedure trgobj.getcpuregister(list:TAsmList;r:Tregister);
  496. var
  497. supreg:Tsuperregister;
  498. begin
  499. supreg:=getsupreg(r);
  500. if supreg>=first_imaginary then
  501. internalerror(2003121503);
  502. include(used_in_proc,supreg);
  503. has_directalloc:=true;
  504. list.concat(Tai_regalloc.alloc(r,nil));
  505. end;
  506. procedure trgobj.alloccpuregisters(list:TAsmList;const r:Tcpuregisterset);
  507. var i:cardinal;
  508. begin
  509. for i:=0 to first_imaginary-1 do
  510. if i in r then
  511. getcpuregister(list,newreg(regtype,i,defaultsub));
  512. end;
  513. procedure trgobj.dealloccpuregisters(list:TAsmList;const r:Tcpuregisterset);
  514. var i:cardinal;
  515. begin
  516. for i:=0 to first_imaginary-1 do
  517. if i in r then
  518. ungetcpuregister(list,newreg(regtype,i,defaultsub));
  519. end;
  520. const
  521. rtindex : longint = 0;
  522. procedure trgobj.do_register_allocation(list:TAsmList;headertai:tai);
  523. var
  524. spillingcounter:byte;
  525. endspill:boolean;
  526. i : Longint;
  527. begin
  528. { Insert regalloc info for imaginary registers }
  529. insert_regalloc_info_all(list);
  530. ibitmap:=tinterferencebitmap.create;
  531. generate_interference_graph(list,headertai);
  532. {$ifdef DEBUG_REGALLOC}
  533. writegraph(rtindex);
  534. {$endif DEBUG_REGALLOC}
  535. inc(rtindex);
  536. { Don't do the real allocation when -sr is passed }
  537. if (cs_no_regalloc in current_settings.globalswitches) then
  538. exit;
  539. {Do register allocation.}
  540. spillingcounter:=0;
  541. repeat
  542. determine_spill_registers(list,headertai);
  543. endspill:=true;
  544. if spillednodes.length<>0 then
  545. begin
  546. inc(spillingcounter);
  547. if spillingcounter>maxspillingcounter then
  548. begin
  549. {$ifdef EXTDEBUG}
  550. { Only exit here so the .s file is still generated. Assembling
  551. the file will still trigger an error }
  552. exit;
  553. {$else}
  554. internalerror(200309041);
  555. {$endif}
  556. end;
  557. endspill:=not spill_registers(list,headertai);
  558. end;
  559. until endspill;
  560. ibitmap.free;
  561. translate_registers(list);
  562. { we need the translation table for debugging info and verbose assembler output,
  563. so not dispose them yet (FK)
  564. }
  565. for i:=0 to High(spillinfo) do
  566. spillinfo[i].interferences.Free;
  567. spillinfo:=nil;
  568. end;
  569. procedure trgobj.add_constraints(reg:Tregister);
  570. begin
  571. end;
  572. procedure trgobj.add_edge(u,v:Tsuperregister);
  573. {This procedure will add an edge to the virtual interference graph.}
  574. procedure addadj(u,v:Tsuperregister);
  575. begin
  576. {$ifdef EXTDEBUG}
  577. if (u>=maxreginfo) then
  578. internalerror(2012101901);
  579. {$endif}
  580. with reginfo[u] do
  581. begin
  582. if adjlist=nil then
  583. new(adjlist,init);
  584. adjlist^.add(v);
  585. end;
  586. end;
  587. begin
  588. if (u<>v) and not(ibitmap[v,u]) then
  589. begin
  590. ibitmap[v,u]:=true;
  591. ibitmap[u,v]:=true;
  592. {Precoloured nodes are not stored in the interference graph.}
  593. if (u>=first_imaginary) then
  594. addadj(u,v);
  595. if (v>=first_imaginary) then
  596. addadj(v,u);
  597. end;
  598. end;
  599. procedure trgobj.add_edges_used(u:Tsuperregister);
  600. var i:cardinal;
  601. begin
  602. with live_registers do
  603. if length>0 then
  604. for i:=0 to length-1 do
  605. add_edge(u,get_alias(buf^[i]));
  606. end;
  607. {$ifdef EXTDEBUG}
  608. procedure trgobj.writegraph(loopidx:longint);
  609. {This procedure writes out the current interference graph in the
  610. register allocator.}
  611. var f:text;
  612. i,j:cardinal;
  613. begin
  614. assign(f,current_procinfo.procdef.mangledname+'_igraph'+tostr(loopidx));
  615. rewrite(f);
  616. writeln(f,'Interference graph of ',current_procinfo.procdef.fullprocname(true));
  617. writeln(f,'First imaginary register is ',first_imaginary,' ($',hexstr(first_imaginary,2),')');
  618. writeln(f);
  619. write(f,' ');
  620. for i:=0 to maxreg div 16 do
  621. for j:=0 to 15 do
  622. write(f,hexstr(i,1));
  623. writeln(f);
  624. write(f,'Weight Degree ');
  625. for i:=0 to maxreg div 16 do
  626. write(f,'0123456789ABCDEF');
  627. writeln(f);
  628. for i:=0 to maxreg-1 do
  629. begin
  630. write(f,reginfo[i].weight:5,' ',reginfo[i].degree:5,' ',reginfo[i].count_uses:5,' ',reginfo[i].total_interferences:5,' ',hexstr(i,2):4);
  631. for j:=0 to maxreg-1 do
  632. if ibitmap[i,j] then
  633. write(f,'*')
  634. else
  635. write(f,'-');
  636. writeln(f);
  637. end;
  638. close(f);
  639. end;
  640. {$endif EXTDEBUG}
  641. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  642. begin
  643. {$ifdef EXTDEBUG}
  644. if (u>=maxreginfo) then
  645. internalerror(2012101902);
  646. {$endif}
  647. with reginfo[u] do
  648. begin
  649. if movelist=nil then
  650. begin
  651. { don't use sizeof(tmovelistheader), because that ignores alignment }
  652. getmem(movelist,ptruint(@movelist^.data)-ptruint(movelist)+16*sizeof(pointer));
  653. movelist^.header.maxcount:=16;
  654. movelist^.header.count:=0;
  655. movelist^.header.sorted_until:=0;
  656. end
  657. else
  658. begin
  659. if movelist^.header.count>=movelist^.header.maxcount then
  660. begin
  661. movelist^.header.maxcount:=movelist^.header.maxcount*2;
  662. { don't use sizeof(tmovelistheader), because that ignores alignment }
  663. reallocmem(movelist,ptruint(@movelist^.data)-ptruint(movelist)+movelist^.header.maxcount*sizeof(pointer));
  664. end;
  665. end;
  666. movelist^.data[movelist^.header.count]:=data;
  667. inc(movelist^.header.count);
  668. end;
  669. end;
  670. procedure trgobj.set_live_range_direction(dir: TRADirection);
  671. begin
  672. if (dir in [rad_backwards,rad_backwards_reinit]) then
  673. begin
  674. if not assigned(extended_backwards) then
  675. begin
  676. { create expects a "size", not a "max bit" parameter -> +1 }
  677. backwards_was_first:=tbitset.create(maxreg+1);
  678. extended_backwards:=tbitset.create(maxreg+1);
  679. end
  680. else
  681. begin
  682. if (dir=rad_backwards_reinit) then
  683. extended_backwards.clear;
  684. backwards_was_first.clear;
  685. end;
  686. int_live_range_direction:=rad_backwards;
  687. end
  688. else
  689. int_live_range_direction:=rad_forward;
  690. end;
  691. procedure trgobj.set_live_start(reg: tsuperregister; t: tai);
  692. begin
  693. reginfo[reg].live_start:=t;
  694. end;
  695. function trgobj.get_live_start(reg: tsuperregister): tai;
  696. begin
  697. result:=reginfo[reg].live_start;
  698. end;
  699. procedure trgobj.set_live_end(reg: tsuperregister; t: tai);
  700. begin
  701. reginfo[reg].live_end:=t;
  702. end;
  703. function trgobj.get_live_end(reg: tsuperregister): tai;
  704. begin
  705. result:=reginfo[reg].live_end;
  706. end;
  707. procedure trgobj.add_reg_instruction(instr:Tai;r:tregister;aweight:longint);
  708. var
  709. supreg : tsuperregister;
  710. begin
  711. supreg:=getsupreg(r);
  712. {$ifdef extdebug}
  713. if not (cs_no_regalloc in current_settings.globalswitches) and
  714. (supreg>=maxreginfo) then
  715. internalerror(200411061);
  716. {$endif extdebug}
  717. if supreg>=first_imaginary then
  718. with reginfo[supreg] do
  719. begin
  720. { avoid overflow }
  721. if high(weight)-aweight<weight then
  722. weight:=high(weight)
  723. else
  724. inc(weight,aweight);
  725. if (live_range_direction=rad_forward) then
  726. begin
  727. if not assigned(live_start) then
  728. live_start:=instr;
  729. live_end:=instr;
  730. end
  731. else
  732. begin
  733. if not extended_backwards.isset(supreg) then
  734. begin
  735. extended_backwards.include(supreg);
  736. live_start := instr;
  737. if not assigned(live_end) then
  738. begin
  739. backwards_was_first.include(supreg);
  740. live_end := instr;
  741. end;
  742. end
  743. else
  744. begin
  745. if backwards_was_first.isset(supreg) then
  746. live_end := instr;
  747. end
  748. end
  749. end;
  750. end;
  751. procedure trgobj.add_move_instruction(instr:Taicpu);
  752. {This procedure notifies a certain as a move instruction so the
  753. register allocator can try to eliminate it.}
  754. var i:Tmoveins;
  755. sreg, dreg : Tregister;
  756. ssupreg,dsupreg:Tsuperregister;
  757. begin
  758. {$ifdef extdebug}
  759. if (instr.oper[O_MOV_SOURCE]^.typ<>top_reg) or
  760. (instr.oper[O_MOV_DEST]^.typ<>top_reg) then
  761. internalerror(200311291);
  762. {$endif}
  763. sreg:=instr.oper[O_MOV_SOURCE]^.reg;
  764. dreg:=instr.oper[O_MOV_DEST]^.reg;
  765. { How should we handle m68k move %d0,%a0? }
  766. if (getregtype(sreg)<>getregtype(dreg)) then
  767. exit;
  768. i:=Tmoveins.create;
  769. i.moveset:=ms_worklist_moves;
  770. worklist_moves.insert(i);
  771. ssupreg:=getsupreg(sreg);
  772. add_to_movelist(ssupreg,i);
  773. dsupreg:=getsupreg(dreg);
  774. { On m68k move can mix address and integer registers,
  775. this leads to problems ... PM }
  776. if (ssupreg<>dsupreg) {and (getregtype(sreg)=getregtype(dreg))} then
  777. {Avoid adding the same move instruction twice to a single register.}
  778. add_to_movelist(dsupreg,i);
  779. i.x:=ssupreg;
  780. i.y:=dsupreg;
  781. end;
  782. function trgobj.move_related(n:Tsuperregister):boolean;
  783. var i:cardinal;
  784. begin
  785. move_related:=false;
  786. with reginfo[n] do
  787. if movelist<>nil then
  788. with movelist^ do
  789. for i:=0 to header.count-1 do
  790. if Tmoveins(data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  791. begin
  792. move_related:=true;
  793. break;
  794. end;
  795. end;
  796. procedure Trgobj.sort_simplify_worklist;
  797. {Sorts the simplifyworklist by the number of interferences the
  798. registers in it cause. This allows simplify to execute in
  799. constant time.}
  800. var p,h,i,leni,lent:longword;
  801. t:Tsuperregister;
  802. adji,adjt:Psuperregisterworklist;
  803. begin
  804. with simplifyworklist do
  805. begin
  806. if length<2 then
  807. exit;
  808. p:=1;
  809. while 2*p<length do
  810. p:=2*p;
  811. while p<>0 do
  812. begin
  813. for h:=p to length-1 do
  814. begin
  815. i:=h;
  816. t:=buf^[i];
  817. adjt:=reginfo[buf^[i]].adjlist;
  818. lent:=0;
  819. if adjt<>nil then
  820. lent:=adjt^.length;
  821. repeat
  822. adji:=reginfo[buf^[i-p]].adjlist;
  823. leni:=0;
  824. if adji<>nil then
  825. leni:=adji^.length;
  826. if leni<=lent then
  827. break;
  828. buf^[i]:=buf^[i-p];
  829. dec(i,p)
  830. until i<p;
  831. buf^[i]:=t;
  832. end;
  833. p:=p shr 1;
  834. end;
  835. end;
  836. end;
  837. { sort spilled nodes by increasing number of interferences }
  838. procedure Trgobj.sort_spillednodes;
  839. var
  840. p,h,i,leni,lent:longword;
  841. t:Tsuperregister;
  842. adji,adjt:Psuperregisterworklist;
  843. begin
  844. with spillednodes do
  845. begin
  846. if length<2 then
  847. exit;
  848. p:=1;
  849. while 2*p<length do
  850. p:=2*p;
  851. while p<>0 do
  852. begin
  853. for h:=p to length-1 do
  854. begin
  855. i:=h;
  856. t:=buf^[i];
  857. adjt:=reginfo[buf^[i]].adjlist;
  858. lent:=0;
  859. if adjt<>nil then
  860. lent:=adjt^.length;
  861. repeat
  862. adji:=reginfo[buf^[i-p]].adjlist;
  863. leni:=0;
  864. if adji<>nil then
  865. leni:=adji^.length;
  866. if leni<=lent then
  867. break;
  868. buf^[i]:=buf^[i-p];
  869. dec(i,p)
  870. until i<p;
  871. buf^[i]:=t;
  872. end;
  873. p:=p shr 1;
  874. end;
  875. end;
  876. end;
  877. procedure trgobj.make_work_list;
  878. var n:cardinal;
  879. begin
  880. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  881. assign it to any of the registers, thus it is significant.}
  882. for n:=first_imaginary to maxreg-1 do
  883. with reginfo[n] do
  884. begin
  885. if adjlist=nil then
  886. degree:=0
  887. else
  888. degree:=adjlist^.length;
  889. if degree>=usable_registers_cnt then
  890. spillworklist.add(n)
  891. else if move_related(n) then
  892. freezeworklist.add(n)
  893. else if not(ri_coalesced in flags) then
  894. simplifyworklist.add(n);
  895. end;
  896. sort_simplify_worklist;
  897. end;
  898. procedure trgobj.prepare_colouring;
  899. begin
  900. make_work_list;
  901. active_moves:=Tlinkedlist.create;
  902. frozen_moves:=Tlinkedlist.create;
  903. coalesced_moves:=Tlinkedlist.create;
  904. constrained_moves:=Tlinkedlist.create;
  905. selectstack.clear;
  906. end;
  907. procedure trgobj.enable_moves(n:Tsuperregister);
  908. var m:Tlinkedlistitem;
  909. i:cardinal;
  910. begin
  911. with reginfo[n] do
  912. if movelist<>nil then
  913. for i:=0 to movelist^.header.count-1 do
  914. begin
  915. m:=movelist^.data[i];
  916. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  917. if Tmoveins(m).moveset=ms_active_moves then
  918. begin
  919. {Move m from the set active_moves to the set worklist_moves.}
  920. active_moves.remove(m);
  921. Tmoveins(m).moveset:=ms_worklist_moves;
  922. worklist_moves.concat(m);
  923. end;
  924. end;
  925. end;
  926. procedure Trgobj.decrement_degree(m:Tsuperregister);
  927. var adj : Psuperregisterworklist;
  928. n : tsuperregister;
  929. d,i : cardinal;
  930. begin
  931. with reginfo[m] do
  932. begin
  933. d:=degree;
  934. if d=0 then
  935. internalerror(200312151);
  936. dec(degree);
  937. if d=usable_registers_cnt then
  938. begin
  939. {Enable moves for m.}
  940. enable_moves(m);
  941. {Enable moves for adjacent.}
  942. adj:=adjlist;
  943. if adj<>nil then
  944. for i:=1 to adj^.length do
  945. begin
  946. n:=adj^.buf^[i-1];
  947. if reginfo[n].flags*[ri_selected,ri_coalesced]<>[] then
  948. enable_moves(n);
  949. end;
  950. {Remove the node from the spillworklist.}
  951. if not spillworklist.delete(m) then
  952. internalerror(200310145);
  953. if move_related(m) then
  954. freezeworklist.add(m)
  955. else
  956. simplifyworklist.add(m);
  957. end;
  958. end;
  959. end;
  960. procedure trgobj.simplify;
  961. var adj : Psuperregisterworklist;
  962. m,n : Tsuperregister;
  963. i : cardinal;
  964. begin
  965. {We take the element with the least interferences out of the
  966. simplifyworklist. Since the simplifyworklist is now sorted, we
  967. no longer need to search, but we can simply take the first element.}
  968. m:=simplifyworklist.get;
  969. {Push it on the selectstack.}
  970. selectstack.add(m);
  971. with reginfo[m] do
  972. begin
  973. include(flags,ri_selected);
  974. adj:=adjlist;
  975. end;
  976. if adj<>nil then
  977. for i:=1 to adj^.length do
  978. begin
  979. n:=adj^.buf^[i-1];
  980. if (n>=first_imaginary) and
  981. (reginfo[n].flags*[ri_selected,ri_coalesced]=[]) then
  982. decrement_degree(n);
  983. end;
  984. end;
  985. function trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  986. begin
  987. while ri_coalesced in reginfo[n].flags do
  988. n:=reginfo[n].alias;
  989. get_alias:=n;
  990. end;
  991. procedure trgobj.add_worklist(u:Tsuperregister);
  992. begin
  993. if (u>=first_imaginary) and
  994. (not move_related(u)) and
  995. (reginfo[u].degree<usable_registers_cnt) then
  996. begin
  997. if not freezeworklist.delete(u) then
  998. internalerror(200308161); {must be found}
  999. simplifyworklist.add(u);
  1000. end;
  1001. end;
  1002. function trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  1003. {Check wether u and v should be coalesced. u is precoloured.}
  1004. function ok(t,r:Tsuperregister):boolean;
  1005. begin
  1006. ok:=(t<first_imaginary) or
  1007. // disabled for now, see issue #22405
  1008. // ((r<first_imaginary) and (r in usable_register_set)) or
  1009. (reginfo[t].degree<usable_registers_cnt) or
  1010. ibitmap[r,t];
  1011. end;
  1012. var adj : Psuperregisterworklist;
  1013. i : cardinal;
  1014. n : tsuperregister;
  1015. begin
  1016. with reginfo[v] do
  1017. begin
  1018. adjacent_ok:=true;
  1019. adj:=adjlist;
  1020. if adj<>nil then
  1021. for i:=1 to adj^.length do
  1022. begin
  1023. n:=adj^.buf^[i-1];
  1024. if (flags*[ri_coalesced,ri_selected]=[]) and not ok(n,u) then
  1025. begin
  1026. adjacent_ok:=false;
  1027. break;
  1028. end;
  1029. end;
  1030. end;
  1031. end;
  1032. function trgobj.conservative(u,v:Tsuperregister):boolean;
  1033. var adj : Psuperregisterworklist;
  1034. done : Tsuperregisterset; {To prevent that we count nodes twice.}
  1035. i,k:cardinal;
  1036. n : tsuperregister;
  1037. begin
  1038. k:=0;
  1039. supregset_reset(done,false,maxreg);
  1040. with reginfo[u] do
  1041. begin
  1042. adj:=adjlist;
  1043. if adj<>nil then
  1044. for i:=1 to adj^.length do
  1045. begin
  1046. n:=adj^.buf^[i-1];
  1047. if reginfo[n].flags*[ri_coalesced,ri_selected]=[] then
  1048. begin
  1049. supregset_include(done,n);
  1050. if reginfo[n].degree>=usable_registers_cnt then
  1051. inc(k);
  1052. end;
  1053. end;
  1054. end;
  1055. adj:=reginfo[v].adjlist;
  1056. if adj<>nil then
  1057. for i:=1 to adj^.length do
  1058. begin
  1059. n:=adj^.buf^[i-1];
  1060. if not supregset_in(done,n) and
  1061. (reginfo[n].degree>=usable_registers_cnt) and
  1062. (reginfo[n].flags*[ri_coalesced,ri_selected]=[]) then
  1063. inc(k);
  1064. end;
  1065. conservative:=(k<usable_registers_cnt);
  1066. end;
  1067. procedure trgobj.set_alias(u,v:Tsuperregister);
  1068. begin
  1069. { don't make registers that the register allocator shouldn't touch (such
  1070. as stack and frame pointers) be aliases for other registers, because
  1071. then it can propagate them and even start changing them if the aliased
  1072. register gets changed }
  1073. if ((u<first_imaginary) and
  1074. not(u in usable_register_set)) or
  1075. ((v<first_imaginary) and
  1076. not(v in usable_register_set)) then
  1077. exit;
  1078. include(reginfo[v].flags,ri_coalesced);
  1079. if reginfo[v].alias<>0 then
  1080. internalerror(200712291);
  1081. reginfo[v].alias:=get_alias(u);
  1082. coalescednodes.add(v);
  1083. end;
  1084. procedure trgobj.combine(u,v:Tsuperregister);
  1085. var adj : Psuperregisterworklist;
  1086. i,n,p,q:cardinal;
  1087. t : tsuperregister;
  1088. searched:Tlinkedlistitem;
  1089. found : boolean;
  1090. begin
  1091. if not freezeworklist.delete(v) then
  1092. spillworklist.delete(v);
  1093. coalescednodes.add(v);
  1094. include(reginfo[v].flags,ri_coalesced);
  1095. reginfo[v].alias:=u;
  1096. {Combine both movelists. Since the movelists are sets, only add
  1097. elements that are not already present. The movelists cannot be
  1098. empty by definition; nodes are only coalesced if there is a move
  1099. between them. To prevent quadratic time blowup (movelists of
  1100. especially machine registers can get very large because of moves
  1101. generated during calls) we need to go into disgusting complexity.
  1102. (See webtbs/tw2242 for an example that stresses this.)
  1103. We want to sort the movelist to be able to search logarithmically.
  1104. Unfortunately, sorting the movelist every time before searching
  1105. is counter-productive, since the movelist usually grows with a few
  1106. items at a time. Therefore, we split the movelist into a sorted
  1107. and an unsorted part and search through both. If the unsorted part
  1108. becomes too large, we sort.}
  1109. if assigned(reginfo[u].movelist) then
  1110. begin
  1111. {We have to weigh the cost of sorting the list against searching
  1112. the cost of the unsorted part. I use factor of 8 here; if the
  1113. number of items is less than 8 times the numer of unsorted items,
  1114. we'll sort the list.}
  1115. with reginfo[u].movelist^ do
  1116. if header.count<8*(header.count-header.sorted_until) then
  1117. sort_movelist(reginfo[u].movelist);
  1118. if assigned(reginfo[v].movelist) then
  1119. begin
  1120. for n:=0 to reginfo[v].movelist^.header.count-1 do
  1121. begin
  1122. {Binary search the sorted part of the list.}
  1123. searched:=reginfo[v].movelist^.data[n];
  1124. p:=0;
  1125. q:=reginfo[u].movelist^.header.sorted_until;
  1126. i:=0;
  1127. if q<>0 then
  1128. repeat
  1129. i:=(p+q) shr 1;
  1130. if ptruint(searched)>ptruint(reginfo[u].movelist^.data[i]) then
  1131. p:=i+1
  1132. else
  1133. q:=i;
  1134. until p=q;
  1135. with reginfo[u].movelist^ do
  1136. if searched<>data[i] then
  1137. begin
  1138. {Linear search the unsorted part of the list.}
  1139. found:=false;
  1140. for i:=header.sorted_until+1 to header.count-1 do
  1141. if searched=data[i] then
  1142. begin
  1143. found:=true;
  1144. break;
  1145. end;
  1146. if not found then
  1147. add_to_movelist(u,searched);
  1148. end;
  1149. end;
  1150. end;
  1151. end;
  1152. enable_moves(v);
  1153. adj:=reginfo[v].adjlist;
  1154. if adj<>nil then
  1155. for i:=1 to adj^.length do
  1156. begin
  1157. t:=adj^.buf^[i-1];
  1158. with reginfo[t] do
  1159. if not(ri_coalesced in flags) then
  1160. begin
  1161. {t has a connection to v. Since we are adding v to u, we
  1162. need to connect t to u. However, beware if t was already
  1163. connected to u...}
  1164. if (ibitmap[t,u]) and not (ri_selected in flags) then
  1165. {... because in that case, we are actually removing an edge
  1166. and the degree of t decreases.}
  1167. decrement_degree(t)
  1168. else
  1169. begin
  1170. add_edge(t,u);
  1171. {We have added an edge to t and u. So their degree increases.
  1172. However, v is added to u. That means its neighbours will
  1173. no longer point to v, but to u instead. Therefore, only the
  1174. degree of u increases.}
  1175. if (u>=first_imaginary) and not (ri_selected in flags) then
  1176. inc(reginfo[u].degree);
  1177. end;
  1178. end;
  1179. end;
  1180. if (reginfo[u].degree>=usable_registers_cnt) and freezeworklist.delete(u) then
  1181. spillworklist.add(u);
  1182. end;
  1183. procedure trgobj.coalesce;
  1184. var m:Tmoveins;
  1185. x,y,u,v:cardinal;
  1186. begin
  1187. m:=Tmoveins(worklist_moves.getfirst);
  1188. x:=get_alias(m.x);
  1189. y:=get_alias(m.y);
  1190. if (y<first_imaginary) then
  1191. begin
  1192. u:=y;
  1193. v:=x;
  1194. end
  1195. else
  1196. begin
  1197. u:=x;
  1198. v:=y;
  1199. end;
  1200. if (u=v) then
  1201. begin
  1202. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1203. coalesced_moves.insert(m);
  1204. add_worklist(u);
  1205. end
  1206. {Do u and v interfere? In that case the move is constrained. Two
  1207. precoloured nodes interfere allways. If v is precoloured, by the above
  1208. code u is precoloured, thus interference...}
  1209. else if (v<first_imaginary) or ibitmap[u,v] then
  1210. begin
  1211. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1212. constrained_moves.insert(m);
  1213. add_worklist(u);
  1214. add_worklist(v);
  1215. end
  1216. {Next test: is it possible and a good idea to coalesce?? Note: don't
  1217. coalesce registers that should not be touched by the register allocator,
  1218. such as stack/framepointers, because otherwise they can be changed }
  1219. else if (((u<first_imaginary) and adjacent_ok(u,v)) or
  1220. conservative(u,v)) and
  1221. ((u>first_imaginary) or
  1222. (u in usable_register_set)) and
  1223. ((v>first_imaginary) or
  1224. (v in usable_register_set)) then
  1225. begin
  1226. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1227. coalesced_moves.insert(m);
  1228. combine(u,v);
  1229. add_worklist(u);
  1230. end
  1231. else
  1232. begin
  1233. m.moveset:=ms_active_moves;
  1234. active_moves.insert(m);
  1235. end;
  1236. end;
  1237. procedure trgobj.freeze_moves(u:Tsuperregister);
  1238. var i:cardinal;
  1239. m:Tlinkedlistitem;
  1240. v,x,y:Tsuperregister;
  1241. begin
  1242. if reginfo[u].movelist<>nil then
  1243. for i:=0 to reginfo[u].movelist^.header.count-1 do
  1244. begin
  1245. m:=reginfo[u].movelist^.data[i];
  1246. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1247. begin
  1248. x:=Tmoveins(m).x;
  1249. y:=Tmoveins(m).y;
  1250. if get_alias(y)=get_alias(u) then
  1251. v:=get_alias(x)
  1252. else
  1253. v:=get_alias(y);
  1254. {Move m from active_moves/worklist_moves to frozen_moves.}
  1255. if Tmoveins(m).moveset=ms_active_moves then
  1256. active_moves.remove(m)
  1257. else
  1258. worklist_moves.remove(m);
  1259. Tmoveins(m).moveset:=ms_frozen_moves;
  1260. frozen_moves.insert(m);
  1261. if (v>=first_imaginary) and not(move_related(v)) and
  1262. (reginfo[v].degree<usable_registers_cnt) then
  1263. begin
  1264. freezeworklist.delete(v);
  1265. simplifyworklist.add(v);
  1266. end;
  1267. end;
  1268. end;
  1269. end;
  1270. procedure trgobj.freeze;
  1271. var n:Tsuperregister;
  1272. begin
  1273. { We need to take a random element out of the freezeworklist. We take
  1274. the last element. Dirty code! }
  1275. n:=freezeworklist.get;
  1276. {Add it to the simplifyworklist.}
  1277. simplifyworklist.add(n);
  1278. freeze_moves(n);
  1279. end;
  1280. { The spilling approach selected by SPILLING_NEW does not work well for AVR as it eploits apparently the problem of the current
  1281. reg. allocator with AVR. The current reg. allocator is not aware of the fact that r1-r15 and r16-r31 are not equal on AVR }
  1282. {$if defined(AVR)}
  1283. {$define SPILLING_OLD}
  1284. {$else defined(AVR)}
  1285. { $define SPILLING_NEW}
  1286. {$endif defined(AVR)}
  1287. {$ifndef SPILLING_NEW}
  1288. {$define SPILLING_OLD}
  1289. {$endif SPILLING_NEW}
  1290. procedure trgobj.select_spill;
  1291. var
  1292. n : tsuperregister;
  1293. adj : psuperregisterworklist;
  1294. maxlength,p,i :word;
  1295. minweight: longint;
  1296. {$ifdef SPILLING_NEW}
  1297. dist: Double;
  1298. {$endif}
  1299. begin
  1300. {$ifdef SPILLING_NEW}
  1301. { This new approach for selecting the next spill candidate takes care of the weight of a register:
  1302. It spills the register with the lowest weight but only if it is expected that it results in convergence of
  1303. register allocation. Convergence is expected if a register is spilled where the average of the active interferences
  1304. - active interference means that the register is used in an instruction - is lower than
  1305. the degree.
  1306. Example (modify means read and the write):
  1307. modify reg1
  1308. loop:
  1309. modify reg2
  1310. modify reg3
  1311. modify reg4
  1312. modify reg5
  1313. modify reg6
  1314. modify reg7
  1315. modify reg1
  1316. In this example, all register have the same degree. However, spilling reg1 is most benefical as it is used least. Furthermore,
  1317. spilling reg1 is a step toward solving the coloring problem as the registers used during spilling will have a lower degree
  1318. as no register are in use at the location where reg1 is spilled.
  1319. }
  1320. minweight:=high(longint);
  1321. p:=0;
  1322. with spillworklist do
  1323. begin
  1324. { Safe: This procedure is only called if length<>0 }
  1325. for i:=0 to length-1 do
  1326. begin
  1327. adj:=reginfo[buf^[i]].adjlist;
  1328. dist:=adj^.length-reginfo[buf^[i]].total_interferences/reginfo[buf^[i]].count_uses;
  1329. if assigned(adj) and
  1330. (reginfo[buf^[i]].weight<minweight) and
  1331. (dist>=1) and
  1332. (reginfo[buf^[i]].weight>0) then
  1333. begin
  1334. p:=i;
  1335. minweight:=reginfo[buf^[i]].weight;
  1336. end;
  1337. end;
  1338. n:=buf^[p];
  1339. deleteidx(p);
  1340. end;
  1341. {$endif SPILLING_NEW}
  1342. {$ifdef SPILLING_OLD}
  1343. { We must look for the element with the most interferences in the
  1344. spillworklist. This is required because those registers are creating
  1345. the most conflicts and keeping them in a register will not reduce the
  1346. complexity and even can cause the help registers for the spilling code
  1347. to get too much conflicts with the result that the spilling code
  1348. will never converge (PFV) }
  1349. maxlength:=0;
  1350. minweight:=high(longint);
  1351. p:=0;
  1352. with spillworklist do
  1353. begin
  1354. {Safe: This procedure is only called if length<>0}
  1355. for i:=0 to length-1 do
  1356. begin
  1357. adj:=reginfo[buf^[i]].adjlist;
  1358. if assigned(adj) and
  1359. (
  1360. (adj^.length>maxlength) or
  1361. ((adj^.length=maxlength) and (reginfo[buf^[i]].weight<minweight))
  1362. ) then
  1363. begin
  1364. p:=i;
  1365. maxlength:=adj^.length;
  1366. minweight:=reginfo[buf^[i]].weight;
  1367. end;
  1368. end;
  1369. n:=buf^[p];
  1370. deleteidx(p);
  1371. end;
  1372. {$endif SPILLING_OLD}
  1373. simplifyworklist.add(n);
  1374. freeze_moves(n);
  1375. end;
  1376. procedure trgobj.assign_colours;
  1377. {Assign_colours assigns the actual colours to the registers.}
  1378. var adj : Psuperregisterworklist;
  1379. i,j,k : cardinal;
  1380. n,a,c : Tsuperregister;
  1381. colourednodes : Tsuperregisterset;
  1382. adj_colours:set of 0..255;
  1383. found : boolean;
  1384. tmpr: tregister;
  1385. begin
  1386. spillednodes.clear;
  1387. {Reset colours}
  1388. for n:=0 to maxreg-1 do
  1389. reginfo[n].colour:=n;
  1390. {Colour the cpu registers...}
  1391. supregset_reset(colourednodes,false,maxreg);
  1392. for n:=0 to first_imaginary-1 do
  1393. supregset_include(colourednodes,n);
  1394. {Now colour the imaginary registers on the select-stack.}
  1395. for i:=selectstack.length downto 1 do
  1396. begin
  1397. n:=selectstack.buf^[i-1];
  1398. {Create a list of colours that we cannot assign to n.}
  1399. adj_colours:=[];
  1400. adj:=reginfo[n].adjlist;
  1401. if adj<>nil then
  1402. for j:=0 to adj^.length-1 do
  1403. begin
  1404. a:=get_alias(adj^.buf^[j]);
  1405. if supregset_in(colourednodes,a) and (reginfo[a].colour<=255) then
  1406. include(adj_colours,reginfo[a].colour);
  1407. end;
  1408. { FIXME: temp variable r is needed here to avoid Internal error 20060521 }
  1409. { while compiling the compiler. }
  1410. tmpr:=NR_STACK_POINTER_REG;
  1411. if regtype=getregtype(tmpr) then
  1412. include(adj_colours,RS_STACK_POINTER_REG);
  1413. {Assume a spill by default...}
  1414. found:=false;
  1415. {Search for a colour not in this list.}
  1416. for k:=0 to usable_registers_cnt-1 do
  1417. begin
  1418. c:=usable_registers[k];
  1419. if not(c in adj_colours) then
  1420. begin
  1421. reginfo[n].colour:=c;
  1422. found:=true;
  1423. supregset_include(colourednodes,n);
  1424. break;
  1425. end;
  1426. end;
  1427. if not found then
  1428. spillednodes.add(n);
  1429. end;
  1430. {Finally colour the nodes that were coalesced.}
  1431. for i:=1 to coalescednodes.length do
  1432. begin
  1433. n:=coalescednodes.buf^[i-1];
  1434. k:=get_alias(n);
  1435. reginfo[n].colour:=reginfo[k].colour;
  1436. end;
  1437. end;
  1438. procedure trgobj.colour_registers;
  1439. begin
  1440. repeat
  1441. if simplifyworklist.length<>0 then
  1442. simplify
  1443. else if not(worklist_moves.empty) then
  1444. coalesce
  1445. else if freezeworklist.length<>0 then
  1446. freeze
  1447. else if spillworklist.length<>0 then
  1448. select_spill;
  1449. until (simplifyworklist.length=0) and
  1450. worklist_moves.empty and
  1451. (freezeworklist.length=0) and
  1452. (spillworklist.length=0);
  1453. assign_colours;
  1454. end;
  1455. procedure trgobj.epilogue_colouring;
  1456. begin
  1457. { remove all items from the worklists, but do not free them, they are still needed for spill coalesce }
  1458. move_garbage.concatList(worklist_moves);
  1459. move_garbage.concatList(active_moves);
  1460. active_moves.Free;
  1461. active_moves:=nil;
  1462. move_garbage.concatList(frozen_moves);
  1463. frozen_moves.Free;
  1464. frozen_moves:=nil;
  1465. move_garbage.concatList(coalesced_moves);
  1466. coalesced_moves.Free;
  1467. coalesced_moves:=nil;
  1468. move_garbage.concatList(constrained_moves);
  1469. constrained_moves.Free;
  1470. constrained_moves:=nil;
  1471. end;
  1472. procedure trgobj.clear_interferences(u:Tsuperregister);
  1473. {Remove node u from the interference graph and remove all collected
  1474. move instructions it is associated with.}
  1475. var i : word;
  1476. v : Tsuperregister;
  1477. adj,adj2 : Psuperregisterworklist;
  1478. begin
  1479. adj:=reginfo[u].adjlist;
  1480. if adj<>nil then
  1481. begin
  1482. for i:=1 to adj^.length do
  1483. begin
  1484. v:=adj^.buf^[i-1];
  1485. {Remove (u,v) and (v,u) from bitmap.}
  1486. ibitmap[u,v]:=false;
  1487. ibitmap[v,u]:=false;
  1488. {Remove (v,u) from adjacency list.}
  1489. adj2:=reginfo[v].adjlist;
  1490. if adj2<>nil then
  1491. begin
  1492. adj2^.delete(u);
  1493. if adj2^.length=0 then
  1494. begin
  1495. dispose(adj2,done);
  1496. reginfo[v].adjlist:=nil;
  1497. end;
  1498. end;
  1499. end;
  1500. {Remove ( u,* ) from adjacency list.}
  1501. dispose(adj,done);
  1502. reginfo[u].adjlist:=nil;
  1503. end;
  1504. end;
  1505. function trgobj.getregisterinline(list:TAsmList;const subregconstraints:Tsubregisterset):Tregister;
  1506. var
  1507. p : Tsuperregister;
  1508. subreg: tsubregister;
  1509. begin
  1510. for subreg:=high(tsubregister) downto low(tsubregister) do
  1511. if subreg in subregconstraints then
  1512. break;
  1513. p:=getnewreg(subreg);
  1514. live_registers.add(p);
  1515. result:=newreg(regtype,p,subreg);
  1516. add_edges_used(p);
  1517. add_constraints(result);
  1518. { also add constraints for other sizes used for this register }
  1519. if subreg<>low(tsubregister) then
  1520. for subreg:=pred(subreg) downto low(tsubregister) do
  1521. if subreg in subregconstraints then
  1522. add_constraints(newreg(regtype,getsupreg(result),subreg));
  1523. end;
  1524. procedure trgobj.ungetregisterinline(list:TAsmList;r:Tregister);
  1525. var
  1526. supreg:Tsuperregister;
  1527. begin
  1528. supreg:=getsupreg(r);
  1529. live_registers.delete(supreg);
  1530. insert_regalloc_info(list,supreg);
  1531. end;
  1532. procedure trgobj.insert_regalloc_info(list:TAsmList;u:tsuperregister);
  1533. var
  1534. p : tai;
  1535. r : tregister;
  1536. palloc,
  1537. pdealloc : tai_regalloc;
  1538. begin
  1539. { Insert regallocs for all imaginary registers }
  1540. with reginfo[u] do
  1541. begin
  1542. r:=newreg(regtype,u,subreg);
  1543. if assigned(live_start) then
  1544. begin
  1545. { Generate regalloc and bind it to an instruction, this
  1546. is needed to find all live registers belonging to an
  1547. instruction during the spilling }
  1548. if live_start.typ=ait_instruction then
  1549. palloc:=tai_regalloc.alloc(r,live_start)
  1550. else
  1551. palloc:=tai_regalloc.alloc(r,nil);
  1552. if live_end.typ=ait_instruction then
  1553. pdealloc:=tai_regalloc.dealloc(r,live_end)
  1554. else
  1555. pdealloc:=tai_regalloc.dealloc(r,nil);
  1556. { Insert live start allocation before the instruction/reg_a_sync }
  1557. list.insertbefore(palloc,live_start);
  1558. { Insert live end deallocation before reg allocations
  1559. to reduce conflicts }
  1560. p:=live_end;
  1561. while assigned(p) and
  1562. assigned(p.previous) and
  1563. (tai(p.previous).typ=ait_regalloc) and
  1564. (tai_regalloc(p.previous).ratype=ra_alloc) and
  1565. (tai_regalloc(p.previous).reg<>r) do
  1566. p:=tai(p.previous);
  1567. { , but add release after a reg_a_sync }
  1568. if assigned(p) and
  1569. (p.typ=ait_regalloc) and
  1570. (tai_regalloc(p).ratype=ra_sync) then
  1571. p:=tai(p.next);
  1572. if assigned(p) then
  1573. list.insertbefore(pdealloc,p)
  1574. else
  1575. list.concat(pdealloc);
  1576. end;
  1577. end;
  1578. end;
  1579. procedure trgobj.insert_regalloc_info_all(list:TAsmList);
  1580. var
  1581. supreg : tsuperregister;
  1582. begin
  1583. { Insert regallocs for all imaginary registers }
  1584. for supreg:=first_imaginary to maxreg-1 do
  1585. insert_regalloc_info(list,supreg);
  1586. end;
  1587. procedure trgobj.determine_spill_registers(list: TAsmList; headertail: tai);
  1588. begin
  1589. prepare_colouring;
  1590. colour_registers;
  1591. epilogue_colouring;
  1592. end;
  1593. procedure trgobj.get_spill_temp(list: TAsmlist; spill_temps: Pspill_temp_list; supreg: tsuperregister);
  1594. var
  1595. size: ptrint;
  1596. begin
  1597. {Get a temp for the spilled register, the size must at least equal a complete register,
  1598. take also care of the fact that subreg can be larger than a single register like doubles
  1599. that occupy 2 registers }
  1600. { only force the whole register in case of integers. Storing a register that contains
  1601. a single precision value as a double can cause conversion errors on e.g. ARM VFP }
  1602. if (regtype=R_INTREGISTER) then
  1603. size:=max(tcgsize2size[reg_cgsize(newreg(regtype,supreg,R_SUBWHOLE))],
  1604. tcgsize2size[reg_cgsize(newreg(regtype,supreg,reginfo[supreg].subreg))])
  1605. else
  1606. size:=tcgsize2size[reg_cgsize(newreg(regtype,supreg,reginfo[supreg].subreg))];
  1607. tg.gettemp(list,
  1608. size,size,
  1609. tt_noreuse,spill_temps^[supreg]);
  1610. end;
  1611. procedure trgobj.add_cpu_interferences(p : tai);
  1612. begin
  1613. end;
  1614. procedure trgobj.generate_interference_graph(list:TAsmList;headertai:tai);
  1615. procedure RecordUse(var r : Treginfo);
  1616. begin
  1617. inc(r.total_interferences,live_registers.length);
  1618. inc(r.count_uses);
  1619. end;
  1620. var
  1621. p : tai;
  1622. i : integer;
  1623. supreg, u: tsuperregister;
  1624. {$ifdef arm}
  1625. so: pshifterop;
  1626. {$endif arm}
  1627. begin
  1628. { All allocations are available. Now we can generate the
  1629. interference graph. Walk through all instructions, we can
  1630. start with the headertai, because before the header tai is
  1631. only symbols. }
  1632. live_registers.clear;
  1633. p:=headertai;
  1634. while assigned(p) do
  1635. begin
  1636. prefetch(pointer(p.next)^);
  1637. case p.typ of
  1638. ait_instruction:
  1639. with Taicpu(p) do
  1640. begin
  1641. current_filepos:=fileinfo;
  1642. {For speed reasons, get_alias isn't used here, instead,
  1643. assign_colours will also set the colour of coalesced nodes.
  1644. If there are registers with colour=0, then the coalescednodes
  1645. list probably doesn't contain these registers, causing
  1646. assign_colours not to do this properly.}
  1647. for i:=0 to ops-1 do
  1648. with oper[i]^ do
  1649. case typ of
  1650. top_reg:
  1651. if (getregtype(reg)=regtype) then
  1652. begin
  1653. u:=getsupreg(reg);
  1654. {$ifdef EXTDEBUG}
  1655. if (u>=maxreginfo) then
  1656. internalerror(2018111701);
  1657. {$endif}
  1658. RecordUse(reginfo[u]);
  1659. end;
  1660. top_ref:
  1661. begin
  1662. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1663. with ref^ do
  1664. begin
  1665. if (base<>NR_NO) and
  1666. (getregtype(base)=regtype) then
  1667. begin
  1668. u:=getsupreg(base);
  1669. {$ifdef EXTDEBUG}
  1670. if (u>=maxreginfo) then
  1671. internalerror(2018111702);
  1672. {$endif}
  1673. RecordUse(reginfo[u]);
  1674. end;
  1675. if (index<>NR_NO) and
  1676. (getregtype(index)=regtype) then
  1677. begin
  1678. u:=getsupreg(index);
  1679. {$ifdef EXTDEBUG}
  1680. if (u>=maxreginfo) then
  1681. internalerror(2018111703);
  1682. {$endif}
  1683. RecordUse(reginfo[u]);
  1684. end;
  1685. {$if defined(x86)}
  1686. if (segment<>NR_NO) and
  1687. (getregtype(segment)=regtype) then
  1688. begin
  1689. u:=getsupreg(segment);
  1690. {$ifdef EXTDEBUG}
  1691. if (u>=maxreginfo) then
  1692. internalerror(2018111704);
  1693. {$endif}
  1694. RecordUse(reginfo[u]);
  1695. end;
  1696. {$endif defined(x86)}
  1697. end;
  1698. end;
  1699. {$ifdef arm}
  1700. Top_shifterop:
  1701. begin
  1702. if regtype=R_INTREGISTER then
  1703. begin
  1704. so:=shifterop;
  1705. if (so^.rs<>NR_NO) and
  1706. (getregtype(so^.rs)=regtype) then
  1707. RecordUse(reginfo[getsupreg(so^.rs)]);
  1708. end;
  1709. end;
  1710. {$endif arm}
  1711. else
  1712. ;
  1713. end;
  1714. end;
  1715. ait_regalloc:
  1716. with Tai_regalloc(p) do
  1717. begin
  1718. if (getregtype(reg)=regtype) then
  1719. begin
  1720. supreg:=getsupreg(reg);
  1721. case ratype of
  1722. ra_alloc :
  1723. begin
  1724. live_registers.add(supreg);
  1725. {$ifdef DEBUG_REGISTERLIFE}
  1726. write(live_registers.length,' ');
  1727. for i:=0 to live_registers.length-1 do
  1728. write(std_regname(newreg(regtype,live_registers.buf^[i],defaultsub)),' ');
  1729. writeln;
  1730. {$endif DEBUG_REGISTERLIFE}
  1731. add_edges_used(supreg);
  1732. end;
  1733. ra_dealloc :
  1734. begin
  1735. live_registers.delete(supreg);
  1736. {$ifdef DEBUG_REGISTERLIFE}
  1737. write(live_registers.length,' ');
  1738. for i:=0 to live_registers.length-1 do
  1739. write(std_regname(newreg(regtype,live_registers.buf^[i],defaultsub)),' ');
  1740. writeln;
  1741. {$endif DEBUG_REGISTERLIFE}
  1742. add_edges_used(supreg);
  1743. end;
  1744. ra_markused :
  1745. if (supreg<first_imaginary) then
  1746. begin
  1747. include(used_in_proc,supreg);
  1748. has_usedmarks:=true;
  1749. end;
  1750. else
  1751. ;
  1752. end;
  1753. { constraints needs always to be updated }
  1754. add_constraints(reg);
  1755. end;
  1756. end;
  1757. else
  1758. ;
  1759. end;
  1760. add_cpu_interferences(p);
  1761. p:=Tai(p.next);
  1762. end;
  1763. {$ifdef EXTDEBUG}
  1764. if live_registers.length>0 then
  1765. begin
  1766. for i:=0 to live_registers.length-1 do
  1767. begin
  1768. { Only report for imaginary registers }
  1769. if live_registers.buf^[i]>=first_imaginary then
  1770. Comment(V_Warning,'Register '+std_regname(newreg(regtype,live_registers.buf^[i],defaultsub))+' not released');
  1771. end;
  1772. end;
  1773. {$endif}
  1774. end;
  1775. procedure trgobj.translate_register(var reg : tregister);
  1776. begin
  1777. if (getregtype(reg)=regtype) then
  1778. setsupreg(reg,reginfo[getsupreg(reg)].colour)
  1779. else
  1780. internalerror(200602021);
  1781. end;
  1782. procedure Trgobj.translate_registers(list:TAsmList);
  1783. var
  1784. hp,p,q:Tai;
  1785. i:shortint;
  1786. u:longint;
  1787. {$ifdef arm}
  1788. so:pshifterop;
  1789. {$endif arm}
  1790. begin
  1791. { Leave when no imaginary registers are used }
  1792. if maxreg<=first_imaginary then
  1793. exit;
  1794. p:=Tai(list.first);
  1795. while assigned(p) do
  1796. begin
  1797. prefetch(pointer(p.next)^);
  1798. case p.typ of
  1799. ait_regalloc:
  1800. with Tai_regalloc(p) do
  1801. begin
  1802. if (getregtype(reg)=regtype) then
  1803. begin
  1804. { Only alloc/dealloc is needed for the optimizer, remove
  1805. other regalloc }
  1806. if not(ratype in [ra_alloc,ra_dealloc]) then
  1807. begin
  1808. q:=Tai(next);
  1809. list.remove(p);
  1810. p.free;
  1811. p:=q;
  1812. continue;
  1813. end
  1814. else
  1815. begin
  1816. u:=reginfo[getsupreg(reg)].colour;
  1817. include(used_in_proc,u);
  1818. {$ifdef EXTDEBUG}
  1819. if u>=maxreginfo then
  1820. internalerror(2015040501);
  1821. {$endif}
  1822. setsupreg(reg,u);
  1823. end;
  1824. end;
  1825. end;
  1826. ait_varloc:
  1827. begin
  1828. if (getregtype(tai_varloc(p).newlocation)=regtype) then
  1829. begin
  1830. if (cs_asm_source in current_settings.globalswitches) then
  1831. begin
  1832. setsupreg(tai_varloc(p).newlocation,reginfo[getsupreg(tai_varloc(p).newlocation)].colour);
  1833. if tai_varloc(p).newlocationhi<>NR_NO then
  1834. begin
  1835. setsupreg(tai_varloc(p).newlocationhi,reginfo[getsupreg(tai_varloc(p).newlocationhi)].colour);
  1836. hp:=Tai_comment.Create(strpnew('Var '+tai_varloc(p).varsym.realname+' located in register '+
  1837. std_regname(tai_varloc(p).newlocationhi)+':'+std_regname(tai_varloc(p).newlocation)));
  1838. end
  1839. else
  1840. hp:=Tai_comment.Create(strpnew('Var '+tai_varloc(p).varsym.realname+' located in register '+
  1841. std_regname(tai_varloc(p).newlocation)));
  1842. list.insertafter(hp,p);
  1843. end;
  1844. q:=tai(p.next);
  1845. list.remove(p);
  1846. p.free;
  1847. p:=q;
  1848. continue;
  1849. end;
  1850. end;
  1851. ait_instruction:
  1852. with Taicpu(p) do
  1853. begin
  1854. current_filepos:=fileinfo;
  1855. {For speed reasons, get_alias isn't used here, instead,
  1856. assign_colours will also set the colour of coalesced nodes.
  1857. If there are registers with colour=0, then the coalescednodes
  1858. list probably doesn't contain these registers, causing
  1859. assign_colours not to do this properly.}
  1860. for i:=0 to ops-1 do
  1861. with oper[i]^ do
  1862. case typ of
  1863. Top_reg:
  1864. if (getregtype(reg)=regtype) then
  1865. begin
  1866. u:=getsupreg(reg);
  1867. {$ifdef EXTDEBUG}
  1868. if (u>=maxreginfo) then
  1869. internalerror(2012101903);
  1870. {$endif}
  1871. setsupreg(reg,reginfo[u].colour);
  1872. end;
  1873. Top_ref:
  1874. begin
  1875. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1876. with ref^ do
  1877. begin
  1878. if (base<>NR_NO) and
  1879. (getregtype(base)=regtype) then
  1880. begin
  1881. u:=getsupreg(base);
  1882. {$ifdef EXTDEBUG}
  1883. if (u>=maxreginfo) then
  1884. internalerror(2012101904);
  1885. {$endif}
  1886. setsupreg(base,reginfo[u].colour);
  1887. end;
  1888. if (index<>NR_NO) and
  1889. (getregtype(index)=regtype) then
  1890. begin
  1891. u:=getsupreg(index);
  1892. {$ifdef EXTDEBUG}
  1893. if (u>=maxreginfo) then
  1894. internalerror(2012101905);
  1895. {$endif}
  1896. setsupreg(index,reginfo[u].colour);
  1897. end;
  1898. {$if defined(x86)}
  1899. if (segment<>NR_NO) and
  1900. (getregtype(segment)=regtype) then
  1901. begin
  1902. u:=getsupreg(segment);
  1903. {$ifdef EXTDEBUG}
  1904. if (u>=maxreginfo) then
  1905. internalerror(2013052401);
  1906. {$endif}
  1907. setsupreg(segment,reginfo[u].colour);
  1908. end;
  1909. {$endif defined(x86)}
  1910. end;
  1911. end;
  1912. {$ifdef arm}
  1913. Top_shifterop:
  1914. begin
  1915. if regtype=R_INTREGISTER then
  1916. begin
  1917. so:=shifterop;
  1918. if (so^.rs<>NR_NO) and
  1919. (getregtype(so^.rs)=regtype) then
  1920. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1921. end;
  1922. end;
  1923. {$endif arm}
  1924. else
  1925. ;
  1926. end;
  1927. { Maybe the operation can be removed when
  1928. it is a move and both arguments are the same }
  1929. if is_same_reg_move(regtype) then
  1930. begin
  1931. q:=Tai(p.next);
  1932. list.remove(p);
  1933. p.free;
  1934. p:=q;
  1935. continue;
  1936. end;
  1937. end;
  1938. else
  1939. ;
  1940. end;
  1941. p:=Tai(p.next);
  1942. end;
  1943. current_filepos:=current_procinfo.exitpos;
  1944. end;
  1945. function trgobj.spill_registers(list:TAsmList;headertai:tai):boolean;
  1946. { Returns true if any help registers have been used }
  1947. var
  1948. i : cardinal;
  1949. t : tsuperregister;
  1950. p,q : Tai;
  1951. regs_to_spill_set:Tsuperregisterset;
  1952. spill_temps : ^Tspill_temp_list;
  1953. supreg,x,y : tsuperregister;
  1954. templist : TAsmList;
  1955. j : Longint;
  1956. getnewspillloc : Boolean;
  1957. begin
  1958. spill_registers:=false;
  1959. live_registers.clear;
  1960. { spilling should start with the node with the highest number of interferences, so we can coalesce as
  1961. much as possible spilled nodes (coalesce in case of spilled node means they share the same memory location) }
  1962. sort_spillednodes;
  1963. for i:=first_imaginary to maxreg-1 do
  1964. exclude(reginfo[i].flags,ri_selected);
  1965. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1966. supregset_reset(regs_to_spill_set,false,$ffff);
  1967. {$ifdef DEBUG_SPILLCOALESCE}
  1968. writeln('trgobj.spill_registers: Got maxreg ',maxreg);
  1969. writeln('trgobj.spill_registers: Spilling ',spillednodes.length,' nodes');
  1970. {$endif DEBUG_SPILLCOALESCE}
  1971. { after each round of spilling, more registers could be used due to allocations for spilling }
  1972. if Length(spillinfo)<maxreg then
  1973. begin
  1974. j:=Length(spillinfo);
  1975. SetLength(spillinfo,maxreg);
  1976. fillchar(spillinfo[j],sizeof(spillinfo[0])*(Length(spillinfo)-j),0);
  1977. end;
  1978. { Allocate temps and insert in front of the list }
  1979. templist:=TAsmList.create;
  1980. { Safe: this procedure is only called if there are spilled nodes. }
  1981. with spillednodes do
  1982. { the node with the highest interferences is the last one }
  1983. for i:=length-1 downto 0 do
  1984. begin
  1985. t:=buf^[i];
  1986. {$ifdef DEBUG_SPILLCOALESCE}
  1987. writeln('trgobj.spill_registers: Spilling ',t);
  1988. {$endif DEBUG_SPILLCOALESCE}
  1989. spillinfo[t].interferences:=Tinterferencebitmap.create;
  1990. { copy interferences }
  1991. for j:=0 to maxreg-1 do
  1992. spillinfo[t].interferences[0,j]:=ibitmap[t,j];
  1993. { Alternative representation. }
  1994. supregset_include(regs_to_spill_set,t);
  1995. { Clear all interferences of the spilled register. }
  1996. clear_interferences(t);
  1997. getnewspillloc:=true;
  1998. { check if we can "coalesce" spilled nodes. To do so, it is required that they do not
  1999. interfere but are connected by a move instruction
  2000. doing so might save some mem->mem moves }
  2001. if (cs_opt_level3 in current_settings.optimizerswitches) and assigned(reginfo[t].movelist) then
  2002. for j:=0 to reginfo[t].movelist^.header.count-1 do
  2003. begin
  2004. x:=Tmoveins(reginfo[t].movelist^.data[j]).x;
  2005. y:=Tmoveins(reginfo[t].movelist^.data[j]).y;
  2006. if (x=t) and
  2007. (spillinfo[get_alias(y)].spilled) and
  2008. not(spillinfo[get_alias(y)].interferences[0,t]) then
  2009. begin
  2010. spill_temps^[t]:=spillinfo[get_alias(y)].spilllocation;
  2011. {$ifdef DEBUG_SPILLCOALESCE}
  2012. writeln('trgobj.spill_registers: Spill coalesce ',t,' to ',y);
  2013. {$endif DEBUG_SPILLCOALESCE}
  2014. getnewspillloc:=false;
  2015. break;
  2016. end
  2017. else if (y=t) and
  2018. (spillinfo[get_alias(x)].spilled) and
  2019. not(spillinfo[get_alias(x)].interferences[0,t]) then
  2020. begin
  2021. {$ifdef DEBUG_SPILLCOALESCE}
  2022. writeln('trgobj.spill_registers: Spill coalesce ',t,' to ',x);
  2023. {$endif DEBUG_SPILLCOALESCE}
  2024. spill_temps^[t]:=spillinfo[get_alias(x)].spilllocation;
  2025. getnewspillloc:=false;
  2026. break;
  2027. end;
  2028. end;
  2029. if getnewspillloc then
  2030. get_spill_temp(templist,spill_temps,t);
  2031. {$ifdef DEBUG_SPILLCOALESCE}
  2032. writeln('trgobj.spill_registers: Spill temp: ',getsupreg(spill_temps^[t].base),'+',spill_temps^[t].offset);
  2033. {$endif DEBUG_SPILLCOALESCE}
  2034. { set spilled only as soon as a temp is assigned, else a mov iregX,iregX results in a spill coalesce with itself }
  2035. spillinfo[t].spilled:=true;
  2036. spillinfo[t].spilllocation:=spill_temps^[t];
  2037. end;
  2038. list.insertlistafter(headertai,templist);
  2039. templist.free;
  2040. { Walk through all instructions, we can start with the headertai,
  2041. because before the header tai is only symbols }
  2042. p:=headertai;
  2043. while assigned(p) do
  2044. begin
  2045. case p.typ of
  2046. ait_regalloc:
  2047. with Tai_regalloc(p) do
  2048. begin
  2049. if (getregtype(reg)=regtype) then
  2050. begin
  2051. {A register allocation of a spilled register can be removed.}
  2052. supreg:=getsupreg(reg);
  2053. if supregset_in(regs_to_spill_set,supreg) then
  2054. begin
  2055. q:=Tai(p.next);
  2056. list.remove(p);
  2057. p.free;
  2058. p:=q;
  2059. continue;
  2060. end
  2061. else
  2062. begin
  2063. case ratype of
  2064. ra_alloc :
  2065. live_registers.add(supreg);
  2066. ra_dealloc :
  2067. live_registers.delete(supreg);
  2068. else
  2069. ;
  2070. end;
  2071. end;
  2072. end;
  2073. end;
  2074. {$ifdef llvm}
  2075. ait_llvmins,
  2076. {$endif llvm}
  2077. ait_instruction:
  2078. with tai_cpu_abstract_sym(p) do
  2079. begin
  2080. // writeln(gas_op2str[tai_cpu_abstract_sym(p).opcode]);
  2081. current_filepos:=fileinfo;
  2082. if instr_spill_register(list,tai_cpu_abstract_sym(p),regs_to_spill_set,spill_temps^) then
  2083. spill_registers:=true;
  2084. end;
  2085. else
  2086. ;
  2087. end;
  2088. p:=Tai(p.next);
  2089. end;
  2090. current_filepos:=current_procinfo.exitpos;
  2091. {Safe: this procedure is only called if there are spilled nodes.}
  2092. with spillednodes do
  2093. for i:=0 to length-1 do
  2094. tg.ungettemp(list,spill_temps^[buf^[i]]);
  2095. freemem(spill_temps);
  2096. end;
  2097. function trgobj.do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;
  2098. begin
  2099. result:=false;
  2100. end;
  2101. procedure trgobj.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister;orgsupreg:tsuperregister);
  2102. var
  2103. ins:tai_cpu_abstract_sym;
  2104. begin
  2105. ins:=spilling_create_load(spilltemp,tempreg);
  2106. add_cpu_interferences(ins);
  2107. list.insertafter(ins,pos);
  2108. {$ifdef DEBUG_SPILLING}
  2109. list.Insertbefore(tai_comment.Create(strpnew('Spilling: Spill Read')),ins);
  2110. {$endif}
  2111. end;
  2112. procedure Trgobj.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister;orgsupreg:tsuperregister);
  2113. var
  2114. ins:tai_cpu_abstract_sym;
  2115. begin
  2116. ins:=spilling_create_store(tempreg,spilltemp);
  2117. add_cpu_interferences(ins);
  2118. list.insertafter(ins,pos);
  2119. {$ifdef DEBUG_SPILLING}
  2120. list.Insertbefore(tai_comment.Create(strpnew('Spilling: Spill Write')),ins);
  2121. {$endif}
  2122. end;
  2123. function trgobj.get_spill_subreg(r : tregister) : tsubregister;
  2124. begin
  2125. result:=defaultsub;
  2126. end;
  2127. function trgobj.addreginfo(var regs: tspillregsinfo; const r: tsuperregisterset; reg: tregister; operation: topertype): boolean;
  2128. var
  2129. i, tmpindex: longint;
  2130. supreg: tsuperregister;
  2131. begin
  2132. result:=false;
  2133. tmpindex := regs.reginfocount;
  2134. supreg := get_alias(getsupreg(reg));
  2135. { did we already encounter this register? }
  2136. for i := 0 to pred(regs.reginfocount) do
  2137. if (regs.reginfo[i].orgreg = supreg) then
  2138. begin
  2139. tmpindex := i;
  2140. break;
  2141. end;
  2142. if tmpindex > high(regs.reginfo) then
  2143. internalerror(2003120301);
  2144. regs.reginfo[tmpindex].orgreg := supreg;
  2145. include(regs.reginfo[tmpindex].spillregconstraints,get_spill_subreg(reg));
  2146. if supregset_in(r,supreg) then
  2147. begin
  2148. { add/update info on this register }
  2149. regs.reginfo[tmpindex].mustbespilled := true;
  2150. case operation of
  2151. operand_read:
  2152. regs.reginfo[tmpindex].regread := true;
  2153. operand_write:
  2154. regs.reginfo[tmpindex].regwritten := true;
  2155. operand_readwrite:
  2156. begin
  2157. regs.reginfo[tmpindex].regread := true;
  2158. regs.reginfo[tmpindex].regwritten := true;
  2159. end;
  2160. end;
  2161. result:=true;
  2162. end;
  2163. inc(regs.reginfocount,ord(regs.reginfocount=tmpindex));
  2164. end;
  2165. function trgobj.instr_get_oper_spilling_info(var regs: tspillregsinfo; const r: tsuperregisterset; instr: tai_cpu_abstract_sym; opidx: longint): boolean;
  2166. begin
  2167. result:=false;
  2168. with instr.oper[opidx]^ do
  2169. begin
  2170. case typ of
  2171. top_reg:
  2172. begin
  2173. if (getregtype(reg) = regtype) then
  2174. result:=addreginfo(regs,r,reg,instr.spilling_get_operation_type(opidx));
  2175. end;
  2176. top_ref:
  2177. begin
  2178. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  2179. with ref^ do
  2180. begin
  2181. if (base <> NR_NO) and
  2182. (getregtype(base)=regtype) then
  2183. result:=addreginfo(regs,r,base,instr.spilling_get_operation_type_ref(opidx,base));
  2184. if (index <> NR_NO) and
  2185. (getregtype(index)=regtype) then
  2186. result:=addreginfo(regs,r,index,instr.spilling_get_operation_type_ref(opidx,index)) or result;
  2187. {$if defined(x86)}
  2188. if (segment <> NR_NO) and
  2189. (getregtype(segment)=regtype) then
  2190. result:=addreginfo(regs,r,segment,instr.spilling_get_operation_type_ref(opidx,segment)) or result;
  2191. {$endif defined(x86)}
  2192. end;
  2193. end;
  2194. {$ifdef ARM}
  2195. top_shifterop:
  2196. begin
  2197. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  2198. if shifterop^.rs<>NR_NO then
  2199. result:=addreginfo(regs,r,shifterop^.rs,operand_read);
  2200. end;
  2201. {$endif ARM}
  2202. else
  2203. ;
  2204. end;
  2205. end;
  2206. end;
  2207. procedure trgobj.try_replace_reg(const regs: tspillregsinfo; var reg: tregister; useloadreg: boolean);
  2208. var
  2209. i: longint;
  2210. supreg: tsuperregister;
  2211. begin
  2212. supreg:=get_alias(getsupreg(reg));
  2213. for i:=0 to pred(regs.reginfocount) do
  2214. if (regs.reginfo[i].mustbespilled) and
  2215. (regs.reginfo[i].orgreg=supreg) then
  2216. begin
  2217. { Only replace supreg }
  2218. if useloadreg then
  2219. setsupreg(reg, getsupreg(regs.reginfo[i].loadreg))
  2220. else
  2221. setsupreg(reg, getsupreg(regs.reginfo[i].storereg));
  2222. break;
  2223. end;
  2224. end;
  2225. procedure trgobj.substitute_spilled_registers(const regs: tspillregsinfo; instr: tai_cpu_abstract_sym; opidx: longint);
  2226. begin
  2227. with instr.oper[opidx]^ do
  2228. case typ of
  2229. top_reg:
  2230. begin
  2231. if (getregtype(reg) = regtype) then
  2232. try_replace_reg(regs, reg, not ssa_safe or
  2233. (instr.spilling_get_operation_type(opidx)=operand_read));
  2234. end;
  2235. top_ref:
  2236. begin
  2237. if regtype in [R_INTREGISTER, R_ADDRESSREGISTER] then
  2238. begin
  2239. if (ref^.base <> NR_NO) and
  2240. (getregtype(ref^.base)=regtype) then
  2241. try_replace_reg(regs, ref^.base,
  2242. not ssa_safe or (instr.spilling_get_operation_type_ref(opidx, ref^.base)=operand_read));
  2243. if (ref^.index <> NR_NO) and
  2244. (getregtype(ref^.index)=regtype) then
  2245. try_replace_reg(regs, ref^.index,
  2246. not ssa_safe or (instr.spilling_get_operation_type_ref(opidx, ref^.index)=operand_read));
  2247. {$if defined(x86)}
  2248. if (ref^.segment <> NR_NO) and
  2249. (getregtype(ref^.segment)=regtype) then
  2250. try_replace_reg(regs, ref^.segment, true { always read-only });
  2251. {$endif defined(x86)}
  2252. end;
  2253. end;
  2254. {$ifdef ARM}
  2255. top_shifterop:
  2256. begin
  2257. if regtype in [R_INTREGISTER, R_ADDRESSREGISTER] then
  2258. try_replace_reg(regs, shifterop^.rs, true { always read-only });
  2259. end;
  2260. {$endif ARM}
  2261. else
  2262. ;
  2263. end;
  2264. end;
  2265. function trgobj.instr_spill_register(list:TAsmList;
  2266. instr:tai_cpu_abstract_sym;
  2267. const r:Tsuperregisterset;
  2268. const spilltemplist:Tspill_temp_list): boolean;
  2269. var
  2270. counter: longint;
  2271. regs: tspillregsinfo;
  2272. spilled: boolean;
  2273. var
  2274. loadpos,
  2275. storepos : tai;
  2276. oldlive_registers : tsuperregisterworklist;
  2277. begin
  2278. result := false;
  2279. fillchar(regs,sizeof(regs),0);
  2280. for counter := low(regs.reginfo) to high(regs.reginfo) do
  2281. begin
  2282. regs.reginfo[counter].orgreg := RS_INVALID;
  2283. regs.reginfo[counter].loadreg := NR_INVALID;
  2284. regs.reginfo[counter].storereg := NR_INVALID;
  2285. end;
  2286. spilled := false;
  2287. { check whether and if so which and how (read/written) this instructions contains
  2288. registers that must be spilled }
  2289. for counter := 0 to instr.ops-1 do
  2290. spilled:=instr_get_oper_spilling_info(regs,r,instr,counter) or spilled;
  2291. { if no spilling for this instruction we can leave }
  2292. if not spilled then
  2293. exit;
  2294. {$if defined(x86) or defined(mips) or defined(sparcgen) or defined(arm) or defined(m68k)}
  2295. { Try replacing the register with the spilltemp. This is useful only
  2296. for the i386,x86_64 that support memory locations for several instructions
  2297. For non-x86 it is nevertheless possible to replace moves to/from the register
  2298. with loads/stores to spilltemp (Sergei) }
  2299. for counter := 0 to pred(regs.reginfocount) do
  2300. with regs.reginfo[counter] do
  2301. begin
  2302. if mustbespilled then
  2303. begin
  2304. if do_spill_replace(list,instr,orgreg,spilltemplist[orgreg]) then
  2305. mustbespilled:=false;
  2306. end;
  2307. end;
  2308. {$endif defined(x86) or defined(mips) or defined(sparcgen) or defined(arm) or defined(m68k)}
  2309. {
  2310. There are registers that need are spilled. We generate the
  2311. following code for it. The used positions where code need
  2312. to be inserted are marked using #. Note that code is always inserted
  2313. before the positions using pos.previous. This way the position is always
  2314. the same since pos doesn't change, but pos.previous is modified everytime
  2315. new code is inserted.
  2316. [
  2317. - reg_allocs load spills
  2318. - load spills
  2319. ]
  2320. [#loadpos
  2321. - reg_deallocs
  2322. - reg_allocs
  2323. ]
  2324. [
  2325. - reg_deallocs for load-only spills
  2326. - reg_allocs for store-only spills
  2327. ]
  2328. [#instr
  2329. - original instruction
  2330. ]
  2331. [
  2332. - store spills
  2333. - reg_deallocs store spills
  2334. ]
  2335. [#storepos
  2336. ]
  2337. }
  2338. result := true;
  2339. oldlive_registers.copyfrom(live_registers);
  2340. { Process all tai_regallocs belonging to this instruction, ignore explicit
  2341. inserted regallocs. These can happend for example in i386:
  2342. mov ref,ireg26
  2343. <regdealloc ireg26, instr=taicpu of lea>
  2344. <regalloc edi, insrt=nil>
  2345. lea [ireg26+ireg17],edi
  2346. All released registers are also added to the live_registers because
  2347. they can't be used during the spilling }
  2348. loadpos:=tai(instr.previous);
  2349. while assigned(loadpos) and
  2350. (loadpos.typ=ait_regalloc) and
  2351. ((tai_regalloc(loadpos).instr=nil) or
  2352. (tai_regalloc(loadpos).instr=instr)) do
  2353. begin
  2354. { Only add deallocs belonging to the instruction. Explicit inserted deallocs
  2355. belong to the previous instruction and not the current instruction }
  2356. if (tai_regalloc(loadpos).instr=instr) and
  2357. (tai_regalloc(loadpos).ratype=ra_dealloc) then
  2358. live_registers.add(getsupreg(tai_regalloc(loadpos).reg));
  2359. loadpos:=tai(loadpos.previous);
  2360. end;
  2361. loadpos:=tai(loadpos.next);
  2362. { Load the spilled registers }
  2363. for counter := 0 to pred(regs.reginfocount) do
  2364. with regs.reginfo[counter] do
  2365. begin
  2366. if mustbespilled and regread then
  2367. begin
  2368. loadreg:=getregisterinline(list,regs.reginfo[counter].spillregconstraints);
  2369. do_spill_read(list,tai(loadpos.previous),spilltemplist[orgreg],loadreg,orgreg);
  2370. end;
  2371. end;
  2372. { Release temp registers of read-only registers, and add reference of the instruction
  2373. to the reginfo }
  2374. for counter := 0 to pred(regs.reginfocount) do
  2375. with regs.reginfo[counter] do
  2376. begin
  2377. if mustbespilled and regread and
  2378. (ssa_safe or
  2379. not regwritten) then
  2380. begin
  2381. { The original instruction will be the next that uses this register
  2382. set weigth of the newly allocated register higher than the old one,
  2383. so it will selected for spilling with a lower priority than
  2384. the original one, this prevents an endless spilling loop if orgreg
  2385. is short living, see e.g. tw25164.pp }
  2386. add_reg_instruction(instr,loadreg,reginfo[orgreg].weight+1);
  2387. ungetregisterinline(list,loadreg);
  2388. end;
  2389. end;
  2390. { Allocate temp registers of write-only registers, and add reference of the instruction
  2391. to the reginfo }
  2392. for counter := 0 to pred(regs.reginfocount) do
  2393. with regs.reginfo[counter] do
  2394. begin
  2395. if mustbespilled and regwritten then
  2396. begin
  2397. { When the register is also loaded there is already a register assigned }
  2398. if (not regread) or
  2399. ssa_safe then
  2400. begin
  2401. storereg:=getregisterinline(list,regs.reginfo[counter].spillregconstraints);
  2402. { we also use loadreg for store replacements in case we
  2403. don't have ensure ssa -> initialise loadreg even if
  2404. there are no reads }
  2405. if not regread then
  2406. loadreg:=storereg;
  2407. end
  2408. else
  2409. storereg:=loadreg;
  2410. { The original instruction will be the next that uses this register, this
  2411. also needs to be done for read-write registers,
  2412. set weigth of the newly allocated register higher than the old one,
  2413. so it will selected for spilling with a lower priority than
  2414. the original one, this prevents an endless spilling loop if orgreg
  2415. is short living, see e.g. tw25164.pp }
  2416. add_reg_instruction(instr,storereg,reginfo[orgreg].weight+1);
  2417. end;
  2418. end;
  2419. { store the spilled registers }
  2420. if not assigned(instr.next) then
  2421. list.concat(tai_marker.Create(mark_Position));
  2422. storepos:=tai(instr.next);
  2423. for counter := 0 to pred(regs.reginfocount) do
  2424. with regs.reginfo[counter] do
  2425. begin
  2426. if mustbespilled and regwritten then
  2427. begin
  2428. do_spill_written(list,tai(storepos.previous),spilltemplist[orgreg],storereg,orgreg);
  2429. ungetregisterinline(list,storereg);
  2430. end;
  2431. end;
  2432. { now all spilling code is generated we can restore the live registers. This
  2433. must be done after the store because the store can need an extra register
  2434. that also needs to conflict with the registers of the instruction }
  2435. live_registers.done;
  2436. live_registers:=oldlive_registers;
  2437. { substitute registers }
  2438. for counter:=0 to instr.ops-1 do
  2439. substitute_spilled_registers(regs,instr,counter);
  2440. { We have modified the instruction; perhaps the new instruction has
  2441. certain constraints regarding which imaginary registers interfere
  2442. with certain physical registers. }
  2443. add_cpu_interferences(instr);
  2444. end;
  2445. end.