csopt386.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. {
  2. $Id$
  3. Copyright (c) 1997-98 by Jonas Maebe
  4. This unit contains the common subexpression elimination procedure.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit CSOpt386;
  19. {$ifdef newOptimizations}
  20. {$define foropt}
  21. {$define replacereg}
  22. {$define arithopt}
  23. {$define foldarithops}
  24. {$endif newOptimizations}
  25. Interface
  26. Uses aasm;
  27. {Procedure CSOpt386(First, Last: Pai);}
  28. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  29. Implementation
  30. Uses
  31. CObjects, verbose, hcodegen, globals,cpubase,cpuasm,DAOpt386;
  32. {
  33. Function PaiInSequence(P: Pai; Const Seq: TContent): Boolean;
  34. Var P1: Pai;
  35. Counter: Byte;
  36. TmpResult: Boolean;
  37. Begin
  38. TmpResult := False;
  39. P1 := Seq.StartMod;
  40. Counter := 1;
  41. While Not(TmpResult) And
  42. (Counter <= Seq.NrOfMods) Do
  43. Begin
  44. If (P = P1) Then TmpResult := True;
  45. Inc(Counter);
  46. p1 := Pai(p1^.Next);
  47. End;
  48. PaiInSequence := TmpResult;
  49. End;
  50. }
  51. Function CheckSequence(p: Pai; Reg: TRegister; Var Found: Longint; Var RegInfo: TRegInfo): Boolean;
  52. {checks whether the current instruction sequence (starting with p) and the
  53. one between StartMod and EndMod of Reg are the same. If so, the number of
  54. instructions that match is stored in Found and true is returned, otherwise
  55. Found holds the number of instructions between StartMod and EndMod and false
  56. is returned}
  57. Var hp2, hp3{, EndMod}: Pai;
  58. PrevNonRemovablePai: Pai;
  59. Cnt, OldNrOfMods: Longint;
  60. OrgRegInfo, HighRegInfo: TRegInfo;
  61. HighFound, OrgRegFound: Byte;
  62. RegCounter: TRegister;
  63. OrgRegResult: Boolean;
  64. TmpResult: Boolean;
  65. TmpState: Byte;
  66. Begin {CheckSequence}
  67. Reg := Reg32(Reg);
  68. TmpResult := False;
  69. FillChar(OrgRegInfo, SizeOf(OrgRegInfo), 0);
  70. OrgRegFound := 0;
  71. HighFound := 0;
  72. OrgRegResult := False;
  73. RegCounter := R_EAX;
  74. GetLastInstruction(p, PrevNonRemovablePai);
  75. While (RegCounter <= R_EDI) And
  76. (PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].Typ <> Con_Ref) Do
  77. Inc(RegCounter);
  78. While (RegCounter <= R_EDI) Do
  79. Begin
  80. FillChar(RegInfo, SizeOf(RegInfo), 0);
  81. RegInfo.NewRegsEncountered := [procinfo^.FramePointer, R_ESP];
  82. RegInfo.OldRegsEncountered := RegInfo.NewRegsEncountered;
  83. RegInfo.New2OldReg[procinfo^.FramePointer] := procinfo^.FramePointer;
  84. RegInfo.New2OldReg[R_ESP] := R_ESP;
  85. Found := 0;
  86. hp2 := PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].StartMod;
  87. If (PrevNonRemovablePai <> PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].StartMod)
  88. Then OldNrOfMods := PPaiProp(PrevNonRemovablePai^.OptInfo)^.Regs[RegCounter].NrOfMods
  89. Else OldNrOfMods := 1;
  90. hp3 := p;
  91. While (Found <> OldNrOfMods) And
  92. { old new }
  93. InstructionsEquivalent(hp2, hp3, RegInfo) Do
  94. Begin
  95. GetNextInstruction(hp2, hp2);
  96. GetNextInstruction(hp3, hp3);
  97. Inc(Found)
  98. End;
  99. If (Found <> OldNrOfMods) 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. { sometimes, registers in RegsLoadedForRef (which normally aren't/shouldn't }
  159. { be used anymore after the sequence, are still used nevertheless (when }
  160. { range checking is on for instance, because this is not "normal" generated }
  161. { code, but more or less manually inserted) }
  162. {$ifndef fpc}
  163. If TmpResult Then
  164. {$else fpc}
  165. If CheckSequence And (Found > 0) Then
  166. {$endif fpc}
  167. For RegCounter := R_EAX to R_EDI Do
  168. If (RegCounter in RegInfo.RegsLoadedForRef) And
  169. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  170. Begin
  171. OldNrOfMods := PPaiProp(PrevNonRemovablePai^.OptInfo)^.
  172. Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
  173. hp2 := p;
  174. For Cnt := 1 to Pred(OldNrOfMods) Do
  175. GetNextInstruction(hp2, hp2);
  176. { hp2 now containts the last instruction of the sequence }
  177. { get the writestate at this point of the register in TmpState }
  178. TmpState := PPaiProp(hp2^.OptInfo)^.Regs[RegCounter].WState;
  179. { hp3 := first instruction after the sequence }
  180. GetNextInstruction(hp2, hp2);
  181. { now, even though reg is in RegsLoadedForRef, sometimes it's still used }
  182. { afterwards. It is not if either it is not in usedregs anymore after the }
  183. { sequence, or if it is loaded with a new value right after the sequence }
  184. If (TmpState = PPaiProp(hp2^.OptInfo)^.Regs[RegCounter].WState) And
  185. (RegCounter in PPaiProp(hp2^.OptInfo)^.UsedRegs) Then
  186. { it is still used, so remove it from RegsLoadedForRef }
  187. Begin
  188. {$ifdef regrefdebug}
  189. hp3 := new(pai_asm_comment,init(strpnew(att_reg2str[regcounter]+
  190. ' removed from regsloadedforref')));
  191. hp3^.fileinfo := hp2^.fileinfo;
  192. hp3^.next := hp2^.next;
  193. hp3^.previous := hp2;
  194. hp2^.next := hp3;
  195. If assigned(hp3^.next) then
  196. Pai(hp3^.next)^.previous := hp3;
  197. {$endif regrefdebug}
  198. Exclude(RegInfo.RegsLoadedForRef,RegCounter);
  199. End;
  200. End;
  201. {$ifndef fpc}
  202. CheckSequence := TmpResult;
  203. {$endif fpc}
  204. End; {CheckSequence}
  205. Procedure AllocRegBetween(AsmL: PAasmOutput; Reg: TRegister; p1, p2: Pai);
  206. { allocates register Reg between (and including) instructions p1 and p2 }
  207. { the type of p1 and p2 must not be in SkipInstr }
  208. var hp: pai;
  209. Begin
  210. If not(assigned(p1)) Then
  211. { this happens with registers which are loaded implicitely, outside the }
  212. { current block (e.g. esi with self) }
  213. exit;
  214. Repeat
  215. If Assigned(p1^.OptInfo) Then
  216. Include(PPaiProp(p1^.OptInfo)^.UsedRegs,Reg);
  217. p1 := Pai(p1^.next);
  218. Repeat
  219. While (p1^.typ in (SkipInstr-[ait_regalloc])) Do
  220. p1 := Pai(p1^.next);
  221. { remove all allocation/deallocation info about the register in between }
  222. If (p1^.typ = ait_regalloc) Then
  223. If (PaiRegAlloc(p1)^.Reg = Reg) Then
  224. Begin
  225. hp := Pai(p1^.Next);
  226. AsmL^.Remove(p1);
  227. Dispose(p1, Done);
  228. p1 := hp;
  229. End
  230. Else p1 := Pai(p1^.next);
  231. Until Not(p1^.typ in SkipInstr);
  232. Until p1 = p2;
  233. End;
  234. {$ifdef alignreg}
  235. Procedure SetAlignReg(p: Pai);
  236. Const alignSearch = 12;
  237. var regsUsable: TRegSet;
  238. prevInstrCount, nextInstrCount: Longint;
  239. prevState, nextWState,nextRState: Array[R_EAX..R_EDI] of byte;
  240. regCounter, lastRemoved: TRegister;
  241. prev, next: Pai;
  242. {$ifdef alignregdebug}
  243. temp: Pai;
  244. {$endif alignregdebug}
  245. begin
  246. regsUsable := [R_EAX,R_ECX,R_EDX,R_EBX,{R_ESP,R_EBP,}R_ESI,R_EDI];
  247. for regCounter := R_EAX to R_EDI do
  248. begin
  249. prevState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  250. nextWState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].wState;
  251. nextRState[regCounter] := PPaiProp(p^.optInfo)^.Regs[regCounter].rState;
  252. end;
  253. getLastInstruction(p,prev);
  254. getNextInstruction(p,next);
  255. lastRemoved := pai_align(p)^.reg;
  256. nextInstrCount := 0;
  257. prevInstrCount := 0;
  258. while ((assigned(prev) and
  259. assigned(prev^.optInfo) and
  260. (prevInstrCount < alignSearch)) or
  261. (assigned(next) and
  262. assigned(next^.optInfo) and
  263. (nextInstrCount < alignSearch))) And
  264. (regsUsable <> []) do
  265. begin
  266. {$ifdef alignregdebug}
  267. if assigned(prev) then
  268. begin
  269. temp := new(pai_asm_comment,init(strpnew('got here')));
  270. temp^.next := prev^.next;
  271. temp^.previous := prev;
  272. prev^.next := temp;
  273. if assigned(temp^.next) then
  274. temp^.next^.previous := temp;
  275. end;
  276. {$endif alignregdebug}
  277. if assigned(prev) and assigned(prev^.optinfo) and
  278. (prevInstrCount < alignSearch) then
  279. begin
  280. if (prev^.typ = ait_instruction) And
  281. (insProp[PaiCpu(prev)^.opcode].ch[1] <> Ch_ALL) and
  282. (PaiCpu(prev)^.opcode <> A_JMP) then
  283. begin
  284. inc(prevInstrCount);
  285. for regCounter := R_EAX to R_EDI do
  286. begin
  287. if (regCounter in regsUsable) And
  288. (PPaiProp(prev^.optInfo)^.Regs[regCounter].wState <>
  289. prevState[regCounter]) then
  290. begin
  291. lastRemoved := regCounter;
  292. exclude(regsUsable,regCounter);
  293. {$ifdef alignregdebug}
  294. temp := new(pai_asm_comment,init(strpnew(
  295. att_reg2str[regCounter]+' removed')));
  296. temp^.next := prev^.next;
  297. temp^.previous := prev;
  298. prev^.next := temp;
  299. if assigned(temp^.next) then
  300. temp^.next^.previous := temp;
  301. if regsUsable = [] then
  302. begin
  303. temp := new(pai_asm_comment,init(strpnew(
  304. 'regsUsable empty here')));
  305. temp^.next := prev^.next;
  306. temp^.previous := prev;
  307. prev^.next := temp;
  308. if assigned(temp^.next) then
  309. temp^.next^.previous := temp;
  310. end;
  311. {$endif alignregdebug}
  312. end;
  313. prevState[regCounter] :=
  314. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState;
  315. end;
  316. getLastInstruction(prev,prev);
  317. end
  318. else
  319. If GetLastInstruction(prev,prev) and
  320. assigned(prev^.optinfo) then
  321. for regCounter := R_EAX to R_EDI do
  322. prevState[regCounter] :=
  323. PPaiProp(prev^.optInfo)^.Regs[regCounter].wState
  324. end;
  325. if assigned(next) and assigned(next^.optInfo) and
  326. (nextInstrCount < alignSearch) then
  327. begin
  328. if (next^.typ = ait_instruction) and
  329. (insProp[PaiCpu(next)^.opcode].ch[1] <> Ch_ALL) and
  330. (PaiCpu(next)^.opcode <> A_JMP) then
  331. begin
  332. inc(nextInstrCount);
  333. for regCounter := R_EAX to R_EDI do
  334. begin
  335. if (regCounter in regsUsable) And
  336. ((PPaiProp(next^.optInfo)^.Regs[regCounter].wState <>
  337. nextWState[regCounter]) or
  338. (PPaiProp(next^.optInfo)^.Regs[regCounter].rState <>
  339. nextRState[regCounter])) Then
  340. begin
  341. lastRemoved := regCounter;
  342. exclude(regsUsable,regCounter);
  343. {$ifdef alignregdebug}
  344. temp := new(pai_asm_comment,init(strpnew(
  345. att_reg2str[regCounter]+' removed')));
  346. temp^.next := next^.next;
  347. temp^.previous := next;
  348. next^.next := temp;
  349. if assigned(temp^.next) then
  350. temp^.next^.previous := temp;
  351. if regsUsable = [] then
  352. begin
  353. temp := new(pai_asm_comment,init(strpnew(
  354. 'regsUsable empty here')));
  355. temp^.next := next^.next;
  356. temp^.previous := next;
  357. next^.next := temp;
  358. if assigned(temp^.next) then
  359. temp^.next^.previous := temp;
  360. end;
  361. {$endif alignregdebug}
  362. end;
  363. nextWState[regCounter] :=
  364. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  365. nextRState[regCounter] :=
  366. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  367. end
  368. end
  369. else
  370. for regCounter := R_EAX to R_EDI do
  371. begin
  372. nextWState[regCounter] :=
  373. PPaiProp(next^.optInfo)^.Regs[regCounter].wState;
  374. nextRState[regCounter] :=
  375. PPaiProp(next^.optInfo)^.Regs[regCounter].rState;
  376. end;
  377. getNextInstruction(next,next);
  378. end;
  379. end;
  380. if regsUsable <> [] then
  381. for regCounter := R_EAX to R_EDI do
  382. if regCounter in regsUsable then
  383. begin
  384. lastRemoved := regCounter;
  385. break
  386. end;
  387. {$ifdef alignregdebug}
  388. next := new(pai_asm_comment,init(strpnew(att_reg2str[lastRemoved]+
  389. ' chosen as alignment register')));
  390. next^.next := p^.next;
  391. next^.previous := p;
  392. p^.next := next;
  393. if assigned(next^.next) then
  394. next^.next^.previous := next;
  395. {$endif alignregdebug}
  396. pai_align(p)^.reg := lastRemoved;
  397. End;
  398. {$endif alignreg}
  399. {$ifdef replacereg}
  400. function FindRegDealloc(reg: tregister; p: pai): boolean;
  401. { assumes reg is a 32bit register }
  402. begin
  403. findregdealloc := false;
  404. while assigned(p^.previous) and
  405. ((Pai(p^.previous)^.typ in (skipinstr+[ait_align])) or
  406. ((Pai(p^.previous)^.typ = ait_label) and
  407. not(Pai_Label(p^.previous)^.l^.is_used))) do
  408. begin
  409. p := pai(p^.previous);
  410. if (p^.typ = ait_regalloc) and
  411. (pairegalloc(p)^.reg = reg) then
  412. begin
  413. findregdealloc := not(pairegalloc(p)^.allocation);
  414. break;
  415. end;
  416. end
  417. end;
  418. function regLoadedWithNewValue(reg: tregister; hp: pai): boolean;
  419. { assumes reg is a 32bit register }
  420. var p: paicpu;
  421. begin
  422. p := paicpu(hp);
  423. regLoadedWithNewValue :=
  424. assigned(hp) and
  425. ((hp^.typ = ait_instruction) and
  426. (((p^.opcode = A_MOV) or
  427. (p^.opcode = A_MOVZX) or
  428. (p^.opcode = A_MOVSX) or
  429. (p^.opcode = A_LEA)) and
  430. (p^.oper[1].typ = top_reg) and
  431. (Reg32(p^.oper[1].reg) = reg)) or
  432. ((p^.opcode = A_POP) and
  433. (Reg32(p^.oper[0].reg) = reg)));
  434. end;
  435. Procedure RestoreRegContentsTo(reg: TRegister; const c: TContent; p: pai);
  436. var
  437. {$ifdef replaceregdebug}
  438. hp: pai;
  439. {$endif replaceregdebug}
  440. tmpState: byte;
  441. begin
  442. {$ifdef replaceregdebug}
  443. hp := new(pai_asm_comment,init(strpnew(
  444. 'restored '+att_reg2str[reg]+' with data from here...')));
  445. hp^.next := p;
  446. hp^.previous := p^.previous;
  447. p^.previous := hp;
  448. if assigned(hp^.previous) then
  449. hp^.previous^.next := hp;
  450. {$endif replaceregdebug}
  451. tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
  452. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  453. while getNextInstruction(p,p) and
  454. (PPaiProp(p^.optInfo)^.Regs[reg].wState = tmpState) do
  455. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  456. {$ifdef replaceregdebug}
  457. if assigned(p) then
  458. begin
  459. hp := new(pai_asm_comment,init(strpnew(
  460. 'restored '+att_reg2str[reg]+' till here...')));
  461. hp^.next := p;
  462. hp^.previous := p^.previous;
  463. p^.previous := hp;
  464. if assigned(hp^.previous) then
  465. hp^.previous^.next := hp;
  466. end;
  467. {$endif replaceregdebug}
  468. end;
  469. Procedure ClearRegContentsFrom(reg: TRegister; p: pai);
  470. var
  471. {$ifdef replaceregdebug}
  472. hp: pai;
  473. {$endif replaceregdebug}
  474. tmpState: byte;
  475. begin
  476. tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
  477. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  478. while getNextInstruction(p,p) and
  479. (PPaiProp(p^.optInfo)^.Regs[reg].wState = tmpState) do
  480. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  481. {$ifdef replaceregdebug}
  482. if assigned(p) then
  483. begin
  484. hp := new(pai_asm_comment,init(strpnew(
  485. 'cleared '+att_reg2str[reg]+' till here...')));
  486. hp^.next := p;
  487. hp^.previous := p^.previous;
  488. p^.previous := hp;
  489. if assigned(hp^.previous) then
  490. hp^.previous^.next := hp;
  491. end;
  492. {$endif replaceregdebug}
  493. end;
  494. function NoHardCodedRegs(p: paicpu): boolean;
  495. var chCount: byte;
  496. begin
  497. NoHardCodedRegs := true;
  498. with InsProp[p^.opcode] do
  499. for chCount := 1 to MaxCh do
  500. if Ch[chCount] in ([Ch_REAX..Ch_MEDI]-[Ch_RESP,Ch_WESP,Ch_RWESP]) then
  501. begin
  502. NoHardCodedRegs := false;
  503. break
  504. end;
  505. end;
  506. Procedure ChangeReg(var Reg: TRegister; orgReg, newReg: TRegister);
  507. begin
  508. if reg = newReg then
  509. reg := orgReg
  510. else if reg = regtoreg8(newReg) then
  511. reg := regtoreg8(orgReg)
  512. else if reg = regtoreg16(newReg) then
  513. reg := regtoreg16(orgReg);
  514. end;
  515. Procedure DoReplaceReg(orgReg,newReg: tregister; hp: paicpu);
  516. var opCount: byte;
  517. begin
  518. for opCount := 0 to 2 do
  519. with hp^.oper[opCount] Do
  520. case typ of
  521. top_reg: ChangeReg(reg,orgReg,newReg);
  522. top_ref:
  523. begin
  524. ChangeReg(ref^.base,orgReg,newReg);
  525. ChangeReg(ref^.index,orgReg,newReg);
  526. end;
  527. end;
  528. end;
  529. function RegSizesOK(oldReg,newReg: TRegister; p: paicpu): boolean;
  530. { oldreg and newreg must be 32bit components }
  531. var opCount: byte;
  532. begin
  533. RegSizesOK := true;
  534. if not(IsGP32reg(oldReg) and IsGP32Reg(newReg)) then
  535. begin
  536. for opCount := 0 to 2 do
  537. if (p^.oper[opCount].typ = top_reg) and
  538. (p^.oper[opCount].reg in [R_AL..R_DH]) then
  539. begin
  540. RegSizesOK := false;
  541. break
  542. end
  543. end;
  544. end;
  545. function RegReadByInstruction(reg: TRegister; hp: pai): boolean;
  546. { assumes hp doesn't modify registers implicitely (like div) }
  547. { and that reg is a 32bit register }
  548. var p: paicpu;
  549. opCount: byte;
  550. begin
  551. RegReadByInstruction := false;
  552. p := paicpu(hp);
  553. if hp^.typ <> ait_instruction then
  554. exit;
  555. for opCount := 0 to 2 do
  556. if (p^.oper[opCount].typ = top_ref) and
  557. RegInRef(reg,p^.oper[opCount].ref^) then
  558. begin
  559. RegReadByInstruction := true;
  560. exit
  561. end;
  562. for opCount := 1 to MaxCh do
  563. case InsProp[p^.opcode].Ch[opCount] of
  564. Ch_RWOp1,Ch_ROp1{$ifdef arithopt},Ch_MOp1{$endif}:
  565. if (p^.oper[0].typ = top_reg) and
  566. (reg32(p^.oper[0].reg) = reg) then
  567. begin
  568. RegReadByInstruction := true;
  569. exit
  570. end;
  571. Ch_RWOp2,Ch_ROp2{$ifdef arithopt},Ch_MOp2{$endif}:
  572. if (p^.oper[1].typ = top_reg) and
  573. (reg32(p^.oper[1].reg) = reg) then
  574. begin
  575. RegReadByInstruction := true;
  576. exit
  577. end;
  578. Ch_RWOp3,Ch_ROp3{$ifdef arithopt},Ch_MOp3{$endif}:
  579. if (p^.oper[2].typ = top_reg) and
  580. (reg32(p^.oper[2].reg) = reg) then
  581. begin
  582. RegReadByInstruction := true;
  583. exit
  584. end;
  585. end;
  586. end;
  587. procedure DoReplaceReadReg(orgReg,newReg: tregister; p: paicpu);
  588. var opCount: byte;
  589. begin
  590. for opCount := 0 to 2 do
  591. if p^.oper[opCount].typ = top_ref then
  592. begin
  593. ChangeReg(p^.oper[opCount].ref^.base,orgReg,newReg);
  594. ChangeReg(p^.oper[opCount].ref^.index,orgReg,newReg);
  595. end;
  596. for opCount := 1 to MaxCh do
  597. case InsProp[p^.opcode].Ch[opCount] of
  598. Ch_RWOp1,Ch_ROp1{$ifdef arithopt},Ch_MOp1{$endif}:
  599. if p^.oper[0].typ = top_reg then
  600. ChangeReg(p^.oper[0].reg,orgReg,newReg);
  601. Ch_RWOp2,Ch_ROp2{$ifdef arithopt},Ch_MOp2{$endif}:
  602. if p^.oper[1].typ = top_reg then
  603. ChangeReg(p^.oper[1].reg,orgReg,newReg);
  604. Ch_RWOp3,Ch_ROp3{$ifdef arithopt},Ch_MOp3{$endif}:
  605. if p^.oper[2].typ = top_reg then
  606. ChangeReg(p^.oper[2].reg,orgReg,newReg);
  607. end;
  608. end;
  609. function ReplaceReg(orgReg, newReg: TRegister; p: pai;
  610. const c: TContent): Boolean;
  611. { Tries to replace orgreg with newreg in all instructions coming after p }
  612. { until orgreg gets loaded with a new value. Returns true if successful, }
  613. { false otherwise. If successful, the contents of newReg are set to c, }
  614. { which should hold the contents of newReg before the current sequence }
  615. { started }
  616. var endP, hp: Pai;
  617. sequenceEnd, tmpResult, newRegModified, orgRegRead: Boolean;
  618. begin
  619. ReplaceReg := false;
  620. tmpResult := true;
  621. sequenceEnd := false;
  622. newRegModified := false;
  623. orgRegRead := false;
  624. endP := pai(p^.previous);
  625. { skip over the instructions that will be removed }
  626. while getNextInstruction(endP,hp) and
  627. assigned(hp) and
  628. PPaiProp(hp^.optInfo)^.canBeRemoved do
  629. endP := hp;
  630. while tmpResult and not sequenceEnd do
  631. begin
  632. tmpResult :=
  633. getNextInstruction(endP,endP);
  634. If tmpResult and
  635. { don't take into account instructions that will be removed }
  636. Not (PPaiProp(hp^.optInfo)^.canBeRemoved) then
  637. begin
  638. sequenceEnd :=
  639. RegLoadedWithNewValue(newReg,paicpu(endP)) or
  640. (GetNextInstruction(endp,hp) and
  641. FindRegDealloc(newReg,hp));
  642. newRegModified :=
  643. newRegModified or
  644. (not(sequenceEnd) and
  645. RegModifiedByInstruction(newReg,endP));
  646. orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
  647. sequenceEnd := SequenceEnd and not(newRegModified and orgRegRead);
  648. tmpResult :=
  649. not(newRegModified and orgRegRead) and
  650. (endP^.typ = ait_instruction) and
  651. not(paicpu(endP)^.is_jmp) and
  652. NoHardCodedRegs(paicpu(endP)) and
  653. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  654. not RegModifiedByInstruction(orgReg,endP);
  655. end;
  656. end;
  657. sequenceEnd := sequenceEnd and
  658. not(newRegModified and
  659. (orgReg in PPaiProp(endP^.optInfo)^.usedRegs) and
  660. not(RegLoadedWithNewValue(orgReg,paicpu(endP))));
  661. if SequenceEnd then
  662. begin
  663. {$ifdef replaceregdebug}
  664. hp := new(pai_asm_comment,init(strpnew(
  665. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  666. ' from here...')));
  667. hp^.next := p;
  668. hp^.previous := p^.previous;
  669. p^.previous := hp;
  670. if assigned(hp^.previous) then
  671. hp^.previous^.next := hp;
  672. hp := new(pai_asm_comment,init(strpnew(
  673. 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  674. ' till here')));
  675. hp^.next := endp^.next;
  676. hp^.previous := endp;
  677. endp^.next := hp;
  678. if assigned(hp^.next) then
  679. hp^.next^.previous := hp;
  680. {$endif replaceregdebug}
  681. ReplaceReg := true;
  682. hp := p;
  683. while hp <> endP do
  684. begin
  685. if not(PPaiProp(hp^.optInfo)^.canBeRemoved) and
  686. (hp^.typ = ait_instruction) then
  687. DoReplaceReg(orgReg,newReg,paicpu(hp));
  688. GetNextInstruction(hp,hp)
  689. end;
  690. if assigned(endp) and (endp^.typ = ait_instruction) then
  691. DoReplaceReadReg(orgReg,newReg,paicpu(endP));
  692. { the replacing stops either at the moment that }
  693. { a) the newreg gets loaded with a new value (one not depending on the }
  694. { current value of newreg) }
  695. { b) newreg is completely replaced in this sequence and it's current value }
  696. { isn't used anymore }
  697. { In case b, the newreg was completely replaced by oldreg, so it's contents }
  698. { are unchanged compared the start of this sequence, so restore them }
  699. if (p <> endp) or
  700. not RegModifiedByInstruction(newReg,endP) then
  701. RestoreRegContentsTo(newReg, c ,p);
  702. { In both case a and b, it is possible that the new register was modified }
  703. { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
  704. { oldreg's contents have been changed. To take this into account, we simply }
  705. { set the contents of orgreg to "unknown" after this sequence }
  706. if newRegModified then
  707. ClearRegContentsFrom(orgReg,p);
  708. end
  709. {$ifdef replaceregdebug}
  710. else
  711. begin
  712. hp := new(pai_asm_comment,init(strpnew(
  713. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  714. ' from here...')));
  715. hp^.previous := p^.previous;
  716. hp^.next := p;
  717. p^.previous := hp;
  718. if assigned(hp^.previous) then
  719. hp^.previous^.next := hp;
  720. hp := new(pai_asm_comment,init(strpnew(
  721. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  722. ' failed here')));
  723. hp^.next := endp^.next;
  724. hp^.previous := endp;
  725. endp^.next := hp;
  726. if assigned(hp^.next) then
  727. hp^.next^.previous := hp;
  728. end;
  729. {$endif replaceregdebug}
  730. End;
  731. {$endif replacereg}
  732. {$ifdef arithopt}
  733. Function FindRegWithConst(p: Pai; size: topsize; l: longint; Var Res: TRegister): Boolean;
  734. {Finds a register which contains the constant l}
  735. Var Counter: TRegister;
  736. {$ifdef testing}
  737. hp: pai;
  738. {$endif testing}
  739. tmpresult: boolean;
  740. Begin
  741. Counter := R_NO;
  742. repeat
  743. inc(counter);
  744. tmpresult := (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) and
  745. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.opsize = size) and
  746. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].val = l);
  747. {$ifdef testing}
  748. if (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) then
  749. begin
  750. hp := new(pai_asm_comment,init(strpnew(
  751. 'checking const load of '+tostr(l)+' here...')));
  752. hp^.next := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod;
  753. hp^.previous := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous;
  754. PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
  755. if assigned(hp^.previous) then
  756. hp^.previous^.next := hp;
  757. end;
  758. {$endif testing}
  759. until tmpresult or (Counter = R_EDI);
  760. res := counter;
  761. FindRegWithConst := tmpResult;
  762. End;
  763. {$endif arithopt}
  764. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  765. {marks the instructions that can be removed by RemoveInstructs. They're not
  766. removed immediately because sometimes an instruction needs to be checked in
  767. two different sequences}
  768. Var Cnt, Cnt2: Longint;
  769. p, hp1, hp2: Pai;
  770. hp3, hp4: Pai;
  771. {$ifdef csdebug}
  772. hp5: pai;
  773. {$endif csdebug}
  774. RegInfo: TRegInfo;
  775. RegCounter: TRegister;
  776. TmpState: Byte;
  777. Begin
  778. p := First;
  779. SkipHead(p);
  780. First := p;
  781. While (p <> Last) Do
  782. Begin
  783. Case p^.typ Of
  784. {$ifdef alignreg}
  785. ait_align:
  786. SetAlignReg(p);
  787. {$endif alignreg}
  788. ait_instruction:
  789. Begin
  790. Case Paicpu(p)^.opcode Of
  791. A_CLD: If GetLastInstruction(p, hp1) And
  792. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  793. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  794. A_MOV, A_MOVZX, A_MOVSX:
  795. Begin
  796. Case Paicpu(p)^.oper[0].typ Of
  797. Top_Ref:
  798. Begin {destination is always a register in this case}
  799. With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  800. Begin
  801. If (p = StartMod) And
  802. GetLastInstruction (p, hp1) And
  803. (hp1^.typ <> ait_marker)
  804. Then
  805. {so we don't try to check a sequence when p is the first instruction of the block}
  806. If CheckSequence(p, Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
  807. (Cnt > 0) Then
  808. Begin
  809. hp1 := nil;
  810. { although it's perfectly ok to remove an instruction which doesn't contain }
  811. { the register that we've just checked (CheckSequence takes care of that), }
  812. { the sequence containing this other register should also be completely }
  813. { checked and removed, otherwise we may get situations like this: }
  814. { }
  815. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  816. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  817. { movl 8(%edx), %edx movl 8(%edx), %edx }
  818. { movl (%eax), eax movl (%eax), eax }
  819. { cmpl %eax, %edx cmpl %eax, %edx }
  820. { jnz l123 getting converted to jnz l123 }
  821. { movl 12(%ebp), %edx movl 4(%eax), eax }
  822. { movl 16(%ebp), %eax }
  823. { movl 8(%edx), %edx }
  824. { movl 4(%eax), eax }
  825. hp2 := p;
  826. Cnt2 := 1;
  827. While Cnt2 <= Cnt Do
  828. Begin
  829. If (hp1 = nil) And
  830. Not(RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  831. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p)) And
  832. Not((p^.typ = ait_instruction) And
  833. (paicpu(p)^.OpCode = A_MOV) And
  834. (paicpu(p)^.Oper[0].typ = top_ref) And
  835. (PPaiProp(p^.OptInfo)^.Regs[Reg32(paicpu(p)^.Oper[1].reg)].NrOfMods
  836. <= (Cnt - Cnt2 + 1)))
  837. Then hp1 := p;
  838. {$ifndef noremove}
  839. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  840. {$endif noremove}
  841. Inc(Cnt2);
  842. GetNextInstruction(p, p);
  843. End;
  844. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  845. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  846. {hp4 is used to get the contents of the registers before the sequence}
  847. GetLastInstruction(hp2, hp4);
  848. {$IfDef CSDebug}
  849. For RegCounter := R_EAX To R_EDI Do
  850. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  851. Begin
  852. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  853. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  854. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  855. End;
  856. {$EndIf CSDebug}
  857. { If some registers were different in the old and the new sequence, move }
  858. { the contents of those old registers to the new ones }
  859. For RegCounter := R_EAX To R_EDI Do
  860. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  861. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  862. Begin
  863. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  864. PPaiProp(hp4^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,hp2);
  865. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  866. {old reg new reg}
  867. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  868. Begin
  869. {$ifdef replacereg}
  870. If not ReplaceReg(RegInfo.New2OldReg[RegCounter],
  871. regCounter,p,
  872. PPaiProp(hp4^.optInfo)^.Regs[regCounter]) then
  873. begin
  874. {$endif replacereg}
  875. hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
  876. {old reg new reg}
  877. RegInfo.New2OldReg[RegCounter], RegCounter));
  878. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
  879. {$ifdef replacereg}
  880. end
  881. {$endif replacereg}
  882. End
  883. Else
  884. { imagine the following code: }
  885. { normal wrong optimized }
  886. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  887. { movl (%eax), %eax movl (%eax), %eax }
  888. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  889. { jne l1 jne l1 }
  890. { movl 8(%ebp), %eax }
  891. { movl (%eax), %edi movl %eax, %edi }
  892. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  893. { movl 8(%ebp), %eax }
  894. { pushl 70(%eax) pushl 70(%eax) }
  895. { }
  896. { The error is that at the moment that the last instruction is executed, }
  897. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  898. { registers that are completely removed from a sequence (= registers in }
  899. { RegLoadedForRef, have to be changed to their contents from before the }
  900. { sequence. }
  901. If RegCounter in RegInfo.RegsLoadedForRef Then
  902. Begin
  903. {load Cnt2 with the total number of instructions of this sequence}
  904. Cnt2 := PPaiProp(hp4^.OptInfo)^.
  905. Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
  906. hp3 := hp2;
  907. For Cnt := 1 to Pred(Cnt2) Do
  908. GetNextInstruction(hp3, hp3);
  909. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  910. GetNextInstruction(hp3, hp3);
  911. {$ifdef csdebug}
  912. Writeln('Cnt2: ',Cnt2);
  913. hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
  914. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  915. {$endif csdebug}
  916. hp3 := hp2;
  917. {first change the contents of the register inside the sequence}
  918. For Cnt := 1 to Cnt2 Do
  919. Begin
  920. {save the WState of the last pai object of the sequence for later use}
  921. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  922. {$ifdef csdebug}
  923. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '
  924. +tostr(tmpstate))));
  925. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  926. {$endif csdebug}
  927. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  928. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  929. GetNextInstruction(hp3, hp3);
  930. End;
  931. {here, hp3 = p = Pai object right after the sequence, TmpState = WState of
  932. RegCounter at the last Pai object of the sequence}
  933. GetLastInstruction(hp3, hp3);
  934. While GetNextInstruction(hp3, hp3) And
  935. (PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState
  936. = TmpState) Do
  937. {$ifdef csdebug}
  938. begin
  939. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '+
  940. tostr(PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState))));
  941. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  942. {$endif csdebug}
  943. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  944. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  945. {$ifdef csdebug}
  946. end;
  947. {$endif csdebug}
  948. {$ifdef csdebug}
  949. hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  950. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  951. {$endif csdebug}
  952. End;
  953. End;
  954. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  955. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  956. If hp1 <> nil Then p := hp1;
  957. Continue;
  958. End
  959. Else
  960. If (Cnt > 0) And
  961. (PPaiProp(p^.OptInfo)^.
  962. Regs[Reg32(Paicpu(p)^.oper[1].reg)].Typ = Con_Ref) And
  963. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  964. Begin
  965. hp2 := p;
  966. Cnt2 := 1;
  967. While Cnt2 <= Cnt Do
  968. Begin
  969. If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  970. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p) Then
  971. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  972. Inc(Cnt2);
  973. GetNextInstruction(p, p);
  974. End;
  975. Continue;
  976. End;
  977. End;
  978. End;
  979. Top_Const:
  980. Begin
  981. Case Paicpu(p)^.oper[1].typ Of
  982. Top_Reg:
  983. Begin
  984. If GetLastInstruction(p, hp1) Then
  985. With PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  986. If (Typ = Con_Const) And
  987. (paicpu(startMod)^.opsize >= paicpu(p)^.opsize) and
  988. (paicpu(StartMod)^.oper[0].val = paicpu(p)^.oper[0].val) Then
  989. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  990. End;
  991. {$ifdef arithopt}
  992. Top_Ref:
  993. if getLastInstruction(p,hp1) and
  994. findRegWithConst(hp1,paicpu(p)^.opsize,paicpu(p)^.oper[0].val,regCounter) then
  995. paicpu(p)^.loadreg(0,regCounter);
  996. {$endif arithopt}
  997. End;
  998. End;
  999. End;
  1000. End;
  1001. A_STD: If GetLastInstruction(p, hp1) And
  1002. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  1003. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1004. End
  1005. End;
  1006. End;
  1007. GetNextInstruction(p, p);
  1008. End;
  1009. End;
  1010. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  1011. {Removes the marked instructions and disposes the PPaiProps of the other
  1012. instructions, restoring their line number}
  1013. Var p, hp1: Pai;
  1014. {$IfDef TP}
  1015. TmpLine: Longint;
  1016. {$EndIf TP}
  1017. InstrCnt: Longint;
  1018. Begin
  1019. p := First;
  1020. SkipHead(P);
  1021. InstrCnt := 1;
  1022. While (p <> Last) Do
  1023. Begin
  1024. {$ifndef noinstremove}
  1025. If PPaiProp(p^.OptInfo)^.CanBeRemoved
  1026. Then
  1027. Begin
  1028. {$IfDef TP}
  1029. Dispose(PPaiProp(p^.OptInfo));
  1030. {$EndIf}
  1031. GetNextInstruction(p, hp1);
  1032. AsmL^.Remove(p);
  1033. Dispose(p, Done);
  1034. p := hp1;
  1035. Inc(InstrCnt);
  1036. End
  1037. Else
  1038. {$endif noinstremove}
  1039. Begin
  1040. {$IfDef TP}
  1041. Dispose(PPaiProp(p^.OptInfo));
  1042. {$EndIf TP}
  1043. p^.OptInfo := nil;
  1044. GetNextInstruction(p, p);
  1045. Inc(InstrCnt);
  1046. End;
  1047. End;
  1048. {$IfNDef TP}
  1049. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  1050. {$EndIf TP}
  1051. End;
  1052. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  1053. Begin
  1054. DoCSE(AsmL, First, Last);
  1055. RemoveInstructs(AsmL, First, Last);
  1056. End;
  1057. End.
  1058. {
  1059. $Log$
  1060. Revision 1.36 1999-12-05 16:48:43 jonas
  1061. * CSE of constant loading in regs works properly again
  1062. + if a constant is stored into memory using "mov const, ref" and
  1063. there is a reg that contains this const, it is changed into
  1064. "mov reg, ref"
  1065. Revision 1.35 1999/12/02 11:26:41 peter
  1066. * newoptimizations define added
  1067. Revision 1.34 1999/11/21 13:09:41 jonas
  1068. * fixed some missed optimizations because 8bit regs were not always
  1069. taken into account
  1070. Revision 1.33 1999/11/20 11:37:03 jonas
  1071. * make cycle works with -dreplacereg (register renaming)! I have not
  1072. tested it yet together with -darithopt, but I don't expect problems
  1073. Revision 1.32 1999/11/14 11:26:53 jonas
  1074. + basic register renaming (not yet working completely, between
  1075. -dreplacereg/-dreplaceregdebug)
  1076. Revision 1.31 1999/11/06 16:21:57 jonas
  1077. + search optimial register to use in alignment code (compile with
  1078. -dalignreg, -dalignregdebug to see chosen register in
  1079. assembler code). Still needs support in ag386bin.
  1080. Revision 1.30 1999/11/06 14:34:20 peter
  1081. * truncated log to 20 revs
  1082. Revision 1.29 1999/11/05 16:01:46 jonas
  1083. + first implementation of choosing least used register for alignment code
  1084. (not yet working, between ifdef alignreg)
  1085. Revision 1.28 1999/10/11 11:11:31 jonas
  1086. * fixed bug which sometimes caused a crash when optimizing blocks of code with
  1087. assembler blocks (didn't notice before because of lack of zero page protection
  1088. under Win9x :( )
  1089. Revision 1.27 1999/10/01 13:51:40 jonas
  1090. * CSE now updates the RegAlloc's
  1091. Revision 1.26 1999/09/30 14:43:13 jonas
  1092. * fixed small efficiency which caused some missed optimizations (saves 1
  1093. assembler instruction on the whole compiler/RTL source tree! :)
  1094. Revision 1.25 1999/09/27 23:44:50 peter
  1095. * procinfo is now a pointer
  1096. * support for result setting in sub procedure
  1097. Revision 1.24 1999/08/25 11:59:58 jonas
  1098. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1099. Revision 1.23 1999/08/04 00:22:58 florian
  1100. * renamed i386asm and i386base to cpuasm and cpubase
  1101. Revision 1.22 1999/06/03 15:45:08 jonas
  1102. * sequences are now checked only once (previously, some long ones were
  1103. checked once completely and then several times partially)
  1104. Revision 1.21 1999/05/08 20:38:03 jonas
  1105. * seperate OPTimizer INFO pointer field in tai object
  1106. Revision 1.20 1999/05/01 13:24:19 peter
  1107. * merged nasm compiler
  1108. * old asm moved to oldasm/
  1109. Revision 1.2 1999/03/29 16:05:45 peter
  1110. * optimizer working for ag386bin
  1111. Revision 1.1 1999/03/26 00:01:09 peter
  1112. * first things for optimizer (compiles but cycle crashes)
  1113. Revision 1.19 1999/02/26 00:48:17 peter
  1114. * assembler writers fixed for ag386bin
  1115. Revision 1.18 1998/12/29 18:48:22 jonas
  1116. + optimize pascal code surrounding assembler blocks
  1117. Revision 1.17 1998/12/17 16:37:39 jonas
  1118. + extra checks in RegsEquivalent so some more optimizations can be done (which
  1119. where disabled by the second fix from revision 1.22)
  1120. Revision 1.16 1998/12/02 16:23:31 jonas
  1121. * changed "if longintvar in set" to case or "if () or () .." statements
  1122. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  1123. Revision 1.15 1998/11/24 19:47:24 jonas
  1124. * fixed problems posiible with 3 operand instructions
  1125. Revision 1.14 1998/11/09 19:40:48 jonas
  1126. * fixed comments from last commit (apparently there's still a 255 char limit :( )
  1127. Revision 1.13 1998/11/09 19:33:39 jonas
  1128. * changed specific bugfix (which was actually wrong implemented, but
  1129. did the right thing in most cases nevertheless) to general bugfix
  1130. * fixed bug that caused
  1131. mov (ebp), edx mov (ebp), edx
  1132. mov (edx), edx mov (edx), edx
  1133. ... being changed to ...
  1134. mov (ebp), edx mov edx, eax
  1135. mov (eax), eax
  1136. but this disabled another small correct optimization...
  1137. Revision 1.12 1998/10/20 09:32:54 peter
  1138. * removed some unused vars
  1139. }