aoptbase.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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;virtual;
  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. function SkipEntryExitMarker(current: tai; var next: tai): boolean;
  56. { processor dependent methods }
  57. { returns the maximum width component of Reg. Only has to be }
  58. { overridden for the 80x86 (afaik) }
  59. Function RegMaxSize(Reg: TRegister): TRegister; Virtual;
  60. { returns true if Reg1 and Reg2 are of the samae width. Only has to }
  61. { overridden for the 80x86 (afaik) }
  62. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean; Virtual;
  63. { returns whether P is a load instruction (load contents from a }
  64. { memory location or (register) variable into a register) }
  65. Function IsLoadMemReg(p: tai): Boolean; Virtual; Abstract;
  66. { returns whether P is a load constant instruction (load a constant }
  67. { into a register) }
  68. Function IsLoadConstReg(p: tai): Boolean; Virtual; Abstract;
  69. { returns whether P is a store instruction (store contents from a
  70. register to a memory location or to a (register) variable) }
  71. Function IsStoreRegMem(p: tai): Boolean; Virtual; Abstract;
  72. { create a paicpu Object that loads the contents of reg1 into reg2 }
  73. Function a_load_reg_reg(reg1, reg2: TRegister): taicpu; Virtual; Abstract;
  74. { returns true if reg is used by any instruction between p1 and p2 }
  75. Function RegUsedBetween(reg: TRegister; p1, p2: tai): Boolean;
  76. { returns true if reg is modified by any instruction between p1 and p2 }
  77. function RegModifiedBetween(reg: TRegister; p1, p2: tai): Boolean;
  78. { returns true if reg is loaded with a new value by hp }
  79. function RegLoadedWithNewValue(reg: tregister; hp: tai): boolean; Virtual;
  80. { returns true if hp loads a value from reg }
  81. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; Virtual;
  82. { compares reg1 and reg2 having the same type and being the same super registers
  83. so the register size is neglected }
  84. function SuperRegistersEqual(reg1,reg2 : TRegister) : Boolean;
  85. end;
  86. function labelCanBeSkipped(p: tai_label): boolean;
  87. implementation
  88. uses
  89. verbose,globtype,globals,aoptcpub;
  90. constructor taoptbase.create;
  91. begin
  92. inherited create;
  93. end;
  94. destructor taoptbase.destroy;
  95. begin
  96. inherited destroy;
  97. end;
  98. Function TAOptBase.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  99. Var
  100. Count: longint;
  101. Begin
  102. result:=false;
  103. if p1.typ<>ait_instruction then
  104. exit;
  105. for Count:=0 to TInstr(p1).ops-1 do
  106. if RegInOp(Reg, TInstr(p1).oper[Count]^) then
  107. exit(true);
  108. result:=false;
  109. End;
  110. Function TAOptBase.RegInOp(Reg: TRegister; const op: toper): Boolean;
  111. Begin
  112. Case op.typ Of
  113. Top_Reg: RegInOp := Reg = op.reg;
  114. Top_Ref: RegInOp := RegInRef(Reg, op.ref^);
  115. {$ifdef arm}
  116. Top_Shifterop: RegInOp := op.shifterop^.rs = Reg;
  117. Top_RegSet: RegInOp := getsupreg(Reg) in op.regset^;
  118. {$endif arm}
  119. Else RegInOp := False
  120. End
  121. End;
  122. Function TAOptBase.RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  123. Begin
  124. Reg := RegMaxSize(Reg);
  125. RegInRef := (Ref.Base = Reg)
  126. {$ifdef cpurefshaveindexreg}
  127. Or (Ref.Index = Reg)
  128. {$endif cpurefshaveindexreg}
  129. End;
  130. Function TAOptBase.RegModifiedByInstruction(Reg: TRegister; p1: tai): Boolean;
  131. Begin
  132. Result:=true;
  133. End;
  134. function labelCanBeSkipped(p: tai_label): boolean;
  135. begin
  136. labelCanBeSkipped := not(p.labsym.is_used) or (p.labsym.labeltype<>alt_jump);
  137. end;
  138. Function TAOptBase.GetNextInstruction(Current: tai; Var Next: tai): Boolean;
  139. Begin
  140. Repeat
  141. Current := tai(Current.Next);
  142. While Assigned(Current) And
  143. ((Current.typ In SkipInstr) or
  144. {$if defined(SPARC) or defined(MIPS)}
  145. ((Current.typ=ait_instruction) and
  146. (taicpu(Current).opcode=A_NOP)
  147. ) or
  148. {$endif SPARC or MIPS}
  149. ((Current.typ = ait_label) And
  150. labelCanBeSkipped(Tai_Label(Current)))) Do
  151. Current := tai(Current.Next);
  152. If Assigned(Current) And
  153. (Current.typ = ait_Marker) And
  154. (Tai_Marker(Current).Kind = mark_NoPropInfoStart) Then
  155. Begin
  156. While Assigned(Current) And
  157. ((Current.typ <> ait_Marker) Or
  158. (Tai_Marker(Current).Kind <> mark_NoPropInfoEnd)) Do
  159. Current := Tai(Current.Next);
  160. End;
  161. Until Not(Assigned(Current)) Or
  162. (Current.typ <> ait_Marker) Or
  163. (Tai_Marker(Current).Kind <> mark_NoPropInfoEnd);
  164. Next := Current;
  165. If Assigned(Current) And
  166. Not((Current.typ In SkipInstr) or
  167. ((Current.typ = ait_label) And
  168. labelCanBeSkipped(Tai_Label(Current))))
  169. Then GetNextInstruction := True
  170. Else
  171. Begin
  172. Next := Nil;
  173. GetNextInstruction := False;
  174. End;
  175. End;
  176. Function TAOptBase.GetLastInstruction(Current: tai; Var Last: tai): Boolean;
  177. Begin
  178. Repeat
  179. Current := Tai(Current.previous);
  180. While Assigned(Current) And
  181. (((Current.typ = ait_Marker) And
  182. Not(Tai_Marker(Current).Kind in [mark_AsmBlockEnd{,mark_NoPropInfoEnd}])) or
  183. (Current.typ In SkipInstr) or
  184. ((Current.typ = ait_label) And
  185. labelCanBeSkipped(Tai_Label(Current)))) Do
  186. Current := Tai(Current.previous);
  187. { If Assigned(Current) And
  188. (Current.typ = ait_Marker) And
  189. (Tai_Marker(Current).Kind = mark_NoPropInfoEnd) Then
  190. Begin
  191. While Assigned(Current) And
  192. ((Current.typ <> ait_Marker) Or
  193. (Tai_Marker(Current).Kind <> mark_NoPropInfoStart)) Do
  194. Current := Tai(Current.previous);
  195. End; }
  196. Until Not(Assigned(Current)) Or
  197. (Current.typ <> ait_Marker) Or
  198. not(tai_Marker(current).Kind in [mark_NoPropInfoStart,mark_NoPropInfoEnd]);
  199. If Not(Assigned(Current)) or
  200. (Current.typ In SkipInstr) or
  201. ((Current.typ = ait_label) And
  202. labelCanBeSkipped(Tai_Label(Current))) or
  203. ((Current.typ = ait_Marker) And
  204. (Tai_Marker(Current).Kind = mark_AsmBlockEnd))
  205. Then
  206. Begin
  207. Last := Nil;
  208. GetLastInstruction := False
  209. End
  210. Else
  211. Begin
  212. Last := Current;
  213. GetLastInstruction := True;
  214. End;
  215. End;
  216. function TAOptBase.SkipEntryExitMarker(current: tai; var next: tai): boolean;
  217. begin
  218. result:=true;
  219. if current.typ<>ait_marker then
  220. exit;
  221. next:=current;
  222. while GetNextInstruction(next,next) do
  223. begin
  224. if (next.typ<>ait_marker) or not(tai_marker(next).Kind in [mark_Position,mark_BlockStart]) then
  225. exit;
  226. end;
  227. result:=false;
  228. end;
  229. Function TAOptBase.RegUsedBetween(reg : TRegister;p1,p2 : tai) : Boolean;
  230. Begin
  231. Result:=false;
  232. while assigned(p1) and assigned(p2) and GetNextInstruction(p1,p1) and (p1<>p2) do
  233. if RegInInstruction(reg,p1) then
  234. begin
  235. Result:=true;
  236. exit;
  237. end;
  238. end;
  239. Function TAOptBase.RegModifiedBetween(reg : TRegister;p1,p2 : tai) : Boolean;
  240. Begin
  241. Result:=false;
  242. while assigned(p1) and assigned(p2) and GetNextInstruction(p1,p1) and (p1<>p2) do
  243. if RegModifiedByInstruction(reg,p1) then
  244. begin
  245. Result:=true;
  246. exit;
  247. end;
  248. end;
  249. function TAoptBase.RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean;
  250. begin
  251. result:=false;
  252. internalerror(2016012401);
  253. end;
  254. function TAoptBase.InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean;
  255. begin
  256. { save approximation }
  257. Result:=true;
  258. end;
  259. function TAOptBase.SuperRegistersEqual(reg1,reg2 : TRegister) : Boolean;
  260. Begin
  261. Result:=(getregtype(reg1) = getregtype(reg2)) and
  262. (getsupreg(reg1) = getsupreg(Reg2));
  263. end;
  264. { ******************* Processor dependent stuff *************************** }
  265. Function TAOptBase.RegMaxSize(Reg: TRegister): TRegister;
  266. Begin
  267. RegMaxSize := Reg
  268. End;
  269. Function TAOptBase.RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  270. Begin
  271. RegsSameSize := True
  272. End;
  273. end.