aoptobj.pas 100 KB

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