aoptcs.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  4. Development Team
  5. This unit contains the common subexpression elimination object of the
  6. assembler optimizer.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit aoptcs;
  21. interface
  22. uses aasm, aoptcpu, aoptobj;
  23. { ************************************************************************* }
  24. { info about the equivalence of registers when comparing two code sequences }
  25. { ************************************************************************* }
  26. TRegInfo = Object(TAoptBaseCpu)
  27. { registers encountered in the new and old sequence }
  28. NewRegsEncountered, OldRegsEncountered,
  29. { registers which only have been loaded for use as base or index in a }
  30. { reference later on }
  31. RegsLoadedForRef: TRegSet;
  32. { to which register in the old sequence corresponds every register in }
  33. { the new sequence }
  34. New2OldReg: TRegArray;
  35. Constructor init;
  36. { clear all information store in the object }
  37. Procedure Clear;
  38. { the contents of OldReg in the old sequence are now being loaded into }
  39. { NewReg in the new sequence }
  40. Procedure AddReg(OldReg, NewReg: TRegister); Virtual;
  41. { the contents of OldOp in the old sequence are now being loaded into }
  42. { NewOp in the new sequence. It is assumed that OldOp and NewOp are }
  43. { equivalent }
  44. Procedure AddOp(const OldOp, NewOp:Toper);
  45. { check if a register in the old sequence (OldReg) can be equivalent to }
  46. { a register in the new sequence (NewReg) if the operation OpAct is }
  47. { performed on it. The RegInfo is updated (not necessary to call AddReg }
  48. { afterwards) }
  49. Function RegsEquivalent(OldReg, NewReg: TRegister; OpAct: TopAction):
  50. Boolean;
  51. { check if a reference in the old sequence (OldRef) can be equivalent }
  52. { to a reference in the new sequence (NewRef) if the operation OpAct is }
  53. { performed on it. The RegInfo is updated (not necessary to call AddOp }
  54. { afterwards) }
  55. Function RefsEquivalent(Const OldRef, NewRef: TReference; OpAct:
  56. TOpAction): Boolean;
  57. { check if an operand in the old sequence (OldOp) can be equivalent to }
  58. { an operand in the new sequence (NewOp) if the operation OpAct is }
  59. { performed on it. The RegInfo is updated (not necessary to call AddOp }
  60. { afterwards) }
  61. Function OpsEquivalent(const OldOp, NewOp: toper; OpAct: TopAction):
  62. Boolean;
  63. { check if an instruction in the old sequence (OldP) can be equivalent }
  64. { to an instruction in the new sequence (Newp). The RegInfo is updated }
  65. Function InstructionsEquivalent(OldP, NewP: Pai): Boolean;
  66. End;
  67. { ************************************************************************* }
  68. { *************** The common subexpression elimination object ************* }
  69. { ************************************************************************* }
  70. Type TAoptCSE = Object(TAoptObj)
  71. { returns true if the instruction p1 modifies the register Reg }
  72. Function RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  73. End;
  74. Implementation
  75. { ************************************************************************* }
  76. { ******************************* TReginfo ******************************** }
  77. { ************************************************************************* }
  78. Constructor TRegInfo.Init;
  79. Begin
  80. Clear;
  81. End;
  82. Procedure TRegInfo.Clear;
  83. Begin
  84. RegsLoadedForRef := [];
  85. NewRegsEncountered := [FRAME_POINTER_REG, STACK_POINTER_REG];
  86. OldRegsEncountered := [FRAME_POINTER_REG, STACK_POINTER_REG];
  87. New2OldReg[FRAME_POINTER_REG] := FRAME_POINTER_REG;
  88. New2OldReg[STACK_POINTER_REG] := STACK_POINTER_REG;
  89. End;
  90. Procedure TRegInfo.AddReg(OldReg, NewReg: TRegister);
  91. { updates the ???RegsEncountered and ???2???Reg fields of RegInfo. Assumes }
  92. { that OldReg and NewReg have the same size (has to be chcked in advance }
  93. { with RegsSameSize) and that neither equals R_NO }
  94. { has to be overridden for architectures like the 80x86 when not all GP }
  95. { regs are of the same size }
  96. Begin
  97. NewRegsEncountered := NewRegsEncountered + [NewReg];
  98. OldRegsEncountered := OldRegsEncountered + [OldReg];
  99. New2OldReg[NewReg] := OldReg;
  100. End;
  101. Procedure TRegInfo.AddOp(const OldOp, NewOp:Toper);
  102. Begin
  103. Case OldOp.typ Of
  104. Top_Reg:
  105. If (OldOp.reg <> R_NO) Then
  106. AddReg(OldOp.reg, NewOp.reg);
  107. Top_Ref:
  108. Begin
  109. If OldOp.ref^.base <> R_NO Then
  110. AddReg(OldOp.ref^.base, NewOp.ref^.base);
  111. {$ifdef RefsHaveIndexReg}
  112. If OldOp.ref^.index <> R_NO Then
  113. AddReg(OldOp.ref^.index, NewOp.ref^.index);
  114. {$endif RefsHaveIndexReg}
  115. End;
  116. End;
  117. End;
  118. Function TRegInfo.RegsEquivalent(OldReg, NewReg: TRegister;
  119. OPAct: TOpAction): Boolean;
  120. Begin
  121. If Not((OldReg = R_NO) Or (NewReg = R_NO)) Then
  122. If RegsSameSize(OldReg, NewReg) Then
  123. { here we always check for the 32 bit component, because it is possible }
  124. { that the 8 bit component has not been set, event though NewReg already }
  125. { has been processed. This happens if it has been compared with a register }
  126. { that doesn't have an 8 bit component (such as EDI). In that case the 8 }
  127. { bit component is still set to R_NO and the comparison in the Else-part }
  128. { will fail }
  129. If (RegMaxSize(OldReg) in OldRegsEncountered) Then
  130. If (RegMaxSize(NewReg) in NewRegsEncountered) Then
  131. RegsEquivalent := (OldReg = New2OldReg[NewReg])
  132. { If we haven't encountered the new register yet, but we have encountered }
  133. { the old one already, the new one can only be correct if it's being }
  134. { written to (and consequently the old one is also being written to), }
  135. { otherwise }
  136. { }
  137. { movl -8(%ebp), %eax and movl -8(%ebp), %eax }
  138. { movl (%eax), %eax movl (%edx), %edx }
  139. { }
  140. { are considered equivalent }
  141. Else
  142. If (OpAct = OpAct_Write) Then
  143. Begin
  144. AddReg(OldReg, NewReg);
  145. RegsEquivalent := True
  146. End
  147. Else Regsequivalent := False
  148. Else
  149. If Not(RegMaxSize(NewReg) in NewRegsEncountered) Then
  150. Begin
  151. AddReg(OldReg, NewReg);
  152. RegsEquivalent := True
  153. End
  154. Else RegsEquivalent := False
  155. Else RegsEquivalent := False
  156. Else RegsEquivalent := OldReg = NewReg
  157. End;
  158. Function TRegInfo.RefsEquivalent(Const OldRef, NewRef: TReference;
  159. OpAct: TOpAction): Boolean;
  160. Begin
  161. If OldRef.is_immediate Then
  162. RefsEquivalent := NewRef.is_immediate and (OldRef.Offset = NewRef.Offset)
  163. Else
  164. RefsEquivalent := (OldRef.Offset+OldRef.OffsetFixup =
  165. NewRef.Offset+NewRef.OffsetFixup) And
  166. RegsEquivalent(OldRef.Base, NewRef.Base, OpAct)
  167. {$ifdef RefsHaveindexReg}
  168. And RegsEquivalent(OldRef.Index, NewRef.Index, OpAct)
  169. {$endif RefsHaveIndexReg}
  170. {$ifdef RefsHaveScale}
  171. And (OldRef.ScaleFactor = NewRef.ScaleFactor)
  172. {$endif RefsHaveScale}
  173. And (OldRef.Symbol = NewRef.Symbol)
  174. {$ifdef RefsHaveSegment}
  175. And (OldRef.Segment = NewRef.Segment)
  176. {$endif RefsHaveSegment}
  177. ;
  178. End;
  179. Function TRegInfo.OpsEquivalent(const OldOp, NewOp: toper; OpAct: TopAction):
  180. Boolean;
  181. Begin
  182. OpsEquivalent := False;
  183. if OldOp.typ=NewOp.typ then
  184. Case OldOp.typ Of
  185. Top_Const: OpsEquivalent := OldOp.val = NewOp.val;
  186. Top_Reg: OpsEquivalent := RegsEquivalent(OldOp.reg,NewOp.reg, OpAct);
  187. Top_Ref: OpsEquivalent := RefsEquivalent(OldOp.ref^, NewOp.ref^, OpAct);
  188. Top_None: OpsEquivalent := True
  189. End;
  190. End;
  191. Function TRegInfo.InstructionsEquivalent(OldP, NewP: Pai): Boolean;
  192. Function OperandTypesEqual: Boolean;
  193. Var Count: AWord;
  194. Begin
  195. OperandTypesEqual := False;
  196. For Count := 0 to max_operands-1 Do
  197. If (PInstr(OldP)^.oper[Count].typ <> PInstr(NewP)^.oper[Count].typ) Then
  198. Exit;
  199. OperandTypesEqual := True
  200. End;
  201. Var Count: AWord;
  202. TmpResult: Boolean;
  203. Begin
  204. If Assigned(OldP) And Assigned(NewP) And
  205. (Pai(OldP)^.typ = ait_instruction) And
  206. (Pai(NewP)^.typ = ait_instruction) And
  207. (PInstr(OldP)^.opcode = PInstr(NewP)^.opcode) And
  208. OperandTypesEqual
  209. Then
  210. { both instructions have the same structure: }
  211. { "<operator> <operand of type1>, <operand of type 2>, ..." }
  212. If IsLoadMemReg(OldP) Then
  213. { then also NewP = loadmemreg because of the previous check }
  214. If Not(RegInRef(PInstr(OldP)^.oper[LoadDst].reg,
  215. PInstr(OldP)^.oper[LoadSrc].ref^)) Then
  216. { the "old" instruction is a load of a register with a new value, not with }
  217. { a value based on the contents of this register (so no "mov (reg), reg") }
  218. If Not(RegInRef(PInstr(NewP)^.oper[LoadDst].reg,
  219. PInstr(NewP)^.oper[LoadSrc].ref^)) And
  220. RefsEqual(PInstr(OldP)^.oper[LoadSrc].ref^,
  221. PInstr(NewP)^.oper[LoadSrc].ref^)
  222. Then
  223. { the "new" instruction is also a load of a register with a new value, and }
  224. { this value is fetched from the same memory location }
  225. Begin
  226. With PInstr(NewP)^.oper[LoadSrc].ref^ Do
  227. Begin
  228. If Not(Base in [ProcInfo.FramePointer, R_NO, STACK_POINTER_REG])
  229. { it won't do any harm if the register is already in RegsLoadedForRef }
  230. Then RegsLoadedForRef := RegsLoadedForRef + [Base];
  231. {$ifdef RefsHaveIndexReg}
  232. If Not(Index in [ProcInfo.FramePointer, R_NO, STACK_POINTER_REG])
  233. Then RegsLoadedForRef := RegsLoadedForRef + [Index];
  234. {$endif RefsHaveIndexReg}
  235. End;
  236. { add the registers from the reference (.oper[Src]) to the RegInfo, all }
  237. { registers from the reference are the same in the old and in the new }
  238. { instruction sequence (refsequal returned true) }
  239. AddOp(PInstr(OldP)^.oper[LoadSrc], PInstr(OldP)^.oper[LoadSrc]);
  240. { the registers from .oper[Dest] have to be equivalent, but not necessarily }
  241. { equal }
  242. InstructionsEquivalent :=
  243. RegsEquivalent(PInstr(OldP)^.oper[LoadDst].reg,
  244. PInstr(NewP)^.oper[LoadDst].reg, OpAct_Write);
  245. End
  246. { the registers are loaded with values from different memory locations. If }
  247. { this were allowed, the instructions "mov -4(%esi),%eax" and }
  248. { "mov -4(%ebp),%eax" would be considered equivalent }
  249. Else InstructionsEquivalent := False
  250. Else
  251. { load register with a value based on the current value of this register }
  252. Begin
  253. With PInstr(NewP)^.oper[0].ref^ Do
  254. { Assume the registers occurring in the reference have only been loaded with }
  255. { the value they contain now to calculate an address (so the value they have }
  256. { now, won't be stored to memory later on) }
  257. Begin
  258. If Not(Base in [ProcInfo.FramePointer,
  259. RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg),
  260. R_NO,STACK_POINTER_REG])
  261. { It won't do any harm if the register is already in RegsLoadedForRef }
  262. Then
  263. Begin
  264. RegsLoadedForRef := RegsLoadedForRef + [Base];
  265. {$ifdef csdebug}
  266. Writeln(std_reg2str[base], ' added');
  267. {$endif csdebug}
  268. end;
  269. {$Ifdef RefsHaveIndexReg}
  270. If Not(Index in [ProcInfo.FramePointer,
  271. RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg),
  272. R_NO,StackPtr])
  273. Then
  274. Begin
  275. RegsLoadedForRef := RegsLoadedForRef + [Index];
  276. {$ifdef csdebug}
  277. Writeln(std_reg2str[index], ' added');
  278. {$endif csdebug}
  279. end;
  280. {$endif RefsHaveIndexReg}
  281. End;
  282. { now, remove the destination register of the load from the }
  283. { RegsLoadedForReg, since if it's loaded with a new value, it certainly }
  284. { will still be used later on }
  285. If Not(RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg) In
  286. [ProcInfo.FramePointer,R_NO,STACK_POINTER_REG])
  287. Then
  288. Begin
  289. RegsLoadedForRef := RegsLoadedForRef -
  290. [RegMaxSize(PInstr(NewP)^.oper[LoadDst].reg)];
  291. {$ifdef csdebug}
  292. Writeln(std_reg2str[RegMaxSize(PInstr(NewP)^.oper[1].reg)], ' removed');
  293. {$endif csdebug}
  294. end;
  295. InstructionsEquivalent :=
  296. OpsEquivalent(PInstr(OldP)^.oper[LoadSrc],
  297. PInstr(NewP)^.oper[LoadSrc], OpAct_Read) And
  298. OpsEquivalent(PInstr(OldP)^.oper[LoadDst],
  299. PInstr(NewP)^.oper[LoadDst], OpAct_Write)
  300. End
  301. Else
  302. { OldP and NewP are not a load instruction, but have the same structure }
  303. { (opcode, operand types), so they're equivalent if all operands are }
  304. { equivalent }
  305. Begin
  306. Count := 0;
  307. TmpResult := true;
  308. Repeat
  309. TmpResult :=
  310. OpsEquivalent(PInstr(OldP)^.oper[Count], PInstr(NewP)^.oper[Count],
  311. OpAct_Unknown);
  312. Inc(Count)
  313. Until (Count = MaxOps) or not(TmpResult);
  314. InstructionsEquivalent := TmpResult
  315. End
  316. { the instructions haven't even got the same structure, so they're certainly }
  317. { not equivalent }
  318. Else InstructionsEquivalent := False;
  319. End;
  320. Function TRegInfo.CheckSequence(p: Pai; Reg: TRegister; Var Found: Longint):
  321. Boolean;
  322. {checks whether the current instruction sequence (starting with p) and the
  323. one between StartMod and EndMod of Reg are the same. If so, the number of
  324. instructions that match is stored in Found and true is returned, otherwise
  325. Found holds the number of instructions between StartMod and EndMod and false
  326. is returned}
  327. { note: the NrOfMods field can hold two deifferent values depending on }
  328. { which instruction it belongs to: }
  329. { * if it is the first instruction of a sequence that describes the }
  330. { contents of a register, NrOfMods contains how many instructions are }
  331. { in the sequence }
  332. { * otherwise, NrOfMods contains how many instructions are in the }
  333. { describing the contents of the register after the current instruction }
  334. { has been executed }
  335. Var oldp, newp: Pai;
  336. PrevNonRemovablePai: Pai;
  337. OrgRegInfo, HighRegInfo: PRegInfo;
  338. HighFound, OrgRegFound: Byte;
  339. RegCounter: TRegister;
  340. OrgRegResult: Boolean;
  341. TmpResult: Boolean;
  342. OldNrOfMods: Byte;
  343. Begin {CheckSequence}
  344. Reg := RegMaxSize(Reg);
  345. { have we found a sequence of instructions equivalent to the new one? }
  346. TmpResult := False;
  347. { HighRegInfo will contain the RegInfo for the longest sequence of matching }
  348. { instructions found }
  349. New(HighRegInfo, Init);
  350. { how many instructions are in the sequence describing the content of Reg }
  351. { (the parameter) in the old sequence }
  352. OrgRegFound := 0;
  353. { how many instructions are in the longest sequence of matching }
  354. { instructions found until now? }
  355. HighFound := 0;
  356. { does the content of Reg in the old equence match the content of Reg in }
  357. { the new sequence }
  358. OrgRegResult := False;
  359. RegCounter := LoGPReg;
  360. { PrevNonRemovablePai's OptInfo contains the contents of the registers }
  361. { before the current instruction is executed. It will be used to compare }
  362. { the new contents with and to see whether the new instructions can be }
  363. { removed }
  364. GetLastInstruction(p, PrevNonRemovablePai);
  365. { don't check registers that only contain a constant or something unknown }
  366. While (RegCounter <= HiGPReg And
  367. (PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].Typ <> Con_Ref) Do
  368. Inc(RegCounter);
  369. While (RegCounter <= HiGPReg) Do
  370. Begin
  371. { reinitialize the reginfo fields }
  372. Init;
  373. { no matching instructions found yet }
  374. Found := 0;
  375. With PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter] Do
  376. Begin
  377. { get the first instruction that describes the content of the }
  378. { the register we're going to check the way it was before the }
  379. { current instruction got executed }
  380. oldp := StartMod;
  381. { how many instructions describe the content of the register }
  382. { before the current instructions got executed? }
  383. OldNrOfMods := NrOfMods
  384. End;
  385. { p is the first instruction that describes the content of Reg }
  386. { after p (= the current instruction) got executed }
  387. newp := p;
  388. { it's possible that the old contents of the current register are }
  389. { described by a sequence of instructions that also contains the }
  390. { one in parameter p. In that case, we have to compare until we }
  391. { encounter p. Otherwise, compare as much instructions as there are }
  392. { in the old sequence or until there's a mismatch }
  393. While (p <> oldp) And
  394. (Found < OldNrOfMods) And
  395. { old new }
  396. InstructionsEquivalent(oldp, newp, RegInfo) Do
  397. Begin
  398. GetNextInstruction(oldp, oldp);
  399. GetNextInstruction(newp, newp);
  400. Inc(Found)
  401. End;
  402. If (Found < OldNrOfMods) Then
  403. Begin
  404. { the old sequence was longer than than the new one, so no match }
  405. TmpResult := False;
  406. { If there is no match, we have to set the CanBeRemoved flag of }
  407. { all pai objects part of the new sequence to false, because it's }
  408. { possible that some of them have already been scheduled for }
  409. { removal after checking another sequence (an instruction can be }
  410. { of more than one sequence). If we return false, the number }
  411. { returned in found denotes how many instructions have to have }
  412. { their CanBeRemoved flag set to false }
  413. { We only have to set those flags to false if their was a partial }
  414. { match of instructions (found > 0), because otherwise they can't }
  415. { have been set to true in a previous comparison }
  416. If (found > 0) Then
  417. Found := PPaiProp(Pai(p)^.OptInfo)^.Regs[Reg].NrOfMods
  418. End
  419. Else TmpResult := True;
  420. If (RegCounter = Reg) Then
  421. Begin
  422. OrgRegFound := Found;
  423. OrgRegResult := TmpResult;
  424. New(OrgRegInfo, InitWithValue(RegInfo));
  425. End
  426. Else
  427. If TmpResult And
  428. (Found > HighFound) Then
  429. Begin
  430. HighFound := Found;
  431. HighRegInfo^.InitWithValue(RegInfo);
  432. End;
  433. RegInfo.Done;
  434. Repeat
  435. Inc(RegCounter);
  436. Until (RegCounter > HiGPReg) or
  437. (PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].Typ =
  438. Con_Ref);
  439. End;
  440. If (HighFound > 0) And
  441. (Not(OrgRegResult) Or
  442. (HighFound > OrgRegFound)) Then
  443. Begin
  444. CheckSequence := True;
  445. Found := HighFound
  446. InitWithValue(HighRegInfo);
  447. End
  448. Else
  449. Begin
  450. CheckSequence := OrgRegResult;
  451. Found := OrgRegFound;
  452. InitWithValue(OrgRegInfo);
  453. End;
  454. Dispose(HighRegInfo, Done);
  455. Dispose(OrgRegInfo, Done)
  456. End; {CheckSequence}
  457. { ************************************************************************* }
  458. { ******************************* TAOptCSE ******************************** }
  459. { ************************************************************************* }
  460. Function TAOptCSE.RegModifiedByInstruction(Reg: TRegister; p1: Pai): Boolean;
  461. Var hp: Pai;
  462. Begin
  463. If GetLastInstruction(p1, hp)
  464. Then
  465. RegModifiedByInstruction :=
  466. PPAiProp(p1^.OptInfo)^.GetWState <>
  467. PPAiProp(hp^.OptInfo)^.GetWState
  468. Else RegModifiedByInstruction := True;
  469. End;
  470. Procedure TAoptCSE.RestoreContents(Current: Pai; Reg: TRegister);
  471. Var Prev, hp3, hp5: Pai;
  472. TmpState: TStateInt;
  473. Cnt, Cnt2: Byte;
  474. Begin
  475. { load Cnt2 with the total number of instructions of this sequence }
  476. Cnt2 := PPaiProp(Prev^.OptInfo)^.Regs[RegInfo.New2OldReg[reg]].
  477. NrOfMods;
  478. { sometimes, a register can not be removed from a sequence, because it's }
  479. { still used afterwards: }
  480. { }
  481. { movl -8(%ebp), %eax movl -8(%ebp), %eax }
  482. { movl 70(%eax), %eax movl 70(%eax), %eax }
  483. { cmpl 74(%eax), %eax cmpl 74(%eax), %eax }
  484. { jne l1 can't be changed to jne l1 }
  485. { movl -8(%ebp), %eax }
  486. { movl 70(%eax), %edi movl %eax, %edi }
  487. { boundl R_282, %edi boundl R_282, %edi }
  488. { pushl 70(%eax) pushl 70(%eax) }
  489. { }
  490. { because eax now contains the wrong value when 70(%eax) is pushed }
  491. { start at the first instruction of the sequence }
  492. hp3 := Current;
  493. For Cnt := 1 to Pred(Cnt2) Do
  494. GetNextInstruction(hp3, hp3);
  495. { hp3 now containts the last instruction of the sequence }
  496. { get the writestate at this point of the register in TmpState }
  497. TmpState := PPaiProp(hp3^.OptInfo)^.GetWState(reg);
  498. { hp3 := first instruction after the sequence }
  499. GetNextInstruction(hp3, hp3);
  500. { now, even though reg is in RegsLoadedForRef, sometimes it's still used }
  501. { afterwards. It is not if either it is not in usedregs anymore after the }
  502. { sequence, or if it is loaded with a new value right after the sequence }
  503. If (TmpState <> PPaiProp(hp3^.OptInfo)^.Regs[reg].WState) Or
  504. Not(reg in PPaiProp(hp3^.OptInfo)^.UsedRegs) Then
  505. { the register is not used anymore after the sequence! }
  506. Begin
  507. {$ifdef csdebug}
  508. Writeln('Cnt2: ',Cnt2);
  509. hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
  510. InsertLLItem(Pai(Current^.previous), Current, hp5);
  511. {$endif csdebug}
  512. hp3 := Current;
  513. { first change the contents of the register inside the sequence }
  514. For Cnt := 1 to Cnt2 Do
  515. Begin
  516. {save the WState of the last pai object of the sequence for later use}
  517. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[reg].WState;
  518. {$ifdef csdebug}
  519. hp5 := new(pai_asm_comment,init(strpnew('WState for '+
  520. std_reg2str[reg]+': '+tostr(tmpstate))));
  521. InsertLLItem(hp3, pai(hp3^.next), hp5);
  522. {$endif csdebug}
  523. PPaiProp(hp3^.OptInfo)^.Regs[reg] :=
  524. PPaiProp(Prev^.OptInfo)^.Regs[reg];
  525. GetNextInstruction(hp3, hp3);
  526. End;
  527. { here, hp3 = p = Pai object right after the sequence, TmpState = WState of }
  528. { reg at the last Pai object of the sequence }
  529. GetLastInstruction(hp3, hp3);
  530. { now, as long as the register isn't modified after the sequence, set its }
  531. { contents to what they were before the sequence }
  532. While GetNextInstruction(hp3, hp3) And
  533. (PPaiProp(hp3^.OptInfo)^.GetWState(Reg) = TmpState) Do
  534. {$ifdef csdebug}
  535. begin
  536. hp5 := new(pai_asm_comment,init(strpnew('WState for '+std_reg2str[reg]+': '+
  537. tostr(PPaiProp(hp3^.OptInfo)^.GetWState(reg)))));
  538. InsertLLItem(hp3, pai(hp3^.next), hp5);
  539. {$endif csdebug}
  540. PPaiProp(hp3^.OptInfo)^.Regs[reg] :=
  541. PPaiProp(Prev^.OptInfo)^.Regs[reg];
  542. {$ifdef csdebug}
  543. end;
  544. {$endif csdebug}
  545. End
  546. Else
  547. { the register is still used after the sequence, so undelete all }
  548. { instructions in the sequence that modify reg }
  549. Begin
  550. {$ifdef csdebug}
  551. Writeln('Got there for ',std_reg2str[reg]);
  552. {$endif csdebug}
  553. hp3 := Current;
  554. For Cnt := 1 to Cnt2 Do
  555. Begin
  556. If RegModifiedByInstruction(reg, hp3) Then
  557. PPaiProp(hp3^.OptInfo)^.CanBeRemoved := False;
  558. GetNextInstruction(hp3, hp3);
  559. End;
  560. End;
  561. {$ifdef csdebug}
  562. hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  563. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  564. {$endif csdebug}
  565. End;
  566. Procedure TAoptCSE.DoCSE;
  567. {marks the instructions that can be removed by RemoveInstructs. They're not
  568. removed immediately because sometimes an instruction needs to be checked in
  569. two different sequences}
  570. Var Cnt, Cnt2: Longint;
  571. p, hp1, Current: Pai;
  572. hp3, Prev: Pai;
  573. {$ifdef csdebug}
  574. hp5: pai;
  575. {$endif csdebug}
  576. RegInfo: TRegInfo;
  577. RegCounter: TRegister;
  578. TmpState: Byte;
  579. Begin
  580. p := SkipHead(BlockStart);
  581. While (p <> BlockEnd) Do
  582. Begin
  583. Case p^.typ Of
  584. ait_instruction:
  585. Begin
  586. { Case PInstr(p)^.opcode Of
  587. A_CLD: If GetLastInstruction(p, hp1) And
  588. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  589. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;}
  590. If IsLoadMemReg(p) Then
  591. Begin
  592. If (p = PPaiProp(p^.OptInfo)^.Regs[RegMaxSize(
  593. PInstr(p)^.oper[LoadDst].reg)].StartMod) And
  594. GetLastInstruction (p, hp1) And
  595. (hp1^.typ <> ait_marker) Then
  596. {so we don't try to check a sequence when p is the first instruction of the block}
  597. If CheckSequence(p, PInstr(p)^.oper[LoadDst].reg, Cnt) And
  598. (Cnt > 0) Then
  599. Begin
  600. hp1 := nil;
  601. { although it's perfectly ok to remove an instruction which doesn't contain }
  602. { the register that we've just checked (CheckSequence takes care of that), }
  603. { the sequence containing this other register should also be completely }
  604. { checked (and either removed or marked as non-removable), otherwise we }
  605. { may get situations like this: }
  606. { }
  607. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  608. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  609. { movl 8(%edx), %edx movl 8(%edx), %edx }
  610. { movl (%eax), eax movl (%eax), eax }
  611. { cmpl %eax, %edx cmpl %eax, %edx }
  612. { jnz l123 getting converted to jnz l123 }
  613. { movl 12(%ebp), %edx movl 4(%eax), eax }
  614. { movl 16(%ebp), %eax }
  615. { movl 8(%edx), %edx }
  616. { movl 4(%eax), eax }
  617. Current := p;
  618. Cnt2 := 1;
  619. { after this while loop, if hp1 <> nil it will contain the pai object }
  620. { that's the start of a sequence that's not completely checked yet }
  621. While Cnt2 <= Cnt Do
  622. Begin
  623. If (hp1 = nil) And
  624. Not(RegInInstruction(
  625. PInstr(Current)^.oper[LoadDst].reg,p) Or
  626. RegInInstruction(RegMaxSize(PInstr(
  627. Current)^.oper[LoadDst].reg), p)) And
  628. { do not recheck a sequence if it's completely part of the one we just }
  629. { checked }
  630. Not(IsLoadMemReg(p) And
  631. (PPaiProp(p^.OptInfo)^.Regs[RegMaxSize(
  632. PInstr(p)^.Oper[LoadDst].reg)]
  633. .NrOfMods <= (Cnt - Cnt2 + 1))) Then
  634. hp1 := p;
  635. {$ifndef noremove}
  636. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  637. {$endif noremove}
  638. Inc(Cnt2);
  639. GetNextInstruction(p, p);
  640. End;
  641. { insert a marker noting that for the following instructions no PPaiProp's }
  642. { (containing optimizer info) have been generated, so GetNext/ }
  643. { LastInstruction will ignore them (it will use the original instructions) }
  644. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  645. InsertLLItem(Pai(Current^.Previous), Current, hp3);
  646. { Prev is used to get the contents of the registers before the sequence }
  647. GetLastInstruction(Current, Prev);
  648. { If some registers were different in the old and the new sequence, move }
  649. { the contents of those old registers to the new ones, e.g. }
  650. { }
  651. { mov mem1, reg1 mov mem1, reg1 }
  652. { ... can be changed to ... }
  653. { mov mem1, reg2 mov reg1, reg2 }
  654. {$IfDef CSDebug}
  655. For RegCounter := LoGPReg To HiGPReg Do
  656. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  657. Begin
  658. hp5 := new(pai_asm_comment,init(strpnew(
  659. 'New: '+std_reg2str[RegCounter]+', Old: '+
  660. std_reg2str[RegInfo.New2OldReg[RegCounter]])));
  661. InsertLLItem(AsmL, Pai(Current^.previous), Current, hp5);
  662. End;
  663. {$EndIf CSDebug}
  664. For RegCounter := LoGPReg to HiGPReg Do
  665. Begin
  666. { if New2OldReg[RegCounter] = R_NO, it means this register doesn't appear }
  667. { the new nor the old sequence }
  668. If (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  669. { if a register is in RegsLoadedForRef, it means this register was loaded }
  670. { with a value only to function as a base or index in a reference. The }
  671. { practical upshot of this is that this value won't be used anymore later }
  672. { on, so even if another register was used in the new sequence for this, }
  673. { we don't have to load it. E.g. }
  674. { }
  675. { movl 8(%ebp), %eax " }
  676. { movl 4(%eax), %eax " }
  677. { movl (%eax), %edi " }
  678. { movl %edi, 12(%ebp) " }
  679. { ... can be changed to " }
  680. { movl 8(%ebp), %edx }
  681. { movl 4(%edx), %edx }
  682. { movl (%edx), %ebx movl %edi, %ebx }
  683. { }
  684. { There is no need to also add a "movl %eax, %edx" }
  685. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  686. {old reg new reg}
  687. { no need to reload the register if it's the same in the old and new }
  688. { sequence }
  689. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  690. Begin
  691. hp3 := a_load_reg_reg(
  692. {old reg new reg}
  693. RegInfo.New2OldReg[RegCounter], RegCounter));
  694. InsertLLItem(Pai(Current^.previous), Current, hp3);
  695. End
  696. Else
  697. { As noted before, if a register is in RegsLoadedForRef, it doesn't have }
  698. { to be loaded. However, when data flow analyzer processed this code, the }
  699. { was loaded, so we need to change that. This is done by setting the }
  700. { contents of the register to its contents before the new sequence, for }
  701. { every instruction until the first load of the register with a new value }
  702. If (RegCounter In RegInfo.RegsLoadedForRef) Then
  703. RestoreOrigContents(Current, RegCounter);
  704. End;
  705. { the end of the area where instructions without optimizer info can occur }
  706. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  707. InsertLLItem(AsmL, Pai(Current^.Previous), Current, hp3);
  708. { if we found an instruction sequence that needs complete re-evaluation, }
  709. { process it }
  710. If hp1 <> nil Then p := hp1;
  711. Continue;
  712. End
  713. Else
  714. { checksequence returned false. In that case, if the current instruction }
  715. { was already deleted (as part of another sequence), we have to undelete }
  716. { all instructions pertaining to the register whose sequence we just }
  717. { checked }
  718. If (Cnt > 0) And
  719. (PPaiProp(p^.OptInfo)^. Regs[RegMaxSize(PInstr(p)^.
  720. oper[LoadDst].reg)].Typ = Con_Ref) And
  721. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  722. Begin
  723. Current := p;
  724. Cnt2 := 1;
  725. While Cnt2 <= Cnt Do
  726. Begin
  727. If RegInInstruction(PInstr(Current)^.
  728. oper[LoadDst].reg, p) Or
  729. RegInInstruction(RegMaxSize(PInstr(Current)^.
  730. oper[LoadDst].reg), p) Then
  731. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  732. Inc(Cnt2);
  733. GetNextInstruction(p, p);
  734. End;
  735. Continue;
  736. End;
  737. End
  738. Else if IsLoadConstReg(p) Then
  739. Begin
  740. If GetLastInstruction(p, hp1) Then
  741. With PPaiProp(hp1^.OptInfo)^.Regs[
  742. RegMaxSize(PInstr(p)^.oper[LoadDst].reg)] Do
  743. If (Typ = Con_Const) And
  744. (StartMod = p) Then
  745. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  746. End
  747. Else
  748. CpuCSE(p);
  749. { A_STD: If GetLastInstruction(p, hp1) And
  750. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  751. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  752. A_XOR:
  753. Begin
  754. If (Paicpu(p)^.oper[0].typ = top_reg) And
  755. (Paicpu(p)^.oper[0].typ = top_reg) And
  756. (Paicpu(p)^.oper[1].reg = Paicpu(p)^.oper[1].reg) And
  757. GetLastInstruction(p, hp1) And
  758. (PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)].typ = con_const) And
  759. (PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)].StartMod = nil)
  760. Then PPaiProp(p^.OptInfo)^.CanBeRemoved := True
  761. End
  762. End;
  763. End;
  764. GetNextInstruction(p, p);
  765. End;
  766. End;
  767. Procedure RemoveInstructs;
  768. {Removes the marked instructions and disposes the PPaiProps of the other
  769. instructions, restoring their line number}
  770. Var p, hp1: Pai;
  771. InstrCnt: Longint;
  772. Begin
  773. p := SkipHead(BlockStart);
  774. InstrCnt := 1;
  775. While (p <> BlockEnd) Do
  776. Begin
  777. {$ifndef noinstremove}
  778. If PPaiProp(p^.OptInfo)^.CanBeRemoved
  779. Then
  780. Begin
  781. Dispose(PPaiProp(p^.OptInfo));
  782. GetNextInstruction(p, hp1);
  783. AsmL^.Remove(p);
  784. Dispose(p, Done);
  785. p := hp1;
  786. Inc(InstrCnt);
  787. End
  788. Else
  789. {$endif noinstremove}
  790. Begin
  791. Dispose(PPaiProp(p^.OptInfo));
  792. p^.OptInfo := nil;
  793. GetNextInstruction(p, p);
  794. Inc(InstrCnt);
  795. End;
  796. End;
  797. End;
  798. Procedure TAoptCSE.CSE;
  799. Begin
  800. DoCSE;
  801. RemoveInstructs;
  802. End;
  803. End.
  804. {
  805. $Log$
  806. Revision 1.8 2003-03-28 19:16:56 peter
  807. * generic constructor working for i386
  808. * remove fixed self register
  809. * esi added as address register for i386
  810. Revision 1.7 2002/05/18 13:34:05 peter
  811. * readded missing revisions
  812. Revision 1.6 2002/05/16 19:46:34 carl
  813. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  814. + try to fix temp allocation (still in ifdef)
  815. + generic constructor calls
  816. + start of tassembler / tmodulebase class cleanup
  817. Revision 1.4 2002/04/20 21:32:23 carl
  818. + generic FPC_CHECKPOINTER
  819. + first parameter offset in stack now portable
  820. * rename some constants
  821. + move some cpu stuff to other units
  822. - remove unused constents
  823. * fix stacksize for some targets
  824. * fix generic size problems which depend now on EXTEND_SIZE constant
  825. Revision 1.3 2002/04/15 18:55:39 carl
  826. + change reg2str array use
  827. Revision 1.2 2002/04/14 16:49:30 carl
  828. + att_reg2str -> gas_reg2str
  829. }