aoptobj.pas 67 KB

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