csopt386.pas 50 KB

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