aoptobj.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. 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. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  163. End;
  164. { ************************************************************************* }
  165. { ************************ Label information ****************************** }
  166. { ************************************************************************* }
  167. TLabelTableItem = Record
  168. PaiObj: Pai;
  169. End;
  170. {$ifndef TP}
  171. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  172. {$else TP}
  173. TLabelTable = Array[0..(65520 div sizeof(TLabelTableItem))] Of TLabelTableItem;
  174. {$endif TP}
  175. PLabelTable = ^TLabelTable;
  176. PLabelInfo = ^TLabelInfo;
  177. TLabelInfo = Record
  178. { the highest and lowest label number occurring in the current code }
  179. { fragment }
  180. LowLabel, HighLabel: AWord;
  181. LabelDif: AWord;
  182. { table that contains the addresses of the Pai_Label objects associated }
  183. { with each label number }
  184. LabelTable: PLabelTable;
  185. End;
  186. { ************************************************************************* }
  187. { ********** General optimizer object, used to derive others from ********* }
  188. { ************************************************************************* }
  189. TAOptObj = Object(TAoptBaseCpu)
  190. { the PAasmOutput list this optimizer instance works on }
  191. AsmL: PAasmOutput;
  192. { The labelinfo record contains the addresses of the Pai objects }
  193. { that are labels, how many labels there are and the min and max }
  194. { label numbers }
  195. LabelInfo: PLabelInfo;
  196. { Start and end of the block that is currently being optimized }
  197. BlockStart, BlockEnd: Pai;
  198. { _AsmL is the PAasmOutpout list that has to be optimized, }
  199. { _BlockStart and _BlockEnd the start and the end of the block }
  200. { that has to be optimized and _LabelInfo a pointer to a }
  201. { TLabelInfo record }
  202. Constructor Init(_AsmL: PAasmOutput; _BlockStart, _BlockEnd: Pai;
  203. _LabelInfo: PLabelInfo);
  204. { processor independent methods }
  205. { returns true if the label L is found between hp and the next }
  206. { instruction }
  207. Function FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  208. { inserts new_one between prev and foll in AsmL }
  209. Procedure InsertLLItem(prev, foll, new_one: PLinkedList_Item);
  210. { If P is a Pai object releveant to the optimizer, P is returned }
  211. { If it is not relevant tot he optimizer, the first object after P }
  212. { that is relevant is returned }
  213. Function SkipHead(P: Pai): Pai;
  214. { returns true if the operands o1 and o2 are completely equal }
  215. Function OpsEqual(const o1,o2:toper): Boolean;
  216. { Returns true if a ait_alloc object for Reg is found in the block }
  217. { of Pai's starting with StartPai and ending with the next "real" }
  218. { instruction }
  219. Function FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  220. { processor dependent methods }
  221. End;
  222. { ***************************** Implementation **************************** }
  223. Implementation
  224. uses hcodegen, globtype, globals, 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 [R_EAX..R_EDI]) Then
  333. For Counter := R_EAX to R_EDI 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 TPaiProp.ArrayRefsEq(const r1, r2: TReference): Boolean;
  349. Begin
  350. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  351. (R1.Segment = R2.Segment) And (R1.Base = R2.Base) And
  352. (R1.Symbol=R2.Symbol);
  353. End;
  354. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  355. var InstrSinceLastMod: TInstrSinceLastMod);
  356. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  357. { is the register whose contents are being written to memory (if this proc }
  358. { is called because of a "mov?? %reg, (mem)" instruction) }
  359. Var RefsEq: TRefCompare;
  360. Counter: TRegister;
  361. Begin
  362. WhichReg := RegMaxSize(WhichReg);
  363. If (Ref.base = procinfo^.FramePointer) or
  364. Assigned(Ref.Symbol) Then
  365. Begin
  366. If
  367. {$ifdef refsHaveIndexReg}
  368. (Ref.Index = R_NO) And
  369. {$endif refsHaveIndexReg}
  370. (Not(Assigned(Ref.Symbol)) or
  371. (Ref.base = R_NO)) Then
  372. { local variable which is not an array }
  373. RefsEq := {$ifdef fpc}@{$endif}RefsEqual
  374. Else
  375. { local variable which is an array }
  376. RefsEq := {$ifdef fpc}@{$endif}ArrayRefsEq;
  377. {write something to a parameter, a local or global variable, so
  378. * with uncertain optimizations on:
  379. - destroy the contents of registers whose contents have somewhere a
  380. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  381. are being written to memory) is not destroyed if it's StartMod is
  382. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  383. pointer or value based on Ref)
  384. * with uncertain optimizations off:
  385. - also destroy registers that contain any pointer}
  386. For Counter := LoGPReg to HiGPReg Do
  387. With Regs[Counter] Do
  388. Begin
  389. If (typ = Con_Ref) And
  390. ((Not(cs_UncertainOpts in aktglobalswitches) And
  391. (NrOfMods <> 1)
  392. ) Or
  393. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  394. ((Counter <> WhichReg) Or
  395. ((NrOfMods <> 1) And
  396. {StarMod is always of the type ait_instruction}
  397. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  398. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  399. )
  400. )
  401. )
  402. )
  403. Then
  404. DestroyReg(Counter, InstrSinceLastMod)
  405. End
  406. Else
  407. {write something to a pointer location, so
  408. * with uncertain optimzations on:
  409. - do not destroy registers which contain a local/global variable or a
  410. parameter, except if DestroyRefs is called because of a "movsl"
  411. * with uncertain optimzations off:
  412. - destroy every register which contains a memory location
  413. }
  414. For Counter := LoGPReg to HiGPReg Do
  415. With Regs[Counter] Do
  416. If (typ = Con_Ref) And
  417. (Not(cs_UncertainOpts in aktglobalswitches) Or
  418. {$ifdef i386}
  419. {for movsl}
  420. (Ref.Base = R_EDI) Or
  421. {$endif}
  422. {don't destroy if reg contains a parameter, local or global variable}
  423. Not((NrOfMods = 1) And
  424. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  425. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo.FramePointer) Or
  426. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  427. )
  428. )
  429. )
  430. Then DestroyReg(Counter, InstrSinceLastMod)
  431. End;
  432. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  433. Var Counter: TRegister;
  434. Begin {initializes/desrtoys all registers}
  435. For Counter := R_EAX To R_EDI Do
  436. Begin
  437. ReadReg(Counter);
  438. DestroyReg(Counter, InstrSinceLastMod);
  439. End;
  440. CondRegs.Init;
  441. { FPURegs.Init; }
  442. End;
  443. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  444. TInstrSinceLastMod);
  445. Begin
  446. Case o.typ Of
  447. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  448. top_ref:
  449. Begin
  450. ReadRef(o.ref^);
  451. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  452. End;
  453. top_symbol:;
  454. End;
  455. End;
  456. Procedure TPaiProp.ReadReg(Reg: TRegister);
  457. Begin
  458. Reg := RegMaxSize(Reg);
  459. If Reg in General_Registers Then
  460. IncRState(RegMaxSize(Reg))
  461. End;
  462. Procedure TPaiProp.ReadRef(Ref: PReference);
  463. Begin
  464. If Ref^.Base <> R_NO Then
  465. ReadReg(Ref^.Base);
  466. If Ref^.Index <> R_NO Then
  467. ReadReg(Ref^.Index);
  468. End;
  469. Procedure TPaiProp.ReadOp(const o:toper);
  470. Begin
  471. Case o.typ Of
  472. top_reg: ReadReg(o.reg);
  473. top_ref: ReadRef(o.ref);
  474. top_symbol : ;
  475. End;
  476. End;
  477. {$ifdef arithopt}
  478. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  479. TInstrSinceLastMod);
  480. Begin
  481. With Regs[reg] Do
  482. If (Typ = Con_Ref)
  483. Then
  484. Begin
  485. IncState(WState);
  486. {also store how many instructions are part of the sequence in the first
  487. instructions PPaiProp, so it can be easily accessed from within
  488. CheckSequence}
  489. Inc(NrOfMods, NrOfInstrSinceLastMod[Reg]);
  490. PPaiProp(StartMod^.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  491. NrOfInstrSinceLastMod[Reg] := 0;
  492. End
  493. Else
  494. DestroyReg(Reg, InstrSinceLastMod);
  495. End;
  496. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  497. TInstrSinceLastMod);
  498. Begin
  499. If oper.typ = top_reg Then
  500. ModifyReg(RegMaxSize(oper.reg))
  501. Else
  502. Begin
  503. ReadOp(oper);
  504. DestroyOp(oper, InstrSinceLastMod);
  505. End
  506. End;
  507. {$endif arithopt}
  508. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  509. Begin
  510. IncState(Regs[Reg].WState);
  511. End;
  512. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  513. Begin
  514. IncState(Regs[Reg].RState);
  515. End;
  516. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  517. Begin
  518. GetWState := Regs[Reg].WState
  519. End;
  520. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  521. Begin
  522. GetRState := Regs[Reg].RState
  523. End;
  524. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  525. Begin
  526. GetRegContentType := Regs[Reg].typ
  527. End;
  528. Destructor TPaiProp.Done;
  529. Begin
  530. UsedRegs.Done;
  531. CondRegs.Done;
  532. { DirFlag: TFlagContents; I386 specific}
  533. End;
  534. { ************************ private TPaiProp stuff ************************* }
  535. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  536. Begin
  537. If s <> High(TStateInt) Then Inc(s)
  538. Else s := 0
  539. End;
  540. Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Pai;
  541. RefsEq: TRefCompare): Boolean;
  542. Var Count: AWord;
  543. TmpResult: Boolean;
  544. Begin
  545. TmpResult := False;
  546. If (p^.typ = ait_instruction) Then
  547. Begin
  548. Count := 0;
  549. Repeat
  550. If (PInstr(p)^.oper[Count].typ = Top_Ref) Then
  551. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count].ref^);
  552. Inc(Count);
  553. Until (Count = MaxOps) or TmpResult;
  554. End;
  555. RefInInstruction := TmpResult;
  556. End;
  557. Function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  558. RefsEq: TRefCompare): Boolean;
  559. Var p: Pai;
  560. Counter: Byte;
  561. TmpResult: Boolean;
  562. Begin
  563. p := Content.StartMod;
  564. TmpResult := False;
  565. Counter := 1;
  566. While Not(TmpResult) And
  567. (Counter <= Content.NrOfMods) Do
  568. Begin
  569. If (p^.typ = ait_instruction) And
  570. RefInInstruction(Ref, p)
  571. Then TmpResult := True;
  572. Inc(Counter);
  573. GetNextInstruction(p,p)
  574. End;
  575. RefInSequence := TmpResult
  576. End;
  577. { ************************************************************************* }
  578. { ***************************** TAoptObj ********************************** }
  579. { ************************************************************************* }
  580. Constructor TAoptObj.Init(_AsmL: PAasmOutput; _BlockStart, _BlockEnd: Pai;
  581. _LabelInfo: PLabelInfo);
  582. Begin
  583. AsmL := _AsmL;
  584. BlockStart := _BlockStart;
  585. BlockEnd := _BlockEnd;
  586. LabelInfo := _LabelInfo
  587. End;
  588. Function TAOptObj.FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  589. Var TempP: Pai;
  590. Begin
  591. TempP := hp;
  592. While Assigned(TempP) and
  593. (TempP^.typ In SkipInstr + [ait_label]) Do
  594. If (TempP^.typ <> ait_Label) Or
  595. (pai_label(TempP)^.l <> L)
  596. Then GetNextInstruction(TempP, TempP)
  597. Else
  598. Begin
  599. hp := TempP;
  600. FindLabel := True;
  601. exit
  602. End;
  603. FindLabel := False;
  604. End;
  605. Procedure TAOptObj.InsertLLItem(prev, foll, new_one: PLinkedList_Item);
  606. Begin
  607. If Assigned(prev) Then
  608. If Assigned(foll) Then
  609. Begin
  610. If Assigned(new_one) Then
  611. Begin
  612. new_one^.previous := prev;
  613. new_one^.next := foll;
  614. prev^.next := new_one;
  615. foll^.previous := new_one;
  616. Pai(new_one)^.fileinfo := Pai(foll)^.fileinfo
  617. End
  618. End
  619. Else AsmL^.Concat(new_one)
  620. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  621. End;
  622. Function TAOptObj.SkipHead(P: Pai): Pai;
  623. Var OldP: Pai;
  624. Begin
  625. Repeat
  626. OldP := P;
  627. If (P^.typ in SkipInstr) Or
  628. ((P^.typ = ait_marker) And
  629. (Pai_Marker(P)^.Kind = AsmBlockEnd)) Then
  630. GetNextInstruction(P, P)
  631. Else If ((P^.Typ = Ait_Marker) And
  632. (Pai_Marker(P)^.Kind = NoPropInfoStart)) Then
  633. { a marker of the type NoPropInfoStart can't be the first instruction of a }
  634. { paasmoutput list }
  635. GetNextInstruction(Pai(P^.Previous),P);
  636. If (P^.Typ = Ait_Marker) And
  637. (Pai_Marker(P)^.Kind = AsmBlockStart) Then
  638. Begin
  639. P := Pai(P^.Next);
  640. While (P^.typ <> Ait_Marker) Or
  641. (Pai_Marker(P)^.Kind <> AsmBlockEnd) Do
  642. P := Pai(P^.Next)
  643. End;
  644. Until P = OldP;
  645. SkipHead := P;
  646. End;
  647. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  648. Begin
  649. if o1.typ=o2.typ then
  650. Case o1.typ Of
  651. Top_Reg :
  652. OpsEqual:=o1.reg=o2.reg;
  653. Top_Ref :
  654. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  655. Top_Const :
  656. OpsEqual:=o1.val=o2.val;
  657. Top_Symbol :
  658. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  659. Top_None :
  660. OpsEqual := True
  661. else OpsEqual := False
  662. End;
  663. End;
  664. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  665. Begin
  666. FindRegAlloc:=False;
  667. Repeat
  668. While Assigned(StartPai) And
  669. ((StartPai^.typ in (SkipInstr - [ait_regAlloc])) Or
  670. ((StartPai^.typ = ait_label) and
  671. Not(Pai_Label(StartPai)^.l^.Is_Used))) Do
  672. StartPai := Pai(StartPai^.Next);
  673. If Assigned(StartPai) And
  674. (StartPai^.typ = ait_regAlloc) and (PairegAlloc(StartPai)^.allocation) Then
  675. Begin
  676. if PairegAlloc(StartPai)^.Reg = Reg then
  677. begin
  678. FindRegAlloc:=true;
  679. exit;
  680. end;
  681. StartPai := Pai(StartPai^.Next);
  682. End
  683. else
  684. exit;
  685. Until false;
  686. End;
  687. End.
  688. {
  689. $Log$
  690. Revision 1.6 1999-09-29 13:50:34 jonas
  691. * fixes from daopt386.pas integrated
  692. Revision 1.5 1999/08/26 14:50:52 jonas
  693. * fixed small type in TP conditional
  694. Revision 1.4 1999/08/18 14:32:22 jonas
  695. + compilable!
  696. + dataflow analyzer finished
  697. + start of CSE units
  698. + aoptbase which contains a base object for all optimizer objects
  699. * some constants and type definitions moved around to avoid circular
  700. dependencies
  701. * moved some methods from base objects to specialized objects because
  702. they're not used anywhere else
  703. Revision 1.2 1999/08/09 14:07:24 jonas
  704. commit.msg
  705. Revision 1.1 1999/08/08 13:24:50 jonas
  706. + added copyright header/GNU license info
  707. * made the assembler optimizer almost completely OOP
  708. * some code style clean up and extra comments
  709. * moved from the new/aopt to the /new and /new/i386 dirs
  710. }