csopt386.pas 27 KB

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