aoptobj.pas 26 KB

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