2
0

aoptobj.pas 105 KB

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