aoptbase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. class function RegInOp(Reg: TRegister; const op: toper): Boolean; static;
  42. { returns true if register Reg is used in the reference Ref }
  43. class function RegInRef(Reg: TRegister; Const Ref: TReference): Boolean; static;
  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. If AlsoStopOn is set, it will also stop on these }
  50. { object types that are normally skipped over. }
  51. class function GetNextInstruction(Current: tai; out Next: tai; AlsoStopOn: taitypes = []): Boolean; static;
  52. { gets the previous tai object after current that contains info }
  53. { relevant to the optimizer in last. If there is none, it returns }
  54. { false and sets last to nil. If AlsoStopOn is set, it will also }
  55. { stop on these object types that are normally skipped over. }
  56. class function GetLastInstruction(Current: tai; out Last: tai; AlsoStopOn: taitypes = []): Boolean; static;
  57. class function SkipEntryExitMarker(current: tai; out next: tai): boolean; static;
  58. { processor dependent methods }
  59. { returns the maximum width component of Reg. Only has to be }
  60. { overridden for the 80x86 (afaik) }
  61. Function RegMaxSize(Reg: TRegister): TRegister; Virtual;
  62. { returns true if Reg1 and Reg2 are of the samae width. Only has to }
  63. { overridden for the 80x86 (afaik) }
  64. Function RegsSameSize(Reg1, Reg2: TRegister): Boolean; Virtual;
  65. { returns whether P is a load instruction (load contents from a }
  66. { memory location or (register) variable into a register) }
  67. Function IsLoadMemReg(p: tai): Boolean; Virtual; Abstract;
  68. { returns whether P is a load constant instruction (load a constant }
  69. { into a register) }
  70. Function IsLoadConstReg(p: tai): Boolean; Virtual; Abstract;
  71. { returns whether P is a store instruction (store contents from a
  72. register to a memory location or to a (register) variable) }
  73. Function IsStoreRegMem(p: tai): Boolean; Virtual; Abstract;
  74. { create a paicpu Object that loads the contents of reg1 into reg2 }
  75. Function a_load_reg_reg(reg1, reg2: TRegister): taicpu; Virtual; Abstract;
  76. { returns true if reg is used by any instruction between p1 and p2 }
  77. Function RegUsedBetween(reg: TRegister; p1, p2: tai): Boolean;
  78. { returns true if reg is modified by any instruction between p1 and p2 }
  79. function RegModifiedBetween(reg: TRegister; p1, p2: tai): Boolean;
  80. { returns true if reg is loaded with a new value by hp }
  81. function RegLoadedWithNewValue(reg: tregister; hp: tai): boolean; Virtual;
  82. { returns true if hp loads a value from reg }
  83. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; Virtual;
  84. { compares reg1 and reg2 having the same type and being the same super registers
  85. so the register size is neglected }
  86. class function SuperRegistersEqual(reg1,reg2 : TRegister) : Boolean; static; {$ifdef USEINLINE}inline;{$endif}
  87. { returns true if changing reg1 changes reg2 or vice versa }
  88. class function RegistersInterfere(reg1,reg2 : TRegister) : Boolean; static; {$ifdef USEINLINE}inline;{$endif}
  89. end;
  90. function labelCanBeSkipped(p: tai_label): boolean; {$ifdef USEINLINE}inline;{$endif}
  91. implementation
  92. uses
  93. verbose,globals,aoptcpub;
  94. constructor taoptbase.create;
  95. begin
  96. inherited create;
  97. end;
  98. destructor taoptbase.destroy;
  99. begin
  100. inherited destroy;
  101. end;
  102. Function TAOptBase.RegInInstruction(Reg: TRegister; p1: tai): Boolean;
  103. Var
  104. Count: longint;
  105. Begin
  106. result:=false;
  107. if p1.typ<>ait_instruction then
  108. exit;
  109. for Count:=0 to TInstr(p1).ops-1 do
  110. if RegInOp(Reg, TInstr(p1).oper[Count]^) then
  111. exit(true);
  112. End;
  113. class function TAOptBase.RegInOp(Reg: TRegister; const op: toper): Boolean;
  114. Begin
  115. Case op.typ Of
  116. Top_Reg: RegInOp := RegistersInterfere(Reg,op.reg);
  117. Top_Ref: RegInOp := RegInRef(Reg, op.ref^);
  118. {$ifdef arm}
  119. Top_Shifterop: RegInOp := op.shifterop^.rs = Reg;
  120. Top_RegSet: RegInOp := getsupreg(Reg) in op.regset^;
  121. {$endif arm}
  122. Else RegInOp := False
  123. End
  124. End;
  125. class function TAOptBase.RegInRef(Reg: TRegister; Const Ref: TReference): Boolean;
  126. Begin
  127. RegInRef := RegistersInterfere(Ref.Base,Reg)
  128. {$ifdef cpurefshaveindexreg}
  129. Or RegistersInterfere(Ref.Index,Reg)
  130. {$endif cpurefshaveindexreg}
  131. {$ifdef x86}
  132. or (Reg=Ref.segment)
  133. { if Ref.segment isn't set, the cpu uses implicitly ss or ds, depending on the base register }
  134. or ((Ref.segment=NR_NO) and (
  135. ((Reg=NR_SS) and (RegistersInterfere(Ref.base,NR_EBP) or RegistersInterfere(Ref.base,NR_ESP))) or
  136. ((Reg=NR_DS) and not(RegistersInterfere(Ref.base,NR_EBP) or RegistersInterfere(Ref.base,NR_ESP)))
  137. ))
  138. {$endif x86}
  139. End;
  140. Function TAOptBase.RegModifiedByInstruction(Reg: TRegister; p1: tai): Boolean;
  141. Begin
  142. Result:=true;
  143. End;
  144. function labelCanBeSkipped(p: tai_label): boolean; {$ifdef USEINLINE}inline;{$endif}
  145. begin
  146. labelCanBeSkipped := not(p.labsym.is_used) or (p.labsym.labeltype<>alt_jump);
  147. end;
  148. class function TAOptBase.GetNextInstruction(Current: tai; out Next: tai; AlsoStopOn: taitypes): Boolean;
  149. Begin
  150. Repeat
  151. Current := tai(Current.Next);
  152. While Assigned(Current) And
  153. ((Current.typ In SkipInstr - AlsoStopOn) or
  154. {$ifdef cpudelayslot}
  155. ((Current.typ=ait_instruction) and
  156. (taicpu(Current).opcode=A_NOP)
  157. ) or
  158. {$endif cpudelayslot}
  159. ((Current.typ = ait_label) And
  160. labelCanBeSkipped(Tai_Label(Current)))) Do
  161. begin
  162. { this won't help the current loop, but it helps when returning from GetNextInstruction
  163. as the next entry is probably already in the cache }
  164. prefetch(pointer(Current.Next)^);
  165. Current := Tai(Current.Next);
  166. end;
  167. If Assigned(Current) And
  168. (Current.typ = ait_Marker) And
  169. (Tai_Marker(Current).Kind = mark_NoPropInfoStart) Then
  170. Begin
  171. While Assigned(Current) And
  172. ((Current.typ <> ait_Marker) Or
  173. (Tai_Marker(Current).Kind <> mark_NoPropInfoEnd)) Do
  174. begin
  175. { this won't help the current loop, but it helps when returning from GetNextInstruction
  176. as the next entry is probably already in the cache }
  177. prefetch(pointer(Current.Next)^);
  178. Current := Tai(Current.Next);
  179. end;
  180. End;
  181. Until Not(Assigned(Current)) Or
  182. (Current.typ <> ait_Marker) Or
  183. (Tai_Marker(Current).Kind <> mark_NoPropInfoEnd);
  184. Next := Current;
  185. If Assigned(Current) And
  186. Not((Current.typ In SkipInstr - AlsoStopOn) or
  187. ((Current.typ = ait_label) And
  188. labelCanBeSkipped(Tai_Label(Current))))
  189. Then GetNextInstruction := True
  190. Else
  191. Begin
  192. Next := Nil;
  193. GetNextInstruction := False;
  194. End;
  195. End;
  196. class function TAOptBase.GetLastInstruction(Current: tai; out Last: tai; AlsoStopOn: taitypes): Boolean;
  197. Begin
  198. Repeat
  199. Current := Tai(Current.previous);
  200. While Assigned(Current) And
  201. (((Current.typ = ait_Marker) And
  202. Not(Tai_Marker(Current).Kind in [mark_AsmBlockEnd{,mark_NoPropInfoEnd}])) or
  203. (Current.typ In SkipInstr - AlsoStopOn) or
  204. ((Current.typ = ait_label) And
  205. labelCanBeSkipped(Tai_Label(Current)))) Do
  206. Current := Tai(Current.previous);
  207. { If Assigned(Current) And
  208. (Current.typ = ait_Marker) And
  209. (Tai_Marker(Current).Kind = mark_NoPropInfoEnd) Then
  210. Begin
  211. While Assigned(Current) And
  212. ((Current.typ <> ait_Marker) Or
  213. (Tai_Marker(Current).Kind <> mark_NoPropInfoStart)) Do
  214. Current := Tai(Current.previous);
  215. End; }
  216. Until Not(Assigned(Current)) Or
  217. (Current.typ <> ait_Marker) Or
  218. not(tai_Marker(current).Kind in [mark_NoPropInfoStart,mark_NoPropInfoEnd]);
  219. If Not(Assigned(Current)) or
  220. (Current.typ In SkipInstr - AlsoStopOn) or
  221. ((Current.typ = ait_label) And
  222. labelCanBeSkipped(Tai_Label(Current))) or
  223. ((Current.typ = ait_Marker) And
  224. (Tai_Marker(Current).Kind = mark_AsmBlockEnd))
  225. Then
  226. Begin
  227. Last := Nil;
  228. GetLastInstruction := False
  229. End
  230. Else
  231. Begin
  232. Last := Current;
  233. GetLastInstruction := True;
  234. End;
  235. End;
  236. class function TAOptBase.SkipEntryExitMarker(current: tai; out next: tai): boolean;
  237. begin
  238. result:=true;
  239. next:=current;
  240. if current.typ<>ait_marker then
  241. exit;
  242. while GetNextInstruction(next,next) do
  243. begin
  244. if (next.typ<>ait_marker) or not(tai_marker(next).Kind in [mark_Position,mark_BlockStart]) then
  245. exit;
  246. end;
  247. result:=false;
  248. end;
  249. Function TAOptBase.RegUsedBetween(reg : TRegister;p1,p2 : tai) : Boolean;
  250. Begin
  251. Result:=false;
  252. while assigned(p1) and assigned(p2) and GetNextInstruction(p1,p1) and (p1<>p2) do
  253. if RegInInstruction(reg,p1) then
  254. begin
  255. Result:=true;
  256. exit;
  257. end;
  258. end;
  259. Function TAOptBase.RegModifiedBetween(reg : TRegister;p1,p2 : tai) : Boolean;
  260. Begin
  261. Result:=false;
  262. while assigned(p1) and assigned(p2) and GetNextInstruction(p1,p1) and (p1<>p2) do
  263. if RegModifiedByInstruction(reg,p1) then
  264. begin
  265. Result:=true;
  266. exit;
  267. end;
  268. end;
  269. function TAoptBase.RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean;
  270. begin
  271. result:=false;
  272. internalerror(2016012401);
  273. end;
  274. function TAoptBase.InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean;
  275. begin
  276. { save approximation }
  277. Result:=true;
  278. end;
  279. class function TAOptBase.SuperRegistersEqual(reg1,reg2 : TRegister) : Boolean;{$ifdef USEINLINE}inline;{$endif}
  280. Begin
  281. { Do an optimized version of
  282. Result:=(getregtype(reg1) = getregtype(reg2)) and
  283. (getsupreg(reg1) = getsupreg(Reg2));
  284. as SuperRegistersEqual is used a lot
  285. }
  286. {$ifdef Z80}
  287. { Z80 registers are indexed in an incompatible way (without R_SUBH), so it
  288. needs a special check. }
  289. Result:=super_registers_equal(reg1,reg2);
  290. {$else Z80}
  291. Result:=(DWord(reg1) and $ff00ffff)=(DWord(reg2) and $ff00ffff);
  292. {$endif Z80}
  293. end;
  294. class function TAOptBase.RegistersInterfere(reg1,reg2 : TRegister) : Boolean; static; {$ifdef USEINLINE}inline;{$endif}
  295. begin
  296. {$ifdef Z80}
  297. result:=registers_interfere(reg1,reg2);
  298. {$else Z80}
  299. result:=SuperRegistersEqual(reg1,reg2);
  300. {$endif Z80}
  301. end;
  302. { ******************* Processor dependent stuff *************************** }
  303. Function TAOptBase.RegMaxSize(Reg: TRegister): TRegister;
  304. Begin
  305. RegMaxSize := Reg
  306. End;
  307. Function TAOptBase.RegsSameSize(Reg1, Reg2: TRegister): Boolean;
  308. Begin
  309. RegsSameSize := True
  310. End;
  311. end.