csopt386.pas 54 KB

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