csopt386.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. begin
  370. findregdealloc := false;
  371. while assigned(p^.previous) and
  372. ((Pai(p^.previous)^.typ in (skipinstr+[ait_align])) or
  373. ((Pai(p^.previous)^.typ = ait_label) and
  374. labelCanBeSkipped(pai_label(p^.previous)))) do
  375. begin
  376. p := pai(p^.previous);
  377. if (p^.typ = ait_regalloc) and
  378. (pairegalloc(p)^.reg = reg) then
  379. begin
  380. findregdealloc := not(pairegalloc(p)^.allocation);
  381. break;
  382. end;
  383. end
  384. end;
  385. Procedure ClearRegContentsFrom(reg: TRegister; p, endP: pai);
  386. { first clears the contents of reg from p till endP. Then the contents are }
  387. { cleared until the first instruction that changes reg }
  388. var
  389. {$ifdef replaceregdebug}
  390. hp: pai;
  391. {$endif replaceregdebug}
  392. tmpState: byte;
  393. begin
  394. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  395. While (p <> endP) Do
  396. Begin
  397. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  398. getNextInstruction(p,p);
  399. end;
  400. tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
  401. repeat
  402. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  403. until not getNextInstruction(p,p) or
  404. (PPaiProp(p^.optInfo)^.Regs[reg].wState <> tmpState);
  405. {$ifdef replaceregdebug}
  406. if assigned(p) then
  407. begin
  408. hp := new(pai_asm_comment,init(strpnew(
  409. 'cleared '+att_reg2str[reg]+' till here...')));
  410. hp^.next := p;
  411. hp^.previous := p^.previous;
  412. p^.previous := hp;
  413. if assigned(hp^.previous) then
  414. hp^.previous^.next := hp;
  415. end;
  416. {$endif replaceregdebug}
  417. end;
  418. function NoHardCodedRegs(p: paicpu; orgReg, newReg: tRegister): boolean;
  419. var chCount: byte;
  420. begin
  421. case p^.opcode of
  422. A_IMUL: noHardCodedRegs := p^.ops <> 1;
  423. A_SHL,A_SHR,A_SHLD,A_SHRD: noHardCodedRegs :=
  424. (p^.oper[0].typ <> top_reg) or
  425. ((orgReg <> R_ECX) and (newReg <> R_ECX));
  426. else
  427. begin
  428. NoHardCodedRegs := true;
  429. with InsProp[p^.opcode] do
  430. for chCount := 1 to MaxCh do
  431. if Ch[chCount] in ([Ch_REAX..Ch_MEDI,Ch_WMemEDI,Ch_All]-[Ch_RESP,Ch_WESP,Ch_RWESP]) then
  432. begin
  433. NoHardCodedRegs := false;
  434. break
  435. end;
  436. end;
  437. end;
  438. end;
  439. Procedure ChangeReg(var Reg: TRegister; orgReg, newReg: TRegister);
  440. begin
  441. if reg = newReg then
  442. reg := orgReg
  443. else if reg = regtoreg8(newReg) then
  444. reg := regtoreg8(orgReg)
  445. else if reg = regtoreg16(newReg) then
  446. reg := regtoreg16(orgReg);
  447. end;
  448. procedure changeOp(var o: toper; orgReg, newReg: tregister);
  449. begin
  450. case o.typ of
  451. top_reg: changeReg(o.reg,orgReg,newReg);
  452. top_ref:
  453. begin
  454. changeReg(o.ref^.base,orgReg,newReg);
  455. changeReg(o.ref^.index,orgReg,newReg);
  456. end;
  457. end;
  458. end;
  459. Procedure DoReplaceReg(orgReg,newReg: tregister; hp: paicpu);
  460. var opCount: byte;
  461. begin
  462. for opCount := 0 to 2 do
  463. changeOp(hp^.oper[opCount],orgReg,newReg)
  464. end;
  465. function RegSizesOK(oldReg,newReg: TRegister; p: paicpu): boolean;
  466. { oldreg and newreg must be 32bit components }
  467. var opCount: byte;
  468. begin
  469. RegSizesOK := true;
  470. { if only one of them is a general purpose register ... }
  471. if (IsGP32reg(oldReg) xor IsGP32Reg(newReg)) then
  472. begin
  473. for opCount := 0 to 2 do
  474. if (p^.oper[opCount].typ = top_reg) and
  475. (p^.oper[opCount].reg in [R_AL..R_DH]) then
  476. begin
  477. RegSizesOK := false;
  478. break
  479. end
  480. end;
  481. end;
  482. procedure DoReplaceReadReg(orgReg,newReg: tregister; p: paicpu);
  483. var opCount: byte;
  484. begin
  485. { handle special case }
  486. case p^.opcode of
  487. A_IMUL:
  488. begin
  489. case p^.ops of
  490. 1: internalerror(1301001);
  491. 2,3:
  492. begin
  493. changeOp(p^.oper[0],orgReg,newReg);
  494. if p^.ops = 3 then
  495. changeOp(p^.oper[1],orgReg,newReg);
  496. end;
  497. end;
  498. end;
  499. A_DIV,A_IDIV,A_MUL: internalerror(1301002);
  500. else
  501. begin
  502. for opCount := 0 to 2 do
  503. if p^.oper[opCount].typ = top_ref then
  504. changeOp(p^.oper[opCount],orgReg,newReg);
  505. for opCount := 1 to MaxCh do
  506. case InsProp[p^.opcode].Ch[opCount] of
  507. Ch_ROp1:
  508. if p^.oper[0].typ = top_reg then
  509. ChangeReg(p^.oper[0].reg,orgReg,newReg);
  510. Ch_ROp2:
  511. if p^.oper[1].typ = top_reg then
  512. ChangeReg(p^.oper[1].reg,orgReg,newReg);
  513. Ch_ROp3:
  514. if p^.oper[2].typ = top_reg then
  515. ChangeReg(p^.oper[2].reg,orgReg,newReg);
  516. end;
  517. end;
  518. end;
  519. end;
  520. function ReplaceReg(asmL: PaasmOutput; orgReg, newReg: TRegister; p: pai;
  521. const c: TContent; orgRegCanBeModified: Boolean;
  522. var returnEndP: pai): Boolean;
  523. { Tries to replace orgreg with newreg in all instructions coming after p }
  524. { until orgreg gets loaded with a new value. Returns true if successful, }
  525. { false otherwise. If successful, the contents of newReg are set to c, }
  526. { which should hold the contents of newReg before the current sequence }
  527. { started }
  528. { if the function returns true, returnEndP holds the last instruction }
  529. { where newReg was replaced by orgReg }
  530. var endP, hp: Pai;
  531. removeLast, sequenceEnd, tmpResult, newRegModified, orgRegRead: Boolean;
  532. function storeBack(p1: pai): boolean;
  533. { returns true if p1 contains an instruction that stores the contents }
  534. { of newReg back to orgReg }
  535. begin
  536. storeBack :=
  537. (p1^.typ = ait_instruction) and
  538. (paicpu(p1)^.opcode = A_MOV) and
  539. (paicpu(p1)^.oper[0].typ = top_reg) and
  540. (paicpu(p1)^.oper[0].reg = newReg) and
  541. (paicpu(p1)^.oper[1].typ = top_reg) and
  542. (paicpu(p1)^.oper[1].reg = orgReg);
  543. end;
  544. begin
  545. ReplaceReg := false;
  546. tmpResult := true;
  547. sequenceEnd := false;
  548. newRegModified := false;
  549. orgRegRead := false;
  550. removeLast := false;
  551. endP := p;
  552. while tmpResult and not sequenceEnd do
  553. begin
  554. tmpResult :=
  555. getNextInstruction(endP,endP) and
  556. (endP^.typ = ait_instruction);
  557. if tmpresult and not assigned(endP^.optInfo) then
  558. begin
  559. { hp := new(pai_asm_comment,init(strpnew('next no optinfo')));
  560. hp^.next := endp;
  561. hp^.previous := endp^.previous;
  562. endp^.previous := hp;
  563. if assigned(hp^.previous) then
  564. hp^.previous^.next := hp;}
  565. exit;
  566. end;
  567. If tmpResult and
  568. { don't take into account instructions that will be removed }
  569. Not (PPaiProp(endP^.optInfo)^.canBeRemoved) then
  570. begin
  571. { if the newReg gets stored back to the oldReg, we can change }
  572. { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
  573. { %oldReg" to "<operations on %oldReg>" }
  574. removeLast := storeBack(endP);
  575. sequenceEnd :=
  576. { no support for (i)div, mul and imul with hardcoded operands }
  577. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  578. { if newReg gets loaded with a new value, we can stop }
  579. { replacing newReg with oldReg here (possibly keeping }
  580. { the original contents of oldReg so we still know them }
  581. { afterwards) }
  582. RegLoadedWithNewValue(newReg,true,paicpu(endP)) or
  583. { we can also stop if we reached the end of the use of }
  584. { newReg's current contents }
  585. (GetNextInstruction(endp,hp) and
  586. FindRegDealloc(newReg,hp)));
  587. { to be able to remove the first and last instruction of }
  588. { movl %reg1, %reg2 }
  589. { <operations on %reg2> (replacing reg2 with reg1 here) }
  590. { movl %reg2, %reg1 }
  591. { %reg2 must not be use afterwards (it can be as the }
  592. { result of a peepholeoptimization) }
  593. removeLast := removeLast and sequenceEnd;
  594. newRegModified :=
  595. newRegModified or
  596. (not(regLoadedWithNewValue(newReg,true,paicpu(endP))) and
  597. RegModifiedByInstruction(newReg,endP));
  598. orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
  599. sequenceEnd := SequenceEnd and
  600. (removeLast or
  601. { since newReg will be replaced by orgReg, we can't allow that newReg }
  602. { gets modified if orgReg is still read afterwards (since after }
  603. { replacing, this would mean that orgReg first gets modified and then }
  604. { gets read in the assumption it still contains the unmodified value) }
  605. not(newRegModified and orgRegRead)) (* and
  606. { since newReg will be replaced by orgReg, we can't allow that newReg }
  607. { gets modified if orgRegCanBeModified = false }
  608. (orgRegCanBeModified or not(newRegModified)) *);
  609. tmpResult :=
  610. not(removeLast) and
  611. not(newRegModified and orgRegRead) and
  612. (* (orgRegCanBeModified or not(newRegModified)) and *)
  613. (endP^.typ = ait_instruction) and
  614. not(paicpu(endP)^.is_jmp) and
  615. NoHardCodedRegs(paicpu(endP),orgReg,newReg) and
  616. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  617. not RegModifiedByInstruction(orgReg,endP);
  618. end;
  619. end;
  620. sequenceEnd := sequenceEnd and
  621. (removeLast or
  622. (orgRegCanBeModified or not(newRegModified))) and
  623. (not(assigned(endp)) or
  624. not(endp^.typ = ait_instruction) or
  625. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  626. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  627. not(newRegModified and
  628. (orgReg in PPaiProp(endP^.optInfo)^.usedRegs) and
  629. not(RegLoadedWithNewValue(orgReg,true,paicpu(endP))))));
  630. if SequenceEnd then
  631. begin
  632. {$ifdef replaceregdebug}
  633. hp := new(pai_asm_comment,init(strpnew(
  634. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  635. ' from here...')));
  636. hp^.next := p;
  637. hp^.previous := p^.previous;
  638. p^.previous := hp;
  639. if assigned(hp^.previous) then
  640. hp^.previous^.next := hp;
  641. hp := new(pai_asm_comment,init(strpnew(
  642. 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  643. ' till here')));
  644. hp^.next := endp^.next;
  645. hp^.previous := endp;
  646. endp^.next := hp;
  647. if assigned(hp^.next) then
  648. hp^.next^.previous := hp;
  649. {$endif replaceregdebug}
  650. replaceReg := true;
  651. returnEndP := endP;
  652. getNextInstruction(p,hp);
  653. while hp <> endP do
  654. begin
  655. if not(PPaiProp(hp^.optInfo)^.canBeRemoved) and
  656. (hp^.typ = ait_instruction) then
  657. DoReplaceReg(orgReg,newReg,paicpu(hp));
  658. GetNextInstruction(hp,hp)
  659. end;
  660. if assigned(endp) and (endp^.typ = ait_instruction) then
  661. DoReplaceReadReg(orgReg,newReg,paicpu(endP));
  662. { the replacing stops either at the moment that }
  663. { a) the newreg gets loaded with a new value (one not depending on the }
  664. { current value of newreg) }
  665. { b) newreg is completely replaced in this sequence and it's current value }
  666. { isn't used anymore }
  667. { In case b, the newreg was completely replaced by oldreg, so it's contents }
  668. { are unchanged compared the start of this sequence, so restore them }
  669. If removeLast or
  670. RegLoadedWithNewValue(newReg,true,endP) then
  671. GetLastInstruction(endP,hp)
  672. else hp := endP;
  673. if removeLast or
  674. (p <> endp) or
  675. not RegLoadedWithNewValue(newReg,true,endP) then
  676. RestoreRegContentsTo(newReg, c ,p, hp);
  677. { In both case a and b, it is possible that the new register was modified }
  678. { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
  679. { oldreg's contents have been changed. To take this into account, we simply }
  680. { set the contents of orgreg to "unknown" after this sequence }
  681. if newRegModified then
  682. ClearRegContentsFrom(orgReg,p,hp);
  683. if removeLast then
  684. ppaiprop(endP^.optinfo)^.canBeRemoved := true;
  685. allocRegBetween(asml,orgReg,p,endP);
  686. end
  687. {$ifdef replaceregdebug}
  688. else
  689. begin
  690. hp := new(pai_asm_comment,init(strpnew(
  691. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  692. ' from here...')));
  693. hp^.previous := p^.previous;
  694. hp^.next := p;
  695. p^.previous := hp;
  696. if assigned(hp^.previous) then
  697. hp^.previous^.next := hp;
  698. hp := new(pai_asm_comment,init(strpnew(
  699. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  700. ' failed here')));
  701. hp^.next := endp^.next;
  702. hp^.previous := endp;
  703. endp^.next := hp;
  704. if assigned(hp^.next) then
  705. hp^.next^.previous := hp;
  706. end;
  707. {$endif replaceregdebug}
  708. End;
  709. Function FindRegWithConst(p: Pai; size: topsize; l: longint; Var Res: TRegister): Boolean;
  710. {Finds a register which contains the constant l}
  711. Var Counter: TRegister;
  712. {$ifdef testing}
  713. hp: pai;
  714. {$endif testing}
  715. tmpresult: boolean;
  716. Begin
  717. Counter := R_NO;
  718. repeat
  719. inc(counter);
  720. tmpresult := (ppaiprop(p^.optInfo)^.regs[counter].typ in
  721. [con_const,con_noRemoveConst]) and
  722. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.opsize = size) and
  723. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].typ = top_const) and
  724. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].val = l);
  725. {$ifdef testing}
  726. if (ppaiprop(p^.optInfo)^.regs[counter].typ in [con_const,con_noRemoveConst]) then
  727. begin
  728. hp := new(pai_asm_comment,init(strpnew(
  729. 'checking const load of '+tostr(l)+' here...')));
  730. hp^.next := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod;
  731. hp^.previous := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous;
  732. PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
  733. if assigned(hp^.previous) then
  734. hp^.previous^.next := hp;
  735. end;
  736. {$endif testing}
  737. until tmpresult or (Counter = R_EDI);
  738. res := counter;
  739. FindRegWithConst := tmpResult;
  740. End;
  741. procedure removePrevNotUsedLoad(p: pai; reg: tRegister; check: boolean);
  742. { If check = true, it means the procedure has to check whether it isn't }
  743. { possible that the contents are still used after p (used when removing }
  744. { instructions because of a "call"), otherwise this is not necessary }
  745. { (e.g. when you have a "mov 8(%ebp),%eax", you can be sure the previous }
  746. { value of %eax isn't used anymore later on) }
  747. var
  748. hp1: pai;
  749. begin
  750. if getLastInstruction(p,hp1) then
  751. with ppaiprop(hp1^.optInfo)^.regs[reg] do
  752. if (typ in [con_ref,con_invalid]) and
  753. (nrOfMods = 1) and
  754. (rState = ppaiprop(startmod^.optInfo)^.regs[reg].rState) and
  755. (not(check) or
  756. (not(regInInstruction(reg,p)) and
  757. (not(reg in usableregs) and
  758. (startmod^.typ = ait_instruction) and
  759. ((paicpu(startmod)^.opcode = A_MOV) or
  760. (paicpu(startmod)^.opcode = A_MOVZX) or
  761. (paicpu(startmod)^.opcode = A_MOVSX)) and
  762. (paicpu(startmod)^.oper[0].typ = top_ref) and
  763. (paicpu(startmod)^.oper[0].ref^.base = stack_pointer)) or
  764. not(reg in ppaiprop(hp1^.optInfo)^.usedRegs) or
  765. findRegDealloc(reg,p))) then
  766. ppaiprop(startMod^.optInfo)^.canBeRemoved := true;
  767. end;
  768. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  769. {marks the instructions that can be removed by RemoveInstructs. They're not
  770. removed immediately because sometimes an instruction needs to be checked in
  771. two different sequences}
  772. Var Cnt, Cnt2: Longint;
  773. p, hp1, hp2: Pai;
  774. hp3, hp4: pai;
  775. hp5 : pai;
  776. RegInfo: TRegInfo;
  777. RegCounter: TRegister;
  778. TmpState: Byte;
  779. Begin
  780. p := First;
  781. SkipHead(p);
  782. First := p;
  783. While (p <> Last) Do
  784. Begin
  785. Case p^.typ Of
  786. ait_align:
  787. if not(pai_align(p)^.use_op) then
  788. SetAlignReg(p);
  789. ait_instruction:
  790. Begin
  791. Case Paicpu(p)^.opcode Of
  792. A_CALL:
  793. for regCounter := R_EAX to R_EBX do
  794. removePrevNotUsedLoad(p,regCounter,true);
  795. A_CLD: If GetLastInstruction(p, hp1) And
  796. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  797. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  798. A_MOV, A_MOVZX, A_MOVSX:
  799. Begin
  800. Case Paicpu(p)^.oper[0].typ Of
  801. Top_Ref:
  802. Begin {destination is always a register in this case}
  803. With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  804. Begin
  805. If (p = StartMod) And
  806. GetLastInstruction (p, hp1) And
  807. (hp1^.typ <> ait_marker) Then
  808. {so we don't try to check a sequence when p is the first instruction of the block}
  809. begin
  810. {$ifdef csdebug}
  811. hp5 := new(pai_asm_comment,init(strpnew(
  812. 'cse checking '+att_reg2str[Reg32(Paicpu(p)^.oper[1].reg)])));
  813. insertLLItem(asml,p,p^.next,hp5);
  814. {$endif csdebug}
  815. If CheckSequence(p, Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
  816. (Cnt > 0) Then
  817. Begin
  818. hp1 := nil;
  819. { although it's perfectly ok to remove an instruction which doesn't contain }
  820. { the register that we've just checked (CheckSequence takes care of that), }
  821. { the sequence containing this other register should also be completely }
  822. { checked and removed, otherwise we may get situations like this: }
  823. { }
  824. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  825. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  826. { movl 8(%edx), %edx movl 8(%edx), %edx }
  827. { movl (%eax), eax movl (%eax), eax }
  828. { cmpl %eax, %edx cmpl %eax, %edx }
  829. { jnz l123 getting converted to jnz l123 }
  830. { movl 12(%ebp), %edx movl 4(%eax), eax }
  831. { movl 16(%ebp), %eax }
  832. { movl 8(%edx), %edx }
  833. { movl 4(%eax), eax }
  834. hp2 := p;
  835. Cnt2 := 1;
  836. While Cnt2 <= Cnt Do
  837. Begin
  838. If (hp1 = nil) And
  839. Not(RegInInstruction(Paicpu(hp2)^.oper[1].reg, p)) And
  840. ((p^.typ = ait_instruction) And
  841. ((paicpu(p)^.OpCode = A_MOV) or
  842. (paicpu(p)^.opcode = A_MOVZX) or
  843. (paicpu(p)^.opcode = A_MOVSX)) And
  844. (paicpu(p)^.Oper[0].typ = top_ref)) Then
  845. hp1 := p;
  846. {$ifndef noremove}
  847. if regInInstruction(Paicpu(hp2)^.oper[1].reg,p) then
  848. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  849. {$endif noremove}
  850. Inc(Cnt2);
  851. GetNextInstruction(p, p);
  852. End;
  853. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  854. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  855. {hp4 is used to get the contents of the registers before the sequence}
  856. GetLastInstruction(hp2, hp4);
  857. {$IfDef CSDebug}
  858. For RegCounter := R_EAX To R_EDI Do
  859. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  860. Begin
  861. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  862. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  863. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  864. End;
  865. {$EndIf CSDebug}
  866. { If some registers were different in the old and the new sequence, move }
  867. { the contents of those old registers to the new ones }
  868. For RegCounter := R_EAX To R_EDI Do
  869. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  870. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  871. Begin
  872. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  873. PPaiProp(hp4^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,hp2);
  874. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  875. {old reg new reg}
  876. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  877. Begin
  878. getLastInstruction(p,hp3);
  879. If not(regCounter in usableRegs + [R_EDI,R_ESI]) or
  880. not ReplaceReg(asmL,RegInfo.New2OldReg[RegCounter],
  881. regCounter,hp3,
  882. PPaiProp(hp4^.optInfo)^.Regs[regCounter],true,hp5) then
  883. begin
  884. hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
  885. {old reg new reg}
  886. RegInfo.New2OldReg[RegCounter], RegCounter));
  887. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
  888. end
  889. End
  890. Else
  891. { imagine the following code: }
  892. { normal wrong optimized }
  893. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  894. { movl (%eax), %eax movl (%eax), %eax }
  895. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  896. { jne l1 jne l1 }
  897. { movl 8(%ebp), %eax }
  898. { movl (%eax), %edi movl %eax, %edi }
  899. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  900. { movl 8(%ebp), %eax }
  901. { pushl 70(%eax) pushl 70(%eax) }
  902. { }
  903. { The error is that at the moment that the last instruction is executed, }
  904. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  905. { registers that are completely removed from a sequence (= registers in }
  906. { RegLoadedForRef, have to be changed to their contents from before the }
  907. { sequence. }
  908. If RegCounter in RegInfo.RegsLoadedForRef Then
  909. Begin
  910. {load Cnt2 with the total number of instructions of this sequence}
  911. Cnt2 := PPaiProp(hp4^.OptInfo)^.
  912. Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
  913. hp3 := hp2;
  914. For Cnt := 1 to Pred(Cnt2) Do
  915. GetNextInstruction(hp3, hp3);
  916. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  917. GetNextInstruction(hp3, hp3);
  918. {$ifdef csdebug}
  919. Writeln('Cnt2: ',Cnt2);
  920. hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
  921. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  922. {$endif csdebug}
  923. hp3 := hp2;
  924. {first change the contents of the register inside the sequence}
  925. For Cnt := 1 to Cnt2 Do
  926. Begin
  927. {save the WState of the last pai object of the sequence for later use}
  928. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  929. {$ifdef csdebug}
  930. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '
  931. +tostr(tmpstate))));
  932. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  933. {$endif csdebug}
  934. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  935. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  936. GetNextInstruction(hp3, hp3);
  937. End;
  938. {here, hp3 = p = Pai object right after the sequence, TmpState = WState of
  939. RegCounter at the last Pai object of the sequence}
  940. GetLastInstruction(hp3, hp3);
  941. While GetNextInstruction(hp3, hp3) And
  942. (PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState
  943. = TmpState) Do
  944. {$ifdef csdebug}
  945. begin
  946. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '+
  947. tostr(PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState))));
  948. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  949. {$endif csdebug}
  950. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  951. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  952. {$ifdef csdebug}
  953. end;
  954. {$endif csdebug}
  955. {$ifdef csdebug}
  956. hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  957. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  958. {$endif csdebug}
  959. End;
  960. End;
  961. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  962. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  963. If hp1 <> nil Then
  964. p := hp1;
  965. Continue;
  966. End
  967. Else
  968. If (PPaiProp(p^.OptInfo)^.
  969. regs[reg32(paicpu(p)^.oper[1].reg)].typ
  970. in [con_ref,con_noRemoveRef]) and
  971. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  972. if (cnt > 0) then
  973. begin
  974. hp2 := p;
  975. Cnt2 := 1;
  976. While Cnt2 <= Cnt Do
  977. Begin
  978. If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Then
  979. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  980. Inc(Cnt2);
  981. GetNextInstruction(p, p);
  982. End;
  983. Continue;
  984. End
  985. else
  986. begin
  987. { Fix for web bug 972 }
  988. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  989. cnt := PPaiProp(p^.optInfo)^.Regs[regCounter].nrOfMods;
  990. hp3 := p;
  991. for cnt2 := 1 to cnt do
  992. if not(regModifiedByInstruction(regCounter,hp3) and
  993. not(PPaiProp(hp3^.optInfo)^.canBeRemoved)) then
  994. getNextInstruction(hp3,hp3)
  995. else
  996. break;
  997. getLastInstruction(p,hp4);
  998. RestoreRegContentsTo(regCounter,
  999. PPaiProp(hp4^.optInfo)^.Regs[regCounter],
  1000. p,hp3);
  1001. end;
  1002. End;
  1003. End;
  1004. if not ppaiprop(p^.optinfo)^.canBeRemoved and
  1005. not regInRef(reg32(paicpu(p)^.oper[1].reg),
  1006. paicpu(p)^.oper[0].ref^) then
  1007. removePrevNotUsedLoad(p,reg32(paicpu(p)^.oper[1].reg),false);
  1008. End;
  1009. top_Reg:
  1010. { try to replace the new reg with the old reg }
  1011. if not(PPaiProp(p^.optInfo)^.canBeRemoved) and
  1012. { only remove if we're not storing something in a regvar }
  1013. (paicpu(p)^.oper[1].reg in (usableregs+[R_EDI])) and
  1014. (paicpu(p)^.opcode = A_MOV) and
  1015. getLastInstruction(p,hp4) then
  1016. begin
  1017. case paicpu(p)^.oper[1].typ of
  1018. top_Reg:
  1019. { we only have to start replacing from the instruction after the mov, }
  1020. { but replacereg only starts with getnextinstruction(p,p) }
  1021. if ReplaceReg(asmL,paicpu(p)^.oper[0].reg,
  1022. paicpu(p)^.oper[1].reg,p,
  1023. PPaiProp(hp4^.optInfo)^.Regs[paicpu(p)^.oper[1].reg],false,hp1) then
  1024. begin
  1025. PPaiProp(p^.optInfo)^.canBeRemoved := true;
  1026. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,
  1027. PPaiProp(p^.optInfo)^.regs[paicpu(p)^.oper[0].reg].startMod,
  1028. hp1);
  1029. end;
  1030. end
  1031. end;
  1032. top_symbol,Top_Const:
  1033. Begin
  1034. Case Paicpu(p)^.oper[1].typ Of
  1035. Top_Reg:
  1036. Begin
  1037. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  1038. If GetLastInstruction(p, hp1) Then
  1039. With PPaiProp(hp1^.OptInfo)^.Regs[regCounter] Do
  1040. if (typ in [con_const,con_noRemoveConst]) and
  1041. (paicpu(startMod)^.opsize >= paicpu(p)^.opsize) and
  1042. opsequal(paicpu(StartMod)^.oper[0],paicpu(p)^.oper[0]) Then
  1043. begin
  1044. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  1045. allocRegBetween(asmL,regCounter,startMod,p);
  1046. end;
  1047. End;
  1048. Top_Ref:
  1049. if (paicpu(p)^.oper[0].typ = top_const) and
  1050. getLastInstruction(p,hp1) and
  1051. findRegWithConst(hp1,paicpu(p)^.opsize,paicpu(p)^.oper[0].val,regCounter) then
  1052. begin
  1053. paicpu(p)^.loadreg(0,regCounter);
  1054. allocRegBetween(AsmL,reg32(regCounter),
  1055. PPaiProp(hp1^.optinfo)^.regs[regCounter].startMod,p);
  1056. end;
  1057. End;
  1058. End;
  1059. End;
  1060. End;
  1061. A_STD: If GetLastInstruction(p, hp1) And
  1062. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  1063. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1064. End
  1065. End;
  1066. End;
  1067. GetNextInstruction(p, p);
  1068. End;
  1069. End;
  1070. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  1071. { Removes the marked instructions and disposes the PPaiProps of the other }
  1072. { instructions }
  1073. Var p, hp1: Pai;
  1074. begin
  1075. p := First;
  1076. While (p <> Last) Do
  1077. Begin
  1078. If (p^.typ = ait_marker) and
  1079. (pai_marker(p)^.kind in [noPropInfoStart,noPropInfoEnd]) then
  1080. begin
  1081. hp1 := pai(p^.next);
  1082. asmL^.remove(p);
  1083. dispose(p,done);
  1084. p := hp1
  1085. end
  1086. else
  1087. {$ifndef noinstremove}
  1088. if assigned(p^.optInfo) and
  1089. PPaiProp(p^.optInfo)^.canBeRemoved then
  1090. begin
  1091. {$IfDef TP}
  1092. Dispose(PPaiProp(p^.OptInfo));
  1093. {$EndIf}
  1094. hp1 := pai(p^.next);
  1095. AsmL^.Remove(p);
  1096. Dispose(p, Done);
  1097. p := hp1;
  1098. End
  1099. Else
  1100. {$endif noinstremove}
  1101. Begin
  1102. {$IfDef TP}
  1103. if assigned(p^.optInfo) then
  1104. Dispose(PPaiProp(p^.OptInfo));
  1105. {$EndIf TP}
  1106. p^.OptInfo := nil;
  1107. p := pai(p^.next);;
  1108. End;
  1109. End;
  1110. {$IfNDef TP}
  1111. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  1112. {$EndIf TP}
  1113. End;
  1114. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  1115. Begin
  1116. DoCSE(AsmL, First, Last);
  1117. RemoveInstructs(AsmL, First, Last);
  1118. End;
  1119. End.
  1120. {
  1121. $Log$
  1122. Revision 1.4 2000-07-21 15:19:54 jonas
  1123. * daopt386: changes to getnextinstruction/getlastinstruction so they
  1124. ignore labels who have is_addr set
  1125. + daopt386/csopt386: remove loads of registers which are overwritten
  1126. before their contents are used (especially usefull for removing superfluous
  1127. maybe_loadesi outputs and push/pops transformed by below optimization
  1128. + popt386: transform pop/pop/pop/.../push/push/push to sequences of
  1129. 'movl x(%esp),%reg' (only active when compiling a go32v2 compiler
  1130. currently because I don't know whether it's safe to do this under Win32/
  1131. Linux (because of problems we had when using esp as frame pointer on
  1132. those os'es)
  1133. Revision 1.3 2000/07/14 05:11:48 michael
  1134. + Patch to 1.1
  1135. Revision 1.2 2000/07/13 11:32:39 michael
  1136. + removed logs
  1137. }