aoptobj.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. { 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. { increase the write state of a register (call every time a register is }
  136. { written to) }
  137. Procedure IncWState(Reg: TRegister);
  138. { increase the read state of a register (call every time a register is }
  139. { read from) }
  140. Procedure IncRState(Reg: TRegister);
  141. { get the write state of a register }
  142. Function GetWState(Reg: TRegister): TStateInt;
  143. { get the read state of a register }
  144. Function GetRState(Reg: TRegister): TStateInt;
  145. { get the type of contents of a register }
  146. Function GetRegContentType(Reg: TRegister): Byte;
  147. Destructor Done;
  148. Private
  149. Procedure IncState(var s: TStateInt);
  150. { returns whether the reference Ref is used somewhere in the loading }
  151. { sequence Content }
  152. Function RefInSequence(Const Ref: TReference; Content: TContent;
  153. RefsEq: TRefCompare): Boolean;
  154. { returns whether the instruction P reads from and/or writes }
  155. { to Reg }
  156. Function RefInInstruction(Const Ref: TReference; p: Pai;
  157. RefsEq: TRefCompare): Boolean;
  158. { returns whether two references with at least one pointing to an array }
  159. { may point to the same memory location }
  160. End;
  161. { ************************************************************************* }
  162. { ************************ Label information ****************************** }
  163. { ************************************************************************* }
  164. TLabelTableItem = Record
  165. PaiObj: Pai;
  166. End;
  167. {$ifndef TP}
  168. TLabelTable = Array[0..2500000] Of TLabelTableItem;
  169. {$else TP}
  170. TLabelTable = Array[0..(65520 div sizeof(TLabelTableItem))] Of TLabelTableItem;
  171. {$endif TP}
  172. PLabelTable = ^TLabelTable;
  173. PLabelInfo = ^TLabelInfo;
  174. TLabelInfo = Record
  175. { the highest and lowest label number occurring in the current code }
  176. { fragment }
  177. LowLabel, HighLabel: AWord;
  178. LabelDif: AWord;
  179. { table that contains the addresses of the Pai_Label objects associated }
  180. { with each label number }
  181. LabelTable: PLabelTable;
  182. End;
  183. { ************************************************************************* }
  184. { ********** General optimizer object, used to derive others from ********* }
  185. { ************************************************************************* }
  186. TAOptObj = Object(TAoptBaseCpu)
  187. { the PAasmOutput list this optimizer instance works on }
  188. AsmL: PAasmOutput;
  189. { The labelinfo record contains the addresses of the Pai objects }
  190. { that are labels, how many labels there are and the min and max }
  191. { label numbers }
  192. LabelInfo: PLabelInfo;
  193. { Start and end of the block that is currently being optimized }
  194. BlockStart, BlockEnd: Pai;
  195. { _AsmL is the PAasmOutpout list that has to be optimized, }
  196. { _BlockStart and _BlockEnd the start and the end of the block }
  197. { that has to be optimized and _LabelInfo a pointer to a }
  198. { TLabelInfo record }
  199. Constructor Init(_AsmL: PAasmOutput; _BlockStart, _BlockEnd: Pai;
  200. _LabelInfo: PLabelInfo);
  201. { processor independent methods }
  202. { returns true if the label L is found between hp and the next }
  203. { instruction }
  204. Function FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  205. { inserts new_one between prev and foll in AsmL }
  206. Procedure InsertLLItem(prev, foll, new_one: PLinkedList_Item);
  207. { If P is a Pai object releveant to the optimizer, P is returned }
  208. { If it is not relevant tot he optimizer, the first object after P }
  209. { that is relevant is returned }
  210. Function SkipHead(P: Pai): Pai;
  211. { returns true if the operands o1 and o2 are completely equal }
  212. Function OpsEqual(const o1,o2:toper): Boolean;
  213. { Returns true if a ait_alloc object for Reg is found in the block }
  214. { of Pai's starting with StartPai and ending with the next "real" }
  215. { instruction }
  216. Function FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  217. { processor dependent methods }
  218. End;
  219. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  220. { ***************************** Implementation **************************** }
  221. Implementation
  222. uses globtype, globals, cgbase, tainst;
  223. { ************************************************************************* }
  224. { ******************************** TUsedRegs ****************************** }
  225. { ************************************************************************* }
  226. Constructor TUsedRegs.init;
  227. Begin
  228. UsedRegs := [];
  229. End;
  230. Constructor TUsedRegs.InitWithValue(Const _RegSet: TRegSet);
  231. Begin
  232. UsedRegs := _RegSet;
  233. End;
  234. Procedure TUsedRegs.Update(p: Pai);
  235. {updates UsedRegs with the RegAlloc Information coming after P}
  236. Begin
  237. Repeat
  238. While Assigned(p) And
  239. ((p^.typ in (SkipInstr - [ait_RegAlloc])) or
  240. ((p^.typ = ait_label) And
  241. Not(Pai_Label(p)^.l^.is_used))) Do
  242. p := Pai(p^.next);
  243. While Assigned(p) And
  244. (p^.typ=ait_RegAlloc) Do
  245. Begin
  246. if pairegalloc(p)^.allocation then
  247. UsedRegs := UsedRegs + [PaiRegAlloc(p)^.Reg]
  248. else
  249. UsedRegs := UsedRegs - [PaiRegAlloc(p)^.Reg];
  250. p := pai(p^.next);
  251. End;
  252. Until Not(Assigned(p)) Or
  253. (Not(p^.typ in SkipInstr) And
  254. Not((p^.typ = ait_label) And
  255. Not(Pai_Label(p)^.l^.is_used)));
  256. End;
  257. Function TUsedRegs.IsUsed(Reg: TRegister): Boolean;
  258. Begin
  259. IsUsed := Reg in UsedRegs
  260. End;
  261. Function TUsedRegs.GetUsedRegs: TRegSet;
  262. Begin
  263. GetUsedRegs := UsedRegs;
  264. End;
  265. Destructor TUsedRegs.Done; {$ifdef inl} inline; {$endif inl}
  266. Begin
  267. end;
  268. { ************************************************************************* }
  269. { **************************** TPaiProp *********************************** }
  270. { ************************************************************************* }
  271. Constructor TPaiProp.Init;
  272. Begin
  273. UsedRegs.Init;
  274. CondRegs.init;
  275. { DirFlag: TFlagContents; I386 specific}
  276. End;
  277. Function TPaiProp.RegInSequence(Reg, which: TRegister): Boolean;
  278. Var p: Pai;
  279. RegsChecked: TRegSet;
  280. content: TContent;
  281. Counter: Byte;
  282. TmpResult: Boolean;
  283. Begin
  284. RegsChecked := [];
  285. content := regs[which];
  286. p := content.StartMod;
  287. TmpResult := False;
  288. Counter := 1;
  289. While Not(TmpResult) And
  290. (Counter <= Content.NrOfMods) Do
  291. Begin
  292. If IsLoadMemReg(p) Then
  293. With PInstr(p)^.oper[LoadSrc].ref^ Do
  294. If (Base = ProcInfo^.FramePointer)
  295. {$ifdef RefsHaveIndexReg}
  296. And (Index = R_NO)
  297. {$endif RefsHaveIndexReg} Then
  298. Begin
  299. RegsChecked := RegsChecked +
  300. [RegMaxSize(PInstr(p)^.oper[LoadDst].reg)];
  301. If Reg = RegMaxSize(PInstr(p)^.oper[LoadDst].reg) Then
  302. Break;
  303. End
  304. Else
  305. Begin
  306. If (Base = Reg) And
  307. Not(Base In RegsChecked)
  308. Then TmpResult := True;
  309. {$ifdef RefsHaveIndexReg}
  310. If Not(TmpResult) And
  311. (Index = Reg) And
  312. Not(Index In RegsChecked)
  313. Then TmpResult := True;
  314. {$Endif RefsHaveIndexReg}
  315. End
  316. Else TmpResult := RegInInstruction(Reg, p);
  317. Inc(Counter);
  318. GetNextInstruction(p,p)
  319. End;
  320. RegInSequence := TmpResult
  321. End;
  322. Procedure TPaiProp.DestroyReg(Reg: TRegister; var InstrSinceLastMod:
  323. TInstrSinceLastMod);
  324. { Destroys the contents of the register Reg in the PPaiProp p1, as well as }
  325. { the contents of registers are loaded with a memory location based on Reg }
  326. Var TmpWState, TmpRState: Byte;
  327. Counter: TRegister;
  328. Begin
  329. Reg := RegMaxSize(Reg);
  330. If (Reg in [LoGPReg..HiGPReg]) Then
  331. For Counter := LoGPReg to HiGPReg Do
  332. With Regs[Counter] Do
  333. If (Counter = reg) Or
  334. ((Typ = Con_Ref) And
  335. RegInSequence(Reg, Counter)) Then
  336. Begin
  337. InstrSinceLastMod[Counter] := 0;
  338. IncWState(Counter);
  339. TmpWState := GetWState(Counter);
  340. TmpRState := GetRState(Counter);
  341. FillChar(Regs[Counter], SizeOf(TContent), 0);
  342. WState := TmpWState;
  343. RState := TmpRState
  344. End
  345. End;
  346. Function ArrayRefsEq(const r1, r2: TReference): Boolean;
  347. Begin
  348. ArrayRefsEq := (R1.Offset+R1.OffsetFixup = R2.Offset+R2.OffsetFixup) And
  349. {$ifdef refsHaveSegmentReg}
  350. (R1.Segment = R2.Segment) And
  351. {$endif}
  352. (R1.Base = R2.Base) And
  353. (R1.Symbol=R2.Symbol);
  354. End;
  355. Procedure TPaiProp.DestroyRefs(Const Ref: TReference; WhichReg: TRegister;
  356. var InstrSinceLastMod: TInstrSinceLastMod);
  357. { destroys all registers which possibly contain a reference to Ref, WhichReg }
  358. { is the register whose contents are being written to memory (if this proc }
  359. { is called because of a "mov?? %reg, (mem)" instruction) }
  360. Var RefsEq: TRefCompare;
  361. Counter: TRegister;
  362. Begin
  363. WhichReg := RegMaxSize(WhichReg);
  364. If (Ref.base = procinfo^.FramePointer) or
  365. Assigned(Ref.Symbol) Then
  366. Begin
  367. If
  368. {$ifdef refsHaveIndexReg}
  369. (Ref.Index = R_NO) And
  370. {$endif refsHaveIndexReg}
  371. (Not(Assigned(Ref.Symbol)) or
  372. (Ref.base = R_NO)) Then
  373. { local variable which is not an array }
  374. RefsEq := {$ifdef fpc}@{$endif}RefsEqual
  375. Else
  376. { local variable which is an array }
  377. RefsEq := {$ifdef fpc}@{$endif}ArrayRefsEq;
  378. {write something to a parameter, a local or global variable, so
  379. * with uncertain optimizations on:
  380. - destroy the contents of registers whose contents have somewhere a
  381. "mov?? (Ref), %reg". WhichReg (this is the register whose contents
  382. are being written to memory) is not destroyed if it's StartMod is
  383. of that form and NrOfMods = 1 (so if it holds ref, but is not a
  384. pointer or value based on Ref)
  385. * with uncertain optimizations off:
  386. - also destroy registers that contain any pointer}
  387. For Counter := LoGPReg to HiGPReg Do
  388. With Regs[Counter] Do
  389. Begin
  390. If (typ = Con_Ref) And
  391. ((Not(cs_UncertainOpts in aktglobalswitches) And
  392. (NrOfMods <> 1)
  393. ) Or
  394. (RefInSequence(Ref,Regs[Counter], RefsEq) And
  395. ((Counter <> WhichReg) Or
  396. ((NrOfMods <> 1) And
  397. {StarMod is always of the type ait_instruction}
  398. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  399. RefsEq(PInstr(StartMod)^.oper[0].ref^, Ref)
  400. )
  401. )
  402. )
  403. )
  404. Then
  405. DestroyReg(Counter, InstrSinceLastMod)
  406. End
  407. End
  408. Else
  409. {write something to a pointer location, so
  410. * with uncertain optimzations on:
  411. - do not destroy registers which contain a local/global variable or a
  412. parameter, except if DestroyRefs is called because of a "movsl"
  413. * with uncertain optimzations off:
  414. - destroy every register which contains a memory location
  415. }
  416. For Counter := LoGPReg to HiGPReg Do
  417. With Regs[Counter] Do
  418. If (typ = Con_Ref) And
  419. (Not(cs_UncertainOpts in aktglobalswitches) Or
  420. {$ifdef i386}
  421. {for movsl}
  422. (Ref.Base = R_EDI) Or
  423. {$endif}
  424. {don't destroy if reg contains a parameter, local or global variable}
  425. Not((NrOfMods = 1) And
  426. (PInstr(StartMod)^.oper[0].typ = top_ref) And
  427. ((PInstr(StartMod)^.oper[0].ref^.base = ProcInfo^.FramePointer) Or
  428. Assigned(PInstr(StartMod)^.oper[0].ref^.Symbol)
  429. )
  430. )
  431. )
  432. Then DestroyReg(Counter, InstrSinceLastMod)
  433. End;
  434. Procedure TPaiProp.DestroyAllRegs(var InstrSinceLastMod: TInstrSinceLastMod);
  435. Var Counter: TRegister;
  436. Begin {initializes/desrtoys all registers}
  437. For Counter := LoGPReg To HiGPReg Do
  438. Begin
  439. ReadReg(Counter);
  440. DestroyReg(Counter, InstrSinceLastMod);
  441. End;
  442. CondRegs.Init;
  443. { FPURegs.Init; }
  444. End;
  445. Procedure TPaiProp.DestroyOp(const o:Toper; var InstrSinceLastMod:
  446. TInstrSinceLastMod);
  447. Begin
  448. Case o.typ Of
  449. top_reg: DestroyReg(o.reg, InstrSinceLastMod);
  450. top_ref:
  451. Begin
  452. ReadRef(o.ref);
  453. DestroyRefs(o.ref^, R_NO, InstrSinceLastMod);
  454. End;
  455. top_symbol:;
  456. End;
  457. End;
  458. Procedure TPaiProp.ReadReg(Reg: TRegister);
  459. Begin
  460. Reg := RegMaxSize(Reg);
  461. If Reg in General_Registers Then
  462. IncRState(RegMaxSize(Reg))
  463. End;
  464. Procedure TPaiProp.ReadRef(Ref: PReference);
  465. Begin
  466. If Ref^.Base <> R_NO Then
  467. ReadReg(Ref^.Base);
  468. {$ifdef refsHaveIndexReg}
  469. If Ref^.Index <> R_NO Then
  470. ReadReg(Ref^.Index);
  471. {$endif}
  472. End;
  473. Procedure TPaiProp.ReadOp(const o:toper);
  474. Begin
  475. Case o.typ Of
  476. top_reg: ReadReg(o.reg);
  477. top_ref: ReadRef(o.ref);
  478. top_symbol : ;
  479. End;
  480. End;
  481. Procedure TPaiProp.ModifyReg(reg: TRegister; Var InstrSinceLastMod:
  482. TInstrSinceLastMod);
  483. Begin
  484. With Regs[reg] Do
  485. If (Typ = Con_Ref)
  486. Then
  487. Begin
  488. IncState(WState);
  489. {also store how many instructions are part of the sequence in the first
  490. instructions PPaiProp, so it can be easily accessed from within
  491. CheckSequence}
  492. Inc(NrOfMods, NrOfInstrSinceLastMod[Reg]);
  493. PPaiProp(StartMod^.OptInfo)^.Regs[Reg].NrOfMods := NrOfMods;
  494. NrOfInstrSinceLastMod[Reg] := 0;
  495. End
  496. Else
  497. DestroyReg(Reg, InstrSinceLastMod);
  498. End;
  499. Procedure TPaiProp.ModifyOp(const oper: TOper; var InstrSinceLastMod:
  500. TInstrSinceLastMod);
  501. Begin
  502. If oper.typ = top_reg Then
  503. ModifyReg(RegMaxSize(oper.reg))
  504. Else
  505. Begin
  506. ReadOp(oper);
  507. DestroyOp(oper, InstrSinceLastMod);
  508. End
  509. End;
  510. Procedure TPaiProp.IncWState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  511. Begin
  512. IncState(Regs[Reg].WState);
  513. End;
  514. Procedure TPaiProp.IncRState(Reg: TRegister);{$ifdef inl} inline;{$endif inl}
  515. Begin
  516. IncState(Regs[Reg].RState);
  517. End;
  518. Function TPaiProp.GetWState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  519. Begin
  520. GetWState := Regs[Reg].WState
  521. End;
  522. Function TPaiProp.GetRState(Reg: TRegister): TStateInt; {$ifdef inl} inline;{$endif inl}
  523. Begin
  524. GetRState := Regs[Reg].RState
  525. End;
  526. Function TPaiProp.GetRegContentType(Reg: TRegister): Byte; {$ifdef inl} inline;{$endif inl}
  527. Begin
  528. GetRegContentType := Regs[Reg].typ
  529. End;
  530. Destructor TPaiProp.Done;
  531. Begin
  532. UsedRegs.Done;
  533. CondRegs.Done;
  534. { DirFlag: TFlagContents; I386 specific}
  535. End;
  536. { ************************ private TPaiProp stuff ************************* }
  537. Procedure TPaiProp.IncState(Var s: TStateInt); {$ifdef inl} inline;{$endif inl}
  538. Begin
  539. If s <> High(TStateInt) Then Inc(s)
  540. Else s := 0
  541. End;
  542. Function TPaiProp.RefInInstruction(Const Ref: TReference; p: Pai;
  543. RefsEq: TRefCompare): Boolean;
  544. Var Count: AWord;
  545. TmpResult: Boolean;
  546. Begin
  547. TmpResult := False;
  548. If (p^.typ = ait_instruction) Then
  549. Begin
  550. Count := 0;
  551. Repeat
  552. If (PInstr(p)^.oper[Count].typ = Top_Ref) Then
  553. TmpResult := RefsEq(Ref, PInstr(p)^.oper[Count].ref^);
  554. Inc(Count);
  555. Until (Count = MaxOps) or TmpResult;
  556. End;
  557. RefInInstruction := TmpResult;
  558. End;
  559. Function TPaiProp.RefInSequence(Const Ref: TReference; Content: TContent;
  560. RefsEq: TRefCompare): Boolean;
  561. Var p: Pai;
  562. Counter: Byte;
  563. TmpResult: Boolean;
  564. Begin
  565. p := Content.StartMod;
  566. TmpResult := False;
  567. Counter := 1;
  568. While Not(TmpResult) And
  569. (Counter <= Content.NrOfMods) Do
  570. Begin
  571. If (p^.typ = ait_instruction) And
  572. RefInInstruction(Ref, p, {$ifdef fpc}@{$endif}RefsEqual)
  573. Then TmpResult := True;
  574. Inc(Counter);
  575. GetNextInstruction(p,p)
  576. End;
  577. RefInSequence := TmpResult
  578. End;
  579. { ************************************************************************* }
  580. { ***************************** TAoptObj ********************************** }
  581. { ************************************************************************* }
  582. Constructor TAoptObj.Init(_AsmL: PAasmOutput; _BlockStart, _BlockEnd: Pai;
  583. _LabelInfo: PLabelInfo);
  584. Begin
  585. AsmL := _AsmL;
  586. BlockStart := _BlockStart;
  587. BlockEnd := _BlockEnd;
  588. LabelInfo := _LabelInfo
  589. End;
  590. Function TAOptObj.FindLabel(L: PasmLabel; Var hp: Pai): Boolean;
  591. Var TempP: Pai;
  592. Begin
  593. TempP := hp;
  594. While Assigned(TempP) and
  595. (TempP^.typ In SkipInstr + [ait_label]) Do
  596. If (TempP^.typ <> ait_Label) Or
  597. (pai_label(TempP)^.l <> L)
  598. Then GetNextInstruction(TempP, TempP)
  599. Else
  600. Begin
  601. hp := TempP;
  602. FindLabel := True;
  603. exit
  604. End;
  605. FindLabel := False;
  606. End;
  607. Procedure TAOptObj.InsertLLItem(prev, foll, new_one: PLinkedList_Item);
  608. Begin
  609. If Assigned(prev) Then
  610. If Assigned(foll) Then
  611. Begin
  612. If Assigned(new_one) Then
  613. Begin
  614. new_one^.previous := prev;
  615. new_one^.next := foll;
  616. prev^.next := new_one;
  617. foll^.previous := new_one;
  618. Pai(new_one)^.fileinfo := Pai(foll)^.fileinfo
  619. End
  620. End
  621. Else AsmL^.Concat(new_one)
  622. Else If Assigned(Foll) Then AsmL^.Insert(new_one)
  623. End;
  624. Function TAOptObj.SkipHead(P: Pai): Pai;
  625. Var OldP: Pai;
  626. Begin
  627. Repeat
  628. OldP := P;
  629. If (P^.typ in SkipInstr) Or
  630. ((P^.typ = ait_marker) And
  631. (Pai_Marker(P)^.Kind = AsmBlockEnd)) Then
  632. GetNextInstruction(P, P)
  633. Else If ((P^.Typ = Ait_Marker) And
  634. (Pai_Marker(P)^.Kind = NoPropInfoStart)) Then
  635. { a marker of the type NoPropInfoStart can't be the first instruction of a }
  636. { paasmoutput list }
  637. GetNextInstruction(Pai(P^.Previous),P);
  638. If (P^.Typ = Ait_Marker) And
  639. (Pai_Marker(P)^.Kind = AsmBlockStart) Then
  640. Begin
  641. P := Pai(P^.Next);
  642. While (P^.typ <> Ait_Marker) Or
  643. (Pai_Marker(P)^.Kind <> AsmBlockEnd) Do
  644. P := Pai(P^.Next)
  645. End;
  646. Until P = OldP;
  647. SkipHead := P;
  648. End;
  649. Function TAOptObj.OpsEqual(const o1,o2:toper): Boolean;
  650. Begin
  651. if o1.typ=o2.typ then
  652. Case o1.typ Of
  653. Top_Reg :
  654. OpsEqual:=o1.reg=o2.reg;
  655. Top_Ref :
  656. OpsEqual := RefsEqual(o1.ref^, o2.ref^);
  657. Top_Const :
  658. OpsEqual:=o1.val=o2.val;
  659. Top_Symbol :
  660. OpsEqual:=(o1.sym=o2.sym) and (o1.symofs=o2.symofs);
  661. Top_None :
  662. OpsEqual := True
  663. else OpsEqual := False
  664. End;
  665. End;
  666. Function TAOptObj.FindRegAlloc(Reg: TRegister; StartPai: Pai): Boolean;
  667. Begin
  668. FindRegAlloc:=False;
  669. Repeat
  670. While Assigned(StartPai) And
  671. ((StartPai^.typ in (SkipInstr - [ait_regAlloc])) Or
  672. ((StartPai^.typ = ait_label) and
  673. Not(Pai_Label(StartPai)^.l^.Is_Used))) Do
  674. StartPai := Pai(StartPai^.Next);
  675. If Assigned(StartPai) And
  676. (StartPai^.typ = ait_regAlloc) and (PairegAlloc(StartPai)^.allocation) Then
  677. Begin
  678. if PairegAlloc(StartPai)^.Reg = Reg then
  679. begin
  680. FindRegAlloc:=true;
  681. exit;
  682. end;
  683. StartPai := Pai(StartPai^.Next);
  684. End
  685. else
  686. exit;
  687. Until false;
  688. End;
  689. End.
  690. {
  691. $Log$
  692. Revision 1.3 2002-09-07 15:25:14 peter
  693. * old logs removed and tabs fixed
  694. }