aoptobj.pas 67 KB

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