csopt386.pas 50 KB

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