2
0

aoptcs.pas 38 KB

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