aoptobj.pas 90 KB

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