csopt386.pas 54 KB

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