aoptbase.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  4. Development Team
  5. This unit contains the base of all optimizer related objects
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit aoptbase;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. aasmbase,aasmcpu,aasmtai,
  24. cpubase,
  25. cgbase,
  26. cgutils;
  27. Type
  28. { the number of tai objects processed by an optimizer object since the last
  29. time a register was modified }
  30. { size at each dimension depends on the registers of this type }
  31. TInstrSinceLastMod = Array[tregistertype] of pbyte;
  32. { the TAopBase object implements the basic methods that most other }
  33. { assembler optimizer objects require }
  34. Type
  35. TAoptBase = class
  36. { processor independent methods }
  37. constructor create;
  38. destructor destroy;override;
  39. { returns true if register Reg is used by instruction p1 }
  40. Function RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  41. { returns true if register Reg occurs in operand op }
  42. Function RegInOp(Reg: TRegister; const op: toper): Boolean;
  43. { returns true if register Reg is used in the reference Ref }
  44. Function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  45. { returns true if the references are completely equal }
  46. {Function RefsEqual(Const R1, R2: TReference): Boolean;}
  47. { gets the next tai object after current that contains info relevant }
  48. { to the optimizer in p1. If there is none, it returns false and }
  49. { sets p1 to nil }
  50. Function GetNextInstruction(Current: tai; Var Next: tai): Boolean;
  51. { gets the previous tai object after current that contains info }
  52. { relevant to the optimizer in last. If there is none, it retuns }
  53. { false and sets last to nil }
  54. Function GetLastInstruction(Current: tai; Var Last: tai): Boolean;
  55. { processor dependent methods }
  56. { returns the maximum width component of Reg. Only has to be }
  57. { overridden for the 80x86 (afaik) }
  58. Function RegMaxSize(Reg: TRegister): TRegister; Virtual;
  59. { returns true if Reg1 and Reg2 are of the samae width. Only has to }
  60. { overridden for the 80x86 (afaik) }
  61. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean; Virtual;
  62. { returns whether P is a load instruction (load contents from a }
  63. { memory location or (register) variable into a register) }
  64. Function IsLoadMemReg(p: tai): Boolean; Virtual;
  65. { returns whether P is a load constant instruction (load a constant }
  66. { into a register) }
  67. Function IsLoadConstReg(p: tai): Boolean; Virtual;
  68. { returns whether P is a store instruction (store contents from a }
  69. { register to a memory location or to a (register) variable) }
  70. Function IsStoreRegMem(p: tai): Boolean; Virtual;
  71. { create a paicpu Object that loads the contents of reg1 into reg2 }
  72. Function a_load_reg_reg(reg1, reg2: TRegister): taicpu; Virtual;
  73. end;
  74. implementation
  75. uses
  76. globtype,globals, aoptcpub;
  77. constructor taoptbase.create;
  78. begin
  79. inherited create;
  80. end;
  81. destructor taoptbase.destroy;
  82. begin
  83. inherited destroy;
  84. end;
  85. Function TAOptBase.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  86. Var Count: AWord;
  87. TmpResult: Boolean;
  88. Begin
  89. TmpResult := False;
  90. Count := 0;
  91. If (p1.typ = ait_instruction) Then
  92. Repeat
  93. TmpResult := RegInOp(Reg, PInstr(p1)^.oper[Count]^);
  94. Inc(Count)
  95. Until (Count = MaxOps) or TmpResult;
  96. RegInInstruction := TmpResult
  97. End;
  98. Function TAOptBase.RegInOp(Reg: TRegister; const op: toper): Boolean;
  99. Begin
  100. Case op.typ Of
  101. Top_Reg: RegInOp := Reg = op.reg;
  102. Top_Ref: RegInOp := RegInRef(Reg, op.ref^)
  103. Else RegInOp := False
  104. End
  105. End;
  106. Function TAOptBase.RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  107. Begin
  108. Reg := RegMaxSize(Reg);
  109. RegInRef := (Ref.Base = Reg)
  110. {$ifdef RefsHaveIndexReg}
  111. Or (Ref.Index = Reg)
  112. {$endif RefsHaveIndexReg}
  113. End;
  114. Function TAOptBase.GetNextInstruction(Current: tai; Var Next: tai): Boolean;
  115. Begin
  116. Repeat
  117. Current := tai(Current.Next);
  118. While Assigned(Current) And
  119. ((Current.typ In SkipInstr) or
  120. {$ifdef SPARC}
  121. ((Current.typ=ait_instruction) and
  122. (taicpu(Current).opcode=A_NOP)
  123. ) or
  124. {$endif SPARC}
  125. ((Current.typ = ait_label) And
  126. Not(Tai_Label(Current).l.is_used))) Do
  127. Current := tai(Current.Next);
  128. If Assigned(Current) And
  129. (Current.typ = ait_Marker) And
  130. (Tai_Marker(Current).Kind = NoPropInfoStart) Then
  131. Begin
  132. While Assigned(Current) And
  133. ((Current.typ <> ait_Marker) Or
  134. (Tai_Marker(Current).Kind <> NoPropInfoEnd)) Do
  135. Current := Tai(Current.Next);
  136. End;
  137. Until Not(Assigned(Current)) Or
  138. (Current.typ <> ait_Marker) Or
  139. (Tai_Marker(Current).Kind <> NoPropInfoEnd);
  140. Next := Current;
  141. If Assigned(Current) And
  142. Not((Current.typ In SkipInstr) or
  143. ((Current.typ = ait_label) And
  144. Not(Tai_Label(Current).l.is_used)))
  145. Then GetNextInstruction := True
  146. Else
  147. Begin
  148. Next := Nil;
  149. GetNextInstruction := False;
  150. End;
  151. End;
  152. Function TAOptBase.GetLastInstruction(Current: tai; Var Last: tai): Boolean;
  153. Begin
  154. Repeat
  155. Current := Tai(Current.previous);
  156. While Assigned(Current) And
  157. (((Current.typ = ait_Marker) And
  158. Not(Tai_Marker(Current).Kind in [AsmBlockEnd,NoPropInfoEnd])) or
  159. (Current.typ In SkipInstr) or
  160. ((Current.typ = ait_label) And
  161. Not(Tai_Label(Current).l.is_used))) Do
  162. Current := Tai(Current.previous);
  163. If Assigned(Current) And
  164. (Current.typ = ait_Marker) And
  165. (Tai_Marker(Current).Kind = NoPropInfoEnd) Then
  166. Begin
  167. While Assigned(Current) And
  168. ((Current.typ <> ait_Marker) Or
  169. (Tai_Marker(Current).Kind <> NoPropInfoStart)) Do
  170. Current := Tai(Current.previous);
  171. End;
  172. Until Not(Assigned(Current)) Or
  173. (Current.typ <> ait_Marker) Or
  174. (Tai_Marker(Current).Kind <> NoPropInfoStart);
  175. If Not(Assigned(Current)) or
  176. (Current.typ In SkipInstr) or
  177. ((Current.typ = ait_label) And
  178. Not(Tai_Label(Current).l.is_used)) or
  179. ((Current.typ = ait_Marker) And
  180. (Tai_Marker(Current).Kind = AsmBlockEnd))
  181. Then
  182. Begin
  183. Last := Nil;
  184. GetLastInstruction := False
  185. End
  186. Else
  187. Begin
  188. Last := Current;
  189. GetLastInstruction := True;
  190. End;
  191. End;
  192. { ******************* Processor dependent stuff *************************** }
  193. Function TAOptBase.RegMaxSize(Reg: TRegister): TRegister;
  194. Begin
  195. RegMaxSize := Reg
  196. End;
  197. Function TAOptBase.RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  198. Begin
  199. RegsSameSize := True
  200. End;
  201. Function TAOptBase.IsLoadMemReg(p: tai): Boolean;
  202. Begin
  203. Abstract
  204. End;
  205. Function TAOptBase.IsLoadConstReg(p: tai): Boolean;
  206. Begin
  207. Abstract
  208. End;
  209. Function TAOptBase.IsStoreRegMem(p: tai): Boolean;
  210. Begin
  211. Abstract
  212. End;
  213. Function TAoptBase.a_load_reg_reg(reg1, reg2: TRegister): taicpu;
  214. Begin
  215. Abstract
  216. End;
  217. end.
  218. {
  219. $Log$
  220. Revision 1.10 2004-11-03 17:51:58 florian
  221. * fixed missing cgutils usage
  222. Revision 1.9 2004/10/31 21:45:02 peter
  223. * generic tlocation
  224. * move tlocation to cgutils
  225. Revision 1.8 2004/10/30 15:21:37 florian
  226. * fixed generic optimizer
  227. * enabled generic optimizer for sparc
  228. Revision 1.7 2004/06/20 08:55:28 florian
  229. * logs truncated
  230. }