aoptobj.pas 91 KB

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