aoptobj.pas 41 KB

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