rgobj.pas 84 KB

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