csopt386.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. {
  2. $Id$
  3. Copyright (c) 1997-98 by Jonas Maebe
  4. This unit contains the common subexpression elimination procedure.
  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 CSOpt386;
  19. Interface
  20. Uses aasm;
  21. {Procedure CSOpt386(First, Last: Pai);}
  22. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  23. Implementation
  24. Uses CObjects, verbose, hcodegen, globals
  25. {$ifdef i386}
  26. ,i386, DAOpt386
  27. {$endif i386}
  28. ;
  29. Function PaiInSequence(P: Pai; Const Seq: TContent): Boolean;
  30. Var P1: Pai;
  31. Counter: Byte;
  32. TmpResult: Boolean;
  33. Begin
  34. TmpResult := False;
  35. P1 := Seq.StartMod;
  36. Counter := 1;
  37. While Not(TmpResult) And
  38. (Counter <= Seq.NrOfMods) Do
  39. Begin
  40. If (P = P1) Then TmpResult := True;
  41. Inc(Counter);
  42. p1 := Pai(p1^.Next);
  43. End;
  44. PaiInSequence := TmpResult;
  45. End;
  46. Function CheckSequence(p: Pai; Reg: TRegister; Var Found: Longint; Var RegInfo: TRegInfo): Boolean;
  47. {checks whether the current instruction sequence (starting with p) and the
  48. one between StartMod and EndMod of Reg are the same. If so, the number of
  49. instructions that match is stored in Found and true is returned, otherwise
  50. Found holds the number of instructions between StartMod and EndMod and false
  51. is returned}
  52. Var hp2, hp3, EndMod: Pai;
  53. PrevNonRemovablePai: Pai;
  54. OrgRegInfo, HighRegInfo: TRegInfo;
  55. HighFound, OrgRegFound: Byte;
  56. RegCounter: TRegister;
  57. OrgRegResult: Boolean;
  58. TmpResult: Boolean;
  59. OldNrOfMods: Byte;
  60. Begin {CheckSequence}
  61. Reg := Reg32(Reg);
  62. TmpResult := False;
  63. HighFound := 0;
  64. RegCounter := R_EAX;
  65. GetLastInstruction(p, PrevNonRemovablePai);
  66. While (PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].Typ <> Con_Ref) Or
  67. { the next check is because what a register contains is stored in the
  68. ppaiprop of the first instruction of the loading sequence. If it's left
  69. out,
  70. movl 8(%ebp), %eax movl 8(%ebp), %eax
  71. movl 8(%ebp), %edx movl %eax, %edx
  72. movl (%edx), %edx would be changed to
  73. movl (%eax), %eax movl (%eax), %eax
  74. cmpl (%edx), %eax cmpl (%edx), %eax}
  75. ((Regcounter <> Reg) And
  76. PaiInSequence(p, PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter])) Do
  77. Inc(RegCounter);
  78. {no need to check whether RegCounter becomes > R_EDI here, because at least
  79. PPaiProp(p^.fileinfo.line)^.Regs[Reg].Typ = Con_Ref}
  80. Repeat
  81. FillChar(RegInfo, SizeOf(RegInfo), 0);
  82. RegInfo.RegsEncountered := [ProcInfo.FramePointer, R_ESP];
  83. RegInfo.SubstRegs[ProcInfo.FramePointer] := ProcInfo.FramePointer;
  84. RegInfo.SubstRegs[R_ESP] := R_ESP;
  85. Found := 0;
  86. hp2 := PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].StartMod;
  87. { EndMod := hp2;
  88. If (PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].NrOfMods = 1)
  89. Then OldNrOfMods := 1
  90. Else
  91. For OldNrOfMods := 2 to PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].NrOfMods Do
  92. GetNextInstruction(EndMod, EndMod);}
  93. OldNrOfMods := PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].NrOfMods;
  94. hp3 := p;
  95. While (Found <> OldNrOfMods) And
  96. { old new }
  97. InstructionsEquivalent(hp2, hp3, RegInfo) Do
  98. Begin
  99. GetNextInstruction(hp2, hp2);
  100. GetNextInstruction(hp3, hp3);
  101. Inc(Found)
  102. End;
  103. If (Found <> OldNrOfMods)
  104. Then
  105. Begin
  106. (* If ((Found+1) = OldNrOfMods) And
  107. Assigned(hp2) And
  108. (Pai(hp2)^.typ = ait_instruction) And
  109. (Pai386(hp2)^._operator In [A_MOV, A_MOVZX]) And
  110. (Pai386(hp2)^.op1t = top_ref) And
  111. (Pai386(hp2)^.op2t = top_reg) And
  112. Assigned(hp3) And
  113. (Pai(hp3)^.typ = ait_instruction) And
  114. (Pai386(hp3)^._operator In [A_MOV, A_MOVZX]) And
  115. (Pai386(hp3)^.op1t = top_ref) And
  116. (Pai386(hp3)^.op2t = top_reg) And
  117. (Pai386(hp2)^._operator <> Pai386(hp3)^._operator) And
  118. {$IfDef RegInfo}
  119. RefsEquivalent(TReference(Pai386(hp2)^.op1^),TReference(Pai386(hp3)^.op1^), RegInfo)
  120. {$Else RegInfo}
  121. RefsEqual(TReference(Pai386(hp2)^.op1^),TReference(Pai386(hp3)^.op1^))
  122. {$EndIf RegInfo}
  123. Then
  124. {hack to be able to optimize
  125. mov (mem), reg
  126. mov (reg), reg
  127. mov (reg), reg [*]
  128. test reg, reg and the oposite (where the marked instructions are
  129. jne l1 switched)
  130. mov (mem), reg
  131. mov (reg), reg
  132. movzx (reg), reg [*]}
  133. If (Pai386(hp2)^._operator = A_MOV)
  134. Then
  135. Begin
  136. If (Pai386(hp2)^.Size = S_B) And
  137. {$IfDef RegInfo}
  138. RegsEquivalent(Reg8toReg32(TRegister(Pai386(hp2)^.op2)),
  139. TRegister(Pai386(hp3)^.op2), RegInfo)
  140. {$Else RegInfo}
  141. (Reg8toReg32(TRegister(Pai386(hp2)^.op2)) =
  142. TRegister(Pai386(hp3)^.op2))
  143. {$EndIf RegInfo}
  144. Then
  145. Begin
  146. Pai386(hp2)^._operator := A_MOVZX;
  147. Pai386(hp2)^.op2 := Pai386(hp3)^.op2;
  148. Pai386(hp2)^.Size := S_BL;
  149. Inc(Found);
  150. TmpResult := True;
  151. End
  152. Else
  153. Begin
  154. TmpResult := False;
  155. If (Found > 0) Then
  156. Found := PPaiProp(Pai(p)^.fileinfo.line)^.Regs[Reg].NrOfMods
  157. End
  158. End
  159. Else
  160. Begin
  161. If (Pai386(hp3)^.Size = S_B) And
  162. {$IfDef RegInfo}
  163. RegsEquivalent(TRegister(Pai386(hp2)^.op2),
  164. Reg8toReg32(TRegister(Pai386(hp3)^.op2)),
  165. RegInfo)
  166. {$Else RegInfo}
  167. (Reg8toReg32(TRegister(Pai386(hp3)^.op2)) =
  168. TRegister(Pai386(hp2)^.op2))
  169. {$EndIf RegInfo}
  170. Then
  171. Begin
  172. TmpResult := True;
  173. Inc(Found)
  174. End
  175. Else
  176. Begin
  177. TmpResult := False;
  178. If (Found > 0) Then
  179. Found := PPaiProp(Pai(p)^.fileinfo.line)^.Regs[Reg].NrOfMods
  180. End
  181. End
  182. Else *)
  183. Begin
  184. TmpResult := False;
  185. If (found > 0) then
  186. {this is correct because we only need to turn off the CanBeRemoved flag
  187. when an instruction has already been processed by CheckSequence
  188. (otherwise CanBeRemoved can't be true and thus can't have to be turned off).
  189. If it has already been processed by CheckSequence and flagged to be
  190. removed, it means that it has been checked against a previous sequence
  191. and that it was equal (otherwise CheckSequence would have returned false
  192. and the instruction wouldn't have been removed). If this "If found > 0"
  193. check is left out, incorrect optimizations are performed.}
  194. Found := PPaiProp(Pai(p)^.fileinfo.line)^.Regs[Reg].NrOfMods
  195. End
  196. End
  197. Else TmpResult := True;
  198. If TmpResult And
  199. (Found > HighFound)
  200. Then
  201. Begin
  202. HighFound := Found;
  203. HighRegInfo := RegInfo;
  204. End;
  205. If (RegCounter = Reg) Then
  206. Begin
  207. OrgRegFound := Found;
  208. OrgRegResult := TmpResult;
  209. OrgRegInfo := RegInfo
  210. End;
  211. Repeat
  212. Inc(RegCounter);
  213. Until (RegCounter > R_EDI) or
  214. ((PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter].Typ = Con_Ref) And
  215. ((Regcounter = Reg) Or
  216. Not(PaiInSequence(p, PPaiProp(PrevNonRemovablePai^.fileinfo.line)^.Regs[RegCounter]))));
  217. Until (RegCounter > R_EDI);
  218. If (HighFound > 0) And
  219. (Not(OrgRegResult) Or
  220. (HighFound > OrgRegFound))
  221. Then
  222. Begin
  223. CheckSequence := True;
  224. RegInfo := HighRegInfo;
  225. Found := HighFound
  226. End
  227. Else
  228. Begin
  229. CheckSequence := OrgRegResult;
  230. Found := OrgRegFound;
  231. RegInfo := OrgRegInfo;
  232. End;
  233. End; {CheckSequence}
  234. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  235. {marks the instructions that can be removed by RemoveInstructs. They're not
  236. removed immediately because sometimes an instruction needs to be checked in
  237. two different sequences}
  238. Var Cnt, Cnt2: Longint;
  239. p, hp1, hp2: Pai;
  240. hp3, hp4: Pai;
  241. {$ifdef csdebug}
  242. hp5: pai;
  243. {$endif csdebug}
  244. RegInfo: TRegInfo;
  245. RegCounter: TRegister;
  246. TmpState: Word;
  247. Begin
  248. p := First;
  249. If (p^.typ in (SkipInstr+ait_marker)) Then
  250. GetNextInstruction(p, p);
  251. First := p;
  252. While Assigned(p) Do
  253. Begin
  254. Case p^.typ Of
  255. ait_instruction:
  256. Begin
  257. Case Pai386(p)^._operator Of
  258. A_CLD: If GetLastInstruction(p, hp1) And
  259. (PPaiProp(hp1^.fileinfo.line)^.DirFlag = F_NotSet) Then
  260. PPaiProp(Pai(p)^.fileinfo.line)^.CanBeRemoved := True;
  261. A_MOV, A_MOVZX, A_MOVSX:
  262. Begin
  263. Case Pai386(p)^.op1t Of
  264. { Top_Reg:
  265. Case Pai386(p)^.op2t Of
  266. Top_Reg:;
  267. Top_Ref:;
  268. End;}
  269. Top_Ref:
  270. Begin {destination is always a register in this case}
  271. With PPaiProp(p^.fileinfo.line)^.Regs[Reg32(Tregister(Pai386(p)^.op2))] Do
  272. Begin
  273. { hp1 := p;
  274. While GetLastInstruction(hp1, hp1) And
  275. PPaiProp(hp1^.fileinfo.line)^.CanBeRemoved Do;}
  276. GetLastInstruction(p, hp1);
  277. If Assigned(hp1) And
  278. (PPaiProp(hp1^.fileinfo.line)^.
  279. Regs[Reg32(TRegister(Pai386(p)^.op2))].typ = con_ref) Then
  280. {so we don't try to check a sequence when the register only contains a constant}
  281. If CheckSequence(p, TRegister(Pai386(p)^.op2), Cnt, RegInfo) And
  282. (Cnt > 0)
  283. Then
  284. Begin
  285. hp1 := nil;
  286. {although it's perfectly ok to remove an instruction which doesn't contain
  287. the register that we've just checked (CheckSequence takes care of that),
  288. the sequence containing this other register should also be completely
  289. checked and removed, otherwise we may get situations like this:
  290. movl 12(%ebp), %edx movl 12(%ebp), %edx
  291. movl 16(%ebp), %eax movl 16(%ebp), %eax
  292. movl 8(%edx), %edx movl 8(%edx), %edx
  293. movl (%eax), eax movl (%eax), eax
  294. cmpl %eax, %edx cmpl %eax, %edx
  295. jnz l123 getting converted to jnz l123
  296. movl 12(%ebp), %edx movl 4(%eax), eax
  297. movl 16(%ebp), %eax
  298. movl 8(%edx), %edx
  299. movl 4(%eax), eax}
  300. hp2 := p;
  301. Cnt2 := 1;
  302. While Cnt2 <= Cnt Do
  303. Begin
  304. If (hp1 = nil) And
  305. Not(RegInInstruction(Tregister(Pai386(hp2)^.op2), p) Or
  306. RegInInstruction(Reg32(Tregister(Pai386(hp2)^.op2)), p))
  307. Then hp1 := p;
  308. {$ifndef noremove}
  309. PPaiProp(p^.fileinfo.line)^.CanBeRemoved := True;
  310. {$endif noremove}
  311. Inc(Cnt2);
  312. GetNextInstruction(p, p);
  313. End;
  314. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  315. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  316. {imagine the following code:
  317. normal wrong optimized
  318. movl 8(%ebp), %eax movl 8(%ebp), %eax
  319. movl (%eax), %eax movl (%eax), %eax
  320. cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax
  321. jne l1 jne l1
  322. movl 8(%ebp), %eax
  323. movl (%eax), %edi movl %eax, %edi
  324. movl %edi, -4(%ebp) movl %edi, -4(%ebp)
  325. movl 8(%ebp), %eax
  326. pushl 70(%eax) pushl 70(%eax)
  327. The error is that at the moment that the last instruction is executed,
  328. %eax doesn't contain 8(%ebp) anymore. Solution: the contents of registers
  329. that are completely removed from a sequence, have to be changed to their
  330. contents from before the sequence.}
  331. {hp4 is used to get the contents of the registers before the sequence}
  332. GetLastInstruction(hp2, hp4);
  333. {If some registers were different in the old and the new sequence, move
  334. the contents of those old registers to the new ones}
  335. {$IfDef CSDebug}
  336. For RegCounter := R_EAX To R_EDI Do
  337. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  338. Begin
  339. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  340. att_reg2str[RegInfo.SubstRegs[RegCounter]])));
  341. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  342. End;
  343. {$EndIf CSDebug}
  344. For RegCounter := R_EAX To R_EDI Do
  345. Begin
  346. If (RegInfo.SubstRegs[RegCounter] <> R_NO) Then
  347. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  348. {new reg old reg}
  349. (RegInfo.SubstRegs[RegCounter] <> RegCounter) Then
  350. Begin
  351. hp3 := New(Pai386,Op_Reg_Reg(A_MOV, S_L,
  352. {old reg new reg}
  353. RegInfo.SubstRegs[RegCounter], RegCounter));
  354. hp3^.fileinfo := hp2^.fileinfo;
  355. hp3^.fileinfo.line := PPaiProp(hp2^.fileinfo.line)^.LineSave;
  356. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
  357. End
  358. Else
  359. If (RegCounter In RegInfo.RegsLoadedForRef) Then
  360. {change the contents of this register to the its contents before the
  361. sequence (for all instructions in and after the sequence, until the register
  362. is reloaded)}
  363. Begin
  364. {load Cnt2 with the total number of instructions of this sequence}
  365. Cnt2 := PPaiProp(hp4^.fileinfo.line)^.
  366. Regs[RegInfo.SubstRegs[RegCounter]].NrOfMods;
  367. {sometimes, a register can not be removed from a sequence, because it's
  368. still used afterwards:
  369. movl -8(%ebp), %eax movl -8(%ebp), %eax
  370. movl 70(%eax), %eax movl 70(%eax), %eax
  371. cmpl 74(%eax), %eax cmpl 74(%eax), %eax
  372. jne l1 can't be changed to jne l1
  373. movl -8(%ebp), %eax
  374. movl 70(%eax), %edi movl %eax, %edi
  375. boundl R_282, %edi boundl R_282, %edi
  376. pushl 70(%eax) pushl 70(%eax)
  377. because eax now contains the wrong value when 70(%eax) is pushed}
  378. hp3 := hp2;
  379. For Cnt := 1 to Pred(Cnt2) Do
  380. GetNextInstruction(hp3, hp3);
  381. TmpState := PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State;
  382. GetNextInstruction(hp3, hp3);
  383. If (TmpState <> PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State) Or
  384. Not(RegCounter in PPaiProp(hp3^.fileinfo.line)^.UsedRegs) Then
  385. Begin
  386. {$ifdef csdebug}
  387. Writeln('Cnt2: ',Cnt2);
  388. hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
  389. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  390. {$endif csdebug}
  391. hp3 := hp2;
  392. {first change the contents of the register inside the sequence}
  393. For Cnt := 1 to Cnt2 Do
  394. Begin
  395. {save the state of the last pai object of the sequence for later use}
  396. TmpState := PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State;
  397. {$ifdef csdebug}
  398. hp5 := new(pai_asm_comment,init(strpnew('State for '+att_reg2str[Regcounter]+': '
  399. +tostr(tmpstate))));
  400. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  401. {$endif csdebug}
  402. PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter] :=
  403. PPaiProp(hp4^.fileinfo.line)^.Regs[RegCounter];
  404. GetNextInstruction(hp3, hp3);
  405. End;
  406. {here, hp3 = p = Pai object right after the sequence, TmpState = state of
  407. RegCounter at the last Pai object of the sequence}
  408. GetLastInstruction(hp3, hp3);
  409. While GetNextInstruction(hp3, hp3) And
  410. (PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State
  411. = TmpState) Do
  412. {$ifdef csdebug}
  413. begin
  414. hp5 := new(pai_asm_comment,init(strpnew('State for '+att_reg2str[Regcounter]+': '+
  415. tostr(PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter].State))));
  416. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  417. {$endif csdebug}
  418. PPaiProp(hp3^.fileinfo.line)^.Regs[RegCounter] :=
  419. PPaiProp(hp4^.fileinfo.line)^.Regs[RegCounter];
  420. {$ifdef csdebug}
  421. end;
  422. {$endif csdebug}
  423. End
  424. Else
  425. Begin
  426. {$ifdef csdebug}
  427. Writeln('Got there for ',att_Reg2Str[RegCounter]);
  428. {$endif csdebug}
  429. hp3 := hp2;
  430. For Cnt := 1 to Cnt2 Do
  431. Begin
  432. If RegModifiedByInstruction(RegCounter, hp3)
  433. Then PPaiProp(hp3^.fileinfo.line)^.CanBeRemoved := False;
  434. GetNextInstruction(hp3, hp3);
  435. End;
  436. End;
  437. {$ifdef csdebug}
  438. hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  439. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  440. {$endif csdebug}
  441. End;
  442. End;
  443. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  444. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  445. If hp1 <> nil Then p := hp1;
  446. Continue;
  447. End
  448. Else
  449. If (Cnt > 0) And
  450. (PPaiProp(p^.fileinfo.line)^.CanBeRemoved) Then
  451. Begin
  452. hp2 := p;
  453. Cnt2 := 1;
  454. While Cnt2 <= Cnt Do
  455. Begin
  456. If RegInInstruction(Tregister(Pai386(hp2)^.op2), p) Or
  457. RegInInstruction(Reg32(Tregister(Pai386(hp2)^.op2)), p)
  458. Then PPaiProp(p^.fileinfo.line)^.CanBeRemoved := False;
  459. Inc(Cnt2);
  460. GetNextInstruction(p, p);
  461. End;
  462. Continue;
  463. End;
  464. End;
  465. End;
  466. Top_Const:
  467. Begin
  468. Case Pai386(p)^.op2t Of
  469. Top_Reg:
  470. Begin
  471. If GetLastInstruction(p, hp1) Then
  472. With PPaiProp(hp1^.fileinfo.line)^.Regs[Reg32(TRegister(Pai386(p)^.op2))] Do
  473. If (Typ = Con_Const) And
  474. (StartMod = Pai386(p)^.op1) Then
  475. PPaiProp(p^.fileinfo.line)^.CanBeRemoved := True;
  476. End;
  477. { Top_Ref:;}
  478. End;
  479. End;
  480. End;
  481. End;
  482. A_STD: If GetLastInstruction(p, hp1) And
  483. (PPaiProp(hp1^.fileinfo.line)^.DirFlag = F_Set) Then
  484. PPaiProp(Pai(p)^.fileinfo.line)^.CanBeRemoved := True;
  485. A_XOR:
  486. Begin
  487. If (Pai386(p)^.op1t = top_reg) And
  488. (Pai386(p)^.op2t = top_reg) And
  489. (Pai386(p)^.op1 = Pai386(p)^.op2) And
  490. GetLastInstruction(p, hp1) And
  491. (PPaiProp(hp1^.fileinfo.line)^.Regs[Reg32(Tregister(Pai386(p)^.op1))].typ = con_const) And
  492. (PPaiProp(hp1^.fileinfo.line)^.Regs[Reg32(Tregister(Pai386(p)^.op1))].StartMod = Pointer(0))
  493. Then PPaiProp(p^.fileinfo.line)^.CanBeRemoved := True
  494. End
  495. End
  496. End;
  497. End;
  498. GetNextInstruction(p, p);
  499. End;
  500. End;
  501. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  502. {Removes the marked instructions and disposes the PPaiProps of the other
  503. instructions, restoring theirline number}
  504. Var p, hp1: Pai;
  505. {$IfDef TP}
  506. TmpLine: Longint;
  507. {$EndIf TP}
  508. InstrCnt: Longint;
  509. Begin
  510. p := First;
  511. If (p^.typ in (SkipInstr + ait_marker)) Then
  512. GetNextInstruction(p, p);
  513. InstrCnt := 1;
  514. While Assigned(p) Do
  515. Begin
  516. {$ifndef noinstremove}
  517. If PPaiProp(p^.fileinfo.line)^.CanBeRemoved
  518. Then
  519. Begin
  520. {$IfDef TP}
  521. Dispose(PPaiProp(p^.fileinfo.line));
  522. {$EndIf}
  523. GetNextInstruction(p, hp1);
  524. AsmL^.Remove(p);
  525. Dispose(p, Done);
  526. p := hp1;
  527. Inc(InstrCnt);
  528. End
  529. Else
  530. {$endif noinstremove}
  531. Begin
  532. {$IfDef TP}
  533. TmpLine := PPaiProp(p^.fileinfo.line)^.linesave;
  534. Dispose(PPaiProp(p^.fileinfo.line));
  535. p^.fileinfo.line := TmpLine;
  536. {$Else TP}
  537. p^.fileinfo.line := PPaiProp(p^.fileinfo.line)^.linesave;
  538. {$EndIf TP}
  539. GetNextInstruction(p, p);
  540. Inc(InstrCnt);
  541. End;
  542. End;
  543. {$IfNDef TP}
  544. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  545. {$EndIf TP}
  546. End;
  547. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  548. Begin
  549. DoCSE(AsmL, First, Last);
  550. RemoveInstructs(AsmL, First, Last);
  551. End;
  552. End.
  553. {
  554. $Log$
  555. Revision 1.9 1998-10-01 20:21:49 jonas
  556. * inter-register CSE, still requires some tweaks (peepholeoptpass2, better RegAlloc)
  557. Revision 1.8 1998/09/21 08:45:09 pierre
  558. + added vmt_offset in tobjectdef.write for fututre use
  559. (first steps to have objects without vmt if no virtual !!)
  560. + added fpu_used field for tabstractprocdef :
  561. sets this level to 2 if the functions return with value in FPU
  562. (is then set to correct value at parsing of implementation)
  563. THIS MIGHT refuse some code with FPU expression too complex
  564. that were accepted before and even in some cases
  565. that don't overflow in fact
  566. ( like if f : float; is a forward that finally in implementation
  567. only uses one fpu register !!)
  568. Nevertheless I think that it will improve security on
  569. FPU operations !!
  570. * most other changes only for UseBrowser code
  571. (added symtable references for record and objects)
  572. local switch for refs to args and local of each function
  573. (static symtable still missing)
  574. UseBrowser still not stable and probably broken by
  575. the definition hash array !!
  576. Revision 1.7 1998/09/20 17:12:35 jonas
  577. * small fix for uncertain optimizations & more cleaning up
  578. Revision 1.5 1998/09/16 17:59:59 jonas
  579. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  580. Revision 1.4 1998/08/06 19:40:27 jonas
  581. * removed $ before and after Log in comment
  582. Revision 1.3 1998/08/05 16:00:12 florian
  583. * some fixes for ansi strings
  584. * log to Log changed
  585. }