2
0

rgobj.pas 96 KB

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