aoptobj.pas 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698
  1. {
  2. Copyright (c) 1998-2004 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit contains the processor independent assembler optimizer
  5. object, base for the dataflow analyzer, peepholeoptimizer and
  6. common subexpression elimination objects.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. Unit AoptObj;
  21. { $define DEBUG_AOPTOBJ}
  22. { $define DEBUG_JUMP}
  23. {$i fpcdefs.inc}
  24. { general, processor independent objects for use by the assembler optimizer }
  25. Interface
  26. uses
  27. globtype,
  28. aasmbase,aasmcpu,aasmtai,aasmdata,
  29. cclasses,
  30. cgbase,cgutils,
  31. cpubase,
  32. aoptbase,aoptcpub,aoptda;
  33. { ************************************************************************* }
  34. { ********************************* Constants ***************************** }
  35. { ************************************************************************* }
  36. Const
  37. {Possible register content types}
  38. con_Unknown = 0;
  39. con_ref = 1;
  40. con_const = 2;
  41. {***************** Types ****************}
  42. Type
  43. { ************************************************************************* }
  44. { ************************* Some general type definitions ***************** }
  45. { ************************************************************************* }
  46. TRefCompare = Function(const r1, r2: TReference): Boolean;
  47. //!!! FIXME
  48. TRegArray = Array[byte] of tsuperregister;
  49. TRegSet = tcpuregisterset;
  50. { possible actions on an operand: read, write or modify (= read & write) }
  51. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  52. { ************************************************************************* }
  53. { * Object to hold information on which regiters are in use and which not * }
  54. { ************************************************************************* }
  55. { TUsedRegs }
  56. TUsedRegs = class
  57. Constructor create(aTyp : TRegisterType);
  58. Constructor create_regset(aTyp : TRegisterType;Const _RegSet: TRegSet);
  59. Destructor Destroy;override;
  60. Procedure Clear;
  61. { update the info with the pairegalloc objects coming after
  62. p }
  63. procedure Update(p: Tai; IgnoreNewAllocs: Boolean=false);
  64. { is Reg currently in use }
  65. Function IsUsed(Reg: TRegister): Boolean;
  66. { get all the currently used registers }
  67. Function GetUsedRegs: TRegSet; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  68. { outputs the current set }
  69. Procedure Dump(var t : text);
  70. Private
  71. Typ : TRegisterType;
  72. UsedRegs: TRegSet;
  73. End;
  74. { ************************************************************************* }
  75. { ******************* Contents of the integer registers ******************* }
  76. { ************************************************************************* }
  77. { size of the integer that holds the state number of a register. Can be any }
  78. { integer type, so it can be changed to reduce the size of the TContent }
  79. { structure or to improve alignment }
  80. TStateInt = Byte;
  81. TContent = Record
  82. { start and end of block instructions that defines the }
  83. { content of this register. If Typ = con_const, then }
  84. { Longint(StartMod) = value of the constant) }
  85. StartMod: Tai;
  86. { starts at 0, gets increased everytime the register is }
  87. { written to }
  88. WState: TStateInt;
  89. { starts at 0, gets increased everytime the register is read }
  90. { from }
  91. RState: TStateInt;
  92. { how many instructions starting with StarMod does the block }
  93. { consist of }
  94. NrOfMods: Byte;
  95. { the type of the content of the register: unknown, memory }
  96. { (variable) or constant }
  97. Typ: Byte;
  98. End;
  99. //!!! FIXME
  100. TRegContent = Array[byte] Of TContent;
  101. { ************************************************************************** }
  102. { information object with the contents of every register. Every Tai object }
  103. { gets one of these assigned: a pointer to it is stored in the OptInfo field }
  104. { ************************************************************************** }
  105. { TPaiProp }
  106. TPaiProp = class(TAoptBaseCpu)
  107. Regs: TRegContent;
  108. { can this instruction be removed? }
  109. CanBeRemoved: Boolean;
  110. Constructor create; reintroduce;
  111. { checks the whole sequence of which (so regs[which].StartMod and and }
  112. { the next NrOfMods Tai objects) to see whether Reg is used somewhere, }
  113. { without it being loaded with something else first }
  114. Function RegInSequence(Reg, which: TRegister): Boolean;
  115. { destroy the contents of a register, as well as those whose contents }
  116. { are based on those of that register }
  117. Procedure DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  118. TInstrSinceLastMod);
  119. { if the contents of WhichReg (can be R_NO in case of a constant) are }
  120. { written to memory at the location Ref, the contents of the registers }
  121. { that depend on Ref have to be destroyed }
  122. Procedure DestroyRefs(Const Ref: TReference; WhichReg: TRegister; var
  123. InstrSinceLastMod: TInstrSinceLastMod);
  124. { an instruction reads from operand o }
  125. Procedure ReadOp(const o:toper);
  126. { an instruction reads from reference Ref }
  127. Procedure ReadRef(Ref: PReference);
  128. { an instruction reads from register Reg }
  129. Procedure ReadReg(Reg: TRegister);
  130. { an instruction writes/modifies operand o and this has special }
  131. { side-effects or modifies the contents in such a way that we can't }
  132. { simply add this instruction to the sequence of instructions that }
  133. { describe the contents of the operand, so destroy it }
  134. Procedure DestroyOp(const o:Toper; var InstrSinceLastMod:
  135. TInstrSinceLastMod);
  136. { destroy the contents of all registers }
  137. Procedure DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  138. { a register's contents are modified, but not destroyed (the new value
  139. depends on the old one) }
  140. Procedure ModifyReg(reg: TRegister; var InstrSinceLastMod:
  141. TInstrSinceLastMod);
  142. { an operand's contents are modified, but not destroyed (the new value
  143. depends on the old one) }
  144. Procedure ModifyOp(const oper: TOper; var InstrSinceLastMod:
  145. TInstrSinceLastMod);
  146. { increase the write state of a register (call every time a register is
  147. written to) }
  148. Procedure IncWState(Reg: TRegister);
  149. { increase the read state of a register (call every time a register is }
  150. { read from) }
  151. Procedure IncRState(Reg: TRegister);
  152. { get the write state of a register }
  153. Function GetWState(Reg: TRegister): TStateInt;
  154. { get the read state of a register }
  155. Function GetRState(Reg: TRegister): TStateInt;
  156. { get the type of contents of a register }
  157. Function GetRegContentType(Reg: TRegister): Byte;
  158. Destructor Done;
  159. Private
  160. Procedure IncState(var s: TStateInt);
  161. { returns whether the reference Ref is used somewhere in the loading }
  162. { sequence Content }
  163. class function RefInSequence(Const Ref: TReference; Content: TContent;
  164. RefsEq: TRefCompare): Boolean; static;
  165. { returns whether the instruction P reads from and/or writes }
  166. { to Reg }
  167. class function RefInInstruction(Const Ref: TReference; p: Tai;
  168. RefsEq: TRefCompare): Boolean; static;
  169. { returns whether two references with at least one pointing to an array }
  170. { may point to the same memory location }
  171. End;
  172. { ************************************************************************* }
  173. { ************************ Label information ****************************** }
  174. { ************************************************************************* }
  175. TLabelTableItem = Record
  176. PaiObj: Tai;
  177. End;
  178. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  179. PLabelTable = ^TLabelTable;
  180. PLabelInfo = ^TLabelInfo;
  181. TLabelInfo = Record
  182. { the highest and lowest label number occurring in the current code }
  183. { fragment }
  184. LowLabel, HighLabel: longint;
  185. LabelDif: cardinal;
  186. { table that contains the addresses of the Pai_Label objects associated
  187. with each label number }
  188. LabelTable: PLabelTable;
  189. End;
  190. { ************************************************************************* }
  191. { ********** General optimizer object, used to derive others from ********* }
  192. { ************************************************************************* }
  193. TAllUsedRegs = array[TRegisterType] of TUsedRegs;
  194. { TAOptObj }
  195. TAOptObj = class(TAoptBaseCpu)
  196. { the PAasmOutput list this optimizer instance works on }
  197. AsmL: TAsmList;
  198. { The labelinfo record contains the addresses of the Tai objects }
  199. { that are labels, how many labels there are and the min and max }
  200. { label numbers }
  201. LabelInfo: PLabelInfo;
  202. { Start and end of the block that is currently being optimized, and
  203. a selected start point after the start of the block }
  204. BlockStart, BlockEnd, StartPoint: Tai;
  205. DFA: TAOptDFA;
  206. UsedRegs: TAllUsedRegs;
  207. { _AsmL is the PAasmOutpout list that has to be optimized, }
  208. { _BlockStart and _BlockEnd the start and the end of the block }
  209. { that has to be optimized and _LabelInfo a pointer to a }
  210. { TLabelInfo record }
  211. Constructor create(_AsmL: TAsmList; _BlockStart, _BlockEnd: Tai;
  212. _LabelInfo: PLabelInfo); virtual; reintroduce;
  213. Destructor Destroy;override;
  214. { processor independent methods }
  215. Procedure CreateUsedRegs(var regs: TAllUsedRegs);
  216. Procedure ClearUsedRegs;
  217. Procedure UpdateUsedRegs(p : Tai);
  218. class procedure UpdateUsedRegs(var Regs: TAllUsedRegs; p: Tai); static;
  219. { If UpdateUsedRegsAndOptimize has read ahead, the result is one before
  220. the next valid entry (so "p.Next" returns what's expected). If no
  221. reading ahead happened, then the result is equal to p. }
  222. function UpdateUsedRegsAndOptimize(p : Tai): Tai;
  223. Function CopyUsedRegs(var dest : TAllUsedRegs) : boolean;
  224. procedure RestoreUsedRegs(const Regs : TAllUsedRegs);
  225. procedure TransferUsedRegs(var dest: TAllUsedRegs);
  226. class procedure ReleaseUsedRegs(const regs : TAllUsedRegs); static;
  227. class function RegInUsedRegs(reg : TRegister;regs : TAllUsedRegs) : boolean; static;
  228. class procedure IncludeRegInUsedRegs(reg : TRegister;var regs : TAllUsedRegs); static;
  229. class procedure ExcludeRegFromUsedRegs(reg: TRegister;var regs : TAllUsedRegs); static;
  230. class function GetAllocationString(const regs : TAllUsedRegs) : string; static;
  231. { returns true if the label L is found between hp and the next }
  232. { instruction }
  233. class function FindLabel(L: TasmLabel; Var hp: Tai): Boolean; static;
  234. { inserts new_one between prev and foll in AsmL }
  235. Procedure InsertLLItem(prev, foll, new_one: TLinkedListItem);
  236. { If P is a Tai object releveant to the optimizer, P is returned
  237. If it is not relevant tot he optimizer, the first object after P
  238. that is relevant is returned }
  239. class function SkipHead(P: Tai): Tai; static;
  240. { returns true if the operands o1 and o2 are completely equal }
  241. class function OpsEqual(const o1,o2:toper): Boolean; static;
  242. { Returns the next ait_alloc object with ratype ra_alloc for
  243. Reg is found in the block
  244. of Tai's starting with StartPai and ending with the next "real"
  245. instruction. If none is found, it returns
  246. nil
  247. }
  248. class function FindRegAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc; static;
  249. { Returns the last ait_alloc object with ratype ra_alloc for
  250. Reg is found in the block
  251. of Tai's starting with StartPai and ending with the next "real"
  252. instruction. If none is found, it returns
  253. nil
  254. }
  255. class function FindRegAllocBackward(Reg : TRegister; StartPai : Tai) : tai_regalloc; static;
  256. { Returns the next ait_alloc object with ratype ra_dealloc
  257. for Reg which is found in the block of Tai's starting with StartPai
  258. and ending with the next "real" instruction. If none is found, it returns
  259. nil }
  260. class function FindRegDeAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc; static;
  261. { allocates register reg between (and including) instructions p1 and p2
  262. the type of p1 and p2 must not be in SkipInstr }
  263. procedure AllocRegBetween(reg : tregister; p1,p2 : tai; var initialusedregs : TAllUsedRegs);
  264. { reg used after p? }
  265. function RegUsedAfterInstruction(reg: Tregister; p: tai; var AllUsedRegs: TAllUsedRegs): Boolean;
  266. { returns true if reg reaches it's end of life at p, this means it is either
  267. reloaded with a new value or it is deallocated afterwards }
  268. function RegEndOfLife(reg: TRegister;p: taicpu): boolean;
  269. { Returns the next ait_tempalloc object with allocation=false
  270. for Offset which is found in the block of Tai's starting with StartPai
  271. and ending with the next "real" instruction. If none is found, it returns
  272. nil }
  273. class function FindTempDeAlloc(Offset: ASizeInt; StartPai: Tai): tai_tempalloc;
  274. { removes p from asml, updates registers and replaces it by a valid value, if this is the case true is returned }
  275. function RemoveCurrentP(var p : tai): boolean;
  276. { removes p from asml, updates registers and replaces p with hp1 (if the next instruction was known beforehand) }
  277. procedure RemoveCurrentP(var p: tai; const hp1: tai); inline;
  278. { removes hp from asml then frees it }
  279. procedure RemoveInstruction(const hp: tai); inline;
  280. { traces sucessive jumps to their final destination and sets it, e.g.
  281. je l1 je l3
  282. <code> <code>
  283. l1: becomes l1:
  284. je l2 je l3
  285. <code> <code>
  286. l2: l2:
  287. jmp l3 jmp l3
  288. the level parameter denotes how deeep we have already followed the jump,
  289. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  290. function GetFinalDestination(hp: taicpu; level: longint): boolean;
  291. function getlabelwithsym(sym: tasmlabel): tai;
  292. { Removes an instruction following hp1 (possibly with reg.deallocations in between),
  293. if its opcode is A_NOP. }
  294. procedure RemoveDelaySlot(hp1: tai);
  295. { peephole optimizer }
  296. procedure PrePeepHoleOpts; virtual;
  297. procedure PeepHoleOptPass1; virtual;
  298. procedure PeepHoleOptPass2; virtual;
  299. procedure PostPeepHoleOpts; virtual;
  300. { processor dependent methods }
  301. // if it returns true, perform a "continue"
  302. function PrePeepHoleOptsCpu(var p: tai): boolean; virtual;
  303. function PeepHoleOptPass1Cpu(var p: tai): boolean; virtual;
  304. function PeepHoleOptPass2Cpu(var p: tai): boolean; virtual;
  305. function PostPeepHoleOptsCpu(var p: tai): boolean; virtual;
  306. { Output debug message to console - null function if EXTDEBUG is not defined }
  307. class procedure DebugWrite(Message: string); static; inline;
  308. { Converts a conditional jump into an unconditional jump. Only call this
  309. procedure on an instruction that you already know is a conditional jump }
  310. procedure MakeUnconditional(p: taicpu); virtual;
  311. { Removes all instructions between an unconditional jump and the next label.
  312. Returns True if a jump in between was removed (as it may open up new
  313. optimisations if the label appeared earlier in the stream) }
  314. function RemoveDeadCodeAfterJump(p: tai): Boolean;
  315. { If hp is a label, strip it if its reference count is zero. Repeat until
  316. a non-label is found, or a label with a non-zero reference count.
  317. True is returned if something was stripped }
  318. function StripDeadLabels(hp: tai; var NextValid: tai): Boolean;
  319. { Strips a label and any aligns that appear before it (if hp points to
  320. them rather than the label). Only call this procedure on a label that
  321. you already know is no longer referenced }
  322. procedure StripLabelFast(hp: tai);
  323. { Checks and removes "jmp @@lbl; @lbl". Returns True if the jump was removed }
  324. function CollapseZeroDistJump(var p: tai; ThisLabel: TAsmLabel): Boolean;
  325. { If a group of labels are clustered, change the jump to point to the last one that is still referenced }
  326. function CollapseLabelCluster(jump: tai; var lbltai: tai): TAsmLabel;
  327. {$ifndef JVM}
  328. function OptimizeConditionalJump(CJLabel: TAsmLabel; var p: tai; hp1: tai; var stoploop: Boolean): Boolean;
  329. {$endif JVM}
  330. { Function to determine if the jump optimisations can be performed }
  331. function CanDoJumpOpts: Boolean; virtual;
  332. { Jump/label optimisation entry method }
  333. function DoJumpOptimizations(var p: tai; var stoploop: Boolean): Boolean;
  334. { insert debug comments about which registers are read and written by
  335. each instruction. Useful for debugging the InstructionLoadsFromReg and
  336. other similar functions. }
  337. procedure Debug_InsertInstrRegisterDependencyInfo; virtual;
  338. protected
  339. { Set to True if this is the second time that Pass 1 is being run }
  340. NotFirstIteration: Boolean;
  341. private
  342. procedure DebugMsg(const s: string; p: tai);
  343. End;
  344. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  345. { Returns a pointer to the operand that contains the destination label }
  346. function JumpTargetOp(ai: taicpu): poper;
  347. { Returns True if hp is any jump to a label }
  348. function IsJumpToLabel(hp: taicpu): boolean;
  349. { Returns True if hp is an unconditional jump to a label }
  350. function IsJumpToLabelUncond(hp: taicpu): boolean;
  351. { ***************************** Implementation **************************** }
  352. Implementation
  353. uses
  354. cutils,
  355. globals,
  356. verbose,
  357. aoptutils,
  358. aasmcfi,
  359. {$if defined(ARM)}
  360. cpuinfo,
  361. {$endif defined(ARM)}
  362. procinfo;
  363. {$ifdef DEBUG_AOPTOBJ}
  364. const
  365. SPeepholeOptimization: shortstring = 'Peephole Optimization: ';
  366. {$else DEBUG_AOPTOBJ}
  367. { Empty strings help the optimizer to remove string concatenations that won't
  368. ever appear to the user on release builds. [Kit] }
  369. const
  370. SPeepholeOptimization = '';
  371. {$endif DEBUG_AOPTOBJ}
  372. function JumpTargetOp(ai: taicpu): poper; inline;
  373. begin
  374. {$if defined(MIPS) or defined(riscv64) or defined(riscv32) or defined(xtensa) or defined(loongarch64)}
  375. { Branches of above archs can have 1,2 or 3 operands, target label is the last one. }
  376. result:=ai.oper[ai.ops-1];
  377. {$elseif defined(SPARC64)}
  378. if ai.ops=2 then
  379. result:=ai.oper[1]
  380. else
  381. result:=ai.oper[0];
  382. {$else MIPS}
  383. result:=ai.oper[0];
  384. {$endif}
  385. end;
  386. { ************************************************************************* }
  387. { ******************************** TUsedRegs ****************************** }
  388. { ************************************************************************* }
  389. Constructor TUsedRegs.create(aTyp : TRegisterType);
  390. Begin
  391. Typ:=aTyp;
  392. UsedRegs := [];
  393. End;
  394. Constructor TUsedRegs.create_regset(aTyp : TRegisterType;Const _RegSet: TRegSet);
  395. Begin
  396. Typ:=aTyp;
  397. UsedRegs := _RegSet;
  398. End;
  399. {
  400. updates UsedRegs with the RegAlloc Information coming after P
  401. }
  402. Procedure TUsedRegs.Update(p: Tai;IgnoreNewAllocs : Boolean = false);
  403. Begin
  404. { this code is normally not used because updating the register allocation information is done in
  405. TAOptObj.UpdateUsedRegs for speed reasons }
  406. repeat
  407. while assigned(p) and
  408. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  409. (p.typ = ait_label) or
  410. ((p.typ = ait_marker) and
  411. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  412. p := tai(p.next);
  413. while assigned(p) and
  414. (p.typ=ait_RegAlloc) Do
  415. begin
  416. if (getregtype(tai_regalloc(p).reg) = typ) then
  417. begin
  418. case tai_regalloc(p).ratype of
  419. ra_alloc :
  420. if not(IgnoreNewAllocs) then
  421. Include(UsedRegs, getsupreg(tai_regalloc(p).reg));
  422. ra_dealloc :
  423. Exclude(UsedRegs, getsupreg(tai_regalloc(p).reg));
  424. else
  425. ;
  426. end;
  427. end;
  428. p := tai(p.next);
  429. end;
  430. until not(assigned(p)) or
  431. (not(p.typ in SkipInstr) and
  432. not((p.typ = ait_label) and
  433. labelCanBeSkipped(tai_label(p))));
  434. End;
  435. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  436. Begin
  437. IsUsed := (getregtype(Reg)=Typ) and (getsupreg(Reg) in UsedRegs);
  438. End;
  439. Function TUsedRegs.GetUsedRegs: TRegSet; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  440. Begin
  441. GetUsedRegs := UsedRegs;
  442. End;
  443. procedure TUsedRegs.Dump(var t: text);
  444. var
  445. i: dword;
  446. begin
  447. write(t,Typ,' ');
  448. for i:=low(TRegSet) to high(TRegSet) do
  449. if i in UsedRegs then
  450. write(t,i,' ');
  451. writeln(t);
  452. end;
  453. Destructor TUsedRegs.Destroy;
  454. Begin
  455. inherited destroy;
  456. end;
  457. procedure TUsedRegs.Clear;
  458. begin
  459. UsedRegs := [];
  460. end;
  461. { ************************************************************************* }
  462. { **************************** TPaiProp *********************************** }
  463. { ************************************************************************* }
  464. Constructor TPaiProp.Create;
  465. Begin
  466. {!!!!!!
  467. UsedRegs.Init;
  468. CondRegs.init;
  469. }
  470. { DirFlag: TFlagContents; I386 specific}
  471. End;
  472. Function TPaiProp.RegInSequence(Reg, which: TRegister): Boolean;
  473. {
  474. Var p: Tai;
  475. RegsChecked: TRegSet;
  476. content: TContent;
  477. Counter: Byte;
  478. TmpResult: Boolean;
  479. }
  480. begin
  481. Result:=False; { unimplemented }
  482. (*!!!!!!!!!!1
  483. RegsChecked := [];
  484. content := regs[which];
  485. p := content.StartMod;
  486. TmpResult := False;
  487. Counter := 1;
  488. While Not(TmpResult) And
  489. (Counter <= Content.NrOfMods) Do
  490. Begin
  491. If IsLoadMemReg(p) Then
  492. With PInstr(p)^.oper[LoadSrc]^.ref^ Do
  493. If (Base = ProcInfo.FramePointer)
  494. {$ifdef cpurefshaveindexreg}
  495. And (Index = R_NO)
  496. {$endif cpurefshaveindexreg} Then
  497. Begin
  498. RegsChecked := RegsChecked +
  499. [RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg)];
  500. If Reg = RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg) Then
  501. Break;
  502. End
  503. Else
  504. Begin
  505. If (Base = Reg) And
  506. Not(Base In RegsChecked)
  507. Then TmpResult := True;
  508. {$ifdef cpurefshaveindexreg}
  509. If Not(TmpResult) And
  510. (Index = Reg) And
  511. Not(Index In RegsChecked)
  512. Then TmpResult := True;
  513. {$Endif cpurefshaveindexreg}
  514. End
  515. Else TmpResult := RegInInstruction(Reg, p);
  516. Inc(Counter);
  517. GetNextInstruction(p,p)
  518. End;
  519. RegInSequence := TmpResult
  520. *)
  521. End;
  522. Procedure TPaiProp.DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  523. TInstrSinceLastMod);
  524. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  525. { the contents of registers are loaded with a memory location based on Reg }
  526. {
  527. Var TmpWState, TmpRState: Byte;
  528. Counter: TRegister;
  529. }
  530. Begin
  531. {!!!!!!!
  532. Reg := RegMaxSize(Reg);
  533. If (Reg in [LoGPReg..HiGPReg]) Then
  534. For Counter := LoGPReg to HiGPReg Do
  535. With Regs[Counter] Do
  536. If (Counter = reg) Or
  537. ((Typ = Con_Ref) And
  538. RegInSequence(Reg, Counter)) Then
  539. Begin
  540. InstrSinceLastMod[Counter] := 0;
  541. IncWState(Counter);
  542. TmpWState := GetWState(Counter);
  543. TmpRState := GetRState(Counter);
  544. FillChar(Regs[Counter], SizeOf(TContent), 0);
  545. WState := TmpWState;
  546. RState := TmpRState
  547. End
  548. }
  549. End;
  550. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  551. Begin
  552. Result:=False; { unimplemented }
  553. (*!!!!!!!!!!
  554. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  555. {$ifdef refsHaveSegmentReg}
  556. (R1.Segment = R2.Segment) And
  557. {$endif}
  558. (R1.Base = R2.Base) And
  559. (R1.Symbol=R2.Symbol);
  560. *)
  561. End;
  562. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  563. var InstrSinceLastMod: TInstrSinceLastMod);
  564. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  565. { is the register whose contents are being written to memory (if this proc }
  566. { is called because of a "mov?? %reg, (mem)" instruction) }
  567. {
  568. Var RefsEq: TRefCompare;
  569. Counter: TRegister;
  570. }
  571. Begin
  572. (*!!!!!!!!!!!
  573. WhichReg := RegMaxSize(WhichReg);
  574. If (Ref.base = procinfo.FramePointer) or
  575. Assigned(Ref.Symbol) Then
  576. Begin
  577. If
  578. {$ifdef cpurefshaveindexreg}
  579. (Ref.Index = R_NO) And
  580. {$endif cpurefshaveindexreg}
  581. (Not(Assigned(Ref.Symbol)) or
  582. (Ref.base = R_NO)) Then
  583. { local variable which is not an array }
  584. RefsEq := @RefsEqual
  585. Else
  586. { local variable which is an array }
  587. RefsEq := @ArrayRefsEq;
  588. {write something to a parameter, a local or global variable, so
  589. * with uncertain optimizations on:
  590. - destroy the contents of registers whose contents have somewhere a
  591. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  592. are being written to memory) is not destroyed if it's StartMod is
  593. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  594. pointer or value based on Ref)
  595. * with uncertain optimizations off:
  596. - also destroy registers that contain any pointer}
  597. For Counter := LoGPReg to HiGPReg Do
  598. With Regs[Counter] Do
  599. Begin
  600. If (typ = Con_Ref) And
  601. ((Not(cs_opt_size in current_settings.optimizerswitches) And
  602. (NrOfMods <> 1)
  603. ) Or
  604. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  605. ((Counter <> WhichReg) Or
  606. ((NrOfMods <> 1) And
  607. {StarMod is always of the type ait_instruction}
  608. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  609. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  610. )
  611. )
  612. )
  613. )
  614. Then
  615. DestroyReg(Counter, InstrSinceLastMod)
  616. End
  617. End
  618. Else
  619. {write something to a pointer location, so
  620. * with uncertain optimzations on:
  621. - do not destroy registers which contain a local/global variable or a
  622. parameter, except if DestroyRefs is called because of a "movsl"
  623. * with uncertain optimzations off:
  624. - destroy every register which contains a memory location
  625. }
  626. For Counter := LoGPReg to HiGPReg Do
  627. With Regs[Counter] Do
  628. If (typ = Con_Ref) And
  629. (Not(cs_opt_size in current_settings.optimizerswitches) Or
  630. {$ifdef x86}
  631. {for movsl}
  632. (Ref.Base = R_EDI) Or
  633. {$endif}
  634. {don't destroy if reg contains a parameter, local or global variable}
  635. Not((NrOfMods = 1) And
  636. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  637. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  638. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  639. )
  640. )
  641. )
  642. Then DestroyReg(Counter, InstrSinceLastMod)
  643. *)
  644. End;
  645. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  646. {Var Counter: TRegister;}
  647. Begin {initializes/desrtoys all registers}
  648. (*!!!!!!!!!
  649. For Counter := LoGPReg To HiGPReg Do
  650. Begin
  651. ReadReg(Counter);
  652. DestroyReg(Counter, InstrSinceLastMod);
  653. End;
  654. CondRegs.Init;
  655. { FPURegs.Init; }
  656. *)
  657. End;
  658. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  659. TInstrSinceLastMod);
  660. Begin
  661. {!!!!!!!
  662. Case o.typ Of
  663. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  664. top_ref:
  665. Begin
  666. ReadRef(o.ref);
  667. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  668. End;
  669. top_symbol:;
  670. End;
  671. }
  672. End;
  673. Procedure TPaiProp.ReadReg(Reg: TRegister);
  674. Begin
  675. {!!!!!!!
  676. Reg := RegMaxSize(Reg);
  677. If Reg in General_Registers Then
  678. IncRState(RegMaxSize(Reg))
  679. }
  680. End;
  681. Procedure TPaiProp.ReadRef(Ref: PReference);
  682. Begin
  683. (*!!!!!!
  684. If Ref^.Base <> R_NO Then
  685. ReadReg(Ref^.Base);
  686. {$ifdef cpurefshaveindexreg}
  687. If Ref^.Index <> R_NO Then
  688. ReadReg(Ref^.Index);
  689. {$endif cpurefshaveindexreg}
  690. *)
  691. End;
  692. Procedure TPaiProp.ReadOp(const o:toper);
  693. Begin
  694. Case o.typ Of
  695. top_reg: ReadReg(o.reg);
  696. top_ref: ReadRef(o.ref);
  697. else
  698. internalerror(200410241);
  699. End;
  700. End;
  701. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  702. TInstrSinceLastMod);
  703. Begin
  704. (*!!!!!!!
  705. With Regs[reg] Do
  706. If (Typ = Con_Ref)
  707. Then
  708. Begin
  709. IncState(WState);
  710. {also store how many instructions are part of the sequence in the first
  711. instructions PPaiProp, so it can be easily accessed from within
  712. CheckSequence}
  713. Inc(NrOfMods, InstrSinceLastMod[Reg]);
  714. PPaiProp(StartMod.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  715. InstrSinceLastMod[Reg] := 0;
  716. End
  717. Else
  718. DestroyReg(Reg, InstrSinceLastMod);
  719. *)
  720. End;
  721. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  722. TInstrSinceLastMod);
  723. Begin
  724. If oper.typ = top_reg Then
  725. ModifyReg(RegMaxSize(oper.reg),InstrSinceLastMod)
  726. Else
  727. Begin
  728. ReadOp(oper);
  729. DestroyOp(oper, InstrSinceLastMod);
  730. End
  731. End;
  732. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  733. Begin
  734. //!!!! IncState(Regs[Reg].WState);
  735. End;
  736. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  737. Begin
  738. //!!!! IncState(Regs[Reg].RState);
  739. End;
  740. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  741. Begin
  742. Result:=0; { unimplemented }
  743. //!!!! GetWState := Regs[Reg].WState
  744. End;
  745. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  746. Begin
  747. Result:=0; { unimplemented }
  748. //!!!! GetRState := Regs[Reg].RState
  749. End;
  750. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  751. Begin
  752. Result:=0; { unimplemented }
  753. //!!!! GetRegContentType := Regs[Reg].typ
  754. End;
  755. Destructor TPaiProp.Done;
  756. Begin
  757. //!!!! UsedRegs.Done;
  758. //!!!! CondRegs.Done;
  759. { DirFlag: TFlagContents; I386 specific}
  760. End;
  761. { ************************ private TPaiProp stuff ************************* }
  762. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  763. Begin
  764. If s <> High(TStateInt) Then Inc(s)
  765. Else s := 0
  766. End;
  767. class Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Tai;
  768. RefsEq: TRefCompare): Boolean;
  769. Var Count: AWord;
  770. TmpResult: Boolean;
  771. Begin
  772. TmpResult := False;
  773. If (p.typ = ait_instruction) Then
  774. Begin
  775. Count := 0;
  776. Repeat
  777. If (TInstr(p).oper[Count]^.typ = Top_Ref) Then
  778. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count]^.ref^);
  779. Inc(Count);
  780. Until (Count = max_operands) or TmpResult;
  781. End;
  782. RefInInstruction := TmpResult;
  783. End;
  784. class function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  785. RefsEq: TRefCompare): Boolean;
  786. Var p: Tai;
  787. Counter: Byte;
  788. TmpResult: Boolean;
  789. Begin
  790. p := Content.StartMod;
  791. TmpResult := False;
  792. Counter := 1;
  793. While Not(TmpResult) And
  794. (Counter <= Content.NrOfMods) Do
  795. Begin
  796. If (p.typ = ait_instruction) And
  797. RefInInstruction(Ref, p, @references_equal)
  798. Then TmpResult := True;
  799. Inc(Counter);
  800. GetNextInstruction(p,p)
  801. End;
  802. RefInSequence := TmpResult
  803. End;
  804. { ************************************************************************* }
  805. { ***************************** TAoptObj ********************************** }
  806. { ************************************************************************* }
  807. Constructor TAoptObj.create(_AsmL: TAsmList; _BlockStart, _BlockEnd: Tai;
  808. _LabelInfo: PLabelInfo);
  809. Begin
  810. AsmL := _AsmL;
  811. BlockStart := _BlockStart;
  812. BlockEnd := _BlockEnd;
  813. LabelInfo := _LabelInfo;
  814. CreateUsedRegs(UsedRegs);
  815. End;
  816. destructor TAOptObj.Destroy;
  817. var
  818. i : TRegisterType;
  819. begin
  820. for i:=low(TRegisterType) to high(TRegisterType) do
  821. UsedRegs[i].Destroy;
  822. inherited Destroy;
  823. end;
  824. {$ifdef DEBUG_AOPTOBJ}
  825. procedure TAOptObj.DebugMsg(const s: string;p : tai);
  826. begin
  827. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  828. end;
  829. {$else DEBUG_AOPTOBJ}
  830. procedure TAOptObj.DebugMsg(const s: string;p : tai);inline;
  831. begin
  832. end;
  833. {$endif DEBUG_AOPTOBJ}
  834. procedure TAOptObj.CreateUsedRegs(var regs: TAllUsedRegs);
  835. var
  836. i : TRegisterType;
  837. begin
  838. for i:=low(TRegisterType) to high(TRegisterType) do
  839. Regs[i]:=TUsedRegs.Create(i);
  840. end;
  841. procedure TAOptObj.ClearUsedRegs;
  842. var
  843. i : TRegisterType;
  844. begin
  845. for i:=low(TRegisterType) to high(TRegisterType) do
  846. UsedRegs[i].Clear;
  847. end;
  848. { If UpdateUsedRegsAndOptimize has read ahead, the result is one before
  849. the next valid entry (so "p.Next" returns what's expected). If no
  850. reading ahead happened, then the result is equal to p. }
  851. function TAOptObj.UpdateUsedRegsAndOptimize(p : Tai): Tai;
  852. var
  853. NotFirst: Boolean;
  854. begin
  855. { this code is based on TUsedRegs.Update to avoid multiple passes through the asmlist,
  856. the code is duplicated here }
  857. Result := p;
  858. if (p.typ in [ait_instruction, ait_label]) then
  859. begin
  860. if (p.next <> BlockEnd) and (tai(p.next).typ <> ait_instruction) then
  861. begin
  862. { Advance one, otherwise the routine exits immediately and wastes time }
  863. p := tai(p.Next);
  864. NotFirst := True;
  865. end
  866. else
  867. { If the next entry is an instruction, nothing will be updated or
  868. optimised here, so exit now to save time }
  869. Exit;
  870. end
  871. else
  872. NotFirst := False;
  873. repeat
  874. while assigned(p) and
  875. ((p.typ in (SkipInstr + [ait_align, ait_label] - [ait_RegAlloc])) or
  876. ((p.typ = ait_marker) and
  877. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  878. begin
  879. prefetch(pointer(p.Next)^);
  880. { Here's the optimise part }
  881. if (p.typ in [ait_align, ait_label]) then
  882. begin
  883. if StripDeadLabels(p, p) then
  884. begin
  885. { Note, if the first instruction is stripped and is
  886. the only one that gets removed, Result will now
  887. contain a dangling pointer, so compensate for this. }
  888. if not NotFirst then
  889. Result := tai(p.Previous);
  890. Continue;
  891. end;
  892. if ((p.typ = ait_label) and not labelCanBeSkipped(tai_label(p))) then
  893. Break;
  894. end;
  895. Result := p;
  896. p := tai(p.next);
  897. end;
  898. while assigned(p) and
  899. (p.typ=ait_RegAlloc) Do
  900. begin
  901. prefetch(pointer(p.Next)^);
  902. case tai_regalloc(p).ratype of
  903. ra_alloc :
  904. Include(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  905. ra_dealloc :
  906. Exclude(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  907. else
  908. { Do nothing };
  909. end;
  910. Result := p;
  911. p := tai(p.next);
  912. end;
  913. NotFirst := True;
  914. until not(assigned(p)) or
  915. (not(p.typ in SkipInstr + [ait_align]) and
  916. not((p.typ = ait_label) and
  917. labelCanBeSkipped(tai_label(p))));
  918. end;
  919. procedure TAOptObj.UpdateUsedRegs(p : Tai);
  920. begin
  921. { this code is based on TUsedRegs.Update to avoid multiple passes through the asmlist,
  922. the code is duplicated here }
  923. repeat
  924. while assigned(p) and
  925. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  926. ((p.typ = ait_label) and
  927. labelCanBeSkipped(tai_label(p))) or
  928. ((p.typ = ait_marker) and
  929. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  930. p := tai(p.next);
  931. while assigned(p) and
  932. (p.typ=ait_RegAlloc) Do
  933. begin
  934. prefetch(pointer(p.Next)^);
  935. case tai_regalloc(p).ratype of
  936. ra_alloc :
  937. Include(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  938. ra_dealloc :
  939. Exclude(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  940. else
  941. ;
  942. end;
  943. p := tai(p.next);
  944. end;
  945. until not(assigned(p)) or
  946. (not(p.typ in SkipInstr) and
  947. not((p.typ = ait_label) and
  948. labelCanBeSkipped(tai_label(p))));
  949. end;
  950. class procedure TAOptObj.UpdateUsedRegs(var Regs : TAllUsedRegs;p : Tai);
  951. var
  952. i : TRegisterType;
  953. begin
  954. for i:=low(TRegisterType) to high(TRegisterType) do
  955. Regs[i].Update(p);
  956. end;
  957. function TAOptObj.CopyUsedRegs(var dest: TAllUsedRegs): boolean;
  958. var
  959. i : TRegisterType;
  960. begin
  961. Result:=true;
  962. for i:=low(TRegisterType) to high(TRegisterType) do
  963. dest[i]:=TUsedRegs.Create_Regset(i,UsedRegs[i].GetUsedRegs);
  964. end;
  965. procedure TAOptObj.RestoreUsedRegs(const Regs: TAllUsedRegs);
  966. var
  967. i : TRegisterType;
  968. begin
  969. { Note that the constructor Create_Regset is being called as a regular
  970. method - it is not instantiating a new object. This is because it is
  971. the only published means to modify the internal state en-masse. [Kit] }
  972. for i:=low(TRegisterType) to high(TRegisterType) do
  973. UsedRegs[i].Create_Regset(i,Regs[i].GetUsedRegs);
  974. end;
  975. procedure TAOptObj.TransferUsedRegs(var dest: TAllUsedRegs);
  976. var
  977. i : TRegisterType;
  978. begin
  979. { Note that the constructor Create_Regset is being called as a regular
  980. method - it is not instantiating a new object. This is because it is
  981. the only published means to modify the internal state en-masse. [Kit] }
  982. for i:=low(TRegisterType) to high(TRegisterType) do
  983. dest[i].Create_Regset(i, UsedRegs[i].GetUsedRegs);
  984. end;
  985. class procedure TAOptObj.ReleaseUsedRegs(const regs: TAllUsedRegs);
  986. var
  987. i : TRegisterType;
  988. begin
  989. for i:=low(TRegisterType) to high(TRegisterType) do
  990. regs[i].Free;
  991. end;
  992. class Function TAOptObj.RegInUsedRegs(reg : TRegister;regs : TAllUsedRegs) : boolean;
  993. begin
  994. result:=regs[getregtype(reg)].IsUsed(reg);
  995. end;
  996. class procedure TAOptObj.IncludeRegInUsedRegs(reg: TRegister;
  997. var regs: TAllUsedRegs);
  998. begin
  999. include(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  1000. end;
  1001. class procedure TAOptObj.ExcludeRegFromUsedRegs(reg: TRegister;
  1002. var regs: TAllUsedRegs);
  1003. begin
  1004. exclude(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  1005. end;
  1006. class function TAOptObj.GetAllocationString(const regs: TAllUsedRegs): string;
  1007. var
  1008. i : TRegisterType;
  1009. j : TSuperRegister;
  1010. begin
  1011. Result:='';
  1012. for i:=low(TRegisterType) to high(TRegisterType) do
  1013. for j in regs[i].UsedRegs do
  1014. Result:=Result+std_regname(newreg(i,j,R_SUBWHOLE))+' ';
  1015. end;
  1016. class function TAOptObj.FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  1017. Var TempP: Tai;
  1018. Begin
  1019. TempP := hp;
  1020. While Assigned(TempP) and
  1021. (TempP.typ In SkipInstr + [ait_label,ait_align]) Do
  1022. If (TempP.typ <> ait_Label) Or
  1023. (Tai_label(TempP).labsym <> L)
  1024. Then GetNextInstruction(TempP, TempP)
  1025. Else
  1026. Begin
  1027. hp := TempP;
  1028. FindLabel := True;
  1029. exit
  1030. End;
  1031. FindLabel := False;
  1032. End;
  1033. Procedure TAOptObj.InsertLLItem(prev, foll, new_one : TLinkedListItem);
  1034. Begin
  1035. If Assigned(prev) Then
  1036. If Assigned(foll) Then
  1037. Begin
  1038. If Assigned(new_one) Then
  1039. Begin
  1040. new_one.previous := prev;
  1041. new_one.next := foll;
  1042. prev.next := new_one;
  1043. foll.previous := new_one;
  1044. { should we update line information? }
  1045. if (not (tai(new_one).typ in SkipLineInfo)) and
  1046. (not (tai(foll).typ in SkipLineInfo)) then
  1047. Tailineinfo(new_one).fileinfo := Tailineinfo(foll).fileinfo
  1048. End
  1049. End
  1050. Else AsmL.Concat(new_one)
  1051. Else If Assigned(Foll) Then AsmL.Insert(new_one)
  1052. End;
  1053. class function TAOptObj.SkipHead(P: Tai): Tai;
  1054. Var OldP: Tai;
  1055. Begin
  1056. Repeat
  1057. OldP := P;
  1058. If (P.typ in SkipInstr) Or
  1059. ((P.typ = ait_marker) And
  1060. (Tai_Marker(P).Kind = mark_AsmBlockEnd)) Then
  1061. GetNextInstruction(P, P)
  1062. Else If ((P.Typ = Ait_Marker) And
  1063. (Tai_Marker(P).Kind = mark_NoPropInfoStart)) Then
  1064. { a marker of the type mark_NoPropInfoStart can't be the first instruction of a }
  1065. { paasmoutput list }
  1066. GetNextInstruction(Tai(P.Previous),P);
  1067. If (P.Typ = Ait_Marker) And
  1068. (Tai_Marker(P).Kind = mark_AsmBlockStart) Then
  1069. Begin
  1070. P := Tai(P.Next);
  1071. While (P.typ <> Ait_Marker) Or
  1072. (Tai_Marker(P).Kind <> mark_AsmBlockEnd) Do
  1073. P := Tai(P.Next)
  1074. End;
  1075. Until P = OldP;
  1076. SkipHead := P;
  1077. End;
  1078. class function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  1079. Begin
  1080. if o1.typ=o2.typ then
  1081. Case o1.typ Of
  1082. Top_Reg :
  1083. OpsEqual:=o1.reg=o2.reg;
  1084. Top_Ref :
  1085. OpsEqual:=
  1086. references_equal(o1.ref^, o2.ref^) and
  1087. (o1.ref^.volatility=[]) and
  1088. (o2.ref^.volatility=[]);
  1089. Top_Const :
  1090. OpsEqual:=o1.val=o2.val;
  1091. Top_None :
  1092. OpsEqual := True
  1093. else OpsEqual := False
  1094. End
  1095. else
  1096. OpsEqual := False;
  1097. End;
  1098. class function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  1099. Begin
  1100. Result:=nil;
  1101. Repeat
  1102. While Assigned(StartPai) And
  1103. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  1104. {$ifdef cpudelayslot}
  1105. ((startpai.typ=ait_instruction) and (taicpu(startpai).opcode=A_NOP)) or
  1106. {$endif cpudelayslot}
  1107. ((StartPai.typ = ait_label) and
  1108. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  1109. StartPai := Tai(StartPai.Next);
  1110. If Assigned(StartPai) And
  1111. (StartPai.typ = ait_regAlloc) Then
  1112. Begin
  1113. if (tai_regalloc(StartPai).ratype=ra_alloc) and
  1114. (getregtype(tai_regalloc(StartPai).Reg) = getregtype(Reg)) and
  1115. (getsupreg(tai_regalloc(StartPai).Reg) = getsupreg(Reg)) then
  1116. begin
  1117. Result:=tai_regalloc(StartPai);
  1118. exit;
  1119. end;
  1120. StartPai := Tai(StartPai.Next);
  1121. End
  1122. else
  1123. exit;
  1124. Until false;
  1125. End;
  1126. class function TAOptObj.FindRegAllocBackward(Reg: TRegister; StartPai: Tai): tai_regalloc;
  1127. Begin
  1128. Result:=nil;
  1129. Repeat
  1130. While Assigned(StartPai) And
  1131. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  1132. ((StartPai.typ = ait_label) and
  1133. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  1134. StartPai := Tai(StartPai.Previous);
  1135. If Assigned(StartPai) And
  1136. (StartPai.typ = ait_regAlloc) Then
  1137. Begin
  1138. if (tai_regalloc(StartPai).ratype=ra_alloc) and
  1139. SuperRegistersEqual(tai_regalloc(StartPai).Reg,Reg) then
  1140. begin
  1141. Result:=tai_regalloc(StartPai);
  1142. exit;
  1143. end;
  1144. StartPai := Tai(StartPai.Previous);
  1145. End
  1146. else
  1147. exit;
  1148. Until false;
  1149. End;
  1150. class function TAOptObj.FindRegDeAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  1151. Begin
  1152. Result:=nil;
  1153. Repeat
  1154. While Assigned(StartPai) And
  1155. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  1156. ((StartPai.typ = ait_label) and
  1157. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  1158. StartPai := Tai(StartPai.Next);
  1159. If Assigned(StartPai) And
  1160. (StartPai.typ = ait_regAlloc) Then
  1161. Begin
  1162. if (tai_regalloc(StartPai).ratype=ra_dealloc) and
  1163. (getregtype(tai_regalloc(StartPai).Reg) = getregtype(Reg)) and
  1164. (getsupreg(tai_regalloc(StartPai).Reg) = getsupreg(Reg)) then
  1165. begin
  1166. Result:=tai_regalloc(StartPai);
  1167. exit;
  1168. end;
  1169. StartPai := Tai(StartPai.Next);
  1170. End
  1171. else
  1172. exit;
  1173. Until false;
  1174. End;
  1175. class function TAOptObj.FindTempDeAlloc(Offset: ASizeInt; StartPai: Tai): tai_tempalloc;
  1176. Begin
  1177. Result:=nil;
  1178. Repeat
  1179. While Assigned(StartPai) And
  1180. ((StartPai.typ in (SkipInstr - [ait_tempalloc])) Or
  1181. ((StartPai.typ = ait_label) and
  1182. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  1183. StartPai := Tai(StartPai.Next);
  1184. If Assigned(StartPai) And
  1185. (StartPai.typ = ait_tempalloc) Then
  1186. Begin
  1187. if not(tai_tempalloc(StartPai).allocation) and
  1188. (tai_tempalloc(StartPai).temppos = Offset) then
  1189. begin
  1190. Result:=tai_tempalloc(StartPai);
  1191. exit;
  1192. end;
  1193. StartPai := Tai(StartPai.Next);
  1194. End
  1195. else
  1196. exit;
  1197. Until false;
  1198. End;
  1199. { allocates register reg between (and including) instructions p1 and p2
  1200. the type of p1 and p2 must not be in SkipInstr }
  1201. procedure TAOptObj.AllocRegBetween(reg: tregister; p1, p2: tai; var initialusedregs: TAllUsedRegs);
  1202. var
  1203. hp, start: tai;
  1204. Po: PInteger;
  1205. removedsomething,
  1206. firstRemovedWasAlloc,
  1207. lastRemovedWasDealloc: boolean;
  1208. begin
  1209. {$ifdef EXTDEBUG}
  1210. { if assigned(p1.optinfo) and
  1211. (ptaiprop(p1.optinfo)^.usedregs <> initialusedregs) then
  1212. internalerror(2004101010); }
  1213. {$endif EXTDEBUG}
  1214. if not Assigned(p2) then
  1215. { We need a valid final instruction }
  1216. InternalError(2022010401);
  1217. start := p1;
  1218. if (reg = NR_STACK_POINTER_REG) or
  1219. (reg = current_procinfo.framepointer) or
  1220. not(assigned(p1)) then
  1221. { this happens with registers which are loaded implicitely, outside the }
  1222. { current block (e.g. esi with self) }
  1223. exit;
  1224. {$ifdef allocregdebug}
  1225. insertllitem(p1.previous,p1,tai_comment.Create(strpnew('allocating '+std_regname(reg)+' from here...')));
  1226. insertllitem(p2,p2.next,tai_comment.Create(strpnew('allocated '+std_regname(reg)+' till here...')));
  1227. {$endif allocregdebug}
  1228. { make sure we allocate it for this instruction }
  1229. getnextinstruction(p2,p2);
  1230. lastRemovedWasDealloc := false;
  1231. removedSomething := false;
  1232. firstRemovedWasAlloc := false;
  1233. { do it the safe way: always allocate the full super register,
  1234. as we do no register re-allocation in the peephole optimizer,
  1235. this does not hurt
  1236. }
  1237. case getregtype(reg) of
  1238. R_MMREGISTER:
  1239. reg:=newreg(R_MMREGISTER,getsupreg(reg),R_SUBMMWHOLE);
  1240. R_INTREGISTER:
  1241. reg:=newreg(R_INTREGISTER,getsupreg(reg),R_SUBWHOLE);
  1242. R_FPUREGISTER:
  1243. reg:=newreg(R_FPUREGISTER,getsupreg(reg),R_SUBWHOLE);
  1244. R_ADDRESSREGISTER:
  1245. reg:=newreg(R_ADDRESSREGISTER,getsupreg(reg),R_SUBWHOLE);
  1246. R_SPECIALREGISTER:
  1247. reg:=newreg(R_SPECIALREGISTER,getsupreg(reg),R_SUBWHOLE);
  1248. else
  1249. Internalerror(2018030701);
  1250. end;
  1251. if not(RegInUsedRegs(reg,initialusedregs)) then
  1252. begin
  1253. hp := tai_regalloc.alloc(reg,nil);
  1254. insertllItem(p1.previous,p1,hp);
  1255. IncludeRegInUsedRegs(reg,initialusedregs);
  1256. lastRemovedWasDealloc := True; { If no tai_regallocs are found at all, treat as if the last one was a deallocation }
  1257. end;
  1258. while assigned(p1) and
  1259. (p1 <> p2) do
  1260. begin
  1261. if assigned(p1.optinfo) then
  1262. internalerror(2014022301); // IncludeRegInUsedRegs(reg,ptaiprop(p1.optinfo)^.usedregs);
  1263. p1 := tai(p1.next);
  1264. repeat
  1265. while assigned(p1) and
  1266. (p1.typ in (SkipInstr-[ait_regalloc])) Do
  1267. p1 := tai(p1.next);
  1268. { remove all allocation/deallocation info about the register in between }
  1269. if assigned(p1) and
  1270. (p1.typ = ait_regalloc) then
  1271. begin
  1272. { same super register, different sub register? }
  1273. if SuperRegistersEqual(reg,tai_regalloc(p1).reg) and (tai_regalloc(p1).reg<>reg) then
  1274. begin
  1275. if (getsubreg(reg)<>R_SUBMMWHOLE) and { R_SUBMMWHOLE is below R_SUBMMX, R_SUBMMY and R_SUBMMZ }
  1276. ((getsubreg(tai_regalloc(p1).reg)>getsubreg(reg)) or (getsubreg(reg)=R_SUBH)) then
  1277. internalerror(2016101501);
  1278. tai_regalloc(p1).reg:=reg;
  1279. end;
  1280. if tai_regalloc(p1).reg=reg then
  1281. begin
  1282. if not removedSomething then
  1283. begin
  1284. firstRemovedWasAlloc := tai_regalloc(p1).ratype=ra_alloc;
  1285. removedSomething := true;
  1286. end;
  1287. lastRemovedWasDealloc := (tai_regalloc(p1).ratype=ra_dealloc);
  1288. hp := tai(p1.Next);
  1289. asml.Remove(p1);
  1290. p1.free;
  1291. p1 := hp;
  1292. end
  1293. else
  1294. p1 := tai(p1.next);
  1295. end;
  1296. until not(assigned(p1)) or
  1297. not(p1.typ in SkipInstr);
  1298. end;
  1299. if assigned(p1) then
  1300. begin
  1301. if firstRemovedWasAlloc then
  1302. begin
  1303. hp := tai_regalloc.Alloc(reg,nil);
  1304. insertLLItem(start.previous,start,hp);
  1305. end;
  1306. if lastRemovedWasDealloc then
  1307. begin
  1308. hp := tai_regalloc.DeAlloc(reg,nil);
  1309. insertLLItem(p1.previous,p1,hp);
  1310. end;
  1311. end;
  1312. end;
  1313. function TAOptObj.RegUsedAfterInstruction(reg: Tregister; p: tai;var AllUsedRegs: TAllUsedRegs): Boolean;
  1314. begin
  1315. AllUsedRegs[getregtype(reg)].Update(tai(p.Next),true);
  1316. RegUsedAfterInstruction :=
  1317. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  1318. not(regLoadedWithNewValue(reg,p)) and
  1319. (
  1320. not(GetNextInstruction(p,p)) or
  1321. InstructionLoadsFromReg(reg,p) or
  1322. not(regLoadedWithNewValue(reg,p))
  1323. );
  1324. end;
  1325. function TAOptObj.RegEndOfLife(reg : TRegister;p : taicpu) : boolean;
  1326. begin
  1327. Result:=assigned(FindRegDealloc(reg,tai(p.Next))) or
  1328. RegLoadedWithNewValue(reg,p);
  1329. end;
  1330. function TAOptObj.RemoveCurrentP(var p : tai) : boolean;
  1331. var
  1332. hp1 : tai;
  1333. begin
  1334. result:=GetNextInstruction(p,hp1);
  1335. { p will be removed, update used register as we continue
  1336. with the next instruction after p }
  1337. UpdateUsedRegs(tai(p.Next));
  1338. AsmL.Remove(p);
  1339. p.Free;
  1340. p:=hp1;
  1341. end;
  1342. procedure TAOptObj.RemoveCurrentP(var p: tai; const hp1: tai); inline;
  1343. begin
  1344. if (p=hp1) then
  1345. internalerror(2020120501);
  1346. UpdateUsedRegs(tai(p.Next));
  1347. AsmL.Remove(p);
  1348. p.Free;
  1349. p := hp1;
  1350. end;
  1351. procedure TAOptObj.RemoveInstruction(const hp: tai); inline;
  1352. begin
  1353. AsmL.Remove(hp);
  1354. hp.Free;
  1355. end;
  1356. function FindLiveLabel(hp: tai; var l: tasmlabel): Boolean;
  1357. var
  1358. next: tai;
  1359. begin
  1360. FindLiveLabel := false;
  1361. while True do
  1362. begin
  1363. while assigned(hp.next) and
  1364. (tai(hp.next).typ in (SkipInstr+[ait_align])) Do
  1365. hp := tai(hp.next);
  1366. next := tai(hp.next);
  1367. if assigned(next) and
  1368. (tai(next).typ = ait_label) then
  1369. begin
  1370. l := tai_label(next).labsym;
  1371. if not l.is_used then
  1372. begin
  1373. { Unsafe label }
  1374. hp := next;
  1375. Continue;
  1376. end;
  1377. FindLiveLabel := true;
  1378. end;
  1379. Exit;
  1380. end;
  1381. end;
  1382. {$push}
  1383. {$r-}
  1384. function TAOptObj.getlabelwithsym(sym: tasmlabel): tai;
  1385. begin
  1386. if (int64(sym.labelnr) >= int64(labelinfo^.lowlabel)) and
  1387. (int64(sym.labelnr) <= int64(labelinfo^.highlabel)) then { range check, a jump can go past an assembler block! }
  1388. getlabelwithsym := labelinfo^.labeltable^[sym.labelnr-labelinfo^.lowlabel].paiobj
  1389. else
  1390. getlabelwithsym := nil;
  1391. end;
  1392. {$pop}
  1393. { Returns True if hp is an unconditional jump to a label }
  1394. function IsJumpToLabelUncond(hp: taicpu): boolean;
  1395. begin
  1396. {$if defined(avr) or defined(z80)}
  1397. result:=(hp.opcode in aopt_uncondjmp) and
  1398. {$else}
  1399. result:=(hp.opcode=aopt_uncondjmp) and
  1400. {$endif}
  1401. {$if defined(arm) or defined(aarch64) or defined(z80)}
  1402. (hp.condition=c_None) and
  1403. {$endif arm or aarch64 or z80}
  1404. (hp.ops>0) and
  1405. {$if defined(riscv32) or defined(riscv64)}
  1406. (hp.oper[0]^.reg=NR_X0) and
  1407. {$endif riscv}
  1408. (JumpTargetOp(hp)^.typ = top_ref) and
  1409. (JumpTargetOp(hp)^.ref^.symbol is TAsmLabel);
  1410. end;
  1411. { Returns True if hp is any jump to a label }
  1412. function IsJumpToLabel(hp: taicpu): boolean;
  1413. begin
  1414. result:=hp.is_jmp and
  1415. (hp.ops>0) and
  1416. (JumpTargetOp(hp)^.typ = top_ref) and
  1417. (JumpTargetOp(hp)^.ref^.symbol is TAsmLabel);
  1418. end;
  1419. procedure TAOptObj.RemoveDelaySlot(hp1:tai);
  1420. var
  1421. hp2: tai;
  1422. begin
  1423. hp2:=tai(hp1.next);
  1424. while assigned(hp2) and (hp2.typ in SkipInstr) do
  1425. hp2:=tai(hp2.next);
  1426. if assigned(hp2) and (hp2.typ=ait_instruction) and
  1427. (taicpu(hp2).opcode=A_NOP) then
  1428. begin
  1429. asml.remove(hp2);
  1430. hp2.free;
  1431. end;
  1432. { Anything except A_NOP must be left in place: these instructions
  1433. execute before branch, so code stays correct if branch is removed. }
  1434. end;
  1435. { Output debug message to console - null function if EXTDEBUG is not defined }
  1436. class procedure TAOptObj.DebugWrite(Message: string); inline;
  1437. begin
  1438. {$ifdef DEBUG_JUMP}
  1439. WriteLn(Message);
  1440. {$else DEBUG_JUMP}
  1441. { Do nothing }
  1442. {$endif DEBUG_JUMP}
  1443. end;
  1444. { Converts a conditional jump into an unconditional jump. Only call this
  1445. procedure on an instruction that you already know is a conditional jump }
  1446. procedure TAOptObj.MakeUnconditional(p: taicpu);
  1447. begin
  1448. { TODO: If anyone can improve this particular optimisation to work on
  1449. AVR, please do (it's currently not called at all). [Kit] }
  1450. {$if not defined(avr)}
  1451. {$if defined(powerpc) or defined(powerpc64)}
  1452. p.condition.cond := C_None;
  1453. p.condition.simple := True;
  1454. {$else powerpc}
  1455. p.condition := C_None;
  1456. {$endif powerpc}
  1457. {$ifndef z80}
  1458. p.opcode := aopt_uncondjmp;
  1459. {$endif not z80}
  1460. {$ifdef RISCV}
  1461. p.loadoper(1, p.oper[p.ops-1]^);
  1462. p.loadreg(0, NR_X0);
  1463. p.ops:=2;
  1464. {$endif}
  1465. {$ifdef xtensa}
  1466. p.opcode := aopt_uncondjmp;
  1467. p.loadoper(0, p.oper[p.ops-1]^);
  1468. p.ops:=1;
  1469. {$endif}
  1470. {$endif not avr}
  1471. {$ifdef mips}
  1472. { MIPS conditional jump instructions also conntain register
  1473. operands. A proper implementation is needed here. }
  1474. internalerror(2020071301);
  1475. {$endif}
  1476. end;
  1477. { Removes all instructions between an unconditional jump and the next label.
  1478. Returns True if a jump in between was removed (as it may open up new
  1479. optimisations if the label appeared earlier in the stream) }
  1480. function TAOptObj.RemoveDeadCodeAfterJump(p: tai): Boolean;
  1481. const
  1482. {$ifdef JVM}
  1483. TaiFence = SkipInstr + [ait_const, ait_realconst, ait_typedconst, ait_label, ait_jcatch];
  1484. {$else JVM}
  1485. { Stop if it reaches SEH directive information in the form of
  1486. consts, which may occur if RemoveDeadCodeAfterJump is called on
  1487. the final RET instruction on x86, for example }
  1488. TaiFence = SkipInstr + [ait_const, ait_realconst, ait_typedconst, ait_label];
  1489. {$endif JVM}
  1490. var
  1491. hp1, hp2: tai;
  1492. begin
  1493. { the following code removes all code between a jmp and the next label,
  1494. because it can never be executed
  1495. }
  1496. Result := False;
  1497. while GetNextInstruction(p, hp1) and
  1498. (hp1 <> BlockEnd) and
  1499. not (hp1.typ in TaiFence) do
  1500. begin
  1501. if (hp1.typ = ait_instruction) and
  1502. taicpu(hp1).is_jmp and
  1503. (JumpTargetOp(taicpu(hp1))^.typ = top_ref) and
  1504. (JumpTargetOp(taicpu(hp1))^.ref^.symbol is TAsmLabel) then
  1505. begin
  1506. { If the destination label appears earlier, it may permit
  1507. further optimisations, so signal this in the Result }
  1508. Result := True;
  1509. TAsmLabel(JumpTargetOp(taicpu(hp1))^.ref^.symbol).decrefs;
  1510. end;
  1511. { don't kill start/end of assembler block,
  1512. no-line-info-start/end etc }
  1513. if (hp1.typ<>ait_marker) and
  1514. ((hp1.typ<>ait_cfi) or
  1515. (tai_cfi_base(hp1).cfityp<>cfi_endproc)) then
  1516. begin
  1517. {$ifdef cpudelayslot}
  1518. if (hp1.typ=ait_instruction) and (taicpu(hp1).is_jmp) then
  1519. RemoveDelaySlot(hp1);
  1520. {$endif cpudelayslot}
  1521. hp2 := hp1;
  1522. while (hp2.typ = ait_align) do
  1523. begin
  1524. { Only remove the align if a label doesn't immediately follow }
  1525. if GetNextInstruction(hp2, hp2) and (hp2.typ = ait_label) then
  1526. { The label is unskippable }
  1527. Exit;
  1528. { Check again in case there's more than one adjacent alignment entry
  1529. (a frequent construct under x86, for example). [Kit] }
  1530. end;
  1531. asml.remove(hp1);
  1532. hp1.free;
  1533. end
  1534. else
  1535. p:=hp1;
  1536. end;
  1537. end;
  1538. { If hp is a label, strip it if its reference count is zero. Repeat until
  1539. a non-label is found, or a label with a non-zero reference count.
  1540. True is returned if something was stripped }
  1541. function TAOptObj.StripDeadLabels(hp: tai; var NextValid: tai): Boolean;
  1542. var
  1543. tmp, tmpNext: tai;
  1544. hp1: tai;
  1545. CurrentAlign: tai;
  1546. begin
  1547. CurrentAlign := nil;
  1548. Result := False;
  1549. hp1 := hp;
  1550. NextValid := hp;
  1551. { Stop if hp is an instruction, for example }
  1552. while (hp1 <> BlockEnd) and (hp1.typ in [ait_label,ait_align]) do
  1553. begin
  1554. prefetch(pointer(hp1.Next)^);
  1555. case hp1.typ of
  1556. ait_label:
  1557. begin
  1558. with tai_label(hp1).labsym do
  1559. if is_used or (bind <> AB_LOCAL) or (labeltype <> alt_jump) then
  1560. begin
  1561. { Valid label }
  1562. if Result then
  1563. NextValid := hp1;
  1564. DebugWrite('JUMP DEBUG: Last label in cluster:' + tostr(labelnr));
  1565. Exit;
  1566. end;
  1567. DebugWrite('JUMP DEBUG: Removed label ' + tostr(TAsmLabel(tai_label(hp1).labsym).labelnr));
  1568. { Set tmp to the next valid entry }
  1569. tmp := tai(hp1.Next);
  1570. { Remove label }
  1571. AsmL.Remove(hp1);
  1572. hp1.Free;
  1573. hp1 := tmp;
  1574. Result := True;
  1575. Continue;
  1576. end;
  1577. { Also remove the align if it comes before an unused label }
  1578. ait_align:
  1579. begin
  1580. tmp := tai(hp1.Next);
  1581. if tmp = BlockEnd then
  1582. { End of block }
  1583. Exit;
  1584. repeat
  1585. case tmp.typ of
  1586. ait_align: { Merge the aligns if permissible }
  1587. begin
  1588. { Check the maxbytes field though, since this may result in the
  1589. alignment being ignored }
  1590. if ((tai_align_abstract(hp1).maxbytes = 0) and (tai_align_abstract(tmp).maxbytes = 0)) or
  1591. { If a maxbytes field is present, only merge if the aligns have the same granularity }
  1592. ((tai_align_abstract(hp1).aligntype = tai_align_abstract(tmp).aligntype)) then
  1593. begin
  1594. with tai_align_abstract(hp1) do
  1595. begin
  1596. aligntype := max(aligntype, tai_align_abstract(tmp).aligntype);
  1597. maxbytes := max(maxbytes, tai_align_abstract(tmp).maxbytes);
  1598. fillsize := max(fillsize, tai_align_abstract(tmp).fillsize);
  1599. use_op := use_op or tai_align_abstract(tmp).use_op;
  1600. if use_op and (tai_align_abstract(tmp).fillop <> 0) then
  1601. fillop := tai_align_abstract(tmp).fillop;
  1602. end;
  1603. tmpNext := tai(tmp.Next);
  1604. AsmL.Remove(tmp);
  1605. tmp.Free;
  1606. Result := True;
  1607. tmp := tmpNext;
  1608. end
  1609. else
  1610. tmp := tai(tmp.Next);
  1611. Continue;
  1612. end;
  1613. ait_label:
  1614. begin
  1615. { Signal that we can possibly delete this align entry }
  1616. CurrentAlign := hp1;
  1617. repeat
  1618. with tai_label(tmp).labsym do
  1619. if is_used or (bind <> AB_LOCAL) or (labeltype <> alt_jump) then
  1620. begin
  1621. { Valid label }
  1622. if Result then
  1623. NextValid := tmp;
  1624. DebugWrite('JUMP DEBUG: Last label in cluster:' + tostr(labelnr));
  1625. Exit;
  1626. end;
  1627. DebugWrite('JUMP DEBUG: Removed label ' + tostr(TAsmLabel(tai_label(tmp).labsym).labelnr));
  1628. { Remove label }
  1629. tmpNext := tai(tmp.Next);
  1630. AsmL.Remove(tmp);
  1631. tmp.Free;
  1632. Result := True;
  1633. tmp := tmpNext;
  1634. { Loop here for a minor performance gain }
  1635. until (tmp = BlockEnd) or (tmp.typ <> ait_label);
  1636. { Re-evaluate the align and see what follows }
  1637. Continue;
  1638. end
  1639. else
  1640. begin
  1641. { Set hp1 to the instruction after the align, because the
  1642. align might get deleted later and hence set NextValid
  1643. to a dangling pointer. [Kit] }
  1644. hp1 := tmp;
  1645. Break;
  1646. end;
  1647. end;
  1648. until (tmp = BlockEnd);
  1649. { Break out of the outer loop if the above Break is called }
  1650. if (hp1 = tmp) then
  1651. Break;
  1652. end
  1653. else
  1654. Break;
  1655. end;
  1656. hp1 := tai(hp1.Next);
  1657. end;
  1658. { hp1 will be the next valid entry }
  1659. NextValid := hp1;
  1660. { Remove the alignment field (but only if the next valid entry is not a live label) }
  1661. while Assigned(CurrentAlign) and (CurrentAlign.typ = ait_align) do
  1662. begin
  1663. DebugWrite('JUMP DEBUG: Alignment field removed');
  1664. tmp := tai(CurrentAlign.next);
  1665. AsmL.Remove(CurrentAlign);
  1666. CurrentAlign.Free;
  1667. CurrentAlign := tmp;
  1668. end;
  1669. end;
  1670. { Strips a label and any aligns that appear before it (if hp points to
  1671. them rather than the label). Only call this procedure on a label that
  1672. you already know is no longer referenced }
  1673. procedure TAOptObj.StripLabelFast(hp: tai);
  1674. var
  1675. tmp: tai;
  1676. begin
  1677. repeat
  1678. case hp.typ of
  1679. ait_align:
  1680. begin
  1681. tmp := tai(hp.Next);
  1682. asml.Remove(hp);
  1683. hp.Free;
  1684. hp := tmp;
  1685. { Control flow will now return to 'repeat' }
  1686. end;
  1687. ait_label:
  1688. begin
  1689. {$ifdef EXTDEBUG}
  1690. { When not in debug mode, deleting a live label will cause an
  1691. access violation later on. [Kit] }
  1692. if tai_label(hp).labsym.getrefs <> 0 then
  1693. InternalError(2019110802);
  1694. {$endif EXTDEBUG}
  1695. asml.Remove(hp);
  1696. hp.Free;
  1697. Exit;
  1698. end;
  1699. else
  1700. begin
  1701. { Might be a comment or temporary allocation entry }
  1702. if not (hp.typ in SkipInstr) then
  1703. InternalError(2019110801);
  1704. hp := tai(hp.Next);
  1705. end;
  1706. end;
  1707. until False;
  1708. end;
  1709. { If a group of labels are clustered, change the jump to point to the last one
  1710. that is still referenced }
  1711. function TAOptObj.CollapseLabelCluster(jump: tai; var lbltai: tai): TAsmLabel;
  1712. var
  1713. LastLabel: TAsmLabel;
  1714. hp2: tai;
  1715. begin
  1716. Result := tai_label(lbltai).labsym;
  1717. LastLabel := Result;
  1718. hp2 := tai(lbltai.next);
  1719. while (hp2 <> BlockEnd) and (hp2.typ in SkipInstr + [ait_align, ait_label]) do
  1720. begin
  1721. if (hp2.typ = ait_label) and
  1722. (tai_label(hp2).labsym.is_used) and
  1723. (tai_label(hp2).labsym.labeltype = alt_jump) then
  1724. LastLabel := tai_label(hp2).labsym;
  1725. hp2 := tai(hp2.next);
  1726. end;
  1727. if (Result <> LastLabel) then
  1728. begin
  1729. Result.decrefs;
  1730. JumpTargetOp(taicpu(jump))^.ref^.symbol := LastLabel;
  1731. LastLabel.increfs;
  1732. Result := LastLabel;
  1733. lbltai := hp2;
  1734. end;
  1735. end;
  1736. {$ifndef JVM}
  1737. function TAOptObj.OptimizeConditionalJump(CJLabel: TAsmLabel; var p: tai; hp1: tai; var stoploop: Boolean): Boolean;
  1738. var
  1739. hp2: tai;
  1740. NCJLabel: TAsmLabel;
  1741. begin
  1742. Result := False;
  1743. while (hp1 <> BlockEnd) do
  1744. begin
  1745. StripDeadLabels(hp1, hp1);
  1746. if (hp1 <> BlockEnd) and
  1747. (tai(hp1).typ=ait_instruction) and
  1748. IsJumpToLabel(taicpu(hp1)) then
  1749. begin
  1750. NCJLabel := TAsmLabel(JumpTargetOp(taicpu(hp1))^.ref^.symbol);
  1751. if IsJumpToLabelUncond(taicpu(hp1)) then
  1752. begin
  1753. { Do it now to get it out of the way and to aid optimisations
  1754. later on in this method }
  1755. if RemoveDeadCodeAfterJump(taicpu(hp1)) then
  1756. stoploop := False;
  1757. hp2 := getlabelwithsym(NCJLabel);
  1758. if Assigned(hp2) then
  1759. { Collapse the cluster now to aid optimisation and potentially
  1760. cut down on the number of iterations required }
  1761. NCJLabel := CollapseLabelCluster(hp1, hp2);
  1762. { GetNextInstruction could be factored out, but hp2 might be
  1763. different after "RemoveDeadCodeAfterJump" }
  1764. GetNextInstruction(hp1, hp2);
  1765. { Check for:
  1766. jmp<cond> @Lbl
  1767. jmp @Lbl
  1768. }
  1769. if (CJLabel = NCJLabel) then
  1770. begin
  1771. DebugMsg(SPeepholeOptimization+'Short-circuited conditional jump',p);
  1772. { Both jumps go to the same label }
  1773. CJLabel.decrefs;
  1774. {$ifdef cpudelayslot}
  1775. RemoveDelaySlot(p);
  1776. {$endif cpudelayslot}
  1777. RemoveCurrentP(p, hp1);
  1778. Result := True;
  1779. Exit;
  1780. end;
  1781. if FindLabel(CJLabel, hp2) then
  1782. begin
  1783. { change the following jumps:
  1784. jmp<cond> CJLabel jmp<inv_cond> NCJLabel
  1785. jmp NCJLabel >>> <code>
  1786. CJLabel: NCJLabel:
  1787. <code>
  1788. NCJLabel:
  1789. }
  1790. {$if defined(arm) or defined(aarch64)}
  1791. if (taicpu(p).condition<>C_None)
  1792. {$if defined(aarch64)}
  1793. { can't have conditional branches to
  1794. global labels on AArch64, because the
  1795. offset may become too big }
  1796. and (NCJLabel.bind=AB_LOCAL)
  1797. {$endif aarch64}
  1798. then
  1799. begin
  1800. {$endif arm or aarch64}
  1801. DebugMsg(SPeepholeOptimization+'Conditional jump inversion',p);
  1802. taicpu(p).condition:=inverse_cond(taicpu(p).condition);
  1803. CJLabel.decrefs;
  1804. JumpTargetOp(taicpu(p))^.ref^.symbol := NCJLabel;
  1805. { when freeing hp1, the reference count
  1806. isn't decreased, so don't increase }
  1807. {$ifdef cpudelayslot}
  1808. RemoveDelaySlot(hp1);
  1809. {$endif cpudelayslot}
  1810. RemoveInstruction(hp1);
  1811. stoploop := False;
  1812. if not CJLabel.is_used then
  1813. begin
  1814. CJLabel := NCJLabel;
  1815. StripDeadLabels(tai(p.Next), hp1);
  1816. if (hp1 = BlockEnd) then
  1817. Exit;
  1818. { Attempt another iteration in case more jumps follow }
  1819. if (hp1.typ in SkipInstr) then
  1820. GetNextInstruction(hp1, hp1);
  1821. Continue;
  1822. end;
  1823. {$if defined(arm) or defined(aarch64)}
  1824. end;
  1825. {$endif arm or aarch64}
  1826. end
  1827. else if CollapseZeroDistJump(hp1, NCJLabel) then
  1828. begin
  1829. if (hp1 = BlockEnd) then
  1830. Exit;
  1831. { Attempt another iteration in case more jumps follow }
  1832. if (hp1.typ in SkipInstr) then
  1833. GetNextInstruction(hp1, hp1);
  1834. Continue;
  1835. end;
  1836. end
  1837. else
  1838. begin
  1839. { Do not try to optimize if the test generating the condition
  1840. is the same instruction, like 'bne $v0,$zero,.Lj3' for MIPS }
  1841. if (taicpu(p).ops>1) or (taicpu(hp1).ops>1) then
  1842. exit;
  1843. { Check for:
  1844. jmp<cond1> @Lbl1
  1845. jmp<cond2> @Lbl2
  1846. Remove 2nd jump if conditions are equal or cond2 is a subset of cond1
  1847. (as if the first jump didn't branch, then neither will the 2nd)
  1848. }
  1849. if condition_in(taicpu(hp1).condition, taicpu(p).condition) then
  1850. begin
  1851. DebugMsg(SPeepholeOptimization+'Dominated conditional jump',p);
  1852. NCJLabel.decrefs;
  1853. GetNextInstruction(hp1, hp2);
  1854. {$ifdef cpudelayslot}
  1855. RemoveDelaySlot(hp1);
  1856. {$endif cpudelayslot}
  1857. RemoveInstruction(hp1);
  1858. hp1 := hp2;
  1859. { Flag another pass in case @Lbl2 appeared earlier in the procedure and is now a dead label }
  1860. stoploop := False;
  1861. { Attempt another iteration in case more jumps follow }
  1862. Continue;
  1863. end;
  1864. { Check for:
  1865. jmp<cond1> @Lbl1
  1866. jmp<cond2> @Lbl2
  1867. And inv(cond1) is a subset of cond2 (e.g. je followed by jne, or jae followed by jbe) )
  1868. }
  1869. if condition_in(inverse_cond(taicpu(p).condition), taicpu(hp1).condition) then
  1870. begin
  1871. GetNextInstruction(hp1, hp2);
  1872. { If @lbl1 immediately follows jmp<cond2>, we can remove
  1873. the first jump completely }
  1874. if FindLabel(CJLabel, hp2) then
  1875. begin
  1876. { However, to be absolutely correct, cond2 must be changed to inv(cond1) }
  1877. taicpu(hp1).condition := inverse_cond(taicpu(p).condition);
  1878. DebugMsg(SPeepholeOptimization+'jmp<cond> before jmp<inv_cond> - removed first jump',p);
  1879. CJLabel.decrefs;
  1880. {$ifdef cpudelayslot}
  1881. RemoveDelaySlot(p);
  1882. {$endif cpudelayslot}
  1883. RemoveCurrentP(p, hp1);
  1884. Result := True;
  1885. Exit;
  1886. {$if not defined(avr)}
  1887. end
  1888. else
  1889. { NOTE: There is currently no watertight, cross-platform way to create
  1890. an unconditional jump without access to the cg object. If anyone can
  1891. improve this particular optimisation to work on AVR,
  1892. please do. [Kit] }
  1893. begin
  1894. { Since inv(cond1) is a subset of cond2, jmp<cond2> will always branch if
  1895. jmp<cond1> does not, so change jmp<cond2> to an unconditional jump. }
  1896. DebugMsg(SPeepholeOptimization+'jmp<cond> before jmp<inv_cond> - made second jump unconditional',p);
  1897. MakeUnconditional(taicpu(hp1));
  1898. { NOTE: Changing the jump to unconditional won't open up new opportunities
  1899. for GetFinalDestination on earlier jumps because there's no live label
  1900. between the two jump instructions, so setting 'stoploop' to False only
  1901. wastes time. [Kit] }
  1902. { See if more optimisations are possible }
  1903. Continue;
  1904. {$endif}
  1905. end;
  1906. end;
  1907. end;
  1908. end;
  1909. if GetFinalDestination(taicpu(p),0) then
  1910. stoploop := False;
  1911. Exit;
  1912. end;
  1913. end;
  1914. {$endif JVM}
  1915. function TAOptObj.CollapseZeroDistJump(var p: tai; ThisLabel: TAsmLabel): Boolean;
  1916. var
  1917. hp1: tai;
  1918. begin
  1919. Result := False;
  1920. if not GetNextInstruction(p,hp1) then
  1921. exit;
  1922. if (hp1 = BlockEnd) then
  1923. Exit;
  1924. { remove jumps to labels coming right after them }
  1925. if FindLabel(ThisLabel, hp1) and
  1926. { Cannot remove the first instruction }
  1927. (p<>StartPoint) then
  1928. begin
  1929. ThisLabel.decrefs;
  1930. {$ifdef cpudelayslot}
  1931. RemoveDelaySlot(p);
  1932. {$endif cpudelayslot}
  1933. hp1 := tai(p.Next);
  1934. { Use RemoveInstruction, not RemoveCurrentP, since the latter also
  1935. updates the registers }
  1936. RemoveInstruction(p);
  1937. p := hp1;
  1938. Result := True;
  1939. end;
  1940. end;
  1941. function TAOptObj.CanDoJumpOpts: Boolean;
  1942. begin
  1943. { Always allow by default }
  1944. Result := True;
  1945. end;
  1946. function TAOptObj.DoJumpOptimizations(var p: tai; var stoploop: Boolean): Boolean;
  1947. var
  1948. hp1, hp2: tai;
  1949. ThisLabel: TAsmLabel;
  1950. ThisPassResult: Boolean;
  1951. begin
  1952. Result := False;
  1953. if (p.typ <> ait_instruction) or not IsJumpToLabel(taicpu(p)) then
  1954. Exit;
  1955. repeat
  1956. ThisPassResult := False;
  1957. if GetNextInstruction(p, hp1) and (hp1 <> BlockEnd) then
  1958. begin
  1959. SkipEntryExitMarker(hp1,hp1);
  1960. if (hp1 = BlockEnd) then
  1961. Exit;
  1962. ThisLabel := TAsmLabel(JumpTargetOp(taicpu(p))^.ref^.symbol);
  1963. hp2 := getlabelwithsym(ThisLabel);
  1964. { getlabelwithsym returning nil occurs if a label is in a
  1965. different block (e.g. on the other side of an asm...end pair). }
  1966. if Assigned(hp2) then
  1967. begin
  1968. { If there are multiple labels in a row, change the destination to the last one
  1969. in order to aid optimisation later }
  1970. ThisLabel := CollapseLabelCluster(p, hp2);
  1971. if CollapseZeroDistJump(p, ThisLabel) then
  1972. begin
  1973. stoploop := False;
  1974. Result := True;
  1975. Exit;
  1976. end;
  1977. if IsJumpToLabelUncond(taicpu(p)) then
  1978. begin
  1979. { Remove unreachable code between the jump and the next label }
  1980. ThisPassResult := RemoveDeadCodeAfterJump(taicpu(p));
  1981. if GetFinalDestination(taicpu(p), 0) or ThisPassResult then
  1982. { Might have caused some earlier labels to become dead }
  1983. stoploop := False;
  1984. end
  1985. {$ifndef JVM}
  1986. else if (taicpu(p).opcode {$ifdef z80}in{$else}={$endif} aopt_condjmp) then
  1987. ThisPassResult := OptimizeConditionalJump(ThisLabel, p, hp1, stoploop)
  1988. {$endif JVM}
  1989. ;
  1990. end;
  1991. end;
  1992. Result := Result or ThisPassResult;
  1993. until not (ThisPassResult and (p.typ = ait_instruction) and IsJumpToLabel(taicpu(p)));
  1994. end;
  1995. function TAOptObj.GetFinalDestination(hp: taicpu; level: longint): boolean;
  1996. {traces sucessive jumps to their final destination and sets it, e.g.
  1997. je l1 je l3 <code> <code>
  1998. l1: becomes l1:
  1999. je l2 je l3
  2000. <code> <code>
  2001. l2: l2:
  2002. jmp l3 jmp l3
  2003. the level parameter denotes how deep we have already followed the jump,
  2004. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  2005. var p1: tai;
  2006. p2: tai;
  2007. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM) and not defined(loongarch64)}
  2008. p3: tai;
  2009. {$endif}
  2010. ThisLabel, l: tasmlabel;
  2011. begin
  2012. GetFinalDestination := false;
  2013. if level > 20 then
  2014. exit;
  2015. ThisLabel := TAsmLabel(JumpTargetOp(hp)^.ref^.symbol);
  2016. p1 := getlabelwithsym(ThisLabel);
  2017. if assigned(p1) then
  2018. begin
  2019. SkipLabels(p1,p1);
  2020. if (p1.typ = ait_instruction) and
  2021. (taicpu(p1).is_jmp) then
  2022. begin
  2023. p2 := tai(p1.Next);
  2024. if p2 = BlockEnd then
  2025. Exit;
  2026. { Collapse any zero distance jumps we stumble across }
  2027. while (p1<>StartPoint) and CollapseZeroDistJump(p1, TAsmLabel(JumpTargetOp(taicpu(p1))^.ref^.symbol)) do
  2028. begin
  2029. { Note: Cannot remove the first instruction }
  2030. if (p1.typ = ait_label) then
  2031. SkipLabels(p1, p1);
  2032. if not Assigned(p1) then
  2033. { No more valid commands }
  2034. Exit;
  2035. { Check to see that we are actually still at a jump }
  2036. if not ((tai(p1).typ = ait_instruction) and (taicpu(p1).is_jmp)) then
  2037. begin
  2038. { Required to ensure recursion works properly, but to also
  2039. return false if a jump isn't modified. [Kit] }
  2040. if level > 0 then GetFinalDestination := True;
  2041. Exit;
  2042. end;
  2043. p2 := tai(p1.Next);
  2044. if p2 = BlockEnd then
  2045. Exit;
  2046. end;
  2047. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM) and not defined(loongarch64)}
  2048. p3 := p2;
  2049. {$endif not MIPS and not RV64 and not RV32 and not JVM and not loongarch64}
  2050. if { the next instruction after the label where the jump hp arrives}
  2051. { is unconditional or of the same type as hp, so continue }
  2052. IsJumpToLabelUncond(taicpu(p1))
  2053. { TODO: For anyone with experience with MIPS or RISC-V, please add support for tracing
  2054. conditional jumps. [Kit] }
  2055. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM) and not defined(loongarch64)}
  2056. { for MIPS, it isn't enough to check the condition; first operands must be same, too. }
  2057. or
  2058. condition_in(hp.condition, taicpu(p1).condition) or
  2059. { the next instruction after the label where the jump hp arrives
  2060. is the opposite of hp (so this one is never taken), but after
  2061. that one there is a branch that will be taken, so perform a
  2062. little hack: set p1 equal to this instruction }
  2063. (condition_in(hp.condition, inverse_cond(taicpu(p1).condition)) and
  2064. SkipLabels(p3,p2) and
  2065. (p2.typ = ait_instruction) and
  2066. (taicpu(p2).is_jmp) and
  2067. (IsJumpToLabelUncond(taicpu(p2)) or
  2068. (condition_in(hp.condition, taicpu(p2).condition))
  2069. ) and
  2070. SetAndTest(p2,p1)
  2071. )
  2072. {$endif not MIPS and not RV64 and not RV32 and not JVM and not loongarch64}
  2073. then
  2074. begin
  2075. { quick check for loops of the form "l5: ; jmp l5" }
  2076. if (TAsmLabel(JumpTargetOp(taicpu(p1))^.ref^.symbol).labelnr = ThisLabel.labelnr) then
  2077. exit;
  2078. if not GetFinalDestination(taicpu(p1),succ(level)) then
  2079. exit;
  2080. { NOTE: Do not move this before the "l5: ; jmp l5" check,
  2081. because GetFinalDestination may change the destination
  2082. label of p1. [Kit] }
  2083. l := tasmlabel(JumpTargetOp(taicpu(p1))^.ref^.symbol);
  2084. {$if defined(aarch64)}
  2085. { can't have conditional branches to
  2086. global labels on AArch64, because the
  2087. offset may become too big }
  2088. if not(taicpu(hp).condition in [C_None,C_AL,C_NV]) and
  2089. (l.bind<>AB_LOCAL) then
  2090. exit;
  2091. {$endif aarch64}
  2092. ThisLabel.decrefs;
  2093. JumpTargetOp(hp)^.ref^.symbol:=l;
  2094. l.increfs;
  2095. GetFinalDestination := True;
  2096. Exit;
  2097. end
  2098. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM) and not defined(loongarch64)}
  2099. else
  2100. if condition_in(inverse_cond(hp.condition), taicpu(p1).condition) then
  2101. begin
  2102. if not FindLiveLabel(p1,l) then
  2103. begin
  2104. {$ifdef finaldestdebug}
  2105. insertllitem(asml,p1,p1.next,tai_comment.Create(
  2106. strpnew('previous label inserted'))));
  2107. {$endif finaldestdebug}
  2108. current_asmdata.getjumplabel(l);
  2109. insertllitem(p1,p1.next,tai_label.Create(l));
  2110. ThisLabel.decrefs;
  2111. JumpTargetOp(hp)^.ref^.symbol := l;
  2112. l.increfs;
  2113. GetFinalDestination := True;
  2114. { this won't work, since the new label isn't in the labeltable }
  2115. { so it will fail the rangecheck. Labeltable should become a }
  2116. { hashtable to support this: }
  2117. { GetFinalDestination(asml, hp); }
  2118. end
  2119. else
  2120. begin
  2121. {$ifdef finaldestdebug}
  2122. insertllitem(asml,p1,p1.next,tai_comment.Create(
  2123. strpnew('next label reused'))));
  2124. {$endif finaldestdebug}
  2125. l.increfs;
  2126. ThisLabel.decrefs;
  2127. JumpTargetOp(hp)^.ref^.symbol := l;
  2128. if not GetFinalDestination(hp,succ(level)) then
  2129. exit;
  2130. end;
  2131. GetFinalDestination := True;
  2132. Exit;
  2133. end;
  2134. {$endif not MIPS and not RV64 and not RV32 and not JVM and not loongarch64}
  2135. end;
  2136. end;
  2137. { Required to ensure recursion works properly, but to also
  2138. return false if a jump isn't modified. [Kit] }
  2139. if level > 0 then GetFinalDestination := True;
  2140. end;
  2141. procedure TAOptObj.PrePeepHoleOpts;
  2142. var
  2143. p: tai;
  2144. begin
  2145. p := BlockStart;
  2146. ClearUsedRegs;
  2147. while (p <> BlockEnd) Do
  2148. begin
  2149. prefetch(pointer(p.Next)^);
  2150. if PrePeepHoleOptsCpu(p) then
  2151. continue;
  2152. if assigned(p) then
  2153. begin
  2154. p:=tai(p.next);
  2155. UpdateUsedRegs(p);
  2156. end;
  2157. end;
  2158. end;
  2159. procedure TAOptObj.PeepHoleOptPass1;
  2160. const
  2161. MaxPasses: array[1..3] of Cardinal = (1, 2, 8);
  2162. var
  2163. p : tai;
  2164. stoploop, FirstInstruction, JumpOptsAvailable: boolean;
  2165. PassCount, MaxCount: Cardinal;
  2166. begin
  2167. JumpOptsAvailable := CanDoJumpOpts();
  2168. StartPoint := BlockStart;
  2169. PassCount := 0;
  2170. { Determine the maximum number of passes allowed based on the compiler switches }
  2171. if (cs_opt_level3 in current_settings.optimizerswitches) then
  2172. { it should never take more than 8 passes, but the limit is finite to protect against faulty optimisations }
  2173. MaxCount := MaxPasses[3]
  2174. else if (cs_opt_level2 in current_settings.optimizerswitches) then
  2175. MaxCount := MaxPasses[2] { The original double run of Pass 1 }
  2176. else
  2177. MaxCount := MaxPasses[1];
  2178. NotFirstIteration := False;
  2179. repeat
  2180. stoploop:=true;
  2181. p := StartPoint;
  2182. FirstInstruction := True;
  2183. ClearUsedRegs;
  2184. while Assigned(p) and (p <> BlockEnd) Do
  2185. begin
  2186. prefetch(pointer(p.Next)^);
  2187. { I'am not sure why this is done, UsedRegs should reflect the register usage before the instruction
  2188. If an instruction needs the information of this, it can easily create a TempUsedRegs (FK)
  2189. UpdateUsedRegs(tai(p.next));
  2190. }
  2191. {$ifdef DEBUG_OPTALLOC}
  2192. if p.Typ=ait_instruction then
  2193. InsertLLItem(tai(p.Previous),p,tai_comment.create(strpnew(GetAllocationString(UsedRegs))));
  2194. {$endif DEBUG_OPTALLOC}
  2195. { Handle jump optimizations first }
  2196. if JumpOptsAvailable and DoJumpOptimizations(p, stoploop) then
  2197. begin
  2198. if FirstInstruction then
  2199. { Update StartPoint, since the old p was removed;
  2200. don't set FirstInstruction to False though, as
  2201. the new p might get removed too. }
  2202. StartPoint := p;
  2203. if (p.typ = ait_instruction) and IsJumpToLabel(taicpu(p)) then
  2204. Continue;
  2205. end;
  2206. if PeepHoleOptPass1Cpu(p) then
  2207. begin
  2208. stoploop:=false;
  2209. UpdateUsedRegs(p);
  2210. if FirstInstruction then
  2211. { Update StartPoint, since the old p was modified;
  2212. don't set FirstInstruction to False though, as
  2213. the new p might get modified too. }
  2214. StartPoint := p;
  2215. continue;
  2216. end;
  2217. FirstInstruction := False;
  2218. if assigned(p) then
  2219. p := tai(UpdateUsedRegsAndOptimize(p).Next);
  2220. end;
  2221. Inc(PassCount);
  2222. if not stoploop then
  2223. NotFirstIteration := True;
  2224. until stoploop or (PassCount >= MaxCount);
  2225. end;
  2226. procedure TAOptObj.PeepHoleOptPass2;
  2227. var
  2228. p: tai;
  2229. begin
  2230. p := BlockStart;
  2231. ClearUsedRegs;
  2232. while (p <> BlockEnd) Do
  2233. begin
  2234. prefetch(pointer(p.Next)^);
  2235. if PeepHoleOptPass2Cpu(p) then
  2236. continue;
  2237. if assigned(p) then
  2238. p := tai(UpdateUsedRegsAndOptimize(p).Next);
  2239. end;
  2240. end;
  2241. procedure TAOptObj.PostPeepHoleOpts;
  2242. var
  2243. p: tai;
  2244. begin
  2245. p := BlockStart;
  2246. ClearUsedRegs;
  2247. while (p <> BlockEnd) Do
  2248. begin
  2249. prefetch(pointer(p.Next)^);
  2250. if PostPeepHoleOptsCpu(p) then
  2251. continue;
  2252. if assigned(p) then
  2253. begin
  2254. p:=tai(p.next);
  2255. UpdateUsedRegs(p);
  2256. end;
  2257. end;
  2258. end;
  2259. function TAOptObj.PrePeepHoleOptsCpu(var p : tai) : boolean;
  2260. begin
  2261. result := false;
  2262. end;
  2263. function TAOptObj.PeepHoleOptPass1Cpu(var p: tai): boolean;
  2264. begin
  2265. result := false;
  2266. end;
  2267. function TAOptObj.PeepHoleOptPass2Cpu(var p : tai) : boolean;
  2268. begin
  2269. result := false;
  2270. end;
  2271. function TAOptObj.PostPeepHoleOptsCpu(var p: tai): boolean;
  2272. begin
  2273. result := false;
  2274. end;
  2275. procedure TAOptObj.Debug_InsertInstrRegisterDependencyInfo;
  2276. var
  2277. p: tai;
  2278. ri: tregisterindex;
  2279. reg: TRegister;
  2280. commentstr: AnsiString;
  2281. registers_found: Boolean;
  2282. begin
  2283. p:=tai(AsmL.First);
  2284. while (p<>AsmL.Last) Do
  2285. begin
  2286. if p.typ=ait_instruction then
  2287. begin
  2288. {$ifdef x86}
  2289. taicpu(p).SetOperandOrder(op_att);
  2290. {$endif x86}
  2291. commentstr:='Instruction reads';
  2292. registers_found:=false;
  2293. for ri in tregisterindex do
  2294. begin
  2295. reg:=regnumber_table[ri];
  2296. if (reg<>NR_NO) and InstructionLoadsFromReg(reg,p) then
  2297. begin
  2298. commentstr:=commentstr+' '+std_regname(reg);
  2299. registers_found:=true;
  2300. end;
  2301. end;
  2302. if not registers_found then
  2303. commentstr:=commentstr+' no registers';
  2304. commentstr:=commentstr+' and writes new values in';
  2305. registers_found:=false;
  2306. for ri in tregisterindex do
  2307. begin
  2308. reg:=regnumber_table[ri];
  2309. if (reg<>NR_NO) and RegLoadedWithNewValue(reg,p) then
  2310. begin
  2311. commentstr:=commentstr+' '+std_regname(reg);
  2312. registers_found:=true;
  2313. end;
  2314. end;
  2315. if not registers_found then
  2316. commentstr:=commentstr+' no registers';
  2317. AsmL.InsertAfter(tai_comment.Create(strpnew(commentstr)),p);
  2318. end;
  2319. p:=tai(p.next);
  2320. end;
  2321. end;
  2322. End.