csopt386.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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,cpubase,cpuasm,DAOpt386;
  26. {
  27. Function PaiInSequence(P: Pai; Const Seq: TContent): Boolean;
  28. Var P1: Pai;
  29. Counter: Byte;
  30. TmpResult: Boolean;
  31. Begin
  32. TmpResult := False;
  33. P1 := Seq.StartMod;
  34. Counter := 1;
  35. While Not(TmpResult) And
  36. (Counter <= Seq.NrOfMods) Do
  37. Begin
  38. If (P = P1) Then TmpResult := True;
  39. Inc(Counter);
  40. p1 := Pai(p1^.Next);
  41. End;
  42. PaiInSequence := TmpResult;
  43. End;
  44. }
  45. Function CheckSequence(p: Pai; Reg: TRegister; Var Found: Longint; Var RegInfo: TRegInfo): Boolean;
  46. {checks whether the current instruction sequence (starting with p) and the
  47. one between StartMod and EndMod of Reg are the same. If so, the number of
  48. instructions that match is stored in Found and true is returned, otherwise
  49. Found holds the number of instructions between StartMod and EndMod and false
  50. is returned}
  51. Var hp2, hp3{, EndMod}: Pai;
  52. PrevNonRemovablePai: Pai;
  53. Cnt, OldNrOfMods: Longint;
  54. OrgRegInfo, HighRegInfo: TRegInfo;
  55. HighFound, OrgRegFound: Byte;
  56. RegCounter: TRegister;
  57. OrgRegResult: Boolean;
  58. TmpResult: Boolean;
  59. TmpState: Byte;
  60. Begin {CheckSequence}
  61. Reg := Reg32(Reg);
  62. TmpResult := False;
  63. FillChar(OrgRegInfo, SizeOf(OrgRegInfo), 0);
  64. OrgRegFound := 0;
  65. HighFound := 0;
  66. OrgRegResult := False;
  67. RegCounter := R_EAX;
  68. GetLastInstruction(p, PrevNonRemovablePai);
  69. While (RegCounter <= R_EDI) And
  70. (PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].Typ <> Con_Ref) Do
  71. Inc(RegCounter);
  72. While (RegCounter <= R_EDI) Do
  73. Begin
  74. FillChar(RegInfo, SizeOf(RegInfo), 0);
  75. RegInfo.NewRegsEncountered := [procinfo^.FramePointer, R_ESP];
  76. RegInfo.OldRegsEncountered := RegInfo.NewRegsEncountered;
  77. RegInfo.New2OldReg[procinfo^.FramePointer] := procinfo^.FramePointer;
  78. RegInfo.New2OldReg[R_ESP] := R_ESP;
  79. Found := 0;
  80. hp2 := PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].StartMod;
  81. If (PrevNonRemovablePai <> PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].StartMod)
  82. Then OldNrOfMods := PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].NrOfMods
  83. Else OldNrOfMods := 1;
  84. hp3 := p;
  85. While (Found <> OldNrOfMods) And
  86. { old new }
  87. InstructionsEquivalent(hp2, hp3, RegInfo) Do
  88. Begin
  89. GetNextInstruction(hp2, hp2);
  90. GetNextInstruction(hp3, hp3);
  91. Inc(Found)
  92. End;
  93. If (Found <> OldNrOfMods) Then
  94. Begin
  95. TmpResult := False;
  96. If (found > 0) then
  97. {this is correct because we only need to turn off the CanBeRemoved flag
  98. when an instruction has already been processed by CheckSequence
  99. (otherwise CanBeRemoved can't be true and thus can't have to be turned off).
  100. If it has already been processed by CheckSequence and flagged to be
  101. removed, it means that it has been checked against a previous sequence
  102. and that it was equal (otherwise CheckSequence would have returned false
  103. and the instruction wouldn't have been removed). If this "If found > 0"
  104. check is left out, incorrect optimizations are performed.}
  105. Found := PPaiProp(Pai(p)^.OptInfo)^.Regs[Reg].NrOfMods
  106. End
  107. Else TmpResult := True;
  108. If TmpResult And
  109. (Found > HighFound)
  110. Then
  111. Begin
  112. HighFound := Found;
  113. HighRegInfo := RegInfo;
  114. End;
  115. If (RegCounter = Reg) Then
  116. Begin
  117. OrgRegFound := Found;
  118. OrgRegResult := TmpResult;
  119. OrgRegInfo := RegInfo
  120. End;
  121. Repeat
  122. Inc(RegCounter);
  123. Until (RegCounter > R_EDI) or
  124. ((PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].Typ = Con_Ref) {And
  125. ((Regcounter = Reg) Or
  126. Not(PaiInSequence(p, PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter]))) }
  127. );
  128. End;
  129. If (HighFound > 0) And
  130. (Not(OrgRegResult) Or
  131. (HighFound > OrgRegFound))
  132. Then
  133. Begin
  134. {$ifndef fpc}
  135. TmpResult := True;
  136. {$else fpc}
  137. CheckSequence := True;
  138. {$endif fpc}
  139. RegInfo := HighRegInfo;
  140. Found := HighFound
  141. End
  142. Else
  143. Begin
  144. {$ifndef fpc}
  145. TmpResult := OrgRegResult;
  146. {$else fpc}
  147. CheckSequence := OrgRegResult;
  148. {$endif fpc}
  149. Found := OrgRegFound;
  150. RegInfo := OrgRegInfo;
  151. End;
  152. { sometimes, registers in RegsLoadedForRef (which normally aren't/shouldn't }
  153. { be used anymore after the sequence, are still used nevertheless (when }
  154. { range checking is on for instance, because this is not "normal" generated }
  155. { code, but more or less manually inserted) }
  156. {$ifndef fpc}
  157. If TmpResult Then
  158. {$else fpc}
  159. If CheckSequence And (Found > 0) Then
  160. {$endif fpc}
  161. For RegCounter := R_EAX to R_EDI Do
  162. If (RegCounter in RegInfo.RegsLoadedForRef) And
  163. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  164. Begin
  165. OldNrOfMods := PPaiProp(PrevNonRemovablePai^.OptInfo)^.
  166. Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
  167. hp2 := p;
  168. For Cnt := 1 to Pred(OldNrOfMods) Do
  169. GetNextInstruction(hp2, hp2);
  170. { hp2 now containts the last instruction of the sequence }
  171. { get the writestate at this point of the register in TmpState }
  172. TmpState := PPaiProp(hp2^.OptInfo)^.Regs[RegCounter].WState;
  173. { hp3 := first instruction after the sequence }
  174. GetNextInstruction(hp2, hp2);
  175. { now, even though reg is in RegsLoadedForRef, sometimes it's still used }
  176. { afterwards. It is not if either it is not in usedregs anymore after the }
  177. { sequence, or if it is loaded with a new value right after the sequence }
  178. If (TmpState = PPaiProp(hp2^.OptInfo)^.Regs[RegCounter].WState) And
  179. (RegCounter in PPaiProp(hp2^.OptInfo)^.UsedRegs) Then
  180. { it is still used, so remove it from RegsLoadedForRef }
  181. Begin
  182. {$ifdef regrefdebug}
  183. hp3 := new(pai_asm_comment,init(strpnew(att_reg2str[regcounter]+
  184. ' removed from regsloadedforref')));
  185. hp3^.fileinfo := hp2^.fileinfo;
  186. hp3^.next := hp2^.next;
  187. hp3^.previous := hp2;
  188. hp2^.next := hp3;
  189. If assigned(hp3^.next) then
  190. Pai(hp3^.next)^.previous := hp3;
  191. {$endif regrefdebug}
  192. Exclude(RegInfo.RegsLoadedForRef,RegCounter);
  193. End;
  194. End;
  195. {$ifndef fpc}
  196. CheckSequence := TmpResult;
  197. {$endif fpc}
  198. End; {CheckSequence}
  199. Procedure AllocRegBetween(AsmL: PAasmOutput; Reg: TRegister; p1, p2: Pai);
  200. { allocates register Reg between (and including) instructions p1 and p2 }
  201. { the type of p1 and p2 must not be in SkipInstr }
  202. var hp: pai;
  203. Begin
  204. If not(assigned(p1)) Then
  205. { this happens with registers which are loaded implicitely, outside the }
  206. { current block (e.g. esi with self) }
  207. exit;
  208. Repeat
  209. If Assigned(p1^.OptInfo) Then
  210. Include(PPaiProp(p1^.OptInfo)^.UsedRegs,Reg);
  211. p1 := Pai(p1^.next);
  212. Repeat
  213. While (p1^.typ in (SkipInstr-[ait_regalloc])) Do
  214. p1 := Pai(p1^.next);
  215. { remove all allocation/deallocation info about the register in between }
  216. If (p1^.typ = ait_regalloc) Then
  217. If (PaiRegAlloc(p1)^.Reg = Reg) Then
  218. Begin
  219. hp := Pai(p1^.Next);
  220. AsmL^.Remove(p1);
  221. Dispose(p1, Done);
  222. p1 := hp;
  223. End
  224. Else p1 := Pai(p1^.next);
  225. Until Not(p1^.typ in SkipInstr);
  226. Until p1 = p2;
  227. End;
  228. {$ifdef alignreg}
  229. Procedure SetAlignReg(p: Pai);
  230. var regsUsable: TRegSet;
  231. prevInstrCount, nextInstrCount: Longint;
  232. prevState, nextWState,nextRState: Array[R_EAX..R_EDI] of byte;
  233. regCounter, lastRemoved: TRegister;
  234. prev, next: Pai;
  235. {$ifdef alignregdebug}
  236. temp: Pai;
  237. {$endif alignregdebug}
  238. begin
  239. regsUsable := [R_EAX,R_ECX,R_EDX,R_EBX,{R_ESP,R_EBP,}R_ESI,R_EDI];
  240. for regCounter := R_EAX to R_EDI do
  241. begin
  242. prevState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  243. nextWState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  244. nextRState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].rState;
  245. end;
  246. getLastInstruction(p,prev);
  247. getNextInstruction(p,next);
  248. lastRemoved := pai_align(p)^.reg;
  249. nextInstrCount := 0;
  250. prevInstrCount := 0;
  251. while ((assigned(prev) and
  252. assigned(prev^.optInfo) and
  253. (prevInstrCount < 10)) or
  254. (assigned(next) and
  255. assigned(next^.optInfo) and
  256. (nextInstrCount < 10))) And
  257. (regsUsable <> []) Do
  258. begin
  259. if assigned(prev) and assigned(prev^.optinfo) and
  260. (prevInstrCount < 10) then
  261. begin
  262. if (prev^.typ = ait_instruction) And
  263. (insProp[PaiCpu(prev)^.opcode].ch[1] <> Ch_ALL) and
  264. (PaiCpu(prev)^.opcode <> a_jmp) then
  265. begin
  266. inc(prevInstrCount);
  267. for regCounter := R_EAX to R_EDI do
  268. begin
  269. if (regCounter in regsUsable) And
  270. (PPaiProp(prev^.optInfo)^.Regs[regCounter].wState <>
  271. prevState[regCounter]) then
  272. begin
  273. lastRemoved := regCounter;
  274. exclude(regsUsable,regCounter);
  275. {$ifdef alignregdebug}
  276. if regsUsable = [] then
  277. begin
  278. temp := new(pai_asm_comment,init(strpnew(
  279. 'regsUsable empty here')));
  280. temp^.next := prev^.next;
  281. temp^.previous := prev;
  282. prev^.next := temp;
  283. if assigned(temp^.next) then
  284. temp^.next^.previous := temp;
  285. end;
  286. {$endif alignregdebug}
  287. end;
  288. prevState[regCounter] :=
  289. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState;
  290. end
  291. end
  292. else
  293. for regCounter := R_EAX to R_EDI do
  294. prevState[regCounter] :=
  295. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState;
  296. getLastInstruction(prev,prev);
  297. end;
  298. if assigned(next) and assigned(next^.optInfo) and
  299. (nextInstrCount < 10) then
  300. begin
  301. if (next^.typ = ait_instruction) and
  302. (insProp[PaiCpu(next)^.opcode].ch[1] <> Ch_ALL) and
  303. (PaiCpu(next)^.opcode <> a_jmp) then
  304. begin
  305. inc(nextInstrCount);
  306. for regCounter := R_EAX to R_EDI do
  307. begin
  308. if (regCounter in regsUsable) And
  309. ((PPaiProp(next^.optInfo)^.Regs[regCounter].wState <>
  310. nextWState[regCounter]) or
  311. (PPaiProp(next^.optInfo)^.Regs[regCounter].rState <>
  312. nextRState[regCounter])) Then
  313. begin
  314. lastRemoved := regCounter;
  315. exclude(regsUsable,regCounter);
  316. {$ifdef alignregdebug}
  317. if regsUsable = [] then
  318. begin
  319. temp := new(pai_asm_comment,init(strpnew(
  320. 'regsUsable empty here')));
  321. temp^.next := next^.next;
  322. temp^.previous := next;
  323. next^.next := temp;
  324. if assigned(temp^.next) then
  325. temp^.next^.previous := temp;
  326. end;
  327. {$endif alignregdebug}
  328. end;
  329. nextWState[regCounter] :=
  330. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  331. nextRState[regCounter] :=
  332. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  333. end
  334. end
  335. else
  336. for regCounter := R_EAX to R_EDI do
  337. begin
  338. nextWState[regCounter] :=
  339. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  340. nextRState[regCounter] :=
  341. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  342. end;
  343. getNextInstruction(next,next);
  344. end;
  345. end;
  346. if regsUsable <> [] then
  347. for regCounter := R_EAX to R_EDI do
  348. if regCounter in regsUsable then
  349. begin
  350. {$ifdef alignregdebug}
  351. next := new(pai_asm_comment,init(strpnew('regsusable not empty')));
  352. next^.next := p^.next;
  353. next^.previous := p;
  354. p^.next := next;
  355. if assigned(next^.next) then
  356. next^.next^.previous := next;
  357. {$endif alignregdebug}
  358. lastRemoved := regCounter;
  359. break
  360. end;
  361. {$ifdef alignregdebug}
  362. next := new(pai_asm_comment,init(strpnew(att_reg2str[lastRemoved]+
  363. ' chosen as alignment register')));
  364. next^.next := p^.next;
  365. next^.previous := p;
  366. p^.next := next;
  367. if assigned(next^.next) then
  368. next^.next^.previous := next;
  369. {$endif alignregdebug}
  370. pai_align(p)^.reg := lastRemoved;
  371. End;
  372. {$endif alignreg}
  373. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  374. {marks the instructions that can be removed by RemoveInstructs. They're not
  375. removed immediately because sometimes an instruction needs to be checked in
  376. two different sequences}
  377. Var Cnt, Cnt2: Longint;
  378. p, hp1, hp2: Pai;
  379. hp3, hp4: Pai;
  380. {$ifdef csdebug}
  381. hp5: pai;
  382. {$endif csdebug}
  383. RegInfo: TRegInfo;
  384. RegCounter: TRegister;
  385. TmpState: Byte;
  386. Begin
  387. p := First;
  388. SkipHead(p);
  389. First := p;
  390. While (p <> Last) Do
  391. Begin
  392. Case p^.typ Of
  393. {$ifdef alignreg}
  394. ait_align:
  395. SetAlignReg(p);
  396. {$endif alignreg}
  397. ait_instruction:
  398. Begin
  399. Case Paicpu(p)^.opcode Of
  400. A_CLD: If GetLastInstruction(p, hp1) And
  401. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  402. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  403. A_MOV, A_MOVZX, A_MOVSX:
  404. Begin
  405. Case Paicpu(p)^.oper[0].typ Of
  406. Top_Ref:
  407. Begin {destination is always a register in this case}
  408. With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  409. Begin
  410. If (p = StartMod) And
  411. GetLastInstruction (p, hp1) And
  412. (hp1^.typ <> ait_marker)
  413. Then
  414. {so we don't try to check a sequence when p is the first instruction of the block}
  415. If CheckSequence(p, Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
  416. (Cnt > 0) Then
  417. Begin
  418. hp1 := nil;
  419. { although it's perfectly ok to remove an instruction which doesn't contain }
  420. { the register that we've just checked (CheckSequence takes care of that), }
  421. { the sequence containing this other register should also be completely }
  422. { checked and removed, otherwise we may get situations like this: }
  423. { }
  424. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  425. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  426. { movl 8(%edx), %edx movl 8(%edx), %edx }
  427. { movl (%eax), eax movl (%eax), eax }
  428. { cmpl %eax, %edx cmpl %eax, %edx }
  429. { jnz l123 getting converted to jnz l123 }
  430. { movl 12(%ebp), %edx movl 4(%eax), eax }
  431. { movl 16(%ebp), %eax }
  432. { movl 8(%edx), %edx }
  433. { movl 4(%eax), eax }
  434. hp2 := p;
  435. Cnt2 := 1;
  436. While Cnt2 <= Cnt Do
  437. Begin
  438. If (hp1 = nil) And
  439. Not(RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  440. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p)) And
  441. Not((p^.typ = ait_instruction) And
  442. (paicpu(p)^.OpCode = A_MOV) And
  443. (paicpu(p)^.Oper[0].typ = top_ref) And
  444. (PPaiProp(p^.OptInfo)^.Regs[Reg32(paicpu(p)^.Oper[1].reg)].NrOfMods
  445. <= (Cnt - Cnt2 + 1)))
  446. Then hp1 := p;
  447. {$ifndef noremove}
  448. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  449. {$endif noremove}
  450. Inc(Cnt2);
  451. GetNextInstruction(p, p);
  452. End;
  453. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  454. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  455. {hp4 is used to get the contents of the registers before the sequence}
  456. GetLastInstruction(hp2, hp4);
  457. {$IfDef CSDebug}
  458. For RegCounter := R_EAX To R_EDI Do
  459. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  460. Begin
  461. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  462. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  463. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  464. End;
  465. {$EndIf CSDebug}
  466. { If some registers were different in the old and the new sequence, move }
  467. { the contents of those old registers to the new ones }
  468. For RegCounter := R_EAX To R_EDI Do
  469. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  470. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  471. Begin
  472. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  473. PPaiProp(hp4^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,hp2);
  474. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  475. {old reg new reg}
  476. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  477. Begin
  478. hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
  479. {old reg new reg}
  480. RegInfo.New2OldReg[RegCounter], RegCounter));
  481. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
  482. End
  483. Else
  484. { imagine the following code: }
  485. { normal wrong optimized }
  486. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  487. { movl (%eax), %eax movl (%eax), %eax }
  488. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  489. { jne l1 jne l1 }
  490. { movl 8(%ebp), %eax }
  491. { movl (%eax), %edi movl %eax, %edi }
  492. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  493. { movl 8(%ebp), %eax }
  494. { pushl 70(%eax) pushl 70(%eax) }
  495. { }
  496. { The error is that at the moment that the last instruction is executed, }
  497. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  498. { registers that are completely removed from a sequence (= registers in }
  499. { RegLoadedForRef, have to be changed to their contents from before the }
  500. { sequence. }
  501. If RegCounter in RegInfo.RegsLoadedForRef Then
  502. Begin
  503. {load Cnt2 with the total number of instructions of this sequence}
  504. Cnt2 := PPaiProp(hp4^.OptInfo)^.
  505. Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
  506. hp3 := hp2;
  507. For Cnt := 1 to Pred(Cnt2) Do
  508. GetNextInstruction(hp3, hp3);
  509. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  510. GetNextInstruction(hp3, hp3);
  511. {$ifdef csdebug}
  512. Writeln('Cnt2: ',Cnt2);
  513. hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
  514. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  515. {$endif csdebug}
  516. hp3 := hp2;
  517. {first change the contents of the register inside the sequence}
  518. For Cnt := 1 to Cnt2 Do
  519. Begin
  520. {save the WState of the last pai object of the sequence for later use}
  521. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  522. {$ifdef csdebug}
  523. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '
  524. +tostr(tmpstate))));
  525. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  526. {$endif csdebug}
  527. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  528. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  529. GetNextInstruction(hp3, hp3);
  530. End;
  531. {here, hp3 = p = Pai object right after the sequence, TmpState = WState of
  532. RegCounter at the last Pai object of the sequence}
  533. GetLastInstruction(hp3, hp3);
  534. While GetNextInstruction(hp3, hp3) And
  535. (PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState
  536. = TmpState) Do
  537. {$ifdef csdebug}
  538. begin
  539. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '+
  540. tostr(PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState))));
  541. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  542. {$endif csdebug}
  543. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  544. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  545. {$ifdef csdebug}
  546. end;
  547. {$endif csdebug}
  548. {$ifdef csdebug}
  549. hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  550. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  551. {$endif csdebug}
  552. End;
  553. End;
  554. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  555. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  556. If hp1 <> nil Then p := hp1;
  557. Continue;
  558. End
  559. Else
  560. If (Cnt > 0) And
  561. (PPaiProp(p^.OptInfo)^.
  562. Regs[Reg32(Paicpu(p)^.oper[1].reg)].Typ = Con_Ref) And
  563. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  564. Begin
  565. hp2 := p;
  566. Cnt2 := 1;
  567. While Cnt2 <= Cnt Do
  568. Begin
  569. If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  570. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p) Then
  571. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  572. Inc(Cnt2);
  573. GetNextInstruction(p, p);
  574. End;
  575. Continue;
  576. End;
  577. End;
  578. End;
  579. Top_Const:
  580. Begin
  581. Case Paicpu(p)^.oper[1].typ Of
  582. Top_Reg:
  583. Begin
  584. If GetLastInstruction(p, hp1) Then
  585. With PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  586. If (Typ = Con_Const) And
  587. (StartMod = p) Then
  588. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  589. End;
  590. { Top_Ref:;}
  591. End;
  592. End;
  593. End;
  594. End;
  595. A_STD: If GetLastInstruction(p, hp1) And
  596. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  597. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  598. A_XOR:
  599. Begin
  600. If (Paicpu(p)^.oper[0].typ = top_reg) And
  601. (Paicpu(p)^.oper[0].typ = top_reg) And
  602. (Paicpu(p)^.oper[1].reg = Paicpu(p)^.oper[1].reg) And
  603. GetLastInstruction(p, hp1) And
  604. (PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)].typ = con_const) And
  605. (PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)].StartMod = nil)
  606. Then PPaiProp(p^.OptInfo)^.CanBeRemoved := True
  607. End
  608. End
  609. End;
  610. End;
  611. GetNextInstruction(p, p);
  612. End;
  613. End;
  614. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  615. {Removes the marked instructions and disposes the PPaiProps of the other
  616. instructions, restoring their line number}
  617. Var p, hp1: Pai;
  618. {$IfDef TP}
  619. TmpLine: Longint;
  620. {$EndIf TP}
  621. InstrCnt: Longint;
  622. Begin
  623. p := First;
  624. SkipHead(P);
  625. InstrCnt := 1;
  626. While (p <> Last) Do
  627. Begin
  628. {$ifndef noinstremove}
  629. If PPaiProp(p^.OptInfo)^.CanBeRemoved
  630. Then
  631. Begin
  632. {$IfDef TP}
  633. Dispose(PPaiProp(p^.OptInfo));
  634. {$EndIf}
  635. GetNextInstruction(p, hp1);
  636. AsmL^.Remove(p);
  637. Dispose(p, Done);
  638. p := hp1;
  639. Inc(InstrCnt);
  640. End
  641. Else
  642. {$endif noinstremove}
  643. Begin
  644. {$IfDef TP}
  645. Dispose(PPaiProp(p^.OptInfo));
  646. {$EndIf TP}
  647. p^.OptInfo := nil;
  648. GetNextInstruction(p, p);
  649. Inc(InstrCnt);
  650. End;
  651. End;
  652. {$IfNDef TP}
  653. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  654. {$EndIf TP}
  655. End;
  656. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  657. Begin
  658. DoCSE(AsmL, First, Last);
  659. RemoveInstructs(AsmL, First, Last);
  660. End;
  661. End.
  662. {
  663. $Log$
  664. Revision 1.30 1999-11-06 14:34:20 peter
  665. * truncated log to 20 revs
  666. Revision 1.29 1999/11/05 16:01:46 jonas
  667. + first implementation of choosing least used register for alignment code
  668. (not yet working, between ifdef alignreg)
  669. Revision 1.28 1999/10/11 11:11:31 jonas
  670. * fixed bug which sometimes caused a crash when optimizing blocks of code with
  671. assembler blocks (didn't notice before because of lack of zero page protection
  672. under Win9x :( )
  673. Revision 1.27 1999/10/01 13:51:40 jonas
  674. * CSE now updates the RegAlloc's
  675. Revision 1.26 1999/09/30 14:43:13 jonas
  676. * fixed small efficiency which caused some missed optimizations (saves 1
  677. assembler instruction on the whole compiler/RTL source tree! :)
  678. Revision 1.25 1999/09/27 23:44:50 peter
  679. * procinfo is now a pointer
  680. * support for result setting in sub procedure
  681. Revision 1.24 1999/08/25 11:59:58 jonas
  682. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  683. Revision 1.23 1999/08/04 00:22:58 florian
  684. * renamed i386asm and i386base to cpuasm and cpubase
  685. Revision 1.22 1999/06/03 15:45:08 jonas
  686. * sequences are now checked only once (previously, some long ones were
  687. checked once completely and then several times partially)
  688. Revision 1.21 1999/05/08 20:38:03 jonas
  689. * seperate OPTimizer INFO pointer field in tai object
  690. Revision 1.20 1999/05/01 13:24:19 peter
  691. * merged nasm compiler
  692. * old asm moved to oldasm/
  693. Revision 1.2 1999/03/29 16:05:45 peter
  694. * optimizer working for ag386bin
  695. Revision 1.1 1999/03/26 00:01:09 peter
  696. * first things for optimizer (compiles but cycle crashes)
  697. Revision 1.19 1999/02/26 00:48:17 peter
  698. * assembler writers fixed for ag386bin
  699. Revision 1.18 1998/12/29 18:48:22 jonas
  700. + optimize pascal code surrounding assembler blocks
  701. Revision 1.17 1998/12/17 16:37:39 jonas
  702. + extra checks in RegsEquivalent so some more optimizations can be done (which
  703. where disabled by the second fix from revision 1.22)
  704. Revision 1.16 1998/12/02 16:23:31 jonas
  705. * changed "if longintvar in set" to case or "if () or () .." statements
  706. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  707. Revision 1.15 1998/11/24 19:47:24 jonas
  708. * fixed problems posiible with 3 operand instructions
  709. Revision 1.14 1998/11/09 19:40:48 jonas
  710. * fixed comments from last commit (apparently there's still a 255 char limit :( )
  711. Revision 1.13 1998/11/09 19:33:39 jonas
  712. * changed specific bugfix (which was actually wrong implemented, but
  713. did the right thing in most cases nevertheless) to general bugfix
  714. * fixed bug that caused
  715. mov (ebp), edx mov (ebp), edx
  716. mov (edx), edx mov (edx), edx
  717. ... being changed to ...
  718. mov (ebp), edx mov edx, eax
  719. mov (eax), eax
  720. but this disabled another small correct optimization...
  721. Revision 1.12 1998/10/20 09:32:54 peter
  722. * removed some unused vars
  723. }