rgobj.pas 94 KB

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