rgobj.pas 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the base class for the register allocator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. {# @abstract(Abstract register allocator unit)
  20. This unit contains services to allocate, free
  21. references and registers which are used by
  22. the code generator.
  23. }
  24. {*******************************************************************************
  25. (applies to new register allocator)
  26. Register allocator introduction.
  27. Free Pascal uses a Chaitin style register allocator. We use a variant similair
  28. to the one described in the book "Modern compiler implementation in C" by
  29. Andrew W. Appel., published by Cambridge University Press.
  30. The register allocator that is described by Appel uses a much improved way
  31. of register coalescing, called "iterated register coalescing". Instead
  32. of doing coalescing as a prepass to the register allocation, the coalescing
  33. is done inside the register allocator. This has the advantage that the
  34. register allocator can coalesce very aggresively without introducing spills.
  35. Reading this book is recommended for a complete understanding. Here is a small
  36. introduction.
  37. The code generator thinks it has an infinite amount of registers. Our processor
  38. has a limited amount of registers. Therefore we must reduce the amount of
  39. registers until there are less enough to fit into the processors registers.
  40. Registers can interfere or not interfere. If two imaginary registers interfere
  41. they cannot be placed into the same psysical register. Reduction of registers
  42. is done by:
  43. - "coalescing" Two registers that do not interfere are combined
  44. into one register.
  45. - "spilling" A register is changed into a memory location and the generated
  46. code is modified to use the memory location instead of the register.
  47. Register allocation is a graph colouring problem. Each register is a colour, and
  48. if two registers interfere there is a connection between them in the graph.
  49. In addition to the imaginary registers in the code generator, the psysical
  50. CPU registers are also present in this graph. This allows us to make
  51. interferences between imaginary registers and cpu registers. This is very
  52. usefull for describing archtectural constraints, like for example that
  53. the div instruction modifies edx, so variables that are in use at that time
  54. cannot be stored into edx. This can be modelled by making edx interfere
  55. with those variables.
  56. Graph colouring is an NP complete problem. Therefore we use an approximation
  57. that pushes registers to colour on to a stack. This is done in the "simplify"
  58. procedure.
  59. The register allocator first checks which registers are a candidate for
  60. coalescing.
  61. *******************************************************************************}
  62. unit rgobj;
  63. interface
  64. uses
  65. cutils, cpubase,
  66. cpuinfo,
  67. aasmbase,aasmtai,aasmcpu,
  68. cclasses,globtype,cginfo,cgbase,node
  69. {$ifdef delphi}
  70. ,dmisc
  71. {$endif}
  72. ;
  73. const
  74. ALL_OTHERREGISTERS=[firstreg..lastreg];
  75. type
  76. regvarother_longintarray = array[firstreg..lastreg] of longint;
  77. regvarother_booleanarray = array[firstreg..lastreg] of boolean;
  78. regvarint_longintarray = array[first_supreg..last_supreg] of longint;
  79. regvarint_ptreearray = array[first_supreg..last_supreg] of tnode;
  80. tpushedsavedloc = record
  81. case byte of
  82. 0: (pushed: boolean);
  83. 1: (ofs: longint);
  84. end;
  85. tpushedsavedother = array[firstreg..lastreg] of tpushedsavedloc;
  86. {$ifndef newra}
  87. Tpushedsavedint = array[first_supreg..last_supreg] of Tpushedsavedloc;
  88. {$endif}
  89. Tinterferencebitmap=array[Tsuperregister] of set of Tsuperregister;
  90. Tinterferenceadjlist=array[Tsuperregister] of Pstring;
  91. Tinterferencegraph=record
  92. bitmap:Tinterferencebitmap;
  93. adjlist:Tinterferenceadjlist;
  94. end;
  95. Pinterferencegraph=^Tinterferencegraph;
  96. Tmovelist=record
  97. count:cardinal;
  98. data:array[0..$ffff] of Tlinkedlistitem;
  99. end;
  100. Pmovelist=^Tmovelist;
  101. {In the register allocator we keep track of move instructions.
  102. These instructions are moved between five linked lists. There
  103. is also a linked list per register to keep track about the moves
  104. it is associated with. Because we need to determine quickly in
  105. which of the five lists it is we add anu enumeradtion to each
  106. move instruction.}
  107. Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
  108. ms_worklist_moves,ms_active_moves);
  109. Tmoveins=class(Tlinkedlistitem)
  110. moveset:Tmoveset;
  111. { $ifdef ra_debug}
  112. x,y:Tsuperregister;
  113. { $endif}
  114. instruction:Taicpu;
  115. end;
  116. {#
  117. This class implements the abstract register allocator
  118. It is used by the code generator to allocate and free
  119. registers which might be valid across nodes. It also
  120. contains utility routines related to registers.
  121. Some of the methods in this class should be overriden
  122. by cpu-specific implementations.
  123. }
  124. trgobj = class
  125. { The "usableregsxxx" contain all registers of type "xxx" that }
  126. { aren't currently allocated to a regvar. The "unusedregsxxx" }
  127. { contain all registers of type "xxx" that aren't currently }
  128. { allocated }
  129. lastintreg,maxintreg:Tsuperregister;
  130. unusedregsint,usableregsint:Tsupregset;
  131. unusedregsaddr,usableregsaddr:Tsupregset;
  132. unusedregsfpu,usableregsfpu : tregisterset;
  133. unusedregsmm,usableregsmm : tregisterset;
  134. { these counters contain the number of elements in the }
  135. { unusedregsxxx/usableregsxxx sets }
  136. {$ifndef newra}
  137. countunusedregsint,
  138. countunusedregsaddr,
  139. {$endif}
  140. countunusedregsfpu,
  141. countunusedregsmm : byte;
  142. countusableregsint,
  143. countusableregsaddr,
  144. countusableregsfpu,
  145. countusableregsmm : byte;
  146. { Contains the registers which are really used by the proc itself.
  147. It doesn't take care of registers used by called procedures
  148. }
  149. {$ifdef newra}
  150. savedintbyproc,
  151. {$endif}
  152. used_in_proc_int,
  153. usedaddrinproc : tsupregset;
  154. used_in_proc_other : tregisterset;
  155. reg_pushes_other : regvarother_longintarray;
  156. {$ifndef newra}
  157. reg_pushes_int : regvarint_longintarray;
  158. {$endif}
  159. is_reg_var_other : regvarother_booleanarray;
  160. is_reg_var_int : Tsupregset;
  161. regvar_loaded_other : regvarother_booleanarray;
  162. regvar_loaded_int : Tsupregset;
  163. {$ifdef newra}
  164. colour : array[Tsuperregister] of Tsuperregister;
  165. spillednodes : string;
  166. {$endif}
  167. { tries to hold the amount of times which the current tree is processed }
  168. t_times: longint;
  169. constructor create(Acpu_registers:byte);
  170. {# Allocate a general purpose register
  171. An internalerror will be generated if there
  172. is no more free registers which can be allocated
  173. }
  174. function getregisterint(list:Taasmoutput;size:Tcgsize):Tregister;{$ifndef newra}virtual;{$endif}
  175. {$ifdef newra}
  176. procedure add_constraints(reg:Tnewregister);virtual;
  177. {# Allocate an ABT register
  178. An internalerror will be generated if there
  179. is no more free registers which can be allocated
  180. An explanantion of abt registers can be found near the implementation.
  181. }
  182. function getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
  183. {$endif}
  184. {# Free a general purpose register
  185. @param(r register to free)
  186. }
  187. procedure ungetregisterint(list: taasmoutput; r : tregister); virtual;
  188. {# Allocate a floating point register
  189. An internalerror will be generated if there
  190. is no more free registers which can be allocated
  191. }
  192. function getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister; virtual;
  193. {# Free a floating point register
  194. @param(r register to free)
  195. }
  196. procedure ungetregisterfpu(list: taasmoutput; r : tregister;size:TCGsize); virtual;
  197. function getregistermm(list: taasmoutput) : tregister; virtual;
  198. procedure ungetregistermm(list: taasmoutput; r : tregister); virtual;
  199. {# Allocate an address register.
  200. Address registers are the only registers which can
  201. be used as a base register in references (treference).
  202. On most cpu's this is the same as a general purpose
  203. register.
  204. An internalerror will be generated if there
  205. is no more free registers which can be allocated
  206. }
  207. function getaddressregister(list:Taasmoutput):Tregister;virtual;
  208. procedure ungetaddressregister(list: taasmoutput; r: tregister); virtual;
  209. {# Verify if the specified register is an address or
  210. general purpose register. Returns TRUE if @var(reg)
  211. is an adress register.
  212. This routine should only be used to check on
  213. general purpose or address register. It will
  214. not work on multimedia or floating point
  215. registers
  216. @param(reg register to verify)
  217. }
  218. function isaddressregister(reg: tregister): boolean; virtual;
  219. {# Tries to allocate the passed register, if possible
  220. @param(r specific register to allocate)
  221. }
  222. function getexplicitregisterint(list:Taasmoutput;r:Tnewregister):Tregister;virtual;
  223. {# Tries to allocate the passed fpu register, if possible
  224. @param(r specific register to allocate)
  225. }
  226. function getexplicitregisterfpu(list : taasmoutput; r : Toldregister) : tregister;virtual;
  227. {$ifdef newra}
  228. procedure allocexplicitregistersint(list:Taasmoutput;r:Tsupregset);
  229. procedure deallocexplicitregistersint(list:Taasmoutput;r:Tsupregset);
  230. {$endif}
  231. {# Deallocate any kind of register }
  232. procedure ungetregister(list: taasmoutput; r : tregister); virtual;
  233. {# Deallocate all registers which are allocated
  234. in the specified reference. On most systems,
  235. this will free the base and index registers
  236. of the specified reference.
  237. @param(ref reference which must have its registers freed)
  238. }
  239. procedure ungetreference(list: taasmoutput; const ref : treference); virtual;
  240. {# Reset the register allocator information (usable registers etc).
  241. Please note that it is mortal sins to call cleartempgen during
  242. graph colouring (that is between prepare_colouring and
  243. epilogue_colouring).
  244. }
  245. procedure cleartempgen;virtual;
  246. {# Convert a register to a specified register size, and return that register size }
  247. function makeregsize(reg: tregister; size: tcgsize): tregister; virtual;
  248. {# saves register variables (restoring happens automatically) }
  249. {$ifndef newra}
  250. procedure saveintregvars(list:Taasmoutput;const s:Tsupregset);
  251. {$endif}
  252. procedure saveotherregvars(list:Taasmoutput;const s:Tregisterset);
  253. {# Saves in temporary references (allocated via the temp. allocator)
  254. the registers defined in @var(s). The registers are only saved
  255. if they are currently in use, otherwise they are left as is.
  256. On processors which have instructions which manipulate the stack,
  257. this routine should be overriden for performance reasons.
  258. @param(list) List to add the instruction to
  259. @param(saved) Array of saved register information
  260. @param(s) Registers which might require saving
  261. }
  262. {$ifndef newra}
  263. procedure saveusedintregisters(list:Taasmoutput;
  264. var saved:Tpushedsavedint;
  265. const s:Tsupregset);virtual;
  266. {$endif}
  267. procedure saveusedotherregisters(list:Taasmoutput;
  268. var saved:Tpushedsavedother;
  269. const s:Tregisterset);virtual;
  270. {# Restores the registers which were saved with a call
  271. to @var(saveusedregisters).
  272. On processors which have instructions which manipulate the stack,
  273. this routine should be overriden for performance reasons.
  274. }
  275. {$ifndef newra}
  276. procedure restoreusedintregisters(list:Taasmoutput;
  277. const saved:Tpushedsavedint);virtual;
  278. {$endif}
  279. procedure restoreusedotherregisters(list:Taasmoutput;
  280. const saved:Tpushedsavedother);virtual;
  281. { used when deciding which registers to use for regvars }
  282. {$ifndef newra}
  283. procedure incrementintregisterpushed(const s:Tsupregset);
  284. {$endif}
  285. procedure incrementotherregisterpushed(const s: tregisterset);
  286. procedure clearregistercount;
  287. procedure resetusableregisters;virtual;
  288. procedure makeregvarint(reg:Tsuperregister);
  289. procedure makeregvarother(reg:Tregister);
  290. procedure saveStateForInline(var state: pointer);virtual;
  291. procedure restoreStateAfterInline(var state: pointer);virtual;
  292. procedure saveUnusedState(var state: pointer);virtual;
  293. procedure restoreUnusedState(var state: pointer);virtual;
  294. {$ifdef newra}
  295. {$ifdef ra_debug2}
  296. procedure writegraph;
  297. {$endif}
  298. procedure add_move_instruction(instr:Taicpu);
  299. procedure prepare_colouring;
  300. procedure epilogue_colouring;
  301. procedure colour_registers;
  302. function spill_registers(list:Taasmoutput;const regs_to_spill:string):boolean;
  303. {$endif newra}
  304. protected
  305. cpu_registers:byte;
  306. {$ifdef newra}
  307. igraph:Tinterferencegraph;
  308. degree:array[0..255] of byte;
  309. alias:array[Tsuperregister] of Tsuperregister;
  310. simplifyworklist,freezeworklist,spillworklist:string;
  311. coalescednodes:string;
  312. selectstack:string;
  313. abtlist:string;
  314. movelist:array[Tsuperregister] of Pmovelist;
  315. worklist_moves,active_moves,frozen_moves,
  316. coalesced_moves,constrained_moves:Tlinkedlist;
  317. {$endif}
  318. { the following two contain the common (generic) code for all }
  319. { get- and ungetregisterxxx functions/procedures }
  320. function getregistergenother(list: taasmoutput; const lowreg, highreg: Toldregister;
  321. var unusedregs:Tregisterset;var countunusedregs:byte): tregister;
  322. function getregistergenint(list:Taasmoutput;subreg:Tsubregister;
  323. const lowreg,highreg:Tsuperregister;
  324. var fusedinproc,unusedregs:Tsupregset
  325. {$ifndef newra};var countunusedregs:byte{$endif}):Tregister;
  326. procedure ungetregistergen(list: taasmoutput; const r: tregister;
  327. const usableregs:tregisterset;var unusedregs: tregisterset; var countunusedregs: byte);
  328. procedure ungetregistergenint(list:taasmoutput;const r:Tregister;
  329. const usableregs:Tsupregset;
  330. var unusedregs:Tsupregset
  331. {$ifndef newra};var countunusedregs:byte{$endif});
  332. {$ifdef newra}
  333. procedure getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  334. procedure ungetregisterintinline(list:Taasmoutput;position:Tai;const r:Tregister);
  335. {$endif}
  336. {$ifdef TEMPREGDEBUG}
  337. reg_user : regvar_ptreearray;
  338. reg_releaser : regvar_ptreearray;
  339. {$endif TEMPREGDEBUG}
  340. {$ifdef TEMPREGDEBUG}
  341. procedure testregisters;
  342. {$endif TEMPREGDEBUGx}
  343. {$ifdef newra}
  344. procedure add_edge(u,v:Tsuperregister);
  345. procedure add_edges_used(u:Tsuperregister);
  346. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  347. function move_related(n:Tsuperregister):boolean;
  348. procedure make_work_list;
  349. procedure enable_moves(n:Tsuperregister);
  350. procedure decrement_degree(m:Tsuperregister);
  351. procedure simplify;
  352. function get_alias(n:Tsuperregister):Tsuperregister;
  353. procedure add_worklist(u:Tsuperregister);
  354. function adjacent_ok(u,v:Tsuperregister):boolean;
  355. function conservative(u,v:Tsuperregister):boolean;
  356. procedure combine(u,v:Tsuperregister);
  357. procedure coalesce;
  358. procedure freeze_moves(u:Tsuperregister);
  359. procedure freeze;
  360. procedure select_spill;
  361. procedure assign_colours;
  362. procedure clear_interferences(u:Tsuperregister);
  363. {$endif}
  364. end;
  365. const
  366. {# This value is used in tsaved. If the array value is equal
  367. to this, then this means that this register is not used.
  368. }
  369. reg_not_saved = $7fffffff;
  370. var
  371. {# This is the class instance used to access the register allocator class }
  372. rg: trgobj;
  373. { trerefence handling }
  374. {# Clear to zero a treference }
  375. procedure reference_reset(var ref : treference);
  376. procedure reference_reset_old(var ref : treference);
  377. {# Clear to zero a treference, and set is base address
  378. to base register.
  379. }
  380. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  381. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  382. procedure reference_release(list: taasmoutput; const ref : treference);
  383. { This routine verifies if two references are the same, and
  384. if so, returns TRUE, otherwise returns false.
  385. }
  386. function references_equal(sref : treference;dref : treference) : boolean;
  387. { tlocation handling }
  388. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  389. procedure location_release(list: taasmoutput; const l : tlocation);
  390. procedure location_freetemp(list: taasmoutput; const l : tlocation);
  391. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  392. procedure location_swap(var destloc,sourceloc : tlocation);
  393. type
  394. psavedstate = ^tsavedstate;
  395. tsavedstate = record
  396. unusedregsint,usableregsint : Tsupregset;
  397. unusedregsaddr,usableregsaddr : Tsupregset;
  398. unusedregsfpu,usableregsfpu : tregisterset;
  399. unusedregsmm,usableregsmm : tregisterset;
  400. {$ifndef newra}
  401. countunusedregsint,
  402. countunusedregsaddr,
  403. {$endif}
  404. countunusedregsfpu,
  405. countunusedregsmm : byte;
  406. countusableregsint,
  407. countusableregsfpu,
  408. countusableregsmm : byte;
  409. { contains the registers which are really used by the proc itself }
  410. used_in_proc_int : tsupregset;
  411. used_in_proc_other : tregisterset;
  412. reg_pushes_other : regvarother_longintarray;
  413. reg_pushes_int : regvarint_longintarray;
  414. is_reg_var_other : regvarother_booleanarray;
  415. is_reg_var_int : Tsupregset;
  416. regvar_loaded_other: regvarother_booleanarray;
  417. regvar_loaded_int: Tsupregset;
  418. {$ifdef TEMPREGDEBUG}
  419. reg_user : regvar_ptreearray;
  420. reg_releaser : regvar_ptreearray;
  421. {$endif TEMPREGDEBUG}
  422. end;
  423. punusedstate = ^tunusedstate;
  424. tunusedstate = record
  425. {$ifndef newra}
  426. unusedregsint : Tsupregset;
  427. {$endif}
  428. unusedregsaddr : Tsupregset;
  429. unusedregsfpu : tregisterset;
  430. unusedregsmm : tregisterset;
  431. {$ifndef newra}
  432. countunusedregsint,
  433. countunusedregsaddr,
  434. {$endif}
  435. countunusedregsfpu,
  436. countunusedregsmm : byte;
  437. end;
  438. implementation
  439. uses
  440. systems,{$ifdef ra_debug2}fmodule,{$endif}
  441. globals,verbose,
  442. cgobj,tgobj,regvars;
  443. constructor Trgobj.create(Acpu_registers:byte);
  444. begin
  445. used_in_proc_int := [];
  446. used_in_proc_other:=[];
  447. t_times := 0;
  448. resetusableregisters;
  449. lastintreg:=0;
  450. maxintreg:=first_imreg;
  451. {What madman decided to change the code like this: (??)
  452. cpu_registers:=last_supreg-first_supreg+1;
  453. The amount of registers available for register allocation is
  454. allmost allways smaller than the amount of registers that exists!
  455. Therefore: }
  456. cpu_registers:=Acpu_registers;
  457. {$ifdef TEMPREGDEBUG}
  458. fillchar(reg_user,sizeof(reg_user),0);
  459. fillchar(reg_releaser,sizeof(reg_releaser),0);
  460. {$endif TEMPREGDEBUG}
  461. {$ifdef newra}
  462. fillchar(igraph,sizeof(igraph),0);
  463. fillchar(degree,sizeof(degree),0);
  464. {Precoloured nodes should have an infinite degree, which we can approach
  465. by 255.}
  466. fillchar(movelist,sizeof(movelist),0);
  467. worklist_moves:=Tlinkedlist.create;
  468. abtlist:='';
  469. {$endif}
  470. end;
  471. function trgobj.getregistergenother(list: taasmoutput; const lowreg, highreg: Toldregister;
  472. var unusedregs: tregisterset; var countunusedregs: byte): tregister;
  473. var
  474. i: Toldregister;
  475. r: Tregister;
  476. begin
  477. for i:=lowreg to highreg do
  478. begin
  479. if i in unusedregs then
  480. begin
  481. exclude(unusedregs,i);
  482. include(used_in_proc_other,i);
  483. dec(countunusedregs);
  484. r.enum:=i;
  485. list.concat(tai_regalloc.alloc(r));
  486. result := r;
  487. exit;
  488. end;
  489. end;
  490. internalerror(10);
  491. end;
  492. function Trgobj.getregistergenint(list:Taasmoutput;
  493. subreg:Tsubregister;
  494. const lowreg,highreg:Tsuperregister;
  495. var fusedinproc,unusedregs:Tsupregset
  496. {$ifndef newra};var countunusedregs:byte{$endif}):Tregister;
  497. {$ifdef powerpc}
  498. {$ifndef newra}
  499. {$define reuseregs}
  500. {$endif newra}
  501. {$endif powerpc}
  502. var i:Tsuperregister;
  503. r:Tregister;
  504. begin
  505. {$ifdef reuseregs}
  506. i := lowreg;
  507. lastintreg := highreg;
  508. {$else reuseregs}
  509. if not (lastintreg in [lowreg..highreg]) then
  510. lastintreg:=lowreg;
  511. i:=lastintreg;
  512. {$endif reuseregs}
  513. repeat
  514. if i=highreg then
  515. i:=lowreg
  516. else
  517. inc(i);
  518. if (i in unusedregs) {$ifdef newra} and (pos(char(i),abtlist)=0) {$endif} then
  519. begin
  520. exclude(unusedregs,i);
  521. include(fusedinproc,i);
  522. {$ifndef newra}
  523. dec(countunusedregs);
  524. {$endif}
  525. r.enum:=R_INTREGISTER;
  526. r.number:=i shl 8 or subreg;
  527. list.concat(Tai_regalloc.alloc(r));
  528. result:=r;
  529. lastintreg:=i;
  530. if i>maxintreg then
  531. maxintreg:=i;
  532. {$ifdef newra}
  533. add_edges_used(i);
  534. {$endif}
  535. exit;
  536. end;
  537. until i=lastintreg;
  538. internalerror(10);
  539. end;
  540. procedure trgobj.ungetregistergen(list: taasmoutput; const r: tregister;
  541. const usableregs: tregisterset; var unusedregs: tregisterset; var countunusedregs: byte);
  542. begin
  543. if r.enum>lastreg then
  544. internalerror(2003010801);
  545. { takes much time }
  546. if not(r.enum in usableregs) then
  547. exit;
  548. {$ifdef TEMPREGDEBUG}
  549. if (r.enum in unusedregs) then
  550. {$ifdef EXTTEMPREGDEBUG}
  551. begin
  552. Comment(V_Debug,'register freed twice '+std_reg2str[r.enum]);
  553. testregisters32;
  554. exit;
  555. end
  556. {$else EXTTEMPREGDEBUG}
  557. exit
  558. {$endif EXTTEMPREGDEBUG}
  559. else
  560. {$endif TEMPREGDEBUG}
  561. inc(countunusedregs);
  562. include(unusedregs,r.enum);
  563. list.concat(tai_regalloc.dealloc(r));
  564. end;
  565. procedure trgobj.ungetregistergenint(list:taasmoutput;const r:Tregister;
  566. const usableregs:Tsupregset;
  567. var unusedregs:Tsupregset
  568. {$ifndef newra};var countunusedregs:byte{$endif});
  569. var supreg:Tsuperregister;
  570. begin
  571. if r.enum<=lastreg then
  572. internalerror(2003010803);
  573. supreg:=r.number shr 8;
  574. { takes much time }
  575. {$ifndef newra}
  576. if not(supreg in usableregs) then
  577. exit;
  578. {$else}
  579. if supreg in is_reg_var_int then
  580. exit;
  581. {$endif}
  582. {$ifdef TEMPREGDEBUG}
  583. if (supreg in unusedregs) then
  584. {$ifdef EXTTEMPREGDEBUG}
  585. begin
  586. comment(v_debug,'register freed twice '+supreg_name(supreg));
  587. testregisters32
  588. exit;
  589. end
  590. {$else EXTTEMPREGDEBUG}
  591. exit
  592. {$endif EXTTEMPREGDEBUG}
  593. else
  594. {$endif TEMPREGDEBUG}
  595. {$ifndef newra}inc(countunusedregs){$endif};
  596. include(unusedregs,supreg);
  597. list.concat(tai_regalloc.dealloc(r));
  598. {$ifdef newra}
  599. add_edges_used(supreg);
  600. {$endif newra}
  601. end;
  602. function trgobj.getregisterint(list:taasmoutput;size:Tcgsize):Tregister;
  603. var subreg:Tsubregister;
  604. begin
  605. {$ifndef newra}
  606. if countunusedregsint=0 then
  607. internalerror(10);
  608. {$ifdef TEMPREGDEBUG}
  609. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  610. internalerror(10);
  611. {$endif TEMPREGDEBUG}
  612. {$ifdef EXTTEMPREGDEBUG}
  613. if curptree^^.usableregs-countunusedregsint>curptree^^.reallyusedregs then
  614. curptree^^.reallyusedregs:=curptree^^.usableregs-countunusedregsint;
  615. {$endif EXTTEMPREGDEBUG}
  616. {$endif}
  617. subreg:=cgsize2subreg(size);
  618. result:=getregistergenint(list,
  619. subreg,
  620. {$ifdef newra}
  621. first_imreg,
  622. last_imreg,
  623. {$else}
  624. first_supreg,
  625. last_supreg,
  626. {$endif}
  627. used_in_proc_int,
  628. unusedregsint{$ifndef newra},
  629. countunusedregsint{$endif});
  630. {$ifdef TEMPREGDEBUG}
  631. reg_user[result]:=curptree^;
  632. testregisters32;
  633. {$endif TEMPREGDEBUG}
  634. {$ifdef newra}
  635. add_constraints(getregisterint.number);
  636. {$endif}
  637. end;
  638. {$ifdef newra}
  639. procedure Trgobj.add_constraints(reg:Tnewregister);
  640. begin
  641. end;
  642. {$endif}
  643. procedure trgobj.ungetregisterint(list : taasmoutput; r : tregister);
  644. begin
  645. ungetregistergenint(list,r,usableregsint,unusedregsint{$ifndef newra},
  646. countunusedregsint{$endif});
  647. {$ifdef TEMPREGDEBUG}
  648. reg_releaser[r]:=curptree^;
  649. testregisters32;
  650. {$endif TEMPREGDEBUG}
  651. end;
  652. { tries to allocate the passed register, if possible }
  653. function trgobj.getexplicitregisterint(list:Taasmoutput;r:Tnewregister):Tregister;
  654. var r2:Tregister;
  655. begin
  656. if (r shr 8) in unusedregsint then
  657. begin
  658. {$ifndef newra}
  659. dec(countunusedregsint);
  660. {$ifdef TEMPREGDEBUG}
  661. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  662. internalerror(10);
  663. reg_user[r shr 8]:=curptree^;
  664. {$endif TEMPREGDEBUG}
  665. {$endif newra}
  666. exclude(unusedregsint,r shr 8);
  667. include(used_in_proc_int,r shr 8);
  668. r2.enum:=R_INTREGISTER;
  669. r2.number:=r;
  670. list.concat(tai_regalloc.alloc(r2));
  671. {$ifdef newra}
  672. add_edges_used(r shr 8);
  673. {$endif}
  674. {$ifdef TEMPREGDEBUG}
  675. testregisters32;
  676. {$endif TEMPREGDEBUG}
  677. end
  678. else
  679. internalerror(200301103);
  680. getexplicitregisterint:=r2;
  681. end;
  682. {$ifdef newra}
  683. procedure Trgobj.allocexplicitregistersint(list:Taasmoutput;r:Tsupregset);
  684. var reg:Tregister;
  685. i:Tsuperregister;
  686. begin
  687. if unusedregsint*r=r then
  688. begin
  689. unusedregsint:=unusedregsint-r;
  690. used_in_proc_int:=used_in_proc_int+r;
  691. for i:=first_supreg to last_supreg do
  692. if i in r then
  693. begin
  694. add_edges_used(i);
  695. reg.enum:=R_INTREGISTER;
  696. reg.number:=i shl 8 or R_SUBWHOLE;
  697. list.concat(Tai_regalloc.alloc(reg));
  698. end;
  699. end
  700. else
  701. internalerror(200305061);
  702. end;
  703. procedure Trgobj.deallocexplicitregistersint(list:Taasmoutput;r:Tsupregset);
  704. var reg:Tregister;
  705. i:Tsuperregister;
  706. begin
  707. if unusedregsint*r=[] then
  708. begin
  709. unusedregsint:=unusedregsint+r;
  710. for i:=last_supreg downto first_supreg do
  711. if i in r then
  712. begin
  713. reg.enum:=R_INTREGISTER;
  714. reg.number:=i shl 8 or R_SUBWHOLE;
  715. list.concat(Tai_regalloc.dealloc(reg));
  716. end;
  717. end
  718. else
  719. internalerror(200305062);
  720. end;
  721. {$endif}
  722. { tries to allocate the passed register, if possible }
  723. function trgobj.getexplicitregisterfpu(list : taasmoutput; r : Toldregister) : tregister;
  724. var r2:Tregister;
  725. begin
  726. if r in unusedregsfpu then
  727. begin
  728. dec(countunusedregsfpu);
  729. {$ifdef TEMPREGDEBUG}
  730. if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
  731. internalerror(10);
  732. reg_user[r]:=curptree^;
  733. {$endif TEMPREGDEBUG}
  734. exclude(unusedregsfpu,r);
  735. include(used_in_proc_other,r);
  736. r2.enum:=r;
  737. list.concat(tai_regalloc.alloc(r2));
  738. getexplicitregisterfpu:=r2;
  739. {$ifdef TEMPREGDEBUG}
  740. testregisters32;
  741. {$endif TEMPREGDEBUG}
  742. end
  743. else
  744. {$warning Size for FPU reg is maybe not correct}
  745. getexplicitregisterfpu:=getregisterfpu(list,OS_F32);
  746. end;
  747. function trgobj.getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister;
  748. begin
  749. if countunusedregsfpu=0 then
  750. internalerror(10);
  751. result := getregistergenother(list,firstsavefpureg,lastsavefpureg,
  752. unusedregsfpu,countunusedregsfpu);
  753. end;
  754. procedure trgobj.ungetregisterfpu(list : taasmoutput; r : tregister;size:TCGsize);
  755. begin
  756. ungetregistergen(list,r,usableregsfpu,unusedregsfpu,
  757. countunusedregsfpu);
  758. end;
  759. function trgobj.getregistermm(list: taasmoutput) : tregister;
  760. begin
  761. if countunusedregsmm=0 then
  762. internalerror(10);
  763. result := getregistergenother(list,firstsavemmreg,lastsavemmreg,
  764. unusedregsmm,countunusedregsmm);
  765. end;
  766. procedure trgobj.ungetregistermm(list: taasmoutput; r: tregister);
  767. begin
  768. ungetregistergen(list,r,usableregsmm,unusedregsmm,
  769. countunusedregsmm);
  770. end;
  771. function trgobj.getaddressregister(list:Taasmoutput): tregister;
  772. begin
  773. {An address register is OS_INT per definition.}
  774. result := getregisterint(list,OS_INT);
  775. end;
  776. procedure trgobj.ungetaddressregister(list: taasmoutput; r: tregister);
  777. begin
  778. ungetregisterint(list,r);
  779. end;
  780. function trgobj.isaddressregister(reg: tregister): boolean;
  781. begin
  782. if reg.number<>0 then; { remove warning }
  783. result := true;
  784. end;
  785. procedure trgobj.ungetregister(list: taasmoutput; r : tregister);
  786. begin
  787. if r.enum=R_NO then
  788. exit;
  789. if r.enum>lastreg then
  790. internalerror(200301081);
  791. if r.enum in fpuregs then
  792. ungetregisterfpu(list,r,OS_NO)
  793. else if r.enum in mmregs then
  794. ungetregistermm(list,r)
  795. else if r.enum in addrregs then
  796. ungetaddressregister(list,r)
  797. else internalerror(2002070602);
  798. end;
  799. procedure Trgobj.cleartempgen;
  800. {$ifdef newra}
  801. var i:Tsuperregister;
  802. {$endif newra}
  803. begin
  804. {$ifndef newra}
  805. countunusedregsint:=countusableregsint;
  806. {$endif}
  807. countunusedregsfpu:=countusableregsfpu;
  808. countunusedregsmm:=countusableregsmm;
  809. lastintreg:=0;
  810. maxintreg:=first_imreg;
  811. {$ifdef newra}
  812. unusedregsint:=[0..255];
  813. {$else}
  814. unusedregsint:=usableregsint;
  815. {$endif}
  816. unusedregsfpu:=usableregsfpu;
  817. unusedregsmm:=usableregsmm;
  818. {$ifdef newra}
  819. {$ifdef powerpc}
  820. savedintbyproc:=[RS_R13..RS_R31];
  821. {$else powerpc}
  822. savedintbyproc:=[];
  823. {$endif powerpc}
  824. for i:=low(Tsuperregister) to high(Tsuperregister) do
  825. begin
  826. if igraph.adjlist[i]<>nil then
  827. dispose(igraph.adjlist[i]);
  828. if movelist[i]<>nil then
  829. dispose(movelist[i]);
  830. end;
  831. fillchar(movelist,sizeof(movelist),0);
  832. fillchar(igraph,sizeof(igraph),0);
  833. fillchar(degree,sizeof(degree),0);
  834. {Precoloured nodes should have an infinite degree, which we can approach
  835. by 255.}
  836. for i:=first_supreg to last_supreg do
  837. degree[i]:=255;
  838. worklist_moves.clear;
  839. abtlist:='';
  840. {$endif}
  841. end;
  842. procedure trgobj.ungetreference(list : taasmoutput; const ref : treference);
  843. begin
  844. if (ref.base.number<>NR_NO) and (ref.base.number<>NR_FRAME_POINTER_REG) then
  845. ungetregisterint(list,ref.base);
  846. if (ref.index.number<>NR_NO) and (ref.index.number<>NR_FRAME_POINTER_REG) then
  847. ungetregisterint(list,ref.index);
  848. end;
  849. {$ifndef newra}
  850. procedure trgobj.saveintregvars(list:Taasmoutput;const s:Tsupregset);
  851. var r:Tsuperregister;
  852. hr: tregister;
  853. begin
  854. if not(cs_regvars in aktglobalswitches) then
  855. exit;
  856. for r:=firstsaveintreg to lastsaveintreg do
  857. if (r in is_reg_var_int) and
  858. (r in s) then
  859. begin
  860. hr.number:=r shl 8;
  861. hr.enum:=R_INTREGISTER;
  862. store_regvar(list,hr);
  863. end;
  864. end;
  865. {$endif}
  866. procedure trgobj.saveotherregvars(list: taasmoutput; const s: tregisterset);
  867. var
  868. r: Tregister;
  869. begin
  870. if not(cs_regvars in aktglobalswitches) then
  871. exit;
  872. if firstsavefpureg <> R_NO then
  873. for r.enum := firstsavefpureg to lastsavefpureg do
  874. if is_reg_var_other[r.enum] and
  875. (r.enum in s) then
  876. store_regvar(list,r);
  877. if firstsavemmreg <> R_NO then
  878. for r.enum := firstsavemmreg to lastsavemmreg do
  879. if is_reg_var_other[r.enum] and
  880. (r.enum in s) then
  881. store_regvar(list,r);
  882. end;
  883. {$ifndef newra}
  884. procedure trgobj.saveusedintregisters(list:Taasmoutput;
  885. var saved:Tpushedsavedint;
  886. const s:Tsupregset);
  887. var r:Tsuperregister;
  888. r2:Tregister;
  889. hr : treference;
  890. begin
  891. used_in_proc_int:=used_in_proc_int+s;
  892. for r:=firstsaveintreg to lastsaveintreg do
  893. begin
  894. saved[r].ofs:=reg_not_saved;
  895. { if the register is used by the calling subroutine and if }
  896. { it's not a regvar (those are handled separately) }
  897. if not (r in is_reg_var_int) and
  898. (r in s) and
  899. { and is present in use }
  900. not(r in unusedregsint) then
  901. begin
  902. { then save it }
  903. tg.GetTemp(list,sizeof(aword),tt_persistent,hr);
  904. saved[r].ofs:=hr.offset;
  905. r2.enum:=R_INTREGISTER;
  906. r2.number:=r shl 8 or R_SUBWHOLE;
  907. cg.a_load_reg_ref(list,OS_INT,OS_INT,r2,hr);
  908. cg.a_reg_dealloc(list,r2);
  909. include(unusedregsint,r);
  910. inc(countunusedregsint);
  911. end;
  912. end;
  913. {$ifdef TEMPREGDEBUG}
  914. testregisters32;
  915. {$endif TEMPREGDEBUG}
  916. end;
  917. {$endif}
  918. procedure trgobj.saveusedotherregisters(list: taasmoutput;
  919. var saved : tpushedsavedother; const s: tregisterset);
  920. var
  921. r : tregister;
  922. hr : treference;
  923. begin
  924. used_in_proc_other:=used_in_proc_other + s;
  925. { don't try to save the fpu registers if not desired (e.g. for }
  926. { the 80x86) }
  927. if firstsavefpureg <> R_NO then
  928. for r.enum:=firstsavefpureg to lastsavefpureg do
  929. begin
  930. saved[r.enum].ofs:=reg_not_saved;
  931. { if the register is used by the calling subroutine and if }
  932. { it's not a regvar (those are handled separately) }
  933. if not is_reg_var_other[r.enum] and
  934. (r.enum in s) and
  935. { and is present in use }
  936. not(r.enum in unusedregsfpu) then
  937. begin
  938. { then save it }
  939. tg.GetTemp(list,extended_size,tt_persistent,hr);
  940. saved[r.enum].ofs:=hr.offset;
  941. cg.a_loadfpu_reg_ref(list,OS_FLOAT,r,hr);
  942. cg.a_reg_dealloc(list,r);
  943. include(unusedregsfpu,r.enum);
  944. inc(countunusedregsfpu);
  945. end;
  946. end;
  947. { don't save the vector registers if there's no support for them }
  948. if firstsavemmreg <> R_NO then
  949. for r.enum:=firstsavemmreg to lastsavemmreg do
  950. begin
  951. saved[r.enum].ofs:=reg_not_saved;
  952. { if the register is in use and if it's not a regvar (those }
  953. { are handled separately), save it }
  954. if not is_reg_var_other[r.enum] and
  955. (r.enum in s) and
  956. { and is present in use }
  957. not(r.enum in unusedregsmm) then
  958. begin
  959. { then save it }
  960. tg.GetTemp(list,mmreg_size,tt_persistent,hr);
  961. saved[r.enum].ofs:=hr.offset;
  962. cg.a_loadmm_reg_ref(list,r,hr);
  963. cg.a_reg_dealloc(list,r);
  964. include(unusedregsmm,r.enum);
  965. inc(countunusedregsmm);
  966. end;
  967. end;
  968. {$ifdef TEMPREGDEBUG}
  969. testregisters32;
  970. {$endif TEMPREGDEBUG}
  971. end;
  972. {$ifndef newra}
  973. procedure trgobj.restoreusedintregisters(list:Taasmoutput;
  974. const saved:Tpushedsavedint);
  975. var r:Tsuperregister;
  976. r2:Tregister;
  977. hr:Treference;
  978. begin
  979. for r:=lastsaveintreg downto firstsaveintreg do
  980. begin
  981. if saved[r].ofs <> reg_not_saved then
  982. begin
  983. r2.enum:=R_INTREGISTER;
  984. r2.number:=NR_FRAME_POINTER_REG;
  985. reference_reset_base(hr,r2,saved[r].ofs);
  986. r2.enum:=R_INTREGISTER;
  987. r2.number:=r shl 8 or R_SUBWHOLE;
  988. cg.a_reg_alloc(list,r2);
  989. cg.a_load_ref_reg(list,OS_INT,OS_INT,hr,r2);
  990. if not (r in unusedregsint) then
  991. { internalerror(10)
  992. in n386cal we always save/restore the reg *state*
  993. using save/restoreunusedstate -> the current state
  994. may not be real (JM) }
  995. else
  996. begin
  997. dec(countunusedregsint);
  998. exclude(unusedregsint,r);
  999. end;
  1000. tg.UnGetTemp(list,hr);
  1001. end;
  1002. end;
  1003. {$ifdef TEMPREGDEBUG}
  1004. testregisters32;
  1005. {$endif TEMPREGDEBUG}
  1006. end;
  1007. {$endif}
  1008. procedure trgobj.restoreusedotherregisters(list : taasmoutput;
  1009. const saved : tpushedsavedother);
  1010. var
  1011. r,r2 : tregister;
  1012. hr : treference;
  1013. begin
  1014. if firstsavemmreg <> R_NO then
  1015. for r.enum:=lastsavemmreg downto firstsavemmreg do
  1016. begin
  1017. if saved[r.enum].ofs <> reg_not_saved then
  1018. begin
  1019. r2.enum:=R_INTREGISTER;
  1020. r2.number:=NR_FRAME_POINTER_REG;
  1021. reference_reset_base(hr,r2,saved[r.enum].ofs);
  1022. cg.a_reg_alloc(list,r);
  1023. cg.a_loadmm_ref_reg(list,hr,r);
  1024. if not (r.enum in unusedregsmm) then
  1025. { internalerror(10)
  1026. in n386cal we always save/restore the reg *state*
  1027. using save/restoreunusedstate -> the current state
  1028. may not be real (JM) }
  1029. else
  1030. begin
  1031. dec(countunusedregsmm);
  1032. exclude(unusedregsmm,r.enum);
  1033. end;
  1034. tg.UnGetTemp(list,hr);
  1035. end;
  1036. end;
  1037. if firstsavefpureg <> R_NO then
  1038. for r.enum:=lastsavefpureg downto firstsavefpureg do
  1039. begin
  1040. if saved[r.enum].ofs <> reg_not_saved then
  1041. begin
  1042. r2.enum:=R_INTREGISTER;
  1043. r2.number:=NR_FRAME_POINTER_REG;
  1044. reference_reset_base(hr,r2,saved[r.enum].ofs);
  1045. cg.a_reg_alloc(list,r);
  1046. cg.a_loadfpu_ref_reg(list,OS_FLOAT,hr,r);
  1047. if not (r.enum in unusedregsfpu) then
  1048. { internalerror(10)
  1049. in n386cal we always save/restore the reg *state*
  1050. using save/restoreunusedstate -> the current state
  1051. may not be real (JM) }
  1052. else
  1053. begin
  1054. dec(countunusedregsfpu);
  1055. exclude(unusedregsfpu,r.enum);
  1056. end;
  1057. tg.UnGetTemp(list,hr);
  1058. end;
  1059. end;
  1060. {$ifdef TEMPREGDEBUG}
  1061. testregisters32;
  1062. {$endif TEMPREGDEBUG}
  1063. end;
  1064. {$ifndef newra}
  1065. procedure trgobj.incrementintregisterpushed(const s:Tsupregset);
  1066. {$ifdef i386}
  1067. var
  1068. regi:Tsuperregister;
  1069. {$endif i386}
  1070. begin
  1071. {$ifdef i386}
  1072. for regi:=firstsaveintreg to lastsaveintreg do
  1073. begin
  1074. if (regi in s) then
  1075. inc(reg_pushes_int[regi],t_times*2);
  1076. end;
  1077. {$endif i386}
  1078. end;
  1079. {$endif}
  1080. procedure trgobj.incrementotherregisterpushed(const s:Tregisterset);
  1081. {$ifdef i386}
  1082. var
  1083. regi : Toldregister;
  1084. {$endif i386}
  1085. begin
  1086. {$ifdef i386}
  1087. if firstsavefpureg <> R_NO then
  1088. for regi:=firstsavefpureg to lastsavefpureg do
  1089. begin
  1090. if (regi in s) then
  1091. inc(reg_pushes_other[regi],t_times*2);
  1092. end;
  1093. if firstsavemmreg <> R_NO then
  1094. for regi:=firstsavemmreg to lastsavemmreg do
  1095. begin
  1096. if (regi in s) then
  1097. inc(reg_pushes_other[regi],t_times*2);
  1098. end;
  1099. {$endif i386}
  1100. end;
  1101. procedure trgobj.clearregistercount;
  1102. begin
  1103. {$ifndef newra}
  1104. fillchar(reg_pushes_int,sizeof(reg_pushes_int),0);
  1105. {$endif}
  1106. fillchar(reg_pushes_other,sizeof(reg_pushes_other),0);
  1107. {ifndef i386}
  1108. { all used registers will have to be saved at the start and restored }
  1109. { at the end, but otoh regpara's do not have to be saved to memory }
  1110. { at the start (there is a move from regpara to regvar most of the }
  1111. { time though) -> set cost to 100+20 }
  1112. {$ifndef newra}
  1113. filldword(reg_pushes_int[firstsaveintreg],lastsaveintreg-firstsaveintreg+1,120);
  1114. {$endif}
  1115. filldword(reg_pushes_other[firstsavefpureg],ord(lastsavefpureg)-ord(firstsavefpureg)+1,120);
  1116. {endif not i386}
  1117. fillchar(is_reg_var_other,sizeof(is_reg_var_other),false);
  1118. is_reg_var_int:=[];
  1119. fillchar(regvar_loaded_other,sizeof(regvar_loaded_other),false);
  1120. regvar_loaded_int:=[];
  1121. end;
  1122. procedure trgobj.resetusableregisters;
  1123. begin
  1124. { initialize fields with constant values from cpubase }
  1125. countusableregsint := cpubase.c_countusableregsint;
  1126. countusableregsfpu := cpubase.c_countusableregsfpu;
  1127. countusableregsmm := cpubase.c_countusableregsmm;
  1128. usableregsint := cpubase.usableregsint;
  1129. usableregsfpu := cpubase.usableregsfpu;
  1130. usableregsmm := cpubase.usableregsmm;
  1131. clearregistercount;
  1132. end;
  1133. procedure trgobj.makeregvarint(reg:Tsuperregister);
  1134. begin
  1135. dec(countusableregsint);
  1136. include(is_reg_var_int,reg);
  1137. {$ifndef newra}
  1138. dec(countunusedregsint);
  1139. exclude(usableregsint,reg);
  1140. exclude(unusedregsint,reg);
  1141. include(used_in_proc_int,reg);
  1142. {$endif}
  1143. end;
  1144. procedure trgobj.makeregvarother(reg: tregister);
  1145. begin
  1146. if reg.enum>lastreg then
  1147. internalerror(200301081);
  1148. if reg.enum in intregs then
  1149. internalerror(200301151)
  1150. else if reg.enum in fpuregs then
  1151. begin
  1152. dec(countusableregsfpu);
  1153. dec(countunusedregsfpu);
  1154. exclude(usableregsfpu,reg.enum);
  1155. exclude(unusedregsfpu,reg.enum);
  1156. include(used_in_proc_other,reg.enum);
  1157. end
  1158. else if reg.enum in mmregs then
  1159. begin
  1160. dec(countusableregsmm);
  1161. dec(countunusedregsmm);
  1162. exclude(usableregsmm,reg.enum);
  1163. exclude(unusedregsmm,reg.enum);
  1164. include(used_in_proc_other,reg.enum);
  1165. end;
  1166. is_reg_var_other[reg.enum]:=true;
  1167. end;
  1168. {$ifdef TEMPREGDEBUG}
  1169. procedure trgobj.testregisters;
  1170. var
  1171. r: tregister;
  1172. test : byte;
  1173. begin
  1174. test:=0;
  1175. for r := firstsaveintreg to lastsaveintreg do
  1176. inc(test,ord(r in unusedregsint));
  1177. if test<>countunusedregsint then
  1178. internalerror(10);
  1179. end;
  1180. {$endif TEMPREGDEBUG}
  1181. procedure trgobj.saveStateForInline(var state: pointer);
  1182. begin
  1183. new(psavedstate(state));
  1184. psavedstate(state)^.unusedregsint := unusedregsint;
  1185. psavedstate(state)^.usableregsint := usableregsint;
  1186. psavedstate(state)^.unusedregsfpu := unusedregsfpu;
  1187. psavedstate(state)^.usableregsfpu := usableregsfpu;
  1188. psavedstate(state)^.unusedregsmm := unusedregsmm;
  1189. psavedstate(state)^.usableregsmm := usableregsmm;
  1190. {$ifndef newra}
  1191. psavedstate(state)^.countunusedregsint := countunusedregsint;
  1192. {$endif}
  1193. psavedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1194. psavedstate(state)^.countunusedregsmm := countunusedregsmm;
  1195. psavedstate(state)^.countusableregsint := countusableregsint;
  1196. psavedstate(state)^.countusableregsfpu := countusableregsfpu;
  1197. psavedstate(state)^.countusableregsmm := countusableregsmm;
  1198. psavedstate(state)^.used_in_proc_int := used_in_proc_int;
  1199. psavedstate(state)^.used_in_proc_other := used_in_proc_other;
  1200. {$ifndef newra}
  1201. psavedstate(state)^.reg_pushes_int := reg_pushes_int;
  1202. {$endif}
  1203. psavedstate(state)^.reg_pushes_other := reg_pushes_other;
  1204. psavedstate(state)^.is_reg_var_int := is_reg_var_int;
  1205. psavedstate(state)^.is_reg_var_other := is_reg_var_other;
  1206. psavedstate(state)^.regvar_loaded_int := regvar_loaded_int;
  1207. psavedstate(state)^.regvar_loaded_other := regvar_loaded_other;
  1208. {$ifdef TEMPREGDEBUG}
  1209. psavedstate(state)^.reg_user := reg_user;
  1210. psavedstate(state)^.reg_releaser := reg_releaser;
  1211. {$endif TEMPREGDEBUG}
  1212. end;
  1213. procedure trgobj.restoreStateAfterInline(var state: pointer);
  1214. begin
  1215. unusedregsint := psavedstate(state)^.unusedregsint;
  1216. usableregsint := psavedstate(state)^.usableregsint;
  1217. unusedregsfpu := psavedstate(state)^.unusedregsfpu;
  1218. usableregsfpu := psavedstate(state)^.usableregsfpu;
  1219. unusedregsmm := psavedstate(state)^.unusedregsmm;
  1220. usableregsmm := psavedstate(state)^.usableregsmm;
  1221. {$ifndef newra}
  1222. countunusedregsint := psavedstate(state)^.countunusedregsint;
  1223. {$endif}
  1224. countunusedregsfpu := psavedstate(state)^.countunusedregsfpu;
  1225. countunusedregsmm := psavedstate(state)^.countunusedregsmm;
  1226. countusableregsint := psavedstate(state)^.countusableregsint;
  1227. countusableregsfpu := psavedstate(state)^.countusableregsfpu;
  1228. countusableregsmm := psavedstate(state)^.countusableregsmm;
  1229. used_in_proc_int := psavedstate(state)^.used_in_proc_int;
  1230. used_in_proc_other := psavedstate(state)^.used_in_proc_other;
  1231. {$ifndef newra}
  1232. reg_pushes_int := psavedstate(state)^.reg_pushes_int;
  1233. {$endif}
  1234. reg_pushes_other := psavedstate(state)^.reg_pushes_other;
  1235. is_reg_var_int := psavedstate(state)^.is_reg_var_int;
  1236. is_reg_var_other := psavedstate(state)^.is_reg_var_other;
  1237. regvar_loaded_other := psavedstate(state)^.regvar_loaded_other;
  1238. regvar_loaded_int := psavedstate(state)^.regvar_loaded_int;
  1239. {$ifdef TEMPREGDEBUG}
  1240. reg_user := psavedstate(state)^.reg_user;
  1241. reg_releaser := psavedstate(state)^.reg_releaser;
  1242. {$endif TEMPREGDEBUG}
  1243. dispose(psavedstate(state));
  1244. state := nil;
  1245. end;
  1246. procedure trgobj.saveUnusedState(var state: pointer);
  1247. begin
  1248. new(punusedstate(state));
  1249. {$ifndef newra}
  1250. punusedstate(state)^.unusedregsint := unusedregsint;
  1251. {$endif}
  1252. punusedstate(state)^.unusedregsfpu := unusedregsfpu;
  1253. punusedstate(state)^.unusedregsmm := unusedregsmm;
  1254. {$ifndef newra}
  1255. punusedstate(state)^.countunusedregsint := countunusedregsint;
  1256. {$endif}
  1257. punusedstate(state)^.countunusedregsfpu := countunusedregsfpu;
  1258. punusedstate(state)^.countunusedregsmm := countunusedregsmm;
  1259. end;
  1260. procedure trgobj.restoreUnusedState(var state: pointer);
  1261. begin
  1262. {$ifndef newra}
  1263. unusedregsint := punusedstate(state)^.unusedregsint;
  1264. {$endif}
  1265. unusedregsfpu := punusedstate(state)^.unusedregsfpu;
  1266. unusedregsmm := punusedstate(state)^.unusedregsmm;
  1267. {$ifndef newra}
  1268. countunusedregsint := punusedstate(state)^.countunusedregsint;
  1269. {$endif}
  1270. countunusedregsfpu := punusedstate(state)^.countunusedregsfpu;
  1271. countunusedregsmm := punusedstate(state)^.countunusedregsmm;
  1272. dispose(punusedstate(state));
  1273. state := nil;
  1274. end;
  1275. {$ifdef newra}
  1276. procedure Trgobj.add_edge(u,v:Tsuperregister);
  1277. {This procedure will add an edge to the virtual interference graph.}
  1278. procedure addadj(u,v:Tsuperregister);
  1279. begin
  1280. if igraph.adjlist[u]=nil then
  1281. begin
  1282. getmem(igraph.adjlist[u],16);
  1283. igraph.adjlist[u]^:='';
  1284. end
  1285. else if (length(igraph.adjlist[u]^) and 15)=15 then
  1286. reallocmem(igraph.adjlist[u],length(igraph.adjlist[u]^)+16);
  1287. igraph.adjlist[u]^:=igraph.adjlist[u]^+char(v);
  1288. end;
  1289. begin
  1290. if (u<>v) and not(v in igraph.bitmap[u]) then
  1291. begin
  1292. include(igraph.bitmap[u],v);
  1293. include(igraph.bitmap[v],u);
  1294. {Precoloured nodes are not stored in the interference graph.}
  1295. if not(u in [first_supreg..last_supreg]) then
  1296. begin
  1297. addadj(u,v);
  1298. inc(degree[u]);
  1299. end;
  1300. if not(v in [first_supreg..last_supreg]) then
  1301. begin
  1302. addadj(v,u);
  1303. inc(degree[v]);
  1304. end;
  1305. end;
  1306. end;
  1307. procedure Trgobj.add_edges_used(u:Tsuperregister);
  1308. var i:Tsuperregister;
  1309. begin
  1310. for i:=1 to maxintreg do
  1311. if not(i in unusedregsint) then
  1312. add_edge(u,i);
  1313. end;
  1314. {$ifdef ra_debug2}
  1315. procedure Trgobj.writegraph;
  1316. {This procedure writes out the current interference graph in the
  1317. register allocator.}
  1318. var f:text;
  1319. i,j:Tsuperregister;
  1320. begin
  1321. assign(f,'igraph.'+Lower(current_module.modulename^));
  1322. rewrite(f);
  1323. writeln(f,'Interference graph');
  1324. writeln(f);
  1325. write(f,' ');
  1326. for i:=0 to 15 do
  1327. for j:=0 to 15 do
  1328. write(f,hexstr(i,1));
  1329. writeln(f);
  1330. write(f,' ');
  1331. for i:=0 to 15 do
  1332. write(f,'0123456789ABCDEF');
  1333. writeln(f);
  1334. for i:=0 to 255 do
  1335. begin
  1336. write(f,hexstr(i,2):4);
  1337. for j:=0 to 255 do
  1338. if j in igraph.bitmap[i] then
  1339. write(f,'*')
  1340. else
  1341. write(f,'-');
  1342. writeln(f);
  1343. end;
  1344. close(f);
  1345. end;
  1346. {$endif}
  1347. procedure Trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  1348. begin
  1349. if movelist[u]=nil then
  1350. begin
  1351. getmem(movelist[u],64);
  1352. movelist[u]^.count:=0;
  1353. end
  1354. else if (movelist[u]^.count and 15)=15 then
  1355. reallocmem(movelist[u],(movelist[u]^.count+1)*4+64);
  1356. movelist[u]^.data[movelist[u]^.count]:=data;
  1357. inc(movelist[u]^.count);
  1358. end;
  1359. procedure Trgobj.add_move_instruction(instr:Taicpu);
  1360. {This procedure notifies a certain as a move instruction so the
  1361. register allocator can try to eliminate it.}
  1362. var i:Tmoveins;
  1363. ssupreg,dsupreg:Tsuperregister;
  1364. begin
  1365. i:=Tmoveins.create;
  1366. i.moveset:=ms_worklist_moves;
  1367. i.instruction:=instr;
  1368. worklist_moves.insert(i);
  1369. ssupreg:=instr.oper[O_MOV_SOURCE].reg.number shr 8;
  1370. add_to_movelist(ssupreg,i);
  1371. dsupreg:=instr.oper[O_MOV_DEST].reg.number shr 8;
  1372. if ssupreg<>dsupreg then
  1373. {Avoid adding the same move instruction twice to a single register.}
  1374. add_to_movelist(dsupreg,i);
  1375. i.x:=ssupreg;
  1376. i.y:=dsupreg;
  1377. end;
  1378. function Trgobj.move_related(n:Tsuperregister):boolean;
  1379. var i:cardinal;
  1380. begin
  1381. move_related:=false;
  1382. if movelist[n]<>nil then
  1383. begin
  1384. for i:=0 to movelist[n]^.count-1 do
  1385. if Tmoveins(movelist[n]^.data[i]).moveset in
  1386. [ms_worklist_moves,ms_active_moves] then
  1387. begin
  1388. move_related:=true;
  1389. break;
  1390. end;
  1391. end;
  1392. end;
  1393. procedure Trgobj.make_work_list;
  1394. var n:Tsuperregister;
  1395. begin
  1396. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  1397. assign it to any of the registers, thus it is significant.}
  1398. for n:=first_imreg to maxintreg do
  1399. if degree[n]>=cpu_registers then
  1400. spillworklist:=spillworklist+char(n)
  1401. else if move_related(n) then
  1402. freezeworklist:=freezeworklist+char(n)
  1403. else
  1404. simplifyworklist:=simplifyworklist+char(n);
  1405. end;
  1406. procedure Trgobj.prepare_colouring;
  1407. begin
  1408. make_work_list;
  1409. active_moves:=Tlinkedlist.create;
  1410. frozen_moves:=Tlinkedlist.create;
  1411. coalesced_moves:=Tlinkedlist.create;
  1412. constrained_moves:=Tlinkedlist.create;
  1413. fillchar(alias,sizeof(alias),0);
  1414. coalescednodes:='';
  1415. selectstack:='';
  1416. end;
  1417. procedure Trgobj.enable_moves(n:Tsuperregister);
  1418. var m:Tlinkedlistitem;
  1419. i:cardinal;
  1420. begin
  1421. if movelist[n]<>nil then
  1422. for i:=0 to movelist[n]^.count-1 do
  1423. begin
  1424. m:=movelist[n]^.data[i];
  1425. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1426. begin
  1427. if Tmoveins(m).moveset=ms_active_moves then
  1428. begin
  1429. {Move m from the set active_moves to the set worklist_moves.}
  1430. active_moves.remove(m);
  1431. Tmoveins(m).moveset:=ms_worklist_moves;
  1432. worklist_moves.concat(m);
  1433. end;
  1434. end;
  1435. end;
  1436. end;
  1437. procedure Trgobj.decrement_degree(m:Tsuperregister);
  1438. var adj:Pstring;
  1439. d:byte;
  1440. i,p:byte;
  1441. n:char;
  1442. begin
  1443. d:=degree[m];
  1444. if degree[m]>0 then
  1445. dec(degree[m]);
  1446. if d=cpu_registers then
  1447. begin
  1448. {Enable moves for m.}
  1449. enable_moves(m);
  1450. {Enable moves for adjacent.}
  1451. adj:=igraph.adjlist[m];
  1452. if adj<>nil then
  1453. for i:=1 to length(adj^) do
  1454. begin
  1455. n:=adj^[i];
  1456. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1457. enable_moves(Tsuperregister(n));
  1458. end;
  1459. {Remove the node from the spillworklist.}
  1460. p:=pos(char(m),spillworklist);
  1461. if p=0 then
  1462. internalerror(200305301); {must be found}
  1463. if length(spillworklist)>1 then
  1464. spillworklist[p]:=spillworklist[length(spillworklist)];
  1465. dec(spillworklist[0]);
  1466. if move_related(m) then
  1467. freezeworklist:=freezeworklist+char(m)
  1468. else
  1469. simplifyworklist:=simplifyworklist+char(m);
  1470. end;
  1471. end;
  1472. procedure Trgobj.simplify;
  1473. var adj:Pstring;
  1474. i,min,p:byte;
  1475. m:char;
  1476. n:Tsuperregister;
  1477. begin
  1478. {We the element with the least interferences out of the
  1479. simplifyworklist.}
  1480. min:=$ff;
  1481. p:=1;
  1482. for i:=1 to length(simplifyworklist) do
  1483. begin
  1484. adj:=igraph.adjlist[Tsuperregister(simplifyworklist[i])];
  1485. if adj=nil then
  1486. begin
  1487. min:=0;
  1488. break; {We won't find smaller ones.}
  1489. end
  1490. else
  1491. if length(adj^)<min then
  1492. begin
  1493. min:=length(adj^);
  1494. if min=0 then
  1495. break; {We won't find smaller ones.}
  1496. p:=i;
  1497. end;
  1498. end;
  1499. n:=Tsuperregister(simplifyworklist[p]);
  1500. if length(simplifyworklist)>1 then
  1501. simplifyworklist[p]:=simplifyworklist[length(simplifyworklist)];
  1502. dec(simplifyworklist[0]);
  1503. {Push it on the selectstack.}
  1504. selectstack:=selectstack+char(n);
  1505. adj:=igraph.adjlist[n];
  1506. if adj<>nil then
  1507. for i:=1 to length(adj^) do
  1508. begin
  1509. m:=adj^[i];
  1510. if (pos(m,selectstack) or pos(m,coalescednodes))=0 then
  1511. decrement_degree(Tsuperregister(m));
  1512. end;
  1513. end;
  1514. function Trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  1515. begin
  1516. while pos(char(n),coalescednodes)<>0 do
  1517. n:=alias[n];
  1518. get_alias:=n;
  1519. end;
  1520. procedure Trgobj.add_worklist(u:Tsuperregister);
  1521. var p:byte;
  1522. begin
  1523. if not(u in [first_supreg..last_supreg]) and not move_related(u) and
  1524. (degree[u]<cpu_registers) then
  1525. begin
  1526. p:=pos(char(u),freezeworklist);
  1527. if p=0 then
  1528. internalerror(200308161); {must be found}
  1529. if length(freezeworklist)>1 then
  1530. freezeworklist[p]:=freezeworklist[length(freezeworklist)];
  1531. dec(freezeworklist[0]);
  1532. { delete(freezeworklist,pos(char(u),freezeworklist),1);}
  1533. simplifyworklist:=simplifyworklist+char(u);
  1534. end;
  1535. end;
  1536. function Trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  1537. {Check wether u and v should be coalesced. u is precoloured.}
  1538. function ok(t,r:Tsuperregister):boolean;
  1539. begin
  1540. ok:=(degree[t]<cpu_registers) or
  1541. (t in [first_supreg..last_supreg]) or
  1542. (r in igraph.bitmap[t]);
  1543. end;
  1544. var adj:Pstring;
  1545. i:byte;
  1546. t:char;
  1547. begin
  1548. adjacent_ok:=true;
  1549. adj:=igraph.adjlist[v];
  1550. if adj<>nil then
  1551. for i:=1 to length(adj^) do
  1552. begin
  1553. t:=adj^[i];
  1554. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1555. if not ok(Tsuperregister(t),u) then
  1556. begin
  1557. adjacent_ok:=false;
  1558. break;
  1559. end;
  1560. end;
  1561. end;
  1562. function Trgobj.conservative(u,v:Tsuperregister):boolean;
  1563. var adj:Pstring;
  1564. done:set of char; {To prevent that we count nodes twice.}
  1565. i,k:byte;
  1566. n:char;
  1567. begin
  1568. k:=0;
  1569. done:=[];
  1570. adj:=igraph.adjlist[u];
  1571. if adj<>nil then
  1572. for i:=1 to length(adj^) do
  1573. begin
  1574. n:=adj^[i];
  1575. if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
  1576. begin
  1577. include(done,n);
  1578. if degree[Tsuperregister(n)]>=cpu_registers then
  1579. inc(k);
  1580. end;
  1581. end;
  1582. adj:=igraph.adjlist[v];
  1583. if adj<>nil then
  1584. for i:=1 to length(adj^) do
  1585. begin
  1586. n:=adj^[i];
  1587. if ((pos(n,selectstack) or pos(n,coalescednodes))=0) and
  1588. not (n in done) and
  1589. (degree[Tsuperregister(n)]>=cpu_registers) then
  1590. inc(k);
  1591. end;
  1592. conservative:=(k<cpu_registers);
  1593. end;
  1594. procedure Trgobj.combine(u,v:Tsuperregister);
  1595. var add:boolean;
  1596. adj:Pstring;
  1597. i,p:byte;
  1598. n,o:cardinal;
  1599. t:char;
  1600. decrement:boolean;
  1601. begin
  1602. p:=pos(char(v),freezeworklist);
  1603. if p<>0 then
  1604. delete(freezeworklist,p,1)
  1605. else
  1606. delete(spillworklist,pos(char(v),spillworklist),1);
  1607. coalescednodes:=coalescednodes+char(v);
  1608. alias[v]:=u;
  1609. {Combine both movelists. Since the movelists are sets, only add
  1610. elements that are not already present.}
  1611. for n:=0 to movelist[v]^.count-1 do
  1612. begin
  1613. add:=true;
  1614. for o:=0 to movelist[u]^.count-1 do
  1615. if movelist[u]^.data[o]=movelist[v]^.data[n] then
  1616. begin
  1617. add:=false;
  1618. break;
  1619. end;
  1620. if add then
  1621. add_to_movelist(u,movelist[v]^.data[n]);
  1622. end;
  1623. enable_moves(v);
  1624. adj:=igraph.adjlist[v];
  1625. if adj<>nil then
  1626. for i:=1 to length(adj^) do
  1627. begin
  1628. t:=adj^[i];
  1629. if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
  1630. begin
  1631. decrement:=(Tsuperregister(t)<>u) and not(u in igraph.bitmap[Tsuperregister(t)]);
  1632. add_edge(Tsuperregister(t),u);
  1633. {Do not call decrement_degree because it might move nodes between
  1634. lists while the degree does not change (add_edge will increase it).
  1635. Instead, we will decrement manually. (Only if the degree has been
  1636. increased.)}
  1637. if decrement and (degree[Tsuperregister(t)]>0) then
  1638. dec(degree[Tsuperregister(t)]);
  1639. end;
  1640. end;
  1641. p:=pos(char(u),freezeworklist);
  1642. if (degree[u]>=cpu_registers) and (p<>0) then
  1643. begin
  1644. delete(freezeworklist,p,1);
  1645. spillworklist:=spillworklist+char(u);
  1646. end;
  1647. end;
  1648. procedure Trgobj.coalesce;
  1649. var m:Tmoveins;
  1650. x,y,u,v:Tsuperregister;
  1651. begin
  1652. m:=Tmoveins(worklist_moves.getfirst);
  1653. x:=get_alias(m.instruction.oper[0].reg.number shr 8);
  1654. y:=get_alias(m.instruction.oper[1].reg.number shr 8);
  1655. if y in [first_supreg..last_supreg] then
  1656. begin
  1657. u:=y;
  1658. v:=x;
  1659. end
  1660. else
  1661. begin
  1662. u:=x;
  1663. v:=y;
  1664. end;
  1665. if (u=v) then
  1666. begin
  1667. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1668. coalesced_moves.insert(m);
  1669. add_worklist(u);
  1670. end
  1671. {Do u and v interfere? In that case the move is constrained. Two
  1672. precoloured nodes interfere allways. If v is precoloured, by the above
  1673. code u is precoloured, thus interference...}
  1674. else if (v in [first_supreg..last_supreg]) or (u in igraph.bitmap[v]) then
  1675. begin
  1676. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1677. constrained_moves.insert(m);
  1678. add_worklist(u);
  1679. add_worklist(v);
  1680. end
  1681. {Next test: is it possible and a good idea to coalesce??}
  1682. else if ((u in [first_supreg..last_supreg]) and adjacent_ok(u,v)) or
  1683. (not(u in [first_supreg..last_supreg]) and conservative(u,v)) then
  1684. begin
  1685. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1686. coalesced_moves.insert(m);
  1687. combine(u,v);
  1688. add_worklist(u);
  1689. end
  1690. else
  1691. begin
  1692. m.moveset:=ms_active_moves;
  1693. active_moves.insert(m);
  1694. end;
  1695. end;
  1696. procedure Trgobj.freeze_moves(u:Tsuperregister);
  1697. var i:cardinal;
  1698. m:Tlinkedlistitem;
  1699. v,x,y:Tsuperregister;
  1700. begin
  1701. if movelist[u]<>nil then
  1702. for i:=0 to movelist[u]^.count-1 do
  1703. begin
  1704. m:=movelist[u]^.data[i];
  1705. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1706. begin
  1707. x:=Tmoveins(m).instruction.oper[0].reg.number shr 8;
  1708. y:=Tmoveins(m).instruction.oper[1].reg.number shr 8;
  1709. if get_alias(y)=get_alias(u) then
  1710. v:=get_alias(x)
  1711. else
  1712. v:=get_alias(y);
  1713. {Move m from active_moves/worklist_moves to frozen_moves.}
  1714. if Tmoveins(m).moveset=ms_active_moves then
  1715. active_moves.remove(m)
  1716. else
  1717. worklist_moves.remove(m);
  1718. Tmoveins(m).moveset:=ms_frozen_moves;
  1719. frozen_moves.insert(m);
  1720. if not(move_related(v)) and (degree[v]<cpu_registers) then
  1721. begin
  1722. delete(freezeworklist,pos(char(v),freezeworklist),1);
  1723. simplifyworklist:=simplifyworklist+char(v);
  1724. end;
  1725. end;
  1726. end;
  1727. end;
  1728. procedure Trgobj.freeze;
  1729. var n:Tsuperregister;
  1730. begin
  1731. {We need to take a random element out of the freezeworklist. We take
  1732. the last element. Dirty code!}
  1733. n:=Tsuperregister(freezeworklist[byte(freezeworklist[0])]);
  1734. dec(freezeworklist[0]);
  1735. {Add it to the simplifyworklist.}
  1736. simplifyworklist:=simplifyworklist+char(n);
  1737. freeze_moves(n);
  1738. end;
  1739. procedure Trgobj.select_spill;
  1740. var n:char;
  1741. begin
  1742. {This code is WAY too naive. We need not to select just a register, but
  1743. the register that is used the least...}
  1744. n:=spillworklist[byte(spillworklist[0])];
  1745. dec(spillworklist[0]);
  1746. simplifyworklist:=simplifyworklist+n;
  1747. freeze_moves(Tsuperregister(n));
  1748. end;
  1749. procedure Trgobj.assign_colours;
  1750. {Assign_colours assigns the actual colours to the registers.}
  1751. var adj:Pstring;
  1752. i,j,k:byte;
  1753. n,a:Tsuperregister;
  1754. adj_colours,colourednodes:set of Tsuperregister;
  1755. w:char;
  1756. begin
  1757. spillednodes:='';
  1758. {Colour the cpu registers...}
  1759. colourednodes:=[first_supreg..last_supreg];
  1760. for i:=first_supreg to last_supreg do
  1761. colour[i]:=i;
  1762. {Now colour the imaginary registers on the select-stack.}
  1763. for i:=length(selectstack) downto 1 do
  1764. begin
  1765. n:=Tsuperregister(selectstack[i]);
  1766. {Create a list of colours that we cannot assign to n.}
  1767. adj_colours:=[];
  1768. adj:=igraph.adjlist[n];
  1769. if adj<>nil then
  1770. for j:=1 to length(adj^) do
  1771. begin
  1772. w:=adj^[j];
  1773. a:=get_alias(Tsuperregister(w));
  1774. if a in colourednodes then
  1775. include(adj_colours,colour[a]);
  1776. end;
  1777. include(adj_colours,RS_STACK_POINTER_REG);
  1778. {Assume a spill by default...}
  1779. spillednodes:=spillednodes+char(n);
  1780. {Search for a colour not in this list.}
  1781. for k:=first_supreg to last_supreg do
  1782. if not(k in adj_colours) then
  1783. begin
  1784. colour[n]:=k;
  1785. dec(spillednodes[0]); {Colour found: no spill.}
  1786. include(colourednodes,n);
  1787. if n in used_in_proc_int then
  1788. include(used_in_proc_int,k);
  1789. break;
  1790. end;
  1791. end;
  1792. {Finally colour the nodes that were coalesced.}
  1793. for i:=1 to length(coalescednodes) do
  1794. begin
  1795. n:=Tsuperregister(coalescednodes[i]);
  1796. k:=get_alias(n);
  1797. colour[n]:=colour[k];
  1798. if n in used_in_proc_int then
  1799. include(used_in_proc_int,colour[k]);
  1800. end;
  1801. { registers which aren't available to the register allocator must }
  1802. { retain their value }
  1803. for i := 1 to first_supreg-1 do
  1804. colour[i] := i;
  1805. {$ifdef ra_debug}
  1806. for i:=first_imreg to maxintreg do
  1807. writeln(i:4,' ',colour[i]:4)
  1808. {$endif}
  1809. end;
  1810. procedure Trgobj.colour_registers;
  1811. begin
  1812. repeat
  1813. if length(simplifyworklist)<>0 then
  1814. simplify
  1815. else if not(worklist_moves.empty) then
  1816. coalesce
  1817. else if length(freezeworklist)<>0 then
  1818. freeze
  1819. else if length(spillworklist)<>0 then
  1820. select_spill;
  1821. until (length(simplifyworklist) or
  1822. byte(not(worklist_moves.empty)) or
  1823. length(freezeworklist) or
  1824. length(spillworklist)
  1825. )=0;
  1826. assign_colours;
  1827. end;
  1828. procedure Trgobj.epilogue_colouring;
  1829. {
  1830. procedure move_to_worklist_moves(list:Tlinkedlist);
  1831. var p:Tlinkedlistitem;
  1832. begin
  1833. p:=list.first;
  1834. while p<>nil do
  1835. begin
  1836. Tmoveins(p).moveset:=ms_worklist_moves;
  1837. p:=p.next;
  1838. end;
  1839. worklist_moves.concatlist(list);
  1840. end;
  1841. }
  1842. var i:Tsuperregister;
  1843. begin
  1844. worklist_moves.clear;
  1845. {$ifdef Principle_wrong_by_definition}
  1846. {Move everything back to worklist_moves.}
  1847. move_to_worklist_moves(active_moves);
  1848. move_to_worklist_moves(frozen_moves);
  1849. move_to_worklist_moves(coalesced_moves);
  1850. move_to_worklist_moves(constrained_moves);
  1851. {$endif}
  1852. active_moves.destroy;
  1853. active_moves:=nil;
  1854. frozen_moves.destroy;
  1855. frozen_moves:=nil;
  1856. coalesced_moves.destroy;
  1857. coalesced_moves:=nil;
  1858. constrained_moves.destroy;
  1859. constrained_moves:=nil;
  1860. for i:=0 to 255 do
  1861. if movelist[i]<>nil then
  1862. begin
  1863. dispose(movelist[i]);
  1864. movelist[i]:=0;
  1865. end;
  1866. end;
  1867. procedure Trgobj.clear_interferences(u:Tsuperregister);
  1868. {Remove node u from the interference graph and remove all collected
  1869. move instructions it is associated with.}
  1870. var i:byte;
  1871. j,k,count:cardinal;
  1872. v:Tsuperregister;
  1873. m,n:Tmoveins;
  1874. begin
  1875. if igraph.adjlist[u]<>nil then
  1876. begin
  1877. for i:=1 to length(igraph.adjlist[u]^) do
  1878. begin
  1879. v:=Tsuperregister(igraph.adjlist[u]^[i]);
  1880. {Remove (u,v) and (v,u) from bitmap.}
  1881. exclude(igraph.bitmap[u],v);
  1882. exclude(igraph.bitmap[v],u);
  1883. {Remove (v,u) from adjacency list.}
  1884. if igraph.adjlist[v]<>nil then
  1885. begin
  1886. delete(igraph.adjlist[v]^,pos(char(v),igraph.adjlist[v]^),1);
  1887. if length(igraph.adjlist[v]^)=0 then
  1888. begin
  1889. dispose(igraph.adjlist[v]);
  1890. igraph.adjlist[v]:=nil;
  1891. end;
  1892. end;
  1893. end;
  1894. {Remove ( u,* ) from adjacency list.}
  1895. dispose(igraph.adjlist[u]);
  1896. igraph.adjlist[u]:=nil;
  1897. end;
  1898. {$ifdef Principle_wrong_by_definition}
  1899. {Now remove the moves.}
  1900. if movelist[u]<>nil then
  1901. begin
  1902. for j:=0 to movelist[u]^.count-1 do
  1903. begin
  1904. m:=Tmoveins(movelist[u]^.data[j]);
  1905. {Get the other register of the move instruction.}
  1906. v:=m.instruction.oper[0].reg.number shr 8;
  1907. if v=u then
  1908. v:=m.instruction.oper[1].reg.number shr 8;
  1909. repeat
  1910. repeat
  1911. if (u<>v) and (movelist[v]<>nil) then
  1912. begin
  1913. {Remove the move from it's movelist.}
  1914. count:=movelist[v]^.count-1;
  1915. for k:=0 to count do
  1916. if m=movelist[v]^.data[k] then
  1917. begin
  1918. if k<>count then
  1919. movelist[v]^.data[k]:=movelist[v]^.data[count];
  1920. dec(movelist[v]^.count);
  1921. if count=0 then
  1922. begin
  1923. dispose(movelist[v]);
  1924. movelist[v]:=nil;
  1925. end;
  1926. break;
  1927. end;
  1928. end;
  1929. {The complexity is enourmous: the register might have been
  1930. coalesced. In that case it's movelists have been added to
  1931. it's coalescing alias. (DM)}
  1932. v:=alias[v];
  1933. until v=0;
  1934. {And also register u might have been coalesced.}
  1935. u:=alias[u];
  1936. until u=0;
  1937. case m.moveset of
  1938. ms_coalesced_moves:
  1939. coalesced_moves.remove(m);
  1940. ms_constrained_moves:
  1941. constrained_moves.remove(m);
  1942. ms_frozen_moves:
  1943. frozen_moves.remove(m);
  1944. ms_worklist_moves:
  1945. worklist_moves.remove(m);
  1946. ms_active_moves:
  1947. active_moves.remove(m);
  1948. end;
  1949. end;
  1950. dispose(movelist[u]);
  1951. movelist[u]:=nil;
  1952. end;
  1953. {$endif}
  1954. end;
  1955. procedure Trgobj.getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  1956. var i:Tsuperregister;
  1957. r:Tregister;
  1958. begin
  1959. if not (lastintreg in [first_imreg..last_imreg]) then
  1960. lastintreg:=first_imreg;
  1961. i:=lastintreg;
  1962. repeat
  1963. if i=last_imreg then
  1964. i:=first_imreg
  1965. else
  1966. inc(i);
  1967. if (i in unusedregsint) and (pos(char(i),abtlist)=0) then
  1968. begin
  1969. exclude(unusedregsint,i);
  1970. include(used_in_proc_int,i);
  1971. r.enum:=R_INTREGISTER;
  1972. r.number:=i shl 8 or subreg;
  1973. if position=nil then
  1974. list.insert(Tai_regalloc.alloc(r))
  1975. else
  1976. list.insertafter(Tai_regalloc.alloc(r),position);
  1977. result:=r;
  1978. lastintreg:=i;
  1979. if i>maxintreg then
  1980. maxintreg:=i;
  1981. add_edges_used(i);
  1982. add_constraints(result.number);
  1983. exit;
  1984. end;
  1985. until i=lastintreg;
  1986. internalerror(10);
  1987. end;
  1988. {In some cases we can get in big trouble. See this example:
  1989. ; register reg23d released
  1990. ; register eax allocated
  1991. ; register ebx allocated
  1992. ; register ecx allocated
  1993. ; register edx allocated
  1994. ; register esi allocated
  1995. ; register edi allocated
  1996. call [reg23d]
  1997. This code is ok, *except* when reg23d is spilled. In that case the
  1998. spilled would introduce a help register which can never get
  1999. allocated to a real register because it interferes with all of them.
  2000. To solve this we introduce the ABT ("avoid big trouble :)" ) registers.
  2001. If you allocate an ABT register you get a register that has less
  2002. than cpu_register interferences and will not be allocated ever again
  2003. by the normal register get procedures. In other words it is for sure it
  2004. will never get spilled.}
  2005. function Trgobj.getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
  2006. var i:Tsuperregister;
  2007. r:Tregister;
  2008. found:boolean;
  2009. begin
  2010. if not (lastintreg in [first_imreg..last_imreg]) then
  2011. lastintreg:=first_imreg;
  2012. found:=false;
  2013. for i:=1 to length(abtlist) do
  2014. if Tsuperregister(abtlist[i]) in unusedregsint then
  2015. begin
  2016. found:=true;
  2017. break;
  2018. end;
  2019. i:=lastintreg;
  2020. repeat
  2021. if i=last_imreg then
  2022. i:=first_imreg
  2023. else
  2024. inc(i);
  2025. if (i in unusedregsint) and ((igraph.adjlist[i]=nil) or (length(igraph.adjlist[i]^)<cpu_registers)) then
  2026. begin
  2027. found:=true;
  2028. break;
  2029. end;
  2030. until i=lastintreg;
  2031. if found then
  2032. begin
  2033. exclude(unusedregsint,i);
  2034. include(used_in_proc_int,i);
  2035. r.enum:=R_INTREGISTER;
  2036. r.number:=i shl 8 or cgsize2subreg(size);
  2037. list.concat(Tai_regalloc.alloc(r));
  2038. getabtregisterint:=r;
  2039. lastintreg:=i;
  2040. if i>maxintreg then
  2041. maxintreg:=i;
  2042. add_edges_used(i);
  2043. if pos(char(i),abtlist)=0 then
  2044. abtlist:=abtlist+char(i);
  2045. end
  2046. else
  2047. internalerror(10);
  2048. {$ifdef newra}
  2049. add_constraints(getabtregisterint.number);
  2050. {$endif}
  2051. end;
  2052. procedure Trgobj.ungetregisterintinline(list:Taasmoutput;position:Tai;const r:Tregister);
  2053. var supreg:Tsuperregister;
  2054. begin
  2055. if r.enum<=lastreg then
  2056. internalerror(200301083);
  2057. supreg:=r.number shr 8;
  2058. include(unusedregsint,supreg);
  2059. if position=nil then
  2060. list.insert(Tai_regalloc.dealloc(r))
  2061. else
  2062. list.insertafter(Tai_regalloc.dealloc(r),position);
  2063. add_edges_used(supreg);
  2064. end;
  2065. function Trgobj.spill_registers(list:Taasmoutput;const regs_to_spill:string):boolean;
  2066. {Returns true if any help registers have been used.}
  2067. var i:byte;
  2068. r:Tsuperregister;
  2069. p,q:Tai;
  2070. regs_to_spill_set:Tsupregset;
  2071. spill_temps:^Tspill_temp_list;
  2072. begin
  2073. spill_registers:=false;
  2074. unusedregsint:=[0..255];
  2075. fillchar(degree,sizeof(degree),0);
  2076. {Precoloured nodes should have an infinite degree, which we can approach
  2077. by 255.}
  2078. for i:=first_supreg to last_supreg do
  2079. degree[i]:=255;
  2080. { exclude(unusedregsint,RS_STACK_POINTER_REG);}
  2081. if current_procinfo.framepointer.number=NR_FRAME_POINTER_REG then
  2082. {Make sure the register allocator won't allocate registers into ebp.}
  2083. exclude(unusedregsint,RS_FRAME_POINTER_REG);
  2084. new(spill_temps);
  2085. fillchar(spill_temps^,sizeof(spill_temps^),0);
  2086. regs_to_spill_set:=[];
  2087. for i:=1 to length(regs_to_spill) do
  2088. begin
  2089. {Alternative representation.}
  2090. include(regs_to_spill_set,Tsuperregister(regs_to_spill[i]));
  2091. {Clear all interferences of the spilled register.}
  2092. clear_interferences(Tsuperregister(regs_to_spill[i]));
  2093. {Get a temp for the spilled register.}
  2094. tg.gettemp(list,4,tt_noreuse,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  2095. end;
  2096. p:=Tai(list.first);
  2097. while assigned(p) do
  2098. begin
  2099. case p.typ of
  2100. ait_regalloc:
  2101. begin
  2102. {A register allocation of a spilled register can be removed.}
  2103. if (Tai_regalloc(p).reg.number shr 8) in regs_to_spill_set then
  2104. begin
  2105. q:=p;
  2106. p:=Tai(p.next);
  2107. list.remove(q);
  2108. continue;
  2109. end
  2110. else
  2111. if Tai_regalloc(p).allocation then
  2112. exclude(unusedregsint,Tai_regalloc(p).reg.number shr 8)
  2113. else
  2114. include(unusedregsint,Tai_regalloc(p).reg.number shr 8);
  2115. end;
  2116. ait_instruction:
  2117. begin
  2118. if Taicpu_abstract(p).spill_registers(list,@getregisterintinline,
  2119. @ungetregisterintinline,
  2120. regs_to_spill_set,
  2121. unusedregsint,
  2122. spill_temps^) then
  2123. spill_registers:=true;
  2124. if Taicpu_abstract(p).is_move then
  2125. add_move_instruction(Taicpu(p));
  2126. end;
  2127. end;
  2128. p:=Tai(p.next);
  2129. end;
  2130. for i:=1 to length(regs_to_spill) do
  2131. begin
  2132. tg.ungettemp(list,spill_temps^[Tsuperregister(regs_to_spill[i])]);
  2133. end;
  2134. dispose(spill_temps);
  2135. end;
  2136. {$endif newra}
  2137. {****************************************************************************
  2138. TReference
  2139. ****************************************************************************}
  2140. procedure reference_reset(var ref : treference);
  2141. begin
  2142. FillChar(ref,sizeof(treference),0);
  2143. ref.base.enum:=R_INTREGISTER;
  2144. ref.index.enum:=R_INTREGISTER;
  2145. {$ifdef i386}
  2146. ref.segment.enum:=R_INTREGISTER;
  2147. {$endif}
  2148. end;
  2149. procedure reference_reset_old(var ref : treference);
  2150. begin
  2151. FillChar(ref,sizeof(treference),0);
  2152. end;
  2153. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  2154. begin
  2155. reference_reset(ref);
  2156. ref.base:=base;
  2157. ref.offset:=offset;
  2158. end;
  2159. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  2160. begin
  2161. reference_reset(ref);
  2162. ref.symbol:=sym;
  2163. ref.offset:=offset;
  2164. end;
  2165. procedure reference_release(list: taasmoutput; const ref : treference);
  2166. begin
  2167. rg.ungetreference(list,ref);
  2168. end;
  2169. function references_equal(sref : treference;dref : treference):boolean;
  2170. begin
  2171. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  2172. end;
  2173. { on most processors , this routine does nothing, overriden currently }
  2174. { only by 80x86 processor. }
  2175. function trgobj.makeregsize(reg: tregister; size: tcgsize): tregister;
  2176. begin
  2177. makeregsize := reg;
  2178. end;
  2179. {****************************************************************************
  2180. TLocation
  2181. ****************************************************************************}
  2182. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  2183. begin
  2184. FillChar(l,sizeof(tlocation),0);
  2185. l.loc:=lt;
  2186. l.size:=lsize;
  2187. case l.loc of
  2188. LOC_REGISTER,LOC_CREGISTER:
  2189. begin
  2190. l.register.enum:=R_INTREGISTER;
  2191. l.registerhigh.enum:=R_INTREGISTER;
  2192. end;
  2193. LOC_REFERENCE,LOC_CREFERENCE:
  2194. begin
  2195. l.reference.base.enum:=R_INTREGISTER;
  2196. l.reference.index.enum:=R_INTREGISTER;
  2197. {$ifdef i386}
  2198. l.reference.segment.enum:=R_INTREGISTER;
  2199. {$endif}
  2200. end;
  2201. end;
  2202. end;
  2203. procedure location_release(list: taasmoutput; const l : tlocation);
  2204. begin
  2205. case l.loc of
  2206. LOC_REGISTER,LOC_CREGISTER :
  2207. begin
  2208. rg.ungetregisterint(list,l.register);
  2209. if l.size in [OS_64,OS_S64] then
  2210. rg.ungetregisterint(list,l.registerhigh);
  2211. end;
  2212. LOC_FPUREGISTER,LOC_CFPUREGISTER :
  2213. rg.ungetregisterfpu(list,l.register,l.size);
  2214. LOC_CREFERENCE,LOC_REFERENCE :
  2215. rg.ungetreference(list, l.reference);
  2216. end;
  2217. end;
  2218. procedure location_freetemp(list:taasmoutput; const l : tlocation);
  2219. begin
  2220. if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  2221. tg.ungetiftemp(list,l.reference);
  2222. end;
  2223. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  2224. begin
  2225. destloc:=sourceloc;
  2226. end;
  2227. procedure location_swap(var destloc,sourceloc : tlocation);
  2228. var
  2229. swapl : tlocation;
  2230. begin
  2231. swapl := destloc;
  2232. destloc := sourceloc;
  2233. sourceloc := swapl;
  2234. end;
  2235. initialization
  2236. ;
  2237. finalization
  2238. rg.free;
  2239. end.
  2240. {
  2241. $Log$
  2242. Revision 1.66 2003-08-17 16:59:20 jonas
  2243. * fixed regvars so they work with newra (at least for ppc)
  2244. * fixed some volatile register bugs
  2245. + -dnotranslation option for -dnewra, which causes the registers not to
  2246. be translated from virtual to normal registers. Requires support in
  2247. the assembler writer as well, which is only implemented in aggas/
  2248. agppcgas currently
  2249. Revision 1.65 2003/08/17 14:32:48 daniel
  2250. * Precoloured nodes now have an infinite degree approached with 255,
  2251. like they should.
  2252. Revision 1.64 2003/08/17 08:48:02 daniel
  2253. * Another register allocator bug fixed.
  2254. * cpu_registers set to 6 for i386
  2255. Revision 1.63 2003/08/09 18:56:54 daniel
  2256. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  2257. allocator
  2258. * Some preventive changes to i386 spillinh code
  2259. Revision 1.62 2003/08/03 14:09:50 daniel
  2260. * Fixed a register allocator bug
  2261. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  2262. statements: changes in location_force. These moves are now no longer
  2263. constrained so they are optimized away.
  2264. Revision 1.61 2003/07/21 13:32:39 jonas
  2265. * add_edges_used() is now also called for registers allocated with
  2266. getexplicitregisterint()
  2267. * writing the intereference graph is now only done with -dradebug2 and
  2268. the created files are now called "igraph.<module_name>"
  2269. Revision 1.60 2003/07/06 15:31:21 daniel
  2270. * Fixed register allocator. *Lots* of fixes.
  2271. Revision 1.59 2003/07/06 15:00:47 jonas
  2272. * fixed my previous completely broken commit. It's not perfect though,
  2273. registers > last_supreg and < max_intreg may still be "translated"
  2274. Revision 1.58 2003/07/06 14:45:05 jonas
  2275. * support integer registers that are not managed by newra (ie. don't
  2276. translate register numbers that fall outside the range
  2277. first_supreg..last_supreg)
  2278. Revision 1.57 2003/07/02 22:18:04 peter
  2279. * paraloc splitted in callerparaloc,calleeparaloc
  2280. * sparc calling convention updates
  2281. Revision 1.56 2003/06/17 16:34:44 jonas
  2282. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  2283. * renamed all_intregisters to volatile_intregisters and made it
  2284. processor dependent
  2285. Revision 1.55 2003/06/14 14:53:50 jonas
  2286. * fixed newra cycle for x86
  2287. * added constants for indicating source and destination operands of the
  2288. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  2289. Revision 1.54 2003/06/13 21:19:31 peter
  2290. * current_procdef removed, use current_procinfo.procdef instead
  2291. Revision 1.53 2003/06/12 21:11:10 peter
  2292. * ungetregisterfpu gets size parameter
  2293. Revision 1.52 2003/06/12 16:43:07 peter
  2294. * newra compiles for sparc
  2295. Revision 1.51 2003/06/09 14:54:26 jonas
  2296. * (de)allocation of registers for parameters is now performed properly
  2297. (and checked on the ppc)
  2298. - removed obsolete allocation of all parameter registers at the start
  2299. of a procedure (and deallocation at the end)
  2300. Revision 1.50 2003/06/03 21:11:09 peter
  2301. * cg.a_load_* get a from and to size specifier
  2302. * makeregsize only accepts newregister
  2303. * i386 uses generic tcgnotnode,tcgunaryminus
  2304. Revision 1.49 2003/06/03 13:01:59 daniel
  2305. * Register allocator finished
  2306. Revision 1.48 2003/06/01 21:38:06 peter
  2307. * getregisterfpu size parameter added
  2308. * op_const_reg size parameter added
  2309. * sparc updates
  2310. Revision 1.47 2003/05/31 20:31:11 jonas
  2311. * set inital costs of assigning a variable to a register to 120 for
  2312. non-i386, because the used register must be store to memory at the
  2313. start and loaded again at the end
  2314. Revision 1.46 2003/05/30 18:55:21 jonas
  2315. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  2316. works for ppc
  2317. Revision 1.45 2003/05/30 12:36:13 jonas
  2318. * use as little different registers on the ppc until newra is released,
  2319. since every used register must be saved
  2320. Revision 1.44 2003/05/17 13:30:08 jonas
  2321. * changed tt_persistant to tt_persistent :)
  2322. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2323. temps, but a ttemptype, so you can also create ansistring temps etc
  2324. Revision 1.43 2003/05/16 14:33:31 peter
  2325. * regvar fixes
  2326. Revision 1.42 2003/04/26 20:03:49 daniel
  2327. * Bug fix in simplify
  2328. Revision 1.41 2003/04/25 20:59:35 peter
  2329. * removed funcretn,funcretsym, function result is now in varsym
  2330. and aliases for result and function name are added using absolutesym
  2331. * vs_hidden parameter for funcret passed in parameter
  2332. * vs_hidden fixes
  2333. * writenode changed to printnode and released from extdebug
  2334. * -vp option added to generate a tree.log with the nodetree
  2335. * nicer printnode for statements, callnode
  2336. Revision 1.40 2003/04/25 08:25:26 daniel
  2337. * Ifdefs around a lot of calls to cleartempgen
  2338. * Fixed registers that are allocated but not freed in several nodes
  2339. * Tweak to register allocator to cause less spills
  2340. * 8-bit registers now interfere with esi,edi and ebp
  2341. Compiler can now compile rtl successfully when using new register
  2342. allocator
  2343. Revision 1.39 2003/04/23 20:23:06 peter
  2344. * compile fix for no-newra
  2345. Revision 1.38 2003/04/23 14:42:07 daniel
  2346. * Further register allocator work. Compiler now smaller with new
  2347. allocator than without.
  2348. * Somebody forgot to adjust ppu version number
  2349. Revision 1.37 2003/04/22 23:50:23 peter
  2350. * firstpass uses expectloc
  2351. * checks if there are differences between the expectloc and
  2352. location.loc from secondpass in EXTDEBUG
  2353. Revision 1.36 2003/04/22 10:09:35 daniel
  2354. + Implemented the actual register allocator
  2355. + Scratch registers unavailable when new register allocator used
  2356. + maybe_save/maybe_restore unavailable when new register allocator used
  2357. Revision 1.35 2003/04/21 19:16:49 peter
  2358. * count address regs separate
  2359. Revision 1.34 2003/04/17 16:48:21 daniel
  2360. * Added some code to keep track of move instructions in register
  2361. allocator
  2362. Revision 1.33 2003/04/17 07:50:24 daniel
  2363. * Some work on interference graph construction
  2364. Revision 1.32 2003/03/28 19:16:57 peter
  2365. * generic constructor working for i386
  2366. * remove fixed self register
  2367. * esi added as address register for i386
  2368. Revision 1.31 2003/03/11 21:46:24 jonas
  2369. * lots of new regallocator fixes, both in generic and ppc-specific code
  2370. (ppc compiler still can't compile the linux system unit though)
  2371. Revision 1.30 2003/03/09 21:18:59 olle
  2372. + added cutils to the uses clause
  2373. Revision 1.29 2003/03/08 20:36:41 daniel
  2374. + Added newra version of Ti386shlshrnode
  2375. + Added interference graph construction code
  2376. Revision 1.28 2003/03/08 13:59:16 daniel
  2377. * Work to handle new register notation in ag386nsm
  2378. + Added newra version of Ti386moddivnode
  2379. Revision 1.27 2003/03/08 10:53:48 daniel
  2380. * Created newra version of secondmul in n386add.pas
  2381. Revision 1.26 2003/03/08 08:59:07 daniel
  2382. + $define newra will enable new register allocator
  2383. + getregisterint will return imaginary registers with $newra
  2384. + -sr switch added, will skip register allocation so you can see
  2385. the direct output of the code generator before register allocation
  2386. Revision 1.25 2003/02/26 20:50:45 daniel
  2387. * Fixed ungetreference
  2388. Revision 1.24 2003/02/19 22:39:56 daniel
  2389. * Fixed a few issues
  2390. Revision 1.23 2003/02/19 22:00:14 daniel
  2391. * Code generator converted to new register notation
  2392. - Horribily outdated todo.txt removed
  2393. Revision 1.22 2003/02/02 19:25:54 carl
  2394. * Several bugfixes for m68k target (register alloc., opcode emission)
  2395. + VIS target
  2396. + Generic add more complete (still not verified)
  2397. Revision 1.21 2003/01/08 18:43:57 daniel
  2398. * Tregister changed into a record
  2399. Revision 1.20 2002/10/05 12:43:28 carl
  2400. * fixes for Delphi 6 compilation
  2401. (warning : Some features do not work under Delphi)
  2402. Revision 1.19 2002/08/23 16:14:49 peter
  2403. * tempgen cleanup
  2404. * tt_noreuse temp type added that will be used in genentrycode
  2405. Revision 1.18 2002/08/17 22:09:47 florian
  2406. * result type handling in tcgcal.pass_2 overhauled
  2407. * better tnode.dowrite
  2408. * some ppc stuff fixed
  2409. Revision 1.17 2002/08/17 09:23:42 florian
  2410. * first part of procinfo rewrite
  2411. Revision 1.16 2002/08/06 20:55:23 florian
  2412. * first part of ppc calling conventions fix
  2413. Revision 1.15 2002/08/05 18:27:48 carl
  2414. + more more more documentation
  2415. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2416. Revision 1.14 2002/08/04 19:06:41 carl
  2417. + added generic exception support (still does not work!)
  2418. + more documentation
  2419. Revision 1.13 2002/07/07 09:52:32 florian
  2420. * powerpc target fixed, very simple units can be compiled
  2421. * some basic stuff for better callparanode handling, far from being finished
  2422. Revision 1.12 2002/07/01 18:46:26 peter
  2423. * internal linker
  2424. * reorganized aasm layer
  2425. Revision 1.11 2002/05/18 13:34:17 peter
  2426. * readded missing revisions
  2427. Revision 1.10 2002/05/16 19:46:44 carl
  2428. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2429. + try to fix temp allocation (still in ifdef)
  2430. + generic constructor calls
  2431. + start of tassembler / tmodulebase class cleanup
  2432. Revision 1.8 2002/04/21 15:23:03 carl
  2433. + makeregsize
  2434. + changeregsize is now a local routine
  2435. Revision 1.7 2002/04/20 21:32:25 carl
  2436. + generic FPC_CHECKPOINTER
  2437. + first parameter offset in stack now portable
  2438. * rename some constants
  2439. + move some cpu stuff to other units
  2440. - remove unused constents
  2441. * fix stacksize for some targets
  2442. * fix generic size problems which depend now on EXTEND_SIZE constant
  2443. Revision 1.6 2002/04/15 19:03:31 carl
  2444. + reg2str -> std_reg2str()
  2445. Revision 1.5 2002/04/06 18:13:01 jonas
  2446. * several powerpc-related additions and fixes
  2447. Revision 1.4 2002/04/04 19:06:04 peter
  2448. * removed unused units
  2449. * use tlocation.size in cg.a_*loc*() routines
  2450. Revision 1.3 2002/04/02 17:11:29 peter
  2451. * tlocation,treference update
  2452. * LOC_CONSTANT added for better constant handling
  2453. * secondadd splitted in multiple routines
  2454. * location_force_reg added for loading a location to a register
  2455. of a specified size
  2456. * secondassignment parses now first the right and then the left node
  2457. (this is compatible with Kylix). This saves a lot of push/pop especially
  2458. with string operations
  2459. * adapted some routines to use the new cg methods
  2460. Revision 1.2 2002/04/01 19:24:25 jonas
  2461. * fixed different parameter name in interface and implementation
  2462. declaration of a method (only 1.0.x detected this)
  2463. Revision 1.1 2002/03/31 20:26:36 jonas
  2464. + a_loadfpu_* and a_loadmm_* methods in tcg
  2465. * register allocation is now handled by a class and is mostly processor
  2466. independent (+rgobj.pas and i386/rgcpu.pas)
  2467. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2468. * some small improvements and fixes to the optimizer
  2469. * some register allocation fixes
  2470. * some fpuvaroffset fixes in the unary minus node
  2471. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2472. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2473. also better optimizable)
  2474. * fixed and optimized register saving/restoring for new/dispose nodes
  2475. * LOC_FPU locations now also require their "register" field to be set to
  2476. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2477. - list field removed of the tnode class because it's not used currently
  2478. and can cause hard-to-find bugs
  2479. }