csopt386.pas 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Jonas Maebe, member of the Free Pascal
  4. development team
  5. This unit contains the common subexpression elimination procedure.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. Unit CSOpt386;
  20. Interface
  21. Uses aasm;
  22. {Procedure CSOpt386(First, Last: Pai);}
  23. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  24. Implementation
  25. Uses
  26. CObjects, verbose, hcodegen, globals,cpubase,cpuasm,DAOpt386, tgeni386;
  27. {
  28. Function PaiInSequence(P: Pai; Const Seq: TContent): Boolean;
  29. Var P1: Pai;
  30. Counter: Byte;
  31. TmpResult: Boolean;
  32. Begin
  33. TmpResult := False;
  34. P1 := Seq.StartMod;
  35. Counter := 1;
  36. While Not(TmpResult) And
  37. (Counter <= Seq.NrOfMods) Do
  38. Begin
  39. If (P = P1) Then TmpResult := True;
  40. Inc(Counter);
  41. p1 := Pai(p1^.Next);
  42. End;
  43. PaiInSequence := TmpResult;
  44. End;
  45. }
  46. Function CheckSequence(p: Pai; Reg: TRegister; Var Found: Longint; Var RegInfo: TRegInfo): Boolean;
  47. {checks whether the current instruction sequence (starting with p) and the
  48. one between StartMod and EndMod of Reg are the same. If so, the number of
  49. instructions that match is stored in Found and true is returned, otherwise
  50. Found holds the number of instructions between StartMod and EndMod and false
  51. is returned}
  52. Var hp2, hp3{, EndMod}: Pai;
  53. PrevNonRemovablePai: Pai;
  54. {Cnt,} OldNrOfMods: Longint;
  55. OrgRegInfo, HighRegInfo: TRegInfo;
  56. HighFound, OrgRegFound: Byte;
  57. RegCounter, regCounter2: TRegister;
  58. OrgRegResult: Boolean;
  59. TmpResult: Boolean;
  60. {TmpState: 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. not(ppaiprop(prevNonRemovablePai^.optInfo)^.regs[regCounter].typ in
  72. [con_ref,con_noRemoveRef]) 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. Found := 0;
  82. hp2 := PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].StartMod;
  83. If (PrevNonRemovablePai <> PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].StartMod)
  84. Then OldNrOfMods := PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].NrOfMods
  85. Else OldNrOfMods := 1;
  86. hp3 := p;
  87. While (Found <> OldNrOfMods) And
  88. { old new }
  89. InstructionsEquivalent(hp2, hp3, RegInfo) Do
  90. Begin
  91. if (hp3^.typ = ait_instruction) and
  92. ((paicpu(hp3)^.opcode = A_MOV) or
  93. (paicpu(hp3)^.opcode = A_MOVZX) or
  94. (paicpu(hp3)^.opcode = A_MOVSX)) and
  95. (paicpu(hp3)^.oper[0].typ in
  96. [top_const,top_ref,top_symbol]) and
  97. (paicpu(hp3)^.oper[1].typ = top_reg) then
  98. regInfo.lastReload
  99. [reg32(paicpu(hp3)^.oper[1].reg)] := hp3;
  100. GetNextInstruction(hp2, hp2);
  101. GetNextInstruction(hp3, hp3);
  102. Inc(Found)
  103. End;
  104. for regCounter2 := R_EAX to R_EDX do
  105. if (regInfo.new2OldReg[regCounter2] <> R_NO) and
  106. (reg in PPaiProp(hp3^.optInfo)^.usedRegs) and
  107. not regLoadedWithNewValue(reg,false,hp3) then
  108. include(regInfo.regsStillUsedAfterSeq,regCounter);
  109. If (Found <> OldNrOfMods) or
  110. { the following is to avoid problems with rangecheck code (see testcse2) }
  111. (assigned(hp3) and
  112. ((reg in regInfo.regsLoadedForRef) and
  113. (reg in PPaiProp(hp3^.optInfo)^.usedRegs) and
  114. not regLoadedWithNewValue(reg,false,hp3))) then
  115. Begin
  116. TmpResult := False;
  117. If (found > 0) then
  118. {this is correct because we only need to turn off the CanBeRemoved flag
  119. when an instruction has already been processed by CheckSequence
  120. (otherwise CanBeRemoved can't be true and thus can't have to be turned off).
  121. If it has already been processed by CheckSequence and flagged to be
  122. removed, it means that it has been checked against a previous sequence
  123. and that it was equal (otherwise CheckSequence would have returned false
  124. and the instruction wouldn't have been removed). If this "If found > 0"
  125. check is left out, incorrect optimizations are performed.}
  126. Found := PPaiProp(Pai(p)^.OptInfo)^.Regs[Reg].NrOfMods
  127. End
  128. Else TmpResult := True;
  129. If TmpResult And
  130. (Found > HighFound)
  131. Then
  132. Begin
  133. HighFound := Found;
  134. HighRegInfo := RegInfo;
  135. End;
  136. If (RegCounter = Reg) Then
  137. Begin
  138. OrgRegFound := Found;
  139. OrgRegResult := TmpResult;
  140. OrgRegInfo := RegInfo
  141. End;
  142. Repeat
  143. Inc(RegCounter);
  144. Until (RegCounter > R_EDI) or
  145. ((ppaiprop(prevNonRemovablePai^.optInfo)^.regs[regCounter].typ
  146. in [con_ref,con_noRemoveRef]) {And
  147. ((Regcounter = Reg) Or
  148. Not(PaiInSequence(p, PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter]))) }
  149. );
  150. End;
  151. If (HighFound > 0) And
  152. (Not(OrgRegResult) Or
  153. (HighFound > OrgRegFound))
  154. Then
  155. Begin
  156. {$ifndef fpc}
  157. TmpResult := True;
  158. {$else fpc}
  159. CheckSequence := True;
  160. {$endif fpc}
  161. RegInfo := HighRegInfo;
  162. Found := HighFound
  163. End
  164. Else
  165. Begin
  166. {$ifndef fpc}
  167. TmpResult := OrgRegResult;
  168. {$else fpc}
  169. CheckSequence := OrgRegResult;
  170. {$endif fpc}
  171. Found := OrgRegFound;
  172. RegInfo := OrgRegInfo;
  173. End;
  174. {$ifndef fpc}
  175. CheckSequence := TmpResult;
  176. {$endif fpc}
  177. End; {CheckSequence}
  178. Procedure SetAlignReg(p: Pai);
  179. Const alignSearch = 12;
  180. var regsUsable: TRegSet;
  181. prevInstrCount, nextInstrCount: Longint;
  182. prevState, nextWState,nextRState: Array[R_EAX..R_EDI] of byte;
  183. regCounter, lastRemoved: TRegister;
  184. prev, next: Pai;
  185. {$ifdef alignregdebug}
  186. temp: Pai;
  187. {$endif alignregdebug}
  188. begin
  189. regsUsable := [R_EAX,R_ECX,R_EDX,R_EBX,{R_ESP,R_EBP,}R_ESI,R_EDI];
  190. for regCounter := R_EAX to R_EDI do
  191. begin
  192. prevState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  193. nextWState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  194. nextRState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].rState;
  195. end;
  196. getLastInstruction(p,prev);
  197. getNextInstruction(p,next);
  198. lastRemoved := pai_align(p)^.reg;
  199. nextInstrCount := 0;
  200. prevInstrCount := 0;
  201. while ((assigned(prev) and
  202. assigned(prev^.optInfo) and
  203. (prevInstrCount < alignSearch)) or
  204. (assigned(next) and
  205. assigned(next^.optInfo) and
  206. (nextInstrCount < alignSearch))) And
  207. (regsUsable <> []) do
  208. begin
  209. {$ifdef alignregdebug}
  210. if assigned(prev) then
  211. begin
  212. temp := new(pai_asm_comment,init(strpnew('got here')));
  213. temp^.next := prev^.next;
  214. temp^.previous := prev;
  215. prev^.next := temp;
  216. if assigned(temp^.next) then
  217. temp^.next^.previous := temp;
  218. end;
  219. {$endif alignregdebug}
  220. if assigned(prev) and assigned(prev^.optinfo) and
  221. (prevInstrCount < alignSearch) then
  222. begin
  223. if (prev^.typ = ait_instruction) And
  224. (insProp[PaiCpu(prev)^.opcode].ch[1] <> Ch_ALL) and
  225. (PaiCpu(prev)^.opcode <> A_JMP) then
  226. begin
  227. inc(prevInstrCount);
  228. for regCounter := R_EAX to R_EDI do
  229. begin
  230. if (regCounter in regsUsable) And
  231. (PPaiProp(prev^.optInfo)^.Regs[regCounter].wState <>
  232. prevState[regCounter]) then
  233. begin
  234. lastRemoved := regCounter;
  235. exclude(regsUsable,regCounter);
  236. {$ifdef alignregdebug}
  237. temp := new(pai_asm_comment,init(strpnew(
  238. att_reg2str[regCounter]+' removed')));
  239. temp^.next := prev^.next;
  240. temp^.previous := prev;
  241. prev^.next := temp;
  242. if assigned(temp^.next) then
  243. temp^.next^.previous := temp;
  244. if regsUsable = [] then
  245. begin
  246. temp := new(pai_asm_comment,init(strpnew(
  247. 'regsUsable empty here')));
  248. temp^.next := prev^.next;
  249. temp^.previous := prev;
  250. prev^.next := temp;
  251. if assigned(temp^.next) then
  252. temp^.next^.previous := temp;
  253. end;
  254. {$endif alignregdebug}
  255. end;
  256. prevState[regCounter] :=
  257. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState;
  258. end;
  259. getLastInstruction(prev,prev);
  260. end
  261. else
  262. If GetLastInstruction(prev,prev) and
  263. assigned(prev^.optinfo) then
  264. for regCounter := R_EAX to R_EDI do
  265. prevState[regCounter] :=
  266. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState
  267. end;
  268. if assigned(next) and assigned(next^.optInfo) and
  269. (nextInstrCount < alignSearch) then
  270. begin
  271. if (next^.typ = ait_instruction) and
  272. (insProp[PaiCpu(next)^.opcode].ch[1] <> Ch_ALL) and
  273. (PaiCpu(next)^.opcode <> A_JMP) then
  274. begin
  275. inc(nextInstrCount);
  276. for regCounter := R_EAX to R_EDI do
  277. begin
  278. if (regCounter in regsUsable) And
  279. ((PPaiProp(next^.optInfo)^.Regs[regCounter].wState <>
  280. nextWState[regCounter]) or
  281. (PPaiProp(next^.optInfo)^.Regs[regCounter].rState <>
  282. nextRState[regCounter])) Then
  283. begin
  284. lastRemoved := regCounter;
  285. exclude(regsUsable,regCounter);
  286. {$ifdef alignregdebug}
  287. temp := new(pai_asm_comment,init(strpnew(
  288. att_reg2str[regCounter]+' removed')));
  289. temp^.next := next^.next;
  290. temp^.previous := next;
  291. next^.next := temp;
  292. if assigned(temp^.next) then
  293. temp^.next^.previous := temp;
  294. if regsUsable = [] then
  295. begin
  296. temp := new(pai_asm_comment,init(strpnew(
  297. 'regsUsable empty here')));
  298. temp^.next := next^.next;
  299. temp^.previous := next;
  300. next^.next := temp;
  301. if assigned(temp^.next) then
  302. temp^.next^.previous := temp;
  303. end;
  304. {$endif alignregdebug}
  305. end;
  306. nextWState[regCounter] :=
  307. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  308. nextRState[regCounter] :=
  309. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  310. end
  311. end
  312. else
  313. for regCounter := R_EAX to R_EDI do
  314. begin
  315. nextWState[regCounter] :=
  316. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  317. nextRState[regCounter] :=
  318. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  319. end;
  320. getNextInstruction(next,next);
  321. end;
  322. end;
  323. if regsUsable <> [] then
  324. for regCounter := R_EAX to R_EDI do
  325. if regCounter in regsUsable then
  326. begin
  327. lastRemoved := regCounter;
  328. break
  329. end;
  330. {$ifdef alignregdebug}
  331. next := new(pai_asm_comment,init(strpnew(att_reg2str[lastRemoved]+
  332. ' chosen as alignment register')));
  333. next^.next := p^.next;
  334. next^.previous := p;
  335. p^.next := next;
  336. if assigned(next^.next) then
  337. next^.next^.previous := next;
  338. {$endif alignregdebug}
  339. pai_align(p)^.reg := lastRemoved;
  340. End;
  341. Procedure RestoreRegContentsTo(reg: TRegister; const c: TContent; p, endP: pai);
  342. var
  343. {$ifdef replaceregdebug}
  344. hp: pai;
  345. {$endif replaceregdebug}
  346. tmpState: byte;
  347. begin
  348. {$ifdef replaceregdebug}
  349. hp := new(pai_asm_comment,init(strpnew(
  350. 'restored '+att_reg2str[reg]+' with data from here...')));
  351. hp^.next := p;
  352. hp^.previous := p^.previous;
  353. p^.previous := hp;
  354. if assigned(hp^.previous) then
  355. hp^.previous^.next := hp;
  356. {$endif replaceregdebug}
  357. { PPaiProp(p^.optInfo)^.Regs[reg] := c;}
  358. While (p <> endP) Do
  359. Begin
  360. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  361. getNextInstruction(p,p);
  362. end;
  363. tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
  364. repeat
  365. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  366. until not getNextInstruction(p,p) or
  367. (PPaiProp(p^.optInfo)^.Regs[reg].wState <> tmpState);
  368. {$ifdef replaceregdebug}
  369. if assigned(p) then
  370. begin
  371. hp := new(pai_asm_comment,init(strpnew(
  372. 'restored '+att_reg2str[reg]+' till here...')));
  373. hp^.next := p;
  374. hp^.previous := p^.previous;
  375. p^.previous := hp;
  376. if assigned(hp^.previous) then
  377. hp^.previous^.next := hp;
  378. end;
  379. {$endif replaceregdebug}
  380. end;
  381. function FindRegDealloc(reg: tregister; p: pai): boolean;
  382. { assumes reg is a 32bit register }
  383. var
  384. hp: pai;
  385. first: boolean;
  386. begin
  387. findregdealloc := false;
  388. first := true;
  389. while assigned(p^.previous) and
  390. ((Pai(p^.previous)^.typ in (skipinstr+[ait_align])) or
  391. ((Pai(p^.previous)^.typ = ait_label) and
  392. labelCanBeSkipped(pai_label(p^.previous)))) do
  393. begin
  394. p := pai(p^.previous);
  395. if (p^.typ = ait_regalloc) and
  396. (pairegalloc(p)^.reg = reg) then
  397. if not(pairegalloc(p)^.allocation) then
  398. if first then
  399. begin
  400. findregdealloc := true;
  401. break;
  402. end
  403. else
  404. begin
  405. findRegDealloc :=
  406. getNextInstruction(p,hp) and
  407. regLoadedWithNewValue(reg,false,hp);
  408. break
  409. end
  410. else
  411. first := false;
  412. end
  413. end;
  414. Procedure ClearRegContentsFrom(reg: TRegister; p, endP: pai);
  415. { first clears the contents of reg from p till endP. Then the contents are }
  416. { cleared until the first instruction that changes reg }
  417. var
  418. {$ifdef replaceregdebug}
  419. hp: pai;
  420. {$endif replaceregdebug}
  421. tmpState: byte;
  422. begin
  423. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  424. While (p <> endP) Do
  425. Begin
  426. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  427. getNextInstruction(p,p);
  428. end;
  429. tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
  430. repeat
  431. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  432. until not getNextInstruction(p,p) or
  433. (PPaiProp(p^.optInfo)^.Regs[reg].wState <> tmpState);
  434. {$ifdef replaceregdebug}
  435. if assigned(p) then
  436. begin
  437. hp := new(pai_asm_comment,init(strpnew(
  438. 'cleared '+att_reg2str[reg]+' till here...')));
  439. hp^.next := p;
  440. hp^.previous := p^.previous;
  441. p^.previous := hp;
  442. if assigned(hp^.previous) then
  443. hp^.previous^.next := hp;
  444. end;
  445. {$endif replaceregdebug}
  446. end;
  447. function NoHardCodedRegs(p: paicpu; orgReg, newReg: tRegister): boolean;
  448. var chCount: byte;
  449. begin
  450. case p^.opcode of
  451. A_IMUL: noHardCodedRegs := p^.ops <> 1;
  452. A_SHL,A_SHR,A_SHLD,A_SHRD: noHardCodedRegs :=
  453. (p^.oper[0].typ <> top_reg) or
  454. ((orgReg <> R_ECX) and (newReg <> R_ECX));
  455. else
  456. begin
  457. NoHardCodedRegs := true;
  458. with InsProp[p^.opcode] do
  459. for chCount := 1 to MaxCh do
  460. if Ch[chCount] in ([Ch_REAX..Ch_MEDI,Ch_WMemEDI,Ch_All]-[Ch_RESP,Ch_WESP,Ch_RWESP]) then
  461. begin
  462. NoHardCodedRegs := false;
  463. break
  464. end;
  465. end;
  466. end;
  467. end;
  468. Procedure ChangeReg(var Reg: TRegister; orgReg, newReg: TRegister);
  469. begin
  470. if reg = newReg then
  471. reg := orgReg
  472. else if reg = regtoreg8(newReg) then
  473. reg := regtoreg8(orgReg)
  474. else if reg = regtoreg16(newReg) then
  475. reg := regtoreg16(orgReg);
  476. end;
  477. procedure changeOp(var o: toper; orgReg, newReg: tregister);
  478. begin
  479. case o.typ of
  480. top_reg: changeReg(o.reg,orgReg,newReg);
  481. top_ref:
  482. begin
  483. changeReg(o.ref^.base,orgReg,newReg);
  484. changeReg(o.ref^.index,orgReg,newReg);
  485. end;
  486. end;
  487. end;
  488. Procedure DoReplaceReg(orgReg,newReg: tregister; hp: paicpu);
  489. var opCount: byte;
  490. begin
  491. for opCount := 0 to 2 do
  492. changeOp(hp^.oper[opCount],orgReg,newReg)
  493. end;
  494. function RegSizesOK(oldReg,newReg: TRegister; p: paicpu): boolean;
  495. { oldreg and newreg must be 32bit components }
  496. var opCount: byte;
  497. begin
  498. RegSizesOK := true;
  499. { if only one of them is a general purpose register ... }
  500. if (IsGP32reg(oldReg) xor IsGP32Reg(newReg)) then
  501. begin
  502. for opCount := 0 to 2 do
  503. if (p^.oper[opCount].typ = top_reg) and
  504. (p^.oper[opCount].reg in [R_AL..R_DH]) then
  505. begin
  506. RegSizesOK := false;
  507. break
  508. end
  509. end;
  510. end;
  511. procedure DoReplaceReadReg(orgReg,newReg: tregister; p: paicpu);
  512. var opCount: byte;
  513. begin
  514. { handle special case }
  515. case p^.opcode of
  516. A_IMUL:
  517. begin
  518. case p^.ops of
  519. 1: internalerror(1301001);
  520. 2,3:
  521. begin
  522. changeOp(p^.oper[0],orgReg,newReg);
  523. if p^.ops = 3 then
  524. changeOp(p^.oper[1],orgReg,newReg);
  525. end;
  526. end;
  527. end;
  528. A_DIV,A_IDIV,A_MUL: internalerror(1301002);
  529. else
  530. begin
  531. for opCount := 0 to 2 do
  532. if p^.oper[opCount].typ = top_ref then
  533. changeOp(p^.oper[opCount],orgReg,newReg);
  534. for opCount := 1 to MaxCh do
  535. case InsProp[p^.opcode].Ch[opCount] of
  536. Ch_ROp1:
  537. if p^.oper[0].typ = top_reg then
  538. ChangeReg(p^.oper[0].reg,orgReg,newReg);
  539. Ch_ROp2:
  540. if p^.oper[1].typ = top_reg then
  541. ChangeReg(p^.oper[1].reg,orgReg,newReg);
  542. Ch_ROp3:
  543. if p^.oper[2].typ = top_reg then
  544. ChangeReg(p^.oper[2].reg,orgReg,newReg);
  545. end;
  546. end;
  547. end;
  548. end;
  549. function ReplaceReg(asmL: PaasmOutput; orgReg, newReg: TRegister; p: pai;
  550. const c: TContent; orgRegCanBeModified: Boolean;
  551. var returnEndP: pai): Boolean;
  552. { Tries to replace orgreg with newreg in all instructions coming after p }
  553. { until orgreg gets loaded with a new value. Returns true if successful, }
  554. { false otherwise. If successful, the contents of newReg are set to c, }
  555. { which should hold the contents of newReg before the current sequence }
  556. { started }
  557. { if the function returns true, returnEndP holds the last instruction }
  558. { where newReg was replaced by orgReg }
  559. var endP, hp: Pai;
  560. removeLast, sequenceEnd, tmpResult, newRegModified, orgRegRead: Boolean;
  561. function storeBack(p1: pai): boolean;
  562. { returns true if p1 contains an instruction that stores the contents }
  563. { of newReg back to orgReg }
  564. begin
  565. storeBack :=
  566. (p1^.typ = ait_instruction) and
  567. (paicpu(p1)^.opcode = A_MOV) and
  568. (paicpu(p1)^.oper[0].typ = top_reg) and
  569. (paicpu(p1)^.oper[0].reg = newReg) and
  570. (paicpu(p1)^.oper[1].typ = top_reg) and
  571. (paicpu(p1)^.oper[1].reg = orgReg);
  572. end;
  573. begin
  574. ReplaceReg := false;
  575. tmpResult := true;
  576. sequenceEnd := false;
  577. newRegModified := false;
  578. orgRegRead := false;
  579. removeLast := false;
  580. endP := p;
  581. while tmpResult and not sequenceEnd do
  582. begin
  583. tmpResult :=
  584. getNextInstruction(endP,endP) and
  585. (endP^.typ = ait_instruction);
  586. if tmpresult and not assigned(endP^.optInfo) then
  587. begin
  588. { hp := new(pai_asm_comment,init(strpnew('next no optinfo')));
  589. hp^.next := endp;
  590. hp^.previous := endp^.previous;
  591. endp^.previous := hp;
  592. if assigned(hp^.previous) then
  593. hp^.previous^.next := hp;}
  594. exit;
  595. end;
  596. If tmpResult and
  597. { don't take into account instructions that will be removed }
  598. Not (PPaiProp(endP^.optInfo)^.canBeRemoved) then
  599. begin
  600. { if the newReg gets stored back to the oldReg, we can change }
  601. { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
  602. { %oldReg" to "<operations on %oldReg>" }
  603. removeLast := storeBack(endP);
  604. sequenceEnd :=
  605. { no support for (i)div, mul and imul with hardcoded operands }
  606. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  607. { if newReg gets loaded with a new value, we can stop }
  608. { replacing newReg with oldReg here (possibly keeping }
  609. { the original contents of oldReg so we still know them }
  610. { afterwards) }
  611. RegLoadedWithNewValue(newReg,true,paicpu(endP)) or
  612. { we can also stop if we reached the end of the use of }
  613. { newReg's current contents }
  614. (GetNextInstruction(endp,hp) and
  615. FindRegDealloc(newReg,hp)));
  616. { to be able to remove the first and last instruction of }
  617. { movl %reg1, %reg2 }
  618. { <operations on %reg2> (replacing reg2 with reg1 here) }
  619. { movl %reg2, %reg1 }
  620. { %reg2 must not be use afterwards (it can be as the }
  621. { result of a peepholeoptimization) }
  622. removeLast := removeLast and sequenceEnd;
  623. newRegModified :=
  624. newRegModified or
  625. (not(regLoadedWithNewValue(newReg,true,paicpu(endP))) and
  626. RegModifiedByInstruction(newReg,endP));
  627. orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
  628. sequenceEnd := SequenceEnd and
  629. (removeLast or
  630. { since newReg will be replaced by orgReg, we can't allow that newReg }
  631. { gets modified if orgReg is still read afterwards (since after }
  632. { replacing, this would mean that orgReg first gets modified and then }
  633. { gets read in the assumption it still contains the unmodified value) }
  634. not(newRegModified and orgRegRead)) (* and
  635. { since newReg will be replaced by orgReg, we can't allow that newReg }
  636. { gets modified if orgRegCanBeModified = false }
  637. (orgRegCanBeModified or not(newRegModified)) *);
  638. tmpResult :=
  639. not(removeLast) and
  640. not(newRegModified and orgRegRead) and
  641. (* (orgRegCanBeModified or not(newRegModified)) and *)
  642. (endP^.typ = ait_instruction) and
  643. not(paicpu(endP)^.is_jmp) and
  644. NoHardCodedRegs(paicpu(endP),orgReg,newReg) and
  645. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  646. not RegModifiedByInstruction(orgReg,endP);
  647. end;
  648. end;
  649. sequenceEnd := sequenceEnd and
  650. (removeLast or
  651. (orgRegCanBeModified or not(newRegModified))) and
  652. (not(assigned(endp)) or
  653. not(endp^.typ = ait_instruction) or
  654. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  655. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  656. not(newRegModified and
  657. (orgReg in PPaiProp(endP^.optInfo)^.usedRegs) and
  658. not(RegLoadedWithNewValue(orgReg,true,paicpu(endP))))));
  659. if SequenceEnd then
  660. begin
  661. {$ifdef replaceregdebug}
  662. hp := new(pai_asm_comment,init(strpnew(
  663. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  664. ' from here...')));
  665. hp^.next := p;
  666. hp^.previous := p^.previous;
  667. p^.previous := hp;
  668. if assigned(hp^.previous) then
  669. hp^.previous^.next := hp;
  670. hp := new(pai_asm_comment,init(strpnew(
  671. 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  672. ' till here')));
  673. hp^.next := endp^.next;
  674. hp^.previous := endp;
  675. endp^.next := hp;
  676. if assigned(hp^.next) then
  677. hp^.next^.previous := hp;
  678. {$endif replaceregdebug}
  679. replaceReg := true;
  680. returnEndP := endP;
  681. getNextInstruction(p,hp);
  682. while hp <> endP do
  683. begin
  684. if not(PPaiProp(hp^.optInfo)^.canBeRemoved) and
  685. (hp^.typ = ait_instruction) then
  686. DoReplaceReg(orgReg,newReg,paicpu(hp));
  687. GetNextInstruction(hp,hp)
  688. end;
  689. if assigned(endp) and (endp^.typ = ait_instruction) then
  690. DoReplaceReadReg(orgReg,newReg,paicpu(endP));
  691. { the replacing stops either at the moment that }
  692. { a) the newreg gets loaded with a new value (one not depending on the }
  693. { current value of newreg) }
  694. { b) newreg is completely replaced in this sequence and it's current value }
  695. { isn't used anymore }
  696. { In case b, the newreg was completely replaced by oldreg, so it's contents }
  697. { are unchanged compared the start of this sequence, so restore them }
  698. If removeLast or
  699. RegLoadedWithNewValue(newReg,true,endP) then
  700. GetLastInstruction(endP,hp)
  701. else hp := endP;
  702. if removeLast or
  703. (p <> endp) or
  704. not RegLoadedWithNewValue(newReg,true,endP) then
  705. RestoreRegContentsTo(newReg, c ,p, hp);
  706. { In both case a and b, it is possible that the new register was modified }
  707. { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
  708. { oldreg's contents have been changed. To take this into account, we simply }
  709. { set the contents of orgreg to "unknown" after this sequence }
  710. if newRegModified then
  711. ClearRegContentsFrom(orgReg,p,hp);
  712. if removeLast then
  713. ppaiprop(endP^.optinfo)^.canBeRemoved := true;
  714. allocRegBetween(asml,orgReg,p,endP);
  715. end
  716. {$ifdef replaceregdebug}
  717. else
  718. begin
  719. hp := new(pai_asm_comment,init(strpnew(
  720. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  721. ' from here...')));
  722. hp^.previous := p^.previous;
  723. hp^.next := p;
  724. p^.previous := hp;
  725. if assigned(hp^.previous) then
  726. hp^.previous^.next := hp;
  727. hp := new(pai_asm_comment,init(strpnew(
  728. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  729. ' failed here')));
  730. hp^.next := endp^.next;
  731. hp^.previous := endp;
  732. endp^.next := hp;
  733. if assigned(hp^.next) then
  734. hp^.next^.previous := hp;
  735. end;
  736. {$endif replaceregdebug}
  737. End;
  738. Function FindRegWithConst(p: Pai; size: topsize; l: longint; Var Res: TRegister): Boolean;
  739. {Finds a register which contains the constant l}
  740. Var Counter: TRegister;
  741. {$ifdef testing}
  742. hp: pai;
  743. {$endif testing}
  744. tmpresult: boolean;
  745. Begin
  746. Counter := R_NO;
  747. repeat
  748. inc(counter);
  749. tmpresult := (ppaiprop(p^.optInfo)^.regs[counter].typ in
  750. [con_const,con_noRemoveConst]) and
  751. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.opsize = size) and
  752. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].typ = top_const) and
  753. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].val = l);
  754. {$ifdef testing}
  755. if (ppaiprop(p^.optInfo)^.regs[counter].typ in [con_const,con_noRemoveConst]) then
  756. begin
  757. hp := new(pai_asm_comment,init(strpnew(
  758. 'checking const load of '+tostr(l)+' here...')));
  759. hp^.next := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod;
  760. hp^.previous := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous;
  761. PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
  762. if assigned(hp^.previous) then
  763. hp^.previous^.next := hp;
  764. end;
  765. {$endif testing}
  766. until tmpresult or (Counter = R_EDI);
  767. res := counter;
  768. FindRegWithConst := tmpResult;
  769. End;
  770. procedure removePrevNotUsedLoad(p: pai; reg: tRegister; check: boolean);
  771. { If check = true, it means the procedure has to check whether it isn't }
  772. { possible that the contents are still used after p (used when removing }
  773. { instructions because of a "call"), otherwise this is not necessary }
  774. { (e.g. when you have a "mov 8(%ebp),%eax", you can be sure the previous }
  775. { value of %eax isn't used anymore later on) }
  776. var
  777. hp1: pai;
  778. begin
  779. if getLastInstruction(p,hp1) then
  780. with ppaiprop(hp1^.optInfo)^.regs[reg] do
  781. if (typ in [con_ref,con_invalid]) and
  782. (nrOfMods = 1) and
  783. (rState = ppaiprop(startmod^.optInfo)^.regs[reg].rState) and
  784. (not(check) or
  785. (not(regInInstruction(reg,p)) and
  786. (not(reg in usableregs) and
  787. (startmod^.typ = ait_instruction) and
  788. ((paicpu(startmod)^.opcode = A_MOV) or
  789. (paicpu(startmod)^.opcode = A_MOVZX) or
  790. (paicpu(startmod)^.opcode = A_MOVSX)) and
  791. (paicpu(startmod)^.oper[0].typ = top_ref) and
  792. (paicpu(startmod)^.oper[0].ref^.base = stack_pointer)) or
  793. not(reg in ppaiprop(hp1^.optInfo)^.usedRegs) or
  794. findRegDealloc(reg,p))) then
  795. ppaiprop(startMod^.optInfo)^.canBeRemoved := true;
  796. end;
  797. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  798. {marks the instructions that can be removed by RemoveInstructs. They're not
  799. removed immediately because sometimes an instruction needs to be checked in
  800. two different sequences}
  801. Var Cnt, Cnt2: Longint;
  802. p, hp1, hp2: Pai;
  803. hp3, hp4: pai;
  804. hp5 : pai;
  805. RegInfo: TRegInfo;
  806. RegCounter: TRegister;
  807. Begin
  808. p := First;
  809. SkipHead(p);
  810. First := p;
  811. While (p <> Last) Do
  812. Begin
  813. Case p^.typ Of
  814. ait_align:
  815. if not(pai_align(p)^.use_op) then
  816. SetAlignReg(p);
  817. ait_instruction:
  818. Begin
  819. Case Paicpu(p)^.opcode Of
  820. A_CALL:
  821. for regCounter := R_EAX to R_EBX do
  822. removePrevNotUsedLoad(p,regCounter,true);
  823. A_CLD: If GetLastInstruction(p, hp1) And
  824. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  825. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  826. A_MOV, A_MOVZX, A_MOVSX:
  827. Begin
  828. Case Paicpu(p)^.oper[0].typ Of
  829. Top_Ref:
  830. Begin {destination is always a register in this case}
  831. With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  832. Begin
  833. If (p = StartMod) And
  834. GetLastInstruction (p, hp1) And
  835. (hp1^.typ <> ait_marker) Then
  836. {so we don't try to check a sequence when p is the first instruction of the block}
  837. begin
  838. {$ifdef csdebug}
  839. hp5 := new(pai_asm_comment,init(strpnew(
  840. 'cse checking '+att_reg2str[Reg32(Paicpu(p)^.oper[1].reg)])));
  841. insertLLItem(asml,p,p^.next,hp5);
  842. {$endif csdebug}
  843. If CheckSequence(p, Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
  844. (Cnt > 0) Then
  845. Begin
  846. hp1 := nil;
  847. { although it's perfectly ok to remove an instruction which doesn't contain }
  848. { the register that we've just checked (CheckSequence takes care of that), }
  849. { the sequence containing this other register should also be completely }
  850. { checked and removed, otherwise we may get situations like this: }
  851. { }
  852. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  853. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  854. { movl 8(%edx), %edx movl 8(%edx), %edx }
  855. { movl (%eax), eax movl (%eax), eax }
  856. { cmpl %eax, %edx cmpl %eax, %edx }
  857. { jnz l123 getting converted to jnz l123 }
  858. { movl 12(%ebp), %edx movl 4(%eax), eax }
  859. { movl 16(%ebp), %eax }
  860. { movl 8(%edx), %edx }
  861. { movl 4(%eax), eax }
  862. hp2 := p;
  863. Cnt2 := 1;
  864. While Cnt2 <= Cnt Do
  865. Begin
  866. If Not(RegInInstruction(reg32(Paicpu(hp2)^.oper[1].reg), p)) then
  867. begin
  868. if ((p^.typ = ait_instruction) And
  869. ((paicpu(p)^.OpCode = A_MOV) or
  870. (paicpu(p)^.opcode = A_MOVZX) or
  871. (paicpu(p)^.opcode = A_MOVSX)) And
  872. (paicpu(p)^.Oper[0].typ in
  873. [top_const,top_ref,top_symbol])) and
  874. (paicpu(p)^.oper[1].typ = top_reg) then
  875. begin
  876. regCounter := reg32(paicpu(p)^.oper[1].reg);
  877. if (regCounter in reginfo.regsStillUsedAfterSeq) then
  878. begin
  879. if (hp1 = nil) then
  880. hp1 := reginfo.lastReload[regCounter];
  881. end
  882. {$ifndef noremove}
  883. else
  884. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  885. {$endif noremove}
  886. end
  887. end
  888. {$ifndef noremove}
  889. else
  890. if regInInstruction(Paicpu(hp2)^.oper[1].reg,p) then
  891. PPaiProp(p^.OptInfo)^.CanBeRemoved := True
  892. {$endif noremove}
  893. ; Inc(Cnt2);
  894. GetNextInstruction(p, p);
  895. End;
  896. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  897. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  898. {hp4 is used to get the contents of the registers before the sequence}
  899. GetLastInstruction(hp2, hp4);
  900. {$IfDef CSDebug}
  901. For RegCounter := R_EAX To R_EDI Do
  902. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  903. Begin
  904. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  905. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  906. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  907. End;
  908. {$EndIf CSDebug}
  909. { If some registers were different in the old and the new sequence, move }
  910. { the contents of those old registers to the new ones }
  911. For RegCounter := R_EAX To R_EDI Do
  912. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  913. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  914. Begin
  915. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  916. PPaiProp(hp4^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,hp2);
  917. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  918. {old reg new reg}
  919. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  920. Begin
  921. getLastInstruction(p,hp3);
  922. If not(regCounter in usableRegs + [R_EDI,R_ESI]) or
  923. not ReplaceReg(asmL,RegInfo.New2OldReg[RegCounter],
  924. regCounter,hp3,
  925. PPaiProp(hp4^.optInfo)^.Regs[regCounter],true,hp5) then
  926. begin
  927. hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
  928. {old reg new reg}
  929. RegInfo.New2OldReg[RegCounter], RegCounter));
  930. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
  931. end
  932. End
  933. Else
  934. { imagine the following code: }
  935. { normal wrong optimized }
  936. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  937. { movl (%eax), %eax movl (%eax), %eax }
  938. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  939. { jne l1 jne l1 }
  940. { movl 8(%ebp), %eax }
  941. { movl (%eax), %edi movl %eax, %edi }
  942. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  943. { movl 8(%ebp), %eax }
  944. { pushl 70(%eax) pushl 70(%eax) }
  945. { }
  946. { The error is that at the moment that the last instruction is executed, }
  947. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  948. { registers that are completely removed from a sequence (= registers in }
  949. { RegLoadedForRef, have to be changed to their contents from before the }
  950. { sequence. }
  951. If RegCounter in RegInfo.RegsLoadedForRef Then
  952. Begin
  953. hp3 := hp2;
  954. { cnt still holds the number of instructions }
  955. { of the sequence, so go to the end of it }
  956. for cnt2 := 1 to pred(cnt) Do
  957. getNextInstruction(hp3,hp3);
  958. { hp4 = instruction prior to start of sequence }
  959. restoreRegContentsTo(regCounter,
  960. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter],
  961. hp2,hp3);
  962. End;
  963. End;
  964. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  965. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  966. If hp1 <> nil Then
  967. p := hp1;
  968. Continue;
  969. End
  970. Else
  971. If (PPaiProp(p^.OptInfo)^.
  972. regs[reg32(paicpu(p)^.oper[1].reg)].typ
  973. in [con_ref,con_noRemoveRef]) and
  974. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  975. if (cnt > 0) then
  976. begin
  977. hp2 := p;
  978. Cnt2 := 1;
  979. While Cnt2 <= Cnt Do
  980. Begin
  981. If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Then
  982. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  983. Inc(Cnt2);
  984. GetNextInstruction(p, p);
  985. End;
  986. Continue;
  987. End
  988. else
  989. begin
  990. { Fix for web bug 972 }
  991. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  992. cnt := PPaiProp(p^.optInfo)^.Regs[regCounter].nrOfMods;
  993. hp3 := p;
  994. for cnt2 := 1 to cnt do
  995. if not(regModifiedByInstruction(regCounter,hp3) and
  996. not(PPaiProp(hp3^.optInfo)^.canBeRemoved)) then
  997. getNextInstruction(hp3,hp3)
  998. else
  999. break;
  1000. getLastInstruction(p,hp4);
  1001. RestoreRegContentsTo(regCounter,
  1002. PPaiProp(hp4^.optInfo)^.Regs[regCounter],
  1003. p,hp3);
  1004. end;
  1005. End;
  1006. End;
  1007. if not ppaiprop(p^.optinfo)^.canBeRemoved and
  1008. not regInRef(reg32(paicpu(p)^.oper[1].reg),
  1009. paicpu(p)^.oper[0].ref^) then
  1010. removePrevNotUsedLoad(p,reg32(paicpu(p)^.oper[1].reg),false);
  1011. End;
  1012. top_Reg:
  1013. { try to replace the new reg with the old reg }
  1014. if not(PPaiProp(p^.optInfo)^.canBeRemoved) and
  1015. { only remove if we're not storing something in a regvar }
  1016. (paicpu(p)^.oper[1].reg in (usableregs+[R_EDI])) and
  1017. (paicpu(p)^.opcode = A_MOV) and
  1018. getLastInstruction(p,hp4) then
  1019. begin
  1020. case paicpu(p)^.oper[1].typ of
  1021. top_Reg:
  1022. { we only have to start replacing from the instruction after the mov, }
  1023. { but replacereg only starts with getnextinstruction(p,p) }
  1024. if ReplaceReg(asmL,paicpu(p)^.oper[0].reg,
  1025. paicpu(p)^.oper[1].reg,p,
  1026. PPaiProp(hp4^.optInfo)^.Regs[paicpu(p)^.oper[1].reg],false,hp1) then
  1027. begin
  1028. PPaiProp(p^.optInfo)^.canBeRemoved := true;
  1029. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,
  1030. PPaiProp(p^.optInfo)^.regs[paicpu(p)^.oper[0].reg].startMod,
  1031. hp1);
  1032. end;
  1033. end
  1034. end;
  1035. top_symbol,Top_Const:
  1036. Begin
  1037. Case Paicpu(p)^.oper[1].typ Of
  1038. Top_Reg:
  1039. Begin
  1040. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  1041. If GetLastInstruction(p, hp1) Then
  1042. With PPaiProp(hp1^.OptInfo)^.Regs[regCounter] Do
  1043. if (typ in [con_const,con_noRemoveConst]) and
  1044. (paicpu(startMod)^.opsize >= paicpu(p)^.opsize) and
  1045. opsequal(paicpu(StartMod)^.oper[0],paicpu(p)^.oper[0]) Then
  1046. begin
  1047. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  1048. allocRegBetween(asmL,regCounter,startMod,p);
  1049. end;
  1050. End;
  1051. Top_Ref:
  1052. if (paicpu(p)^.oper[0].typ = top_const) and
  1053. getLastInstruction(p,hp1) and
  1054. findRegWithConst(hp1,paicpu(p)^.opsize,paicpu(p)^.oper[0].val,regCounter) then
  1055. begin
  1056. paicpu(p)^.loadreg(0,regCounter);
  1057. allocRegBetween(AsmL,reg32(regCounter),
  1058. PPaiProp(hp1^.optinfo)^.regs[regCounter].startMod,p);
  1059. end;
  1060. End;
  1061. End;
  1062. End;
  1063. End;
  1064. A_STD: If GetLastInstruction(p, hp1) And
  1065. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  1066. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1067. End
  1068. End;
  1069. End;
  1070. GetNextInstruction(p, p);
  1071. End;
  1072. End;
  1073. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  1074. { Removes the marked instructions and disposes the PPaiProps of the other }
  1075. { instructions }
  1076. Var p, hp1: Pai;
  1077. begin
  1078. p := First;
  1079. While (p <> Last) Do
  1080. Begin
  1081. If (p^.typ = ait_marker) and
  1082. (pai_marker(p)^.kind in [noPropInfoStart,noPropInfoEnd]) then
  1083. begin
  1084. hp1 := pai(p^.next);
  1085. asmL^.remove(p);
  1086. dispose(p,done);
  1087. p := hp1
  1088. end
  1089. else
  1090. {$ifndef noinstremove}
  1091. if assigned(p^.optInfo) and
  1092. PPaiProp(p^.optInfo)^.canBeRemoved then
  1093. begin
  1094. {$IfDef TP}
  1095. Dispose(PPaiProp(p^.OptInfo));
  1096. {$EndIf}
  1097. hp1 := pai(p^.next);
  1098. AsmL^.Remove(p);
  1099. Dispose(p, Done);
  1100. p := hp1;
  1101. End
  1102. Else
  1103. {$endif noinstremove}
  1104. Begin
  1105. {$IfDef TP}
  1106. if assigned(p^.optInfo) then
  1107. Dispose(PPaiProp(p^.OptInfo));
  1108. {$EndIf TP}
  1109. p^.OptInfo := nil;
  1110. p := pai(p^.next);;
  1111. End;
  1112. End;
  1113. {$IfNDef TP}
  1114. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  1115. {$EndIf TP}
  1116. End;
  1117. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  1118. Begin
  1119. DoCSE(AsmL, First, Last);
  1120. RemoveInstructs(AsmL, First, Last);
  1121. End;
  1122. End.
  1123. {
  1124. $Log$
  1125. Revision 1.6 2000-08-23 12:55:10 jonas
  1126. * fix for web bug 1112 and a bit of clean up in csopt386 (merged from
  1127. fixes branch)
  1128. Revision 1.5 2000/08/04 20:08:03 jonas
  1129. * improved detection of range of instructions which use a register
  1130. (merged from fixes branch)
  1131. Revision 1.4 2000/07/21 15:19:54 jonas
  1132. * daopt386: changes to getnextinstruction/getlastinstruction so they
  1133. ignore labels who have is_addr set
  1134. + daopt386/csopt386: remove loads of registers which are overwritten
  1135. before their contents are used (especially usefull for removing superfluous
  1136. maybe_loadesi outputs and push/pops transformed by below optimization
  1137. + popt386: transform pop/pop/pop/.../push/push/push to sequences of
  1138. 'movl x(%esp),%reg' (only active when compiling a go32v2 compiler
  1139. currently because I don't know whether it's safe to do this under Win32/
  1140. Linux (because of problems we had when using esp as frame pointer on
  1141. those os'es)
  1142. Revision 1.3 2000/07/14 05:11:48 michael
  1143. + Patch to 1.1
  1144. Revision 1.2 2000/07/13 11:32:39 michael
  1145. + removed logs
  1146. }