aoptobj.pas 68 KB

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