aoptobj.pas 40 KB

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