aoptobj.pas 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. {
  2. Copyright (c) 1998-2004 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit contains the processor independent assembler optimizer
  5. object, base for the dataflow analyzer, peepholeoptimizer and
  6. common subexpression elimination objects.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. Unit AoptObj;
  21. {$i fpcdefs.inc}
  22. { general, processor independent objects for use by the assembler optimizer }
  23. Interface
  24. uses
  25. globtype,
  26. aasmbase,aasmcpu,aasmtai,aasmdata,
  27. cclasses,
  28. cgbase,cgutils,
  29. cpubase,
  30. aoptbase,aoptcpub,aoptda;
  31. { ************************************************************************* }
  32. { ********************************* Constants ***************************** }
  33. { ************************************************************************* }
  34. Const
  35. {Possible register content types}
  36. con_Unknown = 0;
  37. con_ref = 1;
  38. con_const = 2;
  39. {***************** Types ****************}
  40. Type
  41. { ************************************************************************* }
  42. { ************************* Some general type definitions ***************** }
  43. { ************************************************************************* }
  44. TRefCompare = Function(const r1, r2: TReference): Boolean;
  45. //!!! FIXME
  46. TRegArray = Array[byte] of tsuperregister;
  47. TRegSet = tcpuregisterset;
  48. { possible actions on an operand: read, write or modify (= read & write) }
  49. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  50. { ************************************************************************* }
  51. { * Object to hold information on which regiters are in use and which not * }
  52. { ************************************************************************* }
  53. { TUsedRegs }
  54. TUsedRegs = class
  55. Constructor create(aTyp : TRegisterType);
  56. Constructor create_regset(aTyp : TRegisterType;Const _RegSet: TRegSet);
  57. Destructor Destroy;override;
  58. Procedure Clear;
  59. { update the info with the pairegalloc objects coming after
  60. p }
  61. procedure Update(p: Tai; IgnoreNewAllocs: Boolean=false);
  62. { is Reg currently in use }
  63. Function IsUsed(Reg: TRegister): Boolean;
  64. { get all the currently used registers }
  65. Function GetUsedRegs: TRegSet;
  66. { outputs the current set }
  67. Procedure Dump(var t : text);
  68. Private
  69. Typ : TRegisterType;
  70. UsedRegs: TRegSet;
  71. End;
  72. { ************************************************************************* }
  73. { ******************* Contents of the integer registers ******************* }
  74. { ************************************************************************* }
  75. { size of the integer that holds the state number of a register. Can be any }
  76. { integer type, so it can be changed to reduce the size of the TContent }
  77. { structure or to improve alignment }
  78. TStateInt = Byte;
  79. TContent = Record
  80. { start and end of block instructions that defines the }
  81. { content of this register. If Typ = con_const, then }
  82. { Longint(StartMod) = value of the constant) }
  83. StartMod: Tai;
  84. { starts at 0, gets increased everytime the register is }
  85. { written to }
  86. WState: TStateInt;
  87. { starts at 0, gets increased everytime the register is read }
  88. { from }
  89. RState: TStateInt;
  90. { how many instructions starting with StarMod does the block }
  91. { consist of }
  92. NrOfMods: Byte;
  93. { the type of the content of the register: unknown, memory }
  94. { (variable) or constant }
  95. Typ: Byte;
  96. End;
  97. //!!! FIXME
  98. TRegContent = Array[byte] Of TContent;
  99. { ************************************************************************** }
  100. { information object with the contents of every register. Every Tai object }
  101. { gets one of these assigned: a pointer to it is stored in the OptInfo field }
  102. { ************************************************************************** }
  103. { TPaiProp }
  104. TPaiProp = class(TAoptBaseCpu)
  105. Regs: TRegContent;
  106. { can this instruction be removed? }
  107. CanBeRemoved: Boolean;
  108. Constructor create; reintroduce;
  109. { checks the whole sequence of which (so regs[which].StartMod and and }
  110. { the next NrOfMods Tai objects) to see whether Reg is used somewhere, }
  111. { without it being loaded with something else first }
  112. Function RegInSequence(Reg, which: TRegister): Boolean;
  113. { destroy the contents of a register, as well as those whose contents }
  114. { are based on those of that register }
  115. Procedure DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  116. TInstrSinceLastMod);
  117. { if the contents of WhichReg (can be R_NO in case of a constant) are }
  118. { written to memory at the location Ref, the contents of the registers }
  119. { that depend on Ref have to be destroyed }
  120. Procedure DestroyRefs(Const Ref: TReference; WhichReg: TRegister; var
  121. InstrSinceLastMod: TInstrSinceLastMod);
  122. { an instruction reads from operand o }
  123. Procedure ReadOp(const o:toper);
  124. { an instruction reads from reference Ref }
  125. Procedure ReadRef(Ref: PReference);
  126. { an instruction reads from register Reg }
  127. Procedure ReadReg(Reg: TRegister);
  128. { an instruction writes/modifies operand o and this has special }
  129. { side-effects or modifies the contents in such a way that we can't }
  130. { simply add this instruction to the sequence of instructions that }
  131. { describe the contents of the operand, so destroy it }
  132. Procedure DestroyOp(const o:Toper; var InstrSinceLastMod:
  133. TInstrSinceLastMod);
  134. { destroy the contents of all registers }
  135. Procedure DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  136. { a register's contents are modified, but not destroyed (the new value
  137. depends on the old one) }
  138. Procedure ModifyReg(reg: TRegister; var InstrSinceLastMod:
  139. TInstrSinceLastMod);
  140. { an operand's contents are modified, but not destroyed (the new value
  141. depends on the old one) }
  142. Procedure ModifyOp(const oper: TOper; var InstrSinceLastMod:
  143. TInstrSinceLastMod);
  144. { increase the write state of a register (call every time a register is
  145. written to) }
  146. Procedure IncWState(Reg: TRegister);
  147. { increase the read state of a register (call every time a register is }
  148. { read from) }
  149. Procedure IncRState(Reg: TRegister);
  150. { get the write state of a register }
  151. Function GetWState(Reg: TRegister): TStateInt;
  152. { get the read state of a register }
  153. Function GetRState(Reg: TRegister): TStateInt;
  154. { get the type of contents of a register }
  155. Function GetRegContentType(Reg: TRegister): Byte;
  156. Destructor Done;
  157. Private
  158. Procedure IncState(var s: TStateInt);
  159. { returns whether the reference Ref is used somewhere in the loading }
  160. { sequence Content }
  161. Function RefInSequence(Const Ref: TReference; Content: TContent;
  162. RefsEq: TRefCompare): Boolean;
  163. { returns whether the instruction P reads from and/or writes }
  164. { to Reg }
  165. Function RefInInstruction(Const Ref: TReference; p: Tai;
  166. RefsEq: TRefCompare): Boolean;
  167. { returns whether two references with at least one pointing to an array }
  168. { may point to the same memory location }
  169. End;
  170. { ************************************************************************* }
  171. { ************************ Label information ****************************** }
  172. { ************************************************************************* }
  173. TLabelTableItem = Record
  174. PaiObj: Tai;
  175. End;
  176. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  177. PLabelTable = ^TLabelTable;
  178. PLabelInfo = ^TLabelInfo;
  179. TLabelInfo = Record
  180. { the highest and lowest label number occurring in the current code }
  181. { fragment }
  182. LowLabel, HighLabel: longint;
  183. LabelDif: cardinal;
  184. { table that contains the addresses of the Pai_Label objects associated
  185. with each label number }
  186. LabelTable: PLabelTable;
  187. End;
  188. { ************************************************************************* }
  189. { ********** General optimizer object, used to derive others from ********* }
  190. { ************************************************************************* }
  191. TAllUsedRegs = array[TRegisterType] of TUsedRegs;
  192. { TAOptObj }
  193. TAOptObj = class(TAoptBaseCpu)
  194. { the PAasmOutput list this optimizer instance works on }
  195. AsmL: TAsmList;
  196. { The labelinfo record contains the addresses of the Tai objects }
  197. { that are labels, how many labels there are and the min and max }
  198. { label numbers }
  199. LabelInfo: PLabelInfo;
  200. { Start and end of the block that is currently being optimized }
  201. BlockStart, BlockEnd: Tai;
  202. DFA: TAOptDFA;
  203. UsedRegs: TAllUsedRegs;
  204. { _AsmL is the PAasmOutpout list that has to be optimized, }
  205. { _BlockStart and _BlockEnd the start and the end of the block }
  206. { that has to be optimized and _LabelInfo a pointer to a }
  207. { TLabelInfo record }
  208. Constructor create(_AsmL: TAsmList; _BlockStart, _BlockEnd: Tai;
  209. _LabelInfo: PLabelInfo); virtual; reintroduce;
  210. Destructor Destroy;override;
  211. { processor independent methods }
  212. Procedure CreateUsedRegs(var regs: TAllUsedRegs);
  213. Procedure ClearUsedRegs;
  214. Procedure UpdateUsedRegs(p : Tai);
  215. class procedure UpdateUsedRegs(var Regs: TAllUsedRegs; p: Tai);
  216. Function CopyUsedRegs(var dest : TAllUsedRegs) : boolean;
  217. procedure RestoreUsedRegs(const Regs : TAllUsedRegs);
  218. procedure TransferUsedRegs(var dest: TAllUsedRegs);
  219. class Procedure ReleaseUsedRegs(const regs : TAllUsedRegs);
  220. class Function RegInUsedRegs(reg : TRegister;regs : TAllUsedRegs) : boolean;
  221. class Procedure IncludeRegInUsedRegs(reg : TRegister;var regs : TAllUsedRegs);
  222. class Procedure ExcludeRegFromUsedRegs(reg: TRegister;var regs : TAllUsedRegs);
  223. Function GetAllocationString(const regs : TAllUsedRegs) : string;
  224. { returns true if the label L is found between hp and the next }
  225. { instruction }
  226. Function FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  227. { inserts new_one between prev and foll in AsmL }
  228. Procedure InsertLLItem(prev, foll, new_one: TLinkedListItem);
  229. { If P is a Tai object releveant to the optimizer, P is returned
  230. If it is not relevant tot he optimizer, the first object after P
  231. that is relevant is returned }
  232. Function SkipHead(P: Tai): Tai;
  233. { returns true if the operands o1 and o2 are completely equal }
  234. Function OpsEqual(const o1,o2:toper): Boolean;
  235. { Returns the next ait_alloc object with ratype ra_alloc for
  236. Reg is found in the block
  237. of Tai's starting with StartPai and ending with the next "real"
  238. instruction. If none is found, it returns
  239. nil
  240. }
  241. Function FindRegAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  242. { Returns the last ait_alloc object with ratype ra_alloc for
  243. Reg is found in the block
  244. of Tai's starting with StartPai and ending with the next "real"
  245. instruction. If none is found, it returns
  246. nil
  247. }
  248. Function FindRegAllocBackward(Reg : TRegister; StartPai : Tai) : tai_regalloc;
  249. { Returns the next ait_alloc object with ratype ra_dealloc
  250. for Reg which is found in the block of Tai's starting with StartPai
  251. and ending with the next "real" instruction. If none is found, it returns
  252. nil }
  253. Function FindRegDeAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  254. { allocates register reg between (and including) instructions p1 and p2
  255. the type of p1 and p2 must not be in SkipInstr }
  256. procedure AllocRegBetween(reg : tregister; p1,p2 : tai; var initialusedregs : TAllUsedRegs);
  257. { reg used after p? }
  258. function RegUsedAfterInstruction(reg: Tregister; p: tai; var AllUsedRegs: TAllUsedRegs): Boolean;
  259. { returns true if reg reaches it's end of life at p, this means it is either
  260. reloaded with a new value or it is deallocated afterwards }
  261. function RegEndOfLife(reg: TRegister;p: taicpu): boolean;
  262. { removes p from asml, updates registers and replaces it by a valid value, if this is the case true is returned }
  263. function RemoveCurrentP(var p : tai): boolean;
  264. { traces sucessive jumps to their final destination and sets it, e.g.
  265. je l1 je l3
  266. <code> <code>
  267. l1: becomes l1:
  268. je l2 je l3
  269. <code> <code>
  270. l2: l2:
  271. jmp l3 jmp l3
  272. the level parameter denotes how deeep we have already followed the jump,
  273. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  274. function GetFinalDestination(hp: taicpu; level: longint): boolean;
  275. function getlabelwithsym(sym: tasmlabel): tai;
  276. { Removes an instruction following hp1 (possibly with reg.deallocations in between),
  277. if its opcode is A_NOP. }
  278. procedure RemoveDelaySlot(hp1: tai);
  279. { peephole optimizer }
  280. procedure PrePeepHoleOpts; virtual;
  281. procedure PeepHoleOptPass1; virtual;
  282. procedure PeepHoleOptPass2; virtual;
  283. procedure PostPeepHoleOpts; virtual;
  284. { processor dependent methods }
  285. // if it returns true, perform a "continue"
  286. function PrePeepHoleOptsCpu(var p: tai): boolean; virtual;
  287. function PeepHoleOptPass1Cpu(var p: tai): boolean; virtual;
  288. function PeepHoleOptPass2Cpu(var p: tai): boolean; virtual;
  289. function PostPeepHoleOptsCpu(var p: tai): boolean; virtual;
  290. { insert debug comments about which registers are read and written by
  291. each instruction. Useful for debugging the InstructionLoadsFromReg and
  292. other similar functions. }
  293. procedure Debug_InsertInstrRegisterDependencyInfo; virtual;
  294. End;
  295. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  296. { ***************************** Implementation **************************** }
  297. Implementation
  298. uses
  299. cutils,
  300. globals,
  301. verbose,
  302. aoptutils,
  303. procinfo;
  304. function JumpTargetOp(ai: taicpu): poper; inline;
  305. begin
  306. {$if defined(MIPS) or defined(riscv64) or defined(riscv32)}
  307. { MIPS or RiscV branches can have 1,2 or 3 operands, target label is the last one. }
  308. result:=ai.oper[ai.ops-1];
  309. {$elseif defined(SPARC64)}
  310. if ai.ops=2 then
  311. result:=ai.oper[1]
  312. else
  313. result:=ai.oper[0];
  314. {$else MIPS}
  315. result:=ai.oper[0];
  316. {$endif}
  317. end;
  318. { ************************************************************************* }
  319. { ******************************** TUsedRegs ****************************** }
  320. { ************************************************************************* }
  321. Constructor TUsedRegs.create(aTyp : TRegisterType);
  322. Begin
  323. Typ:=aTyp;
  324. UsedRegs := [];
  325. End;
  326. Constructor TUsedRegs.create_regset(aTyp : TRegisterType;Const _RegSet: TRegSet);
  327. Begin
  328. Typ:=aTyp;
  329. UsedRegs := _RegSet;
  330. End;
  331. {
  332. updates UsedRegs with the RegAlloc Information coming after P
  333. }
  334. Procedure TUsedRegs.Update(p: Tai;IgnoreNewAllocs : Boolean = false);
  335. Begin
  336. { this code is normally not used because updating the register allocation information is done in
  337. TAOptObj.UpdateUsedRegs for speed reasons }
  338. repeat
  339. while assigned(p) and
  340. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  341. (p.typ = ait_label) or
  342. ((p.typ = ait_marker) and
  343. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  344. p := tai(p.next);
  345. while assigned(p) and
  346. (p.typ=ait_RegAlloc) Do
  347. begin
  348. if (getregtype(tai_regalloc(p).reg) = typ) then
  349. begin
  350. case tai_regalloc(p).ratype of
  351. ra_alloc :
  352. if not(IgnoreNewAllocs) then
  353. Include(UsedRegs, getsupreg(tai_regalloc(p).reg));
  354. ra_dealloc :
  355. Exclude(UsedRegs, getsupreg(tai_regalloc(p).reg));
  356. end;
  357. end;
  358. p := tai(p.next);
  359. end;
  360. until not(assigned(p)) or
  361. (not(p.typ in SkipInstr) and
  362. not((p.typ = ait_label) and
  363. labelCanBeSkipped(tai_label(p))));
  364. End;
  365. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  366. Begin
  367. IsUsed := (getregtype(Reg)=Typ) and (getsupreg(Reg) in UsedRegs);
  368. End;
  369. Function TUsedRegs.GetUsedRegs: TRegSet; inline;
  370. Begin
  371. GetUsedRegs := UsedRegs;
  372. End;
  373. procedure TUsedRegs.Dump(var t: text);
  374. var
  375. i: dword;
  376. begin
  377. write(t,Typ,' ');
  378. for i:=low(TRegSet) to high(TRegSet) do
  379. if i in UsedRegs then
  380. write(t,i,' ');
  381. writeln(t);
  382. end;
  383. Destructor TUsedRegs.Destroy;
  384. Begin
  385. inherited destroy;
  386. end;
  387. procedure TUsedRegs.Clear;
  388. begin
  389. UsedRegs := [];
  390. end;
  391. { ************************************************************************* }
  392. { **************************** TPaiProp *********************************** }
  393. { ************************************************************************* }
  394. Constructor TPaiProp.Create;
  395. Begin
  396. {!!!!!!
  397. UsedRegs.Init;
  398. CondRegs.init;
  399. }
  400. { DirFlag: TFlagContents; I386 specific}
  401. End;
  402. Function TPaiProp.RegInSequence(Reg, which: TRegister): Boolean;
  403. {
  404. Var p: Tai;
  405. RegsChecked: TRegSet;
  406. content: TContent;
  407. Counter: Byte;
  408. TmpResult: Boolean;
  409. }
  410. begin
  411. Result:=False; { unimplemented }
  412. (*!!!!!!!!!!1
  413. RegsChecked := [];
  414. content := regs[which];
  415. p := content.StartMod;
  416. TmpResult := False;
  417. Counter := 1;
  418. While Not(TmpResult) And
  419. (Counter <= Content.NrOfMods) Do
  420. Begin
  421. If IsLoadMemReg(p) Then
  422. With PInstr(p)^.oper[LoadSrc]^.ref^ Do
  423. If (Base = ProcInfo.FramePointer)
  424. {$ifdef cpurefshaveindexreg}
  425. And (Index = R_NO)
  426. {$endif cpurefshaveindexreg} Then
  427. Begin
  428. RegsChecked := RegsChecked +
  429. [RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg)];
  430. If Reg = RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg) Then
  431. Break;
  432. End
  433. Else
  434. Begin
  435. If (Base = Reg) And
  436. Not(Base In RegsChecked)
  437. Then TmpResult := True;
  438. {$ifdef cpurefshaveindexreg}
  439. If Not(TmpResult) And
  440. (Index = Reg) And
  441. Not(Index In RegsChecked)
  442. Then TmpResult := True;
  443. {$Endif cpurefshaveindexreg}
  444. End
  445. Else TmpResult := RegInInstruction(Reg, p);
  446. Inc(Counter);
  447. GetNextInstruction(p,p)
  448. End;
  449. RegInSequence := TmpResult
  450. *)
  451. End;
  452. Procedure TPaiProp.DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  453. TInstrSinceLastMod);
  454. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  455. { the contents of registers are loaded with a memory location based on Reg }
  456. {
  457. Var TmpWState, TmpRState: Byte;
  458. Counter: TRegister;
  459. }
  460. Begin
  461. {!!!!!!!
  462. Reg := RegMaxSize(Reg);
  463. If (Reg in [LoGPReg..HiGPReg]) Then
  464. For Counter := LoGPReg to HiGPReg Do
  465. With Regs[Counter] Do
  466. If (Counter = reg) Or
  467. ((Typ = Con_Ref) And
  468. RegInSequence(Reg, Counter)) Then
  469. Begin
  470. InstrSinceLastMod[Counter] := 0;
  471. IncWState(Counter);
  472. TmpWState := GetWState(Counter);
  473. TmpRState := GetRState(Counter);
  474. FillChar(Regs[Counter], SizeOf(TContent), 0);
  475. WState := TmpWState;
  476. RState := TmpRState
  477. End
  478. }
  479. End;
  480. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  481. Begin
  482. Result:=False; { unimplemented }
  483. (*!!!!!!!!!!
  484. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  485. {$ifdef refsHaveSegmentReg}
  486. (R1.Segment = R2.Segment) And
  487. {$endif}
  488. (R1.Base = R2.Base) And
  489. (R1.Symbol=R2.Symbol);
  490. *)
  491. End;
  492. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  493. var InstrSinceLastMod: TInstrSinceLastMod);
  494. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  495. { is the register whose contents are being written to memory (if this proc }
  496. { is called because of a "mov?? %reg, (mem)" instruction) }
  497. {
  498. Var RefsEq: TRefCompare;
  499. Counter: TRegister;
  500. }
  501. Begin
  502. (*!!!!!!!!!!!
  503. WhichReg := RegMaxSize(WhichReg);
  504. If (Ref.base = procinfo.FramePointer) or
  505. Assigned(Ref.Symbol) Then
  506. Begin
  507. If
  508. {$ifdef cpurefshaveindexreg}
  509. (Ref.Index = R_NO) And
  510. {$endif cpurefshaveindexreg}
  511. (Not(Assigned(Ref.Symbol)) or
  512. (Ref.base = R_NO)) Then
  513. { local variable which is not an array }
  514. RefsEq := @RefsEqual
  515. Else
  516. { local variable which is an array }
  517. RefsEq := @ArrayRefsEq;
  518. {write something to a parameter, a local or global variable, so
  519. * with uncertain optimizations on:
  520. - destroy the contents of registers whose contents have somewhere a
  521. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  522. are being written to memory) is not destroyed if it's StartMod is
  523. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  524. pointer or value based on Ref)
  525. * with uncertain optimizations off:
  526. - also destroy registers that contain any pointer}
  527. For Counter := LoGPReg to HiGPReg Do
  528. With Regs[Counter] Do
  529. Begin
  530. If (typ = Con_Ref) And
  531. ((Not(cs_opt_size in current_settings.optimizerswitches) And
  532. (NrOfMods <> 1)
  533. ) Or
  534. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  535. ((Counter <> WhichReg) Or
  536. ((NrOfMods <> 1) And
  537. {StarMod is always of the type ait_instruction}
  538. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  539. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  540. )
  541. )
  542. )
  543. )
  544. Then
  545. DestroyReg(Counter, InstrSinceLastMod)
  546. End
  547. End
  548. Else
  549. {write something to a pointer location, so
  550. * with uncertain optimzations on:
  551. - do not destroy registers which contain a local/global variable or a
  552. parameter, except if DestroyRefs is called because of a "movsl"
  553. * with uncertain optimzations off:
  554. - destroy every register which contains a memory location
  555. }
  556. For Counter := LoGPReg to HiGPReg Do
  557. With Regs[Counter] Do
  558. If (typ = Con_Ref) And
  559. (Not(cs_opt_size in current_settings.optimizerswitches) Or
  560. {$ifdef x86}
  561. {for movsl}
  562. (Ref.Base = R_EDI) Or
  563. {$endif}
  564. {don't destroy if reg contains a parameter, local or global variable}
  565. Not((NrOfMods = 1) And
  566. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  567. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  568. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  569. )
  570. )
  571. )
  572. Then DestroyReg(Counter, InstrSinceLastMod)
  573. *)
  574. End;
  575. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  576. {Var Counter: TRegister;}
  577. Begin {initializes/desrtoys all registers}
  578. (*!!!!!!!!!
  579. For Counter := LoGPReg To HiGPReg Do
  580. Begin
  581. ReadReg(Counter);
  582. DestroyReg(Counter, InstrSinceLastMod);
  583. End;
  584. CondRegs.Init;
  585. { FPURegs.Init; }
  586. *)
  587. End;
  588. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  589. TInstrSinceLastMod);
  590. Begin
  591. {!!!!!!!
  592. Case o.typ Of
  593. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  594. top_ref:
  595. Begin
  596. ReadRef(o.ref);
  597. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  598. End;
  599. top_symbol:;
  600. End;
  601. }
  602. End;
  603. Procedure TPaiProp.ReadReg(Reg: TRegister);
  604. Begin
  605. {!!!!!!!
  606. Reg := RegMaxSize(Reg);
  607. If Reg in General_Registers Then
  608. IncRState(RegMaxSize(Reg))
  609. }
  610. End;
  611. Procedure TPaiProp.ReadRef(Ref: PReference);
  612. Begin
  613. (*!!!!!!
  614. If Ref^.Base <> R_NO Then
  615. ReadReg(Ref^.Base);
  616. {$ifdef cpurefshaveindexreg}
  617. If Ref^.Index <> R_NO Then
  618. ReadReg(Ref^.Index);
  619. {$endif cpurefshaveindexreg}
  620. *)
  621. End;
  622. Procedure TPaiProp.ReadOp(const o:toper);
  623. Begin
  624. Case o.typ Of
  625. top_reg: ReadReg(o.reg);
  626. top_ref: ReadRef(o.ref);
  627. else
  628. internalerror(200410241);
  629. End;
  630. End;
  631. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  632. TInstrSinceLastMod);
  633. Begin
  634. (*!!!!!!!
  635. With Regs[reg] Do
  636. If (Typ = Con_Ref)
  637. Then
  638. Begin
  639. IncState(WState);
  640. {also store how many instructions are part of the sequence in the first
  641. instructions PPaiProp, so it can be easily accessed from within
  642. CheckSequence}
  643. Inc(NrOfMods, InstrSinceLastMod[Reg]);
  644. PPaiProp(StartMod.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  645. InstrSinceLastMod[Reg] := 0;
  646. End
  647. Else
  648. DestroyReg(Reg, InstrSinceLastMod);
  649. *)
  650. End;
  651. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  652. TInstrSinceLastMod);
  653. Begin
  654. If oper.typ = top_reg Then
  655. ModifyReg(RegMaxSize(oper.reg),InstrSinceLastMod)
  656. Else
  657. Begin
  658. ReadOp(oper);
  659. DestroyOp(oper, InstrSinceLastMod);
  660. End
  661. End;
  662. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  663. Begin
  664. //!!!! IncState(Regs[Reg].WState);
  665. End;
  666. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  667. Begin
  668. //!!!! IncState(Regs[Reg].RState);
  669. End;
  670. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  671. Begin
  672. Result:=0; { unimplemented }
  673. //!!!! GetWState := Regs[Reg].WState
  674. End;
  675. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  676. Begin
  677. Result:=0; { unimplemented }
  678. //!!!! GetRState := Regs[Reg].RState
  679. End;
  680. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  681. Begin
  682. Result:=0; { unimplemented }
  683. //!!!! GetRegContentType := Regs[Reg].typ
  684. End;
  685. Destructor TPaiProp.Done;
  686. Begin
  687. //!!!! UsedRegs.Done;
  688. //!!!! CondRegs.Done;
  689. { DirFlag: TFlagContents; I386 specific}
  690. End;
  691. { ************************ private TPaiProp stuff ************************* }
  692. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  693. Begin
  694. If s <> High(TStateInt) Then Inc(s)
  695. Else s := 0
  696. End;
  697. Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Tai;
  698. RefsEq: TRefCompare): Boolean;
  699. Var Count: AWord;
  700. TmpResult: Boolean;
  701. Begin
  702. TmpResult := False;
  703. If (p.typ = ait_instruction) Then
  704. Begin
  705. Count := 0;
  706. Repeat
  707. If (TInstr(p).oper[Count]^.typ = Top_Ref) Then
  708. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count]^.ref^);
  709. Inc(Count);
  710. Until (Count = max_operands) or TmpResult;
  711. End;
  712. RefInInstruction := TmpResult;
  713. End;
  714. Function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  715. RefsEq: TRefCompare): Boolean;
  716. Var p: Tai;
  717. Counter: Byte;
  718. TmpResult: Boolean;
  719. Begin
  720. p := Content.StartMod;
  721. TmpResult := False;
  722. Counter := 1;
  723. While Not(TmpResult) And
  724. (Counter <= Content.NrOfMods) Do
  725. Begin
  726. If (p.typ = ait_instruction) And
  727. RefInInstruction(Ref, p, @references_equal)
  728. Then TmpResult := True;
  729. Inc(Counter);
  730. GetNextInstruction(p,p)
  731. End;
  732. RefInSequence := TmpResult
  733. End;
  734. { ************************************************************************* }
  735. { ***************************** TAoptObj ********************************** }
  736. { ************************************************************************* }
  737. Constructor TAoptObj.create(_AsmL: TAsmList; _BlockStart, _BlockEnd: Tai;
  738. _LabelInfo: PLabelInfo);
  739. Begin
  740. AsmL := _AsmL;
  741. BlockStart := _BlockStart;
  742. BlockEnd := _BlockEnd;
  743. LabelInfo := _LabelInfo;
  744. CreateUsedRegs(UsedRegs);
  745. End;
  746. destructor TAOptObj.Destroy;
  747. var
  748. i : TRegisterType;
  749. begin
  750. for i:=low(TRegisterType) to high(TRegisterType) do
  751. UsedRegs[i].Destroy;
  752. inherited Destroy;
  753. end;
  754. procedure TAOptObj.CreateUsedRegs(var regs: TAllUsedRegs);
  755. var
  756. i : TRegisterType;
  757. begin
  758. for i:=low(TRegisterType) to high(TRegisterType) do
  759. Regs[i]:=TUsedRegs.Create(i);
  760. end;
  761. procedure TAOptObj.ClearUsedRegs;
  762. var
  763. i : TRegisterType;
  764. begin
  765. for i:=low(TRegisterType) to high(TRegisterType) do
  766. UsedRegs[i].Clear;
  767. end;
  768. procedure TAOptObj.UpdateUsedRegs(p : Tai);
  769. begin
  770. { this code is based on TUsedRegs.Update to avoid multiple passes through the asmlist,
  771. the code is duplicated here }
  772. repeat
  773. while assigned(p) and
  774. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  775. ((p.typ = ait_label) and
  776. labelCanBeSkipped(tai_label(p))) or
  777. ((p.typ = ait_marker) and
  778. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  779. p := tai(p.next);
  780. while assigned(p) and
  781. (p.typ=ait_RegAlloc) Do
  782. begin
  783. case tai_regalloc(p).ratype of
  784. ra_alloc :
  785. Include(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  786. ra_dealloc :
  787. Exclude(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  788. end;
  789. p := tai(p.next);
  790. end;
  791. until not(assigned(p)) or
  792. (not(p.typ in SkipInstr) and
  793. not((p.typ = ait_label) and
  794. labelCanBeSkipped(tai_label(p))));
  795. end;
  796. class procedure TAOptObj.UpdateUsedRegs(var Regs : TAllUsedRegs;p : Tai);
  797. var
  798. i : TRegisterType;
  799. begin
  800. for i:=low(TRegisterType) to high(TRegisterType) do
  801. Regs[i].Update(p);
  802. end;
  803. function TAOptObj.CopyUsedRegs(var dest: TAllUsedRegs): boolean;
  804. var
  805. i : TRegisterType;
  806. begin
  807. Result:=true;
  808. for i:=low(TRegisterType) to high(TRegisterType) do
  809. dest[i]:=TUsedRegs.Create_Regset(i,UsedRegs[i].GetUsedRegs);
  810. end;
  811. procedure TAOptObj.RestoreUsedRegs(const Regs: TAllUsedRegs);
  812. var
  813. i : TRegisterType;
  814. begin
  815. { Note that the constructor Create_Regset is being called as a regular
  816. method - it is not instantiating a new object. This is because it is
  817. the only published means to modify the internal state en-masse. [Kit] }
  818. for i:=low(TRegisterType) to high(TRegisterType) do
  819. UsedRegs[i].Create_Regset(i,Regs[i].GetUsedRegs);
  820. end;
  821. procedure TAOptObj.TransferUsedRegs(var dest: TAllUsedRegs);
  822. var
  823. i : TRegisterType;
  824. begin
  825. { Note that the constructor Create_Regset is being called as a regular
  826. method - it is not instantiating a new object. This is because it is
  827. the only published means to modify the internal state en-masse. [Kit] }
  828. for i:=low(TRegisterType) to high(TRegisterType) do
  829. dest[i].Create_Regset(i, UsedRegs[i].GetUsedRegs);
  830. end;
  831. class procedure TAOptObj.ReleaseUsedRegs(const regs: TAllUsedRegs);
  832. var
  833. i : TRegisterType;
  834. begin
  835. for i:=low(TRegisterType) to high(TRegisterType) do
  836. regs[i].Free;
  837. end;
  838. class Function TAOptObj.RegInUsedRegs(reg : TRegister;regs : TAllUsedRegs) : boolean;
  839. begin
  840. result:=regs[getregtype(reg)].IsUsed(reg);
  841. end;
  842. class procedure TAOptObj.IncludeRegInUsedRegs(reg: TRegister;
  843. var regs: TAllUsedRegs);
  844. begin
  845. include(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  846. end;
  847. class procedure TAOptObj.ExcludeRegFromUsedRegs(reg: TRegister;
  848. var regs: TAllUsedRegs);
  849. begin
  850. exclude(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  851. end;
  852. function TAOptObj.GetAllocationString(const regs: TAllUsedRegs): string;
  853. var
  854. i : TRegisterType;
  855. j : TSuperRegister;
  856. begin
  857. Result:='';
  858. for i:=low(TRegisterType) to high(TRegisterType) do
  859. for j in regs[i].UsedRegs do
  860. Result:=Result+std_regname(newreg(i,j,R_SUBWHOLE))+' ';
  861. end;
  862. Function TAOptObj.FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  863. Var TempP: Tai;
  864. Begin
  865. TempP := hp;
  866. While Assigned(TempP) and
  867. (TempP.typ In SkipInstr + [ait_label,ait_align]) Do
  868. If (TempP.typ <> ait_Label) Or
  869. (Tai_label(TempP).labsym <> L)
  870. Then GetNextInstruction(TempP, TempP)
  871. Else
  872. Begin
  873. hp := TempP;
  874. FindLabel := True;
  875. exit
  876. End;
  877. FindLabel := False;
  878. End;
  879. Procedure TAOptObj.InsertLLItem(prev, foll, new_one : TLinkedListItem);
  880. Begin
  881. If Assigned(prev) Then
  882. If Assigned(foll) Then
  883. Begin
  884. If Assigned(new_one) Then
  885. Begin
  886. new_one.previous := prev;
  887. new_one.next := foll;
  888. prev.next := new_one;
  889. foll.previous := new_one;
  890. { should we update line information? }
  891. if (not (tai(new_one).typ in SkipLineInfo)) and
  892. (not (tai(foll).typ in SkipLineInfo)) then
  893. Tailineinfo(new_one).fileinfo := Tailineinfo(foll).fileinfo
  894. End
  895. End
  896. Else AsmL.Concat(new_one)
  897. Else If Assigned(Foll) Then AsmL.Insert(new_one)
  898. End;
  899. Function TAOptObj.SkipHead(P: Tai): Tai;
  900. Var OldP: Tai;
  901. Begin
  902. Repeat
  903. OldP := P;
  904. If (P.typ in SkipInstr) Or
  905. ((P.typ = ait_marker) And
  906. (Tai_Marker(P).Kind = mark_AsmBlockEnd)) Then
  907. GetNextInstruction(P, P)
  908. Else If ((P.Typ = Ait_Marker) And
  909. (Tai_Marker(P).Kind = mark_NoPropInfoStart)) Then
  910. { a marker of the type mark_NoPropInfoStart can't be the first instruction of a }
  911. { paasmoutput list }
  912. GetNextInstruction(Tai(P.Previous),P);
  913. If (P.Typ = Ait_Marker) And
  914. (Tai_Marker(P).Kind = mark_AsmBlockStart) Then
  915. Begin
  916. P := Tai(P.Next);
  917. While (P.typ <> Ait_Marker) Or
  918. (Tai_Marker(P).Kind <> mark_AsmBlockEnd) Do
  919. P := Tai(P.Next)
  920. End;
  921. Until P = OldP;
  922. SkipHead := P;
  923. End;
  924. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  925. Begin
  926. if o1.typ=o2.typ then
  927. Case o1.typ Of
  928. Top_Reg :
  929. OpsEqual:=o1.reg=o2.reg;
  930. Top_Ref :
  931. OpsEqual:=
  932. references_equal(o1.ref^, o2.ref^) and
  933. (o1.ref^.volatility=[]) and
  934. (o2.ref^.volatility=[]);
  935. Top_Const :
  936. OpsEqual:=o1.val=o2.val;
  937. Top_None :
  938. OpsEqual := True
  939. else OpsEqual := False
  940. End
  941. else
  942. OpsEqual := False;
  943. End;
  944. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  945. Begin
  946. Result:=nil;
  947. Repeat
  948. While Assigned(StartPai) And
  949. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  950. {$ifdef cpudelayslot}
  951. ((startpai.typ=ait_instruction) and (taicpu(startpai).opcode=A_NOP)) or
  952. {$endif cpudelayslot}
  953. ((StartPai.typ = ait_label) and
  954. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  955. StartPai := Tai(StartPai.Next);
  956. If Assigned(StartPai) And
  957. (StartPai.typ = ait_regAlloc) Then
  958. Begin
  959. if (tai_regalloc(StartPai).ratype=ra_alloc) and
  960. (getregtype(tai_regalloc(StartPai).Reg) = getregtype(Reg)) and
  961. (getsupreg(tai_regalloc(StartPai).Reg) = getsupreg(Reg)) then
  962. begin
  963. Result:=tai_regalloc(StartPai);
  964. exit;
  965. end;
  966. StartPai := Tai(StartPai.Next);
  967. End
  968. else
  969. exit;
  970. Until false;
  971. End;
  972. Function TAOptObj.FindRegAllocBackward(Reg: TRegister; StartPai: Tai): tai_regalloc;
  973. Begin
  974. Result:=nil;
  975. Repeat
  976. While Assigned(StartPai) And
  977. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  978. ((StartPai.typ = ait_label) and
  979. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  980. StartPai := Tai(StartPai.Previous);
  981. If Assigned(StartPai) And
  982. (StartPai.typ = ait_regAlloc) Then
  983. Begin
  984. if (tai_regalloc(StartPai).ratype=ra_alloc) and
  985. SuperRegistersEqual(tai_regalloc(StartPai).Reg,Reg) then
  986. begin
  987. Result:=tai_regalloc(StartPai);
  988. exit;
  989. end;
  990. StartPai := Tai(StartPai.Previous);
  991. End
  992. else
  993. exit;
  994. Until false;
  995. End;
  996. function TAOptObj.FindRegDeAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  997. Begin
  998. Result:=nil;
  999. Repeat
  1000. While Assigned(StartPai) And
  1001. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  1002. ((StartPai.typ = ait_label) and
  1003. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  1004. StartPai := Tai(StartPai.Next);
  1005. If Assigned(StartPai) And
  1006. (StartPai.typ = ait_regAlloc) Then
  1007. Begin
  1008. if (tai_regalloc(StartPai).ratype=ra_dealloc) and
  1009. (getregtype(tai_regalloc(StartPai).Reg) = getregtype(Reg)) and
  1010. (getsupreg(tai_regalloc(StartPai).Reg) = getsupreg(Reg)) then
  1011. begin
  1012. Result:=tai_regalloc(StartPai);
  1013. exit;
  1014. end;
  1015. StartPai := Tai(StartPai.Next);
  1016. End
  1017. else
  1018. exit;
  1019. Until false;
  1020. End;
  1021. { allocates register reg between (and including) instructions p1 and p2
  1022. the type of p1 and p2 must not be in SkipInstr }
  1023. procedure TAOptObj.AllocRegBetween(reg: tregister; p1, p2: tai; var initialusedregs: TAllUsedRegs);
  1024. var
  1025. hp, start: tai;
  1026. removedsomething,
  1027. firstRemovedWasAlloc,
  1028. lastRemovedWasDealloc: boolean;
  1029. begin
  1030. {$ifdef EXTDEBUG}
  1031. { if assigned(p1.optinfo) and
  1032. (ptaiprop(p1.optinfo)^.usedregs <> initialusedregs) then
  1033. internalerror(2004101010); }
  1034. {$endif EXTDEBUG}
  1035. start := p1;
  1036. if (reg = NR_STACK_POINTER_REG) or
  1037. (reg = current_procinfo.framepointer) or
  1038. not(assigned(p1)) then
  1039. { this happens with registers which are loaded implicitely, outside the }
  1040. { current block (e.g. esi with self) }
  1041. exit;
  1042. { make sure we allocate it for this instruction }
  1043. getnextinstruction(p2,p2);
  1044. lastRemovedWasDealloc := false;
  1045. removedSomething := false;
  1046. firstRemovedWasAlloc := false;
  1047. {$ifdef allocregdebug}
  1048. hp := tai_comment.Create(strpnew('allocating '+std_regname(newreg(R_INTREGISTER,supreg,R_SUBWHOLE))+
  1049. ' from here...'));
  1050. insertllitem(asml,p1.previous,p1,hp);
  1051. hp := tai_comment.Create(strpnew('allocated '+std_regname(newreg(R_INTREGISTER,supreg,R_SUBWHOLE))+
  1052. ' till here...'));
  1053. insertllitem(asml,p2,p2.next,hp);
  1054. {$endif allocregdebug}
  1055. { do it the safe way: always allocate the full super register,
  1056. as we do no register re-allocation in the peephole optimizer,
  1057. this does not hurt
  1058. }
  1059. case getregtype(reg) of
  1060. R_MMREGISTER:
  1061. reg:=newreg(R_MMREGISTER,getsupreg(reg),R_SUBMMWHOLE);
  1062. R_INTREGISTER:
  1063. reg:=newreg(R_INTREGISTER,getsupreg(reg),R_SUBWHOLE);
  1064. R_FPUREGISTER:
  1065. reg:=newreg(R_FPUREGISTER,getsupreg(reg),R_SUBWHOLE);
  1066. R_ADDRESSREGISTER:
  1067. reg:=newreg(R_ADDRESSREGISTER,getsupreg(reg),R_SUBWHOLE);
  1068. else
  1069. Internalerror(2018030701);
  1070. end;
  1071. if not(RegInUsedRegs(reg,initialusedregs)) then
  1072. begin
  1073. hp := tai_regalloc.alloc(reg,nil);
  1074. insertllItem(p1.previous,p1,hp);
  1075. IncludeRegInUsedRegs(reg,initialusedregs);
  1076. end;
  1077. while assigned(p1) and
  1078. (p1 <> p2) do
  1079. begin
  1080. if assigned(p1.optinfo) then
  1081. internalerror(2014022301); // IncludeRegInUsedRegs(reg,ptaiprop(p1.optinfo)^.usedregs);
  1082. p1 := tai(p1.next);
  1083. repeat
  1084. while assigned(p1) and
  1085. (p1.typ in (SkipInstr-[ait_regalloc])) Do
  1086. p1 := tai(p1.next);
  1087. { remove all allocation/deallocation info about the register in between }
  1088. if assigned(p1) and
  1089. (p1.typ = ait_regalloc) then
  1090. begin
  1091. { same super register, different sub register? }
  1092. if SuperRegistersEqual(reg,tai_regalloc(p1).reg) and (tai_regalloc(p1).reg<>reg) then
  1093. begin
  1094. if (getsubreg(tai_regalloc(p1).reg)>getsubreg(reg)) or (getsubreg(reg)=R_SUBH) then
  1095. internalerror(2016101501);
  1096. tai_regalloc(p1).reg:=reg;
  1097. end;
  1098. if tai_regalloc(p1).reg=reg then
  1099. begin
  1100. if not removedSomething then
  1101. begin
  1102. firstRemovedWasAlloc := tai_regalloc(p1).ratype=ra_alloc;
  1103. removedSomething := true;
  1104. end;
  1105. lastRemovedWasDealloc := (tai_regalloc(p1).ratype=ra_dealloc);
  1106. hp := tai(p1.Next);
  1107. asml.Remove(p1);
  1108. p1.free;
  1109. p1 := hp;
  1110. end
  1111. else
  1112. p1 := tai(p1.next);
  1113. end;
  1114. until not(assigned(p1)) or
  1115. not(p1.typ in SkipInstr);
  1116. end;
  1117. if assigned(p1) then
  1118. begin
  1119. if firstRemovedWasAlloc then
  1120. begin
  1121. hp := tai_regalloc.Alloc(reg,nil);
  1122. insertLLItem(start.previous,start,hp);
  1123. end;
  1124. if lastRemovedWasDealloc then
  1125. begin
  1126. hp := tai_regalloc.DeAlloc(reg,nil);
  1127. insertLLItem(p1.previous,p1,hp);
  1128. end;
  1129. end;
  1130. end;
  1131. function TAOptObj.RegUsedAfterInstruction(reg: Tregister; p: tai;var AllUsedRegs: TAllUsedRegs): Boolean;
  1132. begin
  1133. AllUsedRegs[getregtype(reg)].Update(tai(p.Next),true);
  1134. RegUsedAfterInstruction :=
  1135. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  1136. not(regLoadedWithNewValue(reg,p)) and
  1137. (
  1138. not(GetNextInstruction(p,p)) or
  1139. InstructionLoadsFromReg(reg,p) or
  1140. not(regLoadedWithNewValue(reg,p))
  1141. );
  1142. end;
  1143. function TAOptObj.RegEndOfLife(reg : TRegister;p : taicpu) : boolean;
  1144. begin
  1145. Result:=assigned(FindRegDealloc(reg,tai(p.Next))) or
  1146. RegLoadedWithNewValue(reg,p);
  1147. end;
  1148. function TAOptObj.RemoveCurrentP(var p : tai) : boolean;
  1149. var
  1150. hp1 : tai;
  1151. begin
  1152. result:=GetNextInstruction(p,hp1);
  1153. { p will be removed, update used register as we continue
  1154. with the next instruction after p }
  1155. UpdateUsedRegs(tai(p.Next));
  1156. AsmL.Remove(p);
  1157. p.Free;
  1158. p:=hp1;
  1159. end;
  1160. function FindAnyLabel(hp: tai; var l: tasmlabel): Boolean;
  1161. begin
  1162. FindAnyLabel := false;
  1163. while assigned(hp.next) and
  1164. (tai(hp.next).typ in (SkipInstr+[ait_align])) Do
  1165. hp := tai(hp.next);
  1166. if assigned(hp.next) and
  1167. (tai(hp.next).typ = ait_label) then
  1168. begin
  1169. FindAnyLabel := true;
  1170. l := tai_label(hp.next).labsym;
  1171. end
  1172. end;
  1173. {$push}
  1174. {$r-}
  1175. function TAOptObj.getlabelwithsym(sym: tasmlabel): tai;
  1176. begin
  1177. if (int64(sym.labelnr) >= int64(labelinfo^.lowlabel)) and
  1178. (int64(sym.labelnr) <= int64(labelinfo^.highlabel)) then { range check, a jump can go past an assembler block! }
  1179. getlabelwithsym := labelinfo^.labeltable^[sym.labelnr-labelinfo^.lowlabel].paiobj
  1180. else
  1181. getlabelwithsym := nil;
  1182. end;
  1183. {$pop}
  1184. { Returns True if hp is an unconditional jump to a label }
  1185. function IsJumpToLabelUncond(hp: taicpu): boolean;
  1186. begin
  1187. {$if defined(avr)}
  1188. result:=(hp.opcode in aopt_uncondjmp) and
  1189. {$else avr}
  1190. result:=(hp.opcode=aopt_uncondjmp) and
  1191. {$endif avr}
  1192. {$if defined(arm) or defined(aarch64)}
  1193. (hp.condition=c_None) and
  1194. {$endif arm or aarch64}
  1195. {$if defined(riscv32) or defined(riscv64)}
  1196. (hp.ops>0) and
  1197. (hp.oper[0]^.reg=NR_X0) and
  1198. {$else riscv}
  1199. (hp.ops>0) and
  1200. {$endif riscv}
  1201. (JumpTargetOp(hp)^.typ = top_ref) and
  1202. (JumpTargetOp(hp)^.ref^.symbol is TAsmLabel);
  1203. end;
  1204. { Returns True if hp is any jump to a label }
  1205. function IsJumpToLabel(hp: taicpu): boolean;
  1206. begin
  1207. result:=hp.is_jmp and
  1208. (hp.ops>0) and
  1209. (JumpTargetOp(hp)^.typ = top_ref) and
  1210. (JumpTargetOp(hp)^.ref^.symbol is TAsmLabel);
  1211. end;
  1212. procedure TAOptObj.RemoveDelaySlot(hp1:tai);
  1213. var
  1214. hp2: tai;
  1215. begin
  1216. hp2:=tai(hp1.next);
  1217. while assigned(hp2) and (hp2.typ in SkipInstr) do
  1218. hp2:=tai(hp2.next);
  1219. if assigned(hp2) and (hp2.typ=ait_instruction) and
  1220. (taicpu(hp2).opcode=A_NOP) then
  1221. begin
  1222. asml.remove(hp2);
  1223. hp2.free;
  1224. end;
  1225. { Anything except A_NOP must be left in place: these instructions
  1226. execute before branch, so code stays correct if branch is removed. }
  1227. end;
  1228. function TAOptObj.GetFinalDestination(hp: taicpu; level: longint): boolean;
  1229. {traces sucessive jumps to their final destination and sets it, e.g.
  1230. je l1 je l3
  1231. <code> <code>
  1232. l1: becomes l1:
  1233. je l2 je l3
  1234. <code> <code>
  1235. l2: l2:
  1236. jmp l3 jmp l3
  1237. the level parameter denotes how deeep we have already followed the jump,
  1238. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  1239. var p1: tai;
  1240. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM)}
  1241. p2: tai;
  1242. l: tasmlabel;
  1243. {$endif}
  1244. begin
  1245. GetfinalDestination := false;
  1246. if level > 20 then
  1247. exit;
  1248. p1 := getlabelwithsym(tasmlabel(JumpTargetOp(hp)^.ref^.symbol));
  1249. if assigned(p1) then
  1250. begin
  1251. SkipLabels(p1,p1);
  1252. if (tai(p1).typ = ait_instruction) and
  1253. (taicpu(p1).is_jmp) then
  1254. if { the next instruction after the label where the jump hp arrives}
  1255. { is unconditional or of the same type as hp, so continue }
  1256. IsJumpToLabelUncond(taicpu(p1))
  1257. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM)}
  1258. { for MIPS, it isn't enough to check the condition; first operands must be same, too. }
  1259. or
  1260. conditions_equal(taicpu(p1).condition,hp.condition) or
  1261. { the next instruction after the label where the jump hp arrives
  1262. is the opposite of hp (so this one is never taken), but after
  1263. that one there is a branch that will be taken, so perform a
  1264. little hack: set p1 equal to this instruction (that's what the
  1265. last SkipLabels is for, only works with short bool evaluation)}
  1266. (conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) and
  1267. SkipLabels(p1,p2) and
  1268. (p2.typ = ait_instruction) and
  1269. (taicpu(p2).is_jmp) and
  1270. (IsJumpToLabelUncond(taicpu(p2)) or
  1271. (conditions_equal(taicpu(p2).condition,hp.condition))) and
  1272. SkipLabels(p1,p1))
  1273. {$endif not MIPS and not RV64 and not RV32 and not JVM}
  1274. then
  1275. begin
  1276. { quick check for loops of the form "l5: ; jmp l5 }
  1277. if (tasmlabel(JumpTargetOp(taicpu(p1))^.ref^.symbol).labelnr =
  1278. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).labelnr) then
  1279. exit;
  1280. if not GetFinalDestination(taicpu(p1),succ(level)) then
  1281. exit;
  1282. {$if defined(aarch64)}
  1283. { can't have conditional branches to
  1284. global labels on AArch64, because the
  1285. offset may become too big }
  1286. if not(taicpu(hp).condition in [C_None,C_AL,C_NV]) and
  1287. (tasmlabel(JumpTargetOp(taicpu(p1))^.ref^.symbol).bind<>AB_LOCAL) then
  1288. exit;
  1289. {$endif aarch64}
  1290. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).decrefs;
  1291. JumpTargetOp(hp)^.ref^.symbol:=JumpTargetOp(taicpu(p1))^.ref^.symbol;
  1292. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).increfs;
  1293. end
  1294. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM)}
  1295. else
  1296. if conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) then
  1297. if not FindAnyLabel(p1,l) then
  1298. begin
  1299. {$ifdef finaldestdebug}
  1300. insertllitem(asml,p1,p1.next,tai_comment.Create(
  1301. strpnew('previous label inserted'))));
  1302. {$endif finaldestdebug}
  1303. current_asmdata.getjumplabel(l);
  1304. insertllitem(p1,p1.next,tai_label.Create(l));
  1305. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).decrefs;
  1306. JumpTargetOp(hp)^.ref^.symbol := l;
  1307. l.increfs;
  1308. { this won't work, since the new label isn't in the labeltable }
  1309. { so it will fail the rangecheck. Labeltable should become a }
  1310. { hashtable to support this: }
  1311. { GetFinalDestination(asml, hp); }
  1312. end
  1313. else
  1314. begin
  1315. {$ifdef finaldestdebug}
  1316. insertllitem(asml,p1,p1.next,tai_comment.Create(
  1317. strpnew('next label reused'))));
  1318. {$endif finaldestdebug}
  1319. l.increfs;
  1320. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).decrefs;
  1321. JumpTargetOp(hp)^.ref^.symbol := l;
  1322. if not GetFinalDestination(hp,succ(level)) then
  1323. exit;
  1324. end;
  1325. {$endif not MIPS and not RV64 and not RV32 and not JVM}
  1326. end;
  1327. GetFinalDestination := true;
  1328. end;
  1329. procedure TAOptObj.PrePeepHoleOpts;
  1330. var
  1331. p: tai;
  1332. begin
  1333. p := BlockStart;
  1334. ClearUsedRegs;
  1335. while (p <> BlockEnd) Do
  1336. begin
  1337. UpdateUsedRegs(tai(p.next));
  1338. if PrePeepHoleOptsCpu(p) then
  1339. continue;
  1340. if assigned(p) then
  1341. begin
  1342. UpdateUsedRegs(p);
  1343. p:=tai(p.next);
  1344. end;
  1345. end;
  1346. end;
  1347. procedure TAOptObj.PeepHoleOptPass1;
  1348. var
  1349. p,hp1,hp2 : tai;
  1350. stoploop:boolean;
  1351. begin
  1352. repeat
  1353. stoploop:=true;
  1354. p := BlockStart;
  1355. ClearUsedRegs;
  1356. while (p <> BlockEnd) Do
  1357. begin
  1358. { I'am not sure why this is done, UsedRegs should reflect the register usage before the instruction
  1359. If an instruction needs the information of this, it can easily create a TempUsedRegs (FK)
  1360. UpdateUsedRegs(tai(p.next));
  1361. }
  1362. {$ifdef DEBUG_OPTALLOC}
  1363. if p.Typ=ait_instruction then
  1364. InsertLLItem(tai(p.Previous),p,tai_comment.create(strpnew(GetAllocationString(UsedRegs))));
  1365. {$endif DEBUG_OPTALLOC}
  1366. if PeepHoleOptPass1Cpu(p) then
  1367. begin
  1368. stoploop:=false;
  1369. UpdateUsedRegs(p);
  1370. continue;
  1371. end;
  1372. case p.Typ Of
  1373. ait_instruction:
  1374. begin
  1375. { Handle Jmp Optimizations }
  1376. if taicpu(p).is_jmp then
  1377. begin
  1378. { the following if-block removes all code between a jmp and the next label,
  1379. because it can never be executed
  1380. }
  1381. if IsJumpToLabelUncond(taicpu(p)) then
  1382. begin
  1383. hp2:=p;
  1384. while GetNextInstruction(hp2, hp1) and
  1385. (hp1.typ <> ait_label)
  1386. {$ifdef JVM}
  1387. and (hp1.typ <> ait_jcatch)
  1388. {$endif}
  1389. do
  1390. if not(hp1.typ in ([ait_label]+skipinstr)) then
  1391. begin
  1392. if (hp1.typ = ait_instruction) and
  1393. taicpu(hp1).is_jmp and
  1394. (JumpTargetOp(taicpu(hp1))^.typ = top_ref) and
  1395. (JumpTargetOp(taicpu(hp1))^.ref^.symbol is TAsmLabel) then
  1396. TAsmLabel(JumpTargetOp(taicpu(hp1))^.ref^.symbol).decrefs;
  1397. { don't kill start/end of assembler block,
  1398. no-line-info-start/end etc }
  1399. if not(hp1.typ in [ait_align,ait_marker]) then
  1400. begin
  1401. {$ifdef cpudelayslot}
  1402. if (hp1.typ=ait_instruction) and (taicpu(hp1).is_jmp) then
  1403. RemoveDelaySlot(hp1);
  1404. {$endif cpudelayslot}
  1405. asml.remove(hp1);
  1406. hp1.free;
  1407. stoploop:=false;
  1408. end
  1409. else
  1410. hp2:=hp1;
  1411. end
  1412. else break;
  1413. end;
  1414. if GetNextInstruction(p, hp1) then
  1415. begin
  1416. SkipEntryExitMarker(hp1,hp1);
  1417. { remove unconditional jumps to a label coming right after them }
  1418. if IsJumpToLabelUncond(taicpu(p)) and
  1419. FindLabel(tasmlabel(JumpTargetOp(taicpu(p))^.ref^.symbol), hp1) and
  1420. { TODO: FIXME removing the first instruction fails}
  1421. (p<>blockstart) then
  1422. begin
  1423. tasmlabel(JumpTargetOp(taicpu(p))^.ref^.symbol).decrefs;
  1424. {$ifdef cpudelayslot}
  1425. RemoveDelaySlot(p);
  1426. {$endif cpudelayslot}
  1427. hp2:=tai(hp1.next);
  1428. asml.remove(p);
  1429. p.free;
  1430. p:=hp2;
  1431. stoploop:=false;
  1432. continue;
  1433. end
  1434. else if assigned(hp1) then
  1435. begin
  1436. { change the following jumps:
  1437. jmp<cond> lab_1 jmp<cond_inverted> lab_2
  1438. jmp lab_2 >>> <code>
  1439. lab_1: lab_2:
  1440. <code>
  1441. lab_2:
  1442. }
  1443. if hp1.typ = ait_label then
  1444. SkipLabels(hp1,hp1);
  1445. if (tai(hp1).typ=ait_instruction) and
  1446. IsJumpToLabelUncond(taicpu(hp1)) and
  1447. GetNextInstruction(hp1, hp2) and
  1448. IsJumpToLabel(taicpu(p)) and
  1449. FindLabel(tasmlabel(JumpTargetOp(taicpu(p))^.ref^.symbol), hp2) then
  1450. begin
  1451. if (taicpu(p).opcode=aopt_condjmp)
  1452. {$if defined(arm) or defined(aarch64)}
  1453. and (taicpu(p).condition<>C_None)
  1454. {$endif arm or aarch64}
  1455. {$if defined(aarch64)}
  1456. { can't have conditional branches to
  1457. global labels on AArch64, because the
  1458. offset may become too big }
  1459. and (tasmlabel(JumpTargetOp(taicpu(hp1))^.ref^.symbol).bind=AB_LOCAL)
  1460. {$endif aarch64}
  1461. then
  1462. begin
  1463. taicpu(p).condition:=inverse_cond(taicpu(p).condition);
  1464. tai_label(hp2).labsym.decrefs;
  1465. JumpTargetOp(taicpu(p))^.ref^.symbol:=JumpTargetOp(taicpu(hp1))^.ref^.symbol;
  1466. { when freeing hp1, the reference count
  1467. isn't decreased, so don't increase
  1468. taicpu(p).oper[0]^.ref^.symbol.increfs;
  1469. }
  1470. {$ifdef cpudelayslot}
  1471. RemoveDelaySlot(hp1);
  1472. {$endif cpudelayslot}
  1473. asml.remove(hp1);
  1474. hp1.free;
  1475. stoploop:=false;
  1476. GetFinalDestination(taicpu(p),0);
  1477. end
  1478. else
  1479. begin
  1480. GetFinalDestination(taicpu(p),0);
  1481. p:=tai(p.next);
  1482. continue;
  1483. end;
  1484. end
  1485. else if IsJumpToLabel(taicpu(p)) then
  1486. GetFinalDestination(taicpu(p),0);
  1487. end;
  1488. end;
  1489. end
  1490. else
  1491. { All other optimizes }
  1492. begin
  1493. end; { if is_jmp }
  1494. end;
  1495. end;
  1496. if assigned(p) then
  1497. begin
  1498. UpdateUsedRegs(p);
  1499. p:=tai(p.next);
  1500. end;
  1501. end;
  1502. until stoploop or not(cs_opt_level3 in current_settings.optimizerswitches);
  1503. end;
  1504. procedure TAOptObj.PeepHoleOptPass2;
  1505. var
  1506. p: tai;
  1507. begin
  1508. p := BlockStart;
  1509. ClearUsedRegs;
  1510. while (p <> BlockEnd) Do
  1511. begin
  1512. if PeepHoleOptPass2Cpu(p) then
  1513. continue;
  1514. if assigned(p) then
  1515. begin
  1516. UpdateUsedRegs(p);
  1517. p:=tai(p.next);
  1518. end;
  1519. end;
  1520. end;
  1521. procedure TAOptObj.PostPeepHoleOpts;
  1522. var
  1523. p: tai;
  1524. begin
  1525. p := BlockStart;
  1526. ClearUsedRegs;
  1527. while (p <> BlockEnd) Do
  1528. begin
  1529. UpdateUsedRegs(tai(p.next));
  1530. if PostPeepHoleOptsCpu(p) then
  1531. continue;
  1532. if assigned(p) then
  1533. begin
  1534. UpdateUsedRegs(p);
  1535. p:=tai(p.next);
  1536. end;
  1537. end;
  1538. end;
  1539. function TAOptObj.PrePeepHoleOptsCpu(var p : tai) : boolean;
  1540. begin
  1541. result := false;
  1542. end;
  1543. function TAOptObj.PeepHoleOptPass1Cpu(var p: tai): boolean;
  1544. begin
  1545. result := false;
  1546. end;
  1547. function TAOptObj.PeepHoleOptPass2Cpu(var p : tai) : boolean;
  1548. begin
  1549. result := false;
  1550. end;
  1551. function TAOptObj.PostPeepHoleOptsCpu(var p: tai): boolean;
  1552. begin
  1553. result := false;
  1554. end;
  1555. procedure TAOptObj.Debug_InsertInstrRegisterDependencyInfo;
  1556. var
  1557. p: tai;
  1558. ri: tregisterindex;
  1559. reg: TRegister;
  1560. commentstr: AnsiString;
  1561. registers_found: Boolean;
  1562. begin
  1563. p:=tai(AsmL.First);
  1564. while (p<>AsmL.Last) Do
  1565. begin
  1566. if p.typ=ait_instruction then
  1567. begin
  1568. {$ifdef x86}
  1569. taicpu(p).SetOperandOrder(op_att);
  1570. {$endif x86}
  1571. commentstr:='Instruction reads';
  1572. registers_found:=false;
  1573. for ri in tregisterindex do
  1574. begin
  1575. reg:=regnumber_table[ri];
  1576. if (reg<>NR_NO) and InstructionLoadsFromReg(reg,p) then
  1577. begin
  1578. commentstr:=commentstr+' '+std_regname(reg);
  1579. registers_found:=true;
  1580. end;
  1581. end;
  1582. if not registers_found then
  1583. commentstr:=commentstr+' no registers';
  1584. commentstr:=commentstr+' and writes new values in';
  1585. registers_found:=false;
  1586. for ri in tregisterindex do
  1587. begin
  1588. reg:=regnumber_table[ri];
  1589. if (reg<>NR_NO) and RegLoadedWithNewValue(reg,p) then
  1590. begin
  1591. commentstr:=commentstr+' '+std_regname(reg);
  1592. registers_found:=true;
  1593. end;
  1594. end;
  1595. if not registers_found then
  1596. commentstr:=commentstr+' no registers';
  1597. AsmL.InsertAfter(tai_comment.Create(strpnew(commentstr)),p);
  1598. end;
  1599. p:=tai(p.next);
  1600. end;
  1601. end;
  1602. End.