aoptbase.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. function RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean;virtual;
  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; Abstract;
  65. { returns whether P is a load constant instruction (load a constant }
  66. { into a register) }
  67. Function IsLoadConstReg(p: tai): Boolean; Virtual; Abstract;
  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; Abstract;
  71. { create a paicpu Object that loads the contents of reg1 into reg2 }
  72. Function a_load_reg_reg(reg1, reg2: TRegister): taicpu; Virtual; Abstract;
  73. end;
  74. function labelCanBeSkipped(p: tai_label): boolean;
  75. implementation
  76. uses
  77. globtype,globals,aoptcpub;
  78. constructor taoptbase.create;
  79. begin
  80. inherited create;
  81. end;
  82. destructor taoptbase.destroy;
  83. begin
  84. inherited destroy;
  85. end;
  86. Function TAOptBase.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  87. Var Count: AWord;
  88. TmpResult: Boolean;
  89. Begin
  90. TmpResult := False;
  91. Count := 0;
  92. If (p1.typ = ait_instruction) and assigned(TInstr(p1).oper[0]) Then
  93. Repeat
  94. TmpResult := RegInOp(Reg, TInstr(p1).oper[Count]^);
  95. Inc(Count)
  96. Until (TInstr(p1).oper[Count]=nil) or (Count = MaxOps) or TmpResult;
  97. RegInInstruction := TmpResult
  98. End;
  99. Function TAOptBase.RegInOp(Reg: TRegister; const op: toper): Boolean;
  100. Begin
  101. Case op.typ Of
  102. Top_Reg: RegInOp := Reg = op.reg;
  103. Top_Ref: RegInOp := RegInRef(Reg, op.ref^)
  104. Else RegInOp := False
  105. End
  106. End;
  107. Function TAOptBase.RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  108. Begin
  109. Reg := RegMaxSize(Reg);
  110. RegInRef := (Ref.Base = Reg)
  111. {$ifdef cpurefshaveindexreg}
  112. Or (Ref.Index = Reg)
  113. {$endif cpurefshaveindexreg}
  114. End;
  115. Function TAOptBase.RegModifiedByInstruction(Reg: TRegister; p1: tai): Boolean;
  116. Begin
  117. Result:=true;
  118. End;
  119. function labelCanBeSkipped(p: tai_label): boolean;
  120. begin
  121. labelCanBeSkipped := not(p.labsym.is_used) or (p.labsym.labeltype<>alt_jump);
  122. end;
  123. Function TAOptBase.GetNextInstruction(Current: tai; Var Next: tai): Boolean;
  124. Begin
  125. Repeat
  126. Current := tai(Current.Next);
  127. While Assigned(Current) And
  128. ((Current.typ In SkipInstr) or
  129. {$ifdef SPARC}
  130. ((Current.typ=ait_instruction) and
  131. (taicpu(Current).opcode=A_NOP)
  132. ) or
  133. {$endif SPARC}
  134. ((Current.typ = ait_label) And
  135. labelCanBeSkipped(Tai_Label(Current)))) Do
  136. Current := tai(Current.Next);
  137. If Assigned(Current) And
  138. (Current.typ = ait_Marker) And
  139. (Tai_Marker(Current).Kind = mark_NoPropInfoStart) Then
  140. Begin
  141. While Assigned(Current) And
  142. ((Current.typ <> ait_Marker) Or
  143. (Tai_Marker(Current).Kind <> mark_NoPropInfoEnd)) Do
  144. Current := Tai(Current.Next);
  145. End;
  146. Until Not(Assigned(Current)) Or
  147. (Current.typ <> ait_Marker) Or
  148. (Tai_Marker(Current).Kind <> mark_NoPropInfoEnd);
  149. Next := Current;
  150. If Assigned(Current) And
  151. Not((Current.typ In SkipInstr) or
  152. ((Current.typ = ait_label) And
  153. labelCanBeSkipped(Tai_Label(Current))))
  154. Then GetNextInstruction := True
  155. Else
  156. Begin
  157. Next := Nil;
  158. GetNextInstruction := False;
  159. End;
  160. End;
  161. Function TAOptBase.GetLastInstruction(Current: tai; Var Last: tai): Boolean;
  162. Begin
  163. Repeat
  164. Current := Tai(Current.previous);
  165. While Assigned(Current) And
  166. (((Current.typ = ait_Marker) And
  167. Not(Tai_Marker(Current).Kind in [mark_AsmBlockEnd,mark_NoPropInfoEnd])) or
  168. (Current.typ In SkipInstr) or
  169. ((Current.typ = ait_label) And
  170. labelCanBeSkipped(Tai_Label(Current)))) Do
  171. Current := Tai(Current.previous);
  172. If Assigned(Current) And
  173. (Current.typ = ait_Marker) And
  174. (Tai_Marker(Current).Kind = mark_NoPropInfoEnd) Then
  175. Begin
  176. While Assigned(Current) And
  177. ((Current.typ <> ait_Marker) Or
  178. (Tai_Marker(Current).Kind <> mark_NoPropInfoStart)) Do
  179. Current := Tai(Current.previous);
  180. End;
  181. Until Not(Assigned(Current)) Or
  182. (Current.typ <> ait_Marker) Or
  183. (Tai_Marker(Current).Kind <> mark_NoPropInfoStart);
  184. If Not(Assigned(Current)) or
  185. (Current.typ In SkipInstr) or
  186. ((Current.typ = ait_label) And
  187. labelCanBeSkipped(Tai_Label(Current))) or
  188. ((Current.typ = ait_Marker) And
  189. (Tai_Marker(Current).Kind = mark_AsmBlockEnd))
  190. Then
  191. Begin
  192. Last := Nil;
  193. GetLastInstruction := False
  194. End
  195. Else
  196. Begin
  197. Last := Current;
  198. GetLastInstruction := True;
  199. End;
  200. End;
  201. { ******************* Processor dependent stuff *************************** }
  202. Function TAOptBase.RegMaxSize(Reg: TRegister): TRegister;
  203. Begin
  204. RegMaxSize := Reg
  205. End;
  206. Function TAOptBase.RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  207. Begin
  208. RegsSameSize := True
  209. End;
  210. end.