csopt386.pas 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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) 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): boolean;
  494. var chCount: byte;
  495. begin
  496. case p^.opcode of
  497. A_IMUL: noHardCodedRegs := p^.ops <> 1;
  498. else
  499. begin
  500. NoHardCodedRegs := true;
  501. with InsProp[p^.opcode] do
  502. for chCount := 1 to MaxCh do
  503. if Ch[chCount] in ([Ch_REAX..Ch_MEDI,Ch_WMemEDI,Ch_All]-[Ch_RESP,Ch_WESP,Ch_RWESP]) then
  504. begin
  505. NoHardCodedRegs := false;
  506. break
  507. end;
  508. end;
  509. end;
  510. end;
  511. Procedure ChangeReg(var Reg: TRegister; orgReg, newReg: TRegister);
  512. begin
  513. if reg = newReg then
  514. reg := orgReg
  515. else if reg = regtoreg8(newReg) then
  516. reg := regtoreg8(orgReg)
  517. else if reg = regtoreg16(newReg) then
  518. reg := regtoreg16(orgReg);
  519. end;
  520. procedure changeOp(var o: toper; orgReg, newReg: tregister);
  521. begin
  522. case o.typ of
  523. top_reg: changeReg(o.reg,orgReg,newReg);
  524. top_ref:
  525. begin
  526. changeReg(o.ref^.base,orgReg,newReg);
  527. changeReg(o.ref^.index,orgReg,newReg);
  528. end;
  529. end;
  530. end;
  531. Procedure DoReplaceReg(orgReg,newReg: tregister; hp: paicpu);
  532. var opCount: byte;
  533. begin
  534. for opCount := 0 to 2 do
  535. changeOp(hp^.oper[opCount],orgReg,newReg)
  536. end;
  537. function RegSizesOK(oldReg,newReg: TRegister; p: paicpu): boolean;
  538. { oldreg and newreg must be 32bit components }
  539. var opCount: byte;
  540. begin
  541. RegSizesOK := true;
  542. { if only one of them is a general purpose register ... }
  543. if (IsGP32reg(oldReg) xor IsGP32Reg(newReg)) then
  544. begin
  545. for opCount := 0 to 2 do
  546. if (p^.oper[opCount].typ = top_reg) and
  547. (p^.oper[opCount].reg in [R_AL..R_DH]) then
  548. begin
  549. RegSizesOK := false;
  550. break
  551. end
  552. end;
  553. end;
  554. function RegReadByInstruction(reg: TRegister; hp: pai): boolean;
  555. { assumes hp doesn't modify registers implicitely (like div) }
  556. { and that reg is a 32bit register }
  557. var p: paicpu;
  558. opCount: byte;
  559. begin
  560. RegReadByInstruction := false;
  561. p := paicpu(hp);
  562. if hp^.typ <> ait_instruction then
  563. exit;
  564. case p^.opcode of
  565. A_IMUL:
  566. case p^.ops of
  567. 1: regReadByInstruction := (reg = R_EAX) or reginOp(reg,p^.oper[0]);
  568. 2,3:
  569. regReadByInstruction := regInOp(reg,p^.oper[0]) or
  570. regInOp(reg,p^.oper[1]);
  571. end;
  572. { A_IDIV,A_DIV,A_IMUL:
  573. begin
  574. regReadByInstruction :=
  575. regInOp(reg,p^.oper[0]) or
  576. (((p^.opcode = A_IDIV) or
  577. (p^.opcode = A_DIV)) and
  578. (reg = R_EAX));
  579. end;}
  580. else
  581. begin
  582. for opCount := 0 to 2 do
  583. if (p^.oper[opCount].typ = top_ref) and
  584. RegInRef(reg,p^.oper[opCount].ref^) then
  585. begin
  586. RegReadByInstruction := true;
  587. exit
  588. end;
  589. for opCount := 1 to MaxCh do
  590. case InsProp[p^.opcode].Ch[opCount] of
  591. Ch_RWOp1,Ch_ROp1{$ifdef arithopt},Ch_MOp1{$endif}:
  592. if (p^.oper[0].typ = top_reg) and
  593. (reg32(p^.oper[0].reg) = reg) then
  594. begin
  595. RegReadByInstruction := true;
  596. exit
  597. end;
  598. Ch_RWOp2,Ch_ROp2{$ifdef arithopt},Ch_MOp2{$endif}:
  599. if (p^.oper[1].typ = top_reg) and
  600. (reg32(p^.oper[1].reg) = reg) then
  601. begin
  602. RegReadByInstruction := true;
  603. exit
  604. end;
  605. Ch_RWOp3,Ch_ROp3{$ifdef arithopt},Ch_MOp3{$endif}:
  606. if (p^.oper[2].typ = top_reg) and
  607. (reg32(p^.oper[2].reg) = reg) then
  608. begin
  609. RegReadByInstruction := true;
  610. exit
  611. end;
  612. end;
  613. end;
  614. end;
  615. end;
  616. procedure DoReplaceReadReg(orgReg,newReg: tregister; p: paicpu);
  617. var opCount: byte;
  618. begin
  619. { handle special case }
  620. case p^.opcode of
  621. A_IMUL:
  622. begin
  623. case p^.ops of
  624. 1: internalerror(1301001);
  625. 2,3:
  626. begin
  627. changeOp(p^.oper[0],orgReg,newReg);
  628. if p^.ops = 3 then
  629. changeOp(p^.oper[1],orgReg,newReg);
  630. end;
  631. end;
  632. end;
  633. A_DIV,A_IDIV,A_MUL: internalerror(1301002);
  634. else
  635. begin
  636. for opCount := 0 to 2 do
  637. if p^.oper[opCount].typ = top_ref then
  638. changeOp(p^.oper[opCount],orgReg,newReg);
  639. for opCount := 1 to MaxCh do
  640. case InsProp[p^.opcode].Ch[opCount] of
  641. Ch_ROp1:
  642. if p^.oper[0].typ = top_reg then
  643. ChangeReg(p^.oper[0].reg,orgReg,newReg);
  644. Ch_ROp2:
  645. if p^.oper[1].typ = top_reg then
  646. ChangeReg(p^.oper[1].reg,orgReg,newReg);
  647. Ch_ROp3:
  648. if p^.oper[2].typ = top_reg then
  649. ChangeReg(p^.oper[2].reg,orgReg,newReg);
  650. end;
  651. end;
  652. end;
  653. end;
  654. function ReplaceReg(orgReg, newReg: TRegister; p: pai;
  655. const c: TContent; orgRegCanBeModified: Boolean;
  656. var returnEndP: pai): Boolean;
  657. { Tries to replace orgreg with newreg in all instructions coming after p }
  658. { until orgreg gets loaded with a new value. Returns true if successful, }
  659. { false otherwise. If successful, the contents of newReg are set to c, }
  660. { which should hold the contents of newReg before the current sequence }
  661. { started }
  662. { if the functino returns true, returnEndP holds the lat instruction }
  663. { where newReg was replaced by orgReg }
  664. var endP, hp: Pai;
  665. sequenceEnd, tmpResult, newRegModified, orgRegRead: Boolean;
  666. begin
  667. ReplaceReg := false;
  668. tmpResult := true;
  669. sequenceEnd := false;
  670. newRegModified := false;
  671. orgRegRead := false;
  672. endP := p;
  673. while tmpResult and not sequenceEnd do
  674. begin
  675. tmpResult :=
  676. getNextInstruction(endP,endP) and
  677. (endP^.typ = ait_instruction);
  678. if tmpresult and not assigned(endP^.optInfo) then
  679. begin
  680. hp := new(pai_asm_comment,init(strpnew('next no optinfo')));
  681. hp^.next := endp;
  682. hp^.previous := endp^.previous;
  683. endp^.previous := hp;
  684. if assigned(hp^.previous) then
  685. hp^.previous^.next := hp;
  686. exit;
  687. end;
  688. If tmpResult and
  689. { don't take into account instructions that will be removed }
  690. Not (PPaiProp(endP^.optInfo)^.canBeRemoved) then
  691. begin
  692. sequenceEnd :=
  693. noHardCodedRegs(paicpu(endP)) and
  694. RegLoadedWithNewValue(newReg,true,paicpu(endP)) or
  695. (GetNextInstruction(endp,hp) and
  696. FindRegDealloc(newReg,hp));
  697. newRegModified :=
  698. newRegModified or
  699. (not(sequenceEnd) and
  700. RegModifiedByInstruction(newReg,endP));
  701. orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
  702. sequenceEnd := SequenceEnd and
  703. { since newReg will be replaced by orgReg, we can't allow that newReg }
  704. { gets modified if orgReg is still read afterwards (since after }
  705. { replacing, this would mean that orgReg first gets modified and then }
  706. { gets read in the assumption it still contains the unmodified value) }
  707. not(newRegModified and orgRegRead) and
  708. { since newReg will be replaced by orgReg, we can't allow that newReg }
  709. { gets modified if orgRegCanBeModified = false }
  710. (orgRegCanBeModified or not(newRegModified));
  711. tmpResult :=
  712. not(newRegModified and orgRegRead) and
  713. (orgRegCanBeModified or not(newRegModified)) and
  714. (endP^.typ = ait_instruction) and
  715. not(paicpu(endP)^.is_jmp) and
  716. NoHardCodedRegs(paicpu(endP)) and
  717. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  718. not RegModifiedByInstruction(orgReg,endP);
  719. end;
  720. end;
  721. sequenceEnd := sequenceEnd and
  722. (not(assigned(endp)) or
  723. not(endp^.typ = ait_instruction) or
  724. (noHardCodedRegs(paicpu(endP)) and
  725. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  726. not(newRegModified and
  727. (orgReg in PPaiProp(endP^.optInfo)^.usedRegs) and
  728. not(RegLoadedWithNewValue(orgReg,true,paicpu(endP))))));
  729. if SequenceEnd then
  730. begin
  731. {$ifdef replaceregdebug}
  732. hp := new(pai_asm_comment,init(strpnew(
  733. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  734. ' from here...')));
  735. hp^.next := p;
  736. hp^.previous := p^.previous;
  737. p^.previous := hp;
  738. if assigned(hp^.previous) then
  739. hp^.previous^.next := hp;
  740. hp := new(pai_asm_comment,init(strpnew(
  741. 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  742. ' till here')));
  743. hp^.next := endp^.next;
  744. hp^.previous := endp;
  745. endp^.next := hp;
  746. if assigned(hp^.next) then
  747. hp^.next^.previous := hp;
  748. {$endif replaceregdebug}
  749. ReplaceReg := true;
  750. returnEndP := endP;
  751. hp := p;
  752. while hp <> endP do
  753. begin
  754. if not(PPaiProp(hp^.optInfo)^.canBeRemoved) and
  755. (hp^.typ = ait_instruction) then
  756. DoReplaceReg(orgReg,newReg,paicpu(hp));
  757. GetNextInstruction(hp,hp)
  758. end;
  759. if assigned(endp) and (endp^.typ = ait_instruction) then
  760. DoReplaceReadReg(orgReg,newReg,paicpu(endP));
  761. { the replacing stops either at the moment that }
  762. { a) the newreg gets loaded with a new value (one not depending on the }
  763. { current value of newreg) }
  764. { b) newreg is completely replaced in this sequence and it's current value }
  765. { isn't used anymore }
  766. { In case b, the newreg was completely replaced by oldreg, so it's contents }
  767. { are unchanged compared the start of this sequence, so restore them }
  768. If RegLoadedWithNewValue(newReg,true,endP) then
  769. GetLastInstruction(endP,hp)
  770. else hp := endP;
  771. if (p <> endp) or
  772. not RegLoadedWithNewValue(newReg,true,endP) then
  773. RestoreRegContentsTo(newReg, c ,p, hp);
  774. { In both case a and b, it is possible that the new register was modified }
  775. { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
  776. { oldreg's contents have been changed. To take this into account, we simply }
  777. { set the contents of orgreg to "unknown" after this sequence }
  778. if newRegModified then
  779. ClearRegContentsFrom(orgReg,p,hp);
  780. end
  781. {$ifdef replaceregdebug}
  782. else
  783. begin
  784. hp := new(pai_asm_comment,init(strpnew(
  785. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  786. ' from here...')));
  787. hp^.previous := p^.previous;
  788. hp^.next := p;
  789. p^.previous := hp;
  790. if assigned(hp^.previous) then
  791. hp^.previous^.next := hp;
  792. hp := new(pai_asm_comment,init(strpnew(
  793. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  794. ' failed here')));
  795. hp^.next := endp^.next;
  796. hp^.previous := endp;
  797. endp^.next := hp;
  798. if assigned(hp^.next) then
  799. hp^.next^.previous := hp;
  800. end;
  801. {$endif replaceregdebug}
  802. End;
  803. {$endif replacereg}
  804. {$ifdef arithopt}
  805. Function FindRegWithConst(p: Pai; size: topsize; l: longint; Var Res: TRegister): Boolean;
  806. {Finds a register which contains the constant l}
  807. Var Counter: TRegister;
  808. {$ifdef testing}
  809. hp: pai;
  810. {$endif testing}
  811. tmpresult: boolean;
  812. Begin
  813. Counter := R_NO;
  814. repeat
  815. inc(counter);
  816. tmpresult := (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) and
  817. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.opsize = size) and
  818. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].val = l);
  819. {$ifdef testing}
  820. if (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) then
  821. begin
  822. hp := new(pai_asm_comment,init(strpnew(
  823. 'checking const load of '+tostr(l)+' here...')));
  824. hp^.next := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod;
  825. hp^.previous := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous;
  826. PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
  827. if assigned(hp^.previous) then
  828. hp^.previous^.next := hp;
  829. end;
  830. {$endif testing}
  831. until tmpresult or (Counter = R_EDI);
  832. res := counter;
  833. FindRegWithConst := tmpResult;
  834. End;
  835. {$endif arithopt}
  836. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  837. {marks the instructions that can be removed by RemoveInstructs. They're not
  838. removed immediately because sometimes an instruction needs to be checked in
  839. two different sequences}
  840. Var Cnt, Cnt2: Longint;
  841. p, hp1, hp2: Pai;
  842. hp3, hp4, hp5: pai;
  843. RegInfo: TRegInfo;
  844. RegCounter: TRegister;
  845. TmpState: Byte;
  846. Begin
  847. p := First;
  848. SkipHead(p);
  849. First := p;
  850. While (p <> Last) Do
  851. Begin
  852. Case p^.typ Of
  853. ait_align:
  854. if not(pai_align(p)^.use_op) then
  855. SetAlignReg(p);
  856. ait_instruction:
  857. Begin
  858. Case Paicpu(p)^.opcode Of
  859. A_CLD: If GetLastInstruction(p, hp1) And
  860. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  861. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  862. A_MOV, A_MOVZX, A_MOVSX:
  863. Begin
  864. Case Paicpu(p)^.oper[0].typ Of
  865. Top_Ref:
  866. Begin {destination is always a register in this case}
  867. With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  868. Begin
  869. If (p = StartMod) And
  870. GetLastInstruction (p, hp1) And
  871. (hp1^.typ <> ait_marker)
  872. Then
  873. {so we don't try to check a sequence when p is the first instruction of the block}
  874. If CheckSequence(p, Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
  875. (Cnt > 0) Then
  876. Begin
  877. hp1 := nil;
  878. { although it's perfectly ok to remove an instruction which doesn't contain }
  879. { the register that we've just checked (CheckSequence takes care of that), }
  880. { the sequence containing this other register should also be completely }
  881. { checked and removed, otherwise we may get situations like this: }
  882. { }
  883. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  884. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  885. { movl 8(%edx), %edx movl 8(%edx), %edx }
  886. { movl (%eax), eax movl (%eax), eax }
  887. { cmpl %eax, %edx cmpl %eax, %edx }
  888. { jnz l123 getting converted to jnz l123 }
  889. { movl 12(%ebp), %edx movl 4(%eax), eax }
  890. { movl 16(%ebp), %eax }
  891. { movl 8(%edx), %edx }
  892. { movl 4(%eax), eax }
  893. hp2 := p;
  894. Cnt2 := 1;
  895. While Cnt2 <= Cnt Do
  896. Begin
  897. If (hp1 = nil) And
  898. Not(RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  899. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p)) And
  900. Not((p^.typ = ait_instruction) And
  901. (paicpu(p)^.OpCode = A_MOV) And
  902. (paicpu(p)^.Oper[0].typ = top_ref) And
  903. (PPaiProp(p^.OptInfo)^.Regs[Reg32(paicpu(p)^.Oper[1].reg)].NrOfMods
  904. <= (Cnt - Cnt2 + 1)))
  905. Then hp1 := p;
  906. {$ifndef noremove}
  907. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  908. {$endif noremove}
  909. Inc(Cnt2);
  910. GetNextInstruction(p, p);
  911. End;
  912. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  913. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  914. {hp4 is used to get the contents of the registers before the sequence}
  915. GetLastInstruction(hp2, hp4);
  916. {$IfDef CSDebug}
  917. For RegCounter := R_EAX To R_EDI Do
  918. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  919. Begin
  920. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  921. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  922. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  923. End;
  924. {$EndIf CSDebug}
  925. { If some registers were different in the old and the new sequence, move }
  926. { the contents of those old registers to the new ones }
  927. For RegCounter := R_EAX To R_EDI Do
  928. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  929. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  930. Begin
  931. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  932. PPaiProp(hp4^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,hp2);
  933. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  934. {old reg new reg}
  935. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  936. Begin
  937. {$ifdef replacereg}
  938. getLastInstruction(p,hp3);
  939. If not ReplaceReg(RegInfo.New2OldReg[RegCounter],
  940. regCounter,hp3,
  941. PPaiProp(hp4^.optInfo)^.Regs[regCounter],true,hp5) then
  942. begin
  943. {$endif replacereg}
  944. hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
  945. {old reg new reg}
  946. RegInfo.New2OldReg[RegCounter], RegCounter));
  947. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
  948. {$ifdef replacereg}
  949. end
  950. {$endif replacereg}
  951. End
  952. Else
  953. { imagine the following code: }
  954. { normal wrong optimized }
  955. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  956. { movl (%eax), %eax movl (%eax), %eax }
  957. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  958. { jne l1 jne l1 }
  959. { movl 8(%ebp), %eax }
  960. { movl (%eax), %edi movl %eax, %edi }
  961. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  962. { movl 8(%ebp), %eax }
  963. { pushl 70(%eax) pushl 70(%eax) }
  964. { }
  965. { The error is that at the moment that the last instruction is executed, }
  966. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  967. { registers that are completely removed from a sequence (= registers in }
  968. { RegLoadedForRef, have to be changed to their contents from before the }
  969. { sequence. }
  970. If RegCounter in RegInfo.RegsLoadedForRef Then
  971. Begin
  972. {load Cnt2 with the total number of instructions of this sequence}
  973. Cnt2 := PPaiProp(hp4^.OptInfo)^.
  974. Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
  975. hp3 := hp2;
  976. For Cnt := 1 to Pred(Cnt2) Do
  977. GetNextInstruction(hp3, hp3);
  978. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  979. GetNextInstruction(hp3, hp3);
  980. {$ifdef csdebug}
  981. Writeln('Cnt2: ',Cnt2);
  982. hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
  983. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  984. {$endif csdebug}
  985. hp3 := hp2;
  986. {first change the contents of the register inside the sequence}
  987. For Cnt := 1 to Cnt2 Do
  988. Begin
  989. {save the WState of the last pai object of the sequence for later use}
  990. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  991. {$ifdef csdebug}
  992. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '
  993. +tostr(tmpstate))));
  994. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  995. {$endif csdebug}
  996. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  997. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  998. GetNextInstruction(hp3, hp3);
  999. End;
  1000. {here, hp3 = p = Pai object right after the sequence, TmpState = WState of
  1001. RegCounter at the last Pai object of the sequence}
  1002. GetLastInstruction(hp3, hp3);
  1003. While GetNextInstruction(hp3, hp3) And
  1004. (PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState
  1005. = TmpState) Do
  1006. {$ifdef csdebug}
  1007. begin
  1008. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '+
  1009. tostr(PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState))));
  1010. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  1011. {$endif csdebug}
  1012. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  1013. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  1014. {$ifdef csdebug}
  1015. end;
  1016. {$endif csdebug}
  1017. {$ifdef csdebug}
  1018. hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  1019. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  1020. {$endif csdebug}
  1021. End;
  1022. End;
  1023. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  1024. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  1025. If hp1 <> nil Then p := hp1;
  1026. Continue;
  1027. End
  1028. Else
  1029. If (Cnt > 0) And
  1030. (PPaiProp(p^.OptInfo)^.
  1031. Regs[Reg32(Paicpu(p)^.oper[1].reg)].Typ = Con_Ref) And
  1032. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  1033. Begin
  1034. hp2 := p;
  1035. Cnt2 := 1;
  1036. While Cnt2 <= Cnt Do
  1037. Begin
  1038. If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  1039. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p) Then
  1040. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  1041. Inc(Cnt2);
  1042. GetNextInstruction(p, p);
  1043. End;
  1044. Continue;
  1045. End;
  1046. End;
  1047. End;
  1048. {$ifdef replacereg}
  1049. top_Reg:
  1050. { try to replace the new reg with the old reg }
  1051. if (paicpu(p)^.opcode = A_MOV) and
  1052. getLastInstruction(p,hp4) then
  1053. begin
  1054. Case paicpu(p)^.oper[1].typ of
  1055. top_Reg:
  1056. if ReplaceReg(paicpu(p)^.oper[0].reg,
  1057. paicpu(p)^.oper[1].reg,p,
  1058. PPaiProp(hp4^.optInfo)^.Regs[regCounter],false,hp1) then
  1059. begin
  1060. PPaiProp(p^.optInfo)^.canBeRemoved := true;
  1061. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,
  1062. PPaiProp(p^.optInfo)^.regs[paicpu(p)^.oper[0].reg].startMod,
  1063. hp1);
  1064. end;
  1065. end
  1066. end;
  1067. {$endif replacereg}
  1068. top_symbol,Top_Const:
  1069. Begin
  1070. Case Paicpu(p)^.oper[1].typ Of
  1071. Top_Reg:
  1072. Begin
  1073. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  1074. If GetLastInstruction(p, hp1) Then
  1075. With PPaiProp(hp1^.OptInfo)^.Regs[regCounter] Do
  1076. If (Typ = Con_Const) And
  1077. (paicpu(startMod)^.opsize >= paicpu(p)^.opsize) and
  1078. opsequal(paicpu(StartMod)^.oper[0],paicpu(p)^.oper[0]) Then
  1079. begin
  1080. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  1081. allocRegBetween(asmL,regCounter,startMod,p);
  1082. end;
  1083. End;
  1084. {$ifdef arithopt}
  1085. Top_Ref:
  1086. if (paicpu(p)^.oper[0].typ = top_const) and
  1087. getLastInstruction(p,hp1) and
  1088. findRegWithConst(hp1,paicpu(p)^.opsize,paicpu(p)^.oper[0].val,regCounter) then
  1089. begin
  1090. paicpu(p)^.loadreg(0,regCounter);
  1091. allocRegBetween(AsmL,reg32(regCounter),
  1092. PPaiProp(hp1^.optinfo)^.regs[regCounter].startMod,p);
  1093. end;
  1094. {$endif arithopt}
  1095. End;
  1096. End;
  1097. End;
  1098. End;
  1099. A_STD: If GetLastInstruction(p, hp1) And
  1100. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  1101. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1102. End
  1103. End;
  1104. End;
  1105. GetNextInstruction(p, p);
  1106. End;
  1107. End;
  1108. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  1109. {Removes the marked instructions and disposes the PPaiProps of the other
  1110. instructions, restoring their line number}
  1111. Var p, hp1: Pai;
  1112. {$IfDef TP}
  1113. TmpLine: Longint;
  1114. {$EndIf TP}
  1115. InstrCnt: Longint;
  1116. Begin
  1117. p := First;
  1118. SkipHead(P);
  1119. InstrCnt := 1;
  1120. While (p <> Last) Do
  1121. Begin
  1122. {$ifndef noinstremove}
  1123. If PPaiProp(p^.OptInfo)^.CanBeRemoved
  1124. Then
  1125. Begin
  1126. {$IfDef TP}
  1127. Dispose(PPaiProp(p^.OptInfo));
  1128. {$EndIf}
  1129. GetNextInstruction(p, hp1);
  1130. AsmL^.Remove(p);
  1131. Dispose(p, Done);
  1132. p := hp1;
  1133. Inc(InstrCnt);
  1134. End
  1135. Else
  1136. {$endif noinstremove}
  1137. Begin
  1138. {$IfDef TP}
  1139. Dispose(PPaiProp(p^.OptInfo));
  1140. {$EndIf TP}
  1141. p^.OptInfo := nil;
  1142. GetNextInstruction(p, p);
  1143. Inc(InstrCnt);
  1144. End;
  1145. End;
  1146. {$IfNDef TP}
  1147. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  1148. {$EndIf TP}
  1149. End;
  1150. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  1151. Begin
  1152. DoCSE(AsmL, First, Last);
  1153. RemoveInstructs(AsmL, First, Last);
  1154. End;
  1155. End.
  1156. {
  1157. $Log$
  1158. Revision 1.45 2000-02-10 14:57:13 jonas
  1159. * fixed bug due to lack of support for top_symbol operands
  1160. Revision 1.44 2000/02/09 13:22:51 peter
  1161. * log truncated
  1162. Revision 1.43 2000/02/04 13:52:17 jonas
  1163. * better support for regvars (still needs a move of the call to the optimize
  1164. procedure to a place where resetusableregisters is not yet called to work)
  1165. * small regallocation fixes for -dnewoptimizations
  1166. Revision 1.42 2000/01/28 15:15:31 jonas
  1167. * moved skipinstr from daopt386 to aasm
  1168. * fixed crashing bug with -dreplacereg in csopt386.pas
  1169. Revision 1.41 2000/01/23 11:11:37 michael
  1170. + Fixes from Jonas.
  1171. Revision 1.40 2000/01/22 16:10:06 jonas
  1172. + all code generator generated "mov reg1,reg2" instructions are now
  1173. attempted to be removed using the replacereg code
  1174. (-dnewoptimizations)
  1175. * small fixes to -dreplacereg code
  1176. Revision 1.39 2000/01/13 13:07:05 jonas
  1177. * released -dalignreg
  1178. * some small fixes to -dnewOptimizations helper procedures
  1179. Revision 1.38 2000/01/07 01:14:23 peter
  1180. * updated copyright to 2000
  1181. Revision 1.37 2000/01/03 17:11:17 jonas
  1182. * fixed bug with -dreplacereg
  1183. Revision 1.36 1999/12/05 16:48:43 jonas
  1184. * CSE of constant loading in regs works properly again
  1185. + if a constant is stored into memory using "mov const, ref" and
  1186. there is a reg that contains this const, it is changed into
  1187. "mov reg, ref"
  1188. Revision 1.35 1999/12/02 11:26:41 peter
  1189. * newoptimizations define added
  1190. Revision 1.34 1999/11/21 13:09:41 jonas
  1191. * fixed some missed optimizations because 8bit regs were not always
  1192. taken into account
  1193. Revision 1.33 1999/11/20 11:37:03 jonas
  1194. * make cycle works with -dreplacereg (register renaming)! I have not
  1195. tested it yet together with -darithopt, but I don't expect problems
  1196. Revision 1.32 1999/11/14 11:26:53 jonas
  1197. + basic register renaming (not yet working completely, between
  1198. -dreplacereg/-dreplaceregdebug)
  1199. Revision 1.31 1999/11/06 16:21:57 jonas
  1200. + search optimial register to use in alignment code (compile with
  1201. -dalignreg, -dalignregdebug to see chosen register in
  1202. assembler code). Still needs support in ag386bin.
  1203. Revision 1.30 1999/11/06 14:34:20 peter
  1204. * truncated log to 20 revs
  1205. Revision 1.29 1999/11/05 16:01:46 jonas
  1206. + first implementation of choosing least used register for alignment code
  1207. (not yet working, between ifdef alignreg)
  1208. Revision 1.28 1999/10/11 11:11:31 jonas
  1209. * fixed bug which sometimes caused a crash when optimizing blocks of code with
  1210. assembler blocks (didn't notice before because of lack of zero page protection
  1211. under Win9x :( )
  1212. Revision 1.27 1999/10/01 13:51:40 jonas
  1213. * CSE now updates the RegAlloc's
  1214. Revision 1.26 1999/09/30 14:43:13 jonas
  1215. * fixed small efficiency which caused some missed optimizations (saves 1
  1216. assembler instruction on the whole compiler/RTL source tree! :)
  1217. Revision 1.25 1999/09/27 23:44:50 peter
  1218. * procinfo is now a pointer
  1219. * support for result setting in sub procedure
  1220. Revision 1.24 1999/08/25 11:59:58 jonas
  1221. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1222. }