aoptobj.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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,
  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(r1, r2: TReference): Boolean;
  45. //!!! FIXME
  46. TRegArray = Array[byte] of tsuperregister;
  47. TRegSet = Set of byte;
  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 = class
  54. Constructor create;
  55. Constructor create_regset(Const _RegSet: TRegSet);
  56. Destructor Destroy;override;
  57. { update the info with the pairegalloc objects coming after }
  58. { p }
  59. Procedure Update(p: Tai);
  60. { is Reg currently in use }
  61. Function IsUsed(Reg: TRegister): Boolean;
  62. { get all the currently used registers }
  63. Function GetUsedRegs: TRegSet;
  64. Private
  65. UsedRegs: TRegSet;
  66. End;
  67. { ************************************************************************* }
  68. { ******************* Contents of the integer registers ******************* }
  69. { ************************************************************************* }
  70. { size of the integer that holds the state number of a register. Can be any }
  71. { integer type, so it can be changed to reduce the size of the TContent }
  72. { structure or to improve alignment }
  73. TStateInt = Byte;
  74. TContent = Record
  75. { start and end of block instructions that defines the }
  76. { content of this register. If Typ = con_const, then }
  77. { Longint(StartMod) = value of the constant) }
  78. StartMod: Tai;
  79. { starts at 0, gets increased everytime the register is }
  80. { written to }
  81. WState: TStateInt;
  82. { starts at 0, gets increased everytime the register is read }
  83. { from }
  84. RState: TStateInt;
  85. { how many instructions starting with StarMod does the block }
  86. { consist of }
  87. NrOfMods: Byte;
  88. { the type of the content of the register: unknown, memory }
  89. { (variable) or constant }
  90. Typ: Byte;
  91. End;
  92. //!!! FIXME
  93. TRegContent = Array[byte] Of TContent;
  94. { ************************************************************************** }
  95. { information object with the contents of every register. Every Tai object }
  96. { gets one of these assigned: a pointer to it is stored in the OptInfo field }
  97. { ************************************************************************** }
  98. TPaiProp = class(TAoptBaseCpu)
  99. Regs: TRegContent;
  100. { info about allocation of general purpose integer registers }
  101. UsedRegs: TUsedRegs;
  102. { can this instruction be removed? }
  103. CanBeRemoved: Boolean;
  104. Constructor create;
  105. { checks the whole sequence of which (so regs[which].StartMod and and }
  106. { the next NrOfMods Tai objects) to see whether Reg is used somewhere, }
  107. { without it being loaded with something else first }
  108. Function RegInSequence(Reg, which: TRegister): Boolean;
  109. { destroy the contents of a register, as well as those whose contents }
  110. { are based on those of that register }
  111. Procedure DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  112. TInstrSinceLastMod);
  113. { if the contents of WhichReg (can be R_NO in case of a constant) are }
  114. { written to memory at the location Ref, the contents of the registers }
  115. { that depend on Ref have to be destroyed }
  116. Procedure DestroyRefs(Const Ref: TReference; WhichReg: TRegister; var
  117. InstrSinceLastMod: TInstrSinceLastMod);
  118. { an instruction reads from operand o }
  119. Procedure ReadOp(const o:toper);
  120. { an instruction reads from reference Ref }
  121. Procedure ReadRef(Ref: PReference);
  122. { an instruction reads from register Reg }
  123. Procedure ReadReg(Reg: TRegister);
  124. { an instruction writes/modifies operand o and this has special }
  125. { side-effects or modifies the contents in such a way that we can't }
  126. { simply add this instruction to the sequence of instructions that }
  127. { describe the contents of the operand, so destroy it }
  128. Procedure DestroyOp(const o:Toper; var InstrSinceLastMod:
  129. TInstrSinceLastMod);
  130. { destroy the contents of all registers }
  131. Procedure DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  132. { a register's contents are modified, but not destroyed (the new value }
  133. { depends on the old one) }
  134. Procedure ModifyReg(reg: TRegister; var InstrSinceLastMod:
  135. TInstrSinceLastMod);
  136. { an operand's contents are modified, but not destroyed (the new value }
  137. { depends on the old one) }
  138. Procedure ModifyOp(const oper: TOper; var InstrSinceLastMod:
  139. TInstrSinceLastMod);
  140. { increase the write state of a register (call every time a register is }
  141. { written to) }
  142. Procedure IncWState(Reg: TRegister);
  143. { increase the read state of a register (call every time a register is }
  144. { read from) }
  145. Procedure IncRState(Reg: TRegister);
  146. { get the write state of a register }
  147. Function GetWState(Reg: TRegister): TStateInt;
  148. { get the read state of a register }
  149. Function GetRState(Reg: TRegister): TStateInt;
  150. { get the type of contents of a register }
  151. Function GetRegContentType(Reg: TRegister): Byte;
  152. Destructor Done;
  153. Private
  154. Procedure IncState(var s: TStateInt);
  155. { returns whether the reference Ref is used somewhere in the loading }
  156. { sequence Content }
  157. Function RefInSequence(Const Ref: TReference; Content: TContent;
  158. RefsEq: TRefCompare): Boolean;
  159. { returns whether the instruction P reads from and/or writes }
  160. { to Reg }
  161. Function RefInInstruction(Const Ref: TReference; p: Tai;
  162. RefsEq: TRefCompare): Boolean;
  163. { returns whether two references with at least one pointing to an array }
  164. { may point to the same memory location }
  165. End;
  166. { ************************************************************************* }
  167. { ************************ Label information ****************************** }
  168. { ************************************************************************* }
  169. TLabelTableItem = Record
  170. PaiObj: Tai;
  171. End;
  172. {$ifndef TP}
  173. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  174. {$else TP}
  175. TLabelTable = Array[0..(65520 div sizeof(TLabelTableItem))] Of TLabelTableItem;
  176. {$endif TP}
  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: AWord;
  183. LabelDif: AWord;
  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. TAOptObj = class(TAoptBaseCpu)
  192. { the PAasmOutput list this optimizer instance works on }
  193. AsmL: TAasmOutput;
  194. { The labelinfo record contains the addresses of the Tai objects }
  195. { that are labels, how many labels there are and the min and max }
  196. { label numbers }
  197. LabelInfo: PLabelInfo;
  198. { Start and end of the block that is currently being optimized }
  199. BlockStart, BlockEnd: Tai;
  200. DFA: TAOptDFA;
  201. { _AsmL is the PAasmOutpout list that has to be optimized, }
  202. { _BlockStart and _BlockEnd the start and the end of the block }
  203. { that has to be optimized and _LabelInfo a pointer to a }
  204. { TLabelInfo record }
  205. Constructor create(_AsmL: TAasmOutput; _BlockStart, _BlockEnd: Tai;
  206. _LabelInfo: PLabelInfo); virtual;
  207. { processor independent methods }
  208. { returns true if the label L is found between hp and the next }
  209. { instruction }
  210. Function FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  211. { inserts new_one between prev and foll in AsmL }
  212. Procedure InsertLLItem(prev, foll, new_one: TLinkedListItem);
  213. { If P is a Tai object releveant to the optimizer, P is returned
  214. If it is not relevant tot he optimizer, the first object after P
  215. that is relevant is returned }
  216. Function SkipHead(P: Tai): Tai;
  217. { returns true if the operands o1 and o2 are completely equal }
  218. Function OpsEqual(const o1,o2:toper): Boolean;
  219. { Returns true if a ait_alloc object for Reg is found in the block
  220. of Tai's starting with StartPai and ending with the next "real"
  221. instruction }
  222. Function FindRegAlloc(Reg: TRegister; StartPai: Tai): Boolean;
  223. { traces sucessive jumps to their final destination and sets it, e.g.
  224. je l1 je l3
  225. <code> <code>
  226. l1: becomes l1:
  227. je l2 je l3
  228. <code> <code>
  229. l2: l2:
  230. jmp l3 jmp l3
  231. the level parameter denotes how deeep we have already followed the jump,
  232. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  233. function GetFinalDestination(hp: taicpu; level: longint): boolean;
  234. function getlabelwithsym(sym: tasmlabel): tai;
  235. { peephole optimizer }
  236. procedure PrePeepHoleOpts;
  237. procedure PeepHoleOptPass1;
  238. procedure PeepHoleOptPass2;
  239. procedure PostPeepHoleOpts;
  240. { processor dependent methods }
  241. // if it returns true, perform a "continue"
  242. function PeepHoleOptPass1Cpu(var p: tai): boolean; virtual;
  243. End;
  244. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  245. { ***************************** Implementation **************************** }
  246. Implementation
  247. uses
  248. globals,
  249. verbose,
  250. procinfo;
  251. { ************************************************************************* }
  252. { ******************************** TUsedRegs ****************************** }
  253. { ************************************************************************* }
  254. Constructor TUsedRegs.create;
  255. Begin
  256. UsedRegs := [];
  257. End;
  258. Constructor TUsedRegs.create_regset(Const _RegSet: TRegSet);
  259. Begin
  260. UsedRegs := _RegSet;
  261. End;
  262. Procedure TUsedRegs.Update(p: Tai);
  263. {updates UsedRegs with the RegAlloc Information coming after P}
  264. Begin
  265. Repeat
  266. While Assigned(p) And
  267. ((p.typ in (SkipInstr - [ait_RegAlloc])) or
  268. ((p.typ = ait_label) And
  269. Not(Tai_Label(p).l.is_used))) Do
  270. p := Tai(p.next);
  271. While Assigned(p) And
  272. (p.typ=ait_RegAlloc) Do
  273. Begin
  274. {!!!!!!!! FIXME
  275. if tai_regalloc(p).ratype=ra_alloc then
  276. UsedRegs := UsedRegs + [tai_regalloc(p).Reg]
  277. else
  278. UsedRegs := UsedRegs - [tai_regalloc(p).Reg];
  279. p := Tai(p.next);
  280. }
  281. End;
  282. Until Not(Assigned(p)) Or
  283. (Not(p.typ in SkipInstr) And
  284. Not((p.typ = ait_label) And
  285. Not(Tai_Label(p).l.is_used)));
  286. End;
  287. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  288. Begin
  289. //!!!!!!!!!!! IsUsed := Reg in UsedRegs
  290. End;
  291. Function TUsedRegs.GetUsedRegs: TRegSet;
  292. Begin
  293. GetUsedRegs := UsedRegs;
  294. End;
  295. Destructor TUsedRegs.Destroy;
  296. Begin
  297. inherited destroy;
  298. end;
  299. { ************************************************************************* }
  300. { **************************** TPaiProp *********************************** }
  301. { ************************************************************************* }
  302. Constructor TPaiProp.Create;
  303. Begin
  304. {!!!!!!
  305. UsedRegs.Init;
  306. CondRegs.init;
  307. }
  308. { DirFlag: TFlagContents; I386 specific}
  309. End;
  310. Function TPaiProp.RegInSequence(Reg, which: TRegister): Boolean;
  311. Var p: Tai;
  312. RegsChecked: TRegSet;
  313. content: TContent;
  314. Counter: Byte;
  315. TmpResult: Boolean;
  316. Begin
  317. {!!!!!!!!!!1
  318. RegsChecked := [];
  319. content := regs[which];
  320. p := content.StartMod;
  321. TmpResult := False;
  322. Counter := 1;
  323. While Not(TmpResult) And
  324. (Counter <= Content.NrOfMods) Do
  325. Begin
  326. If IsLoadMemReg(p) Then
  327. With PInstr(p)^.oper[LoadSrc]^.ref^ Do
  328. If (Base = ProcInfo.FramePointer)
  329. {$ifdef RefsHaveIndexReg}
  330. And (Index = R_NO)
  331. {$endif RefsHaveIndexReg} Then
  332. Begin
  333. RegsChecked := RegsChecked +
  334. [RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg)];
  335. If Reg = RegMaxSize(PInstr(p)^.oper[LoadDst]^.reg) Then
  336. Break;
  337. End
  338. Else
  339. Begin
  340. If (Base = Reg) And
  341. Not(Base In RegsChecked)
  342. Then TmpResult := True;
  343. {$ifdef RefsHaveIndexReg}
  344. If Not(TmpResult) And
  345. (Index = Reg) And
  346. Not(Index In RegsChecked)
  347. Then TmpResult := True;
  348. {$Endif RefsHaveIndexReg}
  349. End
  350. Else TmpResult := RegInInstruction(Reg, p);
  351. Inc(Counter);
  352. GetNextInstruction(p,p)
  353. End;
  354. RegInSequence := TmpResult
  355. }
  356. End;
  357. Procedure TPaiProp.DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  358. TInstrSinceLastMod);
  359. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  360. { the contents of registers are loaded with a memory location based on Reg }
  361. Var TmpWState, TmpRState: Byte;
  362. Counter: TRegister;
  363. Begin
  364. {!!!!!!!
  365. Reg := RegMaxSize(Reg);
  366. If (Reg in [LoGPReg..HiGPReg]) Then
  367. For Counter := LoGPReg to HiGPReg Do
  368. With Regs[Counter] Do
  369. If (Counter = reg) Or
  370. ((Typ = Con_Ref) And
  371. RegInSequence(Reg, Counter)) Then
  372. Begin
  373. InstrSinceLastMod[Counter] := 0;
  374. IncWState(Counter);
  375. TmpWState := GetWState(Counter);
  376. TmpRState := GetRState(Counter);
  377. FillChar(Regs[Counter], SizeOf(TContent), 0);
  378. WState := TmpWState;
  379. RState := TmpRState
  380. End
  381. }
  382. End;
  383. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  384. Begin
  385. {!!!!!!!!!!
  386. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  387. {$ifdef refsHaveSegmentReg}
  388. (R1.Segment = R2.Segment) And
  389. {$endif}
  390. (R1.Base = R2.Base) And
  391. (R1.Symbol=R2.Symbol);
  392. }
  393. End;
  394. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  395. var InstrSinceLastMod: TInstrSinceLastMod);
  396. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  397. { is the register whose contents are being written to memory (if this proc }
  398. { is called because of a "mov?? %reg, (mem)" instruction) }
  399. Var RefsEq: TRefCompare;
  400. Counter: TRegister;
  401. Begin
  402. {!!!!!!!!!!!
  403. WhichReg := RegMaxSize(WhichReg);
  404. If (Ref.base = procinfo.FramePointer) or
  405. Assigned(Ref.Symbol) Then
  406. Begin
  407. If
  408. {$ifdef refsHaveIndexReg}
  409. (Ref.Index = R_NO) And
  410. {$endif refsHaveIndexReg}
  411. (Not(Assigned(Ref.Symbol)) or
  412. (Ref.base = R_NO)) Then
  413. { local variable which is not an array }
  414. RefsEq := {$ifdef fpc}@{$endif}RefsEqual
  415. Else
  416. { local variable which is an array }
  417. RefsEq := {$ifdef fpc}@{$endif}ArrayRefsEq;
  418. {write something to a parameter, a local or global variable, so
  419. * with uncertain optimizations on:
  420. - destroy the contents of registers whose contents have somewhere a
  421. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  422. are being written to memory) is not destroyed if it's StartMod is
  423. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  424. pointer or value based on Ref)
  425. * with uncertain optimizations off:
  426. - also destroy registers that contain any pointer}
  427. For Counter := LoGPReg to HiGPReg Do
  428. With Regs[Counter] Do
  429. Begin
  430. If (typ = Con_Ref) And
  431. ((Not(cs_UncertainOpts in aktglobalswitches) And
  432. (NrOfMods <> 1)
  433. ) Or
  434. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  435. ((Counter <> WhichReg) Or
  436. ((NrOfMods <> 1) And
  437. {StarMod is always of the type ait_instruction}
  438. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  439. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  440. )
  441. )
  442. )
  443. )
  444. Then
  445. DestroyReg(Counter, InstrSinceLastMod)
  446. End
  447. End
  448. Else
  449. {write something to a pointer location, so
  450. * with uncertain optimzations on:
  451. - do not destroy registers which contain a local/global variable or a
  452. parameter, except if DestroyRefs is called because of a "movsl"
  453. * with uncertain optimzations off:
  454. - destroy every register which contains a memory location
  455. }
  456. For Counter := LoGPReg to HiGPReg Do
  457. With Regs[Counter] Do
  458. If (typ = Con_Ref) And
  459. (Not(cs_UncertainOpts in aktglobalswitches) Or
  460. {$ifdef x86}
  461. {for movsl}
  462. (Ref.Base = R_EDI) Or
  463. {$endif}
  464. {don't destroy if reg contains a parameter, local or global variable}
  465. Not((NrOfMods = 1) And
  466. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  467. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  468. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  469. )
  470. )
  471. )
  472. Then DestroyReg(Counter, InstrSinceLastMod)
  473. }
  474. End;
  475. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  476. Var Counter: TRegister;
  477. Begin {initializes/desrtoys all registers}
  478. {!!!!!!!!!
  479. For Counter := LoGPReg To HiGPReg Do
  480. Begin
  481. ReadReg(Counter);
  482. DestroyReg(Counter, InstrSinceLastMod);
  483. End;
  484. CondRegs.Init;
  485. { FPURegs.Init; }
  486. }
  487. End;
  488. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  489. TInstrSinceLastMod);
  490. Begin
  491. {!!!!!!!
  492. Case o.typ Of
  493. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  494. top_ref:
  495. Begin
  496. ReadRef(o.ref);
  497. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  498. End;
  499. top_symbol:;
  500. End;
  501. }
  502. End;
  503. Procedure TPaiProp.ReadReg(Reg: TRegister);
  504. Begin
  505. {!!!!!!!
  506. Reg := RegMaxSize(Reg);
  507. If Reg in General_Registers Then
  508. IncRState(RegMaxSize(Reg))
  509. }
  510. End;
  511. Procedure TPaiProp.ReadRef(Ref: PReference);
  512. Begin
  513. {!!!!!!!
  514. If Ref^.Base <> R_NO Then
  515. ReadReg(Ref^.Base);
  516. {$ifdef refsHaveIndexReg}
  517. If Ref^.Index <> R_NO Then
  518. ReadReg(Ref^.Index);
  519. {$endif}
  520. }
  521. End;
  522. Procedure TPaiProp.ReadOp(const o:toper);
  523. Begin
  524. Case o.typ Of
  525. top_reg: ReadReg(o.reg);
  526. top_ref: ReadRef(o.ref);
  527. else
  528. internalerror(200410241);
  529. End;
  530. End;
  531. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  532. TInstrSinceLastMod);
  533. Begin
  534. {!!!!!!!
  535. With Regs[reg] Do
  536. If (Typ = Con_Ref)
  537. Then
  538. Begin
  539. IncState(WState);
  540. {also store how many instructions are part of the sequence in the first
  541. instructions PPaiProp, so it can be easily accessed from within
  542. CheckSequence}
  543. Inc(NrOfMods, InstrSinceLastMod[Reg]);
  544. PPaiProp(StartMod.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  545. InstrSinceLastMod[Reg] := 0;
  546. End
  547. Else
  548. DestroyReg(Reg, InstrSinceLastMod);
  549. }
  550. End;
  551. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  552. TInstrSinceLastMod);
  553. Begin
  554. If oper.typ = top_reg Then
  555. ModifyReg(RegMaxSize(oper.reg),InstrSinceLastMod)
  556. Else
  557. Begin
  558. ReadOp(oper);
  559. DestroyOp(oper, InstrSinceLastMod);
  560. End
  561. End;
  562. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  563. Begin
  564. //!!!! IncState(Regs[Reg].WState);
  565. End;
  566. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  567. Begin
  568. //!!!! IncState(Regs[Reg].RState);
  569. End;
  570. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  571. Begin
  572. //!!!! GetWState := Regs[Reg].WState
  573. End;
  574. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  575. Begin
  576. //!!!! GetRState := Regs[Reg].RState
  577. End;
  578. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  579. Begin
  580. //!!!! GetRegContentType := Regs[Reg].typ
  581. End;
  582. Destructor TPaiProp.Done;
  583. Begin
  584. //!!!! UsedRegs.Done;
  585. //!!!! CondRegs.Done;
  586. { DirFlag: TFlagContents; I386 specific}
  587. End;
  588. { ************************ private TPaiProp stuff ************************* }
  589. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  590. Begin
  591. If s <> High(TStateInt) Then Inc(s)
  592. Else s := 0
  593. End;
  594. Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Tai;
  595. RefsEq: TRefCompare): Boolean;
  596. Var Count: AWord;
  597. TmpResult: Boolean;
  598. Begin
  599. TmpResult := False;
  600. If (p.typ = ait_instruction) Then
  601. Begin
  602. Count := 0;
  603. Repeat
  604. If (TInstr(p).oper[Count]^.typ = Top_Ref) Then
  605. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count]^.ref^);
  606. Inc(Count);
  607. Until (Count = MaxOps) or TmpResult;
  608. End;
  609. RefInInstruction := TmpResult;
  610. End;
  611. Function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  612. RefsEq: TRefCompare): Boolean;
  613. Var p: Tai;
  614. Counter: Byte;
  615. TmpResult: Boolean;
  616. Begin
  617. p := Content.StartMod;
  618. TmpResult := False;
  619. Counter := 1;
  620. While Not(TmpResult) And
  621. (Counter <= Content.NrOfMods) Do
  622. Begin
  623. If (p.typ = ait_instruction) And
  624. RefInInstruction(Ref, p, {$ifdef fpc}@{$endif}references_equal)
  625. Then TmpResult := True;
  626. Inc(Counter);
  627. GetNextInstruction(p,p)
  628. End;
  629. RefInSequence := TmpResult
  630. End;
  631. { ************************************************************************* }
  632. { ***************************** TAoptObj ********************************** }
  633. { ************************************************************************* }
  634. Constructor TAoptObj.create(_AsmL: TAasmOutput; _BlockStart, _BlockEnd: Tai;
  635. _LabelInfo: PLabelInfo);
  636. Begin
  637. AsmL := _AsmL;
  638. BlockStart := _BlockStart;
  639. BlockEnd := _BlockEnd;
  640. LabelInfo := _LabelInfo
  641. End;
  642. Function TAOptObj.FindLabel(L: TasmLabel; Var hp: Tai): Boolean;
  643. Var TempP: Tai;
  644. Begin
  645. TempP := hp;
  646. While Assigned(TempP) and
  647. (TempP.typ In SkipInstr + [ait_label]) Do
  648. If (TempP.typ <> ait_Label) Or
  649. (Tai_label(TempP).l <> L)
  650. Then GetNextInstruction(TempP, TempP)
  651. Else
  652. Begin
  653. hp := TempP;
  654. FindLabel := True;
  655. exit
  656. End;
  657. FindLabel := False;
  658. End;
  659. Procedure TAOptObj.InsertLLItem(prev, foll, new_one : TLinkedListItem);
  660. Begin
  661. If Assigned(prev) Then
  662. If Assigned(foll) Then
  663. Begin
  664. If Assigned(new_one) Then
  665. Begin
  666. new_one.previous := prev;
  667. new_one.next := foll;
  668. prev.next := new_one;
  669. foll.previous := new_one;
  670. { should we update line information? }
  671. if (not (tai(new_one).typ in SkipLineInfo)) and
  672. (not (tai(foll).typ in SkipLineInfo)) then
  673. Tailineinfo(new_one).fileinfo := Tailineinfo(foll).fileinfo
  674. End
  675. End
  676. Else AsmL.Concat(new_one)
  677. Else If Assigned(Foll) Then AsmL.Insert(new_one)
  678. End;
  679. Function TAOptObj.SkipHead(P: Tai): Tai;
  680. Var OldP: Tai;
  681. Begin
  682. Repeat
  683. OldP := P;
  684. If (P.typ in SkipInstr) Or
  685. ((P.typ = ait_marker) And
  686. (Tai_Marker(P).Kind = AsmBlockEnd)) Then
  687. GetNextInstruction(P, P)
  688. Else If ((P.Typ = Ait_Marker) And
  689. (Tai_Marker(P).Kind = NoPropInfoStart)) Then
  690. { a marker of the type NoPropInfoStart can't be the first instruction of a }
  691. { paasmoutput list }
  692. GetNextInstruction(Tai(P.Previous),P);
  693. If (P.Typ = Ait_Marker) And
  694. (Tai_Marker(P).Kind = AsmBlockStart) Then
  695. Begin
  696. P := Tai(P.Next);
  697. While (P.typ <> Ait_Marker) Or
  698. (Tai_Marker(P).Kind <> AsmBlockEnd) Do
  699. P := Tai(P.Next)
  700. End;
  701. Until P = OldP;
  702. SkipHead := P;
  703. End;
  704. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  705. Begin
  706. if o1.typ=o2.typ then
  707. Case o1.typ Of
  708. Top_Reg :
  709. OpsEqual:=o1.reg=o2.reg;
  710. Top_Ref :
  711. OpsEqual := references_equal(o1.ref^, o2.ref^);
  712. Top_Const :
  713. OpsEqual:=o1.val=o2.val;
  714. Top_None :
  715. OpsEqual := True
  716. else OpsEqual := False
  717. End;
  718. End;
  719. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Tai): Boolean;
  720. Begin
  721. FindRegAlloc:=False;
  722. Repeat
  723. While Assigned(StartPai) And
  724. ((StartPai.typ in (SkipInstr - [ait_regAlloc])) Or
  725. ((StartPai.typ = ait_label) and
  726. Not(Tai_Label(StartPai).l.Is_Used))) Do
  727. StartPai := Tai(StartPai.Next);
  728. If Assigned(StartPai) And
  729. (StartPai.typ = ait_regAlloc) and (tai_regalloc(StartPai).ratype=ra_alloc) Then
  730. Begin
  731. if tai_regalloc(StartPai).Reg = Reg then
  732. begin
  733. FindRegAlloc:=true;
  734. exit;
  735. end;
  736. StartPai := Tai(StartPai.Next);
  737. End
  738. else
  739. exit;
  740. Until false;
  741. End;
  742. function SkipLabels(hp: tai; var hp2: tai): boolean;
  743. {skips all labels and returns the next "real" instruction}
  744. begin
  745. while assigned(hp.next) and
  746. (tai(hp.next).typ in SkipInstr + [ait_label,ait_align]) Do
  747. hp := tai(hp.next);
  748. if assigned(hp.next) then
  749. begin
  750. SkipLabels := True;
  751. hp2 := tai(hp.next)
  752. end
  753. else
  754. begin
  755. hp2 := hp;
  756. SkipLabels := False
  757. end;
  758. end;
  759. function FindAnyLabel(hp: tai; var l: tasmlabel): Boolean;
  760. begin
  761. FindAnyLabel := false;
  762. while assigned(hp.next) and
  763. (tai(hp.next).typ in (SkipInstr+[ait_align])) Do
  764. hp := tai(hp.next);
  765. if assigned(hp.next) and
  766. (tai(hp.next).typ = ait_label) then
  767. begin
  768. FindAnyLabel := true;
  769. l := tai_label(hp.next).l;
  770. end
  771. end;
  772. {$ifopt r+}
  773. {$define rangewason}
  774. {$r-}
  775. {$endif}
  776. function tAOptObj.getlabelwithsym(sym: tasmlabel): tai;
  777. begin
  778. if (sym.labelnr >= labelinfo^.lowlabel) and
  779. (sym.labelnr <= labelinfo^.highlabel) then { range check, a jump can go past an assembler block! }
  780. getlabelwithsym := labelinfo^.labeltable^[sym.labelnr-labelinfo^.lowlabel].paiobj
  781. else
  782. getlabelwithsym := nil;
  783. end;
  784. {$ifdef rangewason}
  785. {$r+}
  786. {$undef rangewason}
  787. {$endif}
  788. function TAOptObj.GetFinalDestination(hp: taicpu; level: longint): boolean;
  789. {traces sucessive jumps to their final destination and sets it, e.g.
  790. je l1 je l3
  791. <code> <code>
  792. l1: becomes l1:
  793. je l2 je l3
  794. <code> <code>
  795. l2: l2:
  796. jmp l3 jmp l3
  797. the level parameter denotes how deeep we have already followed the jump,
  798. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  799. var p1, p2: tai;
  800. l: tasmlabel;
  801. begin
  802. GetfinalDestination := false;
  803. if level > 20 then
  804. exit;
  805. p1 := getlabelwithsym(tasmlabel(hp.oper[0]^.ref^.symbol));
  806. if assigned(p1) then
  807. begin
  808. SkipLabels(p1,p1);
  809. if (tai(p1).typ = ait_instruction) and
  810. (taicpu(p1).is_jmp) then
  811. if { the next instruction after the label where the jump hp arrives}
  812. { is unconditional or of the same type as hp, so continue }
  813. (((taicpu(p1).opcode = aopt_uncondjmp) and
  814. (taicpu(p1).oper[0]^.typ = top_ref) and
  815. (assigned(taicpu(p1).oper[0]^.ref^.symbol)) and
  816. (taicpu(p1).oper[0]^.ref^.symbol is TAsmLabel)) or
  817. conditions_equal(taicpu(p1).condition,hp.condition)) or
  818. { the next instruction after the label where the jump hp arrives}
  819. { is the opposite of hp (so this one is never taken), but after }
  820. { that one there is a branch that will be taken, so perform a }
  821. { little hack: set p1 equal to this instruction (that's what the}
  822. { last SkipLabels is for, only works with short bool evaluation)}
  823. (conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) and
  824. SkipLabels(p1,p2) and
  825. (p2.typ = ait_instruction) and
  826. (taicpu(p2).is_jmp) and
  827. (((taicpu(p2).opcode = aopt_uncondjmp) and
  828. (taicpu(p2).oper[0]^.typ = top_ref) and
  829. (assigned(taicpu(p2).oper[0]^.ref^.symbol)) and
  830. (taicpu(p2).oper[0]^.ref^.symbol is TAsmLabel)) or
  831. (conditions_equal(taicpu(p2).condition,hp.condition))) and
  832. SkipLabels(p1,p1)) then
  833. begin
  834. { quick check for loops of the form "l5: ; jmp l5 }
  835. if (tasmlabel(taicpu(p1).oper[0]^.ref^.symbol).labelnr =
  836. tasmlabel(hp.oper[0]^.ref^.symbol).labelnr) then
  837. exit;
  838. if not GetFinalDestination(taicpu(p1),succ(level)) then
  839. exit;
  840. tasmlabel(hp.oper[0]^.ref^.symbol).decrefs;
  841. hp.oper[0]^.ref^.symbol:=taicpu(p1).oper[0]^.ref^.symbol;
  842. tasmlabel(hp.oper[0]^.ref^.symbol).increfs;
  843. end
  844. else
  845. if conditions_equal(taicpu(p1).condition,inverse_cond(hp.condition)) then
  846. if not FindAnyLabel(p1,l) then
  847. begin
  848. {$ifdef finaldestdebug}
  849. insertllitem(asml,p1,p1.next,tai_comment.Create(
  850. strpnew('previous label inserted'))));
  851. {$endif finaldestdebug}
  852. objectlibrary.getjumplabel(l);
  853. insertllitem(p1,p1.next,tai_label.Create(l));
  854. tasmlabel(taicpu(hp).oper[0]^.ref^.symbol).decrefs;
  855. hp.oper[0]^.ref^.symbol := l;
  856. l.increfs;
  857. { this won't work, since the new label isn't in the labeltable }
  858. { so it will fail the rangecheck. Labeltable should become a }
  859. { hashtable to support this: }
  860. { GetFinalDestination(asml, hp); }
  861. end
  862. else
  863. begin
  864. {$ifdef finaldestdebug}
  865. insertllitem(asml,p1,p1.next,tai_comment.Create(
  866. strpnew('next label reused'))));
  867. {$endif finaldestdebug}
  868. l.increfs;
  869. hp.oper[0]^.ref^.symbol := l;
  870. if not GetFinalDestination(hp,succ(level)) then
  871. exit;
  872. end;
  873. end;
  874. GetFinalDestination := true;
  875. end;
  876. procedure TAOptObj.PrePeepHoleOpts;
  877. begin
  878. end;
  879. procedure TAOptObj.PeepHoleOptPass1;
  880. var
  881. p,hp1,hp2 : tai;
  882. begin
  883. p := BlockStart;
  884. //!!!! UsedRegs := [];
  885. while (p <> BlockEnd) Do
  886. begin
  887. //!!!! UpDateUsedRegs(UsedRegs, tai(p.next));
  888. if PeepHoleOptPass1Cpu(p) then
  889. continue;
  890. case p.Typ Of
  891. ait_instruction:
  892. begin
  893. { Handle Jmp Optimizations }
  894. if taicpu(p).is_jmp then
  895. begin
  896. { the following if-block removes all code between a jmp and the next label,
  897. because it can never be executed
  898. }
  899. if (taicpu(p).opcode = aopt_uncondjmp) and
  900. (taicpu(p).oper[0]^.typ = top_ref) and
  901. (assigned(taicpu(p).oper[0]^.ref^.symbol)) and
  902. (taicpu(p).oper[0]^.ref^.symbol is TAsmLabel) then
  903. begin
  904. while GetNextInstruction(p, hp1) and
  905. (hp1.typ <> ait_label) do
  906. if not(hp1.typ in ([ait_label,ait_align]+skipinstr)) then
  907. begin
  908. asml.remove(hp1);
  909. hp1.free;
  910. end
  911. else break;
  912. end;
  913. { remove jumps to a label coming right after them }
  914. if GetNextInstruction(p, hp1) then
  915. begin
  916. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp1) and
  917. {$warning FIXME removing the first instruction fails}
  918. (p<>blockstart) then
  919. begin
  920. hp2:=tai(hp1.next);
  921. asml.remove(p);
  922. p.free;
  923. p:=hp2;
  924. continue;
  925. end
  926. else
  927. begin
  928. if hp1.typ = ait_label then
  929. SkipLabels(hp1,hp1);
  930. if (tai(hp1).typ=ait_instruction) and
  931. (taicpu(hp1).opcode=aopt_uncondjmp) and
  932. (taicpu(hp1).oper[0]^.typ = top_ref) and
  933. (assigned(taicpu(hp1).oper[0]^.ref^.symbol)) and
  934. (taicpu(hp1).oper[0]^.ref^.symbol is TAsmLabel) and
  935. GetNextInstruction(hp1, hp2) and
  936. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp2) then
  937. begin
  938. if taicpu(p).opcode=aopt_condjmp then
  939. begin
  940. taicpu(p).condition:=inverse_cond(taicpu(p).condition);
  941. tai_label(hp2).l.decrefs;
  942. taicpu(p).oper[0]^.ref^.symbol:=taicpu(hp1).oper[0]^.ref^.symbol;
  943. taicpu(p).oper[0]^.ref^.symbol.increfs;
  944. {$ifdef SPARC}
  945. hp2:=tai(hp1.next);
  946. asml.remove(hp2);
  947. hp2.free;
  948. {$endif SPARC}
  949. asml.remove(hp1);
  950. hp1.free;
  951. GetFinalDestination(taicpu(p),0);
  952. end
  953. else
  954. begin
  955. GetFinalDestination(taicpu(p),0);
  956. p:=tai(p.next);
  957. continue;
  958. end;
  959. end
  960. else
  961. GetFinalDestination(taicpu(p),0);
  962. end;
  963. end;
  964. end
  965. else
  966. { All other optimizes }
  967. begin
  968. end; { if is_jmp }
  969. end;
  970. end;
  971. //!!!!!!!! updateUsedRegs(UsedRegs,p);
  972. p:=tai(p.next);
  973. end;
  974. end;
  975. procedure TAOptObj.PeepHoleOptPass2;
  976. begin
  977. end;
  978. procedure TAOptObj.PostPeepHoleOpts;
  979. begin
  980. end;
  981. function TAOptObj.PeepHoleOptPass1Cpu(var p: tai): boolean;
  982. begin
  983. result := false;
  984. end;
  985. End.