aoptobj.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. { general, processor independent objects for use by the assembler optimizer }
  23. Interface
  24. uses aasm, Cobjects, cpuinfo, cpubase, aoptbase, aoptcpub;
  25. { ************************************************************************* }
  26. { ********************************* Constants ***************************** }
  27. { ************************************************************************* }
  28. Const
  29. {Possible register content types}
  30. con_Unknown = 0;
  31. con_ref = 1;
  32. con_const = 2;
  33. {***************** Types ****************}
  34. Type
  35. { ************************************************************************* }
  36. { ************************* Some general type definitions ***************** }
  37. { ************************************************************************* }
  38. TRefCompare = Function(const r1, r2: TReference): Boolean;
  39. TRegArray = Array[LoReg..HiReg] of TRegister;
  40. TRegSet = Set of LoReg..HiReg;
  41. { possible actions on an operand: read, write or modify (= read & write) }
  42. TOpAction = (OpAct_Read, OpAct_Write, OpAct_Modify, OpAct_Unknown);
  43. { ************************************************************************* }
  44. { * Object to hold information on which regiters are in use and which not * }
  45. { ************************************************************************* }
  46. TUsedRegs = Object
  47. Constructor init;
  48. Constructor InitWithValue(Const _RegSet: TRegSet);
  49. { update the info with the pairegalloc objects coming after }
  50. { p }
  51. Procedure Update(p: Pai);
  52. { is Reg currently in use }
  53. Function IsUsed(Reg: TRegister): Boolean;
  54. { get all the currently used registers }
  55. Function GetUsedRegs: TRegSet;
  56. Destructor Done;
  57. Private
  58. UsedRegs: TRegSet;
  59. End;
  60. { ************************************************************************* }
  61. { ******************* Contents of the integer registers ******************* }
  62. { ************************************************************************* }
  63. { size of the integer that holds the state number of a register. Can be any }
  64. { integer type, so it can be changed to reduce the size of the TContent }
  65. { structure or to improve alignment }
  66. TStateInt = Byte;
  67. TContent = Packed Record
  68. { start and end of block instructions that defines the }
  69. { content of this register. If Typ = con_const, then }
  70. { Longint(StartMod) = value of the constant) }
  71. StartMod: pai;
  72. { starts at 0, gets increased everytime the register is }
  73. { written to }
  74. WState: TStateInt;
  75. { starts at 0, gets increased everytime the register is read }
  76. { from }
  77. RState: TStateInt;
  78. { how many instructions starting with StarMod does the block }
  79. { consist of }
  80. NrOfMods: Byte;
  81. { the type of the content of the register: unknown, memory }
  82. { (variable) or constant }
  83. Typ: Byte;
  84. End;
  85. TRegContent = Array[LoGPReg..HiGPReg] Of TContent;
  86. { ************************************************************************** }
  87. { information object with the contents of every register. Every Pai object }
  88. { gets one of these assigned: a pointer to it is stored in the OptInfo field }
  89. { ************************************************************************** }
  90. PPaiProp = ^TPaiProp;
  91. TPaiProp = Object(TAoptBaseCpu)
  92. Regs: TRegContent;
  93. { info about allocation of general purpose integer registers }
  94. UsedRegs: TUsedRegs;
  95. { info about the conditional registers }
  96. CondRegs: TCondRegs;
  97. { can this instruction be removed? }
  98. CanBeRemoved: Boolean;
  99. Constructor init;
  100. { checks the whole sequence of which (so regs[which].StartMod and and }
  101. { the next NrOfMods Pai objects) to see whether Reg is used somewhere, }
  102. { without it being loaded with something else first }
  103. Function RegInSequence(Reg, which: TRegister): Boolean;
  104. { destroy the contents of a register, as well as those whose contents }
  105. { are based on those of that register }
  106. Procedure DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  107. TInstrSinceLastMod);
  108. { if the contents of WhichReg (can be R_NO in case of a constant) are }
  109. { written to memory at the location Ref, the contents of the registers }
  110. { that depend on Ref have to be destroyed }
  111. Procedure DestroyRefs(Const Ref: TReference; WhichReg: TRegister; var
  112. InstrSinceLastMod: TInstrSinceLastMod);
  113. { an instruction reads from operand o }
  114. Procedure ReadOp(const o:toper);
  115. { an instruction reads from reference Ref }
  116. Procedure ReadRef(Ref: PReference);
  117. { an instruction reads from register Reg }
  118. Procedure ReadReg(Reg: TRegister);
  119. { an instruction writes/modifies operand o and this has special }
  120. { side-effects or modifies the contents in such a way that we can't }
  121. { simply add this instruction to the sequence of instructions that }
  122. { describe the contents of the operand, so destroy it }
  123. Procedure DestroyOp(const o:Toper; var InstrSinceLastMod:
  124. TInstrSinceLastMod);
  125. { destroy the contents of all registers }
  126. Procedure DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  127. {$ifdef arithopt}
  128. { a register's contents are modified, but not destroyed (the new value }
  129. { depends on the old one) }
  130. Procedure ModifyReg(reg: TRegister; var InstrSinceLastMod:
  131. TInstrSinceLastMod);
  132. { an operand's contents are modified, but not destroyed (the new value }
  133. { depends on the old one) }
  134. Procedure ModifyOp(const oper: TOper; var InstrSinceLastMod:
  135. TInstrSinceLastMod);
  136. {$endif arithopt}
  137. { increase the write state of a register (call every time a register is }
  138. { written to) }
  139. Procedure IncWState(Reg: TRegister);
  140. { increase the read state of a register (call every time a register is }
  141. { read from) }
  142. Procedure IncRState(Reg: TRegister);
  143. { get the write state of a register }
  144. Function GetWState(Reg: TRegister): TStateInt;
  145. { get the read state of a register }
  146. Function GetRState(Reg: TRegister): TStateInt;
  147. { get the type of contents of a register }
  148. Function GetRegContentType(Reg: TRegister): Byte;
  149. Destructor Done;
  150. Private
  151. Procedure IncState(var s: TStateInt);
  152. { returns whether the reference Ref is used somewhere in the loading }
  153. { sequence Content }
  154. Function RefInSequence(Const Ref: TReference; Content: TContent;
  155. RefsEq: TRefCompare): Boolean;
  156. { returns whether the instruction P reads from and/or writes }
  157. { to Reg }
  158. Function RefInInstruction(Const Ref: TReference; p: Pai;
  159. RefsEq: TRefCompare): Boolean;
  160. { returns whether two references with at least one pointing to an array }
  161. { may point to the same memory location }
  162. End;
  163. { ************************************************************************* }
  164. { ************************ Label information ****************************** }
  165. { ************************************************************************* }
  166. TLabelTableItem = Record
  167. PaiObj: Pai;
  168. End;
  169. {$ifndef TP}
  170. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  171. {$else TP}
  172. TLabelTable = Array[0..(65520 div sizeof(TLabelTableItem))] Of TLabelTableItem;
  173. {$endif TP}
  174. PLabelTable = ^TLabelTable;
  175. PLabelInfo = ^TLabelInfo;
  176. TLabelInfo = Record
  177. { the highest and lowest label number occurring in the current code }
  178. { fragment }
  179. LowLabel, HighLabel: AWord;
  180. LabelDif: AWord;
  181. { table that contains the addresses of the Pai_Label objects associated }
  182. { with each label number }
  183. LabelTable: PLabelTable;
  184. End;
  185. { ************************************************************************* }
  186. { ********** General optimizer object, used to derive others from ********* }
  187. { ************************************************************************* }
  188. TAOptObj = Object(TAoptBaseCpu)
  189. { the PAasmOutput list this optimizer instance works on }
  190. AsmL: PAasmOutput;
  191. { The labelinfo record contains the addresses of the Pai objects }
  192. { that are labels, how many labels there are and the min and max }
  193. { label numbers }
  194. LabelInfo: PLabelInfo;
  195. { Start and end of the block that is currently being optimized }
  196. BlockStart, BlockEnd: Pai;
  197. { _AsmL is the PAasmOutpout list that has to be optimized, }
  198. { _BlockStart and _BlockEnd the start and the end of the block }
  199. { that has to be optimized and _LabelInfo a pointer to a }
  200. { TLabelInfo record }
  201. Constructor Init(_AsmL: PAasmOutput; _BlockStart, _BlockEnd: Pai;
  202. _LabelInfo: PLabelInfo);
  203. { processor independent methods }
  204. { returns true if the label L is found between hp and the next }
  205. { instruction }
  206. Function FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  207. { inserts new_one between prev and foll in AsmL }
  208. Procedure InsertLLItem(prev, foll, new_one: PLinkedList_Item);
  209. { If P is a Pai object releveant to the optimizer, P is returned }
  210. { If it is not relevant tot he optimizer, the first object after P }
  211. { that is relevant is returned }
  212. Function SkipHead(P: Pai): Pai;
  213. { returns true if the operands o1 and o2 are completely equal }
  214. Function OpsEqual(const o1,o2:toper): Boolean;
  215. { Returns true if a ait_alloc object for Reg is found in the block }
  216. { of Pai's starting with StartPai and ending with the next "real" }
  217. { instruction }
  218. Function FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  219. { processor dependent methods }
  220. End;
  221. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  222. { ***************************** Implementation **************************** }
  223. Implementation
  224. uses globtype, globals, cgbase, tainst;
  225. { ************************************************************************* }
  226. { ******************************** TUsedRegs ****************************** }
  227. { ************************************************************************* }
  228. Constructor TUsedRegs.init;
  229. Begin
  230. UsedRegs := [];
  231. End;
  232. Constructor TUsedRegs.InitWithValue(Const _RegSet: TRegSet);
  233. Begin
  234. UsedRegs := _RegSet;
  235. End;
  236. Procedure TUsedRegs.Update(p: Pai);
  237. {updates UsedRegs with the RegAlloc Information coming after P}
  238. Begin
  239. Repeat
  240. While Assigned(p) And
  241. ((p^.typ in (SkipInstr - [ait_RegAlloc])) or
  242. ((p^.typ = ait_label) And
  243. Not(Pai_Label(p)^.l^.is_used))) Do
  244. p := Pai(p^.next);
  245. While Assigned(p) And
  246. (p^.typ=ait_RegAlloc) Do
  247. Begin
  248. if pairegalloc(p)^.allocation then
  249. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  250. else
  251. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  252. p := pai(p^.next);
  253. End;
  254. Until Not(Assigned(p)) Or
  255. (Not(p^.typ in SkipInstr) And
  256. Not((p^.typ = ait_label) And
  257. Not(Pai_Label(p)^.l^.is_used)));
  258. End;
  259. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  260. Begin
  261. IsUsed := Reg in UsedRegs
  262. End;
  263. Function TUsedRegs.GetUsedRegs: TRegSet;
  264. Begin
  265. GetUsedRegs := UsedRegs;
  266. End;
  267. Destructor TUsedRegs.Done; {$ifdef inl} inline; {$endif inl}
  268. Begin
  269. end;
  270. { ************************************************************************* }
  271. { **************************** TPaiProp *********************************** }
  272. { ************************************************************************* }
  273. Constructor TPaiProp.Init;
  274. Begin
  275. UsedRegs.Init;
  276. CondRegs.init;
  277. { DirFlag: TFlagContents; I386 specific}
  278. End;
  279. Function TPaiProp.RegInSequence(Reg, which: TRegister): Boolean;
  280. Var p: Pai;
  281. RegsChecked: TRegSet;
  282. content: TContent;
  283. Counter: Byte;
  284. TmpResult: Boolean;
  285. Begin
  286. RegsChecked := [];
  287. content := regs[which];
  288. p := content.StartMod;
  289. TmpResult := False;
  290. Counter := 1;
  291. While Not(TmpResult) And
  292. (Counter <= Content.NrOfMods) Do
  293. Begin
  294. If IsLoadMemReg(p) Then
  295. With PInstr(p)^.oper[LoadSrc].ref^ Do
  296. If (Base = ProcInfo^.FramePointer)
  297. {$ifdef RefsHaveIndexReg}
  298. And (Index = R_NO)
  299. {$endif RefsHaveIndexReg} Then
  300. Begin
  301. RegsChecked := RegsChecked +
  302. [RegMaxSize(PInstr(p)^.oper[LoadDst].reg)];
  303. If Reg = RegMaxSize(PInstr(p)^.oper[LoadDst].reg) Then
  304. Break;
  305. End
  306. Else
  307. Begin
  308. If (Base = Reg) And
  309. Not(Base In RegsChecked)
  310. Then TmpResult := True;
  311. {$ifdef RefsHaveIndexReg}
  312. If Not(TmpResult) And
  313. (Index = Reg) And
  314. Not(Index In RegsChecked)
  315. Then TmpResult := True;
  316. {$Endif RefsHaveIndexReg}
  317. End
  318. Else TmpResult := RegInInstruction(Reg, p);
  319. Inc(Counter);
  320. GetNextInstruction(p,p)
  321. End;
  322. RegInSequence := TmpResult
  323. End;
  324. Procedure TPaiProp.DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  325. TInstrSinceLastMod);
  326. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  327. { the contents of registers are loaded with a memory location based on Reg }
  328. Var TmpWState, TmpRState: Byte;
  329. Counter: TRegister;
  330. Begin
  331. Reg := RegMaxSize(Reg);
  332. If (Reg in [LoGPReg..HiGPReg]) Then
  333. For Counter := LoGPReg to HiGPReg Do
  334. With Regs[Counter] Do
  335. If (Counter = reg) Or
  336. ((Typ = Con_Ref) And
  337. RegInSequence(Reg, Counter)) Then
  338. Begin
  339. InstrSinceLastMod[Counter] := 0;
  340. IncWState(Counter);
  341. TmpWState := GetWState(Counter);
  342. TmpRState := GetRState(Counter);
  343. FillChar(Regs[Counter], SizeOf(TContent), 0);
  344. WState := TmpWState;
  345. RState := TmpRState
  346. End
  347. End;
  348. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  349. Begin
  350. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  351. {$ifdef refsHaveSegmentReg}
  352. (R1.Segment = R2.Segment) And
  353. {$endif}
  354. (R1.Base = R2.Base) And
  355. (R1.Symbol=R2.Symbol);
  356. End;
  357. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  358. var InstrSinceLastMod: TInstrSinceLastMod);
  359. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  360. { is the register whose contents are being written to memory (if this proc }
  361. { is called because of a "mov?? %reg, (mem)" instruction) }
  362. Var RefsEq: TRefCompare;
  363. Counter: TRegister;
  364. Begin
  365. WhichReg := RegMaxSize(WhichReg);
  366. If (Ref.base = procinfo^.FramePointer) or
  367. Assigned(Ref.Symbol) Then
  368. Begin
  369. If
  370. {$ifdef refsHaveIndexReg}
  371. (Ref.Index = R_NO) And
  372. {$endif refsHaveIndexReg}
  373. (Not(Assigned(Ref.Symbol)) or
  374. (Ref.base = R_NO)) Then
  375. { local variable which is not an array }
  376. RefsEq := {$ifdef fpc}@{$endif}RefsEqual
  377. Else
  378. { local variable which is an array }
  379. RefsEq := {$ifdef fpc}@{$endif}ArrayRefsEq;
  380. {write something to a parameter, a local or global variable, so
  381. * with uncertain optimizations on:
  382. - destroy the contents of registers whose contents have somewhere a
  383. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  384. are being written to memory) is not destroyed if it's StartMod is
  385. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  386. pointer or value based on Ref)
  387. * with uncertain optimizations off:
  388. - also destroy registers that contain any pointer}
  389. For Counter := LoGPReg to HiGPReg Do
  390. With Regs[Counter] Do
  391. Begin
  392. If (typ = Con_Ref) And
  393. ((Not(cs_UncertainOpts in aktglobalswitches) And
  394. (NrOfMods <> 1)
  395. ) Or
  396. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  397. ((Counter <> WhichReg) Or
  398. ((NrOfMods <> 1) And
  399. {StarMod is always of the type ait_instruction}
  400. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  401. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  402. )
  403. )
  404. )
  405. )
  406. Then
  407. DestroyReg(Counter, InstrSinceLastMod)
  408. End
  409. End
  410. Else
  411. {write something to a pointer location, so
  412. * with uncertain optimzations on:
  413. - do not destroy registers which contain a local/global variable or a
  414. parameter, except if DestroyRefs is called because of a "movsl"
  415. * with uncertain optimzations off:
  416. - destroy every register which contains a memory location
  417. }
  418. For Counter := LoGPReg to HiGPReg Do
  419. With Regs[Counter] Do
  420. If (typ = Con_Ref) And
  421. (Not(cs_UncertainOpts in aktglobalswitches) Or
  422. {$ifdef i386}
  423. {for movsl}
  424. (Ref.Base = R_EDI) Or
  425. {$endif}
  426. {don't destroy if reg contains a parameter, local or global variable}
  427. Not((NrOfMods = 1) And
  428. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  429. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo^.FramePointer) Or
  430. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  431. )
  432. )
  433. )
  434. Then DestroyReg(Counter, InstrSinceLastMod)
  435. End;
  436. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  437. Var Counter: TRegister;
  438. Begin {initializes/desrtoys all registers}
  439. For Counter := LoGPReg To HiGPReg Do
  440. Begin
  441. ReadReg(Counter);
  442. DestroyReg(Counter, InstrSinceLastMod);
  443. End;
  444. CondRegs.Init;
  445. { FPURegs.Init; }
  446. End;
  447. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  448. TInstrSinceLastMod);
  449. Begin
  450. Case o.typ Of
  451. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  452. top_ref:
  453. Begin
  454. ReadRef(o.ref);
  455. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  456. End;
  457. top_symbol:;
  458. End;
  459. End;
  460. Procedure TPaiProp.ReadReg(Reg: TRegister);
  461. Begin
  462. Reg := RegMaxSize(Reg);
  463. If Reg in General_Registers Then
  464. IncRState(RegMaxSize(Reg))
  465. End;
  466. Procedure TPaiProp.ReadRef(Ref: PReference);
  467. Begin
  468. If Ref^.Base <> R_NO Then
  469. ReadReg(Ref^.Base);
  470. {$ifdef refsHaveIndexReg}
  471. If Ref^.Index <> R_NO Then
  472. ReadReg(Ref^.Index);
  473. {$endif}
  474. End;
  475. Procedure TPaiProp.ReadOp(const o:toper);
  476. Begin
  477. Case o.typ Of
  478. top_reg: ReadReg(o.reg);
  479. top_ref: ReadRef(o.ref);
  480. top_symbol : ;
  481. End;
  482. End;
  483. {$ifdef arithopt}
  484. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  485. TInstrSinceLastMod);
  486. Begin
  487. With Regs[reg] Do
  488. If (Typ = Con_Ref)
  489. Then
  490. Begin
  491. IncState(WState);
  492. {also store how many instructions are part of the sequence in the first
  493. instructions PPaiProp, so it can be easily accessed from within
  494. CheckSequence}
  495. Inc(NrOfMods, NrOfInstrSinceLastMod[Reg]);
  496. PPaiProp(StartMod^.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  497. NrOfInstrSinceLastMod[Reg] := 0;
  498. End
  499. Else
  500. DestroyReg(Reg, InstrSinceLastMod);
  501. End;
  502. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  503. TInstrSinceLastMod);
  504. Begin
  505. If oper.typ = top_reg Then
  506. ModifyReg(RegMaxSize(oper.reg))
  507. Else
  508. Begin
  509. ReadOp(oper);
  510. DestroyOp(oper, InstrSinceLastMod);
  511. End
  512. End;
  513. {$endif arithopt}
  514. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  515. Begin
  516. IncState(Regs[Reg].WState);
  517. End;
  518. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  519. Begin
  520. IncState(Regs[Reg].RState);
  521. End;
  522. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  523. Begin
  524. GetWState := Regs[Reg].WState
  525. End;
  526. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  527. Begin
  528. GetRState := Regs[Reg].RState
  529. End;
  530. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  531. Begin
  532. GetRegContentType := Regs[Reg].typ
  533. End;
  534. Destructor TPaiProp.Done;
  535. Begin
  536. UsedRegs.Done;
  537. CondRegs.Done;
  538. { DirFlag: TFlagContents; I386 specific}
  539. End;
  540. { ************************ private TPaiProp stuff ************************* }
  541. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  542. Begin
  543. If s <> High(TStateInt) Then Inc(s)
  544. Else s := 0
  545. End;
  546. Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Pai;
  547. RefsEq: TRefCompare): Boolean;
  548. Var Count: AWord;
  549. TmpResult: Boolean;
  550. Begin
  551. TmpResult := False;
  552. If (p^.typ = ait_instruction) Then
  553. Begin
  554. Count := 0;
  555. Repeat
  556. If (PInstr(p)^.oper[Count].typ = Top_Ref) Then
  557. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count].ref^);
  558. Inc(Count);
  559. Until (Count = MaxOps) or TmpResult;
  560. End;
  561. RefInInstruction := TmpResult;
  562. End;
  563. Function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  564. RefsEq: TRefCompare): Boolean;
  565. Var p: Pai;
  566. Counter: Byte;
  567. TmpResult: Boolean;
  568. Begin
  569. p := Content.StartMod;
  570. TmpResult := False;
  571. Counter := 1;
  572. While Not(TmpResult) And
  573. (Counter <= Content.NrOfMods) Do
  574. Begin
  575. If (p^.typ = ait_instruction) And
  576. RefInInstruction(Ref, p, {$ifdef fpc}@{$endif}RefsEqual)
  577. Then TmpResult := True;
  578. Inc(Counter);
  579. GetNextInstruction(p,p)
  580. End;
  581. RefInSequence := TmpResult
  582. End;
  583. { ************************************************************************* }
  584. { ***************************** TAoptObj ********************************** }
  585. { ************************************************************************* }
  586. Constructor TAoptObj.Init(_AsmL: PAasmOutput; _BlockStart, _BlockEnd: Pai;
  587. _LabelInfo: PLabelInfo);
  588. Begin
  589. AsmL := _AsmL;
  590. BlockStart := _BlockStart;
  591. BlockEnd := _BlockEnd;
  592. LabelInfo := _LabelInfo
  593. End;
  594. Function TAOptObj.FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  595. Var TempP: Pai;
  596. Begin
  597. TempP := hp;
  598. While Assigned(TempP) and
  599. (TempP^.typ In SkipInstr + [ait_label]) Do
  600. If (TempP^.typ <> ait_Label) Or
  601. (pai_label(TempP)^.l <> L)
  602. Then GetNextInstruction(TempP, TempP)
  603. Else
  604. Begin
  605. hp := TempP;
  606. FindLabel := True;
  607. exit
  608. End;
  609. FindLabel := False;
  610. End;
  611. Procedure TAOptObj.InsertLLItem(prev, foll, new_one: PLinkedList_Item);
  612. Begin
  613. If Assigned(prev) Then
  614. If Assigned(foll) Then
  615. Begin
  616. If Assigned(new_one) Then
  617. Begin
  618. new_one^.previous := prev;
  619. new_one^.next := foll;
  620. prev^.next := new_one;
  621. foll^.previous := new_one;
  622. Pai(new_one)^.fileinfo := Pai(foll)^.fileinfo
  623. End
  624. End
  625. Else AsmL^.Concat(new_one)
  626. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  627. End;
  628. Function TAOptObj.SkipHead(P: Pai): Pai;
  629. Var OldP: Pai;
  630. Begin
  631. Repeat
  632. OldP := P;
  633. If (P^.typ in SkipInstr) Or
  634. ((P^.typ = ait_marker) And
  635. (Pai_Marker(P)^.Kind = AsmBlockEnd)) Then
  636. GetNextInstruction(P, P)
  637. Else If ((P^.Typ = Ait_Marker) And
  638. (Pai_Marker(P)^.Kind = NoPropInfoStart)) Then
  639. { a marker of the type NoPropInfoStart can't be the first instruction of a }
  640. { paasmoutput list }
  641. GetNextInstruction(Pai(P^.Previous),P);
  642. If (P^.Typ = Ait_Marker) And
  643. (Pai_Marker(P)^.Kind = AsmBlockStart) Then
  644. Begin
  645. P := Pai(P^.Next);
  646. While (P^.typ <> Ait_Marker) Or
  647. (Pai_Marker(P)^.Kind <> AsmBlockEnd) Do
  648. P := Pai(P^.Next)
  649. End;
  650. Until P = OldP;
  651. SkipHead := P;
  652. End;
  653. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  654. Begin
  655. if o1.typ=o2.typ then
  656. Case o1.typ Of
  657. Top_Reg :
  658. OpsEqual:=o1.reg=o2.reg;
  659. Top_Ref :
  660. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  661. Top_Const :
  662. OpsEqual:=o1.val=o2.val;
  663. Top_Symbol :
  664. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  665. Top_None :
  666. OpsEqual := True
  667. else OpsEqual := False
  668. End;
  669. End;
  670. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  671. Begin
  672. FindRegAlloc:=False;
  673. Repeat
  674. While Assigned(StartPai) And
  675. ((StartPai^.typ in (SkipInstr - [ait_regAlloc])) Or
  676. ((StartPai^.typ = ait_label) and
  677. Not(Pai_Label(StartPai)^.l^.Is_Used))) Do
  678. StartPai := Pai(StartPai^.Next);
  679. If Assigned(StartPai) And
  680. (StartPai^.typ = ait_regAlloc) and (PairegAlloc(StartPai)^.allocation) Then
  681. Begin
  682. if PairegAlloc(StartPai)^.Reg = Reg then
  683. begin
  684. FindRegAlloc:=true;
  685. exit;
  686. end;
  687. StartPai := Pai(StartPai^.Next);
  688. End
  689. else
  690. exit;
  691. Until false;
  692. End;
  693. End.
  694. {
  695. $Log$
  696. Revision 1.8 2000-01-07 01:14:52 peter
  697. * updated copyright to 2000
  698. Revision 1.7 1999/11/09 22:57:08 peter
  699. * compiles again both i386,alpha both with optimizer
  700. Revision 1.6 1999/09/29 13:50:34 jonas
  701. * fixes from daopt386.pas integrated
  702. Revision 1.5 1999/08/26 14:50:52 jonas
  703. * fixed small type in TP conditional
  704. Revision 1.4 1999/08/18 14:32:22 jonas
  705. + compilable!
  706. + dataflow analyzer finished
  707. + start of CSE units
  708. + aoptbase which contains a base object for all optimizer objects
  709. * some constants and type definitions moved around to avoid circular
  710. dependencies
  711. * moved some methods from base objects to specialized objects because
  712. they're not used anywhere else
  713. Revision 1.2 1999/08/09 14:07:24 jonas
  714. commit.msg
  715. Revision 1.1 1999/08/08 13:24:50 jonas
  716. + added copyright header/GNU license info
  717. * made the assembler optimizer almost completely OOP
  718. * some code style clean up and extra comments
  719. * moved from the new/aopt to the /new and /new/i386 dirs
  720. }