2
0

csopt386.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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, endP: 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. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  452. While (p <> endP) Do
  453. Begin
  454. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  455. getNextInstruction(p,p);
  456. end;
  457. tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
  458. repeat
  459. PPaiProp(p^.optInfo)^.Regs[reg] := c;
  460. until not getNextInstruction(p,p) or
  461. (PPaiProp(p^.optInfo)^.Regs[reg].wState <> tmpState);
  462. {$ifdef replaceregdebug}
  463. if assigned(p) then
  464. begin
  465. hp := new(pai_asm_comment,init(strpnew(
  466. 'restored '+att_reg2str[reg]+' till here...')));
  467. hp^.next := p;
  468. hp^.previous := p^.previous;
  469. p^.previous := hp;
  470. if assigned(hp^.previous) then
  471. hp^.previous^.next := hp;
  472. end;
  473. {$endif replaceregdebug}
  474. end;
  475. Procedure ClearRegContentsFrom(reg: TRegister; p, endP: pai);
  476. { first clears the contents of reg from p till endP. Then the contents are }
  477. { cleared until the first instruction that changes reg }
  478. var
  479. {$ifdef replaceregdebug}
  480. hp: pai;
  481. {$endif replaceregdebug}
  482. tmpState: byte;
  483. begin
  484. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  485. While (p <> endP) Do
  486. Begin
  487. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  488. getNextInstruction(p,p);
  489. end;
  490. tmpState := PPaiProp(p^.optInfo)^.Regs[reg].wState;
  491. repeat
  492. PPaiProp(p^.optInfo)^.Regs[reg].typ := con_unknown;
  493. until not getNextInstruction(p,p) or
  494. (PPaiProp(p^.optInfo)^.Regs[reg].wState <> tmpState);
  495. {$ifdef replaceregdebug}
  496. if assigned(p) then
  497. begin
  498. hp := new(pai_asm_comment,init(strpnew(
  499. 'cleared '+att_reg2str[reg]+' till here...')));
  500. hp^.next := p;
  501. hp^.previous := p^.previous;
  502. p^.previous := hp;
  503. if assigned(hp^.previous) then
  504. hp^.previous^.next := hp;
  505. end;
  506. {$endif replaceregdebug}
  507. end;
  508. function NoHardCodedRegs(p: paicpu): boolean;
  509. var chCount: byte;
  510. begin
  511. NoHardCodedRegs := true;
  512. with InsProp[p^.opcode] do
  513. for chCount := 1 to MaxCh do
  514. if Ch[chCount] in ([Ch_REAX..Ch_MEDI]-[Ch_RESP,Ch_WESP,Ch_RWESP]) then
  515. begin
  516. NoHardCodedRegs := false;
  517. break
  518. end;
  519. end;
  520. Procedure ChangeReg(var Reg: TRegister; orgReg, newReg: TRegister);
  521. begin
  522. if reg = newReg then
  523. reg := orgReg
  524. else if reg = regtoreg8(newReg) then
  525. reg := regtoreg8(orgReg)
  526. else if reg = regtoreg16(newReg) then
  527. reg := regtoreg16(orgReg);
  528. end;
  529. Procedure DoReplaceReg(orgReg,newReg: tregister; hp: paicpu);
  530. var opCount: byte;
  531. begin
  532. for opCount := 0 to 2 do
  533. with hp^.oper[opCount] Do
  534. case typ of
  535. top_reg: ChangeReg(reg,orgReg,newReg);
  536. top_ref:
  537. begin
  538. ChangeReg(ref^.base,orgReg,newReg);
  539. ChangeReg(ref^.index,orgReg,newReg);
  540. end;
  541. end;
  542. end;
  543. function RegSizesOK(oldReg,newReg: TRegister; p: paicpu): boolean;
  544. { oldreg and newreg must be 32bit components }
  545. var opCount: byte;
  546. begin
  547. RegSizesOK := true;
  548. if not(IsGP32reg(oldReg) and IsGP32Reg(newReg)) then
  549. begin
  550. for opCount := 0 to 2 do
  551. if (p^.oper[opCount].typ = top_reg) and
  552. (p^.oper[opCount].reg in [R_AL..R_DH]) then
  553. begin
  554. RegSizesOK := false;
  555. break
  556. end
  557. end;
  558. end;
  559. function RegReadByInstruction(reg: TRegister; hp: pai): boolean;
  560. { assumes hp doesn't modify registers implicitely (like div) }
  561. { and that reg is a 32bit register }
  562. var p: paicpu;
  563. opCount: byte;
  564. begin
  565. RegReadByInstruction := false;
  566. p := paicpu(hp);
  567. if hp^.typ <> ait_instruction then
  568. exit;
  569. for opCount := 0 to 2 do
  570. if (p^.oper[opCount].typ = top_ref) and
  571. RegInRef(reg,p^.oper[opCount].ref^) then
  572. begin
  573. RegReadByInstruction := true;
  574. exit
  575. end;
  576. for opCount := 1 to MaxCh do
  577. case InsProp[p^.opcode].Ch[opCount] of
  578. Ch_RWOp1,Ch_ROp1{$ifdef arithopt},Ch_MOp1{$endif}:
  579. if (p^.oper[0].typ = top_reg) and
  580. (reg32(p^.oper[0].reg) = reg) then
  581. begin
  582. RegReadByInstruction := true;
  583. exit
  584. end;
  585. Ch_RWOp2,Ch_ROp2{$ifdef arithopt},Ch_MOp2{$endif}:
  586. if (p^.oper[1].typ = top_reg) and
  587. (reg32(p^.oper[1].reg) = reg) then
  588. begin
  589. RegReadByInstruction := true;
  590. exit
  591. end;
  592. Ch_RWOp3,Ch_ROp3{$ifdef arithopt},Ch_MOp3{$endif}:
  593. if (p^.oper[2].typ = top_reg) and
  594. (reg32(p^.oper[2].reg) = reg) then
  595. begin
  596. RegReadByInstruction := true;
  597. exit
  598. end;
  599. end;
  600. end;
  601. procedure DoReplaceReadReg(orgReg,newReg: tregister; p: paicpu);
  602. var opCount: byte;
  603. begin
  604. for opCount := 0 to 2 do
  605. if p^.oper[opCount].typ = top_ref then
  606. begin
  607. ChangeReg(p^.oper[opCount].ref^.base,orgReg,newReg);
  608. ChangeReg(p^.oper[opCount].ref^.index,orgReg,newReg);
  609. end;
  610. for opCount := 1 to MaxCh do
  611. case InsProp[p^.opcode].Ch[opCount] of
  612. Ch_RWOp1,Ch_ROp1{$ifdef arithopt},Ch_MOp1{$endif}:
  613. if p^.oper[0].typ = top_reg then
  614. ChangeReg(p^.oper[0].reg,orgReg,newReg);
  615. Ch_RWOp2,Ch_ROp2{$ifdef arithopt},Ch_MOp2{$endif}:
  616. if p^.oper[1].typ = top_reg then
  617. ChangeReg(p^.oper[1].reg,orgReg,newReg);
  618. Ch_RWOp3,Ch_ROp3{$ifdef arithopt},Ch_MOp3{$endif}:
  619. if p^.oper[2].typ = top_reg then
  620. ChangeReg(p^.oper[2].reg,orgReg,newReg);
  621. end;
  622. end;
  623. function ReplaceReg(orgReg, newReg: TRegister; p: pai;
  624. const c: TContent): Boolean;
  625. { Tries to replace orgreg with newreg in all instructions coming after p }
  626. { until orgreg gets loaded with a new value. Returns true if successful, }
  627. { false otherwise. If successful, the contents of newReg are set to c, }
  628. { which should hold the contents of newReg before the current sequence }
  629. { started }
  630. var endP, hp: Pai;
  631. sequenceEnd, tmpResult, newRegModified, orgRegRead: Boolean;
  632. begin
  633. ReplaceReg := false;
  634. tmpResult := true;
  635. sequenceEnd := false;
  636. newRegModified := false;
  637. orgRegRead := false;
  638. endP := p;
  639. (*
  640. endP := pai(p^.previous);
  641. { skip over the instructions that will be removed }
  642. while getNextInstruction(endP,hp) and
  643. assigned(hp) and
  644. PPaiProp(hp^.optInfo)^.canBeRemoved do
  645. endP := hp;
  646. *)
  647. while tmpResult and not sequenceEnd do
  648. begin
  649. tmpResult :=
  650. getNextInstruction(endP,endP);
  651. If tmpResult and
  652. { don't take into account instructions that will be removed }
  653. Not (PPaiProp(endP^.optInfo)^.canBeRemoved) then
  654. begin
  655. sequenceEnd :=
  656. RegLoadedWithNewValue(newReg,paicpu(endP)) or
  657. (GetNextInstruction(endp,hp) and
  658. FindRegDealloc(newReg,hp));
  659. newRegModified :=
  660. newRegModified or
  661. (not(sequenceEnd) and
  662. RegModifiedByInstruction(newReg,endP));
  663. orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
  664. sequenceEnd := SequenceEnd and not(newRegModified and orgRegRead);
  665. tmpResult :=
  666. not(newRegModified and orgRegRead) and
  667. (endP^.typ = ait_instruction) and
  668. not(paicpu(endP)^.is_jmp) and
  669. NoHardCodedRegs(paicpu(endP)) and
  670. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  671. not RegModifiedByInstruction(orgReg,endP);
  672. end;
  673. end;
  674. sequenceEnd := sequenceEnd and
  675. not(newRegModified and
  676. (orgReg in PPaiProp(endP^.optInfo)^.usedRegs) and
  677. not(RegLoadedWithNewValue(orgReg,paicpu(endP))));
  678. if SequenceEnd then
  679. begin
  680. {$ifdef replaceregdebug}
  681. hp := new(pai_asm_comment,init(strpnew(
  682. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  683. ' from here...')));
  684. hp^.next := p;
  685. hp^.previous := p^.previous;
  686. p^.previous := hp;
  687. if assigned(hp^.previous) then
  688. hp^.previous^.next := hp;
  689. hp := new(pai_asm_comment,init(strpnew(
  690. 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  691. ' till here')));
  692. hp^.next := endp^.next;
  693. hp^.previous := endp;
  694. endp^.next := hp;
  695. if assigned(hp^.next) then
  696. hp^.next^.previous := hp;
  697. {$endif replaceregdebug}
  698. ReplaceReg := true;
  699. hp := p;
  700. while hp <> endP do
  701. begin
  702. if not(PPaiProp(hp^.optInfo)^.canBeRemoved) and
  703. (hp^.typ = ait_instruction) then
  704. DoReplaceReg(orgReg,newReg,paicpu(hp));
  705. GetNextInstruction(hp,hp)
  706. end;
  707. if assigned(endp) and (endp^.typ = ait_instruction) then
  708. DoReplaceReadReg(orgReg,newReg,paicpu(endP));
  709. { the replacing stops either at the moment that }
  710. { a) the newreg gets loaded with a new value (one not depending on the }
  711. { current value of newreg) }
  712. { b) newreg is completely replaced in this sequence and it's current value }
  713. { isn't used anymore }
  714. { In case b, the newreg was completely replaced by oldreg, so it's contents }
  715. { are unchanged compared the start of this sequence, so restore them }
  716. If RegLoadedWithNewValue(newReg,endP) then
  717. GetLastInstruction(endP,hp)
  718. else hp := endP;
  719. if (p <> endp) or
  720. not RegLoadedWithNewValue(newReg,endP) then
  721. RestoreRegContentsTo(newReg, c ,p, hp);
  722. { In both case a and b, it is possible that the new register was modified }
  723. { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
  724. { oldreg's contents have been changed. To take this into account, we simply }
  725. { set the contents of orgreg to "unknown" after this sequence }
  726. if newRegModified then
  727. ClearRegContentsFrom(orgReg,p,hp);
  728. end
  729. {$ifdef replaceregdebug}
  730. else
  731. begin
  732. hp := new(pai_asm_comment,init(strpnew(
  733. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  734. ' from here...')));
  735. hp^.previous := p^.previous;
  736. hp^.next := p;
  737. p^.previous := hp;
  738. if assigned(hp^.previous) then
  739. hp^.previous^.next := hp;
  740. hp := new(pai_asm_comment,init(strpnew(
  741. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  742. ' failed here')));
  743. hp^.next := endp^.next;
  744. hp^.previous := endp;
  745. endp^.next := hp;
  746. if assigned(hp^.next) then
  747. hp^.next^.previous := hp;
  748. end;
  749. {$endif replaceregdebug}
  750. End;
  751. {$endif replacereg}
  752. {$ifdef arithopt}
  753. Function FindRegWithConst(p: Pai; size: topsize; l: longint; Var Res: TRegister): Boolean;
  754. {Finds a register which contains the constant l}
  755. Var Counter: TRegister;
  756. {$ifdef testing}
  757. hp: pai;
  758. {$endif testing}
  759. tmpresult: boolean;
  760. Begin
  761. Counter := R_NO;
  762. repeat
  763. inc(counter);
  764. tmpresult := (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) and
  765. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.opsize = size) and
  766. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].val = l);
  767. {$ifdef testing}
  768. if (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) then
  769. begin
  770. hp := new(pai_asm_comment,init(strpnew(
  771. 'checking const load of '+tostr(l)+' here...')));
  772. hp^.next := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod;
  773. hp^.previous := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous;
  774. PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
  775. if assigned(hp^.previous) then
  776. hp^.previous^.next := hp;
  777. end;
  778. {$endif testing}
  779. until tmpresult or (Counter = R_EDI);
  780. res := counter;
  781. FindRegWithConst := tmpResult;
  782. End;
  783. {$endif arithopt}
  784. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  785. {marks the instructions that can be removed by RemoveInstructs. They're not
  786. removed immediately because sometimes an instruction needs to be checked in
  787. two different sequences}
  788. Var Cnt, Cnt2: Longint;
  789. p, hp1, hp2: Pai;
  790. hp3, hp4: Pai;
  791. {$ifdef csdebug}
  792. hp5: pai;
  793. {$endif csdebug}
  794. RegInfo: TRegInfo;
  795. RegCounter: TRegister;
  796. TmpState: Byte;
  797. Begin
  798. p := First;
  799. SkipHead(p);
  800. First := p;
  801. While (p <> Last) Do
  802. Begin
  803. Case p^.typ Of
  804. {$ifdef alignreg}
  805. ait_align:
  806. SetAlignReg(p);
  807. {$endif alignreg}
  808. ait_instruction:
  809. Begin
  810. Case Paicpu(p)^.opcode Of
  811. A_CLD: If GetLastInstruction(p, hp1) And
  812. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  813. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  814. A_MOV, A_MOVZX, A_MOVSX:
  815. Begin
  816. Case Paicpu(p)^.oper[0].typ Of
  817. Top_Ref:
  818. Begin {destination is always a register in this case}
  819. With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  820. Begin
  821. If (p = StartMod) And
  822. GetLastInstruction (p, hp1) And
  823. (hp1^.typ <> ait_marker)
  824. Then
  825. {so we don't try to check a sequence when p is the first instruction of the block}
  826. If CheckSequence(p, Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
  827. (Cnt > 0) Then
  828. Begin
  829. hp1 := nil;
  830. { although it's perfectly ok to remove an instruction which doesn't contain }
  831. { the register that we've just checked (CheckSequence takes care of that), }
  832. { the sequence containing this other register should also be completely }
  833. { checked and removed, otherwise we may get situations like this: }
  834. { }
  835. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  836. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  837. { movl 8(%edx), %edx movl 8(%edx), %edx }
  838. { movl (%eax), eax movl (%eax), eax }
  839. { cmpl %eax, %edx cmpl %eax, %edx }
  840. { jnz l123 getting converted to jnz l123 }
  841. { movl 12(%ebp), %edx movl 4(%eax), eax }
  842. { movl 16(%ebp), %eax }
  843. { movl 8(%edx), %edx }
  844. { movl 4(%eax), eax }
  845. hp2 := p;
  846. Cnt2 := 1;
  847. While Cnt2 <= Cnt Do
  848. Begin
  849. If (hp1 = nil) And
  850. Not(RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  851. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p)) And
  852. Not((p^.typ = ait_instruction) And
  853. (paicpu(p)^.OpCode = A_MOV) And
  854. (paicpu(p)^.Oper[0].typ = top_ref) And
  855. (PPaiProp(p^.OptInfo)^.Regs[Reg32(paicpu(p)^.Oper[1].reg)].NrOfMods
  856. <= (Cnt - Cnt2 + 1)))
  857. Then hp1 := p;
  858. {$ifndef noremove}
  859. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  860. {$endif noremove}
  861. Inc(Cnt2);
  862. GetNextInstruction(p, p);
  863. End;
  864. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  865. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  866. {hp4 is used to get the contents of the registers before the sequence}
  867. GetLastInstruction(hp2, hp4);
  868. {$IfDef CSDebug}
  869. For RegCounter := R_EAX To R_EDI Do
  870. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  871. Begin
  872. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  873. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  874. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  875. End;
  876. {$EndIf CSDebug}
  877. { If some registers were different in the old and the new sequence, move }
  878. { the contents of those old registers to the new ones }
  879. For RegCounter := R_EAX To R_EDI Do
  880. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  881. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  882. Begin
  883. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  884. PPaiProp(hp4^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,hp2);
  885. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  886. {old reg new reg}
  887. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  888. Begin
  889. {$ifdef replacereg}
  890. getLastInstruction(p,hp3);
  891. If not ReplaceReg(RegInfo.New2OldReg[RegCounter],
  892. regCounter,hp3,
  893. PPaiProp(hp4^.optInfo)^.Regs[regCounter]) then
  894. begin
  895. {$endif replacereg}
  896. hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
  897. {old reg new reg}
  898. RegInfo.New2OldReg[RegCounter], RegCounter));
  899. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
  900. {$ifdef replacereg}
  901. end
  902. {$endif replacereg}
  903. End
  904. Else
  905. { imagine the following code: }
  906. { normal wrong optimized }
  907. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  908. { movl (%eax), %eax movl (%eax), %eax }
  909. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  910. { jne l1 jne l1 }
  911. { movl 8(%ebp), %eax }
  912. { movl (%eax), %edi movl %eax, %edi }
  913. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  914. { movl 8(%ebp), %eax }
  915. { pushl 70(%eax) pushl 70(%eax) }
  916. { }
  917. { The error is that at the moment that the last instruction is executed, }
  918. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  919. { registers that are completely removed from a sequence (= registers in }
  920. { RegLoadedForRef, have to be changed to their contents from before the }
  921. { sequence. }
  922. If RegCounter in RegInfo.RegsLoadedForRef Then
  923. Begin
  924. {load Cnt2 with the total number of instructions of this sequence}
  925. Cnt2 := PPaiProp(hp4^.OptInfo)^.
  926. Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
  927. hp3 := hp2;
  928. For Cnt := 1 to Pred(Cnt2) Do
  929. GetNextInstruction(hp3, hp3);
  930. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  931. GetNextInstruction(hp3, hp3);
  932. {$ifdef csdebug}
  933. Writeln('Cnt2: ',Cnt2);
  934. hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
  935. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  936. {$endif csdebug}
  937. hp3 := hp2;
  938. {first change the contents of the register inside the sequence}
  939. For Cnt := 1 to Cnt2 Do
  940. Begin
  941. {save the WState of the last pai object of the sequence for later use}
  942. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  943. {$ifdef csdebug}
  944. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '
  945. +tostr(tmpstate))));
  946. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  947. {$endif csdebug}
  948. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  949. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  950. GetNextInstruction(hp3, hp3);
  951. End;
  952. {here, hp3 = p = Pai object right after the sequence, TmpState = WState of
  953. RegCounter at the last Pai object of the sequence}
  954. GetLastInstruction(hp3, hp3);
  955. While GetNextInstruction(hp3, hp3) And
  956. (PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState
  957. = TmpState) Do
  958. {$ifdef csdebug}
  959. begin
  960. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '+
  961. tostr(PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState))));
  962. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  963. {$endif csdebug}
  964. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  965. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  966. {$ifdef csdebug}
  967. end;
  968. {$endif csdebug}
  969. {$ifdef csdebug}
  970. hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  971. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  972. {$endif csdebug}
  973. End;
  974. End;
  975. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  976. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  977. If hp1 <> nil Then p := hp1;
  978. Continue;
  979. End
  980. Else
  981. If (Cnt > 0) And
  982. (PPaiProp(p^.OptInfo)^.
  983. Regs[Reg32(Paicpu(p)^.oper[1].reg)].Typ = Con_Ref) And
  984. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  985. Begin
  986. hp2 := p;
  987. Cnt2 := 1;
  988. While Cnt2 <= Cnt Do
  989. Begin
  990. If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  991. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p) Then
  992. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  993. Inc(Cnt2);
  994. GetNextInstruction(p, p);
  995. End;
  996. Continue;
  997. End;
  998. End;
  999. End;
  1000. Top_Const:
  1001. Begin
  1002. Case Paicpu(p)^.oper[1].typ Of
  1003. Top_Reg:
  1004. Begin
  1005. If GetLastInstruction(p, hp1) Then
  1006. With PPaiProp(hp1^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  1007. If (Typ = Con_Const) And
  1008. (paicpu(startMod)^.opsize >= paicpu(p)^.opsize) and
  1009. (paicpu(StartMod)^.oper[0].val = paicpu(p)^.oper[0].val) Then
  1010. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  1011. End;
  1012. {$ifdef arithopt}
  1013. Top_Ref:
  1014. if getLastInstruction(p,hp1) and
  1015. findRegWithConst(hp1,paicpu(p)^.opsize,paicpu(p)^.oper[0].val,regCounter) then
  1016. paicpu(p)^.loadreg(0,regCounter);
  1017. {$endif arithopt}
  1018. End;
  1019. End;
  1020. End;
  1021. End;
  1022. A_STD: If GetLastInstruction(p, hp1) And
  1023. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  1024. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1025. End
  1026. End;
  1027. End;
  1028. GetNextInstruction(p, p);
  1029. End;
  1030. End;
  1031. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  1032. {Removes the marked instructions and disposes the PPaiProps of the other
  1033. instructions, restoring their line number}
  1034. Var p, hp1: Pai;
  1035. {$IfDef TP}
  1036. TmpLine: Longint;
  1037. {$EndIf TP}
  1038. InstrCnt: Longint;
  1039. Begin
  1040. p := First;
  1041. SkipHead(P);
  1042. InstrCnt := 1;
  1043. While (p <> Last) Do
  1044. Begin
  1045. {$ifndef noinstremove}
  1046. If PPaiProp(p^.OptInfo)^.CanBeRemoved
  1047. Then
  1048. Begin
  1049. {$IfDef TP}
  1050. Dispose(PPaiProp(p^.OptInfo));
  1051. {$EndIf}
  1052. GetNextInstruction(p, hp1);
  1053. AsmL^.Remove(p);
  1054. Dispose(p, Done);
  1055. p := hp1;
  1056. Inc(InstrCnt);
  1057. End
  1058. Else
  1059. {$endif noinstremove}
  1060. Begin
  1061. {$IfDef TP}
  1062. Dispose(PPaiProp(p^.OptInfo));
  1063. {$EndIf TP}
  1064. p^.OptInfo := nil;
  1065. GetNextInstruction(p, p);
  1066. Inc(InstrCnt);
  1067. End;
  1068. End;
  1069. {$IfNDef TP}
  1070. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  1071. {$EndIf TP}
  1072. End;
  1073. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  1074. Begin
  1075. DoCSE(AsmL, First, Last);
  1076. RemoveInstructs(AsmL, First, Last);
  1077. End;
  1078. End.
  1079. {
  1080. $Log$
  1081. Revision 1.37 2000-01-03 17:11:17 jonas
  1082. * fixed bug with -dreplacereg
  1083. Revision 1.36 1999/12/05 16:48:43 jonas
  1084. * CSE of constant loading in regs works properly again
  1085. + if a constant is stored into memory using "mov const, ref" and
  1086. there is a reg that contains this const, it is changed into
  1087. "mov reg, ref"
  1088. Revision 1.35 1999/12/02 11:26:41 peter
  1089. * newoptimizations define added
  1090. Revision 1.34 1999/11/21 13:09:41 jonas
  1091. * fixed some missed optimizations because 8bit regs were not always
  1092. taken into account
  1093. Revision 1.33 1999/11/20 11:37:03 jonas
  1094. * make cycle works with -dreplacereg (register renaming)! I have not
  1095. tested it yet together with -darithopt, but I don't expect problems
  1096. Revision 1.32 1999/11/14 11:26:53 jonas
  1097. + basic register renaming (not yet working completely, between
  1098. -dreplacereg/-dreplaceregdebug)
  1099. Revision 1.31 1999/11/06 16:21:57 jonas
  1100. + search optimial register to use in alignment code (compile with
  1101. -dalignreg, -dalignregdebug to see chosen register in
  1102. assembler code). Still needs support in ag386bin.
  1103. Revision 1.30 1999/11/06 14:34:20 peter
  1104. * truncated log to 20 revs
  1105. Revision 1.29 1999/11/05 16:01:46 jonas
  1106. + first implementation of choosing least used register for alignment code
  1107. (not yet working, between ifdef alignreg)
  1108. Revision 1.28 1999/10/11 11:11:31 jonas
  1109. * fixed bug which sometimes caused a crash when optimizing blocks of code with
  1110. assembler blocks (didn't notice before because of lack of zero page protection
  1111. under Win9x :( )
  1112. Revision 1.27 1999/10/01 13:51:40 jonas
  1113. * CSE now updates the RegAlloc's
  1114. Revision 1.26 1999/09/30 14:43:13 jonas
  1115. * fixed small efficiency which caused some missed optimizations (saves 1
  1116. assembler instruction on the whole compiler/RTL source tree! :)
  1117. Revision 1.25 1999/09/27 23:44:50 peter
  1118. * procinfo is now a pointer
  1119. * support for result setting in sub procedure
  1120. Revision 1.24 1999/08/25 11:59:58 jonas
  1121. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1122. Revision 1.23 1999/08/04 00:22:58 florian
  1123. * renamed i386asm and i386base to cpuasm and cpubase
  1124. Revision 1.22 1999/06/03 15:45:08 jonas
  1125. * sequences are now checked only once (previously, some long ones were
  1126. checked once completely and then several times partially)
  1127. Revision 1.21 1999/05/08 20:38:03 jonas
  1128. * seperate OPTimizer INFO pointer field in tai object
  1129. Revision 1.20 1999/05/01 13:24:19 peter
  1130. * merged nasm compiler
  1131. * old asm moved to oldasm/
  1132. Revision 1.2 1999/03/29 16:05:45 peter
  1133. * optimizer working for ag386bin
  1134. Revision 1.1 1999/03/26 00:01:09 peter
  1135. * first things for optimizer (compiles but cycle crashes)
  1136. Revision 1.19 1999/02/26 00:48:17 peter
  1137. * assembler writers fixed for ag386bin
  1138. Revision 1.18 1998/12/29 18:48:22 jonas
  1139. + optimize pascal code surrounding assembler blocks
  1140. Revision 1.17 1998/12/17 16:37:39 jonas
  1141. + extra checks in RegsEquivalent so some more optimizations can be done (which
  1142. where disabled by the second fix from revision 1.22)
  1143. Revision 1.16 1998/12/02 16:23:31 jonas
  1144. * changed "if longintvar in set" to case or "if () or () .." statements
  1145. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  1146. Revision 1.15 1998/11/24 19:47:24 jonas
  1147. * fixed problems posiible with 3 operand instructions
  1148. Revision 1.14 1998/11/09 19:40:48 jonas
  1149. * fixed comments from last commit (apparently there's still a 255 char limit :( )
  1150. Revision 1.13 1998/11/09 19:33:39 jonas
  1151. * changed specific bugfix (which was actually wrong implemented, but
  1152. did the right thing in most cases nevertheless) to general bugfix
  1153. * fixed bug that caused
  1154. mov (ebp), edx mov (ebp), edx
  1155. mov (edx), edx mov (edx), edx
  1156. ... being changed to ...
  1157. mov (ebp), edx mov edx, eax
  1158. mov (eax), eax
  1159. but this disabled another small correct optimization...
  1160. Revision 1.12 1998/10/20 09:32:54 peter
  1161. * removed some unused vars
  1162. }