2
0

aoptbase.pas 8.3 KB

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