csopt386.pas 48 KB

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