aoptobj.pas 106 KB

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