csopt386.pas 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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. removeLast := storeBack(endP);
  709. sequenceEnd :=
  710. removeLast or
  711. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  712. RegLoadedWithNewValue(newReg,true,paicpu(endP)) or
  713. (GetNextInstruction(endp,hp) and
  714. FindRegDealloc(newReg,hp)));
  715. newRegModified :=
  716. newRegModified or
  717. (not(sequenceEnd) and
  718. RegModifiedByInstruction(newReg,endP));
  719. orgRegRead := newRegModified and RegReadByInstruction(orgReg,endP);
  720. sequenceEnd := SequenceEnd and
  721. (removeLast or
  722. { since newReg will be replaced by orgReg, we can't allow that newReg }
  723. { gets modified if orgReg is still read afterwards (since after }
  724. { replacing, this would mean that orgReg first gets modified and then }
  725. { gets read in the assumption it still contains the unmodified value) }
  726. not(newRegModified and orgRegRead)) (* and
  727. { since newReg will be replaced by orgReg, we can't allow that newReg }
  728. { gets modified if orgRegCanBeModified = false }
  729. (orgRegCanBeModified or not(newRegModified)) *);
  730. tmpResult :=
  731. not(removeLast) and
  732. not(newRegModified and orgRegRead) and
  733. (* (orgRegCanBeModified or not(newRegModified)) and *)
  734. (endP^.typ = ait_instruction) and
  735. not(paicpu(endP)^.is_jmp) and
  736. NoHardCodedRegs(paicpu(endP),orgReg,newReg) and
  737. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  738. not RegModifiedByInstruction(orgReg,endP);
  739. end;
  740. end;
  741. sequenceEnd := sequenceEnd and
  742. (removeLast or
  743. (orgRegCanBeModified or not(newRegModified))) and
  744. (not(assigned(endp)) or
  745. not(endp^.typ = ait_instruction) or
  746. (noHardCodedRegs(paicpu(endP),orgReg,newReg) and
  747. RegSizesOk(orgReg,newReg,paicpu(endP)) and
  748. not(newRegModified and
  749. (orgReg in PPaiProp(endP^.optInfo)^.usedRegs) and
  750. not(RegLoadedWithNewValue(orgReg,true,paicpu(endP))))));
  751. if SequenceEnd then
  752. begin
  753. {$ifdef replaceregdebug}
  754. hp := new(pai_asm_comment,init(strpnew(
  755. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  756. ' from here...')));
  757. hp^.next := p;
  758. hp^.previous := p^.previous;
  759. p^.previous := hp;
  760. if assigned(hp^.previous) then
  761. hp^.previous^.next := hp;
  762. hp := new(pai_asm_comment,init(strpnew(
  763. 'replaced '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  764. ' till here')));
  765. hp^.next := endp^.next;
  766. hp^.previous := endp;
  767. endp^.next := hp;
  768. if assigned(hp^.next) then
  769. hp^.next^.previous := hp;
  770. {$endif replaceregdebug}
  771. replaceReg := true;
  772. returnEndP := endP;
  773. getNextInstruction(p,hp);
  774. while hp <> endP do
  775. begin
  776. if not(PPaiProp(hp^.optInfo)^.canBeRemoved) and
  777. (hp^.typ = ait_instruction) then
  778. DoReplaceReg(orgReg,newReg,paicpu(hp));
  779. GetNextInstruction(hp,hp)
  780. end;
  781. if assigned(endp) and (endp^.typ = ait_instruction) then
  782. DoReplaceReadReg(orgReg,newReg,paicpu(endP));
  783. { the replacing stops either at the moment that }
  784. { a) the newreg gets loaded with a new value (one not depending on the }
  785. { current value of newreg) }
  786. { b) newreg is completely replaced in this sequence and it's current value }
  787. { isn't used anymore }
  788. { In case b, the newreg was completely replaced by oldreg, so it's contents }
  789. { are unchanged compared the start of this sequence, so restore them }
  790. If removeLast or
  791. RegLoadedWithNewValue(newReg,true,endP) then
  792. GetLastInstruction(endP,hp)
  793. else hp := endP;
  794. if (p <> endp) or
  795. not RegLoadedWithNewValue(newReg,true,endP) then
  796. RestoreRegContentsTo(newReg, c ,p, hp);
  797. { In both case a and b, it is possible that the new register was modified }
  798. { (e.g. an add/sub), so if it was replaced by oldreg in that instruction, }
  799. { oldreg's contents have been changed. To take this into account, we simply }
  800. { set the contents of orgreg to "unknown" after this sequence }
  801. if newRegModified then
  802. ClearRegContentsFrom(orgReg,p,hp);
  803. if removeLast then
  804. ppaiprop(endP^.optinfo)^.canBeRemoved := true;
  805. end
  806. {$ifdef replaceregdebug}
  807. else
  808. begin
  809. hp := new(pai_asm_comment,init(strpnew(
  810. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  811. ' from here...')));
  812. hp^.previous := p^.previous;
  813. hp^.next := p;
  814. p^.previous := hp;
  815. if assigned(hp^.previous) then
  816. hp^.previous^.next := hp;
  817. hp := new(pai_asm_comment,init(strpnew(
  818. 'replacing '+att_reg2str[newreg]+' with '+att_reg2str[orgreg]+
  819. ' failed here')));
  820. hp^.next := endp^.next;
  821. hp^.previous := endp;
  822. endp^.next := hp;
  823. if assigned(hp^.next) then
  824. hp^.next^.previous := hp;
  825. end;
  826. {$endif replaceregdebug}
  827. End;
  828. {$endif replacereg}
  829. {$ifdef arithopt}
  830. Function FindRegWithConst(p: Pai; size: topsize; l: longint; Var Res: TRegister): Boolean;
  831. {Finds a register which contains the constant l}
  832. Var Counter: TRegister;
  833. {$ifdef testing}
  834. hp: pai;
  835. {$endif testing}
  836. tmpresult: boolean;
  837. Begin
  838. Counter := R_NO;
  839. repeat
  840. inc(counter);
  841. tmpresult := (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) and
  842. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.opsize = size) and
  843. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].typ = top_const) and
  844. (paicpu(PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod)^.oper[0].val = l);
  845. {$ifdef testing}
  846. if (PPaiProp(p^.OptInfo)^.Regs[Counter].Typ = Con_Const) then
  847. begin
  848. hp := new(pai_asm_comment,init(strpnew(
  849. 'checking const load of '+tostr(l)+' here...')));
  850. hp^.next := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod;
  851. hp^.previous := PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous;
  852. PPaiProp(p^.OptInfo)^.Regs[Counter].StartMod^.previous := hp;
  853. if assigned(hp^.previous) then
  854. hp^.previous^.next := hp;
  855. end;
  856. {$endif testing}
  857. until tmpresult or (Counter = R_EDI);
  858. res := counter;
  859. FindRegWithConst := tmpResult;
  860. End;
  861. {$endif arithopt}
  862. Procedure DoCSE(AsmL: PAasmOutput; First, Last: Pai);
  863. {marks the instructions that can be removed by RemoveInstructs. They're not
  864. removed immediately because sometimes an instruction needs to be checked in
  865. two different sequences}
  866. Var Cnt, Cnt2: Longint;
  867. p, hp1, hp2: Pai;
  868. hp3, hp4, hp5: pai;
  869. RegInfo: TRegInfo;
  870. RegCounter: TRegister;
  871. TmpState: Byte;
  872. Begin
  873. p := First;
  874. SkipHead(p);
  875. First := p;
  876. While (p <> Last) Do
  877. Begin
  878. Case p^.typ Of
  879. ait_align:
  880. if not(pai_align(p)^.use_op) then
  881. SetAlignReg(p);
  882. ait_instruction:
  883. Begin
  884. Case Paicpu(p)^.opcode Of
  885. A_CLD: If GetLastInstruction(p, hp1) And
  886. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_NotSet) Then
  887. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  888. A_MOV, A_MOVZX, A_MOVSX:
  889. Begin
  890. Case Paicpu(p)^.oper[0].typ Of
  891. Top_Ref:
  892. Begin {destination is always a register in this case}
  893. With PPaiProp(p^.OptInfo)^.Regs[Reg32(Paicpu(p)^.oper[1].reg)] Do
  894. Begin
  895. If (p = StartMod) And
  896. GetLastInstruction (p, hp1) And
  897. (hp1^.typ <> ait_marker)
  898. Then
  899. {so we don't try to check a sequence when p is the first instruction of the block}
  900. If CheckSequence(p, Paicpu(p)^.oper[1].reg, Cnt, RegInfo) And
  901. (Cnt > 0) Then
  902. Begin
  903. hp1 := nil;
  904. { although it's perfectly ok to remove an instruction which doesn't contain }
  905. { the register that we've just checked (CheckSequence takes care of that), }
  906. { the sequence containing this other register should also be completely }
  907. { checked and removed, otherwise we may get situations like this: }
  908. { }
  909. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  910. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  911. { movl 8(%edx), %edx movl 8(%edx), %edx }
  912. { movl (%eax), eax movl (%eax), eax }
  913. { cmpl %eax, %edx cmpl %eax, %edx }
  914. { jnz l123 getting converted to jnz l123 }
  915. { movl 12(%ebp), %edx movl 4(%eax), eax }
  916. { movl 16(%ebp), %eax }
  917. { movl 8(%edx), %edx }
  918. { movl 4(%eax), eax }
  919. hp2 := p;
  920. Cnt2 := 1;
  921. While Cnt2 <= Cnt Do
  922. Begin
  923. If (hp1 = nil) And
  924. Not(RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  925. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p)) And
  926. Not((p^.typ = ait_instruction) And
  927. (paicpu(p)^.OpCode = A_MOV) And
  928. (paicpu(p)^.Oper[0].typ = top_ref) And
  929. (PPaiProp(p^.OptInfo)^.Regs[Reg32(paicpu(p)^.Oper[1].reg)].NrOfMods
  930. <= (Cnt - Cnt2 + 1)))
  931. Then hp1 := p;
  932. {$ifndef noremove}
  933. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  934. {$endif noremove}
  935. Inc(Cnt2);
  936. GetNextInstruction(p, p);
  937. End;
  938. hp3 := New(Pai_Marker,Init(NoPropInfoStart));
  939. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  940. {hp4 is used to get the contents of the registers before the sequence}
  941. GetLastInstruction(hp2, hp4);
  942. {$IfDef CSDebug}
  943. For RegCounter := R_EAX To R_EDI Do
  944. If (RegCounter in RegInfo.RegsLoadedForRef) Then
  945. Begin
  946. hp5 := new(pai_asm_comment,init(strpnew('New: '+att_reg2str[RegCounter]+', Old: '+
  947. att_reg2str[RegInfo.New2OldReg[RegCounter]])));
  948. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  949. End;
  950. {$EndIf CSDebug}
  951. { If some registers were different in the old and the new sequence, move }
  952. { the contents of those old registers to the new ones }
  953. For RegCounter := R_EAX To R_EDI Do
  954. If Not(RegCounter in [R_ESP,procinfo^.framepointer]) And
  955. (RegInfo.New2OldReg[RegCounter] <> R_NO) Then
  956. Begin
  957. AllocRegBetween(AsmL,RegInfo.New2OldReg[RegCounter],
  958. PPaiProp(hp4^.OptInfo)^.Regs[RegInfo.New2OldReg[RegCounter]].StartMod,hp2);
  959. If Not(RegCounter In RegInfo.RegsLoadedForRef) And
  960. {old reg new reg}
  961. (RegInfo.New2OldReg[RegCounter] <> RegCounter) Then
  962. Begin
  963. {$ifdef replacereg}
  964. getLastInstruction(p,hp3);
  965. If not ReplaceReg(RegInfo.New2OldReg[RegCounter],
  966. regCounter,hp3,
  967. PPaiProp(hp4^.optInfo)^.Regs[regCounter],true,hp5) then
  968. begin
  969. {$endif replacereg}
  970. hp3 := New(Paicpu,Op_Reg_Reg(A_MOV, S_L,
  971. {old reg new reg}
  972. RegInfo.New2OldReg[RegCounter], RegCounter));
  973. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp3);
  974. {$ifdef replacereg}
  975. end
  976. {$endif replacereg}
  977. End
  978. Else
  979. { imagine the following code: }
  980. { normal wrong optimized }
  981. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  982. { movl (%eax), %eax movl (%eax), %eax }
  983. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  984. { jne l1 jne l1 }
  985. { movl 8(%ebp), %eax }
  986. { movl (%eax), %edi movl %eax, %edi }
  987. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  988. { movl 8(%ebp), %eax }
  989. { pushl 70(%eax) pushl 70(%eax) }
  990. { }
  991. { The error is that at the moment that the last instruction is executed, }
  992. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  993. { registers that are completely removed from a sequence (= registers in }
  994. { RegLoadedForRef, have to be changed to their contents from before the }
  995. { sequence. }
  996. If RegCounter in RegInfo.RegsLoadedForRef Then
  997. Begin
  998. {load Cnt2 with the total number of instructions of this sequence}
  999. Cnt2 := PPaiProp(hp4^.OptInfo)^.
  1000. Regs[RegInfo.New2OldReg[RegCounter]].NrOfMods;
  1001. hp3 := hp2;
  1002. For Cnt := 1 to Pred(Cnt2) Do
  1003. GetNextInstruction(hp3, hp3);
  1004. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  1005. GetNextInstruction(hp3, hp3);
  1006. {$ifdef csdebug}
  1007. Writeln('Cnt2: ',Cnt2);
  1008. hp5 := new(pai_asm_comment,init(strpnew('starting here...')));
  1009. InsertLLItem(AsmL, Pai(hp2^.previous), hp2, hp5);
  1010. {$endif csdebug}
  1011. hp3 := hp2;
  1012. {first change the contents of the register inside the sequence}
  1013. For Cnt := 1 to Cnt2 Do
  1014. Begin
  1015. {save the WState of the last pai object of the sequence for later use}
  1016. TmpState := PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState;
  1017. {$ifdef csdebug}
  1018. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '
  1019. +tostr(tmpstate))));
  1020. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  1021. {$endif csdebug}
  1022. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  1023. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  1024. GetNextInstruction(hp3, hp3);
  1025. End;
  1026. {here, hp3 = p = Pai object right after the sequence, TmpState = WState of
  1027. RegCounter at the last Pai object of the sequence}
  1028. GetLastInstruction(hp3, hp3);
  1029. While GetNextInstruction(hp3, hp3) And
  1030. (PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState
  1031. = TmpState) Do
  1032. {$ifdef csdebug}
  1033. begin
  1034. hp5 := new(pai_asm_comment,init(strpnew('WState for '+att_reg2str[Regcounter]+': '+
  1035. tostr(PPaiProp(hp3^.OptInfo)^.Regs[RegCounter].WState))));
  1036. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  1037. {$endif csdebug}
  1038. PPaiProp(hp3^.OptInfo)^.Regs[RegCounter] :=
  1039. PPaiProp(hp4^.OptInfo)^.Regs[RegCounter];
  1040. {$ifdef csdebug}
  1041. end;
  1042. {$endif csdebug}
  1043. {$ifdef csdebug}
  1044. hp5 := new(pai_asm_comment,init(strpnew('stopping here...')));
  1045. InsertLLItem(AsmL, hp3, pai(hp3^.next), hp5);
  1046. {$endif csdebug}
  1047. End;
  1048. End;
  1049. hp3 := New(Pai_Marker,Init(NoPropInfoEnd));
  1050. InsertLLItem(AsmL, Pai(hp2^.Previous), hp2, hp3);
  1051. If hp1 <> nil Then p := hp1;
  1052. Continue;
  1053. End
  1054. Else
  1055. If (Cnt > 0) And
  1056. (PPaiProp(p^.OptInfo)^.
  1057. Regs[Reg32(Paicpu(p)^.oper[1].reg)].Typ = Con_Ref) And
  1058. (PPaiProp(p^.OptInfo)^.CanBeRemoved) Then
  1059. Begin
  1060. hp2 := p;
  1061. Cnt2 := 1;
  1062. While Cnt2 <= Cnt Do
  1063. Begin
  1064. If RegInInstruction(Paicpu(hp2)^.oper[1].reg, p) Or
  1065. RegInInstruction(Reg32(Paicpu(hp2)^.oper[1].reg), p) Then
  1066. PPaiProp(p^.OptInfo)^.CanBeRemoved := False;
  1067. Inc(Cnt2);
  1068. GetNextInstruction(p, p);
  1069. End;
  1070. Continue;
  1071. End;
  1072. End;
  1073. End;
  1074. {$ifdef replacereg}
  1075. top_Reg:
  1076. { try to replace the new reg with the old reg }
  1077. if (paicpu(p)^.opcode = A_MOV) and
  1078. getLastInstruction(p,hp4) then
  1079. begin
  1080. Case paicpu(p)^.oper[1].typ of
  1081. top_Reg:
  1082. { we only have to start replacing from the instruction after the mov, }
  1083. { but replacereg only starts with getnextinstruction(p,p) }
  1084. if ReplaceReg(paicpu(p)^.oper[0].reg,
  1085. paicpu(p)^.oper[1].reg,p,
  1086. PPaiProp(hp4^.optInfo)^.Regs[paicpu(p)^.oper[1].reg],false,hp1) then
  1087. begin
  1088. PPaiProp(p^.optInfo)^.canBeRemoved := true;
  1089. allocRegBetween(asmL,paicpu(p)^.oper[0].reg,
  1090. PPaiProp(p^.optInfo)^.regs[paicpu(p)^.oper[0].reg].startMod,
  1091. hp1);
  1092. end;
  1093. end
  1094. end;
  1095. {$endif replacereg}
  1096. top_symbol,Top_Const:
  1097. Begin
  1098. Case Paicpu(p)^.oper[1].typ Of
  1099. Top_Reg:
  1100. Begin
  1101. regCounter := Reg32(Paicpu(p)^.oper[1].reg);
  1102. If GetLastInstruction(p, hp1) Then
  1103. With PPaiProp(hp1^.OptInfo)^.Regs[regCounter] Do
  1104. If (Typ = Con_Const) And
  1105. (paicpu(startMod)^.opsize >= paicpu(p)^.opsize) and
  1106. opsequal(paicpu(StartMod)^.oper[0],paicpu(p)^.oper[0]) Then
  1107. begin
  1108. PPaiProp(p^.OptInfo)^.CanBeRemoved := True;
  1109. allocRegBetween(asmL,regCounter,startMod,p);
  1110. end;
  1111. End;
  1112. {$ifdef arithopt}
  1113. Top_Ref:
  1114. if (paicpu(p)^.oper[0].typ = top_const) and
  1115. getLastInstruction(p,hp1) and
  1116. findRegWithConst(hp1,paicpu(p)^.opsize,paicpu(p)^.oper[0].val,regCounter) then
  1117. begin
  1118. paicpu(p)^.loadreg(0,regCounter);
  1119. allocRegBetween(AsmL,reg32(regCounter),
  1120. PPaiProp(hp1^.optinfo)^.regs[regCounter].startMod,p);
  1121. end;
  1122. {$endif arithopt}
  1123. End;
  1124. End;
  1125. End;
  1126. End;
  1127. A_STD: If GetLastInstruction(p, hp1) And
  1128. (PPaiProp(hp1^.OptInfo)^.DirFlag = F_Set) Then
  1129. PPaiProp(Pai(p)^.OptInfo)^.CanBeRemoved := True;
  1130. End
  1131. End;
  1132. End;
  1133. GetNextInstruction(p, p);
  1134. End;
  1135. End;
  1136. Procedure RemoveInstructs(AsmL: PAasmOutput; First, Last: Pai);
  1137. { Removes the marked instructions and disposes the PPaiProps of the other }
  1138. { instructions }
  1139. Var p, hp1: Pai;
  1140. begin
  1141. p := First;
  1142. While (p <> Last) Do
  1143. Begin
  1144. If (p^.typ = ait_marker) and
  1145. (pai_marker(p)^.kind in [noPropInfoStart,noPropInfoEnd]) then
  1146. begin
  1147. hp1 := pai(p^.next);
  1148. asmL^.remove(p);
  1149. dispose(p,done);
  1150. p := hp1
  1151. end
  1152. else
  1153. {$ifndef noinstremove}
  1154. if assigned(p^.optInfo) and
  1155. PPaiProp(p^.optInfo)^.canBeRemoved then
  1156. begin
  1157. {$IfDef TP}
  1158. Dispose(PPaiProp(p^.OptInfo));
  1159. {$EndIf}
  1160. hp1 := pai(p^.next);
  1161. AsmL^.Remove(p);
  1162. Dispose(p, Done);
  1163. p := hp1;
  1164. End
  1165. Else
  1166. {$endif noinstremove}
  1167. Begin
  1168. {$IfDef TP}
  1169. Dispose(PPaiProp(p^.OptInfo));
  1170. {$EndIf TP}
  1171. p^.OptInfo := nil;
  1172. p := pai(p^.next);;
  1173. End;
  1174. End;
  1175. {$IfNDef TP}
  1176. FreeMem(PaiPropBlock, NrOfPaiObjs*(((SizeOf(TPaiProp)+3)div 4)*4))
  1177. {$EndIf TP}
  1178. End;
  1179. Procedure CSE(AsmL: PAasmOutput; First, Last: Pai);
  1180. Begin
  1181. DoCSE(AsmL, First, Last);
  1182. RemoveInstructs(AsmL, First, Last);
  1183. End;
  1184. End.
  1185. {
  1186. $Log$
  1187. Revision 1.51 2000-02-12 19:28:56 jonas
  1188. * fix for imul optimization in popt386 (exclude top_ref as first
  1189. argument)
  1190. * in csopt386: change "mov reg1,reg2; <several operations on reg2>;
  1191. mov reg2,reg1" to "<several operations on reg1>" (-dnewopt...)
  1192. Revision 1.50 2000/02/12 14:10:14 jonas
  1193. + change "mov reg1,reg2;imul x,reg2" to "imul x,reg1,reg2" in popt386
  1194. (-dnewoptimizations)
  1195. * shl(d) and shr(d) are considered to have a hardcoded register if
  1196. they use cl as shift count (since you can't replace them with
  1197. another register) in csopt386 (also for -dnewoptimizations)
  1198. Revision 1.49 2000/02/12 10:54:18 jonas
  1199. * fixed edi allocation in allocRegBetween
  1200. * fixed bug I introduced yesterday, added comment to prevent it from
  1201. happening again in the future
  1202. Revision 1.48 2000/02/11 23:50:03 jonas
  1203. * fixed crashing bug under Dos with -dnewoptimizations (found it,
  1204. John!). Don't understand why it didn't crash under Linux :(
  1205. Revision 1.47 2000/02/10 16:04:43 jonas
  1206. * fixed stupid typo!
  1207. Revision 1.46 2000/02/10 15:07:41 jonas
  1208. * fixed small bug introduced with my previous fix
  1209. Revision 1.45 2000/02/10 14:57:13 jonas
  1210. * fixed bug due to lack of support for top_symbol operands
  1211. Revision 1.44 2000/02/09 13:22:51 peter
  1212. * log truncated
  1213. Revision 1.43 2000/02/04 13:52:17 jonas
  1214. * better support for regvars (still needs a move of the call to the optimize
  1215. procedure to a place where resetusableregisters is not yet called to work)
  1216. * small regallocation fixes for -dnewoptimizations
  1217. Revision 1.42 2000/01/28 15:15:31 jonas
  1218. * moved skipinstr from daopt386 to aasm
  1219. * fixed crashing bug with -dreplacereg in csopt386.pas
  1220. Revision 1.41 2000/01/23 11:11:37 michael
  1221. + Fixes from Jonas.
  1222. Revision 1.40 2000/01/22 16:10:06 jonas
  1223. + all code generator generated "mov reg1,reg2" instructions are now
  1224. attempted to be removed using the replacereg code
  1225. (-dnewoptimizations)
  1226. * small fixes to -dreplacereg code
  1227. Revision 1.39 2000/01/13 13:07:05 jonas
  1228. * released -dalignreg
  1229. * some small fixes to -dnewOptimizations helper procedures
  1230. Revision 1.38 2000/01/07 01:14:23 peter
  1231. * updated copyright to 2000
  1232. Revision 1.37 2000/01/03 17:11:17 jonas
  1233. * fixed bug with -dreplacereg
  1234. Revision 1.36 1999/12/05 16:48:43 jonas
  1235. * CSE of constant loading in regs works properly again
  1236. + if a constant is stored into memory using "mov const, ref" and
  1237. there is a reg that contains this const, it is changed into
  1238. "mov reg, ref"
  1239. Revision 1.35 1999/12/02 11:26:41 peter
  1240. * newoptimizations define added
  1241. Revision 1.34 1999/11/21 13:09:41 jonas
  1242. * fixed some missed optimizations because 8bit regs were not always
  1243. taken into account
  1244. Revision 1.33 1999/11/20 11:37:03 jonas
  1245. * make cycle works with -dreplacereg (register renaming)! I have not
  1246. tested it yet together with -darithopt, but I don't expect problems
  1247. Revision 1.32 1999/11/14 11:26:53 jonas
  1248. + basic register renaming (not yet working completely, between
  1249. -dreplacereg/-dreplaceregdebug)
  1250. Revision 1.31 1999/11/06 16:21:57 jonas
  1251. + search optimial register to use in alignment code (compile with
  1252. -dalignreg, -dalignregdebug to see chosen register in
  1253. assembler code). Still needs support in ag386bin.
  1254. Revision 1.30 1999/11/06 14:34:20 peter
  1255. * truncated log to 20 revs
  1256. Revision 1.29 1999/11/05 16:01:46 jonas
  1257. + first implementation of choosing least used register for alignment code
  1258. (not yet working, between ifdef alignreg)
  1259. Revision 1.28 1999/10/11 11:11:31 jonas
  1260. * fixed bug which sometimes caused a crash when optimizing blocks of code with
  1261. assembler blocks (didn't notice before because of lack of zero page protection
  1262. under Win9x :( )
  1263. Revision 1.27 1999/10/01 13:51:40 jonas
  1264. * CSE now updates the RegAlloc's
  1265. Revision 1.26 1999/09/30 14:43:13 jonas
  1266. * fixed small efficiency which caused some missed optimizations (saves 1
  1267. assembler instruction on the whole compiler/RTL source tree! :)
  1268. Revision 1.25 1999/09/27 23:44:50 peter
  1269. * procinfo is now a pointer
  1270. * support for result setting in sub procedure
  1271. Revision 1.24 1999/08/25 11:59:58 jonas
  1272. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1273. }