rgobj.pas 93 KB

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