rgobj.pas 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734
  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,'Register type: ',regtype,', 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 Uses IntfCnt ');
  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,' ');
  631. if (i<first_imaginary) and
  632. (findreg_by_number(newreg(regtype,TSuperRegister(i),defaultsub))<>0) then
  633. write(f,std_regname(newreg(regtype,TSuperRegister(i),defaultsub))+':'+hexstr(i,2):7)
  634. else
  635. write(f,' ',hexstr(i,2):4);
  636. for j:=0 to maxreg-1 do
  637. if ibitmap[i,j] then
  638. write(f,'*')
  639. else
  640. write(f,'-');
  641. writeln(f);
  642. end;
  643. close(f);
  644. end;
  645. {$endif EXTDEBUG}
  646. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  647. begin
  648. {$ifdef EXTDEBUG}
  649. if (u>=maxreginfo) then
  650. internalerror(2012101902);
  651. {$endif}
  652. with reginfo[u] do
  653. begin
  654. if movelist=nil then
  655. begin
  656. { don't use sizeof(tmovelistheader), because that ignores alignment }
  657. getmem(movelist,ptruint(@movelist^.data)-ptruint(movelist)+16*sizeof(pointer));
  658. movelist^.header.maxcount:=16;
  659. movelist^.header.count:=0;
  660. movelist^.header.sorted_until:=0;
  661. end
  662. else
  663. begin
  664. if movelist^.header.count>=movelist^.header.maxcount then
  665. begin
  666. movelist^.header.maxcount:=movelist^.header.maxcount*2;
  667. { don't use sizeof(tmovelistheader), because that ignores alignment }
  668. reallocmem(movelist,ptruint(@movelist^.data)-ptruint(movelist)+movelist^.header.maxcount*sizeof(pointer));
  669. end;
  670. end;
  671. movelist^.data[movelist^.header.count]:=data;
  672. inc(movelist^.header.count);
  673. end;
  674. end;
  675. procedure trgobj.set_live_range_direction(dir: TRADirection);
  676. begin
  677. if (dir in [rad_backwards,rad_backwards_reinit]) then
  678. begin
  679. if not assigned(extended_backwards) then
  680. begin
  681. { create expects a "size", not a "max bit" parameter -> +1 }
  682. backwards_was_first:=tbitset.create(maxreg+1);
  683. extended_backwards:=tbitset.create(maxreg+1);
  684. end
  685. else
  686. begin
  687. if (dir=rad_backwards_reinit) then
  688. extended_backwards.clear;
  689. backwards_was_first.clear;
  690. end;
  691. int_live_range_direction:=rad_backwards;
  692. end
  693. else
  694. int_live_range_direction:=rad_forward;
  695. end;
  696. procedure trgobj.set_live_start(reg: tsuperregister; t: tai);
  697. begin
  698. reginfo[reg].live_start:=t;
  699. end;
  700. function trgobj.get_live_start(reg: tsuperregister): tai;
  701. begin
  702. result:=reginfo[reg].live_start;
  703. end;
  704. procedure trgobj.set_live_end(reg: tsuperregister; t: tai);
  705. begin
  706. reginfo[reg].live_end:=t;
  707. end;
  708. function trgobj.get_live_end(reg: tsuperregister): tai;
  709. begin
  710. result:=reginfo[reg].live_end;
  711. end;
  712. procedure trgobj.add_reg_instruction(instr:Tai;r:tregister;aweight:longint);
  713. var
  714. supreg : tsuperregister;
  715. begin
  716. supreg:=getsupreg(r);
  717. {$ifdef extdebug}
  718. if not (cs_no_regalloc in current_settings.globalswitches) and
  719. (supreg>=maxreginfo) then
  720. internalerror(200411061);
  721. {$endif extdebug}
  722. if supreg>=first_imaginary then
  723. with reginfo[supreg] do
  724. begin
  725. { avoid overflow }
  726. if high(weight)-aweight<weight then
  727. weight:=high(weight)
  728. else
  729. inc(weight,aweight);
  730. if (live_range_direction=rad_forward) then
  731. begin
  732. if not assigned(live_start) then
  733. live_start:=instr;
  734. live_end:=instr;
  735. end
  736. else
  737. begin
  738. if not extended_backwards.isset(supreg) then
  739. begin
  740. extended_backwards.include(supreg);
  741. live_start := instr;
  742. if not assigned(live_end) then
  743. begin
  744. backwards_was_first.include(supreg);
  745. live_end := instr;
  746. end;
  747. end
  748. else
  749. begin
  750. if backwards_was_first.isset(supreg) then
  751. live_end := instr;
  752. end
  753. end
  754. end;
  755. end;
  756. procedure trgobj.add_move_instruction(instr:Taicpu);
  757. {This procedure notifies a certain as a move instruction so the
  758. register allocator can try to eliminate it.}
  759. var i:Tmoveins;
  760. sreg, dreg : Tregister;
  761. ssupreg,dsupreg:Tsuperregister;
  762. begin
  763. {$ifdef extdebug}
  764. if (instr.oper[O_MOV_SOURCE]^.typ<>top_reg) or
  765. (instr.oper[O_MOV_DEST]^.typ<>top_reg) then
  766. internalerror(200311291);
  767. {$endif}
  768. sreg:=instr.oper[O_MOV_SOURCE]^.reg;
  769. dreg:=instr.oper[O_MOV_DEST]^.reg;
  770. { How should we handle m68k move %d0,%a0? }
  771. if (getregtype(sreg)<>getregtype(dreg)) then
  772. exit;
  773. i:=Tmoveins.create;
  774. i.moveset:=ms_worklist_moves;
  775. worklist_moves.insert(i);
  776. ssupreg:=getsupreg(sreg);
  777. add_to_movelist(ssupreg,i);
  778. dsupreg:=getsupreg(dreg);
  779. { On m68k move can mix address and integer registers,
  780. this leads to problems ... PM }
  781. if (ssupreg<>dsupreg) {and (getregtype(sreg)=getregtype(dreg))} then
  782. {Avoid adding the same move instruction twice to a single register.}
  783. add_to_movelist(dsupreg,i);
  784. i.x:=ssupreg;
  785. i.y:=dsupreg;
  786. end;
  787. function trgobj.move_related(n:Tsuperregister):boolean;
  788. var i:cardinal;
  789. begin
  790. move_related:=false;
  791. with reginfo[n] do
  792. if movelist<>nil then
  793. with movelist^ do
  794. for i:=0 to header.count-1 do
  795. if Tmoveins(data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  796. begin
  797. move_related:=true;
  798. break;
  799. end;
  800. end;
  801. procedure Trgobj.sort_simplify_worklist;
  802. {Sorts the simplifyworklist by the number of interferences the
  803. registers in it cause. This allows simplify to execute in
  804. constant time.}
  805. var p,h,i,leni,lent:longword;
  806. t:Tsuperregister;
  807. adji,adjt:Psuperregisterworklist;
  808. begin
  809. with simplifyworklist do
  810. begin
  811. if length<2 then
  812. exit;
  813. p:=1;
  814. while 2*p<length do
  815. p:=2*p;
  816. while p<>0 do
  817. begin
  818. for h:=p to length-1 do
  819. begin
  820. i:=h;
  821. t:=buf^[i];
  822. adjt:=reginfo[buf^[i]].adjlist;
  823. lent:=0;
  824. if adjt<>nil then
  825. lent:=adjt^.length;
  826. repeat
  827. adji:=reginfo[buf^[i-p]].adjlist;
  828. leni:=0;
  829. if adji<>nil then
  830. leni:=adji^.length;
  831. if leni<=lent then
  832. break;
  833. buf^[i]:=buf^[i-p];
  834. dec(i,p)
  835. until i<p;
  836. buf^[i]:=t;
  837. end;
  838. p:=p shr 1;
  839. end;
  840. end;
  841. end;
  842. { sort spilled nodes by increasing number of interferences }
  843. procedure Trgobj.sort_spillednodes;
  844. var
  845. p,h,i,leni,lent:longword;
  846. t:Tsuperregister;
  847. adji,adjt:Psuperregisterworklist;
  848. begin
  849. with spillednodes do
  850. begin
  851. if length<2 then
  852. exit;
  853. p:=1;
  854. while 2*p<length do
  855. p:=2*p;
  856. while p<>0 do
  857. begin
  858. for h:=p to length-1 do
  859. begin
  860. i:=h;
  861. t:=buf^[i];
  862. adjt:=reginfo[buf^[i]].adjlist;
  863. lent:=0;
  864. if adjt<>nil then
  865. lent:=adjt^.length;
  866. repeat
  867. adji:=reginfo[buf^[i-p]].adjlist;
  868. leni:=0;
  869. if adji<>nil then
  870. leni:=adji^.length;
  871. if leni<=lent then
  872. break;
  873. buf^[i]:=buf^[i-p];
  874. dec(i,p)
  875. until i<p;
  876. buf^[i]:=t;
  877. end;
  878. p:=p shr 1;
  879. end;
  880. end;
  881. end;
  882. procedure trgobj.make_work_list;
  883. var n:cardinal;
  884. begin
  885. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  886. assign it to any of the registers, thus it is significant.}
  887. for n:=first_imaginary to maxreg-1 do
  888. with reginfo[n] do
  889. begin
  890. if adjlist=nil then
  891. degree:=0
  892. else
  893. degree:=adjlist^.length;
  894. if degree>=usable_registers_cnt then
  895. spillworklist.add(n)
  896. else if move_related(n) then
  897. freezeworklist.add(n)
  898. else if not(ri_coalesced in flags) then
  899. simplifyworklist.add(n);
  900. end;
  901. sort_simplify_worklist;
  902. end;
  903. procedure trgobj.prepare_colouring;
  904. begin
  905. make_work_list;
  906. active_moves:=Tlinkedlist.create;
  907. frozen_moves:=Tlinkedlist.create;
  908. coalesced_moves:=Tlinkedlist.create;
  909. constrained_moves:=Tlinkedlist.create;
  910. selectstack.clear;
  911. end;
  912. procedure trgobj.enable_moves(n:Tsuperregister);
  913. var m:Tlinkedlistitem;
  914. i:cardinal;
  915. begin
  916. with reginfo[n] do
  917. if movelist<>nil then
  918. for i:=0 to movelist^.header.count-1 do
  919. begin
  920. m:=movelist^.data[i];
  921. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  922. if Tmoveins(m).moveset=ms_active_moves then
  923. begin
  924. {Move m from the set active_moves to the set worklist_moves.}
  925. active_moves.remove(m);
  926. Tmoveins(m).moveset:=ms_worklist_moves;
  927. worklist_moves.concat(m);
  928. end;
  929. end;
  930. end;
  931. procedure Trgobj.decrement_degree(m:Tsuperregister);
  932. var adj : Psuperregisterworklist;
  933. n : tsuperregister;
  934. d,i : cardinal;
  935. begin
  936. with reginfo[m] do
  937. begin
  938. d:=degree;
  939. if d=0 then
  940. internalerror(200312151);
  941. dec(degree);
  942. if d=usable_registers_cnt then
  943. begin
  944. {Enable moves for m.}
  945. enable_moves(m);
  946. {Enable moves for adjacent.}
  947. adj:=adjlist;
  948. if adj<>nil then
  949. for i:=1 to adj^.length do
  950. begin
  951. n:=adj^.buf^[i-1];
  952. if reginfo[n].flags*[ri_selected,ri_coalesced]<>[] then
  953. enable_moves(n);
  954. end;
  955. {Remove the node from the spillworklist.}
  956. if not spillworklist.delete(m) then
  957. internalerror(200310145);
  958. if move_related(m) then
  959. freezeworklist.add(m)
  960. else
  961. simplifyworklist.add(m);
  962. end;
  963. end;
  964. end;
  965. procedure trgobj.simplify;
  966. var adj : Psuperregisterworklist;
  967. m,n : Tsuperregister;
  968. i : cardinal;
  969. begin
  970. {We take the element with the least interferences out of the
  971. simplifyworklist. Since the simplifyworklist is now sorted, we
  972. no longer need to search, but we can simply take the first element.}
  973. m:=simplifyworklist.get;
  974. {Push it on the selectstack.}
  975. selectstack.add(m);
  976. with reginfo[m] do
  977. begin
  978. include(flags,ri_selected);
  979. adj:=adjlist;
  980. end;
  981. if adj<>nil then
  982. for i:=1 to adj^.length do
  983. begin
  984. n:=adj^.buf^[i-1];
  985. if (n>=first_imaginary) and
  986. (reginfo[n].flags*[ri_selected,ri_coalesced]=[]) then
  987. decrement_degree(n);
  988. end;
  989. end;
  990. function trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  991. begin
  992. while ri_coalesced in reginfo[n].flags do
  993. n:=reginfo[n].alias;
  994. get_alias:=n;
  995. end;
  996. procedure trgobj.add_worklist(u:Tsuperregister);
  997. begin
  998. if (u>=first_imaginary) and
  999. (not move_related(u)) and
  1000. (reginfo[u].degree<usable_registers_cnt) then
  1001. begin
  1002. if not freezeworklist.delete(u) then
  1003. internalerror(200308161); {must be found}
  1004. simplifyworklist.add(u);
  1005. end;
  1006. end;
  1007. function trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  1008. {Check wether u and v should be coalesced. u is precoloured.}
  1009. function ok(t,r:Tsuperregister):boolean;
  1010. begin
  1011. ok:=(t<first_imaginary) or
  1012. // disabled for now, see issue #22405
  1013. // ((r<first_imaginary) and (r in usable_register_set)) or
  1014. (reginfo[t].degree<usable_registers_cnt) or
  1015. ibitmap[r,t];
  1016. end;
  1017. var adj : Psuperregisterworklist;
  1018. i : cardinal;
  1019. n : tsuperregister;
  1020. begin
  1021. with reginfo[v] do
  1022. begin
  1023. adjacent_ok:=true;
  1024. adj:=adjlist;
  1025. if adj<>nil then
  1026. for i:=1 to adj^.length do
  1027. begin
  1028. n:=adj^.buf^[i-1];
  1029. if (flags*[ri_coalesced,ri_selected]=[]) and not ok(n,u) then
  1030. begin
  1031. adjacent_ok:=false;
  1032. break;
  1033. end;
  1034. end;
  1035. end;
  1036. end;
  1037. function trgobj.conservative(u,v:Tsuperregister):boolean;
  1038. var adj : Psuperregisterworklist;
  1039. done : Tsuperregisterset; {To prevent that we count nodes twice.}
  1040. i,k:cardinal;
  1041. n : tsuperregister;
  1042. begin
  1043. k:=0;
  1044. supregset_reset(done,false,maxreg);
  1045. with reginfo[u] do
  1046. begin
  1047. adj:=adjlist;
  1048. if adj<>nil then
  1049. for i:=1 to adj^.length do
  1050. begin
  1051. n:=adj^.buf^[i-1];
  1052. if reginfo[n].flags*[ri_coalesced,ri_selected]=[] then
  1053. begin
  1054. supregset_include(done,n);
  1055. if reginfo[n].degree>=usable_registers_cnt then
  1056. inc(k);
  1057. end;
  1058. end;
  1059. end;
  1060. adj:=reginfo[v].adjlist;
  1061. if adj<>nil then
  1062. for i:=1 to adj^.length do
  1063. begin
  1064. n:=adj^.buf^[i-1];
  1065. if not supregset_in(done,n) and
  1066. (reginfo[n].degree>=usable_registers_cnt) and
  1067. (reginfo[n].flags*[ri_coalesced,ri_selected]=[]) then
  1068. inc(k);
  1069. end;
  1070. conservative:=(k<usable_registers_cnt);
  1071. end;
  1072. procedure trgobj.set_alias(u,v:Tsuperregister);
  1073. begin
  1074. { don't make registers that the register allocator shouldn't touch (such
  1075. as stack and frame pointers) be aliases for other registers, because
  1076. then it can propagate them and even start changing them if the aliased
  1077. register gets changed }
  1078. if ((u<first_imaginary) and
  1079. not(u in usable_register_set)) or
  1080. ((v<first_imaginary) and
  1081. not(v in usable_register_set)) then
  1082. exit;
  1083. include(reginfo[v].flags,ri_coalesced);
  1084. if reginfo[v].alias<>0 then
  1085. internalerror(200712291);
  1086. reginfo[v].alias:=get_alias(u);
  1087. coalescednodes.add(v);
  1088. end;
  1089. procedure trgobj.combine(u,v:Tsuperregister);
  1090. var adj : Psuperregisterworklist;
  1091. i,n,p,q:cardinal;
  1092. t : tsuperregister;
  1093. searched:Tlinkedlistitem;
  1094. found : boolean;
  1095. begin
  1096. if not freezeworklist.delete(v) then
  1097. spillworklist.delete(v);
  1098. coalescednodes.add(v);
  1099. include(reginfo[v].flags,ri_coalesced);
  1100. reginfo[v].alias:=u;
  1101. {Combine both movelists. Since the movelists are sets, only add
  1102. elements that are not already present. The movelists cannot be
  1103. empty by definition; nodes are only coalesced if there is a move
  1104. between them. To prevent quadratic time blowup (movelists of
  1105. especially machine registers can get very large because of moves
  1106. generated during calls) we need to go into disgusting complexity.
  1107. (See webtbs/tw2242 for an example that stresses this.)
  1108. We want to sort the movelist to be able to search logarithmically.
  1109. Unfortunately, sorting the movelist every time before searching
  1110. is counter-productive, since the movelist usually grows with a few
  1111. items at a time. Therefore, we split the movelist into a sorted
  1112. and an unsorted part and search through both. If the unsorted part
  1113. becomes too large, we sort.}
  1114. if assigned(reginfo[u].movelist) then
  1115. begin
  1116. {We have to weigh the cost of sorting the list against searching
  1117. the cost of the unsorted part. I use factor of 8 here; if the
  1118. number of items is less than 8 times the numer of unsorted items,
  1119. we'll sort the list.}
  1120. with reginfo[u].movelist^ do
  1121. if header.count<8*(header.count-header.sorted_until) then
  1122. sort_movelist(reginfo[u].movelist);
  1123. if assigned(reginfo[v].movelist) then
  1124. begin
  1125. for n:=0 to reginfo[v].movelist^.header.count-1 do
  1126. begin
  1127. {Binary search the sorted part of the list.}
  1128. searched:=reginfo[v].movelist^.data[n];
  1129. p:=0;
  1130. q:=reginfo[u].movelist^.header.sorted_until;
  1131. i:=0;
  1132. if q<>0 then
  1133. repeat
  1134. i:=(p+q) shr 1;
  1135. if ptruint(searched)>ptruint(reginfo[u].movelist^.data[i]) then
  1136. p:=i+1
  1137. else
  1138. q:=i;
  1139. until p=q;
  1140. with reginfo[u].movelist^ do
  1141. if searched<>data[i] then
  1142. begin
  1143. {Linear search the unsorted part of the list.}
  1144. found:=false;
  1145. for i:=header.sorted_until+1 to header.count-1 do
  1146. if searched=data[i] then
  1147. begin
  1148. found:=true;
  1149. break;
  1150. end;
  1151. if not found then
  1152. add_to_movelist(u,searched);
  1153. end;
  1154. end;
  1155. end;
  1156. end;
  1157. enable_moves(v);
  1158. adj:=reginfo[v].adjlist;
  1159. if adj<>nil then
  1160. for i:=1 to adj^.length do
  1161. begin
  1162. t:=adj^.buf^[i-1];
  1163. with reginfo[t] do
  1164. if not(ri_coalesced in flags) then
  1165. begin
  1166. {t has a connection to v. Since we are adding v to u, we
  1167. need to connect t to u. However, beware if t was already
  1168. connected to u...}
  1169. if (ibitmap[t,u]) and not (ri_selected in flags) then
  1170. {... because in that case, we are actually removing an edge
  1171. and the degree of t decreases.}
  1172. decrement_degree(t)
  1173. else
  1174. begin
  1175. add_edge(t,u);
  1176. {We have added an edge to t and u. So their degree increases.
  1177. However, v is added to u. That means its neighbours will
  1178. no longer point to v, but to u instead. Therefore, only the
  1179. degree of u increases.}
  1180. if (u>=first_imaginary) and not (ri_selected in flags) then
  1181. inc(reginfo[u].degree);
  1182. end;
  1183. end;
  1184. end;
  1185. if (reginfo[u].degree>=usable_registers_cnt) and freezeworklist.delete(u) then
  1186. spillworklist.add(u);
  1187. end;
  1188. procedure trgobj.coalesce;
  1189. var m:Tmoveins;
  1190. x,y,u,v:cardinal;
  1191. begin
  1192. m:=Tmoveins(worklist_moves.getfirst);
  1193. x:=get_alias(m.x);
  1194. y:=get_alias(m.y);
  1195. if (y<first_imaginary) then
  1196. begin
  1197. u:=y;
  1198. v:=x;
  1199. end
  1200. else
  1201. begin
  1202. u:=x;
  1203. v:=y;
  1204. end;
  1205. if (u=v) then
  1206. begin
  1207. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1208. coalesced_moves.insert(m);
  1209. add_worklist(u);
  1210. end
  1211. {Do u and v interfere? In that case the move is constrained. Two
  1212. precoloured nodes interfere allways. If v is precoloured, by the above
  1213. code u is precoloured, thus interference...}
  1214. else if (v<first_imaginary) or ibitmap[u,v] then
  1215. begin
  1216. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1217. constrained_moves.insert(m);
  1218. add_worklist(u);
  1219. add_worklist(v);
  1220. end
  1221. {Next test: is it possible and a good idea to coalesce?? Note: don't
  1222. coalesce registers that should not be touched by the register allocator,
  1223. such as stack/framepointers, because otherwise they can be changed }
  1224. else if (((u<first_imaginary) and adjacent_ok(u,v)) or
  1225. conservative(u,v)) and
  1226. ((u>first_imaginary) or
  1227. (u in usable_register_set)) and
  1228. ((v>first_imaginary) or
  1229. (v in usable_register_set)) then
  1230. begin
  1231. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1232. coalesced_moves.insert(m);
  1233. combine(u,v);
  1234. add_worklist(u);
  1235. end
  1236. else
  1237. begin
  1238. m.moveset:=ms_active_moves;
  1239. active_moves.insert(m);
  1240. end;
  1241. end;
  1242. procedure trgobj.freeze_moves(u:Tsuperregister);
  1243. var i:cardinal;
  1244. m:Tlinkedlistitem;
  1245. v,x,y:Tsuperregister;
  1246. begin
  1247. if reginfo[u].movelist<>nil then
  1248. for i:=0 to reginfo[u].movelist^.header.count-1 do
  1249. begin
  1250. m:=reginfo[u].movelist^.data[i];
  1251. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1252. begin
  1253. x:=Tmoveins(m).x;
  1254. y:=Tmoveins(m).y;
  1255. if get_alias(y)=get_alias(u) then
  1256. v:=get_alias(x)
  1257. else
  1258. v:=get_alias(y);
  1259. {Move m from active_moves/worklist_moves to frozen_moves.}
  1260. if Tmoveins(m).moveset=ms_active_moves then
  1261. active_moves.remove(m)
  1262. else
  1263. worklist_moves.remove(m);
  1264. Tmoveins(m).moveset:=ms_frozen_moves;
  1265. frozen_moves.insert(m);
  1266. if (v>=first_imaginary) and not(move_related(v)) and
  1267. (reginfo[v].degree<usable_registers_cnt) then
  1268. begin
  1269. freezeworklist.delete(v);
  1270. simplifyworklist.add(v);
  1271. end;
  1272. end;
  1273. end;
  1274. end;
  1275. procedure trgobj.freeze;
  1276. var n:Tsuperregister;
  1277. begin
  1278. { We need to take a random element out of the freezeworklist. We take
  1279. the last element. Dirty code! }
  1280. n:=freezeworklist.get;
  1281. {Add it to the simplifyworklist.}
  1282. simplifyworklist.add(n);
  1283. freeze_moves(n);
  1284. end;
  1285. { The spilling approach selected by SPILLING_NEW does not work well for AVR as it eploits apparently the problem of the current
  1286. 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 }
  1287. {$if defined(AVR)}
  1288. {$define SPILLING_OLD}
  1289. {$else defined(AVR)}
  1290. { $define SPILLING_NEW}
  1291. {$endif defined(AVR)}
  1292. {$ifndef SPILLING_NEW}
  1293. {$define SPILLING_OLD}
  1294. {$endif SPILLING_NEW}
  1295. procedure trgobj.select_spill;
  1296. var
  1297. n : tsuperregister;
  1298. adj : psuperregisterworklist;
  1299. maxlength,p,i :word;
  1300. minweight: longint;
  1301. {$ifdef SPILLING_NEW}
  1302. dist: Double;
  1303. {$endif}
  1304. begin
  1305. {$ifdef SPILLING_NEW}
  1306. { This new approach for selecting the next spill candidate takes care of the weight of a register:
  1307. It spills the register with the lowest weight but only if it is expected that it results in convergence of
  1308. register allocation. Convergence is expected if a register is spilled where the average of the active interferences
  1309. - active interference means that the register is used in an instruction - is lower than
  1310. the degree.
  1311. Example (modify means read and the write):
  1312. modify reg1
  1313. loop:
  1314. modify reg2
  1315. modify reg3
  1316. modify reg4
  1317. modify reg5
  1318. modify reg6
  1319. modify reg7
  1320. modify reg1
  1321. In this example, all register have the same degree. However, spilling reg1 is most benefical as it is used least. Furthermore,
  1322. spilling reg1 is a step toward solving the coloring problem as the registers used during spilling will have a lower degree
  1323. as no register are in use at the location where reg1 is spilled.
  1324. }
  1325. minweight:=high(longint);
  1326. p:=0;
  1327. with spillworklist do
  1328. begin
  1329. { Safe: This procedure is only called if length<>0 }
  1330. for i:=0 to length-1 do
  1331. begin
  1332. adj:=reginfo[buf^[i]].adjlist;
  1333. dist:=adj^.length-reginfo[buf^[i]].total_interferences/reginfo[buf^[i]].count_uses;
  1334. if assigned(adj) and
  1335. (reginfo[buf^[i]].weight<minweight) and
  1336. (dist>=1) and
  1337. (reginfo[buf^[i]].weight>0) then
  1338. begin
  1339. p:=i;
  1340. minweight:=reginfo[buf^[i]].weight;
  1341. end;
  1342. end;
  1343. n:=buf^[p];
  1344. deleteidx(p);
  1345. end;
  1346. {$endif SPILLING_NEW}
  1347. {$ifdef SPILLING_OLD}
  1348. { We must look for the element with the most interferences in the
  1349. spillworklist. This is required because those registers are creating
  1350. the most conflicts and keeping them in a register will not reduce the
  1351. complexity and even can cause the help registers for the spilling code
  1352. to get too much conflicts with the result that the spilling code
  1353. will never converge (PFV) }
  1354. maxlength:=0;
  1355. minweight:=high(longint);
  1356. p:=0;
  1357. with spillworklist do
  1358. begin
  1359. {Safe: This procedure is only called if length<>0}
  1360. for i:=0 to length-1 do
  1361. begin
  1362. adj:=reginfo[buf^[i]].adjlist;
  1363. if assigned(adj) and
  1364. (
  1365. (adj^.length>maxlength) or
  1366. ((adj^.length=maxlength) and (reginfo[buf^[i]].weight<minweight))
  1367. ) then
  1368. begin
  1369. p:=i;
  1370. maxlength:=adj^.length;
  1371. minweight:=reginfo[buf^[i]].weight;
  1372. end;
  1373. end;
  1374. n:=buf^[p];
  1375. deleteidx(p);
  1376. end;
  1377. {$endif SPILLING_OLD}
  1378. simplifyworklist.add(n);
  1379. freeze_moves(n);
  1380. end;
  1381. procedure trgobj.assign_colours;
  1382. {Assign_colours assigns the actual colours to the registers.}
  1383. var adj : Psuperregisterworklist;
  1384. i,j,k : cardinal;
  1385. n,a,c : Tsuperregister;
  1386. colourednodes : Tsuperregisterset;
  1387. adj_colours:set of 0..255;
  1388. found : boolean;
  1389. tmpr: tregister;
  1390. begin
  1391. spillednodes.clear;
  1392. {Reset colours}
  1393. for n:=0 to maxreg-1 do
  1394. reginfo[n].colour:=n;
  1395. {Colour the cpu registers...}
  1396. supregset_reset(colourednodes,false,maxreg);
  1397. for n:=0 to first_imaginary-1 do
  1398. supregset_include(colourednodes,n);
  1399. {Now colour the imaginary registers on the select-stack.}
  1400. for i:=selectstack.length downto 1 do
  1401. begin
  1402. n:=selectstack.buf^[i-1];
  1403. {Create a list of colours that we cannot assign to n.}
  1404. adj_colours:=[];
  1405. adj:=reginfo[n].adjlist;
  1406. if adj<>nil then
  1407. for j:=0 to adj^.length-1 do
  1408. begin
  1409. a:=get_alias(adj^.buf^[j]);
  1410. if supregset_in(colourednodes,a) and (reginfo[a].colour<=255) then
  1411. include(adj_colours,reginfo[a].colour);
  1412. end;
  1413. { FIXME: temp variable r is needed here to avoid Internal error 20060521 }
  1414. { while compiling the compiler. }
  1415. tmpr:=NR_STACK_POINTER_REG;
  1416. { e.g. AVR does not have a stack pointer register }
  1417. {$if defined(RS_STACK_POINTER_REG)}
  1418. {$if (RS_STACK_POINTER_REG<>RS_INVALID)}
  1419. if (regtype=getregtype(tmpr)) then
  1420. include(adj_colours,RS_STACK_POINTER_REG);
  1421. {$ifend}
  1422. {$ifend}
  1423. {Assume a spill by default...}
  1424. found:=false;
  1425. {Search for a colour not in this list.}
  1426. for k:=0 to usable_registers_cnt-1 do
  1427. begin
  1428. c:=usable_registers[k];
  1429. if not(c in adj_colours) then
  1430. begin
  1431. reginfo[n].colour:=c;
  1432. found:=true;
  1433. supregset_include(colourednodes,n);
  1434. break;
  1435. end;
  1436. end;
  1437. if not found then
  1438. spillednodes.add(n);
  1439. end;
  1440. {Finally colour the nodes that were coalesced.}
  1441. for i:=1 to coalescednodes.length do
  1442. begin
  1443. n:=coalescednodes.buf^[i-1];
  1444. k:=get_alias(n);
  1445. reginfo[n].colour:=reginfo[k].colour;
  1446. end;
  1447. end;
  1448. procedure trgobj.colour_registers;
  1449. begin
  1450. repeat
  1451. if simplifyworklist.length<>0 then
  1452. simplify
  1453. else if not(worklist_moves.empty) then
  1454. coalesce
  1455. else if freezeworklist.length<>0 then
  1456. freeze
  1457. else if spillworklist.length<>0 then
  1458. select_spill;
  1459. until (simplifyworklist.length=0) and
  1460. worklist_moves.empty and
  1461. (freezeworklist.length=0) and
  1462. (spillworklist.length=0);
  1463. assign_colours;
  1464. end;
  1465. procedure trgobj.epilogue_colouring;
  1466. begin
  1467. { remove all items from the worklists, but do not free them, they are still needed for spill coalesce }
  1468. move_garbage.concatList(worklist_moves);
  1469. move_garbage.concatList(active_moves);
  1470. active_moves.Free;
  1471. active_moves:=nil;
  1472. move_garbage.concatList(frozen_moves);
  1473. frozen_moves.Free;
  1474. frozen_moves:=nil;
  1475. move_garbage.concatList(coalesced_moves);
  1476. coalesced_moves.Free;
  1477. coalesced_moves:=nil;
  1478. move_garbage.concatList(constrained_moves);
  1479. constrained_moves.Free;
  1480. constrained_moves:=nil;
  1481. end;
  1482. procedure trgobj.clear_interferences(u:Tsuperregister);
  1483. {Remove node u from the interference graph and remove all collected
  1484. move instructions it is associated with.}
  1485. var i : word;
  1486. v : Tsuperregister;
  1487. adj,adj2 : Psuperregisterworklist;
  1488. begin
  1489. adj:=reginfo[u].adjlist;
  1490. if adj<>nil then
  1491. begin
  1492. for i:=1 to adj^.length do
  1493. begin
  1494. v:=adj^.buf^[i-1];
  1495. {Remove (u,v) and (v,u) from bitmap.}
  1496. ibitmap[u,v]:=false;
  1497. ibitmap[v,u]:=false;
  1498. {Remove (v,u) from adjacency list.}
  1499. adj2:=reginfo[v].adjlist;
  1500. if adj2<>nil then
  1501. begin
  1502. adj2^.delete(u);
  1503. if adj2^.length=0 then
  1504. begin
  1505. dispose(adj2,done);
  1506. reginfo[v].adjlist:=nil;
  1507. end;
  1508. end;
  1509. end;
  1510. {Remove ( u,* ) from adjacency list.}
  1511. dispose(adj,done);
  1512. reginfo[u].adjlist:=nil;
  1513. end;
  1514. end;
  1515. function trgobj.getregisterinline(list:TAsmList;const subregconstraints:Tsubregisterset):Tregister;
  1516. var
  1517. p : Tsuperregister;
  1518. subreg: tsubregister;
  1519. begin
  1520. for subreg:=high(tsubregister) downto low(tsubregister) do
  1521. if subreg in subregconstraints then
  1522. break;
  1523. p:=getnewreg(subreg);
  1524. live_registers.add(p);
  1525. result:=newreg(regtype,p,subreg);
  1526. add_edges_used(p);
  1527. add_constraints(result);
  1528. { also add constraints for other sizes used for this register }
  1529. if subreg<>low(tsubregister) then
  1530. for subreg:=pred(subreg) downto low(tsubregister) do
  1531. if subreg in subregconstraints then
  1532. add_constraints(newreg(regtype,getsupreg(result),subreg));
  1533. end;
  1534. procedure trgobj.ungetregisterinline(list:TAsmList;r:Tregister);
  1535. var
  1536. supreg:Tsuperregister;
  1537. begin
  1538. supreg:=getsupreg(r);
  1539. live_registers.delete(supreg);
  1540. insert_regalloc_info(list,supreg);
  1541. end;
  1542. procedure trgobj.insert_regalloc_info(list:TAsmList;u:tsuperregister);
  1543. var
  1544. p : tai;
  1545. r : tregister;
  1546. palloc,
  1547. pdealloc : tai_regalloc;
  1548. begin
  1549. { Insert regallocs for all imaginary registers }
  1550. with reginfo[u] do
  1551. begin
  1552. r:=newreg(regtype,u,subreg);
  1553. if assigned(live_start) then
  1554. begin
  1555. { Generate regalloc and bind it to an instruction, this
  1556. is needed to find all live registers belonging to an
  1557. instruction during the spilling }
  1558. if live_start.typ=ait_instruction then
  1559. palloc:=tai_regalloc.alloc(r,live_start)
  1560. else
  1561. palloc:=tai_regalloc.alloc(r,nil);
  1562. if live_end.typ=ait_instruction then
  1563. pdealloc:=tai_regalloc.dealloc(r,live_end)
  1564. else
  1565. pdealloc:=tai_regalloc.dealloc(r,nil);
  1566. { Insert live start allocation before the instruction/reg_a_sync }
  1567. list.insertbefore(palloc,live_start);
  1568. { Insert live end deallocation before reg allocations
  1569. to reduce conflicts }
  1570. p:=live_end;
  1571. while assigned(p) and
  1572. assigned(p.previous) and
  1573. (tai(p.previous).typ=ait_regalloc) and
  1574. (tai_regalloc(p.previous).ratype=ra_alloc) and
  1575. (tai_regalloc(p.previous).reg<>r) do
  1576. p:=tai(p.previous);
  1577. { , but add release after a reg_a_sync }
  1578. if assigned(p) and
  1579. (p.typ=ait_regalloc) and
  1580. (tai_regalloc(p).ratype=ra_sync) then
  1581. p:=tai(p.next);
  1582. if assigned(p) then
  1583. list.insertbefore(pdealloc,p)
  1584. else
  1585. list.concat(pdealloc);
  1586. end;
  1587. end;
  1588. end;
  1589. procedure trgobj.insert_regalloc_info_all(list:TAsmList);
  1590. var
  1591. supreg : tsuperregister;
  1592. begin
  1593. { Insert regallocs for all imaginary registers }
  1594. for supreg:=first_imaginary to maxreg-1 do
  1595. insert_regalloc_info(list,supreg);
  1596. end;
  1597. procedure trgobj.determine_spill_registers(list: TAsmList; headertail: tai);
  1598. begin
  1599. prepare_colouring;
  1600. colour_registers;
  1601. epilogue_colouring;
  1602. end;
  1603. procedure trgobj.get_spill_temp(list: TAsmlist; spill_temps: Pspill_temp_list; supreg: tsuperregister);
  1604. var
  1605. size: ptrint;
  1606. begin
  1607. {Get a temp for the spilled register, the size must at least equal a complete register,
  1608. take also care of the fact that subreg can be larger than a single register like doubles
  1609. that occupy 2 registers }
  1610. { only force the whole register in case of integers. Storing a register that contains
  1611. a single precision value as a double can cause conversion errors on e.g. ARM VFP }
  1612. if (regtype=R_INTREGISTER) then
  1613. size:=max(tcgsize2size[reg_cgsize(newreg(regtype,supreg,R_SUBWHOLE))],
  1614. tcgsize2size[reg_cgsize(newreg(regtype,supreg,reginfo[supreg].subreg))])
  1615. else
  1616. size:=tcgsize2size[reg_cgsize(newreg(regtype,supreg,reginfo[supreg].subreg))];
  1617. tg.gettemp(list,
  1618. size,size,
  1619. tt_noreuse,spill_temps^[supreg]);
  1620. end;
  1621. procedure trgobj.add_cpu_interferences(p : tai);
  1622. begin
  1623. end;
  1624. procedure trgobj.generate_interference_graph(list:TAsmList;headertai:tai);
  1625. procedure RecordUse(var r : Treginfo);
  1626. begin
  1627. inc(r.total_interferences,live_registers.length);
  1628. inc(r.count_uses);
  1629. end;
  1630. var
  1631. p : tai;
  1632. i : integer;
  1633. supreg, u: tsuperregister;
  1634. {$ifdef arm}
  1635. so: pshifterop;
  1636. {$endif arm}
  1637. begin
  1638. { All allocations are available. Now we can generate the
  1639. interference graph. Walk through all instructions, we can
  1640. start with the headertai, because before the header tai is
  1641. only symbols. }
  1642. live_registers.clear;
  1643. p:=headertai;
  1644. while assigned(p) do
  1645. begin
  1646. prefetch(pointer(p.next)^);
  1647. case p.typ of
  1648. ait_instruction:
  1649. with Taicpu(p) do
  1650. begin
  1651. current_filepos:=fileinfo;
  1652. {For speed reasons, get_alias isn't used here, instead,
  1653. assign_colours will also set the colour of coalesced nodes.
  1654. If there are registers with colour=0, then the coalescednodes
  1655. list probably doesn't contain these registers, causing
  1656. assign_colours not to do this properly.}
  1657. for i:=0 to ops-1 do
  1658. with oper[i]^ do
  1659. case typ of
  1660. top_reg:
  1661. if (getregtype(reg)=regtype) then
  1662. begin
  1663. u:=getsupreg(reg);
  1664. {$ifdef EXTDEBUG}
  1665. if (u>=maxreginfo) then
  1666. internalerror(2018111701);
  1667. {$endif}
  1668. RecordUse(reginfo[u]);
  1669. end;
  1670. top_ref:
  1671. begin
  1672. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1673. with ref^ do
  1674. begin
  1675. if (base<>NR_NO) and
  1676. (getregtype(base)=regtype) then
  1677. begin
  1678. u:=getsupreg(base);
  1679. {$ifdef EXTDEBUG}
  1680. if (u>=maxreginfo) then
  1681. internalerror(2018111702);
  1682. {$endif}
  1683. RecordUse(reginfo[u]);
  1684. end;
  1685. if (index<>NR_NO) and
  1686. (getregtype(index)=regtype) then
  1687. begin
  1688. u:=getsupreg(index);
  1689. {$ifdef EXTDEBUG}
  1690. if (u>=maxreginfo) then
  1691. internalerror(2018111703);
  1692. {$endif}
  1693. RecordUse(reginfo[u]);
  1694. end;
  1695. {$if defined(x86)}
  1696. if (segment<>NR_NO) and
  1697. (getregtype(segment)=regtype) then
  1698. begin
  1699. u:=getsupreg(segment);
  1700. {$ifdef EXTDEBUG}
  1701. if (u>=maxreginfo) then
  1702. internalerror(2018111704);
  1703. {$endif}
  1704. RecordUse(reginfo[u]);
  1705. end;
  1706. {$endif defined(x86)}
  1707. end;
  1708. end;
  1709. {$ifdef arm}
  1710. Top_shifterop:
  1711. begin
  1712. if regtype=R_INTREGISTER then
  1713. begin
  1714. so:=shifterop;
  1715. if (so^.rs<>NR_NO) and
  1716. (getregtype(so^.rs)=regtype) then
  1717. RecordUse(reginfo[getsupreg(so^.rs)]);
  1718. end;
  1719. end;
  1720. {$endif arm}
  1721. else
  1722. ;
  1723. end;
  1724. end;
  1725. ait_regalloc:
  1726. with Tai_regalloc(p) do
  1727. begin
  1728. if (getregtype(reg)=regtype) then
  1729. begin
  1730. supreg:=getsupreg(reg);
  1731. case ratype of
  1732. ra_alloc :
  1733. begin
  1734. live_registers.add(supreg);
  1735. {$ifdef DEBUG_REGISTERLIFE}
  1736. write(live_registers.length,' ');
  1737. for i:=0 to live_registers.length-1 do
  1738. write(std_regname(newreg(regtype,live_registers.buf^[i],defaultsub)),' ');
  1739. writeln;
  1740. {$endif DEBUG_REGISTERLIFE}
  1741. add_edges_used(supreg);
  1742. end;
  1743. ra_dealloc :
  1744. begin
  1745. live_registers.delete(supreg);
  1746. {$ifdef DEBUG_REGISTERLIFE}
  1747. write(live_registers.length,' ');
  1748. for i:=0 to live_registers.length-1 do
  1749. write(std_regname(newreg(regtype,live_registers.buf^[i],defaultsub)),' ');
  1750. writeln;
  1751. {$endif DEBUG_REGISTERLIFE}
  1752. add_edges_used(supreg);
  1753. end;
  1754. ra_markused :
  1755. if (supreg<first_imaginary) then
  1756. begin
  1757. include(used_in_proc,supreg);
  1758. has_usedmarks:=true;
  1759. end;
  1760. else
  1761. ;
  1762. end;
  1763. { constraints needs always to be updated }
  1764. add_constraints(reg);
  1765. end;
  1766. end;
  1767. else
  1768. ;
  1769. end;
  1770. add_cpu_interferences(p);
  1771. p:=Tai(p.next);
  1772. end;
  1773. {$ifdef EXTDEBUG}
  1774. if live_registers.length>0 then
  1775. begin
  1776. for i:=0 to live_registers.length-1 do
  1777. begin
  1778. { Only report for imaginary registers }
  1779. if live_registers.buf^[i]>=first_imaginary then
  1780. Comment(V_Warning,'Register '+std_regname(newreg(regtype,live_registers.buf^[i],defaultsub))+' not released');
  1781. end;
  1782. end;
  1783. {$endif}
  1784. end;
  1785. procedure trgobj.translate_register(var reg : tregister);
  1786. begin
  1787. if (getregtype(reg)=regtype) then
  1788. setsupreg(reg,reginfo[getsupreg(reg)].colour)
  1789. else
  1790. internalerror(200602021);
  1791. end;
  1792. procedure Trgobj.translate_registers(list:TAsmList);
  1793. var
  1794. hp,p,q:Tai;
  1795. i:shortint;
  1796. u:longint;
  1797. {$ifdef arm}
  1798. so:pshifterop;
  1799. {$endif arm}
  1800. begin
  1801. { Leave when no imaginary registers are used }
  1802. if maxreg<=first_imaginary then
  1803. exit;
  1804. p:=Tai(list.first);
  1805. while assigned(p) do
  1806. begin
  1807. prefetch(pointer(p.next)^);
  1808. case p.typ of
  1809. ait_regalloc:
  1810. with Tai_regalloc(p) do
  1811. begin
  1812. if (getregtype(reg)=regtype) then
  1813. begin
  1814. { Only alloc/dealloc is needed for the optimizer, remove
  1815. other regalloc }
  1816. if not(ratype in [ra_alloc,ra_dealloc]) then
  1817. begin
  1818. q:=Tai(next);
  1819. list.remove(p);
  1820. p.free;
  1821. p:=q;
  1822. continue;
  1823. end
  1824. else
  1825. begin
  1826. u:=reginfo[getsupreg(reg)].colour;
  1827. include(used_in_proc,u);
  1828. {$ifdef EXTDEBUG}
  1829. if u>=maxreginfo then
  1830. internalerror(2015040501);
  1831. {$endif}
  1832. setsupreg(reg,u);
  1833. end;
  1834. end;
  1835. end;
  1836. ait_varloc:
  1837. begin
  1838. if (getregtype(tai_varloc(p).newlocation)=regtype) then
  1839. begin
  1840. if (cs_asm_source in current_settings.globalswitches) then
  1841. begin
  1842. setsupreg(tai_varloc(p).newlocation,reginfo[getsupreg(tai_varloc(p).newlocation)].colour);
  1843. if tai_varloc(p).newlocationhi<>NR_NO then
  1844. begin
  1845. setsupreg(tai_varloc(p).newlocationhi,reginfo[getsupreg(tai_varloc(p).newlocationhi)].colour);
  1846. hp:=Tai_comment.Create(strpnew('Var '+tai_varloc(p).varsym.realname+' located in register '+
  1847. std_regname(tai_varloc(p).newlocationhi)+':'+std_regname(tai_varloc(p).newlocation)));
  1848. end
  1849. else
  1850. hp:=Tai_comment.Create(strpnew('Var '+tai_varloc(p).varsym.realname+' located in register '+
  1851. std_regname(tai_varloc(p).newlocation)));
  1852. list.insertafter(hp,p);
  1853. end;
  1854. q:=tai(p.next);
  1855. list.remove(p);
  1856. p.free;
  1857. p:=q;
  1858. continue;
  1859. end;
  1860. end;
  1861. ait_instruction:
  1862. with Taicpu(p) do
  1863. begin
  1864. current_filepos:=fileinfo;
  1865. {For speed reasons, get_alias isn't used here, instead,
  1866. assign_colours will also set the colour of coalesced nodes.
  1867. If there are registers with colour=0, then the coalescednodes
  1868. list probably doesn't contain these registers, causing
  1869. assign_colours not to do this properly.}
  1870. for i:=0 to ops-1 do
  1871. with oper[i]^ do
  1872. case typ of
  1873. Top_reg:
  1874. if (getregtype(reg)=regtype) then
  1875. begin
  1876. u:=getsupreg(reg);
  1877. {$ifdef EXTDEBUG}
  1878. if (u>=maxreginfo) then
  1879. internalerror(2012101903);
  1880. {$endif}
  1881. setsupreg(reg,reginfo[u].colour);
  1882. end;
  1883. Top_ref:
  1884. begin
  1885. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1886. with ref^ do
  1887. begin
  1888. if (base<>NR_NO) and
  1889. (getregtype(base)=regtype) then
  1890. begin
  1891. u:=getsupreg(base);
  1892. {$ifdef EXTDEBUG}
  1893. if (u>=maxreginfo) then
  1894. internalerror(2012101904);
  1895. {$endif}
  1896. setsupreg(base,reginfo[u].colour);
  1897. end;
  1898. if (index<>NR_NO) and
  1899. (getregtype(index)=regtype) then
  1900. begin
  1901. u:=getsupreg(index);
  1902. {$ifdef EXTDEBUG}
  1903. if (u>=maxreginfo) then
  1904. internalerror(2012101905);
  1905. {$endif}
  1906. setsupreg(index,reginfo[u].colour);
  1907. end;
  1908. {$if defined(x86)}
  1909. if (segment<>NR_NO) and
  1910. (getregtype(segment)=regtype) then
  1911. begin
  1912. u:=getsupreg(segment);
  1913. {$ifdef EXTDEBUG}
  1914. if (u>=maxreginfo) then
  1915. internalerror(2013052401);
  1916. {$endif}
  1917. setsupreg(segment,reginfo[u].colour);
  1918. end;
  1919. {$endif defined(x86)}
  1920. end;
  1921. end;
  1922. {$ifdef arm}
  1923. Top_shifterop:
  1924. begin
  1925. if regtype=R_INTREGISTER then
  1926. begin
  1927. so:=shifterop;
  1928. if (so^.rs<>NR_NO) and
  1929. (getregtype(so^.rs)=regtype) then
  1930. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1931. end;
  1932. end;
  1933. {$endif arm}
  1934. else
  1935. ;
  1936. end;
  1937. { Maybe the operation can be removed when
  1938. it is a move and both arguments are the same }
  1939. if is_same_reg_move(regtype) then
  1940. begin
  1941. q:=Tai(p.next);
  1942. list.remove(p);
  1943. p.free;
  1944. p:=q;
  1945. continue;
  1946. end;
  1947. end;
  1948. else
  1949. ;
  1950. end;
  1951. p:=Tai(p.next);
  1952. end;
  1953. current_filepos:=current_procinfo.exitpos;
  1954. end;
  1955. function trgobj.spill_registers(list:TAsmList;headertai:tai):boolean;
  1956. { Returns true if any help registers have been used }
  1957. var
  1958. i : cardinal;
  1959. t : tsuperregister;
  1960. p,q : Tai;
  1961. regs_to_spill_set:Tsuperregisterset;
  1962. spill_temps : ^Tspill_temp_list;
  1963. supreg,x,y : tsuperregister;
  1964. templist : TAsmList;
  1965. j : Longint;
  1966. getnewspillloc : Boolean;
  1967. begin
  1968. spill_registers:=false;
  1969. live_registers.clear;
  1970. { spilling should start with the node with the highest number of interferences, so we can coalesce as
  1971. much as possible spilled nodes (coalesce in case of spilled node means they share the same memory location) }
  1972. sort_spillednodes;
  1973. for i:=first_imaginary to maxreg-1 do
  1974. exclude(reginfo[i].flags,ri_selected);
  1975. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1976. supregset_reset(regs_to_spill_set,false,$ffff);
  1977. {$ifdef DEBUG_SPILLCOALESCE}
  1978. writeln('trgobj.spill_registers: Got maxreg ',maxreg);
  1979. writeln('trgobj.spill_registers: Spilling ',spillednodes.length,' nodes');
  1980. {$endif DEBUG_SPILLCOALESCE}
  1981. { after each round of spilling, more registers could be used due to allocations for spilling }
  1982. if Length(spillinfo)<maxreg then
  1983. begin
  1984. j:=Length(spillinfo);
  1985. SetLength(spillinfo,maxreg);
  1986. fillchar(spillinfo[j],sizeof(spillinfo[0])*(Length(spillinfo)-j),0);
  1987. end;
  1988. { Allocate temps and insert in front of the list }
  1989. templist:=TAsmList.create;
  1990. { Safe: this procedure is only called if there are spilled nodes. }
  1991. with spillednodes do
  1992. { the node with the highest interferences is the last one }
  1993. for i:=length-1 downto 0 do
  1994. begin
  1995. t:=buf^[i];
  1996. {$ifdef DEBUG_SPILLCOALESCE}
  1997. writeln('trgobj.spill_registers: Spilling ',t);
  1998. {$endif DEBUG_SPILLCOALESCE}
  1999. spillinfo[t].interferences:=Tinterferencebitmap.create;
  2000. { copy interferences }
  2001. for j:=0 to maxreg-1 do
  2002. spillinfo[t].interferences[0,j]:=ibitmap[t,j];
  2003. { Alternative representation. }
  2004. supregset_include(regs_to_spill_set,t);
  2005. { Clear all interferences of the spilled register. }
  2006. clear_interferences(t);
  2007. getnewspillloc:=true;
  2008. { check if we can "coalesce" spilled nodes. To do so, it is required that they do not
  2009. interfere but are connected by a move instruction
  2010. doing so might save some mem->mem moves }
  2011. if (cs_opt_level3 in current_settings.optimizerswitches) and assigned(reginfo[t].movelist) then
  2012. for j:=0 to reginfo[t].movelist^.header.count-1 do
  2013. begin
  2014. x:=Tmoveins(reginfo[t].movelist^.data[j]).x;
  2015. y:=Tmoveins(reginfo[t].movelist^.data[j]).y;
  2016. if (x=t) and
  2017. (spillinfo[get_alias(y)].spilled) and
  2018. not(spillinfo[get_alias(y)].interferences[0,t]) then
  2019. begin
  2020. spill_temps^[t]:=spillinfo[get_alias(y)].spilllocation;
  2021. {$ifdef DEBUG_SPILLCOALESCE}
  2022. writeln('trgobj.spill_registers: Spill coalesce ',t,' to ',y);
  2023. {$endif DEBUG_SPILLCOALESCE}
  2024. getnewspillloc:=false;
  2025. break;
  2026. end
  2027. else if (y=t) and
  2028. (spillinfo[get_alias(x)].spilled) and
  2029. not(spillinfo[get_alias(x)].interferences[0,t]) then
  2030. begin
  2031. {$ifdef DEBUG_SPILLCOALESCE}
  2032. writeln('trgobj.spill_registers: Spill coalesce ',t,' to ',x);
  2033. {$endif DEBUG_SPILLCOALESCE}
  2034. spill_temps^[t]:=spillinfo[get_alias(x)].spilllocation;
  2035. getnewspillloc:=false;
  2036. break;
  2037. end;
  2038. end;
  2039. if getnewspillloc then
  2040. get_spill_temp(templist,spill_temps,t);
  2041. {$ifdef DEBUG_SPILLCOALESCE}
  2042. writeln('trgobj.spill_registers: Spill temp: ',getsupreg(spill_temps^[t].base),'+',spill_temps^[t].offset);
  2043. {$endif DEBUG_SPILLCOALESCE}
  2044. { set spilled only as soon as a temp is assigned, else a mov iregX,iregX results in a spill coalesce with itself }
  2045. spillinfo[t].spilled:=true;
  2046. spillinfo[t].spilllocation:=spill_temps^[t];
  2047. end;
  2048. list.insertlistafter(headertai,templist);
  2049. templist.free;
  2050. { Walk through all instructions, we can start with the headertai,
  2051. because before the header tai is only symbols }
  2052. p:=headertai;
  2053. while assigned(p) do
  2054. begin
  2055. case p.typ of
  2056. ait_regalloc:
  2057. with Tai_regalloc(p) do
  2058. begin
  2059. if (getregtype(reg)=regtype) then
  2060. begin
  2061. {A register allocation of a spilled register can be removed.}
  2062. supreg:=getsupreg(reg);
  2063. if supregset_in(regs_to_spill_set,supreg) then
  2064. begin
  2065. q:=Tai(p.next);
  2066. list.remove(p);
  2067. p.free;
  2068. p:=q;
  2069. continue;
  2070. end
  2071. else
  2072. begin
  2073. case ratype of
  2074. ra_alloc :
  2075. live_registers.add(supreg);
  2076. ra_dealloc :
  2077. live_registers.delete(supreg);
  2078. else
  2079. ;
  2080. end;
  2081. end;
  2082. end;
  2083. end;
  2084. {$ifdef llvm}
  2085. ait_llvmins,
  2086. {$endif llvm}
  2087. ait_instruction:
  2088. with tai_cpu_abstract_sym(p) do
  2089. begin
  2090. // writeln(gas_op2str[tai_cpu_abstract_sym(p).opcode]);
  2091. current_filepos:=fileinfo;
  2092. if instr_spill_register(list,tai_cpu_abstract_sym(p),regs_to_spill_set,spill_temps^) then
  2093. spill_registers:=true;
  2094. end;
  2095. else
  2096. ;
  2097. end;
  2098. p:=Tai(p.next);
  2099. end;
  2100. current_filepos:=current_procinfo.exitpos;
  2101. {Safe: this procedure is only called if there are spilled nodes.}
  2102. with spillednodes do
  2103. for i:=0 to length-1 do
  2104. tg.ungettemp(list,spill_temps^[buf^[i]]);
  2105. freemem(spill_temps);
  2106. end;
  2107. function trgobj.do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;
  2108. begin
  2109. result:=false;
  2110. end;
  2111. procedure trgobj.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister;orgsupreg:tsuperregister);
  2112. var
  2113. ins:tai_cpu_abstract_sym;
  2114. begin
  2115. ins:=spilling_create_load(spilltemp,tempreg);
  2116. add_cpu_interferences(ins);
  2117. list.insertafter(ins,pos);
  2118. {$ifdef DEBUG_SPILLING}
  2119. list.Insertbefore(tai_comment.Create(strpnew('Spilling: Spill Read')),ins);
  2120. {$endif}
  2121. end;
  2122. procedure Trgobj.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister;orgsupreg:tsuperregister);
  2123. var
  2124. ins:tai_cpu_abstract_sym;
  2125. begin
  2126. ins:=spilling_create_store(tempreg,spilltemp);
  2127. add_cpu_interferences(ins);
  2128. list.insertafter(ins,pos);
  2129. {$ifdef DEBUG_SPILLING}
  2130. list.Insertbefore(tai_comment.Create(strpnew('Spilling: Spill Write')),ins);
  2131. {$endif}
  2132. end;
  2133. function trgobj.get_spill_subreg(r : tregister) : tsubregister;
  2134. begin
  2135. result:=defaultsub;
  2136. end;
  2137. function trgobj.addreginfo(var regs: tspillregsinfo; const r: tsuperregisterset; reg: tregister; operation: topertype): boolean;
  2138. var
  2139. i, tmpindex: longint;
  2140. supreg: tsuperregister;
  2141. begin
  2142. result:=false;
  2143. tmpindex := regs.reginfocount;
  2144. supreg := get_alias(getsupreg(reg));
  2145. { did we already encounter this register? }
  2146. for i := 0 to pred(regs.reginfocount) do
  2147. if (regs.reginfo[i].orgreg = supreg) then
  2148. begin
  2149. tmpindex := i;
  2150. break;
  2151. end;
  2152. if tmpindex > high(regs.reginfo) then
  2153. internalerror(2003120301);
  2154. regs.reginfo[tmpindex].orgreg := supreg;
  2155. include(regs.reginfo[tmpindex].spillregconstraints,get_spill_subreg(reg));
  2156. if supregset_in(r,supreg) then
  2157. begin
  2158. { add/update info on this register }
  2159. regs.reginfo[tmpindex].mustbespilled := true;
  2160. case operation of
  2161. operand_read:
  2162. regs.reginfo[tmpindex].regread := true;
  2163. operand_write:
  2164. regs.reginfo[tmpindex].regwritten := true;
  2165. operand_readwrite:
  2166. begin
  2167. regs.reginfo[tmpindex].regread := true;
  2168. regs.reginfo[tmpindex].regwritten := true;
  2169. end;
  2170. end;
  2171. result:=true;
  2172. end;
  2173. inc(regs.reginfocount,ord(regs.reginfocount=tmpindex));
  2174. end;
  2175. function trgobj.instr_get_oper_spilling_info(var regs: tspillregsinfo; const r: tsuperregisterset; instr: tai_cpu_abstract_sym; opidx: longint): boolean;
  2176. begin
  2177. result:=false;
  2178. with instr.oper[opidx]^ do
  2179. begin
  2180. case typ of
  2181. top_reg:
  2182. begin
  2183. if (getregtype(reg) = regtype) then
  2184. result:=addreginfo(regs,r,reg,instr.spilling_get_operation_type(opidx));
  2185. end;
  2186. top_ref:
  2187. begin
  2188. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  2189. with ref^ do
  2190. begin
  2191. if (base <> NR_NO) and
  2192. (getregtype(base)=regtype) then
  2193. result:=addreginfo(regs,r,base,instr.spilling_get_operation_type_ref(opidx,base));
  2194. if (index <> NR_NO) and
  2195. (getregtype(index)=regtype) then
  2196. result:=addreginfo(regs,r,index,instr.spilling_get_operation_type_ref(opidx,index)) or result;
  2197. {$if defined(x86)}
  2198. if (segment <> NR_NO) and
  2199. (getregtype(segment)=regtype) then
  2200. result:=addreginfo(regs,r,segment,instr.spilling_get_operation_type_ref(opidx,segment)) or result;
  2201. {$endif defined(x86)}
  2202. end;
  2203. end;
  2204. {$ifdef ARM}
  2205. top_shifterop:
  2206. begin
  2207. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  2208. if shifterop^.rs<>NR_NO then
  2209. result:=addreginfo(regs,r,shifterop^.rs,operand_read);
  2210. end;
  2211. {$endif ARM}
  2212. else
  2213. ;
  2214. end;
  2215. end;
  2216. end;
  2217. procedure trgobj.try_replace_reg(const regs: tspillregsinfo; var reg: tregister; useloadreg: boolean);
  2218. var
  2219. i: longint;
  2220. supreg: tsuperregister;
  2221. begin
  2222. supreg:=get_alias(getsupreg(reg));
  2223. for i:=0 to pred(regs.reginfocount) do
  2224. if (regs.reginfo[i].mustbespilled) and
  2225. (regs.reginfo[i].orgreg=supreg) then
  2226. begin
  2227. { Only replace supreg }
  2228. if useloadreg then
  2229. setsupreg(reg, getsupreg(regs.reginfo[i].loadreg))
  2230. else
  2231. setsupreg(reg, getsupreg(regs.reginfo[i].storereg));
  2232. break;
  2233. end;
  2234. end;
  2235. procedure trgobj.substitute_spilled_registers(const regs: tspillregsinfo; instr: tai_cpu_abstract_sym; opidx: longint);
  2236. begin
  2237. with instr.oper[opidx]^ do
  2238. case typ of
  2239. top_reg:
  2240. begin
  2241. if (getregtype(reg) = regtype) then
  2242. try_replace_reg(regs, reg, not ssa_safe or
  2243. (instr.spilling_get_operation_type(opidx)=operand_read));
  2244. end;
  2245. top_ref:
  2246. begin
  2247. if regtype in [R_INTREGISTER, R_ADDRESSREGISTER] then
  2248. begin
  2249. if (ref^.base <> NR_NO) and
  2250. (getregtype(ref^.base)=regtype) then
  2251. try_replace_reg(regs, ref^.base,
  2252. not ssa_safe or (instr.spilling_get_operation_type_ref(opidx, ref^.base)=operand_read));
  2253. if (ref^.index <> NR_NO) and
  2254. (getregtype(ref^.index)=regtype) then
  2255. try_replace_reg(regs, ref^.index,
  2256. not ssa_safe or (instr.spilling_get_operation_type_ref(opidx, ref^.index)=operand_read));
  2257. {$if defined(x86)}
  2258. if (ref^.segment <> NR_NO) and
  2259. (getregtype(ref^.segment)=regtype) then
  2260. try_replace_reg(regs, ref^.segment, true { always read-only });
  2261. {$endif defined(x86)}
  2262. end;
  2263. end;
  2264. {$ifdef ARM}
  2265. top_shifterop:
  2266. begin
  2267. if regtype in [R_INTREGISTER, R_ADDRESSREGISTER] then
  2268. try_replace_reg(regs, shifterop^.rs, true { always read-only });
  2269. end;
  2270. {$endif ARM}
  2271. else
  2272. ;
  2273. end;
  2274. end;
  2275. function trgobj.instr_spill_register(list:TAsmList;
  2276. instr:tai_cpu_abstract_sym;
  2277. const r:Tsuperregisterset;
  2278. const spilltemplist:Tspill_temp_list): boolean;
  2279. var
  2280. counter: longint;
  2281. regs: tspillregsinfo;
  2282. spilled: boolean;
  2283. var
  2284. loadpos,
  2285. storepos : tai;
  2286. oldlive_registers : tsuperregisterworklist;
  2287. begin
  2288. result := false;
  2289. fillchar(regs,sizeof(regs),0);
  2290. for counter := low(regs.reginfo) to high(regs.reginfo) do
  2291. begin
  2292. regs.reginfo[counter].orgreg := RS_INVALID;
  2293. regs.reginfo[counter].loadreg := NR_INVALID;
  2294. regs.reginfo[counter].storereg := NR_INVALID;
  2295. end;
  2296. spilled := false;
  2297. { check whether and if so which and how (read/written) this instructions contains
  2298. registers that must be spilled }
  2299. for counter := 0 to instr.ops-1 do
  2300. spilled:=instr_get_oper_spilling_info(regs,r,instr,counter) or spilled;
  2301. { if no spilling for this instruction we can leave }
  2302. if not spilled then
  2303. exit;
  2304. {$if defined(x86) or defined(mips) or defined(sparcgen) or defined(arm) or defined(m68k)}
  2305. { Try replacing the register with the spilltemp. This is useful only
  2306. for the i386,x86_64 that support memory locations for several instructions
  2307. For non-x86 it is nevertheless possible to replace moves to/from the register
  2308. with loads/stores to spilltemp (Sergei) }
  2309. for counter := 0 to pred(regs.reginfocount) do
  2310. with regs.reginfo[counter] do
  2311. begin
  2312. if mustbespilled then
  2313. begin
  2314. if do_spill_replace(list,instr,orgreg,spilltemplist[orgreg]) then
  2315. mustbespilled:=false;
  2316. end;
  2317. end;
  2318. {$endif defined(x86) or defined(mips) or defined(sparcgen) or defined(arm) or defined(m68k)}
  2319. {
  2320. There are registers that need are spilled. We generate the
  2321. following code for it. The used positions where code need
  2322. to be inserted are marked using #. Note that code is always inserted
  2323. before the positions using pos.previous. This way the position is always
  2324. the same since pos doesn't change, but pos.previous is modified everytime
  2325. new code is inserted.
  2326. [
  2327. - reg_allocs load spills
  2328. - load spills
  2329. ]
  2330. [#loadpos
  2331. - reg_deallocs
  2332. - reg_allocs
  2333. ]
  2334. [
  2335. - reg_deallocs for load-only spills
  2336. - reg_allocs for store-only spills
  2337. ]
  2338. [#instr
  2339. - original instruction
  2340. ]
  2341. [
  2342. - store spills
  2343. - reg_deallocs store spills
  2344. ]
  2345. [#storepos
  2346. ]
  2347. }
  2348. result := true;
  2349. oldlive_registers.copyfrom(live_registers);
  2350. { Process all tai_regallocs belonging to this instruction, ignore explicit
  2351. inserted regallocs. These can happend for example in i386:
  2352. mov ref,ireg26
  2353. <regdealloc ireg26, instr=taicpu of lea>
  2354. <regalloc edi, insrt=nil>
  2355. lea [ireg26+ireg17],edi
  2356. All released registers are also added to the live_registers because
  2357. they can't be used during the spilling }
  2358. loadpos:=tai(instr.previous);
  2359. while assigned(loadpos) and
  2360. (loadpos.typ=ait_regalloc) and
  2361. ((tai_regalloc(loadpos).instr=nil) or
  2362. (tai_regalloc(loadpos).instr=instr)) do
  2363. begin
  2364. { Only add deallocs belonging to the instruction. Explicit inserted deallocs
  2365. belong to the previous instruction and not the current instruction }
  2366. if (tai_regalloc(loadpos).instr=instr) and
  2367. (tai_regalloc(loadpos).ratype=ra_dealloc) then
  2368. live_registers.add(getsupreg(tai_regalloc(loadpos).reg));
  2369. loadpos:=tai(loadpos.previous);
  2370. end;
  2371. loadpos:=tai(loadpos.next);
  2372. { Load the spilled registers }
  2373. for counter := 0 to pred(regs.reginfocount) do
  2374. with regs.reginfo[counter] do
  2375. begin
  2376. if mustbespilled and regread then
  2377. begin
  2378. loadreg:=getregisterinline(list,regs.reginfo[counter].spillregconstraints);
  2379. do_spill_read(list,tai(loadpos.previous),spilltemplist[orgreg],loadreg,orgreg);
  2380. end;
  2381. end;
  2382. { Release temp registers of read-only registers, and add reference of the instruction
  2383. to the reginfo }
  2384. for counter := 0 to pred(regs.reginfocount) do
  2385. with regs.reginfo[counter] do
  2386. begin
  2387. if mustbespilled and regread and
  2388. (ssa_safe or
  2389. not regwritten) then
  2390. begin
  2391. { The original instruction will be the next that uses this register
  2392. set weigth of the newly allocated register higher than the old one,
  2393. so it will selected for spilling with a lower priority than
  2394. the original one, this prevents an endless spilling loop if orgreg
  2395. is short living, see e.g. tw25164.pp }
  2396. add_reg_instruction(instr,loadreg,reginfo[orgreg].weight+1);
  2397. ungetregisterinline(list,loadreg);
  2398. end;
  2399. end;
  2400. { Allocate temp registers of write-only registers, and add reference of the instruction
  2401. to the reginfo }
  2402. for counter := 0 to pred(regs.reginfocount) do
  2403. with regs.reginfo[counter] do
  2404. begin
  2405. if mustbespilled and regwritten then
  2406. begin
  2407. { When the register is also loaded there is already a register assigned }
  2408. if (not regread) or
  2409. ssa_safe then
  2410. begin
  2411. storereg:=getregisterinline(list,regs.reginfo[counter].spillregconstraints);
  2412. { we also use loadreg for store replacements in case we
  2413. don't have ensure ssa -> initialise loadreg even if
  2414. there are no reads }
  2415. if not regread then
  2416. loadreg:=storereg;
  2417. end
  2418. else
  2419. storereg:=loadreg;
  2420. { The original instruction will be the next that uses this register, this
  2421. also needs to be done for read-write registers,
  2422. set weigth of the newly allocated register higher than the old one,
  2423. so it will selected for spilling with a lower priority than
  2424. the original one, this prevents an endless spilling loop if orgreg
  2425. is short living, see e.g. tw25164.pp }
  2426. add_reg_instruction(instr,storereg,reginfo[orgreg].weight+1);
  2427. end;
  2428. end;
  2429. { store the spilled registers }
  2430. if not assigned(instr.next) then
  2431. list.concat(tai_marker.Create(mark_Position));
  2432. storepos:=tai(instr.next);
  2433. for counter := 0 to pred(regs.reginfocount) do
  2434. with regs.reginfo[counter] do
  2435. begin
  2436. if mustbespilled and regwritten then
  2437. begin
  2438. do_spill_written(list,tai(storepos.previous),spilltemplist[orgreg],storereg,orgreg);
  2439. ungetregisterinline(list,storereg);
  2440. end;
  2441. end;
  2442. { now all spilling code is generated we can restore the live registers. This
  2443. must be done after the store because the store can need an extra register
  2444. that also needs to conflict with the registers of the instruction }
  2445. live_registers.done;
  2446. live_registers:=oldlive_registers;
  2447. { substitute registers }
  2448. for counter:=0 to instr.ops-1 do
  2449. substitute_spilled_registers(regs,instr,counter);
  2450. { We have modified the instruction; perhaps the new instruction has
  2451. certain constraints regarding which imaginary registers interfere
  2452. with certain physical registers. }
  2453. add_cpu_interferences(instr);
  2454. end;
  2455. end.