rgobj.pas 84 KB

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