csopt386.pas 46 KB

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