csopt386.pas 54 KB

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