csopt386.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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^.OptInfo)^.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^.OptInfo)^.Regs[RegCounter].StartMod;
  82. If (PrevNonRemovablePai <> PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].StartMod)
  83. Then OldNrOfMods := PPaiProp(PrevNonRemovablePai^.OptInfo)^.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)^.OptInfo)^.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)^.OptInfo)^.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)^.OptInfo)^.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^.OptInfo)^.Regs[RegCounter].Typ = Con_Ref) {And
  194. ((Regcounter = Reg) Or
  195. Not(PaiInSequence(p, PPaiProp(PrevNonRemovablePai^.OptInfo)^.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^.OptInfo)^.DirFlag = F_NotSet) Then
  239. PPaiProp(Pai(p)^.OptInfo)^.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^.OptInfo)^.Regs[Reg32(Pai386(p)^.oper[1].reg)] Do
  251. Begin
  252. If (p = StartMod) And
  253. GetLastInstruction (p, hp1) And
  254. (hp1^.typ <> ait_marker)
  255. Then
  256. {so we don't try to check a sequence when p is the first instruction of the block}
  257. If CheckSequence(p, Pai386(p)^.oper[1].reg, Cnt, RegInfo) And
  258. (Cnt > 0)
  259. Then
  260. Begin
  261. hp1 := nil;
  262. {although it's perfectly ok to remove an instruction which doesn't contain
  263. the register that we've just checked (CheckSequence takes care of that),
  264. the sequence containing this other register should also be completely
  265. checked and removed, otherwise we may get situations like this:
  266. movl 12(%ebp), %edx movl 12(%ebp), %edx
  267. movl 16(%ebp), %eax movl 16(%ebp), %eax
  268. movl 8(%edx), %edx movl 8(%edx), %edx
  269. movl (%eax), eax movl (%eax), eax
  270. cmpl %eax, %edx cmpl %eax, %edx
  271. jnz l123 getting converted to jnz l123
  272. movl 12(%ebp), %edx movl 4(%eax), eax
  273. movl 16(%ebp), %eax
  274. movl 8(%edx), %edx
  275. movl 4(%eax), eax}
  276. hp2 := p;
  277. Cnt2 := 1;
  278. While Cnt2 <= Cnt Do
  279. Begin
  280. If (hp1 = nil) And
  281. Not(RegInInstruction(Pai386(hp2)^.oper[1].reg, p) Or
  282. RegInInstruction(Reg32(Pai386(hp2)^.oper[1].reg), p)) And
  283. Not((p^.typ = ait_instruction) And
  284. (pai386(p)^.OpCode = A_MOV) And
  285. (pai386(p)^.Oper[0].typ = top_ref) And
  286. (PPaiProp(p^.OptInfo)^.Regs[Reg32(pai386(p)^.Oper[1].reg)].NrOfMods
  287. <= (Cnt - Cnt2 + 1)))
  288. Then hp1 := p;
  289. {$ifndef noremove}
  290. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  291. {$endif noremove}
  292. Inc(Cnt2);
  293. GetNextInstruction(p, p);
  294. End;
  295. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  296. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  297. {imagine the following code:
  298. normal wrong optimized
  299. movl 8(%ebp), %eax movl 8(%ebp), %eax
  300. movl (%eax), %eax movl (%eax), %eax
  301. cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax
  302. jne l1 jne l1
  303. movl 8(%ebp), %eax
  304. movl (%eax), %edi movl %eax, %edi
  305. movl %edi, -4(%ebp) movl %edi, -4(%ebp)
  306. movl 8(%ebp), %eax
  307. pushl 70(%eax) pushl 70(%eax)
  308. The error is that at the moment that the last instruction is executed,
  309. %eax doesn't contain 8(%ebp) anymore. Solution: the contents of registers
  310. that are completely removed from a sequence, have to be changed to their
  311. contents from before the sequence.}
  312. {hp4 is used to get the contents of the registers before the sequence}
  313. GetLastInstruction(hp2, hp4);
  314. {If some registers were different in the old and the new sequence, move
  315. the contents of those old registers to the new ones}
  316. {$IfDef CSDebug}
  317. For RegCounter := R_EAX To R_EDI Do
  318. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  319. Begin
  320. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  321. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  322. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  323. End;
  324. {$EndIf CSDebug}
  325. For RegCounter := R_EAX To R_EDI Do
  326. Begin
  327. If (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  328. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  329. {old reg new reg}
  330. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  331. Begin
  332. hp3 := New(Pai386,Op_Reg_Reg(A_MOV, S_L,
  333. {old reg new reg}
  334. RegInfo.New2OldReg[RegCounter], RegCounter));
  335. hp3^.fileinfo := hp2^.fileinfo;
  336. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
  337. End
  338. Else
  339. If (RegCounter In RegInfo.RegsLoadedForRef) Then
  340. {change the contents of this register to the its contents before the
  341. sequence (for all instructions in and after the sequence, until the register
  342. is reloaded)}
  343. Begin
  344. {load Cnt2 with the total number of instructions of this sequence}
  345. Cnt2 := PPaiProp(hp4^.OptInfo)^.
  346. Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
  347. {sometimes, a register can not be removed from a sequence, because it's
  348. still used afterwards:
  349. movl -8(%ebp), %eax movl -8(%ebp), %eax
  350. movl 70(%eax), %eax movl 70(%eax), %eax
  351. cmpl 74(%eax), %eax cmpl 74(%eax), %eax
  352. jne l1 can't be changed to jne l1
  353. movl -8(%ebp), %eax
  354. movl 70(%eax), %edi movl %eax, %edi
  355. boundl R_282, %edi boundl R_282, %edi
  356. pushl 70(%eax) pushl 70(%eax)
  357. because eax now contains the wrong value when 70(%eax) is pushed}
  358. hp3 := hp2;
  359. For Cnt := 1 to Pred(Cnt2) Do
  360. GetNextInstruction(hp3, hp3);
  361. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  362. GetNextInstruction(hp3, hp3);
  363. If (TmpState <> PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState) Or
  364. Not(RegCounter in PPaiProp(hp3^.OptInfo)^.UsedRegs) Then
  365. Begin
  366. {$ifdef csdebug}
  367. Writeln('Cnt2: ',Cnt2);
  368. hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
  369. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  370. {$endif csdebug}
  371. hp3 := hp2;
  372. {first change the contents of the register inside the sequence}
  373. For Cnt := 1 to Cnt2 Do
  374. Begin
  375. {save the WState of the last pai object of the sequence for later use}
  376. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  377. {$ifdef csdebug}
  378. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '
  379. +tostr(tmpstate))));
  380. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  381. {$endif csdebug}
  382. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  383. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  384. GetNextInstruction(hp3, hp3);
  385. End;
  386. {here, hp3 = p = Pai object right after the sequence, TmpState = WState of
  387. RegCounter at the last Pai object of the sequence}
  388. GetLastInstruction(hp3, hp3);
  389. While GetNextInstruction(hp3, hp3) And
  390. (PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState
  391. = TmpState) Do
  392. {$ifdef csdebug}
  393. begin
  394. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '+
  395. tostr(PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState))));
  396. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  397. {$endif csdebug}
  398. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  399. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  400. {$ifdef csdebug}
  401. end;
  402. {$endif csdebug}
  403. End
  404. Else
  405. Begin
  406. {$ifdef csdebug}
  407. Writeln('Got there for ',att_Reg2Str[RegCounter]);
  408. {$endif csdebug}
  409. hp3 := hp2;
  410. For Cnt := 1 to Cnt2 Do
  411. Begin
  412. If RegModifiedByInstruction(RegCounter, hp3)
  413. Then PPaiProp(hp3^.OptInfo)^.CanBeRemoved := False;
  414. GetNextInstruction(hp3, hp3);
  415. End;
  416. End;
  417. {$ifdef csdebug}
  418. hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  419. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  420. {$endif csdebug}
  421. End;
  422. End;
  423. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  424. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  425. If hp1 <> nil Then p := hp1;
  426. Continue;
  427. End
  428. Else
  429. If (Cnt > 0) And
  430. (PPaiProp(p^.OptInfo)^.
  431. Regs[Reg32(Pai386(p)^.oper[1].reg)].Typ = Con_Ref) And
  432. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  433. Begin
  434. hp2 := p;
  435. Cnt2 := 1;
  436. While Cnt2 <= Cnt Do
  437. Begin
  438. If RegInInstruction(Pai386(hp2)^.oper[1].reg, p) Or
  439. RegInInstruction(Reg32(Pai386(hp2)^.oper[1].reg), p) Then
  440. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  441. Inc(Cnt2);
  442. GetNextInstruction(p, p);
  443. End;
  444. Continue;
  445. End;
  446. End;
  447. End;
  448. Top_Const:
  449. Begin
  450. Case Pai386(p)^.oper[1].typ Of
  451. Top_Reg:
  452. Begin
  453. If GetLastInstruction(p, hp1) Then
  454. With PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Pai386(p)^.oper[1].reg)] Do
  455. If (Typ = Con_Const) And
  456. (StartMod = p) Then
  457. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  458. End;
  459. { Top_Ref:;}
  460. End;
  461. End;
  462. End;
  463. End;
  464. A_STD: If GetLastInstruction(p, hp1) And
  465. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  466. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  467. A_XOR:
  468. Begin
  469. If (Pai386(p)^.oper[0].typ = top_reg) And
  470. (Pai386(p)^.oper[0].typ = top_reg) And
  471. (Pai386(p)^.oper[1].reg = Pai386(p)^.oper[1].reg) And
  472. GetLastInstruction(p, hp1) And
  473. (PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Pai386(p)^.oper[1].reg)].typ = con_const) And
  474. (PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Pai386(p)^.oper[1].reg)].StartMod = nil)
  475. Then PPaiProp(p^.OptInfo)^.CanBeRemoved := True
  476. End
  477. End
  478. End;
  479. End;
  480. GetNextInstruction(p, p);
  481. End;
  482. End;
  483. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  484. {Removes the marked instructions and disposes the PPaiProps of the other
  485. instructions, restoring their line number}
  486. Var p, hp1: Pai;
  487. {$IfDef TP}
  488. TmpLine: Longint;
  489. {$EndIf TP}
  490. InstrCnt: Longint;
  491. Begin
  492. p := First;
  493. SkipHead(P);
  494. InstrCnt := 1;
  495. While (p <> Last) Do
  496. Begin
  497. {$ifndef noinstremove}
  498. If PPaiProp(p^.OptInfo)^.CanBeRemoved
  499. Then
  500. Begin
  501. {$IfDef TP}
  502. Dispose(PPaiProp(p^.OptInfo));
  503. {$EndIf}
  504. GetNextInstruction(p, hp1);
  505. AsmL^.Remove(p);
  506. Dispose(p, Done);
  507. p := hp1;
  508. Inc(InstrCnt);
  509. End
  510. Else
  511. {$endif noinstremove}
  512. Begin
  513. {$IfDef TP}
  514. Dispose(PPaiProp(p^.OptInfo));
  515. {$EndIf TP}
  516. p^.OptInfo := nil;
  517. GetNextInstruction(p, p);
  518. Inc(InstrCnt);
  519. End;
  520. End;
  521. {$IfNDef TP}
  522. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  523. {$EndIf TP}
  524. End;
  525. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  526. Begin
  527. DoCSE(AsmL, First, Last);
  528. RemoveInstructs(AsmL, First, Last);
  529. End;
  530. End.
  531. {
  532. $Log$
  533. Revision 1.22 1999-06-03 15:45:08 jonas
  534. * sequences are now checked only once (previously, some long ones were
  535. checked once completely and then several times partially)
  536. Revision 1.21 1999/05/08 20:38:03 jonas
  537. * seperate OPTimizer INFO pointer field in tai object
  538. Revision 1.20 1999/05/01 13:24:19 peter
  539. * merged nasm compiler
  540. * old asm moved to oldasm/
  541. Revision 1.2 1999/03/29 16:05:45 peter
  542. * optimizer working for ag386bin
  543. Revision 1.1 1999/03/26 00:01:09 peter
  544. * first things for optimizer (compiles but cycle crashes)
  545. Revision 1.19 1999/02/26 00:48:17 peter
  546. * assembler writers fixed for ag386bin
  547. Revision 1.18 1998/12/29 18:48:22 jonas
  548. + optimize pascal code surrounding assembler blocks
  549. Revision 1.17 1998/12/17 16:37:39 jonas
  550. + extra checks in RegsEquivalent so some more optimizations can be done (which
  551. where disabled by the second fix from revision 1.22)
  552. Revision 1.16 1998/12/02 16:23:31 jonas
  553. * changed "if longintvar in set" to case or "if () or () .." statements
  554. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  555. Revision 1.15 1998/11/24 19:47:24 jonas
  556. * fixed problems posiible with 3 operand instructions
  557. Revision 1.14 1998/11/09 19:40:48 jonas
  558. * fixed comments from last commit (apparently there's still a 255 char limit :( )
  559. Revision 1.13 1998/11/09 19:33:39 jonas
  560. * changed specific bugfix (which was actually wrong implemented, but
  561. did the right thing in most cases nevertheless) to general bugfix
  562. * fixed bug that caused
  563. mov (ebp), edx mov (ebp), edx
  564. mov (edx), edx mov (edx), edx
  565. ... being changed to ...
  566. mov (ebp), edx mov edx, eax
  567. mov (eax), eax
  568. but this disabled another small correct optimization...
  569. Revision 1.12 1998/10/20 09:32:54 peter
  570. * removed some unused vars
  571. Revision 1.11 1998/10/07 16:24:52 jonas
  572. * changed state to WState (WriteState), added RState for future use in
  573. instruction scheduling
  574. Revision 1.10 1998/10/02 17:29:23 jonas
  575. * much better interregister CSE
  576. Revision 1.9 1998/10/01 20:21:49 jonas
  577. * inter-register CSE, still requires some tweaks (peepholeoptpass2, better RegAlloc)
  578. Revision 1.8 1998/09/21 08:45:09 pierre
  579. + added vmt_offset in tobjectdef.write for fututre use
  580. (first steps to have objects without vmt if no virtual !!)
  581. + added fpu_used field for tabstractprocdef :
  582. sets this level to 2 if the functions return with value in FPU
  583. (is then set to correct value at parsing of implementation)
  584. THIS MIGHT refuse some code with FPU expression too complex
  585. that were accepted before and even in some cases
  586. that don't overflow in fact
  587. ( like if f : float; is a forward that finally in implementation
  588. only uses one fpu register !!)
  589. Nevertheless I think that it will improve security on
  590. FPU operations !!
  591. * most other changes only for UseBrowser code
  592. (added symtable references for record and objects)
  593. local switch for refs to args and local of each function
  594. (static symtable still missing)
  595. UseBrowser still not stable and probably broken by
  596. the definition hash array !!
  597. Revision 1.7 1998/09/20 17:12:35 jonas
  598. * small fix for uncertain optimizations & more cleaning up
  599. Revision 1.5 1998/09/16 17:59:59 jonas
  600. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  601. Revision 1.4 1998/08/06 19:40:27 jonas
  602. * removed $ before and after Log in comment
  603. Revision 1.3 1998/08/05 16:00:12 florian
  604. * some fixes for ansi strings
  605. * log to Log changed
  606. }