csopt386.pas 53 KB

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