csopt386.pas 49 KB

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