csopt386.pas 27 KB

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