aoptobj.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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. else
  357. ;
  358. end;
  359. end;
  360. p := tai(p.next);
  361. end;
  362. until not(assigned(p)) or
  363. (not(p.typ in SkipInstr) and
  364. not((p.typ = ait_label) and
  365. labelCanBeSkipped(tai_label(p))));
  366. End;
  367. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  368. Begin
  369. IsUsed := (getregtype(Reg)=Typ) and (getsupreg(Reg) in UsedRegs);
  370. End;
  371. Function TUsedRegs.GetUsedRegs: TRegSet; inline;
  372. Begin
  373. GetUsedRegs := UsedRegs;
  374. End;
  375. procedure TUsedRegs.Dump(var t: text);
  376. var
  377. i: dword;
  378. begin
  379. write(t,Typ,' ');
  380. for i:=low(TRegSet) to high(TRegSet) do
  381. if i in UsedRegs then
  382. write(t,i,' ');
  383. writeln(t);
  384. end;
  385. Destructor TUsedRegs.Destroy;
  386. Begin
  387. inherited destroy;
  388. end;
  389. procedure TUsedRegs.Clear;
  390. begin
  391. UsedRegs := [];
  392. end;
  393. { ************************************************************************* }
  394. { **************************** TPaiProp *********************************** }
  395. { ************************************************************************* }
  396. Constructor TPaiProp.Create;
  397. Begin
  398. {!!!!!!
  399. UsedRegs.Init;
  400. CondRegs.init;
  401. }
  402. { DirFlag: TFlagContents; I386 specific}
  403. End;
  404. Function TPaiProp.RegInSequence(Reg, which: TRegister): Boolean;
  405. {
  406. Var p: Tai;
  407. RegsChecked: TRegSet;
  408. content: TContent;
  409. Counter: Byte;
  410. TmpResult: Boolean;
  411. }
  412. begin
  413. Result:=False; { unimplemented }
  414. (*!!!!!!!!!!1
  415. RegsChecked := [];
  416. content := regs[which];
  417. p := content.StartMod;
  418. TmpResult := False;
  419. Counter := 1;
  420. While Not(TmpResult) And
  421. (Counter <= Content.NrOfMods) Do
  422. Begin
  423. If IsLoadMemReg(p) Then
  424. With PInstr(p)^.oper[LoadSrc]^.ref^ Do
  425. If (Base = ProcInfo.FramePointer)
  426. {$ifdef cpurefshaveindexreg}
  427. And (Index = R_NO)
  428. {$endif cpurefshaveindexreg} Then
  429. Begin
  430. RegsChecked := RegsChecked +
  431. [RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg)];
  432. If Reg = RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg) Then
  433. Break;
  434. End
  435. Else
  436. Begin
  437. If (Base = Reg) And
  438. Not(Base In RegsChecked)
  439. Then TmpResult := True;
  440. {$ifdef cpurefshaveindexreg}
  441. If Not(TmpResult) And
  442. (Index = Reg) And
  443. Not(Index In RegsChecked)
  444. Then TmpResult := True;
  445. {$Endif cpurefshaveindexreg}
  446. End
  447. Else TmpResult := RegInInstruction(Reg, p);
  448. Inc(Counter);
  449. GetNextInstruction(p,p)
  450. End;
  451. RegInSequence := TmpResult
  452. *)
  453. End;
  454. Procedure TPaiProp.DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  455. TInstrSinceLastMod);
  456. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  457. { the contents of registers are loaded with a memory location based on Reg }
  458. {
  459. Var TmpWState, TmpRState: Byte;
  460. Counter: TRegister;
  461. }
  462. Begin
  463. {!!!!!!!
  464. Reg := RegMaxSize(Reg);
  465. If (Reg in [LoGPReg..HiGPReg]) Then
  466. For Counter := LoGPReg to HiGPReg Do
  467. With Regs[Counter] Do
  468. If (Counter = reg) Or
  469. ((Typ = Con_Ref) And
  470. RegInSequence(Reg, Counter)) Then
  471. Begin
  472. InstrSinceLastMod[Counter] := 0;
  473. IncWState(Counter);
  474. TmpWState := GetWState(Counter);
  475. TmpRState := GetRState(Counter);
  476. FillChar(Regs[Counter], SizeOf(TContent), 0);
  477. WState := TmpWState;
  478. RState := TmpRState
  479. End
  480. }
  481. End;
  482. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  483. Begin
  484. Result:=False; { unimplemented }
  485. (*!!!!!!!!!!
  486. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  487. {$ifdef refsHaveSegmentReg}
  488. (R1.Segment = R2.Segment) And
  489. {$endif}
  490. (R1.Base = R2.Base) And
  491. (R1.Symbol=R2.Symbol);
  492. *)
  493. End;
  494. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  495. var InstrSinceLastMod: TInstrSinceLastMod);
  496. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  497. { is the register whose contents are being written to memory (if this proc }
  498. { is called because of a "mov?? %reg, (mem)" instruction) }
  499. {
  500. Var RefsEq: TRefCompare;
  501. Counter: TRegister;
  502. }
  503. Begin
  504. (*!!!!!!!!!!!
  505. WhichReg := RegMaxSize(WhichReg);
  506. If (Ref.base = procinfo.FramePointer) or
  507. Assigned(Ref.Symbol) Then
  508. Begin
  509. If
  510. {$ifdef cpurefshaveindexreg}
  511. (Ref.Index = R_NO) And
  512. {$endif cpurefshaveindexreg}
  513. (Not(Assigned(Ref.Symbol)) or
  514. (Ref.base = R_NO)) Then
  515. { local variable which is not an array }
  516. RefsEq := @RefsEqual
  517. Else
  518. { local variable which is an array }
  519. RefsEq := @ArrayRefsEq;
  520. {write something to a parameter, a local or global variable, so
  521. * with uncertain optimizations on:
  522. - destroy the contents of registers whose contents have somewhere a
  523. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  524. are being written to memory) is not destroyed if it's StartMod is
  525. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  526. pointer or value based on Ref)
  527. * with uncertain optimizations off:
  528. - also destroy registers that contain any pointer}
  529. For Counter := LoGPReg to HiGPReg Do
  530. With Regs[Counter] Do
  531. Begin
  532. If (typ = Con_Ref) And
  533. ((Not(cs_opt_size in current_settings.optimizerswitches) And
  534. (NrOfMods <> 1)
  535. ) Or
  536. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  537. ((Counter <> WhichReg) Or
  538. ((NrOfMods <> 1) And
  539. {StarMod is always of the type ait_instruction}
  540. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  541. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  542. )
  543. )
  544. )
  545. )
  546. Then
  547. DestroyReg(Counter, InstrSinceLastMod)
  548. End
  549. End
  550. Else
  551. {write something to a pointer location, so
  552. * with uncertain optimzations on:
  553. - do not destroy registers which contain a local/global variable or a
  554. parameter, except if DestroyRefs is called because of a "movsl"
  555. * with uncertain optimzations off:
  556. - destroy every register which contains a memory location
  557. }
  558. For Counter := LoGPReg to HiGPReg Do
  559. With Regs[Counter] Do
  560. If (typ = Con_Ref) And
  561. (Not(cs_opt_size in current_settings.optimizerswitches) Or
  562. {$ifdef x86}
  563. {for movsl}
  564. (Ref.Base = R_EDI) Or
  565. {$endif}
  566. {don't destroy if reg contains a parameter, local or global variable}
  567. Not((NrOfMods = 1) And
  568. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  569. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  570. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  571. )
  572. )
  573. )
  574. Then DestroyReg(Counter, InstrSinceLastMod)
  575. *)
  576. End;
  577. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  578. {Var Counter: TRegister;}
  579. Begin {initializes/desrtoys all registers}
  580. (*!!!!!!!!!
  581. For Counter := LoGPReg To HiGPReg Do
  582. Begin
  583. ReadReg(Counter);
  584. DestroyReg(Counter, InstrSinceLastMod);
  585. End;
  586. CondRegs.Init;
  587. { FPURegs.Init; }
  588. *)
  589. End;
  590. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  591. TInstrSinceLastMod);
  592. Begin
  593. {!!!!!!!
  594. Case o.typ Of
  595. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  596. top_ref:
  597. Begin
  598. ReadRef(o.ref);
  599. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  600. End;
  601. top_symbol:;
  602. End;
  603. }
  604. End;
  605. Procedure TPaiProp.ReadReg(Reg: TRegister);
  606. Begin
  607. {!!!!!!!
  608. Reg := RegMaxSize(Reg);
  609. If Reg in General_Registers Then
  610. IncRState(RegMaxSize(Reg))
  611. }
  612. End;
  613. Procedure TPaiProp.ReadRef(Ref: PReference);
  614. Begin
  615. (*!!!!!!
  616. If Ref^.Base <> R_NO Then
  617. ReadReg(Ref^.Base);
  618. {$ifdef cpurefshaveindexreg}
  619. If Ref^.Index <> R_NO Then
  620. ReadReg(Ref^.Index);
  621. {$endif cpurefshaveindexreg}
  622. *)
  623. End;
  624. Procedure TPaiProp.ReadOp(const o:toper);
  625. Begin
  626. Case o.typ Of
  627. top_reg: ReadReg(o.reg);
  628. top_ref: ReadRef(o.ref);
  629. else
  630. internalerror(200410241);
  631. End;
  632. End;
  633. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  634. TInstrSinceLastMod);
  635. Begin
  636. (*!!!!!!!
  637. With Regs[reg] Do
  638. If (Typ = Con_Ref)
  639. Then
  640. Begin
  641. IncState(WState);
  642. {also store how many instructions are part of the sequence in the first
  643. instructions PPaiProp, so it can be easily accessed from within
  644. CheckSequence}
  645. Inc(NrOfMods, InstrSinceLastMod[Reg]);
  646. PPaiProp(StartMod.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  647. InstrSinceLastMod[Reg] := 0;
  648. End
  649. Else
  650. DestroyReg(Reg, InstrSinceLastMod);
  651. *)
  652. End;
  653. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  654. TInstrSinceLastMod);
  655. Begin
  656. If oper.typ = top_reg Then
  657. ModifyReg(RegMaxSize(oper.reg),InstrSinceLastMod)
  658. Else
  659. Begin
  660. ReadOp(oper);
  661. DestroyOp(oper, InstrSinceLastMod);
  662. End
  663. End;
  664. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  665. Begin
  666. //!!!! IncState(Regs[Reg].WState);
  667. End;
  668. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  669. Begin
  670. //!!!! IncState(Regs[Reg].RState);
  671. End;
  672. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  673. Begin
  674. Result:=0; { unimplemented }
  675. //!!!! GetWState := Regs[Reg].WState
  676. End;
  677. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  678. Begin
  679. Result:=0; { unimplemented }
  680. //!!!! GetRState := Regs[Reg].RState
  681. End;
  682. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  683. Begin
  684. Result:=0; { unimplemented }
  685. //!!!! GetRegContentType := Regs[Reg].typ
  686. End;
  687. Destructor TPaiProp.Done;
  688. Begin
  689. //!!!! UsedRegs.Done;
  690. //!!!! CondRegs.Done;
  691. { DirFlag: TFlagContents; I386 specific}
  692. End;
  693. { ************************ private TPaiProp stuff ************************* }
  694. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  695. Begin
  696. If s <> High(TStateInt) Then Inc(s)
  697. Else s := 0
  698. End;
  699. Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Tai;
  700. RefsEq: TRefCompare): Boolean;
  701. Var Count: AWord;
  702. TmpResult: Boolean;
  703. Begin
  704. TmpResult := False;
  705. If (p.typ = ait_instruction) Then
  706. Begin
  707. Count := 0;
  708. Repeat
  709. If (TInstr(p).oper[Count]^.typ = Top_Ref) Then
  710. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count]^.ref^);
  711. Inc(Count);
  712. Until (Count = max_operands) or TmpResult;
  713. End;
  714. RefInInstruction := TmpResult;
  715. End;
  716. Function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  717. RefsEq: TRefCompare): Boolean;
  718. Var p: Tai;
  719. Counter: Byte;
  720. TmpResult: Boolean;
  721. Begin
  722. p := Content.StartMod;
  723. TmpResult := False;
  724. Counter := 1;
  725. While Not(TmpResult) And
  726. (Counter <= Content.NrOfMods) Do
  727. Begin
  728. If (p.typ = ait_instruction) And
  729. RefInInstruction(Ref, p, @references_equal)
  730. Then TmpResult := True;
  731. Inc(Counter);
  732. GetNextInstruction(p,p)
  733. End;
  734. RefInSequence := TmpResult
  735. End;
  736. { ************************************************************************* }
  737. { ***************************** TAoptObj ********************************** }
  738. { ************************************************************************* }
  739. Constructor TAoptObj.create(_AsmL: TAsmList; _BlockStart, _BlockEnd: Tai;
  740. _LabelInfo: PLabelInfo);
  741. Begin
  742. AsmL := _AsmL;
  743. BlockStart := _BlockStart;
  744. BlockEnd := _BlockEnd;
  745. LabelInfo := _LabelInfo;
  746. CreateUsedRegs(UsedRegs);
  747. End;
  748. destructor TAOptObj.Destroy;
  749. var
  750. i : TRegisterType;
  751. begin
  752. for i:=low(TRegisterType) to high(TRegisterType) do
  753. UsedRegs[i].Destroy;
  754. inherited Destroy;
  755. end;
  756. procedure TAOptObj.CreateUsedRegs(var regs: TAllUsedRegs);
  757. var
  758. i : TRegisterType;
  759. begin
  760. for i:=low(TRegisterType) to high(TRegisterType) do
  761. Regs[i]:=TUsedRegs.Create(i);
  762. end;
  763. procedure TAOptObj.ClearUsedRegs;
  764. var
  765. i : TRegisterType;
  766. begin
  767. for i:=low(TRegisterType) to high(TRegisterType) do
  768. UsedRegs[i].Clear;
  769. end;
  770. procedure TAOptObj.UpdateUsedRegs(p : Tai);
  771. begin
  772. { this code is based on TUsedRegs.Update to avoid multiple passes through the asmlist,
  773. the code is duplicated here }
  774. repeat
  775. while assigned(p) and
  776. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  777. ((p.typ = ait_label) and
  778. labelCanBeSkipped(tai_label(p))) or
  779. ((p.typ = ait_marker) and
  780. (tai_Marker(p).Kind in [mark_AsmBlockEnd,mark_NoLineInfoStart,mark_NoLineInfoEnd]))) do
  781. p := tai(p.next);
  782. while assigned(p) and
  783. (p.typ=ait_RegAlloc) Do
  784. begin
  785. case tai_regalloc(p).ratype of
  786. ra_alloc :
  787. Include(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  788. ra_dealloc :
  789. Exclude(UsedRegs[getregtype(tai_regalloc(p).reg)].UsedRegs, getsupreg(tai_regalloc(p).reg));
  790. else
  791. ;
  792. end;
  793. p := tai(p.next);
  794. end;
  795. until not(assigned(p)) or
  796. (not(p.typ in SkipInstr) and
  797. not((p.typ = ait_label) and
  798. labelCanBeSkipped(tai_label(p))));
  799. end;
  800. class procedure TAOptObj.UpdateUsedRegs(var Regs : TAllUsedRegs;p : Tai);
  801. var
  802. i : TRegisterType;
  803. begin
  804. for i:=low(TRegisterType) to high(TRegisterType) do
  805. Regs[i].Update(p);
  806. end;
  807. function TAOptObj.CopyUsedRegs(var dest: TAllUsedRegs): boolean;
  808. var
  809. i : TRegisterType;
  810. begin
  811. Result:=true;
  812. for i:=low(TRegisterType) to high(TRegisterType) do
  813. dest[i]:=TUsedRegs.Create_Regset(i,UsedRegs[i].GetUsedRegs);
  814. end;
  815. procedure TAOptObj.RestoreUsedRegs(const Regs: TAllUsedRegs);
  816. var
  817. i : TRegisterType;
  818. begin
  819. { Note that the constructor Create_Regset is being called as a regular
  820. method - it is not instantiating a new object. This is because it is
  821. the only published means to modify the internal state en-masse. [Kit] }
  822. for i:=low(TRegisterType) to high(TRegisterType) do
  823. UsedRegs[i].Create_Regset(i,Regs[i].GetUsedRegs);
  824. end;
  825. procedure TAOptObj.TransferUsedRegs(var dest: TAllUsedRegs);
  826. var
  827. i : TRegisterType;
  828. begin
  829. { Note that the constructor Create_Regset is being called as a regular
  830. method - it is not instantiating a new object. This is because it is
  831. the only published means to modify the internal state en-masse. [Kit] }
  832. for i:=low(TRegisterType) to high(TRegisterType) do
  833. dest[i].Create_Regset(i, UsedRegs[i].GetUsedRegs);
  834. end;
  835. class procedure TAOptObj.ReleaseUsedRegs(const regs: TAllUsedRegs);
  836. var
  837. i : TRegisterType;
  838. begin
  839. for i:=low(TRegisterType) to high(TRegisterType) do
  840. regs[i].Free;
  841. end;
  842. class Function TAOptObj.RegInUsedRegs(reg : TRegister;regs : TAllUsedRegs) : boolean;
  843. begin
  844. result:=regs[getregtype(reg)].IsUsed(reg);
  845. end;
  846. class procedure TAOptObj.IncludeRegInUsedRegs(reg: TRegister;
  847. var regs: TAllUsedRegs);
  848. begin
  849. include(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  850. end;
  851. class procedure TAOptObj.ExcludeRegFromUsedRegs(reg: TRegister;
  852. var regs: TAllUsedRegs);
  853. begin
  854. exclude(regs[getregtype(reg)].UsedRegs,getsupreg(Reg));
  855. end;
  856. function TAOptObj.GetAllocationString(const regs: TAllUsedRegs): string;
  857. var
  858. i : TRegisterType;
  859. j : TSuperRegister;
  860. begin
  861. Result:='';
  862. for i:=low(TRegisterType) to high(TRegisterType) do
  863. for j in regs[i].UsedRegs do
  864. Result:=Result+std_regname(newreg(i,j,R_SUBWHOLE))+' ';
  865. end;
  866. Function TAOptObj.FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  867. Var TempP: Tai;
  868. Begin
  869. TempP := hp;
  870. While Assigned(TempP) and
  871. (TempP.typ In SkipInstr + [ait_label,ait_align]) Do
  872. If (TempP.typ <> ait_Label) Or
  873. (Tai_label(TempP).labsym <> L)
  874. Then GetNextInstruction(TempP, TempP)
  875. Else
  876. Begin
  877. hp := TempP;
  878. FindLabel := True;
  879. exit
  880. End;
  881. FindLabel := False;
  882. End;
  883. Procedure TAOptObj.InsertLLItem(prev, foll, new_one : TLinkedListItem);
  884. Begin
  885. If Assigned(prev) Then
  886. If Assigned(foll) Then
  887. Begin
  888. If Assigned(new_one) Then
  889. Begin
  890. new_one.previous := prev;
  891. new_one.next := foll;
  892. prev.next := new_one;
  893. foll.previous := new_one;
  894. { should we update line information? }
  895. if (not (tai(new_one).typ in SkipLineInfo)) and
  896. (not (tai(foll).typ in SkipLineInfo)) then
  897. Tailineinfo(new_one).fileinfo := Tailineinfo(foll).fileinfo
  898. End
  899. End
  900. Else AsmL.Concat(new_one)
  901. Else If Assigned(Foll) Then AsmL.Insert(new_one)
  902. End;
  903. Function TAOptObj.SkipHead(P: Tai): Tai;
  904. Var OldP: Tai;
  905. Begin
  906. Repeat
  907. OldP := P;
  908. If (P.typ in SkipInstr) Or
  909. ((P.typ = ait_marker) And
  910. (Tai_Marker(P).Kind = mark_AsmBlockEnd)) Then
  911. GetNextInstruction(P, P)
  912. Else If ((P.Typ = Ait_Marker) And
  913. (Tai_Marker(P).Kind = mark_NoPropInfoStart)) Then
  914. { a marker of the type mark_NoPropInfoStart can't be the first instruction of a }
  915. { paasmoutput list }
  916. GetNextInstruction(Tai(P.Previous),P);
  917. If (P.Typ = Ait_Marker) And
  918. (Tai_Marker(P).Kind = mark_AsmBlockStart) Then
  919. Begin
  920. P := Tai(P.Next);
  921. While (P.typ <> Ait_Marker) Or
  922. (Tai_Marker(P).Kind <> mark_AsmBlockEnd) Do
  923. P := Tai(P.Next)
  924. End;
  925. Until P = OldP;
  926. SkipHead := P;
  927. End;
  928. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  929. Begin
  930. if o1.typ=o2.typ then
  931. Case o1.typ Of
  932. Top_Reg :
  933. OpsEqual:=o1.reg=o2.reg;
  934. Top_Ref :
  935. OpsEqual:=
  936. references_equal(o1.ref^, o2.ref^) and
  937. (o1.ref^.volatility=[]) and
  938. (o2.ref^.volatility=[]);
  939. Top_Const :
  940. OpsEqual:=o1.val=o2.val;
  941. Top_None :
  942. OpsEqual := True
  943. else OpsEqual := False
  944. End
  945. else
  946. OpsEqual := False;
  947. End;
  948. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  949. Begin
  950. Result:=nil;
  951. Repeat
  952. While Assigned(StartPai) And
  953. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  954. {$ifdef cpudelayslot}
  955. ((startpai.typ=ait_instruction) and (taicpu(startpai).opcode=A_NOP)) or
  956. {$endif cpudelayslot}
  957. ((StartPai.typ = ait_label) and
  958. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  959. StartPai := Tai(StartPai.Next);
  960. If Assigned(StartPai) And
  961. (StartPai.typ = ait_regAlloc) Then
  962. Begin
  963. if (tai_regalloc(StartPai).ratype=ra_alloc) and
  964. (getregtype(tai_regalloc(StartPai).Reg) = getregtype(Reg)) and
  965. (getsupreg(tai_regalloc(StartPai).Reg) = getsupreg(Reg)) then
  966. begin
  967. Result:=tai_regalloc(StartPai);
  968. exit;
  969. end;
  970. StartPai := Tai(StartPai.Next);
  971. End
  972. else
  973. exit;
  974. Until false;
  975. End;
  976. Function TAOptObj.FindRegAllocBackward(Reg: TRegister; StartPai: Tai): tai_regalloc;
  977. Begin
  978. Result:=nil;
  979. Repeat
  980. While Assigned(StartPai) And
  981. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  982. ((StartPai.typ = ait_label) and
  983. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  984. StartPai := Tai(StartPai.Previous);
  985. If Assigned(StartPai) And
  986. (StartPai.typ = ait_regAlloc) Then
  987. Begin
  988. if (tai_regalloc(StartPai).ratype=ra_alloc) and
  989. SuperRegistersEqual(tai_regalloc(StartPai).Reg,Reg) then
  990. begin
  991. Result:=tai_regalloc(StartPai);
  992. exit;
  993. end;
  994. StartPai := Tai(StartPai.Previous);
  995. End
  996. else
  997. exit;
  998. Until false;
  999. End;
  1000. function TAOptObj.FindRegDeAlloc(Reg: TRegister; StartPai: Tai): tai_regalloc;
  1001. Begin
  1002. Result:=nil;
  1003. Repeat
  1004. While Assigned(StartPai) And
  1005. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  1006. ((StartPai.typ = ait_label) and
  1007. Not(Tai_Label(StartPai).labsym.Is_Used))) Do
  1008. StartPai := Tai(StartPai.Next);
  1009. If Assigned(StartPai) And
  1010. (StartPai.typ = ait_regAlloc) Then
  1011. Begin
  1012. if (tai_regalloc(StartPai).ratype=ra_dealloc) and
  1013. (getregtype(tai_regalloc(StartPai).Reg) = getregtype(Reg)) and
  1014. (getsupreg(tai_regalloc(StartPai).Reg) = getsupreg(Reg)) then
  1015. begin
  1016. Result:=tai_regalloc(StartPai);
  1017. exit;
  1018. end;
  1019. StartPai := Tai(StartPai.Next);
  1020. End
  1021. else
  1022. exit;
  1023. Until false;
  1024. End;
  1025. { allocates register reg between (and including) instructions p1 and p2
  1026. the type of p1 and p2 must not be in SkipInstr }
  1027. procedure TAOptObj.AllocRegBetween(reg: tregister; p1, p2: tai; var initialusedregs: TAllUsedRegs);
  1028. var
  1029. hp, start: tai;
  1030. removedsomething,
  1031. firstRemovedWasAlloc,
  1032. lastRemovedWasDealloc: boolean;
  1033. begin
  1034. {$ifdef EXTDEBUG}
  1035. { if assigned(p1.optinfo) and
  1036. (ptaiprop(p1.optinfo)^.usedregs <> initialusedregs) then
  1037. internalerror(2004101010); }
  1038. {$endif EXTDEBUG}
  1039. start := p1;
  1040. if (reg = NR_STACK_POINTER_REG) or
  1041. (reg = current_procinfo.framepointer) or
  1042. not(assigned(p1)) then
  1043. { this happens with registers which are loaded implicitely, outside the }
  1044. { current block (e.g. esi with self) }
  1045. exit;
  1046. { make sure we allocate it for this instruction }
  1047. getnextinstruction(p2,p2);
  1048. lastRemovedWasDealloc := false;
  1049. removedSomething := false;
  1050. firstRemovedWasAlloc := false;
  1051. {$ifdef allocregdebug}
  1052. hp := tai_comment.Create(strpnew('allocating '+std_regname(newreg(R_INTREGISTER,supreg,R_SUBWHOLE))+
  1053. ' from here...'));
  1054. insertllitem(asml,p1.previous,p1,hp);
  1055. hp := tai_comment.Create(strpnew('allocated '+std_regname(newreg(R_INTREGISTER,supreg,R_SUBWHOLE))+
  1056. ' till here...'));
  1057. insertllitem(asml,p2,p2.next,hp);
  1058. {$endif allocregdebug}
  1059. { do it the safe way: always allocate the full super register,
  1060. as we do no register re-allocation in the peephole optimizer,
  1061. this does not hurt
  1062. }
  1063. case getregtype(reg) of
  1064. R_MMREGISTER:
  1065. reg:=newreg(R_MMREGISTER,getsupreg(reg),R_SUBMMWHOLE);
  1066. R_INTREGISTER:
  1067. reg:=newreg(R_INTREGISTER,getsupreg(reg),R_SUBWHOLE);
  1068. R_FPUREGISTER:
  1069. reg:=newreg(R_FPUREGISTER,getsupreg(reg),R_SUBWHOLE);
  1070. R_ADDRESSREGISTER:
  1071. reg:=newreg(R_ADDRESSREGISTER,getsupreg(reg),R_SUBWHOLE);
  1072. else
  1073. Internalerror(2018030701);
  1074. end;
  1075. if not(RegInUsedRegs(reg,initialusedregs)) then
  1076. begin
  1077. hp := tai_regalloc.alloc(reg,nil);
  1078. insertllItem(p1.previous,p1,hp);
  1079. IncludeRegInUsedRegs(reg,initialusedregs);
  1080. end;
  1081. while assigned(p1) and
  1082. (p1 <> p2) do
  1083. begin
  1084. if assigned(p1.optinfo) then
  1085. internalerror(2014022301); // IncludeRegInUsedRegs(reg,ptaiprop(p1.optinfo)^.usedregs);
  1086. p1 := tai(p1.next);
  1087. repeat
  1088. while assigned(p1) and
  1089. (p1.typ in (SkipInstr-[ait_regalloc])) Do
  1090. p1 := tai(p1.next);
  1091. { remove all allocation/deallocation info about the register in between }
  1092. if assigned(p1) and
  1093. (p1.typ = ait_regalloc) then
  1094. begin
  1095. { same super register, different sub register? }
  1096. if SuperRegistersEqual(reg,tai_regalloc(p1).reg) and (tai_regalloc(p1).reg<>reg) then
  1097. begin
  1098. if (getsubreg(tai_regalloc(p1).reg)>getsubreg(reg)) or (getsubreg(reg)=R_SUBH) then
  1099. internalerror(2016101501);
  1100. tai_regalloc(p1).reg:=reg;
  1101. end;
  1102. if tai_regalloc(p1).reg=reg then
  1103. begin
  1104. if not removedSomething then
  1105. begin
  1106. firstRemovedWasAlloc := tai_regalloc(p1).ratype=ra_alloc;
  1107. removedSomething := true;
  1108. end;
  1109. lastRemovedWasDealloc := (tai_regalloc(p1).ratype=ra_dealloc);
  1110. hp := tai(p1.Next);
  1111. asml.Remove(p1);
  1112. p1.free;
  1113. p1 := hp;
  1114. end
  1115. else
  1116. p1 := tai(p1.next);
  1117. end;
  1118. until not(assigned(p1)) or
  1119. not(p1.typ in SkipInstr);
  1120. end;
  1121. if assigned(p1) then
  1122. begin
  1123. if firstRemovedWasAlloc then
  1124. begin
  1125. hp := tai_regalloc.Alloc(reg,nil);
  1126. insertLLItem(start.previous,start,hp);
  1127. end;
  1128. if lastRemovedWasDealloc then
  1129. begin
  1130. hp := tai_regalloc.DeAlloc(reg,nil);
  1131. insertLLItem(p1.previous,p1,hp);
  1132. end;
  1133. end;
  1134. end;
  1135. function TAOptObj.RegUsedAfterInstruction(reg: Tregister; p: tai;var AllUsedRegs: TAllUsedRegs): Boolean;
  1136. begin
  1137. AllUsedRegs[getregtype(reg)].Update(tai(p.Next),true);
  1138. RegUsedAfterInstruction :=
  1139. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  1140. not(regLoadedWithNewValue(reg,p)) and
  1141. (
  1142. not(GetNextInstruction(p,p)) or
  1143. InstructionLoadsFromReg(reg,p) or
  1144. not(regLoadedWithNewValue(reg,p))
  1145. );
  1146. end;
  1147. function TAOptObj.RegEndOfLife(reg : TRegister;p : taicpu) : boolean;
  1148. begin
  1149. Result:=assigned(FindRegDealloc(reg,tai(p.Next))) or
  1150. RegLoadedWithNewValue(reg,p);
  1151. end;
  1152. function TAOptObj.RemoveCurrentP(var p : tai) : boolean;
  1153. var
  1154. hp1 : tai;
  1155. begin
  1156. result:=GetNextInstruction(p,hp1);
  1157. { p will be removed, update used register as we continue
  1158. with the next instruction after p }
  1159. UpdateUsedRegs(tai(p.Next));
  1160. AsmL.Remove(p);
  1161. p.Free;
  1162. p:=hp1;
  1163. end;
  1164. function FindAnyLabel(hp: tai; var l: tasmlabel): Boolean;
  1165. begin
  1166. FindAnyLabel := false;
  1167. while assigned(hp.next) and
  1168. (tai(hp.next).typ in (SkipInstr+[ait_align])) Do
  1169. hp := tai(hp.next);
  1170. if assigned(hp.next) and
  1171. (tai(hp.next).typ = ait_label) then
  1172. begin
  1173. FindAnyLabel := true;
  1174. l := tai_label(hp.next).labsym;
  1175. end
  1176. end;
  1177. {$push}
  1178. {$r-}
  1179. function TAOptObj.getlabelwithsym(sym: tasmlabel): tai;
  1180. begin
  1181. if (int64(sym.labelnr) >= int64(labelinfo^.lowlabel)) and
  1182. (int64(sym.labelnr) <= int64(labelinfo^.highlabel)) then { range check, a jump can go past an assembler block! }
  1183. getlabelwithsym := labelinfo^.labeltable^[sym.labelnr-labelinfo^.lowlabel].paiobj
  1184. else
  1185. getlabelwithsym := nil;
  1186. end;
  1187. {$pop}
  1188. { Returns True if hp is an unconditional jump to a label }
  1189. function IsJumpToLabelUncond(hp: taicpu): boolean;
  1190. begin
  1191. {$if defined(avr)}
  1192. result:=(hp.opcode in aopt_uncondjmp) and
  1193. {$else avr}
  1194. result:=(hp.opcode=aopt_uncondjmp) and
  1195. {$endif avr}
  1196. {$if defined(arm) or defined(aarch64)}
  1197. (hp.condition=c_None) and
  1198. {$endif arm or aarch64}
  1199. {$if defined(riscv32) or defined(riscv64)}
  1200. (hp.ops>0) and
  1201. (hp.oper[0]^.reg=NR_X0) and
  1202. {$else riscv}
  1203. (hp.ops>0) and
  1204. {$endif riscv}
  1205. (JumpTargetOp(hp)^.typ = top_ref) and
  1206. (JumpTargetOp(hp)^.ref^.symbol is TAsmLabel);
  1207. end;
  1208. { Returns True if hp is any jump to a label }
  1209. function IsJumpToLabel(hp: taicpu): boolean;
  1210. begin
  1211. result:=hp.is_jmp and
  1212. (hp.ops>0) and
  1213. (JumpTargetOp(hp)^.typ = top_ref) and
  1214. (JumpTargetOp(hp)^.ref^.symbol is TAsmLabel);
  1215. end;
  1216. procedure TAOptObj.RemoveDelaySlot(hp1:tai);
  1217. var
  1218. hp2: tai;
  1219. begin
  1220. hp2:=tai(hp1.next);
  1221. while assigned(hp2) and (hp2.typ in SkipInstr) do
  1222. hp2:=tai(hp2.next);
  1223. if assigned(hp2) and (hp2.typ=ait_instruction) and
  1224. (taicpu(hp2).opcode=A_NOP) then
  1225. begin
  1226. asml.remove(hp2);
  1227. hp2.free;
  1228. end;
  1229. { Anything except A_NOP must be left in place: these instructions
  1230. execute before branch, so code stays correct if branch is removed. }
  1231. end;
  1232. function TAOptObj.GetFinalDestination(hp: taicpu; level: longint): boolean;
  1233. {traces sucessive jumps to their final destination and sets it, e.g.
  1234. je l1 je l3
  1235. <code> <code>
  1236. l1: becomes l1:
  1237. je l2 je l3
  1238. <code> <code>
  1239. l2: l2:
  1240. jmp l3 jmp l3
  1241. the level parameter denotes how deeep we have already followed the jump,
  1242. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  1243. var p1: tai;
  1244. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM)}
  1245. p2: tai;
  1246. l: tasmlabel;
  1247. {$endif}
  1248. begin
  1249. GetfinalDestination := false;
  1250. if level > 20 then
  1251. exit;
  1252. p1 := getlabelwithsym(tasmlabel(JumpTargetOp(hp)^.ref^.symbol));
  1253. if assigned(p1) then
  1254. begin
  1255. SkipLabels(p1,p1);
  1256. if (tai(p1).typ = ait_instruction) and
  1257. (taicpu(p1).is_jmp) then
  1258. if { the next instruction after the label where the jump hp arrives}
  1259. { is unconditional or of the same type as hp, so continue }
  1260. IsJumpToLabelUncond(taicpu(p1))
  1261. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM)}
  1262. { for MIPS, it isn't enough to check the condition; first operands must be same, too. }
  1263. or
  1264. conditions_equal(taicpu(p1).condition,hp.condition) or
  1265. { the next instruction after the label where the jump hp arrives
  1266. is the opposite of hp (so this one is never taken), but after
  1267. that one there is a branch that will be taken, so perform a
  1268. little hack: set p1 equal to this instruction (that's what the
  1269. last SkipLabels is for, only works with short bool evaluation)}
  1270. (conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) and
  1271. SkipLabels(p1,p2) and
  1272. (p2.typ = ait_instruction) and
  1273. (taicpu(p2).is_jmp) and
  1274. (IsJumpToLabelUncond(taicpu(p2)) or
  1275. (conditions_equal(taicpu(p2).condition,hp.condition))) and
  1276. SkipLabels(p1,p1))
  1277. {$endif not MIPS and not RV64 and not RV32 and not JVM}
  1278. then
  1279. begin
  1280. { quick check for loops of the form "l5: ; jmp l5 }
  1281. if (tasmlabel(JumpTargetOp(taicpu(p1))^.ref^.symbol).labelnr =
  1282. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).labelnr) then
  1283. exit;
  1284. if not GetFinalDestination(taicpu(p1),succ(level)) then
  1285. exit;
  1286. {$if defined(aarch64)}
  1287. { can't have conditional branches to
  1288. global labels on AArch64, because the
  1289. offset may become too big }
  1290. if not(taicpu(hp).condition in [C_None,C_AL,C_NV]) and
  1291. (tasmlabel(JumpTargetOp(taicpu(p1))^.ref^.symbol).bind<>AB_LOCAL) then
  1292. exit;
  1293. {$endif aarch64}
  1294. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).decrefs;
  1295. JumpTargetOp(hp)^.ref^.symbol:=JumpTargetOp(taicpu(p1))^.ref^.symbol;
  1296. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).increfs;
  1297. end
  1298. {$if not defined(MIPS) and not defined(riscv64) and not defined(riscv32) and not defined(JVM)}
  1299. else
  1300. if conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) then
  1301. if not FindAnyLabel(p1,l) then
  1302. begin
  1303. {$ifdef finaldestdebug}
  1304. insertllitem(asml,p1,p1.next,tai_comment.Create(
  1305. strpnew('previous label inserted'))));
  1306. {$endif finaldestdebug}
  1307. current_asmdata.getjumplabel(l);
  1308. insertllitem(p1,p1.next,tai_label.Create(l));
  1309. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).decrefs;
  1310. JumpTargetOp(hp)^.ref^.symbol := l;
  1311. l.increfs;
  1312. { this won't work, since the new label isn't in the labeltable }
  1313. { so it will fail the rangecheck. Labeltable should become a }
  1314. { hashtable to support this: }
  1315. { GetFinalDestination(asml, hp); }
  1316. end
  1317. else
  1318. begin
  1319. {$ifdef finaldestdebug}
  1320. insertllitem(asml,p1,p1.next,tai_comment.Create(
  1321. strpnew('next label reused'))));
  1322. {$endif finaldestdebug}
  1323. l.increfs;
  1324. tasmlabel(JumpTargetOp(hp)^.ref^.symbol).decrefs;
  1325. JumpTargetOp(hp)^.ref^.symbol := l;
  1326. if not GetFinalDestination(hp,succ(level)) then
  1327. exit;
  1328. end;
  1329. {$endif not MIPS and not RV64 and not RV32 and not JVM}
  1330. end;
  1331. GetFinalDestination := true;
  1332. end;
  1333. procedure TAOptObj.PrePeepHoleOpts;
  1334. var
  1335. p: tai;
  1336. begin
  1337. p := BlockStart;
  1338. ClearUsedRegs;
  1339. while (p <> BlockEnd) Do
  1340. begin
  1341. UpdateUsedRegs(tai(p.next));
  1342. if PrePeepHoleOptsCpu(p) then
  1343. continue;
  1344. if assigned(p) then
  1345. begin
  1346. UpdateUsedRegs(p);
  1347. p:=tai(p.next);
  1348. end;
  1349. end;
  1350. end;
  1351. procedure TAOptObj.PeepHoleOptPass1;
  1352. var
  1353. p,hp1,hp2 : tai;
  1354. stoploop:boolean;
  1355. begin
  1356. repeat
  1357. stoploop:=true;
  1358. p := BlockStart;
  1359. ClearUsedRegs;
  1360. while (p <> BlockEnd) Do
  1361. begin
  1362. { I'am not sure why this is done, UsedRegs should reflect the register usage before the instruction
  1363. If an instruction needs the information of this, it can easily create a TempUsedRegs (FK)
  1364. UpdateUsedRegs(tai(p.next));
  1365. }
  1366. {$ifdef DEBUG_OPTALLOC}
  1367. if p.Typ=ait_instruction then
  1368. InsertLLItem(tai(p.Previous),p,tai_comment.create(strpnew(GetAllocationString(UsedRegs))));
  1369. {$endif DEBUG_OPTALLOC}
  1370. if PeepHoleOptPass1Cpu(p) then
  1371. begin
  1372. stoploop:=false;
  1373. UpdateUsedRegs(p);
  1374. continue;
  1375. end;
  1376. case p.Typ Of
  1377. ait_instruction:
  1378. begin
  1379. { Handle Jmp Optimizations }
  1380. if taicpu(p).is_jmp then
  1381. begin
  1382. { the following if-block removes all code between a jmp and the next label,
  1383. because it can never be executed
  1384. }
  1385. if IsJumpToLabelUncond(taicpu(p)) then
  1386. begin
  1387. hp2:=p;
  1388. while GetNextInstruction(hp2, hp1) and
  1389. (hp1.typ <> ait_label)
  1390. {$ifdef JVM}
  1391. and (hp1.typ <> ait_jcatch)
  1392. {$endif}
  1393. do
  1394. if not(hp1.typ in ([ait_label]+skipinstr)) then
  1395. begin
  1396. if (hp1.typ = ait_instruction) and
  1397. taicpu(hp1).is_jmp and
  1398. (JumpTargetOp(taicpu(hp1))^.typ = top_ref) and
  1399. (JumpTargetOp(taicpu(hp1))^.ref^.symbol is TAsmLabel) then
  1400. TAsmLabel(JumpTargetOp(taicpu(hp1))^.ref^.symbol).decrefs;
  1401. { don't kill start/end of assembler block,
  1402. no-line-info-start/end etc }
  1403. if not(hp1.typ in [ait_align,ait_marker]) then
  1404. begin
  1405. {$ifdef cpudelayslot}
  1406. if (hp1.typ=ait_instruction) and (taicpu(hp1).is_jmp) then
  1407. RemoveDelaySlot(hp1);
  1408. {$endif cpudelayslot}
  1409. asml.remove(hp1);
  1410. hp1.free;
  1411. stoploop:=false;
  1412. end
  1413. else
  1414. hp2:=hp1;
  1415. end
  1416. else break;
  1417. end;
  1418. if GetNextInstruction(p, hp1) then
  1419. begin
  1420. SkipEntryExitMarker(hp1,hp1);
  1421. { remove unconditional jumps to a label coming right after them }
  1422. if IsJumpToLabelUncond(taicpu(p)) and
  1423. FindLabel(tasmlabel(JumpTargetOp(taicpu(p))^.ref^.symbol), hp1) and
  1424. { TODO: FIXME removing the first instruction fails}
  1425. (p<>blockstart) then
  1426. begin
  1427. tasmlabel(JumpTargetOp(taicpu(p))^.ref^.symbol).decrefs;
  1428. {$ifdef cpudelayslot}
  1429. RemoveDelaySlot(p);
  1430. {$endif cpudelayslot}
  1431. hp2:=tai(hp1.next);
  1432. asml.remove(p);
  1433. p.free;
  1434. p:=hp2;
  1435. stoploop:=false;
  1436. continue;
  1437. end
  1438. else if assigned(hp1) then
  1439. begin
  1440. { change the following jumps:
  1441. jmp<cond> lab_1 jmp<cond_inverted> lab_2
  1442. jmp lab_2 >>> <code>
  1443. lab_1: lab_2:
  1444. <code>
  1445. lab_2:
  1446. }
  1447. if hp1.typ = ait_label then
  1448. SkipLabels(hp1,hp1);
  1449. if (tai(hp1).typ=ait_instruction) and
  1450. IsJumpToLabelUncond(taicpu(hp1)) and
  1451. GetNextInstruction(hp1, hp2) and
  1452. IsJumpToLabel(taicpu(p)) and
  1453. FindLabel(tasmlabel(JumpTargetOp(taicpu(p))^.ref^.symbol), hp2) then
  1454. begin
  1455. if (taicpu(p).opcode=aopt_condjmp)
  1456. {$if defined(arm) or defined(aarch64)}
  1457. and (taicpu(p).condition<>C_None)
  1458. {$endif arm or aarch64}
  1459. {$if defined(aarch64)}
  1460. { can't have conditional branches to
  1461. global labels on AArch64, because the
  1462. offset may become too big }
  1463. and (tasmlabel(JumpTargetOp(taicpu(hp1))^.ref^.symbol).bind=AB_LOCAL)
  1464. {$endif aarch64}
  1465. then
  1466. begin
  1467. taicpu(p).condition:=inverse_cond(taicpu(p).condition);
  1468. tai_label(hp2).labsym.decrefs;
  1469. JumpTargetOp(taicpu(p))^.ref^.symbol:=JumpTargetOp(taicpu(hp1))^.ref^.symbol;
  1470. { when freeing hp1, the reference count
  1471. isn't decreased, so don't increase
  1472. taicpu(p).oper[0]^.ref^.symbol.increfs;
  1473. }
  1474. {$ifdef cpudelayslot}
  1475. RemoveDelaySlot(hp1);
  1476. {$endif cpudelayslot}
  1477. asml.remove(hp1);
  1478. hp1.free;
  1479. stoploop:=false;
  1480. GetFinalDestination(taicpu(p),0);
  1481. end
  1482. else
  1483. begin
  1484. GetFinalDestination(taicpu(p),0);
  1485. p:=tai(p.next);
  1486. continue;
  1487. end;
  1488. end
  1489. else if IsJumpToLabel(taicpu(p)) then
  1490. GetFinalDestination(taicpu(p),0);
  1491. end;
  1492. end;
  1493. end
  1494. else
  1495. { All other optimizes }
  1496. begin
  1497. end; { if is_jmp }
  1498. end;
  1499. else
  1500. ;
  1501. end;
  1502. if assigned(p) then
  1503. begin
  1504. UpdateUsedRegs(p);
  1505. p:=tai(p.next);
  1506. end;
  1507. end;
  1508. until stoploop or not(cs_opt_level3 in current_settings.optimizerswitches);
  1509. end;
  1510. procedure TAOptObj.PeepHoleOptPass2;
  1511. var
  1512. p: tai;
  1513. begin
  1514. p := BlockStart;
  1515. ClearUsedRegs;
  1516. while (p <> BlockEnd) Do
  1517. begin
  1518. if PeepHoleOptPass2Cpu(p) then
  1519. continue;
  1520. if assigned(p) then
  1521. begin
  1522. UpdateUsedRegs(p);
  1523. p:=tai(p.next);
  1524. end;
  1525. end;
  1526. end;
  1527. procedure TAOptObj.PostPeepHoleOpts;
  1528. var
  1529. p: tai;
  1530. begin
  1531. p := BlockStart;
  1532. ClearUsedRegs;
  1533. while (p <> BlockEnd) Do
  1534. begin
  1535. UpdateUsedRegs(tai(p.next));
  1536. if PostPeepHoleOptsCpu(p) then
  1537. continue;
  1538. if assigned(p) then
  1539. begin
  1540. UpdateUsedRegs(p);
  1541. p:=tai(p.next);
  1542. end;
  1543. end;
  1544. end;
  1545. function TAOptObj.PrePeepHoleOptsCpu(var p : tai) : boolean;
  1546. begin
  1547. result := false;
  1548. end;
  1549. function TAOptObj.PeepHoleOptPass1Cpu(var p: tai): boolean;
  1550. begin
  1551. result := false;
  1552. end;
  1553. function TAOptObj.PeepHoleOptPass2Cpu(var p : tai) : boolean;
  1554. begin
  1555. result := false;
  1556. end;
  1557. function TAOptObj.PostPeepHoleOptsCpu(var p: tai): boolean;
  1558. begin
  1559. result := false;
  1560. end;
  1561. procedure TAOptObj.Debug_InsertInstrRegisterDependencyInfo;
  1562. var
  1563. p: tai;
  1564. ri: tregisterindex;
  1565. reg: TRegister;
  1566. commentstr: AnsiString;
  1567. registers_found: Boolean;
  1568. begin
  1569. p:=tai(AsmL.First);
  1570. while (p<>AsmL.Last) Do
  1571. begin
  1572. if p.typ=ait_instruction then
  1573. begin
  1574. {$ifdef x86}
  1575. taicpu(p).SetOperandOrder(op_att);
  1576. {$endif x86}
  1577. commentstr:='Instruction reads';
  1578. registers_found:=false;
  1579. for ri in tregisterindex do
  1580. begin
  1581. reg:=regnumber_table[ri];
  1582. if (reg<>NR_NO) and InstructionLoadsFromReg(reg,p) then
  1583. begin
  1584. commentstr:=commentstr+' '+std_regname(reg);
  1585. registers_found:=true;
  1586. end;
  1587. end;
  1588. if not registers_found then
  1589. commentstr:=commentstr+' no registers';
  1590. commentstr:=commentstr+' and writes new values in';
  1591. registers_found:=false;
  1592. for ri in tregisterindex do
  1593. begin
  1594. reg:=regnumber_table[ri];
  1595. if (reg<>NR_NO) and RegLoadedWithNewValue(reg,p) then
  1596. begin
  1597. commentstr:=commentstr+' '+std_regname(reg);
  1598. registers_found:=true;
  1599. end;
  1600. end;
  1601. if not registers_found then
  1602. commentstr:=commentstr+' no registers';
  1603. AsmL.InsertAfter(tai_comment.Create(strpnew(commentstr)),p);
  1604. end;
  1605. p:=tai(p.next);
  1606. end;
  1607. end;
  1608. End.